platform.c 14 KB

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