leon_pci_grpci2.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * leon_pci_grpci2.c: GRPCI2 Host PCI driver
  4. *
  5. * Copyright (C) 2011 Aeroflex Gaisler AB, Daniel Hellstrom
  6. *
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/pci.h>
  10. #include <linux/slab.h>
  11. #include <linux/delay.h>
  12. #include <linux/export.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <asm/io.h>
  16. #include <asm/leon.h>
  17. #include <asm/vaddrs.h>
  18. #include <asm/sections.h>
  19. #include <asm/leon_pci.h>
  20. #include "irq.h"
  21. struct grpci2_barcfg {
  22. unsigned long pciadr; /* PCI Space Address */
  23. unsigned long ahbadr; /* PCI Base address mapped to this AHB addr */
  24. };
  25. /* Device Node Configuration options:
  26. * - barcfgs : Custom Configuration of Host's 6 target BARs
  27. * - irq_mask : Limit which PCI interrupts are enabled
  28. * - do_reset : Force PCI Reset on startup
  29. *
  30. * barcfgs
  31. * =======
  32. *
  33. * Optional custom Target BAR configuration (see struct grpci2_barcfg). All
  34. * addresses are physical. Array always contains 6 elements (len=2*4*6 bytes)
  35. *
  36. * -1 means not configured (let host driver do default setup).
  37. *
  38. * [i*2+0] = PCI Address of BAR[i] on target interface
  39. * [i*2+1] = Accessing PCI address of BAR[i] result in this AMBA address
  40. *
  41. *
  42. * irq_mask
  43. * ========
  44. *
  45. * Limit which PCI interrupts are enabled. 0=Disable, 1=Enable. By default
  46. * all are enabled. Use this when PCI interrupt pins are floating on PCB.
  47. * int, len=4.
  48. * bit0 = PCI INTA#
  49. * bit1 = PCI INTB#
  50. * bit2 = PCI INTC#
  51. * bit3 = PCI INTD#
  52. *
  53. *
  54. * reset
  55. * =====
  56. *
  57. * Force PCI reset on startup. int, len=4
  58. */
  59. /* Enable Debugging Configuration Space Access */
  60. #undef GRPCI2_DEBUG_CFGACCESS
  61. /*
  62. * GRPCI2 APB Register MAP
  63. */
  64. struct grpci2_regs {
  65. unsigned int ctrl; /* 0x00 Control */
  66. unsigned int sts_cap; /* 0x04 Status / Capabilities */
  67. int res1; /* 0x08 */
  68. unsigned int io_map; /* 0x0C I/O Map address */
  69. unsigned int dma_ctrl; /* 0x10 DMA */
  70. unsigned int dma_bdbase; /* 0x14 DMA */
  71. int res2[2]; /* 0x18 */
  72. unsigned int bars[6]; /* 0x20 read-only PCI BARs */
  73. int res3[2]; /* 0x38 */
  74. unsigned int ahbmst_map[16]; /* 0x40 AHB->PCI Map per AHB Master */
  75. /* PCI Trace Buffer Registers (OPTIONAL) */
  76. unsigned int t_ctrl; /* 0x80 */
  77. unsigned int t_cnt; /* 0x84 */
  78. unsigned int t_adpat; /* 0x88 */
  79. unsigned int t_admask; /* 0x8C */
  80. unsigned int t_sigpat; /* 0x90 */
  81. unsigned int t_sigmask; /* 0x94 */
  82. unsigned int t_adstate; /* 0x98 */
  83. unsigned int t_sigstate; /* 0x9C */
  84. };
  85. #define REGLOAD(a) (be32_to_cpu(__raw_readl(&(a))))
  86. #define REGSTORE(a, v) (__raw_writel(cpu_to_be32(v), &(a)))
  87. #define CTRL_BUS_BIT 16
  88. #define CTRL_RESET (1<<31)
  89. #define CTRL_SI (1<<27)
  90. #define CTRL_PE (1<<26)
  91. #define CTRL_EI (1<<25)
  92. #define CTRL_ER (1<<24)
  93. #define CTRL_BUS (0xff<<CTRL_BUS_BIT)
  94. #define CTRL_HOSTINT 0xf
  95. #define STS_HOST_BIT 31
  96. #define STS_MST_BIT 30
  97. #define STS_TAR_BIT 29
  98. #define STS_DMA_BIT 28
  99. #define STS_DI_BIT 27
  100. #define STS_HI_BIT 26
  101. #define STS_IRQMODE_BIT 24
  102. #define STS_TRACE_BIT 23
  103. #define STS_CFGERRVALID_BIT 20
  104. #define STS_CFGERR_BIT 19
  105. #define STS_INTTYPE_BIT 12
  106. #define STS_INTSTS_BIT 8
  107. #define STS_FDEPTH_BIT 2
  108. #define STS_FNUM_BIT 0
  109. #define STS_HOST (1<<STS_HOST_BIT)
  110. #define STS_MST (1<<STS_MST_BIT)
  111. #define STS_TAR (1<<STS_TAR_BIT)
  112. #define STS_DMA (1<<STS_DMA_BIT)
  113. #define STS_DI (1<<STS_DI_BIT)
  114. #define STS_HI (1<<STS_HI_BIT)
  115. #define STS_IRQMODE (0x3<<STS_IRQMODE_BIT)
  116. #define STS_TRACE (1<<STS_TRACE_BIT)
  117. #define STS_CFGERRVALID (1<<STS_CFGERRVALID_BIT)
  118. #define STS_CFGERR (1<<STS_CFGERR_BIT)
  119. #define STS_INTTYPE (0x3f<<STS_INTTYPE_BIT)
  120. #define STS_INTSTS (0xf<<STS_INTSTS_BIT)
  121. #define STS_FDEPTH (0x7<<STS_FDEPTH_BIT)
  122. #define STS_FNUM (0x3<<STS_FNUM_BIT)
  123. #define STS_ISYSERR (1<<17)
  124. #define STS_IDMA (1<<16)
  125. #define STS_IDMAERR (1<<15)
  126. #define STS_IMSTABRT (1<<14)
  127. #define STS_ITGTABRT (1<<13)
  128. #define STS_IPARERR (1<<12)
  129. #define STS_ERR_IRQ (STS_ISYSERR | STS_IMSTABRT | STS_ITGTABRT | STS_IPARERR)
  130. struct grpci2_bd_chan {
  131. unsigned int ctrl; /* 0x00 DMA Control */
  132. unsigned int nchan; /* 0x04 Next DMA Channel Address */
  133. unsigned int nbd; /* 0x08 Next Data Descriptor in chan */
  134. unsigned int res; /* 0x0C Reserved */
  135. };
  136. #define BD_CHAN_EN 0x80000000
  137. #define BD_CHAN_TYPE 0x00300000
  138. #define BD_CHAN_BDCNT 0x0000ffff
  139. #define BD_CHAN_EN_BIT 31
  140. #define BD_CHAN_TYPE_BIT 20
  141. #define BD_CHAN_BDCNT_BIT 0
  142. struct grpci2_bd_data {
  143. unsigned int ctrl; /* 0x00 DMA Data Control */
  144. unsigned int pci_adr; /* 0x04 PCI Start Address */
  145. unsigned int ahb_adr; /* 0x08 AHB Start address */
  146. unsigned int next; /* 0x0C Next Data Descriptor in chan */
  147. };
  148. #define BD_DATA_EN 0x80000000
  149. #define BD_DATA_IE 0x40000000
  150. #define BD_DATA_DR 0x20000000
  151. #define BD_DATA_TYPE 0x00300000
  152. #define BD_DATA_ER 0x00080000
  153. #define BD_DATA_LEN 0x0000ffff
  154. #define BD_DATA_EN_BIT 31
  155. #define BD_DATA_IE_BIT 30
  156. #define BD_DATA_DR_BIT 29
  157. #define BD_DATA_TYPE_BIT 20
  158. #define BD_DATA_ER_BIT 19
  159. #define BD_DATA_LEN_BIT 0
  160. /* GRPCI2 Capability */
  161. struct grpci2_cap_first {
  162. unsigned int ctrl;
  163. unsigned int pci2ahb_map[6];
  164. unsigned int ext2ahb_map;
  165. unsigned int io_map;
  166. unsigned int pcibar_size[6];
  167. };
  168. #define CAP9_CTRL_OFS 0
  169. #define CAP9_BAR_OFS 0x4
  170. #define CAP9_IOMAP_OFS 0x20
  171. #define CAP9_BARSIZE_OFS 0x24
  172. #define TGT 256
  173. struct grpci2_priv {
  174. struct leon_pci_info info; /* must be on top of this structure */
  175. struct grpci2_regs __iomem *regs;
  176. char irq;
  177. char irq_mode; /* IRQ Mode from CAPSTS REG */
  178. char bt_enabled;
  179. char do_reset;
  180. char irq_mask;
  181. u32 pciid; /* PCI ID of Host */
  182. unsigned char irq_map[4];
  183. /* Virtual IRQ numbers */
  184. unsigned int virq_err;
  185. unsigned int virq_dma;
  186. /* AHB PCI Windows */
  187. unsigned long pci_area; /* MEMORY */
  188. unsigned long pci_area_end;
  189. unsigned long pci_io; /* I/O */
  190. unsigned long pci_conf; /* CONFIGURATION */
  191. unsigned long pci_conf_end;
  192. unsigned long pci_io_va;
  193. struct grpci2_barcfg tgtbars[6];
  194. };
  195. static DEFINE_SPINLOCK(grpci2_dev_lock);
  196. static struct grpci2_priv *grpci2priv;
  197. static int grpci2_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  198. {
  199. struct grpci2_priv *priv = dev->bus->sysdata;
  200. int irq_group;
  201. /* Use default IRQ decoding on PCI BUS0 according slot numbering */
  202. irq_group = slot & 0x3;
  203. pin = ((pin - 1) + irq_group) & 0x3;
  204. return priv->irq_map[pin];
  205. }
  206. static int grpci2_cfg_r32(struct grpci2_priv *priv, unsigned int bus,
  207. unsigned int devfn, int where, u32 *val)
  208. {
  209. unsigned int *pci_conf;
  210. unsigned long flags;
  211. u32 tmp;
  212. if (where & 0x3)
  213. return -EINVAL;
  214. if (bus == 0) {
  215. devfn += (0x8 * 6); /* start at AD16=Device0 */
  216. } else if (bus == TGT) {
  217. bus = 0;
  218. devfn = 0; /* special case: bridge controller itself */
  219. }
  220. /* Select bus */
  221. spin_lock_irqsave(&grpci2_dev_lock, flags);
  222. REGSTORE(priv->regs->ctrl, (REGLOAD(priv->regs->ctrl) & ~(0xff << 16)) |
  223. (bus << 16));
  224. spin_unlock_irqrestore(&grpci2_dev_lock, flags);
  225. /* clear old status */
  226. REGSTORE(priv->regs->sts_cap, (STS_CFGERR | STS_CFGERRVALID));
  227. pci_conf = (unsigned int *) (priv->pci_conf |
  228. (devfn << 8) | (where & 0xfc));
  229. tmp = LEON3_BYPASS_LOAD_PA(pci_conf);
  230. /* Wait until GRPCI2 signals that CFG access is done, it should be
  231. * done instantaneously unless a DMA operation is ongoing...
  232. */
  233. while ((REGLOAD(priv->regs->sts_cap) & STS_CFGERRVALID) == 0)
  234. ;
  235. if (REGLOAD(priv->regs->sts_cap) & STS_CFGERR) {
  236. *val = 0xffffffff;
  237. } else {
  238. /* Bus always little endian (unaffected by byte-swapping) */
  239. *val = swab32(tmp);
  240. }
  241. return 0;
  242. }
  243. static int grpci2_cfg_r16(struct grpci2_priv *priv, unsigned int bus,
  244. unsigned int devfn, int where, u32 *val)
  245. {
  246. u32 v;
  247. int ret;
  248. if (where & 0x1)
  249. return -EINVAL;
  250. ret = grpci2_cfg_r32(priv, bus, devfn, where & ~0x3, &v);
  251. *val = 0xffff & (v >> (8 * (where & 0x3)));
  252. return ret;
  253. }
  254. static int grpci2_cfg_r8(struct grpci2_priv *priv, unsigned int bus,
  255. unsigned int devfn, int where, u32 *val)
  256. {
  257. u32 v;
  258. int ret;
  259. ret = grpci2_cfg_r32(priv, bus, devfn, where & ~0x3, &v);
  260. *val = 0xff & (v >> (8 * (where & 3)));
  261. return ret;
  262. }
  263. static int grpci2_cfg_w32(struct grpci2_priv *priv, unsigned int bus,
  264. unsigned int devfn, int where, u32 val)
  265. {
  266. unsigned int *pci_conf;
  267. unsigned long flags;
  268. if (where & 0x3)
  269. return -EINVAL;
  270. if (bus == 0) {
  271. devfn += (0x8 * 6); /* start at AD16=Device0 */
  272. } else if (bus == TGT) {
  273. bus = 0;
  274. devfn = 0; /* special case: bridge controller itself */
  275. }
  276. /* Select bus */
  277. spin_lock_irqsave(&grpci2_dev_lock, flags);
  278. REGSTORE(priv->regs->ctrl, (REGLOAD(priv->regs->ctrl) & ~(0xff << 16)) |
  279. (bus << 16));
  280. spin_unlock_irqrestore(&grpci2_dev_lock, flags);
  281. /* clear old status */
  282. REGSTORE(priv->regs->sts_cap, (STS_CFGERR | STS_CFGERRVALID));
  283. pci_conf = (unsigned int *) (priv->pci_conf |
  284. (devfn << 8) | (where & 0xfc));
  285. LEON3_BYPASS_STORE_PA(pci_conf, swab32(val));
  286. /* Wait until GRPCI2 signals that CFG access is done, it should be
  287. * done instantaneously unless a DMA operation is ongoing...
  288. */
  289. while ((REGLOAD(priv->regs->sts_cap) & STS_CFGERRVALID) == 0)
  290. ;
  291. return 0;
  292. }
  293. static int grpci2_cfg_w16(struct grpci2_priv *priv, unsigned int bus,
  294. unsigned int devfn, int where, u32 val)
  295. {
  296. int ret;
  297. u32 v;
  298. if (where & 0x1)
  299. return -EINVAL;
  300. ret = grpci2_cfg_r32(priv, bus, devfn, where&~3, &v);
  301. if (ret)
  302. return ret;
  303. v = (v & ~(0xffff << (8 * (where & 0x3)))) |
  304. ((0xffff & val) << (8 * (where & 0x3)));
  305. return grpci2_cfg_w32(priv, bus, devfn, where & ~0x3, v);
  306. }
  307. static int grpci2_cfg_w8(struct grpci2_priv *priv, unsigned int bus,
  308. unsigned int devfn, int where, u32 val)
  309. {
  310. int ret;
  311. u32 v;
  312. ret = grpci2_cfg_r32(priv, bus, devfn, where & ~0x3, &v);
  313. if (ret != 0)
  314. return ret;
  315. v = (v & ~(0xff << (8 * (where & 0x3)))) |
  316. ((0xff & val) << (8 * (where & 0x3)));
  317. return grpci2_cfg_w32(priv, bus, devfn, where & ~0x3, v);
  318. }
  319. /* Read from Configuration Space. When entering here the PCI layer has taken
  320. * the pci_lock spinlock and IRQ is off.
  321. */
  322. static int grpci2_read_config(struct pci_bus *bus, unsigned int devfn,
  323. int where, int size, u32 *val)
  324. {
  325. struct grpci2_priv *priv = grpci2priv;
  326. unsigned int busno = bus->number;
  327. int ret;
  328. if (PCI_SLOT(devfn) > 15 || busno > 255) {
  329. *val = ~0;
  330. return 0;
  331. }
  332. switch (size) {
  333. case 1:
  334. ret = grpci2_cfg_r8(priv, busno, devfn, where, val);
  335. break;
  336. case 2:
  337. ret = grpci2_cfg_r16(priv, busno, devfn, where, val);
  338. break;
  339. case 4:
  340. ret = grpci2_cfg_r32(priv, busno, devfn, where, val);
  341. break;
  342. default:
  343. ret = -EINVAL;
  344. break;
  345. }
  346. #ifdef GRPCI2_DEBUG_CFGACCESS
  347. printk(KERN_INFO "grpci2_read_config: [%02x:%02x:%x] ofs=%d val=%x "
  348. "size=%d\n", busno, PCI_SLOT(devfn), PCI_FUNC(devfn), where,
  349. *val, size);
  350. #endif
  351. return ret;
  352. }
  353. /* Write to Configuration Space. When entering here the PCI layer has taken
  354. * the pci_lock spinlock and IRQ is off.
  355. */
  356. static int grpci2_write_config(struct pci_bus *bus, unsigned int devfn,
  357. int where, int size, u32 val)
  358. {
  359. struct grpci2_priv *priv = grpci2priv;
  360. unsigned int busno = bus->number;
  361. if (PCI_SLOT(devfn) > 15 || busno > 255)
  362. return 0;
  363. #ifdef GRPCI2_DEBUG_CFGACCESS
  364. printk(KERN_INFO "grpci2_write_config: [%02x:%02x:%x] ofs=%d size=%d "
  365. "val=%x\n", busno, PCI_SLOT(devfn), PCI_FUNC(devfn),
  366. where, size, val);
  367. #endif
  368. switch (size) {
  369. default:
  370. return -EINVAL;
  371. case 1:
  372. return grpci2_cfg_w8(priv, busno, devfn, where, val);
  373. case 2:
  374. return grpci2_cfg_w16(priv, busno, devfn, where, val);
  375. case 4:
  376. return grpci2_cfg_w32(priv, busno, devfn, where, val);
  377. }
  378. }
  379. static struct pci_ops grpci2_ops = {
  380. .read = grpci2_read_config,
  381. .write = grpci2_write_config,
  382. };
  383. /* GENIRQ IRQ chip implementation for GRPCI2 irqmode=0..2. In configuration
  384. * 3 where all PCI Interrupts has a separate IRQ on the system IRQ controller
  385. * this is not needed and the standard IRQ controller can be used.
  386. */
  387. static void grpci2_mask_irq(struct irq_data *data)
  388. {
  389. unsigned long flags;
  390. unsigned int irqidx;
  391. struct grpci2_priv *priv = grpci2priv;
  392. irqidx = (unsigned int)data->chip_data - 1;
  393. if (irqidx > 3) /* only mask PCI interrupts here */
  394. return;
  395. spin_lock_irqsave(&grpci2_dev_lock, flags);
  396. REGSTORE(priv->regs->ctrl, REGLOAD(priv->regs->ctrl) & ~(1 << irqidx));
  397. spin_unlock_irqrestore(&grpci2_dev_lock, flags);
  398. }
  399. static void grpci2_unmask_irq(struct irq_data *data)
  400. {
  401. unsigned long flags;
  402. unsigned int irqidx;
  403. struct grpci2_priv *priv = grpci2priv;
  404. irqidx = (unsigned int)data->chip_data - 1;
  405. if (irqidx > 3) /* only unmask PCI interrupts here */
  406. return;
  407. spin_lock_irqsave(&grpci2_dev_lock, flags);
  408. REGSTORE(priv->regs->ctrl, REGLOAD(priv->regs->ctrl) | (1 << irqidx));
  409. spin_unlock_irqrestore(&grpci2_dev_lock, flags);
  410. }
  411. static unsigned int grpci2_startup_irq(struct irq_data *data)
  412. {
  413. grpci2_unmask_irq(data);
  414. return 0;
  415. }
  416. static void grpci2_shutdown_irq(struct irq_data *data)
  417. {
  418. grpci2_mask_irq(data);
  419. }
  420. static struct irq_chip grpci2_irq = {
  421. .name = "grpci2",
  422. .irq_startup = grpci2_startup_irq,
  423. .irq_shutdown = grpci2_shutdown_irq,
  424. .irq_mask = grpci2_mask_irq,
  425. .irq_unmask = grpci2_unmask_irq,
  426. };
  427. /* Handle one or multiple IRQs from the PCI core */
  428. static void grpci2_pci_flow_irq(struct irq_desc *desc)
  429. {
  430. struct grpci2_priv *priv = grpci2priv;
  431. int i, ack = 0;
  432. unsigned int ctrl, sts_cap, pci_ints;
  433. ctrl = REGLOAD(priv->regs->ctrl);
  434. sts_cap = REGLOAD(priv->regs->sts_cap);
  435. /* Error Interrupt? */
  436. if (sts_cap & STS_ERR_IRQ) {
  437. generic_handle_irq(priv->virq_err);
  438. ack = 1;
  439. }
  440. /* PCI Interrupt? */
  441. pci_ints = ((~sts_cap) >> STS_INTSTS_BIT) & ctrl & CTRL_HOSTINT;
  442. if (pci_ints) {
  443. /* Call respective PCI Interrupt handler */
  444. for (i = 0; i < 4; i++) {
  445. if (pci_ints & (1 << i))
  446. generic_handle_irq(priv->irq_map[i]);
  447. }
  448. ack = 1;
  449. }
  450. /*
  451. * Decode DMA Interrupt only when shared with Err and PCI INTX#, when
  452. * the DMA is a unique IRQ the DMA interrupts doesn't end up here, they
  453. * goes directly to DMA ISR.
  454. */
  455. if ((priv->irq_mode == 0) && (sts_cap & (STS_IDMA | STS_IDMAERR))) {
  456. generic_handle_irq(priv->virq_dma);
  457. ack = 1;
  458. }
  459. /*
  460. * Call "first level" IRQ chip end-of-irq handler. It will ACK LEON IRQ
  461. * Controller, this must be done after IRQ sources have been handled to
  462. * avoid double IRQ generation
  463. */
  464. if (ack)
  465. desc->irq_data.chip->irq_eoi(&desc->irq_data);
  466. }
  467. /* Create a virtual IRQ */
  468. static unsigned int grpci2_build_device_irq(unsigned int irq)
  469. {
  470. unsigned int virq = 0, pil;
  471. pil = 1 << 8;
  472. virq = irq_alloc(irq, pil);
  473. if (virq == 0)
  474. goto out;
  475. irq_set_chip_and_handler_name(virq, &grpci2_irq, handle_simple_irq,
  476. "pcilvl");
  477. irq_set_chip_data(virq, (void *)irq);
  478. out:
  479. return virq;
  480. }
  481. static void grpci2_hw_init(struct grpci2_priv *priv)
  482. {
  483. u32 ahbadr, pciadr, bar_sz, capptr, io_map, data;
  484. struct grpci2_regs __iomem *regs = priv->regs;
  485. int i;
  486. struct grpci2_barcfg *barcfg = priv->tgtbars;
  487. /* Reset any earlier setup */
  488. if (priv->do_reset) {
  489. printk(KERN_INFO "GRPCI2: Resetting PCI bus\n");
  490. REGSTORE(regs->ctrl, CTRL_RESET);
  491. ssleep(1); /* Wait for boards to settle */
  492. }
  493. REGSTORE(regs->ctrl, 0);
  494. REGSTORE(regs->sts_cap, ~0); /* Clear Status */
  495. REGSTORE(regs->dma_ctrl, 0);
  496. REGSTORE(regs->dma_bdbase, 0);
  497. /* Translate I/O accesses to 0, I/O Space always @ PCI low 64Kbytes */
  498. REGSTORE(regs->io_map, REGLOAD(regs->io_map) & 0x0000ffff);
  499. /* set 1:1 mapping between AHB -> PCI memory space, for all Masters
  500. * Each AHB master has its own mapping registers. Max 16 AHB masters.
  501. */
  502. for (i = 0; i < 16; i++)
  503. REGSTORE(regs->ahbmst_map[i], priv->pci_area);
  504. /* Get the GRPCI2 Host PCI ID */
  505. grpci2_cfg_r32(priv, TGT, 0, PCI_VENDOR_ID, &priv->pciid);
  506. /* Get address to first (always defined) capability structure */
  507. grpci2_cfg_r8(priv, TGT, 0, PCI_CAPABILITY_LIST, &capptr);
  508. /* Enable/Disable Byte twisting */
  509. grpci2_cfg_r32(priv, TGT, 0, capptr+CAP9_IOMAP_OFS, &io_map);
  510. io_map = (io_map & ~0x1) | (priv->bt_enabled ? 1 : 0);
  511. grpci2_cfg_w32(priv, TGT, 0, capptr+CAP9_IOMAP_OFS, io_map);
  512. /* Setup the Host's PCI Target BARs for other peripherals to access,
  513. * and do DMA to the host's memory. The target BARs can be sized and
  514. * enabled individually.
  515. *
  516. * User may set custom target BARs, but default is:
  517. * The first BARs is used to map kernel low (DMA is part of normal
  518. * region on sparc which is SRMMU_MAXMEM big) main memory 1:1 to the
  519. * PCI bus, the other BARs are disabled. We assume that the first BAR
  520. * is always available.
  521. */
  522. for (i = 0; i < 6; i++) {
  523. if (barcfg[i].pciadr != ~0 && barcfg[i].ahbadr != ~0) {
  524. /* Target BARs must have the proper alignment */
  525. ahbadr = barcfg[i].ahbadr;
  526. pciadr = barcfg[i].pciadr;
  527. bar_sz = ((pciadr - 1) & ~pciadr) + 1;
  528. } else {
  529. if (i == 0) {
  530. /* Map main memory */
  531. bar_sz = 0xf0000008; /* 256MB prefetchable */
  532. ahbadr = 0xf0000000 & (u32)__pa(PAGE_ALIGN(
  533. (unsigned long) &_end));
  534. pciadr = ahbadr;
  535. } else {
  536. bar_sz = 0;
  537. ahbadr = 0;
  538. pciadr = 0;
  539. }
  540. }
  541. grpci2_cfg_w32(priv, TGT, 0, capptr+CAP9_BARSIZE_OFS+i*4,
  542. bar_sz);
  543. grpci2_cfg_w32(priv, TGT, 0, PCI_BASE_ADDRESS_0+i*4, pciadr);
  544. grpci2_cfg_w32(priv, TGT, 0, capptr+CAP9_BAR_OFS+i*4, ahbadr);
  545. printk(KERN_INFO " TGT BAR[%d]: 0x%08x (PCI)-> 0x%08x\n",
  546. i, pciadr, ahbadr);
  547. }
  548. /* set as bus master and enable pci memory responses */
  549. grpci2_cfg_r32(priv, TGT, 0, PCI_COMMAND, &data);
  550. data |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  551. grpci2_cfg_w32(priv, TGT, 0, PCI_COMMAND, data);
  552. /* Enable Error respone (CPU-TRAP) on illegal memory access. */
  553. REGSTORE(regs->ctrl, CTRL_ER | CTRL_PE);
  554. }
  555. static irqreturn_t grpci2_jump_interrupt(int irq, void *arg)
  556. {
  557. printk(KERN_ERR "GRPCI2: Jump IRQ happened\n");
  558. return IRQ_NONE;
  559. }
  560. /* Handle GRPCI2 Error Interrupt */
  561. static irqreturn_t grpci2_err_interrupt(int irq, void *arg)
  562. {
  563. struct grpci2_priv *priv = arg;
  564. struct grpci2_regs __iomem *regs = priv->regs;
  565. unsigned int status;
  566. status = REGLOAD(regs->sts_cap);
  567. if ((status & STS_ERR_IRQ) == 0)
  568. return IRQ_NONE;
  569. if (status & STS_IPARERR)
  570. printk(KERN_ERR "GRPCI2: Parity Error\n");
  571. if (status & STS_ITGTABRT)
  572. printk(KERN_ERR "GRPCI2: Target Abort\n");
  573. if (status & STS_IMSTABRT)
  574. printk(KERN_ERR "GRPCI2: Master Abort\n");
  575. if (status & STS_ISYSERR)
  576. printk(KERN_ERR "GRPCI2: System Error\n");
  577. /* Clear handled INT TYPE IRQs */
  578. REGSTORE(regs->sts_cap, status & STS_ERR_IRQ);
  579. return IRQ_HANDLED;
  580. }
  581. static int grpci2_of_probe(struct platform_device *ofdev)
  582. {
  583. struct grpci2_regs __iomem *regs;
  584. struct grpci2_priv *priv;
  585. int err, i, len;
  586. const int *tmp;
  587. unsigned int capability;
  588. if (grpci2priv) {
  589. printk(KERN_ERR "GRPCI2: only one GRPCI2 core supported\n");
  590. return -ENODEV;
  591. }
  592. if (ofdev->num_resources < 3) {
  593. printk(KERN_ERR "GRPCI2: not enough APB/AHB resources\n");
  594. return -EIO;
  595. }
  596. /* Find Device Address */
  597. regs = of_ioremap(&ofdev->resource[0], 0,
  598. resource_size(&ofdev->resource[0]),
  599. "grlib-grpci2 regs");
  600. if (regs == NULL) {
  601. printk(KERN_ERR "GRPCI2: ioremap failed\n");
  602. return -EIO;
  603. }
  604. /*
  605. * Check that we're in Host Slot and that we can act as a Host Bridge
  606. * and not only as target.
  607. */
  608. capability = REGLOAD(regs->sts_cap);
  609. if ((capability & STS_HOST) || !(capability & STS_MST)) {
  610. printk(KERN_INFO "GRPCI2: not in host system slot\n");
  611. err = -EIO;
  612. goto err1;
  613. }
  614. priv = grpci2priv = kzalloc(sizeof(struct grpci2_priv), GFP_KERNEL);
  615. if (grpci2priv == NULL) {
  616. err = -ENOMEM;
  617. goto err1;
  618. }
  619. priv->regs = regs;
  620. priv->irq = ofdev->archdata.irqs[0]; /* BASE IRQ */
  621. priv->irq_mode = (capability & STS_IRQMODE) >> STS_IRQMODE_BIT;
  622. printk(KERN_INFO "GRPCI2: host found at %p, irq%d\n", regs, priv->irq);
  623. /* Byte twisting should be made configurable from kernel command line */
  624. priv->bt_enabled = 1;
  625. /* Let user do custom Target BAR assignment */
  626. tmp = of_get_property(ofdev->dev.of_node, "barcfg", &len);
  627. if (tmp && (len == 2*4*6))
  628. memcpy(priv->tgtbars, tmp, 2*4*6);
  629. else
  630. memset(priv->tgtbars, -1, 2*4*6);
  631. /* Limit IRQ unmasking in irq_mode 2 and 3 */
  632. tmp = of_get_property(ofdev->dev.of_node, "irq_mask", &len);
  633. if (tmp && (len == 4))
  634. priv->do_reset = *tmp;
  635. else
  636. priv->irq_mask = 0xf;
  637. /* Optional PCI reset. Force PCI reset on startup */
  638. tmp = of_get_property(ofdev->dev.of_node, "reset", &len);
  639. if (tmp && (len == 4))
  640. priv->do_reset = *tmp;
  641. else
  642. priv->do_reset = 0;
  643. /* Find PCI Memory, I/O and Configuration Space Windows */
  644. priv->pci_area = ofdev->resource[1].start;
  645. priv->pci_area_end = ofdev->resource[1].end+1;
  646. priv->pci_io = ofdev->resource[2].start;
  647. priv->pci_conf = ofdev->resource[2].start + 0x10000;
  648. priv->pci_conf_end = priv->pci_conf + 0x10000;
  649. priv->pci_io_va = (unsigned long)ioremap(priv->pci_io, 0x10000);
  650. if (!priv->pci_io_va) {
  651. err = -EIO;
  652. goto err2;
  653. }
  654. printk(KERN_INFO
  655. "GRPCI2: MEMORY SPACE [0x%08lx - 0x%08lx]\n"
  656. " I/O SPACE [0x%08lx - 0x%08lx]\n"
  657. " CONFIG SPACE [0x%08lx - 0x%08lx]\n",
  658. priv->pci_area, priv->pci_area_end-1,
  659. priv->pci_io, priv->pci_conf-1,
  660. priv->pci_conf, priv->pci_conf_end-1);
  661. /*
  662. * I/O Space resources in I/O Window mapped into Virtual Adr Space
  663. * We never use low 4KB because some devices seem have problems using
  664. * address 0.
  665. */
  666. memset(&priv->info.io_space, 0, sizeof(struct resource));
  667. priv->info.io_space.name = "GRPCI2 PCI I/O Space";
  668. priv->info.io_space.start = priv->pci_io_va + 0x1000;
  669. priv->info.io_space.end = priv->pci_io_va + 0x10000 - 1;
  670. priv->info.io_space.flags = IORESOURCE_IO;
  671. /*
  672. * GRPCI2 has no prefetchable memory, map everything as
  673. * non-prefetchable memory
  674. */
  675. memset(&priv->info.mem_space, 0, sizeof(struct resource));
  676. priv->info.mem_space.name = "GRPCI2 PCI MEM Space";
  677. priv->info.mem_space.start = priv->pci_area;
  678. priv->info.mem_space.end = priv->pci_area_end - 1;
  679. priv->info.mem_space.flags = IORESOURCE_MEM;
  680. if (request_resource(&iomem_resource, &priv->info.mem_space) < 0)
  681. goto err3;
  682. if (request_resource(&ioport_resource, &priv->info.io_space) < 0)
  683. goto err4;
  684. /* setup maximum supported PCI buses */
  685. priv->info.busn.name = "GRPCI2 busn";
  686. priv->info.busn.start = 0;
  687. priv->info.busn.end = 255;
  688. grpci2_hw_init(priv);
  689. /*
  690. * Get PCI Interrupt to System IRQ mapping and setup IRQ handling
  691. * Error IRQ always on PCI INTA.
  692. */
  693. if (priv->irq_mode < 2) {
  694. /* All PCI interrupts are shared using the same system IRQ */
  695. leon_update_virq_handling(priv->irq, grpci2_pci_flow_irq,
  696. "pcilvl", 0);
  697. priv->irq_map[0] = grpci2_build_device_irq(1);
  698. priv->irq_map[1] = grpci2_build_device_irq(2);
  699. priv->irq_map[2] = grpci2_build_device_irq(3);
  700. priv->irq_map[3] = grpci2_build_device_irq(4);
  701. priv->virq_err = grpci2_build_device_irq(5);
  702. if (priv->irq_mode & 1)
  703. priv->virq_dma = ofdev->archdata.irqs[1];
  704. else
  705. priv->virq_dma = grpci2_build_device_irq(6);
  706. /* Enable IRQs on LEON IRQ controller */
  707. err = request_irq(priv->irq, grpci2_jump_interrupt, 0,
  708. "GRPCI2_JUMP", priv);
  709. if (err)
  710. printk(KERN_ERR "GRPCI2: ERR IRQ request failed\n");
  711. } else {
  712. /* All PCI interrupts have an unique IRQ interrupt */
  713. for (i = 0; i < 4; i++) {
  714. /* Make LEON IRQ layer handle level IRQ by acking */
  715. leon_update_virq_handling(ofdev->archdata.irqs[i],
  716. handle_fasteoi_irq, "pcilvl",
  717. 1);
  718. priv->irq_map[i] = ofdev->archdata.irqs[i];
  719. }
  720. priv->virq_err = priv->irq_map[0];
  721. if (priv->irq_mode & 1)
  722. priv->virq_dma = ofdev->archdata.irqs[4];
  723. else
  724. priv->virq_dma = priv->irq_map[0];
  725. /* Unmask all PCI interrupts, request_irq will not do that */
  726. REGSTORE(regs->ctrl, REGLOAD(regs->ctrl)|(priv->irq_mask&0xf));
  727. }
  728. /* Setup IRQ handler for non-configuration space access errors */
  729. err = request_irq(priv->virq_err, grpci2_err_interrupt, IRQF_SHARED,
  730. "GRPCI2_ERR", priv);
  731. if (err) {
  732. printk(KERN_DEBUG "GRPCI2: ERR VIRQ request failed: %d\n", err);
  733. goto err5;
  734. }
  735. /*
  736. * Enable Error Interrupts. PCI interrupts are unmasked once request_irq
  737. * is called by the PCI Device drivers
  738. */
  739. REGSTORE(regs->ctrl, REGLOAD(regs->ctrl) | CTRL_EI | CTRL_SI);
  740. /* Init common layer and scan buses */
  741. priv->info.ops = &grpci2_ops;
  742. priv->info.map_irq = grpci2_map_irq;
  743. leon_pci_init(ofdev, &priv->info);
  744. return 0;
  745. err5:
  746. release_resource(&priv->info.io_space);
  747. err4:
  748. release_resource(&priv->info.mem_space);
  749. err3:
  750. err = -ENOMEM;
  751. iounmap((void __iomem *)priv->pci_io_va);
  752. err2:
  753. kfree(priv);
  754. err1:
  755. of_iounmap(&ofdev->resource[0], regs,
  756. resource_size(&ofdev->resource[0]));
  757. return err;
  758. }
  759. static const struct of_device_id grpci2_of_match[] = {
  760. {
  761. .name = "GAISLER_GRPCI2",
  762. },
  763. {
  764. .name = "01_07c",
  765. },
  766. {},
  767. };
  768. static struct platform_driver grpci2_of_driver = {
  769. .driver = {
  770. .name = "grpci2",
  771. .of_match_table = grpci2_of_match,
  772. },
  773. .probe = grpci2_of_probe,
  774. };
  775. static int __init grpci2_init(void)
  776. {
  777. return platform_driver_register(&grpci2_of_driver);
  778. }
  779. subsys_initcall(grpci2_init);