ts209-setup.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * QNAP TS-109/TS-209 Board Setup
  3. *
  4. * Maintainer: Byron Bradley <byron.bbradley@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pci.h>
  15. #include <linux/irq.h>
  16. #include <linux/mtd/physmap.h>
  17. #include <linux/mtd/nand.h>
  18. #include <linux/mv643xx_eth.h>
  19. #include <linux/gpio_keys.h>
  20. #include <linux/input.h>
  21. #include <linux/i2c.h>
  22. #include <linux/serial_reg.h>
  23. #include <linux/ata_platform.h>
  24. #include <asm/mach-types.h>
  25. #include <asm/gpio.h>
  26. #include <asm/mach/arch.h>
  27. #include <asm/mach/pci.h>
  28. #include <asm/arch/orion5x.h>
  29. #include "common.h"
  30. #include "mpp.h"
  31. #define QNAP_TS209_NOR_BOOT_BASE 0xf4000000
  32. #define QNAP_TS209_NOR_BOOT_SIZE SZ_8M
  33. /****************************************************************************
  34. * 8MiB NOR flash. The struct mtd_partition is not in the same order as the
  35. * partitions on the device because we want to keep compatability with
  36. * existing QNAP firmware.
  37. *
  38. * Layout as used by QNAP:
  39. * [2] 0x00000000-0x00200000 : "Kernel"
  40. * [3] 0x00200000-0x00600000 : "RootFS1"
  41. * [4] 0x00600000-0x00700000 : "RootFS2"
  42. * [6] 0x00700000-0x00760000 : "NAS Config" (read-only)
  43. * [5] 0x00760000-0x00780000 : "U-Boot Config"
  44. * [1] 0x00780000-0x00800000 : "U-Boot" (read-only)
  45. ***************************************************************************/
  46. static struct mtd_partition qnap_ts209_partitions[] = {
  47. {
  48. .name = "U-Boot",
  49. .size = 0x00080000,
  50. .offset = 0x00780000,
  51. .mask_flags = MTD_WRITEABLE,
  52. }, {
  53. .name = "Kernel",
  54. .size = 0x00200000,
  55. .offset = 0,
  56. }, {
  57. .name = "RootFS1",
  58. .size = 0x00400000,
  59. .offset = 0x00200000,
  60. }, {
  61. .name = "RootFS2",
  62. .size = 0x00100000,
  63. .offset = 0x00600000,
  64. }, {
  65. .name = "U-Boot Config",
  66. .size = 0x00020000,
  67. .offset = 0x00760000,
  68. }, {
  69. .name = "NAS Config",
  70. .size = 0x00060000,
  71. .offset = 0x00700000,
  72. .mask_flags = MTD_WRITEABLE,
  73. },
  74. };
  75. static struct physmap_flash_data qnap_ts209_nor_flash_data = {
  76. .width = 1,
  77. .parts = qnap_ts209_partitions,
  78. .nr_parts = ARRAY_SIZE(qnap_ts209_partitions)
  79. };
  80. static struct resource qnap_ts209_nor_flash_resource = {
  81. .flags = IORESOURCE_MEM,
  82. .start = QNAP_TS209_NOR_BOOT_BASE,
  83. .end = QNAP_TS209_NOR_BOOT_BASE + QNAP_TS209_NOR_BOOT_SIZE - 1,
  84. };
  85. static struct platform_device qnap_ts209_nor_flash = {
  86. .name = "physmap-flash",
  87. .id = 0,
  88. .dev = {
  89. .platform_data = &qnap_ts209_nor_flash_data,
  90. },
  91. .resource = &qnap_ts209_nor_flash_resource,
  92. .num_resources = 1,
  93. };
  94. /*****************************************************************************
  95. * PCI
  96. ****************************************************************************/
  97. #define QNAP_TS209_PCI_SLOT0_OFFS 7
  98. #define QNAP_TS209_PCI_SLOT0_IRQ_PIN 6
  99. #define QNAP_TS209_PCI_SLOT1_IRQ_PIN 7
  100. void __init qnap_ts209_pci_preinit(void)
  101. {
  102. int pin;
  103. /*
  104. * Configure PCI GPIO IRQ pins
  105. */
  106. pin = QNAP_TS209_PCI_SLOT0_IRQ_PIN;
  107. if (gpio_request(pin, "PCI Int1") == 0) {
  108. if (gpio_direction_input(pin) == 0) {
  109. set_irq_type(gpio_to_irq(pin), IRQT_LOW);
  110. } else {
  111. printk(KERN_ERR "qnap_ts209_pci_preinit failed to "
  112. "set_irq_type pin %d\n", pin);
  113. gpio_free(pin);
  114. }
  115. } else {
  116. printk(KERN_ERR "qnap_ts209_pci_preinit failed to gpio_request "
  117. "%d\n", pin);
  118. }
  119. pin = QNAP_TS209_PCI_SLOT1_IRQ_PIN;
  120. if (gpio_request(pin, "PCI Int2") == 0) {
  121. if (gpio_direction_input(pin) == 0) {
  122. set_irq_type(gpio_to_irq(pin), IRQT_LOW);
  123. } else {
  124. printk(KERN_ERR "qnap_ts209_pci_preinit failed "
  125. "to set_irq_type pin %d\n", pin);
  126. gpio_free(pin);
  127. }
  128. } else {
  129. printk(KERN_ERR "qnap_ts209_pci_preinit failed to gpio_request "
  130. "%d\n", pin);
  131. }
  132. }
  133. static int __init qnap_ts209_pci_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
  134. {
  135. int irq;
  136. /*
  137. * Check for devices with hard-wired IRQs.
  138. */
  139. irq = orion5x_pci_map_irq(dev, slot, pin);
  140. if (irq != -1)
  141. return irq;
  142. /*
  143. * PCI IRQs are connected via GPIOs.
  144. */
  145. switch (slot - QNAP_TS209_PCI_SLOT0_OFFS) {
  146. case 0:
  147. return gpio_to_irq(QNAP_TS209_PCI_SLOT0_IRQ_PIN);
  148. case 1:
  149. return gpio_to_irq(QNAP_TS209_PCI_SLOT1_IRQ_PIN);
  150. default:
  151. return -1;
  152. }
  153. }
  154. static struct hw_pci qnap_ts209_pci __initdata = {
  155. .nr_controllers = 2,
  156. .preinit = qnap_ts209_pci_preinit,
  157. .swizzle = pci_std_swizzle,
  158. .setup = orion5x_pci_sys_setup,
  159. .scan = orion5x_pci_sys_scan_bus,
  160. .map_irq = qnap_ts209_pci_map_irq,
  161. };
  162. static int __init qnap_ts209_pci_init(void)
  163. {
  164. if (machine_is_ts_x09())
  165. pci_common_init(&qnap_ts209_pci);
  166. return 0;
  167. }
  168. subsys_initcall(qnap_ts209_pci_init);
  169. /*****************************************************************************
  170. * Ethernet
  171. ****************************************************************************/
  172. static struct mv643xx_eth_platform_data qnap_ts209_eth_data = {
  173. .phy_addr = 8,
  174. .force_phy_addr = 1,
  175. };
  176. static int __init parse_hex_nibble(char n)
  177. {
  178. if (n >= '0' && n <= '9')
  179. return n - '0';
  180. if (n >= 'A' && n <= 'F')
  181. return n - 'A' + 10;
  182. if (n >= 'a' && n <= 'f')
  183. return n - 'a' + 10;
  184. return -1;
  185. }
  186. static int __init parse_hex_byte(const char *b)
  187. {
  188. int hi;
  189. int lo;
  190. hi = parse_hex_nibble(b[0]);
  191. lo = parse_hex_nibble(b[1]);
  192. if (hi < 0 || lo < 0)
  193. return -1;
  194. return (hi << 4) | lo;
  195. }
  196. static int __init check_mac_addr(const char *addr_str)
  197. {
  198. u_int8_t addr[6];
  199. int i;
  200. for (i = 0; i < 6; i++) {
  201. int byte;
  202. /*
  203. * Enforce "xx:xx:xx:xx:xx:xx\n" format.
  204. */
  205. if (addr_str[(i * 3) + 2] != ((i < 5) ? ':' : '\n'))
  206. return -1;
  207. byte = parse_hex_byte(addr_str + (i * 3));
  208. if (byte < 0)
  209. return -1;
  210. addr[i] = byte;
  211. }
  212. printk(KERN_INFO "ts209: found ethernet mac address ");
  213. for (i = 0; i < 6; i++)
  214. printk("%.2x%s", addr[i], (i < 5) ? ":" : ".\n");
  215. memcpy(qnap_ts209_eth_data.mac_addr, addr, 6);
  216. return 0;
  217. }
  218. /*
  219. * The 'NAS Config' flash partition has an ext2 filesystem which
  220. * contains a file that has the ethernet MAC address in plain text
  221. * (format "xx:xx:xx:xx:xx:xx\n".)
  222. */
  223. static void __init ts209_find_mac_addr(void)
  224. {
  225. unsigned long addr;
  226. for (addr = 0x00700000; addr < 0x00760000; addr += 1024) {
  227. char *nor_page;
  228. int ret = 0;
  229. nor_page = ioremap(QNAP_TS209_NOR_BOOT_BASE + addr, 1024);
  230. if (nor_page != NULL) {
  231. ret = check_mac_addr(nor_page);
  232. iounmap(nor_page);
  233. }
  234. if (ret == 0)
  235. break;
  236. }
  237. }
  238. /*****************************************************************************
  239. * RTC S35390A on I2C bus
  240. ****************************************************************************/
  241. #define TS209_RTC_GPIO 3
  242. static struct i2c_board_info __initdata qnap_ts209_i2c_rtc = {
  243. I2C_BOARD_INFO("s35390a", 0x30),
  244. .irq = 0,
  245. };
  246. /****************************************************************************
  247. * GPIO Attached Keys
  248. * Power button is attached to the PIC microcontroller
  249. ****************************************************************************/
  250. #define QNAP_TS209_GPIO_KEY_MEDIA 1
  251. #define QNAP_TS209_GPIO_KEY_RESET 2
  252. static struct gpio_keys_button qnap_ts209_buttons[] = {
  253. {
  254. .code = KEY_RESTART,
  255. .gpio = QNAP_TS209_GPIO_KEY_MEDIA,
  256. .desc = "USB Copy Button",
  257. .active_low = 1,
  258. }, {
  259. .code = KEY_POWER,
  260. .gpio = QNAP_TS209_GPIO_KEY_RESET,
  261. .desc = "Reset Button",
  262. .active_low = 1,
  263. },
  264. };
  265. static struct gpio_keys_platform_data qnap_ts209_button_data = {
  266. .buttons = qnap_ts209_buttons,
  267. .nbuttons = ARRAY_SIZE(qnap_ts209_buttons),
  268. };
  269. static struct platform_device qnap_ts209_button_device = {
  270. .name = "gpio-keys",
  271. .id = -1,
  272. .num_resources = 0,
  273. .dev = {
  274. .platform_data = &qnap_ts209_button_data,
  275. },
  276. };
  277. /*****************************************************************************
  278. * SATA
  279. ****************************************************************************/
  280. static struct mv_sata_platform_data qnap_ts209_sata_data = {
  281. .n_ports = 2,
  282. };
  283. /*****************************************************************************
  284. * General Setup
  285. ****************************************************************************/
  286. static struct orion5x_mpp_mode ts209_mpp_modes[] __initdata = {
  287. { 0, MPP_UNUSED },
  288. { 1, MPP_GPIO }, /* USB copy button */
  289. { 2, MPP_GPIO }, /* Load defaults button */
  290. { 3, MPP_GPIO }, /* GPIO RTC */
  291. { 4, MPP_UNUSED },
  292. { 5, MPP_UNUSED },
  293. { 6, MPP_GPIO }, /* PCI Int A */
  294. { 7, MPP_GPIO }, /* PCI Int B */
  295. { 8, MPP_UNUSED },
  296. { 9, MPP_UNUSED },
  297. { 10, MPP_UNUSED },
  298. { 11, MPP_UNUSED },
  299. { 12, MPP_SATA_LED }, /* SATA 0 presence */
  300. { 13, MPP_SATA_LED }, /* SATA 1 presence */
  301. { 14, MPP_SATA_LED }, /* SATA 0 active */
  302. { 15, MPP_SATA_LED }, /* SATA 1 active */
  303. { 16, MPP_UART }, /* UART1 RXD */
  304. { 17, MPP_UART }, /* UART1 TXD */
  305. { 18, MPP_GPIO }, /* SW_RST */
  306. { 19, MPP_UNUSED },
  307. { -1 },
  308. };
  309. /*
  310. * QNAP TS-[12]09 specific power off method via UART1-attached PIC
  311. */
  312. #define UART1_REG(x) (UART1_VIRT_BASE + ((UART_##x) << 2))
  313. static void qnap_ts209_power_off(void)
  314. {
  315. /* 19200 baud divisor */
  316. const unsigned divisor = ((ORION5X_TCLK + (8 * 19200)) / (16 * 19200));
  317. pr_info("%s: triggering power-off...\n", __func__);
  318. /* hijack uart1 and reset into sane state (19200,8n1) */
  319. orion5x_write(UART1_REG(LCR), 0x83);
  320. orion5x_write(UART1_REG(DLL), divisor & 0xff);
  321. orion5x_write(UART1_REG(DLM), (divisor >> 8) & 0xff);
  322. orion5x_write(UART1_REG(LCR), 0x03);
  323. orion5x_write(UART1_REG(IER), 0x00);
  324. orion5x_write(UART1_REG(FCR), 0x00);
  325. orion5x_write(UART1_REG(MCR), 0x00);
  326. /* send the power-off command 'A' to PIC */
  327. orion5x_write(UART1_REG(TX), 'A');
  328. }
  329. static void __init qnap_ts209_init(void)
  330. {
  331. /*
  332. * Setup basic Orion functions. Need to be called early.
  333. */
  334. orion5x_init();
  335. orion5x_mpp_conf(ts209_mpp_modes);
  336. /*
  337. * MPP[20] PCI clock 0
  338. * MPP[21] PCI clock 1
  339. * MPP[22] USB 0 over current
  340. * MPP[23-25] Reserved
  341. */
  342. /*
  343. * Configure peripherals.
  344. */
  345. orion5x_ehci0_init();
  346. orion5x_ehci1_init();
  347. ts209_find_mac_addr();
  348. orion5x_eth_init(&qnap_ts209_eth_data);
  349. orion5x_i2c_init();
  350. orion5x_sata_init(&qnap_ts209_sata_data);
  351. orion5x_uart0_init();
  352. orion5x_setup_dev_boot_win(QNAP_TS209_NOR_BOOT_BASE,
  353. QNAP_TS209_NOR_BOOT_SIZE);
  354. platform_device_register(&qnap_ts209_nor_flash);
  355. platform_device_register(&qnap_ts209_button_device);
  356. /* Get RTC IRQ and register the chip */
  357. if (gpio_request(TS209_RTC_GPIO, "rtc") == 0) {
  358. if (gpio_direction_input(TS209_RTC_GPIO) == 0)
  359. qnap_ts209_i2c_rtc.irq = gpio_to_irq(TS209_RTC_GPIO);
  360. else
  361. gpio_free(TS209_RTC_GPIO);
  362. }
  363. if (qnap_ts209_i2c_rtc.irq == 0)
  364. pr_warning("qnap_ts209_init: failed to get RTC IRQ\n");
  365. i2c_register_board_info(0, &qnap_ts209_i2c_rtc, 1);
  366. /* register ts209 specific power-off method */
  367. pm_power_off = qnap_ts209_power_off;
  368. }
  369. MACHINE_START(TS209, "QNAP TS-109/TS-209")
  370. /* Maintainer: Byron Bradley <byron.bbradley@gmail.com> */
  371. .phys_io = ORION5X_REGS_PHYS_BASE,
  372. .io_pg_offst = ((ORION5X_REGS_VIRT_BASE) >> 18) & 0xFFFC,
  373. .boot_params = 0x00000100,
  374. .init_machine = qnap_ts209_init,
  375. .map_io = orion5x_map_io,
  376. .init_irq = orion5x_init_irq,
  377. .timer = &orion5x_timer,
  378. .fixup = tag_fixup_mem32,
  379. MACHINE_END