blob: 370c2668b081e68f5d94483fce0515846d30b7e1 [file] [log] [blame]
Marek Behúnf835bed2018-04-24 17:21:31 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
Marek Behúnd63726e2022-06-01 17:17:06 +02003 * Copyright (C) 2018 Marek Behún <kabel@kernel.org>
Marek Behúnf835bed2018-04-24 17:21:31 +02004 */
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>
Marek Behún1c8eb902022-04-27 12:41:50 +020016#include <dm/of_extra.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060017#include <env.h>
Simon Glass1cedca12023-08-21 21:17:01 -060018#include <event.h>
Marek Behún9602a8c2018-08-21 12:22:09 +020019#include <fdt_support.h>
Marek Behún28e935f2020-04-08 12:02:08 +020020#include <init.h>
Marek Behún859ada52021-06-07 16:34:47 +020021#include <led.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Marek Behún28e935f2020-04-08 12:02:08 +020023#include <linux/libfdt.h>
24#include <linux/string.h>
25#include <miiphy.h>
Marek Behún28e935f2020-04-08 12:02:08 +020026#include <spi.h>
Pali Rohár584256a2022-08-29 15:44:48 +020027#include <spi_flash.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020028
Marek Behún35572c92018-12-17 16:10:08 +010029#include "mox_sp.h"
30
Marek Behún9602a8c2018-08-21 12:22:09 +020031#define MAX_MOX_MODULES 10
32
33#define MOX_MODULE_SFP 0x1
34#define MOX_MODULE_PCI 0x2
35#define MOX_MODULE_TOPAZ 0x3
36#define MOX_MODULE_PERIDOT 0x4
37#define MOX_MODULE_USB3 0x5
38#define MOX_MODULE_PASSPCI 0x6
39
Marek Behún1055ce12020-04-08 12:02:07 +020040#define ARMADA_37XX_NB_GPIO_SEL (MVEBU_REGISTER(0x13830))
41#define ARMADA_37XX_SPI_CTRL (MVEBU_REGISTER(0x10600))
42#define ARMADA_37XX_SPI_CFG (MVEBU_REGISTER(0x10604))
43#define ARMADA_37XX_SPI_DOUT (MVEBU_REGISTER(0x10608))
44#define ARMADA_37XX_SPI_DIN (MVEBU_REGISTER(0x1060c))
Marek Behún9602a8c2018-08-21 12:22:09 +020045
Marek Behúnf835bed2018-04-24 17:21:31 +020046DECLARE_GLOBAL_DATA_PTR;
47
Marek Behún9602a8c2018-08-21 12:22:09 +020048#if defined(CONFIG_OF_BOARD_FIXUP)
49int board_fix_fdt(void *blob)
50{
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010051 enum fdt_status status_pcie, status_eth1;
Marek Behún9602a8c2018-08-21 12:22:09 +020052 u8 topology[MAX_MOX_MODULES];
Marek Behúnd050eab2021-11-26 14:57:11 +010053 int i, size, ret;
Pali Rohár951b9822022-01-18 10:33:19 +010054 bool eth1_sgmii;
Marek Behún9602a8c2018-08-21 12:22:09 +020055
56 /*
57 * SPI driver is not loaded in driver model yet, but we have to find out
58 * if pcie should be enabled in U-Boot's device tree. Therefore we have
59 * to read SPI by reading/writing SPI registers directly
60 */
61
Marek Behúna4e697f2020-04-08 12:02:03 +020062 /* put pin from GPIO to SPI mode */
63 clrbits_le32(ARMADA_37XX_NB_GPIO_SEL, BIT(12));
Marek Behún77d93122021-11-26 14:57:14 +010064 /* configure cpol, cpha, prescale */
65 writel(0x10df, ARMADA_37XX_SPI_CFG);
66 mdelay(1);
Marek Behúna4e697f2020-04-08 12:02:03 +020067 /* enable SPI CS1 */
68 setbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +020069
70 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
71 udelay(1);
72
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010073 status_pcie = FDT_STATUS_DISABLED;
74 status_eth1 = FDT_STATUS_DISABLED;
Pali Rohár951b9822022-01-18 10:33:19 +010075 eth1_sgmii = false;
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010076
Marek Behún9602a8c2018-08-21 12:22:09 +020077 for (i = 0; i < MAX_MOX_MODULES; ++i) {
78 writel(0x0, ARMADA_37XX_SPI_DOUT);
79
80 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
81 udelay(1);
82
83 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
84 if (topology[i] == 0xff)
85 break;
86
87 topology[i] &= 0xf;
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010088
Pali Rohár951b9822022-01-18 10:33:19 +010089 if (topology[i] == MOX_MODULE_SFP &&
90 status_pcie == FDT_STATUS_DISABLED)
91 eth1_sgmii = true;
92
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010093 if (topology[i] == MOX_MODULE_SFP ||
94 topology[i] == MOX_MODULE_TOPAZ ||
95 topology[i] == MOX_MODULE_PERIDOT)
96 status_eth1 = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +020097 }
98
99 size = i;
100
Marek Behúna4e697f2020-04-08 12:02:03 +0200101 /* disable SPI CS1 */
102 clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +0200103
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100104 ret = fdt_set_status_by_alias(blob, "ethernet1", status_eth1);
105 if (ret < 0)
106 printf("Cannot set status for eth1 in U-Boot's device tree: %s!\n",
107 fdt_strerror(ret));
108
Pali Rohár951b9822022-01-18 10:33:19 +0100109 if (eth1_sgmii) {
110 ret = fdt_path_offset(blob, "ethernet1");
111 if (ret >= 0)
112 ret = fdt_setprop_string(blob, ret, "phy-mode", "sgmii");
113 if (ret < 0)
114 printf("Cannot set phy-mode for eth1 to sgmii in U-Boot device tree: %s!\n",
115 fdt_strerror(ret));
116 }
117
Marek Behún9602a8c2018-08-21 12:22:09 +0200118 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
119 topology[1] == MOX_MODULE_USB3 ||
120 topology[1] == MOX_MODULE_PASSPCI))
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100121 status_pcie = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +0200122
Marek Behúnd050eab2021-11-26 14:57:11 +0100123 ret = fdt_set_status_by_compatible(blob, "marvell,armada-3700-pcie",
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100124 status_pcie);
Marek Behúnd050eab2021-11-26 14:57:11 +0100125 if (ret < 0) {
126 printf("Cannot set status for PCIe in U-Boot's device tree: %s!\n",
127 fdt_strerror(ret));
Marek Behún9602a8c2018-08-21 12:22:09 +0200128 return 0;
129 }
130
Marek Behún455ef5b2020-04-08 19:25:22 +0200131 if (a3700_fdt_fix_pcie_regions(blob) < 0) {
132 printf("Cannot fix PCIe regions in U-Boot's device tree!\n");
133 return 0;
134 }
135
Marek Behún9602a8c2018-08-21 12:22:09 +0200136 return 0;
137}
138#endif
139
Marek Behúnf835bed2018-04-24 17:21:31 +0200140int board_init(void)
141{
142 /* address of boot parameters */
Tom Rinibb4dd962022-11-16 13:10:37 -0500143 gd->bd->bi_boot_params = CFG_SYS_SDRAM_BASE + 0x100;
Marek Behúnf835bed2018-04-24 17:21:31 +0200144
Marek Behúnf835bed2018-04-24 17:21:31 +0200145 return 0;
146}
147
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100148static int mox_do_spi(u8 *in, u8 *out, size_t size)
Marek Behúnf835bed2018-04-24 17:21:31 +0200149{
150 struct spi_slave *slave;
151 struct udevice *dev;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100152 int ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200153
Patrice Chotard86e06ae2022-03-30 09:33:13 +0200154 ret = _spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
155 "spi_generic_drv", "moxtet@1", &dev,
156 &slave);
Marek Behúnf835bed2018-04-24 17:21:31 +0200157 if (ret)
158 goto fail;
159
160 ret = spi_claim_bus(slave);
161 if (ret)
162 goto fail_free;
163
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100164 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
165
166 spi_release_bus(slave);
167fail_free:
168 spi_free_slave(slave);
169fail:
170 return ret;
171}
172
173static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
174{
175 static int is_sd;
176 static u8 topology[MAX_MOX_MODULES - 1];
177 static int size;
178 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
179 int ret, i;
180
181 if (size) {
182 if (ptopology)
183 *ptopology = topology;
184 if (psize)
185 *psize = size;
186 if (pis_sd)
187 *pis_sd = is_sd;
188 return 0;
189 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200190
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100191 memset(din, 0, MAX_MOX_MODULES);
192 memset(dout, 0, MAX_MOX_MODULES);
193
194 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
Marek Behúnf835bed2018-04-24 17:21:31 +0200195 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100196 return ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200197
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100198 if (din[0] == 0x10)
199 is_sd = 1;
200 else if (din[0] == 0x00)
201 is_sd = 0;
202 else
203 return -ENODEV;
Marek Behúnf835bed2018-04-24 17:21:31 +0200204
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100205 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
206 topology[i - 1] = din[i] & 0xf;
207 size = i - 1;
208
209 if (ptopology)
210 *ptopology = topology;
211 if (psize)
212 *psize = size;
213 if (pis_sd)
214 *pis_sd = is_sd;
215
216 return 0;
217}
218
Marek Behún35b35132018-12-17 16:10:03 +0100219#define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
220#define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
221
Marek Behún1c8eb902022-04-27 12:41:50 +0200222static int sw_multi_read(struct udevice *bus, int sw, int dev, int reg)
Marek Behún35b35132018-12-17 16:10:03 +0100223{
Marek Behún1c8eb902022-04-27 12:41:50 +0200224 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 0, SW_SMI_CMD_R(dev, reg));
Marek Behún35b35132018-12-17 16:10:03 +0100225 mdelay(5);
Marek Behún1c8eb902022-04-27 12:41:50 +0200226 return dm_mdio_read(bus, sw, MDIO_DEVAD_NONE, 1);
Marek Behún35b35132018-12-17 16:10:03 +0100227}
228
Marek Behún1c8eb902022-04-27 12:41:50 +0200229static void sw_multi_write(struct udevice *bus, int sw, int dev, int reg,
Marek Behún35b35132018-12-17 16:10:03 +0100230 u16 val)
231{
Marek Behún1c8eb902022-04-27 12:41:50 +0200232 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 1, val);
233 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 0, SW_SMI_CMD_W(dev, reg));
Marek Behún35b35132018-12-17 16:10:03 +0100234 mdelay(5);
235}
236
Marek Behún1c8eb902022-04-27 12:41:50 +0200237static int sw_scratch_read(struct udevice *bus, int sw, int reg)
Marek Behún35b35132018-12-17 16:10:03 +0100238{
239 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
240 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
241}
242
Marek Behún1c8eb902022-04-27 12:41:50 +0200243static void sw_led_write(struct udevice *bus, int sw, int port, int reg,
Marek Behún35b35132018-12-17 16:10:03 +0100244 u16 val)
245{
246 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
247 | (val & 0x7ff));
248}
249
Marek Behún1c8eb902022-04-27 12:41:50 +0200250static void sw_blink_leds(struct udevice *bus, int peridot, int topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100251{
252 int i, p;
253 struct {
254 int port;
255 u16 val;
256 int wait;
257 } regs[] = {
258 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
259 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
260 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
261 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
262 };
263
264 for (i = 0; i < 12; ++i) {
265 for (p = 0; p < peridot; ++p) {
266 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
267 regs[i].val);
268 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
269 regs[i].val);
270 }
271 if (topaz) {
272 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
273 regs[i].val);
274 }
275
276 if (regs[i].wait)
277 mdelay(75);
278 }
279}
280
Marek Behún1c8eb902022-04-27 12:41:50 +0200281static void check_switch_address(struct udevice *bus, int addr)
Marek Behún35b35132018-12-17 16:10:03 +0100282{
283 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
284 printf("Check of switch MDIO address failed for 0x%02x\n",
285 addr);
286}
287
288static int sfp, pci, topaz, peridot, usb, passpci;
289static int sfp_pos, peridot_pos[3];
290static int module_count;
291
292static int configure_peridots(struct gpio_desc *reset_gpio)
293{
294 int i, ret;
295 u8 dout[MAX_MOX_MODULES];
296
297 memset(dout, 0, MAX_MOX_MODULES);
298
299 /* set addresses of Peridot modules */
300 for (i = 0; i < peridot; ++i)
301 dout[module_count - peridot_pos[i]] = (~i) & 3;
302
303 /*
304 * if there is a SFP module connected to the last Peridot module, set
305 * the P10_SMODE to 1 for the Peridot module
306 */
307 if (sfp)
308 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
309
310 dm_gpio_set_value(reset_gpio, 1);
311 mdelay(10);
312
313 ret = mox_do_spi(NULL, dout, module_count + 1);
314
315 mdelay(10);
316 dm_gpio_set_value(reset_gpio, 0);
317
318 mdelay(50);
319
320 return ret;
321}
322
323static int get_reset_gpio(struct gpio_desc *reset_gpio)
324{
325 int node;
326
327 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
328 if (node < 0) {
329 printf("Cannot find Moxtet bus device node!\n");
330 return -1;
331 }
332
333 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
334 reset_gpio, GPIOD_IS_OUT);
335
336 if (!dm_gpio_is_valid(reset_gpio)) {
337 printf("Cannot find reset GPIO for Moxtet bus!\n");
338 return -1;
339 }
340
Marek Behún35572c92018-12-17 16:10:08 +0100341 return 0;
342}
343
Pali Rohár584256a2022-08-29 15:44:48 +0200344/* Load default system DTB binary to $fdr_addr */
345static void load_spi_dtb(void)
346{
347 const char *const env_name[1] = { "fdt_addr" };
348 unsigned long size, offset;
349 struct udevice *spi_dev;
350 struct spi_flash *flash;
351 const char *addr_str;
352 unsigned long addr;
353 void *buf;
354
355 addr_str = env_get(env_name[0]);
356 if (!addr_str) {
357 env_set_default_vars(1, (char * const *)env_name, 0);
358 addr_str = env_get(env_name[0]);
359 }
360
361 if (!addr_str)
362 return;
363
364 addr = hextoul(addr_str, NULL);
365 if (!addr)
366 return;
367
368 spi_flash_probe_bus_cs(CONFIG_SF_DEFAULT_BUS, CONFIG_SF_DEFAULT_CS, &spi_dev);
369 flash = dev_get_uclass_priv(spi_dev);
370 if (!flash)
371 return;
372
373 /*
374 * SPI NOR "dtb" partition offset & size hardcoded for now because the
375 * mtd subsystem does not offer finding the partition yet and we do not
376 * want to reimplement OF partition parser here.
377 */
378 offset = 0x7f0000;
379 size = 0x10000;
380
381 buf = map_physmem(addr, size, MAP_WRBACK);
382 if (!buf)
383 return;
384
385 spi_flash_read(flash, offset, size, buf);
386 unmap_physmem(buf, size);
387}
388
Marek Behún35572c92018-12-17 16:10:08 +0100389int misc_init_r(void)
390{
Marek Behún98c86c42021-10-09 19:33:42 +0200391 u8 mac[2][6];
392 int i, ret;
Marek Behún35572c92018-12-17 16:10:08 +0100393
Marek Behún98c86c42021-10-09 19:33:42 +0200394 ret = mbox_sp_get_board_info(NULL, mac[0], mac[1], NULL, NULL);
Marek Behún35572c92018-12-17 16:10:08 +0100395 if (ret < 0) {
396 printf("Cannot read data from OTP!\n");
397 return 0;
398 }
399
Marek Behún98c86c42021-10-09 19:33:42 +0200400 for (i = 0; i < 2; ++i) {
401 u8 oldmac[6];
Marek Behún35572c92018-12-17 16:10:08 +0100402
Marek Behún98c86c42021-10-09 19:33:42 +0200403 if (is_valid_ethaddr(mac[i]) &&
404 !eth_env_get_enetaddr_by_index("eth", i, oldmac))
405 eth_env_set_enetaddr_by_index("eth", i, mac[i]);
406 }
Marek Behún35572c92018-12-17 16:10:08 +0100407
Pali Rohár584256a2022-08-29 15:44:48 +0200408 load_spi_dtb();
409
Marek Behún35b35132018-12-17 16:10:03 +0100410 return 0;
411}
412
Marek Behúnd1a30792021-06-07 16:34:48 +0200413static void mox_phy_modify(struct phy_device *phydev, int page, int reg,
414 u16 mask, u16 set)
415{
416 int val;
417
418 val = phydev->drv->readext(phydev, MDIO_DEVAD_NONE, page, reg);
419 val &= ~mask;
420 val |= set;
421 phydev->drv->writeext(phydev, MDIO_DEVAD_NONE, page, reg, val);
422}
423
424static void mox_phy_leds_start_blinking(void)
425{
426 struct phy_device *phydev;
Marek Behún1c8eb902022-04-27 12:41:50 +0200427 ofnode phy_node;
Pali Rohára524db02022-02-14 11:34:26 +0100428
Marek Behún1c8eb902022-04-27 12:41:50 +0200429 phy_node = ofnode_get_phy_node(ofnode_path("ethernet0"));
430 if (!ofnode_valid(phy_node))
431 goto err;
Marek Behúnd1a30792021-06-07 16:34:48 +0200432
Marek Behún1c8eb902022-04-27 12:41:50 +0200433 phydev = dm_phy_find_by_ofnode(phy_node);
434 if (!phydev)
435 goto err;
Marek Behúnd1a30792021-06-07 16:34:48 +0200436
437 mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400);
438 mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb);
Marek Behún1c8eb902022-04-27 12:41:50 +0200439
440 return;
441err:
442 printf("Cannot get ethernet PHY!\n");
Marek Behúnd1a30792021-06-07 16:34:48 +0200443}
444
Marek Behún859ada52021-06-07 16:34:47 +0200445static bool read_reset_button(void)
446{
447 struct udevice *button, *led;
448 int i;
449
Marek Behúnd050eab2021-11-26 14:57:11 +0100450 if (device_get_global_by_ofnode(
451 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
452 "gpio-keys")),
453 &button)) {
Marek Behún859ada52021-06-07 16:34:47 +0200454 printf("Cannot find reset button!\n");
455 return false;
456 }
457
Marek Behúnd050eab2021-11-26 14:57:11 +0100458 if (device_get_global_by_ofnode(
459 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
460 "gpio-leds")),
461 &led)) {
Marek Behún859ada52021-06-07 16:34:47 +0200462 printf("Cannot find status LED!\n");
463 return false;
464 }
465
466 led_set_state(led, LEDST_ON);
467
468 for (i = 0; i < 21; ++i) {
469 if (button_get_state(button) != BUTTON_ON)
470 return false;
471 if (i < 20)
472 mdelay(50);
473 }
474
475 led_set_state(led, LEDST_OFF);
476
477 return true;
478}
479
480static void handle_reset_button(void)
481{
Pali Rohár905c3bf2021-06-14 16:45:58 +0200482 const char * const vars[1] = { "bootcmd_rescue", };
483
484 /*
485 * Ensure that bootcmd_rescue has always stock value, so that running
486 * run bootcmd_rescue
487 * always works correctly.
488 */
489 env_set_default_vars(1, (char * const *)vars, 0);
490
Marek Behún859ada52021-06-07 16:34:47 +0200491 if (read_reset_button()) {
Pali Rohár8adec652022-08-27 20:49:20 +0200492 const char * const vars[3] = {
Marek Behún859ada52021-06-07 16:34:47 +0200493 "bootcmd",
Pali Rohár8adec652022-08-27 20:49:20 +0200494 "bootdelay",
Marek Behún859ada52021-06-07 16:34:47 +0200495 "distro_bootcmd",
496 };
497
498 /*
499 * Set the above envs to their default values, in case the user
500 * managed to break them.
501 */
Pali Rohár8adec652022-08-27 20:49:20 +0200502 env_set_default_vars(3, (char * const *)vars, 0);
Marek Behún859ada52021-06-07 16:34:47 +0200503
504 /* Ensure bootcmd_rescue is used by distroboot */
505 env_set("boot_targets", "rescue");
506
Marek Behúnd1a30792021-06-07 16:34:48 +0200507 /* start blinking PHY LEDs */
508 mox_phy_leds_start_blinking();
509
Marek Behún859ada52021-06-07 16:34:47 +0200510 printf("RESET button was pressed, overwriting boot_targets!\n");
511 } else {
512 /*
513 * In case the user somehow managed to save environment with
514 * boot_targets=rescue, reset boot_targets to default value.
515 * This could happen in subsequent commands if bootcmd_rescue
516 * failed.
517 */
518 if (!strcmp(env_get("boot_targets"), "rescue")) {
519 const char * const vars[1] = {
520 "boot_targets",
521 };
522
523 env_set_default_vars(1, (char * const *)vars, 0);
524 }
525 }
526}
527
Marek Behún84ee2cd2021-10-09 19:33:40 +0200528int show_board_info(void)
Marek Behún35572c92018-12-17 16:10:08 +0100529{
Marek Behún84ee2cd2021-10-09 19:33:40 +0200530 int i, ret, board_version, ram_size, is_sd;
Marek Behún35572c92018-12-17 16:10:08 +0100531 const char *pub_key;
Marek Behún84ee2cd2021-10-09 19:33:40 +0200532 const u8 *topology;
533 u64 serial_number;
534
535 printf("Model: CZ.NIC Turris Mox Board\n");
Marek Behún35572c92018-12-17 16:10:08 +0100536
537 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
538 &ram_size);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200539 if (ret < 0) {
540 printf(" Cannot read board info: %i\n", ret);
541 } else {
542 printf(" Board version: %i\n", board_version);
543 printf(" RAM size: %i MiB\n", ram_size);
544 printf(" Serial Number: %016llX\n", serial_number);
545 }
Marek Behún35572c92018-12-17 16:10:08 +0100546
547 pub_key = mox_sp_get_ecdsa_public_key();
548 if (pub_key)
549 printf(" ECDSA Public Key: %s\n", pub_key);
550 else
Marek Behún84ee2cd2021-10-09 19:33:40 +0200551 printf(" Cannot read ECDSA Public Key\n");
Marek Behún35572c92018-12-17 16:10:08 +0100552
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100553 ret = mox_get_topology(&topology, &module_count, &is_sd);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200554 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100555 printf("Cannot read module topology!\n");
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100556
Marek Behún35572c92018-12-17 16:10:08 +0100557 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
558
559 if (module_count)
560 printf("Module Topology:\n");
561
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100562 for (i = 0; i < module_count; ++i) {
563 switch (topology[i]) {
564 case MOX_MODULE_SFP:
565 printf("% 4i: SFP Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200566 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100567 case MOX_MODULE_PCI:
568 printf("% 4i: Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200569 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100570 case MOX_MODULE_TOPAZ:
571 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
572 break;
573 case MOX_MODULE_PERIDOT:
574 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
575 break;
576 case MOX_MODULE_USB3:
577 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
578 break;
579 case MOX_MODULE_PASSPCI:
580 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200581 break;
582 default:
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100583 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
Marek Behúnf835bed2018-04-24 17:21:31 +0200584 }
585 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200586
Marek Behún84ee2cd2021-10-09 19:33:40 +0200587 /* check if modules are connected in supported mode */
Marek Behún35b35132018-12-17 16:10:03 +0100588 for (i = 0; i < module_count; ++i) {
589 switch (topology[i]) {
590 case MOX_MODULE_SFP:
591 if (sfp) {
592 printf("Error: Only one SFP module is supported!\n");
593 } else if (topaz) {
594 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
595 } else {
596 sfp_pos = i;
597 ++sfp;
598 }
599 break;
600 case MOX_MODULE_PCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200601 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100602 printf("Error: Only one Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200603 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100604 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200605 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100606 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 +0200607 else
Marek Behún35b35132018-12-17 16:10:03 +0100608 ++pci;
Marek Behún35b35132018-12-17 16:10:03 +0100609 break;
610 case MOX_MODULE_TOPAZ:
Marek Behún4529fce2020-04-08 12:02:05 +0200611 if (topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100612 printf("Error: Only one Topaz module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200613 else if (peridot >= 3)
Marek Behún35b35132018-12-17 16:10:03 +0100614 printf("Error: At most two Peridot modules can come before Topaz module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200615 else
Marek Behún35b35132018-12-17 16:10:03 +0100616 ++topaz;
Marek Behún35b35132018-12-17 16:10:03 +0100617 break;
618 case MOX_MODULE_PERIDOT:
619 if (sfp || topaz) {
620 printf("Error: Peridot module must come before SFP or Topaz module!\n");
621 } else if (peridot >= 3) {
622 printf("Error: At most three Peridot modules are supported!\n");
623 } else {
624 peridot_pos[peridot] = i;
625 ++peridot;
626 }
627 break;
628 case MOX_MODULE_USB3:
Marek Behún4529fce2020-04-08 12:02:05 +0200629 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100630 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200631 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100632 printf("Error: Only one USB 3.0 module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200633 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100634 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 +0200635 else
Marek Behún35b35132018-12-17 16:10:03 +0100636 ++usb;
Marek Behún35b35132018-12-17 16:10:03 +0100637 break;
638 case MOX_MODULE_PASSPCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200639 if (passpci)
Marek Behún35b35132018-12-17 16:10:03 +0100640 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200641 else if (i != 0)
Marek Behún35b35132018-12-17 16:10:03 +0100642 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200643 else
Marek Behún35b35132018-12-17 16:10:03 +0100644 ++passpci;
Marek Behún35b35132018-12-17 16:10:03 +0100645 }
646 }
647
Marek Behún84ee2cd2021-10-09 19:33:40 +0200648 return 0;
649}
Marek Behún35b35132018-12-17 16:10:03 +0100650
Marek Behún1c8eb902022-04-27 12:41:50 +0200651static struct udevice *mox_mdio_bus(void)
652{
653 struct udevice *bus;
654 ofnode node;
655
656 node = ofnode_by_compatible(ofnode_null(), "marvell,orion-mdio");
657 if (!ofnode_valid(node))
658 goto err;
659
660 dm_mdio_probe_devices();
661
662 if (uclass_get_device_by_ofnode(UCLASS_MDIO, node, &bus))
663 goto err;
664
665 return bus;
666err:
667 printf("Cannot get MDIO bus device!\n");
668 return NULL;
669}
670
Simon Glass1cedca12023-08-21 21:17:01 -0600671static int last_stage_init(void)
Marek Behún84ee2cd2021-10-09 19:33:40 +0200672{
673 struct gpio_desc reset_gpio = {};
674
675 /* configure modules */
Marek Behún35b35132018-12-17 16:10:03 +0100676 if (get_reset_gpio(&reset_gpio) < 0)
Marek Behún84d67ac2021-10-09 19:33:41 +0200677 goto handle_reset_btn;
Marek Behún35b35132018-12-17 16:10:03 +0100678
679 if (peridot > 0) {
680 if (configure_peridots(&reset_gpio) < 0) {
681 printf("Cannot configure Peridot modules!\n");
682 peridot = 0;
683 }
684 } else {
685 dm_gpio_set_value(&reset_gpio, 1);
686 mdelay(50);
687 dm_gpio_set_value(&reset_gpio, 0);
688 mdelay(50);
689 }
690
Marek Behún84ee2cd2021-10-09 19:33:40 +0200691 /*
692 * check if the addresses are set by reading Scratch & Misc register
693 * 0x70 of Peridot (and potentially Topaz) modules
694 */
Marek Behún35b35132018-12-17 16:10:03 +0100695 if (peridot || topaz) {
Marek Behún1c8eb902022-04-27 12:41:50 +0200696 struct udevice *bus = mox_mdio_bus();
Marek Behún35b35132018-12-17 16:10:03 +0100697
Marek Behún1c8eb902022-04-27 12:41:50 +0200698 if (bus) {
Marek Behún84ee2cd2021-10-09 19:33:40 +0200699 int i;
700
Marek Behún35b35132018-12-17 16:10:03 +0100701 for (i = 0; i < peridot; ++i)
702 check_switch_address(bus, 0x10 + i);
703
704 if (topaz)
705 check_switch_address(bus, 0x2);
706
707 sw_blink_leds(bus, peridot, topaz);
708 }
709 }
710
Marek Behún84d67ac2021-10-09 19:33:41 +0200711handle_reset_btn:
Marek Behún859ada52021-06-07 16:34:47 +0200712 handle_reset_button();
713
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100714 return 0;
Marek Behúnf835bed2018-04-24 17:21:31 +0200715}
Simon Glass1cedca12023-08-21 21:17:01 -0600716EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, last_stage_init);
Marek Behún4529fce2020-04-08 12:02:05 +0200717
718#if defined(CONFIG_OF_BOARD_SETUP)
719
Marek Behúnd050eab2021-11-26 14:57:11 +0100720static bool is_topaz(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200721{
Marek Behúnd050eab2021-11-26 14:57:11 +0100722 return topaz && id == peridot + topaz - 1;
Marek Behún4529fce2020-04-08 12:02:05 +0200723}
724
Marek Behúnd050eab2021-11-26 14:57:11 +0100725static int switch_addr(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200726{
Marek Behúnd050eab2021-11-26 14:57:11 +0100727 return is_topaz(id) ? 0x2 : 0x10 + id;
Marek Behún4529fce2020-04-08 12:02:05 +0200728}
729
Marek Behúnd050eab2021-11-26 14:57:11 +0100730static int setup_switch(void *blob, int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200731{
Marek Behúnd050eab2021-11-26 14:57:11 +0100732 int res, addr, i, node;
733 char mdio_path[64];
Marek Behún4529fce2020-04-08 12:02:05 +0200734
Marek Behúnd050eab2021-11-26 14:57:11 +0100735 node = fdt_node_offset_by_compatible(blob, -1, "marvell,orion-mdio");
Marek Behún4529fce2020-04-08 12:02:05 +0200736 if (node < 0)
737 return node;
738
Marek Behúnd050eab2021-11-26 14:57:11 +0100739 res = fdt_get_path(blob, node, mdio_path, sizeof(mdio_path));
Marek Behún4529fce2020-04-08 12:02:05 +0200740 if (res < 0)
741 return res;
742
Marek Behún4529fce2020-04-08 12:02:05 +0200743 addr = switch_addr(id);
744
745 /* first enable the switch by setting status = "okay" */
Marek Behúnd050eab2021-11-26 14:57:11 +0100746 res = fdt_status_okay_by_pathf(blob, "%s/switch%i@%x", mdio_path, id,
747 addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200748 if (res < 0)
749 return res;
750
751 /*
752 * now if there are more switches or a SFP module coming after,
753 * enable corresponding ports
754 */
755 if (id < peridot + topaz - 1) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100756 res = fdt_status_okay_by_pathf(blob,
757 "%s/switch%i@%x/ports/port@a",
758 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200759 } else if (id == peridot - 1 && !topaz && sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100760 res = fdt_status_okay_by_pathf(blob,
761 "%s/switch%i@%x/ports/port-sfp@a",
762 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200763 } else {
764 res = 0;
765 }
766 if (res < 0)
767 return res;
768
769 if (id >= peridot + topaz - 1)
770 return 0;
771
772 /* finally change link property if needed */
Marek Behúnd050eab2021-11-26 14:57:11 +0100773 node = fdt_node_offset_by_pathf(blob, "%s/switch%i@%x/ports/port@a",
774 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200775 if (node < 0)
776 return node;
777
778 for (i = id + 1; i < peridot + topaz; ++i) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100779 unsigned int phandle;
780
781 phandle = fdt_create_phandle_by_pathf(blob,
782 "%s/switch%i@%x/ports/port@%x",
783 mdio_path, i,
784 switch_addr(i),
785 is_topaz(i) ? 5 : 9);
786 if (!phandle)
787 return -FDT_ERR_NOPHANDLES;
Marek Behún4529fce2020-04-08 12:02:05 +0200788
789 if (i == id + 1)
790 res = fdt_setprop_u32(blob, node, "link", phandle);
791 else
792 res = fdt_appendprop_u32(blob, node, "link", phandle);
793 if (res < 0)
794 return res;
795 }
796
797 return 0;
798}
799
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900800int ft_board_setup(void *blob, struct bd_info *bd)
Marek Behún4529fce2020-04-08 12:02:05 +0200801{
Marek Behúnd050eab2021-11-26 14:57:11 +0100802 int res;
Marek Behún4529fce2020-04-08 12:02:05 +0200803
804 /*
805 * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
806 * connected, enable the PCIe node.
807 */
808 if (pci || usb || passpci) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100809 res = fdt_status_okay_by_compatible(blob,
810 "marvell,armada-3700-pcie");
Marek Behún4529fce2020-04-08 12:02:05 +0200811 if (res < 0)
812 return res;
Marek Behún455ef5b2020-04-08 19:25:22 +0200813
814 /* Fix PCIe regions for devices with 4 GB RAM */
815 res = a3700_fdt_fix_pcie_regions(blob);
816 if (res < 0)
817 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200818 }
819
820 /*
821 * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
822 * enable the eth1 node and setup the switches.
823 */
824 if (peridot || topaz) {
825 int i;
826
Marek Behúnd050eab2021-11-26 14:57:11 +0100827 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200828 if (res < 0)
829 return res;
830
831 for (i = 0; i < peridot + topaz; ++i) {
832 res = setup_switch(blob, i);
833 if (res < 0)
834 return res;
835 }
836 }
837
838 /*
839 * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
840 * node. If there is no Peridot switch between MOX A and MOX D, add link
841 * to the SFP node to eth1 node.
842 * Also enable and configure SFP GPIO controller node.
843 */
844 if (sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100845 int node;
846
847 res = fdt_status_okay_by_compatible(blob, "sff,sfp");
Marek Behún4529fce2020-04-08 12:02:05 +0200848 if (res < 0)
849 return res;
850
Marek Behúnd050eab2021-11-26 14:57:11 +0100851 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200852 if (res < 0)
853 return res;
854
855 if (!peridot) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100856 unsigned int phandle;
Marek Behún4529fce2020-04-08 12:02:05 +0200857
Marek Behúnd050eab2021-11-26 14:57:11 +0100858 phandle = fdt_create_phandle_by_compatible(blob,
859 "sff,sfp");
860 if (!phandle)
861 return -FDT_ERR_NOPHANDLES;
862
863 node = fdt_path_offset(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200864 if (node < 0)
865 return node;
866
867 res = fdt_setprop_u32(blob, node, "sfp", phandle);
868 if (res < 0)
869 return res;
870
871 res = fdt_setprop_string(blob, node, "phy-mode",
872 "sgmii");
873 if (res < 0)
874 return res;
Pali Rohárdb69aef2022-08-10 12:54:11 +0200875
876 res = fdt_setprop_string(blob, node, "label",
877 "sfp");
878 if (res < 0)
879 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200880 }
881
Marek Behúnd050eab2021-11-26 14:57:11 +0100882 res = fdt_status_okay_by_compatible(blob, "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200883 if (res < 0)
884 return res;
885
886 if (sfp_pos) {
887 char newname[16];
888
889 /* moxtet-sfp is on non-zero position, change default */
Marek Behúnd050eab2021-11-26 14:57:11 +0100890 node = fdt_node_offset_by_compatible(blob, -1,
891 "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200892 if (node < 0)
893 return node;
894
895 res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
896 if (res < 0)
897 return res;
898
899 sprintf(newname, "gpio@%x", sfp_pos);
900
901 res = fdt_set_name(blob, node, newname);
902 if (res < 0)
903 return res;
904 }
905 }
906
907 fdt_fixup_ethernet(blob);
908
909 /* Finally remove disabled nodes, as per Rob Herring's request. */
Marek Behúnac4d9ff2021-11-26 14:57:15 +0100910 fdt_delete_disabled_nodes(blob);
Marek Behún4529fce2020-04-08 12:02:05 +0200911
912 return 0;
913}
914
915#endif