pci-rt2880.c 7.5 KB

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