blob: 9d60bdb7e1030378f70e815b0339728ffb30b018 [file] [log] [blame]
Mario Scheithauer7fd20be2017-05-08 16:09:26 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2017 Siemens AG.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <device/i2c.h>
Werner Zeh57cbd212017-05-29 14:28:15 +020017#include <device/smbus.h>
Mario Scheithauer7fd20be2017-05-08 16:09:26 +020018#include <device/device.h>
19#include <version.h>
20#include <console/console.h>
21#include <bcd.h>
22#include "chip.h"
23#include "rx6110sa.h"
24
25#define I2C_BUS_NUM (dev->bus->secondary - 1)
26#define I2C_DEV_NUM (dev->path.i2c.device)
27
Werner Zeh57cbd212017-05-29 14:28:15 +020028/* Function to write a register in the RTC with the given value. */
29static void rx6110sa_write(struct device *dev, uint8_t reg, uint8_t val)
30{
31 if (IS_ENABLED(CONFIG_RX6110SA_USE_SMBUS))
32 smbus_write_byte(dev, reg, val);
33 else
34 i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, reg, val);
35}
36
37/* Function to read a register in the RTC. */
38static uint8_t rx6110sa_read(struct device *dev, uint8_t reg)
39{
40 uint8_t val = 0;
41
42 if (IS_ENABLED(CONFIG_RX6110SA_USE_SMBUS))
43 val = smbus_read_byte(dev, reg);
44 else
45 i2c_readb(I2C_BUS_NUM, I2C_DEV_NUM, reg, &val);
46 return val;
47}
48
Mario Scheithauer7fd20be2017-05-08 16:09:26 +020049/* Set RTC date from coreboot build date. */
50static void rx6110sa_set_build_date(struct device *dev)
51{
Werner Zeh57cbd212017-05-29 14:28:15 +020052 rx6110sa_write(dev, YEAR_REG, coreboot_build_date.year);
53 rx6110sa_write(dev, MONTH_REG, coreboot_build_date.month);
54 rx6110sa_write(dev, DAY_REG, coreboot_build_date.day);
55 rx6110sa_write(dev, WEEK_REG, (1 << coreboot_build_date.weekday));
Mario Scheithauer7fd20be2017-05-08 16:09:26 +020056}
57
58/* Set RTC date from user defined date (available in e.g. device tree). */
59static void rx6110sa_set_user_date(struct device *dev)
60{
61 struct drivers_i2c_rx6110sa_config *config = dev->chip_info;
62
Werner Zeh57cbd212017-05-29 14:28:15 +020063 rx6110sa_write(dev, YEAR_REG, bin2bcd(config->user_year));
64 rx6110sa_write(dev, MONTH_REG, bin2bcd(config->user_month));
65 rx6110sa_write(dev, DAY_REG, bin2bcd(config->user_day));
66 rx6110sa_write(dev, WEEK_REG, (1 << config->user_weekday));
Mario Scheithauer7fd20be2017-05-08 16:09:26 +020067}
68
69static void rx6110sa_final(struct device *dev)
70{
71 uint8_t hour, minute, second, year, month, day;
72
73 /* Read back current RTC date and time and print it to the console. */
Werner Zeh57cbd212017-05-29 14:28:15 +020074 hour = rx6110sa_read(dev, HOUR_REG);
75 minute = rx6110sa_read(dev, MINUTE_REG);
76 second = rx6110sa_read(dev, SECOND_REG);
77 year = rx6110sa_read(dev, YEAR_REG);
78 month = rx6110sa_read(dev, MONTH_REG);
79 day = rx6110sa_read(dev, DAY_REG);
Mario Scheithauer7fd20be2017-05-08 16:09:26 +020080
81 printk(BIOS_INFO, "%s: Current date %02d.%02d.%02d %02d:%02d:%02d\n",
82 dev->chip_ops->name, bcd2bin(month), bcd2bin(day),
83 bcd2bin(year), bcd2bin(hour), bcd2bin(minute), bcd2bin(second));
84}
85
86static void rx6110sa_init(struct device *dev)
87{
88 struct drivers_i2c_rx6110sa_config *config = dev->chip_info;
89 uint8_t reg;
90
91 /* Do a dummy read first. */
Werner Zeh57cbd212017-05-29 14:28:15 +020092 reg = rx6110sa_read(dev, SECOND_REG);
Mario Scheithauer7fd20be2017-05-08 16:09:26 +020093
94 /*
95 * Check VLF-bit which indicates the RTC data loss, such as due to a
96 * supply voltage drop.
97 */
Werner Zeh57cbd212017-05-29 14:28:15 +020098 reg = rx6110sa_read(dev, FLAG_REGISTER);
Mario Scheithauer7fd20be2017-05-08 16:09:26 +020099
100 if (!(reg & VLF_BIT))
101 /* No voltage low detected, everything is well. */
102 return;
103
104 /*
105 * Voltage low detected, initialize RX6110 SA again.
106 * Set first some registers to known state.
107 */
Werner Zeh57cbd212017-05-29 14:28:15 +0200108 rx6110sa_write(dev, BATTERY_BACKUP_REG, 0x00);
109 rx6110sa_write(dev, RESERVED_BIT_REG, RTC_INIT_VALUE);
110 rx6110sa_write(dev, DIGITAL_REG, 0x00);
111 rx6110sa_write(dev, IRQ_CONTROL_REG, 0x00);
Mario Scheithauer7fd20be2017-05-08 16:09:26 +0200112
113 /* Clear timer enable bit and set frequency of clock output. */
Werner Zeh57cbd212017-05-29 14:28:15 +0200114
115 reg = rx6110sa_read(dev, EXTENSION_REG);
Mario Scheithauer7fd20be2017-05-08 16:09:26 +0200116 reg &= ~(FSEL_MASK | TE_BIT);
117 reg |= (config->cof_selection << 6);
Werner Zeh57cbd212017-05-29 14:28:15 +0200118 rx6110sa_write(dev, EXTENSION_REG, reg);
Mario Scheithauer7fd20be2017-05-08 16:09:26 +0200119
120 /* Clear voltage low detect bit. */
Werner Zeh57cbd212017-05-29 14:28:15 +0200121 reg = rx6110sa_read(dev, FLAG_REGISTER);
Mario Scheithauer7fd20be2017-05-08 16:09:26 +0200122 reg &= ~VLF_BIT;
Werner Zeh57cbd212017-05-29 14:28:15 +0200123 rx6110sa_write(dev, FLAG_REGISTER, reg);
Mario Scheithauer7fd20be2017-05-08 16:09:26 +0200124
125 /* Before setting the clock stop oscillator. */
Werner Zeh57cbd212017-05-29 14:28:15 +0200126 rx6110sa_write(dev, CTRL_REG, STOP_BIT);
127
Mario Scheithauer7fd20be2017-05-08 16:09:26 +0200128 if (config->set_user_date) {
129 /* Set user date defined in device tree. */
130 printk(BIOS_DEBUG, "%s: Set to user date\n",
131 dev->chip_ops->name);
132 rx6110sa_set_user_date(dev);
133 } else {
134 /* Set date from coreboot build. */
135 printk(BIOS_DEBUG, "%s: Set to coreboot build date\n",
136 dev->chip_ops->name);
137 rx6110sa_set_build_date(dev);
138 }
Werner Zeh57cbd212017-05-29 14:28:15 +0200139 rx6110sa_write(dev, HOUR_REG, 1);
140 rx6110sa_write(dev, MINUTE_REG, 0);
141 rx6110sa_write(dev, SECOND_REG, 0);
Mario Scheithauer7fd20be2017-05-08 16:09:26 +0200142 /* Start oscillator again as the RTC is set up now. */
Werner Zeh57cbd212017-05-29 14:28:15 +0200143 rx6110sa_write(dev, CTRL_REG, 0x00);
Mario Scheithauer7fd20be2017-05-08 16:09:26 +0200144}
145
146static struct device_operations rx6110sa_ops = {
147 .read_resources = DEVICE_NOOP,
148 .set_resources = DEVICE_NOOP,
149 .enable_resources = DEVICE_NOOP,
150 .init = rx6110sa_init,
151 .final = rx6110sa_final
152};
153
154static void rx6110sa_enable(struct device *dev)
155{
156 dev->ops = &rx6110sa_ops;
157}
158
159struct chip_operations drivers_i2c_rx6110sa_ops = {
160 CHIP_NAME("RX6110 SA")
161 .enable_dev = rx6110sa_enable
162};