leon_pci_grpci1.c 19 KB

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