blob: 9713a9292fbeb816ffa88bd68a5f1bef0673e721 [file] [log] [blame]
Julius Werner7a757c92014-09-10 19:37:15 -07001/*
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 Werner7a453eb2014-10-20 13:14:55 -070022#include <delay.h>
Julius Werner7a757c92014-09-10 19:37:15 -070023#include <device/i2c.h>
Julius Werner7a453eb2014-10-20 13:14:55 -070024#include <soc/rk808.h>
Julius Werner7a757c92014-09-10 19:37:15 -070025#include <stdint.h>
26#include <stdlib.h>
Julius Werner7a757c92014-09-10 19:37:15 -070027
28#define RK808_ADDR 0x1b
29
Julius Werner8f3883d2014-09-26 21:01:08 -070030#define DCDC_EN 0x23
Julius Werner7a757c92014-09-10 19:37:15 -070031#define LDO_EN 0x24
huang lin08884e32014-10-10 20:28:47 -070032#define BUCK1SEL 0x2f
33#define BUCK4SEL 0x38
Julius Werner7a757c92014-09-10 19:37:15 -070034#define LDO_ONSEL(i) (0x39 + 2 * i)
35#define LDO_SLPSEL(i) (0x3a + 2 * i)
36
37static 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 Werner8f3883d2014-09-26 21:01:08 -070046void 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 Werner7a757c92014-09-10 19:37:15 -070052void rk808_configure_ldo(uint8_t bus, int ldo, int millivolts)
53{
54 uint8_t vsel;
55
Julius Wernerdbfa9d52014-12-05 17:29:42 -080056 if (!millivolts) {
57 rk808_clrsetbits(bus, LDO_EN, 1 << (ldo - 1), 0);
58 return;
59 }
60
Julius Werner7a757c92014-09-10 19:37:15 -070061 switch (ldo) {
62 case 1:
63 case 2:
64 case 4:
65 case 5:
66 case 8:
huang lin08884e32014-10-10 20:28:47 -070067 vsel = div_round_up(millivolts, 100) - 18;
Julius Werneref639b22014-11-06 14:33:12 -080068 assert(vsel <= 0x10);
Julius Werner7a757c92014-09-10 19:37:15 -070069 break;
70 case 3:
71 case 6:
72 case 7:
huang lin08884e32014-10-10 20:28:47 -070073 vsel = div_round_up(millivolts, 100) - 8;
Julius Werneref639b22014-11-06 14:33:12 -080074 assert(vsel <= 0x11);
Julius Werner7a757c92014-09-10 19:37:15 -070075 break;
76 default:
77 die("Unknown LDO index!");
78 }
Julius Werner7a757c92014-09-10 19:37:15 -070079
80 rk808_clrsetbits(bus, LDO_ONSEL(ldo), 0x1f, vsel);
81 rk808_clrsetbits(bus, LDO_EN, 0, 1 << (ldo - 1));
82}
huang lin08884e32014-10-10 20:28:47 -070083
84void rk808_configure_buck(uint8_t bus, int buck, int millivolts)
85{
86 uint8_t vsel;
87 uint8_t buck_reg;
88
89 switch (buck) {
90 case 1:
91 case 2:
92 /*base on 725mv, use 25mv step */
93 vsel = (div_round_up(millivolts, 25) - 29) * 2 + 1;
94 assert(vsel <= 0x3f);
95 buck_reg = BUCK1SEL + 4 * (buck - 1);
96 break;
97 case 4:
98 vsel = div_round_up(millivolts, 100) - 18;
99 assert(vsel <= 0xf);
100 buck_reg = BUCK4SEL;
101 break;
102 default:
103 die("fault buck index!");
104 }
105 rk808_clrsetbits(bus, buck_reg, 0x3f, vsel);
106 rk808_clrsetbits(bus, DCDC_EN, 0, 1 << (buck - 1));
huang lin08884e32014-10-10 20:28:47 -0700107}