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