blob: a52a032e4d5d1674427e398ddfb1e181e68e1633 [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>
Marek Behún9602a8c2018-08-21 12:22:09 +020018#include <fdt_support.h>
Marek Behún28e935f2020-04-08 12:02:08 +020019#include <init.h>
Marek Behún859ada52021-06-07 16:34:47 +020020#include <led.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Marek Behún28e935f2020-04-08 12:02:08 +020022#include <linux/libfdt.h>
23#include <linux/string.h>
24#include <miiphy.h>
Marek Behún28e935f2020-04-08 12:02:08 +020025#include <spi.h>
Pali Rohár584256a2022-08-29 15:44:48 +020026#include <spi_flash.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020027
Marek Behún35572c92018-12-17 16:10:08 +010028#include "mox_sp.h"
29
Marek Behún9602a8c2018-08-21 12:22:09 +020030#define MAX_MOX_MODULES 10
31
32#define MOX_MODULE_SFP 0x1
33#define MOX_MODULE_PCI 0x2
34#define MOX_MODULE_TOPAZ 0x3
35#define MOX_MODULE_PERIDOT 0x4
36#define MOX_MODULE_USB3 0x5
37#define MOX_MODULE_PASSPCI 0x6
38
Marek Behún1055ce12020-04-08 12:02:07 +020039#define ARMADA_37XX_NB_GPIO_SEL (MVEBU_REGISTER(0x13830))
40#define ARMADA_37XX_SPI_CTRL (MVEBU_REGISTER(0x10600))
41#define ARMADA_37XX_SPI_CFG (MVEBU_REGISTER(0x10604))
42#define ARMADA_37XX_SPI_DOUT (MVEBU_REGISTER(0x10608))
43#define ARMADA_37XX_SPI_DIN (MVEBU_REGISTER(0x1060c))
Marek Behún9602a8c2018-08-21 12:22:09 +020044
Marek Behúnf835bed2018-04-24 17:21:31 +020045DECLARE_GLOBAL_DATA_PTR;
46
Marek Behún9602a8c2018-08-21 12:22:09 +020047#if defined(CONFIG_OF_BOARD_FIXUP)
48int board_fix_fdt(void *blob)
49{
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010050 enum fdt_status status_pcie, status_eth1;
Marek Behún9602a8c2018-08-21 12:22:09 +020051 u8 topology[MAX_MOX_MODULES];
Marek Behúnd050eab2021-11-26 14:57:11 +010052 int i, size, ret;
Pali Rohár951b9822022-01-18 10:33:19 +010053 bool eth1_sgmii;
Marek Behún9602a8c2018-08-21 12:22:09 +020054
55 /*
56 * SPI driver is not loaded in driver model yet, but we have to find out
57 * if pcie should be enabled in U-Boot's device tree. Therefore we have
58 * to read SPI by reading/writing SPI registers directly
59 */
60
Marek Behúna4e697f2020-04-08 12:02:03 +020061 /* put pin from GPIO to SPI mode */
62 clrbits_le32(ARMADA_37XX_NB_GPIO_SEL, BIT(12));
Marek Behún77d93122021-11-26 14:57:14 +010063 /* configure cpol, cpha, prescale */
64 writel(0x10df, ARMADA_37XX_SPI_CFG);
65 mdelay(1);
Marek Behúna4e697f2020-04-08 12:02:03 +020066 /* enable SPI CS1 */
67 setbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +020068
69 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
70 udelay(1);
71
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010072 status_pcie = FDT_STATUS_DISABLED;
73 status_eth1 = FDT_STATUS_DISABLED;
Pali Rohár951b9822022-01-18 10:33:19 +010074 eth1_sgmii = false;
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010075
Marek Behún9602a8c2018-08-21 12:22:09 +020076 for (i = 0; i < MAX_MOX_MODULES; ++i) {
77 writel(0x0, ARMADA_37XX_SPI_DOUT);
78
79 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
80 udelay(1);
81
82 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
83 if (topology[i] == 0xff)
84 break;
85
86 topology[i] &= 0xf;
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010087
Pali Rohár951b9822022-01-18 10:33:19 +010088 if (topology[i] == MOX_MODULE_SFP &&
89 status_pcie == FDT_STATUS_DISABLED)
90 eth1_sgmii = true;
91
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010092 if (topology[i] == MOX_MODULE_SFP ||
93 topology[i] == MOX_MODULE_TOPAZ ||
94 topology[i] == MOX_MODULE_PERIDOT)
95 status_eth1 = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +020096 }
97
98 size = i;
99
Marek Behúna4e697f2020-04-08 12:02:03 +0200100 /* disable SPI CS1 */
101 clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +0200102
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100103 ret = fdt_set_status_by_alias(blob, "ethernet1", status_eth1);
104 if (ret < 0)
105 printf("Cannot set status for eth1 in U-Boot's device tree: %s!\n",
106 fdt_strerror(ret));
107
Pali Rohár951b9822022-01-18 10:33:19 +0100108 if (eth1_sgmii) {
109 ret = fdt_path_offset(blob, "ethernet1");
110 if (ret >= 0)
111 ret = fdt_setprop_string(blob, ret, "phy-mode", "sgmii");
112 if (ret < 0)
113 printf("Cannot set phy-mode for eth1 to sgmii in U-Boot device tree: %s!\n",
114 fdt_strerror(ret));
115 }
116
Marek Behún9602a8c2018-08-21 12:22:09 +0200117 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
118 topology[1] == MOX_MODULE_USB3 ||
119 topology[1] == MOX_MODULE_PASSPCI))
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100120 status_pcie = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +0200121
Marek Behúnd050eab2021-11-26 14:57:11 +0100122 ret = fdt_set_status_by_compatible(blob, "marvell,armada-3700-pcie",
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100123 status_pcie);
Marek Behúnd050eab2021-11-26 14:57:11 +0100124 if (ret < 0) {
125 printf("Cannot set status for PCIe in U-Boot's device tree: %s!\n",
126 fdt_strerror(ret));
Marek Behún9602a8c2018-08-21 12:22:09 +0200127 return 0;
128 }
129
Marek Behún455ef5b2020-04-08 19:25:22 +0200130 if (a3700_fdt_fix_pcie_regions(blob) < 0) {
131 printf("Cannot fix PCIe regions in U-Boot's device tree!\n");
132 return 0;
133 }
134
Marek Behún9602a8c2018-08-21 12:22:09 +0200135 return 0;
136}
137#endif
138
Marek Behúnf835bed2018-04-24 17:21:31 +0200139int board_init(void)
140{
141 /* address of boot parameters */
Tom Rinibb4dd962022-11-16 13:10:37 -0500142 gd->bd->bi_boot_params = CFG_SYS_SDRAM_BASE + 0x100;
Marek Behúnf835bed2018-04-24 17:21:31 +0200143
Marek Behúnf835bed2018-04-24 17:21:31 +0200144 return 0;
145}
146
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100147static int mox_do_spi(u8 *in, u8 *out, size_t size)
Marek Behúnf835bed2018-04-24 17:21:31 +0200148{
149 struct spi_slave *slave;
150 struct udevice *dev;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100151 int ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200152
Patrice Chotard86e06ae2022-03-30 09:33:13 +0200153 ret = _spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
154 "spi_generic_drv", "moxtet@1", &dev,
155 &slave);
Marek Behúnf835bed2018-04-24 17:21:31 +0200156 if (ret)
157 goto fail;
158
159 ret = spi_claim_bus(slave);
160 if (ret)
161 goto fail_free;
162
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100163 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
164
165 spi_release_bus(slave);
166fail_free:
167 spi_free_slave(slave);
168fail:
169 return ret;
170}
171
172static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
173{
174 static int is_sd;
175 static u8 topology[MAX_MOX_MODULES - 1];
176 static int size;
177 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
178 int ret, i;
179
180 if (size) {
181 if (ptopology)
182 *ptopology = topology;
183 if (psize)
184 *psize = size;
185 if (pis_sd)
186 *pis_sd = is_sd;
187 return 0;
188 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200189
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100190 memset(din, 0, MAX_MOX_MODULES);
191 memset(dout, 0, MAX_MOX_MODULES);
192
193 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
Marek Behúnf835bed2018-04-24 17:21:31 +0200194 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100195 return ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200196
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100197 if (din[0] == 0x10)
198 is_sd = 1;
199 else if (din[0] == 0x00)
200 is_sd = 0;
201 else
202 return -ENODEV;
Marek Behúnf835bed2018-04-24 17:21:31 +0200203
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100204 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
205 topology[i - 1] = din[i] & 0xf;
206 size = i - 1;
207
208 if (ptopology)
209 *ptopology = topology;
210 if (psize)
211 *psize = size;
212 if (pis_sd)
213 *pis_sd = is_sd;
214
215 return 0;
216}
217
Marek Behún35b35132018-12-17 16:10:03 +0100218#define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
219#define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
220
Marek Behún1c8eb902022-04-27 12:41:50 +0200221static int sw_multi_read(struct udevice *bus, int sw, int dev, int reg)
Marek Behún35b35132018-12-17 16:10:03 +0100222{
Marek Behún1c8eb902022-04-27 12:41:50 +0200223 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 0, SW_SMI_CMD_R(dev, reg));
Marek Behún35b35132018-12-17 16:10:03 +0100224 mdelay(5);
Marek Behún1c8eb902022-04-27 12:41:50 +0200225 return dm_mdio_read(bus, sw, MDIO_DEVAD_NONE, 1);
Marek Behún35b35132018-12-17 16:10:03 +0100226}
227
Marek Behún1c8eb902022-04-27 12:41:50 +0200228static void sw_multi_write(struct udevice *bus, int sw, int dev, int reg,
Marek Behún35b35132018-12-17 16:10:03 +0100229 u16 val)
230{
Marek Behún1c8eb902022-04-27 12:41:50 +0200231 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 1, val);
232 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 0, SW_SMI_CMD_W(dev, reg));
Marek Behún35b35132018-12-17 16:10:03 +0100233 mdelay(5);
234}
235
Marek Behún1c8eb902022-04-27 12:41:50 +0200236static int sw_scratch_read(struct udevice *bus, int sw, int reg)
Marek Behún35b35132018-12-17 16:10:03 +0100237{
238 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
239 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
240}
241
Marek Behún1c8eb902022-04-27 12:41:50 +0200242static void sw_led_write(struct udevice *bus, int sw, int port, int reg,
Marek Behún35b35132018-12-17 16:10:03 +0100243 u16 val)
244{
245 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
246 | (val & 0x7ff));
247}
248
Marek Behún1c8eb902022-04-27 12:41:50 +0200249static void sw_blink_leds(struct udevice *bus, int peridot, int topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100250{
251 int i, p;
252 struct {
253 int port;
254 u16 val;
255 int wait;
256 } regs[] = {
257 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
258 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
259 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
260 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
261 };
262
263 for (i = 0; i < 12; ++i) {
264 for (p = 0; p < peridot; ++p) {
265 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
266 regs[i].val);
267 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
268 regs[i].val);
269 }
270 if (topaz) {
271 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
272 regs[i].val);
273 }
274
275 if (regs[i].wait)
276 mdelay(75);
277 }
278}
279
Marek Behún1c8eb902022-04-27 12:41:50 +0200280static void check_switch_address(struct udevice *bus, int addr)
Marek Behún35b35132018-12-17 16:10:03 +0100281{
282 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
283 printf("Check of switch MDIO address failed for 0x%02x\n",
284 addr);
285}
286
287static int sfp, pci, topaz, peridot, usb, passpci;
288static int sfp_pos, peridot_pos[3];
289static int module_count;
290
291static int configure_peridots(struct gpio_desc *reset_gpio)
292{
293 int i, ret;
294 u8 dout[MAX_MOX_MODULES];
295
296 memset(dout, 0, MAX_MOX_MODULES);
297
298 /* set addresses of Peridot modules */
299 for (i = 0; i < peridot; ++i)
300 dout[module_count - peridot_pos[i]] = (~i) & 3;
301
302 /*
303 * if there is a SFP module connected to the last Peridot module, set
304 * the P10_SMODE to 1 for the Peridot module
305 */
306 if (sfp)
307 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
308
309 dm_gpio_set_value(reset_gpio, 1);
310 mdelay(10);
311
312 ret = mox_do_spi(NULL, dout, module_count + 1);
313
314 mdelay(10);
315 dm_gpio_set_value(reset_gpio, 0);
316
317 mdelay(50);
318
319 return ret;
320}
321
322static int get_reset_gpio(struct gpio_desc *reset_gpio)
323{
324 int node;
325
326 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
327 if (node < 0) {
328 printf("Cannot find Moxtet bus device node!\n");
329 return -1;
330 }
331
332 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
333 reset_gpio, GPIOD_IS_OUT);
334
335 if (!dm_gpio_is_valid(reset_gpio)) {
336 printf("Cannot find reset GPIO for Moxtet bus!\n");
337 return -1;
338 }
339
Marek Behún35572c92018-12-17 16:10:08 +0100340 return 0;
341}
342
Pali Rohár584256a2022-08-29 15:44:48 +0200343/* Load default system DTB binary to $fdr_addr */
344static void load_spi_dtb(void)
345{
346 const char *const env_name[1] = { "fdt_addr" };
347 unsigned long size, offset;
348 struct udevice *spi_dev;
349 struct spi_flash *flash;
350 const char *addr_str;
351 unsigned long addr;
352 void *buf;
353
354 addr_str = env_get(env_name[0]);
355 if (!addr_str) {
356 env_set_default_vars(1, (char * const *)env_name, 0);
357 addr_str = env_get(env_name[0]);
358 }
359
360 if (!addr_str)
361 return;
362
363 addr = hextoul(addr_str, NULL);
364 if (!addr)
365 return;
366
367 spi_flash_probe_bus_cs(CONFIG_SF_DEFAULT_BUS, CONFIG_SF_DEFAULT_CS, &spi_dev);
368 flash = dev_get_uclass_priv(spi_dev);
369 if (!flash)
370 return;
371
372 /*
373 * SPI NOR "dtb" partition offset & size hardcoded for now because the
374 * mtd subsystem does not offer finding the partition yet and we do not
375 * want to reimplement OF partition parser here.
376 */
377 offset = 0x7f0000;
378 size = 0x10000;
379
380 buf = map_physmem(addr, size, MAP_WRBACK);
381 if (!buf)
382 return;
383
384 spi_flash_read(flash, offset, size, buf);
385 unmap_physmem(buf, size);
386}
387
Marek Behún35572c92018-12-17 16:10:08 +0100388int misc_init_r(void)
389{
Marek Behún98c86c42021-10-09 19:33:42 +0200390 u8 mac[2][6];
391 int i, ret;
Marek Behún35572c92018-12-17 16:10:08 +0100392
Marek Behún98c86c42021-10-09 19:33:42 +0200393 ret = mbox_sp_get_board_info(NULL, mac[0], mac[1], NULL, NULL);
Marek Behún35572c92018-12-17 16:10:08 +0100394 if (ret < 0) {
395 printf("Cannot read data from OTP!\n");
396 return 0;
397 }
398
Marek Behún98c86c42021-10-09 19:33:42 +0200399 for (i = 0; i < 2; ++i) {
400 u8 oldmac[6];
Marek Behún35572c92018-12-17 16:10:08 +0100401
Marek Behún98c86c42021-10-09 19:33:42 +0200402 if (is_valid_ethaddr(mac[i]) &&
403 !eth_env_get_enetaddr_by_index("eth", i, oldmac))
404 eth_env_set_enetaddr_by_index("eth", i, mac[i]);
405 }
Marek Behún35572c92018-12-17 16:10:08 +0100406
Pali Rohár584256a2022-08-29 15:44:48 +0200407 load_spi_dtb();
408
Marek Behún35b35132018-12-17 16:10:03 +0100409 return 0;
410}
411
Marek Behúnd1a30792021-06-07 16:34:48 +0200412static void mox_phy_modify(struct phy_device *phydev, int page, int reg,
413 u16 mask, u16 set)
414{
415 int val;
416
417 val = phydev->drv->readext(phydev, MDIO_DEVAD_NONE, page, reg);
418 val &= ~mask;
419 val |= set;
420 phydev->drv->writeext(phydev, MDIO_DEVAD_NONE, page, reg, val);
421}
422
423static void mox_phy_leds_start_blinking(void)
424{
425 struct phy_device *phydev;
Marek Behún1c8eb902022-04-27 12:41:50 +0200426 ofnode phy_node;
Pali Rohára524db02022-02-14 11:34:26 +0100427
Marek Behún1c8eb902022-04-27 12:41:50 +0200428 phy_node = ofnode_get_phy_node(ofnode_path("ethernet0"));
429 if (!ofnode_valid(phy_node))
430 goto err;
Marek Behúnd1a30792021-06-07 16:34:48 +0200431
Marek Behún1c8eb902022-04-27 12:41:50 +0200432 phydev = dm_phy_find_by_ofnode(phy_node);
433 if (!phydev)
434 goto err;
Marek Behúnd1a30792021-06-07 16:34:48 +0200435
436 mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400);
437 mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb);
Marek Behún1c8eb902022-04-27 12:41:50 +0200438
439 return;
440err:
441 printf("Cannot get ethernet PHY!\n");
Marek Behúnd1a30792021-06-07 16:34:48 +0200442}
443
Marek Behún859ada52021-06-07 16:34:47 +0200444static bool read_reset_button(void)
445{
446 struct udevice *button, *led;
447 int i;
448
Marek Behúnd050eab2021-11-26 14:57:11 +0100449 if (device_get_global_by_ofnode(
450 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
451 "gpio-keys")),
452 &button)) {
Marek Behún859ada52021-06-07 16:34:47 +0200453 printf("Cannot find reset button!\n");
454 return false;
455 }
456
Marek Behúnd050eab2021-11-26 14:57:11 +0100457 if (device_get_global_by_ofnode(
458 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
459 "gpio-leds")),
460 &led)) {
Marek Behún859ada52021-06-07 16:34:47 +0200461 printf("Cannot find status LED!\n");
462 return false;
463 }
464
465 led_set_state(led, LEDST_ON);
466
467 for (i = 0; i < 21; ++i) {
468 if (button_get_state(button) != BUTTON_ON)
469 return false;
470 if (i < 20)
471 mdelay(50);
472 }
473
474 led_set_state(led, LEDST_OFF);
475
476 return true;
477}
478
479static void handle_reset_button(void)
480{
Pali Rohár905c3bf2021-06-14 16:45:58 +0200481 const char * const vars[1] = { "bootcmd_rescue", };
482
483 /*
484 * Ensure that bootcmd_rescue has always stock value, so that running
485 * run bootcmd_rescue
486 * always works correctly.
487 */
488 env_set_default_vars(1, (char * const *)vars, 0);
489
Marek Behún859ada52021-06-07 16:34:47 +0200490 if (read_reset_button()) {
Pali Rohár8adec652022-08-27 20:49:20 +0200491 const char * const vars[3] = {
Marek Behún859ada52021-06-07 16:34:47 +0200492 "bootcmd",
Pali Rohár8adec652022-08-27 20:49:20 +0200493 "bootdelay",
Marek Behún859ada52021-06-07 16:34:47 +0200494 "distro_bootcmd",
495 };
496
497 /*
498 * Set the above envs to their default values, in case the user
499 * managed to break them.
500 */
Pali Rohár8adec652022-08-27 20:49:20 +0200501 env_set_default_vars(3, (char * const *)vars, 0);
Marek Behún859ada52021-06-07 16:34:47 +0200502
503 /* Ensure bootcmd_rescue is used by distroboot */
504 env_set("boot_targets", "rescue");
505
Marek Behúnd1a30792021-06-07 16:34:48 +0200506 /* start blinking PHY LEDs */
507 mox_phy_leds_start_blinking();
508
Marek Behún859ada52021-06-07 16:34:47 +0200509 printf("RESET button was pressed, overwriting boot_targets!\n");
510 } else {
511 /*
512 * In case the user somehow managed to save environment with
513 * boot_targets=rescue, reset boot_targets to default value.
514 * This could happen in subsequent commands if bootcmd_rescue
515 * failed.
516 */
517 if (!strcmp(env_get("boot_targets"), "rescue")) {
518 const char * const vars[1] = {
519 "boot_targets",
520 };
521
522 env_set_default_vars(1, (char * const *)vars, 0);
523 }
524 }
525}
526
Marek Behún84ee2cd2021-10-09 19:33:40 +0200527int show_board_info(void)
Marek Behún35572c92018-12-17 16:10:08 +0100528{
Marek Behún84ee2cd2021-10-09 19:33:40 +0200529 int i, ret, board_version, ram_size, is_sd;
Marek Behún35572c92018-12-17 16:10:08 +0100530 const char *pub_key;
Marek Behún84ee2cd2021-10-09 19:33:40 +0200531 const u8 *topology;
532 u64 serial_number;
533
534 printf("Model: CZ.NIC Turris Mox Board\n");
Marek Behún35572c92018-12-17 16:10:08 +0100535
536 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
537 &ram_size);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200538 if (ret < 0) {
539 printf(" Cannot read board info: %i\n", ret);
540 } else {
541 printf(" Board version: %i\n", board_version);
542 printf(" RAM size: %i MiB\n", ram_size);
543 printf(" Serial Number: %016llX\n", serial_number);
544 }
Marek Behún35572c92018-12-17 16:10:08 +0100545
546 pub_key = mox_sp_get_ecdsa_public_key();
547 if (pub_key)
548 printf(" ECDSA Public Key: %s\n", pub_key);
549 else
Marek Behún84ee2cd2021-10-09 19:33:40 +0200550 printf(" Cannot read ECDSA Public Key\n");
Marek Behún35572c92018-12-17 16:10:08 +0100551
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100552 ret = mox_get_topology(&topology, &module_count, &is_sd);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200553 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100554 printf("Cannot read module topology!\n");
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100555
Marek Behún35572c92018-12-17 16:10:08 +0100556 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
557
558 if (module_count)
559 printf("Module Topology:\n");
560
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100561 for (i = 0; i < module_count; ++i) {
562 switch (topology[i]) {
563 case MOX_MODULE_SFP:
564 printf("% 4i: SFP Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200565 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100566 case MOX_MODULE_PCI:
567 printf("% 4i: Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200568 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100569 case MOX_MODULE_TOPAZ:
570 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
571 break;
572 case MOX_MODULE_PERIDOT:
573 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
574 break;
575 case MOX_MODULE_USB3:
576 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
577 break;
578 case MOX_MODULE_PASSPCI:
579 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200580 break;
581 default:
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100582 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
Marek Behúnf835bed2018-04-24 17:21:31 +0200583 }
584 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200585
Marek Behún84ee2cd2021-10-09 19:33:40 +0200586 /* check if modules are connected in supported mode */
Marek Behún35b35132018-12-17 16:10:03 +0100587 for (i = 0; i < module_count; ++i) {
588 switch (topology[i]) {
589 case MOX_MODULE_SFP:
590 if (sfp) {
591 printf("Error: Only one SFP module is supported!\n");
592 } else if (topaz) {
593 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
594 } else {
595 sfp_pos = i;
596 ++sfp;
597 }
598 break;
599 case MOX_MODULE_PCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200600 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100601 printf("Error: Only one Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200602 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100603 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200604 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100605 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 +0200606 else
Marek Behún35b35132018-12-17 16:10:03 +0100607 ++pci;
Marek Behún35b35132018-12-17 16:10:03 +0100608 break;
609 case MOX_MODULE_TOPAZ:
Marek Behún4529fce2020-04-08 12:02:05 +0200610 if (topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100611 printf("Error: Only one Topaz module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200612 else if (peridot >= 3)
Marek Behún35b35132018-12-17 16:10:03 +0100613 printf("Error: At most two Peridot modules can come before Topaz module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200614 else
Marek Behún35b35132018-12-17 16:10:03 +0100615 ++topaz;
Marek Behún35b35132018-12-17 16:10:03 +0100616 break;
617 case MOX_MODULE_PERIDOT:
618 if (sfp || topaz) {
619 printf("Error: Peridot module must come before SFP or Topaz module!\n");
620 } else if (peridot >= 3) {
621 printf("Error: At most three Peridot modules are supported!\n");
622 } else {
623 peridot_pos[peridot] = i;
624 ++peridot;
625 }
626 break;
627 case MOX_MODULE_USB3:
Marek Behún4529fce2020-04-08 12:02:05 +0200628 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100629 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200630 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100631 printf("Error: Only one USB 3.0 module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200632 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100633 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 +0200634 else
Marek Behún35b35132018-12-17 16:10:03 +0100635 ++usb;
Marek Behún35b35132018-12-17 16:10:03 +0100636 break;
637 case MOX_MODULE_PASSPCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200638 if (passpci)
Marek Behún35b35132018-12-17 16:10:03 +0100639 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200640 else if (i != 0)
Marek Behún35b35132018-12-17 16:10:03 +0100641 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200642 else
Marek Behún35b35132018-12-17 16:10:03 +0100643 ++passpci;
Marek Behún35b35132018-12-17 16:10:03 +0100644 }
645 }
646
Marek Behún84ee2cd2021-10-09 19:33:40 +0200647 return 0;
648}
Marek Behún35b35132018-12-17 16:10:03 +0100649
Marek Behún1c8eb902022-04-27 12:41:50 +0200650static struct udevice *mox_mdio_bus(void)
651{
652 struct udevice *bus;
653 ofnode node;
654
655 node = ofnode_by_compatible(ofnode_null(), "marvell,orion-mdio");
656 if (!ofnode_valid(node))
657 goto err;
658
659 dm_mdio_probe_devices();
660
661 if (uclass_get_device_by_ofnode(UCLASS_MDIO, node, &bus))
662 goto err;
663
664 return bus;
665err:
666 printf("Cannot get MDIO bus device!\n");
667 return NULL;
668}
669
Marek Behún84ee2cd2021-10-09 19:33:40 +0200670int last_stage_init(void)
671{
672 struct gpio_desc reset_gpio = {};
673
674 /* configure modules */
Marek Behún35b35132018-12-17 16:10:03 +0100675 if (get_reset_gpio(&reset_gpio) < 0)
Marek Behún84d67ac2021-10-09 19:33:41 +0200676 goto handle_reset_btn;
Marek Behún35b35132018-12-17 16:10:03 +0100677
678 if (peridot > 0) {
679 if (configure_peridots(&reset_gpio) < 0) {
680 printf("Cannot configure Peridot modules!\n");
681 peridot = 0;
682 }
683 } else {
684 dm_gpio_set_value(&reset_gpio, 1);
685 mdelay(50);
686 dm_gpio_set_value(&reset_gpio, 0);
687 mdelay(50);
688 }
689
Marek Behún84ee2cd2021-10-09 19:33:40 +0200690 /*
691 * check if the addresses are set by reading Scratch & Misc register
692 * 0x70 of Peridot (and potentially Topaz) modules
693 */
Marek Behún35b35132018-12-17 16:10:03 +0100694 if (peridot || topaz) {
Marek Behún1c8eb902022-04-27 12:41:50 +0200695 struct udevice *bus = mox_mdio_bus();
Marek Behún35b35132018-12-17 16:10:03 +0100696
Marek Behún1c8eb902022-04-27 12:41:50 +0200697 if (bus) {
Marek Behún84ee2cd2021-10-09 19:33:40 +0200698 int i;
699
Marek Behún35b35132018-12-17 16:10:03 +0100700 for (i = 0; i < peridot; ++i)
701 check_switch_address(bus, 0x10 + i);
702
703 if (topaz)
704 check_switch_address(bus, 0x2);
705
706 sw_blink_leds(bus, peridot, topaz);
707 }
708 }
709
Marek Behún84d67ac2021-10-09 19:33:41 +0200710handle_reset_btn:
Marek Behún859ada52021-06-07 16:34:47 +0200711 handle_reset_button();
712
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100713 return 0;
Marek Behúnf835bed2018-04-24 17:21:31 +0200714}
Marek Behún4529fce2020-04-08 12:02:05 +0200715
716#if defined(CONFIG_OF_BOARD_SETUP)
717
Marek Behúnd050eab2021-11-26 14:57:11 +0100718static bool is_topaz(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200719{
Marek Behúnd050eab2021-11-26 14:57:11 +0100720 return topaz && id == peridot + topaz - 1;
Marek Behún4529fce2020-04-08 12:02:05 +0200721}
722
Marek Behúnd050eab2021-11-26 14:57:11 +0100723static int switch_addr(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200724{
Marek Behúnd050eab2021-11-26 14:57:11 +0100725 return is_topaz(id) ? 0x2 : 0x10 + id;
Marek Behún4529fce2020-04-08 12:02:05 +0200726}
727
Marek Behúnd050eab2021-11-26 14:57:11 +0100728static int setup_switch(void *blob, int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200729{
Marek Behúnd050eab2021-11-26 14:57:11 +0100730 int res, addr, i, node;
731 char mdio_path[64];
Marek Behún4529fce2020-04-08 12:02:05 +0200732
Marek Behúnd050eab2021-11-26 14:57:11 +0100733 node = fdt_node_offset_by_compatible(blob, -1, "marvell,orion-mdio");
Marek Behún4529fce2020-04-08 12:02:05 +0200734 if (node < 0)
735 return node;
736
Marek Behúnd050eab2021-11-26 14:57:11 +0100737 res = fdt_get_path(blob, node, mdio_path, sizeof(mdio_path));
Marek Behún4529fce2020-04-08 12:02:05 +0200738 if (res < 0)
739 return res;
740
Marek Behún4529fce2020-04-08 12:02:05 +0200741 addr = switch_addr(id);
742
743 /* first enable the switch by setting status = "okay" */
Marek Behúnd050eab2021-11-26 14:57:11 +0100744 res = fdt_status_okay_by_pathf(blob, "%s/switch%i@%x", mdio_path, id,
745 addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200746 if (res < 0)
747 return res;
748
749 /*
750 * now if there are more switches or a SFP module coming after,
751 * enable corresponding ports
752 */
753 if (id < peridot + topaz - 1) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100754 res = fdt_status_okay_by_pathf(blob,
755 "%s/switch%i@%x/ports/port@a",
756 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200757 } else if (id == peridot - 1 && !topaz && sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100758 res = fdt_status_okay_by_pathf(blob,
759 "%s/switch%i@%x/ports/port-sfp@a",
760 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200761 } else {
762 res = 0;
763 }
764 if (res < 0)
765 return res;
766
767 if (id >= peridot + topaz - 1)
768 return 0;
769
770 /* finally change link property if needed */
Marek Behúnd050eab2021-11-26 14:57:11 +0100771 node = fdt_node_offset_by_pathf(blob, "%s/switch%i@%x/ports/port@a",
772 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200773 if (node < 0)
774 return node;
775
776 for (i = id + 1; i < peridot + topaz; ++i) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100777 unsigned int phandle;
778
779 phandle = fdt_create_phandle_by_pathf(blob,
780 "%s/switch%i@%x/ports/port@%x",
781 mdio_path, i,
782 switch_addr(i),
783 is_topaz(i) ? 5 : 9);
784 if (!phandle)
785 return -FDT_ERR_NOPHANDLES;
Marek Behún4529fce2020-04-08 12:02:05 +0200786
787 if (i == id + 1)
788 res = fdt_setprop_u32(blob, node, "link", phandle);
789 else
790 res = fdt_appendprop_u32(blob, node, "link", phandle);
791 if (res < 0)
792 return res;
793 }
794
795 return 0;
796}
797
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900798int ft_board_setup(void *blob, struct bd_info *bd)
Marek Behún4529fce2020-04-08 12:02:05 +0200799{
Marek Behúnd050eab2021-11-26 14:57:11 +0100800 int res;
Marek Behún4529fce2020-04-08 12:02:05 +0200801
802 /*
803 * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
804 * connected, enable the PCIe node.
805 */
806 if (pci || usb || passpci) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100807 res = fdt_status_okay_by_compatible(blob,
808 "marvell,armada-3700-pcie");
Marek Behún4529fce2020-04-08 12:02:05 +0200809 if (res < 0)
810 return res;
Marek Behún455ef5b2020-04-08 19:25:22 +0200811
812 /* Fix PCIe regions for devices with 4 GB RAM */
813 res = a3700_fdt_fix_pcie_regions(blob);
814 if (res < 0)
815 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200816 }
817
818 /*
819 * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
820 * enable the eth1 node and setup the switches.
821 */
822 if (peridot || topaz) {
823 int i;
824
Marek Behúnd050eab2021-11-26 14:57:11 +0100825 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200826 if (res < 0)
827 return res;
828
829 for (i = 0; i < peridot + topaz; ++i) {
830 res = setup_switch(blob, i);
831 if (res < 0)
832 return res;
833 }
834 }
835
836 /*
837 * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
838 * node. If there is no Peridot switch between MOX A and MOX D, add link
839 * to the SFP node to eth1 node.
840 * Also enable and configure SFP GPIO controller node.
841 */
842 if (sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100843 int node;
844
845 res = fdt_status_okay_by_compatible(blob, "sff,sfp");
Marek Behún4529fce2020-04-08 12:02:05 +0200846 if (res < 0)
847 return res;
848
Marek Behúnd050eab2021-11-26 14:57:11 +0100849 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200850 if (res < 0)
851 return res;
852
853 if (!peridot) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100854 unsigned int phandle;
Marek Behún4529fce2020-04-08 12:02:05 +0200855
Marek Behúnd050eab2021-11-26 14:57:11 +0100856 phandle = fdt_create_phandle_by_compatible(blob,
857 "sff,sfp");
858 if (!phandle)
859 return -FDT_ERR_NOPHANDLES;
860
861 node = fdt_path_offset(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200862 if (node < 0)
863 return node;
864
865 res = fdt_setprop_u32(blob, node, "sfp", phandle);
866 if (res < 0)
867 return res;
868
869 res = fdt_setprop_string(blob, node, "phy-mode",
870 "sgmii");
871 if (res < 0)
872 return res;
Pali Rohárdb69aef2022-08-10 12:54:11 +0200873
874 res = fdt_setprop_string(blob, node, "label",
875 "sfp");
876 if (res < 0)
877 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200878 }
879
Marek Behúnd050eab2021-11-26 14:57:11 +0100880 res = fdt_status_okay_by_compatible(blob, "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200881 if (res < 0)
882 return res;
883
884 if (sfp_pos) {
885 char newname[16];
886
887 /* moxtet-sfp is on non-zero position, change default */
Marek Behúnd050eab2021-11-26 14:57:11 +0100888 node = fdt_node_offset_by_compatible(blob, -1,
889 "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200890 if (node < 0)
891 return node;
892
893 res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
894 if (res < 0)
895 return res;
896
897 sprintf(newname, "gpio@%x", sfp_pos);
898
899 res = fdt_set_name(blob, node, newname);
900 if (res < 0)
901 return res;
902 }
903 }
904
905 fdt_fixup_ethernet(blob);
906
907 /* Finally remove disabled nodes, as per Rob Herring's request. */
Marek Behúnac4d9ff2021-11-26 14:57:15 +0100908 fdt_delete_disabled_nodes(blob);
Marek Behún4529fce2020-04-08 12:02:05 +0200909
910 return 0;
911}
912
913#endif