common: Drop linux/delay.h from common header
[oweals/u-boot.git] / board / gdsys / mpc8308 / strider.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2014
4  * Dirk Eibach,  Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
5  */
6
7 #include <common.h>
8 #include <env.h>
9 #include <flash.h>
10 #include <hwconfig.h>
11 #include <i2c.h>
12 #include <init.h>
13 #include <spi.h>
14 #include <linux/delay.h>
15 #include <linux/libfdt.h>
16 #include <fdt_support.h>
17 #include <pci.h>
18 #include <mpc83xx.h>
19 #include <fsl_esdhc.h>
20 #include <asm/io.h>
21 #include <asm/fsl_serdes.h>
22 #include <asm/fsl_mpc83xx_serdes.h>
23
24 #include "mpc8308.h"
25
26 #include <gdsys_fpga.h>
27
28 #include "../common/adv7611.h"
29 #include "../common/ch7301.h"
30 #include "../common/dp501.h"
31 #include "../common/ioep-fpga.h"
32 #include "../common/mclink.h"
33 #include "../common/osd.h"
34 #include "../common/phy.h"
35 #include "../common/fanctrl.h"
36
37 #include <pca953x.h>
38 #include <pca9698.h>
39
40 #include <miiphy.h>
41
42 #define MAX_MUX_CHANNELS 2
43
44 enum {
45         MCFPGA_DONE = 1 << 0,
46         MCFPGA_INIT_N = 1 << 1,
47         MCFPGA_PROGRAM_N = 1 << 2,
48         MCFPGA_UPDATE_ENABLE_N = 1 << 3,
49         MCFPGA_RESET_N = 1 << 4,
50 };
51
52 enum {
53         GPIO_MDC = 1 << 14,
54         GPIO_MDIO = 1 << 15,
55 };
56
57 uint mclink_fpgacount;
58 struct ihs_fpga *fpga_ptr[] = CONFIG_SYS_FPGA_PTR;
59
60 struct {
61         u8 bus;
62         u8 addr;
63 } strider_fans[] = CONFIG_STRIDER_FANS;
64
65 int fpga_set_reg(u32 fpga, u16 *reg, off_t regoff, u16 data)
66 {
67         int res;
68
69         switch (fpga) {
70         case 0:
71                 out_le16(reg, data);
72                 break;
73         default:
74                 res = mclink_send(fpga - 1, regoff, data);
75                 if (res < 0) {
76                         printf("mclink_send reg %02lx data %04x returned %d\n",
77                                regoff, data, res);
78                         return res;
79                 }
80                 break;
81         }
82
83         return 0;
84 }
85
86 int fpga_get_reg(u32 fpga, u16 *reg, off_t regoff, u16 *data)
87 {
88         int res;
89
90         switch (fpga) {
91         case 0:
92                 *data = in_le16(reg);
93                 break;
94         default:
95                 if (fpga > mclink_fpgacount)
96                         return -EINVAL;
97                 res = mclink_receive(fpga - 1, regoff, data);
98                 if (res < 0) {
99                         printf("mclink_receive reg %02lx returned %d\n",
100                                regoff, res);
101                         return res;
102                 }
103         }
104
105         return 0;
106 }
107
108 int checkboard(void)
109 {
110         char *s = env_get("serial#");
111         bool hw_type_cat = pca9698_get_value(0x20, 18);
112
113         puts("Board: ");
114
115         printf("Strider %s", hw_type_cat ? "CAT" : "Fiber");
116
117         if (s) {
118                 puts(", serial# ");
119                 puts(s);
120         }
121
122         puts("\n");
123
124         return 0;
125 }
126
127 int last_stage_init(void)
128 {
129         int slaves;
130         uint k;
131         uint mux_ch;
132         uchar mclink_controllers_dvi[] = { 0x3c, 0x3d, 0x3e };
133 #ifdef CONFIG_STRIDER_CPU
134         uchar mclink_controllers_dp[] = { 0x24, 0x25, 0x26 };
135 #endif
136         bool hw_type_cat = pca9698_get_value(0x20, 18);
137 #ifdef CONFIG_STRIDER_CON_DP
138         bool is_dh = pca9698_get_value(0x20, 25);
139 #endif
140         bool ch0_sgmii2_present;
141
142         /* Turn on Analog Devices ADV7611 */
143         pca9698_direction_output(0x20, 8, 0);
144
145         /* Turn on Parade DP501 */
146         pca9698_direction_output(0x20, 10, 1);
147         pca9698_direction_output(0x20, 11, 1);
148
149         ch0_sgmii2_present = !pca9698_get_value(0x20, 37);
150
151         /* wait for FPGA done, then reset FPGA */
152         for (k = 0; k < ARRAY_SIZE(mclink_controllers_dvi); ++k) {
153                 uint ctr = 0;
154                 uchar *mclink_controllers = mclink_controllers_dvi;
155
156 #ifdef CONFIG_STRIDER_CPU
157                 if (i2c_probe(mclink_controllers[k])) {
158                         mclink_controllers = mclink_controllers_dp;
159                         if (i2c_probe(mclink_controllers[k]))
160                                 continue;
161                 }
162 #else
163                 if (i2c_probe(mclink_controllers[k]))
164                         continue;
165 #endif
166                 while (!(pca953x_get_val(mclink_controllers[k])
167                        & MCFPGA_DONE)) {
168                         mdelay(100);
169                         if (ctr++ > 5) {
170                                 printf("no done for mclink_controller %d\n", k);
171                                 break;
172                         }
173                 }
174
175                 pca953x_set_dir(mclink_controllers[k], MCFPGA_RESET_N, 0);
176                 pca953x_set_val(mclink_controllers[k], MCFPGA_RESET_N, 0);
177                 udelay(10);
178                 pca953x_set_val(mclink_controllers[k], MCFPGA_RESET_N,
179                                 MCFPGA_RESET_N);
180         }
181
182         if (hw_type_cat) {
183                 int retval;
184                 struct mii_dev *mdiodev = mdio_alloc();
185
186                 if (!mdiodev)
187                         return -ENOMEM;
188                 strncpy(mdiodev->name, bb_miiphy_buses[0].name, MDIO_NAME_LEN);
189                 mdiodev->read = bb_miiphy_read;
190                 mdiodev->write = bb_miiphy_write;
191
192                 retval = mdio_register(mdiodev);
193                 if (retval < 0)
194                         return retval;
195                 for (mux_ch = 0; mux_ch < MAX_MUX_CHANNELS; ++mux_ch) {
196                         if ((mux_ch == 1) && !ch0_sgmii2_present)
197                                 continue;
198
199                         setup_88e1514(bb_miiphy_buses[0].name, mux_ch);
200                 }
201         }
202
203         /* give slave-PLLs and Parade DP501 some time to be up and running */
204         mdelay(500);
205
206         mclink_fpgacount = CONFIG_SYS_MCLINK_MAX;
207         slaves = mclink_probe();
208         mclink_fpgacount = 0;
209
210         ioep_fpga_print_info(0);
211
212         if (!adv7611_probe(0))
213                 printf("       Advantiv ADV7611 HDMI Receiver\n");
214
215 #ifdef CONFIG_STRIDER_CON
216         if (ioep_fpga_has_osd(0))
217                 osd_probe(0);
218 #endif
219
220 #ifdef CONFIG_STRIDER_CON_DP
221         if (ioep_fpga_has_osd(0)) {
222                 osd_probe(0);
223                 if (is_dh)
224                         osd_probe(4);
225         }
226 #endif
227
228 #ifdef CONFIG_STRIDER_CPU
229         ch7301_probe(0, false);
230         dp501_probe(0, false);
231 #endif
232
233         if (slaves <= 0)
234                 return 0;
235
236         mclink_fpgacount = slaves;
237
238 #ifdef CONFIG_STRIDER_CPU
239         /* get ADV7611 out of reset, power up DP501, give some time to wakeup */
240         for (k = 1; k <= slaves; ++k)
241                 FPGA_SET_REG(k, extended_control, 0x10); /* enable video */
242
243         mdelay(500);
244 #endif
245
246         for (k = 1; k <= slaves; ++k) {
247                 ioep_fpga_print_info(k);
248 #ifdef CONFIG_STRIDER_CON
249                 if (ioep_fpga_has_osd(k))
250                         osd_probe(k);
251 #endif
252 #ifdef CONFIG_STRIDER_CON_DP
253                 if (ioep_fpga_has_osd(k)) {
254                         osd_probe(k);
255                         if (is_dh)
256                                 osd_probe(k + 4);
257                 }
258 #endif
259 #ifdef CONFIG_STRIDER_CPU
260                 if (!adv7611_probe(k))
261                         printf("       Advantiv ADV7611 HDMI Receiver\n");
262                 ch7301_probe(k, false);
263                 dp501_probe(k, false);
264 #endif
265                 if (hw_type_cat) {
266                         int retval;
267                         struct mii_dev *mdiodev = mdio_alloc();
268
269                         if (!mdiodev)
270                                 return -ENOMEM;
271                         strncpy(mdiodev->name, bb_miiphy_buses[k].name,
272                                 MDIO_NAME_LEN);
273                         mdiodev->read = bb_miiphy_read;
274                         mdiodev->write = bb_miiphy_write;
275
276                         retval = mdio_register(mdiodev);
277                         if (retval < 0)
278                                 return retval;
279                         setup_88e1514(bb_miiphy_buses[k].name, 0);
280                 }
281         }
282
283         for (k = 0; k < ARRAY_SIZE(strider_fans); ++k) {
284                 i2c_set_bus_num(strider_fans[k].bus);
285                 init_fan_controller(strider_fans[k].addr);
286         }
287
288         return 0;
289 }
290
291 /*
292  * provide access to fpga gpios (for I2C bitbang)
293  * (these may look all too simple but make iocon.h much more readable)
294  */
295 void fpga_gpio_set(uint bus, int pin)
296 {
297         FPGA_SET_REG(bus, gpio.set, pin);
298 }
299
300 void fpga_gpio_clear(uint bus, int pin)
301 {
302         FPGA_SET_REG(bus, gpio.clear, pin);
303 }
304
305 int fpga_gpio_get(uint bus, int pin)
306 {
307         u16 val;
308
309         FPGA_GET_REG(bus, gpio.read, &val);
310
311         return val & pin;
312 }
313
314 #ifdef CONFIG_STRIDER_CON_DP
315 void fpga_control_set(uint bus, int pin)
316 {
317         u16 val;
318
319         FPGA_GET_REG(bus, control, &val);
320         FPGA_SET_REG(bus, control, val | pin);
321 }
322
323 void fpga_control_clear(uint bus, int pin)
324 {
325         u16 val;
326
327         FPGA_GET_REG(bus, control, &val);
328         FPGA_SET_REG(bus, control, val & ~pin);
329 }
330 #endif
331
332 void mpc8308_init(void)
333 {
334         pca9698_direction_output(0x20, 26, 1);
335 }
336
337 void mpc8308_set_fpga_reset(uint state)
338 {
339         pca9698_set_value(0x20, 26, state ? 0 : 1);
340 }
341
342 void mpc8308_setup_hw(void)
343 {
344         immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
345
346         /*
347          * set "startup-finished"-gpios
348          */
349         setbits_be32(&immr->gpio[0].dir, BIT(31 - 11) | BIT(31 - 12));
350         setbits_gpio0_out(BIT(31 - 12));
351 }
352
353 int mpc8308_get_fpga_done(uint fpga)
354 {
355         return pca9698_get_value(0x20, 20);
356 }
357
358 #ifdef CONFIG_FSL_ESDHC
359 int board_mmc_init(bd_t *bd)
360 {
361         immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
362         sysconf83xx_t *sysconf = &immr->sysconf;
363
364         /* Enable cache snooping in eSDHC system configuration register */
365         out_be32(&sysconf->sdhccr, 0x02000000);
366
367         return fsl_esdhc_mmc_init(bd);
368 }
369 #endif
370
371 static struct pci_region pcie_regions_0[] = {
372         {
373                 .bus_start = CONFIG_SYS_PCIE1_MEM_BASE,
374                 .phys_start = CONFIG_SYS_PCIE1_MEM_PHYS,
375                 .size = CONFIG_SYS_PCIE1_MEM_SIZE,
376                 .flags = PCI_REGION_MEM,
377         },
378         {
379                 .bus_start = CONFIG_SYS_PCIE1_IO_BASE,
380                 .phys_start = CONFIG_SYS_PCIE1_IO_PHYS,
381                 .size = CONFIG_SYS_PCIE1_IO_SIZE,
382                 .flags = PCI_REGION_IO,
383         },
384 };
385
386 void pci_init_board(void)
387 {
388         immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
389         sysconf83xx_t *sysconf = &immr->sysconf;
390         law83xx_t *pcie_law = sysconf->pcielaw;
391         struct pci_region *pcie_reg[] = { pcie_regions_0 };
392
393         fsl_setup_serdes(CONFIG_FSL_SERDES1, FSL_SERDES_PROTO_PEX,
394                          FSL_SERDES_CLK_100, FSL_SERDES_VDD_1V);
395
396         /* Deassert the resets in the control register */
397         out_be32(&sysconf->pecr1, 0xE0008000);
398         udelay(2000);
399
400         /* Configure PCI Express Local Access Windows */
401         out_be32(&pcie_law[0].bar, CONFIG_SYS_PCIE1_BASE & LAWBAR_BAR);
402         out_be32(&pcie_law[0].ar, LBLAWAR_EN | LBLAWAR_512MB);
403
404         mpc83xx_pcie_init(1, pcie_reg);
405 }
406
407 ulong board_flash_get_legacy(ulong base, int banknum, flash_info_t *info)
408 {
409         info->portwidth = FLASH_CFI_16BIT;
410         info->chipwidth = FLASH_CFI_BY16;
411         info->interface = FLASH_CFI_X16;
412         return 1;
413 }
414
415 #if defined(CONFIG_OF_BOARD_SETUP)
416 int ft_board_setup(void *blob, bd_t *bd)
417 {
418         ft_cpu_setup(blob, bd);
419         fsl_fdt_fixup_dr_usb(blob, bd);
420         fdt_fixup_esdhc(blob, bd);
421
422         return 0;
423 }
424 #endif
425
426 /*
427  * FPGA MII bitbang implementation
428  */
429
430 struct fpga_mii {
431         uint fpga;
432         int mdio;
433 } fpga_mii[] = {
434         { 0, 1},
435         { 1, 1},
436         { 2, 1},
437         { 3, 1},
438 };
439
440 static int mii_dummy_init(struct bb_miiphy_bus *bus)
441 {
442         return 0;
443 }
444
445 static int mii_mdio_active(struct bb_miiphy_bus *bus)
446 {
447         struct fpga_mii *fpga_mii = bus->priv;
448
449         if (fpga_mii->mdio)
450                 FPGA_SET_REG(fpga_mii->fpga, gpio.set, GPIO_MDIO);
451         else
452                 FPGA_SET_REG(fpga_mii->fpga, gpio.clear, GPIO_MDIO);
453
454         return 0;
455 }
456
457 static int mii_mdio_tristate(struct bb_miiphy_bus *bus)
458 {
459         struct fpga_mii *fpga_mii = bus->priv;
460
461         FPGA_SET_REG(fpga_mii->fpga, gpio.set, GPIO_MDIO);
462
463         return 0;
464 }
465
466 static int mii_set_mdio(struct bb_miiphy_bus *bus, int v)
467 {
468         struct fpga_mii *fpga_mii = bus->priv;
469
470         if (v)
471                 FPGA_SET_REG(fpga_mii->fpga, gpio.set, GPIO_MDIO);
472         else
473                 FPGA_SET_REG(fpga_mii->fpga, gpio.clear, GPIO_MDIO);
474
475         fpga_mii->mdio = v;
476
477         return 0;
478 }
479
480 static int mii_get_mdio(struct bb_miiphy_bus *bus, int *v)
481 {
482         u16 gpio;
483         struct fpga_mii *fpga_mii = bus->priv;
484
485         FPGA_GET_REG(fpga_mii->fpga, gpio.read, &gpio);
486
487         *v = ((gpio & GPIO_MDIO) != 0);
488
489         return 0;
490 }
491
492 static int mii_set_mdc(struct bb_miiphy_bus *bus, int v)
493 {
494         struct fpga_mii *fpga_mii = bus->priv;
495
496         if (v)
497                 FPGA_SET_REG(fpga_mii->fpga, gpio.set, GPIO_MDC);
498         else
499                 FPGA_SET_REG(fpga_mii->fpga, gpio.clear, GPIO_MDC);
500
501         return 0;
502 }
503
504 static int mii_delay(struct bb_miiphy_bus *bus)
505 {
506         udelay(1);
507
508         return 0;
509 }
510
511 struct bb_miiphy_bus bb_miiphy_buses[] = {
512         {
513                 .name = "board0",
514                 .init = mii_dummy_init,
515                 .mdio_active = mii_mdio_active,
516                 .mdio_tristate = mii_mdio_tristate,
517                 .set_mdio = mii_set_mdio,
518                 .get_mdio = mii_get_mdio,
519                 .set_mdc = mii_set_mdc,
520                 .delay = mii_delay,
521                 .priv = &fpga_mii[0],
522         },
523         {
524                 .name = "board1",
525                 .init = mii_dummy_init,
526                 .mdio_active = mii_mdio_active,
527                 .mdio_tristate = mii_mdio_tristate,
528                 .set_mdio = mii_set_mdio,
529                 .get_mdio = mii_get_mdio,
530                 .set_mdc = mii_set_mdc,
531                 .delay = mii_delay,
532                 .priv = &fpga_mii[1],
533         },
534         {
535                 .name = "board2",
536                 .init = mii_dummy_init,
537                 .mdio_active = mii_mdio_active,
538                 .mdio_tristate = mii_mdio_tristate,
539                 .set_mdio = mii_set_mdio,
540                 .get_mdio = mii_get_mdio,
541                 .set_mdc = mii_set_mdc,
542                 .delay = mii_delay,
543                 .priv = &fpga_mii[2],
544         },
545         {
546                 .name = "board3",
547                 .init = mii_dummy_init,
548                 .mdio_active = mii_mdio_active,
549                 .mdio_tristate = mii_mdio_tristate,
550                 .set_mdio = mii_set_mdio,
551                 .get_mdio = mii_get_mdio,
552                 .set_mdc = mii_set_mdc,
553                 .delay = mii_delay,
554                 .priv = &fpga_mii[3],
555         },
556 };
557
558 int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);