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