pci-rt2880.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Ralink RT288x SoC PCI register definitions
  3. *
  4. * Copyright (C) 2009 John Crispin <blogic@openwrt.org>
  5. * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
  6. *
  7. * Parts of this file are based on Ralink's 2.6.21 BSP
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/types.h>
  15. #include <linux/pci.h>
  16. #include <linux/io.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_pci.h>
  22. #include <asm/mach-ralink/rt288x.h>
  23. #define RT2880_PCI_BASE 0x00440000
  24. #define RT288X_CPU_IRQ_PCI 4
  25. #define RT2880_PCI_MEM_BASE 0x20000000
  26. #define RT2880_PCI_MEM_SIZE 0x10000000
  27. #define RT2880_PCI_IO_BASE 0x00460000
  28. #define RT2880_PCI_IO_SIZE 0x00010000
  29. #define RT2880_PCI_REG_PCICFG_ADDR 0x00
  30. #define RT2880_PCI_REG_PCIMSK_ADDR 0x0c
  31. #define RT2880_PCI_REG_BAR0SETUP_ADDR 0x10
  32. #define RT2880_PCI_REG_IMBASEBAR0_ADDR 0x18
  33. #define RT2880_PCI_REG_CONFIG_ADDR 0x20
  34. #define RT2880_PCI_REG_CONFIG_DATA 0x24
  35. #define RT2880_PCI_REG_MEMBASE 0x28
  36. #define RT2880_PCI_REG_IOBASE 0x2c
  37. #define RT2880_PCI_REG_ID 0x30
  38. #define RT2880_PCI_REG_CLASS 0x34
  39. #define RT2880_PCI_REG_SUBID 0x38
  40. #define RT2880_PCI_REG_ARBCTL 0x80
  41. static void __iomem *rt2880_pci_base;
  42. static DEFINE_SPINLOCK(rt2880_pci_lock);
  43. static u32 rt2880_pci_reg_read(u32 reg)
  44. {
  45. return readl(rt2880_pci_base + reg);
  46. }
  47. static void rt2880_pci_reg_write(u32 val, u32 reg)
  48. {
  49. writel(val, rt2880_pci_base + reg);
  50. }
  51. static inline u32 rt2880_pci_get_cfgaddr(unsigned int bus, unsigned int slot,
  52. unsigned int func, unsigned int where)
  53. {
  54. return ((bus << 16) | (slot << 11) | (func << 8) | (where & 0xfc) |
  55. 0x80000000);
  56. }
  57. static int rt2880_pci_config_read(struct pci_bus *bus, unsigned int devfn,
  58. int where, int size, u32 *val)
  59. {
  60. unsigned long flags;
  61. u32 address;
  62. u32 data;
  63. address = rt2880_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
  64. PCI_FUNC(devfn), where);
  65. spin_lock_irqsave(&rt2880_pci_lock, flags);
  66. rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
  67. data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
  68. spin_unlock_irqrestore(&rt2880_pci_lock, flags);
  69. switch (size) {
  70. case 1:
  71. *val = (data >> ((where & 3) << 3)) & 0xff;
  72. break;
  73. case 2:
  74. *val = (data >> ((where & 3) << 3)) & 0xffff;
  75. break;
  76. case 4:
  77. *val = data;
  78. break;
  79. }
  80. return PCIBIOS_SUCCESSFUL;
  81. }
  82. static int rt2880_pci_config_write(struct pci_bus *bus, unsigned int devfn,
  83. int where, int size, u32 val)
  84. {
  85. unsigned long flags;
  86. u32 address;
  87. u32 data;
  88. address = rt2880_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
  89. PCI_FUNC(devfn), where);
  90. spin_lock_irqsave(&rt2880_pci_lock, flags);
  91. rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
  92. data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
  93. switch (size) {
  94. case 1:
  95. data = (data & ~(0xff << ((where & 3) << 3))) |
  96. (val << ((where & 3) << 3));
  97. break;
  98. case 2:
  99. data = (data & ~(0xffff << ((where & 3) << 3))) |
  100. (val << ((where & 3) << 3));
  101. break;
  102. case 4:
  103. data = val;
  104. break;
  105. }
  106. rt2880_pci_reg_write(data, RT2880_PCI_REG_CONFIG_DATA);
  107. spin_unlock_irqrestore(&rt2880_pci_lock, flags);
  108. return PCIBIOS_SUCCESSFUL;
  109. }
  110. static struct pci_ops rt2880_pci_ops = {
  111. .read = rt2880_pci_config_read,
  112. .write = rt2880_pci_config_write,
  113. };
  114. static struct resource rt2880_pci_mem_resource = {
  115. .name = "PCI MEM space",
  116. .start = RT2880_PCI_MEM_BASE,
  117. .end = RT2880_PCI_MEM_BASE + RT2880_PCI_MEM_SIZE - 1,
  118. .flags = IORESOURCE_MEM,
  119. };
  120. static struct resource rt2880_pci_io_resource = {
  121. .name = "PCI IO space",
  122. .start = RT2880_PCI_IO_BASE,
  123. .end = RT2880_PCI_IO_BASE + RT2880_PCI_IO_SIZE - 1,
  124. .flags = IORESOURCE_IO,
  125. };
  126. static struct pci_controller rt2880_pci_controller = {
  127. .pci_ops = &rt2880_pci_ops,
  128. .mem_resource = &rt2880_pci_mem_resource,
  129. .io_resource = &rt2880_pci_io_resource,
  130. };
  131. static inline u32 rt2880_pci_read_u32(unsigned long reg)
  132. {
  133. unsigned long flags;
  134. u32 address;
  135. u32 ret;
  136. address = rt2880_pci_get_cfgaddr(0, 0, 0, reg);
  137. spin_lock_irqsave(&rt2880_pci_lock, flags);
  138. rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
  139. ret = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
  140. spin_unlock_irqrestore(&rt2880_pci_lock, flags);
  141. return ret;
  142. }
  143. static inline void rt2880_pci_write_u32(unsigned long reg, u32 val)
  144. {
  145. unsigned long flags;
  146. u32 address;
  147. address = rt2880_pci_get_cfgaddr(0, 0, 0, reg);
  148. spin_lock_irqsave(&rt2880_pci_lock, flags);
  149. rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
  150. rt2880_pci_reg_write(val, RT2880_PCI_REG_CONFIG_DATA);
  151. spin_unlock_irqrestore(&rt2880_pci_lock, flags);
  152. }
  153. int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  154. {
  155. u16 cmd;
  156. int irq = -1;
  157. if (dev->bus->number != 0)
  158. return irq;
  159. switch (PCI_SLOT(dev->devfn)) {
  160. case 0x00:
  161. rt2880_pci_write_u32(PCI_BASE_ADDRESS_0, 0x08000000);
  162. (void) rt2880_pci_read_u32(PCI_BASE_ADDRESS_0);
  163. break;
  164. case 0x11:
  165. irq = RT288X_CPU_IRQ_PCI;
  166. break;
  167. default:
  168. pr_err("%s:%s[%d] trying to alloc unknown pci irq\n",
  169. __FILE__, __func__, __LINE__);
  170. BUG();
  171. break;
  172. }
  173. pci_write_config_byte((struct pci_dev *) dev,
  174. PCI_CACHE_LINE_SIZE, 0x14);
  175. pci_write_config_byte((struct pci_dev *) dev, PCI_LATENCY_TIMER, 0xFF);
  176. pci_read_config_word((struct pci_dev *) dev, PCI_COMMAND, &cmd);
  177. cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  178. PCI_COMMAND_INVALIDATE | PCI_COMMAND_FAST_BACK |
  179. PCI_COMMAND_SERR | PCI_COMMAND_WAIT | PCI_COMMAND_PARITY;
  180. pci_write_config_word((struct pci_dev *) dev, PCI_COMMAND, cmd);
  181. pci_write_config_byte((struct pci_dev *) dev, PCI_INTERRUPT_LINE,
  182. dev->irq);
  183. return irq;
  184. }
  185. static int rt288x_pci_probe(struct platform_device *pdev)
  186. {
  187. void __iomem *io_map_base;
  188. rt2880_pci_base = ioremap_nocache(RT2880_PCI_BASE, PAGE_SIZE);
  189. io_map_base = ioremap(RT2880_PCI_IO_BASE, RT2880_PCI_IO_SIZE);
  190. rt2880_pci_controller.io_map_base = (unsigned long) io_map_base;
  191. set_io_port_base((unsigned long) io_map_base);
  192. ioport_resource.start = RT2880_PCI_IO_BASE;
  193. ioport_resource.end = RT2880_PCI_IO_BASE + RT2880_PCI_IO_SIZE - 1;
  194. rt2880_pci_reg_write(0, RT2880_PCI_REG_PCICFG_ADDR);
  195. udelay(1);
  196. rt2880_pci_reg_write(0x79, RT2880_PCI_REG_ARBCTL);
  197. rt2880_pci_reg_write(0x07FF0001, RT2880_PCI_REG_BAR0SETUP_ADDR);
  198. rt2880_pci_reg_write(RT2880_PCI_MEM_BASE, RT2880_PCI_REG_MEMBASE);
  199. rt2880_pci_reg_write(RT2880_PCI_IO_BASE, RT2880_PCI_REG_IOBASE);
  200. rt2880_pci_reg_write(0x08000000, RT2880_PCI_REG_IMBASEBAR0_ADDR);
  201. rt2880_pci_reg_write(0x08021814, RT2880_PCI_REG_ID);
  202. rt2880_pci_reg_write(0x00800001, RT2880_PCI_REG_CLASS);
  203. rt2880_pci_reg_write(0x28801814, RT2880_PCI_REG_SUBID);
  204. rt2880_pci_reg_write(0x000c0000, RT2880_PCI_REG_PCIMSK_ADDR);
  205. rt2880_pci_write_u32(PCI_BASE_ADDRESS_0, 0x08000000);
  206. (void) rt2880_pci_read_u32(PCI_BASE_ADDRESS_0);
  207. register_pci_controller(&rt2880_pci_controller);
  208. return 0;
  209. }
  210. int pcibios_plat_dev_init(struct pci_dev *dev)
  211. {
  212. return 0;
  213. }
  214. static const struct of_device_id rt288x_pci_match[] = {
  215. { .compatible = "ralink,rt288x-pci" },
  216. {},
  217. };
  218. MODULE_DEVICE_TABLE(of, rt288x_pci_match);
  219. static struct platform_driver rt288x_pci_driver = {
  220. .probe = rt288x_pci_probe,
  221. .driver = {
  222. .name = "rt288x-pci",
  223. .of_match_table = rt288x_pci_match,
  224. },
  225. };
  226. int __init pcibios_init(void)
  227. {
  228. int ret = platform_driver_register(&rt288x_pci_driver);
  229. if (ret)
  230. pr_info("rt288x-pci: Error registering platform driver!");
  231. return ret;
  232. }
  233. arch_initcall(pcibios_init);