Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2014 Rockchip Inc. |
| 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 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <assert.h> |
David Hendricks | 3c4951e | 2015-01-12 14:08:10 -0800 | [diff] [blame] | 21 | #include <bcd.h> |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 22 | #include <console/console.h> |
Julius Werner | 7a453eb | 2014-10-20 13:14:55 -0700 | [diff] [blame] | 23 | #include <delay.h> |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 24 | #include <device/i2c.h> |
David Hendricks | 3c4951e | 2015-01-12 14:08:10 -0800 | [diff] [blame] | 25 | #include <rtc.h> |
Julius Werner | 7a453eb | 2014-10-20 13:14:55 -0700 | [diff] [blame] | 26 | #include <soc/rk808.h> |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | #include <stdlib.h> |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 29 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 30 | #if CONFIG_PMIC_BUS < 0 |
| 31 | #error "PMIC_BUS must be set in mainboard's Kconfig." |
| 32 | #endif |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 33 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 34 | #define RK808_ADDR 0x1b |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 35 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 36 | #define DCDC_EN 0x23 |
| 37 | #define LDO_EN 0x24 |
| 38 | #define BUCK1SEL 0x2f |
| 39 | #define BUCK4SEL 0x38 |
| 40 | #define LDO_ONSEL(i) (0x39 + 2 * i) |
| 41 | #define LDO_SLPSEL(i) (0x3a + 2 * i) |
| 42 | |
David Hendricks | 3c4951e | 2015-01-12 14:08:10 -0800 | [diff] [blame] | 43 | #define RTC_SECOND 0x00 |
| 44 | #define RTC_MINUTE 0x01 |
| 45 | #define RTC_HOUR 0x02 |
| 46 | #define RTC_DAY 0x03 |
| 47 | #define RTC_MONTH 0x04 |
| 48 | #define RTC_YEAR 0x05 |
| 49 | #define RTC_WEEKS 0x06 |
| 50 | #define RTC_CTRL 0x10 |
| 51 | #define RTC_STATUS 0x11 |
| 52 | |
| 53 | #define RTC_CTRL_STOP_RTC (1 << 0) |
| 54 | #define RTC_CTRL_GET_TIME (1 << 6) |
| 55 | #define RTC_CTRL_RTC_READSEL (1 << 7) |
| 56 | |
huang lin | 3704e69 | 2015-02-27 19:35:04 -0800 | [diff] [blame^] | 57 | #define DCDC_UV_ACT 0x28 |
huang lin | 75f431a | 2015-01-23 14:48:42 +0800 | [diff] [blame] | 58 | #define DCDC_ILMAX 0x90 |
| 59 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 60 | static int rk808_read(uint8_t reg, uint8_t *value) |
| 61 | { |
| 62 | return i2c_readb(CONFIG_PMIC_BUS, RK808_ADDR, reg, value); |
| 63 | } |
| 64 | |
| 65 | static int rk808_write(uint8_t reg, uint8_t value) |
| 66 | { |
| 67 | return i2c_writeb(CONFIG_PMIC_BUS, RK808_ADDR, reg, value); |
| 68 | } |
| 69 | |
| 70 | static void rk808_clrsetbits(uint8_t reg, uint8_t clr, uint8_t set) |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 71 | { |
| 72 | uint8_t value; |
| 73 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 74 | if (rk808_read(reg, &value) || rk808_write(reg, (value & ~clr) | set)) |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 75 | printk(BIOS_ERR, "ERROR: Cannot set Rk808[%#x]!\n", reg); |
| 76 | } |
| 77 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 78 | void rk808_configure_switch(int sw, int enabled) |
Julius Werner | 8f3883d | 2014-09-26 21:01:08 -0700 | [diff] [blame] | 79 | { |
| 80 | assert(sw == 1 || sw == 2); |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 81 | rk808_clrsetbits(DCDC_EN, 1 << (sw + 4), !!enabled << (sw + 4)); |
Julius Werner | 8f3883d | 2014-09-26 21:01:08 -0700 | [diff] [blame] | 82 | } |
| 83 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 84 | void rk808_configure_ldo(int ldo, int millivolts) |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 85 | { |
| 86 | uint8_t vsel; |
| 87 | |
Julius Werner | dbfa9d5 | 2014-12-05 17:29:42 -0800 | [diff] [blame] | 88 | if (!millivolts) { |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 89 | rk808_clrsetbits(LDO_EN, 1 << (ldo - 1), 0); |
Julius Werner | dbfa9d5 | 2014-12-05 17:29:42 -0800 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 93 | switch (ldo) { |
| 94 | case 1: |
| 95 | case 2: |
| 96 | case 4: |
| 97 | case 5: |
| 98 | case 8: |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 99 | vsel = div_round_up(millivolts, 100) - 18; |
Julius Werner | ef639b2 | 2014-11-06 14:33:12 -0800 | [diff] [blame] | 100 | assert(vsel <= 0x10); |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 101 | break; |
| 102 | case 3: |
| 103 | case 6: |
| 104 | case 7: |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 105 | vsel = div_round_up(millivolts, 100) - 8; |
Julius Werner | ef639b2 | 2014-11-06 14:33:12 -0800 | [diff] [blame] | 106 | assert(vsel <= 0x11); |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 107 | break; |
| 108 | default: |
| 109 | die("Unknown LDO index!"); |
| 110 | } |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 111 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 112 | rk808_clrsetbits(LDO_ONSEL(ldo), 0x1f, vsel); |
| 113 | rk808_clrsetbits(LDO_EN, 0, 1 << (ldo - 1)); |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 114 | } |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 115 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 116 | void rk808_configure_buck(int buck, int millivolts) |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 117 | { |
| 118 | uint8_t vsel; |
| 119 | uint8_t buck_reg; |
| 120 | |
| 121 | switch (buck) { |
| 122 | case 1: |
| 123 | case 2: |
huang lin | 75f431a | 2015-01-23 14:48:42 +0800 | [diff] [blame] | 124 | /* 25mV steps. base = 29 * 25mV = 725 */ |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 125 | vsel = (div_round_up(millivolts, 25) - 29) * 2 + 1; |
| 126 | assert(vsel <= 0x3f); |
| 127 | buck_reg = BUCK1SEL + 4 * (buck - 1); |
| 128 | break; |
| 129 | case 4: |
| 130 | vsel = div_round_up(millivolts, 100) - 18; |
| 131 | assert(vsel <= 0xf); |
| 132 | buck_reg = BUCK4SEL; |
| 133 | break; |
| 134 | default: |
huang lin | 75f431a | 2015-01-23 14:48:42 +0800 | [diff] [blame] | 135 | die("Unknown buck index!"); |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 136 | } |
huang lin | 75f431a | 2015-01-23 14:48:42 +0800 | [diff] [blame] | 137 | rk808_clrsetbits(DCDC_ILMAX, 0, 3 << ((buck - 1) * 2)); |
huang lin | 3704e69 | 2015-02-27 19:35:04 -0800 | [diff] [blame^] | 138 | |
| 139 | /* undervoltage detection may be wrong, disable it */ |
| 140 | rk808_clrsetbits(DCDC_UV_ACT, 1 << (buck - 1), 0); |
| 141 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 142 | rk808_clrsetbits(buck_reg, 0x3f, vsel); |
| 143 | rk808_clrsetbits(DCDC_EN, 0, 1 << (buck - 1)); |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 144 | } |
David Hendricks | 3c4951e | 2015-01-12 14:08:10 -0800 | [diff] [blame] | 145 | |
| 146 | static void rk808rtc_stop(void) |
| 147 | { |
| 148 | rk808_clrsetbits(RTC_CTRL, RTC_CTRL_STOP_RTC, 0); |
| 149 | } |
| 150 | |
| 151 | static void rk808rtc_start(void) |
| 152 | { |
| 153 | rk808_clrsetbits(RTC_CTRL, 0, RTC_CTRL_STOP_RTC); |
| 154 | } |
| 155 | |
| 156 | int rtc_set(const struct rtc_time *time) |
| 157 | { |
| 158 | int ret = 0; |
| 159 | |
| 160 | /* RTC time can only be set when RTC is frozen */ |
| 161 | rk808rtc_stop(); |
| 162 | |
| 163 | ret |= rk808_write(RTC_SECOND, bin2bcd(time->sec)); |
| 164 | ret |= rk808_write(RTC_MINUTE, bin2bcd(time->min)); |
| 165 | ret |= rk808_write(RTC_HOUR, bin2bcd(time->hour)); |
| 166 | ret |= rk808_write(RTC_DAY, bin2bcd(time->mday)); |
| 167 | ret |= rk808_write(RTC_MONTH, bin2bcd(time->mon)); |
| 168 | ret |= rk808_write(RTC_YEAR, bin2bcd(time->year)); |
| 169 | |
| 170 | rk808rtc_start(); |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | int rtc_get(struct rtc_time *time) |
| 175 | { |
| 176 | uint8_t value; |
| 177 | int ret = 0; |
| 178 | |
| 179 | /* |
| 180 | * Set RTC_READSEL to cause reads to access shadow registers and |
| 181 | * transition GET_TIME from 0 to 1 to cause dynamic register content |
| 182 | * to be copied into shadow registers. This ensures a coherent reading |
| 183 | * of time values as we access each register using slow I2C transfers. |
| 184 | */ |
| 185 | rk808_clrsetbits(RTC_CTRL, RTC_CTRL_GET_TIME, 0); |
| 186 | rk808_clrsetbits(RTC_CTRL, 0, RTC_CTRL_GET_TIME | RTC_CTRL_RTC_READSEL); |
| 187 | |
| 188 | ret |= rk808_read(RTC_SECOND, &value); |
| 189 | time->sec = bcd2bin(value & 0x7f); |
| 190 | |
| 191 | ret |= rk808_read(RTC_MINUTE, &value); |
| 192 | time->min = bcd2bin(value & 0x7f); |
| 193 | |
| 194 | ret |= rk808_read(RTC_HOUR, &value); |
| 195 | time->hour = bcd2bin(value & 0x3f); |
| 196 | |
| 197 | ret |= rk808_read(RTC_DAY, &value); |
| 198 | time->mday = bcd2bin(value & 0x3f); |
| 199 | |
| 200 | ret |= rk808_read(RTC_MONTH, &value); |
| 201 | time->mon = bcd2bin(value & 0x1f); |
| 202 | |
| 203 | ret |= rk808_read(RTC_YEAR, &value); |
| 204 | time->year = bcd2bin(value); |
| 205 | |
| 206 | return ret; |
| 207 | } |