pci-ar724x.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/spinlock.h>
  12. #include <linux/irq.h>
  13. #include <linux/pci.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <asm/mach-ath79/ath79.h>
  17. #include <asm/mach-ath79/ar71xx_regs.h>
  18. #define AR724X_PCI_REG_RESET 0x18
  19. #define AR724X_PCI_REG_INT_STATUS 0x4c
  20. #define AR724X_PCI_REG_INT_MASK 0x50
  21. #define AR724X_PCI_RESET_LINK_UP BIT(0)
  22. #define AR724X_PCI_INT_DEV0 BIT(14)
  23. #define AR724X_PCI_IRQ_COUNT 1
  24. #define AR7240_BAR0_WAR_VALUE 0xffff
  25. struct ar724x_pci_controller {
  26. void __iomem *devcfg_base;
  27. void __iomem *ctrl_base;
  28. int irq;
  29. bool link_up;
  30. bool bar0_is_cached;
  31. u32 bar0_value;
  32. spinlock_t lock;
  33. struct pci_controller pci_controller;
  34. struct resource io_res;
  35. struct resource mem_res;
  36. };
  37. static inline bool ar724x_pci_check_link(struct ar724x_pci_controller *apc)
  38. {
  39. u32 reset;
  40. reset = __raw_readl(apc->ctrl_base + AR724X_PCI_REG_RESET);
  41. return reset & AR724X_PCI_RESET_LINK_UP;
  42. }
  43. static inline struct ar724x_pci_controller *
  44. pci_bus_to_ar724x_controller(struct pci_bus *bus)
  45. {
  46. struct pci_controller *hose;
  47. hose = (struct pci_controller *) bus->sysdata;
  48. return container_of(hose, struct ar724x_pci_controller, pci_controller);
  49. }
  50. static int ar724x_pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  51. int size, uint32_t *value)
  52. {
  53. struct ar724x_pci_controller *apc;
  54. unsigned long flags;
  55. void __iomem *base;
  56. u32 data;
  57. apc = pci_bus_to_ar724x_controller(bus);
  58. if (!apc->link_up)
  59. return PCIBIOS_DEVICE_NOT_FOUND;
  60. if (devfn)
  61. return PCIBIOS_DEVICE_NOT_FOUND;
  62. base = apc->devcfg_base;
  63. spin_lock_irqsave(&apc->lock, flags);
  64. data = __raw_readl(base + (where & ~3));
  65. switch (size) {
  66. case 1:
  67. if (where & 1)
  68. data >>= 8;
  69. if (where & 2)
  70. data >>= 16;
  71. data &= 0xff;
  72. break;
  73. case 2:
  74. if (where & 2)
  75. data >>= 16;
  76. data &= 0xffff;
  77. break;
  78. case 4:
  79. break;
  80. default:
  81. spin_unlock_irqrestore(&apc->lock, flags);
  82. return PCIBIOS_BAD_REGISTER_NUMBER;
  83. }
  84. spin_unlock_irqrestore(&apc->lock, flags);
  85. if (where == PCI_BASE_ADDRESS_0 && size == 4 &&
  86. apc->bar0_is_cached) {
  87. /* use the cached value */
  88. *value = apc->bar0_value;
  89. } else {
  90. *value = data;
  91. }
  92. return PCIBIOS_SUCCESSFUL;
  93. }
  94. static int ar724x_pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  95. int size, uint32_t value)
  96. {
  97. struct ar724x_pci_controller *apc;
  98. unsigned long flags;
  99. void __iomem *base;
  100. u32 data;
  101. int s;
  102. apc = pci_bus_to_ar724x_controller(bus);
  103. if (!apc->link_up)
  104. return PCIBIOS_DEVICE_NOT_FOUND;
  105. if (devfn)
  106. return PCIBIOS_DEVICE_NOT_FOUND;
  107. if (soc_is_ar7240() && where == PCI_BASE_ADDRESS_0 && size == 4) {
  108. if (value != 0xffffffff) {
  109. /*
  110. * WAR for a hw issue. If the BAR0 register of the
  111. * device is set to the proper base address, the
  112. * memory space of the device is not accessible.
  113. *
  114. * Cache the intended value so it can be read back,
  115. * and write a SoC specific constant value to the
  116. * BAR0 register in order to make the device memory
  117. * accessible.
  118. */
  119. apc->bar0_is_cached = true;
  120. apc->bar0_value = value;
  121. value = AR7240_BAR0_WAR_VALUE;
  122. } else {
  123. apc->bar0_is_cached = false;
  124. }
  125. }
  126. base = apc->devcfg_base;
  127. spin_lock_irqsave(&apc->lock, flags);
  128. data = __raw_readl(base + (where & ~3));
  129. switch (size) {
  130. case 1:
  131. s = ((where & 3) * 8);
  132. data &= ~(0xff << s);
  133. data |= ((value & 0xff) << s);
  134. break;
  135. case 2:
  136. s = ((where & 2) * 8);
  137. data &= ~(0xffff << s);
  138. data |= ((value & 0xffff) << s);
  139. break;
  140. case 4:
  141. data = value;
  142. break;
  143. default:
  144. spin_unlock_irqrestore(&apc->lock, flags);
  145. return PCIBIOS_BAD_REGISTER_NUMBER;
  146. }
  147. __raw_writel(data, base + (where & ~3));
  148. /* flush write */
  149. __raw_readl(base + (where & ~3));
  150. spin_unlock_irqrestore(&apc->lock, flags);
  151. return PCIBIOS_SUCCESSFUL;
  152. }
  153. static struct pci_ops ar724x_pci_ops = {
  154. .read = ar724x_pci_read,
  155. .write = ar724x_pci_write,
  156. };
  157. static void ar724x_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
  158. {
  159. struct ar724x_pci_controller *apc;
  160. void __iomem *base;
  161. u32 pending;
  162. apc = irq_get_handler_data(irq);
  163. base = apc->ctrl_base;
  164. pending = __raw_readl(base + AR724X_PCI_REG_INT_STATUS) &
  165. __raw_readl(base + AR724X_PCI_REG_INT_MASK);
  166. if (pending & AR724X_PCI_INT_DEV0)
  167. generic_handle_irq(ATH79_PCI_IRQ(0));
  168. else
  169. spurious_interrupt();
  170. }
  171. static void ar724x_pci_irq_unmask(struct irq_data *d)
  172. {
  173. struct ar724x_pci_controller *apc;
  174. void __iomem *base;
  175. u32 t;
  176. apc = irq_data_get_irq_chip_data(d);
  177. base = apc->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. struct ar724x_pci_controller *apc;
  190. void __iomem *base;
  191. u32 t;
  192. apc = irq_data_get_irq_chip_data(d);
  193. base = apc->ctrl_base;
  194. switch (d->irq) {
  195. case ATH79_PCI_IRQ(0):
  196. t = __raw_readl(base + AR724X_PCI_REG_INT_MASK);
  197. __raw_writel(t & ~AR724X_PCI_INT_DEV0,
  198. base + AR724X_PCI_REG_INT_MASK);
  199. /* flush write */
  200. __raw_readl(base + AR724X_PCI_REG_INT_MASK);
  201. t = __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
  202. __raw_writel(t | AR724X_PCI_INT_DEV0,
  203. base + AR724X_PCI_REG_INT_STATUS);
  204. /* flush write */
  205. __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
  206. }
  207. }
  208. static struct irq_chip ar724x_pci_irq_chip = {
  209. .name = "AR724X PCI ",
  210. .irq_mask = ar724x_pci_irq_mask,
  211. .irq_unmask = ar724x_pci_irq_unmask,
  212. .irq_mask_ack = ar724x_pci_irq_mask,
  213. };
  214. static void ar724x_pci_irq_init(struct ar724x_pci_controller *apc)
  215. {
  216. void __iomem *base;
  217. int i;
  218. base = apc->ctrl_base;
  219. __raw_writel(0, base + AR724X_PCI_REG_INT_MASK);
  220. __raw_writel(0, base + AR724X_PCI_REG_INT_STATUS);
  221. BUILD_BUG_ON(ATH79_PCI_IRQ_COUNT < AR724X_PCI_IRQ_COUNT);
  222. for (i = ATH79_PCI_IRQ_BASE;
  223. i < ATH79_PCI_IRQ_BASE + AR724X_PCI_IRQ_COUNT; i++) {
  224. irq_set_chip_and_handler(i, &ar724x_pci_irq_chip,
  225. handle_level_irq);
  226. irq_set_chip_data(i, apc);
  227. }
  228. irq_set_handler_data(apc->irq, apc);
  229. irq_set_chained_handler(apc->irq, ar724x_pci_irq_handler);
  230. }
  231. static int ar724x_pci_probe(struct platform_device *pdev)
  232. {
  233. struct ar724x_pci_controller *apc;
  234. struct resource *res;
  235. apc = devm_kzalloc(&pdev->dev, sizeof(struct ar724x_pci_controller),
  236. GFP_KERNEL);
  237. if (!apc)
  238. return -ENOMEM;
  239. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl_base");
  240. if (!res)
  241. return -EINVAL;
  242. apc->ctrl_base = devm_request_and_ioremap(&pdev->dev, res);
  243. if (apc->ctrl_base == NULL)
  244. return -EBUSY;
  245. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_base");
  246. if (!res)
  247. return -EINVAL;
  248. apc->devcfg_base = devm_request_and_ioremap(&pdev->dev, res);
  249. if (!apc->devcfg_base)
  250. return -EBUSY;
  251. apc->irq = platform_get_irq(pdev, 0);
  252. if (apc->irq < 0)
  253. return -EINVAL;
  254. spin_lock_init(&apc->lock);
  255. res = platform_get_resource_byname(pdev, IORESOURCE_IO, "io_base");
  256. if (!res)
  257. return -EINVAL;
  258. apc->io_res.parent = res;
  259. apc->io_res.name = "PCI IO space";
  260. apc->io_res.start = res->start;
  261. apc->io_res.end = res->end;
  262. apc->io_res.flags = IORESOURCE_IO;
  263. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem_base");
  264. if (!res)
  265. return -EINVAL;
  266. apc->mem_res.parent = res;
  267. apc->mem_res.name = "PCI memory space";
  268. apc->mem_res.start = res->start;
  269. apc->mem_res.end = res->end;
  270. apc->mem_res.flags = IORESOURCE_MEM;
  271. apc->pci_controller.pci_ops = &ar724x_pci_ops;
  272. apc->pci_controller.io_resource = &apc->io_res;
  273. apc->pci_controller.mem_resource = &apc->mem_res;
  274. apc->link_up = ar724x_pci_check_link(apc);
  275. if (!apc->link_up)
  276. dev_warn(&pdev->dev, "PCIe link is down\n");
  277. ar724x_pci_irq_init(apc);
  278. register_pci_controller(&apc->pci_controller);
  279. return 0;
  280. }
  281. static struct platform_driver ar724x_pci_driver = {
  282. .probe = ar724x_pci_probe,
  283. .driver = {
  284. .name = "ar724x-pci",
  285. .owner = THIS_MODULE,
  286. },
  287. };
  288. static int __init ar724x_pci_init(void)
  289. {
  290. return platform_driver_register(&ar724x_pci_driver);
  291. }
  292. postcore_initcall(ar724x_pci_init);