pci-ar724x.c 9.2 KB

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