blob: 0ec067fcb514617b76c89735bca9f136bd9557ab [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"
31
32#define QNAP_TS209_NOR_BOOT_BASE 0xf4000000
33#define QNAP_TS209_NOR_BOOT_SIZE SZ_8M
34
35/****************************************************************************
36 * 8MiB NOR flash. The struct mtd_partition is not in the same order as the
37 * partitions on the device because we want to keep compatability with
38 * existing QNAP firmware.
39 *
40 * Layout as used by QNAP:
41 * [2] 0x00000000-0x00200000 : "Kernel"
42 * [3] 0x00200000-0x00600000 : "RootFS1"
43 * [4] 0x00600000-0x00700000 : "RootFS2"
44 * [6] 0x00700000-0x00760000 : "NAS Config" (read-only)
45 * [5] 0x00760000-0x00780000 : "U-Boot Config"
46 * [1] 0x00780000-0x00800000 : "U-Boot" (read-only)
47 ***************************************************************************/
48static struct mtd_partition qnap_ts209_partitions[] = {
49 {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020050 .name = "U-Boot",
51 .size = 0x00080000,
52 .offset = 0x00780000,
53 .mask_flags = MTD_WRITEABLE,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000054 }, {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020055 .name = "Kernel",
56 .size = 0x00200000,
57 .offset = 0,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000058 }, {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020059 .name = "RootFS1",
60 .size = 0x00400000,
61 .offset = 0x00200000,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000062 }, {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020063 .name = "RootFS2",
64 .size = 0x00100000,
65 .offset = 0x00600000,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000066 }, {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020067 .name = "U-Boot Config",
68 .size = 0x00020000,
69 .offset = 0x00760000,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000070 }, {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020071 .name = "NAS Config",
72 .size = 0x00060000,
73 .offset = 0x00700000,
74 .mask_flags = MTD_WRITEABLE,
75 },
Byron Bradley3faf2ee2007-12-15 20:05:49 +000076};
77
78static struct physmap_flash_data qnap_ts209_nor_flash_data = {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020079 .width = 1,
80 .parts = qnap_ts209_partitions,
81 .nr_parts = ARRAY_SIZE(qnap_ts209_partitions)
Byron Bradley3faf2ee2007-12-15 20:05:49 +000082};
83
84static struct resource qnap_ts209_nor_flash_resource = {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020085 .flags = IORESOURCE_MEM,
86 .start = QNAP_TS209_NOR_BOOT_BASE,
87 .end = QNAP_TS209_NOR_BOOT_BASE + QNAP_TS209_NOR_BOOT_SIZE - 1,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000088};
89
90static struct platform_device qnap_ts209_nor_flash = {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +020091 .name = "physmap-flash",
92 .id = 0,
93 .dev = {
94 .platform_data = &qnap_ts209_nor_flash_data,
95 },
96 .resource = &qnap_ts209_nor_flash_resource,
97 .num_resources = 1,
Byron Bradley3faf2ee2007-12-15 20:05:49 +000098};
99
100/*****************************************************************************
101 * PCI
102 ****************************************************************************/
103
104#define QNAP_TS209_PCI_SLOT0_OFFS 7
105#define QNAP_TS209_PCI_SLOT0_IRQ_PIN 6
106#define QNAP_TS209_PCI_SLOT1_IRQ_PIN 7
107
108void __init qnap_ts209_pci_preinit(void)
109{
110 int pin;
111
112 /*
113 * Configure PCI GPIO IRQ pins
114 */
115 pin = QNAP_TS209_PCI_SLOT0_IRQ_PIN;
116 if (gpio_request(pin, "PCI Int1") == 0) {
117 if (gpio_direction_input(pin) == 0) {
118 set_irq_type(gpio_to_irq(pin), IRQT_LOW);
119 } else {
120 printk(KERN_ERR "qnap_ts209_pci_preinit failed to "
121 "set_irq_type pin %d\n", pin);
122 gpio_free(pin);
123 }
124 } else {
125 printk(KERN_ERR "qnap_ts209_pci_preinit failed to gpio_request "
126 "%d\n", pin);
127 }
128
129 pin = QNAP_TS209_PCI_SLOT1_IRQ_PIN;
130 if (gpio_request(pin, "PCI Int2") == 0) {
131 if (gpio_direction_input(pin) == 0) {
132 set_irq_type(gpio_to_irq(pin), IRQT_LOW);
133 } else {
134 printk(KERN_ERR "qnap_ts209_pci_preinit failed "
135 "to set_irq_type pin %d\n", pin);
136 gpio_free(pin);
137 }
138 } else {
139 printk(KERN_ERR "qnap_ts209_pci_preinit failed to gpio_request "
140 "%d\n", pin);
141 }
142}
143
144static int __init qnap_ts209_pci_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
145{
Lennert Buytenhek92b913b2008-04-25 16:28:33 -0400146 int irq;
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000147
148 /*
Lennert Buytenhek92b913b2008-04-25 16:28:33 -0400149 * Check for devices with hard-wired IRQs.
150 */
151 irq = orion5x_pci_map_irq(dev, slot, pin);
152 if (irq != -1)
153 return irq;
154
155 /*
156 * PCI IRQs are connected via GPIOs.
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000157 */
158 switch (slot - QNAP_TS209_PCI_SLOT0_OFFS) {
159 case 0:
160 return gpio_to_irq(QNAP_TS209_PCI_SLOT0_IRQ_PIN);
161 case 1:
162 return gpio_to_irq(QNAP_TS209_PCI_SLOT1_IRQ_PIN);
163 default:
164 return -1;
165 }
166}
167
168static struct hw_pci qnap_ts209_pci __initdata = {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200169 .nr_controllers = 2,
170 .preinit = qnap_ts209_pci_preinit,
171 .swizzle = pci_std_swizzle,
172 .setup = orion5x_pci_sys_setup,
173 .scan = orion5x_pci_sys_scan_bus,
174 .map_irq = qnap_ts209_pci_map_irq,
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000175};
176
177static int __init qnap_ts209_pci_init(void)
178{
179 if (machine_is_ts_x09())
180 pci_common_init(&qnap_ts209_pci);
181
182 return 0;
183}
184
185subsys_initcall(qnap_ts209_pci_init);
186
187/*****************************************************************************
188 * Ethernet
189 ****************************************************************************/
190
191static struct mv643xx_eth_platform_data qnap_ts209_eth_data = {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200192 .phy_addr = 8,
193 .force_phy_addr = 1,
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000194};
195
Lennert Buytenhekbee036b2008-04-01 16:21:49 +0100196static int __init parse_hex_nibble(char n)
197{
198 if (n >= '0' && n <= '9')
199 return n - '0';
200
201 if (n >= 'A' && n <= 'F')
202 return n - 'A' + 10;
203
204 if (n >= 'a' && n <= 'f')
205 return n - 'a' + 10;
206
207 return -1;
208}
209
210static int __init parse_hex_byte(const char *b)
211{
212 int hi;
213 int lo;
214
215 hi = parse_hex_nibble(b[0]);
216 lo = parse_hex_nibble(b[1]);
217
218 if (hi < 0 || lo < 0)
219 return -1;
220
221 return (hi << 4) | lo;
222}
223
224static int __init check_mac_addr(const char *addr_str)
225{
226 u_int8_t addr[6];
227 int i;
228
229 for (i = 0; i < 6; i++) {
230 int byte;
231
232 /*
233 * Enforce "xx:xx:xx:xx:xx:xx\n" format.
234 */
235 if (addr_str[(i * 3) + 2] != ((i < 5) ? ':' : '\n'))
236 return -1;
237
238 byte = parse_hex_byte(addr_str + (i * 3));
239 if (byte < 0)
240 return -1;
241 addr[i] = byte;
242 }
243
244 printk(KERN_INFO "ts209: found ethernet mac address ");
245 for (i = 0; i < 6; i++)
246 printk("%.2x%s", addr[i], (i < 5) ? ":" : ".\n");
247
248 memcpy(qnap_ts209_eth_data.mac_addr, addr, 6);
249
250 return 0;
251}
252
253/*
254 * The 'NAS Config' flash partition has an ext2 filesystem which
255 * contains a file that has the ethernet MAC address in plain text
256 * (format "xx:xx:xx:xx:xx:xx\n".)
257 */
258static void __init ts209_find_mac_addr(void)
259{
260 unsigned long addr;
261
262 for (addr = 0x00700000; addr < 0x00760000; addr += 1024) {
263 char *nor_page;
264 int ret = 0;
265
266 nor_page = ioremap(QNAP_TS209_NOR_BOOT_BASE + addr, 1024);
267 if (nor_page != NULL) {
268 ret = check_mac_addr(nor_page);
269 iounmap(nor_page);
270 }
271
272 if (ret == 0)
273 break;
274 }
275}
276
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000277/*****************************************************************************
278 * RTC S35390A on I2C bus
279 ****************************************************************************/
Byron Bradley59e8ce52008-02-10 22:31:09 +0100280
281#define TS209_RTC_GPIO 3
282
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000283static struct i2c_board_info __initdata qnap_ts209_i2c_rtc = {
Jean Delvare3760f732008-04-29 23:11:40 +0200284 I2C_BOARD_INFO("s35390a", 0x30),
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200285 .irq = 0,
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000286};
287
288/****************************************************************************
289 * GPIO Attached Keys
290 * Power button is attached to the PIC microcontroller
291 ****************************************************************************/
292
293#define QNAP_TS209_GPIO_KEY_MEDIA 1
294#define QNAP_TS209_GPIO_KEY_RESET 2
295
296static struct gpio_keys_button qnap_ts209_buttons[] = {
297 {
298 .code = KEY_RESTART,
299 .gpio = QNAP_TS209_GPIO_KEY_MEDIA,
300 .desc = "USB Copy Button",
301 .active_low = 1,
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200302 }, {
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000303 .code = KEY_POWER,
304 .gpio = QNAP_TS209_GPIO_KEY_RESET,
305 .desc = "Reset Button",
306 .active_low = 1,
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200307 },
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000308};
309
310static struct gpio_keys_platform_data qnap_ts209_button_data = {
311 .buttons = qnap_ts209_buttons,
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200312 .nbuttons = ARRAY_SIZE(qnap_ts209_buttons),
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000313};
314
315static struct platform_device qnap_ts209_button_device = {
316 .name = "gpio-keys",
317 .id = -1,
318 .num_resources = 0,
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200319 .dev = {
320 .platform_data = &qnap_ts209_button_data,
321 },
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000322};
323
324/*****************************************************************************
Byron Bradleyee443912008-02-08 18:20:23 +0000325 * SATA
326 ****************************************************************************/
327static struct mv_sata_platform_data qnap_ts209_sata_data = {
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200328 .n_ports = 2,
Byron Bradleyee443912008-02-08 18:20:23 +0000329};
330
331/*****************************************************************************
332
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000333 * General Setup
334 ****************************************************************************/
Herbert Valerio Riedel8f86dda2007-12-16 17:42:31 +0100335/*
336 * QNAP TS-[12]09 specific power off method via UART1-attached PIC
337 */
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200338#define UART1_REG(x) (UART1_VIRT_BASE + ((UART_##x) << 2))
Herbert Valerio Riedel8f86dda2007-12-16 17:42:31 +0100339
340static void qnap_ts209_power_off(void)
341{
342 /* 19200 baud divisor */
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400343 const unsigned divisor = ((ORION5X_TCLK + (8 * 19200)) / (16 * 19200));
Herbert Valerio Riedel8f86dda2007-12-16 17:42:31 +0100344
345 pr_info("%s: triggering power-off...\n", __func__);
346
347 /* hijack uart1 and reset into sane state (19200,8n1) */
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400348 orion5x_write(UART1_REG(LCR), 0x83);
349 orion5x_write(UART1_REG(DLL), divisor & 0xff);
350 orion5x_write(UART1_REG(DLM), (divisor >> 8) & 0xff);
351 orion5x_write(UART1_REG(LCR), 0x03);
352 orion5x_write(UART1_REG(IER), 0x00);
353 orion5x_write(UART1_REG(FCR), 0x00);
354 orion5x_write(UART1_REG(MCR), 0x00);
Herbert Valerio Riedel8f86dda2007-12-16 17:42:31 +0100355
356 /* send the power-off command 'A' to PIC */
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400357 orion5x_write(UART1_REG(TX), 'A');
Herbert Valerio Riedel8f86dda2007-12-16 17:42:31 +0100358}
359
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000360static void __init qnap_ts209_init(void)
361{
362 /*
363 * Setup basic Orion functions. Need to be called early.
364 */
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400365 orion5x_init();
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000366
367 /*
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000368 * Setup Multiplexing Pins --
369 * MPP[0] Reserved
370 * MPP[1] USB copy button (0 active)
371 * MPP[2] Load defaults button (0 active)
372 * MPP[3] GPIO RTC
373 * MPP[4-5] Reserved
374 * MPP[6] PCI Int A
375 * MPP[7] PCI Int B
376 * MPP[8-11] Reserved
377 * MPP[12] SATA 0 presence
378 * MPP[13] SATA 1 presence
379 * MPP[14] SATA 0 active
380 * MPP[15] SATA 1 active
381 * MPP[16] UART1 RXD
382 * MPP[17] UART1 TXD
383 * MPP[18] SW_RST (0 active)
384 * MPP[19] Reserved
385 * MPP[20] PCI clock 0
386 * MPP[21] PCI clock 1
387 * MPP[22] USB 0 over current
388 * MPP[23-25] Reserved
389 */
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400390 orion5x_write(MPP_0_7_CTRL, 0x3);
391 orion5x_write(MPP_8_15_CTRL, 0x55550000);
392 orion5x_write(MPP_16_19_CTRL, 0x5500);
393 orion5x_gpio_set_valid_pins(0x3cc0fff);
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000394
Lennert Buytenhek044f6c72008-04-22 05:37:12 +0200395 /*
396 * Configure peripherals.
397 */
398 orion5x_ehci0_init();
399 orion5x_ehci1_init();
400 ts209_find_mac_addr();
401 orion5x_eth_init(&qnap_ts209_eth_data);
402 orion5x_i2c_init();
403 orion5x_sata_init(&qnap_ts209_sata_data);
404 orion5x_uart0_init();
Herbert Valerio Riedel8f86dda2007-12-16 17:42:31 +0100405
Lennert Buytenhek044f6c72008-04-22 05:37:12 +0200406 orion5x_setup_dev_boot_win(QNAP_TS209_NOR_BOOT_BASE,
407 QNAP_TS209_NOR_BOOT_SIZE);
408 platform_device_register(&qnap_ts209_nor_flash);
409
410 platform_device_register(&qnap_ts209_button_device);
Byron Bradley59e8ce52008-02-10 22:31:09 +0100411
412 /* Get RTC IRQ and register the chip */
413 if (gpio_request(TS209_RTC_GPIO, "rtc") == 0) {
414 if (gpio_direction_input(TS209_RTC_GPIO) == 0)
415 qnap_ts209_i2c_rtc.irq = gpio_to_irq(TS209_RTC_GPIO);
416 else
417 gpio_free(TS209_RTC_GPIO);
418 }
419 if (qnap_ts209_i2c_rtc.irq == 0)
420 pr_warning("qnap_ts209_init: failed to get RTC IRQ\n");
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000421 i2c_register_board_info(0, &qnap_ts209_i2c_rtc, 1);
Byron Bradley59e8ce52008-02-10 22:31:09 +0100422
Lennert Buytenhek044f6c72008-04-22 05:37:12 +0200423 /* register ts209 specific power-off method */
424 pm_power_off = qnap_ts209_power_off;
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000425}
426
427MACHINE_START(TS209, "QNAP TS-109/TS-209")
Lennert Buytenheke7068ad2008-05-10 16:30:01 +0200428 /* Maintainer: Byron Bradley <byron.bbradley@gmail.com> */
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400429 .phys_io = ORION5X_REGS_PHYS_BASE,
430 .io_pg_offst = ((ORION5X_REGS_VIRT_BASE) >> 18) & 0xFFFC,
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000431 .boot_params = 0x00000100,
432 .init_machine = qnap_ts209_init,
Lennert Buytenhek9dd0b192008-03-27 14:51:41 -0400433 .map_io = orion5x_map_io,
434 .init_irq = orion5x_init_irq,
435 .timer = &orion5x_timer,
Guennadi Liakhovetskibe73a342008-02-29 21:12:57 +0100436 .fixup = tag_fixup_mem32,
Byron Bradley3faf2ee2007-12-15 20:05:49 +0000437MACHINE_END