terastation_pro2-setup.c 9.2 KB

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