turris_mox.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Marek Behun <marek.behun@nic.cz>
  4. */
  5. #include <common.h>
  6. #include <asm/arch/cpu.h>
  7. #include <asm/arch/soc.h>
  8. #include <net.h>
  9. #include <asm/global_data.h>
  10. #include <asm/io.h>
  11. #include <asm/gpio.h>
  12. #include <button.h>
  13. #include <clk.h>
  14. #include <dm.h>
  15. #include <env.h>
  16. #include <fdt_support.h>
  17. #include <init.h>
  18. #include <led.h>
  19. #include <linux/delay.h>
  20. #include <linux/libfdt.h>
  21. #include <linux/string.h>
  22. #include <miiphy.h>
  23. #include <mvebu/comphy.h>
  24. #include <spi.h>
  25. #include "mox_sp.h"
  26. #define MAX_MOX_MODULES 10
  27. #define MOX_MODULE_SFP 0x1
  28. #define MOX_MODULE_PCI 0x2
  29. #define MOX_MODULE_TOPAZ 0x3
  30. #define MOX_MODULE_PERIDOT 0x4
  31. #define MOX_MODULE_USB3 0x5
  32. #define MOX_MODULE_PASSPCI 0x6
  33. #define ARMADA_37XX_NB_GPIO_SEL (MVEBU_REGISTER(0x13830))
  34. #define ARMADA_37XX_SPI_CTRL (MVEBU_REGISTER(0x10600))
  35. #define ARMADA_37XX_SPI_CFG (MVEBU_REGISTER(0x10604))
  36. #define ARMADA_37XX_SPI_DOUT (MVEBU_REGISTER(0x10608))
  37. #define ARMADA_37XX_SPI_DIN (MVEBU_REGISTER(0x1060c))
  38. #define ETH1_PATH "/soc/internal-regs@d0000000/ethernet@40000"
  39. #define MDIO_PATH "/soc/internal-regs@d0000000/mdio@32004"
  40. #define SFP_GPIO_PATH "/soc/internal-regs@d0000000/spi@10600/moxtet@1/gpio@0"
  41. #define PCIE_PATH "/soc/pcie@d0070000"
  42. #define SFP_PATH "/sfp"
  43. #define LED_PATH "/leds/led"
  44. #define BUTTON_PATH "/gpio-keys/reset"
  45. DECLARE_GLOBAL_DATA_PTR;
  46. #if defined(CONFIG_OF_BOARD_FIXUP)
  47. int board_fix_fdt(void *blob)
  48. {
  49. u8 topology[MAX_MOX_MODULES];
  50. int i, size, node;
  51. bool enable;
  52. /*
  53. * SPI driver is not loaded in driver model yet, but we have to find out
  54. * if pcie should be enabled in U-Boot's device tree. Therefore we have
  55. * to read SPI by reading/writing SPI registers directly
  56. */
  57. writel(0x10df, ARMADA_37XX_SPI_CFG);
  58. /* put pin from GPIO to SPI mode */
  59. clrbits_le32(ARMADA_37XX_NB_GPIO_SEL, BIT(12));
  60. /* enable SPI CS1 */
  61. setbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
  62. while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
  63. udelay(1);
  64. for (i = 0; i < MAX_MOX_MODULES; ++i) {
  65. writel(0x0, ARMADA_37XX_SPI_DOUT);
  66. while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
  67. udelay(1);
  68. topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
  69. if (topology[i] == 0xff)
  70. break;
  71. topology[i] &= 0xf;
  72. }
  73. size = i;
  74. /* disable SPI CS1 */
  75. clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
  76. if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
  77. topology[1] == MOX_MODULE_USB3 ||
  78. topology[1] == MOX_MODULE_PASSPCI))
  79. enable = true;
  80. else
  81. enable = false;
  82. node = fdt_path_offset(blob, PCIE_PATH);
  83. if (node < 0) {
  84. printf("Cannot find PCIe node in U-Boot's device tree!\n");
  85. return 0;
  86. }
  87. if (fdt_setprop_string(blob, node, "status",
  88. enable ? "okay" : "disabled") < 0) {
  89. printf("Cannot %s PCIe in U-Boot's device tree!\n",
  90. enable ? "enable" : "disable");
  91. return 0;
  92. }
  93. if (a3700_fdt_fix_pcie_regions(blob) < 0) {
  94. printf("Cannot fix PCIe regions in U-Boot's device tree!\n");
  95. return 0;
  96. }
  97. return 0;
  98. }
  99. #endif
  100. int board_init(void)
  101. {
  102. /* address of boot parameters */
  103. gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
  104. return 0;
  105. }
  106. static int mox_do_spi(u8 *in, u8 *out, size_t size)
  107. {
  108. struct spi_slave *slave;
  109. struct udevice *dev;
  110. int ret;
  111. ret = spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
  112. "spi_generic_drv", "moxtet@1", &dev,
  113. &slave);
  114. if (ret)
  115. goto fail;
  116. ret = spi_claim_bus(slave);
  117. if (ret)
  118. goto fail_free;
  119. ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
  120. spi_release_bus(slave);
  121. fail_free:
  122. spi_free_slave(slave);
  123. fail:
  124. return ret;
  125. }
  126. static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
  127. {
  128. static int is_sd;
  129. static u8 topology[MAX_MOX_MODULES - 1];
  130. static int size;
  131. u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
  132. int ret, i;
  133. if (size) {
  134. if (ptopology)
  135. *ptopology = topology;
  136. if (psize)
  137. *psize = size;
  138. if (pis_sd)
  139. *pis_sd = is_sd;
  140. return 0;
  141. }
  142. memset(din, 0, MAX_MOX_MODULES);
  143. memset(dout, 0, MAX_MOX_MODULES);
  144. ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
  145. if (ret)
  146. return ret;
  147. if (din[0] == 0x10)
  148. is_sd = 1;
  149. else if (din[0] == 0x00)
  150. is_sd = 0;
  151. else
  152. return -ENODEV;
  153. for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
  154. topology[i - 1] = din[i] & 0xf;
  155. size = i - 1;
  156. if (ptopology)
  157. *ptopology = topology;
  158. if (psize)
  159. *psize = size;
  160. if (pis_sd)
  161. *pis_sd = is_sd;
  162. return 0;
  163. }
  164. int comphy_update_map(struct comphy_map *serdes_map, int count)
  165. {
  166. int ret, i, size, sfpindex = -1, swindex = -1;
  167. const u8 *topology;
  168. ret = mox_get_topology(&topology, &size, NULL);
  169. if (ret)
  170. return ret;
  171. for (i = 0; i < size; ++i) {
  172. if (topology[i] == MOX_MODULE_SFP && sfpindex == -1)
  173. sfpindex = i;
  174. else if ((topology[i] == MOX_MODULE_TOPAZ ||
  175. topology[i] == MOX_MODULE_PERIDOT) &&
  176. swindex == -1)
  177. swindex = i;
  178. }
  179. if (sfpindex >= 0 && swindex >= 0) {
  180. if (sfpindex < swindex)
  181. serdes_map[0].speed = COMPHY_SPEED_1_25G;
  182. else
  183. serdes_map[0].speed = COMPHY_SPEED_3_125G;
  184. } else if (sfpindex >= 0) {
  185. serdes_map[0].speed = COMPHY_SPEED_1_25G;
  186. } else if (swindex >= 0) {
  187. serdes_map[0].speed = COMPHY_SPEED_3_125G;
  188. }
  189. return 0;
  190. }
  191. #define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
  192. #define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
  193. static int sw_multi_read(struct mii_dev *bus, int sw, int dev, int reg)
  194. {
  195. bus->write(bus, sw, 0, 0, SW_SMI_CMD_R(dev, reg));
  196. mdelay(5);
  197. return bus->read(bus, sw, 0, 1);
  198. }
  199. static void sw_multi_write(struct mii_dev *bus, int sw, int dev, int reg,
  200. u16 val)
  201. {
  202. bus->write(bus, sw, 0, 1, val);
  203. bus->write(bus, sw, 0, 0, SW_SMI_CMD_W(dev, reg));
  204. mdelay(5);
  205. }
  206. static int sw_scratch_read(struct mii_dev *bus, int sw, int reg)
  207. {
  208. sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
  209. return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
  210. }
  211. static void sw_led_write(struct mii_dev *bus, int sw, int port, int reg,
  212. u16 val)
  213. {
  214. sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
  215. | (val & 0x7ff));
  216. }
  217. static void sw_blink_leds(struct mii_dev *bus, int peridot, int topaz)
  218. {
  219. int i, p;
  220. struct {
  221. int port;
  222. u16 val;
  223. int wait;
  224. } regs[] = {
  225. { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
  226. { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
  227. { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
  228. { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
  229. };
  230. for (i = 0; i < 12; ++i) {
  231. for (p = 0; p < peridot; ++p) {
  232. sw_led_write(bus, 0x10 + p, regs[i].port, 0,
  233. regs[i].val);
  234. sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
  235. regs[i].val);
  236. }
  237. if (topaz) {
  238. sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
  239. regs[i].val);
  240. }
  241. if (regs[i].wait)
  242. mdelay(75);
  243. }
  244. }
  245. static void check_switch_address(struct mii_dev *bus, int addr)
  246. {
  247. if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
  248. printf("Check of switch MDIO address failed for 0x%02x\n",
  249. addr);
  250. }
  251. static int sfp, pci, topaz, peridot, usb, passpci;
  252. static int sfp_pos, peridot_pos[3];
  253. static int module_count;
  254. static int configure_peridots(struct gpio_desc *reset_gpio)
  255. {
  256. int i, ret;
  257. u8 dout[MAX_MOX_MODULES];
  258. memset(dout, 0, MAX_MOX_MODULES);
  259. /* set addresses of Peridot modules */
  260. for (i = 0; i < peridot; ++i)
  261. dout[module_count - peridot_pos[i]] = (~i) & 3;
  262. /*
  263. * if there is a SFP module connected to the last Peridot module, set
  264. * the P10_SMODE to 1 for the Peridot module
  265. */
  266. if (sfp)
  267. dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
  268. dm_gpio_set_value(reset_gpio, 1);
  269. mdelay(10);
  270. ret = mox_do_spi(NULL, dout, module_count + 1);
  271. mdelay(10);
  272. dm_gpio_set_value(reset_gpio, 0);
  273. mdelay(50);
  274. return ret;
  275. }
  276. static int get_reset_gpio(struct gpio_desc *reset_gpio)
  277. {
  278. int node;
  279. node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
  280. if (node < 0) {
  281. printf("Cannot find Moxtet bus device node!\n");
  282. return -1;
  283. }
  284. gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
  285. reset_gpio, GPIOD_IS_OUT);
  286. if (!dm_gpio_is_valid(reset_gpio)) {
  287. printf("Cannot find reset GPIO for Moxtet bus!\n");
  288. return -1;
  289. }
  290. return 0;
  291. }
  292. int misc_init_r(void)
  293. {
  294. int ret;
  295. u8 mac1[6], mac2[6];
  296. ret = mbox_sp_get_board_info(NULL, mac1, mac2, NULL, NULL);
  297. if (ret < 0) {
  298. printf("Cannot read data from OTP!\n");
  299. return 0;
  300. }
  301. if (is_valid_ethaddr(mac1) && !env_get("ethaddr"))
  302. eth_env_set_enetaddr("ethaddr", mac1);
  303. if (is_valid_ethaddr(mac2) && !env_get("eth1addr"))
  304. eth_env_set_enetaddr("eth1addr", mac2);
  305. return 0;
  306. }
  307. static void mox_phy_modify(struct phy_device *phydev, int page, int reg,
  308. u16 mask, u16 set)
  309. {
  310. int val;
  311. val = phydev->drv->readext(phydev, MDIO_DEVAD_NONE, page, reg);
  312. val &= ~mask;
  313. val |= set;
  314. phydev->drv->writeext(phydev, MDIO_DEVAD_NONE, page, reg, val);
  315. }
  316. static void mox_phy_leds_start_blinking(void)
  317. {
  318. struct phy_device *phydev;
  319. struct mii_dev *bus;
  320. bus = miiphy_get_dev_by_name("neta@30000");
  321. if (!bus) {
  322. printf("Cannot get MDIO bus device!\n");
  323. return;
  324. }
  325. phydev = phy_find_by_mask(bus, BIT(1), PHY_INTERFACE_MODE_RGMII);
  326. if (!phydev) {
  327. printf("Cannot get ethernet PHY!\n");
  328. return;
  329. }
  330. mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400);
  331. mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb);
  332. }
  333. static bool read_reset_button(void)
  334. {
  335. struct udevice *button, *led;
  336. int i;
  337. if (device_get_global_by_ofnode(ofnode_path(BUTTON_PATH), &button)) {
  338. printf("Cannot find reset button!\n");
  339. return false;
  340. }
  341. if (device_get_global_by_ofnode(ofnode_path(LED_PATH), &led)) {
  342. printf("Cannot find status LED!\n");
  343. return false;
  344. }
  345. led_set_state(led, LEDST_ON);
  346. for (i = 0; i < 21; ++i) {
  347. if (button_get_state(button) != BUTTON_ON)
  348. return false;
  349. if (i < 20)
  350. mdelay(50);
  351. }
  352. led_set_state(led, LEDST_OFF);
  353. return true;
  354. }
  355. static void handle_reset_button(void)
  356. {
  357. const char * const vars[1] = { "bootcmd_rescue", };
  358. /*
  359. * Ensure that bootcmd_rescue has always stock value, so that running
  360. * run bootcmd_rescue
  361. * always works correctly.
  362. */
  363. env_set_default_vars(1, (char * const *)vars, 0);
  364. if (read_reset_button()) {
  365. const char * const vars[2] = {
  366. "bootcmd",
  367. "distro_bootcmd",
  368. };
  369. /*
  370. * Set the above envs to their default values, in case the user
  371. * managed to break them.
  372. */
  373. env_set_default_vars(2, (char * const *)vars, 0);
  374. /* Ensure bootcmd_rescue is used by distroboot */
  375. env_set("boot_targets", "rescue");
  376. /* start blinking PHY LEDs */
  377. mox_phy_leds_start_blinking();
  378. printf("RESET button was pressed, overwriting boot_targets!\n");
  379. } else {
  380. /*
  381. * In case the user somehow managed to save environment with
  382. * boot_targets=rescue, reset boot_targets to default value.
  383. * This could happen in subsequent commands if bootcmd_rescue
  384. * failed.
  385. */
  386. if (!strcmp(env_get("boot_targets"), "rescue")) {
  387. const char * const vars[1] = {
  388. "boot_targets",
  389. };
  390. env_set_default_vars(1, (char * const *)vars, 0);
  391. }
  392. }
  393. }
  394. static void mox_print_info(void)
  395. {
  396. int ret, board_version, ram_size;
  397. u64 serial_number;
  398. const char *pub_key;
  399. ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
  400. &ram_size);
  401. if (ret < 0)
  402. return;
  403. printf("Turris Mox:\n");
  404. printf(" Board version: %i\n", board_version);
  405. printf(" RAM size: %i MiB\n", ram_size);
  406. printf(" Serial Number: %016llX\n", serial_number);
  407. pub_key = mox_sp_get_ecdsa_public_key();
  408. if (pub_key)
  409. printf(" ECDSA Public Key: %s\n", pub_key);
  410. else
  411. printf("Cannot read ECDSA Public Key\n");
  412. }
  413. int last_stage_init(void)
  414. {
  415. int ret, i;
  416. const u8 *topology;
  417. int is_sd;
  418. struct mii_dev *bus;
  419. struct gpio_desc reset_gpio = {};
  420. mox_print_info();
  421. ret = mox_get_topology(&topology, &module_count, &is_sd);
  422. if (ret) {
  423. printf("Cannot read module topology!\n");
  424. return 0;
  425. }
  426. printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
  427. if (module_count)
  428. printf("Module Topology:\n");
  429. for (i = 0; i < module_count; ++i) {
  430. switch (topology[i]) {
  431. case MOX_MODULE_SFP:
  432. printf("% 4i: SFP Module\n", i + 1);
  433. break;
  434. case MOX_MODULE_PCI:
  435. printf("% 4i: Mini-PCIe Module\n", i + 1);
  436. break;
  437. case MOX_MODULE_TOPAZ:
  438. printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
  439. break;
  440. case MOX_MODULE_PERIDOT:
  441. printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
  442. break;
  443. case MOX_MODULE_USB3:
  444. printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
  445. break;
  446. case MOX_MODULE_PASSPCI:
  447. printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
  448. break;
  449. default:
  450. printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
  451. }
  452. }
  453. /* now check if modules are connected in supported mode */
  454. for (i = 0; i < module_count; ++i) {
  455. switch (topology[i]) {
  456. case MOX_MODULE_SFP:
  457. if (sfp) {
  458. printf("Error: Only one SFP module is supported!\n");
  459. } else if (topaz) {
  460. printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
  461. } else {
  462. sfp_pos = i;
  463. ++sfp;
  464. }
  465. break;
  466. case MOX_MODULE_PCI:
  467. if (pci)
  468. printf("Error: Only one Mini-PCIe module is supported!\n");
  469. else if (usb)
  470. printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
  471. else if (i && (i != 1 || !passpci))
  472. printf("Error: Mini-PCIe module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
  473. else
  474. ++pci;
  475. break;
  476. case MOX_MODULE_TOPAZ:
  477. if (topaz)
  478. printf("Error: Only one Topaz module is supported!\n");
  479. else if (peridot >= 3)
  480. printf("Error: At most two Peridot modules can come before Topaz module!\n");
  481. else
  482. ++topaz;
  483. break;
  484. case MOX_MODULE_PERIDOT:
  485. if (sfp || topaz) {
  486. printf("Error: Peridot module must come before SFP or Topaz module!\n");
  487. } else if (peridot >= 3) {
  488. printf("Error: At most three Peridot modules are supported!\n");
  489. } else {
  490. peridot_pos[peridot] = i;
  491. ++peridot;
  492. }
  493. break;
  494. case MOX_MODULE_USB3:
  495. if (pci)
  496. printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
  497. else if (usb)
  498. printf("Error: Only one USB 3.0 module is supported!\n");
  499. else if (i && (i != 1 || !passpci))
  500. printf("Error: USB 3.0 module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
  501. else
  502. ++usb;
  503. break;
  504. case MOX_MODULE_PASSPCI:
  505. if (passpci)
  506. printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
  507. else if (i != 0)
  508. printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
  509. else
  510. ++passpci;
  511. }
  512. }
  513. /* now configure modules */
  514. if (get_reset_gpio(&reset_gpio) < 0)
  515. return 0;
  516. if (peridot > 0) {
  517. if (configure_peridots(&reset_gpio) < 0) {
  518. printf("Cannot configure Peridot modules!\n");
  519. peridot = 0;
  520. }
  521. } else {
  522. dm_gpio_set_value(&reset_gpio, 1);
  523. mdelay(50);
  524. dm_gpio_set_value(&reset_gpio, 0);
  525. mdelay(50);
  526. }
  527. if (peridot || topaz) {
  528. /*
  529. * now check if the addresses are set by reading Scratch & Misc
  530. * register 0x70 of Peridot (and potentially Topaz) modules
  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. if (topaz)
  539. check_switch_address(bus, 0x2);
  540. sw_blink_leds(bus, peridot, topaz);
  541. }
  542. }
  543. printf("\n");
  544. handle_reset_button();
  545. return 0;
  546. }
  547. #if defined(CONFIG_OF_BOARD_SETUP)
  548. static int vnode_by_path(void *blob, const char *fmt, va_list ap)
  549. {
  550. char path[128];
  551. vsnprintf(path, 128, fmt, ap);
  552. return fdt_path_offset(blob, path);
  553. }
  554. static int node_by_path(void *blob, const char *fmt, ...)
  555. {
  556. va_list ap;
  557. int res;
  558. va_start(ap, fmt);
  559. res = vnode_by_path(blob, fmt, ap);
  560. va_end(ap);
  561. return res;
  562. }
  563. static int phandle_by_path(void *blob, const char *fmt, ...)
  564. {
  565. va_list ap;
  566. int node, phandle, res;
  567. va_start(ap, fmt);
  568. node = vnode_by_path(blob, fmt, ap);
  569. va_end(ap);
  570. if (node < 0)
  571. return node;
  572. phandle = fdt_get_phandle(blob, node);
  573. if (phandle > 0)
  574. return phandle;
  575. phandle = fdt_get_max_phandle(blob);
  576. if (phandle < 0)
  577. return phandle;
  578. phandle += 1;
  579. res = fdt_setprop_u32(blob, node, "linux,phandle", phandle);
  580. if (res < 0)
  581. return res;
  582. res = fdt_setprop_u32(blob, node, "phandle", phandle);
  583. if (res < 0)
  584. return res;
  585. return phandle;
  586. }
  587. static int enable_by_path(void *blob, const char *fmt, ...)
  588. {
  589. va_list ap;
  590. int node;
  591. va_start(ap, fmt);
  592. node = vnode_by_path(blob, fmt, ap);
  593. va_end(ap);
  594. if (node < 0)
  595. return node;
  596. return fdt_setprop_string(blob, node, "status", "okay");
  597. }
  598. static bool is_topaz(int id)
  599. {
  600. return topaz && id == peridot + topaz - 1;
  601. }
  602. static int switch_addr(int id)
  603. {
  604. return is_topaz(id) ? 0x2 : 0x10 + id;
  605. }
  606. static int setup_switch(void *blob, int id)
  607. {
  608. int res, addr, i, node, phandle;
  609. addr = switch_addr(id);
  610. /* first enable the switch by setting status = "okay" */
  611. res = enable_by_path(blob, MDIO_PATH "/switch%i@%x", id, addr);
  612. if (res < 0)
  613. return res;
  614. /*
  615. * now if there are more switches or a SFP module coming after,
  616. * enable corresponding ports
  617. */
  618. if (id < peridot + topaz - 1) {
  619. res = enable_by_path(blob,
  620. MDIO_PATH "/switch%i@%x/ports/port@a",
  621. id, addr);
  622. } else if (id == peridot - 1 && !topaz && sfp) {
  623. res = enable_by_path(blob,
  624. MDIO_PATH "/switch%i@%x/ports/port-sfp@a",
  625. id, addr);
  626. } else {
  627. res = 0;
  628. }
  629. if (res < 0)
  630. return res;
  631. if (id >= peridot + topaz - 1)
  632. return 0;
  633. /* finally change link property if needed */
  634. node = node_by_path(blob, MDIO_PATH "/switch%i@%x/ports/port@a", id,
  635. addr);
  636. if (node < 0)
  637. return node;
  638. for (i = id + 1; i < peridot + topaz; ++i) {
  639. phandle = phandle_by_path(blob,
  640. MDIO_PATH "/switch%i@%x/ports/port@%x",
  641. i, switch_addr(i),
  642. is_topaz(i) ? 5 : 9);
  643. if (phandle < 0)
  644. return phandle;
  645. if (i == id + 1)
  646. res = fdt_setprop_u32(blob, node, "link", phandle);
  647. else
  648. res = fdt_appendprop_u32(blob, node, "link", phandle);
  649. if (res < 0)
  650. return res;
  651. }
  652. return 0;
  653. }
  654. static int remove_disabled_nodes(void *blob)
  655. {
  656. while (1) {
  657. int res, offset;
  658. offset = fdt_node_offset_by_prop_value(blob, -1, "status",
  659. "disabled", 9);
  660. if (offset < 0)
  661. break;
  662. res = fdt_del_node(blob, offset);
  663. if (res < 0)
  664. return res;
  665. }
  666. return 0;
  667. }
  668. int ft_board_setup(void *blob, struct bd_info *bd)
  669. {
  670. int node, phandle, res;
  671. /*
  672. * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
  673. * connected, enable the PCIe node.
  674. */
  675. if (pci || usb || passpci) {
  676. node = fdt_path_offset(blob, PCIE_PATH);
  677. if (node < 0)
  678. return node;
  679. res = fdt_setprop_string(blob, node, "status", "okay");
  680. if (res < 0)
  681. return res;
  682. /* Fix PCIe regions for devices with 4 GB RAM */
  683. res = a3700_fdt_fix_pcie_regions(blob);
  684. if (res < 0)
  685. return res;
  686. }
  687. /*
  688. * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
  689. * enable the eth1 node and setup the switches.
  690. */
  691. if (peridot || topaz) {
  692. int i;
  693. res = enable_by_path(blob, ETH1_PATH);
  694. if (res < 0)
  695. return res;
  696. for (i = 0; i < peridot + topaz; ++i) {
  697. res = setup_switch(blob, i);
  698. if (res < 0)
  699. return res;
  700. }
  701. }
  702. /*
  703. * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
  704. * node. If there is no Peridot switch between MOX A and MOX D, add link
  705. * to the SFP node to eth1 node.
  706. * Also enable and configure SFP GPIO controller node.
  707. */
  708. if (sfp) {
  709. res = enable_by_path(blob, SFP_PATH);
  710. if (res < 0)
  711. return res;
  712. res = enable_by_path(blob, ETH1_PATH);
  713. if (res < 0)
  714. return res;
  715. if (!peridot) {
  716. phandle = phandle_by_path(blob, SFP_PATH);
  717. if (phandle < 0)
  718. return res;
  719. node = node_by_path(blob, ETH1_PATH);
  720. if (node < 0)
  721. return node;
  722. res = fdt_setprop_u32(blob, node, "sfp", phandle);
  723. if (res < 0)
  724. return res;
  725. res = fdt_setprop_string(blob, node, "phy-mode",
  726. "sgmii");
  727. if (res < 0)
  728. return res;
  729. }
  730. res = enable_by_path(blob, SFP_GPIO_PATH);
  731. if (res < 0)
  732. return res;
  733. if (sfp_pos) {
  734. char newname[16];
  735. /* moxtet-sfp is on non-zero position, change default */
  736. node = node_by_path(blob, SFP_GPIO_PATH);
  737. if (node < 0)
  738. return node;
  739. res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
  740. if (res < 0)
  741. return res;
  742. sprintf(newname, "gpio@%x", sfp_pos);
  743. res = fdt_set_name(blob, node, newname);
  744. if (res < 0)
  745. return res;
  746. }
  747. }
  748. fdt_fixup_ethernet(blob);
  749. /* Finally remove disabled nodes, as per Rob Herring's request. */
  750. remove_disabled_nodes(blob);
  751. return 0;
  752. }
  753. #endif