blob: 28259e71405f64baa0e8d27aaaf4aae0dada1844 [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>
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;
Pali Rohár951b9822022-01-18 10:33:19 +010052 bool eth1_sgmii;
Marek Behún9602a8c2018-08-21 12:22:09 +020053
54 /*
55 * SPI driver is not loaded in driver model yet, but we have to find out
56 * if pcie should be enabled in U-Boot's device tree. Therefore we have
57 * to read SPI by reading/writing SPI registers directly
58 */
59
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));
Marek Behún77d93122021-11-26 14:57:14 +010062 /* configure cpol, cpha, prescale */
63 writel(0x10df, ARMADA_37XX_SPI_CFG);
64 mdelay(1);
Marek Behúna4e697f2020-04-08 12:02:03 +020065 /* enable SPI CS1 */
66 setbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +020067
68 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
69 udelay(1);
70
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010071 status_pcie = FDT_STATUS_DISABLED;
72 status_eth1 = FDT_STATUS_DISABLED;
Pali Rohár951b9822022-01-18 10:33:19 +010073 eth1_sgmii = false;
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010074
Marek Behún9602a8c2018-08-21 12:22:09 +020075 for (i = 0; i < MAX_MOX_MODULES; ++i) {
76 writel(0x0, ARMADA_37XX_SPI_DOUT);
77
78 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
79 udelay(1);
80
81 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
82 if (topology[i] == 0xff)
83 break;
84
85 topology[i] &= 0xf;
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010086
Pali Rohár951b9822022-01-18 10:33:19 +010087 if (topology[i] == MOX_MODULE_SFP &&
88 status_pcie == FDT_STATUS_DISABLED)
89 eth1_sgmii = true;
90
Pali Rohárcbaaf5e2021-11-26 14:57:12 +010091 if (topology[i] == MOX_MODULE_SFP ||
92 topology[i] == MOX_MODULE_TOPAZ ||
93 topology[i] == MOX_MODULE_PERIDOT)
94 status_eth1 = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +020095 }
96
97 size = i;
98
Marek Behúna4e697f2020-04-08 12:02:03 +020099 /* disable SPI CS1 */
100 clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
Marek Behún9602a8c2018-08-21 12:22:09 +0200101
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100102 ret = fdt_set_status_by_alias(blob, "ethernet1", status_eth1);
103 if (ret < 0)
104 printf("Cannot set status for eth1 in U-Boot's device tree: %s!\n",
105 fdt_strerror(ret));
106
Pali Rohár951b9822022-01-18 10:33:19 +0100107 if (eth1_sgmii) {
108 ret = fdt_path_offset(blob, "ethernet1");
109 if (ret >= 0)
110 ret = fdt_setprop_string(blob, ret, "phy-mode", "sgmii");
111 if (ret < 0)
112 printf("Cannot set phy-mode for eth1 to sgmii in U-Boot device tree: %s!\n",
113 fdt_strerror(ret));
114 }
115
Marek Behún9602a8c2018-08-21 12:22:09 +0200116 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
117 topology[1] == MOX_MODULE_USB3 ||
118 topology[1] == MOX_MODULE_PASSPCI))
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100119 status_pcie = FDT_STATUS_OKAY;
Marek Behún9602a8c2018-08-21 12:22:09 +0200120
Marek Behúnd050eab2021-11-26 14:57:11 +0100121 ret = fdt_set_status_by_compatible(blob, "marvell,armada-3700-pcie",
Pali Rohárcbaaf5e2021-11-26 14:57:12 +0100122 status_pcie);
Marek Behúnd050eab2021-11-26 14:57:11 +0100123 if (ret < 0) {
124 printf("Cannot set status for PCIe in U-Boot's device tree: %s!\n",
125 fdt_strerror(ret));
Marek Behún9602a8c2018-08-21 12:22:09 +0200126 return 0;
127 }
128
Marek Behún455ef5b2020-04-08 19:25:22 +0200129 if (a3700_fdt_fix_pcie_regions(blob) < 0) {
130 printf("Cannot fix PCIe regions in U-Boot's device tree!\n");
131 return 0;
132 }
133
Marek Behún9602a8c2018-08-21 12:22:09 +0200134 return 0;
135}
136#endif
137
Marek Behúnf835bed2018-04-24 17:21:31 +0200138int board_init(void)
139{
140 /* address of boot parameters */
141 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
142
Marek Behúnf835bed2018-04-24 17:21:31 +0200143 return 0;
144}
145
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100146static int mox_do_spi(u8 *in, u8 *out, size_t size)
Marek Behúnf835bed2018-04-24 17:21:31 +0200147{
148 struct spi_slave *slave;
149 struct udevice *dev;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100150 int ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200151
Patrice Chotard86e06ae2022-03-30 09:33:13 +0200152 ret = _spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
153 "spi_generic_drv", "moxtet@1", &dev,
154 &slave);
Marek Behúnf835bed2018-04-24 17:21:31 +0200155 if (ret)
156 goto fail;
157
158 ret = spi_claim_bus(slave);
159 if (ret)
160 goto fail_free;
161
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100162 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
163
164 spi_release_bus(slave);
165fail_free:
166 spi_free_slave(slave);
167fail:
168 return ret;
169}
170
171static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
172{
173 static int is_sd;
174 static u8 topology[MAX_MOX_MODULES - 1];
175 static int size;
176 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
177 int ret, i;
178
179 if (size) {
180 if (ptopology)
181 *ptopology = topology;
182 if (psize)
183 *psize = size;
184 if (pis_sd)
185 *pis_sd = is_sd;
186 return 0;
187 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200188
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100189 memset(din, 0, MAX_MOX_MODULES);
190 memset(dout, 0, MAX_MOX_MODULES);
191
192 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
Marek Behúnf835bed2018-04-24 17:21:31 +0200193 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100194 return ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200195
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100196 if (din[0] == 0x10)
197 is_sd = 1;
198 else if (din[0] == 0x00)
199 is_sd = 0;
200 else
201 return -ENODEV;
Marek Behúnf835bed2018-04-24 17:21:31 +0200202
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100203 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
204 topology[i - 1] = din[i] & 0xf;
205 size = i - 1;
206
207 if (ptopology)
208 *ptopology = topology;
209 if (psize)
210 *psize = size;
211 if (pis_sd)
212 *pis_sd = is_sd;
213
214 return 0;
215}
216
Marek Behún35b35132018-12-17 16:10:03 +0100217#define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
218#define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
219
Marek Behún1c8eb902022-04-27 12:41:50 +0200220static int sw_multi_read(struct udevice *bus, int sw, int dev, int reg)
Marek Behún35b35132018-12-17 16:10:03 +0100221{
Marek Behún1c8eb902022-04-27 12:41:50 +0200222 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 0, SW_SMI_CMD_R(dev, reg));
Marek Behún35b35132018-12-17 16:10:03 +0100223 mdelay(5);
Marek Behún1c8eb902022-04-27 12:41:50 +0200224 return dm_mdio_read(bus, sw, MDIO_DEVAD_NONE, 1);
Marek Behún35b35132018-12-17 16:10:03 +0100225}
226
Marek Behún1c8eb902022-04-27 12:41:50 +0200227static void sw_multi_write(struct udevice *bus, int sw, int dev, int reg,
Marek Behún35b35132018-12-17 16:10:03 +0100228 u16 val)
229{
Marek Behún1c8eb902022-04-27 12:41:50 +0200230 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 1, val);
231 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 0, SW_SMI_CMD_W(dev, reg));
Marek Behún35b35132018-12-17 16:10:03 +0100232 mdelay(5);
233}
234
Marek Behún1c8eb902022-04-27 12:41:50 +0200235static int sw_scratch_read(struct udevice *bus, int sw, int reg)
Marek Behún35b35132018-12-17 16:10:03 +0100236{
237 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
238 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
239}
240
Marek Behún1c8eb902022-04-27 12:41:50 +0200241static void sw_led_write(struct udevice *bus, int sw, int port, int reg,
Marek Behún35b35132018-12-17 16:10:03 +0100242 u16 val)
243{
244 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
245 | (val & 0x7ff));
246}
247
Marek Behún1c8eb902022-04-27 12:41:50 +0200248static void sw_blink_leds(struct udevice *bus, int peridot, int topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100249{
250 int i, p;
251 struct {
252 int port;
253 u16 val;
254 int wait;
255 } regs[] = {
256 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
257 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
258 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
259 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
260 };
261
262 for (i = 0; i < 12; ++i) {
263 for (p = 0; p < peridot; ++p) {
264 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
265 regs[i].val);
266 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
267 regs[i].val);
268 }
269 if (topaz) {
270 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
271 regs[i].val);
272 }
273
274 if (regs[i].wait)
275 mdelay(75);
276 }
277}
278
Marek Behún1c8eb902022-04-27 12:41:50 +0200279static void check_switch_address(struct udevice *bus, int addr)
Marek Behún35b35132018-12-17 16:10:03 +0100280{
281 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
282 printf("Check of switch MDIO address failed for 0x%02x\n",
283 addr);
284}
285
286static int sfp, pci, topaz, peridot, usb, passpci;
287static int sfp_pos, peridot_pos[3];
288static int module_count;
289
290static int configure_peridots(struct gpio_desc *reset_gpio)
291{
292 int i, ret;
293 u8 dout[MAX_MOX_MODULES];
294
295 memset(dout, 0, MAX_MOX_MODULES);
296
297 /* set addresses of Peridot modules */
298 for (i = 0; i < peridot; ++i)
299 dout[module_count - peridot_pos[i]] = (~i) & 3;
300
301 /*
302 * if there is a SFP module connected to the last Peridot module, set
303 * the P10_SMODE to 1 for the Peridot module
304 */
305 if (sfp)
306 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
307
308 dm_gpio_set_value(reset_gpio, 1);
309 mdelay(10);
310
311 ret = mox_do_spi(NULL, dout, module_count + 1);
312
313 mdelay(10);
314 dm_gpio_set_value(reset_gpio, 0);
315
316 mdelay(50);
317
318 return ret;
319}
320
321static int get_reset_gpio(struct gpio_desc *reset_gpio)
322{
323 int node;
324
325 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
326 if (node < 0) {
327 printf("Cannot find Moxtet bus device node!\n");
328 return -1;
329 }
330
331 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
332 reset_gpio, GPIOD_IS_OUT);
333
334 if (!dm_gpio_is_valid(reset_gpio)) {
335 printf("Cannot find reset GPIO for Moxtet bus!\n");
336 return -1;
337 }
338
Marek Behún35572c92018-12-17 16:10:08 +0100339 return 0;
340}
341
342int misc_init_r(void)
343{
Marek Behún98c86c42021-10-09 19:33:42 +0200344 u8 mac[2][6];
345 int i, ret;
Marek Behún35572c92018-12-17 16:10:08 +0100346
Marek Behún98c86c42021-10-09 19:33:42 +0200347 ret = mbox_sp_get_board_info(NULL, mac[0], mac[1], NULL, NULL);
Marek Behún35572c92018-12-17 16:10:08 +0100348 if (ret < 0) {
349 printf("Cannot read data from OTP!\n");
350 return 0;
351 }
352
Marek Behún98c86c42021-10-09 19:33:42 +0200353 for (i = 0; i < 2; ++i) {
354 u8 oldmac[6];
Marek Behún35572c92018-12-17 16:10:08 +0100355
Marek Behún98c86c42021-10-09 19:33:42 +0200356 if (is_valid_ethaddr(mac[i]) &&
357 !eth_env_get_enetaddr_by_index("eth", i, oldmac))
358 eth_env_set_enetaddr_by_index("eth", i, mac[i]);
359 }
Marek Behún35572c92018-12-17 16:10:08 +0100360
Marek Behún35b35132018-12-17 16:10:03 +0100361 return 0;
362}
363
Marek Behúnd1a30792021-06-07 16:34:48 +0200364static void mox_phy_modify(struct phy_device *phydev, int page, int reg,
365 u16 mask, u16 set)
366{
367 int val;
368
369 val = phydev->drv->readext(phydev, MDIO_DEVAD_NONE, page, reg);
370 val &= ~mask;
371 val |= set;
372 phydev->drv->writeext(phydev, MDIO_DEVAD_NONE, page, reg, val);
373}
374
375static void mox_phy_leds_start_blinking(void)
376{
377 struct phy_device *phydev;
Marek Behún1c8eb902022-04-27 12:41:50 +0200378 ofnode phy_node;
Pali Rohára524db02022-02-14 11:34:26 +0100379
Marek Behún1c8eb902022-04-27 12:41:50 +0200380 phy_node = ofnode_get_phy_node(ofnode_path("ethernet0"));
381 if (!ofnode_valid(phy_node))
382 goto err;
Marek Behúnd1a30792021-06-07 16:34:48 +0200383
Marek Behún1c8eb902022-04-27 12:41:50 +0200384 phydev = dm_phy_find_by_ofnode(phy_node);
385 if (!phydev)
386 goto err;
Marek Behúnd1a30792021-06-07 16:34:48 +0200387
388 mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400);
389 mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb);
Marek Behún1c8eb902022-04-27 12:41:50 +0200390
391 return;
392err:
393 printf("Cannot get ethernet PHY!\n");
Marek Behúnd1a30792021-06-07 16:34:48 +0200394}
395
Marek Behún859ada52021-06-07 16:34:47 +0200396static bool read_reset_button(void)
397{
398 struct udevice *button, *led;
399 int i;
400
Marek Behúnd050eab2021-11-26 14:57:11 +0100401 if (device_get_global_by_ofnode(
402 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
403 "gpio-keys")),
404 &button)) {
Marek Behún859ada52021-06-07 16:34:47 +0200405 printf("Cannot find reset button!\n");
406 return false;
407 }
408
Marek Behúnd050eab2021-11-26 14:57:11 +0100409 if (device_get_global_by_ofnode(
410 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
411 "gpio-leds")),
412 &led)) {
Marek Behún859ada52021-06-07 16:34:47 +0200413 printf("Cannot find status LED!\n");
414 return false;
415 }
416
417 led_set_state(led, LEDST_ON);
418
419 for (i = 0; i < 21; ++i) {
420 if (button_get_state(button) != BUTTON_ON)
421 return false;
422 if (i < 20)
423 mdelay(50);
424 }
425
426 led_set_state(led, LEDST_OFF);
427
428 return true;
429}
430
431static void handle_reset_button(void)
432{
Pali Rohár905c3bf2021-06-14 16:45:58 +0200433 const char * const vars[1] = { "bootcmd_rescue", };
434
435 /*
436 * Ensure that bootcmd_rescue has always stock value, so that running
437 * run bootcmd_rescue
438 * always works correctly.
439 */
440 env_set_default_vars(1, (char * const *)vars, 0);
441
Marek Behún859ada52021-06-07 16:34:47 +0200442 if (read_reset_button()) {
Pali Rohár905c3bf2021-06-14 16:45:58 +0200443 const char * const vars[2] = {
Marek Behún859ada52021-06-07 16:34:47 +0200444 "bootcmd",
Marek Behún859ada52021-06-07 16:34:47 +0200445 "distro_bootcmd",
446 };
447
448 /*
449 * Set the above envs to their default values, in case the user
450 * managed to break them.
451 */
Pali Rohár905c3bf2021-06-14 16:45:58 +0200452 env_set_default_vars(2, (char * const *)vars, 0);
Marek Behún859ada52021-06-07 16:34:47 +0200453
454 /* Ensure bootcmd_rescue is used by distroboot */
455 env_set("boot_targets", "rescue");
456
Marek Behúnd1a30792021-06-07 16:34:48 +0200457 /* start blinking PHY LEDs */
458 mox_phy_leds_start_blinking();
459
Marek Behún859ada52021-06-07 16:34:47 +0200460 printf("RESET button was pressed, overwriting boot_targets!\n");
461 } else {
462 /*
463 * In case the user somehow managed to save environment with
464 * boot_targets=rescue, reset boot_targets to default value.
465 * This could happen in subsequent commands if bootcmd_rescue
466 * failed.
467 */
468 if (!strcmp(env_get("boot_targets"), "rescue")) {
469 const char * const vars[1] = {
470 "boot_targets",
471 };
472
473 env_set_default_vars(1, (char * const *)vars, 0);
474 }
475 }
476}
477
Marek Behún84ee2cd2021-10-09 19:33:40 +0200478int show_board_info(void)
Marek Behún35572c92018-12-17 16:10:08 +0100479{
Marek Behún84ee2cd2021-10-09 19:33:40 +0200480 int i, ret, board_version, ram_size, is_sd;
Marek Behún35572c92018-12-17 16:10:08 +0100481 const char *pub_key;
Marek Behún84ee2cd2021-10-09 19:33:40 +0200482 const u8 *topology;
483 u64 serial_number;
484
485 printf("Model: CZ.NIC Turris Mox Board\n");
Marek Behún35572c92018-12-17 16:10:08 +0100486
487 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
488 &ram_size);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200489 if (ret < 0) {
490 printf(" Cannot read board info: %i\n", ret);
491 } else {
492 printf(" Board version: %i\n", board_version);
493 printf(" RAM size: %i MiB\n", ram_size);
494 printf(" Serial Number: %016llX\n", serial_number);
495 }
Marek Behún35572c92018-12-17 16:10:08 +0100496
497 pub_key = mox_sp_get_ecdsa_public_key();
498 if (pub_key)
499 printf(" ECDSA Public Key: %s\n", pub_key);
500 else
Marek Behún84ee2cd2021-10-09 19:33:40 +0200501 printf(" Cannot read ECDSA Public Key\n");
Marek Behún35572c92018-12-17 16:10:08 +0100502
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100503 ret = mox_get_topology(&topology, &module_count, &is_sd);
Marek Behún84ee2cd2021-10-09 19:33:40 +0200504 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100505 printf("Cannot read module topology!\n");
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100506
Marek Behún35572c92018-12-17 16:10:08 +0100507 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
508
509 if (module_count)
510 printf("Module Topology:\n");
511
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100512 for (i = 0; i < module_count; ++i) {
513 switch (topology[i]) {
514 case MOX_MODULE_SFP:
515 printf("% 4i: SFP Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200516 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100517 case MOX_MODULE_PCI:
518 printf("% 4i: Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200519 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100520 case MOX_MODULE_TOPAZ:
521 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
522 break;
523 case MOX_MODULE_PERIDOT:
524 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
525 break;
526 case MOX_MODULE_USB3:
527 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
528 break;
529 case MOX_MODULE_PASSPCI:
530 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200531 break;
532 default:
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100533 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
Marek Behúnf835bed2018-04-24 17:21:31 +0200534 }
535 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200536
Marek Behún84ee2cd2021-10-09 19:33:40 +0200537 /* check if modules are connected in supported mode */
Marek Behún35b35132018-12-17 16:10:03 +0100538 for (i = 0; i < module_count; ++i) {
539 switch (topology[i]) {
540 case MOX_MODULE_SFP:
541 if (sfp) {
542 printf("Error: Only one SFP module is supported!\n");
543 } else if (topaz) {
544 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
545 } else {
546 sfp_pos = i;
547 ++sfp;
548 }
549 break;
550 case MOX_MODULE_PCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200551 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100552 printf("Error: Only one Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200553 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100554 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200555 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100556 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 +0200557 else
Marek Behún35b35132018-12-17 16:10:03 +0100558 ++pci;
Marek Behún35b35132018-12-17 16:10:03 +0100559 break;
560 case MOX_MODULE_TOPAZ:
Marek Behún4529fce2020-04-08 12:02:05 +0200561 if (topaz)
Marek Behún35b35132018-12-17 16:10:03 +0100562 printf("Error: Only one Topaz module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200563 else if (peridot >= 3)
Marek Behún35b35132018-12-17 16:10:03 +0100564 printf("Error: At most two Peridot modules can come before Topaz module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200565 else
Marek Behún35b35132018-12-17 16:10:03 +0100566 ++topaz;
Marek Behún35b35132018-12-17 16:10:03 +0100567 break;
568 case MOX_MODULE_PERIDOT:
569 if (sfp || topaz) {
570 printf("Error: Peridot module must come before SFP or Topaz module!\n");
571 } else if (peridot >= 3) {
572 printf("Error: At most three Peridot modules are supported!\n");
573 } else {
574 peridot_pos[peridot] = i;
575 ++peridot;
576 }
577 break;
578 case MOX_MODULE_USB3:
Marek Behún4529fce2020-04-08 12:02:05 +0200579 if (pci)
Marek Behún35b35132018-12-17 16:10:03 +0100580 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200581 else if (usb)
Marek Behún35b35132018-12-17 16:10:03 +0100582 printf("Error: Only one USB 3.0 module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200583 else if (i && (i != 1 || !passpci))
Marek Behún35b35132018-12-17 16:10:03 +0100584 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 +0200585 else
Marek Behún35b35132018-12-17 16:10:03 +0100586 ++usb;
Marek Behún35b35132018-12-17 16:10:03 +0100587 break;
588 case MOX_MODULE_PASSPCI:
Marek Behún4529fce2020-04-08 12:02:05 +0200589 if (passpci)
Marek Behún35b35132018-12-17 16:10:03 +0100590 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200591 else if (i != 0)
Marek Behún35b35132018-12-17 16:10:03 +0100592 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
Marek Behún4529fce2020-04-08 12:02:05 +0200593 else
Marek Behún35b35132018-12-17 16:10:03 +0100594 ++passpci;
Marek Behún35b35132018-12-17 16:10:03 +0100595 }
596 }
597
Marek Behún84ee2cd2021-10-09 19:33:40 +0200598 return 0;
599}
Marek Behún35b35132018-12-17 16:10:03 +0100600
Marek Behún1c8eb902022-04-27 12:41:50 +0200601static struct udevice *mox_mdio_bus(void)
602{
603 struct udevice *bus;
604 ofnode node;
605
606 node = ofnode_by_compatible(ofnode_null(), "marvell,orion-mdio");
607 if (!ofnode_valid(node))
608 goto err;
609
610 dm_mdio_probe_devices();
611
612 if (uclass_get_device_by_ofnode(UCLASS_MDIO, node, &bus))
613 goto err;
614
615 return bus;
616err:
617 printf("Cannot get MDIO bus device!\n");
618 return NULL;
619}
620
Marek Behún84ee2cd2021-10-09 19:33:40 +0200621int last_stage_init(void)
622{
623 struct gpio_desc reset_gpio = {};
624
625 /* configure modules */
Marek Behún35b35132018-12-17 16:10:03 +0100626 if (get_reset_gpio(&reset_gpio) < 0)
Marek Behún84d67ac2021-10-09 19:33:41 +0200627 goto handle_reset_btn;
Marek Behún35b35132018-12-17 16:10:03 +0100628
629 if (peridot > 0) {
630 if (configure_peridots(&reset_gpio) < 0) {
631 printf("Cannot configure Peridot modules!\n");
632 peridot = 0;
633 }
634 } else {
635 dm_gpio_set_value(&reset_gpio, 1);
636 mdelay(50);
637 dm_gpio_set_value(&reset_gpio, 0);
638 mdelay(50);
639 }
640
Marek Behún84ee2cd2021-10-09 19:33:40 +0200641 /*
642 * check if the addresses are set by reading Scratch & Misc register
643 * 0x70 of Peridot (and potentially Topaz) modules
644 */
Marek Behún35b35132018-12-17 16:10:03 +0100645 if (peridot || topaz) {
Marek Behún1c8eb902022-04-27 12:41:50 +0200646 struct udevice *bus = mox_mdio_bus();
Marek Behún35b35132018-12-17 16:10:03 +0100647
Marek Behún1c8eb902022-04-27 12:41:50 +0200648 if (bus) {
Marek Behún84ee2cd2021-10-09 19:33:40 +0200649 int i;
650
Marek Behún35b35132018-12-17 16:10:03 +0100651 for (i = 0; i < peridot; ++i)
652 check_switch_address(bus, 0x10 + i);
653
654 if (topaz)
655 check_switch_address(bus, 0x2);
656
657 sw_blink_leds(bus, peridot, topaz);
658 }
659 }
660
Marek Behún84d67ac2021-10-09 19:33:41 +0200661handle_reset_btn:
Marek Behún859ada52021-06-07 16:34:47 +0200662 handle_reset_button();
663
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100664 return 0;
Marek Behúnf835bed2018-04-24 17:21:31 +0200665}
Marek Behún4529fce2020-04-08 12:02:05 +0200666
667#if defined(CONFIG_OF_BOARD_SETUP)
668
Marek Behúnd050eab2021-11-26 14:57:11 +0100669static bool is_topaz(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200670{
Marek Behúnd050eab2021-11-26 14:57:11 +0100671 return topaz && id == peridot + topaz - 1;
Marek Behún4529fce2020-04-08 12:02:05 +0200672}
673
Marek Behúnd050eab2021-11-26 14:57:11 +0100674static int switch_addr(int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200675{
Marek Behúnd050eab2021-11-26 14:57:11 +0100676 return is_topaz(id) ? 0x2 : 0x10 + id;
Marek Behún4529fce2020-04-08 12:02:05 +0200677}
678
Marek Behúnd050eab2021-11-26 14:57:11 +0100679static int setup_switch(void *blob, int id)
Marek Behún4529fce2020-04-08 12:02:05 +0200680{
Marek Behúnd050eab2021-11-26 14:57:11 +0100681 int res, addr, i, node;
682 char mdio_path[64];
Marek Behún4529fce2020-04-08 12:02:05 +0200683
Marek Behúnd050eab2021-11-26 14:57:11 +0100684 node = fdt_node_offset_by_compatible(blob, -1, "marvell,orion-mdio");
Marek Behún4529fce2020-04-08 12:02:05 +0200685 if (node < 0)
686 return node;
687
Marek Behúnd050eab2021-11-26 14:57:11 +0100688 res = fdt_get_path(blob, node, mdio_path, sizeof(mdio_path));
Marek Behún4529fce2020-04-08 12:02:05 +0200689 if (res < 0)
690 return res;
691
Marek Behún4529fce2020-04-08 12:02:05 +0200692 addr = switch_addr(id);
693
694 /* first enable the switch by setting status = "okay" */
Marek Behúnd050eab2021-11-26 14:57:11 +0100695 res = fdt_status_okay_by_pathf(blob, "%s/switch%i@%x", mdio_path, id,
696 addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200697 if (res < 0)
698 return res;
699
700 /*
701 * now if there are more switches or a SFP module coming after,
702 * enable corresponding ports
703 */
704 if (id < peridot + topaz - 1) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100705 res = fdt_status_okay_by_pathf(blob,
706 "%s/switch%i@%x/ports/port@a",
707 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200708 } else if (id == peridot - 1 && !topaz && sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100709 res = fdt_status_okay_by_pathf(blob,
710 "%s/switch%i@%x/ports/port-sfp@a",
711 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200712 } else {
713 res = 0;
714 }
715 if (res < 0)
716 return res;
717
718 if (id >= peridot + topaz - 1)
719 return 0;
720
721 /* finally change link property if needed */
Marek Behúnd050eab2021-11-26 14:57:11 +0100722 node = fdt_node_offset_by_pathf(blob, "%s/switch%i@%x/ports/port@a",
723 mdio_path, id, addr);
Marek Behún4529fce2020-04-08 12:02:05 +0200724 if (node < 0)
725 return node;
726
727 for (i = id + 1; i < peridot + topaz; ++i) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100728 unsigned int phandle;
729
730 phandle = fdt_create_phandle_by_pathf(blob,
731 "%s/switch%i@%x/ports/port@%x",
732 mdio_path, i,
733 switch_addr(i),
734 is_topaz(i) ? 5 : 9);
735 if (!phandle)
736 return -FDT_ERR_NOPHANDLES;
Marek Behún4529fce2020-04-08 12:02:05 +0200737
738 if (i == id + 1)
739 res = fdt_setprop_u32(blob, node, "link", phandle);
740 else
741 res = fdt_appendprop_u32(blob, node, "link", phandle);
742 if (res < 0)
743 return res;
744 }
745
746 return 0;
747}
748
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900749int ft_board_setup(void *blob, struct bd_info *bd)
Marek Behún4529fce2020-04-08 12:02:05 +0200750{
Marek Behúnd050eab2021-11-26 14:57:11 +0100751 int res;
Marek Behún4529fce2020-04-08 12:02:05 +0200752
753 /*
754 * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
755 * connected, enable the PCIe node.
756 */
757 if (pci || usb || passpci) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100758 res = fdt_status_okay_by_compatible(blob,
759 "marvell,armada-3700-pcie");
Marek Behún4529fce2020-04-08 12:02:05 +0200760 if (res < 0)
761 return res;
Marek Behún455ef5b2020-04-08 19:25:22 +0200762
763 /* Fix PCIe regions for devices with 4 GB RAM */
764 res = a3700_fdt_fix_pcie_regions(blob);
765 if (res < 0)
766 return res;
Marek Behún4529fce2020-04-08 12:02:05 +0200767 }
768
769 /*
770 * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
771 * enable the eth1 node and setup the switches.
772 */
773 if (peridot || topaz) {
774 int i;
775
Marek Behúnd050eab2021-11-26 14:57:11 +0100776 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200777 if (res < 0)
778 return res;
779
780 for (i = 0; i < peridot + topaz; ++i) {
781 res = setup_switch(blob, i);
782 if (res < 0)
783 return res;
784 }
785 }
786
787 /*
788 * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
789 * node. If there is no Peridot switch between MOX A and MOX D, add link
790 * to the SFP node to eth1 node.
791 * Also enable and configure SFP GPIO controller node.
792 */
793 if (sfp) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100794 int node;
795
796 res = fdt_status_okay_by_compatible(blob, "sff,sfp");
Marek Behún4529fce2020-04-08 12:02:05 +0200797 if (res < 0)
798 return res;
799
Marek Behúnd050eab2021-11-26 14:57:11 +0100800 res = fdt_status_okay_by_alias(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200801 if (res < 0)
802 return res;
803
804 if (!peridot) {
Marek Behúnd050eab2021-11-26 14:57:11 +0100805 unsigned int phandle;
Marek Behún4529fce2020-04-08 12:02:05 +0200806
Marek Behúnd050eab2021-11-26 14:57:11 +0100807 phandle = fdt_create_phandle_by_compatible(blob,
808 "sff,sfp");
809 if (!phandle)
810 return -FDT_ERR_NOPHANDLES;
811
812 node = fdt_path_offset(blob, "ethernet1");
Marek Behún4529fce2020-04-08 12:02:05 +0200813 if (node < 0)
814 return node;
815
816 res = fdt_setprop_u32(blob, node, "sfp", phandle);
817 if (res < 0)
818 return res;
819
820 res = fdt_setprop_string(blob, node, "phy-mode",
821 "sgmii");
822 if (res < 0)
823 return res;
824 }
825
Marek Behúnd050eab2021-11-26 14:57:11 +0100826 res = fdt_status_okay_by_compatible(blob, "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200827 if (res < 0)
828 return res;
829
830 if (sfp_pos) {
831 char newname[16];
832
833 /* moxtet-sfp is on non-zero position, change default */
Marek Behúnd050eab2021-11-26 14:57:11 +0100834 node = fdt_node_offset_by_compatible(blob, -1,
835 "cznic,moxtet-gpio");
Marek Behún4529fce2020-04-08 12:02:05 +0200836 if (node < 0)
837 return node;
838
839 res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
840 if (res < 0)
841 return res;
842
843 sprintf(newname, "gpio@%x", sfp_pos);
844
845 res = fdt_set_name(blob, node, newname);
846 if (res < 0)
847 return res;
848 }
849 }
850
851 fdt_fixup_ethernet(blob);
852
853 /* Finally remove disabled nodes, as per Rob Herring's request. */
Marek Behúnac4d9ff2021-11-26 14:57:15 +0100854 fdt_delete_disabled_nodes(blob);
Marek Behún4529fce2020-04-08 12:02:05 +0200855
856 return 0;
857}
858
859#endif