pci-ar724x.c 10 KB

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