blob: e8ec240dd7df3aeec2e4bdd10e1882b903e8fe6f [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>
Marek Behún455ef5b2020-04-08 19:25:22 +02007#include <asm/arch/cpu.h>
Marek Behún1055ce12020-04-08 12:02:07 +02008#include <asm/arch/soc.h>
Simon Glass274e0b02020-05-10 11:39:56 -06009#include <net.h>
Marek Behún9602a8c2018-08-21 12:22:09 +020010#include <asm/io.h>
Marek Behún28e935f2020-04-08 12:02:08 +020011#include <asm/gpio.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020012#include <clk.h>
Marek Behún28e935f2020-04-08 12:02:08 +020013#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060014#include <env.h>
Marek Behún9602a8c2018-08-21 12:22:09 +020015#include <fdt_support.h>
Marek Behún28e935f2020-04-08 12:02:08 +020016#include <init.h>
17#include <linux/libfdt.h>
18#include <linux/string.h>
19#include <miiphy.h>
20#include <mvebu/comphy.h>
21#include <spi.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020022
Marek Behún35572c92018-12-17 16:10:08 +010023#include "mox_sp.h"
24
Marek Behún9602a8c2018-08-21 12:22:09 +020025#define MAX_MOX_MODULES 10
26
27#define MOX_MODULE_SFP 0x1
28#define MOX_MODULE_PCI 0x2
29#define MOX_MODULE_TOPAZ 0x3
30#define MOX_MODULE_PERIDOT 0x4
31#define MOX_MODULE_USB3 0x5
32#define MOX_MODULE_PASSPCI 0x6
33
Marek Behún1055ce12020-04-08 12:02:07 +020034#define ARMADA_37XX_NB_GPIO_SEL (MVEBU_REGISTER(0x13830))
35#define ARMADA_37XX_SPI_CTRL (MVEBU_REGISTER(0x10600))
36#define ARMADA_37XX_SPI_CFG (MVEBU_REGISTER(0x10604))
37#define ARMADA_37XX_SPI_DOUT (MVEBU_REGISTER(0x10608))
38#define ARMADA_37XX_SPI_DIN (MVEBU_REGISTER(0x1060c))
Marek Behún9602a8c2018-08-21 12:22:09 +020039
Marek Behún4529fce2020-04-08 12:02:05 +020040#define ETH1_PATH "/soc/internal-regs@d0000000/ethernet@40000"
41#define MDIO_PATH "/soc/internal-regs@d0000000/mdio@32004"
42#define SFP_GPIO_PATH "/soc/internal-regs@d0000000/spi@10600/moxtet@1/gpio@0"
Marek Behún9602a8c2018-08-21 12:22:09 +020043#define PCIE_PATH "/soc/pcie@d0070000"
Marek Behún4529fce2020-04-08 12:02:05 +020044#define SFP_PATH "/sfp"
Marek Behún9602a8c2018-08-21 12:22:09 +020045
Marek Behúnf835bed2018-04-24 17:21:31 +020046DECLARE_GLOBAL_DATA_PTR;
47
Marek Behún9602a8c2018-08-21 12:22:09 +020048#if defined(CONFIG_OF_BOARD_FIXUP)
49int board_fix_fdt(void *blob)
50{
51 u8 topology[MAX_MOX_MODULES];
52 int i, size, node;
53 bool enable;
54
55 /*
56 * SPI driver is not loaded in driver model yet, but we have to find out
57 * if pcie should be enabled in U-Boot's device tree. Therefore we have
58 * to read SPI by reading/writing SPI registers directly
59 */
60
Marek Behún9602a8c2018-08-21 12:22:09 +020061 writel(0x10df, ARMADA_37XX_SPI_CFG);
Marek Behúna4e697f2020-04-08 12:02:03 +020062 /* put pin from GPIO to SPI mode */
63 clrbits_le32(ARMADA_37XX_NB_GPIO_SEL, BIT(12));
64 /* enable SPI CS1 */
65 setbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +020066
67 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
68 udelay(1);
69
70 for (i = 0; i < MAX_MOX_MODULES; ++i) {
71 writel(0x0, ARMADA_37XX_SPI_DOUT);
72
73 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
74 udelay(1);
75
76 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
77 if (topology[i] == 0xff)
78 break;
79
80 topology[i] &= 0xf;
81 }
82
83 size = i;
84
Marek Behúna4e697f2020-04-08 12:02:03 +020085 /* disable SPI CS1 */
86 clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +020087
88 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
89 topology[1] == MOX_MODULE_USB3 ||
90 topology[1] == MOX_MODULE_PASSPCI))
91 enable = true;
92 else
93 enable = false;
94
95 node = fdt_path_offset(blob, PCIE_PATH);
96
97 if (node < 0) {
98 printf("Cannot find PCIe node in U-Boot's device tree!\n");
99 return 0;
100 }
101
102 if (fdt_setprop_string(blob, node, "status",
103 enable ? "okay" : "disabled") < 0) {
104 printf("Cannot %s PCIe in U-Boot's device tree!\n",
105 enable ? "enable" : "disable");
106 return 0;
107 }
108
Marek Behún455ef5b2020-04-08 19:25:22 +0200109 if (a3700_fdt_fix_pcie_regions(blob) < 0) {
110 printf("Cannot fix PCIe regions in U-Boot's device tree!\n");
111 return 0;
112 }
113
Marek Behún9602a8c2018-08-21 12:22:09 +0200114 return 0;
115}
116#endif
117
Marek Behúnf835bed2018-04-24 17:21:31 +0200118int board_init(void)
119{
120 /* address of boot parameters */
121 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
122
Marek Behúnf835bed2018-04-24 17:21:31 +0200123 return 0;
124}
125
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100126static int mox_do_spi(u8 *in, u8 *out, size_t size)
Marek Behúnf835bed2018-04-24 17:21:31 +0200127{
128 struct spi_slave *slave;
129 struct udevice *dev;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100130 int ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200131
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100132 ret = spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
133 "spi_generic_drv", "moxtet@1", &dev,
134 &slave);
Marek Behúnf835bed2018-04-24 17:21:31 +0200135 if (ret)
136 goto fail;
137
138 ret = spi_claim_bus(slave);
139 if (ret)
140 goto fail_free;
141
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100142 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
143
144 spi_release_bus(slave);
145fail_free:
146 spi_free_slave(slave);
147fail:
148 return ret;
149}
150
151static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
152{
153 static int is_sd;
154 static u8 topology[MAX_MOX_MODULES - 1];
155 static int size;
156 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
157 int ret, i;
158
159 if (size) {
160 if (ptopology)
161 *ptopology = topology;
162 if (psize)
163 *psize = size;
164 if (pis_sd)
165 *pis_sd = is_sd;
166 return 0;
167 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200168
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100169 memset(din, 0, MAX_MOX_MODULES);
170 memset(dout, 0, MAX_MOX_MODULES);
171
172 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
Marek Behúnf835bed2018-04-24 17:21:31 +0200173 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100174 return ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200175
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100176 if (din[0] == 0x10)
177 is_sd = 1;
178 else if (din[0] == 0x00)
179 is_sd = 0;
180 else
181 return -ENODEV;
Marek Behúnf835bed2018-04-24 17:21:31 +0200182
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100183 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
184 topology[i - 1] = din[i] & 0xf;
185 size = i - 1;
186
187 if (ptopology)
188 *ptopology = topology;
189 if (psize)
190 *psize = size;
191 if (pis_sd)
192 *pis_sd = is_sd;
193
194 return 0;
195}
196
Marek Behúna261d132018-12-17 16:10:02 +0100197int comphy_update_map(struct comphy_map *serdes_map, int count)
198{
199 int ret, i, size, sfpindex = -1, swindex = -1;
200 const u8 *topology;
201
202 ret = mox_get_topology(&topology, &size, NULL);
203 if (ret)
204 return ret;
205
206 for (i = 0; i < size; ++i) {
207 if (topology[i] == MOX_MODULE_SFP && sfpindex == -1)
208 sfpindex = i;
209 else if ((topology[i] == MOX_MODULE_TOPAZ ||
210 topology[i] == MOX_MODULE_PERIDOT) &&
211 swindex == -1)
212 swindex = i;
213 }
214
215 if (sfpindex >= 0 && swindex >= 0) {
216 if (sfpindex < swindex)
217 serdes_map[0].speed = PHY_SPEED_1_25G;
218 else
219 serdes_map[0].speed = PHY_SPEED_3_125G;
220 } else if (sfpindex >= 0) {
221 serdes_map[0].speed = PHY_SPEED_1_25G;
222 } else if (swindex >= 0) {
223 serdes_map[0].speed = PHY_SPEED_3_125G;
224 }
225
226 return 0;
227}
228
Marek Behún35b35132018-12-17 16:10:03 +0100229#define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
230#define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
231
232static int sw_multi_read(struct mii_dev *bus, int sw, int dev, int reg)
233{
234 bus->write(bus, sw, 0, 0, SW_SMI_CMD_R(dev, reg));
235 mdelay(5);
236 return bus->read(bus, sw, 0, 1);
237}
238
239static void sw_multi_write(struct mii_dev *bus, int sw, int dev, int reg,
240 u16 val)
241{
242 bus->write(bus, sw, 0, 1, val);
243 bus->write(bus, sw, 0, 0, SW_SMI_CMD_W(dev, reg));
244 mdelay(5);
245}
246
247static int sw_scratch_read(struct mii_dev *bus, int sw, int reg)
248{
249 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
250 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
251}
252
253static void sw_led_write(struct mii_dev *bus, int sw, int port, int reg,
254 u16 val)
255{
256 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
257 | (val & 0x7ff));
258}
259
260static void sw_blink_leds(struct mii_dev *bus, int peridot, int topaz)
261{
262 int i, p;
263 struct {
264 int port;
265 u16 val;
266 int wait;
267 } regs[] = {
268 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
269 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
270 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
271 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
272 };
273
274 for (i = 0; i < 12; ++i) {
275 for (p = 0; p < peridot; ++p) {
276 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
277 regs[i].val);
278 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
279 regs[i].val);
280 }
281 if (topaz) {
282 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
283 regs[i].val);
284 }
285
286 if (regs[i].wait)
287 mdelay(75);
288 }
289}
290
291static void check_switch_address(struct mii_dev *bus, int addr)
292{
293 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
294 printf("Check of switch MDIO address failed for 0x%02x\n",
295 addr);
296}
297
298static int sfp, pci, topaz, peridot, usb, passpci;
299static int sfp_pos, peridot_pos[3];
300static int module_count;
301
302static int configure_peridots(struct gpio_desc *reset_gpio)
303{
304 int i, ret;
305 u8 dout[MAX_MOX_MODULES];
306
307 memset(dout, 0, MAX_MOX_MODULES);
308
309 /* set addresses of Peridot modules */
310 for (i = 0; i < peridot; ++i)
311 dout[module_count - peridot_pos[i]] = (~i) & 3;
312
313 /*
314 * if there is a SFP module connected to the last Peridot module, set
315 * the P10_SMODE to 1 for the Peridot module
316 */
317 if (sfp)
318 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
319
320 dm_gpio_set_value(reset_gpio, 1);
321 mdelay(10);
322
323 ret = mox_do_spi(NULL, dout, module_count + 1);
324
325 mdelay(10);
326 dm_gpio_set_value(reset_gpio, 0);
327
328 mdelay(50);
329
330 return ret;
331}
332
333static int get_reset_gpio(struct gpio_desc *reset_gpio)
334{
335 int node;
336
337 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
338 if (node < 0) {
339 printf("Cannot find Moxtet bus device node!\n");
340 return -1;
341 }
342
343 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
344 reset_gpio, GPIOD_IS_OUT);
345
346 if (!dm_gpio_is_valid(reset_gpio)) {
347 printf("Cannot find reset GPIO for Moxtet bus!\n");
348 return -1;
349 }
350
Marek Behún35572c92018-12-17 16:10:08 +0100351 return 0;
352}
353
354int misc_init_r(void)
355{
356 int ret;
357 u8 mac1[6], mac2[6];
358
359 ret = mbox_sp_get_board_info(NULL, mac1, mac2, NULL, NULL);
360 if (ret < 0) {
361 printf("Cannot read data from OTP!\n");
362 return 0;
363 }
364
365 if (is_valid_ethaddr(mac1) && !env_get("ethaddr"))
366 eth_env_set_enetaddr("ethaddr", mac1);
367
368 if (is_valid_ethaddr(mac2) && !env_get("eth1addr"))
369 eth_env_set_enetaddr("eth1addr", mac2);
370
Marek Behún35b35132018-12-17 16:10:03 +0100371 return 0;
372}
373
Marek Behún35572c92018-12-17 16:10:08 +0100374static void mox_print_info(void)
375{
376 int ret, board_version, ram_size;
377 u64 serial_number;
378 const char *pub_key;
379
380 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
381 &ram_size);
382 if (ret < 0)
383 return;
384
385 printf("Turris Mox:\n");
386 printf(" Board version: %i\n", board_version);
387 printf(" RAM size: %i MiB\n", ram_size);
388 printf(" Serial Number: %016llX\n", serial_number);
389
390 pub_key = mox_sp_get_ecdsa_public_key();
391 if (pub_key)
392 printf(" ECDSA Public Key: %s\n", pub_key);
393 else
394 printf("Cannot read ECDSA Public Key\n");
395}
396
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100397int last_stage_init(void)
398{
399 int ret, i;
400 const u8 *topology;
Marek Behún35b35132018-12-17 16:10:03 +0100401 int is_sd;
402 struct mii_dev *bus;
403 struct gpio_desc reset_gpio = {};
Marek Behúnf835bed2018-04-24 17:21:31 +0200404
Marek Behún35572c92018-12-17 16:10:08 +0100405 mox_print_info();
406
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100407 ret = mox_get_topology(&topology, &module_count, &is_sd);
408 if (ret) {
409 printf("Cannot read module topology!\n");
410 return 0;
411 }
412
Marek Behún35572c92018-12-17 16:10:08 +0100413 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
414
415 if (module_count)
416 printf("Module Topology:\n");
417
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100418 for (i = 0; i < module_count; ++i) {
419 switch (topology[i]) {
420 case MOX_MODULE_SFP:
421 printf("% 4i: SFP Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200422 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100423 case MOX_MODULE_PCI:
424 printf("% 4i: Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200425 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100426 case MOX_MODULE_TOPAZ:
427 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
428 break;
429 case MOX_MODULE_PERIDOT:
430 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
431 break;
432 case MOX_MODULE_USB3:
433 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
434 break;
435 case MOX_MODULE_PASSPCI:
436 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200437 break;
438 default:
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100439 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
Marek Behúnf835bed2018-04-24 17:21:31 +0200440 }
441 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200442
Marek Behún35b35132018-12-17 16:10:03 +0100443 /* now check if modules are connected in supported mode */
444
445 for (i = 0; i < module_count; ++i) {
446 switch (topology[i]) {
447 case MOX_MODULE_SFP:
448 if (sfp) {
449 printf("Error: Only one SFP module is supported!\n");
450 } else if (topaz) {
451 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
452 } else {
453 sfp_pos = i;
454 ++sfp;
455 }
456 break;
457 case MOX_MODULE_PCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200458 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100459 printf("Error: Only one Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200460 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100461 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200462 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100463 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 +0200464 else
Marek Behún35b35132018-12-17 16:10:03 +0100465 ++pci;
Marek Behún35b35132018-12-17 16:10:03 +0100466 break;
467 case MOX_MODULE_TOPAZ:
Marek Behún4529fce2020-04-08 12:02:05 +0200468 if (topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100469 printf("Error: Only one Topaz module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200470 else if (peridot >= 3)
Marek Behún35b35132018-12-17 16:10:03 +0100471 printf("Error: At most two Peridot modules can come before Topaz module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200472 else
Marek Behún35b35132018-12-17 16:10:03 +0100473 ++topaz;
Marek Behún35b35132018-12-17 16:10:03 +0100474 break;
475 case MOX_MODULE_PERIDOT:
476 if (sfp || topaz) {
477 printf("Error: Peridot module must come before SFP or Topaz module!\n");
478 } else if (peridot >= 3) {
479 printf("Error: At most three Peridot modules are supported!\n");
480 } else {
481 peridot_pos[peridot] = i;
482 ++peridot;
483 }
484 break;
485 case MOX_MODULE_USB3:
Marek Behún4529fce2020-04-08 12:02:05 +0200486 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100487 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200488 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100489 printf("Error: Only one USB 3.0 module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200490 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100491 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 +0200492 else
Marek Behún35b35132018-12-17 16:10:03 +0100493 ++usb;
Marek Behún35b35132018-12-17 16:10:03 +0100494 break;
495 case MOX_MODULE_PASSPCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200496 if (passpci)
Marek Behún35b35132018-12-17 16:10:03 +0100497 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200498 else if (i != 0)
Marek Behún35b35132018-12-17 16:10:03 +0100499 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200500 else
Marek Behún35b35132018-12-17 16:10:03 +0100501 ++passpci;
Marek Behún35b35132018-12-17 16:10:03 +0100502 }
503 }
504
505 /* now configure modules */
506
507 if (get_reset_gpio(&reset_gpio) < 0)
508 return 0;
509
510 if (peridot > 0) {
511 if (configure_peridots(&reset_gpio) < 0) {
512 printf("Cannot configure Peridot modules!\n");
513 peridot = 0;
514 }
515 } else {
516 dm_gpio_set_value(&reset_gpio, 1);
517 mdelay(50);
518 dm_gpio_set_value(&reset_gpio, 0);
519 mdelay(50);
520 }
521
522 if (peridot || topaz) {
523 /*
524 * now check if the addresses are set by reading Scratch & Misc
525 * register 0x70 of Peridot (and potentially Topaz) modules
526 */
527
528 bus = miiphy_get_dev_by_name("neta@30000");
529 if (!bus) {
530 printf("Cannot get MDIO bus device!\n");
531 } else {
532 for (i = 0; i < peridot; ++i)
533 check_switch_address(bus, 0x10 + i);
534
535 if (topaz)
536 check_switch_address(bus, 0x2);
537
538 sw_blink_leds(bus, peridot, topaz);
539 }
540 }
541
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100542 printf("\n");
Marek Behúnf835bed2018-04-24 17:21:31 +0200543
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100544 return 0;
Marek Behúnf835bed2018-04-24 17:21:31 +0200545}
Marek Behún4529fce2020-04-08 12:02:05 +0200546
547#if defined(CONFIG_OF_BOARD_SETUP)
548
549static int vnode_by_path(void *blob, const char *fmt, va_list ap)
550{
551 char path[128];
552
553 vsnprintf(path, 128, fmt, ap);
554 return fdt_path_offset(blob, path);
555}
556
557static int node_by_path(void *blob, const char *fmt, ...)
558{
559 va_list ap;
560 int res;
561
562 va_start(ap, fmt);
563 res = vnode_by_path(blob, fmt, ap);
564 va_end(ap);
565
566 return res;
567}
568
569static int phandle_by_path(void *blob, const char *fmt, ...)
570{
571 va_list ap;
572 int node, phandle, res;
573
574 va_start(ap, fmt);
575 node = vnode_by_path(blob, fmt, ap);
576 va_end(ap);
577
578 if (node < 0)
579 return node;
580
581 phandle = fdt_get_phandle(blob, node);
582 if (phandle > 0)
583 return phandle;
584
585 phandle = fdt_get_max_phandle(blob);
586 if (phandle < 0)
587 return phandle;
588
589 phandle += 1;
590
591 res = fdt_setprop_u32(blob, node, "linux,phandle", phandle);
592 if (res < 0)
593 return res;
594
595 res = fdt_setprop_u32(blob, node, "phandle", phandle);
596 if (res < 0)
597 return res;
598
599 return phandle;
600}
601
602static int enable_by_path(void *blob, const char *fmt, ...)
603{
604 va_list ap;
605 int node;
606
607 va_start(ap, fmt);
608 node = vnode_by_path(blob, fmt, ap);
609 va_end(ap);
610
611 if (node < 0)
612 return node;
613
614 return fdt_setprop_string(blob, node, "status", "okay");
615}
616
617static bool is_topaz(int id)
618{
619 return topaz && id == peridot + topaz - 1;
620}
621
622static int switch_addr(int id)
623{
624 return is_topaz(id) ? 0x2 : 0x10 + id;
625}
626
627static int setup_switch(void *blob, int id)
628{
629 int res, addr, i, node, phandle;
630
631 addr = switch_addr(id);
632
633 /* first enable the switch by setting status = "okay" */
634 res = enable_by_path(blob, MDIO_PATH "/switch%i@%x", id, addr);
635 if (res < 0)
636 return res;
637
638 /*
639 * now if there are more switches or a SFP module coming after,
640 * enable corresponding ports
641 */
642 if (id < peridot + topaz - 1) {
643 res = enable_by_path(blob,
644 MDIO_PATH "/switch%i@%x/ports/port@a",
645 id, addr);
646 } else if (id == peridot - 1 && !topaz && sfp) {
647 res = enable_by_path(blob,
648 MDIO_PATH "/switch%i@%x/ports/port-sfp@a",
649 id, addr);
650 } else {
651 res = 0;
652 }
653 if (res < 0)
654 return res;
655
656 if (id >= peridot + topaz - 1)
657 return 0;
658
659 /* finally change link property if needed */
660 node = node_by_path(blob, MDIO_PATH "/switch%i@%x/ports/port@a", id,
661 addr);
662 if (node < 0)
663 return node;
664
665 for (i = id + 1; i < peridot + topaz; ++i) {
666 phandle = phandle_by_path(blob,
667 MDIO_PATH "/switch%i@%x/ports/port@%x",
668 i, switch_addr(i),
669 is_topaz(i) ? 5 : 9);
670 if (phandle < 0)
671 return phandle;
672
673 if (i == id + 1)
674 res = fdt_setprop_u32(blob, node, "link", phandle);
675 else
676 res = fdt_appendprop_u32(blob, node, "link", phandle);
677 if (res < 0)
678 return res;
679 }
680
681 return 0;
682}
683
684static int remove_disabled_nodes(void *blob)
685{
686 while (1) {
687 int res, offset;
688
689 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
690 "disabled", 9);
691 if (offset < 0)
692 break;
693
694 res = fdt_del_node(blob, offset);
695 if (res < 0)
696 return res;
697 }
698
699 return 0;
700}
701
702int ft_board_setup(void *blob, bd_t *bd)
703{
704 int node, phandle, res;
705
706 /*
707 * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
708 * connected, enable the PCIe node.
709 */
710 if (pci || usb || passpci) {
711 node = fdt_path_offset(blob, PCIE_PATH);
712 if (node < 0)
713 return node;
714
715 res = fdt_setprop_string(blob, node, "status", "okay");
716 if (res < 0)
717 return res;
Marek Behún455ef5b2020-04-08 19:25:22 +0200718
719 /* Fix PCIe regions for devices with 4 GB RAM */
720 res = a3700_fdt_fix_pcie_regions(blob);
721 if (res < 0)
722 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200723 }
724
725 /*
726 * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
727 * enable the eth1 node and setup the switches.
728 */
729 if (peridot || topaz) {
730 int i;
731
732 res = enable_by_path(blob, ETH1_PATH);
733 if (res < 0)
734 return res;
735
736 for (i = 0; i < peridot + topaz; ++i) {
737 res = setup_switch(blob, i);
738 if (res < 0)
739 return res;
740 }
741 }
742
743 /*
744 * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
745 * node. If there is no Peridot switch between MOX A and MOX D, add link
746 * to the SFP node to eth1 node.
747 * Also enable and configure SFP GPIO controller node.
748 */
749 if (sfp) {
750 res = enable_by_path(blob, SFP_PATH);
751 if (res < 0)
752 return res;
753
754 res = enable_by_path(blob, ETH1_PATH);
755 if (res < 0)
756 return res;
757
758 if (!peridot) {
759 phandle = phandle_by_path(blob, SFP_PATH);
760 if (phandle < 0)
761 return res;
762
763 node = node_by_path(blob, ETH1_PATH);
764 if (node < 0)
765 return node;
766
767 res = fdt_setprop_u32(blob, node, "sfp", phandle);
768 if (res < 0)
769 return res;
770
771 res = fdt_setprop_string(blob, node, "phy-mode",
772 "sgmii");
773 if (res < 0)
774 return res;
775 }
776
777 res = enable_by_path(blob, SFP_GPIO_PATH);
778 if (res < 0)
779 return res;
780
781 if (sfp_pos) {
782 char newname[16];
783
784 /* moxtet-sfp is on non-zero position, change default */
785 node = node_by_path(blob, SFP_GPIO_PATH);
786 if (node < 0)
787 return node;
788
789 res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
790 if (res < 0)
791 return res;
792
793 sprintf(newname, "gpio@%x", sfp_pos);
794
795 res = fdt_set_name(blob, node, newname);
796 if (res < 0)
797 return res;
798 }
799 }
800
801 fdt_fixup_ethernet(blob);
802
803 /* Finally remove disabled nodes, as per Rob Herring's request. */
804 remove_disabled_nodes(blob);
805
806 return 0;
807}
808
809#endif