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