pci-ar724x.c 6.5 KB

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