pci-ar2315.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /**
  16. * Both AR2315 and AR2316 chips have PCI interface unit, which supports DMA
  17. * and interrupt. PCI interface supports MMIO access method, but does not
  18. * seem to support I/O ports.
  19. *
  20. * Read/write operation in the region 0x80000000-0xBFFFFFFF causes
  21. * a memory read/write command on the PCI bus. 30 LSBs of address on
  22. * the bus are taken from memory read/write request and 2 MSBs are
  23. * determined by PCI unit configuration.
  24. *
  25. * To work with the configuration space instead of memory is necessary set
  26. * the CFG_SEL bit in the PCI_MISC_CONFIG register.
  27. *
  28. * Devices on the bus can perform DMA requests via chip BAR1. PCI host
  29. * controller BARs are programmend as if an external device is programmed.
  30. * Which means that during configuration, IDSEL pin of the chip should be
  31. * asserted.
  32. *
  33. * We know (and support) only one board that uses the PCI interface -
  34. * Fonera 2.0g (FON2202). It has a USB EHCI controller connected to the
  35. * AR2315 PCI bus. IDSEL pin of USB controller is connected to AD[13] line
  36. * and IDSEL pin of AR2315 is connected to AD[16] line.
  37. */
  38. #include <linux/types.h>
  39. #include <linux/pci.h>
  40. #include <linux/platform_device.h>
  41. #include <linux/kernel.h>
  42. #include <linux/init.h>
  43. #include <linux/mm.h>
  44. #include <linux/delay.h>
  45. #include <linux/bitops.h>
  46. #include <linux/irq.h>
  47. #include <linux/irqdomain.h>
  48. #include <linux/io.h>
  49. #include <asm/paccess.h>
  50. /*
  51. * PCI Bus Interface Registers
  52. */
  53. #define AR2315_PCI_1MS_REG 0x0008
  54. #define AR2315_PCI_1MS_MASK 0x3FFFF /* # of AHB clk cycles in 1ms */
  55. #define AR2315_PCI_MISC_CONFIG 0x000c
  56. #define AR2315_PCIMISC_TXD_EN 0x00000001 /* Enable TXD for fragments */
  57. #define AR2315_PCIMISC_CFG_SEL 0x00000002 /* Mem or Config cycles */
  58. #define AR2315_PCIMISC_GIG_MASK 0x0000000C /* bits 31-30 for pci req */
  59. #define AR2315_PCIMISC_RST_MODE 0x00000030
  60. #define AR2315_PCIRST_INPUT 0x00000000 /* 4:5=0 rst is input */
  61. #define AR2315_PCIRST_LOW 0x00000010 /* 4:5=1 rst to GND */
  62. #define AR2315_PCIRST_HIGH 0x00000020 /* 4:5=2 rst to VDD */
  63. #define AR2315_PCIGRANT_EN 0x00000000 /* 6:7=0 early grant en */
  64. #define AR2315_PCIGRANT_FRAME 0x00000040 /* 6:7=1 grant waits 4 frame */
  65. #define AR2315_PCIGRANT_IDLE 0x00000080 /* 6:7=2 grant waits 4 idle */
  66. #define AR2315_PCIGRANT_GAP 0x00000000 /* 6:7=2 grant waits 4 idle */
  67. #define AR2315_PCICACHE_DIS 0x00001000 /* PCI external access cache
  68. * disable */
  69. #define AR2315_PCI_OUT_TSTAMP 0x0010
  70. #define AR2315_PCI_UNCACHE_CFG 0x0014
  71. #define AR2315_PCI_IN_EN 0x0100
  72. #define AR2315_PCI_IN_EN0 0x01 /* Enable chain 0 */
  73. #define AR2315_PCI_IN_EN1 0x02 /* Enable chain 1 */
  74. #define AR2315_PCI_IN_EN2 0x04 /* Enable chain 2 */
  75. #define AR2315_PCI_IN_EN3 0x08 /* Enable chain 3 */
  76. #define AR2315_PCI_IN_DIS 0x0104
  77. #define AR2315_PCI_IN_DIS0 0x01 /* Disable chain 0 */
  78. #define AR2315_PCI_IN_DIS1 0x02 /* Disable chain 1 */
  79. #define AR2315_PCI_IN_DIS2 0x04 /* Disable chain 2 */
  80. #define AR2315_PCI_IN_DIS3 0x08 /* Disable chain 3 */
  81. #define AR2315_PCI_IN_PTR 0x0200
  82. #define AR2315_PCI_OUT_EN 0x0400
  83. #define AR2315_PCI_OUT_EN0 0x01 /* Enable chain 0 */
  84. #define AR2315_PCI_OUT_DIS 0x0404
  85. #define AR2315_PCI_OUT_DIS0 0x01 /* Disable chain 0 */
  86. #define AR2315_PCI_OUT_PTR 0x0408
  87. /* PCI interrupt status (write one to clear) */
  88. #define AR2315_PCI_ISR 0x0500
  89. #define AR2315_PCI_INT_TX 0x00000001 /* Desc In Completed */
  90. #define AR2315_PCI_INT_TXOK 0x00000002 /* Desc In OK */
  91. #define AR2315_PCI_INT_TXERR 0x00000004 /* Desc In ERR */
  92. #define AR2315_PCI_INT_TXEOL 0x00000008 /* Desc In End-of-List */
  93. #define AR2315_PCI_INT_RX 0x00000010 /* Desc Out Completed */
  94. #define AR2315_PCI_INT_RXOK 0x00000020 /* Desc Out OK */
  95. #define AR2315_PCI_INT_RXERR 0x00000040 /* Desc Out ERR */
  96. #define AR2315_PCI_INT_RXEOL 0x00000080 /* Desc Out EOL */
  97. #define AR2315_PCI_INT_TXOOD 0x00000200 /* Desc In Out-of-Desc */
  98. #define AR2315_PCI_INT_DESCMASK 0x0000FFFF /* Desc Mask */
  99. #define AR2315_PCI_INT_EXT 0x02000000 /* Extern PCI INTA */
  100. #define AR2315_PCI_INT_ABORT 0x04000000 /* PCI bus abort event */
  101. /* PCI interrupt mask */
  102. #define AR2315_PCI_IMR 0x0504
  103. /* Global PCI interrupt enable */
  104. #define AR2315_PCI_IER 0x0508
  105. #define AR2315_PCI_IER_DISABLE 0x00 /* disable pci interrupts */
  106. #define AR2315_PCI_IER_ENABLE 0x01 /* enable pci interrupts */
  107. #define AR2315_PCI_HOST_IN_EN 0x0800
  108. #define AR2315_PCI_HOST_IN_DIS 0x0804
  109. #define AR2315_PCI_HOST_IN_PTR 0x0810
  110. #define AR2315_PCI_HOST_OUT_EN 0x0900
  111. #define AR2315_PCI_HOST_OUT_DIS 0x0904
  112. #define AR2315_PCI_HOST_OUT_PTR 0x0908
  113. /*
  114. * PCI interrupts, which share IP5
  115. * Keep ordered according to AR2315_PCI_INT_XXX bits
  116. */
  117. #define AR2315_PCI_IRQ_EXT 25
  118. #define AR2315_PCI_IRQ_ABORT 26
  119. #define AR2315_PCI_IRQ_COUNT 27
  120. /* Arbitrary size of memory region to access the configuration space */
  121. #define AR2315_PCI_CFG_SIZE 0x00100000
  122. #define AR2315_PCI_HOST_SLOT 3
  123. #define AR2315_PCI_HOST_DEVID ((0xff18 << 16) | PCI_VENDOR_ID_ATHEROS)
  124. /* ??? access BAR */
  125. #define AR2315_PCI_HOST_MBAR0 0x10000000
  126. /* RAM access BAR */
  127. #define AR2315_PCI_HOST_MBAR1 AR2315_PCI_HOST_SDRAM_BASEADDR
  128. /* ??? access BAR */
  129. #define AR2315_PCI_HOST_MBAR2 0x30000000
  130. struct ar2315_pci_ctrl {
  131. void __iomem *cfg_mem;
  132. void __iomem *mmr_mem;
  133. unsigned irq;
  134. unsigned irq_ext;
  135. struct irq_domain *domain;
  136. struct pci_controller pci_ctrl;
  137. struct resource mem_res;
  138. struct resource io_res;
  139. };
  140. static inline struct ar2315_pci_ctrl *ar2315_pci_bus_to_apc(struct pci_bus *bus)
  141. {
  142. struct pci_controller *hose = bus->sysdata;
  143. return container_of(hose, struct ar2315_pci_ctrl, pci_ctrl);
  144. }
  145. static inline u32 ar2315_pci_reg_read(struct ar2315_pci_ctrl *apc, u32 reg)
  146. {
  147. return __raw_readl(apc->mmr_mem + reg);
  148. }
  149. static inline void ar2315_pci_reg_write(struct ar2315_pci_ctrl *apc, u32 reg,
  150. u32 val)
  151. {
  152. __raw_writel(val, apc->mmr_mem + reg);
  153. }
  154. static inline void ar2315_pci_reg_mask(struct ar2315_pci_ctrl *apc, u32 reg,
  155. u32 mask, u32 val)
  156. {
  157. u32 ret = ar2315_pci_reg_read(apc, reg);
  158. ret &= ~mask;
  159. ret |= val;
  160. ar2315_pci_reg_write(apc, reg, ret);
  161. }
  162. static int ar2315_pci_cfg_access(struct ar2315_pci_ctrl *apc, unsigned devfn,
  163. int where, int size, u32 *ptr, bool write)
  164. {
  165. int func = PCI_FUNC(devfn);
  166. int dev = PCI_SLOT(devfn);
  167. u32 addr = (1 << (13 + dev)) | (func << 8) | (where & ~3);
  168. u32 mask = 0xffffffff >> 8 * (4 - size);
  169. u32 sh = (where & 3) * 8;
  170. u32 value, isr;
  171. /* Prevent access past the remapped area */
  172. if (addr >= AR2315_PCI_CFG_SIZE || dev > 18)
  173. return PCIBIOS_DEVICE_NOT_FOUND;
  174. /* Clear pending errors */
  175. ar2315_pci_reg_write(apc, AR2315_PCI_ISR, AR2315_PCI_INT_ABORT);
  176. /* Select Configuration access */
  177. ar2315_pci_reg_mask(apc, AR2315_PCI_MISC_CONFIG, 0,
  178. AR2315_PCIMISC_CFG_SEL);
  179. mb(); /* PCI must see space change before we begin */
  180. value = __raw_readl(apc->cfg_mem + addr);
  181. isr = ar2315_pci_reg_read(apc, AR2315_PCI_ISR);
  182. if (isr & AR2315_PCI_INT_ABORT)
  183. goto exit_err;
  184. if (write) {
  185. value = (value & ~(mask << sh)) | *ptr << sh;
  186. __raw_writel(value, apc->cfg_mem + addr);
  187. isr = ar2315_pci_reg_read(apc, AR2315_PCI_ISR);
  188. if (isr & AR2315_PCI_INT_ABORT)
  189. goto exit_err;
  190. } else {
  191. *ptr = (value >> sh) & mask;
  192. }
  193. goto exit;
  194. exit_err:
  195. ar2315_pci_reg_write(apc, AR2315_PCI_ISR, AR2315_PCI_INT_ABORT);
  196. if (!write)
  197. *ptr = 0xffffffff;
  198. exit:
  199. /* Select Memory access */
  200. ar2315_pci_reg_mask(apc, AR2315_PCI_MISC_CONFIG, AR2315_PCIMISC_CFG_SEL,
  201. 0);
  202. return isr & AR2315_PCI_INT_ABORT ? PCIBIOS_DEVICE_NOT_FOUND :
  203. PCIBIOS_SUCCESSFUL;
  204. }
  205. static inline int ar2315_pci_local_cfg_rd(struct ar2315_pci_ctrl *apc,
  206. unsigned devfn, int where, u32 *val)
  207. {
  208. return ar2315_pci_cfg_access(apc, devfn, where, sizeof(u32), val,
  209. false);
  210. }
  211. static inline int ar2315_pci_local_cfg_wr(struct ar2315_pci_ctrl *apc,
  212. unsigned devfn, int where, u32 val)
  213. {
  214. return ar2315_pci_cfg_access(apc, devfn, where, sizeof(u32), &val,
  215. true);
  216. }
  217. static int ar2315_pci_cfg_read(struct pci_bus *bus, unsigned devfn, int where,
  218. int size, u32 *value)
  219. {
  220. struct ar2315_pci_ctrl *apc = ar2315_pci_bus_to_apc(bus);
  221. if (PCI_SLOT(devfn) == AR2315_PCI_HOST_SLOT)
  222. return PCIBIOS_DEVICE_NOT_FOUND;
  223. return ar2315_pci_cfg_access(apc, devfn, where, size, value, false);
  224. }
  225. static int ar2315_pci_cfg_write(struct pci_bus *bus, unsigned devfn, int where,
  226. int size, u32 value)
  227. {
  228. struct ar2315_pci_ctrl *apc = ar2315_pci_bus_to_apc(bus);
  229. if (PCI_SLOT(devfn) == AR2315_PCI_HOST_SLOT)
  230. return PCIBIOS_DEVICE_NOT_FOUND;
  231. return ar2315_pci_cfg_access(apc, devfn, where, size, &value, true);
  232. }
  233. static struct pci_ops ar2315_pci_ops = {
  234. .read = ar2315_pci_cfg_read,
  235. .write = ar2315_pci_cfg_write,
  236. };
  237. static int ar2315_pci_host_setup(struct ar2315_pci_ctrl *apc)
  238. {
  239. unsigned devfn = PCI_DEVFN(AR2315_PCI_HOST_SLOT, 0);
  240. int res;
  241. u32 id;
  242. res = ar2315_pci_local_cfg_rd(apc, devfn, PCI_VENDOR_ID, &id);
  243. if (res != PCIBIOS_SUCCESSFUL || id != AR2315_PCI_HOST_DEVID)
  244. return -ENODEV;
  245. /* Program MBARs */
  246. ar2315_pci_local_cfg_wr(apc, devfn, PCI_BASE_ADDRESS_0,
  247. AR2315_PCI_HOST_MBAR0);
  248. ar2315_pci_local_cfg_wr(apc, devfn, PCI_BASE_ADDRESS_1,
  249. AR2315_PCI_HOST_MBAR1);
  250. ar2315_pci_local_cfg_wr(apc, devfn, PCI_BASE_ADDRESS_2,
  251. AR2315_PCI_HOST_MBAR2);
  252. /* Run */
  253. ar2315_pci_local_cfg_wr(apc, devfn, PCI_COMMAND, PCI_COMMAND_MEMORY |
  254. PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL |
  255. PCI_COMMAND_INVALIDATE | PCI_COMMAND_PARITY |
  256. PCI_COMMAND_SERR | PCI_COMMAND_FAST_BACK);
  257. return 0;
  258. }
  259. static void ar2315_pci_irq_handler(unsigned irq, struct irq_desc *desc)
  260. {
  261. struct ar2315_pci_ctrl *apc = irq_desc_get_handler_data(desc);
  262. u32 pending = ar2315_pci_reg_read(apc, AR2315_PCI_ISR) &
  263. ar2315_pci_reg_read(apc, AR2315_PCI_IMR);
  264. unsigned pci_irq = 0;
  265. if (pending)
  266. pci_irq = irq_find_mapping(apc->domain, __ffs(pending));
  267. if (pci_irq)
  268. generic_handle_irq(pci_irq);
  269. else
  270. spurious_interrupt();
  271. }
  272. static void ar2315_pci_irq_mask(struct irq_data *d)
  273. {
  274. struct ar2315_pci_ctrl *apc = irq_data_get_irq_chip_data(d);
  275. ar2315_pci_reg_mask(apc, AR2315_PCI_IMR, BIT(d->hwirq), 0);
  276. }
  277. static void ar2315_pci_irq_mask_ack(struct irq_data *d)
  278. {
  279. struct ar2315_pci_ctrl *apc = irq_data_get_irq_chip_data(d);
  280. u32 m = BIT(d->hwirq);
  281. ar2315_pci_reg_mask(apc, AR2315_PCI_IMR, m, 0);
  282. ar2315_pci_reg_write(apc, AR2315_PCI_ISR, m);
  283. }
  284. static void ar2315_pci_irq_unmask(struct irq_data *d)
  285. {
  286. struct ar2315_pci_ctrl *apc = irq_data_get_irq_chip_data(d);
  287. ar2315_pci_reg_mask(apc, AR2315_PCI_IMR, 0, BIT(d->hwirq));
  288. }
  289. static struct irq_chip ar2315_pci_irq_chip = {
  290. .name = "AR2315-PCI",
  291. .irq_mask = ar2315_pci_irq_mask,
  292. .irq_mask_ack = ar2315_pci_irq_mask_ack,
  293. .irq_unmask = ar2315_pci_irq_unmask,
  294. };
  295. static int ar2315_pci_irq_map(struct irq_domain *d, unsigned irq,
  296. irq_hw_number_t hw)
  297. {
  298. irq_set_chip_and_handler(irq, &ar2315_pci_irq_chip, handle_level_irq);
  299. irq_set_chip_data(irq, d->host_data);
  300. return 0;
  301. }
  302. static struct irq_domain_ops ar2315_pci_irq_domain_ops = {
  303. .map = ar2315_pci_irq_map,
  304. };
  305. static void ar2315_pci_irq_init(struct ar2315_pci_ctrl *apc)
  306. {
  307. ar2315_pci_reg_mask(apc, AR2315_PCI_IER, AR2315_PCI_IER_ENABLE, 0);
  308. ar2315_pci_reg_mask(apc, AR2315_PCI_IMR, (AR2315_PCI_INT_ABORT |
  309. AR2315_PCI_INT_EXT), 0);
  310. apc->irq_ext = irq_create_mapping(apc->domain, AR2315_PCI_IRQ_EXT);
  311. irq_set_chained_handler_and_data(apc->irq, ar2315_pci_irq_handler,
  312. apc);
  313. /* Clear any pending Abort or external Interrupts
  314. * and enable interrupt processing */
  315. ar2315_pci_reg_write(apc, AR2315_PCI_ISR, AR2315_PCI_INT_ABORT |
  316. AR2315_PCI_INT_EXT);
  317. ar2315_pci_reg_mask(apc, AR2315_PCI_IER, 0, AR2315_PCI_IER_ENABLE);
  318. }
  319. static int ar2315_pci_probe(struct platform_device *pdev)
  320. {
  321. struct ar2315_pci_ctrl *apc;
  322. struct device *dev = &pdev->dev;
  323. struct resource *res;
  324. int irq, err;
  325. apc = devm_kzalloc(dev, sizeof(*apc), GFP_KERNEL);
  326. if (!apc)
  327. return -ENOMEM;
  328. irq = platform_get_irq(pdev, 0);
  329. if (irq < 0)
  330. return -EINVAL;
  331. apc->irq = irq;
  332. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  333. "ar2315-pci-ctrl");
  334. apc->mmr_mem = devm_ioremap_resource(dev, res);
  335. if (IS_ERR(apc->mmr_mem))
  336. return PTR_ERR(apc->mmr_mem);
  337. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  338. "ar2315-pci-ext");
  339. if (!res)
  340. return -EINVAL;
  341. apc->mem_res.name = "AR2315 PCI mem space";
  342. apc->mem_res.parent = res;
  343. apc->mem_res.start = res->start;
  344. apc->mem_res.end = res->end;
  345. apc->mem_res.flags = IORESOURCE_MEM;
  346. /* Remap PCI config space */
  347. apc->cfg_mem = devm_ioremap_nocache(dev, res->start,
  348. AR2315_PCI_CFG_SIZE);
  349. if (!apc->cfg_mem) {
  350. dev_err(dev, "failed to remap PCI config space\n");
  351. return -ENOMEM;
  352. }
  353. /* Reset the PCI bus by setting bits 5-4 in PCI_MCFG */
  354. ar2315_pci_reg_mask(apc, AR2315_PCI_MISC_CONFIG,
  355. AR2315_PCIMISC_RST_MODE,
  356. AR2315_PCIRST_LOW);
  357. msleep(100);
  358. /* Bring the PCI out of reset */
  359. ar2315_pci_reg_mask(apc, AR2315_PCI_MISC_CONFIG,
  360. AR2315_PCIMISC_RST_MODE,
  361. AR2315_PCIRST_HIGH | AR2315_PCICACHE_DIS | 0x8);
  362. ar2315_pci_reg_write(apc, AR2315_PCI_UNCACHE_CFG,
  363. 0x1E | /* 1GB uncached */
  364. (1 << 5) | /* Enable uncached */
  365. (0x2 << 30) /* Base: 0x80000000 */);
  366. ar2315_pci_reg_read(apc, AR2315_PCI_UNCACHE_CFG);
  367. msleep(500);
  368. err = ar2315_pci_host_setup(apc);
  369. if (err)
  370. return err;
  371. apc->domain = irq_domain_add_linear(NULL, AR2315_PCI_IRQ_COUNT,
  372. &ar2315_pci_irq_domain_ops, apc);
  373. if (!apc->domain) {
  374. dev_err(dev, "failed to add IRQ domain\n");
  375. return -ENOMEM;
  376. }
  377. ar2315_pci_irq_init(apc);
  378. /* PCI controller does not support I/O ports */
  379. apc->io_res.name = "AR2315 IO space";
  380. apc->io_res.start = 0;
  381. apc->io_res.end = 0;
  382. apc->io_res.flags = IORESOURCE_IO,
  383. apc->pci_ctrl.pci_ops = &ar2315_pci_ops;
  384. apc->pci_ctrl.mem_resource = &apc->mem_res,
  385. apc->pci_ctrl.io_resource = &apc->io_res,
  386. register_pci_controller(&apc->pci_ctrl);
  387. dev_info(dev, "register PCI controller\n");
  388. return 0;
  389. }
  390. static struct platform_driver ar2315_pci_driver = {
  391. .probe = ar2315_pci_probe,
  392. .driver = {
  393. .name = "ar2315-pci",
  394. },
  395. };
  396. static int __init ar2315_pci_init(void)
  397. {
  398. return platform_driver_register(&ar2315_pci_driver);
  399. }
  400. arch_initcall(ar2315_pci_init);
  401. int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  402. {
  403. struct ar2315_pci_ctrl *apc = ar2315_pci_bus_to_apc(dev->bus);
  404. return slot ? 0 : apc->irq_ext;
  405. }
  406. int pcibios_plat_dev_init(struct pci_dev *dev)
  407. {
  408. return 0;
  409. }