blob: 5ad7ad9b9461f66efb95f881dccd043768e6dabb [file] [log] [blame]
Byron Bradley3faf2ee2007-12-15 20:05:49 +00001/*
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
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/platform_device.h>
15#include <linux/pci.h>
16#include <linux/irq.h>
17#include <linux/mtd/physmap.h>
18#include <linux/mtd/nand.h>
19#include <linux/mv643xx_eth.h>
20#include <linux/gpio_keys.h>
21#include <linux/input.h>
22#include <linux/i2c.h>
Herbert Valerio Riedel8f86dda2007-12-16 17:42:31 +010023#include <linux/serial_reg.h>
Byron Bradleyee443912008-02-08 18:20:23 +000024#include <linux/ata_platform.h>
Byron Bradley3faf2ee2007-12-15 20:05:49 +000025#include <asm/mach-types.h>
26#include <asm/gpio.h>
27#include <asm/mach/arch.h>
28#include <asm/mach/pci.h>
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -040029#include <asm/arch/orion5x.h>
Byron Bradley3faf2ee2007-12-15 20:05:49 +000030#include "common.h"
Lennert Buytenhek19cfd5c2008-05-10 23:25:46 +020031#include "mpp.h"
Byron Bradley3faf2ee2007-12-15 20:05:49 +000032
33#define QNAP_TS209_NOR_BOOT_BASE 0xf4000000
34#define QNAP_TS209_NOR_BOOT_SIZE SZ_8M
35
36/****************************************************************************
37 * 8MiB NOR flash. The struct mtd_partition is not in the same order as the
38 * partitions on the device because we want to keep compatability with
39 * existing QNAP firmware.
40 *
41 * Layout as used by QNAP:
42 * [2] 0x00000000-0x00200000 : "Kernel"
43 * [3] 0x00200000-0x00600000 : "RootFS1"
44 * [4] 0x00600000-0x00700000 : "RootFS2"
45 * [6] 0x00700000-0x00760000 : "NAS Config" (read-only)
46 * [5] 0x00760000-0x00780000 : "U-Boot Config"
47 * [1] 0x00780000-0x00800000 : "U-Boot" (read-only)
48 ***************************************************************************/
49static struct mtd_partition qnap_ts209_partitions[] = {
50 {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020051 .name = "U-Boot",
52 .size = 0x00080000,
53 .offset = 0x00780000,
54 .mask_flags = MTD_WRITEABLE,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000055 }, {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020056 .name = "Kernel",
57 .size = 0x00200000,
58 .offset = 0,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000059 }, {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020060 .name = "RootFS1",
61 .size = 0x00400000,
62 .offset = 0x00200000,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000063 }, {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020064 .name = "RootFS2",
65 .size = 0x00100000,
66 .offset = 0x00600000,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000067 }, {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020068 .name = "U-Boot Config",
69 .size = 0x00020000,
70 .offset = 0x00760000,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000071 }, {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020072 .name = "NAS Config",
73 .size = 0x00060000,
74 .offset = 0x00700000,
75 .mask_flags = MTD_WRITEABLE,
76 },
Byron Bradley3faf2ee2007-12-15 20:05:49 +000077};
78
79static struct physmap_flash_data qnap_ts209_nor_flash_data = {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020080 .width = 1,
81 .parts = qnap_ts209_partitions,
82 .nr_parts = ARRAY_SIZE(qnap_ts209_partitions)
Byron Bradley3faf2ee2007-12-15 20:05:49 +000083};
84
85static struct resource qnap_ts209_nor_flash_resource = {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020086 .flags = IORESOURCE_MEM,
87 .start = QNAP_TS209_NOR_BOOT_BASE,
88 .end = QNAP_TS209_NOR_BOOT_BASE + QNAP_TS209_NOR_BOOT_SIZE - 1,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000089};
90
91static struct platform_device qnap_ts209_nor_flash = {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020092 .name = "physmap-flash",
93 .id = 0,
94 .dev = {
95 .platform_data = &qnap_ts209_nor_flash_data,
96 },
97 .resource = &qnap_ts209_nor_flash_resource,
98 .num_resources = 1,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000099};
100
101/*****************************************************************************
102 * PCI
103 ****************************************************************************/
104
105#define QNAP_TS209_PCI_SLOT0_OFFS 7
106#define QNAP_TS209_PCI_SLOT0_IRQ_PIN 6
107#define QNAP_TS209_PCI_SLOT1_IRQ_PIN 7
108
109void __init qnap_ts209_pci_preinit(void)
110{
111 int pin;
112
113 /*
114 * Configure PCI GPIO IRQ pins
115 */
116 pin = QNAP_TS209_PCI_SLOT0_IRQ_PIN;
117 if (gpio_request(pin, "PCI Int1") == 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 to "
122 "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 pin = QNAP_TS209_PCI_SLOT1_IRQ_PIN;
131 if (gpio_request(pin, "PCI Int2") == 0) {
132 if (gpio_direction_input(pin) == 0) {
133 set_irq_type(gpio_to_irq(pin), IRQT_LOW);
134 } else {
135 printk(KERN_ERR "qnap_ts209_pci_preinit failed "
136 "to set_irq_type pin %d\n", pin);
137 gpio_free(pin);
138 }
139 } else {
140 printk(KERN_ERR "qnap_ts209_pci_preinit failed to gpio_request "
141 "%d\n", pin);
142 }
143}
144
145static int __init qnap_ts209_pci_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
146{
Lennert Buytenhek92b913b2008-04-25 16:28:33 -0400147 int irq;
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000148
149 /*
Lennert Buytenhek92b913b2008-04-25 16:28:33 -0400150 * Check for devices with hard-wired IRQs.
151 */
152 irq = orion5x_pci_map_irq(dev, slot, pin);
153 if (irq != -1)
154 return irq;
155
156 /*
157 * PCI IRQs are connected via GPIOs.
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000158 */
159 switch (slot - QNAP_TS209_PCI_SLOT0_OFFS) {
160 case 0:
161 return gpio_to_irq(QNAP_TS209_PCI_SLOT0_IRQ_PIN);
162 case 1:
163 return gpio_to_irq(QNAP_TS209_PCI_SLOT1_IRQ_PIN);
164 default:
165 return -1;
166 }
167}
168
169static struct hw_pci qnap_ts209_pci __initdata = {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200170 .nr_controllers = 2,
171 .preinit = qnap_ts209_pci_preinit,
172 .swizzle = pci_std_swizzle,
173 .setup = orion5x_pci_sys_setup,
174 .scan = orion5x_pci_sys_scan_bus,
175 .map_irq = qnap_ts209_pci_map_irq,
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000176};
177
178static int __init qnap_ts209_pci_init(void)
179{
180 if (machine_is_ts_x09())
181 pci_common_init(&qnap_ts209_pci);
182
183 return 0;
184}
185
186subsys_initcall(qnap_ts209_pci_init);
187
188/*****************************************************************************
189 * Ethernet
190 ****************************************************************************/
191
192static struct mv643xx_eth_platform_data qnap_ts209_eth_data = {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200193 .phy_addr = 8,
194 .force_phy_addr = 1,
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000195};
196
Lennert Buytenhekbee036b2008-04-01 16:21:49 +0100197static int __init parse_hex_nibble(char n)
198{
199 if (n >= '0' && n <= '9')
200 return n - '0';
201
202 if (n >= 'A' && n <= 'F')
203 return n - 'A' + 10;
204
205 if (n >= 'a' && n <= 'f')
206 return n - 'a' + 10;
207
208 return -1;
209}
210
211static int __init parse_hex_byte(const char *b)
212{
213 int hi;
214 int lo;
215
216 hi = parse_hex_nibble(b[0]);
217 lo = parse_hex_nibble(b[1]);
218
219 if (hi < 0 || lo < 0)
220 return -1;
221
222 return (hi << 4) | lo;
223}
224
225static int __init check_mac_addr(const char *addr_str)
226{
227 u_int8_t addr[6];
228 int i;
229
230 for (i = 0; i < 6; i++) {
231 int byte;
232
233 /*
234 * Enforce "xx:xx:xx:xx:xx:xx\n" format.
235 */
236 if (addr_str[(i * 3) + 2] != ((i < 5) ? ':' : '\n'))
237 return -1;
238
239 byte = parse_hex_byte(addr_str + (i * 3));
240 if (byte < 0)
241 return -1;
242 addr[i] = byte;
243 }
244
245 printk(KERN_INFO "ts209: found ethernet mac address ");
246 for (i = 0; i < 6; i++)
247 printk("%.2x%s", addr[i], (i < 5) ? ":" : ".\n");
248
249 memcpy(qnap_ts209_eth_data.mac_addr, addr, 6);
250
251 return 0;
252}
253
254/*
255 * The 'NAS Config' flash partition has an ext2 filesystem which
256 * contains a file that has the ethernet MAC address in plain text
257 * (format "xx:xx:xx:xx:xx:xx\n".)
258 */
259static void __init ts209_find_mac_addr(void)
260{
261 unsigned long addr;
262
263 for (addr = 0x00700000; addr < 0x00760000; addr += 1024) {
264 char *nor_page;
265 int ret = 0;
266
267 nor_page = ioremap(QNAP_TS209_NOR_BOOT_BASE + addr, 1024);
268 if (nor_page != NULL) {
269 ret = check_mac_addr(nor_page);
270 iounmap(nor_page);
271 }
272
273 if (ret == 0)
274 break;
275 }
276}
277
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000278/*****************************************************************************
279 * RTC S35390A on I2C bus
280 ****************************************************************************/
Byron Bradley59e8ce52008-02-10 22:31:09 +0100281
282#define TS209_RTC_GPIO 3
283
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000284static struct i2c_board_info __initdata qnap_ts209_i2c_rtc = {
Jean Delvare3760f732008-04-29 23:11:40 +0200285 I2C_BOARD_INFO("s35390a", 0x30),
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200286 .irq = 0,
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000287};
288
289/****************************************************************************
290 * GPIO Attached Keys
291 * Power button is attached to the PIC microcontroller
292 ****************************************************************************/
293
294#define QNAP_TS209_GPIO_KEY_MEDIA 1
295#define QNAP_TS209_GPIO_KEY_RESET 2
296
297static struct gpio_keys_button qnap_ts209_buttons[] = {
298 {
299 .code = KEY_RESTART,
300 .gpio = QNAP_TS209_GPIO_KEY_MEDIA,
301 .desc = "USB Copy Button",
302 .active_low = 1,
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200303 }, {
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000304 .code = KEY_POWER,
305 .gpio = QNAP_TS209_GPIO_KEY_RESET,
306 .desc = "Reset Button",
307 .active_low = 1,
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200308 },
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000309};
310
311static struct gpio_keys_platform_data qnap_ts209_button_data = {
312 .buttons = qnap_ts209_buttons,
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200313 .nbuttons = ARRAY_SIZE(qnap_ts209_buttons),
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000314};
315
316static struct platform_device qnap_ts209_button_device = {
317 .name = "gpio-keys",
318 .id = -1,
319 .num_resources = 0,
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200320 .dev = {
321 .platform_data = &qnap_ts209_button_data,
322 },
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000323};
324
325/*****************************************************************************
Byron Bradleyee443912008-02-08 18:20:23 +0000326 * SATA
327 ****************************************************************************/
328static struct mv_sata_platform_data qnap_ts209_sata_data = {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200329 .n_ports = 2,
Byron Bradleyee443912008-02-08 18:20:23 +0000330};
331
332/*****************************************************************************
333
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000334 * General Setup
335 ****************************************************************************/
Lennert Buytenhek19cfd5c2008-05-10 23:25:46 +0200336static struct orion5x_mpp_mode ts209_mpp_modes[] __initdata = {
337 { 0, MPP_UNUSED },
338 { 1, MPP_GPIO }, /* USB copy button */
339 { 2, MPP_GPIO }, /* Load defaults button */
340 { 3, MPP_GPIO }, /* GPIO RTC */
341 { 4, MPP_UNUSED },
342 { 5, MPP_UNUSED },
343 { 6, MPP_GPIO }, /* PCI Int A */
344 { 7, MPP_GPIO }, /* PCI Int B */
345 { 8, MPP_UNUSED },
346 { 9, MPP_UNUSED },
347 { 10, MPP_UNUSED },
348 { 11, MPP_UNUSED },
349 { 12, MPP_SATA_LED }, /* SATA 0 presence */
350 { 13, MPP_SATA_LED }, /* SATA 1 presence */
351 { 14, MPP_SATA_LED }, /* SATA 0 active */
352 { 15, MPP_SATA_LED }, /* SATA 1 active */
353 { 16, MPP_UART }, /* UART1 RXD */
354 { 17, MPP_UART }, /* UART1 TXD */
355 { 18, MPP_GPIO }, /* SW_RST */
356 { 19, MPP_UNUSED },
357 { -1 },
358};
359
Herbert Valerio Riedel8f86dda2007-12-16 17:42:31 +0100360/*
361 * QNAP TS-[12]09 specific power off method via UART1-attached PIC
362 */
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200363#define UART1_REG(x) (UART1_VIRT_BASE + ((UART_##x) << 2))
Herbert Valerio Riedel8f86dda2007-12-16 17:42:31 +0100364
365static void qnap_ts209_power_off(void)
366{
367 /* 19200 baud divisor */
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400368 const unsigned divisor = ((ORION5X_TCLK + (8 * 19200)) / (16 * 19200));
Herbert Valerio Riedel8f86dda2007-12-16 17:42:31 +0100369
370 pr_info("%s: triggering power-off...\n", __func__);
371
372 /* hijack uart1 and reset into sane state (19200,8n1) */
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400373 orion5x_write(UART1_REG(LCR), 0x83);
374 orion5x_write(UART1_REG(DLL), divisor & 0xff);
375 orion5x_write(UART1_REG(DLM), (divisor >> 8) & 0xff);
376 orion5x_write(UART1_REG(LCR), 0x03);
377 orion5x_write(UART1_REG(IER), 0x00);
378 orion5x_write(UART1_REG(FCR), 0x00);
379 orion5x_write(UART1_REG(MCR), 0x00);
Herbert Valerio Riedel8f86dda2007-12-16 17:42:31 +0100380
381 /* send the power-off command 'A' to PIC */
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400382 orion5x_write(UART1_REG(TX), 'A');
Herbert Valerio Riedel8f86dda2007-12-16 17:42:31 +0100383}
384
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000385static void __init qnap_ts209_init(void)
386{
387 /*
388 * Setup basic Orion functions. Need to be called early.
389 */
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400390 orion5x_init();
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000391
Lennert Buytenhek19cfd5c2008-05-10 23:25:46 +0200392 orion5x_mpp_conf(ts209_mpp_modes);
393
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000394 /*
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000395 * MPP[20] PCI clock 0
396 * MPP[21] PCI clock 1
397 * MPP[22] USB 0 over current
398 * MPP[23-25] Reserved
399 */
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000400
Lennert Buytenhek044f6c72008-04-22 05:37:12 +0200401 /*
402 * Configure peripherals.
403 */
404 orion5x_ehci0_init();
405 orion5x_ehci1_init();
406 ts209_find_mac_addr();
407 orion5x_eth_init(&qnap_ts209_eth_data);
408 orion5x_i2c_init();
409 orion5x_sata_init(&qnap_ts209_sata_data);
410 orion5x_uart0_init();
Herbert Valerio Riedel8f86dda2007-12-16 17:42:31 +0100411
Lennert Buytenhek044f6c72008-04-22 05:37:12 +0200412 orion5x_setup_dev_boot_win(QNAP_TS209_NOR_BOOT_BASE,
413 QNAP_TS209_NOR_BOOT_SIZE);
414 platform_device_register(&qnap_ts209_nor_flash);
415
416 platform_device_register(&qnap_ts209_button_device);
Byron Bradley59e8ce52008-02-10 22:31:09 +0100417
418 /* Get RTC IRQ and register the chip */
419 if (gpio_request(TS209_RTC_GPIO, "rtc") == 0) {
420 if (gpio_direction_input(TS209_RTC_GPIO) == 0)
421 qnap_ts209_i2c_rtc.irq = gpio_to_irq(TS209_RTC_GPIO);
422 else
423 gpio_free(TS209_RTC_GPIO);
424 }
425 if (qnap_ts209_i2c_rtc.irq == 0)
426 pr_warning("qnap_ts209_init: failed to get RTC IRQ\n");
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000427 i2c_register_board_info(0, &qnap_ts209_i2c_rtc, 1);
Byron Bradley59e8ce52008-02-10 22:31:09 +0100428
Lennert Buytenhek044f6c72008-04-22 05:37:12 +0200429 /* register ts209 specific power-off method */
430 pm_power_off = qnap_ts209_power_off;
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000431}
432
433MACHINE_START(TS209, "QNAP TS-109/TS-209")
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200434 /* Maintainer: Byron Bradley <byron.bbradley@gmail.com> */
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400435 .phys_io = ORION5X_REGS_PHYS_BASE,
436 .io_pg_offst = ((ORION5X_REGS_VIRT_BASE) >> 18) & 0xFFFC,
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000437 .boot_params = 0x00000100,
438 .init_machine = qnap_ts209_init,
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400439 .map_io = orion5x_map_io,
440 .init_irq = orion5x_init_irq,
441 .timer = &orion5x_timer,
Guennadi Liakhovetskibe73a342008-02-29 21:12:57 +0100442 .fixup = tag_fixup_mem32,
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000443MACHINE_END