leon_pci_grpci1.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * leon_pci_grpci1.c: GRPCI1 Host PCI driver
  3. *
  4. * Copyright (C) 2013 Aeroflex Gaisler AB
  5. *
  6. * This GRPCI1 driver does not support PCI interrupts taken from
  7. * GPIO pins. Interrupt generation at PCI parity and system error
  8. * detection is by default turned off since some GRPCI1 cores does
  9. * not support detection. It can be turned on from the bootloader
  10. * using the all_pci_errors property.
  11. *
  12. * Contributors: Daniel Hellstrom <daniel@gaisler.com>
  13. */
  14. #include <linux/of_device.h>
  15. #include <linux/export.h>
  16. #include <linux/kernel.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/delay.h>
  19. #include <linux/pci.h>
  20. #include <asm/leon_pci.h>
  21. #include <asm/sections.h>
  22. #include <asm/vaddrs.h>
  23. #include <asm/leon.h>
  24. #include <asm/io.h>
  25. #include "irq.h"
  26. /* Enable/Disable Debugging Configuration Space Access */
  27. #undef GRPCI1_DEBUG_CFGACCESS
  28. /*
  29. * GRPCI1 APB Register MAP
  30. */
  31. struct grpci1_regs {
  32. unsigned int cfg_stat; /* 0x00 Configuration / Status */
  33. unsigned int bar0; /* 0x04 BAR0 (RO) */
  34. unsigned int page0; /* 0x08 PAGE0 (RO) */
  35. unsigned int bar1; /* 0x0C BAR1 (RO) */
  36. unsigned int page1; /* 0x10 PAGE1 */
  37. unsigned int iomap; /* 0x14 IO Map */
  38. unsigned int stat_cmd; /* 0x18 PCI Status & Command (RO) */
  39. unsigned int irq; /* 0x1C Interrupt register */
  40. };
  41. #define REGLOAD(a) (be32_to_cpu(__raw_readl(&(a))))
  42. #define REGSTORE(a, v) (__raw_writel(cpu_to_be32(v), &(a)))
  43. #define PAGE0_BTEN_BIT 0
  44. #define PAGE0_BTEN (1 << PAGE0_BTEN_BIT)
  45. #define CFGSTAT_HOST_BIT 13
  46. #define CFGSTAT_CTO_BIT 8
  47. #define CFGSTAT_HOST (1 << CFGSTAT_HOST_BIT)
  48. #define CFGSTAT_CTO (1 << CFGSTAT_CTO_BIT)
  49. #define IRQ_DPE (1 << 9)
  50. #define IRQ_SSE (1 << 8)
  51. #define IRQ_RMA (1 << 7)
  52. #define IRQ_RTA (1 << 6)
  53. #define IRQ_STA (1 << 5)
  54. #define IRQ_DPED (1 << 4)
  55. #define IRQ_INTD (1 << 3)
  56. #define IRQ_INTC (1 << 2)
  57. #define IRQ_INTB (1 << 1)
  58. #define IRQ_INTA (1 << 0)
  59. #define IRQ_DEF_ERRORS (IRQ_RMA | IRQ_RTA | IRQ_STA)
  60. #define IRQ_ALL_ERRORS (IRQ_DPED | IRQ_DEF_ERRORS | IRQ_SSE | IRQ_DPE)
  61. #define IRQ_INTX (IRQ_INTA | IRQ_INTB | IRQ_INTC | IRQ_INTD)
  62. #define IRQ_MASK_BIT 16
  63. #define DEF_PCI_ERRORS (PCI_STATUS_SIG_TARGET_ABORT | \
  64. PCI_STATUS_REC_TARGET_ABORT | \
  65. PCI_STATUS_REC_MASTER_ABORT)
  66. #define ALL_PCI_ERRORS (PCI_STATUS_PARITY | PCI_STATUS_DETECTED_PARITY | \
  67. PCI_STATUS_SIG_SYSTEM_ERROR | DEF_PCI_ERRORS)
  68. #define TGT 256
  69. struct grpci1_priv {
  70. struct leon_pci_info info; /* must be on top of this structure */
  71. struct grpci1_regs __iomem *regs; /* GRPCI register map */
  72. struct device *dev;
  73. int pci_err_mask; /* STATUS register error mask */
  74. int irq; /* LEON irqctrl GRPCI IRQ */
  75. unsigned char irq_map[4]; /* GRPCI nexus PCI INTX# IRQs */
  76. unsigned int irq_err; /* GRPCI nexus Virt Error IRQ */
  77. /* AHB PCI Windows */
  78. unsigned long pci_area; /* MEMORY */
  79. unsigned long pci_area_end;
  80. unsigned long pci_io; /* I/O */
  81. unsigned long pci_conf; /* CONFIGURATION */
  82. unsigned long pci_conf_end;
  83. unsigned long pci_io_va;
  84. };
  85. static struct grpci1_priv *grpci1priv;
  86. static int grpci1_cfg_w32(struct grpci1_priv *priv, unsigned int bus,
  87. unsigned int devfn, int where, u32 val);
  88. static int grpci1_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  89. {
  90. struct grpci1_priv *priv = dev->bus->sysdata;
  91. int irq_group;
  92. /* Use default IRQ decoding on PCI BUS0 according slot numbering */
  93. irq_group = slot & 0x3;
  94. pin = ((pin - 1) + irq_group) & 0x3;
  95. return priv->irq_map[pin];
  96. }
  97. static int grpci1_cfg_r32(struct grpci1_priv *priv, unsigned int bus,
  98. unsigned int devfn, int where, u32 *val)
  99. {
  100. u32 *pci_conf, tmp, cfg;
  101. if (where & 0x3)
  102. return -EINVAL;
  103. if (bus == 0) {
  104. devfn += (0x8 * 6); /* start at AD16=Device0 */
  105. } else if (bus == TGT) {
  106. bus = 0;
  107. devfn = 0; /* special case: bridge controller itself */
  108. }
  109. /* Select bus */
  110. cfg = REGLOAD(priv->regs->cfg_stat);
  111. REGSTORE(priv->regs->cfg_stat, (cfg & ~(0xf << 23)) | (bus << 23));
  112. /* do read access */
  113. pci_conf = (u32 *) (priv->pci_conf | (devfn << 8) | (where & 0xfc));
  114. tmp = LEON3_BYPASS_LOAD_PA(pci_conf);
  115. /* check if master abort was received */
  116. if (REGLOAD(priv->regs->cfg_stat) & CFGSTAT_CTO) {
  117. *val = 0xffffffff;
  118. /* Clear Master abort bit in PCI cfg space (is set) */
  119. tmp = REGLOAD(priv->regs->stat_cmd);
  120. grpci1_cfg_w32(priv, TGT, 0, PCI_COMMAND, tmp);
  121. } else {
  122. /* Bus always little endian (unaffected by byte-swapping) */
  123. *val = swab32(tmp);
  124. }
  125. return 0;
  126. }
  127. static int grpci1_cfg_r16(struct grpci1_priv *priv, unsigned int bus,
  128. unsigned int devfn, int where, u32 *val)
  129. {
  130. u32 v;
  131. int ret;
  132. if (where & 0x1)
  133. return -EINVAL;
  134. ret = grpci1_cfg_r32(priv, bus, devfn, where & ~0x3, &v);
  135. *val = 0xffff & (v >> (8 * (where & 0x3)));
  136. return ret;
  137. }
  138. static int grpci1_cfg_r8(struct grpci1_priv *priv, unsigned int bus,
  139. unsigned int devfn, int where, u32 *val)
  140. {
  141. u32 v;
  142. int ret;
  143. ret = grpci1_cfg_r32(priv, bus, devfn, where & ~0x3, &v);
  144. *val = 0xff & (v >> (8 * (where & 3)));
  145. return ret;
  146. }
  147. static int grpci1_cfg_w32(struct grpci1_priv *priv, unsigned int bus,
  148. unsigned int devfn, int where, u32 val)
  149. {
  150. unsigned int *pci_conf;
  151. u32 cfg;
  152. if (where & 0x3)
  153. return -EINVAL;
  154. if (bus == 0) {
  155. devfn += (0x8 * 6); /* start at AD16=Device0 */
  156. } else if (bus == TGT) {
  157. bus = 0;
  158. devfn = 0; /* special case: bridge controller itself */
  159. }
  160. /* Select bus */
  161. cfg = REGLOAD(priv->regs->cfg_stat);
  162. REGSTORE(priv->regs->cfg_stat, (cfg & ~(0xf << 23)) | (bus << 23));
  163. pci_conf = (unsigned int *) (priv->pci_conf |
  164. (devfn << 8) | (where & 0xfc));
  165. LEON3_BYPASS_STORE_PA(pci_conf, swab32(val));
  166. return 0;
  167. }
  168. static int grpci1_cfg_w16(struct grpci1_priv *priv, unsigned int bus,
  169. unsigned int devfn, int where, u32 val)
  170. {
  171. int ret;
  172. u32 v;
  173. if (where & 0x1)
  174. return -EINVAL;
  175. ret = grpci1_cfg_r32(priv, bus, devfn, where&~3, &v);
  176. if (ret)
  177. return ret;
  178. v = (v & ~(0xffff << (8 * (where & 0x3)))) |
  179. ((0xffff & val) << (8 * (where & 0x3)));
  180. return grpci1_cfg_w32(priv, bus, devfn, where & ~0x3, v);
  181. }
  182. static int grpci1_cfg_w8(struct grpci1_priv *priv, unsigned int bus,
  183. unsigned int devfn, int where, u32 val)
  184. {
  185. int ret;
  186. u32 v;
  187. ret = grpci1_cfg_r32(priv, bus, devfn, where & ~0x3, &v);
  188. if (ret != 0)
  189. return ret;
  190. v = (v & ~(0xff << (8 * (where & 0x3)))) |
  191. ((0xff & val) << (8 * (where & 0x3)));
  192. return grpci1_cfg_w32(priv, bus, devfn, where & ~0x3, v);
  193. }
  194. /* Read from Configuration Space. When entering here the PCI layer has taken
  195. * the pci_lock spinlock and IRQ is off.
  196. */
  197. static int grpci1_read_config(struct pci_bus *bus, unsigned int devfn,
  198. int where, int size, u32 *val)
  199. {
  200. struct grpci1_priv *priv = grpci1priv;
  201. unsigned int busno = bus->number;
  202. int ret;
  203. if (PCI_SLOT(devfn) > 15 || busno > 15) {
  204. *val = ~0;
  205. return 0;
  206. }
  207. switch (size) {
  208. case 1:
  209. ret = grpci1_cfg_r8(priv, busno, devfn, where, val);
  210. break;
  211. case 2:
  212. ret = grpci1_cfg_r16(priv, busno, devfn, where, val);
  213. break;
  214. case 4:
  215. ret = grpci1_cfg_r32(priv, busno, devfn, where, val);
  216. break;
  217. default:
  218. ret = -EINVAL;
  219. break;
  220. }
  221. #ifdef GRPCI1_DEBUG_CFGACCESS
  222. printk(KERN_INFO
  223. "grpci1_read_config: [%02x:%02x:%x] ofs=%d val=%x size=%d\n",
  224. busno, PCI_SLOT(devfn), PCI_FUNC(devfn), where, *val, size);
  225. #endif
  226. return ret;
  227. }
  228. /* Write to Configuration Space. When entering here the PCI layer has taken
  229. * the pci_lock spinlock and IRQ is off.
  230. */
  231. static int grpci1_write_config(struct pci_bus *bus, unsigned int devfn,
  232. int where, int size, u32 val)
  233. {
  234. struct grpci1_priv *priv = grpci1priv;
  235. unsigned int busno = bus->number;
  236. if (PCI_SLOT(devfn) > 15 || busno > 15)
  237. return 0;
  238. #ifdef GRPCI1_DEBUG_CFGACCESS
  239. printk(KERN_INFO
  240. "grpci1_write_config: [%02x:%02x:%x] ofs=%d size=%d val=%x\n",
  241. busno, PCI_SLOT(devfn), PCI_FUNC(devfn), where, size, val);
  242. #endif
  243. switch (size) {
  244. default:
  245. return -EINVAL;
  246. case 1:
  247. return grpci1_cfg_w8(priv, busno, devfn, where, val);
  248. case 2:
  249. return grpci1_cfg_w16(priv, busno, devfn, where, val);
  250. case 4:
  251. return grpci1_cfg_w32(priv, busno, devfn, where, val);
  252. }
  253. }
  254. static struct pci_ops grpci1_ops = {
  255. .read = grpci1_read_config,
  256. .write = grpci1_write_config,
  257. };
  258. /* GENIRQ IRQ chip implementation for grpci1 irqmode=0..2. In configuration
  259. * 3 where all PCI Interrupts has a separate IRQ on the system IRQ controller
  260. * this is not needed and the standard IRQ controller can be used.
  261. */
  262. static void grpci1_mask_irq(struct irq_data *data)
  263. {
  264. u32 irqidx;
  265. struct grpci1_priv *priv = grpci1priv;
  266. irqidx = (u32)data->chip_data - 1;
  267. if (irqidx > 3) /* only mask PCI interrupts here */
  268. return;
  269. irqidx += IRQ_MASK_BIT;
  270. REGSTORE(priv->regs->irq, REGLOAD(priv->regs->irq) & ~(1 << irqidx));
  271. }
  272. static void grpci1_unmask_irq(struct irq_data *data)
  273. {
  274. u32 irqidx;
  275. struct grpci1_priv *priv = grpci1priv;
  276. irqidx = (u32)data->chip_data - 1;
  277. if (irqidx > 3) /* only unmask PCI interrupts here */
  278. return;
  279. irqidx += IRQ_MASK_BIT;
  280. REGSTORE(priv->regs->irq, REGLOAD(priv->regs->irq) | (1 << irqidx));
  281. }
  282. static unsigned int grpci1_startup_irq(struct irq_data *data)
  283. {
  284. grpci1_unmask_irq(data);
  285. return 0;
  286. }
  287. static void grpci1_shutdown_irq(struct irq_data *data)
  288. {
  289. grpci1_mask_irq(data);
  290. }
  291. static struct irq_chip grpci1_irq = {
  292. .name = "grpci1",
  293. .irq_startup = grpci1_startup_irq,
  294. .irq_shutdown = grpci1_shutdown_irq,
  295. .irq_mask = grpci1_mask_irq,
  296. .irq_unmask = grpci1_unmask_irq,
  297. };
  298. /* Handle one or multiple IRQs from the PCI core */
  299. static void grpci1_pci_flow_irq(struct irq_desc *desc)
  300. {
  301. struct grpci1_priv *priv = grpci1priv;
  302. int i, ack = 0;
  303. unsigned int irqreg;
  304. irqreg = REGLOAD(priv->regs->irq);
  305. irqreg = (irqreg >> IRQ_MASK_BIT) & irqreg;
  306. /* Error Interrupt? */
  307. if (irqreg & IRQ_ALL_ERRORS) {
  308. generic_handle_irq(priv->irq_err);
  309. ack = 1;
  310. }
  311. /* PCI Interrupt? */
  312. if (irqreg & IRQ_INTX) {
  313. /* Call respective PCI Interrupt handler */
  314. for (i = 0; i < 4; i++) {
  315. if (irqreg & (1 << i))
  316. generic_handle_irq(priv->irq_map[i]);
  317. }
  318. ack = 1;
  319. }
  320. /*
  321. * Call "first level" IRQ chip end-of-irq handler. It will ACK LEON IRQ
  322. * Controller, this must be done after IRQ sources have been handled to
  323. * avoid double IRQ generation
  324. */
  325. if (ack)
  326. desc->irq_data.chip->irq_eoi(&desc->irq_data);
  327. }
  328. /* Create a virtual IRQ */
  329. static unsigned int grpci1_build_device_irq(unsigned int irq)
  330. {
  331. unsigned int virq = 0, pil;
  332. pil = 1 << 8;
  333. virq = irq_alloc(irq, pil);
  334. if (virq == 0)
  335. goto out;
  336. irq_set_chip_and_handler_name(virq, &grpci1_irq, handle_simple_irq,
  337. "pcilvl");
  338. irq_set_chip_data(virq, (void *)irq);
  339. out:
  340. return virq;
  341. }
  342. /*
  343. * Initialize mappings AMBA<->PCI, clear IRQ state, setup PCI interface
  344. *
  345. * Target BARs:
  346. * BAR0: unused in this implementation
  347. * BAR1: peripheral DMA to host's memory (size at least 256MByte)
  348. * BAR2..BAR5: not implemented in hardware
  349. */
  350. static void grpci1_hw_init(struct grpci1_priv *priv)
  351. {
  352. u32 ahbadr, bar_sz, data, pciadr;
  353. struct grpci1_regs __iomem *regs = priv->regs;
  354. /* set 1:1 mapping between AHB -> PCI memory space */
  355. REGSTORE(regs->cfg_stat, priv->pci_area & 0xf0000000);
  356. /* map PCI accesses to target BAR1 to Linux kernel memory 1:1 */
  357. ahbadr = 0xf0000000 & (u32)__pa(PAGE_ALIGN((unsigned long) &_end));
  358. REGSTORE(regs->page1, ahbadr);
  359. /* translate I/O accesses to 0, I/O Space always @ PCI low 64Kbytes */
  360. REGSTORE(regs->iomap, REGLOAD(regs->iomap) & 0x0000ffff);
  361. /* disable and clear pending interrupts */
  362. REGSTORE(regs->irq, 0);
  363. /* Setup BAR0 outside access range so that it does not conflict with
  364. * peripheral DMA. There is no need to set up the PAGE0 register.
  365. */
  366. grpci1_cfg_w32(priv, TGT, 0, PCI_BASE_ADDRESS_0, 0xffffffff);
  367. grpci1_cfg_r32(priv, TGT, 0, PCI_BASE_ADDRESS_0, &bar_sz);
  368. bar_sz = ~bar_sz + 1;
  369. pciadr = priv->pci_area - bar_sz;
  370. grpci1_cfg_w32(priv, TGT, 0, PCI_BASE_ADDRESS_0, pciadr);
  371. /*
  372. * Setup the Host's PCI Target BAR1 for other peripherals to access,
  373. * and do DMA to the host's memory.
  374. */
  375. grpci1_cfg_w32(priv, TGT, 0, PCI_BASE_ADDRESS_1, ahbadr);
  376. /*
  377. * Setup Latency Timer and cache line size. Default cache line
  378. * size will result in poor performance (256 word fetches), 0xff
  379. * will set it according to the max size of the PCI FIFO.
  380. */
  381. grpci1_cfg_w8(priv, TGT, 0, PCI_CACHE_LINE_SIZE, 0xff);
  382. grpci1_cfg_w8(priv, TGT, 0, PCI_LATENCY_TIMER, 0x40);
  383. /* set as bus master, enable pci memory responses, clear status bits */
  384. grpci1_cfg_r32(priv, TGT, 0, PCI_COMMAND, &data);
  385. data |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  386. grpci1_cfg_w32(priv, TGT, 0, PCI_COMMAND, data);
  387. }
  388. static irqreturn_t grpci1_jump_interrupt(int irq, void *arg)
  389. {
  390. struct grpci1_priv *priv = arg;
  391. dev_err(priv->dev, "Jump IRQ happened\n");
  392. return IRQ_NONE;
  393. }
  394. /* Handle GRPCI1 Error Interrupt */
  395. static irqreturn_t grpci1_err_interrupt(int irq, void *arg)
  396. {
  397. struct grpci1_priv *priv = arg;
  398. u32 status;
  399. grpci1_cfg_r16(priv, TGT, 0, PCI_STATUS, &status);
  400. status &= priv->pci_err_mask;
  401. if (status == 0)
  402. return IRQ_NONE;
  403. if (status & PCI_STATUS_PARITY)
  404. dev_err(priv->dev, "Data Parity Error\n");
  405. if (status & PCI_STATUS_SIG_TARGET_ABORT)
  406. dev_err(priv->dev, "Signalled Target Abort\n");
  407. if (status & PCI_STATUS_REC_TARGET_ABORT)
  408. dev_err(priv->dev, "Received Target Abort\n");
  409. if (status & PCI_STATUS_REC_MASTER_ABORT)
  410. dev_err(priv->dev, "Received Master Abort\n");
  411. if (status & PCI_STATUS_SIG_SYSTEM_ERROR)
  412. dev_err(priv->dev, "Signalled System Error\n");
  413. if (status & PCI_STATUS_DETECTED_PARITY)
  414. dev_err(priv->dev, "Parity Error\n");
  415. /* Clear handled INT TYPE IRQs */
  416. grpci1_cfg_w16(priv, TGT, 0, PCI_STATUS, status);
  417. return IRQ_HANDLED;
  418. }
  419. static int grpci1_of_probe(struct platform_device *ofdev)
  420. {
  421. struct grpci1_regs __iomem *regs;
  422. struct grpci1_priv *priv;
  423. int err, len;
  424. const int *tmp;
  425. u32 cfg, size, err_mask;
  426. struct resource *res;
  427. if (grpci1priv) {
  428. dev_err(&ofdev->dev, "only one GRPCI1 supported\n");
  429. return -ENODEV;
  430. }
  431. if (ofdev->num_resources < 3) {
  432. dev_err(&ofdev->dev, "not enough APB/AHB resources\n");
  433. return -EIO;
  434. }
  435. priv = devm_kzalloc(&ofdev->dev, sizeof(*priv), GFP_KERNEL);
  436. if (!priv) {
  437. dev_err(&ofdev->dev, "memory allocation failed\n");
  438. return -ENOMEM;
  439. }
  440. platform_set_drvdata(ofdev, priv);
  441. priv->dev = &ofdev->dev;
  442. /* find device register base address */
  443. res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
  444. regs = devm_ioremap_resource(&ofdev->dev, res);
  445. if (IS_ERR(regs))
  446. return PTR_ERR(regs);
  447. /*
  448. * check that we're in Host Slot and that we can act as a Host Bridge
  449. * and not only as target/peripheral.
  450. */
  451. cfg = REGLOAD(regs->cfg_stat);
  452. if ((cfg & CFGSTAT_HOST) == 0) {
  453. dev_err(&ofdev->dev, "not in host system slot\n");
  454. return -EIO;
  455. }
  456. /* check that BAR1 support 256 MByte so that we can map kernel space */
  457. REGSTORE(regs->page1, 0xffffffff);
  458. size = ~REGLOAD(regs->page1) + 1;
  459. if (size < 0x10000000) {
  460. dev_err(&ofdev->dev, "BAR1 must be at least 256MByte\n");
  461. return -EIO;
  462. }
  463. /* hardware must support little-endian PCI (byte-twisting) */
  464. if ((REGLOAD(regs->page0) & PAGE0_BTEN) == 0) {
  465. dev_err(&ofdev->dev, "byte-twisting is required\n");
  466. return -EIO;
  467. }
  468. priv->regs = regs;
  469. priv->irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  470. dev_info(&ofdev->dev, "host found at 0x%p, irq%d\n", regs, priv->irq);
  471. /* Find PCI Memory, I/O and Configuration Space Windows */
  472. priv->pci_area = ofdev->resource[1].start;
  473. priv->pci_area_end = ofdev->resource[1].end+1;
  474. priv->pci_io = ofdev->resource[2].start;
  475. priv->pci_conf = ofdev->resource[2].start + 0x10000;
  476. priv->pci_conf_end = priv->pci_conf + 0x10000;
  477. priv->pci_io_va = (unsigned long)ioremap(priv->pci_io, 0x10000);
  478. if (!priv->pci_io_va) {
  479. dev_err(&ofdev->dev, "unable to map PCI I/O area\n");
  480. return -EIO;
  481. }
  482. printk(KERN_INFO
  483. "GRPCI1: MEMORY SPACE [0x%08lx - 0x%08lx]\n"
  484. " I/O SPACE [0x%08lx - 0x%08lx]\n"
  485. " CONFIG SPACE [0x%08lx - 0x%08lx]\n",
  486. priv->pci_area, priv->pci_area_end-1,
  487. priv->pci_io, priv->pci_conf-1,
  488. priv->pci_conf, priv->pci_conf_end-1);
  489. /*
  490. * I/O Space resources in I/O Window mapped into Virtual Adr Space
  491. * We never use low 4KB because some devices seem have problems using
  492. * address 0.
  493. */
  494. priv->info.io_space.name = "GRPCI1 PCI I/O Space";
  495. priv->info.io_space.start = priv->pci_io_va + 0x1000;
  496. priv->info.io_space.end = priv->pci_io_va + 0x10000 - 1;
  497. priv->info.io_space.flags = IORESOURCE_IO;
  498. /*
  499. * grpci1 has no prefetchable memory, map everything as
  500. * non-prefetchable memory
  501. */
  502. priv->info.mem_space.name = "GRPCI1 PCI MEM Space";
  503. priv->info.mem_space.start = priv->pci_area;
  504. priv->info.mem_space.end = priv->pci_area_end - 1;
  505. priv->info.mem_space.flags = IORESOURCE_MEM;
  506. if (request_resource(&iomem_resource, &priv->info.mem_space) < 0) {
  507. dev_err(&ofdev->dev, "unable to request PCI memory area\n");
  508. err = -ENOMEM;
  509. goto err1;
  510. }
  511. if (request_resource(&ioport_resource, &priv->info.io_space) < 0) {
  512. dev_err(&ofdev->dev, "unable to request PCI I/O area\n");
  513. err = -ENOMEM;
  514. goto err2;
  515. }
  516. /* setup maximum supported PCI buses */
  517. priv->info.busn.name = "GRPCI1 busn";
  518. priv->info.busn.start = 0;
  519. priv->info.busn.end = 15;
  520. grpci1priv = priv;
  521. /* Initialize hardware */
  522. grpci1_hw_init(priv);
  523. /*
  524. * Get PCI Interrupt to System IRQ mapping and setup IRQ handling
  525. * Error IRQ. All PCI and PCI-Error interrupts are shared using the
  526. * same system IRQ.
  527. */
  528. leon_update_virq_handling(priv->irq, grpci1_pci_flow_irq, "pcilvl", 0);
  529. priv->irq_map[0] = grpci1_build_device_irq(1);
  530. priv->irq_map[1] = grpci1_build_device_irq(2);
  531. priv->irq_map[2] = grpci1_build_device_irq(3);
  532. priv->irq_map[3] = grpci1_build_device_irq(4);
  533. priv->irq_err = grpci1_build_device_irq(5);
  534. printk(KERN_INFO " PCI INTA..D#: IRQ%d, IRQ%d, IRQ%d, IRQ%d\n",
  535. priv->irq_map[0], priv->irq_map[1], priv->irq_map[2],
  536. priv->irq_map[3]);
  537. /* Enable IRQs on LEON IRQ controller */
  538. err = devm_request_irq(&ofdev->dev, priv->irq, grpci1_jump_interrupt, 0,
  539. "GRPCI1_JUMP", priv);
  540. if (err) {
  541. dev_err(&ofdev->dev, "ERR IRQ request failed: %d\n", err);
  542. goto err3;
  543. }
  544. /* Setup IRQ handler for access errors */
  545. err = devm_request_irq(&ofdev->dev, priv->irq_err,
  546. grpci1_err_interrupt, IRQF_SHARED, "GRPCI1_ERR",
  547. priv);
  548. if (err) {
  549. dev_err(&ofdev->dev, "ERR VIRQ request failed: %d\n", err);
  550. goto err3;
  551. }
  552. tmp = of_get_property(ofdev->dev.of_node, "all_pci_errors", &len);
  553. if (tmp && (len == 4)) {
  554. priv->pci_err_mask = ALL_PCI_ERRORS;
  555. err_mask = IRQ_ALL_ERRORS << IRQ_MASK_BIT;
  556. } else {
  557. priv->pci_err_mask = DEF_PCI_ERRORS;
  558. err_mask = IRQ_DEF_ERRORS << IRQ_MASK_BIT;
  559. }
  560. /*
  561. * Enable Error Interrupts. PCI interrupts are unmasked once request_irq
  562. * is called by the PCI Device drivers
  563. */
  564. REGSTORE(regs->irq, err_mask);
  565. /* Init common layer and scan buses */
  566. priv->info.ops = &grpci1_ops;
  567. priv->info.map_irq = grpci1_map_irq;
  568. leon_pci_init(ofdev, &priv->info);
  569. return 0;
  570. err3:
  571. release_resource(&priv->info.io_space);
  572. err2:
  573. release_resource(&priv->info.mem_space);
  574. err1:
  575. iounmap((void __iomem *)priv->pci_io_va);
  576. grpci1priv = NULL;
  577. return err;
  578. }
  579. static struct of_device_id grpci1_of_match[] = {
  580. {
  581. .name = "GAISLER_PCIFBRG",
  582. },
  583. {
  584. .name = "01_014",
  585. },
  586. {},
  587. };
  588. static struct platform_driver grpci1_of_driver = {
  589. .driver = {
  590. .name = "grpci1",
  591. .of_match_table = grpci1_of_match,
  592. },
  593. .probe = grpci1_of_probe,
  594. };
  595. static int __init grpci1_init(void)
  596. {
  597. return platform_driver_register(&grpci1_of_driver);
  598. }
  599. subsys_initcall(grpci1_init);