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 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 57 | static int rk808_read(uint8_t reg, uint8_t *value) |
| 58 | { |
| 59 | return i2c_readb(CONFIG_PMIC_BUS, RK808_ADDR, reg, value); |
| 60 | } |
| 61 | |
| 62 | static int rk808_write(uint8_t reg, uint8_t value) |
| 63 | { |
| 64 | return i2c_writeb(CONFIG_PMIC_BUS, RK808_ADDR, reg, value); |
| 65 | } |
| 66 | |
| 67 | 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] | 68 | { |
| 69 | uint8_t value; |
| 70 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 71 | if (rk808_read(reg, &value) || rk808_write(reg, (value & ~clr) | set)) |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 72 | printk(BIOS_ERR, "ERROR: Cannot set Rk808[%#x]!\n", reg); |
| 73 | } |
| 74 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 75 | void rk808_configure_switch(int sw, int enabled) |
Julius Werner | 8f3883d | 2014-09-26 21:01:08 -0700 | [diff] [blame] | 76 | { |
| 77 | assert(sw == 1 || sw == 2); |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 78 | rk808_clrsetbits(DCDC_EN, 1 << (sw + 4), !!enabled << (sw + 4)); |
Julius Werner | 8f3883d | 2014-09-26 21:01:08 -0700 | [diff] [blame] | 79 | } |
| 80 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 81 | void rk808_configure_ldo(int ldo, int millivolts) |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 82 | { |
| 83 | uint8_t vsel; |
| 84 | |
Julius Werner | dbfa9d5 | 2014-12-05 17:29:42 -0800 | [diff] [blame] | 85 | if (!millivolts) { |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 86 | rk808_clrsetbits(LDO_EN, 1 << (ldo - 1), 0); |
Julius Werner | dbfa9d5 | 2014-12-05 17:29:42 -0800 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 90 | switch (ldo) { |
| 91 | case 1: |
| 92 | case 2: |
| 93 | case 4: |
| 94 | case 5: |
| 95 | case 8: |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 96 | vsel = div_round_up(millivolts, 100) - 18; |
Julius Werner | ef639b2 | 2014-11-06 14:33:12 -0800 | [diff] [blame] | 97 | assert(vsel <= 0x10); |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 98 | break; |
| 99 | case 3: |
| 100 | case 6: |
| 101 | case 7: |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 102 | vsel = div_round_up(millivolts, 100) - 8; |
Julius Werner | ef639b2 | 2014-11-06 14:33:12 -0800 | [diff] [blame] | 103 | assert(vsel <= 0x11); |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 104 | break; |
| 105 | default: |
| 106 | die("Unknown LDO index!"); |
| 107 | } |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 108 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 109 | rk808_clrsetbits(LDO_ONSEL(ldo), 0x1f, vsel); |
| 110 | rk808_clrsetbits(LDO_EN, 0, 1 << (ldo - 1)); |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 111 | } |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 112 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 113 | void rk808_configure_buck(int buck, int millivolts) |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 114 | { |
| 115 | uint8_t vsel; |
| 116 | uint8_t buck_reg; |
| 117 | |
| 118 | switch (buck) { |
| 119 | case 1: |
| 120 | case 2: |
| 121 | /*base on 725mv, use 25mv step */ |
| 122 | vsel = (div_round_up(millivolts, 25) - 29) * 2 + 1; |
| 123 | assert(vsel <= 0x3f); |
| 124 | buck_reg = BUCK1SEL + 4 * (buck - 1); |
| 125 | break; |
| 126 | case 4: |
| 127 | vsel = div_round_up(millivolts, 100) - 18; |
| 128 | assert(vsel <= 0xf); |
| 129 | buck_reg = BUCK4SEL; |
| 130 | break; |
| 131 | default: |
| 132 | die("fault buck index!"); |
| 133 | } |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame] | 134 | rk808_clrsetbits(buck_reg, 0x3f, vsel); |
| 135 | rk808_clrsetbits(DCDC_EN, 0, 1 << (buck - 1)); |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 136 | } |
David Hendricks | 3c4951e | 2015-01-12 14:08:10 -0800 | [diff] [blame^] | 137 | |
| 138 | static void rk808rtc_stop(void) |
| 139 | { |
| 140 | rk808_clrsetbits(RTC_CTRL, RTC_CTRL_STOP_RTC, 0); |
| 141 | } |
| 142 | |
| 143 | static void rk808rtc_start(void) |
| 144 | { |
| 145 | rk808_clrsetbits(RTC_CTRL, 0, RTC_CTRL_STOP_RTC); |
| 146 | } |
| 147 | |
| 148 | int rtc_set(const struct rtc_time *time) |
| 149 | { |
| 150 | int ret = 0; |
| 151 | |
| 152 | /* RTC time can only be set when RTC is frozen */ |
| 153 | rk808rtc_stop(); |
| 154 | |
| 155 | ret |= rk808_write(RTC_SECOND, bin2bcd(time->sec)); |
| 156 | ret |= rk808_write(RTC_MINUTE, bin2bcd(time->min)); |
| 157 | ret |= rk808_write(RTC_HOUR, bin2bcd(time->hour)); |
| 158 | ret |= rk808_write(RTC_DAY, bin2bcd(time->mday)); |
| 159 | ret |= rk808_write(RTC_MONTH, bin2bcd(time->mon)); |
| 160 | ret |= rk808_write(RTC_YEAR, bin2bcd(time->year)); |
| 161 | |
| 162 | rk808rtc_start(); |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | int rtc_get(struct rtc_time *time) |
| 167 | { |
| 168 | uint8_t value; |
| 169 | int ret = 0; |
| 170 | |
| 171 | /* |
| 172 | * Set RTC_READSEL to cause reads to access shadow registers and |
| 173 | * transition GET_TIME from 0 to 1 to cause dynamic register content |
| 174 | * to be copied into shadow registers. This ensures a coherent reading |
| 175 | * of time values as we access each register using slow I2C transfers. |
| 176 | */ |
| 177 | rk808_clrsetbits(RTC_CTRL, RTC_CTRL_GET_TIME, 0); |
| 178 | rk808_clrsetbits(RTC_CTRL, 0, RTC_CTRL_GET_TIME | RTC_CTRL_RTC_READSEL); |
| 179 | |
| 180 | ret |= rk808_read(RTC_SECOND, &value); |
| 181 | time->sec = bcd2bin(value & 0x7f); |
| 182 | |
| 183 | ret |= rk808_read(RTC_MINUTE, &value); |
| 184 | time->min = bcd2bin(value & 0x7f); |
| 185 | |
| 186 | ret |= rk808_read(RTC_HOUR, &value); |
| 187 | time->hour = bcd2bin(value & 0x3f); |
| 188 | |
| 189 | ret |= rk808_read(RTC_DAY, &value); |
| 190 | time->mday = bcd2bin(value & 0x3f); |
| 191 | |
| 192 | ret |= rk808_read(RTC_MONTH, &value); |
| 193 | time->mon = bcd2bin(value & 0x1f); |
| 194 | |
| 195 | ret |= rk808_read(RTC_YEAR, &value); |
| 196 | time->year = bcd2bin(value); |
| 197 | |
| 198 | return ret; |
| 199 | } |