platform.c 11 KB

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