pci-ar724x.c 9.8 KB

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