leon_pci_grpci2.c 24 KB

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