blob: 6e937826d0273e57318a48dfa45b200921396e7a [file] [log] [blame]
Marek Behúnf835bed2018-04-24 17:21:31 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Marek Behun <marek.behun@nic.cz>
4 */
5
6#include <common.h>
Simon Glassa7b51302019-11-14 12:57:46 -07007#include <init.h>
Marek Behún35b35132018-12-17 16:10:03 +01008#include <asm/gpio.h>
Marek Behún9602a8c2018-08-21 12:22:09 +02009#include <asm/io.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020010#include <dm.h>
11#include <clk.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060012#include <env.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020013#include <spi.h>
Marek Behúna261d132018-12-17 16:10:02 +010014#include <mvebu/comphy.h>
Marek Behún35b35132018-12-17 16:10:03 +010015#include <miiphy.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020016#include <linux/string.h>
Marek Behún9602a8c2018-08-21 12:22:09 +020017#include <linux/libfdt.h>
18#include <fdt_support.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020019
Marek Behún35572c92018-12-17 16:10:08 +010020#include "mox_sp.h"
21
Marek Behún9602a8c2018-08-21 12:22:09 +020022#define MAX_MOX_MODULES 10
23
24#define MOX_MODULE_SFP 0x1
25#define MOX_MODULE_PCI 0x2
26#define MOX_MODULE_TOPAZ 0x3
27#define MOX_MODULE_PERIDOT 0x4
28#define MOX_MODULE_USB3 0x5
29#define MOX_MODULE_PASSPCI 0x6
30
31#define ARMADA_37XX_NB_GPIO_SEL 0xd0013830
32#define ARMADA_37XX_SPI_CTRL 0xd0010600
33#define ARMADA_37XX_SPI_CFG 0xd0010604
34#define ARMADA_37XX_SPI_DOUT 0xd0010608
35#define ARMADA_37XX_SPI_DIN 0xd001060c
36
Marek Behún4529fce2020-04-08 12:02:05 +020037#define ETH1_PATH "/soc/internal-regs@d0000000/ethernet@40000"
38#define MDIO_PATH "/soc/internal-regs@d0000000/mdio@32004"
39#define SFP_GPIO_PATH "/soc/internal-regs@d0000000/spi@10600/moxtet@1/gpio@0"
Marek Behún9602a8c2018-08-21 12:22:09 +020040#define PCIE_PATH "/soc/pcie@d0070000"
Marek Behún4529fce2020-04-08 12:02:05 +020041#define SFP_PATH "/sfp"
Marek Behún9602a8c2018-08-21 12:22:09 +020042
Marek Behúnf835bed2018-04-24 17:21:31 +020043DECLARE_GLOBAL_DATA_PTR;
44
Marek Behún22a0ce32018-12-17 16:10:09 +010045int dram_init(void)
46{
47 gd->ram_base = 0;
48 gd->ram_size = (phys_size_t)get_ram_size(0, 0x40000000);
49
50 return 0;
51}
52
53int dram_init_banksize(void)
54{
55 gd->bd->bi_dram[0].start = (phys_addr_t)0;
56 gd->bd->bi_dram[0].size = gd->ram_size;
57
58 return 0;
59}
60
Marek Behún9602a8c2018-08-21 12:22:09 +020061#if defined(CONFIG_OF_BOARD_FIXUP)
62int board_fix_fdt(void *blob)
63{
64 u8 topology[MAX_MOX_MODULES];
65 int i, size, node;
66 bool enable;
67
68 /*
69 * SPI driver is not loaded in driver model yet, but we have to find out
70 * if pcie should be enabled in U-Boot's device tree. Therefore we have
71 * to read SPI by reading/writing SPI registers directly
72 */
73
Marek Behún9602a8c2018-08-21 12:22:09 +020074 writel(0x10df, ARMADA_37XX_SPI_CFG);
Marek Behúna4e697f2020-04-08 12:02:03 +020075 /* put pin from GPIO to SPI mode */
76 clrbits_le32(ARMADA_37XX_NB_GPIO_SEL, BIT(12));
77 /* enable SPI CS1 */
78 setbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +020079
80 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
81 udelay(1);
82
83 for (i = 0; i < MAX_MOX_MODULES; ++i) {
84 writel(0x0, ARMADA_37XX_SPI_DOUT);
85
86 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
87 udelay(1);
88
89 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
90 if (topology[i] == 0xff)
91 break;
92
93 topology[i] &= 0xf;
94 }
95
96 size = i;
97
Marek Behúna4e697f2020-04-08 12:02:03 +020098 /* disable SPI CS1 */
99 clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +0200100
101 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
102 topology[1] == MOX_MODULE_USB3 ||
103 topology[1] == MOX_MODULE_PASSPCI))
104 enable = true;
105 else
106 enable = false;
107
108 node = fdt_path_offset(blob, PCIE_PATH);
109
110 if (node < 0) {
111 printf("Cannot find PCIe node in U-Boot's device tree!\n");
112 return 0;
113 }
114
115 if (fdt_setprop_string(blob, node, "status",
116 enable ? "okay" : "disabled") < 0) {
117 printf("Cannot %s PCIe in U-Boot's device tree!\n",
118 enable ? "enable" : "disable");
119 return 0;
120 }
121
122 return 0;
123}
124#endif
125
Marek Behúnf835bed2018-04-24 17:21:31 +0200126int board_init(void)
127{
128 /* address of boot parameters */
129 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
130
Marek Behúnf835bed2018-04-24 17:21:31 +0200131 return 0;
132}
133
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100134static int mox_do_spi(u8 *in, u8 *out, size_t size)
Marek Behúnf835bed2018-04-24 17:21:31 +0200135{
136 struct spi_slave *slave;
137 struct udevice *dev;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100138 int ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200139
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100140 ret = spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
141 "spi_generic_drv", "moxtet@1", &dev,
142 &slave);
Marek Behúnf835bed2018-04-24 17:21:31 +0200143 if (ret)
144 goto fail;
145
146 ret = spi_claim_bus(slave);
147 if (ret)
148 goto fail_free;
149
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100150 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
151
152 spi_release_bus(slave);
153fail_free:
154 spi_free_slave(slave);
155fail:
156 return ret;
157}
158
159static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
160{
161 static int is_sd;
162 static u8 topology[MAX_MOX_MODULES - 1];
163 static int size;
164 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
165 int ret, i;
166
167 if (size) {
168 if (ptopology)
169 *ptopology = topology;
170 if (psize)
171 *psize = size;
172 if (pis_sd)
173 *pis_sd = is_sd;
174 return 0;
175 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200176
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100177 memset(din, 0, MAX_MOX_MODULES);
178 memset(dout, 0, MAX_MOX_MODULES);
179
180 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
Marek Behúnf835bed2018-04-24 17:21:31 +0200181 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100182 return ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200183
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100184 if (din[0] == 0x10)
185 is_sd = 1;
186 else if (din[0] == 0x00)
187 is_sd = 0;
188 else
189 return -ENODEV;
Marek Behúnf835bed2018-04-24 17:21:31 +0200190
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100191 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
192 topology[i - 1] = din[i] & 0xf;
193 size = i - 1;
194
195 if (ptopology)
196 *ptopology = topology;
197 if (psize)
198 *psize = size;
199 if (pis_sd)
200 *pis_sd = is_sd;
201
202 return 0;
203}
204
Marek Behúna261d132018-12-17 16:10:02 +0100205int comphy_update_map(struct comphy_map *serdes_map, int count)
206{
207 int ret, i, size, sfpindex = -1, swindex = -1;
208 const u8 *topology;
209
210 ret = mox_get_topology(&topology, &size, NULL);
211 if (ret)
212 return ret;
213
214 for (i = 0; i < size; ++i) {
215 if (topology[i] == MOX_MODULE_SFP && sfpindex == -1)
216 sfpindex = i;
217 else if ((topology[i] == MOX_MODULE_TOPAZ ||
218 topology[i] == MOX_MODULE_PERIDOT) &&
219 swindex == -1)
220 swindex = i;
221 }
222
223 if (sfpindex >= 0 && swindex >= 0) {
224 if (sfpindex < swindex)
225 serdes_map[0].speed = PHY_SPEED_1_25G;
226 else
227 serdes_map[0].speed = PHY_SPEED_3_125G;
228 } else if (sfpindex >= 0) {
229 serdes_map[0].speed = PHY_SPEED_1_25G;
230 } else if (swindex >= 0) {
231 serdes_map[0].speed = PHY_SPEED_3_125G;
232 }
233
234 return 0;
235}
236
Marek Behún35b35132018-12-17 16:10:03 +0100237#define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
238#define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
239
240static int sw_multi_read(struct mii_dev *bus, int sw, int dev, int reg)
241{
242 bus->write(bus, sw, 0, 0, SW_SMI_CMD_R(dev, reg));
243 mdelay(5);
244 return bus->read(bus, sw, 0, 1);
245}
246
247static void sw_multi_write(struct mii_dev *bus, int sw, int dev, int reg,
248 u16 val)
249{
250 bus->write(bus, sw, 0, 1, val);
251 bus->write(bus, sw, 0, 0, SW_SMI_CMD_W(dev, reg));
252 mdelay(5);
253}
254
255static int sw_scratch_read(struct mii_dev *bus, int sw, int reg)
256{
257 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
258 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
259}
260
261static void sw_led_write(struct mii_dev *bus, int sw, int port, int reg,
262 u16 val)
263{
264 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
265 | (val & 0x7ff));
266}
267
268static void sw_blink_leds(struct mii_dev *bus, int peridot, int topaz)
269{
270 int i, p;
271 struct {
272 int port;
273 u16 val;
274 int wait;
275 } regs[] = {
276 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
277 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
278 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
279 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
280 };
281
282 for (i = 0; i < 12; ++i) {
283 for (p = 0; p < peridot; ++p) {
284 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
285 regs[i].val);
286 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
287 regs[i].val);
288 }
289 if (topaz) {
290 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
291 regs[i].val);
292 }
293
294 if (regs[i].wait)
295 mdelay(75);
296 }
297}
298
299static void check_switch_address(struct mii_dev *bus, int addr)
300{
301 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
302 printf("Check of switch MDIO address failed for 0x%02x\n",
303 addr);
304}
305
306static int sfp, pci, topaz, peridot, usb, passpci;
307static int sfp_pos, peridot_pos[3];
308static int module_count;
309
310static int configure_peridots(struct gpio_desc *reset_gpio)
311{
312 int i, ret;
313 u8 dout[MAX_MOX_MODULES];
314
315 memset(dout, 0, MAX_MOX_MODULES);
316
317 /* set addresses of Peridot modules */
318 for (i = 0; i < peridot; ++i)
319 dout[module_count - peridot_pos[i]] = (~i) & 3;
320
321 /*
322 * if there is a SFP module connected to the last Peridot module, set
323 * the P10_SMODE to 1 for the Peridot module
324 */
325 if (sfp)
326 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
327
328 dm_gpio_set_value(reset_gpio, 1);
329 mdelay(10);
330
331 ret = mox_do_spi(NULL, dout, module_count + 1);
332
333 mdelay(10);
334 dm_gpio_set_value(reset_gpio, 0);
335
336 mdelay(50);
337
338 return ret;
339}
340
341static int get_reset_gpio(struct gpio_desc *reset_gpio)
342{
343 int node;
344
345 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
346 if (node < 0) {
347 printf("Cannot find Moxtet bus device node!\n");
348 return -1;
349 }
350
351 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
352 reset_gpio, GPIOD_IS_OUT);
353
354 if (!dm_gpio_is_valid(reset_gpio)) {
355 printf("Cannot find reset GPIO for Moxtet bus!\n");
356 return -1;
357 }
358
Marek Behún35572c92018-12-17 16:10:08 +0100359 return 0;
360}
361
362int misc_init_r(void)
363{
364 int ret;
365 u8 mac1[6], mac2[6];
366
367 ret = mbox_sp_get_board_info(NULL, mac1, mac2, NULL, NULL);
368 if (ret < 0) {
369 printf("Cannot read data from OTP!\n");
370 return 0;
371 }
372
373 if (is_valid_ethaddr(mac1) && !env_get("ethaddr"))
374 eth_env_set_enetaddr("ethaddr", mac1);
375
376 if (is_valid_ethaddr(mac2) && !env_get("eth1addr"))
377 eth_env_set_enetaddr("eth1addr", mac2);
378
Marek Behún35b35132018-12-17 16:10:03 +0100379 return 0;
380}
381
Marek Behún35572c92018-12-17 16:10:08 +0100382static void mox_print_info(void)
383{
384 int ret, board_version, ram_size;
385 u64 serial_number;
386 const char *pub_key;
387
388 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
389 &ram_size);
390 if (ret < 0)
391 return;
392
393 printf("Turris Mox:\n");
394 printf(" Board version: %i\n", board_version);
395 printf(" RAM size: %i MiB\n", ram_size);
396 printf(" Serial Number: %016llX\n", serial_number);
397
398 pub_key = mox_sp_get_ecdsa_public_key();
399 if (pub_key)
400 printf(" ECDSA Public Key: %s\n", pub_key);
401 else
402 printf("Cannot read ECDSA Public Key\n");
403}
404
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100405int last_stage_init(void)
406{
407 int ret, i;
408 const u8 *topology;
Marek Behún35b35132018-12-17 16:10:03 +0100409 int is_sd;
410 struct mii_dev *bus;
411 struct gpio_desc reset_gpio = {};
Marek Behúnf835bed2018-04-24 17:21:31 +0200412
Marek Behún35572c92018-12-17 16:10:08 +0100413 mox_print_info();
414
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100415 ret = mox_get_topology(&topology, &module_count, &is_sd);
416 if (ret) {
417 printf("Cannot read module topology!\n");
418 return 0;
419 }
420
Marek Behún35572c92018-12-17 16:10:08 +0100421 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
422
423 if (module_count)
424 printf("Module Topology:\n");
425
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100426 for (i = 0; i < module_count; ++i) {
427 switch (topology[i]) {
428 case MOX_MODULE_SFP:
429 printf("% 4i: SFP Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200430 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100431 case MOX_MODULE_PCI:
432 printf("% 4i: Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200433 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100434 case MOX_MODULE_TOPAZ:
435 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
436 break;
437 case MOX_MODULE_PERIDOT:
438 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
439 break;
440 case MOX_MODULE_USB3:
441 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
442 break;
443 case MOX_MODULE_PASSPCI:
444 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200445 break;
446 default:
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100447 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
Marek Behúnf835bed2018-04-24 17:21:31 +0200448 }
449 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200450
Marek Behún35b35132018-12-17 16:10:03 +0100451 /* now check if modules are connected in supported mode */
452
453 for (i = 0; i < module_count; ++i) {
454 switch (topology[i]) {
455 case MOX_MODULE_SFP:
456 if (sfp) {
457 printf("Error: Only one SFP module is supported!\n");
458 } else if (topaz) {
459 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
460 } else {
461 sfp_pos = i;
462 ++sfp;
463 }
464 break;
465 case MOX_MODULE_PCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200466 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100467 printf("Error: Only one Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200468 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100469 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200470 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100471 printf("Error: Mini-PCIe module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200472 else
Marek Behún35b35132018-12-17 16:10:03 +0100473 ++pci;
Marek Behún35b35132018-12-17 16:10:03 +0100474 break;
475 case MOX_MODULE_TOPAZ:
Marek Behún4529fce2020-04-08 12:02:05 +0200476 if (topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100477 printf("Error: Only one Topaz module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200478 else if (peridot >= 3)
Marek Behún35b35132018-12-17 16:10:03 +0100479 printf("Error: At most two Peridot modules can come before Topaz module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200480 else
Marek Behún35b35132018-12-17 16:10:03 +0100481 ++topaz;
Marek Behún35b35132018-12-17 16:10:03 +0100482 break;
483 case MOX_MODULE_PERIDOT:
484 if (sfp || topaz) {
485 printf("Error: Peridot module must come before SFP or Topaz module!\n");
486 } else if (peridot >= 3) {
487 printf("Error: At most three Peridot modules are supported!\n");
488 } else {
489 peridot_pos[peridot] = i;
490 ++peridot;
491 }
492 break;
493 case MOX_MODULE_USB3:
Marek Behún4529fce2020-04-08 12:02:05 +0200494 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100495 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200496 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100497 printf("Error: Only one USB 3.0 module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200498 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100499 printf("Error: USB 3.0 module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200500 else
Marek Behún35b35132018-12-17 16:10:03 +0100501 ++usb;
Marek Behún35b35132018-12-17 16:10:03 +0100502 break;
503 case MOX_MODULE_PASSPCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200504 if (passpci)
Marek Behún35b35132018-12-17 16:10:03 +0100505 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200506 else if (i != 0)
Marek Behún35b35132018-12-17 16:10:03 +0100507 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200508 else
Marek Behún35b35132018-12-17 16:10:03 +0100509 ++passpci;
Marek Behún35b35132018-12-17 16:10:03 +0100510 }
511 }
512
513 /* now configure modules */
514
515 if (get_reset_gpio(&reset_gpio) < 0)
516 return 0;
517
518 if (peridot > 0) {
519 if (configure_peridots(&reset_gpio) < 0) {
520 printf("Cannot configure Peridot modules!\n");
521 peridot = 0;
522 }
523 } else {
524 dm_gpio_set_value(&reset_gpio, 1);
525 mdelay(50);
526 dm_gpio_set_value(&reset_gpio, 0);
527 mdelay(50);
528 }
529
530 if (peridot || topaz) {
531 /*
532 * now check if the addresses are set by reading Scratch & Misc
533 * register 0x70 of Peridot (and potentially Topaz) modules
534 */
535
536 bus = miiphy_get_dev_by_name("neta@30000");
537 if (!bus) {
538 printf("Cannot get MDIO bus device!\n");
539 } else {
540 for (i = 0; i < peridot; ++i)
541 check_switch_address(bus, 0x10 + i);
542
543 if (topaz)
544 check_switch_address(bus, 0x2);
545
546 sw_blink_leds(bus, peridot, topaz);
547 }
548 }
549
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100550 printf("\n");
Marek Behúnf835bed2018-04-24 17:21:31 +0200551
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100552 return 0;
Marek Behúnf835bed2018-04-24 17:21:31 +0200553}
Marek Behún4529fce2020-04-08 12:02:05 +0200554
555#if defined(CONFIG_OF_BOARD_SETUP)
556
557static int vnode_by_path(void *blob, const char *fmt, va_list ap)
558{
559 char path[128];
560
561 vsnprintf(path, 128, fmt, ap);
562 return fdt_path_offset(blob, path);
563}
564
565static int node_by_path(void *blob, const char *fmt, ...)
566{
567 va_list ap;
568 int res;
569
570 va_start(ap, fmt);
571 res = vnode_by_path(blob, fmt, ap);
572 va_end(ap);
573
574 return res;
575}
576
577static int phandle_by_path(void *blob, const char *fmt, ...)
578{
579 va_list ap;
580 int node, phandle, res;
581
582 va_start(ap, fmt);
583 node = vnode_by_path(blob, fmt, ap);
584 va_end(ap);
585
586 if (node < 0)
587 return node;
588
589 phandle = fdt_get_phandle(blob, node);
590 if (phandle > 0)
591 return phandle;
592
593 phandle = fdt_get_max_phandle(blob);
594 if (phandle < 0)
595 return phandle;
596
597 phandle += 1;
598
599 res = fdt_setprop_u32(blob, node, "linux,phandle", phandle);
600 if (res < 0)
601 return res;
602
603 res = fdt_setprop_u32(blob, node, "phandle", phandle);
604 if (res < 0)
605 return res;
606
607 return phandle;
608}
609
610static int enable_by_path(void *blob, const char *fmt, ...)
611{
612 va_list ap;
613 int node;
614
615 va_start(ap, fmt);
616 node = vnode_by_path(blob, fmt, ap);
617 va_end(ap);
618
619 if (node < 0)
620 return node;
621
622 return fdt_setprop_string(blob, node, "status", "okay");
623}
624
625static bool is_topaz(int id)
626{
627 return topaz && id == peridot + topaz - 1;
628}
629
630static int switch_addr(int id)
631{
632 return is_topaz(id) ? 0x2 : 0x10 + id;
633}
634
635static int setup_switch(void *blob, int id)
636{
637 int res, addr, i, node, phandle;
638
639 addr = switch_addr(id);
640
641 /* first enable the switch by setting status = "okay" */
642 res = enable_by_path(blob, MDIO_PATH "/switch%i@%x", id, addr);
643 if (res < 0)
644 return res;
645
646 /*
647 * now if there are more switches or a SFP module coming after,
648 * enable corresponding ports
649 */
650 if (id < peridot + topaz - 1) {
651 res = enable_by_path(blob,
652 MDIO_PATH "/switch%i@%x/ports/port@a",
653 id, addr);
654 } else if (id == peridot - 1 && !topaz && sfp) {
655 res = enable_by_path(blob,
656 MDIO_PATH "/switch%i@%x/ports/port-sfp@a",
657 id, addr);
658 } else {
659 res = 0;
660 }
661 if (res < 0)
662 return res;
663
664 if (id >= peridot + topaz - 1)
665 return 0;
666
667 /* finally change link property if needed */
668 node = node_by_path(blob, MDIO_PATH "/switch%i@%x/ports/port@a", id,
669 addr);
670 if (node < 0)
671 return node;
672
673 for (i = id + 1; i < peridot + topaz; ++i) {
674 phandle = phandle_by_path(blob,
675 MDIO_PATH "/switch%i@%x/ports/port@%x",
676 i, switch_addr(i),
677 is_topaz(i) ? 5 : 9);
678 if (phandle < 0)
679 return phandle;
680
681 if (i == id + 1)
682 res = fdt_setprop_u32(blob, node, "link", phandle);
683 else
684 res = fdt_appendprop_u32(blob, node, "link", phandle);
685 if (res < 0)
686 return res;
687 }
688
689 return 0;
690}
691
692static int remove_disabled_nodes(void *blob)
693{
694 while (1) {
695 int res, offset;
696
697 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
698 "disabled", 9);
699 if (offset < 0)
700 break;
701
702 res = fdt_del_node(blob, offset);
703 if (res < 0)
704 return res;
705 }
706
707 return 0;
708}
709
710int ft_board_setup(void *blob, bd_t *bd)
711{
712 int node, phandle, res;
713
714 /*
715 * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
716 * connected, enable the PCIe node.
717 */
718 if (pci || usb || passpci) {
719 node = fdt_path_offset(blob, PCIE_PATH);
720 if (node < 0)
721 return node;
722
723 res = fdt_setprop_string(blob, node, "status", "okay");
724 if (res < 0)
725 return res;
726 }
727
728 /*
729 * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
730 * enable the eth1 node and setup the switches.
731 */
732 if (peridot || topaz) {
733 int i;
734
735 res = enable_by_path(blob, ETH1_PATH);
736 if (res < 0)
737 return res;
738
739 for (i = 0; i < peridot + topaz; ++i) {
740 res = setup_switch(blob, i);
741 if (res < 0)
742 return res;
743 }
744 }
745
746 /*
747 * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
748 * node. If there is no Peridot switch between MOX A and MOX D, add link
749 * to the SFP node to eth1 node.
750 * Also enable and configure SFP GPIO controller node.
751 */
752 if (sfp) {
753 res = enable_by_path(blob, SFP_PATH);
754 if (res < 0)
755 return res;
756
757 res = enable_by_path(blob, ETH1_PATH);
758 if (res < 0)
759 return res;
760
761 if (!peridot) {
762 phandle = phandle_by_path(blob, SFP_PATH);
763 if (phandle < 0)
764 return res;
765
766 node = node_by_path(blob, ETH1_PATH);
767 if (node < 0)
768 return node;
769
770 res = fdt_setprop_u32(blob, node, "sfp", phandle);
771 if (res < 0)
772 return res;
773
774 res = fdt_setprop_string(blob, node, "phy-mode",
775 "sgmii");
776 if (res < 0)
777 return res;
778 }
779
780 res = enable_by_path(blob, SFP_GPIO_PATH);
781 if (res < 0)
782 return res;
783
784 if (sfp_pos) {
785 char newname[16];
786
787 /* moxtet-sfp is on non-zero position, change default */
788 node = node_by_path(blob, SFP_GPIO_PATH);
789 if (node < 0)
790 return node;
791
792 res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
793 if (res < 0)
794 return res;
795
796 sprintf(newname, "gpio@%x", sfp_pos);
797
798 res = fdt_set_name(blob, node, newname);
799 if (res < 0)
800 return res;
801 }
802 }
803
804 fdt_fixup_ethernet(blob);
805
806 /* Finally remove disabled nodes, as per Rob Herring's request. */
807 remove_disabled_nodes(blob);
808
809 return 0;
810}
811
812#endif