pci-ar724x.c 8.4 KB

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