pcie-xilinx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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/module.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. * @irq_domain: IRQ domain pointer
  93. * @resources: Bus Resources
  94. */
  95. struct xilinx_pcie_port {
  96. void __iomem *reg_base;
  97. u32 irq;
  98. unsigned long msi_pages;
  99. u8 root_busno;
  100. struct device *dev;
  101. struct irq_domain *irq_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_is_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. unsigned long val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
  125. if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
  126. dev_dbg(port->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_is_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. /*
  150. * Do not read more than one device on the bus directly attached
  151. * to RC.
  152. */
  153. if (bus->primary == port->root_busno && devfn > 0)
  154. return false;
  155. return true;
  156. }
  157. /**
  158. * xilinx_pcie_map_bus - Get configuration base
  159. * @bus: PCI Bus structure
  160. * @devfn: Device/function
  161. * @where: Offset from base
  162. *
  163. * Return: Base address of the configuration space needed to be
  164. * accessed.
  165. */
  166. static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
  167. unsigned int devfn, int where)
  168. {
  169. struct xilinx_pcie_port *port = bus->sysdata;
  170. int relbus;
  171. if (!xilinx_pcie_valid_device(bus, devfn))
  172. return NULL;
  173. relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
  174. (devfn << ECAM_DEV_NUM_SHIFT);
  175. return port->reg_base + relbus + where;
  176. }
  177. /* PCIe operations */
  178. static struct pci_ops xilinx_pcie_ops = {
  179. .map_bus = xilinx_pcie_map_bus,
  180. .read = pci_generic_config_read,
  181. .write = pci_generic_config_write,
  182. };
  183. /* MSI functions */
  184. /**
  185. * xilinx_pcie_destroy_msi - Free MSI number
  186. * @irq: IRQ to be freed
  187. */
  188. static void xilinx_pcie_destroy_msi(unsigned int irq)
  189. {
  190. struct msi_desc *msi;
  191. struct xilinx_pcie_port *port;
  192. if (!test_bit(irq, 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(irq, msi_irq_in_use);
  198. }
  199. }
  200. /**
  201. * xilinx_pcie_assign_msi - Allocate MSI number
  202. * @port: PCIe port structure
  203. *
  204. * Return: A valid IRQ on success and error value on failure.
  205. */
  206. static int xilinx_pcie_assign_msi(struct xilinx_pcie_port *port)
  207. {
  208. int pos;
  209. pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
  210. if (pos < XILINX_NUM_MSI_IRQS)
  211. set_bit(pos, msi_irq_in_use);
  212. else
  213. return -ENOSPC;
  214. return pos;
  215. }
  216. /**
  217. * xilinx_msi_teardown_irq - Destroy the MSI
  218. * @chip: MSI Chip descriptor
  219. * @irq: MSI IRQ to destroy
  220. */
  221. static void xilinx_msi_teardown_irq(struct msi_controller *chip,
  222. unsigned int irq)
  223. {
  224. xilinx_pcie_destroy_msi(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(port);
  244. if (hwirq < 0)
  245. return hwirq;
  246. irq = irq_create_mapping(port->irq_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. };
  321. /* PCIe HW Functions */
  322. /**
  323. * xilinx_pcie_intr_handler - Interrupt Service Handler
  324. * @irq: IRQ number
  325. * @data: PCIe port information
  326. *
  327. * Return: IRQ_HANDLED on success and IRQ_NONE on failure
  328. */
  329. static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
  330. {
  331. struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
  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(port->dev, "Link Down\n");
  341. if (status & XILINX_PCIE_INTR_ECRC_ERR)
  342. dev_warn(port->dev, "ECRC failed\n");
  343. if (status & XILINX_PCIE_INTR_STR_ERR)
  344. dev_warn(port->dev, "Streaming error\n");
  345. if (status & XILINX_PCIE_INTR_HOT_RESET)
  346. dev_info(port->dev, "Hot reset\n");
  347. if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
  348. dev_warn(port->dev, "ECAM access timeout\n");
  349. if (status & XILINX_PCIE_INTR_CORRECTABLE) {
  350. dev_warn(port->dev, "Correctable error message\n");
  351. xilinx_pcie_clear_err_interrupts(port);
  352. }
  353. if (status & XILINX_PCIE_INTR_NONFATAL) {
  354. dev_warn(port->dev, "Non fatal error message\n");
  355. xilinx_pcie_clear_err_interrupts(port);
  356. }
  357. if (status & XILINX_PCIE_INTR_FATAL) {
  358. dev_warn(port->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(port->dev, "RP Intr FIFO1 read error\n");
  367. return IRQ_HANDLED;
  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->irq_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(port->dev, "RP Intr FIFO1 read error\n");
  385. return IRQ_HANDLED;
  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(port->dev, "Slave unsupported request\n");
  401. if (status & XILINX_PCIE_INTR_SLV_UNEXP)
  402. dev_warn(port->dev, "Slave unexpected completion\n");
  403. if (status & XILINX_PCIE_INTR_SLV_COMPL)
  404. dev_warn(port->dev, "Slave completion timeout\n");
  405. if (status & XILINX_PCIE_INTR_SLV_ERRP)
  406. dev_warn(port->dev, "Slave Error Poison\n");
  407. if (status & XILINX_PCIE_INTR_SLV_CMPABT)
  408. dev_warn(port->dev, "Slave Completer Abort\n");
  409. if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
  410. dev_warn(port->dev, "Slave Illegal Burst\n");
  411. if (status & XILINX_PCIE_INTR_MST_DECERR)
  412. dev_warn(port->dev, "Master decode error\n");
  413. if (status & XILINX_PCIE_INTR_MST_SLVERR)
  414. dev_warn(port->dev, "Master slave error\n");
  415. if (status & XILINX_PCIE_INTR_MST_ERRP)
  416. dev_warn(port->dev, "Master error poison\n");
  417. /* Clear the Interrupt Decode register */
  418. pcie_write(port, status, XILINX_PCIE_REG_IDR);
  419. return IRQ_HANDLED;
  420. }
  421. /**
  422. * xilinx_pcie_free_irq_domain - Free IRQ domain
  423. * @port: PCIe port information
  424. */
  425. static void xilinx_pcie_free_irq_domain(struct xilinx_pcie_port *port)
  426. {
  427. int i;
  428. u32 irq, num_irqs;
  429. /* Free IRQ Domain */
  430. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  431. free_pages(port->msi_pages, 0);
  432. num_irqs = XILINX_NUM_MSI_IRQS;
  433. } else {
  434. /* INTx */
  435. num_irqs = 4;
  436. }
  437. for (i = 0; i < num_irqs; i++) {
  438. irq = irq_find_mapping(port->irq_domain, i);
  439. if (irq > 0)
  440. irq_dispose_mapping(irq);
  441. }
  442. irq_domain_remove(port->irq_domain);
  443. }
  444. /**
  445. * xilinx_pcie_init_irq_domain - Initialize IRQ domain
  446. * @port: PCIe port information
  447. *
  448. * Return: '0' on success and error value on failure
  449. */
  450. static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
  451. {
  452. struct device *dev = port->dev;
  453. struct device_node *node = dev->of_node;
  454. struct device_node *pcie_intc_node;
  455. /* Setup INTx */
  456. pcie_intc_node = of_get_next_child(node, NULL);
  457. if (!pcie_intc_node) {
  458. dev_err(dev, "No PCIe Intc node found\n");
  459. return PTR_ERR(pcie_intc_node);
  460. }
  461. port->irq_domain = irq_domain_add_linear(pcie_intc_node, 4,
  462. &intx_domain_ops,
  463. port);
  464. if (!port->irq_domain) {
  465. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  466. return PTR_ERR(port->irq_domain);
  467. }
  468. /* Setup MSI */
  469. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  470. port->irq_domain = irq_domain_add_linear(node,
  471. XILINX_NUM_MSI_IRQS,
  472. &msi_domain_ops,
  473. &xilinx_pcie_msi_chip);
  474. if (!port->irq_domain) {
  475. dev_err(dev, "Failed to get a MSI IRQ domain\n");
  476. return PTR_ERR(port->irq_domain);
  477. }
  478. xilinx_pcie_enable_msi(port);
  479. }
  480. return 0;
  481. }
  482. /**
  483. * xilinx_pcie_init_port - Initialize hardware
  484. * @port: PCIe port information
  485. */
  486. static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
  487. {
  488. if (xilinx_pcie_link_is_up(port))
  489. dev_info(port->dev, "PCIe Link is UP\n");
  490. else
  491. dev_info(port->dev, "PCIe Link is DOWN\n");
  492. /* Disable all interrupts */
  493. pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
  494. XILINX_PCIE_REG_IMR);
  495. /* Clear pending interrupts */
  496. pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
  497. XILINX_PCIE_IMR_ALL_MASK,
  498. XILINX_PCIE_REG_IDR);
  499. /* Enable all interrupts */
  500. pcie_write(port, XILINX_PCIE_IMR_ALL_MASK, XILINX_PCIE_REG_IMR);
  501. /* Enable the Bridge enable bit */
  502. pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
  503. XILINX_PCIE_REG_RPSC_BEN,
  504. XILINX_PCIE_REG_RPSC);
  505. }
  506. /**
  507. * xilinx_pcie_parse_dt - Parse Device tree
  508. * @port: PCIe port information
  509. *
  510. * Return: '0' on success and error value on failure
  511. */
  512. static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
  513. {
  514. struct device *dev = port->dev;
  515. struct device_node *node = dev->of_node;
  516. struct resource regs;
  517. const char *type;
  518. int err;
  519. type = of_get_property(node, "device_type", NULL);
  520. if (!type || strcmp(type, "pci")) {
  521. dev_err(dev, "invalid \"device_type\" %s\n", type);
  522. return -EINVAL;
  523. }
  524. err = of_address_to_resource(node, 0, &regs);
  525. if (err) {
  526. dev_err(dev, "missing \"reg\" property\n");
  527. return err;
  528. }
  529. port->reg_base = devm_ioremap_resource(dev, &regs);
  530. if (IS_ERR(port->reg_base))
  531. return PTR_ERR(port->reg_base);
  532. port->irq = irq_of_parse_and_map(node, 0);
  533. err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
  534. IRQF_SHARED | IRQF_NO_THREAD,
  535. "xilinx-pcie", port);
  536. if (err) {
  537. dev_err(dev, "unable to request irq %d\n", port->irq);
  538. return err;
  539. }
  540. return 0;
  541. }
  542. /**
  543. * xilinx_pcie_probe - Probe function
  544. * @pdev: Platform device pointer
  545. *
  546. * Return: '0' on success and error value on failure
  547. */
  548. static int xilinx_pcie_probe(struct platform_device *pdev)
  549. {
  550. struct xilinx_pcie_port *port;
  551. struct device *dev = &pdev->dev;
  552. struct pci_bus *bus;
  553. int err;
  554. resource_size_t iobase = 0;
  555. LIST_HEAD(res);
  556. if (!dev->of_node)
  557. return -ENODEV;
  558. port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  559. if (!port)
  560. return -ENOMEM;
  561. port->dev = dev;
  562. err = xilinx_pcie_parse_dt(port);
  563. if (err) {
  564. dev_err(dev, "Parsing DT failed\n");
  565. return err;
  566. }
  567. xilinx_pcie_init_port(port);
  568. err = xilinx_pcie_init_irq_domain(port);
  569. if (err) {
  570. dev_err(dev, "Failed creating IRQ Domain\n");
  571. return err;
  572. }
  573. err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff, &res,
  574. &iobase);
  575. if (err) {
  576. dev_err(dev, "Getting bridge resources failed\n");
  577. return err;
  578. }
  579. bus = pci_create_root_bus(&pdev->dev, 0,
  580. &xilinx_pcie_ops, port, &res);
  581. if (!bus)
  582. return -ENOMEM;
  583. #ifdef CONFIG_PCI_MSI
  584. xilinx_pcie_msi_chip.dev = port->dev;
  585. bus->msi = &xilinx_pcie_msi_chip;
  586. #endif
  587. pci_scan_child_bus(bus);
  588. pci_assign_unassigned_bus_resources(bus);
  589. #ifndef CONFIG_MICROBLAZE
  590. pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
  591. #endif
  592. pci_bus_add_devices(bus);
  593. platform_set_drvdata(pdev, port);
  594. return 0;
  595. }
  596. /**
  597. * xilinx_pcie_remove - Remove function
  598. * @pdev: Platform device pointer
  599. *
  600. * Return: '0' always
  601. */
  602. static int xilinx_pcie_remove(struct platform_device *pdev)
  603. {
  604. struct xilinx_pcie_port *port = platform_get_drvdata(pdev);
  605. xilinx_pcie_free_irq_domain(port);
  606. return 0;
  607. }
  608. static struct of_device_id xilinx_pcie_of_match[] = {
  609. { .compatible = "xlnx,axi-pcie-host-1.00.a", },
  610. {}
  611. };
  612. static struct platform_driver xilinx_pcie_driver = {
  613. .driver = {
  614. .name = "xilinx-pcie",
  615. .of_match_table = xilinx_pcie_of_match,
  616. .suppress_bind_attrs = true,
  617. },
  618. .probe = xilinx_pcie_probe,
  619. .remove = xilinx_pcie_remove,
  620. };
  621. module_platform_driver(xilinx_pcie_driver);
  622. MODULE_AUTHOR("Xilinx Inc");
  623. MODULE_DESCRIPTION("Xilinx AXI PCIe driver");
  624. MODULE_LICENSE("GPL v2");