imx: add imx8x capricorn giedi board
[platform/kernel/u-boot.git] / board / siemens / capricorn / board.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2017-2019 NXP
4  *
5  * Copyright 2019 Siemens AG
6  *
7  */
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <netdev.h>
12 #include <env_internal.h>
13 #include <fsl_esdhc_imx.h>
14 #include <i2c.h>
15 #include <led.h>
16 #include <pca953x.h>
17 #include <power-domain.h>
18 #include <asm/gpio.h>
19 #include <asm/arch/imx8-pins.h>
20 #include <asm/arch/iomux.h>
21 #include <asm/arch/sci/sci.h>
22 #include <asm/arch/sys_proto.h>
23 #ifndef CONFIG_SPL
24 #include <asm/arch-imx8/clock.h>
25 #endif
26 #include "../common/factoryset.h"
27
28 #define GPIO_PAD_CTRL \
29                 ((SC_PAD_CONFIG_NORMAL << PADRING_CONFIG_SHIFT) | \
30                  (SC_PAD_ISO_OFF << PADRING_LPCONFIG_SHIFT) | \
31                  (SC_PAD_28FDSOI_DSE_DV_HIGH << PADRING_DSE_SHIFT) | \
32                  (SC_PAD_28FDSOI_PS_PU << PADRING_PULL_SHIFT))
33
34 #define ENET_NORMAL_PAD_CTRL \
35                 ((SC_PAD_CONFIG_NORMAL << PADRING_CONFIG_SHIFT) | \
36                  (SC_PAD_ISO_OFF << PADRING_LPCONFIG_SHIFT) | \
37                  (SC_PAD_28FDSOI_DSE_18V_10MA << PADRING_DSE_SHIFT) | \
38                  (SC_PAD_28FDSOI_PS_PU << PADRING_PULL_SHIFT))
39
40 #define UART_PAD_CTRL \
41                 ((SC_PAD_CONFIG_OUT_IN << PADRING_CONFIG_SHIFT) | \
42                  (SC_PAD_ISO_OFF << PADRING_LPCONFIG_SHIFT) | \
43                  (SC_PAD_28FDSOI_DSE_DV_HIGH << PADRING_DSE_SHIFT) | \
44                  (SC_PAD_28FDSOI_PS_PU << PADRING_PULL_SHIFT))
45
46 static iomux_cfg_t uart2_pads[] = {
47         SC_P_UART2_RX | MUX_PAD_CTRL(UART_PAD_CTRL),
48         SC_P_UART2_TX | MUX_PAD_CTRL(UART_PAD_CTRL),
49 };
50
51 static void setup_iomux_uart(void)
52 {
53         imx8_iomux_setup_multiple_pads(uart2_pads, ARRAY_SIZE(uart2_pads));
54 }
55
56 int board_early_init_f(void)
57 {
58         /* Set UART clock root to 80 MHz */
59         sc_pm_clock_rate_t rate = SC_80MHZ;
60         int ret;
61
62         ret = sc_pm_setup_uart(SC_R_UART_0, rate);
63         ret |= sc_pm_setup_uart(SC_R_UART_2, rate);
64         if (ret)
65                 return ret;
66
67         setup_iomux_uart();
68
69         return 0;
70 }
71
72 #define ENET_PHY_RESET  IMX_GPIO_NR(0, 3)
73 #define ENET_TEST_1     IMX_GPIO_NR(0, 8)
74 #define ENET_TEST_2     IMX_GPIO_NR(0, 9)
75
76 /*#define ETH_IO_TEST*/
77 static iomux_cfg_t enet_reset[] = {
78         SC_P_ESAI0_SCKT | MUX_MODE_ALT(4) | MUX_PAD_CTRL(GPIO_PAD_CTRL),
79 #ifdef ETH_IO_TEST
80         /* GPIO0.IO08 MODE3: TXD0 */
81         SC_P_ESAI0_TX4_RX1 | MUX_MODE_ALT(4) |
82         MUX_PAD_CTRL(ENET_NORMAL_PAD_CTRL),
83         /* GPIO0.IO09 MODE3: TXD1 */
84         SC_P_ESAI0_TX5_RX0 | MUX_MODE_ALT(4) |
85         MUX_PAD_CTRL(ENET_NORMAL_PAD_CTRL),
86 #endif
87 };
88
89 static void enet_device_phy_reset(void)
90 {
91         int ret = 0;
92
93         imx8_iomux_setup_multiple_pads(enet_reset, ARRAY_SIZE(enet_reset));
94
95         ret = gpio_request(ENET_PHY_RESET, "enet_phy_reset");
96         if (!ret) {
97                 gpio_direction_output(ENET_PHY_RESET, 1);
98                 gpio_set_value(ENET_PHY_RESET, 0);
99                 /* SMSC9303 TRM chapter 14.5.2 */
100                 udelay(200);
101                 gpio_set_value(ENET_PHY_RESET, 1);
102         } else {
103                 printf("ENET RESET failed!\n");
104         }
105
106 #ifdef ETH_IO_TEST
107         ret =  gpio_request(ENET_TEST_1, "enet_test1");
108         if (!ret) {
109                 int i;
110
111                 printf("ENET TEST 1!\n");
112                 for (i = 0; i < 20; i++) {
113                         gpio_direction_output(ENET_TEST_1, 1);
114                         gpio_set_value(ENET_TEST_1, 0);
115                         udelay(50);
116                         gpio_set_value(ENET_TEST_1, 1);
117                         udelay(50);
118                 }
119                 gpio_free(ENET_TEST_1);
120         } else {
121                 printf("GPIO for ENET TEST 1 failed!\n");
122         }
123         ret =  gpio_request(ENET_TEST_2, "enet_test2");
124         if (!ret) {
125                 int i;
126
127                 printf("ENET TEST 2!\n");
128                 for (i = 0; i < 20; i++) {
129                         gpio_direction_output(ENET_TEST_2, 1);
130                         gpio_set_value(ENET_TEST_2, 0);
131                         udelay(50);
132                         gpio_set_value(ENET_TEST_2, 1);
133                         udelay(50);
134                 }
135                 gpio_free(ENET_TEST_2);
136         } else {
137                 printf("GPIO for ENET TEST 2 failed!\n");
138         }
139 #endif
140 }
141
142 int setup_gpr_fec(void)
143 {
144         sc_ipc_t ipc_handle = -1;
145         sc_err_t err = 0;
146         unsigned int test;
147
148         /*
149          * TX_CLK_SEL: it controls a mux between clock coming from the pad 50M
150          * input pin and clock generated internally to connectivity subsystem
151          *      0: internal clock
152          *      1: external clock --->  your choice for RMII
153          *
154          * CLKDIV_SEL: it controls a div by 2 on the internal clock path à
155          *      it should be don’t care when using external clock
156          *      0: non-divided clock
157          *      1: clock divided by 2
158          * 50_DISABLE or 125_DISABLE:
159          *      it’s used to disable the clock tree going outside the chip
160          *      when reference clock is generated internally.
161          *      It should be don’t care when reference clock is provided
162          *      externally.
163          *      0: clock is enabled
164          *      1: clock is disabled
165          *
166          * SC_C_TXCLK           = 24,
167          * SC_C_CLKDIV          = 25,
168          * SC_C_DISABLE_50      = 26,
169          * SC_C_DISABLE_125     = 27,
170          */
171
172         err = sc_misc_set_control(ipc_handle, SC_R_ENET_1, SC_C_TXCLK, 1);
173         if (err != SC_ERR_NONE)
174                 printf("Error in setting up SC_C %d\n\r", SC_C_TXCLK);
175
176         sc_misc_get_control(ipc_handle, SC_R_ENET_1, SC_C_TXCLK, &test);
177         debug("TEST SC_C %d-->%d\n\r", SC_C_TXCLK, test);
178
179         err = sc_misc_set_control(ipc_handle, SC_R_ENET_1, SC_C_CLKDIV, 0);
180         if (err != SC_ERR_NONE)
181                 printf("Error in setting up SC_C %d\n\r", SC_C_CLKDIV);
182
183         sc_misc_get_control(ipc_handle, SC_R_ENET_1, SC_C_CLKDIV, &test);
184         debug("TEST SC_C %d-->%d\n\r", SC_C_CLKDIV, test);
185
186         err = sc_misc_set_control(ipc_handle, SC_R_ENET_1, SC_C_DISABLE_50, 0);
187         if (err != SC_ERR_NONE)
188                 printf("Error in setting up SC_C %d\n\r", SC_C_DISABLE_50);
189
190         sc_misc_get_control(ipc_handle, SC_R_ENET_1, SC_C_TXCLK, &test);
191         debug("TEST SC_C %d-->%d\n\r", SC_C_DISABLE_50, test);
192
193         err = sc_misc_set_control(ipc_handle, SC_R_ENET_1, SC_C_DISABLE_125, 1);
194         if (err != SC_ERR_NONE)
195                 printf("Error in setting up SC_C %d\n\r", SC_C_DISABLE_125);
196
197         sc_misc_get_control(ipc_handle, SC_R_ENET_1, SC_C_TXCLK, &test);
198         debug("TEST SC_C %d-->%d\n\r", SC_C_DISABLE_125, test);
199
200         err = sc_misc_set_control(ipc_handle, SC_R_ENET_1, SC_C_SEL_125, 1);
201         if (err != SC_ERR_NONE)
202                 printf("Error in setting up SC_C %d\n\r", SC_C_SEL_125);
203
204         sc_misc_get_control(ipc_handle, SC_R_ENET_1, SC_C_SEL_125, &test);
205         debug("TEST SC_C %d-->%d\n\r", SC_C_SEL_125, test);
206
207         return 0;
208 }
209
210 #if IS_ENABLED(CONFIG_FEC_MXC)
211 #include <miiphy.h>
212 int board_phy_config(struct phy_device *phydev)
213 {
214         if (phydev->drv->config)
215                 phydev->drv->config(phydev);
216
217         return 0;
218 }
219
220 #endif
221
222 static int setup_fec(void)
223 {
224         setup_gpr_fec();
225         /* Reset ENET PHY */
226         enet_device_phy_reset();
227         return 0;
228 }
229
230 void reset_cpu(ulong addr)
231 {
232 }
233
234 #ifndef CONFIG_SPL_BUILD
235 /* LED's */
236 static int board_led_init(void)
237 {
238         struct udevice *bus, *dev;
239         u8 pca_led[2] = { 0x00, 0x00 };
240         int ret;
241
242         /* init all GPIO LED's */
243         if (IS_ENABLED(CONFIG_LED))
244                 led_default_state();
245
246         /* enable all leds on PCA9552 */
247         ret = uclass_get_device_by_seq(UCLASS_I2C, PCA9552_1_I2C_BUS, &bus);
248         if (ret) {
249                 printf("ERROR: I2C get %d\n", ret);
250                 return ret;
251         }
252
253         ret = dm_i2c_probe(bus, PCA9552_1_I2C_ADDR, 0, &dev);
254         if (ret) {
255                 printf("ERROR: PCA9552 probe failed\n");
256                 return ret;
257         }
258
259         ret = dm_i2c_write(dev, 0x16, pca_led, sizeof(pca_led));
260         if (ret) {
261                 printf("ERROR: PCA9552 write failed\n");
262                 return ret;
263         }
264
265         mdelay(1);
266         return ret;
267 }
268 #endif /* !CONFIG_SPL_BUILD */
269
270 int checkboard(void)
271 {
272         puts("Board: Capricorn\n");
273
274         /*
275          * Running build_info() doesn't work with current SCFW blob.
276          * Uncomment below call when new blob is available.
277          */
278         /*build_info();*/
279
280         print_bootinfo();
281         return 0;
282 }
283
284 int board_init(void)
285 {
286         setup_fec();
287         return 0;
288 }
289
290 #ifdef CONFIG_OF_BOARD_SETUP
291 int ft_board_setup(void *blob, bd_t *bd)
292 {
293         return 0;
294 }
295 #endif
296
297 int board_mmc_get_env_dev(int devno)
298 {
299         return devno;
300 }
301
302 static int check_mmc_autodetect(void)
303 {
304         char *autodetect_str = env_get("mmcautodetect");
305
306         if (autodetect_str && (strcmp(autodetect_str, "yes") == 0))
307                 return 1;
308
309         return 0;
310 }
311
312 /* This should be defined for each board */
313 __weak int mmc_map_to_kernel_blk(int dev_no)
314 {
315         return dev_no;
316 }
317
318 void board_late_mmc_env_init(void)
319 {
320         char cmd[32];
321         char mmcblk[32];
322         u32 dev_no = mmc_get_env_dev();
323
324         if (!check_mmc_autodetect())
325                 return;
326
327         env_set_ulong("mmcdev", dev_no);
328
329         /* Set mmcblk env */
330         sprintf(mmcblk, "/dev/mmcblk%dp2 rootwait rw",
331                 mmc_map_to_kernel_blk(dev_no));
332         env_set("mmcroot", mmcblk);
333
334         sprintf(cmd, "mmc dev %d", dev_no);
335         run_command(cmd, 0);
336 }
337
338 #ifndef CONFIG_SPL_BUILD
339 int factoryset_read_eeprom(int i2c_addr);
340
341 static int load_parameters_from_factoryset(void)
342 {
343         int ret;
344
345         ret = factoryset_read_eeprom(EEPROM_I2C_ADDR);
346         if (ret)
347                 return ret;
348
349         return factoryset_env_set();
350 }
351
352 int board_late_init(void)
353 {
354         env_set("sec_boot", "no");
355 #ifdef CONFIG_AHAB_BOOT
356         env_set("sec_boot", "yes");
357 #endif
358
359 #ifdef CONFIG_ENV_IS_IN_MMC
360         board_late_mmc_env_init();
361 #endif
362         /* Init LEDs */
363         if (board_led_init())
364                 printf("I2C LED init failed\n");
365
366         /* Set environment from factoryset */
367         if (load_parameters_from_factoryset())
368                 printf("Loading factoryset parameters failed!\n");
369
370         return 0;
371 }
372
373 /* Service button */
374 #define MAX_PIN_NUMBER                  128
375 #define BOARD_DEFAULT_BUTTON_GPIO       IMX_GPIO_NR(1, 31)
376
377 unsigned char get_button_state(char * const envname, unsigned char def)
378 {
379         int button = 0;
380         int gpio;
381         char *ptr_env;
382
383         /* If button is not found we take default */
384         ptr_env = env_get(envname);
385         if (!ptr_env) {
386                 printf("Using default: %u\n", def);
387                 gpio = def;
388         } else {
389                 gpio = (unsigned char)simple_strtoul(ptr_env, NULL, 0);
390                 if (gpio > MAX_PIN_NUMBER)
391                         gpio = def;
392         }
393
394         gpio_request(gpio, "");
395         gpio_direction_input(gpio);
396         if (gpio_get_value(gpio))
397                 button = 1;
398         else
399                 button = 0;
400
401         gpio_free(gpio);
402
403         return button;
404 }
405
406 /*
407  * This command returns the status of the user button on
408  * Input - none
409  * Returns -    1 if button is held down
410  *              0 if button is not held down
411  */
412 static int
413 do_userbutton(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
414 {
415         int button = 0;
416
417         button = get_button_state("button_usr1", BOARD_DEFAULT_BUTTON_GPIO);
418
419         if (argc > 1)
420                 printf("Button state: %u\n", button);
421
422         return button;
423 }
424
425 U_BOOT_CMD(
426         usrbutton, CONFIG_SYS_MAXARGS, 2, do_userbutton,
427         "Return the status of user button",
428         "[print]"
429 );
430
431 #define ERST    IMX_GPIO_NR(0, 3)
432
433 static int
434 do_eth_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
435 {
436         gpio_request(ERST, "ERST");
437         gpio_direction_output(ERST, 0);
438         udelay(200);
439         gpio_set_value(ERST, 1);
440         return 0;
441 }
442
443 U_BOOT_CMD(
444         switch_rst, CONFIG_SYS_MAXARGS, 2, do_eth_reset,
445         "Reset eth phy",
446         "[print]"
447 );
448 #endif /* ! CONFIG_SPL_BUILD */