a084f0a0a0db1303516173fe445c5c032fc270e8
[depthcharge.git] / src / board / oak / board.c
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but without any warranty; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <assert.h>
19 #include <libpayload.h>
20
21 #include "base/init_funcs.h"
22 #include "boot/ramoops.h"
23 #include "drivers/bus/i2c/mtk_i2c.h"
24 #include "drivers/bus/i2s/mt8173.h"
25 #include "drivers/bus/spi/mt8173.h"
26 #include "drivers/bus/usb/usb.h"
27 #include "drivers/ec/cros/ec.h"
28 #include "drivers/ec/cros/spi.h"
29 #include "drivers/flash/spi.h"
30 #include "drivers/gpio/sysinfo.h"
31 #include "drivers/gpio/mtk_gpio.h"
32 #include "drivers/power/mt6397.h"
33 #include "drivers/sound/i2s.h"
34 #include "drivers/sound/rt5645.h"
35 #include "drivers/sound/rt5677.h"
36 #include "drivers/storage/mtk_mmc.h"
37 #include "drivers/flash/mtk_nor_flash.h"
38 #include "drivers/tpm/slb9635_i2c.h"
39 #include "drivers/tpm/tpm.h"
40 #include "vboot/util/flag.h"
41
42 #include "drivers/video/display.h"
43 #include "drivers/video/mt8173_ddp.h"
44
45
46
47 int oak_backlight_update(DisplayOps *me, uint8_t enable)
48 {
49         static GpioOps *panel_lcd_power_en, *disp_pwm0, *panel_power_en;
50
51         if (!panel_power_en) {
52                 switch (lib_sysinfo.board_id + CONFIG_BOARD_ID_ADJUSTMENT) {
53                 case 1:
54                 case 2:
55                         panel_lcd_power_en = NULL;
56                         break;
57                 case 3:
58                         panel_lcd_power_en = new_mtk_gpio_output(PAD_UCTS2);
59                         break;
60                 case 4:
61                         panel_lcd_power_en = new_mtk_gpio_output(PAD_SRCLKENAI);
62                         break;
63                 default:
64                         panel_lcd_power_en = new_mtk_gpio_output(PAD_UTXD2);
65                 }
66
67                 disp_pwm0 = new_mtk_gpio_output(PAD_DISP_PWM0);
68                 panel_power_en = new_mtk_gpio_output(PAD_PCM_TX);
69         }
70
71         if (enable) {
72                 if (panel_lcd_power_en) {
73                         panel_lcd_power_en->set(panel_lcd_power_en, 1);
74                         mdelay(1);
75                 }
76
77                 disp_pwm0->set(disp_pwm0, 1);
78                 panel_power_en->set(panel_power_en, 1);
79         } else {
80                 panel_power_en->set(panel_power_en, 0);
81                 disp_pwm0->set(disp_pwm0, 0);
82                 if (panel_lcd_power_en)
83                         panel_lcd_power_en->set(panel_lcd_power_en, 0);
84         }
85
86         return 0;
87 }
88
89 static int sound_setup(void)
90 {
91         MtkI2s *i2s0 = new_mtk_i2s(0x11220000, 2, 48000);
92         I2sSource *i2s_source = new_i2s_source(&i2s0->ops, 48000, 2, 16000);
93         SoundRoute *sound_route = new_sound_route(&i2s_source->ops);
94         MTKI2c *i2c0 = new_mtk_i2c(0x11007000, 0x11000100);
95         rt5645Codec *rt5645 = new_rt5645_codec(&i2c0->ops, 0x1a);
96
97         list_insert_after(&rt5645->component.list_node, &sound_route->components);
98
99         if (lib_sysinfo.board_id + CONFIG_BOARD_ID_ADJUSTMENT < 5) {
100                 rt5677Codec *rt5677 = new_rt5677_codec(&i2c0->ops, 0x2c, 16,
101                                                        48000, 256, 0, 1);
102                 list_insert_after(&rt5677->component.list_node,
103                                   &sound_route->components);
104         }
105
106         /*
107          * Realtek codecs need SoC's I2S on before codecs are enabled
108          * or codecs' PLL will be in wrong state. Make i2s a route component
109          * so it can be enabled before codecs during route_enable_components()
110          * by inserting i2s before codec nodes
111          */
112         list_insert_after(&i2s0->component.list_node, &sound_route->components);
113         sound_set_ops(&sound_route->ops);
114         return 0;
115 }
116
117 static int board_setup(void)
118 {
119         MtkMmcTuneReg emmc_tune_reg = {.msdc_iocon = 0, .pad_tune = 0x10 << 16};
120         MtkMmcTuneReg sd_card_tune_reg = {.msdc_iocon = 0, .pad_tune = 0};
121
122         sysinfo_install_flags(new_mtk_gpio_input);
123
124         MTKI2c *i2c2 = new_mtk_i2c(0x11009000, 0x11000200);
125         tpm_set_ops(&new_slb9635_i2c(&i2c2->ops, 0x20)->base.ops);
126
127         MtkSpi *spibus = new_mtk_spi(0x1100A000);
128         CrosEcSpiBus *cros_ec_spi_bus = new_cros_ec_spi_bus(&spibus->ops);
129         GpioOps *ec_int = sysinfo_lookup_gpio("EC interrupt", 1,
130                                               new_mtk_gpio_input);
131         CrosEc *cros_ec = new_cros_ec(&cros_ec_spi_bus->ops, 0, ec_int);
132         CrosEc *cros_pd = new_cros_ec(&cros_ec_spi_bus->ops, 1, NULL);
133         register_vboot_ec(&cros_ec->vboot, 0);
134         register_vboot_ec(&cros_pd->vboot, 1);
135
136         Mt6397Pmic *pmic = new_mt6397_power(0x1000D000, 0x10007000);
137         power_set_ops(&pmic->ops);
138
139         MtkMmcHost *emmc = new_mtk_mmc_host(0x11230000, 200 * MHz, 50 * MHz,
140                                             emmc_tune_reg, 8, 0, NULL);
141         GpioOps *card_detect_ops = new_gpio_not(new_mtk_gpio_input(PAD_EINT1));
142         MtkMmcHost *sd_card = new_mtk_mmc_host(0x11240000, 200 * MHz, 25 * MHz,
143                                                sd_card_tune_reg, 4, 1,
144                                                card_detect_ops);
145
146         list_insert_after(&emmc->mmc.ctrlr.list_node,
147                           &fixed_block_dev_controllers);
148         list_insert_after(&sd_card->mmc.ctrlr.list_node,
149                           &removable_block_dev_controllers);
150
151         /* Set display ops */
152         if (lib_sysinfo.framebuffer)
153                 display_set_ops(new_mt8173_display(oak_backlight_update));
154
155         UsbHostController *usb_host = new_usb_hc(XHCI, 0x11270000);
156
157         list_insert_after(&usb_host->list_node, &usb_host_controllers);
158
159         sound_setup();
160
161         ramoops_buffer(0xb1f00000, 0x100000, 0x20000);
162
163         /* Setup Nor flash ops */
164         MtkNorFlash *nor_flash = new_mtk_nor_flash(0x1100D000);
165         flash_set_ops(&nor_flash->ops);
166
167         return 0;
168 }
169
170 INIT_FUNC(board_setup);