pcie-xilinx.c 18 KB

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