common: Drop net.h from common header
[oweals/u-boot.git] / board / freescale / ls1012ardb / ls1012ardb.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2016 Freescale Semiconductor, Inc.
4  */
5
6 #include <common.h>
7 #include <fdt_support.h>
8 #include <hang.h>
9 #include <i2c.h>
10 #include <asm/cache.h>
11 #include <asm/io.h>
12 #include <asm/arch/clock.h>
13 #include <asm/arch/fsl_serdes.h>
14 #ifdef CONFIG_FSL_LS_PPA
15 #include <asm/arch/ppa.h>
16 #endif
17 #include <asm/arch/mmu.h>
18 #include <asm/arch/soc.h>
19 #include <hwconfig.h>
20 #include <ahci.h>
21 #include <mmc.h>
22 #include <scsi.h>
23 #include <fsl_esdhc.h>
24 #include <env_internal.h>
25 #include <fsl_mmdc.h>
26 #include <netdev.h>
27 #include <fsl_sec.h>
28
29 DECLARE_GLOBAL_DATA_PTR;
30
31 #define BOOT_FROM_UPPER_BANK    0x2
32 #define BOOT_FROM_LOWER_BANK    0x1
33
34 int checkboard(void)
35 {
36 #ifdef CONFIG_TARGET_LS1012ARDB
37         u8 in1;
38         int ret, bus_num = 0;
39
40         puts("Board: LS1012ARDB ");
41
42         /* Initialize i2c early for Serial flash bank information */
43 #if defined(CONFIG_DM_I2C)
44         struct udevice *dev;
45
46         ret = i2c_get_chip_for_busnum(bus_num, I2C_MUX_IO_ADDR,
47                                       1, &dev);
48         if (ret) {
49                 printf("%s: Cannot find udev for a bus %d\n", __func__,
50                        bus_num);
51                 return -ENXIO;
52         }
53         ret = dm_i2c_read(dev, I2C_MUX_IO_1, &in1, 1);
54 #else /* Non DM I2C support - will be removed */
55         i2c_set_bus_num(bus_num);
56         ret = i2c_read(I2C_MUX_IO_ADDR, I2C_MUX_IO_1, 1, &in1, 1);
57 #endif
58         if (ret < 0) {
59                 printf("Error reading i2c boot information!\n");
60                 return 0; /* Don't want to hang() on this error */
61         }
62
63         puts("Version");
64         switch (in1 & SW_REV_MASK) {
65         case SW_REV_A:
66                 puts(": RevA");
67                 break;
68         case SW_REV_B:
69                 puts(": RevB");
70                 break;
71         case SW_REV_C:
72                 puts(": RevC");
73                 break;
74         case SW_REV_C1:
75                 puts(": RevC1");
76                 break;
77         case SW_REV_C2:
78                 puts(": RevC2");
79                 break;
80         case SW_REV_D:
81                 puts(": RevD");
82                 break;
83         case SW_REV_E:
84                 puts(": RevE");
85                 break;
86         default:
87                 puts(": unknown");
88                 break;
89         }
90
91         printf(", boot from QSPI");
92         if ((in1 & SW_BOOT_MASK) == SW_BOOT_EMU)
93                 puts(": emu\n");
94         else if ((in1 & SW_BOOT_MASK) == SW_BOOT_BANK1)
95                 puts(": bank1\n");
96         else if ((in1 & SW_BOOT_MASK) == SW_BOOT_BANK2)
97                 puts(": bank2\n");
98         else
99                 puts("unknown\n");
100 #else
101
102         puts("Board: LS1012A2G5RDB ");
103 #endif
104         return 0;
105 }
106
107 #ifdef CONFIG_TFABOOT
108 int dram_init(void)
109 {
110         gd->ram_size = tfa_get_dram_size();
111         if (!gd->ram_size)
112                 gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
113
114         return 0;
115 }
116 #else
117 int dram_init(void)
118 {
119 #ifndef CONFIG_TFABOOT
120         static const struct fsl_mmdc_info mparam = {
121                 0x05180000,     /* mdctl */
122                 0x00030035,     /* mdpdc */
123                 0x12554000,     /* mdotc */
124                 0xbabf7954,     /* mdcfg0 */
125                 0xdb328f64,     /* mdcfg1 */
126                 0x01ff00db,     /* mdcfg2 */
127                 0x00001680,     /* mdmisc */
128                 0x0f3c8000,     /* mdref */
129                 0x00002000,     /* mdrwd */
130                 0x00bf1023,     /* mdor */
131                 0x0000003f,     /* mdasp */
132                 0x0000022a,     /* mpodtctrl */
133                 0xa1390003,     /* mpzqhwctrl */
134         };
135
136         mmdc_init(&mparam);
137 #endif
138
139         gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
140 #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
141         /* This will break-before-make MMU for DDR */
142         update_early_mmu_table();
143 #endif
144
145         return 0;
146 }
147 #endif
148
149
150 int board_early_init_f(void)
151 {
152         fsl_lsch2_early_init_f();
153
154         return 0;
155 }
156
157 int board_init(void)
158 {
159         struct ccsr_cci400 *cci = (struct ccsr_cci400 *)(CONFIG_SYS_IMMR +
160                                         CONFIG_SYS_CCI400_OFFSET);
161         /*
162          * Set CCI-400 control override register to enable barrier
163          * transaction
164          */
165         if (current_el() == 3)
166                 out_le32(&cci->ctrl_ord, CCI400_CTRLORD_EN_BARRIER);
167
168 #ifdef CONFIG_SYS_FSL_ERRATUM_A010315
169         erratum_a010315();
170 #endif
171
172 #ifdef CONFIG_ENV_IS_NOWHERE
173         gd->env_addr = (ulong)&default_environment[0];
174 #endif
175
176 #ifdef CONFIG_FSL_CAAM
177         sec_init();
178 #endif
179
180 #ifdef CONFIG_FSL_LS_PPA
181         ppa_init();
182 #endif
183         return 0;
184 }
185
186 #ifdef CONFIG_TARGET_LS1012ARDB
187 int esdhc_status_fixup(void *blob, const char *compat)
188 {
189         char esdhc1_path[] = "/soc/esdhc@1580000";
190         bool sdhc2_en = false;
191         u8 mux_sdhc2;
192         u8 io = 0;
193         int ret, bus_num = 0;
194
195 #if defined(CONFIG_DM_I2C)
196         struct udevice *dev;
197
198         ret = i2c_get_chip_for_busnum(bus_num, I2C_MUX_IO_ADDR,
199                                       1, &dev);
200         if (ret) {
201                 printf("%s: Cannot find udev for a bus %d\n", __func__,
202                        bus_num);
203                 return -ENXIO;
204         }
205         ret = dm_i2c_read(dev, I2C_MUX_IO_1, &io, 1);
206 #else
207         i2c_set_bus_num(bus_num);
208         /* IO1[7:3] is the field of board revision info. */
209         ret = i2c_read(I2C_MUX_IO_ADDR, I2C_MUX_IO_1, 1, &io, 1);
210 #endif
211         if (ret < 0) {
212                 printf("Error reading i2c boot information!\n");
213                 return 0;
214         }
215
216         /* hwconfig method is used for RevD and later versions. */
217         if ((io & SW_REV_MASK) <= SW_REV_D) {
218 #ifdef CONFIG_HWCONFIG
219                 if (hwconfig("esdhc1"))
220                         sdhc2_en = true;
221 #endif
222         } else {
223                 /*
224                  * The I2C IO-expander for mux select is used to control
225                  * the muxing of various onboard interfaces.
226                  *
227                  * IO0[3:2] indicates SDHC2 interface demultiplexer
228                  * select lines.
229                  *      00 - SDIO wifi
230                  *      01 - GPIO (to Arduino)
231                  *      10 - eMMC Memory
232                  *      11 - SPI
233                  */
234 #if defined(CONFIG_DM_I2C)
235                 ret = dm_i2c_read(dev, I2C_MUX_IO_0, &io, 1);
236 #else
237                 ret = i2c_read(I2C_MUX_IO_ADDR, I2C_MUX_IO_0, 1, &io, 1);
238 #endif
239                 if (ret < 0) {
240                         printf("Error reading i2c boot information!\n");
241                         return 0;
242                 }
243
244                 mux_sdhc2 = (io & 0x0c) >> 2;
245                 /* Enable SDHC2 only when use SDIO wifi and eMMC */
246                 if (mux_sdhc2 == 2 || mux_sdhc2 == 0)
247                         sdhc2_en = true;
248         }
249         if (sdhc2_en)
250                 do_fixup_by_path(blob, esdhc1_path, "status", "okay",
251                                  sizeof("okay"), 1);
252         else
253                 do_fixup_by_path(blob, esdhc1_path, "status", "disabled",
254                                  sizeof("disabled"), 1);
255         return 0;
256 }
257 #endif
258
259 int ft_board_setup(void *blob, bd_t *bd)
260 {
261         arch_fixup_fdt(blob);
262
263         ft_cpu_setup(blob, bd);
264
265         return 0;
266 }
267
268 static int switch_to_bank1(void)
269 {
270         u8 data = 0xf4, chip_addr = 0x24, offset_addr = 0x03;
271         int ret, bus_num = 0;
272
273 #if defined(CONFIG_DM_I2C)
274         struct udevice *dev;
275
276         ret = i2c_get_chip_for_busnum(bus_num, chip_addr,
277                                       1, &dev);
278         if (ret) {
279                 printf("%s: Cannot find udev for a bus %d\n", __func__,
280                        bus_num);
281                 return -ENXIO;
282         }
283         /*
284          * --------------------------------------------------------------------
285          * |bus |I2C address|       Device     |          Notes               |
286          * --------------------------------------------------------------------
287          * |I2C1|0x24, 0x25,| IO expander (CFG,| Provides 16bits of General   |
288          * |    |0x26       | RESET, and INT/  | Purpose parallel Input/Output|
289          * |    |           | KW41GPIO) - NXP  | (GPIO) expansion for the     |
290          * |    |           | PCAL9555AHF      | I2C bus                      |
291          * ----- --------------------------------------------------------------
292          * - mount three IO expander(PCAL9555AHF) on I2C1
293          *
294          * PCAL9555A device address
295          *           slave address
296          *  --------------------------------------
297          *  | 0 | 1 | 0 | 0 | A2 | A1 | A0 | R/W |
298          *  --------------------------------------
299          *  |     fixed     | hardware selectable|
300          *
301          * Output port 1(Pinter register bits = 0x03)
302          *
303          * P1_[7~0] = 0xf4
304          * P1_0 <---> CFG_MUX_QSPI_S0
305          * P1_1 <---> CFG_MUX_QSPI_S1
306          * CFG_MUX_QSPI_S[1:0] = 0b00
307          *
308          * QSPI chip-select demultiplexer select
309          * ---------------------------------------------------------------------
310          * CFG_MUX_QSPI_S1|CFG_MUX_QSPI_S0|              Values
311          * ---------------------------------------------------------------------
312          *    0           | 0            |CS routed to SPI memory bank1(default)
313          * ---------------------------------------------------------------------
314          *    0           | 1             |CS routed to SPI memory bank2
315          * ---------------------------------------------------------------------
316          *
317          */
318         ret = dm_i2c_write(dev, offset_addr, &data, 1);
319 #else /* Non DM I2C support - will be removed */
320         i2c_set_bus_num(bus_num);
321         ret = i2c_write(chip_addr, offset_addr, 1, &data, 1);
322 #endif
323
324         if (ret) {
325                 printf("i2c write error to chip : %u, addr : %u, data : %u\n",
326                        chip_addr, offset_addr, data);
327         }
328
329         return ret;
330 }
331
332 static int switch_to_bank2(void)
333 {
334         u8 data[2] = {0xfc, 0xf5}, offset_addr[2] = {0x7, 0x3};
335         u8 chip_addr = 0x24;
336         int ret, i, bus_num = 0;
337
338 #if defined(CONFIG_DM_I2C)
339         struct udevice *dev;
340
341         ret = i2c_get_chip_for_busnum(bus_num, chip_addr,
342                                       1, &dev);
343         if (ret) {
344                 printf("%s: Cannot find udev for a bus %d\n", __func__,
345                        bus_num);
346                 return -ENXIO;
347         }
348 #else /* Non DM I2C support - will be removed */
349         i2c_set_bus_num(bus_num);
350 #endif
351
352         /*
353          * 1th step: config port 1
354          *      - the port 1 pin is enabled as an output
355          * 2th step: output port 1
356          *      - P1_[7:0] output 0xf5,
357          *        then CFG_MUX_QSPI_S[1:0] equal to 0b01,
358          *        CS routed to SPI memory bank2
359          */
360         for (i = 0; i < sizeof(data); i++) {
361 #if defined(CONFIG_DM_I2C)
362                 ret = dm_i2c_write(dev, offset_addr[i], &data[i], 1);
363 #else /* Non DM I2C support - will be removed */
364                 ret = i2c_write(chip_addr, offset_addr[i], 1, &data[i], 1);
365 #endif
366                 if (ret) {
367                         printf("i2c write error to chip : %u, addr : %u, data : %u\n",
368                                chip_addr, offset_addr[i], data[i]);
369                         goto err;
370                 }
371         }
372
373 err:
374         return ret;
375 }
376
377 static int convert_flash_bank(int bank)
378 {
379         int ret = 0;
380
381         switch (bank) {
382         case BOOT_FROM_UPPER_BANK:
383                 ret = switch_to_bank2();
384                 break;
385         case BOOT_FROM_LOWER_BANK:
386                 ret = switch_to_bank1();
387                 break;
388         default:
389                 ret = CMD_RET_USAGE;
390                 break;
391         };
392
393         return ret;
394 }
395
396 static int flash_bank_cmd(cmd_tbl_t *cmdtp, int flag, int argc,
397                           char * const argv[])
398 {
399         if (argc != 2)
400                 return CMD_RET_USAGE;
401         if (strcmp(argv[1], "1") == 0)
402                 convert_flash_bank(BOOT_FROM_LOWER_BANK);
403         else if (strcmp(argv[1], "2") == 0)
404                 convert_flash_bank(BOOT_FROM_UPPER_BANK);
405         else
406                 return CMD_RET_USAGE;
407
408         return 0;
409 }
410
411 U_BOOT_CMD(
412         boot_bank, 2, 0, flash_bank_cmd,
413         "Flash bank Selection Control",
414         "bank[1-lower bank/2-upper bank] (e.g. boot_bank 1)"
415 );