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> |
| 21 | #include <console/console.h> |
Julius Werner | 7a453eb | 2014-10-20 13:14:55 -0700 | [diff] [blame] | 22 | #include <delay.h> |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 23 | #include <device/i2c.h> |
Julius Werner | 7a453eb | 2014-10-20 13:14:55 -0700 | [diff] [blame] | 24 | #include <soc/rk808.h> |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 25 | #include <stdint.h> |
| 26 | #include <stdlib.h> |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 27 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame^] | 28 | #if CONFIG_PMIC_BUS < 0 |
| 29 | #error "PMIC_BUS must be set in mainboard's Kconfig." |
| 30 | #endif |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 31 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame^] | 32 | #define RK808_ADDR 0x1b |
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 DCDC_EN 0x23 |
| 35 | #define LDO_EN 0x24 |
| 36 | #define BUCK1SEL 0x2f |
| 37 | #define BUCK4SEL 0x38 |
| 38 | #define LDO_ONSEL(i) (0x39 + 2 * i) |
| 39 | #define LDO_SLPSEL(i) (0x3a + 2 * i) |
| 40 | |
| 41 | static int rk808_read(uint8_t reg, uint8_t *value) |
| 42 | { |
| 43 | return i2c_readb(CONFIG_PMIC_BUS, RK808_ADDR, reg, value); |
| 44 | } |
| 45 | |
| 46 | static int rk808_write(uint8_t reg, uint8_t value) |
| 47 | { |
| 48 | return i2c_writeb(CONFIG_PMIC_BUS, RK808_ADDR, reg, value); |
| 49 | } |
| 50 | |
| 51 | 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] | 52 | { |
| 53 | uint8_t value; |
| 54 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame^] | 55 | if (rk808_read(reg, &value) || rk808_write(reg, (value & ~clr) | set)) |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 56 | printk(BIOS_ERR, "ERROR: Cannot set Rk808[%#x]!\n", reg); |
| 57 | } |
| 58 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame^] | 59 | void rk808_configure_switch(int sw, int enabled) |
Julius Werner | 8f3883d | 2014-09-26 21:01:08 -0700 | [diff] [blame] | 60 | { |
| 61 | assert(sw == 1 || sw == 2); |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame^] | 62 | rk808_clrsetbits(DCDC_EN, 1 << (sw + 4), !!enabled << (sw + 4)); |
Julius Werner | 8f3883d | 2014-09-26 21:01:08 -0700 | [diff] [blame] | 63 | } |
| 64 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame^] | 65 | void rk808_configure_ldo(int ldo, int millivolts) |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 66 | { |
| 67 | uint8_t vsel; |
| 68 | |
Julius Werner | dbfa9d5 | 2014-12-05 17:29:42 -0800 | [diff] [blame] | 69 | if (!millivolts) { |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame^] | 70 | rk808_clrsetbits(LDO_EN, 1 << (ldo - 1), 0); |
Julius Werner | dbfa9d5 | 2014-12-05 17:29:42 -0800 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 74 | switch (ldo) { |
| 75 | case 1: |
| 76 | case 2: |
| 77 | case 4: |
| 78 | case 5: |
| 79 | case 8: |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 80 | vsel = div_round_up(millivolts, 100) - 18; |
Julius Werner | ef639b2 | 2014-11-06 14:33:12 -0800 | [diff] [blame] | 81 | assert(vsel <= 0x10); |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 82 | break; |
| 83 | case 3: |
| 84 | case 6: |
| 85 | case 7: |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 86 | vsel = div_round_up(millivolts, 100) - 8; |
Julius Werner | ef639b2 | 2014-11-06 14:33:12 -0800 | [diff] [blame] | 87 | assert(vsel <= 0x11); |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 88 | break; |
| 89 | default: |
| 90 | die("Unknown LDO index!"); |
| 91 | } |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 92 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame^] | 93 | rk808_clrsetbits(LDO_ONSEL(ldo), 0x1f, vsel); |
| 94 | rk808_clrsetbits(LDO_EN, 0, 1 << (ldo - 1)); |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 95 | } |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 96 | |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame^] | 97 | void rk808_configure_buck(int buck, int millivolts) |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 98 | { |
| 99 | uint8_t vsel; |
| 100 | uint8_t buck_reg; |
| 101 | |
| 102 | switch (buck) { |
| 103 | case 1: |
| 104 | case 2: |
| 105 | /*base on 725mv, use 25mv step */ |
| 106 | vsel = (div_round_up(millivolts, 25) - 29) * 2 + 1; |
| 107 | assert(vsel <= 0x3f); |
| 108 | buck_reg = BUCK1SEL + 4 * (buck - 1); |
| 109 | break; |
| 110 | case 4: |
| 111 | vsel = div_round_up(millivolts, 100) - 18; |
| 112 | assert(vsel <= 0xf); |
| 113 | buck_reg = BUCK4SEL; |
| 114 | break; |
| 115 | default: |
| 116 | die("fault buck index!"); |
| 117 | } |
David Hendricks | 4d24421 | 2015-01-12 13:13:30 -0800 | [diff] [blame^] | 118 | rk808_clrsetbits(buck_reg, 0x3f, vsel); |
| 119 | rk808_clrsetbits(DCDC_EN, 0, 1 << (buck - 1)); |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 120 | } |