pcie-xilinx.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * PCIe host controller driver for Xilinx AXI PCIe Bridge
  3. *
  4. * Copyright (c) 2012 - 2014 Xilinx, Inc.
  5. *
  6. * Based on the Tegra PCIe driver
  7. *
  8. * Bits taken from Synopsys Designware Host controller driver and
  9. * ARM PCI Host generic driver.
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/msi.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_pci.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/pci.h>
  27. #include <linux/platform_device.h>
  28. /* Register definitions */
  29. #define XILINX_PCIE_REG_BIR 0x00000130
  30. #define XILINX_PCIE_REG_IDR 0x00000138
  31. #define XILINX_PCIE_REG_IMR 0x0000013c
  32. #define XILINX_PCIE_REG_PSCR 0x00000144
  33. #define XILINX_PCIE_REG_RPSC 0x00000148
  34. #define XILINX_PCIE_REG_MSIBASE1 0x0000014c
  35. #define XILINX_PCIE_REG_MSIBASE2 0x00000150
  36. #define XILINX_PCIE_REG_RPEFR 0x00000154
  37. #define XILINX_PCIE_REG_RPIFR1 0x00000158
  38. #define XILINX_PCIE_REG_RPIFR2 0x0000015c
  39. /* Interrupt registers definitions */
  40. #define XILINX_PCIE_INTR_LINK_DOWN BIT(0)
  41. #define XILINX_PCIE_INTR_ECRC_ERR BIT(1)
  42. #define XILINX_PCIE_INTR_STR_ERR BIT(2)
  43. #define XILINX_PCIE_INTR_HOT_RESET BIT(3)
  44. #define XILINX_PCIE_INTR_CFG_TIMEOUT BIT(8)
  45. #define XILINX_PCIE_INTR_CORRECTABLE BIT(9)
  46. #define XILINX_PCIE_INTR_NONFATAL BIT(10)
  47. #define XILINX_PCIE_INTR_FATAL BIT(11)
  48. #define XILINX_PCIE_INTR_INTX BIT(16)
  49. #define XILINX_PCIE_INTR_MSI BIT(17)
  50. #define XILINX_PCIE_INTR_SLV_UNSUPP BIT(20)
  51. #define XILINX_PCIE_INTR_SLV_UNEXP BIT(21)
  52. #define XILINX_PCIE_INTR_SLV_COMPL BIT(22)
  53. #define XILINX_PCIE_INTR_SLV_ERRP BIT(23)
  54. #define XILINX_PCIE_INTR_SLV_CMPABT BIT(24)
  55. #define XILINX_PCIE_INTR_SLV_ILLBUR BIT(25)
  56. #define XILINX_PCIE_INTR_MST_DECERR BIT(26)
  57. #define XILINX_PCIE_INTR_MST_SLVERR BIT(27)
  58. #define XILINX_PCIE_INTR_MST_ERRP BIT(28)
  59. #define XILINX_PCIE_IMR_ALL_MASK 0x1FF30FED
  60. #define XILINX_PCIE_IDR_ALL_MASK 0xFFFFFFFF
  61. /* Root Port Error FIFO Read Register definitions */
  62. #define XILINX_PCIE_RPEFR_ERR_VALID BIT(18)
  63. #define XILINX_PCIE_RPEFR_REQ_ID GENMASK(15, 0)
  64. #define XILINX_PCIE_RPEFR_ALL_MASK 0xFFFFFFFF
  65. /* Root Port Interrupt FIFO Read Register 1 definitions */
  66. #define XILINX_PCIE_RPIFR1_INTR_VALID BIT(31)
  67. #define XILINX_PCIE_RPIFR1_MSI_INTR BIT(30)
  68. #define XILINX_PCIE_RPIFR1_INTR_MASK GENMASK(28, 27)
  69. #define XILINX_PCIE_RPIFR1_ALL_MASK 0xFFFFFFFF
  70. #define XILINX_PCIE_RPIFR1_INTR_SHIFT 27
  71. /* Bridge Info Register definitions */
  72. #define XILINX_PCIE_BIR_ECAM_SZ_MASK GENMASK(18, 16)
  73. #define XILINX_PCIE_BIR_ECAM_SZ_SHIFT 16
  74. /* Root Port Interrupt FIFO Read Register 2 definitions */
  75. #define XILINX_PCIE_RPIFR2_MSG_DATA GENMASK(15, 0)
  76. /* Root Port Status/control Register definitions */
  77. #define XILINX_PCIE_REG_RPSC_BEN BIT(0)
  78. /* Phy Status/Control Register definitions */
  79. #define XILINX_PCIE_REG_PSCR_LNKUP BIT(11)
  80. /* ECAM definitions */
  81. #define ECAM_BUS_NUM_SHIFT 20
  82. #define ECAM_DEV_NUM_SHIFT 12
  83. /* Number of MSI IRQs */
  84. #define XILINX_NUM_MSI_IRQS 128
  85. /**
  86. * struct xilinx_pcie_port - PCIe port information
  87. * @reg_base: IO Mapped Register Base
  88. * @irq: Interrupt number
  89. * @msi_pages: MSI pages
  90. * @root_busno: Root Bus number
  91. * @dev: Device pointer
  92. * @msi_domain: MSI IRQ domain pointer
  93. * @leg_domain: Legacy IRQ domain pointer
  94. * @resources: Bus Resources
  95. */
  96. struct xilinx_pcie_port {
  97. void __iomem *reg_base;
  98. u32 irq;
  99. unsigned long msi_pages;
  100. u8 root_busno;
  101. struct device *dev;
  102. struct irq_domain *msi_domain;
  103. struct irq_domain *leg_domain;
  104. struct list_head resources;
  105. };
  106. static DECLARE_BITMAP(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
  107. static inline u32 pcie_read(struct xilinx_pcie_port *port, u32 reg)
  108. {
  109. return readl(port->reg_base + reg);
  110. }
  111. static inline void pcie_write(struct xilinx_pcie_port *port, u32 val, u32 reg)
  112. {
  113. writel(val, port->reg_base + reg);
  114. }
  115. static inline bool xilinx_pcie_link_is_up(struct xilinx_pcie_port *port)
  116. {
  117. return (pcie_read(port, XILINX_PCIE_REG_PSCR) &
  118. XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
  119. }
  120. /**
  121. * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
  122. * @port: PCIe port information
  123. */
  124. static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port *port)
  125. {
  126. struct device *dev = port->dev;
  127. unsigned long val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
  128. if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
  129. dev_dbg(dev, "Requester ID %lu\n",
  130. val & XILINX_PCIE_RPEFR_REQ_ID);
  131. pcie_write(port, XILINX_PCIE_RPEFR_ALL_MASK,
  132. XILINX_PCIE_REG_RPEFR);
  133. }
  134. }
  135. /**
  136. * xilinx_pcie_valid_device - Check if a valid device is present on bus
  137. * @bus: PCI Bus structure
  138. * @devfn: device/function
  139. *
  140. * Return: 'true' on success and 'false' if invalid device is found
  141. */
  142. static bool xilinx_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
  143. {
  144. struct xilinx_pcie_port *port = bus->sysdata;
  145. /* Check if link is up when trying to access downstream ports */
  146. if (bus->number != port->root_busno)
  147. if (!xilinx_pcie_link_is_up(port))
  148. return false;
  149. /* Only one device down on each root port */
  150. if (bus->number == port->root_busno && devfn > 0)
  151. return false;
  152. return true;
  153. }
  154. /**
  155. * xilinx_pcie_map_bus - Get configuration base
  156. * @bus: PCI Bus structure
  157. * @devfn: Device/function
  158. * @where: Offset from base
  159. *
  160. * Return: Base address of the configuration space needed to be
  161. * accessed.
  162. */
  163. static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
  164. unsigned int devfn, int where)
  165. {
  166. struct xilinx_pcie_port *port = bus->sysdata;
  167. int relbus;
  168. if (!xilinx_pcie_valid_device(bus, devfn))
  169. return NULL;
  170. relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
  171. (devfn << ECAM_DEV_NUM_SHIFT);
  172. return port->reg_base + relbus + where;
  173. }
  174. /* PCIe operations */
  175. static struct pci_ops xilinx_pcie_ops = {
  176. .map_bus = xilinx_pcie_map_bus,
  177. .read = pci_generic_config_read,
  178. .write = pci_generic_config_write,
  179. };
  180. /* MSI functions */
  181. /**
  182. * xilinx_pcie_destroy_msi - Free MSI number
  183. * @irq: IRQ to be freed
  184. */
  185. static void xilinx_pcie_destroy_msi(unsigned int irq)
  186. {
  187. struct msi_desc *msi;
  188. struct xilinx_pcie_port *port;
  189. struct irq_data *d = irq_get_irq_data(irq);
  190. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  191. if (!test_bit(hwirq, msi_irq_in_use)) {
  192. msi = irq_get_msi_desc(irq);
  193. port = msi_desc_to_pci_sysdata(msi);
  194. dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
  195. } else {
  196. clear_bit(hwirq, msi_irq_in_use);
  197. }
  198. }
  199. /**
  200. * xilinx_pcie_assign_msi - Allocate MSI number
  201. *
  202. * Return: A valid IRQ on success and error value on failure.
  203. */
  204. static int xilinx_pcie_assign_msi(void)
  205. {
  206. int pos;
  207. pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
  208. if (pos < XILINX_NUM_MSI_IRQS)
  209. set_bit(pos, msi_irq_in_use);
  210. else
  211. return -ENOSPC;
  212. return pos;
  213. }
  214. /**
  215. * xilinx_msi_teardown_irq - Destroy the MSI
  216. * @chip: MSI Chip descriptor
  217. * @irq: MSI IRQ to destroy
  218. */
  219. static void xilinx_msi_teardown_irq(struct msi_controller *chip,
  220. unsigned int irq)
  221. {
  222. xilinx_pcie_destroy_msi(irq);
  223. irq_dispose_mapping(irq);
  224. }
  225. /**
  226. * xilinx_pcie_msi_setup_irq - Setup MSI request
  227. * @chip: MSI chip pointer
  228. * @pdev: PCIe device pointer
  229. * @desc: MSI descriptor pointer
  230. *
  231. * Return: '0' on success and error value on failure
  232. */
  233. static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
  234. struct pci_dev *pdev,
  235. struct msi_desc *desc)
  236. {
  237. struct xilinx_pcie_port *port = pdev->bus->sysdata;
  238. unsigned int irq;
  239. int hwirq;
  240. struct msi_msg msg;
  241. phys_addr_t msg_addr;
  242. hwirq = xilinx_pcie_assign_msi();
  243. if (hwirq < 0)
  244. return hwirq;
  245. irq = irq_create_mapping(port->msi_domain, hwirq);
  246. if (!irq)
  247. return -EINVAL;
  248. irq_set_msi_desc(irq, desc);
  249. msg_addr = virt_to_phys((void *)port->msi_pages);
  250. msg.address_hi = 0;
  251. msg.address_lo = msg_addr;
  252. msg.data = irq;
  253. pci_write_msi_msg(irq, &msg);
  254. return 0;
  255. }
  256. /* MSI Chip Descriptor */
  257. static struct msi_controller xilinx_pcie_msi_chip = {
  258. .setup_irq = xilinx_pcie_msi_setup_irq,
  259. .teardown_irq = xilinx_msi_teardown_irq,
  260. };
  261. /* HW Interrupt Chip Descriptor */
  262. static struct irq_chip xilinx_msi_irq_chip = {
  263. .name = "Xilinx PCIe MSI",
  264. .irq_enable = pci_msi_unmask_irq,
  265. .irq_disable = pci_msi_mask_irq,
  266. .irq_mask = pci_msi_mask_irq,
  267. .irq_unmask = pci_msi_unmask_irq,
  268. };
  269. /**
  270. * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
  271. * @domain: IRQ domain
  272. * @irq: Virtual IRQ number
  273. * @hwirq: HW interrupt number
  274. *
  275. * Return: Always returns 0.
  276. */
  277. static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
  278. irq_hw_number_t hwirq)
  279. {
  280. irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
  281. irq_set_chip_data(irq, domain->host_data);
  282. return 0;
  283. }
  284. /* IRQ Domain operations */
  285. static const struct irq_domain_ops msi_domain_ops = {
  286. .map = xilinx_pcie_msi_map,
  287. };
  288. /**
  289. * xilinx_pcie_enable_msi - Enable MSI support
  290. * @port: PCIe port information
  291. */
  292. static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
  293. {
  294. phys_addr_t msg_addr;
  295. port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
  296. msg_addr = virt_to_phys((void *)port->msi_pages);
  297. pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
  298. pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
  299. }
  300. /* INTx Functions */
  301. /**
  302. * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
  303. * @domain: IRQ domain
  304. * @irq: Virtual IRQ number
  305. * @hwirq: HW interrupt number
  306. *
  307. * Return: Always returns 0.
  308. */
  309. static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  310. irq_hw_number_t hwirq)
  311. {
  312. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  313. irq_set_chip_data(irq, domain->host_data);
  314. return 0;
  315. }
  316. /* INTx IRQ Domain operations */
  317. static const struct irq_domain_ops intx_domain_ops = {
  318. .map = xilinx_pcie_intx_map,
  319. };
  320. /* PCIe HW Functions */
  321. /**
  322. * xilinx_pcie_intr_handler - Interrupt Service Handler
  323. * @irq: IRQ number
  324. * @data: PCIe port information
  325. *
  326. * Return: IRQ_HANDLED on success and IRQ_NONE on failure
  327. */
  328. static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
  329. {
  330. struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
  331. struct device *dev = port->dev;
  332. u32 val, mask, status, msi_data;
  333. /* Read interrupt decode and mask registers */
  334. val = pcie_read(port, XILINX_PCIE_REG_IDR);
  335. mask = pcie_read(port, XILINX_PCIE_REG_IMR);
  336. status = val & mask;
  337. if (!status)
  338. return IRQ_NONE;
  339. if (status & XILINX_PCIE_INTR_LINK_DOWN)
  340. dev_warn(dev, "Link Down\n");
  341. if (status & XILINX_PCIE_INTR_ECRC_ERR)
  342. dev_warn(dev, "ECRC failed\n");
  343. if (status & XILINX_PCIE_INTR_STR_ERR)
  344. dev_warn(dev, "Streaming error\n");
  345. if (status & XILINX_PCIE_INTR_HOT_RESET)
  346. dev_info(dev, "Hot reset\n");
  347. if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
  348. dev_warn(dev, "ECAM access timeout\n");
  349. if (status & XILINX_PCIE_INTR_CORRECTABLE) {
  350. dev_warn(dev, "Correctable error message\n");
  351. xilinx_pcie_clear_err_interrupts(port);
  352. }
  353. if (status & XILINX_PCIE_INTR_NONFATAL) {
  354. dev_warn(dev, "Non fatal error message\n");
  355. xilinx_pcie_clear_err_interrupts(port);
  356. }
  357. if (status & XILINX_PCIE_INTR_FATAL) {
  358. dev_warn(dev, "Fatal error message\n");
  359. xilinx_pcie_clear_err_interrupts(port);
  360. }
  361. if (status & XILINX_PCIE_INTR_INTX) {
  362. /* INTx interrupt received */
  363. val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
  364. /* Check whether interrupt valid */
  365. if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
  366. dev_warn(dev, "RP Intr FIFO1 read error\n");
  367. goto error;
  368. }
  369. if (!(val & XILINX_PCIE_RPIFR1_MSI_INTR)) {
  370. /* Clear interrupt FIFO register 1 */
  371. pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
  372. XILINX_PCIE_REG_RPIFR1);
  373. /* Handle INTx Interrupt */
  374. val = ((val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
  375. XILINX_PCIE_RPIFR1_INTR_SHIFT) + 1;
  376. generic_handle_irq(irq_find_mapping(port->leg_domain,
  377. val));
  378. }
  379. }
  380. if (status & XILINX_PCIE_INTR_MSI) {
  381. /* MSI Interrupt */
  382. val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
  383. if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
  384. dev_warn(dev, "RP Intr FIFO1 read error\n");
  385. goto error;
  386. }
  387. if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
  388. msi_data = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
  389. XILINX_PCIE_RPIFR2_MSG_DATA;
  390. /* Clear interrupt FIFO register 1 */
  391. pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
  392. XILINX_PCIE_REG_RPIFR1);
  393. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  394. /* Handle MSI Interrupt */
  395. generic_handle_irq(msi_data);
  396. }
  397. }
  398. }
  399. if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
  400. dev_warn(dev, "Slave unsupported request\n");
  401. if (status & XILINX_PCIE_INTR_SLV_UNEXP)
  402. dev_warn(dev, "Slave unexpected completion\n");
  403. if (status & XILINX_PCIE_INTR_SLV_COMPL)
  404. dev_warn(dev, "Slave completion timeout\n");
  405. if (status & XILINX_PCIE_INTR_SLV_ERRP)
  406. dev_warn(dev, "Slave Error Poison\n");
  407. if (status & XILINX_PCIE_INTR_SLV_CMPABT)
  408. dev_warn(dev, "Slave Completer Abort\n");
  409. if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
  410. dev_warn(dev, "Slave Illegal Burst\n");
  411. if (status & XILINX_PCIE_INTR_MST_DECERR)
  412. dev_warn(dev, "Master decode error\n");
  413. if (status & XILINX_PCIE_INTR_MST_SLVERR)
  414. dev_warn(dev, "Master slave error\n");
  415. if (status & XILINX_PCIE_INTR_MST_ERRP)
  416. dev_warn(dev, "Master error poison\n");
  417. error:
  418. /* Clear the Interrupt Decode register */
  419. pcie_write(port, status, XILINX_PCIE_REG_IDR);
  420. return IRQ_HANDLED;
  421. }
  422. /**
  423. * xilinx_pcie_init_irq_domain - Initialize IRQ domain
  424. * @port: PCIe port information
  425. *
  426. * Return: '0' on success and error value on failure
  427. */
  428. static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
  429. {
  430. struct device *dev = port->dev;
  431. struct device_node *node = dev->of_node;
  432. struct device_node *pcie_intc_node;
  433. /* Setup INTx */
  434. pcie_intc_node = of_get_next_child(node, NULL);
  435. if (!pcie_intc_node) {
  436. dev_err(dev, "No PCIe Intc node found\n");
  437. return -ENODEV;
  438. }
  439. port->leg_domain = irq_domain_add_linear(pcie_intc_node, 4,
  440. &intx_domain_ops,
  441. port);
  442. if (!port->leg_domain) {
  443. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  444. return -ENODEV;
  445. }
  446. /* Setup MSI */
  447. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  448. port->msi_domain = irq_domain_add_linear(node,
  449. XILINX_NUM_MSI_IRQS,
  450. &msi_domain_ops,
  451. &xilinx_pcie_msi_chip);
  452. if (!port->msi_domain) {
  453. dev_err(dev, "Failed to get a MSI IRQ domain\n");
  454. return -ENODEV;
  455. }
  456. xilinx_pcie_enable_msi(port);
  457. }
  458. return 0;
  459. }
  460. /**
  461. * xilinx_pcie_init_port - Initialize hardware
  462. * @port: PCIe port information
  463. */
  464. static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
  465. {
  466. struct device *dev = port->dev;
  467. if (xilinx_pcie_link_is_up(port))
  468. dev_info(dev, "PCIe Link is UP\n");
  469. else
  470. dev_info(dev, "PCIe Link is DOWN\n");
  471. /* Disable all interrupts */
  472. pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
  473. XILINX_PCIE_REG_IMR);
  474. /* Clear pending interrupts */
  475. pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
  476. XILINX_PCIE_IMR_ALL_MASK,
  477. XILINX_PCIE_REG_IDR);
  478. /* Enable all interrupts */
  479. pcie_write(port, XILINX_PCIE_IMR_ALL_MASK, XILINX_PCIE_REG_IMR);
  480. /* Enable the Bridge enable bit */
  481. pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
  482. XILINX_PCIE_REG_RPSC_BEN,
  483. XILINX_PCIE_REG_RPSC);
  484. }
  485. /**
  486. * xilinx_pcie_parse_dt - Parse Device tree
  487. * @port: PCIe port information
  488. *
  489. * Return: '0' on success and error value on failure
  490. */
  491. static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
  492. {
  493. struct device *dev = port->dev;
  494. struct device_node *node = dev->of_node;
  495. struct resource regs;
  496. const char *type;
  497. int err;
  498. type = of_get_property(node, "device_type", NULL);
  499. if (!type || strcmp(type, "pci")) {
  500. dev_err(dev, "invalid \"device_type\" %s\n", type);
  501. return -EINVAL;
  502. }
  503. err = of_address_to_resource(node, 0, &regs);
  504. if (err) {
  505. dev_err(dev, "missing \"reg\" property\n");
  506. return err;
  507. }
  508. port->reg_base = devm_ioremap_resource(dev, &regs);
  509. if (IS_ERR(port->reg_base))
  510. return PTR_ERR(port->reg_base);
  511. port->irq = irq_of_parse_and_map(node, 0);
  512. err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
  513. IRQF_SHARED | IRQF_NO_THREAD,
  514. "xilinx-pcie", port);
  515. if (err) {
  516. dev_err(dev, "unable to request irq %d\n", port->irq);
  517. return err;
  518. }
  519. return 0;
  520. }
  521. /**
  522. * xilinx_pcie_probe - Probe function
  523. * @pdev: Platform device pointer
  524. *
  525. * Return: '0' on success and error value on failure
  526. */
  527. static int xilinx_pcie_probe(struct platform_device *pdev)
  528. {
  529. struct device *dev = &pdev->dev;
  530. struct xilinx_pcie_port *port;
  531. struct pci_bus *bus;
  532. int err;
  533. resource_size_t iobase = 0;
  534. LIST_HEAD(res);
  535. if (!dev->of_node)
  536. return -ENODEV;
  537. port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  538. if (!port)
  539. return -ENOMEM;
  540. port->dev = dev;
  541. err = xilinx_pcie_parse_dt(port);
  542. if (err) {
  543. dev_err(dev, "Parsing DT failed\n");
  544. return err;
  545. }
  546. xilinx_pcie_init_port(port);
  547. err = xilinx_pcie_init_irq_domain(port);
  548. if (err) {
  549. dev_err(dev, "Failed creating IRQ Domain\n");
  550. return err;
  551. }
  552. err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff, &res,
  553. &iobase);
  554. if (err) {
  555. dev_err(dev, "Getting bridge resources failed\n");
  556. return err;
  557. }
  558. err = devm_request_pci_bus_resources(dev, &res);
  559. if (err)
  560. goto error;
  561. bus = pci_create_root_bus(dev, 0, &xilinx_pcie_ops, port, &res);
  562. if (!bus) {
  563. err = -ENOMEM;
  564. goto error;
  565. }
  566. #ifdef CONFIG_PCI_MSI
  567. xilinx_pcie_msi_chip.dev = dev;
  568. bus->msi = &xilinx_pcie_msi_chip;
  569. #endif
  570. pci_scan_child_bus(bus);
  571. pci_assign_unassigned_bus_resources(bus);
  572. #ifndef CONFIG_MICROBLAZE
  573. pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
  574. #endif
  575. pci_bus_add_devices(bus);
  576. return 0;
  577. error:
  578. pci_free_resource_list(&res);
  579. return err;
  580. }
  581. static struct of_device_id xilinx_pcie_of_match[] = {
  582. { .compatible = "xlnx,axi-pcie-host-1.00.a", },
  583. {}
  584. };
  585. static struct platform_driver xilinx_pcie_driver = {
  586. .driver = {
  587. .name = "xilinx-pcie",
  588. .of_match_table = xilinx_pcie_of_match,
  589. .suppress_bind_attrs = true,
  590. },
  591. .probe = xilinx_pcie_probe,
  592. };
  593. builtin_platform_driver(xilinx_pcie_driver);