platform.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2006,2007 Felix Fietkau <nbd@openwrt.org>
  4. * Copyright (C) 2006,2007 Eugene Konev <ejka@openwrt.org>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/types.h>
  8. #include <linux/delay.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/mtd/physmap.h>
  12. #include <linux/serial.h>
  13. #include <linux/serial_8250.h>
  14. #include <linux/ioport.h>
  15. #include <linux/io.h>
  16. #include <linux/vlynq.h>
  17. #include <linux/leds.h>
  18. #include <linux/string.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/phy.h>
  21. #include <linux/phy_fixed.h>
  22. #include <linux/gpio.h>
  23. #include <linux/clk.h>
  24. #include <asm/addrspace.h>
  25. #include <asm/mach-ar7/ar7.h>
  26. #include <asm/mach-ar7/prom.h>
  27. /*****************************************************************************
  28. * VLYNQ Bus
  29. ****************************************************************************/
  30. struct plat_vlynq_data {
  31. struct plat_vlynq_ops ops;
  32. int gpio_bit;
  33. int reset_bit;
  34. };
  35. static int vlynq_on(struct vlynq_device *dev)
  36. {
  37. int ret;
  38. struct plat_vlynq_data *pdata = dev->dev.platform_data;
  39. ret = gpio_request(pdata->gpio_bit, "vlynq");
  40. if (ret)
  41. goto out;
  42. ar7_device_reset(pdata->reset_bit);
  43. ret = ar7_gpio_disable(pdata->gpio_bit);
  44. if (ret)
  45. goto out_enabled;
  46. ret = ar7_gpio_enable(pdata->gpio_bit);
  47. if (ret)
  48. goto out_enabled;
  49. ret = gpio_direction_output(pdata->gpio_bit, 0);
  50. if (ret)
  51. goto out_gpio_enabled;
  52. msleep(50);
  53. gpio_set_value(pdata->gpio_bit, 1);
  54. msleep(50);
  55. return 0;
  56. out_gpio_enabled:
  57. ar7_gpio_disable(pdata->gpio_bit);
  58. out_enabled:
  59. ar7_device_disable(pdata->reset_bit);
  60. gpio_free(pdata->gpio_bit);
  61. out:
  62. return ret;
  63. }
  64. static void vlynq_off(struct vlynq_device *dev)
  65. {
  66. struct plat_vlynq_data *pdata = dev->dev.platform_data;
  67. ar7_gpio_disable(pdata->gpio_bit);
  68. gpio_free(pdata->gpio_bit);
  69. ar7_device_disable(pdata->reset_bit);
  70. }
  71. static struct resource vlynq_low_res[] = {
  72. {
  73. .name = "regs",
  74. .flags = IORESOURCE_MEM,
  75. .start = AR7_REGS_VLYNQ0,
  76. .end = AR7_REGS_VLYNQ0 + 0xff,
  77. },
  78. {
  79. .name = "irq",
  80. .flags = IORESOURCE_IRQ,
  81. .start = 29,
  82. .end = 29,
  83. },
  84. {
  85. .name = "mem",
  86. .flags = IORESOURCE_MEM,
  87. .start = 0x04000000,
  88. .end = 0x04ffffff,
  89. },
  90. {
  91. .name = "devirq",
  92. .flags = IORESOURCE_IRQ,
  93. .start = 80,
  94. .end = 111,
  95. },
  96. };
  97. static struct resource vlynq_high_res[] = {
  98. {
  99. .name = "regs",
  100. .flags = IORESOURCE_MEM,
  101. .start = AR7_REGS_VLYNQ1,
  102. .end = AR7_REGS_VLYNQ1 + 0xff,
  103. },
  104. {
  105. .name = "irq",
  106. .flags = IORESOURCE_IRQ,
  107. .start = 33,
  108. .end = 33,
  109. },
  110. {
  111. .name = "mem",
  112. .flags = IORESOURCE_MEM,
  113. .start = 0x0c000000,
  114. .end = 0x0cffffff,
  115. },
  116. {
  117. .name = "devirq",
  118. .flags = IORESOURCE_IRQ,
  119. .start = 112,
  120. .end = 143,
  121. },
  122. };
  123. static struct plat_vlynq_data vlynq_low_data = {
  124. .ops = {
  125. .on = vlynq_on,
  126. .off = vlynq_off,
  127. },
  128. .reset_bit = 20,
  129. .gpio_bit = 18,
  130. };
  131. static struct plat_vlynq_data vlynq_high_data = {
  132. .ops = {
  133. .on = vlynq_on,
  134. .off = vlynq_off,
  135. },
  136. .reset_bit = 16,
  137. .gpio_bit = 19,
  138. };
  139. static struct platform_device vlynq_low = {
  140. .id = 0,
  141. .name = "vlynq",
  142. .dev = {
  143. .platform_data = &vlynq_low_data,
  144. },
  145. .resource = vlynq_low_res,
  146. .num_resources = ARRAY_SIZE(vlynq_low_res),
  147. };
  148. static struct platform_device vlynq_high = {
  149. .id = 1,
  150. .name = "vlynq",
  151. .dev = {
  152. .platform_data = &vlynq_high_data,
  153. },
  154. .resource = vlynq_high_res,
  155. .num_resources = ARRAY_SIZE(vlynq_high_res),
  156. };
  157. /*****************************************************************************
  158. * Flash
  159. ****************************************************************************/
  160. static struct resource physmap_flash_resource = {
  161. .name = "mem",
  162. .flags = IORESOURCE_MEM,
  163. .start = 0x10000000,
  164. .end = 0x107fffff,
  165. };
  166. static const char *ar7_probe_types[] = { "ar7part", NULL };
  167. static struct physmap_flash_data physmap_flash_data = {
  168. .width = 2,
  169. .part_probe_types = ar7_probe_types,
  170. };
  171. static struct platform_device physmap_flash = {
  172. .name = "physmap-flash",
  173. .dev = {
  174. .platform_data = &physmap_flash_data,
  175. },
  176. .resource = &physmap_flash_resource,
  177. .num_resources = 1,
  178. };
  179. /*****************************************************************************
  180. * Ethernet
  181. ****************************************************************************/
  182. static struct resource cpmac_low_res[] = {
  183. {
  184. .name = "regs",
  185. .flags = IORESOURCE_MEM,
  186. .start = AR7_REGS_MAC0,
  187. .end = AR7_REGS_MAC0 + 0x7ff,
  188. },
  189. {
  190. .name = "irq",
  191. .flags = IORESOURCE_IRQ,
  192. .start = 27,
  193. .end = 27,
  194. },
  195. };
  196. static struct resource cpmac_high_res[] = {
  197. {
  198. .name = "regs",
  199. .flags = IORESOURCE_MEM,
  200. .start = AR7_REGS_MAC1,
  201. .end = AR7_REGS_MAC1 + 0x7ff,
  202. },
  203. {
  204. .name = "irq",
  205. .flags = IORESOURCE_IRQ,
  206. .start = 41,
  207. .end = 41,
  208. },
  209. };
  210. static struct fixed_phy_status fixed_phy_status __initdata = {
  211. .link = 1,
  212. .speed = 100,
  213. .duplex = 1,
  214. };
  215. static struct plat_cpmac_data cpmac_low_data = {
  216. .reset_bit = 17,
  217. .power_bit = 20,
  218. .phy_mask = 0x80000000,
  219. };
  220. static struct plat_cpmac_data cpmac_high_data = {
  221. .reset_bit = 21,
  222. .power_bit = 22,
  223. .phy_mask = 0x7fffffff,
  224. };
  225. static u64 cpmac_dma_mask = DMA_BIT_MASK(32);
  226. static struct platform_device cpmac_low = {
  227. .id = 0,
  228. .name = "cpmac",
  229. .dev = {
  230. .dma_mask = &cpmac_dma_mask,
  231. .coherent_dma_mask = DMA_BIT_MASK(32),
  232. .platform_data = &cpmac_low_data,
  233. },
  234. .resource = cpmac_low_res,
  235. .num_resources = ARRAY_SIZE(cpmac_low_res),
  236. };
  237. static struct platform_device cpmac_high = {
  238. .id = 1,
  239. .name = "cpmac",
  240. .dev = {
  241. .dma_mask = &cpmac_dma_mask,
  242. .coherent_dma_mask = DMA_BIT_MASK(32),
  243. .platform_data = &cpmac_high_data,
  244. },
  245. .resource = cpmac_high_res,
  246. .num_resources = ARRAY_SIZE(cpmac_high_res),
  247. };
  248. static void __init cpmac_get_mac(int instance, unsigned char *dev_addr)
  249. {
  250. char name[5], *mac;
  251. sprintf(name, "mac%c", 'a' + instance);
  252. mac = prom_getenv(name);
  253. if (!mac && instance) {
  254. sprintf(name, "mac%c", 'a');
  255. mac = prom_getenv(name);
  256. }
  257. if (mac) {
  258. if (!mac_pton(mac, dev_addr)) {
  259. pr_warn("cannot parse mac address, using random address\n");
  260. eth_random_addr(dev_addr);
  261. }
  262. } else
  263. eth_random_addr(dev_addr);
  264. }
  265. /*****************************************************************************
  266. * USB
  267. ****************************************************************************/
  268. static struct resource usb_res[] = {
  269. {
  270. .name = "regs",
  271. .flags = IORESOURCE_MEM,
  272. .start = AR7_REGS_USB,
  273. .end = AR7_REGS_USB + 0xff,
  274. },
  275. {
  276. .name = "irq",
  277. .flags = IORESOURCE_IRQ,
  278. .start = 32,
  279. .end = 32,
  280. },
  281. {
  282. .name = "mem",
  283. .flags = IORESOURCE_MEM,
  284. .start = 0x03400000,
  285. .end = 0x03401fff,
  286. },
  287. };
  288. static struct platform_device ar7_udc = {
  289. .name = "ar7_udc",
  290. .resource = usb_res,
  291. .num_resources = ARRAY_SIZE(usb_res),
  292. };
  293. /*****************************************************************************
  294. * LEDs
  295. ****************************************************************************/
  296. static const struct gpio_led default_leds[] = {
  297. {
  298. .name = "status",
  299. .gpio = 8,
  300. .active_low = 1,
  301. },
  302. };
  303. static const struct gpio_led titan_leds[] = {
  304. { .name = "status", .gpio = 8, .active_low = 1, },
  305. { .name = "wifi", .gpio = 13, .active_low = 1, },
  306. };
  307. static const struct gpio_led dsl502t_leds[] = {
  308. {
  309. .name = "status",
  310. .gpio = 9,
  311. .active_low = 1,
  312. },
  313. {
  314. .name = "ethernet",
  315. .gpio = 7,
  316. .active_low = 1,
  317. },
  318. {
  319. .name = "usb",
  320. .gpio = 12,
  321. .active_low = 1,
  322. },
  323. };
  324. static const struct gpio_led dg834g_leds[] = {
  325. {
  326. .name = "ppp",
  327. .gpio = 6,
  328. .active_low = 1,
  329. },
  330. {
  331. .name = "status",
  332. .gpio = 7,
  333. .active_low = 1,
  334. },
  335. {
  336. .name = "adsl",
  337. .gpio = 8,
  338. .active_low = 1,
  339. },
  340. {
  341. .name = "wifi",
  342. .gpio = 12,
  343. .active_low = 1,
  344. },
  345. {
  346. .name = "power",
  347. .gpio = 14,
  348. .active_low = 1,
  349. .default_trigger = "default-on",
  350. },
  351. };
  352. static const struct gpio_led fb_sl_leds[] = {
  353. {
  354. .name = "1",
  355. .gpio = 7,
  356. },
  357. {
  358. .name = "2",
  359. .gpio = 13,
  360. .active_low = 1,
  361. },
  362. {
  363. .name = "3",
  364. .gpio = 10,
  365. .active_low = 1,
  366. },
  367. {
  368. .name = "4",
  369. .gpio = 12,
  370. .active_low = 1,
  371. },
  372. {
  373. .name = "5",
  374. .gpio = 9,
  375. .active_low = 1,
  376. },
  377. };
  378. static const struct gpio_led fb_fon_leds[] = {
  379. {
  380. .name = "1",
  381. .gpio = 8,
  382. },
  383. {
  384. .name = "2",
  385. .gpio = 3,
  386. .active_low = 1,
  387. },
  388. {
  389. .name = "3",
  390. .gpio = 5,
  391. },
  392. {
  393. .name = "4",
  394. .gpio = 4,
  395. .active_low = 1,
  396. },
  397. {
  398. .name = "5",
  399. .gpio = 11,
  400. .active_low = 1,
  401. },
  402. };
  403. static const struct gpio_led gt701_leds[] = {
  404. {
  405. .name = "inet:green",
  406. .gpio = 13,
  407. .active_low = 1,
  408. },
  409. {
  410. .name = "usb",
  411. .gpio = 12,
  412. .active_low = 1,
  413. },
  414. {
  415. .name = "inet:red",
  416. .gpio = 9,
  417. .active_low = 1,
  418. },
  419. {
  420. .name = "power:red",
  421. .gpio = 7,
  422. .active_low = 1,
  423. },
  424. {
  425. .name = "power:green",
  426. .gpio = 8,
  427. .active_low = 1,
  428. .default_trigger = "default-on",
  429. },
  430. {
  431. .name = "ethernet",
  432. .gpio = 10,
  433. .active_low = 1,
  434. },
  435. };
  436. static struct gpio_led_platform_data ar7_led_data;
  437. static struct platform_device ar7_gpio_leds = {
  438. .name = "leds-gpio",
  439. .dev = {
  440. .platform_data = &ar7_led_data,
  441. }
  442. };
  443. static void __init detect_leds(void)
  444. {
  445. char *prid, *usb_prod;
  446. /* Default LEDs */
  447. ar7_led_data.num_leds = ARRAY_SIZE(default_leds);
  448. ar7_led_data.leds = default_leds;
  449. /* FIXME: the whole thing is unreliable */
  450. prid = prom_getenv("ProductID");
  451. usb_prod = prom_getenv("usb_prod");
  452. /* If we can't get the product id from PROM, use the default LEDs */
  453. if (!prid)
  454. return;
  455. if (strstr(prid, "Fritz_Box_FON")) {
  456. ar7_led_data.num_leds = ARRAY_SIZE(fb_fon_leds);
  457. ar7_led_data.leds = fb_fon_leds;
  458. } else if (strstr(prid, "Fritz_Box_")) {
  459. ar7_led_data.num_leds = ARRAY_SIZE(fb_sl_leds);
  460. ar7_led_data.leds = fb_sl_leds;
  461. } else if ((!strcmp(prid, "AR7RD") || !strcmp(prid, "AR7DB"))
  462. && usb_prod != NULL && strstr(usb_prod, "DSL-502T")) {
  463. ar7_led_data.num_leds = ARRAY_SIZE(dsl502t_leds);
  464. ar7_led_data.leds = dsl502t_leds;
  465. } else if (strstr(prid, "DG834")) {
  466. ar7_led_data.num_leds = ARRAY_SIZE(dg834g_leds);
  467. ar7_led_data.leds = dg834g_leds;
  468. } else if (strstr(prid, "CYWM") || strstr(prid, "CYWL")) {
  469. ar7_led_data.num_leds = ARRAY_SIZE(titan_leds);
  470. ar7_led_data.leds = titan_leds;
  471. } else if (strstr(prid, "GT701")) {
  472. ar7_led_data.num_leds = ARRAY_SIZE(gt701_leds);
  473. ar7_led_data.leds = gt701_leds;
  474. }
  475. }
  476. /*****************************************************************************
  477. * Watchdog
  478. ****************************************************************************/
  479. static struct resource ar7_wdt_res = {
  480. .name = "regs",
  481. .flags = IORESOURCE_MEM,
  482. .start = -1, /* Filled at runtime */
  483. .end = -1, /* Filled at runtime */
  484. };
  485. static struct platform_device ar7_wdt = {
  486. .name = "ar7_wdt",
  487. .resource = &ar7_wdt_res,
  488. .num_resources = 1,
  489. };
  490. /*****************************************************************************
  491. * Init
  492. ****************************************************************************/
  493. static int __init ar7_register_uarts(void)
  494. {
  495. #ifdef CONFIG_SERIAL_8250
  496. static struct uart_port uart_port __initdata;
  497. struct clk *bus_clk;
  498. int res;
  499. memset(&uart_port, 0, sizeof(struct uart_port));
  500. bus_clk = clk_get(NULL, "bus");
  501. if (IS_ERR(bus_clk))
  502. panic("unable to get bus clk");
  503. uart_port.type = PORT_AR7;
  504. uart_port.uartclk = clk_get_rate(bus_clk) / 2;
  505. uart_port.iotype = UPIO_MEM32;
  506. uart_port.flags = UPF_FIXED_TYPE | UPF_BOOT_AUTOCONF;
  507. uart_port.regshift = 2;
  508. uart_port.line = 0;
  509. uart_port.irq = AR7_IRQ_UART0;
  510. uart_port.mapbase = AR7_REGS_UART0;
  511. uart_port.membase = ioremap(uart_port.mapbase, 256);
  512. res = early_serial_setup(&uart_port);
  513. if (res)
  514. return res;
  515. /* Only TNETD73xx have a second serial port */
  516. if (ar7_has_second_uart()) {
  517. uart_port.line = 1;
  518. uart_port.irq = AR7_IRQ_UART1;
  519. uart_port.mapbase = UR8_REGS_UART1;
  520. uart_port.membase = ioremap(uart_port.mapbase, 256);
  521. res = early_serial_setup(&uart_port);
  522. if (res)
  523. return res;
  524. }
  525. #endif
  526. return 0;
  527. }
  528. static void __init titan_fixup_devices(void)
  529. {
  530. /* Set vlynq0 data */
  531. vlynq_low_data.reset_bit = 15;
  532. vlynq_low_data.gpio_bit = 14;
  533. /* Set vlynq1 data */
  534. vlynq_high_data.reset_bit = 16;
  535. vlynq_high_data.gpio_bit = 7;
  536. /* Set vlynq0 resources */
  537. vlynq_low_res[0].start = TITAN_REGS_VLYNQ0;
  538. vlynq_low_res[0].end = TITAN_REGS_VLYNQ0 + 0xff;
  539. vlynq_low_res[1].start = 33;
  540. vlynq_low_res[1].end = 33;
  541. vlynq_low_res[2].start = 0x0c000000;
  542. vlynq_low_res[2].end = 0x0fffffff;
  543. vlynq_low_res[3].start = 80;
  544. vlynq_low_res[3].end = 111;
  545. /* Set vlynq1 resources */
  546. vlynq_high_res[0].start = TITAN_REGS_VLYNQ1;
  547. vlynq_high_res[0].end = TITAN_REGS_VLYNQ1 + 0xff;
  548. vlynq_high_res[1].start = 34;
  549. vlynq_high_res[1].end = 34;
  550. vlynq_high_res[2].start = 0x40000000;
  551. vlynq_high_res[2].end = 0x43ffffff;
  552. vlynq_high_res[3].start = 112;
  553. vlynq_high_res[3].end = 143;
  554. /* Set cpmac0 data */
  555. cpmac_low_data.phy_mask = 0x40000000;
  556. /* Set cpmac1 data */
  557. cpmac_high_data.phy_mask = 0x80000000;
  558. /* Set cpmac0 resources */
  559. cpmac_low_res[0].start = TITAN_REGS_MAC0;
  560. cpmac_low_res[0].end = TITAN_REGS_MAC0 + 0x7ff;
  561. /* Set cpmac1 resources */
  562. cpmac_high_res[0].start = TITAN_REGS_MAC1;
  563. cpmac_high_res[0].end = TITAN_REGS_MAC1 + 0x7ff;
  564. }
  565. static int __init ar7_register_devices(void)
  566. {
  567. void __iomem *bootcr;
  568. u32 val;
  569. int res;
  570. res = ar7_gpio_init();
  571. if (res)
  572. pr_warn("unable to register gpios: %d\n", res);
  573. res = ar7_register_uarts();
  574. if (res)
  575. pr_err("unable to setup uart(s): %d\n", res);
  576. res = platform_device_register(&physmap_flash);
  577. if (res)
  578. pr_warn("unable to register physmap-flash: %d\n", res);
  579. if (ar7_is_titan())
  580. titan_fixup_devices();
  581. ar7_device_disable(vlynq_low_data.reset_bit);
  582. res = platform_device_register(&vlynq_low);
  583. if (res)
  584. pr_warn("unable to register vlynq-low: %d\n", res);
  585. if (ar7_has_high_vlynq()) {
  586. ar7_device_disable(vlynq_high_data.reset_bit);
  587. res = platform_device_register(&vlynq_high);
  588. if (res)
  589. pr_warn("unable to register vlynq-high: %d\n", res);
  590. }
  591. if (ar7_has_high_cpmac()) {
  592. res = fixed_phy_add(PHY_POLL, cpmac_high.id,
  593. &fixed_phy_status);
  594. if (!res) {
  595. cpmac_get_mac(1, cpmac_high_data.dev_addr);
  596. res = platform_device_register(&cpmac_high);
  597. if (res)
  598. pr_warn("unable to register cpmac-high: %d\n",
  599. res);
  600. } else
  601. pr_warn("unable to add cpmac-high phy: %d\n", res);
  602. } else
  603. cpmac_low_data.phy_mask = 0xffffffff;
  604. res = fixed_phy_add(PHY_POLL, cpmac_low.id, &fixed_phy_status);
  605. if (!res) {
  606. cpmac_get_mac(0, cpmac_low_data.dev_addr);
  607. res = platform_device_register(&cpmac_low);
  608. if (res)
  609. pr_warn("unable to register cpmac-low: %d\n", res);
  610. } else
  611. pr_warn("unable to add cpmac-low phy: %d\n", res);
  612. detect_leds();
  613. res = platform_device_register(&ar7_gpio_leds);
  614. if (res)
  615. pr_warn("unable to register leds: %d\n", res);
  616. res = platform_device_register(&ar7_udc);
  617. if (res)
  618. pr_warn("unable to register usb slave: %d\n", res);
  619. /* Register watchdog only if enabled in hardware */
  620. bootcr = ioremap(AR7_REGS_DCL, 4);
  621. val = readl(bootcr);
  622. iounmap(bootcr);
  623. if (val & AR7_WDT_HW_ENA) {
  624. if (ar7_has_high_vlynq())
  625. ar7_wdt_res.start = UR8_REGS_WDT;
  626. else
  627. ar7_wdt_res.start = AR7_REGS_WDT;
  628. ar7_wdt_res.end = ar7_wdt_res.start + 0x20;
  629. res = platform_device_register(&ar7_wdt);
  630. if (res)
  631. pr_warn("unable to register watchdog: %d\n", res);
  632. }
  633. return 0;
  634. }
  635. device_initcall(ar7_register_devices);