blob: 502f6fc0109e758d2e6b2fdd124438ef6bc61dc3 [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ún9602a8c2018-08-21 12:22:09 +020059 writel(0x10df, ARMADA_37XX_SPI_CFG);
Marek Behúna4e697f2020-04-08 12:02:03 +020060 /* put pin from GPIO to SPI mode */
61 clrbits_le32(ARMADA_37XX_NB_GPIO_SEL, BIT(12));
62 /* enable SPI CS1 */
63 setbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +020064
65 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
66 udelay(1);
67
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010068 status_pcie = FDT_STATUS_DISABLED;
69 status_eth1 = FDT_STATUS_DISABLED;
70
Marek Behún9602a8c2018-08-21 12:22:09 +020071 for (i = 0; i < MAX_MOX_MODULES; ++i) {
72 writel(0x0, ARMADA_37XX_SPI_DOUT);
73
74 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
75 udelay(1);
76
77 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
78 if (topology[i] == 0xff)
79 break;
80
81 topology[i] &= 0xf;
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010082
83 if (topology[i] == MOX_MODULE_SFP ||
84 topology[i] == MOX_MODULE_TOPAZ ||
85 topology[i] == MOX_MODULE_PERIDOT)
86 status_eth1 = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +020087 }
88
89 size = i;
90
Marek Behúna4e697f2020-04-08 12:02:03 +020091 /* disable SPI CS1 */
92 clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +020093
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010094 ret = fdt_set_status_by_alias(blob, "ethernet1", status_eth1);
95 if (ret < 0)
96 printf("Cannot set status for eth1 in U-Boot's device tree: %s!\n",
97 fdt_strerror(ret));
98
Marek Behún9602a8c2018-08-21 12:22:09 +020099 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
100 topology[1] == MOX_MODULE_USB3 ||
101 topology[1] == MOX_MODULE_PASSPCI))
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100102 status_pcie = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +0200103
Marek Behúnd050eab2021-11-26 14:57:11 +0100104 ret = fdt_set_status_by_compatible(blob, "marvell,armada-3700-pcie",
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100105 status_pcie);
Marek Behúnd050eab2021-11-26 14:57:11 +0100106 if (ret < 0) {
107 printf("Cannot set status for PCIe in U-Boot's device tree: %s!\n",
108 fdt_strerror(ret));
Marek Behún9602a8c2018-08-21 12:22:09 +0200109 return 0;
110 }
111
Marek Behún455ef5b2020-04-08 19:25:22 +0200112 if (a3700_fdt_fix_pcie_regions(blob) < 0) {
113 printf("Cannot fix PCIe regions in U-Boot's device tree!\n");
114 return 0;
115 }
116
Marek Behún9602a8c2018-08-21 12:22:09 +0200117 return 0;
118}
119#endif
120
Marek Behúnf835bed2018-04-24 17:21:31 +0200121int board_init(void)
122{
123 /* address of boot parameters */
124 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
125
Marek Behúnf835bed2018-04-24 17:21:31 +0200126 return 0;
127}
128
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100129static int mox_do_spi(u8 *in, u8 *out, size_t size)
Marek Behúnf835bed2018-04-24 17:21:31 +0200130{
131 struct spi_slave *slave;
132 struct udevice *dev;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100133 int ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200134
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100135 ret = spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
136 "spi_generic_drv", "moxtet@1", &dev,
137 &slave);
Marek Behúnf835bed2018-04-24 17:21:31 +0200138 if (ret)
139 goto fail;
140
141 ret = spi_claim_bus(slave);
142 if (ret)
143 goto fail_free;
144
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100145 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
146
147 spi_release_bus(slave);
148fail_free:
149 spi_free_slave(slave);
150fail:
151 return ret;
152}
153
154static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
155{
156 static int is_sd;
157 static u8 topology[MAX_MOX_MODULES - 1];
158 static int size;
159 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
160 int ret, i;
161
162 if (size) {
163 if (ptopology)
164 *ptopology = topology;
165 if (psize)
166 *psize = size;
167 if (pis_sd)
168 *pis_sd = is_sd;
169 return 0;
170 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200171
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100172 memset(din, 0, MAX_MOX_MODULES);
173 memset(dout, 0, MAX_MOX_MODULES);
174
175 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
Marek Behúnf835bed2018-04-24 17:21:31 +0200176 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100177 return ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200178
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100179 if (din[0] == 0x10)
180 is_sd = 1;
181 else if (din[0] == 0x00)
182 is_sd = 0;
183 else
184 return -ENODEV;
Marek Behúnf835bed2018-04-24 17:21:31 +0200185
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100186 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
187 topology[i - 1] = din[i] & 0xf;
188 size = i - 1;
189
190 if (ptopology)
191 *ptopology = topology;
192 if (psize)
193 *psize = size;
194 if (pis_sd)
195 *pis_sd = is_sd;
196
197 return 0;
198}
199
Marek Behúna261d132018-12-17 16:10:02 +0100200int comphy_update_map(struct comphy_map *serdes_map, int count)
201{
202 int ret, i, size, sfpindex = -1, swindex = -1;
203 const u8 *topology;
204
205 ret = mox_get_topology(&topology, &size, NULL);
206 if (ret)
207 return ret;
208
209 for (i = 0; i < size; ++i) {
210 if (topology[i] == MOX_MODULE_SFP && sfpindex == -1)
211 sfpindex = i;
212 else if ((topology[i] == MOX_MODULE_TOPAZ ||
213 topology[i] == MOX_MODULE_PERIDOT) &&
214 swindex == -1)
215 swindex = i;
216 }
217
218 if (sfpindex >= 0 && swindex >= 0) {
219 if (sfpindex < swindex)
Igal Libermanffd5d2f2017-04-26 15:40:00 +0300220 serdes_map[0].speed = COMPHY_SPEED_1_25G;
Marek Behúna261d132018-12-17 16:10:02 +0100221 else
Igal Libermanffd5d2f2017-04-26 15:40:00 +0300222 serdes_map[0].speed = COMPHY_SPEED_3_125G;
Marek Behúna261d132018-12-17 16:10:02 +0100223 } else if (sfpindex >= 0) {
Igal Libermanffd5d2f2017-04-26 15:40:00 +0300224 serdes_map[0].speed = COMPHY_SPEED_1_25G;
Marek Behúna261d132018-12-17 16:10:02 +0100225 } else if (swindex >= 0) {
Igal Libermanffd5d2f2017-04-26 15:40:00 +0300226 serdes_map[0].speed = COMPHY_SPEED_3_125G;
Marek Behúna261d132018-12-17 16:10:02 +0100227 }
228
229 return 0;
230}
231
Marek Behún35b35132018-12-17 16:10:03 +0100232#define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
233#define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
234
235static int sw_multi_read(struct mii_dev *bus, int sw, int dev, int reg)
236{
237 bus->write(bus, sw, 0, 0, SW_SMI_CMD_R(dev, reg));
238 mdelay(5);
239 return bus->read(bus, sw, 0, 1);
240}
241
242static void sw_multi_write(struct mii_dev *bus, int sw, int dev, int reg,
243 u16 val)
244{
245 bus->write(bus, sw, 0, 1, val);
246 bus->write(bus, sw, 0, 0, SW_SMI_CMD_W(dev, reg));
247 mdelay(5);
248}
249
250static int sw_scratch_read(struct mii_dev *bus, int sw, int reg)
251{
252 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
253 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
254}
255
256static void sw_led_write(struct mii_dev *bus, int sw, int port, int reg,
257 u16 val)
258{
259 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
260 | (val & 0x7ff));
261}
262
263static void sw_blink_leds(struct mii_dev *bus, int peridot, int topaz)
264{
265 int i, p;
266 struct {
267 int port;
268 u16 val;
269 int wait;
270 } regs[] = {
271 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
272 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
273 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
274 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
275 };
276
277 for (i = 0; i < 12; ++i) {
278 for (p = 0; p < peridot; ++p) {
279 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
280 regs[i].val);
281 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
282 regs[i].val);
283 }
284 if (topaz) {
285 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
286 regs[i].val);
287 }
288
289 if (regs[i].wait)
290 mdelay(75);
291 }
292}
293
294static void check_switch_address(struct mii_dev *bus, int addr)
295{
296 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
297 printf("Check of switch MDIO address failed for 0x%02x\n",
298 addr);
299}
300
301static int sfp, pci, topaz, peridot, usb, passpci;
302static int sfp_pos, peridot_pos[3];
303static int module_count;
304
305static int configure_peridots(struct gpio_desc *reset_gpio)
306{
307 int i, ret;
308 u8 dout[MAX_MOX_MODULES];
309
310 memset(dout, 0, MAX_MOX_MODULES);
311
312 /* set addresses of Peridot modules */
313 for (i = 0; i < peridot; ++i)
314 dout[module_count - peridot_pos[i]] = (~i) & 3;
315
316 /*
317 * if there is a SFP module connected to the last Peridot module, set
318 * the P10_SMODE to 1 for the Peridot module
319 */
320 if (sfp)
321 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
322
323 dm_gpio_set_value(reset_gpio, 1);
324 mdelay(10);
325
326 ret = mox_do_spi(NULL, dout, module_count + 1);
327
328 mdelay(10);
329 dm_gpio_set_value(reset_gpio, 0);
330
331 mdelay(50);
332
333 return ret;
334}
335
336static int get_reset_gpio(struct gpio_desc *reset_gpio)
337{
338 int node;
339
340 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
341 if (node < 0) {
342 printf("Cannot find Moxtet bus device node!\n");
343 return -1;
344 }
345
346 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
347 reset_gpio, GPIOD_IS_OUT);
348
349 if (!dm_gpio_is_valid(reset_gpio)) {
350 printf("Cannot find reset GPIO for Moxtet bus!\n");
351 return -1;
352 }
353
Marek Behún35572c92018-12-17 16:10:08 +0100354 return 0;
355}
356
357int misc_init_r(void)
358{
Marek Behún98c86c42021-10-09 19:33:42 +0200359 u8 mac[2][6];
360 int i, ret;
Marek Behún35572c92018-12-17 16:10:08 +0100361
Marek Behún98c86c42021-10-09 19:33:42 +0200362 ret = mbox_sp_get_board_info(NULL, mac[0], mac[1], NULL, NULL);
Marek Behún35572c92018-12-17 16:10:08 +0100363 if (ret < 0) {
364 printf("Cannot read data from OTP!\n");
365 return 0;
366 }
367
Marek Behún98c86c42021-10-09 19:33:42 +0200368 for (i = 0; i < 2; ++i) {
369 u8 oldmac[6];
Marek Behún35572c92018-12-17 16:10:08 +0100370
Marek Behún98c86c42021-10-09 19:33:42 +0200371 if (is_valid_ethaddr(mac[i]) &&
372 !eth_env_get_enetaddr_by_index("eth", i, oldmac))
373 eth_env_set_enetaddr_by_index("eth", i, mac[i]);
374 }
Marek Behún35572c92018-12-17 16:10:08 +0100375
Marek Behún35b35132018-12-17 16:10:03 +0100376 return 0;
377}
378
Marek Behúnd1a30792021-06-07 16:34:48 +0200379static void mox_phy_modify(struct phy_device *phydev, int page, int reg,
380 u16 mask, u16 set)
381{
382 int val;
383
384 val = phydev->drv->readext(phydev, MDIO_DEVAD_NONE, page, reg);
385 val &= ~mask;
386 val |= set;
387 phydev->drv->writeext(phydev, MDIO_DEVAD_NONE, page, reg, val);
388}
389
390static void mox_phy_leds_start_blinking(void)
391{
392 struct phy_device *phydev;
393 struct mii_dev *bus;
394
395 bus = miiphy_get_dev_by_name("neta@30000");
396 if (!bus) {
397 printf("Cannot get MDIO bus device!\n");
398 return;
399 }
400
401 phydev = phy_find_by_mask(bus, BIT(1), PHY_INTERFACE_MODE_RGMII);
402 if (!phydev) {
403 printf("Cannot get ethernet PHY!\n");
404 return;
405 }
406
407 mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400);
408 mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb);
409}
410
Marek Behún859ada52021-06-07 16:34:47 +0200411static bool read_reset_button(void)
412{
413 struct udevice *button, *led;
414 int i;
415
Marek Behúnd050eab2021-11-26 14:57:11 +0100416 if (device_get_global_by_ofnode(
417 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
418 "gpio-keys")),
419 &button)) {
Marek Behún859ada52021-06-07 16:34:47 +0200420 printf("Cannot find reset button!\n");
421 return false;
422 }
423
Marek Behúnd050eab2021-11-26 14:57:11 +0100424 if (device_get_global_by_ofnode(
425 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
426 "gpio-leds")),
427 &led)) {
Marek Behún859ada52021-06-07 16:34:47 +0200428 printf("Cannot find status LED!\n");
429 return false;
430 }
431
432 led_set_state(led, LEDST_ON);
433
434 for (i = 0; i < 21; ++i) {
435 if (button_get_state(button) != BUTTON_ON)
436 return false;
437 if (i < 20)
438 mdelay(50);
439 }
440
441 led_set_state(led, LEDST_OFF);
442
443 return true;
444}
445
446static void handle_reset_button(void)
447{
Pali Rohár905c3bf2021-06-14 16:45:58 +0200448 const char * const vars[1] = { "bootcmd_rescue", };
449
450 /*
451 * Ensure that bootcmd_rescue has always stock value, so that running
452 * run bootcmd_rescue
453 * always works correctly.
454 */
455 env_set_default_vars(1, (char * const *)vars, 0);
456
Marek Behún859ada52021-06-07 16:34:47 +0200457 if (read_reset_button()) {
Pali Rohár905c3bf2021-06-14 16:45:58 +0200458 const char * const vars[2] = {
Marek Behún859ada52021-06-07 16:34:47 +0200459 "bootcmd",
Marek Behún859ada52021-06-07 16:34:47 +0200460 "distro_bootcmd",
461 };
462
463 /*
464 * Set the above envs to their default values, in case the user
465 * managed to break them.
466 */
Pali Rohár905c3bf2021-06-14 16:45:58 +0200467 env_set_default_vars(2, (char * const *)vars, 0);
Marek Behún859ada52021-06-07 16:34:47 +0200468
469 /* Ensure bootcmd_rescue is used by distroboot */
470 env_set("boot_targets", "rescue");
471
Marek Behúnd1a30792021-06-07 16:34:48 +0200472 /* start blinking PHY LEDs */
473 mox_phy_leds_start_blinking();
474
Marek Behún859ada52021-06-07 16:34:47 +0200475 printf("RESET button was pressed, overwriting boot_targets!\n");
476 } else {
477 /*
478 * In case the user somehow managed to save environment with
479 * boot_targets=rescue, reset boot_targets to default value.
480 * This could happen in subsequent commands if bootcmd_rescue
481 * failed.
482 */
483 if (!strcmp(env_get("boot_targets"), "rescue")) {
484 const char * const vars[1] = {
485 "boot_targets",
486 };
487
488 env_set_default_vars(1, (char * const *)vars, 0);
489 }
490 }
491}
492
Marek Behún84ee2cd2021-10-09 19:33:40 +0200493int show_board_info(void)
Marek Behún35572c92018-12-17 16:10:08 +0100494{
Marek Behún84ee2cd2021-10-09 19:33:40 +0200495 int i, ret, board_version, ram_size, is_sd;
Marek Behún35572c92018-12-17 16:10:08 +0100496 const char *pub_key;
Marek Behún84ee2cd2021-10-09 19:33:40 +0200497 const u8 *topology;
498 u64 serial_number;
499
500 printf("Model: CZ.NIC Turris Mox Board\n");
Marek Behún35572c92018-12-17 16:10:08 +0100501
502 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
503 &ram_size);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200504 if (ret < 0) {
505 printf(" Cannot read board info: %i\n", ret);
506 } else {
507 printf(" Board version: %i\n", board_version);
508 printf(" RAM size: %i MiB\n", ram_size);
509 printf(" Serial Number: %016llX\n", serial_number);
510 }
Marek Behún35572c92018-12-17 16:10:08 +0100511
512 pub_key = mox_sp_get_ecdsa_public_key();
513 if (pub_key)
514 printf(" ECDSA Public Key: %s\n", pub_key);
515 else
Marek Behún84ee2cd2021-10-09 19:33:40 +0200516 printf(" Cannot read ECDSA Public Key\n");
Marek Behún35572c92018-12-17 16:10:08 +0100517
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100518 ret = mox_get_topology(&topology, &module_count, &is_sd);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200519 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100520 printf("Cannot read module topology!\n");
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100521
Marek Behún35572c92018-12-17 16:10:08 +0100522 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
523
524 if (module_count)
525 printf("Module Topology:\n");
526
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100527 for (i = 0; i < module_count; ++i) {
528 switch (topology[i]) {
529 case MOX_MODULE_SFP:
530 printf("% 4i: SFP Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200531 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100532 case MOX_MODULE_PCI:
533 printf("% 4i: Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200534 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100535 case MOX_MODULE_TOPAZ:
536 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
537 break;
538 case MOX_MODULE_PERIDOT:
539 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
540 break;
541 case MOX_MODULE_USB3:
542 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
543 break;
544 case MOX_MODULE_PASSPCI:
545 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200546 break;
547 default:
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100548 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
Marek Behúnf835bed2018-04-24 17:21:31 +0200549 }
550 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200551
Marek Behún84ee2cd2021-10-09 19:33:40 +0200552 /* check if modules are connected in supported mode */
Marek Behún35b35132018-12-17 16:10:03 +0100553 for (i = 0; i < module_count; ++i) {
554 switch (topology[i]) {
555 case MOX_MODULE_SFP:
556 if (sfp) {
557 printf("Error: Only one SFP module is supported!\n");
558 } else if (topaz) {
559 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
560 } else {
561 sfp_pos = i;
562 ++sfp;
563 }
564 break;
565 case MOX_MODULE_PCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200566 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100567 printf("Error: Only one Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200568 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100569 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200570 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100571 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 +0200572 else
Marek Behún35b35132018-12-17 16:10:03 +0100573 ++pci;
Marek Behún35b35132018-12-17 16:10:03 +0100574 break;
575 case MOX_MODULE_TOPAZ:
Marek Behún4529fce2020-04-08 12:02:05 +0200576 if (topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100577 printf("Error: Only one Topaz module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200578 else if (peridot >= 3)
Marek Behún35b35132018-12-17 16:10:03 +0100579 printf("Error: At most two Peridot modules can come before Topaz module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200580 else
Marek Behún35b35132018-12-17 16:10:03 +0100581 ++topaz;
Marek Behún35b35132018-12-17 16:10:03 +0100582 break;
583 case MOX_MODULE_PERIDOT:
584 if (sfp || topaz) {
585 printf("Error: Peridot module must come before SFP or Topaz module!\n");
586 } else if (peridot >= 3) {
587 printf("Error: At most three Peridot modules are supported!\n");
588 } else {
589 peridot_pos[peridot] = i;
590 ++peridot;
591 }
592 break;
593 case MOX_MODULE_USB3:
Marek Behún4529fce2020-04-08 12:02:05 +0200594 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100595 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200596 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100597 printf("Error: Only one USB 3.0 module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200598 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100599 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 +0200600 else
Marek Behún35b35132018-12-17 16:10:03 +0100601 ++usb;
Marek Behún35b35132018-12-17 16:10:03 +0100602 break;
603 case MOX_MODULE_PASSPCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200604 if (passpci)
Marek Behún35b35132018-12-17 16:10:03 +0100605 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200606 else if (i != 0)
Marek Behún35b35132018-12-17 16:10:03 +0100607 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200608 else
Marek Behún35b35132018-12-17 16:10:03 +0100609 ++passpci;
Marek Behún35b35132018-12-17 16:10:03 +0100610 }
611 }
612
Marek Behún84ee2cd2021-10-09 19:33:40 +0200613 if (module_count)
614 printf("\n");
615
616 return 0;
617}
Marek Behún35b35132018-12-17 16:10:03 +0100618
Marek Behún84ee2cd2021-10-09 19:33:40 +0200619int last_stage_init(void)
620{
621 struct gpio_desc reset_gpio = {};
622
623 /* configure modules */
Marek Behún35b35132018-12-17 16:10:03 +0100624 if (get_reset_gpio(&reset_gpio) < 0)
Marek Behún84d67ac2021-10-09 19:33:41 +0200625 goto handle_reset_btn;
Marek Behún35b35132018-12-17 16:10:03 +0100626
627 if (peridot > 0) {
628 if (configure_peridots(&reset_gpio) < 0) {
629 printf("Cannot configure Peridot modules!\n");
630 peridot = 0;
631 }
632 } else {
633 dm_gpio_set_value(&reset_gpio, 1);
634 mdelay(50);
635 dm_gpio_set_value(&reset_gpio, 0);
636 mdelay(50);
637 }
638
Marek Behún84ee2cd2021-10-09 19:33:40 +0200639 /*
640 * check if the addresses are set by reading Scratch & Misc register
641 * 0x70 of Peridot (and potentially Topaz) modules
642 */
Marek Behún35b35132018-12-17 16:10:03 +0100643 if (peridot || topaz) {
Marek Behún84ee2cd2021-10-09 19:33:40 +0200644 struct mii_dev *bus;
Marek Behún35b35132018-12-17 16:10:03 +0100645
646 bus = miiphy_get_dev_by_name("neta@30000");
647 if (!bus) {
648 printf("Cannot get MDIO bus device!\n");
649 } else {
Marek Behún84ee2cd2021-10-09 19:33:40 +0200650 int i;
651
Marek Behún35b35132018-12-17 16:10:03 +0100652 for (i = 0; i < peridot; ++i)
653 check_switch_address(bus, 0x10 + i);
654
655 if (topaz)
656 check_switch_address(bus, 0x2);
657
658 sw_blink_leds(bus, peridot, topaz);
659 }
660 }
661
Marek Behún84d67ac2021-10-09 19:33:41 +0200662handle_reset_btn:
Marek Behún859ada52021-06-07 16:34:47 +0200663 handle_reset_button();
664
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100665 return 0;
Marek Behúnf835bed2018-04-24 17:21:31 +0200666}
Marek Behún4529fce2020-04-08 12:02:05 +0200667
668#if defined(CONFIG_OF_BOARD_SETUP)
669
Marek Behúnd050eab2021-11-26 14:57:11 +0100670static bool is_topaz(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200671{
Marek Behúnd050eab2021-11-26 14:57:11 +0100672 return topaz && id == peridot + topaz - 1;
Marek Behún4529fce2020-04-08 12:02:05 +0200673}
674
Marek Behúnd050eab2021-11-26 14:57:11 +0100675static int switch_addr(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200676{
Marek Behúnd050eab2021-11-26 14:57:11 +0100677 return is_topaz(id) ? 0x2 : 0x10 + id;
Marek Behún4529fce2020-04-08 12:02:05 +0200678}
679
Marek Behúnd050eab2021-11-26 14:57:11 +0100680static int setup_switch(void *blob, int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200681{
Marek Behúnd050eab2021-11-26 14:57:11 +0100682 int res, addr, i, node;
683 char mdio_path[64];
Marek Behún4529fce2020-04-08 12:02:05 +0200684
Marek Behúnd050eab2021-11-26 14:57:11 +0100685 node = fdt_node_offset_by_compatible(blob, -1, "marvell,orion-mdio");
Marek Behún4529fce2020-04-08 12:02:05 +0200686 if (node < 0)
687 return node;
688
Marek Behúnd050eab2021-11-26 14:57:11 +0100689 res = fdt_get_path(blob, node, mdio_path, sizeof(mdio_path));
Marek Behún4529fce2020-04-08 12:02:05 +0200690 if (res < 0)
691 return res;
692
Marek Behún4529fce2020-04-08 12:02:05 +0200693 addr = switch_addr(id);
694
695 /* first enable the switch by setting status = "okay" */
Marek Behúnd050eab2021-11-26 14:57:11 +0100696 res = fdt_status_okay_by_pathf(blob, "%s/switch%i@%x", mdio_path, id,
697 addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200698 if (res < 0)
699 return res;
700
701 /*
702 * now if there are more switches or a SFP module coming after,
703 * enable corresponding ports
704 */
705 if (id < peridot + topaz - 1) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100706 res = fdt_status_okay_by_pathf(blob,
707 "%s/switch%i@%x/ports/port@a",
708 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200709 } else if (id == peridot - 1 && !topaz && sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100710 res = fdt_status_okay_by_pathf(blob,
711 "%s/switch%i@%x/ports/port-sfp@a",
712 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200713 } else {
714 res = 0;
715 }
716 if (res < 0)
717 return res;
718
719 if (id >= peridot + topaz - 1)
720 return 0;
721
722 /* finally change link property if needed */
Marek Behúnd050eab2021-11-26 14:57:11 +0100723 node = fdt_node_offset_by_pathf(blob, "%s/switch%i@%x/ports/port@a",
724 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200725 if (node < 0)
726 return node;
727
728 for (i = id + 1; i < peridot + topaz; ++i) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100729 unsigned int phandle;
730
731 phandle = fdt_create_phandle_by_pathf(blob,
732 "%s/switch%i@%x/ports/port@%x",
733 mdio_path, i,
734 switch_addr(i),
735 is_topaz(i) ? 5 : 9);
736 if (!phandle)
737 return -FDT_ERR_NOPHANDLES;
Marek Behún4529fce2020-04-08 12:02:05 +0200738
739 if (i == id + 1)
740 res = fdt_setprop_u32(blob, node, "link", phandle);
741 else
742 res = fdt_appendprop_u32(blob, node, "link", phandle);
743 if (res < 0)
744 return res;
745 }
746
747 return 0;
748}
749
750static int remove_disabled_nodes(void *blob)
751{
752 while (1) {
753 int res, offset;
754
755 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
756 "disabled", 9);
757 if (offset < 0)
758 break;
759
760 res = fdt_del_node(blob, offset);
761 if (res < 0)
762 return res;
763 }
764
765 return 0;
766}
767
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900768int ft_board_setup(void *blob, struct bd_info *bd)
Marek Behún4529fce2020-04-08 12:02:05 +0200769{
Marek Behúnd050eab2021-11-26 14:57:11 +0100770 int res;
Marek Behún4529fce2020-04-08 12:02:05 +0200771
772 /*
773 * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
774 * connected, enable the PCIe node.
775 */
776 if (pci || usb || passpci) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100777 res = fdt_status_okay_by_compatible(blob,
778 "marvell,armada-3700-pcie");
Marek Behún4529fce2020-04-08 12:02:05 +0200779 if (res < 0)
780 return res;
Marek Behún455ef5b2020-04-08 19:25:22 +0200781
782 /* Fix PCIe regions for devices with 4 GB RAM */
783 res = a3700_fdt_fix_pcie_regions(blob);
784 if (res < 0)
785 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200786 }
787
788 /*
789 * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
790 * enable the eth1 node and setup the switches.
791 */
792 if (peridot || topaz) {
793 int i;
794
Marek Behúnd050eab2021-11-26 14:57:11 +0100795 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200796 if (res < 0)
797 return res;
798
799 for (i = 0; i < peridot + topaz; ++i) {
800 res = setup_switch(blob, i);
801 if (res < 0)
802 return res;
803 }
804 }
805
806 /*
807 * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
808 * node. If there is no Peridot switch between MOX A and MOX D, add link
809 * to the SFP node to eth1 node.
810 * Also enable and configure SFP GPIO controller node.
811 */
812 if (sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100813 int node;
814
815 res = fdt_status_okay_by_compatible(blob, "sff,sfp");
Marek Behún4529fce2020-04-08 12:02:05 +0200816 if (res < 0)
817 return res;
818
Marek Behúnd050eab2021-11-26 14:57:11 +0100819 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200820 if (res < 0)
821 return res;
822
823 if (!peridot) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100824 unsigned int phandle;
Marek Behún4529fce2020-04-08 12:02:05 +0200825
Marek Behúnd050eab2021-11-26 14:57:11 +0100826 phandle = fdt_create_phandle_by_compatible(blob,
827 "sff,sfp");
828 if (!phandle)
829 return -FDT_ERR_NOPHANDLES;
830
831 node = fdt_path_offset(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200832 if (node < 0)
833 return node;
834
835 res = fdt_setprop_u32(blob, node, "sfp", phandle);
836 if (res < 0)
837 return res;
838
839 res = fdt_setprop_string(blob, node, "phy-mode",
840 "sgmii");
841 if (res < 0)
842 return res;
843 }
844
Marek Behúnd050eab2021-11-26 14:57:11 +0100845 res = fdt_status_okay_by_compatible(blob, "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200846 if (res < 0)
847 return res;
848
849 if (sfp_pos) {
850 char newname[16];
851
852 /* moxtet-sfp is on non-zero position, change default */
Marek Behúnd050eab2021-11-26 14:57:11 +0100853 node = fdt_node_offset_by_compatible(blob, -1,
854 "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200855 if (node < 0)
856 return node;
857
858 res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
859 if (res < 0)
860 return res;
861
862 sprintf(newname, "gpio@%x", sfp_pos);
863
864 res = fdt_set_name(blob, node, newname);
865 if (res < 0)
866 return res;
867 }
868 }
869
870 fdt_fixup_ethernet(blob);
871
872 /* Finally remove disabled nodes, as per Rob Herring's request. */
873 remove_disabled_nodes(blob);
874
875 return 0;
876}
877
878#endif