platform.c 12 KB

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