pci-ar71xx.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * Atheros AR71xx PCI host controller driver
  3. *
  4. * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
  5. * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
  6. *
  7. * Parts of this file are based on Atheros' 2.6.15 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/resource.h>
  14. #include <linux/types.h>
  15. #include <linux/delay.h>
  16. #include <linux/bitops.h>
  17. #include <linux/pci.h>
  18. #include <linux/pci_regs.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <asm/mach-ath79/ar71xx_regs.h>
  23. #include <asm/mach-ath79/ath79.h>
  24. #define AR71XX_PCI_REG_CRP_AD_CBE 0x00
  25. #define AR71XX_PCI_REG_CRP_WRDATA 0x04
  26. #define AR71XX_PCI_REG_CRP_RDDATA 0x08
  27. #define AR71XX_PCI_REG_CFG_AD 0x0c
  28. #define AR71XX_PCI_REG_CFG_CBE 0x10
  29. #define AR71XX_PCI_REG_CFG_WRDATA 0x14
  30. #define AR71XX_PCI_REG_CFG_RDDATA 0x18
  31. #define AR71XX_PCI_REG_PCI_ERR 0x1c
  32. #define AR71XX_PCI_REG_PCI_ERR_ADDR 0x20
  33. #define AR71XX_PCI_REG_AHB_ERR 0x24
  34. #define AR71XX_PCI_REG_AHB_ERR_ADDR 0x28
  35. #define AR71XX_PCI_CRP_CMD_WRITE 0x00010000
  36. #define AR71XX_PCI_CRP_CMD_READ 0x00000000
  37. #define AR71XX_PCI_CFG_CMD_READ 0x0000000a
  38. #define AR71XX_PCI_CFG_CMD_WRITE 0x0000000b
  39. #define AR71XX_PCI_INT_CORE BIT(4)
  40. #define AR71XX_PCI_INT_DEV2 BIT(2)
  41. #define AR71XX_PCI_INT_DEV1 BIT(1)
  42. #define AR71XX_PCI_INT_DEV0 BIT(0)
  43. #define AR71XX_PCI_IRQ_COUNT 5
  44. static DEFINE_SPINLOCK(ar71xx_pci_lock);
  45. static void __iomem *ar71xx_pcicfg_base;
  46. /* Byte lane enable bits */
  47. static const u8 ar71xx_pci_ble_table[4][4] = {
  48. {0x0, 0xf, 0xf, 0xf},
  49. {0xe, 0xd, 0xb, 0x7},
  50. {0xc, 0xf, 0x3, 0xf},
  51. {0xf, 0xf, 0xf, 0xf},
  52. };
  53. static const u32 ar71xx_pci_read_mask[8] = {
  54. 0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0
  55. };
  56. static inline u32 ar71xx_pci_get_ble(int where, int size, int local)
  57. {
  58. u32 t;
  59. t = ar71xx_pci_ble_table[size & 3][where & 3];
  60. BUG_ON(t == 0xf);
  61. t <<= (local) ? 20 : 4;
  62. return t;
  63. }
  64. static inline u32 ar71xx_pci_bus_addr(struct pci_bus *bus, unsigned int devfn,
  65. int where)
  66. {
  67. u32 ret;
  68. if (!bus->number) {
  69. /* type 0 */
  70. ret = (1 << PCI_SLOT(devfn)) | (PCI_FUNC(devfn) << 8) |
  71. (where & ~3);
  72. } else {
  73. /* type 1 */
  74. ret = (bus->number << 16) | (PCI_SLOT(devfn) << 11) |
  75. (PCI_FUNC(devfn) << 8) | (where & ~3) | 1;
  76. }
  77. return ret;
  78. }
  79. static int ar71xx_pci_check_error(int quiet)
  80. {
  81. void __iomem *base = ar71xx_pcicfg_base;
  82. u32 pci_err;
  83. u32 ahb_err;
  84. pci_err = __raw_readl(base + AR71XX_PCI_REG_PCI_ERR) & 3;
  85. if (pci_err) {
  86. if (!quiet) {
  87. u32 addr;
  88. addr = __raw_readl(base + AR71XX_PCI_REG_PCI_ERR_ADDR);
  89. pr_crit("ar71xx: %s bus error %d at addr 0x%x\n",
  90. "PCI", pci_err, addr);
  91. }
  92. /* clear PCI error status */
  93. __raw_writel(pci_err, base + AR71XX_PCI_REG_PCI_ERR);
  94. }
  95. ahb_err = __raw_readl(base + AR71XX_PCI_REG_AHB_ERR) & 1;
  96. if (ahb_err) {
  97. if (!quiet) {
  98. u32 addr;
  99. addr = __raw_readl(base + AR71XX_PCI_REG_AHB_ERR_ADDR);
  100. pr_crit("ar71xx: %s bus error %d at addr 0x%x\n",
  101. "AHB", ahb_err, addr);
  102. }
  103. /* clear AHB error status */
  104. __raw_writel(ahb_err, base + AR71XX_PCI_REG_AHB_ERR);
  105. }
  106. return !!(ahb_err | pci_err);
  107. }
  108. static inline void ar71xx_pci_local_write(int where, int size, u32 value)
  109. {
  110. void __iomem *base = ar71xx_pcicfg_base;
  111. u32 ad_cbe;
  112. value = value << (8 * (where & 3));
  113. ad_cbe = AR71XX_PCI_CRP_CMD_WRITE | (where & ~3);
  114. ad_cbe |= ar71xx_pci_get_ble(where, size, 1);
  115. __raw_writel(ad_cbe, base + AR71XX_PCI_REG_CRP_AD_CBE);
  116. __raw_writel(value, base + AR71XX_PCI_REG_CRP_WRDATA);
  117. }
  118. static inline int ar71xx_pci_set_cfgaddr(struct pci_bus *bus,
  119. unsigned int devfn,
  120. int where, int size, u32 cmd)
  121. {
  122. void __iomem *base = ar71xx_pcicfg_base;
  123. u32 addr;
  124. addr = ar71xx_pci_bus_addr(bus, devfn, where);
  125. __raw_writel(addr, base + AR71XX_PCI_REG_CFG_AD);
  126. __raw_writel(cmd | ar71xx_pci_get_ble(where, size, 0),
  127. base + AR71XX_PCI_REG_CFG_CBE);
  128. return ar71xx_pci_check_error(1);
  129. }
  130. static int ar71xx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  131. int where, int size, u32 *value)
  132. {
  133. void __iomem *base = ar71xx_pcicfg_base;
  134. unsigned long flags;
  135. u32 data;
  136. int err;
  137. int ret;
  138. ret = PCIBIOS_SUCCESSFUL;
  139. data = ~0;
  140. spin_lock_irqsave(&ar71xx_pci_lock, flags);
  141. err = ar71xx_pci_set_cfgaddr(bus, devfn, where, size,
  142. AR71XX_PCI_CFG_CMD_READ);
  143. if (err)
  144. ret = PCIBIOS_DEVICE_NOT_FOUND;
  145. else
  146. data = __raw_readl(base + AR71XX_PCI_REG_CFG_RDDATA);
  147. spin_unlock_irqrestore(&ar71xx_pci_lock, flags);
  148. *value = (data >> (8 * (where & 3))) & ar71xx_pci_read_mask[size & 7];
  149. return ret;
  150. }
  151. static int ar71xx_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  152. int where, int size, u32 value)
  153. {
  154. void __iomem *base = ar71xx_pcicfg_base;
  155. unsigned long flags;
  156. int err;
  157. int ret;
  158. value = value << (8 * (where & 3));
  159. ret = PCIBIOS_SUCCESSFUL;
  160. spin_lock_irqsave(&ar71xx_pci_lock, flags);
  161. err = ar71xx_pci_set_cfgaddr(bus, devfn, where, size,
  162. AR71XX_PCI_CFG_CMD_WRITE);
  163. if (err)
  164. ret = PCIBIOS_DEVICE_NOT_FOUND;
  165. else
  166. __raw_writel(value, base + AR71XX_PCI_REG_CFG_WRDATA);
  167. spin_unlock_irqrestore(&ar71xx_pci_lock, flags);
  168. return ret;
  169. }
  170. static struct pci_ops ar71xx_pci_ops = {
  171. .read = ar71xx_pci_read_config,
  172. .write = ar71xx_pci_write_config,
  173. };
  174. static struct resource ar71xx_pci_io_resource = {
  175. .name = "PCI IO space",
  176. .start = 0,
  177. .end = 0,
  178. .flags = IORESOURCE_IO,
  179. };
  180. static struct resource ar71xx_pci_mem_resource = {
  181. .name = "PCI memory space",
  182. .start = AR71XX_PCI_MEM_BASE,
  183. .end = AR71XX_PCI_MEM_BASE + AR71XX_PCI_MEM_SIZE - 1,
  184. .flags = IORESOURCE_MEM
  185. };
  186. static struct pci_controller ar71xx_pci_controller = {
  187. .pci_ops = &ar71xx_pci_ops,
  188. .mem_resource = &ar71xx_pci_mem_resource,
  189. .io_resource = &ar71xx_pci_io_resource,
  190. };
  191. static void ar71xx_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
  192. {
  193. void __iomem *base = ath79_reset_base;
  194. u32 pending;
  195. pending = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_STATUS) &
  196. __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  197. if (pending & AR71XX_PCI_INT_DEV0)
  198. generic_handle_irq(ATH79_PCI_IRQ(0));
  199. else if (pending & AR71XX_PCI_INT_DEV1)
  200. generic_handle_irq(ATH79_PCI_IRQ(1));
  201. else if (pending & AR71XX_PCI_INT_DEV2)
  202. generic_handle_irq(ATH79_PCI_IRQ(2));
  203. else if (pending & AR71XX_PCI_INT_CORE)
  204. generic_handle_irq(ATH79_PCI_IRQ(4));
  205. else
  206. spurious_interrupt();
  207. }
  208. static void ar71xx_pci_irq_unmask(struct irq_data *d)
  209. {
  210. unsigned int irq = d->irq - ATH79_PCI_IRQ_BASE;
  211. void __iomem *base = ath79_reset_base;
  212. u32 t;
  213. t = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  214. __raw_writel(t | (1 << irq), base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  215. /* flush write */
  216. __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  217. }
  218. static void ar71xx_pci_irq_mask(struct irq_data *d)
  219. {
  220. unsigned int irq = d->irq - ATH79_PCI_IRQ_BASE;
  221. void __iomem *base = ath79_reset_base;
  222. u32 t;
  223. t = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  224. __raw_writel(t & ~(1 << irq), base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  225. /* flush write */
  226. __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  227. }
  228. static struct irq_chip ar71xx_pci_irq_chip = {
  229. .name = "AR71XX PCI",
  230. .irq_mask = ar71xx_pci_irq_mask,
  231. .irq_unmask = ar71xx_pci_irq_unmask,
  232. .irq_mask_ack = ar71xx_pci_irq_mask,
  233. };
  234. static void ar71xx_pci_irq_init(int irq)
  235. {
  236. void __iomem *base = ath79_reset_base;
  237. int i;
  238. __raw_writel(0, base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  239. __raw_writel(0, base + AR71XX_RESET_REG_PCI_INT_STATUS);
  240. BUILD_BUG_ON(ATH79_PCI_IRQ_COUNT < AR71XX_PCI_IRQ_COUNT);
  241. for (i = ATH79_PCI_IRQ_BASE;
  242. i < ATH79_PCI_IRQ_BASE + AR71XX_PCI_IRQ_COUNT; i++)
  243. irq_set_chip_and_handler(i, &ar71xx_pci_irq_chip,
  244. handle_level_irq);
  245. irq_set_chained_handler(irq, ar71xx_pci_irq_handler);
  246. }
  247. static void ar71xx_pci_reset(void)
  248. {
  249. void __iomem *ddr_base = ath79_ddr_base;
  250. ath79_device_reset_set(AR71XX_RESET_PCI_BUS | AR71XX_RESET_PCI_CORE);
  251. mdelay(100);
  252. ath79_device_reset_clear(AR71XX_RESET_PCI_BUS | AR71XX_RESET_PCI_CORE);
  253. mdelay(100);
  254. __raw_writel(AR71XX_PCI_WIN0_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN0);
  255. __raw_writel(AR71XX_PCI_WIN1_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN1);
  256. __raw_writel(AR71XX_PCI_WIN2_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN2);
  257. __raw_writel(AR71XX_PCI_WIN3_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN3);
  258. __raw_writel(AR71XX_PCI_WIN4_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN4);
  259. __raw_writel(AR71XX_PCI_WIN5_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN5);
  260. __raw_writel(AR71XX_PCI_WIN6_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN6);
  261. __raw_writel(AR71XX_PCI_WIN7_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN7);
  262. mdelay(100);
  263. }
  264. static int ar71xx_pci_probe(struct platform_device *pdev)
  265. {
  266. struct resource *res;
  267. int irq;
  268. u32 t;
  269. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_base");
  270. if (!res)
  271. return -EINVAL;
  272. ar71xx_pcicfg_base = devm_request_and_ioremap(&pdev->dev, res);
  273. if (!ar71xx_pcicfg_base)
  274. return -ENOMEM;
  275. irq = platform_get_irq(pdev, 0);
  276. if (irq < 0)
  277. return -EINVAL;
  278. ar71xx_pci_reset();
  279. /* setup COMMAND register */
  280. t = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
  281. | PCI_COMMAND_PARITY | PCI_COMMAND_SERR | PCI_COMMAND_FAST_BACK;
  282. ar71xx_pci_local_write(PCI_COMMAND, 4, t);
  283. /* clear bus errors */
  284. ar71xx_pci_check_error(1);
  285. ar71xx_pci_irq_init(irq);
  286. register_pci_controller(&ar71xx_pci_controller);
  287. return 0;
  288. }
  289. static struct platform_driver ar71xx_pci_driver = {
  290. .probe = ar71xx_pci_probe,
  291. .driver = {
  292. .name = "ar71xx-pci",
  293. .owner = THIS_MODULE,
  294. },
  295. };
  296. static int __init ar71xx_pci_init(void)
  297. {
  298. return platform_driver_register(&ar71xx_pci_driver);
  299. }
  300. postcore_initcall(ar71xx_pci_init);