pci-ar71xx.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Atheros AR71xx PCI host controller driver
  3. *
  4. * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
  5. * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
  6. *
  7. * Parts of this file are based on Atheros' 2.6.15 BSP
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. */
  13. #include <linux/resource.h>
  14. #include <linux/types.h>
  15. #include <linux/delay.h>
  16. #include <linux/bitops.h>
  17. #include <linux/pci.h>
  18. #include <linux/pci_regs.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <asm/mach-ath79/ar71xx_regs.h>
  23. #include <asm/mach-ath79/ath79.h>
  24. #define AR71XX_PCI_REG_CRP_AD_CBE 0x00
  25. #define AR71XX_PCI_REG_CRP_WRDATA 0x04
  26. #define AR71XX_PCI_REG_CRP_RDDATA 0x08
  27. #define AR71XX_PCI_REG_CFG_AD 0x0c
  28. #define AR71XX_PCI_REG_CFG_CBE 0x10
  29. #define AR71XX_PCI_REG_CFG_WRDATA 0x14
  30. #define AR71XX_PCI_REG_CFG_RDDATA 0x18
  31. #define AR71XX_PCI_REG_PCI_ERR 0x1c
  32. #define AR71XX_PCI_REG_PCI_ERR_ADDR 0x20
  33. #define AR71XX_PCI_REG_AHB_ERR 0x24
  34. #define AR71XX_PCI_REG_AHB_ERR_ADDR 0x28
  35. #define AR71XX_PCI_CRP_CMD_WRITE 0x00010000
  36. #define AR71XX_PCI_CRP_CMD_READ 0x00000000
  37. #define AR71XX_PCI_CFG_CMD_READ 0x0000000a
  38. #define AR71XX_PCI_CFG_CMD_WRITE 0x0000000b
  39. #define AR71XX_PCI_INT_CORE BIT(4)
  40. #define AR71XX_PCI_INT_DEV2 BIT(2)
  41. #define AR71XX_PCI_INT_DEV1 BIT(1)
  42. #define AR71XX_PCI_INT_DEV0 BIT(0)
  43. #define AR71XX_PCI_IRQ_COUNT 5
  44. struct ar71xx_pci_controller {
  45. void __iomem *cfg_base;
  46. int irq;
  47. int irq_base;
  48. struct pci_controller pci_ctrl;
  49. struct resource io_res;
  50. struct resource mem_res;
  51. };
  52. /* Byte lane enable bits */
  53. static const u8 ar71xx_pci_ble_table[4][4] = {
  54. {0x0, 0xf, 0xf, 0xf},
  55. {0xe, 0xd, 0xb, 0x7},
  56. {0xc, 0xf, 0x3, 0xf},
  57. {0xf, 0xf, 0xf, 0xf},
  58. };
  59. static const u32 ar71xx_pci_read_mask[8] = {
  60. 0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0
  61. };
  62. static inline u32 ar71xx_pci_get_ble(int where, int size, int local)
  63. {
  64. u32 t;
  65. t = ar71xx_pci_ble_table[size & 3][where & 3];
  66. BUG_ON(t == 0xf);
  67. t <<= (local) ? 20 : 4;
  68. return t;
  69. }
  70. static inline u32 ar71xx_pci_bus_addr(struct pci_bus *bus, unsigned int devfn,
  71. int where)
  72. {
  73. u32 ret;
  74. if (!bus->number) {
  75. /* type 0 */
  76. ret = (1 << PCI_SLOT(devfn)) | (PCI_FUNC(devfn) << 8) |
  77. (where & ~3);
  78. } else {
  79. /* type 1 */
  80. ret = (bus->number << 16) | (PCI_SLOT(devfn) << 11) |
  81. (PCI_FUNC(devfn) << 8) | (where & ~3) | 1;
  82. }
  83. return ret;
  84. }
  85. static inline struct ar71xx_pci_controller *
  86. pci_bus_to_ar71xx_controller(struct pci_bus *bus)
  87. {
  88. struct pci_controller *hose;
  89. hose = (struct pci_controller *) bus->sysdata;
  90. return container_of(hose, struct ar71xx_pci_controller, pci_ctrl);
  91. }
  92. static int ar71xx_pci_check_error(struct ar71xx_pci_controller *apc, int quiet)
  93. {
  94. void __iomem *base = apc->cfg_base;
  95. u32 pci_err;
  96. u32 ahb_err;
  97. pci_err = __raw_readl(base + AR71XX_PCI_REG_PCI_ERR) & 3;
  98. if (pci_err) {
  99. if (!quiet) {
  100. u32 addr;
  101. addr = __raw_readl(base + AR71XX_PCI_REG_PCI_ERR_ADDR);
  102. pr_crit("ar71xx: %s bus error %d at addr 0x%x\n",
  103. "PCI", pci_err, addr);
  104. }
  105. /* clear PCI error status */
  106. __raw_writel(pci_err, base + AR71XX_PCI_REG_PCI_ERR);
  107. }
  108. ahb_err = __raw_readl(base + AR71XX_PCI_REG_AHB_ERR) & 1;
  109. if (ahb_err) {
  110. if (!quiet) {
  111. u32 addr;
  112. addr = __raw_readl(base + AR71XX_PCI_REG_AHB_ERR_ADDR);
  113. pr_crit("ar71xx: %s bus error %d at addr 0x%x\n",
  114. "AHB", ahb_err, addr);
  115. }
  116. /* clear AHB error status */
  117. __raw_writel(ahb_err, base + AR71XX_PCI_REG_AHB_ERR);
  118. }
  119. return !!(ahb_err | pci_err);
  120. }
  121. static inline void ar71xx_pci_local_write(struct ar71xx_pci_controller *apc,
  122. int where, int size, u32 value)
  123. {
  124. void __iomem *base = apc->cfg_base;
  125. u32 ad_cbe;
  126. value = value << (8 * (where & 3));
  127. ad_cbe = AR71XX_PCI_CRP_CMD_WRITE | (where & ~3);
  128. ad_cbe |= ar71xx_pci_get_ble(where, size, 1);
  129. __raw_writel(ad_cbe, base + AR71XX_PCI_REG_CRP_AD_CBE);
  130. __raw_writel(value, base + AR71XX_PCI_REG_CRP_WRDATA);
  131. }
  132. static inline int ar71xx_pci_set_cfgaddr(struct pci_bus *bus,
  133. unsigned int devfn,
  134. int where, int size, u32 cmd)
  135. {
  136. struct ar71xx_pci_controller *apc = pci_bus_to_ar71xx_controller(bus);
  137. void __iomem *base = apc->cfg_base;
  138. u32 addr;
  139. addr = ar71xx_pci_bus_addr(bus, devfn, where);
  140. __raw_writel(addr, base + AR71XX_PCI_REG_CFG_AD);
  141. __raw_writel(cmd | ar71xx_pci_get_ble(where, size, 0),
  142. base + AR71XX_PCI_REG_CFG_CBE);
  143. return ar71xx_pci_check_error(apc, 1);
  144. }
  145. static int ar71xx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  146. int where, int size, u32 *value)
  147. {
  148. struct ar71xx_pci_controller *apc = pci_bus_to_ar71xx_controller(bus);
  149. void __iomem *base = apc->cfg_base;
  150. u32 data;
  151. int err;
  152. int ret;
  153. ret = PCIBIOS_SUCCESSFUL;
  154. data = ~0;
  155. err = ar71xx_pci_set_cfgaddr(bus, devfn, where, size,
  156. AR71XX_PCI_CFG_CMD_READ);
  157. if (err)
  158. ret = PCIBIOS_DEVICE_NOT_FOUND;
  159. else
  160. data = __raw_readl(base + AR71XX_PCI_REG_CFG_RDDATA);
  161. *value = (data >> (8 * (where & 3))) & ar71xx_pci_read_mask[size & 7];
  162. return ret;
  163. }
  164. static int ar71xx_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  165. int where, int size, u32 value)
  166. {
  167. struct ar71xx_pci_controller *apc = pci_bus_to_ar71xx_controller(bus);
  168. void __iomem *base = apc->cfg_base;
  169. int err;
  170. int ret;
  171. value = value << (8 * (where & 3));
  172. ret = PCIBIOS_SUCCESSFUL;
  173. err = ar71xx_pci_set_cfgaddr(bus, devfn, where, size,
  174. AR71XX_PCI_CFG_CMD_WRITE);
  175. if (err)
  176. ret = PCIBIOS_DEVICE_NOT_FOUND;
  177. else
  178. __raw_writel(value, base + AR71XX_PCI_REG_CFG_WRDATA);
  179. return ret;
  180. }
  181. static struct pci_ops ar71xx_pci_ops = {
  182. .read = ar71xx_pci_read_config,
  183. .write = ar71xx_pci_write_config,
  184. };
  185. static void ar71xx_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
  186. {
  187. struct ar71xx_pci_controller *apc;
  188. void __iomem *base = ath79_reset_base;
  189. u32 pending;
  190. apc = irq_desc_get_handler_data(desc);
  191. pending = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_STATUS) &
  192. __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  193. if (pending & AR71XX_PCI_INT_DEV0)
  194. generic_handle_irq(apc->irq_base + 0);
  195. else if (pending & AR71XX_PCI_INT_DEV1)
  196. generic_handle_irq(apc->irq_base + 1);
  197. else if (pending & AR71XX_PCI_INT_DEV2)
  198. generic_handle_irq(apc->irq_base + 2);
  199. else if (pending & AR71XX_PCI_INT_CORE)
  200. generic_handle_irq(apc->irq_base + 4);
  201. else
  202. spurious_interrupt();
  203. }
  204. static void ar71xx_pci_irq_unmask(struct irq_data *d)
  205. {
  206. struct ar71xx_pci_controller *apc;
  207. unsigned int irq;
  208. void __iomem *base = ath79_reset_base;
  209. u32 t;
  210. apc = irq_data_get_irq_chip_data(d);
  211. irq = d->irq - apc->irq_base;
  212. t = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  213. __raw_writel(t | (1 << irq), base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  214. /* flush write */
  215. __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  216. }
  217. static void ar71xx_pci_irq_mask(struct irq_data *d)
  218. {
  219. struct ar71xx_pci_controller *apc;
  220. unsigned int irq;
  221. void __iomem *base = ath79_reset_base;
  222. u32 t;
  223. apc = irq_data_get_irq_chip_data(d);
  224. irq = d->irq - apc->irq_base;
  225. t = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  226. __raw_writel(t & ~(1 << irq), base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  227. /* flush write */
  228. __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  229. }
  230. static struct irq_chip ar71xx_pci_irq_chip = {
  231. .name = "AR71XX PCI",
  232. .irq_mask = ar71xx_pci_irq_mask,
  233. .irq_unmask = ar71xx_pci_irq_unmask,
  234. .irq_mask_ack = ar71xx_pci_irq_mask,
  235. };
  236. static void ar71xx_pci_irq_init(struct ar71xx_pci_controller *apc)
  237. {
  238. void __iomem *base = ath79_reset_base;
  239. int i;
  240. __raw_writel(0, base + AR71XX_RESET_REG_PCI_INT_ENABLE);
  241. __raw_writel(0, base + AR71XX_RESET_REG_PCI_INT_STATUS);
  242. BUILD_BUG_ON(ATH79_PCI_IRQ_COUNT < AR71XX_PCI_IRQ_COUNT);
  243. apc->irq_base = ATH79_PCI_IRQ_BASE;
  244. for (i = apc->irq_base;
  245. i < apc->irq_base + AR71XX_PCI_IRQ_COUNT; i++) {
  246. irq_set_chip_and_handler(i, &ar71xx_pci_irq_chip,
  247. handle_level_irq);
  248. irq_set_chip_data(i, apc);
  249. }
  250. irq_set_handler_data(apc->irq, apc);
  251. irq_set_chained_handler(apc->irq, ar71xx_pci_irq_handler);
  252. }
  253. static void ar71xx_pci_reset(void)
  254. {
  255. ath79_device_reset_set(AR71XX_RESET_PCI_BUS | AR71XX_RESET_PCI_CORE);
  256. mdelay(100);
  257. ath79_device_reset_clear(AR71XX_RESET_PCI_BUS | AR71XX_RESET_PCI_CORE);
  258. mdelay(100);
  259. ath79_ddr_set_pci_windows();
  260. mdelay(100);
  261. }
  262. static int ar71xx_pci_probe(struct platform_device *pdev)
  263. {
  264. struct ar71xx_pci_controller *apc;
  265. struct resource *res;
  266. u32 t;
  267. apc = devm_kzalloc(&pdev->dev, sizeof(struct ar71xx_pci_controller),
  268. GFP_KERNEL);
  269. if (!apc)
  270. return -ENOMEM;
  271. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_base");
  272. apc->cfg_base = devm_ioremap_resource(&pdev->dev, res);
  273. if (IS_ERR(apc->cfg_base))
  274. return PTR_ERR(apc->cfg_base);
  275. apc->irq = platform_get_irq(pdev, 0);
  276. if (apc->irq < 0)
  277. return -EINVAL;
  278. res = platform_get_resource_byname(pdev, IORESOURCE_IO, "io_base");
  279. if (!res)
  280. return -EINVAL;
  281. apc->io_res.parent = res;
  282. apc->io_res.name = "PCI IO space";
  283. apc->io_res.start = res->start;
  284. apc->io_res.end = res->end;
  285. apc->io_res.flags = IORESOURCE_IO;
  286. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem_base");
  287. if (!res)
  288. return -EINVAL;
  289. apc->mem_res.parent = res;
  290. apc->mem_res.name = "PCI memory space";
  291. apc->mem_res.start = res->start;
  292. apc->mem_res.end = res->end;
  293. apc->mem_res.flags = IORESOURCE_MEM;
  294. ar71xx_pci_reset();
  295. /* setup COMMAND register */
  296. t = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
  297. | PCI_COMMAND_PARITY | PCI_COMMAND_SERR | PCI_COMMAND_FAST_BACK;
  298. ar71xx_pci_local_write(apc, PCI_COMMAND, 4, t);
  299. /* clear bus errors */
  300. ar71xx_pci_check_error(apc, 1);
  301. ar71xx_pci_irq_init(apc);
  302. apc->pci_ctrl.pci_ops = &ar71xx_pci_ops;
  303. apc->pci_ctrl.mem_resource = &apc->mem_res;
  304. apc->pci_ctrl.io_resource = &apc->io_res;
  305. register_pci_controller(&apc->pci_ctrl);
  306. return 0;
  307. }
  308. static struct platform_driver ar71xx_pci_driver = {
  309. .probe = ar71xx_pci_probe,
  310. .driver = {
  311. .name = "ar71xx-pci",
  312. },
  313. };
  314. static int __init ar71xx_pci_init(void)
  315. {
  316. return platform_driver_register(&ar71xx_pci_driver);
  317. }
  318. postcore_initcall(ar71xx_pci_init);