pci-rt2880.c 6.8 KB

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