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 | |
| 28 | #define RK808_ADDR 0x1b |
| 29 | |
Julius Werner | 8f3883d | 2014-09-26 21:01:08 -0700 | [diff] [blame] | 30 | #define DCDC_EN 0x23 |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 31 | #define LDO_EN 0x24 |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 32 | #define BUCK1SEL 0x2f |
| 33 | #define BUCK4SEL 0x38 |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 34 | #define LDO_ONSEL(i) (0x39 + 2 * i) |
| 35 | #define LDO_SLPSEL(i) (0x3a + 2 * i) |
| 36 | |
| 37 | static void rk808_clrsetbits(uint8_t bus, uint8_t reg, uint8_t clr, uint8_t set) |
| 38 | { |
| 39 | uint8_t value; |
| 40 | |
| 41 | if (i2c_readb(bus, RK808_ADDR, reg, &value) || |
| 42 | i2c_writeb(bus, RK808_ADDR, reg, (value & ~clr) | set)) |
| 43 | printk(BIOS_ERR, "ERROR: Cannot set Rk808[%#x]!\n", reg); |
| 44 | } |
| 45 | |
Julius Werner | 8f3883d | 2014-09-26 21:01:08 -0700 | [diff] [blame] | 46 | void rk808_configure_switch(uint8_t bus, int sw, int enabled) |
| 47 | { |
| 48 | assert(sw == 1 || sw == 2); |
| 49 | rk808_clrsetbits(bus, DCDC_EN, 1 << (sw + 4), !!enabled << (sw + 4)); |
| 50 | } |
| 51 | |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 52 | void rk808_configure_ldo(uint8_t bus, int ldo, int millivolts) |
| 53 | { |
| 54 | uint8_t vsel; |
| 55 | |
| 56 | switch (ldo) { |
| 57 | case 1: |
| 58 | case 2: |
| 59 | case 4: |
| 60 | case 5: |
| 61 | case 8: |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 62 | vsel = div_round_up(millivolts, 100) - 18; |
Julius Werner | ef639b2 | 2014-11-06 14:33:12 -0800 | [diff] [blame^] | 63 | assert(vsel <= 0x10); |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 64 | break; |
| 65 | case 3: |
| 66 | case 6: |
| 67 | case 7: |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 68 | vsel = div_round_up(millivolts, 100) - 8; |
Julius Werner | ef639b2 | 2014-11-06 14:33:12 -0800 | [diff] [blame^] | 69 | assert(vsel <= 0x11); |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 70 | break; |
| 71 | default: |
| 72 | die("Unknown LDO index!"); |
| 73 | } |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 74 | |
| 75 | rk808_clrsetbits(bus, LDO_ONSEL(ldo), 0x1f, vsel); |
| 76 | rk808_clrsetbits(bus, LDO_EN, 0, 1 << (ldo - 1)); |
| 77 | } |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 78 | |
| 79 | void rk808_configure_buck(uint8_t bus, int buck, int millivolts) |
| 80 | { |
| 81 | uint8_t vsel; |
| 82 | uint8_t buck_reg; |
| 83 | |
| 84 | switch (buck) { |
| 85 | case 1: |
| 86 | case 2: |
| 87 | /*base on 725mv, use 25mv step */ |
| 88 | vsel = (div_round_up(millivolts, 25) - 29) * 2 + 1; |
| 89 | assert(vsel <= 0x3f); |
| 90 | buck_reg = BUCK1SEL + 4 * (buck - 1); |
| 91 | break; |
| 92 | case 4: |
| 93 | vsel = div_round_up(millivolts, 100) - 18; |
| 94 | assert(vsel <= 0xf); |
| 95 | buck_reg = BUCK4SEL; |
| 96 | break; |
| 97 | default: |
| 98 | die("fault buck index!"); |
| 99 | } |
| 100 | rk808_clrsetbits(bus, buck_reg, 0x3f, vsel); |
| 101 | rk808_clrsetbits(bus, DCDC_EN, 0, 1 << (buck - 1)); |
huang lin | 08884e3 | 2014-10-10 20:28:47 -0700 | [diff] [blame] | 102 | } |