pcie-xilinx-cpm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCIe host controller driver for Xilinx Versal CPM DMA Bridge
  4. *
  5. * (C) Copyright 2019 - 2020, Xilinx, Inc.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/irq.h>
  10. #include <linux/irqchip.h>
  11. #include <linux/irqchip/chained_irq.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_pci.h>
  17. #include <linux/of_platform.h>
  18. #include "../pci.h"
  19. #include "pcie-xilinx-common.h"
  20. /* Register definitions */
  21. #define XILINX_CPM_PCIE_REG_IDR 0x00000E10
  22. #define XILINX_CPM_PCIE_REG_IMR 0x00000E14
  23. #define XILINX_CPM_PCIE_REG_PSCR 0x00000E1C
  24. #define XILINX_CPM_PCIE_REG_RPSC 0x00000E20
  25. #define XILINX_CPM_PCIE_REG_RPEFR 0x00000E2C
  26. #define XILINX_CPM_PCIE_REG_IDRN 0x00000E38
  27. #define XILINX_CPM_PCIE_REG_IDRN_MASK 0x00000E3C
  28. #define XILINX_CPM_PCIE_MISC_IR_STATUS 0x00000340
  29. #define XILINX_CPM_PCIE_MISC_IR_ENABLE 0x00000348
  30. #define XILINX_CPM_PCIE_MISC_IR_LOCAL BIT(1)
  31. #define XILINX_CPM_PCIE_IR_STATUS 0x000002A0
  32. #define XILINX_CPM_PCIE_IR_ENABLE 0x000002A8
  33. #define XILINX_CPM_PCIE_IR_LOCAL BIT(0)
  34. #define IMR(x) BIT(XILINX_PCIE_INTR_ ##x)
  35. #define XILINX_CPM_PCIE_IMR_ALL_MASK \
  36. ( \
  37. IMR(LINK_DOWN) | \
  38. IMR(HOT_RESET) | \
  39. IMR(CFG_PCIE_TIMEOUT) | \
  40. IMR(CFG_TIMEOUT) | \
  41. IMR(CORRECTABLE) | \
  42. IMR(NONFATAL) | \
  43. IMR(FATAL) | \
  44. IMR(CFG_ERR_POISON) | \
  45. IMR(PME_TO_ACK_RCVD) | \
  46. IMR(INTX) | \
  47. IMR(PM_PME_RCVD) | \
  48. IMR(SLV_UNSUPP) | \
  49. IMR(SLV_UNEXP) | \
  50. IMR(SLV_COMPL) | \
  51. IMR(SLV_ERRP) | \
  52. IMR(SLV_CMPABT) | \
  53. IMR(SLV_ILLBUR) | \
  54. IMR(MST_DECERR) | \
  55. IMR(MST_SLVERR) | \
  56. IMR(SLV_PCIE_TIMEOUT) \
  57. )
  58. #define XILINX_CPM_PCIE_IDR_ALL_MASK 0xFFFFFFFF
  59. #define XILINX_CPM_PCIE_IDRN_MASK GENMASK(19, 16)
  60. #define XILINX_CPM_PCIE_IDRN_SHIFT 16
  61. /* Root Port Error FIFO Read Register definitions */
  62. #define XILINX_CPM_PCIE_RPEFR_ERR_VALID BIT(18)
  63. #define XILINX_CPM_PCIE_RPEFR_REQ_ID GENMASK(15, 0)
  64. #define XILINX_CPM_PCIE_RPEFR_ALL_MASK 0xFFFFFFFF
  65. /* Root Port Status/control Register definitions */
  66. #define XILINX_CPM_PCIE_REG_RPSC_BEN BIT(0)
  67. /* Phy Status/Control Register definitions */
  68. #define XILINX_CPM_PCIE_REG_PSCR_LNKUP BIT(11)
  69. enum xilinx_cpm_version {
  70. CPM,
  71. CPM5,
  72. };
  73. /**
  74. * struct xilinx_cpm_variant - CPM variant information
  75. * @version: CPM version
  76. */
  77. struct xilinx_cpm_variant {
  78. enum xilinx_cpm_version version;
  79. };
  80. /**
  81. * struct xilinx_cpm_pcie - PCIe port information
  82. * @dev: Device pointer
  83. * @reg_base: Bridge Register Base
  84. * @cpm_base: CPM System Level Control and Status Register(SLCR) Base
  85. * @intx_domain: Legacy IRQ domain pointer
  86. * @cpm_domain: CPM IRQ domain pointer
  87. * @cfg: Holds mappings of config space window
  88. * @intx_irq: legacy interrupt number
  89. * @irq: Error interrupt number
  90. * @lock: lock protecting shared register access
  91. * @variant: CPM version check pointer
  92. */
  93. struct xilinx_cpm_pcie {
  94. struct device *dev;
  95. void __iomem *reg_base;
  96. void __iomem *cpm_base;
  97. struct irq_domain *intx_domain;
  98. struct irq_domain *cpm_domain;
  99. struct pci_config_window *cfg;
  100. int intx_irq;
  101. int irq;
  102. raw_spinlock_t lock;
  103. const struct xilinx_cpm_variant *variant;
  104. };
  105. static u32 pcie_read(struct xilinx_cpm_pcie *port, u32 reg)
  106. {
  107. return readl_relaxed(port->reg_base + reg);
  108. }
  109. static void pcie_write(struct xilinx_cpm_pcie *port,
  110. u32 val, u32 reg)
  111. {
  112. writel_relaxed(val, port->reg_base + reg);
  113. }
  114. static bool cpm_pcie_link_up(struct xilinx_cpm_pcie *port)
  115. {
  116. return (pcie_read(port, XILINX_CPM_PCIE_REG_PSCR) &
  117. XILINX_CPM_PCIE_REG_PSCR_LNKUP);
  118. }
  119. static void cpm_pcie_clear_err_interrupts(struct xilinx_cpm_pcie *port)
  120. {
  121. unsigned long val = pcie_read(port, XILINX_CPM_PCIE_REG_RPEFR);
  122. if (val & XILINX_CPM_PCIE_RPEFR_ERR_VALID) {
  123. dev_dbg(port->dev, "Requester ID %lu\n",
  124. val & XILINX_CPM_PCIE_RPEFR_REQ_ID);
  125. pcie_write(port, XILINX_CPM_PCIE_RPEFR_ALL_MASK,
  126. XILINX_CPM_PCIE_REG_RPEFR);
  127. }
  128. }
  129. static void xilinx_cpm_mask_leg_irq(struct irq_data *data)
  130. {
  131. struct xilinx_cpm_pcie *port = irq_data_get_irq_chip_data(data);
  132. unsigned long flags;
  133. u32 mask;
  134. u32 val;
  135. mask = BIT(data->hwirq + XILINX_CPM_PCIE_IDRN_SHIFT);
  136. raw_spin_lock_irqsave(&port->lock, flags);
  137. val = pcie_read(port, XILINX_CPM_PCIE_REG_IDRN_MASK);
  138. pcie_write(port, (val & (~mask)), XILINX_CPM_PCIE_REG_IDRN_MASK);
  139. raw_spin_unlock_irqrestore(&port->lock, flags);
  140. }
  141. static void xilinx_cpm_unmask_leg_irq(struct irq_data *data)
  142. {
  143. struct xilinx_cpm_pcie *port = irq_data_get_irq_chip_data(data);
  144. unsigned long flags;
  145. u32 mask;
  146. u32 val;
  147. mask = BIT(data->hwirq + XILINX_CPM_PCIE_IDRN_SHIFT);
  148. raw_spin_lock_irqsave(&port->lock, flags);
  149. val = pcie_read(port, XILINX_CPM_PCIE_REG_IDRN_MASK);
  150. pcie_write(port, (val | mask), XILINX_CPM_PCIE_REG_IDRN_MASK);
  151. raw_spin_unlock_irqrestore(&port->lock, flags);
  152. }
  153. static struct irq_chip xilinx_cpm_leg_irq_chip = {
  154. .name = "INTx",
  155. .irq_mask = xilinx_cpm_mask_leg_irq,
  156. .irq_unmask = xilinx_cpm_unmask_leg_irq,
  157. };
  158. /**
  159. * xilinx_cpm_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
  160. * @domain: IRQ domain
  161. * @irq: Virtual IRQ number
  162. * @hwirq: HW interrupt number
  163. *
  164. * Return: Always returns 0.
  165. */
  166. static int xilinx_cpm_pcie_intx_map(struct irq_domain *domain,
  167. unsigned int irq, irq_hw_number_t hwirq)
  168. {
  169. irq_set_chip_and_handler(irq, &xilinx_cpm_leg_irq_chip,
  170. handle_level_irq);
  171. irq_set_chip_data(irq, domain->host_data);
  172. irq_set_status_flags(irq, IRQ_LEVEL);
  173. return 0;
  174. }
  175. /* INTx IRQ Domain operations */
  176. static const struct irq_domain_ops intx_domain_ops = {
  177. .map = xilinx_cpm_pcie_intx_map,
  178. };
  179. static void xilinx_cpm_pcie_intx_flow(struct irq_desc *desc)
  180. {
  181. struct xilinx_cpm_pcie *port = irq_desc_get_handler_data(desc);
  182. struct irq_chip *chip = irq_desc_get_chip(desc);
  183. unsigned long val;
  184. int i;
  185. chained_irq_enter(chip, desc);
  186. val = FIELD_GET(XILINX_CPM_PCIE_IDRN_MASK,
  187. pcie_read(port, XILINX_CPM_PCIE_REG_IDRN));
  188. for_each_set_bit(i, &val, PCI_NUM_INTX)
  189. generic_handle_domain_irq(port->intx_domain, i);
  190. chained_irq_exit(chip, desc);
  191. }
  192. static void xilinx_cpm_mask_event_irq(struct irq_data *d)
  193. {
  194. struct xilinx_cpm_pcie *port = irq_data_get_irq_chip_data(d);
  195. u32 val;
  196. raw_spin_lock(&port->lock);
  197. val = pcie_read(port, XILINX_CPM_PCIE_REG_IMR);
  198. val &= ~BIT(d->hwirq);
  199. pcie_write(port, val, XILINX_CPM_PCIE_REG_IMR);
  200. raw_spin_unlock(&port->lock);
  201. }
  202. static void xilinx_cpm_unmask_event_irq(struct irq_data *d)
  203. {
  204. struct xilinx_cpm_pcie *port = irq_data_get_irq_chip_data(d);
  205. u32 val;
  206. raw_spin_lock(&port->lock);
  207. val = pcie_read(port, XILINX_CPM_PCIE_REG_IMR);
  208. val |= BIT(d->hwirq);
  209. pcie_write(port, val, XILINX_CPM_PCIE_REG_IMR);
  210. raw_spin_unlock(&port->lock);
  211. }
  212. static struct irq_chip xilinx_cpm_event_irq_chip = {
  213. .name = "RC-Event",
  214. .irq_mask = xilinx_cpm_mask_event_irq,
  215. .irq_unmask = xilinx_cpm_unmask_event_irq,
  216. };
  217. static int xilinx_cpm_pcie_event_map(struct irq_domain *domain,
  218. unsigned int irq, irq_hw_number_t hwirq)
  219. {
  220. irq_set_chip_and_handler(irq, &xilinx_cpm_event_irq_chip,
  221. handle_level_irq);
  222. irq_set_chip_data(irq, domain->host_data);
  223. irq_set_status_flags(irq, IRQ_LEVEL);
  224. return 0;
  225. }
  226. static const struct irq_domain_ops event_domain_ops = {
  227. .map = xilinx_cpm_pcie_event_map,
  228. };
  229. static void xilinx_cpm_pcie_event_flow(struct irq_desc *desc)
  230. {
  231. struct xilinx_cpm_pcie *port = irq_desc_get_handler_data(desc);
  232. struct irq_chip *chip = irq_desc_get_chip(desc);
  233. unsigned long val;
  234. int i;
  235. chained_irq_enter(chip, desc);
  236. val = pcie_read(port, XILINX_CPM_PCIE_REG_IDR);
  237. val &= pcie_read(port, XILINX_CPM_PCIE_REG_IMR);
  238. for_each_set_bit(i, &val, 32)
  239. generic_handle_domain_irq(port->cpm_domain, i);
  240. pcie_write(port, val, XILINX_CPM_PCIE_REG_IDR);
  241. if (port->variant->version == CPM5) {
  242. val = readl_relaxed(port->cpm_base + XILINX_CPM_PCIE_IR_STATUS);
  243. if (val)
  244. writel_relaxed(val, port->cpm_base +
  245. XILINX_CPM_PCIE_IR_STATUS);
  246. }
  247. /*
  248. * XILINX_CPM_PCIE_MISC_IR_STATUS register is mapped to
  249. * CPM SLCR block.
  250. */
  251. val = readl_relaxed(port->cpm_base + XILINX_CPM_PCIE_MISC_IR_STATUS);
  252. if (val)
  253. writel_relaxed(val,
  254. port->cpm_base + XILINX_CPM_PCIE_MISC_IR_STATUS);
  255. chained_irq_exit(chip, desc);
  256. }
  257. #define _IC(x, s) \
  258. [XILINX_PCIE_INTR_ ## x] = { __stringify(x), s }
  259. static const struct {
  260. const char *sym;
  261. const char *str;
  262. } intr_cause[32] = {
  263. _IC(LINK_DOWN, "Link Down"),
  264. _IC(HOT_RESET, "Hot reset"),
  265. _IC(CFG_TIMEOUT, "ECAM access timeout"),
  266. _IC(CORRECTABLE, "Correctable error message"),
  267. _IC(NONFATAL, "Non fatal error message"),
  268. _IC(FATAL, "Fatal error message"),
  269. _IC(SLV_UNSUPP, "Slave unsupported request"),
  270. _IC(SLV_UNEXP, "Slave unexpected completion"),
  271. _IC(SLV_COMPL, "Slave completion timeout"),
  272. _IC(SLV_ERRP, "Slave Error Poison"),
  273. _IC(SLV_CMPABT, "Slave Completer Abort"),
  274. _IC(SLV_ILLBUR, "Slave Illegal Burst"),
  275. _IC(MST_DECERR, "Master decode error"),
  276. _IC(MST_SLVERR, "Master slave error"),
  277. _IC(CFG_PCIE_TIMEOUT, "PCIe ECAM access timeout"),
  278. _IC(CFG_ERR_POISON, "ECAM poisoned completion received"),
  279. _IC(PME_TO_ACK_RCVD, "PME_TO_ACK message received"),
  280. _IC(PM_PME_RCVD, "PM_PME message received"),
  281. _IC(SLV_PCIE_TIMEOUT, "PCIe completion timeout received"),
  282. };
  283. static irqreturn_t xilinx_cpm_pcie_intr_handler(int irq, void *dev_id)
  284. {
  285. struct xilinx_cpm_pcie *port = dev_id;
  286. struct device *dev = port->dev;
  287. struct irq_data *d;
  288. d = irq_domain_get_irq_data(port->cpm_domain, irq);
  289. switch (d->hwirq) {
  290. case XILINX_PCIE_INTR_CORRECTABLE:
  291. case XILINX_PCIE_INTR_NONFATAL:
  292. case XILINX_PCIE_INTR_FATAL:
  293. cpm_pcie_clear_err_interrupts(port);
  294. fallthrough;
  295. default:
  296. if (intr_cause[d->hwirq].str)
  297. dev_warn(dev, "%s\n", intr_cause[d->hwirq].str);
  298. else
  299. dev_warn(dev, "Unknown IRQ %ld\n", d->hwirq);
  300. }
  301. return IRQ_HANDLED;
  302. }
  303. static void xilinx_cpm_free_irq_domains(struct xilinx_cpm_pcie *port)
  304. {
  305. if (port->intx_domain) {
  306. irq_domain_remove(port->intx_domain);
  307. port->intx_domain = NULL;
  308. }
  309. if (port->cpm_domain) {
  310. irq_domain_remove(port->cpm_domain);
  311. port->cpm_domain = NULL;
  312. }
  313. }
  314. /**
  315. * xilinx_cpm_pcie_init_irq_domain - Initialize IRQ domain
  316. * @port: PCIe port information
  317. *
  318. * Return: '0' on success and error value on failure
  319. */
  320. static int xilinx_cpm_pcie_init_irq_domain(struct xilinx_cpm_pcie *port)
  321. {
  322. struct device *dev = port->dev;
  323. struct device_node *node = dev->of_node;
  324. struct device_node *pcie_intc_node;
  325. /* Setup INTx */
  326. pcie_intc_node = of_get_next_child(node, NULL);
  327. if (!pcie_intc_node) {
  328. dev_err(dev, "No PCIe Intc node found\n");
  329. return -EINVAL;
  330. }
  331. port->cpm_domain = irq_domain_add_linear(pcie_intc_node, 32,
  332. &event_domain_ops,
  333. port);
  334. if (!port->cpm_domain)
  335. goto out;
  336. irq_domain_update_bus_token(port->cpm_domain, DOMAIN_BUS_NEXUS);
  337. port->intx_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
  338. &intx_domain_ops,
  339. port);
  340. if (!port->intx_domain)
  341. goto out;
  342. irq_domain_update_bus_token(port->intx_domain, DOMAIN_BUS_WIRED);
  343. of_node_put(pcie_intc_node);
  344. raw_spin_lock_init(&port->lock);
  345. return 0;
  346. out:
  347. xilinx_cpm_free_irq_domains(port);
  348. of_node_put(pcie_intc_node);
  349. dev_err(dev, "Failed to allocate IRQ domains\n");
  350. return -ENOMEM;
  351. }
  352. static int xilinx_cpm_setup_irq(struct xilinx_cpm_pcie *port)
  353. {
  354. struct device *dev = port->dev;
  355. struct platform_device *pdev = to_platform_device(dev);
  356. int i, irq;
  357. port->irq = platform_get_irq(pdev, 0);
  358. if (port->irq < 0)
  359. return port->irq;
  360. for (i = 0; i < ARRAY_SIZE(intr_cause); i++) {
  361. int err;
  362. if (!intr_cause[i].str)
  363. continue;
  364. irq = irq_create_mapping(port->cpm_domain, i);
  365. if (!irq) {
  366. dev_err(dev, "Failed to map interrupt\n");
  367. return -ENXIO;
  368. }
  369. err = devm_request_irq(dev, irq, xilinx_cpm_pcie_intr_handler,
  370. 0, intr_cause[i].sym, port);
  371. if (err) {
  372. dev_err(dev, "Failed to request IRQ %d\n", irq);
  373. return err;
  374. }
  375. }
  376. port->intx_irq = irq_create_mapping(port->cpm_domain,
  377. XILINX_PCIE_INTR_INTX);
  378. if (!port->intx_irq) {
  379. dev_err(dev, "Failed to map INTx interrupt\n");
  380. return -ENXIO;
  381. }
  382. /* Plug the INTx chained handler */
  383. irq_set_chained_handler_and_data(port->intx_irq,
  384. xilinx_cpm_pcie_intx_flow, port);
  385. /* Plug the main event chained handler */
  386. irq_set_chained_handler_and_data(port->irq,
  387. xilinx_cpm_pcie_event_flow, port);
  388. return 0;
  389. }
  390. /**
  391. * xilinx_cpm_pcie_init_port - Initialize hardware
  392. * @port: PCIe port information
  393. */
  394. static void xilinx_cpm_pcie_init_port(struct xilinx_cpm_pcie *port)
  395. {
  396. if (cpm_pcie_link_up(port))
  397. dev_info(port->dev, "PCIe Link is UP\n");
  398. else
  399. dev_info(port->dev, "PCIe Link is DOWN\n");
  400. /* Disable all interrupts */
  401. pcie_write(port, ~XILINX_CPM_PCIE_IDR_ALL_MASK,
  402. XILINX_CPM_PCIE_REG_IMR);
  403. /* Clear pending interrupts */
  404. pcie_write(port, pcie_read(port, XILINX_CPM_PCIE_REG_IDR) &
  405. XILINX_CPM_PCIE_IMR_ALL_MASK,
  406. XILINX_CPM_PCIE_REG_IDR);
  407. /*
  408. * XILINX_CPM_PCIE_MISC_IR_ENABLE register is mapped to
  409. * CPM SLCR block.
  410. */
  411. writel(XILINX_CPM_PCIE_MISC_IR_LOCAL,
  412. port->cpm_base + XILINX_CPM_PCIE_MISC_IR_ENABLE);
  413. if (port->variant->version == CPM5) {
  414. writel(XILINX_CPM_PCIE_IR_LOCAL,
  415. port->cpm_base + XILINX_CPM_PCIE_IR_ENABLE);
  416. }
  417. /* Enable the Bridge enable bit */
  418. pcie_write(port, pcie_read(port, XILINX_CPM_PCIE_REG_RPSC) |
  419. XILINX_CPM_PCIE_REG_RPSC_BEN,
  420. XILINX_CPM_PCIE_REG_RPSC);
  421. }
  422. /**
  423. * xilinx_cpm_pcie_parse_dt - Parse Device tree
  424. * @port: PCIe port information
  425. * @bus_range: Bus resource
  426. *
  427. * Return: '0' on success and error value on failure
  428. */
  429. static int xilinx_cpm_pcie_parse_dt(struct xilinx_cpm_pcie *port,
  430. struct resource *bus_range)
  431. {
  432. struct device *dev = port->dev;
  433. struct platform_device *pdev = to_platform_device(dev);
  434. struct resource *res;
  435. port->cpm_base = devm_platform_ioremap_resource_byname(pdev,
  436. "cpm_slcr");
  437. if (IS_ERR(port->cpm_base))
  438. return PTR_ERR(port->cpm_base);
  439. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
  440. if (!res)
  441. return -ENXIO;
  442. port->cfg = pci_ecam_create(dev, res, bus_range,
  443. &pci_generic_ecam_ops);
  444. if (IS_ERR(port->cfg))
  445. return PTR_ERR(port->cfg);
  446. if (port->variant->version == CPM5) {
  447. port->reg_base = devm_platform_ioremap_resource_byname(pdev,
  448. "cpm_csr");
  449. if (IS_ERR(port->reg_base))
  450. return PTR_ERR(port->reg_base);
  451. } else {
  452. port->reg_base = port->cfg->win;
  453. }
  454. return 0;
  455. }
  456. static void xilinx_cpm_free_interrupts(struct xilinx_cpm_pcie *port)
  457. {
  458. irq_set_chained_handler_and_data(port->intx_irq, NULL, NULL);
  459. irq_set_chained_handler_and_data(port->irq, NULL, NULL);
  460. }
  461. /**
  462. * xilinx_cpm_pcie_probe - Probe function
  463. * @pdev: Platform device pointer
  464. *
  465. * Return: '0' on success and error value on failure
  466. */
  467. static int xilinx_cpm_pcie_probe(struct platform_device *pdev)
  468. {
  469. struct xilinx_cpm_pcie *port;
  470. struct device *dev = &pdev->dev;
  471. struct pci_host_bridge *bridge;
  472. struct resource_entry *bus;
  473. int err;
  474. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*port));
  475. if (!bridge)
  476. return -ENODEV;
  477. port = pci_host_bridge_priv(bridge);
  478. port->dev = dev;
  479. err = xilinx_cpm_pcie_init_irq_domain(port);
  480. if (err)
  481. return err;
  482. bus = resource_list_first_type(&bridge->windows, IORESOURCE_BUS);
  483. if (!bus)
  484. return -ENODEV;
  485. port->variant = of_device_get_match_data(dev);
  486. err = xilinx_cpm_pcie_parse_dt(port, bus->res);
  487. if (err) {
  488. dev_err(dev, "Parsing DT failed\n");
  489. goto err_parse_dt;
  490. }
  491. xilinx_cpm_pcie_init_port(port);
  492. err = xilinx_cpm_setup_irq(port);
  493. if (err) {
  494. dev_err(dev, "Failed to set up interrupts\n");
  495. goto err_setup_irq;
  496. }
  497. bridge->sysdata = port->cfg;
  498. bridge->ops = (struct pci_ops *)&pci_generic_ecam_ops.pci_ops;
  499. err = pci_host_probe(bridge);
  500. if (err < 0)
  501. goto err_host_bridge;
  502. return 0;
  503. err_host_bridge:
  504. xilinx_cpm_free_interrupts(port);
  505. err_setup_irq:
  506. pci_ecam_free(port->cfg);
  507. err_parse_dt:
  508. xilinx_cpm_free_irq_domains(port);
  509. return err;
  510. }
  511. static const struct xilinx_cpm_variant cpm_host = {
  512. .version = CPM,
  513. };
  514. static const struct xilinx_cpm_variant cpm5_host = {
  515. .version = CPM5,
  516. };
  517. static const struct of_device_id xilinx_cpm_pcie_of_match[] = {
  518. {
  519. .compatible = "xlnx,versal-cpm-host-1.00",
  520. .data = &cpm_host,
  521. },
  522. {
  523. .compatible = "xlnx,versal-cpm5-host",
  524. .data = &cpm5_host,
  525. },
  526. {}
  527. };
  528. static struct platform_driver xilinx_cpm_pcie_driver = {
  529. .driver = {
  530. .name = "xilinx-cpm-pcie",
  531. .of_match_table = xilinx_cpm_pcie_of_match,
  532. .suppress_bind_attrs = true,
  533. },
  534. .probe = xilinx_cpm_pcie_probe,
  535. };
  536. builtin_platform_driver(xilinx_cpm_pcie_driver);