ts209-setup.c 11 KB

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