blob: 240a5493e854c66ddadd16ca05d2a4c94fbbd616 [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
philipchen21b08522017-04-27 18:25:11 -070017#include <assert.h>
Vadim Bendebury8e8a00c2016-04-22 12:25:07 -070018#include <boardid.h>
Julius Wernerc49782c2016-11-21 20:14:18 -080019#include <console/console.h>
Lin Huangb497b482016-03-31 18:44:13 +080020#include <delay.h>
huang lina6dbfb52016-03-02 18:38:40 +080021#include <device/device.h>
Lin Huangb497b482016-03-31 18:44:13 +080022#include <device/i2c.h>
Julius Wernerc49782c2016-11-21 20:14:18 -080023#include <ec/google/chromeec/ec.h>
Vadim Bendebury1e80ab32016-03-28 00:44:54 -070024#include <gpio.h>
Lin Huang5a4be8a2016-05-17 15:45:53 +080025#include <soc/bl31_plat_params.h>
Vadim Bendebury1e80ab32016-03-28 00:44:54 -070026#include <soc/clock.h>
Lin Huangb497b482016-03-31 18:44:13 +080027#include <soc/display.h>
Vadim Bendebury1e80ab32016-03-28 00:44:54 -070028#include <soc/grf.h>
Lin Huangb497b482016-03-31 18:44:13 +080029#include <soc/i2c.h>
Liangfeng Wu76655cb2016-05-26 16:06:58 +080030#include <soc/usb.h>
Simon Glassbc679bc2016-06-19 16:09:21 -060031#include <vendorcode/google/chromeos/chromeos.h>
Vadim Bendebury1e80ab32016-03-28 00:44:54 -070032
Vadim Bendebury993dbe12016-05-22 15:53:37 -070033#include "board.h"
34
Brian Norrise06a1b82016-09-21 18:16:54 -070035/*
Caesar Wang212a0262017-05-24 18:02:25 +080036 * We have to drive the stronger pull-up within 1 second of powering up the
Julius Werner6486e782017-07-14 14:30:29 -070037 * touchpad to prevent its firmware from falling into recovery. Not on Scarlet.
Caesar Wang212a0262017-05-24 18:02:25 +080038 */
39static void configure_touchpad(void)
40{
Julius Werner6486e782017-07-14 14:30:29 -070041 gpio_output(GPIO_TP_RST_L, 1); /* TP's I2C pull-up rail */
Caesar Wang212a0262017-05-24 18:02:25 +080042}
43
44/*
Brian Norrise06a1b82016-09-21 18:16:54 -070045 * Wifi's PDN/RST line is pulled down by its (unpowered) voltage rails, but
46 * this reset pin is pulled up by default. Let's drive it low as early as we
Julius Werner6486e782017-07-14 14:30:29 -070047 * can. Scarlet uses a different WiFi chip that doesn't have this pin anymore.
Brian Norrise06a1b82016-09-21 18:16:54 -070048 */
Julius Werner6486e782017-07-14 14:30:29 -070049static void assert_wifi_reset(void)
Brian Norrise06a1b82016-09-21 18:16:54 -070050{
Julius Werner6486e782017-07-14 14:30:29 -070051 gpio_output(GPIO_WLAN_RST_L, 0); /* Assert WLAN_MODULE_RST# */
Brian Norrise06a1b82016-09-21 18:16:54 -070052}
53
Lin Huang2f7ed8d2016-04-08 18:56:20 +080054static void configure_emmc(void)
55{
56 /* Host controller does not support programmable clock generator.
57 * If we don't do this setting, when we use phy to control the
58 * emmc clock(when clock exceed 50MHz), it will get wrong clock.
59 *
60 * Refer to TRM V0.3 Part 1 Chapter 15 PAGE 782 for this register.
61 * Please search "_CON11[7:0]" to locate register description.
62 */
63 write32(&rk3399_grf->emmccore_con[11], RK_CLRSETBITS(0xff, 0));
64
65 rkclk_configure_emmc();
66}
67
Lin Huangc9fea5c2016-08-30 15:34:42 -070068static void register_apio_suspend(void)
69{
70 static struct bl31_apio_param param_apio = {
71 .h = {
72 .type = PARAM_SUSPEND_APIO,
73 },
74 .apio = {
75 .apio1 = 1,
76 .apio2 = 1,
77 .apio3 = 1,
78 .apio4 = 1,
79 .apio5 = 1,
80 },
81 };
82 register_bl31_param(&param_apio.h);
83}
84
Lin Huang7d8ccfb2016-08-22 17:35:40 -070085static void register_gpio_suspend(void)
86{
87 /*
88 * These three GPIO params are used to shut down the 1.5V, 1.8V and
89 * 3.3V power rails, which need to be shut down ordered by voltage,
90 * with highest voltage first.
91 * Since register_bl31() appends to the front of the list, we need to
92 * register them backwards, with 1.5V coming first.
93 */
94 static struct bl31_gpio_param param_p15_en = {
95 .h = {
96 .type = PARAM_SUSPEND_GPIO,
97 },
98 .gpio = {
99 .polarity = BL31_GPIO_LEVEL_LOW,
100 },
101 };
Julius Werner4ed8b302017-07-14 14:25:39 -0700102 param_p15_en.gpio.index = GPIO_P15V_EN.raw;
Lin Huang7d8ccfb2016-08-22 17:35:40 -0700103 register_bl31_param(&param_p15_en.h);
104
105 static struct bl31_gpio_param param_p18_audio_en = {
106 .h = {
107 .type = PARAM_SUSPEND_GPIO,
108 },
109 .gpio = {
110 .polarity = BL31_GPIO_LEVEL_LOW,
111 },
112 };
Julius Werner4ed8b302017-07-14 14:25:39 -0700113 param_p18_audio_en.gpio.index = GPIO_P18V_AUDIO_PWREN.raw;
Lin Huang7d8ccfb2016-08-22 17:35:40 -0700114 register_bl31_param(&param_p18_audio_en.h);
115
116 static struct bl31_gpio_param param_p30_en = {
117 .h = {
118 .type = PARAM_SUSPEND_GPIO,
119 },
120 .gpio = {
121 .polarity = BL31_GPIO_LEVEL_LOW,
122 },
123 };
Julius Werner4ed8b302017-07-14 14:25:39 -0700124 param_p30_en.gpio.index = GPIO_P30V_EN.raw;
Lin Huang7d8ccfb2016-08-22 17:35:40 -0700125 register_bl31_param(&param_p30_en.h);
126}
127
Lin Huang5a4be8a2016-05-17 15:45:53 +0800128static void register_reset_to_bl31(void)
129{
130 static struct bl31_gpio_param param_reset = {
131 .h = {
132 .type = PARAM_RESET,
133 },
134 .gpio = {
135 .polarity = 1,
136 },
137 };
138
139 /* gru/kevin reset pin: gpio0b3 */
Julius Werner4ed8b302017-07-14 14:25:39 -0700140 param_reset.gpio.index = GPIO_RESET.raw,
Lin Huang5a4be8a2016-05-17 15:45:53 +0800141
142 register_bl31_param(&param_reset.h);
143}
144
Lin Huang9a5c4fe2016-05-19 11:11:23 +0800145static void register_poweroff_to_bl31(void)
146{
147 static struct bl31_gpio_param param_poweroff = {
148 .h = {
149 .type = PARAM_POWEROFF,
150 },
151 .gpio = {
152 .polarity = 1,
153 },
154 };
155
156 /*
157 * gru/kevin power off pin: gpio1a6,
158 * reuse with tsadc int pin, so iomux need set back to
159 * gpio in BL31 and depthcharge before you setting this gpio
160 */
Julius Werner4ed8b302017-07-14 14:25:39 -0700161 param_poweroff.gpio.index = GPIO_POWEROFF.raw,
Lin Huang9a5c4fe2016-05-19 11:11:23 +0800162
163 register_bl31_param(&param_poweroff.h);
164}
165
Vadim Bendebury1e80ab32016-03-28 00:44:54 -0700166static void configure_sdmmc(void)
167{
Vadim Bendebury1e80ab32016-03-28 00:44:54 -0700168 gpio_output(GPIO(2, A, 2), 1); /* SDMMC_SDIO_PWR_EN */
Vadim Bendebury2832c412016-05-11 15:03:44 +0800169
Lin Huanga2c5b2f2017-07-25 09:50:10 +0800170 /* set SDMMC_DET_L pin */
171 if (IS_ENABLED(CONFIG_BOARD_GOOGLE_SCARLET))
172 /*
173 * do not have external pull up, so need to
174 * set this pin internal pull up
175 */
176 gpio_input_pullup(GPIO(1, B, 3));
Vadim Bendebury2832c412016-05-11 15:03:44 +0800177 else
Vadim Bendebury8e8a00c2016-04-22 12:25:07 -0700178 gpio_input(GPIO(4, D, 0));
Vadim Bendebury2832c412016-05-11 15:03:44 +0800179
Lin Huanga2c5b2f2017-07-25 09:50:10 +0800180 /*
181 * Keep sd card io domain 3v
182 * In Scarlet this GPIO set to high will get 3v,
183 * With other board variants setting this GPIO low results in 3V.
184 */
185 if (IS_ENABLED(CONFIG_BOARD_GOOGLE_SCARLET))
186 gpio_output(GPIO(2, D, 4), 1);
187 else
188 gpio_output(GPIO(2, D, 4), 0);
Vadim Bendebury8e8a00c2016-04-22 12:25:07 -0700189
Julius Werner7feb86b2016-09-02 11:25:56 -0700190 gpio_input(GPIO(4, B, 0)); /* SDMMC0_D0 remove pull-up */
191 gpio_input(GPIO(4, B, 1)); /* SDMMC0_D1 remove pull-up */
192 gpio_input(GPIO(4, B, 2)); /* SDMMC0_D2 remove pull-up */
193 gpio_input(GPIO(4, B, 3)); /* SDMMC0_D3 remove pull-up */
194 gpio_input(GPIO(4, B, 4)); /* SDMMC0_CLK remove pull-down */
195 gpio_input(GPIO(4, B, 5)); /* SDMMC0_CMD remove pull-up */
196
Vadim Bendeburyad6ee022016-05-12 16:54:00 +0800197 write32(&rk3399_grf->gpio2_p[2][1], RK_CLRSETBITS(0xfff, 0));
198
199 /*
200 * Set all outputs' drive strength to 8 mA. Group 4 bank B driver
201 * strength requires three bits per pin. Value of 2 written in that
202 * three bit field means '8 mA', as deduced from the kernel code.
203 *
204 * Thus the six pins involved in SDMMC interface require 18 bits to
205 * configure drive strength, but each 32 bit register provides only 16
206 * bits for this setting, this covers 5 pins fully and one bit from
207 * the 6th pin. Two more bits spill over to the next register. This is
208 * described on page 378 of rk3399 TRM Version 0.3 Part 1.
209 */
210 write32(&rk3399_grf->gpio4b_e01,
211 RK_CLRSETBITS(0xffff,
212 (2 << 0) | (2 << 3) |
213 (2 << 6) | (2 << 9) | (2 << 12)));
214 write32(&rk3399_grf->gpio4b_e2, RK_CLRSETBITS(3, 1));
215
216 /* And now set the multiplexor to enable SDMMC0. */
Vadim Bendebury1e80ab32016-03-28 00:44:54 -0700217 write32(&rk3399_grf->iomux_sdmmc, IOMUX_SDMMC);
218}
huang lina6dbfb52016-03-02 18:38:40 +0800219
Xing Zheng96fbc312016-05-19 11:39:20 +0800220static void configure_codec(void)
221{
Julius Werner7feb86b2016-09-02 11:25:56 -0700222 gpio_input(GPIO(3, D, 0)); /* I2S0_SCLK remove pull-up */
223 gpio_input(GPIO(3, D, 1)); /* I2S0_RX remove pull-up */
224 gpio_input(GPIO(3, D, 2)); /* I2S0_TX remove pull-up */
225 gpio_input(GPIO(3, D, 3)); /* I2S0_SDI0 remove pull-up */
226 gpio_input(GPIO(3, D, 4)); /* I2S0_SDI1 remove pull-up */
227 /* GPIO3_D5 (I2S0_SDI2SDO2) not connected */
228 gpio_input(GPIO(3, D, 6)); /* I2S0_SDO1 remove pull-up */
229 gpio_input(GPIO(3, D, 7)); /* I2S0_SDO0 remove pull-up */
230 gpio_input(GPIO(4, A, 0)); /* I2S0_MCLK remove pull-up */
231
Xing Zheng96fbc312016-05-19 11:39:20 +0800232 write32(&rk3399_grf->iomux_i2s0, IOMUX_I2S0);
233 write32(&rk3399_grf->iomux_i2sclk, IOMUX_I2SCLK);
234
235 /* AUDIO IO domain 1.8V voltage selection */
236 write32(&rk3399_grf->io_vsel, RK_SETBITS(1 << 1));
237
238 /* CPU1_P1.8V_AUDIO_PWREN for P1.8_AUDIO */
239 gpio_output(GPIO(0, A, 2), 1);
240
241 /* set CPU1_SPK_PA_EN output */
242 gpio_output(GPIO(1, A, 2), 0);
243
244 rkclk_configure_i2s(12288000);
245}
246
Lin Huangb497b482016-03-31 18:44:13 +0800247static void configure_display(void)
248{
Julius Werner6486e782017-07-14 14:30:29 -0700249 if (IS_ENABLED(CONFIG_BOARD_GOOGLE_SCARLET)) {
250 gpio_output(GPIO(4, D, 1), 0); /* DISPLAY_RST_L */
251 gpio_output(GPIO(4, D, 3), 1); /* PPVARP_LCD */
252 mdelay(10);
253 gpio_output(GPIO(4, D, 4), 1); /* PPVARN_LCD */
254 mdelay(20 + 2); /* add 2ms for bias rise time */
255 gpio_output(GPIO(4, D, 1), 1); /* DISPLAY_RST_L */
256 mdelay(30);
257 } else {
258 /* set pinmux for edp HPD */
259 gpio_input_pulldown(GPIO(4, C, 7));
260 write32(&rk3399_grf->iomux_edp_hotplug, IOMUX_EDP_HOTPLUG);
Lin Huangb497b482016-03-31 18:44:13 +0800261
Julius Werner6486e782017-07-14 14:30:29 -0700262 gpio_output(GPIO(4, D, 3), 1); /* P3.3V_DISP */
263 }
Lin Huangb497b482016-03-31 18:44:13 +0800264}
265
Julius Wernerc49782c2016-11-21 20:14:18 -0800266static void usb_power_cycle(int port)
267{
268 if (google_chromeec_set_usb_pd_role(port, USB_PD_CTRL_ROLE_FORCE_SINK))
269 printk(BIOS_ERR, "ERROR: Cannot force USB%d PD sink\n", port);
270
271 mdelay(10); /* Make sure USB stick is fully depowered. */
272
273 if (google_chromeec_set_usb_pd_role(port, USB_PD_CTRL_ROLE_TOGGLE_ON))
274 printk(BIOS_ERR, "ERROR: Cannot restore USB%d PD mode\n", port);
275}
276
philipchen21b08522017-04-27 18:25:11 -0700277static void setup_usb(int port)
Liangfeng Wu76655cb2016-05-26 16:06:58 +0800278{
philipchen21b08522017-04-27 18:25:11 -0700279 /* Must be PHY0 or PHY1. */
280 assert(port == 0 || port == 1);
281
William wu605a87c2017-01-09 19:02:39 +0800282 /*
283 * A few magic PHY tuning values that improve eye diagram amplitude
284 * and make it extra sure we get reliable communication in firmware
285 * Set max ODT compensation voltage and current tuning reference.
286 */
philipchen21b08522017-04-27 18:25:11 -0700287 write32(&rk3399_grf->usbphy_ctrl[port][3], RK_CLRSETBITS(0xfff, 0x2e3));
William wu9f470b12016-11-10 19:34:45 +0800288
Caesar Wang9e588002017-02-10 11:16:13 +0800289 /* Set max pre-emphasis level on PHY0 and PHY1. */
philipchen21b08522017-04-27 18:25:11 -0700290 write32(&rk3399_grf->usbphy_ctrl[port][12],
Caesar Wang9e588002017-02-10 11:16:13 +0800291 RK_CLRSETBITS(0xffff, 0xa7));
William wu9f470b12016-11-10 19:34:45 +0800292
Caesar Wang9e588002017-02-10 11:16:13 +0800293 /*
William wuebbdd282017-01-23 20:54:22 +0800294 * 1. Disable the pre-emphasize in eop state and chirp
Caesar Wang9e588002017-02-10 11:16:13 +0800295 * state to avoid mis-trigger the disconnect detection
296 * and also avoid high-speed handshake fail for PHY0
297 * and PHY1 consist of otg-port and host-port.
William wuebbdd282017-01-23 20:54:22 +0800298 *
299 * 2. Configure PHY0 and PHY1 otg-ports squelch detection
300 * threshold to 125mV (default is 150mV).
Caesar Wang9e588002017-02-10 11:16:13 +0800301 */
philipchen21b08522017-04-27 18:25:11 -0700302 write32(&rk3399_grf->usbphy_ctrl[port][0],
William wuebbdd282017-01-23 20:54:22 +0800303 RK_CLRSETBITS(7 << 13 | 3 << 0, 6 << 13));
philipchen21b08522017-04-27 18:25:11 -0700304 write32(&rk3399_grf->usbphy_ctrl[port][13], RK_CLRBITS(3 << 0));
William wu9f470b12016-11-10 19:34:45 +0800305
Caesar Wang9e588002017-02-10 11:16:13 +0800306 /*
307 * ODT auto compensation bypass, and set max driver
308 * strength only for PHY0 and PHY1 otg-port.
309 */
philipchen21b08522017-04-27 18:25:11 -0700310 write32(&rk3399_grf->usbphy_ctrl[port][2],
Caesar Wang9e588002017-02-10 11:16:13 +0800311 RK_CLRSETBITS(0x7e << 4, 0x60 << 4));
William wu9f470b12016-11-10 19:34:45 +0800312
Caesar Wang9e588002017-02-10 11:16:13 +0800313 /*
314 * ODT auto refresh bypass, and set the max bias current
315 * tuning reference only for PHY0 and PHY1 otg-port.
316 */
philipchen21b08522017-04-27 18:25:11 -0700317 write32(&rk3399_grf->usbphy_ctrl[port][3],
Caesar Wang9e588002017-02-10 11:16:13 +0800318 RK_CLRSETBITS(0x21c, 1 << 4));
William wu605a87c2017-01-09 19:02:39 +0800319
Caesar Wang9e588002017-02-10 11:16:13 +0800320 /*
321 * ODT auto compensation bypass, and set default driver
322 * strength only for PHY0 and PHY1 host-port.
323 */
philipchen21b08522017-04-27 18:25:11 -0700324 write32(&rk3399_grf->usbphy_ctrl[port][15], RK_SETBITS(1 << 10));
William wu605a87c2017-01-09 19:02:39 +0800325
Caesar Wang9e588002017-02-10 11:16:13 +0800326 /* ODT auto refresh bypass only for PHY0 and PHY1 host-port. */
philipchen21b08522017-04-27 18:25:11 -0700327 write32(&rk3399_grf->usbphy_ctrl[port][16], RK_CLRBITS(1 << 9));
Julius Werner1c8491c2016-08-15 17:58:05 -0700328
philipchen21b08522017-04-27 18:25:11 -0700329 if (port == 0)
330 setup_usb_otg0();
331 else
332 setup_usb_otg1();
Julius Wernerc49782c2016-11-21 20:14:18 -0800333
334 /*
335 * Need to power-cycle USB ports for use in firmware, since some devices
336 * can't fall back to USB 2.0 after they saw SuperSpeed terminations.
337 * This takes about a dozen milliseconds, so only do it in boot modes
338 * that have firmware UI (which one could select USB boot from).
339 */
philipchen21b08522017-04-27 18:25:11 -0700340 if (display_init_required())
341 usb_power_cycle(port);
Liangfeng Wu76655cb2016-05-26 16:06:58 +0800342}
343
huang lina6dbfb52016-03-02 18:38:40 +0800344static void mainboard_init(device_t dev)
345{
Vadim Bendebury1e80ab32016-03-28 00:44:54 -0700346 configure_sdmmc();
Lin Huang2f7ed8d2016-04-08 18:56:20 +0800347 configure_emmc();
Xing Zheng96fbc312016-05-19 11:39:20 +0800348 configure_codec();
Julius Werner6486e782017-07-14 14:30:29 -0700349 if (display_init_required())
350 configure_display();
philipchen21b08522017-04-27 18:25:11 -0700351 setup_usb(0);
Julius Werner6486e782017-07-14 14:30:29 -0700352 if (!IS_ENABLED(CONFIG_BOARD_GOOGLE_SCARLET)) {
353 assert_wifi_reset(); /* Scarlet: no WIFI_PD# line */
354 configure_touchpad(); /* Scarlet: works differently */
355 setup_usb(1); /* Scarlet: only one USB port */
356 register_gpio_suspend(); /* Scarlet: all EC-controlled */
357 }
Lin Huang5a4be8a2016-05-17 15:45:53 +0800358 register_reset_to_bl31();
Lin Huang9a5c4fe2016-05-19 11:11:23 +0800359 register_poweroff_to_bl31();
Lin Huangc9fea5c2016-08-30 15:34:42 -0700360 register_apio_suspend();
Lin Huangb497b482016-03-31 18:44:13 +0800361}
362
Philip Chena304b692017-04-06 10:17:08 -0700363static void prepare_backlight_i2c(void)
Lin Huangb497b482016-03-31 18:44:13 +0800364{
Julius Werner7feb86b2016-09-02 11:25:56 -0700365 gpio_input(GPIO(1, B, 7)); /* I2C0_SDA remove pull_up */
366 gpio_input(GPIO(1, C, 0)); /* I2C0_SCL remove pull_up */
367
368 i2c_init(0, 100*KHz);
369
Lin Huangb497b482016-03-31 18:44:13 +0800370 write32(&rk3399_pmugrf->iomux_i2c0_sda, IOMUX_I2C0_SDA);
371 write32(&rk3399_pmugrf->iomux_i2c0_scl, IOMUX_I2C0_SCL);
Lin Huangb497b482016-03-31 18:44:13 +0800372}
373
374void mainboard_power_on_backlight(void)
375{
Vadim Bendeburyf1343df2016-05-22 15:53:37 -0700376 gpio_output(GPIO_BACKLIGHT, 1); /* BL_EN */
Lin Huangb497b482016-03-31 18:44:13 +0800377
Philip Chena304b692017-04-06 10:17:08 -0700378 if (IS_ENABLED(CONFIG_BOARD_GOOGLE_GRU))
379 prepare_backlight_i2c();
huang lina6dbfb52016-03-02 18:38:40 +0800380}
381
382static void mainboard_enable(device_t dev)
383{
384 dev->ops->init = &mainboard_init;
385}
386
387struct chip_operations mainboard_ops = {
388 .name = CONFIG_MAINBOARD_PART_NUMBER,
389 .enable_dev = mainboard_enable,
390};