blob: 9702d1fc7822bc07af9afb40f786419e57516e11 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Marek Behún9602a8c2018-08-21 12:22:09 +020011#include <asm/io.h>
Marek Behún28e935f2020-04-08 12:02:08 +020012#include <asm/gpio.h>
Marek Behún859ada52021-06-07 16:34:47 +020013#include <button.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020014#include <clk.h>
Marek Behún28e935f2020-04-08 12:02:08 +020015#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060016#include <env.h>
Marek Behún9602a8c2018-08-21 12:22:09 +020017#include <fdt_support.h>
Marek Behún28e935f2020-04-08 12:02:08 +020018#include <init.h>
Marek Behún859ada52021-06-07 16:34:47 +020019#include <led.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Marek Behún28e935f2020-04-08 12:02:08 +020021#include <linux/libfdt.h>
22#include <linux/string.h>
23#include <miiphy.h>
Marek Behún28e935f2020-04-08 12:02:08 +020024#include <spi.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020025
Marek Behún35572c92018-12-17 16:10:08 +010026#include "mox_sp.h"
27
Marek Behún9602a8c2018-08-21 12:22:09 +020028#define MAX_MOX_MODULES 10
29
30#define MOX_MODULE_SFP 0x1
31#define MOX_MODULE_PCI 0x2
32#define MOX_MODULE_TOPAZ 0x3
33#define MOX_MODULE_PERIDOT 0x4
34#define MOX_MODULE_USB3 0x5
35#define MOX_MODULE_PASSPCI 0x6
36
Marek Behún1055ce12020-04-08 12:02:07 +020037#define ARMADA_37XX_NB_GPIO_SEL (MVEBU_REGISTER(0x13830))
38#define ARMADA_37XX_SPI_CTRL (MVEBU_REGISTER(0x10600))
39#define ARMADA_37XX_SPI_CFG (MVEBU_REGISTER(0x10604))
40#define ARMADA_37XX_SPI_DOUT (MVEBU_REGISTER(0x10608))
41#define ARMADA_37XX_SPI_DIN (MVEBU_REGISTER(0x1060c))
Marek Behún9602a8c2018-08-21 12:22:09 +020042
Marek Behúnf835bed2018-04-24 17:21:31 +020043DECLARE_GLOBAL_DATA_PTR;
44
Marek Behún9602a8c2018-08-21 12:22:09 +020045#if defined(CONFIG_OF_BOARD_FIXUP)
46int board_fix_fdt(void *blob)
47{
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010048 enum fdt_status status_pcie, status_eth1;
Marek Behún9602a8c2018-08-21 12:22:09 +020049 u8 topology[MAX_MOX_MODULES];
Marek Behúnd050eab2021-11-26 14:57:11 +010050 int i, size, ret;
Pali Rohár951b9822022-01-18 10:33:19 +010051 bool eth1_sgmii;
Marek Behún9602a8c2018-08-21 12:22:09 +020052
53 /*
54 * SPI driver is not loaded in driver model yet, but we have to find out
55 * if pcie should be enabled in U-Boot's device tree. Therefore we have
56 * to read SPI by reading/writing SPI registers directly
57 */
58
Marek Behúna4e697f2020-04-08 12:02:03 +020059 /* put pin from GPIO to SPI mode */
60 clrbits_le32(ARMADA_37XX_NB_GPIO_SEL, BIT(12));
Marek Behún77d93122021-11-26 14:57:14 +010061 /* configure cpol, cpha, prescale */
62 writel(0x10df, ARMADA_37XX_SPI_CFG);
63 mdelay(1);
Marek Behúna4e697f2020-04-08 12:02:03 +020064 /* 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
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010070 status_pcie = FDT_STATUS_DISABLED;
71 status_eth1 = FDT_STATUS_DISABLED;
Pali Rohár951b9822022-01-18 10:33:19 +010072 eth1_sgmii = false;
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010073
Marek Behún9602a8c2018-08-21 12:22:09 +020074 for (i = 0; i < MAX_MOX_MODULES; ++i) {
75 writel(0x0, ARMADA_37XX_SPI_DOUT);
76
77 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
78 udelay(1);
79
80 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
81 if (topology[i] == 0xff)
82 break;
83
84 topology[i] &= 0xf;
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010085
Pali Rohár951b9822022-01-18 10:33:19 +010086 if (topology[i] == MOX_MODULE_SFP &&
87 status_pcie == FDT_STATUS_DISABLED)
88 eth1_sgmii = true;
89
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010090 if (topology[i] == MOX_MODULE_SFP ||
91 topology[i] == MOX_MODULE_TOPAZ ||
92 topology[i] == MOX_MODULE_PERIDOT)
93 status_eth1 = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +020094 }
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
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100101 ret = fdt_set_status_by_alias(blob, "ethernet1", status_eth1);
102 if (ret < 0)
103 printf("Cannot set status for eth1 in U-Boot's device tree: %s!\n",
104 fdt_strerror(ret));
105
Pali Rohár951b9822022-01-18 10:33:19 +0100106 if (eth1_sgmii) {
107 ret = fdt_path_offset(blob, "ethernet1");
108 if (ret >= 0)
109 ret = fdt_setprop_string(blob, ret, "phy-mode", "sgmii");
110 if (ret < 0)
111 printf("Cannot set phy-mode for eth1 to sgmii in U-Boot device tree: %s!\n",
112 fdt_strerror(ret));
113 }
114
Marek Behún9602a8c2018-08-21 12:22:09 +0200115 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
116 topology[1] == MOX_MODULE_USB3 ||
117 topology[1] == MOX_MODULE_PASSPCI))
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100118 status_pcie = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +0200119
Marek Behúnd050eab2021-11-26 14:57:11 +0100120 ret = fdt_set_status_by_compatible(blob, "marvell,armada-3700-pcie",
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100121 status_pcie);
Marek Behúnd050eab2021-11-26 14:57:11 +0100122 if (ret < 0) {
123 printf("Cannot set status for PCIe in U-Boot's device tree: %s!\n",
124 fdt_strerror(ret));
Marek Behún9602a8c2018-08-21 12:22:09 +0200125 return 0;
126 }
127
Marek Behún455ef5b2020-04-08 19:25:22 +0200128 if (a3700_fdt_fix_pcie_regions(blob) < 0) {
129 printf("Cannot fix PCIe regions in U-Boot's device tree!\n");
130 return 0;
131 }
132
Marek Behún9602a8c2018-08-21 12:22:09 +0200133 return 0;
134}
135#endif
136
Marek Behúnf835bed2018-04-24 17:21:31 +0200137int board_init(void)
138{
139 /* address of boot parameters */
140 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
141
Marek Behúnf835bed2018-04-24 17:21:31 +0200142 return 0;
143}
144
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100145static int mox_do_spi(u8 *in, u8 *out, size_t size)
Marek Behúnf835bed2018-04-24 17:21:31 +0200146{
147 struct spi_slave *slave;
148 struct udevice *dev;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100149 int ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200150
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100151 ret = spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
152 "spi_generic_drv", "moxtet@1", &dev,
153 &slave);
Marek Behúnf835bed2018-04-24 17:21:31 +0200154 if (ret)
155 goto fail;
156
157 ret = spi_claim_bus(slave);
158 if (ret)
159 goto fail_free;
160
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100161 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
162
163 spi_release_bus(slave);
164fail_free:
165 spi_free_slave(slave);
166fail:
167 return ret;
168}
169
170static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
171{
172 static int is_sd;
173 static u8 topology[MAX_MOX_MODULES - 1];
174 static int size;
175 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
176 int ret, i;
177
178 if (size) {
179 if (ptopology)
180 *ptopology = topology;
181 if (psize)
182 *psize = size;
183 if (pis_sd)
184 *pis_sd = is_sd;
185 return 0;
186 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200187
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100188 memset(din, 0, MAX_MOX_MODULES);
189 memset(dout, 0, MAX_MOX_MODULES);
190
191 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
Marek Behúnf835bed2018-04-24 17:21:31 +0200192 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100193 return ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200194
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100195 if (din[0] == 0x10)
196 is_sd = 1;
197 else if (din[0] == 0x00)
198 is_sd = 0;
199 else
200 return -ENODEV;
Marek Behúnf835bed2018-04-24 17:21:31 +0200201
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100202 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
203 topology[i - 1] = din[i] & 0xf;
204 size = i - 1;
205
206 if (ptopology)
207 *ptopology = topology;
208 if (psize)
209 *psize = size;
210 if (pis_sd)
211 *pis_sd = is_sd;
212
213 return 0;
214}
215
Marek Behún35b35132018-12-17 16:10:03 +0100216#define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
217#define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
218
219static int sw_multi_read(struct mii_dev *bus, int sw, int dev, int reg)
220{
221 bus->write(bus, sw, 0, 0, SW_SMI_CMD_R(dev, reg));
222 mdelay(5);
223 return bus->read(bus, sw, 0, 1);
224}
225
226static void sw_multi_write(struct mii_dev *bus, int sw, int dev, int reg,
227 u16 val)
228{
229 bus->write(bus, sw, 0, 1, val);
230 bus->write(bus, sw, 0, 0, SW_SMI_CMD_W(dev, reg));
231 mdelay(5);
232}
233
234static int sw_scratch_read(struct mii_dev *bus, int sw, int reg)
235{
236 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
237 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
238}
239
240static void sw_led_write(struct mii_dev *bus, int sw, int port, int reg,
241 u16 val)
242{
243 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
244 | (val & 0x7ff));
245}
246
247static void sw_blink_leds(struct mii_dev *bus, int peridot, int topaz)
248{
249 int i, p;
250 struct {
251 int port;
252 u16 val;
253 int wait;
254 } regs[] = {
255 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
256 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
257 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
258 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
259 };
260
261 for (i = 0; i < 12; ++i) {
262 for (p = 0; p < peridot; ++p) {
263 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
264 regs[i].val);
265 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
266 regs[i].val);
267 }
268 if (topaz) {
269 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
270 regs[i].val);
271 }
272
273 if (regs[i].wait)
274 mdelay(75);
275 }
276}
277
278static void check_switch_address(struct mii_dev *bus, int addr)
279{
280 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
281 printf("Check of switch MDIO address failed for 0x%02x\n",
282 addr);
283}
284
285static int sfp, pci, topaz, peridot, usb, passpci;
286static int sfp_pos, peridot_pos[3];
287static int module_count;
288
289static int configure_peridots(struct gpio_desc *reset_gpio)
290{
291 int i, ret;
292 u8 dout[MAX_MOX_MODULES];
293
294 memset(dout, 0, MAX_MOX_MODULES);
295
296 /* set addresses of Peridot modules */
297 for (i = 0; i < peridot; ++i)
298 dout[module_count - peridot_pos[i]] = (~i) & 3;
299
300 /*
301 * if there is a SFP module connected to the last Peridot module, set
302 * the P10_SMODE to 1 for the Peridot module
303 */
304 if (sfp)
305 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
306
307 dm_gpio_set_value(reset_gpio, 1);
308 mdelay(10);
309
310 ret = mox_do_spi(NULL, dout, module_count + 1);
311
312 mdelay(10);
313 dm_gpio_set_value(reset_gpio, 0);
314
315 mdelay(50);
316
317 return ret;
318}
319
320static int get_reset_gpio(struct gpio_desc *reset_gpio)
321{
322 int node;
323
324 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
325 if (node < 0) {
326 printf("Cannot find Moxtet bus device node!\n");
327 return -1;
328 }
329
330 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
331 reset_gpio, GPIOD_IS_OUT);
332
333 if (!dm_gpio_is_valid(reset_gpio)) {
334 printf("Cannot find reset GPIO for Moxtet bus!\n");
335 return -1;
336 }
337
Marek Behún35572c92018-12-17 16:10:08 +0100338 return 0;
339}
340
341int misc_init_r(void)
342{
Marek Behún98c86c42021-10-09 19:33:42 +0200343 u8 mac[2][6];
344 int i, ret;
Marek Behún35572c92018-12-17 16:10:08 +0100345
Marek Behún98c86c42021-10-09 19:33:42 +0200346 ret = mbox_sp_get_board_info(NULL, mac[0], mac[1], NULL, NULL);
Marek Behún35572c92018-12-17 16:10:08 +0100347 if (ret < 0) {
348 printf("Cannot read data from OTP!\n");
349 return 0;
350 }
351
Marek Behún98c86c42021-10-09 19:33:42 +0200352 for (i = 0; i < 2; ++i) {
353 u8 oldmac[6];
Marek Behún35572c92018-12-17 16:10:08 +0100354
Marek Behún98c86c42021-10-09 19:33:42 +0200355 if (is_valid_ethaddr(mac[i]) &&
356 !eth_env_get_enetaddr_by_index("eth", i, oldmac))
357 eth_env_set_enetaddr_by_index("eth", i, mac[i]);
358 }
Marek Behún35572c92018-12-17 16:10:08 +0100359
Marek Behún35b35132018-12-17 16:10:03 +0100360 return 0;
361}
362
Marek Behúnd1a30792021-06-07 16:34:48 +0200363static void mox_phy_modify(struct phy_device *phydev, int page, int reg,
364 u16 mask, u16 set)
365{
366 int val;
367
368 val = phydev->drv->readext(phydev, MDIO_DEVAD_NONE, page, reg);
369 val &= ~mask;
370 val |= set;
371 phydev->drv->writeext(phydev, MDIO_DEVAD_NONE, page, reg, val);
372}
373
374static void mox_phy_leds_start_blinking(void)
375{
376 struct phy_device *phydev;
377 struct mii_dev *bus;
Pali Rohára524db02022-02-14 11:34:26 +0100378 const char *node_name;
379 int node;
Marek Behúnd1a30792021-06-07 16:34:48 +0200380
Pali Rohára524db02022-02-14 11:34:26 +0100381 node = fdt_path_offset(gd->fdt_blob, "ethernet0");
382 if (node < 0) {
383 printf("Cannot get eth0!\n");
384 return;
385 }
386
387 node_name = fdt_get_name(gd->fdt_blob, node, NULL);
388 if (!node_name) {
389 printf("Cannot get eth0 node name!\n");
390 return;
391 }
392
393 bus = miiphy_get_dev_by_name(node_name);
Marek Behúnd1a30792021-06-07 16:34:48 +0200394 if (!bus) {
395 printf("Cannot get MDIO bus device!\n");
396 return;
397 }
398
Marek Behún3927efb2022-04-07 00:33:08 +0200399 phydev = phy_find_by_mask(bus, BIT(1));
Marek Behúnd1a30792021-06-07 16:34:48 +0200400 if (!phydev) {
401 printf("Cannot get ethernet PHY!\n");
402 return;
403 }
404
405 mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400);
406 mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb);
407}
408
Marek Behún859ada52021-06-07 16:34:47 +0200409static bool read_reset_button(void)
410{
411 struct udevice *button, *led;
412 int i;
413
Marek Behúnd050eab2021-11-26 14:57:11 +0100414 if (device_get_global_by_ofnode(
415 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
416 "gpio-keys")),
417 &button)) {
Marek Behún859ada52021-06-07 16:34:47 +0200418 printf("Cannot find reset button!\n");
419 return false;
420 }
421
Marek Behúnd050eab2021-11-26 14:57:11 +0100422 if (device_get_global_by_ofnode(
423 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
424 "gpio-leds")),
425 &led)) {
Marek Behún859ada52021-06-07 16:34:47 +0200426 printf("Cannot find status LED!\n");
427 return false;
428 }
429
430 led_set_state(led, LEDST_ON);
431
432 for (i = 0; i < 21; ++i) {
433 if (button_get_state(button) != BUTTON_ON)
434 return false;
435 if (i < 20)
436 mdelay(50);
437 }
438
439 led_set_state(led, LEDST_OFF);
440
441 return true;
442}
443
444static void handle_reset_button(void)
445{
Pali Rohár905c3bf2021-06-14 16:45:58 +0200446 const char * const vars[1] = { "bootcmd_rescue", };
447
448 /*
449 * Ensure that bootcmd_rescue has always stock value, so that running
450 * run bootcmd_rescue
451 * always works correctly.
452 */
453 env_set_default_vars(1, (char * const *)vars, 0);
454
Marek Behún859ada52021-06-07 16:34:47 +0200455 if (read_reset_button()) {
Pali Rohár905c3bf2021-06-14 16:45:58 +0200456 const char * const vars[2] = {
Marek Behún859ada52021-06-07 16:34:47 +0200457 "bootcmd",
Marek Behún859ada52021-06-07 16:34:47 +0200458 "distro_bootcmd",
459 };
460
461 /*
462 * Set the above envs to their default values, in case the user
463 * managed to break them.
464 */
Pali Rohár905c3bf2021-06-14 16:45:58 +0200465 env_set_default_vars(2, (char * const *)vars, 0);
Marek Behún859ada52021-06-07 16:34:47 +0200466
467 /* Ensure bootcmd_rescue is used by distroboot */
468 env_set("boot_targets", "rescue");
469
Marek Behúnd1a30792021-06-07 16:34:48 +0200470 /* start blinking PHY LEDs */
471 mox_phy_leds_start_blinking();
472
Marek Behún859ada52021-06-07 16:34:47 +0200473 printf("RESET button was pressed, overwriting boot_targets!\n");
474 } else {
475 /*
476 * In case the user somehow managed to save environment with
477 * boot_targets=rescue, reset boot_targets to default value.
478 * This could happen in subsequent commands if bootcmd_rescue
479 * failed.
480 */
481 if (!strcmp(env_get("boot_targets"), "rescue")) {
482 const char * const vars[1] = {
483 "boot_targets",
484 };
485
486 env_set_default_vars(1, (char * const *)vars, 0);
487 }
488 }
489}
490
Marek Behún84ee2cd2021-10-09 19:33:40 +0200491int show_board_info(void)
Marek Behún35572c92018-12-17 16:10:08 +0100492{
Marek Behún84ee2cd2021-10-09 19:33:40 +0200493 int i, ret, board_version, ram_size, is_sd;
Marek Behún35572c92018-12-17 16:10:08 +0100494 const char *pub_key;
Marek Behún84ee2cd2021-10-09 19:33:40 +0200495 const u8 *topology;
496 u64 serial_number;
497
498 printf("Model: CZ.NIC Turris Mox Board\n");
Marek Behún35572c92018-12-17 16:10:08 +0100499
500 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
501 &ram_size);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200502 if (ret < 0) {
503 printf(" Cannot read board info: %i\n", ret);
504 } else {
505 printf(" Board version: %i\n", board_version);
506 printf(" RAM size: %i MiB\n", ram_size);
507 printf(" Serial Number: %016llX\n", serial_number);
508 }
Marek Behún35572c92018-12-17 16:10:08 +0100509
510 pub_key = mox_sp_get_ecdsa_public_key();
511 if (pub_key)
512 printf(" ECDSA Public Key: %s\n", pub_key);
513 else
Marek Behún84ee2cd2021-10-09 19:33:40 +0200514 printf(" Cannot read ECDSA Public Key\n");
Marek Behún35572c92018-12-17 16:10:08 +0100515
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100516 ret = mox_get_topology(&topology, &module_count, &is_sd);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200517 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100518 printf("Cannot read module topology!\n");
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100519
Marek Behún35572c92018-12-17 16:10:08 +0100520 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
521
522 if (module_count)
523 printf("Module Topology:\n");
524
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100525 for (i = 0; i < module_count; ++i) {
526 switch (topology[i]) {
527 case MOX_MODULE_SFP:
528 printf("% 4i: SFP Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200529 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100530 case MOX_MODULE_PCI:
531 printf("% 4i: Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200532 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100533 case MOX_MODULE_TOPAZ:
534 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
535 break;
536 case MOX_MODULE_PERIDOT:
537 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
538 break;
539 case MOX_MODULE_USB3:
540 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
541 break;
542 case MOX_MODULE_PASSPCI:
543 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200544 break;
545 default:
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100546 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
Marek Behúnf835bed2018-04-24 17:21:31 +0200547 }
548 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200549
Marek Behún84ee2cd2021-10-09 19:33:40 +0200550 /* check if modules are connected in supported mode */
Marek Behún35b35132018-12-17 16:10:03 +0100551 for (i = 0; i < module_count; ++i) {
552 switch (topology[i]) {
553 case MOX_MODULE_SFP:
554 if (sfp) {
555 printf("Error: Only one SFP module is supported!\n");
556 } else if (topaz) {
557 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
558 } else {
559 sfp_pos = i;
560 ++sfp;
561 }
562 break;
563 case MOX_MODULE_PCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200564 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100565 printf("Error: Only one Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200566 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100567 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200568 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100569 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 +0200570 else
Marek Behún35b35132018-12-17 16:10:03 +0100571 ++pci;
Marek Behún35b35132018-12-17 16:10:03 +0100572 break;
573 case MOX_MODULE_TOPAZ:
Marek Behún4529fce2020-04-08 12:02:05 +0200574 if (topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100575 printf("Error: Only one Topaz module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200576 else if (peridot >= 3)
Marek Behún35b35132018-12-17 16:10:03 +0100577 printf("Error: At most two Peridot modules can come before Topaz module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200578 else
Marek Behún35b35132018-12-17 16:10:03 +0100579 ++topaz;
Marek Behún35b35132018-12-17 16:10:03 +0100580 break;
581 case MOX_MODULE_PERIDOT:
582 if (sfp || topaz) {
583 printf("Error: Peridot module must come before SFP or Topaz module!\n");
584 } else if (peridot >= 3) {
585 printf("Error: At most three Peridot modules are supported!\n");
586 } else {
587 peridot_pos[peridot] = i;
588 ++peridot;
589 }
590 break;
591 case MOX_MODULE_USB3:
Marek Behún4529fce2020-04-08 12:02:05 +0200592 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100593 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200594 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100595 printf("Error: Only one USB 3.0 module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200596 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100597 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 +0200598 else
Marek Behún35b35132018-12-17 16:10:03 +0100599 ++usb;
Marek Behún35b35132018-12-17 16:10:03 +0100600 break;
601 case MOX_MODULE_PASSPCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200602 if (passpci)
Marek Behún35b35132018-12-17 16:10:03 +0100603 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200604 else if (i != 0)
Marek Behún35b35132018-12-17 16:10:03 +0100605 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200606 else
Marek Behún35b35132018-12-17 16:10:03 +0100607 ++passpci;
Marek Behún35b35132018-12-17 16:10:03 +0100608 }
609 }
610
Marek Behún84ee2cd2021-10-09 19:33:40 +0200611 return 0;
612}
Marek Behún35b35132018-12-17 16:10:03 +0100613
Marek Behún84ee2cd2021-10-09 19:33:40 +0200614int last_stage_init(void)
615{
616 struct gpio_desc reset_gpio = {};
617
618 /* configure modules */
Marek Behún35b35132018-12-17 16:10:03 +0100619 if (get_reset_gpio(&reset_gpio) < 0)
Marek Behún84d67ac2021-10-09 19:33:41 +0200620 goto handle_reset_btn;
Marek Behún35b35132018-12-17 16:10:03 +0100621
622 if (peridot > 0) {
623 if (configure_peridots(&reset_gpio) < 0) {
624 printf("Cannot configure Peridot modules!\n");
625 peridot = 0;
626 }
627 } else {
628 dm_gpio_set_value(&reset_gpio, 1);
629 mdelay(50);
630 dm_gpio_set_value(&reset_gpio, 0);
631 mdelay(50);
632 }
633
Marek Behún84ee2cd2021-10-09 19:33:40 +0200634 /*
635 * check if the addresses are set by reading Scratch & Misc register
636 * 0x70 of Peridot (and potentially Topaz) modules
637 */
Marek Behún35b35132018-12-17 16:10:03 +0100638 if (peridot || topaz) {
Marek Behún84ee2cd2021-10-09 19:33:40 +0200639 struct mii_dev *bus;
Pali Rohára524db02022-02-14 11:34:26 +0100640 const char *node_name;
641 int node;
Marek Behún35b35132018-12-17 16:10:03 +0100642
Pali Rohára524db02022-02-14 11:34:26 +0100643 node = fdt_path_offset(gd->fdt_blob, "ethernet0");
644 node_name = (node >= 0) ? fdt_get_name(gd->fdt_blob, node, NULL) : NULL;
645 bus = node_name ? miiphy_get_dev_by_name(node_name) : NULL;
Marek Behún35b35132018-12-17 16:10:03 +0100646 if (!bus) {
647 printf("Cannot get MDIO bus device!\n");
648 } else {
Marek Behún84ee2cd2021-10-09 19:33:40 +0200649 int i;
650
Marek Behún35b35132018-12-17 16:10:03 +0100651 for (i = 0; i < peridot; ++i)
652 check_switch_address(bus, 0x10 + i);
653
654 if (topaz)
655 check_switch_address(bus, 0x2);
656
657 sw_blink_leds(bus, peridot, topaz);
658 }
659 }
660
Marek Behún84d67ac2021-10-09 19:33:41 +0200661handle_reset_btn:
Marek Behún859ada52021-06-07 16:34:47 +0200662 handle_reset_button();
663
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100664 return 0;
Marek Behúnf835bed2018-04-24 17:21:31 +0200665}
Marek Behún4529fce2020-04-08 12:02:05 +0200666
667#if defined(CONFIG_OF_BOARD_SETUP)
668
Marek Behúnd050eab2021-11-26 14:57:11 +0100669static bool is_topaz(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200670{
Marek Behúnd050eab2021-11-26 14:57:11 +0100671 return topaz && id == peridot + topaz - 1;
Marek Behún4529fce2020-04-08 12:02:05 +0200672}
673
Marek Behúnd050eab2021-11-26 14:57:11 +0100674static int switch_addr(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200675{
Marek Behúnd050eab2021-11-26 14:57:11 +0100676 return is_topaz(id) ? 0x2 : 0x10 + id;
Marek Behún4529fce2020-04-08 12:02:05 +0200677}
678
Marek Behúnd050eab2021-11-26 14:57:11 +0100679static int setup_switch(void *blob, int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200680{
Marek Behúnd050eab2021-11-26 14:57:11 +0100681 int res, addr, i, node;
682 char mdio_path[64];
Marek Behún4529fce2020-04-08 12:02:05 +0200683
Marek Behúnd050eab2021-11-26 14:57:11 +0100684 node = fdt_node_offset_by_compatible(blob, -1, "marvell,orion-mdio");
Marek Behún4529fce2020-04-08 12:02:05 +0200685 if (node < 0)
686 return node;
687
Marek Behúnd050eab2021-11-26 14:57:11 +0100688 res = fdt_get_path(blob, node, mdio_path, sizeof(mdio_path));
Marek Behún4529fce2020-04-08 12:02:05 +0200689 if (res < 0)
690 return res;
691
Marek Behún4529fce2020-04-08 12:02:05 +0200692 addr = switch_addr(id);
693
694 /* first enable the switch by setting status = "okay" */
Marek Behúnd050eab2021-11-26 14:57:11 +0100695 res = fdt_status_okay_by_pathf(blob, "%s/switch%i@%x", mdio_path, id,
696 addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200697 if (res < 0)
698 return res;
699
700 /*
701 * now if there are more switches or a SFP module coming after,
702 * enable corresponding ports
703 */
704 if (id < peridot + topaz - 1) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100705 res = fdt_status_okay_by_pathf(blob,
706 "%s/switch%i@%x/ports/port@a",
707 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200708 } else if (id == peridot - 1 && !topaz && sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100709 res = fdt_status_okay_by_pathf(blob,
710 "%s/switch%i@%x/ports/port-sfp@a",
711 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200712 } else {
713 res = 0;
714 }
715 if (res < 0)
716 return res;
717
718 if (id >= peridot + topaz - 1)
719 return 0;
720
721 /* finally change link property if needed */
Marek Behúnd050eab2021-11-26 14:57:11 +0100722 node = fdt_node_offset_by_pathf(blob, "%s/switch%i@%x/ports/port@a",
723 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200724 if (node < 0)
725 return node;
726
727 for (i = id + 1; i < peridot + topaz; ++i) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100728 unsigned int phandle;
729
730 phandle = fdt_create_phandle_by_pathf(blob,
731 "%s/switch%i@%x/ports/port@%x",
732 mdio_path, i,
733 switch_addr(i),
734 is_topaz(i) ? 5 : 9);
735 if (!phandle)
736 return -FDT_ERR_NOPHANDLES;
Marek Behún4529fce2020-04-08 12:02:05 +0200737
738 if (i == id + 1)
739 res = fdt_setprop_u32(blob, node, "link", phandle);
740 else
741 res = fdt_appendprop_u32(blob, node, "link", phandle);
742 if (res < 0)
743 return res;
744 }
745
746 return 0;
747}
748
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900749int ft_board_setup(void *blob, struct bd_info *bd)
Marek Behún4529fce2020-04-08 12:02:05 +0200750{
Marek Behúnd050eab2021-11-26 14:57:11 +0100751 int res;
Marek Behún4529fce2020-04-08 12:02:05 +0200752
753 /*
754 * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
755 * connected, enable the PCIe node.
756 */
757 if (pci || usb || passpci) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100758 res = fdt_status_okay_by_compatible(blob,
759 "marvell,armada-3700-pcie");
Marek Behún4529fce2020-04-08 12:02:05 +0200760 if (res < 0)
761 return res;
Marek Behún455ef5b2020-04-08 19:25:22 +0200762
763 /* Fix PCIe regions for devices with 4 GB RAM */
764 res = a3700_fdt_fix_pcie_regions(blob);
765 if (res < 0)
766 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200767 }
768
769 /*
770 * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
771 * enable the eth1 node and setup the switches.
772 */
773 if (peridot || topaz) {
774 int i;
775
Marek Behúnd050eab2021-11-26 14:57:11 +0100776 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200777 if (res < 0)
778 return res;
779
780 for (i = 0; i < peridot + topaz; ++i) {
781 res = setup_switch(blob, i);
782 if (res < 0)
783 return res;
784 }
785 }
786
787 /*
788 * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
789 * node. If there is no Peridot switch between MOX A and MOX D, add link
790 * to the SFP node to eth1 node.
791 * Also enable and configure SFP GPIO controller node.
792 */
793 if (sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100794 int node;
795
796 res = fdt_status_okay_by_compatible(blob, "sff,sfp");
Marek Behún4529fce2020-04-08 12:02:05 +0200797 if (res < 0)
798 return res;
799
Marek Behúnd050eab2021-11-26 14:57:11 +0100800 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200801 if (res < 0)
802 return res;
803
804 if (!peridot) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100805 unsigned int phandle;
Marek Behún4529fce2020-04-08 12:02:05 +0200806
Marek Behúnd050eab2021-11-26 14:57:11 +0100807 phandle = fdt_create_phandle_by_compatible(blob,
808 "sff,sfp");
809 if (!phandle)
810 return -FDT_ERR_NOPHANDLES;
811
812 node = fdt_path_offset(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200813 if (node < 0)
814 return node;
815
816 res = fdt_setprop_u32(blob, node, "sfp", phandle);
817 if (res < 0)
818 return res;
819
820 res = fdt_setprop_string(blob, node, "phy-mode",
821 "sgmii");
822 if (res < 0)
823 return res;
824 }
825
Marek Behúnd050eab2021-11-26 14:57:11 +0100826 res = fdt_status_okay_by_compatible(blob, "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200827 if (res < 0)
828 return res;
829
830 if (sfp_pos) {
831 char newname[16];
832
833 /* moxtet-sfp is on non-zero position, change default */
Marek Behúnd050eab2021-11-26 14:57:11 +0100834 node = fdt_node_offset_by_compatible(blob, -1,
835 "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200836 if (node < 0)
837 return node;
838
839 res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
840 if (res < 0)
841 return res;
842
843 sprintf(newname, "gpio@%x", sfp_pos);
844
845 res = fdt_set_name(blob, node, newname);
846 if (res < 0)
847 return res;
848 }
849 }
850
851 fdt_fixup_ethernet(blob);
852
853 /* Finally remove disabled nodes, as per Rob Herring's request. */
Marek Behúnac4d9ff2021-11-26 14:57:15 +0100854 fdt_delete_disabled_nodes(blob);
Marek Behún4529fce2020-04-08 12:02:05 +0200855
856 return 0;
857}
858
859#endif