pci-ar724x.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_INT_STATUS 0x4c
  23. #define AR724X_PCI_REG_INT_MASK 0x50
  24. #define AR724X_PCI_INT_DEV0 BIT(14)
  25. #define AR724X_PCI_IRQ_COUNT 1
  26. #define AR7240_BAR0_WAR_VALUE 0xffff
  27. static DEFINE_SPINLOCK(ar724x_pci_lock);
  28. static void __iomem *ar724x_pci_devcfg_base;
  29. static void __iomem *ar724x_pci_ctrl_base;
  30. static u32 ar724x_pci_bar0_value;
  31. static bool ar724x_pci_bar0_is_cached;
  32. static int ar724x_pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  33. int size, uint32_t *value)
  34. {
  35. unsigned long flags;
  36. void __iomem *base;
  37. u32 data;
  38. if (devfn)
  39. return PCIBIOS_DEVICE_NOT_FOUND;
  40. base = ar724x_pci_devcfg_base;
  41. spin_lock_irqsave(&ar724x_pci_lock, flags);
  42. data = __raw_readl(base + (where & ~3));
  43. switch (size) {
  44. case 1:
  45. if (where & 1)
  46. data >>= 8;
  47. if (where & 2)
  48. data >>= 16;
  49. data &= 0xff;
  50. break;
  51. case 2:
  52. if (where & 2)
  53. data >>= 16;
  54. data &= 0xffff;
  55. break;
  56. case 4:
  57. break;
  58. default:
  59. spin_unlock_irqrestore(&ar724x_pci_lock, flags);
  60. return PCIBIOS_BAD_REGISTER_NUMBER;
  61. }
  62. spin_unlock_irqrestore(&ar724x_pci_lock, flags);
  63. if (where == PCI_BASE_ADDRESS_0 && size == 4 &&
  64. ar724x_pci_bar0_is_cached) {
  65. /* use the cached value */
  66. *value = ar724x_pci_bar0_value;
  67. } else {
  68. *value = data;
  69. }
  70. return PCIBIOS_SUCCESSFUL;
  71. }
  72. static int ar724x_pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  73. int size, uint32_t value)
  74. {
  75. unsigned long flags;
  76. void __iomem *base;
  77. u32 data;
  78. int s;
  79. if (devfn)
  80. return PCIBIOS_DEVICE_NOT_FOUND;
  81. if (soc_is_ar7240() && where == PCI_BASE_ADDRESS_0 && size == 4) {
  82. if (value != 0xffffffff) {
  83. /*
  84. * WAR for a hw issue. If the BAR0 register of the
  85. * device is set to the proper base address, the
  86. * memory space of the device is not accessible.
  87. *
  88. * Cache the intended value so it can be read back,
  89. * and write a SoC specific constant value to the
  90. * BAR0 register in order to make the device memory
  91. * accessible.
  92. */
  93. ar724x_pci_bar0_is_cached = true;
  94. ar724x_pci_bar0_value = value;
  95. value = AR7240_BAR0_WAR_VALUE;
  96. } else {
  97. ar724x_pci_bar0_is_cached = false;
  98. }
  99. }
  100. base = ar724x_pci_devcfg_base;
  101. spin_lock_irqsave(&ar724x_pci_lock, flags);
  102. data = __raw_readl(base + (where & ~3));
  103. switch (size) {
  104. case 1:
  105. s = ((where & 3) * 8);
  106. data &= ~(0xff << s);
  107. data |= ((value & 0xff) << s);
  108. break;
  109. case 2:
  110. s = ((where & 2) * 8);
  111. data &= ~(0xffff << s);
  112. data |= ((value & 0xffff) << s);
  113. break;
  114. case 4:
  115. data = value;
  116. break;
  117. default:
  118. spin_unlock_irqrestore(&ar724x_pci_lock, flags);
  119. return PCIBIOS_BAD_REGISTER_NUMBER;
  120. }
  121. __raw_writel(data, base + (where & ~3));
  122. /* flush write */
  123. __raw_readl(base + (where & ~3));
  124. spin_unlock_irqrestore(&ar724x_pci_lock, flags);
  125. return PCIBIOS_SUCCESSFUL;
  126. }
  127. static struct pci_ops ar724x_pci_ops = {
  128. .read = ar724x_pci_read,
  129. .write = ar724x_pci_write,
  130. };
  131. static struct resource ar724x_io_resource = {
  132. .name = "PCI IO space",
  133. .start = 0,
  134. .end = 0,
  135. .flags = IORESOURCE_IO,
  136. };
  137. static struct resource ar724x_mem_resource = {
  138. .name = "PCI memory space",
  139. .start = AR724X_PCI_MEM_BASE,
  140. .end = AR724X_PCI_MEM_BASE + AR724X_PCI_MEM_SIZE - 1,
  141. .flags = IORESOURCE_MEM,
  142. };
  143. static struct pci_controller ar724x_pci_controller = {
  144. .pci_ops = &ar724x_pci_ops,
  145. .io_resource = &ar724x_io_resource,
  146. .mem_resource = &ar724x_mem_resource,
  147. };
  148. static void ar724x_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
  149. {
  150. void __iomem *base;
  151. u32 pending;
  152. base = ar724x_pci_ctrl_base;
  153. pending = __raw_readl(base + AR724X_PCI_REG_INT_STATUS) &
  154. __raw_readl(base + AR724X_PCI_REG_INT_MASK);
  155. if (pending & AR724X_PCI_INT_DEV0)
  156. generic_handle_irq(ATH79_PCI_IRQ(0));
  157. else
  158. spurious_interrupt();
  159. }
  160. static void ar724x_pci_irq_unmask(struct irq_data *d)
  161. {
  162. void __iomem *base;
  163. u32 t;
  164. base = ar724x_pci_ctrl_base;
  165. switch (d->irq) {
  166. case ATH79_PCI_IRQ(0):
  167. t = __raw_readl(base + AR724X_PCI_REG_INT_MASK);
  168. __raw_writel(t | AR724X_PCI_INT_DEV0,
  169. base + AR724X_PCI_REG_INT_MASK);
  170. /* flush write */
  171. __raw_readl(base + AR724X_PCI_REG_INT_MASK);
  172. }
  173. }
  174. static void ar724x_pci_irq_mask(struct irq_data *d)
  175. {
  176. void __iomem *base;
  177. u32 t;
  178. base = ar724x_pci_ctrl_base;
  179. switch (d->irq) {
  180. case ATH79_PCI_IRQ(0):
  181. t = __raw_readl(base + AR724X_PCI_REG_INT_MASK);
  182. __raw_writel(t & ~AR724X_PCI_INT_DEV0,
  183. base + AR724X_PCI_REG_INT_MASK);
  184. /* flush write */
  185. __raw_readl(base + AR724X_PCI_REG_INT_MASK);
  186. t = __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
  187. __raw_writel(t | AR724X_PCI_INT_DEV0,
  188. base + AR724X_PCI_REG_INT_STATUS);
  189. /* flush write */
  190. __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
  191. }
  192. }
  193. static struct irq_chip ar724x_pci_irq_chip = {
  194. .name = "AR724X PCI ",
  195. .irq_mask = ar724x_pci_irq_mask,
  196. .irq_unmask = ar724x_pci_irq_unmask,
  197. .irq_mask_ack = ar724x_pci_irq_mask,
  198. };
  199. static void __init ar724x_pci_irq_init(int irq)
  200. {
  201. void __iomem *base;
  202. int i;
  203. base = ar724x_pci_ctrl_base;
  204. __raw_writel(0, base + AR724X_PCI_REG_INT_MASK);
  205. __raw_writel(0, base + AR724X_PCI_REG_INT_STATUS);
  206. BUILD_BUG_ON(ATH79_PCI_IRQ_COUNT < AR724X_PCI_IRQ_COUNT);
  207. for (i = ATH79_PCI_IRQ_BASE;
  208. i < ATH79_PCI_IRQ_BASE + AR724X_PCI_IRQ_COUNT; i++)
  209. irq_set_chip_and_handler(i, &ar724x_pci_irq_chip,
  210. handle_level_irq);
  211. irq_set_chained_handler(irq, ar724x_pci_irq_handler);
  212. }
  213. int __init ar724x_pcibios_init(int irq)
  214. {
  215. int ret;
  216. ret = -ENOMEM;
  217. ar724x_pci_devcfg_base = ioremap(AR724X_PCI_CFG_BASE,
  218. AR724X_PCI_CFG_SIZE);
  219. if (ar724x_pci_devcfg_base == NULL)
  220. goto err;
  221. ar724x_pci_ctrl_base = ioremap(AR724X_PCI_CTRL_BASE,
  222. AR724X_PCI_CTRL_SIZE);
  223. if (ar724x_pci_ctrl_base == NULL)
  224. goto err_unmap_devcfg;
  225. ar724x_pci_irq_init(irq);
  226. register_pci_controller(&ar724x_pci_controller);
  227. return PCIBIOS_SUCCESSFUL;
  228. err_unmap_devcfg:
  229. iounmap(ar724x_pci_devcfg_base);
  230. err:
  231. return ret;
  232. }