Merge branch 'depends/rmk/devel-stable' into next/board
[pandora-kernel.git] / arch / arm / mach-orion5x / terastation_pro2-setup.c
1 /*
2  * Buffalo Terastation Pro II/Live Board Setup
3  *
4  * Maintainer: Sylver Bruneau <sylver.bruneau@googlemail.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/gpio.h>
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/delay.h>
18 #include <linux/mtd/physmap.h>
19 #include <linux/mv643xx_eth.h>
20 #include <linux/i2c.h>
21 #include <linux/serial_reg.h>
22 #include <asm/mach-types.h>
23 #include <asm/mach/arch.h>
24 #include <asm/mach/pci.h>
25 #include <mach/orion5x.h>
26 #include "common.h"
27 #include "mpp.h"
28
29 /*****************************************************************************
30  * Terastation Pro 2/Live Info
31  ****************************************************************************/
32
33 /*
34  * Terastation Pro 2 hardware :
35  * - Marvell 88F5281-D0
36  * - Marvell 88SX6042 SATA controller (PCI)
37  * - Marvell 88E1118 Gigabit Ethernet PHY
38  * - 256KB NOR flash
39  * - 128MB of DDR RAM
40  * - PCIe port (not equipped)
41  */
42
43 /*
44  * 256K NOR flash Device bus boot chip select
45  */
46
47 #define TSP2_NOR_BOOT_BASE      0xf4000000
48 #define TSP2_NOR_BOOT_SIZE      SZ_256K
49
50 /*****************************************************************************
51  * 256KB NOR Flash on BOOT Device
52  ****************************************************************************/
53
54 static struct physmap_flash_data tsp2_nor_flash_data = {
55         .width    = 1,
56 };
57
58 static struct resource tsp2_nor_flash_resource = {
59         .flags = IORESOURCE_MEM,
60         .start = TSP2_NOR_BOOT_BASE,
61         .end   = TSP2_NOR_BOOT_BASE + TSP2_NOR_BOOT_SIZE - 1,
62 };
63
64 static struct platform_device tsp2_nor_flash = {
65         .name          = "physmap-flash",
66         .id            = 0,
67         .dev           = {
68                 .platform_data  = &tsp2_nor_flash_data,
69         },
70         .num_resources = 1,
71         .resource      = &tsp2_nor_flash_resource,
72 };
73
74 /*****************************************************************************
75  * PCI
76  ****************************************************************************/
77 #define TSP2_PCI_SLOT0_OFFS             7
78 #define TSP2_PCI_SLOT0_IRQ_PIN          11
79
80 void __init tsp2_pci_preinit(void)
81 {
82         int pin;
83
84         /*
85          * Configure PCI GPIO IRQ pins
86          */
87         pin = TSP2_PCI_SLOT0_IRQ_PIN;
88         if (gpio_request(pin, "PCI Int1") == 0) {
89                 if (gpio_direction_input(pin) == 0) {
90                         irq_set_irq_type(gpio_to_irq(pin), IRQ_TYPE_LEVEL_LOW);
91                 } else {
92                         printk(KERN_ERR "tsp2_pci_preinit failed "
93                                         "to set_irq_type pin %d\n", pin);
94                         gpio_free(pin);
95                 }
96         } else {
97                 printk(KERN_ERR "tsp2_pci_preinit failed to "
98                                 "gpio_request %d\n", pin);
99         }
100 }
101
102 static int __init tsp2_pci_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
103 {
104         int irq;
105
106         /*
107          * Check for devices with hard-wired IRQs.
108          */
109         irq = orion5x_pci_map_irq(dev, slot, pin);
110         if (irq != -1)
111                 return irq;
112
113         /*
114          * PCI IRQs are connected via GPIOs.
115          */
116         if (slot == TSP2_PCI_SLOT0_OFFS)
117                 return gpio_to_irq(TSP2_PCI_SLOT0_IRQ_PIN);
118
119         return -1;
120 }
121
122 static struct hw_pci tsp2_pci __initdata = {
123         .nr_controllers = 2,
124         .preinit        = tsp2_pci_preinit,
125         .swizzle        = pci_std_swizzle,
126         .setup          = orion5x_pci_sys_setup,
127         .scan           = orion5x_pci_sys_scan_bus,
128         .map_irq        = tsp2_pci_map_irq,
129 };
130
131 static int __init tsp2_pci_init(void)
132 {
133         if (machine_is_terastation_pro2())
134                 pci_common_init(&tsp2_pci);
135
136         return 0;
137 }
138
139 subsys_initcall(tsp2_pci_init);
140
141 /*****************************************************************************
142  * Ethernet
143  ****************************************************************************/
144
145 static struct mv643xx_eth_platform_data tsp2_eth_data = {
146         .phy_addr       = 0,
147 };
148
149 /*****************************************************************************
150  * RTC 5C372a on I2C bus
151  ****************************************************************************/
152
153 #define TSP2_RTC_GPIO   9
154
155 static struct i2c_board_info __initdata tsp2_i2c_rtc = {
156         I2C_BOARD_INFO("rs5c372a", 0x32),
157 };
158
159 /*****************************************************************************
160  * Terastation Pro II specific power off method via UART1-attached
161  * microcontroller
162  ****************************************************************************/
163
164 #define UART1_REG(x)    (UART1_VIRT_BASE + ((UART_##x) << 2))
165
166 static int tsp2_miconread(unsigned char *buf, int count)
167 {
168         int i;
169         int timeout;
170
171         for (i = 0; i < count; i++) {
172                 timeout = 10;
173
174                 while (!(readl(UART1_REG(LSR)) & UART_LSR_DR)) {
175                         if (--timeout == 0)
176                                 break;
177                         udelay(1000);
178                 }
179
180                 if (timeout == 0)
181                         break;
182                 buf[i] = readl(UART1_REG(RX));
183         }
184
185         /* return read bytes */
186         return i;
187 }
188
189 static int tsp2_miconwrite(const unsigned char *buf, int count)
190 {
191         int i = 0;
192
193         while (count--) {
194                 while (!(readl(UART1_REG(LSR)) & UART_LSR_THRE))
195                         barrier();
196                 writel(buf[i++], UART1_REG(TX));
197         }
198
199         return 0;
200 }
201
202 static int tsp2_miconsend(const unsigned char *data, int count)
203 {
204         int i;
205         unsigned char checksum = 0;
206         unsigned char recv_buf[40];
207         unsigned char send_buf[40];
208         unsigned char correct_ack[3];
209         int retry = 2;
210
211         /* Generate checksum */
212         for (i = 0; i < count; i++)
213                 checksum -=  data[i];
214
215         do {
216                 /* Send data */
217                 tsp2_miconwrite(data, count);
218
219                 /* send checksum */
220                 tsp2_miconwrite(&checksum, 1);
221
222                 if (tsp2_miconread(recv_buf, sizeof(recv_buf)) <= 3) {
223                         printk(KERN_ERR ">%s: receive failed.\n", __func__);
224
225                         /* send preamble to clear the receive buffer */
226                         memset(&send_buf, 0xff, sizeof(send_buf));
227                         tsp2_miconwrite(send_buf, sizeof(send_buf));
228
229                         /* make dummy reads */
230                         mdelay(100);
231                         tsp2_miconread(recv_buf, sizeof(recv_buf));
232                 } else {
233                         /* Generate expected ack */
234                         correct_ack[0] = 0x01;
235                         correct_ack[1] = data[1];
236                         correct_ack[2] = 0x00;
237
238                         /* checksum Check */
239                         if ((recv_buf[0] + recv_buf[1] + recv_buf[2] +
240                              recv_buf[3]) & 0xFF) {
241                                 printk(KERN_ERR ">%s: Checksum Error : "
242                                         "Received data[%02x, %02x, %02x, %02x]"
243                                         "\n", __func__, recv_buf[0],
244                                         recv_buf[1], recv_buf[2], recv_buf[3]);
245                         } else {
246                                 /* Check Received Data */
247                                 if (correct_ack[0] == recv_buf[0] &&
248                                     correct_ack[1] == recv_buf[1] &&
249                                     correct_ack[2] == recv_buf[2]) {
250                                         /* Interval for next command */
251                                         mdelay(10);
252
253                                         /* Receive ACK */
254                                         return 0;
255                                 }
256                         }
257                         /* Received NAK or illegal Data */
258                         printk(KERN_ERR ">%s: Error : NAK or Illegal Data "
259                                         "Received\n", __func__);
260                 }
261         } while (retry--);
262
263         /* Interval for next command */
264         mdelay(10);
265
266         return -1;
267 }
268
269 static void tsp2_power_off(void)
270 {
271         const unsigned char watchdogkill[]      = {0x01, 0x35, 0x00};
272         const unsigned char shutdownwait[]      = {0x00, 0x0c};
273         const unsigned char poweroff[]          = {0x00, 0x06};
274         /* 38400 baud divisor */
275         const unsigned divisor = ((orion5x_tclk + (8 * 38400)) / (16 * 38400));
276
277         pr_info("%s: triggering power-off...\n", __func__);
278
279         /* hijack uart1 and reset into sane state (38400,8n1,even parity) */
280         writel(0x83, UART1_REG(LCR));
281         writel(divisor & 0xff, UART1_REG(DLL));
282         writel((divisor >> 8) & 0xff, UART1_REG(DLM));
283         writel(0x1b, UART1_REG(LCR));
284         writel(0x00, UART1_REG(IER));
285         writel(0x07, UART1_REG(FCR));
286         writel(0x00, UART1_REG(MCR));
287
288         /* Send the commands to shutdown the Terastation Pro II */
289         tsp2_miconsend(watchdogkill, sizeof(watchdogkill)) ;
290         tsp2_miconsend(shutdownwait, sizeof(shutdownwait)) ;
291         tsp2_miconsend(poweroff, sizeof(poweroff));
292 }
293
294 /*****************************************************************************
295  * General Setup
296  ****************************************************************************/
297 static unsigned int tsp2_mpp_modes[] __initdata = {
298         MPP0_PCIE_RST_OUTn,
299         MPP1_UNUSED,
300         MPP2_UNUSED,
301         MPP3_UNUSED,
302         MPP4_NAND,              /* BOOT NAND Flash REn */
303         MPP5_NAND,              /* BOOT NAND Flash WEn */
304         MPP6_NAND,              /* BOOT NAND Flash HREn[0] */
305         MPP7_NAND,              /* BOOT NAND Flash WEn[0] */
306         MPP8_GPIO,              /* MICON int */
307         MPP9_GPIO,              /* RTC int */
308         MPP10_UNUSED,
309         MPP11_GPIO,             /* PCI Int A */
310         MPP12_UNUSED,
311         MPP13_GPIO,             /* UPS on UART0 enable */
312         MPP14_GPIO,             /* UPS low battery detection */
313         MPP15_UNUSED,
314         MPP16_UART,             /* UART1 RXD */
315         MPP17_UART,             /* UART1 TXD */
316         MPP18_UART,             /* UART1 CTSn */
317         MPP19_UART,             /* UART1 RTSn */
318         0,
319 };
320
321 static void __init tsp2_init(void)
322 {
323         /*
324          * Setup basic Orion functions. Need to be called early.
325          */
326         orion5x_init();
327
328         orion5x_mpp_conf(tsp2_mpp_modes);
329
330         /*
331          * Configure peripherals.
332          */
333         orion5x_setup_dev_boot_win(TSP2_NOR_BOOT_BASE,
334                                    TSP2_NOR_BOOT_SIZE);
335         platform_device_register(&tsp2_nor_flash);
336
337         orion5x_ehci0_init();
338         orion5x_eth_init(&tsp2_eth_data);
339         orion5x_i2c_init();
340         orion5x_uart0_init();
341         orion5x_uart1_init();
342
343         /* Get RTC IRQ and register the chip */
344         if (gpio_request(TSP2_RTC_GPIO, "rtc") == 0) {
345                 if (gpio_direction_input(TSP2_RTC_GPIO) == 0)
346                         tsp2_i2c_rtc.irq = gpio_to_irq(TSP2_RTC_GPIO);
347                 else
348                         gpio_free(TSP2_RTC_GPIO);
349         }
350         if (tsp2_i2c_rtc.irq == 0)
351                 pr_warning("tsp2_init: failed to get RTC IRQ\n");
352         i2c_register_board_info(0, &tsp2_i2c_rtc, 1);
353
354         /* register Terastation Pro II specific power-off method */
355         pm_power_off = tsp2_power_off;
356 }
357
358 MACHINE_START(TERASTATION_PRO2, "Buffalo Terastation Pro II/Live")
359         /* Maintainer:  Sylver Bruneau <sylver.bruneau@googlemail.com> */
360         .atag_offset    = 0x100,
361         .init_machine   = tsp2_init,
362         .map_io         = orion5x_map_io,
363         .init_early     = orion5x_init_early,
364         .init_irq       = orion5x_init_irq,
365         .timer          = &orion5x_timer,
366         .fixup          = tag_fixup_mem32,
367 MACHINE_END