pci-ar71xx.c 9.6 KB

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