0031-PCI-MIPS-adds-rt2880-pci-support.patch 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. From 5b0bcc314005dd14eeae190948165a81eef7da1f Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  3. Date: Sun, 27 Jul 2014 09:36:02 +0100
  4. Subject: [PATCH 31/57] PCI: MIPS: adds rt2880 pci support
  5. Add support for the pci found on the rt2880 SoC.
  6. Signed-off-by: John Crispin <blogic@openwrt.org>
  7. ---
  8. arch/mips/pci/Makefile | 1 +
  9. arch/mips/pci/pci-rt2880.c | 281 ++++++++++++++++++++++++++++++++++++++++++++
  10. arch/mips/ralink/Kconfig | 1 +
  11. 3 files changed, 283 insertions(+)
  12. create mode 100644 arch/mips/pci/pci-rt2880.c
  13. --- a/arch/mips/pci/Makefile
  14. +++ b/arch/mips/pci/Makefile
  15. @@ -43,6 +43,7 @@ obj-$(CONFIG_SNI_RM) += fixup-sni.o ops
  16. obj-$(CONFIG_LANTIQ) += fixup-lantiq.o
  17. obj-$(CONFIG_PCI_LANTIQ) += pci-lantiq.o ops-lantiq.o
  18. obj-$(CONFIG_SOC_MT7621) += pci-mt7621.o
  19. +obj-$(CONFIG_SOC_RT2880) += pci-rt2880.o
  20. obj-$(CONFIG_SOC_RT3883) += pci-rt3883.o
  21. obj-$(CONFIG_TANBAC_TB0219) += fixup-tb0219.o
  22. obj-$(CONFIG_TANBAC_TB0226) += fixup-tb0226.o
  23. --- /dev/null
  24. +++ b/arch/mips/pci/pci-rt2880.c
  25. @@ -0,0 +1,281 @@
  26. +/*
  27. + * Ralink RT288x SoC PCI register definitions
  28. + *
  29. + * Copyright (C) 2009 John Crispin <blogic@openwrt.org>
  30. + * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
  31. + *
  32. + * Parts of this file are based on Ralink's 2.6.21 BSP
  33. + *
  34. + * This program is free software; you can redistribute it and/or modify it
  35. + * under the terms of the GNU General Public License version 2 as published
  36. + * by the Free Software Foundation.
  37. + */
  38. +
  39. +#include <linux/types.h>
  40. +#include <linux/pci.h>
  41. +#include <linux/io.h>
  42. +#include <linux/init.h>
  43. +#include <linux/module.h>
  44. +#include <linux/of_platform.h>
  45. +#include <linux/of_irq.h>
  46. +#include <linux/of_pci.h>
  47. +
  48. +#include <asm/mach-ralink/rt288x.h>
  49. +
  50. +#define RT2880_PCI_BASE 0x00440000
  51. +#define RT288X_CPU_IRQ_PCI 4
  52. +
  53. +#define RT2880_PCI_MEM_BASE 0x20000000
  54. +#define RT2880_PCI_MEM_SIZE 0x10000000
  55. +#define RT2880_PCI_IO_BASE 0x00460000
  56. +#define RT2880_PCI_IO_SIZE 0x00010000
  57. +
  58. +#define RT2880_PCI_REG_PCICFG_ADDR 0x00
  59. +#define RT2880_PCI_REG_PCIMSK_ADDR 0x0c
  60. +#define RT2880_PCI_REG_BAR0SETUP_ADDR 0x10
  61. +#define RT2880_PCI_REG_IMBASEBAR0_ADDR 0x18
  62. +#define RT2880_PCI_REG_CONFIG_ADDR 0x20
  63. +#define RT2880_PCI_REG_CONFIG_DATA 0x24
  64. +#define RT2880_PCI_REG_MEMBASE 0x28
  65. +#define RT2880_PCI_REG_IOBASE 0x2c
  66. +#define RT2880_PCI_REG_ID 0x30
  67. +#define RT2880_PCI_REG_CLASS 0x34
  68. +#define RT2880_PCI_REG_SUBID 0x38
  69. +#define RT2880_PCI_REG_ARBCTL 0x80
  70. +
  71. +static void __iomem *rt2880_pci_base;
  72. +static DEFINE_SPINLOCK(rt2880_pci_lock);
  73. +
  74. +static u32 rt2880_pci_reg_read(u32 reg)
  75. +{
  76. + return readl(rt2880_pci_base + reg);
  77. +}
  78. +
  79. +static void rt2880_pci_reg_write(u32 val, u32 reg)
  80. +{
  81. + writel(val, rt2880_pci_base + reg);
  82. +}
  83. +
  84. +static inline u32 rt2880_pci_get_cfgaddr(unsigned int bus, unsigned int slot,
  85. + unsigned int func, unsigned int where)
  86. +{
  87. + return ((bus << 16) | (slot << 11) | (func << 8) | (where & 0xfc) |
  88. + 0x80000000);
  89. +}
  90. +
  91. +static int rt2880_pci_config_read(struct pci_bus *bus, unsigned int devfn,
  92. + int where, int size, u32 *val)
  93. +{
  94. + unsigned long flags;
  95. + u32 address;
  96. + u32 data;
  97. +
  98. + address = rt2880_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
  99. + PCI_FUNC(devfn), where);
  100. +
  101. + spin_lock_irqsave(&rt2880_pci_lock, flags);
  102. + rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
  103. + data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
  104. + spin_unlock_irqrestore(&rt2880_pci_lock, flags);
  105. +
  106. + switch (size) {
  107. + case 1:
  108. + *val = (data >> ((where & 3) << 3)) & 0xff;
  109. + break;
  110. + case 2:
  111. + *val = (data >> ((where & 3) << 3)) & 0xffff;
  112. + break;
  113. + case 4:
  114. + *val = data;
  115. + break;
  116. + }
  117. +
  118. + return PCIBIOS_SUCCESSFUL;
  119. +}
  120. +
  121. +static int rt2880_pci_config_write(struct pci_bus *bus, unsigned int devfn,
  122. + int where, int size, u32 val)
  123. +{
  124. + unsigned long flags;
  125. + u32 address;
  126. + u32 data;
  127. +
  128. + address = rt2880_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
  129. + PCI_FUNC(devfn), where);
  130. +
  131. + spin_lock_irqsave(&rt2880_pci_lock, flags);
  132. + rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
  133. + data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
  134. +
  135. + switch (size) {
  136. + case 1:
  137. + data = (data & ~(0xff << ((where & 3) << 3))) |
  138. + (val << ((where & 3) << 3));
  139. + break;
  140. + case 2:
  141. + data = (data & ~(0xffff << ((where & 3) << 3))) |
  142. + (val << ((where & 3) << 3));
  143. + break;
  144. + case 4:
  145. + data = val;
  146. + break;
  147. + }
  148. +
  149. + rt2880_pci_reg_write(data, RT2880_PCI_REG_CONFIG_DATA);
  150. + spin_unlock_irqrestore(&rt2880_pci_lock, flags);
  151. +
  152. + return PCIBIOS_SUCCESSFUL;
  153. +}
  154. +
  155. +static struct pci_ops rt2880_pci_ops = {
  156. + .read = rt2880_pci_config_read,
  157. + .write = rt2880_pci_config_write,
  158. +};
  159. +
  160. +static struct resource rt2880_pci_mem_resource = {
  161. + .name = "PCI MEM space",
  162. + .start = RT2880_PCI_MEM_BASE,
  163. + .end = RT2880_PCI_MEM_BASE + RT2880_PCI_MEM_SIZE - 1,
  164. + .flags = IORESOURCE_MEM,
  165. +};
  166. +
  167. +static struct resource rt2880_pci_io_resource = {
  168. + .name = "PCI IO space",
  169. + .start = RT2880_PCI_IO_BASE,
  170. + .end = RT2880_PCI_IO_BASE + RT2880_PCI_IO_SIZE - 1,
  171. + .flags = IORESOURCE_IO,
  172. +};
  173. +
  174. +static struct pci_controller rt2880_pci_controller = {
  175. + .pci_ops = &rt2880_pci_ops,
  176. + .mem_resource = &rt2880_pci_mem_resource,
  177. + .io_resource = &rt2880_pci_io_resource,
  178. +};
  179. +
  180. +static inline u32 rt2880_pci_read_u32(unsigned long reg)
  181. +{
  182. + unsigned long flags;
  183. + u32 address;
  184. + u32 ret;
  185. +
  186. + address = rt2880_pci_get_cfgaddr(0, 0, 0, reg);
  187. +
  188. + spin_lock_irqsave(&rt2880_pci_lock, flags);
  189. + rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
  190. + ret = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
  191. + spin_unlock_irqrestore(&rt2880_pci_lock, flags);
  192. +
  193. + return ret;
  194. +}
  195. +
  196. +static inline void rt2880_pci_write_u32(unsigned long reg, u32 val)
  197. +{
  198. + unsigned long flags;
  199. + u32 address;
  200. +
  201. + address = rt2880_pci_get_cfgaddr(0, 0, 0, reg);
  202. +
  203. + spin_lock_irqsave(&rt2880_pci_lock, flags);
  204. + rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
  205. + rt2880_pci_reg_write(val, RT2880_PCI_REG_CONFIG_DATA);
  206. + spin_unlock_irqrestore(&rt2880_pci_lock, flags);
  207. +}
  208. +
  209. +int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  210. +{
  211. + u16 cmd;
  212. + int irq = -1;
  213. +
  214. + if (dev->bus->number != 0)
  215. + return irq;
  216. +
  217. + switch (PCI_SLOT(dev->devfn)) {
  218. + case 0x00:
  219. + rt2880_pci_write_u32(PCI_BASE_ADDRESS_0, 0x08000000);
  220. + (void) rt2880_pci_read_u32(PCI_BASE_ADDRESS_0);
  221. + break;
  222. + case 0x11:
  223. + irq = RT288X_CPU_IRQ_PCI;
  224. + break;
  225. + default:
  226. + printk("%s:%s[%d] trying to alloc unknown pci irq\n",
  227. + __FILE__, __func__, __LINE__);
  228. + BUG();
  229. + break;
  230. + }
  231. +
  232. + pci_write_config_byte((struct pci_dev*)dev, PCI_CACHE_LINE_SIZE, 0x14);
  233. + pci_write_config_byte((struct pci_dev*)dev, PCI_LATENCY_TIMER, 0xFF);
  234. + pci_read_config_word((struct pci_dev*)dev, PCI_COMMAND, &cmd);
  235. + cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  236. + PCI_COMMAND_INVALIDATE | PCI_COMMAND_FAST_BACK |
  237. + PCI_COMMAND_SERR | PCI_COMMAND_WAIT | PCI_COMMAND_PARITY;
  238. + pci_write_config_word((struct pci_dev*)dev, PCI_COMMAND, cmd);
  239. + pci_write_config_byte((struct pci_dev*)dev, PCI_INTERRUPT_LINE,
  240. + dev->irq);
  241. + return irq;
  242. +}
  243. +
  244. +static int rt288x_pci_probe(struct platform_device *pdev)
  245. +{
  246. + void __iomem *io_map_base;
  247. + int i;
  248. +
  249. + rt2880_pci_base = ioremap_nocache(RT2880_PCI_BASE, PAGE_SIZE);
  250. +
  251. + io_map_base = ioremap(RT2880_PCI_IO_BASE, RT2880_PCI_IO_SIZE);
  252. + rt2880_pci_controller.io_map_base = (unsigned long) io_map_base;
  253. + set_io_port_base((unsigned long) io_map_base);
  254. +
  255. + ioport_resource.start = RT2880_PCI_IO_BASE;
  256. + ioport_resource.end = RT2880_PCI_IO_BASE + RT2880_PCI_IO_SIZE - 1;
  257. +
  258. + rt2880_pci_reg_write(0, RT2880_PCI_REG_PCICFG_ADDR);
  259. + for(i = 0; i < 0xfffff; i++) {}
  260. +
  261. + rt2880_pci_reg_write(0x79, RT2880_PCI_REG_ARBCTL);
  262. + rt2880_pci_reg_write(0x07FF0001, RT2880_PCI_REG_BAR0SETUP_ADDR);
  263. + rt2880_pci_reg_write(RT2880_PCI_MEM_BASE, RT2880_PCI_REG_MEMBASE);
  264. + rt2880_pci_reg_write(RT2880_PCI_IO_BASE, RT2880_PCI_REG_IOBASE);
  265. + rt2880_pci_reg_write(0x08000000, RT2880_PCI_REG_IMBASEBAR0_ADDR);
  266. + rt2880_pci_reg_write(0x08021814, RT2880_PCI_REG_ID);
  267. + rt2880_pci_reg_write(0x00800001, RT2880_PCI_REG_CLASS);
  268. + rt2880_pci_reg_write(0x28801814, RT2880_PCI_REG_SUBID);
  269. + rt2880_pci_reg_write(0x000c0000, RT2880_PCI_REG_PCIMSK_ADDR);
  270. +
  271. + rt2880_pci_write_u32(PCI_BASE_ADDRESS_0, 0x08000000);
  272. + (void) rt2880_pci_read_u32(PCI_BASE_ADDRESS_0);
  273. +
  274. + register_pci_controller(&rt2880_pci_controller);
  275. + return 0;
  276. +}
  277. +
  278. +int pcibios_plat_dev_init(struct pci_dev *dev)
  279. +{
  280. + return 0;
  281. +}
  282. +
  283. +static const struct of_device_id rt288x_pci_match[] = {
  284. + { .compatible = "ralink,rt288x-pci" },
  285. + {},
  286. +};
  287. +MODULE_DEVICE_TABLE(of, rt288x_pci_match);
  288. +
  289. +static struct platform_driver rt288x_pci_driver = {
  290. + .probe = rt288x_pci_probe,
  291. + .driver = {
  292. + .name = "rt288x-pci",
  293. + .owner = THIS_MODULE,
  294. + .of_match_table = rt288x_pci_match,
  295. + },
  296. +};
  297. +
  298. +int __init pcibios_init(void)
  299. +{
  300. + int ret = platform_driver_register(&rt288x_pci_driver);
  301. + if (ret)
  302. + pr_info("rt288x-pci: Error registering platform driver!");
  303. + return ret;
  304. +}
  305. +
  306. +arch_initcall(pcibios_init);
  307. --- a/arch/mips/ralink/Kconfig
  308. +++ b/arch/mips/ralink/Kconfig
  309. @@ -31,6 +31,7 @@ choice
  310. config SOC_RT288X
  311. bool "RT288x"
  312. select MIPS_L1_CACHE_SHIFT_4
  313. + select HW_HAS_PCI
  314. config SOC_RT305X
  315. bool "RT305x"