platform.c 11 KB

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