leon_pci_grpci2.c 24 KB

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