devices.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * RouterBoard 500 Platform devices
  4. *
  5. * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
  6. * Copyright (C) 2007 Florian Fainelli <florian@openwrt.org>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/export.h>
  10. #include <linux/hex.h>
  11. #include <linux/init.h>
  12. #include <linux/ctype.h>
  13. #include <linux/string.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/mtd/platnand.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/gpio.h>
  18. #include <linux/gpio/machine.h>
  19. #include <linux/gpio_keys.h>
  20. #include <linux/input.h>
  21. #include <linux/serial_8250.h>
  22. #include <asm/bootinfo.h>
  23. #include <asm/mach-rc32434/rc32434.h>
  24. #include <asm/mach-rc32434/dma.h>
  25. #include <asm/mach-rc32434/dma_v.h>
  26. #include <asm/mach-rc32434/eth.h>
  27. #include <asm/mach-rc32434/rb.h>
  28. #include <asm/mach-rc32434/integ.h>
  29. #include <asm/mach-rc32434/gpio.h>
  30. #include <asm/mach-rc32434/irq.h>
  31. #define ETH0_RX_DMA_ADDR (DMA0_BASE_ADDR + 0 * DMA_CHAN_OFFSET)
  32. #define ETH0_TX_DMA_ADDR (DMA0_BASE_ADDR + 1 * DMA_CHAN_OFFSET)
  33. extern unsigned int idt_cpu_freq;
  34. static struct mpmc_device dev3;
  35. void set_latch_u5(unsigned char or_mask, unsigned char nand_mask)
  36. {
  37. unsigned long flags;
  38. spin_lock_irqsave(&dev3.lock, flags);
  39. dev3.state = (dev3.state | or_mask) & ~nand_mask;
  40. writeb(dev3.state, dev3.base);
  41. spin_unlock_irqrestore(&dev3.lock, flags);
  42. }
  43. EXPORT_SYMBOL(set_latch_u5);
  44. unsigned char get_latch_u5(void)
  45. {
  46. return dev3.state;
  47. }
  48. EXPORT_SYMBOL(get_latch_u5);
  49. static struct resource korina_dev0_res[] = {
  50. {
  51. .name = "emac",
  52. .start = ETH0_BASE_ADDR,
  53. .end = ETH0_BASE_ADDR + sizeof(struct eth_regs),
  54. .flags = IORESOURCE_MEM,
  55. }, {
  56. .name = "rx",
  57. .start = ETH0_DMA_RX_IRQ,
  58. .end = ETH0_DMA_RX_IRQ,
  59. .flags = IORESOURCE_IRQ
  60. }, {
  61. .name = "tx",
  62. .start = ETH0_DMA_TX_IRQ,
  63. .end = ETH0_DMA_TX_IRQ,
  64. .flags = IORESOURCE_IRQ
  65. }, {
  66. .name = "dma_rx",
  67. .start = ETH0_RX_DMA_ADDR,
  68. .end = ETH0_RX_DMA_ADDR + DMA_CHAN_OFFSET - 1,
  69. .flags = IORESOURCE_MEM,
  70. }, {
  71. .name = "dma_tx",
  72. .start = ETH0_TX_DMA_ADDR,
  73. .end = ETH0_TX_DMA_ADDR + DMA_CHAN_OFFSET - 1,
  74. .flags = IORESOURCE_MEM,
  75. }
  76. };
  77. static struct korina_device korina_dev0_data = {
  78. .name = "korina0",
  79. .mac = {0xde, 0xca, 0xff, 0xc0, 0xff, 0xee}
  80. };
  81. static struct platform_device korina_dev0 = {
  82. .id = -1,
  83. .name = "korina",
  84. .resource = korina_dev0_res,
  85. .num_resources = ARRAY_SIZE(korina_dev0_res),
  86. .dev = {
  87. .platform_data = &korina_dev0_data.mac,
  88. }
  89. };
  90. static struct resource cf_slot0_res[] = {
  91. {
  92. .name = "cf_membase",
  93. .flags = IORESOURCE_MEM
  94. }, {
  95. .name = "cf_irq",
  96. .start = (8 + 4 * 32 + CF_GPIO_NUM), /* 149 */
  97. .end = (8 + 4 * 32 + CF_GPIO_NUM),
  98. .flags = IORESOURCE_IRQ
  99. }
  100. };
  101. static struct gpiod_lookup_table cf_slot0_gpio_table = {
  102. .dev_id = "pata-rb532-cf",
  103. .table = {
  104. GPIO_LOOKUP("gpio0", CF_GPIO_NUM,
  105. NULL, GPIO_ACTIVE_HIGH),
  106. { },
  107. },
  108. };
  109. static struct platform_device cf_slot0 = {
  110. .id = -1,
  111. .name = "pata-rb532-cf",
  112. .resource = cf_slot0_res,
  113. .num_resources = ARRAY_SIZE(cf_slot0_res),
  114. };
  115. /* Resources and device for NAND */
  116. static int rb532_dev_ready(struct nand_chip *chip)
  117. {
  118. return gpio_get_value(GPIO_RDY);
  119. }
  120. static void rb532_cmd_ctrl(struct nand_chip *chip, int cmd, unsigned int ctrl)
  121. {
  122. unsigned char orbits, nandbits;
  123. if (ctrl & NAND_CTRL_CHANGE) {
  124. orbits = (ctrl & NAND_CLE) << 1;
  125. orbits |= (ctrl & NAND_ALE) >> 1;
  126. nandbits = (~ctrl & NAND_CLE) << 1;
  127. nandbits |= (~ctrl & NAND_ALE) >> 1;
  128. set_latch_u5(orbits, nandbits);
  129. }
  130. if (cmd != NAND_CMD_NONE)
  131. writeb(cmd, chip->legacy.IO_ADDR_W);
  132. }
  133. static struct resource nand_slot0_res[] = {
  134. [0] = {
  135. .name = "nand_membase",
  136. .flags = IORESOURCE_MEM
  137. }
  138. };
  139. static struct platform_nand_data rb532_nand_data = {
  140. .ctrl.dev_ready = rb532_dev_ready,
  141. .ctrl.cmd_ctrl = rb532_cmd_ctrl,
  142. };
  143. static struct platform_device nand_slot0 = {
  144. .name = "gen_nand",
  145. .id = -1,
  146. .resource = nand_slot0_res,
  147. .num_resources = ARRAY_SIZE(nand_slot0_res),
  148. .dev.platform_data = &rb532_nand_data,
  149. };
  150. static struct mtd_partition rb532_partition_info[] = {
  151. {
  152. .name = "Routerboard NAND boot",
  153. .offset = 0,
  154. .size = 4 * 1024 * 1024,
  155. }, {
  156. .name = "rootfs",
  157. .offset = MTDPART_OFS_NXTBLK,
  158. .size = MTDPART_SIZ_FULL,
  159. }
  160. };
  161. static struct platform_device rb532_led = {
  162. .name = "rb532-led",
  163. .id = -1,
  164. };
  165. static struct platform_device rb532_button = {
  166. .name = "rb532-button",
  167. .id = -1,
  168. };
  169. static struct resource rb532_wdt_res[] = {
  170. {
  171. .name = "rb532_wdt_res",
  172. .start = INTEG0_BASE_ADDR,
  173. .end = INTEG0_BASE_ADDR + sizeof(struct integ),
  174. .flags = IORESOURCE_MEM,
  175. }
  176. };
  177. static struct platform_device rb532_wdt = {
  178. .name = "rc32434_wdt",
  179. .id = -1,
  180. .resource = rb532_wdt_res,
  181. .num_resources = ARRAY_SIZE(rb532_wdt_res),
  182. };
  183. static struct plat_serial8250_port rb532_uart_res[] = {
  184. {
  185. .type = PORT_16550A,
  186. .mapbase = REGBASE + UART0BASE,
  187. .mapsize = 0x1000,
  188. .irq = UART0_IRQ,
  189. .regshift = 2,
  190. .iotype = UPIO_MEM,
  191. .flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP,
  192. },
  193. {
  194. .flags = 0,
  195. }
  196. };
  197. static struct platform_device rb532_uart = {
  198. .name = "serial8250",
  199. .id = PLAT8250_DEV_PLATFORM,
  200. .dev.platform_data = &rb532_uart_res,
  201. };
  202. static struct platform_device *rb532_devs[] = {
  203. &korina_dev0,
  204. &nand_slot0,
  205. &cf_slot0,
  206. &rb532_led,
  207. &rb532_button,
  208. &rb532_uart,
  209. &rb532_wdt
  210. };
  211. /* NAND definitions */
  212. #define NAND_CHIP_DELAY 25
  213. static void __init rb532_nand_setup(void)
  214. {
  215. switch (mips_machtype) {
  216. case MACH_MIKROTIK_RB532A:
  217. set_latch_u5(LO_FOFF | LO_CEX,
  218. LO_ULED | LO_ALE | LO_CLE | LO_WPX);
  219. break;
  220. default:
  221. set_latch_u5(LO_WPX | LO_FOFF | LO_CEX,
  222. LO_ULED | LO_ALE | LO_CLE);
  223. break;
  224. }
  225. /* Setup NAND specific settings */
  226. rb532_nand_data.chip.nr_chips = 1;
  227. rb532_nand_data.chip.nr_partitions = ARRAY_SIZE(rb532_partition_info);
  228. rb532_nand_data.chip.partitions = rb532_partition_info;
  229. rb532_nand_data.chip.chip_delay = NAND_CHIP_DELAY;
  230. }
  231. static int __init plat_setup_devices(void)
  232. {
  233. /* Look for the CF card reader */
  234. if (!readl(IDT434_REG_BASE + DEV1MASK))
  235. rb532_devs[2] = NULL; /* disable cf_slot0 at index 2 */
  236. else {
  237. cf_slot0_res[0].start =
  238. readl(IDT434_REG_BASE + DEV1BASE);
  239. cf_slot0_res[0].end = cf_slot0_res[0].start + 0x1000;
  240. }
  241. /* Read the NAND resources from the device controller */
  242. nand_slot0_res[0].start = readl(IDT434_REG_BASE + DEV2BASE);
  243. nand_slot0_res[0].end = nand_slot0_res[0].start + 0x1000;
  244. /* Read and map device controller 3 */
  245. dev3.base = ioremap(readl(IDT434_REG_BASE + DEV3BASE), 1);
  246. if (!dev3.base) {
  247. printk(KERN_ERR "rb532: cannot remap device controller 3\n");
  248. return -ENXIO;
  249. }
  250. /* Initialise the NAND device */
  251. rb532_nand_setup();
  252. /* set the uart clock to the current cpu frequency */
  253. rb532_uart_res[0].uartclk = idt_cpu_freq;
  254. gpiod_add_lookup_table(&cf_slot0_gpio_table);
  255. return platform_add_devices(rb532_devs, ARRAY_SIZE(rb532_devs));
  256. }
  257. #ifdef CONFIG_NET
  258. static int __init setup_kmac(char *s)
  259. {
  260. printk(KERN_INFO "korina mac = %s\n", s);
  261. if (!mac_pton(s, korina_dev0_data.mac))
  262. printk(KERN_ERR "Invalid mac\n");
  263. return 1;
  264. }
  265. __setup("kmac=", setup_kmac);
  266. #endif /* CONFIG_NET */
  267. arch_initcall(plat_setup_devices);