env: Move env_set() to env.h
[oweals/u-boot.git] / board / liebherr / display5 / display5.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 DENX Software Engineering
4  * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <asm/io.h>
10 #include <asm/arch/clock.h>
11 #include <asm/arch/imx-regs.h>
12 #include <asm/arch/iomux.h>
13 #include <asm/arch/mx6-pins.h>
14 #include <asm/arch/mx6-ddr.h>
15 #include <asm/arch/sys_proto.h>
16 #include <env.h>
17 #include <errno.h>
18 #include <asm/gpio.h>
19 #include <malloc.h>
20 #include <asm/mach-imx/iomux-v3.h>
21 #include <asm/mach-imx/mxc_i2c.h>
22 #include <asm/mach-imx/boot_mode.h>
23 #include <asm/mach-imx/spi.h>
24 #include <mmc.h>
25 #include <fsl_esdhc_imx.h>
26 #include <miiphy.h>
27 #include <netdev.h>
28 #include <i2c.h>
29 #include <environment.h>
30
31 #include <dm.h>
32 #include <dm/platform_data/serial_mxc.h>
33 #include <dm/platdata.h>
34
35 #ifndef CONFIG_MXC_SPI
36 #error "CONFIG_SPI must be set for this board"
37 #error "Please check your config file"
38 #endif
39
40 #include "common.h"
41
42 DECLARE_GLOBAL_DATA_PTR;
43
44 static bool hw_ids_valid;
45 static bool sw_ids_valid;
46 static u32 cpu_id;
47 static u32 unit_id;
48
49 #define EM_PAD IMX_GPIO_NR(3, 29)
50 #define SW0     IMX_GPIO_NR(2, 4)
51 #define SW1     IMX_GPIO_NR(2, 5)
52 #define SW2     IMX_GPIO_NR(2, 6)
53 #define SW3     IMX_GPIO_NR(2, 7)
54 #define HW0     IMX_GPIO_NR(6, 7)
55 #define HW1     IMX_GPIO_NR(6, 9)
56 #define HW2     IMX_GPIO_NR(6, 10)
57 #define HW3     IMX_GPIO_NR(6, 11)
58 #define HW4     IMX_GPIO_NR(4, 7)
59 #define HW5     IMX_GPIO_NR(4, 11)
60 #define HW6     IMX_GPIO_NR(4, 13)
61 #define HW7     IMX_GPIO_NR(4, 15)
62
63 int gpio_table_sw_ids[] = {
64         SW0, SW1, SW2, SW3
65 };
66
67 const char *gpio_table_sw_ids_names[] = {
68         "sw0", "sw1", "sw2", "sw3"
69 };
70
71 int gpio_table_hw_ids[] = {
72         HW0, HW1, HW2, HW3, HW4, HW5, HW6, HW7
73 };
74
75 const char *gpio_table_hw_ids_names[] = {
76         "hw0", "hw1", "hw2", "hw3", "hw4", "hw5", "hw6", "hw7"
77 };
78
79 static int get_board_id(int *ids, const char **c, int size,
80                         bool *valid, u32 *id)
81 {
82         int i, ret, val;
83
84         *valid = false;
85
86         for (i = 0; i < size; i++) {
87                 ret = gpio_request(ids[i], c[i]);
88                 if (ret) {
89                         printf("Can't request SWx gpios\n");
90                         return ret;
91                 }
92         }
93
94         for (i = 0; i < size; i++) {
95                 ret = gpio_direction_input(ids[i]);
96                 if (ret) {
97                         printf("Can't set SWx gpios direction\n");
98                         return ret;
99                 }
100         }
101
102         for (i = 0; i < size; i++) {
103                 val = gpio_get_value(ids[i]);
104                 if (val < 0) {
105                         printf("Can't get SW%d ID\n", i);
106                         *id = 0;
107                         return val;
108                 }
109                 *id |= val << i;
110         }
111         *valid = true;
112
113         return 0;
114 }
115
116 int dram_init(void)
117 {
118         gd->ram_size = imx_ddr_size();
119
120         return 0;
121 }
122
123 #define PC      MUX_PAD_CTRL(I2C_PAD_CTRL)
124 /* I2C1: TFA9879 */
125 struct i2c_pads_info i2c_pad_info0 = {
126         .scl = {
127                 .i2c_mode = MX6_PAD_EIM_D21__I2C1_SCL | PC,
128                 .gpio_mode = MX6_PAD_EIM_D21__GPIO3_IO21 | PC,
129                 .gp = IMX_GPIO_NR(3, 21)
130         },
131         .sda = {
132                 .i2c_mode = MX6_PAD_EIM_D28__I2C1_SDA | PC,
133                 .gpio_mode = MX6_PAD_EIM_D28__GPIO3_IO28 | PC,
134                 .gp = IMX_GPIO_NR(3, 28)
135         }
136 };
137
138 /* I2C2: TIVO TM4C123 */
139 struct i2c_pads_info i2c_pad_info1 = {
140         .scl = {
141                 .i2c_mode = MX6_PAD_EIM_EB2__I2C2_SCL | PC,
142                 .gpio_mode = MX6_PAD_EIM_EB2__GPIO2_IO30 | PC,
143                 .gp = IMX_GPIO_NR(2, 30)
144         },
145         .sda = {
146                 .i2c_mode = MX6_PAD_EIM_D16__I2C2_SDA | PC,
147                 .gpio_mode = MX6_PAD_EIM_D16__GPIO3_IO16 | PC,
148                 .gp = IMX_GPIO_NR(3, 16)
149         }
150 };
151
152 /* I2C3: PMIC PF0100, EEPROM AT24C256C */
153 struct i2c_pads_info i2c_pad_info2 = {
154         .scl = {
155                 .i2c_mode = MX6_PAD_EIM_D17__I2C3_SCL | PC,
156                 .gpio_mode = MX6_PAD_EIM_D17__GPIO3_IO17 | PC,
157                 .gp = IMX_GPIO_NR(3, 17)
158         },
159         .sda = {
160                 .i2c_mode = MX6_PAD_EIM_D18__I2C3_SDA | PC,
161                 .gpio_mode = MX6_PAD_EIM_D18__GPIO3_IO18 | PC,
162                 .gp = IMX_GPIO_NR(3, 18)
163         }
164 };
165
166 iomux_v3_cfg_t const misc_pads[] = {
167         /* Prod ID GPIO pins */
168         MX6_PAD_NANDF_D4__GPIO2_IO04    | MUX_PAD_CTRL(NO_PAD_CTRL),
169         MX6_PAD_NANDF_D5__GPIO2_IO05    | MUX_PAD_CTRL(NO_PAD_CTRL),
170         MX6_PAD_NANDF_D6__GPIO2_IO06    | MUX_PAD_CTRL(NO_PAD_CTRL),
171         MX6_PAD_NANDF_D7__GPIO2_IO07    | MUX_PAD_CTRL(NO_PAD_CTRL),
172
173         /* HW revision GPIO pins */
174         MX6_PAD_NANDF_CLE__GPIO6_IO07   | MUX_PAD_CTRL(NO_PAD_CTRL),
175         MX6_PAD_NANDF_WP_B__GPIO6_IO09  | MUX_PAD_CTRL(NO_PAD_CTRL),
176         MX6_PAD_NANDF_RB0__GPIO6_IO10   | MUX_PAD_CTRL(NO_PAD_CTRL),
177         MX6_PAD_NANDF_CS0__GPIO6_IO11   | MUX_PAD_CTRL(NO_PAD_CTRL),
178         MX6_PAD_KEY_ROW0__GPIO4_IO07 | MUX_PAD_CTRL(NO_PAD_CTRL),
179         MX6_PAD_KEY_ROW2__GPIO4_IO11 | MUX_PAD_CTRL(NO_PAD_CTRL),
180         MX6_PAD_KEY_ROW3__GPIO4_IO13 | MUX_PAD_CTRL(NO_PAD_CTRL),
181         MX6_PAD_KEY_ROW4__GPIO4_IO15 | MUX_PAD_CTRL(NO_PAD_CTRL),
182
183         /* XTALOSC */
184         MX6_PAD_GPIO_3__XTALOSC_REF_CLK_24M | MUX_PAD_CTRL(NO_PAD_CTRL),
185
186         /* Emergency recovery pin */
187         MX6_PAD_EIM_D29__GPIO3_IO29 | MUX_PAD_CTRL(NO_PAD_CTRL),
188 };
189
190 #ifdef CONFIG_FSL_ESDHC_IMX
191 struct fsl_esdhc_cfg usdhc_cfg[1] = {
192         { USDHC4_BASE_ADDR, 0, 8, },
193 };
194
195 int board_mmc_getcd(struct mmc *mmc)
196 {
197         return 1;
198 }
199
200 int board_mmc_init(bd_t *bis)
201 {
202         displ5_set_iomux_usdhc();
203
204         usdhc_cfg[0].sdhc_clk = mxc_get_clock(MXC_ESDHC4_CLK);
205
206         return fsl_esdhc_initialize(bis, &usdhc_cfg[0]);
207 }
208 #endif /* CONFIG_FSL_ESDHC_IMX */
209
210 static void displ5_setup_ecspi(void)
211 {
212         int ret;
213
214         displ5_set_iomux_ecspi();
215
216         ret = gpio_request(IMX_GPIO_NR(5, 29), "spi2_cs0");
217         if (!ret)
218                 gpio_direction_output(IMX_GPIO_NR(5, 29), 1);
219
220         ret = gpio_request(IMX_GPIO_NR(7, 0), "spi2_#wp");
221         if (!ret)
222                 gpio_direction_output(IMX_GPIO_NR(7, 0), 1);
223 }
224
225 #ifdef CONFIG_FEC_MXC
226 iomux_v3_cfg_t const enet_pads[] = {
227         MX6_PAD_ENET_TXD1__ENET_1588_EVENT0_IN  | MUX_PAD_CTRL(ENET_PAD_CTRL),
228         MX6_PAD_ENET_RXD1__ENET_1588_EVENT3_OUT | MUX_PAD_CTRL(ENET_PAD_CTRL),
229         MX6_PAD_ENET_MDIO__ENET_MDIO            | MUX_PAD_CTRL(ENET_PAD_CTRL),
230         MX6_PAD_ENET_MDC__ENET_MDC              | MUX_PAD_CTRL(ENET_PAD_CTRL),
231         MX6_PAD_ENET_REF_CLK__ENET_TX_CLK       | MUX_PAD_CTRL(ENET_PAD_CTRL),
232
233         /* for old evalboard with R159 present and R160 not populated */
234         MX6_PAD_GPIO_16__ENET_REF_CLK           | MUX_PAD_CTRL(NO_PAD_CTRL),
235
236         MX6_PAD_RGMII_TXC__RGMII_TXC            | MUX_PAD_CTRL(ENET_PAD_CTRL),
237         MX6_PAD_RGMII_TD0__RGMII_TD0            | MUX_PAD_CTRL(ENET_PAD_CTRL),
238         MX6_PAD_RGMII_TD1__RGMII_TD1            | MUX_PAD_CTRL(ENET_PAD_CTRL),
239         MX6_PAD_RGMII_TD2__RGMII_TD2            | MUX_PAD_CTRL(ENET_PAD_CTRL),
240         MX6_PAD_RGMII_TD3__RGMII_TD3            | MUX_PAD_CTRL(ENET_PAD_CTRL),
241         MX6_PAD_RGMII_TX_CTL__RGMII_TX_CTL      | MUX_PAD_CTRL(ENET_PAD_CTRL),
242
243         MX6_PAD_RGMII_RXC__RGMII_RXC            | MUX_PAD_CTRL(ENET_PAD_CTRL),
244         MX6_PAD_RGMII_RD0__RGMII_RD0            | MUX_PAD_CTRL(ENET_PAD_CTRL),
245         MX6_PAD_RGMII_RD1__RGMII_RD1            | MUX_PAD_CTRL(ENET_PAD_CTRL),
246         MX6_PAD_RGMII_RD2__RGMII_RD2            | MUX_PAD_CTRL(ENET_PAD_CTRL),
247         MX6_PAD_RGMII_RD3__RGMII_RD3            | MUX_PAD_CTRL(ENET_PAD_CTRL),
248         MX6_PAD_RGMII_RX_CTL__RGMII_RX_CTL      | MUX_PAD_CTRL(ENET_PAD_CTRL),
249         /*INT#_GBE*/
250         MX6_PAD_ENET_TX_EN__GPIO1_IO28          | MUX_PAD_CTRL(NO_PAD_CTRL),
251 };
252
253 static void setup_iomux_enet(void)
254 {
255         SETUP_IOMUX_PADS(enet_pads);
256         gpio_direction_input(IMX_GPIO_NR(1, 28)); /*INT#_GBE*/
257 }
258
259 static int setup_mac_from_fuse(void)
260 {
261         unsigned char enetaddr[6];
262         int ret;
263
264         ret = eth_env_get_enetaddr("ethaddr", enetaddr);
265         if (ret)        /* ethaddr is already set */
266                 return 0;
267
268         imx_get_mac_from_fuse(0, enetaddr);
269
270         if (is_valid_ethaddr(enetaddr)) {
271                 eth_env_set_enetaddr("ethaddr", enetaddr);
272                 return 0;
273         }
274
275         return 0;
276 }
277
278 int board_eth_init(bd_t *bd)
279 {
280         struct phy_device *phydev;
281         struct mii_dev *bus;
282         int ret;
283
284         setup_iomux_enet();
285
286         iomuxc_set_rgmii_io_voltage(DDR_SEL_1P5V_IO);
287
288         ret = enable_fec_anatop_clock(0, ENET_125MHZ);
289         if (ret)
290                 return ret;
291
292         setup_mac_from_fuse();
293
294         bus = fec_get_miibus(IMX_FEC_BASE, -1);
295         if (!bus)
296                 return -ENODEV;
297
298         /*
299          * We use here the "rgmii-id" mode of operation and allow M88E1512
300          * PHY to use its internally callibrated RX/TX delays
301          */
302         phydev = phy_find_by_mask(bus, 0xffffffff /* (0xf << 4) */,
303                                   PHY_INTERFACE_MODE_RGMII_ID);
304         if (!phydev) {
305                 ret = -ENODEV;
306                 goto err_phy;
307         }
308
309         /* display5 due to PCB routing can only work with 100 Mbps */
310         phydev->advertising &= ~(ADVERTISED_1000baseX_Half |
311                                  ADVERTISED_1000baseX_Full |
312                                  SUPPORTED_1000baseT_Half |
313                                  SUPPORTED_1000baseT_Full);
314
315         ret  = fec_probe(bd, -1, IMX_FEC_BASE, bus, phydev);
316         if (ret)
317                 goto err_sw;
318
319         return 0;
320
321 err_sw:
322         free(phydev);
323 err_phy:
324         mdio_unregister(bus);
325         free(bus);
326         return ret;
327 }
328 #endif /* CONFIG_FEC_MXC */
329
330 /*
331  * Do not overwrite the console
332  * Always use serial for U-Boot console
333  */
334 int overwrite_console(void)
335 {
336         return 1;
337 }
338
339 #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
340 int ft_board_setup(void *blob, bd_t *bd)
341 {
342         fdt_fixup_ethernet(blob);
343         return 0;
344 }
345 #endif
346
347 int board_init(void)
348 {
349         debug("board init\n");
350         /* address of boot parameters */
351         gd->bd->bi_boot_params = PHYS_SDRAM + 0x100;
352
353         /* Setup iomux for non console UARTS */
354         displ5_set_iomux_uart();
355
356         displ5_setup_ecspi();
357
358         SETUP_IOMUX_PADS(misc_pads);
359
360         get_board_id(gpio_table_sw_ids, &gpio_table_sw_ids_names[0],
361                      ARRAY_SIZE(gpio_table_sw_ids), &sw_ids_valid, &unit_id);
362         debug("SWx unit_id 0x%x\n", unit_id);
363
364         get_board_id(gpio_table_hw_ids, &gpio_table_hw_ids_names[0],
365                      ARRAY_SIZE(gpio_table_hw_ids), &hw_ids_valid, &cpu_id);
366         debug("HWx cpu_id 0x%x\n", cpu_id);
367
368         if (hw_ids_valid && sw_ids_valid)
369                 printf("ID:    unit type 0x%x rev 0x%x\n", unit_id, cpu_id);
370
371         udelay(25);
372
373         setup_i2c(0, CONFIG_SYS_I2C_SPEED, 0x7f, &i2c_pad_info0);
374         setup_i2c(1, CONFIG_SYS_I2C_SPEED, 0x7f, &i2c_pad_info1);
375         setup_i2c(2, CONFIG_SYS_I2C_SPEED, 0x7f, &i2c_pad_info2);
376
377         return 0;
378 }
379
380 #ifdef CONFIG_CMD_BMODE
381 static const struct boot_mode board_boot_modes[] = {
382         /* eMMC, USDHC-4, 8-bit bus width */
383         /* SPI-NOR, ECSPI-2 SS0, 3-bytes addressing */
384         {"emmc",    MAKE_CFGVAL(0x60, 0x58, 0x00, 0x00)},
385         {"spinor",  MAKE_CFGVAL(0x30, 0x00, 0x00, 0x09)},
386         {NULL,  0},
387 };
388
389 static void setup_boot_modes(void)
390 {
391         add_board_boot_modes(board_boot_modes);
392 }
393 #else
394 static inline void setup_boot_modes(void) {}
395 #endif
396
397 int misc_init_r(void)
398 {
399         int ret;
400
401         setup_boot_modes();
402
403         ret = gpio_request(EM_PAD, "Emergency_PAD");
404         if (ret) {
405                 printf("Can't request emergency PAD gpio\n");
406                 return ret;
407         }
408
409         ret = gpio_direction_input(EM_PAD);
410         if (ret) {
411                 printf("Can't set emergency PAD direction\n");
412                 return ret;
413         }
414
415         return 0;
416 }