blob: 1812a2ab411bd9d5f20fa3a55bbc962eb78ac853 [file] [log] [blame]
huang lina6dbfb52016-03-02 18:38:40 +08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2016 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 */
16
Vadim Bendebury8e8a00c2016-04-22 12:25:07 -070017#include <boardid.h>
Lin Huangb497b482016-03-31 18:44:13 +080018#include <delay.h>
huang lina6dbfb52016-03-02 18:38:40 +080019#include <device/device.h>
Lin Huangb497b482016-03-31 18:44:13 +080020#include <device/i2c.h>
Vadim Bendebury1e80ab32016-03-28 00:44:54 -070021#include <gpio.h>
Lin Huang5a4be8a2016-05-17 15:45:53 +080022#include <soc/bl31_plat_params.h>
Vadim Bendebury1e80ab32016-03-28 00:44:54 -070023#include <soc/clock.h>
Lin Huangb497b482016-03-31 18:44:13 +080024#include <soc/display.h>
Vadim Bendebury1e80ab32016-03-28 00:44:54 -070025#include <soc/grf.h>
Lin Huangb497b482016-03-31 18:44:13 +080026#include <soc/i2c.h>
Liangfeng Wu76655cb2016-05-26 16:06:58 +080027#include <soc/usb.h>
Simon Glassbc679bc2016-06-19 16:09:21 -060028#include <vendorcode/google/chromeos/chromeos.h>
Vadim Bendebury1e80ab32016-03-28 00:44:54 -070029
Vadim Bendebury993dbe12016-05-22 15:53:37 -070030#include "board.h"
31
Lin Huang2f7ed8d2016-04-08 18:56:20 +080032static void configure_emmc(void)
33{
34 /* Host controller does not support programmable clock generator.
35 * If we don't do this setting, when we use phy to control the
36 * emmc clock(when clock exceed 50MHz), it will get wrong clock.
37 *
38 * Refer to TRM V0.3 Part 1 Chapter 15 PAGE 782 for this register.
39 * Please search "_CON11[7:0]" to locate register description.
40 */
41 write32(&rk3399_grf->emmccore_con[11], RK_CLRSETBITS(0xff, 0));
42
43 rkclk_configure_emmc();
44}
45
Lin Huang7d8ccfb2016-08-22 17:35:40 -070046static void register_gpio_suspend(void)
47{
48 /*
49 * These three GPIO params are used to shut down the 1.5V, 1.8V and
50 * 3.3V power rails, which need to be shut down ordered by voltage,
51 * with highest voltage first.
52 * Since register_bl31() appends to the front of the list, we need to
53 * register them backwards, with 1.5V coming first.
54 */
55 static struct bl31_gpio_param param_p15_en = {
56 .h = {
57 .type = PARAM_SUSPEND_GPIO,
58 },
59 .gpio = {
60 .polarity = BL31_GPIO_LEVEL_LOW,
61 },
62 };
63 param_p15_en.gpio.index = GET_GPIO_NUM(GPIO_P15V_EN);
64 register_bl31_param(&param_p15_en.h);
65
66 static struct bl31_gpio_param param_p18_audio_en = {
67 .h = {
68 .type = PARAM_SUSPEND_GPIO,
69 },
70 .gpio = {
71 .polarity = BL31_GPIO_LEVEL_LOW,
72 },
73 };
74 param_p18_audio_en.gpio.index = GET_GPIO_NUM(GPIO_P18V_AUDIO_PWREN);
75 register_bl31_param(&param_p18_audio_en.h);
76
77 static struct bl31_gpio_param param_p30_en = {
78 .h = {
79 .type = PARAM_SUSPEND_GPIO,
80 },
81 .gpio = {
82 .polarity = BL31_GPIO_LEVEL_LOW,
83 },
84 };
85 param_p30_en.gpio.index = GET_GPIO_NUM(GPIO_P30V_EN);
86 register_bl31_param(&param_p30_en.h);
87}
88
Lin Huang5a4be8a2016-05-17 15:45:53 +080089static void register_reset_to_bl31(void)
90{
91 static struct bl31_gpio_param param_reset = {
92 .h = {
93 .type = PARAM_RESET,
94 },
95 .gpio = {
96 .polarity = 1,
97 },
98 };
99
100 /* gru/kevin reset pin: gpio0b3 */
101 param_reset.gpio.index = GET_GPIO_NUM(GPIO_RESET),
102
103 register_bl31_param(&param_reset.h);
104}
105
Lin Huang9a5c4fe2016-05-19 11:11:23 +0800106static void register_poweroff_to_bl31(void)
107{
108 static struct bl31_gpio_param param_poweroff = {
109 .h = {
110 .type = PARAM_POWEROFF,
111 },
112 .gpio = {
113 .polarity = 1,
114 },
115 };
116
117 /*
118 * gru/kevin power off pin: gpio1a6,
119 * reuse with tsadc int pin, so iomux need set back to
120 * gpio in BL31 and depthcharge before you setting this gpio
121 */
122 param_poweroff.gpio.index = GET_GPIO_NUM(GPIO_POWEROFF),
123
124 register_bl31_param(&param_poweroff.h);
125}
126
Vadim Bendebury1e80ab32016-03-28 00:44:54 -0700127static void configure_sdmmc(void)
128{
129 gpio_output(GPIO(4, D, 5), 1); /* SDMMC_PWR_EN */
130 gpio_output(GPIO(2, A, 2), 1); /* SDMMC_SDIO_PWR_EN */
Vadim Bendebury2832c412016-05-11 15:03:44 +0800131
132 /* SDMMC_DET_L is different on Kevin board revision 0. */
133 if (IS_ENABLED(CONFIG_BOARD_GOOGLE_KEVIN) && (board_id() == 0))
Vadim Bendebury8e8a00c2016-04-22 12:25:07 -0700134 gpio_input(GPIO(4, D, 2));
Vadim Bendebury2832c412016-05-11 15:03:44 +0800135 else
Vadim Bendebury8e8a00c2016-04-22 12:25:07 -0700136 gpio_input(GPIO(4, D, 0));
Vadim Bendebury2832c412016-05-11 15:03:44 +0800137
Vadim Bendebury1e80ab32016-03-28 00:44:54 -0700138 gpio_output(GPIO(2, D, 4), 0); /* Keep the max voltage */
Vadim Bendebury8e8a00c2016-04-22 12:25:07 -0700139
Julius Werner7feb86b2016-09-02 11:25:56 -0700140 gpio_input(GPIO(4, B, 0)); /* SDMMC0_D0 remove pull-up */
141 gpio_input(GPIO(4, B, 1)); /* SDMMC0_D1 remove pull-up */
142 gpio_input(GPIO(4, B, 2)); /* SDMMC0_D2 remove pull-up */
143 gpio_input(GPIO(4, B, 3)); /* SDMMC0_D3 remove pull-up */
144 gpio_input(GPIO(4, B, 4)); /* SDMMC0_CLK remove pull-down */
145 gpio_input(GPIO(4, B, 5)); /* SDMMC0_CMD remove pull-up */
146
Vadim Bendeburyad6ee022016-05-12 16:54:00 +0800147 write32(&rk3399_grf->gpio2_p[2][1], RK_CLRSETBITS(0xfff, 0));
148
149 /*
150 * Set all outputs' drive strength to 8 mA. Group 4 bank B driver
151 * strength requires three bits per pin. Value of 2 written in that
152 * three bit field means '8 mA', as deduced from the kernel code.
153 *
154 * Thus the six pins involved in SDMMC interface require 18 bits to
155 * configure drive strength, but each 32 bit register provides only 16
156 * bits for this setting, this covers 5 pins fully and one bit from
157 * the 6th pin. Two more bits spill over to the next register. This is
158 * described on page 378 of rk3399 TRM Version 0.3 Part 1.
159 */
160 write32(&rk3399_grf->gpio4b_e01,
161 RK_CLRSETBITS(0xffff,
162 (2 << 0) | (2 << 3) |
163 (2 << 6) | (2 << 9) | (2 << 12)));
164 write32(&rk3399_grf->gpio4b_e2, RK_CLRSETBITS(3, 1));
165
166 /* And now set the multiplexor to enable SDMMC0. */
Vadim Bendebury1e80ab32016-03-28 00:44:54 -0700167 write32(&rk3399_grf->iomux_sdmmc, IOMUX_SDMMC);
168}
huang lina6dbfb52016-03-02 18:38:40 +0800169
Xing Zheng96fbc312016-05-19 11:39:20 +0800170static void configure_codec(void)
171{
Julius Werner7feb86b2016-09-02 11:25:56 -0700172 gpio_input(GPIO(3, D, 0)); /* I2S0_SCLK remove pull-up */
173 gpio_input(GPIO(3, D, 1)); /* I2S0_RX remove pull-up */
174 gpio_input(GPIO(3, D, 2)); /* I2S0_TX remove pull-up */
175 gpio_input(GPIO(3, D, 3)); /* I2S0_SDI0 remove pull-up */
176 gpio_input(GPIO(3, D, 4)); /* I2S0_SDI1 remove pull-up */
177 /* GPIO3_D5 (I2S0_SDI2SDO2) not connected */
178 gpio_input(GPIO(3, D, 6)); /* I2S0_SDO1 remove pull-up */
179 gpio_input(GPIO(3, D, 7)); /* I2S0_SDO0 remove pull-up */
180 gpio_input(GPIO(4, A, 0)); /* I2S0_MCLK remove pull-up */
181
Xing Zheng96fbc312016-05-19 11:39:20 +0800182 write32(&rk3399_grf->iomux_i2s0, IOMUX_I2S0);
183 write32(&rk3399_grf->iomux_i2sclk, IOMUX_I2SCLK);
184
185 /* AUDIO IO domain 1.8V voltage selection */
186 write32(&rk3399_grf->io_vsel, RK_SETBITS(1 << 1));
187
188 /* CPU1_P1.8V_AUDIO_PWREN for P1.8_AUDIO */
189 gpio_output(GPIO(0, A, 2), 1);
190
191 /* set CPU1_SPK_PA_EN output */
192 gpio_output(GPIO(1, A, 2), 0);
193
194 rkclk_configure_i2s(12288000);
195}
196
Lin Huangb497b482016-03-31 18:44:13 +0800197static void configure_display(void)
198{
199 /* set pinmux for edp HPD*/
200 gpio_input_pulldown(GPIO(4, C, 7));
201 write32(&rk3399_grf->iomux_edp_hotplug, IOMUX_EDP_HOTPLUG);
202
203 gpio_output(GPIO(4, D, 3), 1); /* CPU3_EDP_VDDEN for P3.3V_DISP */
204}
205
Liangfeng Wu76655cb2016-05-26 16:06:58 +0800206static void setup_usb(void)
207{
Julius Werner1c8491c2016-08-15 17:58:05 -0700208 /* A few magic PHY tuning values that improve eye diagram amplitude
209 * and make it extra sure we get reliable communication in firmware. */
210 /* Set max ODT compensation voltage and current tuning reference. */
211 write32(&rk3399_grf->usbphy0_ctrl[3], 0x0fff02e3);
212 write32(&rk3399_grf->usbphy1_ctrl[3], 0x0fff02e3);
213 /* Set max pre-emphasis level, only on Kevin PHY0. */
214 if (IS_ENABLED(CONFIG_BOARD_GOOGLE_KEVIN))
215 write32(&rk3399_grf->usbphy0_ctrl[12], 0xffff00a7);
216
Julius Werner785ff1b2016-08-03 19:18:39 -0700217 setup_usb_otg0();
218 setup_usb_otg1();
Liangfeng Wu76655cb2016-05-26 16:06:58 +0800219}
220
huang lina6dbfb52016-03-02 18:38:40 +0800221static void mainboard_init(device_t dev)
222{
Vadim Bendebury1e80ab32016-03-28 00:44:54 -0700223 configure_sdmmc();
Lin Huang2f7ed8d2016-04-08 18:56:20 +0800224 configure_emmc();
Xing Zheng96fbc312016-05-19 11:39:20 +0800225 configure_codec();
Lin Huangb497b482016-03-31 18:44:13 +0800226 configure_display();
Liangfeng Wu76655cb2016-05-26 16:06:58 +0800227 setup_usb();
Lin Huang5a4be8a2016-05-17 15:45:53 +0800228 register_reset_to_bl31();
Lin Huang9a5c4fe2016-05-19 11:11:23 +0800229 register_poweroff_to_bl31();
Lin Huang7d8ccfb2016-08-22 17:35:40 -0700230 register_gpio_suspend();
Lin Huangb497b482016-03-31 18:44:13 +0800231}
232
233static void enable_backlight_booster(void)
234{
235 const struct {
236 uint8_t reg;
237 uint8_t value;
238 } i2c_writes[] = {
239 {1, 0x84},
240 {1, 0x85},
241 {0, 0x26}
242 };
243 int i;
244 const int booster_i2c_port = 0;
245 uint8_t i2c_buf[2];
246 struct i2c_seg i2c_command = { .read = 0, .chip = 0x2c,
247 .buf = i2c_buf, .len = sizeof(i2c_buf)
248 };
249
250 /*
251 * This function is called on Gru right after BL_EN is asserted. It
252 * takes time for the switcher chip to come online, let's wait a bit
253 * to let the voltage settle, so that the chip can be accessed.
254 */
255 udelay(1000);
256
Julius Werner7feb86b2016-09-02 11:25:56 -0700257 gpio_input(GPIO(1, B, 7)); /* I2C0_SDA remove pull_up */
258 gpio_input(GPIO(1, C, 0)); /* I2C0_SCL remove pull_up */
259
260 i2c_init(0, 100*KHz);
261
Lin Huangb497b482016-03-31 18:44:13 +0800262 write32(&rk3399_pmugrf->iomux_i2c0_sda, IOMUX_I2C0_SDA);
263 write32(&rk3399_pmugrf->iomux_i2c0_scl, IOMUX_I2C0_SCL);
Lin Huangb497b482016-03-31 18:44:13 +0800264
265 for (i = 0; i < ARRAY_SIZE(i2c_writes); i++) {
266 i2c_buf[0] = i2c_writes[i].reg;
267 i2c_buf[1] = i2c_writes[i].value;
268 i2c_transfer(booster_i2c_port, &i2c_command, 1);
269 }
270}
271
272void mainboard_power_on_backlight(void)
273{
274 gpio_output(GPIO(1, C, 1), 1); /* BL_EN */
275
Julius Werner5e6771b2016-07-29 16:15:04 -0700276 if (IS_ENABLED(CONFIG_BOARD_GOOGLE_GRU) && board_id() == 0)
Lin Huangb497b482016-03-31 18:44:13 +0800277 enable_backlight_booster();
huang lina6dbfb52016-03-02 18:38:40 +0800278}
279
280static void mainboard_enable(device_t dev)
281{
282 dev->ops->init = &mainboard_init;
283}
284
285struct chip_operations mainboard_ops = {
286 .name = CONFIG_MAINBOARD_PART_NUMBER,
287 .enable_dev = mainboard_enable,
288};