pcie-xilinx.c 18 KB

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