blob: 946e20ab492f8990c38fd76ac103563be97bf8be [file] [log] [blame]
Marek Behúnf835bed2018-04-24 17:21:31 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Marek Behun <marek.behun@nic.cz>
4 */
5
6#include <common.h>
Marek Behún35b35132018-12-17 16:10:03 +01007#include <asm/gpio.h>
Marek Behún9602a8c2018-08-21 12:22:09 +02008#include <asm/io.h>
Marek Behúnf835bed2018-04-24 17:21:31 +02009#include <dm.h>
10#include <clk.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060011#include <env.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020012#include <spi.h>
Marek Behúna261d132018-12-17 16:10:02 +010013#include <mvebu/comphy.h>
Marek Behún35b35132018-12-17 16:10:03 +010014#include <miiphy.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020015#include <linux/string.h>
Marek Behún9602a8c2018-08-21 12:22:09 +020016#include <linux/libfdt.h>
17#include <fdt_support.h>
Marek Behúnf835bed2018-04-24 17:21:31 +020018
Marek Behún35572c92018-12-17 16:10:08 +010019#include "mox_sp.h"
20
Marek Behún9602a8c2018-08-21 12:22:09 +020021#define MAX_MOX_MODULES 10
22
23#define MOX_MODULE_SFP 0x1
24#define MOX_MODULE_PCI 0x2
25#define MOX_MODULE_TOPAZ 0x3
26#define MOX_MODULE_PERIDOT 0x4
27#define MOX_MODULE_USB3 0x5
28#define MOX_MODULE_PASSPCI 0x6
29
30#define ARMADA_37XX_NB_GPIO_SEL 0xd0013830
31#define ARMADA_37XX_SPI_CTRL 0xd0010600
32#define ARMADA_37XX_SPI_CFG 0xd0010604
33#define ARMADA_37XX_SPI_DOUT 0xd0010608
34#define ARMADA_37XX_SPI_DIN 0xd001060c
35
36#define PCIE_PATH "/soc/pcie@d0070000"
37
Marek Behúnf835bed2018-04-24 17:21:31 +020038DECLARE_GLOBAL_DATA_PTR;
39
Marek Behún22a0ce32018-12-17 16:10:09 +010040int dram_init(void)
41{
42 gd->ram_base = 0;
43 gd->ram_size = (phys_size_t)get_ram_size(0, 0x40000000);
44
45 return 0;
46}
47
48int dram_init_banksize(void)
49{
50 gd->bd->bi_dram[0].start = (phys_addr_t)0;
51 gd->bd->bi_dram[0].size = gd->ram_size;
52
53 return 0;
54}
55
Marek Behún9602a8c2018-08-21 12:22:09 +020056#if defined(CONFIG_OF_BOARD_FIXUP)
57int board_fix_fdt(void *blob)
58{
59 u8 topology[MAX_MOX_MODULES];
60 int i, size, node;
61 bool enable;
62
63 /*
64 * SPI driver is not loaded in driver model yet, but we have to find out
65 * if pcie should be enabled in U-Boot's device tree. Therefore we have
66 * to read SPI by reading/writing SPI registers directly
67 */
68
69 writel(0x563fa, ARMADA_37XX_NB_GPIO_SEL);
70 writel(0x10df, ARMADA_37XX_SPI_CFG);
71 writel(0x2005b, ARMADA_37XX_SPI_CTRL);
72
73 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
74 udelay(1);
75
76 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;
87 }
88
89 size = i;
90
91 writel(0x5b, ARMADA_37XX_SPI_CTRL);
92
93 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
94 topology[1] == MOX_MODULE_USB3 ||
95 topology[1] == MOX_MODULE_PASSPCI))
96 enable = true;
97 else
98 enable = false;
99
100 node = fdt_path_offset(blob, PCIE_PATH);
101
102 if (node < 0) {
103 printf("Cannot find PCIe node in U-Boot's device tree!\n");
104 return 0;
105 }
106
107 if (fdt_setprop_string(blob, node, "status",
108 enable ? "okay" : "disabled") < 0) {
109 printf("Cannot %s PCIe in U-Boot's device tree!\n",
110 enable ? "enable" : "disable");
111 return 0;
112 }
113
114 return 0;
115}
116#endif
117
Marek Behúnf835bed2018-04-24 17:21:31 +0200118int board_init(void)
119{
120 /* address of boot parameters */
121 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
122
Marek Behúnf835bed2018-04-24 17:21:31 +0200123 return 0;
124}
125
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100126static int mox_do_spi(u8 *in, u8 *out, size_t size)
Marek Behúnf835bed2018-04-24 17:21:31 +0200127{
128 struct spi_slave *slave;
129 struct udevice *dev;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100130 int ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200131
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100132 ret = spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
133 "spi_generic_drv", "moxtet@1", &dev,
134 &slave);
Marek Behúnf835bed2018-04-24 17:21:31 +0200135 if (ret)
136 goto fail;
137
138 ret = spi_claim_bus(slave);
139 if (ret)
140 goto fail_free;
141
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100142 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
143
144 spi_release_bus(slave);
145fail_free:
146 spi_free_slave(slave);
147fail:
148 return ret;
149}
150
151static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
152{
153 static int is_sd;
154 static u8 topology[MAX_MOX_MODULES - 1];
155 static int size;
156 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
157 int ret, i;
158
159 if (size) {
160 if (ptopology)
161 *ptopology = topology;
162 if (psize)
163 *psize = size;
164 if (pis_sd)
165 *pis_sd = is_sd;
166 return 0;
167 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200168
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100169 memset(din, 0, MAX_MOX_MODULES);
170 memset(dout, 0, MAX_MOX_MODULES);
171
172 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
Marek Behúnf835bed2018-04-24 17:21:31 +0200173 if (ret)
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100174 return ret;
Marek Behúnf835bed2018-04-24 17:21:31 +0200175
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100176 if (din[0] == 0x10)
177 is_sd = 1;
178 else if (din[0] == 0x00)
179 is_sd = 0;
180 else
181 return -ENODEV;
Marek Behúnf835bed2018-04-24 17:21:31 +0200182
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100183 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
184 topology[i - 1] = din[i] & 0xf;
185 size = i - 1;
186
187 if (ptopology)
188 *ptopology = topology;
189 if (psize)
190 *psize = size;
191 if (pis_sd)
192 *pis_sd = is_sd;
193
194 return 0;
195}
196
Marek Behúna261d132018-12-17 16:10:02 +0100197int comphy_update_map(struct comphy_map *serdes_map, int count)
198{
199 int ret, i, size, sfpindex = -1, swindex = -1;
200 const u8 *topology;
201
202 ret = mox_get_topology(&topology, &size, NULL);
203 if (ret)
204 return ret;
205
206 for (i = 0; i < size; ++i) {
207 if (topology[i] == MOX_MODULE_SFP && sfpindex == -1)
208 sfpindex = i;
209 else if ((topology[i] == MOX_MODULE_TOPAZ ||
210 topology[i] == MOX_MODULE_PERIDOT) &&
211 swindex == -1)
212 swindex = i;
213 }
214
215 if (sfpindex >= 0 && swindex >= 0) {
216 if (sfpindex < swindex)
217 serdes_map[0].speed = PHY_SPEED_1_25G;
218 else
219 serdes_map[0].speed = PHY_SPEED_3_125G;
220 } else if (sfpindex >= 0) {
221 serdes_map[0].speed = PHY_SPEED_1_25G;
222 } else if (swindex >= 0) {
223 serdes_map[0].speed = PHY_SPEED_3_125G;
224 }
225
226 return 0;
227}
228
Marek Behún35b35132018-12-17 16:10:03 +0100229#define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
230#define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
231
232static int sw_multi_read(struct mii_dev *bus, int sw, int dev, int reg)
233{
234 bus->write(bus, sw, 0, 0, SW_SMI_CMD_R(dev, reg));
235 mdelay(5);
236 return bus->read(bus, sw, 0, 1);
237}
238
239static void sw_multi_write(struct mii_dev *bus, int sw, int dev, int reg,
240 u16 val)
241{
242 bus->write(bus, sw, 0, 1, val);
243 bus->write(bus, sw, 0, 0, SW_SMI_CMD_W(dev, reg));
244 mdelay(5);
245}
246
247static int sw_scratch_read(struct mii_dev *bus, int sw, int reg)
248{
249 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
250 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
251}
252
253static void sw_led_write(struct mii_dev *bus, int sw, int port, int reg,
254 u16 val)
255{
256 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
257 | (val & 0x7ff));
258}
259
260static void sw_blink_leds(struct mii_dev *bus, int peridot, int topaz)
261{
262 int i, p;
263 struct {
264 int port;
265 u16 val;
266 int wait;
267 } regs[] = {
268 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
269 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
270 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
271 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
272 };
273
274 for (i = 0; i < 12; ++i) {
275 for (p = 0; p < peridot; ++p) {
276 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
277 regs[i].val);
278 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
279 regs[i].val);
280 }
281 if (topaz) {
282 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
283 regs[i].val);
284 }
285
286 if (regs[i].wait)
287 mdelay(75);
288 }
289}
290
291static void check_switch_address(struct mii_dev *bus, int addr)
292{
293 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
294 printf("Check of switch MDIO address failed for 0x%02x\n",
295 addr);
296}
297
298static int sfp, pci, topaz, peridot, usb, passpci;
299static int sfp_pos, peridot_pos[3];
300static int module_count;
301
302static int configure_peridots(struct gpio_desc *reset_gpio)
303{
304 int i, ret;
305 u8 dout[MAX_MOX_MODULES];
306
307 memset(dout, 0, MAX_MOX_MODULES);
308
309 /* set addresses of Peridot modules */
310 for (i = 0; i < peridot; ++i)
311 dout[module_count - peridot_pos[i]] = (~i) & 3;
312
313 /*
314 * if there is a SFP module connected to the last Peridot module, set
315 * the P10_SMODE to 1 for the Peridot module
316 */
317 if (sfp)
318 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
319
320 dm_gpio_set_value(reset_gpio, 1);
321 mdelay(10);
322
323 ret = mox_do_spi(NULL, dout, module_count + 1);
324
325 mdelay(10);
326 dm_gpio_set_value(reset_gpio, 0);
327
328 mdelay(50);
329
330 return ret;
331}
332
333static int get_reset_gpio(struct gpio_desc *reset_gpio)
334{
335 int node;
336
337 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
338 if (node < 0) {
339 printf("Cannot find Moxtet bus device node!\n");
340 return -1;
341 }
342
343 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
344 reset_gpio, GPIOD_IS_OUT);
345
346 if (!dm_gpio_is_valid(reset_gpio)) {
347 printf("Cannot find reset GPIO for Moxtet bus!\n");
348 return -1;
349 }
350
Marek Behún35572c92018-12-17 16:10:08 +0100351 return 0;
352}
353
354int misc_init_r(void)
355{
356 int ret;
357 u8 mac1[6], mac2[6];
358
359 ret = mbox_sp_get_board_info(NULL, mac1, mac2, NULL, NULL);
360 if (ret < 0) {
361 printf("Cannot read data from OTP!\n");
362 return 0;
363 }
364
365 if (is_valid_ethaddr(mac1) && !env_get("ethaddr"))
366 eth_env_set_enetaddr("ethaddr", mac1);
367
368 if (is_valid_ethaddr(mac2) && !env_get("eth1addr"))
369 eth_env_set_enetaddr("eth1addr", mac2);
370
Marek Behún35b35132018-12-17 16:10:03 +0100371 return 0;
372}
373
Marek Behún35572c92018-12-17 16:10:08 +0100374static void mox_print_info(void)
375{
376 int ret, board_version, ram_size;
377 u64 serial_number;
378 const char *pub_key;
379
380 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
381 &ram_size);
382 if (ret < 0)
383 return;
384
385 printf("Turris Mox:\n");
386 printf(" Board version: %i\n", board_version);
387 printf(" RAM size: %i MiB\n", ram_size);
388 printf(" Serial Number: %016llX\n", serial_number);
389
390 pub_key = mox_sp_get_ecdsa_public_key();
391 if (pub_key)
392 printf(" ECDSA Public Key: %s\n", pub_key);
393 else
394 printf("Cannot read ECDSA Public Key\n");
395}
396
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100397int last_stage_init(void)
398{
399 int ret, i;
400 const u8 *topology;
Marek Behún35b35132018-12-17 16:10:03 +0100401 int is_sd;
402 struct mii_dev *bus;
403 struct gpio_desc reset_gpio = {};
Marek Behúnf835bed2018-04-24 17:21:31 +0200404
Marek Behún35572c92018-12-17 16:10:08 +0100405 mox_print_info();
406
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100407 ret = mox_get_topology(&topology, &module_count, &is_sd);
408 if (ret) {
409 printf("Cannot read module topology!\n");
410 return 0;
411 }
412
Marek Behún35572c92018-12-17 16:10:08 +0100413 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
414
415 if (module_count)
416 printf("Module Topology:\n");
417
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100418 for (i = 0; i < module_count; ++i) {
419 switch (topology[i]) {
420 case MOX_MODULE_SFP:
421 printf("% 4i: SFP Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200422 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100423 case MOX_MODULE_PCI:
424 printf("% 4i: Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200425 break;
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100426 case MOX_MODULE_TOPAZ:
427 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
428 break;
429 case MOX_MODULE_PERIDOT:
430 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
431 break;
432 case MOX_MODULE_USB3:
433 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
434 break;
435 case MOX_MODULE_PASSPCI:
436 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
Marek Behúnf835bed2018-04-24 17:21:31 +0200437 break;
438 default:
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100439 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
Marek Behúnf835bed2018-04-24 17:21:31 +0200440 }
441 }
Marek Behúnf835bed2018-04-24 17:21:31 +0200442
Marek Behún35b35132018-12-17 16:10:03 +0100443 /* now check if modules are connected in supported mode */
444
445 for (i = 0; i < module_count; ++i) {
446 switch (topology[i]) {
447 case MOX_MODULE_SFP:
448 if (sfp) {
449 printf("Error: Only one SFP module is supported!\n");
450 } else if (topaz) {
451 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
452 } else {
453 sfp_pos = i;
454 ++sfp;
455 }
456 break;
457 case MOX_MODULE_PCI:
458 if (pci) {
459 printf("Error: Only one Mini-PCIe module is supported!\n");
460 } else if (usb) {
461 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
462 } else if (i && (i != 1 || !passpci)) {
463 printf("Error: Mini-PCIe module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
464 } else {
465 ++pci;
466 }
467 break;
468 case MOX_MODULE_TOPAZ:
469 if (topaz) {
470 printf("Error: Only one Topaz module is supported!\n");
471 } else if (peridot >= 3) {
472 printf("Error: At most two Peridot modules can come before Topaz module!\n");
473 } else {
474 ++topaz;
475 }
476 break;
477 case MOX_MODULE_PERIDOT:
478 if (sfp || topaz) {
479 printf("Error: Peridot module must come before SFP or Topaz module!\n");
480 } else if (peridot >= 3) {
481 printf("Error: At most three Peridot modules are supported!\n");
482 } else {
483 peridot_pos[peridot] = i;
484 ++peridot;
485 }
486 break;
487 case MOX_MODULE_USB3:
488 if (pci) {
489 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
490 } else if (usb) {
491 printf("Error: Only one USB 3.0 module is supported!\n");
492 } else if (i && (i != 1 || !passpci)) {
493 printf("Error: USB 3.0 module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
494 } else {
495 ++usb;
496 }
497 break;
498 case MOX_MODULE_PASSPCI:
499 if (passpci) {
500 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
501 } else if (i != 0) {
502 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
503 } else {
504 ++passpci;
505 }
506 }
507 }
508
509 /* now configure modules */
510
511 if (get_reset_gpio(&reset_gpio) < 0)
512 return 0;
513
514 if (peridot > 0) {
515 if (configure_peridots(&reset_gpio) < 0) {
516 printf("Cannot configure Peridot modules!\n");
517 peridot = 0;
518 }
519 } else {
520 dm_gpio_set_value(&reset_gpio, 1);
521 mdelay(50);
522 dm_gpio_set_value(&reset_gpio, 0);
523 mdelay(50);
524 }
525
526 if (peridot || topaz) {
527 /*
528 * now check if the addresses are set by reading Scratch & Misc
529 * register 0x70 of Peridot (and potentially Topaz) modules
530 */
531
532 bus = miiphy_get_dev_by_name("neta@30000");
533 if (!bus) {
534 printf("Cannot get MDIO bus device!\n");
535 } else {
536 for (i = 0; i < peridot; ++i)
537 check_switch_address(bus, 0x10 + i);
538
539 if (topaz)
540 check_switch_address(bus, 0x2);
541
542 sw_blink_leds(bus, peridot, topaz);
543 }
544 }
545
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100546 printf("\n");
Marek Behúnf835bed2018-04-24 17:21:31 +0200547
Marek Behúnf68ed8b2018-12-17 16:10:01 +0100548 return 0;
Marek Behúnf835bed2018-04-24 17:21:31 +0200549}