rx6110sa: Add possibility to use both I2C and SMBus for the RTC

The driver for the RTC RX6110SA is designed to be used with I2C bus.
This patch adds the possibility to use SMBus operations to access the
RTC. For this purpose the Kconfig switch RX6110SA_USE_SMBUS is added. It
is not enabled per default so that I2C will be used. One can set this
switch on board level to use SMBus instead.

Change-Id: I4827ae2c544e8002399d94a1159acacd8176c5e9
Signed-off-by: Werner Zeh <werner.zeh@siemens.com>
Reviewed-on: https://review.coreboot.org/19978
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Werner Zeh 2017-05-29 14:28:15 +02:00
parent 07a597feff
commit 57cbd21a52
2 changed files with 60 additions and 37 deletions

View File

@ -3,3 +3,10 @@ config DRIVERS_I2C_RX6110SA
default n default n
help help
Enable support for external RTC chip RX6110 SA. Enable support for external RTC chip RX6110 SA.
config RX6110SA_USE_SMBUS
bool
default n
help
Select this option if the RTC RX6110SA is connected to a SMBus controller.
If not selected I2C controller is used instead.

View File

@ -14,6 +14,7 @@
*/ */
#include <device/i2c.h> #include <device/i2c.h>
#include <device/smbus.h>
#include <device/device.h> #include <device/device.h>
#include <version.h> #include <version.h>
#include <console/console.h> #include <console/console.h>
@ -24,17 +25,34 @@
#define I2C_BUS_NUM (dev->bus->secondary - 1) #define I2C_BUS_NUM (dev->bus->secondary - 1)
#define I2C_DEV_NUM (dev->path.i2c.device) #define I2C_DEV_NUM (dev->path.i2c.device)
/* Function to write a register in the RTC with the given value. */
static void rx6110sa_write(struct device *dev, uint8_t reg, uint8_t val)
{
if (IS_ENABLED(CONFIG_RX6110SA_USE_SMBUS))
smbus_write_byte(dev, reg, val);
else
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, reg, val);
}
/* Function to read a register in the RTC. */
static uint8_t rx6110sa_read(struct device *dev, uint8_t reg)
{
uint8_t val = 0;
if (IS_ENABLED(CONFIG_RX6110SA_USE_SMBUS))
val = smbus_read_byte(dev, reg);
else
i2c_readb(I2C_BUS_NUM, I2C_DEV_NUM, reg, &val);
return val;
}
/* Set RTC date from coreboot build date. */ /* Set RTC date from coreboot build date. */
static void rx6110sa_set_build_date(struct device *dev) static void rx6110sa_set_build_date(struct device *dev)
{ {
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, YEAR_REG, rx6110sa_write(dev, YEAR_REG, coreboot_build_date.year);
coreboot_build_date.year); rx6110sa_write(dev, MONTH_REG, coreboot_build_date.month);
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, MONTH_REG, rx6110sa_write(dev, DAY_REG, coreboot_build_date.day);
coreboot_build_date.month); rx6110sa_write(dev, WEEK_REG, (1 << coreboot_build_date.weekday));
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, DAY_REG,
coreboot_build_date.day);
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, WEEK_REG,
(1 << coreboot_build_date.weekday));
} }
/* Set RTC date from user defined date (available in e.g. device tree). */ /* Set RTC date from user defined date (available in e.g. device tree). */
@ -42,14 +60,10 @@ static void rx6110sa_set_user_date(struct device *dev)
{ {
struct drivers_i2c_rx6110sa_config *config = dev->chip_info; struct drivers_i2c_rx6110sa_config *config = dev->chip_info;
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, YEAR_REG, rx6110sa_write(dev, YEAR_REG, bin2bcd(config->user_year));
bin2bcd(config->user_year)); rx6110sa_write(dev, MONTH_REG, bin2bcd(config->user_month));
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, MONTH_REG, rx6110sa_write(dev, DAY_REG, bin2bcd(config->user_day));
bin2bcd(config->user_month)); rx6110sa_write(dev, WEEK_REG, (1 << config->user_weekday));
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, DAY_REG,
bin2bcd(config->user_day));
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, WEEK_REG,
(1 << config->user_weekday));
} }
static void rx6110sa_final(struct device *dev) static void rx6110sa_final(struct device *dev)
@ -57,12 +71,12 @@ static void rx6110sa_final(struct device *dev)
uint8_t hour, minute, second, year, month, day; uint8_t hour, minute, second, year, month, day;
/* Read back current RTC date and time and print it to the console. */ /* Read back current RTC date and time and print it to the console. */
i2c_readb(I2C_BUS_NUM, I2C_DEV_NUM, HOUR_REG, &hour); hour = rx6110sa_read(dev, HOUR_REG);
i2c_readb(I2C_BUS_NUM, I2C_DEV_NUM, MINUTE_REG, &minute); minute = rx6110sa_read(dev, MINUTE_REG);
i2c_readb(I2C_BUS_NUM, I2C_DEV_NUM, SECOND_REG, &second); second = rx6110sa_read(dev, SECOND_REG);
i2c_readb(I2C_BUS_NUM, I2C_DEV_NUM, YEAR_REG, &year); year = rx6110sa_read(dev, YEAR_REG);
i2c_readb(I2C_BUS_NUM, I2C_DEV_NUM, MONTH_REG, &month); month = rx6110sa_read(dev, MONTH_REG);
i2c_readb(I2C_BUS_NUM, I2C_DEV_NUM, DAY_REG, &day); day = rx6110sa_read(dev, DAY_REG);
printk(BIOS_INFO, "%s: Current date %02d.%02d.%02d %02d:%02d:%02d\n", printk(BIOS_INFO, "%s: Current date %02d.%02d.%02d %02d:%02d:%02d\n",
dev->chip_ops->name, bcd2bin(month), bcd2bin(day), dev->chip_ops->name, bcd2bin(month), bcd2bin(day),
@ -75,13 +89,13 @@ static void rx6110sa_init(struct device *dev)
uint8_t reg; uint8_t reg;
/* Do a dummy read first. */ /* Do a dummy read first. */
i2c_readb(I2C_BUS_NUM, I2C_DEV_NUM, SECOND_REG, &reg); reg = rx6110sa_read(dev, SECOND_REG);
/* /*
* Check VLF-bit which indicates the RTC data loss, such as due to a * Check VLF-bit which indicates the RTC data loss, such as due to a
* supply voltage drop. * supply voltage drop.
*/ */
i2c_readb(I2C_BUS_NUM, I2C_DEV_NUM, FLAG_REGISTER, &reg); reg = rx6110sa_read(dev, FLAG_REGISTER);
if (!(reg & VLF_BIT)) if (!(reg & VLF_BIT))
/* No voltage low detected, everything is well. */ /* No voltage low detected, everything is well. */
@ -91,24 +105,26 @@ static void rx6110sa_init(struct device *dev)
* Voltage low detected, initialize RX6110 SA again. * Voltage low detected, initialize RX6110 SA again.
* Set first some registers to known state. * Set first some registers to known state.
*/ */
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, BATTERY_BACKUP_REG, 0x00); rx6110sa_write(dev, BATTERY_BACKUP_REG, 0x00);
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, RESERVED_BIT_REG, RTC_INIT_VALUE); rx6110sa_write(dev, RESERVED_BIT_REG, RTC_INIT_VALUE);
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, DIGITAL_REG, 0x00); rx6110sa_write(dev, DIGITAL_REG, 0x00);
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, IRQ_CONTROL_REG, 0x00); rx6110sa_write(dev, IRQ_CONTROL_REG, 0x00);
/* Clear timer enable bit and set frequency of clock output. */ /* Clear timer enable bit and set frequency of clock output. */
i2c_readb(I2C_BUS_NUM, I2C_DEV_NUM, EXTENSION_REG, &reg);
reg = rx6110sa_read(dev, EXTENSION_REG);
reg &= ~(FSEL_MASK | TE_BIT); reg &= ~(FSEL_MASK | TE_BIT);
reg |= (config->cof_selection << 6); reg |= (config->cof_selection << 6);
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, EXTENSION_REG, reg); rx6110sa_write(dev, EXTENSION_REG, reg);
/* Clear voltage low detect bit. */ /* Clear voltage low detect bit. */
i2c_readb(I2C_BUS_NUM, I2C_DEV_NUM, FLAG_REGISTER, &reg); reg = rx6110sa_read(dev, FLAG_REGISTER);
reg &= ~VLF_BIT; reg &= ~VLF_BIT;
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, FLAG_REGISTER, reg); rx6110sa_write(dev, FLAG_REGISTER, reg);
/* Before setting the clock stop oscillator. */ /* Before setting the clock stop oscillator. */
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, CTRL_REG, STOP_BIT); rx6110sa_write(dev, CTRL_REG, STOP_BIT);
if (config->set_user_date) { if (config->set_user_date) {
/* Set user date defined in device tree. */ /* Set user date defined in device tree. */
printk(BIOS_DEBUG, "%s: Set to user date\n", printk(BIOS_DEBUG, "%s: Set to user date\n",
@ -120,11 +136,11 @@ static void rx6110sa_init(struct device *dev)
dev->chip_ops->name); dev->chip_ops->name);
rx6110sa_set_build_date(dev); rx6110sa_set_build_date(dev);
} }
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, HOUR_REG, 1); rx6110sa_write(dev, HOUR_REG, 1);
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, MINUTE_REG, 0); rx6110sa_write(dev, MINUTE_REG, 0);
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, SECOND_REG, 0); rx6110sa_write(dev, SECOND_REG, 0);
/* Start oscillator again as the RTC is set up now. */ /* Start oscillator again as the RTC is set up now. */
i2c_writeb(I2C_BUS_NUM, I2C_DEV_NUM, CTRL_REG, 0x00); rx6110sa_write(dev, CTRL_REG, 0x00);
} }
static struct device_operations rx6110sa_ops = { static struct device_operations rx6110sa_ops = {