pci-ar724x.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Atheros AR724X PCI host controller driver
  3. *
  4. * Copyright (C) 2011 René Bolldorf <xsecute@googlemail.com>
  5. * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. */
  11. #include <linux/irq.h>
  12. #include <linux/pci.h>
  13. #include <asm/mach-ath79/ath79.h>
  14. #include <asm/mach-ath79/ar71xx_regs.h>
  15. #include <asm/mach-ath79/pci.h>
  16. #define AR724X_PCI_CFG_BASE 0x14000000
  17. #define AR724X_PCI_CFG_SIZE 0x1000
  18. #define AR724X_PCI_CTRL_BASE (AR71XX_APB_BASE + 0x000f0000)
  19. #define AR724X_PCI_CTRL_SIZE 0x100
  20. #define AR724X_PCI_MEM_BASE 0x10000000
  21. #define AR724X_PCI_MEM_SIZE 0x08000000
  22. #define AR724X_PCI_REG_RESET 0x18
  23. #define AR724X_PCI_REG_INT_STATUS 0x4c
  24. #define AR724X_PCI_REG_INT_MASK 0x50
  25. #define AR724X_PCI_RESET_LINK_UP BIT(0)
  26. #define AR724X_PCI_INT_DEV0 BIT(14)
  27. #define AR724X_PCI_IRQ_COUNT 1
  28. #define AR7240_BAR0_WAR_VALUE 0xffff
  29. static DEFINE_SPINLOCK(ar724x_pci_lock);
  30. static void __iomem *ar724x_pci_devcfg_base;
  31. static void __iomem *ar724x_pci_ctrl_base;
  32. static u32 ar724x_pci_bar0_value;
  33. static bool ar724x_pci_bar0_is_cached;
  34. static bool ar724x_pci_link_up;
  35. static inline bool ar724x_pci_check_link(void)
  36. {
  37. u32 reset;
  38. reset = __raw_readl(ar724x_pci_ctrl_base + AR724X_PCI_REG_RESET);
  39. return reset & AR724X_PCI_RESET_LINK_UP;
  40. }
  41. static int ar724x_pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  42. int size, uint32_t *value)
  43. {
  44. unsigned long flags;
  45. void __iomem *base;
  46. u32 data;
  47. if (!ar724x_pci_link_up)
  48. return PCIBIOS_DEVICE_NOT_FOUND;
  49. if (devfn)
  50. return PCIBIOS_DEVICE_NOT_FOUND;
  51. base = ar724x_pci_devcfg_base;
  52. spin_lock_irqsave(&ar724x_pci_lock, flags);
  53. data = __raw_readl(base + (where & ~3));
  54. switch (size) {
  55. case 1:
  56. if (where & 1)
  57. data >>= 8;
  58. if (where & 2)
  59. data >>= 16;
  60. data &= 0xff;
  61. break;
  62. case 2:
  63. if (where & 2)
  64. data >>= 16;
  65. data &= 0xffff;
  66. break;
  67. case 4:
  68. break;
  69. default:
  70. spin_unlock_irqrestore(&ar724x_pci_lock, flags);
  71. return PCIBIOS_BAD_REGISTER_NUMBER;
  72. }
  73. spin_unlock_irqrestore(&ar724x_pci_lock, flags);
  74. if (where == PCI_BASE_ADDRESS_0 && size == 4 &&
  75. ar724x_pci_bar0_is_cached) {
  76. /* use the cached value */
  77. *value = ar724x_pci_bar0_value;
  78. } else {
  79. *value = data;
  80. }
  81. return PCIBIOS_SUCCESSFUL;
  82. }
  83. static int ar724x_pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  84. int size, uint32_t value)
  85. {
  86. unsigned long flags;
  87. void __iomem *base;
  88. u32 data;
  89. int s;
  90. if (!ar724x_pci_link_up)
  91. return PCIBIOS_DEVICE_NOT_FOUND;
  92. if (devfn)
  93. return PCIBIOS_DEVICE_NOT_FOUND;
  94. if (soc_is_ar7240() && where == PCI_BASE_ADDRESS_0 && size == 4) {
  95. if (value != 0xffffffff) {
  96. /*
  97. * WAR for a hw issue. If the BAR0 register of the
  98. * device is set to the proper base address, the
  99. * memory space of the device is not accessible.
  100. *
  101. * Cache the intended value so it can be read back,
  102. * and write a SoC specific constant value to the
  103. * BAR0 register in order to make the device memory
  104. * accessible.
  105. */
  106. ar724x_pci_bar0_is_cached = true;
  107. ar724x_pci_bar0_value = value;
  108. value = AR7240_BAR0_WAR_VALUE;
  109. } else {
  110. ar724x_pci_bar0_is_cached = false;
  111. }
  112. }
  113. base = ar724x_pci_devcfg_base;
  114. spin_lock_irqsave(&ar724x_pci_lock, flags);
  115. data = __raw_readl(base + (where & ~3));
  116. switch (size) {
  117. case 1:
  118. s = ((where & 3) * 8);
  119. data &= ~(0xff << s);
  120. data |= ((value & 0xff) << s);
  121. break;
  122. case 2:
  123. s = ((where & 2) * 8);
  124. data &= ~(0xffff << s);
  125. data |= ((value & 0xffff) << s);
  126. break;
  127. case 4:
  128. data = value;
  129. break;
  130. default:
  131. spin_unlock_irqrestore(&ar724x_pci_lock, flags);
  132. return PCIBIOS_BAD_REGISTER_NUMBER;
  133. }
  134. __raw_writel(data, base + (where & ~3));
  135. /* flush write */
  136. __raw_readl(base + (where & ~3));
  137. spin_unlock_irqrestore(&ar724x_pci_lock, flags);
  138. return PCIBIOS_SUCCESSFUL;
  139. }
  140. static struct pci_ops ar724x_pci_ops = {
  141. .read = ar724x_pci_read,
  142. .write = ar724x_pci_write,
  143. };
  144. static struct resource ar724x_io_resource = {
  145. .name = "PCI IO space",
  146. .start = 0,
  147. .end = 0,
  148. .flags = IORESOURCE_IO,
  149. };
  150. static struct resource ar724x_mem_resource = {
  151. .name = "PCI memory space",
  152. .start = AR724X_PCI_MEM_BASE,
  153. .end = AR724X_PCI_MEM_BASE + AR724X_PCI_MEM_SIZE - 1,
  154. .flags = IORESOURCE_MEM,
  155. };
  156. static struct pci_controller ar724x_pci_controller = {
  157. .pci_ops = &ar724x_pci_ops,
  158. .io_resource = &ar724x_io_resource,
  159. .mem_resource = &ar724x_mem_resource,
  160. };
  161. static void ar724x_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
  162. {
  163. void __iomem *base;
  164. u32 pending;
  165. base = ar724x_pci_ctrl_base;
  166. pending = __raw_readl(base + AR724X_PCI_REG_INT_STATUS) &
  167. __raw_readl(base + AR724X_PCI_REG_INT_MASK);
  168. if (pending & AR724X_PCI_INT_DEV0)
  169. generic_handle_irq(ATH79_PCI_IRQ(0));
  170. else
  171. spurious_interrupt();
  172. }
  173. static void ar724x_pci_irq_unmask(struct irq_data *d)
  174. {
  175. void __iomem *base;
  176. u32 t;
  177. base = ar724x_pci_ctrl_base;
  178. switch (d->irq) {
  179. case ATH79_PCI_IRQ(0):
  180. t = __raw_readl(base + AR724X_PCI_REG_INT_MASK);
  181. __raw_writel(t | AR724X_PCI_INT_DEV0,
  182. base + AR724X_PCI_REG_INT_MASK);
  183. /* flush write */
  184. __raw_readl(base + AR724X_PCI_REG_INT_MASK);
  185. }
  186. }
  187. static void ar724x_pci_irq_mask(struct irq_data *d)
  188. {
  189. void __iomem *base;
  190. u32 t;
  191. base = ar724x_pci_ctrl_base;
  192. switch (d->irq) {
  193. case ATH79_PCI_IRQ(0):
  194. t = __raw_readl(base + AR724X_PCI_REG_INT_MASK);
  195. __raw_writel(t & ~AR724X_PCI_INT_DEV0,
  196. base + AR724X_PCI_REG_INT_MASK);
  197. /* flush write */
  198. __raw_readl(base + AR724X_PCI_REG_INT_MASK);
  199. t = __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
  200. __raw_writel(t | AR724X_PCI_INT_DEV0,
  201. base + AR724X_PCI_REG_INT_STATUS);
  202. /* flush write */
  203. __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
  204. }
  205. }
  206. static struct irq_chip ar724x_pci_irq_chip = {
  207. .name = "AR724X PCI ",
  208. .irq_mask = ar724x_pci_irq_mask,
  209. .irq_unmask = ar724x_pci_irq_unmask,
  210. .irq_mask_ack = ar724x_pci_irq_mask,
  211. };
  212. static void __init ar724x_pci_irq_init(int irq)
  213. {
  214. void __iomem *base;
  215. int i;
  216. base = ar724x_pci_ctrl_base;
  217. __raw_writel(0, base + AR724X_PCI_REG_INT_MASK);
  218. __raw_writel(0, base + AR724X_PCI_REG_INT_STATUS);
  219. BUILD_BUG_ON(ATH79_PCI_IRQ_COUNT < AR724X_PCI_IRQ_COUNT);
  220. for (i = ATH79_PCI_IRQ_BASE;
  221. i < ATH79_PCI_IRQ_BASE + AR724X_PCI_IRQ_COUNT; i++)
  222. irq_set_chip_and_handler(i, &ar724x_pci_irq_chip,
  223. handle_level_irq);
  224. irq_set_chained_handler(irq, ar724x_pci_irq_handler);
  225. }
  226. int __init ar724x_pcibios_init(int irq)
  227. {
  228. int ret;
  229. ret = -ENOMEM;
  230. ar724x_pci_devcfg_base = ioremap(AR724X_PCI_CFG_BASE,
  231. AR724X_PCI_CFG_SIZE);
  232. if (ar724x_pci_devcfg_base == NULL)
  233. goto err;
  234. ar724x_pci_ctrl_base = ioremap(AR724X_PCI_CTRL_BASE,
  235. AR724X_PCI_CTRL_SIZE);
  236. if (ar724x_pci_ctrl_base == NULL)
  237. goto err_unmap_devcfg;
  238. ar724x_pci_link_up = ar724x_pci_check_link();
  239. if (!ar724x_pci_link_up)
  240. pr_warn("ar724x: PCIe link is down\n");
  241. ar724x_pci_irq_init(irq);
  242. register_pci_controller(&ar724x_pci_controller);
  243. return PCIBIOS_SUCCESSFUL;
  244. err_unmap_devcfg:
  245. iounmap(ar724x_pci_devcfg_base);
  246. err:
  247. return ret;
  248. }