common: Drop init.h from common header
[platform/kernel/u-boot.git] / board / toradex / apalis-tk1 / apalis-tk1.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2016-2018 Toradex, Inc.
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <init.h>
9 #include <asm/arch-tegra/ap.h>
10 #include <asm/gpio.h>
11 #include <asm/io.h>
12 #include <asm/arch/gpio.h>
13 #include <asm/arch/pinmux.h>
14 #include <env_internal.h>
15 #include <pci_tegra.h>
16 #include <power/as3722.h>
17 #include <power/pmic.h>
18
19 #include "../common/tdx-common.h"
20 #include "pinmux-config-apalis-tk1.h"
21
22 #define LAN_DEV_OFF_N   TEGRA_GPIO(O, 6)
23 #define LAN_RESET_N     TEGRA_GPIO(S, 2)
24 #define FAN_EN          TEGRA_GPIO(DD, 2)
25 #define LAN_WAKE_N      TEGRA_GPIO(O, 5)
26 #ifdef CONFIG_APALIS_TK1_PCIE_EVALBOARD_INIT
27 #define PEX_PERST_N     TEGRA_GPIO(DD, 1) /* Apalis GPIO7 */
28 #define RESET_MOCI_CTRL TEGRA_GPIO(U, 4)
29 #endif /* CONFIG_APALIS_TK1_PCIE_EVALBOARD_INIT */
30 #define VCC_USBH        TEGRA_GPIO(T, 6)
31 #define VCC_USBH_V1_0   TEGRA_GPIO(N, 5)
32 #define VCC_USBO1       TEGRA_GPIO(T, 5)
33 #define VCC_USBO1_V1_0  TEGRA_GPIO(N, 4)
34
35 int arch_misc_init(void)
36 {
37         if (readl(NV_PA_BASE_SRAM + NVBOOTINFOTABLE_BOOTTYPE) ==
38             NVBOOTTYPE_RECOVERY)
39                 printf("USB recovery mode\n");
40
41         /* PCB Version Indication: V1.2 and later have GPIO_PV0 wired to GND */
42         gpio_request(TEGRA_GPIO(V, 0), "PCB Version Indication");
43         gpio_direction_input(TEGRA_GPIO(V, 0));
44         if (gpio_get_value(TEGRA_GPIO(V, 0))) {
45                 /*
46                  * if using the default device tree for new V1.2 and later HW,
47                  * use version for older V1.0 and V1.1 HW
48                  */
49                 char *fdt_env = env_get("fdt_module");
50
51                 if (fdt_env && !strcmp(FDT_MODULE, fdt_env)) {
52                         env_set("fdt_module", FDT_MODULE_V1_0);
53                         printf("patching fdt_module to " FDT_MODULE_V1_0
54                                " for older V1.0 and V1.1 HW\n");
55 #ifndef CONFIG_ENV_IS_NOWHERE
56                         env_save();
57 #endif
58                 }
59
60                 /* activate USB power enable GPIOs */
61                 gpio_request(VCC_USBH_V1_0, "VCC_USBH");
62                 gpio_direction_output(VCC_USBH_V1_0, 1);
63                 gpio_request(VCC_USBO1_V1_0, "VCC_USBO1");
64                 gpio_direction_output(VCC_USBO1_V1_0, 1);
65         } else {
66                 /* activate USB power enable GPIOs */
67                 gpio_request(VCC_USBH, "VCC_USBH");
68                 gpio_direction_output(VCC_USBH, 1);
69                 gpio_request(VCC_USBO1, "VCC_USBO1");
70                 gpio_direction_output(VCC_USBO1, 1);
71         }
72
73         return 0;
74 }
75
76 int checkboard(void)
77 {
78         puts("Model: Toradex Apalis TK1 2GB\n");
79
80         return 0;
81 }
82
83 #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
84 int ft_board_setup(void *blob, bd_t *bd)
85 {
86         return ft_common_board_setup(blob, bd);
87 }
88 #endif
89
90 /*
91  * Routine: pinmux_init
92  * Description: Do individual peripheral pinmux configs
93  */
94 void pinmux_init(void)
95 {
96         pinmux_clear_tristate_input_clamping();
97
98         gpio_config_table(apalis_tk1_gpio_inits,
99                           ARRAY_SIZE(apalis_tk1_gpio_inits));
100
101         pinmux_config_pingrp_table(apalis_tk1_pingrps,
102                                    ARRAY_SIZE(apalis_tk1_pingrps));
103
104         pinmux_config_drvgrp_table(apalis_tk1_drvgrps,
105                                    ARRAY_SIZE(apalis_tk1_drvgrps));
106 }
107
108 #ifdef CONFIG_PCI_TEGRA
109 /* TODO: Convert to driver model */
110 static int as3722_sd_enable(struct udevice *pmic, unsigned int sd)
111 {
112         int err;
113
114         if (sd > 6)
115                 return -EINVAL;
116
117         err = pmic_clrsetbits(pmic, AS3722_SD_CONTROL, 0, 1 << sd);
118         if (err) {
119                 pr_err("failed to update SD control register: %d", err);
120                 return err;
121         }
122
123         return 0;
124 }
125
126 /* TODO: Convert to driver model */
127 static int as3722_ldo_enable(struct udevice *pmic, unsigned int ldo)
128 {
129         int err;
130         u8 ctrl_reg = AS3722_LDO_CONTROL0;
131
132         if (ldo > 11)
133                 return -EINVAL;
134
135         if (ldo > 7) {
136                 ctrl_reg = AS3722_LDO_CONTROL1;
137                 ldo -= 8;
138         }
139
140         err = pmic_clrsetbits(pmic, ctrl_reg, 0, 1 << ldo);
141         if (err) {
142                 pr_err("failed to update LDO control register: %d", err);
143                 return err;
144         }
145
146         return 0;
147 }
148
149 int tegra_pcie_board_init(void)
150 {
151         struct udevice *dev;
152         int ret;
153
154         ret = uclass_get_device_by_driver(UCLASS_PMIC,
155                                           DM_GET_DRIVER(pmic_as3722), &dev);
156         if (ret) {
157                 pr_err("failed to find AS3722 PMIC: %d\n", ret);
158                 return ret;
159         }
160
161         ret = as3722_sd_enable(dev, 4);
162         if (ret < 0) {
163                 pr_err("failed to enable SD4: %d\n", ret);
164                 return ret;
165         }
166
167         ret = as3722_sd_set_voltage(dev, 4, 0x24);
168         if (ret < 0) {
169                 pr_err("failed to set SD4 voltage: %d\n", ret);
170                 return ret;
171         }
172
173         gpio_request(LAN_DEV_OFF_N, "LAN_DEV_OFF_N");
174         gpio_request(LAN_RESET_N, "LAN_RESET_N");
175         gpio_request(LAN_WAKE_N, "LAN_WAKE_N");
176
177 #ifdef CONFIG_APALIS_TK1_PCIE_EVALBOARD_INIT
178         gpio_request(PEX_PERST_N, "PEX_PERST_N");
179         gpio_request(RESET_MOCI_CTRL, "RESET_MOCI_CTRL");
180 #endif /* CONFIG_APALIS_TK1_PCIE_EVALBOARD_INIT */
181
182         return 0;
183 }
184
185 void tegra_pcie_board_port_reset(struct tegra_pcie_port *port)
186 {
187         int index = tegra_pcie_port_index_of_port(port);
188
189         if (index == 1) { /* I210 Gigabit Ethernet Controller (On-module) */
190                 struct udevice *dev;
191                 int ret;
192
193                 ret = uclass_get_device_by_driver(UCLASS_PMIC,
194                                                   DM_GET_DRIVER(pmic_as3722),
195                                                   &dev);
196                 if (ret) {
197                         debug("%s: Failed to find PMIC\n", __func__);
198                         return;
199                 }
200
201                 /* Reset I210 Gigabit Ethernet Controller */
202                 gpio_direction_output(LAN_RESET_N, 0);
203
204                 /*
205                  * Make sure we don't get any back feeding from DEV_OFF_N resp.
206                  * LAN_WAKE_N
207                  */
208                 gpio_direction_output(LAN_DEV_OFF_N, 0);
209                 gpio_direction_output(LAN_WAKE_N, 0);
210
211                 /* Make sure LDO9 and LDO10 are initially enabled @ 0V */
212                 ret = as3722_ldo_enable(dev, 9);
213                 if (ret < 0) {
214                         pr_err("failed to enable LDO9: %d\n", ret);
215                         return;
216                 }
217                 ret = as3722_ldo_enable(dev, 10);
218                 if (ret < 0) {
219                         pr_err("failed to enable LDO10: %d\n", ret);
220                         return;
221                 }
222                 ret = as3722_ldo_set_voltage(dev, 9, 0x80);
223                 if (ret < 0) {
224                         pr_err("failed to set LDO9 voltage: %d\n", ret);
225                         return;
226                 }
227                 ret = as3722_ldo_set_voltage(dev, 10, 0x80);
228                 if (ret < 0) {
229                         pr_err("failed to set LDO10 voltage: %d\n", ret);
230                         return;
231                 }
232
233                 /* Make sure controller gets enabled by disabling DEV_OFF_N */
234                 gpio_set_value(LAN_DEV_OFF_N, 1);
235
236                 /*
237                  * Enable LDO9 and LDO10 for +V3.3_ETH on patched prototype
238                  * V1.0A and sample V1.0B and newer modules
239                  */
240                 ret = as3722_ldo_set_voltage(dev, 9, 0xff);
241                 if (ret < 0) {
242                         pr_err("failed to set LDO9 voltage: %d\n", ret);
243                         return;
244                 }
245                 ret = as3722_ldo_set_voltage(dev, 10, 0xff);
246                 if (ret < 0) {
247                         pr_err("failed to set LDO10 voltage: %d\n", ret);
248                         return;
249                 }
250
251                 /*
252                  * Must be asserted for 100 ms after power and clocks are stable
253                  */
254                 mdelay(100);
255
256                 gpio_set_value(LAN_RESET_N, 1);
257         } else if (index == 0) { /* Apalis PCIe */
258 #ifdef CONFIG_APALIS_TK1_PCIE_EVALBOARD_INIT
259                 /*
260                  * Reset PLX PEX 8605 PCIe Switch plus PCIe devices on Apalis
261                  * Evaluation Board
262                  */
263                 gpio_direction_output(PEX_PERST_N, 0);
264                 gpio_direction_output(RESET_MOCI_CTRL, 0);
265
266                 /*
267                  * Must be asserted for 100 ms after power and clocks are stable
268                  */
269                 mdelay(100);
270
271                 gpio_set_value(PEX_PERST_N, 1);
272                 /*
273                  * Err_5: PEX_REFCLK_OUTpx/nx Clock Outputs is not Guaranteed
274                  * Until 900 us After PEX_PERST# De-assertion
275                  */
276                 mdelay(1);
277                 gpio_set_value(RESET_MOCI_CTRL, 1);
278 #endif /* CONFIG_APALIS_TK1_PCIE_EVALBOARD_INIT */
279         }
280 }
281 #endif /* CONFIG_PCI_TEGRA */
282
283 /*
284  * Enable/start PWM CPU fan
285  */
286 void start_cpu_fan(void)
287 {
288         gpio_request(FAN_EN, "FAN_EN");
289         gpio_direction_output(FAN_EN, 1);
290 }
291
292 /*
293  * Backlight off before OS handover
294  */
295 void board_preboot_os(void)
296 {
297         gpio_request(TEGRA_GPIO(BB, 5), "BL_ON");
298         gpio_direction_output(TEGRA_GPIO(BB, 5), 0);
299 }