pcie-xilinx.c 18 KB

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