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