blob: 809ce82d2014e9bedf6d3881bfcae156d4b27379 [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>
24#include <mvebu/comphy.h>
25#include <spi.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020026
Marek Behún35572c92018-12-17 16:10:08 +010027#include "mox_sp.h"
28
Marek Behún9602a8c2018-08-21 12:22:09 +020029#define MAX_MOX_MODULES 10
30
31#define MOX_MODULE_SFP 0x1
32#define MOX_MODULE_PCI 0x2
33#define MOX_MODULE_TOPAZ 0x3
34#define MOX_MODULE_PERIDOT 0x4
35#define MOX_MODULE_USB3 0x5
36#define MOX_MODULE_PASSPCI 0x6
37
Marek Behún1055ce12020-04-08 12:02:07 +020038#define ARMADA_37XX_NB_GPIO_SEL (MVEBU_REGISTER(0x13830))
39#define ARMADA_37XX_SPI_CTRL (MVEBU_REGISTER(0x10600))
40#define ARMADA_37XX_SPI_CFG (MVEBU_REGISTER(0x10604))
41#define ARMADA_37XX_SPI_DOUT (MVEBU_REGISTER(0x10608))
42#define ARMADA_37XX_SPI_DIN (MVEBU_REGISTER(0x1060c))
Marek Behún9602a8c2018-08-21 12:22:09 +020043
Marek Behúnf835bed2018-04-24 17:21:31 +020044DECLARE_GLOBAL_DATA_PTR;
45
Marek Behún9602a8c2018-08-21 12:22:09 +020046#if defined(CONFIG_OF_BOARD_FIXUP)
47int board_fix_fdt(void *blob)
48{
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010049 enum fdt_status status_pcie, status_eth1;
Marek Behún9602a8c2018-08-21 12:22:09 +020050 u8 topology[MAX_MOX_MODULES];
Marek Behúnd050eab2021-11-26 14:57:11 +010051 int i, size, ret;
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;
72
Marek Behún9602a8c2018-08-21 12:22:09 +020073 for (i = 0; i < MAX_MOX_MODULES; ++i) {
74 writel(0x0, ARMADA_37XX_SPI_DOUT);
75
76 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
77 udelay(1);
78
79 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
80 if (topology[i] == 0xff)
81 break;
82
83 topology[i] &= 0xf;
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010084
85 if (topology[i] == MOX_MODULE_SFP ||
86 topology[i] == MOX_MODULE_TOPAZ ||
87 topology[i] == MOX_MODULE_PERIDOT)
88 status_eth1 = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +020089 }
90
91 size = i;
92
Marek Behúna4e697f2020-04-08 12:02:03 +020093 /* disable SPI CS1 */
94 clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +020095
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010096 ret = fdt_set_status_by_alias(blob, "ethernet1", status_eth1);
97 if (ret < 0)
98 printf("Cannot set status for eth1 in U-Boot's device tree: %s!\n",
99 fdt_strerror(ret));
100
Marek Behún9602a8c2018-08-21 12:22:09 +0200101 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
102 topology[1] == MOX_MODULE_USB3 ||
103 topology[1] == MOX_MODULE_PASSPCI))
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100104 status_pcie = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +0200105
Marek Behúnd050eab2021-11-26 14:57:11 +0100106 ret = fdt_set_status_by_compatible(blob, "marvell,armada-3700-pcie",
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100107 status_pcie);
Marek Behúnd050eab2021-11-26 14:57:11 +0100108 if (ret < 0) {
109 printf("Cannot set status for PCIe in U-Boot's device tree: %s!\n",
110 fdt_strerror(ret));
Marek Behún9602a8c2018-08-21 12:22:09 +0200111 return 0;
112 }
113
Marek Behún455ef5b2020-04-08 19:25:22 +0200114 if (a3700_fdt_fix_pcie_regions(blob) < 0) {
115 printf("Cannot fix PCIe regions in U-Boot's device tree!\n");
116 return 0;
117 }
118
Marek Behún9602a8c2018-08-21 12:22:09 +0200119 return 0;
120}
121#endif
122
Marek Behúnf835bed2018-04-24 17:21:31 +0200123int board_init(void)
124{
125 /* address of boot parameters */
126 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
127
Marek Behúnf835bed2018-04-24 17:21:31 +0200128 return 0;
129}
130
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100131static int mox_do_spi(u8 *in, u8 *out, size_t size)
Marek Behúnf835bed2018-04-24 17:21:31 +0200132{
133 struct spi_slave *slave;
134 struct udevice *dev;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100135 int ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200136
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100137 ret = spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
138 "spi_generic_drv", "moxtet@1", &dev,
139 &slave);
Marek Behúnf835bed2018-04-24 17:21:31 +0200140 if (ret)
141 goto fail;
142
143 ret = spi_claim_bus(slave);
144 if (ret)
145 goto fail_free;
146
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100147 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
148
149 spi_release_bus(slave);
150fail_free:
151 spi_free_slave(slave);
152fail:
153 return ret;
154}
155
156static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
157{
158 static int is_sd;
159 static u8 topology[MAX_MOX_MODULES - 1];
160 static int size;
161 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
162 int ret, i;
163
164 if (size) {
165 if (ptopology)
166 *ptopology = topology;
167 if (psize)
168 *psize = size;
169 if (pis_sd)
170 *pis_sd = is_sd;
171 return 0;
172 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200173
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100174 memset(din, 0, MAX_MOX_MODULES);
175 memset(dout, 0, MAX_MOX_MODULES);
176
177 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
Marek Behúnf835bed2018-04-24 17:21:31 +0200178 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100179 return ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200180
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100181 if (din[0] == 0x10)
182 is_sd = 1;
183 else if (din[0] == 0x00)
184 is_sd = 0;
185 else
186 return -ENODEV;
Marek Behúnf835bed2018-04-24 17:21:31 +0200187
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100188 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
189 topology[i - 1] = din[i] & 0xf;
190 size = i - 1;
191
192 if (ptopology)
193 *ptopology = topology;
194 if (psize)
195 *psize = size;
196 if (pis_sd)
197 *pis_sd = is_sd;
198
199 return 0;
200}
201
Marek Behúna261d132018-12-17 16:10:02 +0100202int comphy_update_map(struct comphy_map *serdes_map, int count)
203{
204 int ret, i, size, sfpindex = -1, swindex = -1;
205 const u8 *topology;
206
207 ret = mox_get_topology(&topology, &size, NULL);
208 if (ret)
209 return ret;
210
211 for (i = 0; i < size; ++i) {
212 if (topology[i] == MOX_MODULE_SFP && sfpindex == -1)
213 sfpindex = i;
214 else if ((topology[i] == MOX_MODULE_TOPAZ ||
215 topology[i] == MOX_MODULE_PERIDOT) &&
216 swindex == -1)
217 swindex = i;
218 }
219
220 if (sfpindex >= 0 && swindex >= 0) {
221 if (sfpindex < swindex)
Igal Libermanffd5d2f2017-04-26 15:40:00 +0300222 serdes_map[0].speed = COMPHY_SPEED_1_25G;
Marek Behúna261d132018-12-17 16:10:02 +0100223 else
Igal Libermanffd5d2f2017-04-26 15:40:00 +0300224 serdes_map[0].speed = COMPHY_SPEED_3_125G;
Marek Behúna261d132018-12-17 16:10:02 +0100225 } else if (sfpindex >= 0) {
Igal Libermanffd5d2f2017-04-26 15:40:00 +0300226 serdes_map[0].speed = COMPHY_SPEED_1_25G;
Marek Behúna261d132018-12-17 16:10:02 +0100227 } else if (swindex >= 0) {
Igal Libermanffd5d2f2017-04-26 15:40:00 +0300228 serdes_map[0].speed = COMPHY_SPEED_3_125G;
Marek Behúna261d132018-12-17 16:10:02 +0100229 }
230
231 return 0;
232}
233
Marek Behún35b35132018-12-17 16:10:03 +0100234#define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
235#define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
236
237static int sw_multi_read(struct mii_dev *bus, int sw, int dev, int reg)
238{
239 bus->write(bus, sw, 0, 0, SW_SMI_CMD_R(dev, reg));
240 mdelay(5);
241 return bus->read(bus, sw, 0, 1);
242}
243
244static void sw_multi_write(struct mii_dev *bus, int sw, int dev, int reg,
245 u16 val)
246{
247 bus->write(bus, sw, 0, 1, val);
248 bus->write(bus, sw, 0, 0, SW_SMI_CMD_W(dev, reg));
249 mdelay(5);
250}
251
252static int sw_scratch_read(struct mii_dev *bus, int sw, int reg)
253{
254 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
255 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
256}
257
258static void sw_led_write(struct mii_dev *bus, int sw, int port, int reg,
259 u16 val)
260{
261 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
262 | (val & 0x7ff));
263}
264
265static void sw_blink_leds(struct mii_dev *bus, int peridot, int topaz)
266{
267 int i, p;
268 struct {
269 int port;
270 u16 val;
271 int wait;
272 } regs[] = {
273 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
274 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
275 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
276 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
277 };
278
279 for (i = 0; i < 12; ++i) {
280 for (p = 0; p < peridot; ++p) {
281 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
282 regs[i].val);
283 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
284 regs[i].val);
285 }
286 if (topaz) {
287 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
288 regs[i].val);
289 }
290
291 if (regs[i].wait)
292 mdelay(75);
293 }
294}
295
296static void check_switch_address(struct mii_dev *bus, int addr)
297{
298 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
299 printf("Check of switch MDIO address failed for 0x%02x\n",
300 addr);
301}
302
303static int sfp, pci, topaz, peridot, usb, passpci;
304static int sfp_pos, peridot_pos[3];
305static int module_count;
306
307static int configure_peridots(struct gpio_desc *reset_gpio)
308{
309 int i, ret;
310 u8 dout[MAX_MOX_MODULES];
311
312 memset(dout, 0, MAX_MOX_MODULES);
313
314 /* set addresses of Peridot modules */
315 for (i = 0; i < peridot; ++i)
316 dout[module_count - peridot_pos[i]] = (~i) & 3;
317
318 /*
319 * if there is a SFP module connected to the last Peridot module, set
320 * the P10_SMODE to 1 for the Peridot module
321 */
322 if (sfp)
323 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
324
325 dm_gpio_set_value(reset_gpio, 1);
326 mdelay(10);
327
328 ret = mox_do_spi(NULL, dout, module_count + 1);
329
330 mdelay(10);
331 dm_gpio_set_value(reset_gpio, 0);
332
333 mdelay(50);
334
335 return ret;
336}
337
338static int get_reset_gpio(struct gpio_desc *reset_gpio)
339{
340 int node;
341
342 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
343 if (node < 0) {
344 printf("Cannot find Moxtet bus device node!\n");
345 return -1;
346 }
347
348 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
349 reset_gpio, GPIOD_IS_OUT);
350
351 if (!dm_gpio_is_valid(reset_gpio)) {
352 printf("Cannot find reset GPIO for Moxtet bus!\n");
353 return -1;
354 }
355
Marek Behún35572c92018-12-17 16:10:08 +0100356 return 0;
357}
358
359int misc_init_r(void)
360{
Marek Behún98c86c42021-10-09 19:33:42 +0200361 u8 mac[2][6];
362 int i, ret;
Marek Behún35572c92018-12-17 16:10:08 +0100363
Marek Behún98c86c42021-10-09 19:33:42 +0200364 ret = mbox_sp_get_board_info(NULL, mac[0], mac[1], NULL, NULL);
Marek Behún35572c92018-12-17 16:10:08 +0100365 if (ret < 0) {
366 printf("Cannot read data from OTP!\n");
367 return 0;
368 }
369
Marek Behún98c86c42021-10-09 19:33:42 +0200370 for (i = 0; i < 2; ++i) {
371 u8 oldmac[6];
Marek Behún35572c92018-12-17 16:10:08 +0100372
Marek Behún98c86c42021-10-09 19:33:42 +0200373 if (is_valid_ethaddr(mac[i]) &&
374 !eth_env_get_enetaddr_by_index("eth", i, oldmac))
375 eth_env_set_enetaddr_by_index("eth", i, mac[i]);
376 }
Marek Behún35572c92018-12-17 16:10:08 +0100377
Marek Behún35b35132018-12-17 16:10:03 +0100378 return 0;
379}
380
Marek Behúnd1a30792021-06-07 16:34:48 +0200381static void mox_phy_modify(struct phy_device *phydev, int page, int reg,
382 u16 mask, u16 set)
383{
384 int val;
385
386 val = phydev->drv->readext(phydev, MDIO_DEVAD_NONE, page, reg);
387 val &= ~mask;
388 val |= set;
389 phydev->drv->writeext(phydev, MDIO_DEVAD_NONE, page, reg, val);
390}
391
392static void mox_phy_leds_start_blinking(void)
393{
394 struct phy_device *phydev;
395 struct mii_dev *bus;
396
397 bus = miiphy_get_dev_by_name("neta@30000");
398 if (!bus) {
399 printf("Cannot get MDIO bus device!\n");
400 return;
401 }
402
403 phydev = phy_find_by_mask(bus, BIT(1), PHY_INTERFACE_MODE_RGMII);
404 if (!phydev) {
405 printf("Cannot get ethernet PHY!\n");
406 return;
407 }
408
409 mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400);
410 mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb);
411}
412
Marek Behún859ada52021-06-07 16:34:47 +0200413static bool read_reset_button(void)
414{
415 struct udevice *button, *led;
416 int i;
417
Marek Behúnd050eab2021-11-26 14:57:11 +0100418 if (device_get_global_by_ofnode(
419 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
420 "gpio-keys")),
421 &button)) {
Marek Behún859ada52021-06-07 16:34:47 +0200422 printf("Cannot find reset button!\n");
423 return false;
424 }
425
Marek Behúnd050eab2021-11-26 14:57:11 +0100426 if (device_get_global_by_ofnode(
427 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
428 "gpio-leds")),
429 &led)) {
Marek Behún859ada52021-06-07 16:34:47 +0200430 printf("Cannot find status LED!\n");
431 return false;
432 }
433
434 led_set_state(led, LEDST_ON);
435
436 for (i = 0; i < 21; ++i) {
437 if (button_get_state(button) != BUTTON_ON)
438 return false;
439 if (i < 20)
440 mdelay(50);
441 }
442
443 led_set_state(led, LEDST_OFF);
444
445 return true;
446}
447
448static void handle_reset_button(void)
449{
Pali Rohár905c3bf2021-06-14 16:45:58 +0200450 const char * const vars[1] = { "bootcmd_rescue", };
451
452 /*
453 * Ensure that bootcmd_rescue has always stock value, so that running
454 * run bootcmd_rescue
455 * always works correctly.
456 */
457 env_set_default_vars(1, (char * const *)vars, 0);
458
Marek Behún859ada52021-06-07 16:34:47 +0200459 if (read_reset_button()) {
Pali Rohár905c3bf2021-06-14 16:45:58 +0200460 const char * const vars[2] = {
Marek Behún859ada52021-06-07 16:34:47 +0200461 "bootcmd",
Marek Behún859ada52021-06-07 16:34:47 +0200462 "distro_bootcmd",
463 };
464
465 /*
466 * Set the above envs to their default values, in case the user
467 * managed to break them.
468 */
Pali Rohár905c3bf2021-06-14 16:45:58 +0200469 env_set_default_vars(2, (char * const *)vars, 0);
Marek Behún859ada52021-06-07 16:34:47 +0200470
471 /* Ensure bootcmd_rescue is used by distroboot */
472 env_set("boot_targets", "rescue");
473
Marek Behúnd1a30792021-06-07 16:34:48 +0200474 /* start blinking PHY LEDs */
475 mox_phy_leds_start_blinking();
476
Marek Behún859ada52021-06-07 16:34:47 +0200477 printf("RESET button was pressed, overwriting boot_targets!\n");
478 } else {
479 /*
480 * In case the user somehow managed to save environment with
481 * boot_targets=rescue, reset boot_targets to default value.
482 * This could happen in subsequent commands if bootcmd_rescue
483 * failed.
484 */
485 if (!strcmp(env_get("boot_targets"), "rescue")) {
486 const char * const vars[1] = {
487 "boot_targets",
488 };
489
490 env_set_default_vars(1, (char * const *)vars, 0);
491 }
492 }
493}
494
Marek Behún84ee2cd2021-10-09 19:33:40 +0200495int show_board_info(void)
Marek Behún35572c92018-12-17 16:10:08 +0100496{
Marek Behún84ee2cd2021-10-09 19:33:40 +0200497 int i, ret, board_version, ram_size, is_sd;
Marek Behún35572c92018-12-17 16:10:08 +0100498 const char *pub_key;
Marek Behún84ee2cd2021-10-09 19:33:40 +0200499 const u8 *topology;
500 u64 serial_number;
501
502 printf("Model: CZ.NIC Turris Mox Board\n");
Marek Behún35572c92018-12-17 16:10:08 +0100503
504 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
505 &ram_size);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200506 if (ret < 0) {
507 printf(" Cannot read board info: %i\n", ret);
508 } else {
509 printf(" Board version: %i\n", board_version);
510 printf(" RAM size: %i MiB\n", ram_size);
511 printf(" Serial Number: %016llX\n", serial_number);
512 }
Marek Behún35572c92018-12-17 16:10:08 +0100513
514 pub_key = mox_sp_get_ecdsa_public_key();
515 if (pub_key)
516 printf(" ECDSA Public Key: %s\n", pub_key);
517 else
Marek Behún84ee2cd2021-10-09 19:33:40 +0200518 printf(" Cannot read ECDSA Public Key\n");
Marek Behún35572c92018-12-17 16:10:08 +0100519
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100520 ret = mox_get_topology(&topology, &module_count, &is_sd);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200521 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100522 printf("Cannot read module topology!\n");
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100523
Marek Behún35572c92018-12-17 16:10:08 +0100524 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
525
526 if (module_count)
527 printf("Module Topology:\n");
528
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100529 for (i = 0; i < module_count; ++i) {
530 switch (topology[i]) {
531 case MOX_MODULE_SFP:
532 printf("% 4i: SFP Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200533 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100534 case MOX_MODULE_PCI:
535 printf("% 4i: Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200536 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100537 case MOX_MODULE_TOPAZ:
538 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
539 break;
540 case MOX_MODULE_PERIDOT:
541 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
542 break;
543 case MOX_MODULE_USB3:
544 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
545 break;
546 case MOX_MODULE_PASSPCI:
547 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200548 break;
549 default:
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100550 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
Marek Behúnf835bed2018-04-24 17:21:31 +0200551 }
552 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200553
Marek Behún84ee2cd2021-10-09 19:33:40 +0200554 /* check if modules are connected in supported mode */
Marek Behún35b35132018-12-17 16:10:03 +0100555 for (i = 0; i < module_count; ++i) {
556 switch (topology[i]) {
557 case MOX_MODULE_SFP:
558 if (sfp) {
559 printf("Error: Only one SFP module is supported!\n");
560 } else if (topaz) {
561 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
562 } else {
563 sfp_pos = i;
564 ++sfp;
565 }
566 break;
567 case MOX_MODULE_PCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200568 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100569 printf("Error: Only one Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200570 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100571 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200572 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100573 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 +0200574 else
Marek Behún35b35132018-12-17 16:10:03 +0100575 ++pci;
Marek Behún35b35132018-12-17 16:10:03 +0100576 break;
577 case MOX_MODULE_TOPAZ:
Marek Behún4529fce2020-04-08 12:02:05 +0200578 if (topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100579 printf("Error: Only one Topaz module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200580 else if (peridot >= 3)
Marek Behún35b35132018-12-17 16:10:03 +0100581 printf("Error: At most two Peridot modules can come before Topaz module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200582 else
Marek Behún35b35132018-12-17 16:10:03 +0100583 ++topaz;
Marek Behún35b35132018-12-17 16:10:03 +0100584 break;
585 case MOX_MODULE_PERIDOT:
586 if (sfp || topaz) {
587 printf("Error: Peridot module must come before SFP or Topaz module!\n");
588 } else if (peridot >= 3) {
589 printf("Error: At most three Peridot modules are supported!\n");
590 } else {
591 peridot_pos[peridot] = i;
592 ++peridot;
593 }
594 break;
595 case MOX_MODULE_USB3:
Marek Behún4529fce2020-04-08 12:02:05 +0200596 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100597 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200598 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100599 printf("Error: Only one USB 3.0 module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200600 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100601 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 +0200602 else
Marek Behún35b35132018-12-17 16:10:03 +0100603 ++usb;
Marek Behún35b35132018-12-17 16:10:03 +0100604 break;
605 case MOX_MODULE_PASSPCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200606 if (passpci)
Marek Behún35b35132018-12-17 16:10:03 +0100607 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200608 else if (i != 0)
Marek Behún35b35132018-12-17 16:10:03 +0100609 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200610 else
Marek Behún35b35132018-12-17 16:10:03 +0100611 ++passpci;
Marek Behún35b35132018-12-17 16:10:03 +0100612 }
613 }
614
Marek Behún84ee2cd2021-10-09 19:33:40 +0200615 if (module_count)
616 printf("\n");
617
618 return 0;
619}
Marek Behún35b35132018-12-17 16:10:03 +0100620
Marek Behún84ee2cd2021-10-09 19:33:40 +0200621int last_stage_init(void)
622{
623 struct gpio_desc reset_gpio = {};
624
625 /* configure modules */
Marek Behún35b35132018-12-17 16:10:03 +0100626 if (get_reset_gpio(&reset_gpio) < 0)
Marek Behún84d67ac2021-10-09 19:33:41 +0200627 goto handle_reset_btn;
Marek Behún35b35132018-12-17 16:10:03 +0100628
629 if (peridot > 0) {
630 if (configure_peridots(&reset_gpio) < 0) {
631 printf("Cannot configure Peridot modules!\n");
632 peridot = 0;
633 }
634 } else {
635 dm_gpio_set_value(&reset_gpio, 1);
636 mdelay(50);
637 dm_gpio_set_value(&reset_gpio, 0);
638 mdelay(50);
639 }
640
Marek Behún84ee2cd2021-10-09 19:33:40 +0200641 /*
642 * check if the addresses are set by reading Scratch & Misc register
643 * 0x70 of Peridot (and potentially Topaz) modules
644 */
Marek Behún35b35132018-12-17 16:10:03 +0100645 if (peridot || topaz) {
Marek Behún84ee2cd2021-10-09 19:33:40 +0200646 struct mii_dev *bus;
Marek Behún35b35132018-12-17 16:10:03 +0100647
648 bus = miiphy_get_dev_by_name("neta@30000");
649 if (!bus) {
650 printf("Cannot get MDIO bus device!\n");
651 } else {
Marek Behún84ee2cd2021-10-09 19:33:40 +0200652 int i;
653
Marek Behún35b35132018-12-17 16:10:03 +0100654 for (i = 0; i < peridot; ++i)
655 check_switch_address(bus, 0x10 + i);
656
657 if (topaz)
658 check_switch_address(bus, 0x2);
659
660 sw_blink_leds(bus, peridot, topaz);
661 }
662 }
663
Marek Behún84d67ac2021-10-09 19:33:41 +0200664handle_reset_btn:
Marek Behún859ada52021-06-07 16:34:47 +0200665 handle_reset_button();
666
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100667 return 0;
Marek Behúnf835bed2018-04-24 17:21:31 +0200668}
Marek Behún4529fce2020-04-08 12:02:05 +0200669
670#if defined(CONFIG_OF_BOARD_SETUP)
671
Marek Behúnd050eab2021-11-26 14:57:11 +0100672static bool is_topaz(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200673{
Marek Behúnd050eab2021-11-26 14:57:11 +0100674 return topaz && id == peridot + topaz - 1;
Marek Behún4529fce2020-04-08 12:02:05 +0200675}
676
Marek Behúnd050eab2021-11-26 14:57:11 +0100677static int switch_addr(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200678{
Marek Behúnd050eab2021-11-26 14:57:11 +0100679 return is_topaz(id) ? 0x2 : 0x10 + id;
Marek Behún4529fce2020-04-08 12:02:05 +0200680}
681
Marek Behúnd050eab2021-11-26 14:57:11 +0100682static int setup_switch(void *blob, int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200683{
Marek Behúnd050eab2021-11-26 14:57:11 +0100684 int res, addr, i, node;
685 char mdio_path[64];
Marek Behún4529fce2020-04-08 12:02:05 +0200686
Marek Behúnd050eab2021-11-26 14:57:11 +0100687 node = fdt_node_offset_by_compatible(blob, -1, "marvell,orion-mdio");
Marek Behún4529fce2020-04-08 12:02:05 +0200688 if (node < 0)
689 return node;
690
Marek Behúnd050eab2021-11-26 14:57:11 +0100691 res = fdt_get_path(blob, node, mdio_path, sizeof(mdio_path));
Marek Behún4529fce2020-04-08 12:02:05 +0200692 if (res < 0)
693 return res;
694
Marek Behún4529fce2020-04-08 12:02:05 +0200695 addr = switch_addr(id);
696
697 /* first enable the switch by setting status = "okay" */
Marek Behúnd050eab2021-11-26 14:57:11 +0100698 res = fdt_status_okay_by_pathf(blob, "%s/switch%i@%x", mdio_path, id,
699 addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200700 if (res < 0)
701 return res;
702
703 /*
704 * now if there are more switches or a SFP module coming after,
705 * enable corresponding ports
706 */
707 if (id < peridot + topaz - 1) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100708 res = fdt_status_okay_by_pathf(blob,
709 "%s/switch%i@%x/ports/port@a",
710 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200711 } else if (id == peridot - 1 && !topaz && sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100712 res = fdt_status_okay_by_pathf(blob,
713 "%s/switch%i@%x/ports/port-sfp@a",
714 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200715 } else {
716 res = 0;
717 }
718 if (res < 0)
719 return res;
720
721 if (id >= peridot + topaz - 1)
722 return 0;
723
724 /* finally change link property if needed */
Marek Behúnd050eab2021-11-26 14:57:11 +0100725 node = fdt_node_offset_by_pathf(blob, "%s/switch%i@%x/ports/port@a",
726 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200727 if (node < 0)
728 return node;
729
730 for (i = id + 1; i < peridot + topaz; ++i) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100731 unsigned int phandle;
732
733 phandle = fdt_create_phandle_by_pathf(blob,
734 "%s/switch%i@%x/ports/port@%x",
735 mdio_path, i,
736 switch_addr(i),
737 is_topaz(i) ? 5 : 9);
738 if (!phandle)
739 return -FDT_ERR_NOPHANDLES;
Marek Behún4529fce2020-04-08 12:02:05 +0200740
741 if (i == id + 1)
742 res = fdt_setprop_u32(blob, node, "link", phandle);
743 else
744 res = fdt_appendprop_u32(blob, node, "link", phandle);
745 if (res < 0)
746 return res;
747 }
748
749 return 0;
750}
751
752static int remove_disabled_nodes(void *blob)
753{
754 while (1) {
755 int res, offset;
756
757 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
758 "disabled", 9);
759 if (offset < 0)
760 break;
761
762 res = fdt_del_node(blob, offset);
763 if (res < 0)
764 return res;
765 }
766
767 return 0;
768}
769
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900770int ft_board_setup(void *blob, struct bd_info *bd)
Marek Behún4529fce2020-04-08 12:02:05 +0200771{
Marek Behúnd050eab2021-11-26 14:57:11 +0100772 int res;
Marek Behún4529fce2020-04-08 12:02:05 +0200773
774 /*
775 * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
776 * connected, enable the PCIe node.
777 */
778 if (pci || usb || passpci) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100779 res = fdt_status_okay_by_compatible(blob,
780 "marvell,armada-3700-pcie");
Marek Behún4529fce2020-04-08 12:02:05 +0200781 if (res < 0)
782 return res;
Marek Behún455ef5b2020-04-08 19:25:22 +0200783
784 /* Fix PCIe regions for devices with 4 GB RAM */
785 res = a3700_fdt_fix_pcie_regions(blob);
786 if (res < 0)
787 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200788 }
789
790 /*
791 * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
792 * enable the eth1 node and setup the switches.
793 */
794 if (peridot || topaz) {
795 int i;
796
Marek Behúnd050eab2021-11-26 14:57:11 +0100797 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200798 if (res < 0)
799 return res;
800
801 for (i = 0; i < peridot + topaz; ++i) {
802 res = setup_switch(blob, i);
803 if (res < 0)
804 return res;
805 }
806 }
807
808 /*
809 * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
810 * node. If there is no Peridot switch between MOX A and MOX D, add link
811 * to the SFP node to eth1 node.
812 * Also enable and configure SFP GPIO controller node.
813 */
814 if (sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100815 int node;
816
817 res = fdt_status_okay_by_compatible(blob, "sff,sfp");
Marek Behún4529fce2020-04-08 12:02:05 +0200818 if (res < 0)
819 return res;
820
Marek Behúnd050eab2021-11-26 14:57:11 +0100821 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200822 if (res < 0)
823 return res;
824
825 if (!peridot) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100826 unsigned int phandle;
Marek Behún4529fce2020-04-08 12:02:05 +0200827
Marek Behúnd050eab2021-11-26 14:57:11 +0100828 phandle = fdt_create_phandle_by_compatible(blob,
829 "sff,sfp");
830 if (!phandle)
831 return -FDT_ERR_NOPHANDLES;
832
833 node = fdt_path_offset(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200834 if (node < 0)
835 return node;
836
837 res = fdt_setprop_u32(blob, node, "sfp", phandle);
838 if (res < 0)
839 return res;
840
841 res = fdt_setprop_string(blob, node, "phy-mode",
842 "sgmii");
843 if (res < 0)
844 return res;
845 }
846
Marek Behúnd050eab2021-11-26 14:57:11 +0100847 res = fdt_status_okay_by_compatible(blob, "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200848 if (res < 0)
849 return res;
850
851 if (sfp_pos) {
852 char newname[16];
853
854 /* moxtet-sfp is on non-zero position, change default */
Marek Behúnd050eab2021-11-26 14:57:11 +0100855 node = fdt_node_offset_by_compatible(blob, -1,
856 "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200857 if (node < 0)
858 return node;
859
860 res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
861 if (res < 0)
862 return res;
863
864 sprintf(newname, "gpio@%x", sfp_pos);
865
866 res = fdt_set_name(blob, node, newname);
867 if (res < 0)
868 return res;
869 }
870 }
871
872 fdt_fixup_ethernet(blob);
873
874 /* Finally remove disabled nodes, as per Rob Herring's request. */
875 remove_disabled_nodes(blob);
876
877 return 0;
878}
879
880#endif