leon_pci_grpci1.c 19 KB

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