blob: f0c5aa6a520b957e8f4fe278a0cba298d54e6caf [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;
378
379 bus = miiphy_get_dev_by_name("neta@30000");
380 if (!bus) {
381 printf("Cannot get MDIO bus device!\n");
382 return;
383 }
384
385 phydev = phy_find_by_mask(bus, BIT(1), PHY_INTERFACE_MODE_RGMII);
386 if (!phydev) {
387 printf("Cannot get ethernet PHY!\n");
388 return;
389 }
390
391 mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400);
392 mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb);
393}
394
Marek Behún859ada52021-06-07 16:34:47 +0200395static bool read_reset_button(void)
396{
397 struct udevice *button, *led;
398 int i;
399
Marek Behúnd050eab2021-11-26 14:57:11 +0100400 if (device_get_global_by_ofnode(
401 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
402 "gpio-keys")),
403 &button)) {
Marek Behún859ada52021-06-07 16:34:47 +0200404 printf("Cannot find reset button!\n");
405 return false;
406 }
407
Marek Behúnd050eab2021-11-26 14:57:11 +0100408 if (device_get_global_by_ofnode(
409 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
410 "gpio-leds")),
411 &led)) {
Marek Behún859ada52021-06-07 16:34:47 +0200412 printf("Cannot find status LED!\n");
413 return false;
414 }
415
416 led_set_state(led, LEDST_ON);
417
418 for (i = 0; i < 21; ++i) {
419 if (button_get_state(button) != BUTTON_ON)
420 return false;
421 if (i < 20)
422 mdelay(50);
423 }
424
425 led_set_state(led, LEDST_OFF);
426
427 return true;
428}
429
430static void handle_reset_button(void)
431{
Pali Rohár905c3bf2021-06-14 16:45:58 +0200432 const char * const vars[1] = { "bootcmd_rescue", };
433
434 /*
435 * Ensure that bootcmd_rescue has always stock value, so that running
436 * run bootcmd_rescue
437 * always works correctly.
438 */
439 env_set_default_vars(1, (char * const *)vars, 0);
440
Marek Behún859ada52021-06-07 16:34:47 +0200441 if (read_reset_button()) {
Pali Rohár905c3bf2021-06-14 16:45:58 +0200442 const char * const vars[2] = {
Marek Behún859ada52021-06-07 16:34:47 +0200443 "bootcmd",
Marek Behún859ada52021-06-07 16:34:47 +0200444 "distro_bootcmd",
445 };
446
447 /*
448 * Set the above envs to their default values, in case the user
449 * managed to break them.
450 */
Pali Rohár905c3bf2021-06-14 16:45:58 +0200451 env_set_default_vars(2, (char * const *)vars, 0);
Marek Behún859ada52021-06-07 16:34:47 +0200452
453 /* Ensure bootcmd_rescue is used by distroboot */
454 env_set("boot_targets", "rescue");
455
Marek Behúnd1a30792021-06-07 16:34:48 +0200456 /* start blinking PHY LEDs */
457 mox_phy_leds_start_blinking();
458
Marek Behún859ada52021-06-07 16:34:47 +0200459 printf("RESET button was pressed, overwriting boot_targets!\n");
460 } else {
461 /*
462 * In case the user somehow managed to save environment with
463 * boot_targets=rescue, reset boot_targets to default value.
464 * This could happen in subsequent commands if bootcmd_rescue
465 * failed.
466 */
467 if (!strcmp(env_get("boot_targets"), "rescue")) {
468 const char * const vars[1] = {
469 "boot_targets",
470 };
471
472 env_set_default_vars(1, (char * const *)vars, 0);
473 }
474 }
475}
476
Marek Behún84ee2cd2021-10-09 19:33:40 +0200477int show_board_info(void)
Marek Behún35572c92018-12-17 16:10:08 +0100478{
Marek Behún84ee2cd2021-10-09 19:33:40 +0200479 int i, ret, board_version, ram_size, is_sd;
Marek Behún35572c92018-12-17 16:10:08 +0100480 const char *pub_key;
Marek Behún84ee2cd2021-10-09 19:33:40 +0200481 const u8 *topology;
482 u64 serial_number;
483
484 printf("Model: CZ.NIC Turris Mox Board\n");
Marek Behún35572c92018-12-17 16:10:08 +0100485
486 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
487 &ram_size);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200488 if (ret < 0) {
489 printf(" Cannot read board info: %i\n", ret);
490 } else {
491 printf(" Board version: %i\n", board_version);
492 printf(" RAM size: %i MiB\n", ram_size);
493 printf(" Serial Number: %016llX\n", serial_number);
494 }
Marek Behún35572c92018-12-17 16:10:08 +0100495
496 pub_key = mox_sp_get_ecdsa_public_key();
497 if (pub_key)
498 printf(" ECDSA Public Key: %s\n", pub_key);
499 else
Marek Behún84ee2cd2021-10-09 19:33:40 +0200500 printf(" Cannot read ECDSA Public Key\n");
Marek Behún35572c92018-12-17 16:10:08 +0100501
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100502 ret = mox_get_topology(&topology, &module_count, &is_sd);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200503 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100504 printf("Cannot read module topology!\n");
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100505
Marek Behún35572c92018-12-17 16:10:08 +0100506 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
507
508 if (module_count)
509 printf("Module Topology:\n");
510
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100511 for (i = 0; i < module_count; ++i) {
512 switch (topology[i]) {
513 case MOX_MODULE_SFP:
514 printf("% 4i: SFP Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200515 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100516 case MOX_MODULE_PCI:
517 printf("% 4i: Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200518 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100519 case MOX_MODULE_TOPAZ:
520 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
521 break;
522 case MOX_MODULE_PERIDOT:
523 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
524 break;
525 case MOX_MODULE_USB3:
526 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
527 break;
528 case MOX_MODULE_PASSPCI:
529 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200530 break;
531 default:
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100532 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
Marek Behúnf835bed2018-04-24 17:21:31 +0200533 }
534 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200535
Marek Behún84ee2cd2021-10-09 19:33:40 +0200536 /* check if modules are connected in supported mode */
Marek Behún35b35132018-12-17 16:10:03 +0100537 for (i = 0; i < module_count; ++i) {
538 switch (topology[i]) {
539 case MOX_MODULE_SFP:
540 if (sfp) {
541 printf("Error: Only one SFP module is supported!\n");
542 } else if (topaz) {
543 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
544 } else {
545 sfp_pos = i;
546 ++sfp;
547 }
548 break;
549 case MOX_MODULE_PCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200550 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100551 printf("Error: Only one Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200552 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100553 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200554 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100555 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 +0200556 else
Marek Behún35b35132018-12-17 16:10:03 +0100557 ++pci;
Marek Behún35b35132018-12-17 16:10:03 +0100558 break;
559 case MOX_MODULE_TOPAZ:
Marek Behún4529fce2020-04-08 12:02:05 +0200560 if (topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100561 printf("Error: Only one Topaz module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200562 else if (peridot >= 3)
Marek Behún35b35132018-12-17 16:10:03 +0100563 printf("Error: At most two Peridot modules can come before Topaz module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200564 else
Marek Behún35b35132018-12-17 16:10:03 +0100565 ++topaz;
Marek Behún35b35132018-12-17 16:10:03 +0100566 break;
567 case MOX_MODULE_PERIDOT:
568 if (sfp || topaz) {
569 printf("Error: Peridot module must come before SFP or Topaz module!\n");
570 } else if (peridot >= 3) {
571 printf("Error: At most three Peridot modules are supported!\n");
572 } else {
573 peridot_pos[peridot] = i;
574 ++peridot;
575 }
576 break;
577 case MOX_MODULE_USB3:
Marek Behún4529fce2020-04-08 12:02:05 +0200578 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100579 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200580 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100581 printf("Error: Only one USB 3.0 module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200582 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100583 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 +0200584 else
Marek Behún35b35132018-12-17 16:10:03 +0100585 ++usb;
Marek Behún35b35132018-12-17 16:10:03 +0100586 break;
587 case MOX_MODULE_PASSPCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200588 if (passpci)
Marek Behún35b35132018-12-17 16:10:03 +0100589 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200590 else if (i != 0)
Marek Behún35b35132018-12-17 16:10:03 +0100591 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200592 else
Marek Behún35b35132018-12-17 16:10:03 +0100593 ++passpci;
Marek Behún35b35132018-12-17 16:10:03 +0100594 }
595 }
596
Marek Behún84ee2cd2021-10-09 19:33:40 +0200597 return 0;
598}
Marek Behún35b35132018-12-17 16:10:03 +0100599
Marek Behún84ee2cd2021-10-09 19:33:40 +0200600int last_stage_init(void)
601{
602 struct gpio_desc reset_gpio = {};
603
604 /* configure modules */
Marek Behún35b35132018-12-17 16:10:03 +0100605 if (get_reset_gpio(&reset_gpio) < 0)
Marek Behún84d67ac2021-10-09 19:33:41 +0200606 goto handle_reset_btn;
Marek Behún35b35132018-12-17 16:10:03 +0100607
608 if (peridot > 0) {
609 if (configure_peridots(&reset_gpio) < 0) {
610 printf("Cannot configure Peridot modules!\n");
611 peridot = 0;
612 }
613 } else {
614 dm_gpio_set_value(&reset_gpio, 1);
615 mdelay(50);
616 dm_gpio_set_value(&reset_gpio, 0);
617 mdelay(50);
618 }
619
Marek Behún84ee2cd2021-10-09 19:33:40 +0200620 /*
621 * check if the addresses are set by reading Scratch & Misc register
622 * 0x70 of Peridot (and potentially Topaz) modules
623 */
Marek Behún35b35132018-12-17 16:10:03 +0100624 if (peridot || topaz) {
Marek Behún84ee2cd2021-10-09 19:33:40 +0200625 struct mii_dev *bus;
Marek Behún35b35132018-12-17 16:10:03 +0100626
627 bus = miiphy_get_dev_by_name("neta@30000");
628 if (!bus) {
629 printf("Cannot get MDIO bus device!\n");
630 } else {
Marek Behún84ee2cd2021-10-09 19:33:40 +0200631 int i;
632
Marek Behún35b35132018-12-17 16:10:03 +0100633 for (i = 0; i < peridot; ++i)
634 check_switch_address(bus, 0x10 + i);
635
636 if (topaz)
637 check_switch_address(bus, 0x2);
638
639 sw_blink_leds(bus, peridot, topaz);
640 }
641 }
642
Marek Behún84d67ac2021-10-09 19:33:41 +0200643handle_reset_btn:
Marek Behún859ada52021-06-07 16:34:47 +0200644 handle_reset_button();
645
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100646 return 0;
Marek Behúnf835bed2018-04-24 17:21:31 +0200647}
Marek Behún4529fce2020-04-08 12:02:05 +0200648
649#if defined(CONFIG_OF_BOARD_SETUP)
650
Marek Behúnd050eab2021-11-26 14:57:11 +0100651static bool is_topaz(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200652{
Marek Behúnd050eab2021-11-26 14:57:11 +0100653 return topaz && id == peridot + topaz - 1;
Marek Behún4529fce2020-04-08 12:02:05 +0200654}
655
Marek Behúnd050eab2021-11-26 14:57:11 +0100656static int switch_addr(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200657{
Marek Behúnd050eab2021-11-26 14:57:11 +0100658 return is_topaz(id) ? 0x2 : 0x10 + id;
Marek Behún4529fce2020-04-08 12:02:05 +0200659}
660
Marek Behúnd050eab2021-11-26 14:57:11 +0100661static int setup_switch(void *blob, int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200662{
Marek Behúnd050eab2021-11-26 14:57:11 +0100663 int res, addr, i, node;
664 char mdio_path[64];
Marek Behún4529fce2020-04-08 12:02:05 +0200665
Marek Behúnd050eab2021-11-26 14:57:11 +0100666 node = fdt_node_offset_by_compatible(blob, -1, "marvell,orion-mdio");
Marek Behún4529fce2020-04-08 12:02:05 +0200667 if (node < 0)
668 return node;
669
Marek Behúnd050eab2021-11-26 14:57:11 +0100670 res = fdt_get_path(blob, node, mdio_path, sizeof(mdio_path));
Marek Behún4529fce2020-04-08 12:02:05 +0200671 if (res < 0)
672 return res;
673
Marek Behún4529fce2020-04-08 12:02:05 +0200674 addr = switch_addr(id);
675
676 /* first enable the switch by setting status = "okay" */
Marek Behúnd050eab2021-11-26 14:57:11 +0100677 res = fdt_status_okay_by_pathf(blob, "%s/switch%i@%x", mdio_path, id,
678 addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200679 if (res < 0)
680 return res;
681
682 /*
683 * now if there are more switches or a SFP module coming after,
684 * enable corresponding ports
685 */
686 if (id < peridot + topaz - 1) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100687 res = fdt_status_okay_by_pathf(blob,
688 "%s/switch%i@%x/ports/port@a",
689 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200690 } else if (id == peridot - 1 && !topaz && sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100691 res = fdt_status_okay_by_pathf(blob,
692 "%s/switch%i@%x/ports/port-sfp@a",
693 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200694 } else {
695 res = 0;
696 }
697 if (res < 0)
698 return res;
699
700 if (id >= peridot + topaz - 1)
701 return 0;
702
703 /* finally change link property if needed */
Marek Behúnd050eab2021-11-26 14:57:11 +0100704 node = fdt_node_offset_by_pathf(blob, "%s/switch%i@%x/ports/port@a",
705 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200706 if (node < 0)
707 return node;
708
709 for (i = id + 1; i < peridot + topaz; ++i) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100710 unsigned int phandle;
711
712 phandle = fdt_create_phandle_by_pathf(blob,
713 "%s/switch%i@%x/ports/port@%x",
714 mdio_path, i,
715 switch_addr(i),
716 is_topaz(i) ? 5 : 9);
717 if (!phandle)
718 return -FDT_ERR_NOPHANDLES;
Marek Behún4529fce2020-04-08 12:02:05 +0200719
720 if (i == id + 1)
721 res = fdt_setprop_u32(blob, node, "link", phandle);
722 else
723 res = fdt_appendprop_u32(blob, node, "link", phandle);
724 if (res < 0)
725 return res;
726 }
727
728 return 0;
729}
730
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900731int ft_board_setup(void *blob, struct bd_info *bd)
Marek Behún4529fce2020-04-08 12:02:05 +0200732{
Marek Behúnd050eab2021-11-26 14:57:11 +0100733 int res;
Marek Behún4529fce2020-04-08 12:02:05 +0200734
735 /*
736 * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
737 * connected, enable the PCIe node.
738 */
739 if (pci || usb || passpci) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100740 res = fdt_status_okay_by_compatible(blob,
741 "marvell,armada-3700-pcie");
Marek Behún4529fce2020-04-08 12:02:05 +0200742 if (res < 0)
743 return res;
Marek Behún455ef5b2020-04-08 19:25:22 +0200744
745 /* Fix PCIe regions for devices with 4 GB RAM */
746 res = a3700_fdt_fix_pcie_regions(blob);
747 if (res < 0)
748 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200749 }
750
751 /*
752 * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
753 * enable the eth1 node and setup the switches.
754 */
755 if (peridot || topaz) {
756 int i;
757
Marek Behúnd050eab2021-11-26 14:57:11 +0100758 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200759 if (res < 0)
760 return res;
761
762 for (i = 0; i < peridot + topaz; ++i) {
763 res = setup_switch(blob, i);
764 if (res < 0)
765 return res;
766 }
767 }
768
769 /*
770 * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
771 * node. If there is no Peridot switch between MOX A and MOX D, add link
772 * to the SFP node to eth1 node.
773 * Also enable and configure SFP GPIO controller node.
774 */
775 if (sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100776 int node;
777
778 res = fdt_status_okay_by_compatible(blob, "sff,sfp");
Marek Behún4529fce2020-04-08 12:02:05 +0200779 if (res < 0)
780 return res;
781
Marek Behúnd050eab2021-11-26 14:57:11 +0100782 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200783 if (res < 0)
784 return res;
785
786 if (!peridot) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100787 unsigned int phandle;
Marek Behún4529fce2020-04-08 12:02:05 +0200788
Marek Behúnd050eab2021-11-26 14:57:11 +0100789 phandle = fdt_create_phandle_by_compatible(blob,
790 "sff,sfp");
791 if (!phandle)
792 return -FDT_ERR_NOPHANDLES;
793
794 node = fdt_path_offset(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200795 if (node < 0)
796 return node;
797
798 res = fdt_setprop_u32(blob, node, "sfp", phandle);
799 if (res < 0)
800 return res;
801
802 res = fdt_setprop_string(blob, node, "phy-mode",
803 "sgmii");
804 if (res < 0)
805 return res;
806 }
807
Marek Behúnd050eab2021-11-26 14:57:11 +0100808 res = fdt_status_okay_by_compatible(blob, "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200809 if (res < 0)
810 return res;
811
812 if (sfp_pos) {
813 char newname[16];
814
815 /* moxtet-sfp is on non-zero position, change default */
Marek Behúnd050eab2021-11-26 14:57:11 +0100816 node = fdt_node_offset_by_compatible(blob, -1,
817 "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200818 if (node < 0)
819 return node;
820
821 res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
822 if (res < 0)
823 return res;
824
825 sprintf(newname, "gpio@%x", sfp_pos);
826
827 res = fdt_set_name(blob, node, newname);
828 if (res < 0)
829 return res;
830 }
831 }
832
833 fdt_fixup_ethernet(blob);
834
835 /* Finally remove disabled nodes, as per Rob Herring's request. */
Marek Behúnac4d9ff2021-11-26 14:57:15 +0100836 fdt_delete_disabled_nodes(blob);
Marek Behún4529fce2020-04-08 12:02:05 +0200837
838 return 0;
839}
840
841#endif