pcie-xilinx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. /* Number of Memory Resources */
  86. #define XILINX_MAX_NUM_RESOURCES 3
  87. /**
  88. * struct xilinx_pcie_port - PCIe port information
  89. * @reg_base: IO Mapped Register Base
  90. * @irq: Interrupt number
  91. * @msi_pages: MSI pages
  92. * @root_busno: Root Bus number
  93. * @dev: Device pointer
  94. * @irq_domain: IRQ domain pointer
  95. * @bus_range: Bus range
  96. * @resources: Bus Resources
  97. */
  98. struct xilinx_pcie_port {
  99. void __iomem *reg_base;
  100. u32 irq;
  101. unsigned long msi_pages;
  102. u8 root_busno;
  103. struct device *dev;
  104. struct irq_domain *irq_domain;
  105. struct resource bus_range;
  106. struct list_head resources;
  107. };
  108. static DECLARE_BITMAP(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
  109. static inline struct xilinx_pcie_port *sys_to_pcie(struct pci_sys_data *sys)
  110. {
  111. return sys->private_data;
  112. }
  113. static inline u32 pcie_read(struct xilinx_pcie_port *port, u32 reg)
  114. {
  115. return readl(port->reg_base + reg);
  116. }
  117. static inline void pcie_write(struct xilinx_pcie_port *port, u32 val, u32 reg)
  118. {
  119. writel(val, port->reg_base + reg);
  120. }
  121. static inline bool xilinx_pcie_link_is_up(struct xilinx_pcie_port *port)
  122. {
  123. return (pcie_read(port, XILINX_PCIE_REG_PSCR) &
  124. XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
  125. }
  126. /**
  127. * xilinx_pcie_clear_err_interrupts - Clear Error Interrupts
  128. * @port: PCIe port information
  129. */
  130. static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port *port)
  131. {
  132. unsigned long val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
  133. if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
  134. dev_dbg(port->dev, "Requester ID %lu\n",
  135. val & XILINX_PCIE_RPEFR_REQ_ID);
  136. pcie_write(port, XILINX_PCIE_RPEFR_ALL_MASK,
  137. XILINX_PCIE_REG_RPEFR);
  138. }
  139. }
  140. /**
  141. * xilinx_pcie_valid_device - Check if a valid device is present on bus
  142. * @bus: PCI Bus structure
  143. * @devfn: device/function
  144. *
  145. * Return: 'true' on success and 'false' if invalid device is found
  146. */
  147. static bool xilinx_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
  148. {
  149. struct xilinx_pcie_port *port = sys_to_pcie(bus->sysdata);
  150. /* Check if link is up when trying to access downstream ports */
  151. if (bus->number != port->root_busno)
  152. if (!xilinx_pcie_link_is_up(port))
  153. return false;
  154. /* Only one device down on each root port */
  155. if (bus->number == port->root_busno && devfn > 0)
  156. return false;
  157. /*
  158. * Do not read more than one device on the bus directly attached
  159. * to RC.
  160. */
  161. if (bus->primary == port->root_busno && devfn > 0)
  162. return false;
  163. return true;
  164. }
  165. /**
  166. * xilinx_pcie_map_bus - Get configuration base
  167. * @bus: PCI Bus structure
  168. * @devfn: Device/function
  169. * @where: Offset from base
  170. *
  171. * Return: Base address of the configuration space needed to be
  172. * accessed.
  173. */
  174. static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
  175. unsigned int devfn, int where)
  176. {
  177. struct xilinx_pcie_port *port = sys_to_pcie(bus->sysdata);
  178. int relbus;
  179. if (!xilinx_pcie_valid_device(bus, devfn))
  180. return NULL;
  181. relbus = (bus->number << ECAM_BUS_NUM_SHIFT) |
  182. (devfn << ECAM_DEV_NUM_SHIFT);
  183. return port->reg_base + relbus + where;
  184. }
  185. /* PCIe operations */
  186. static struct pci_ops xilinx_pcie_ops = {
  187. .map_bus = xilinx_pcie_map_bus,
  188. .read = pci_generic_config_read,
  189. .write = pci_generic_config_write,
  190. };
  191. /* MSI functions */
  192. /**
  193. * xilinx_pcie_destroy_msi - Free MSI number
  194. * @irq: IRQ to be freed
  195. */
  196. static void xilinx_pcie_destroy_msi(unsigned int irq)
  197. {
  198. struct msi_desc *msi;
  199. struct xilinx_pcie_port *port;
  200. if (!test_bit(irq, msi_irq_in_use)) {
  201. msi = irq_get_msi_desc(irq);
  202. port = sys_to_pcie(msi_desc_to_pci_sysdata(msi));
  203. dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
  204. } else {
  205. clear_bit(irq, msi_irq_in_use);
  206. }
  207. }
  208. /**
  209. * xilinx_pcie_assign_msi - Allocate MSI number
  210. * @port: PCIe port structure
  211. *
  212. * Return: A valid IRQ on success and error value on failure.
  213. */
  214. static int xilinx_pcie_assign_msi(struct xilinx_pcie_port *port)
  215. {
  216. int pos;
  217. pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
  218. if (pos < XILINX_NUM_MSI_IRQS)
  219. set_bit(pos, msi_irq_in_use);
  220. else
  221. return -ENOSPC;
  222. return pos;
  223. }
  224. /**
  225. * xilinx_msi_teardown_irq - Destroy the MSI
  226. * @chip: MSI Chip descriptor
  227. * @irq: MSI IRQ to destroy
  228. */
  229. static void xilinx_msi_teardown_irq(struct msi_controller *chip,
  230. unsigned int irq)
  231. {
  232. xilinx_pcie_destroy_msi(irq);
  233. }
  234. /**
  235. * xilinx_pcie_msi_setup_irq - Setup MSI request
  236. * @chip: MSI chip pointer
  237. * @pdev: PCIe device pointer
  238. * @desc: MSI descriptor pointer
  239. *
  240. * Return: '0' on success and error value on failure
  241. */
  242. static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
  243. struct pci_dev *pdev,
  244. struct msi_desc *desc)
  245. {
  246. struct xilinx_pcie_port *port = sys_to_pcie(pdev->bus->sysdata);
  247. unsigned int irq;
  248. int hwirq;
  249. struct msi_msg msg;
  250. phys_addr_t msg_addr;
  251. hwirq = xilinx_pcie_assign_msi(port);
  252. if (hwirq < 0)
  253. return hwirq;
  254. irq = irq_create_mapping(port->irq_domain, hwirq);
  255. if (!irq)
  256. return -EINVAL;
  257. irq_set_msi_desc(irq, desc);
  258. msg_addr = virt_to_phys((void *)port->msi_pages);
  259. msg.address_hi = 0;
  260. msg.address_lo = msg_addr;
  261. msg.data = irq;
  262. pci_write_msi_msg(irq, &msg);
  263. return 0;
  264. }
  265. /* MSI Chip Descriptor */
  266. static struct msi_controller xilinx_pcie_msi_chip = {
  267. .setup_irq = xilinx_pcie_msi_setup_irq,
  268. .teardown_irq = xilinx_msi_teardown_irq,
  269. };
  270. /* HW Interrupt Chip Descriptor */
  271. static struct irq_chip xilinx_msi_irq_chip = {
  272. .name = "Xilinx PCIe MSI",
  273. .irq_enable = pci_msi_unmask_irq,
  274. .irq_disable = pci_msi_mask_irq,
  275. .irq_mask = pci_msi_mask_irq,
  276. .irq_unmask = pci_msi_unmask_irq,
  277. };
  278. /**
  279. * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
  280. * @domain: IRQ domain
  281. * @irq: Virtual IRQ number
  282. * @hwirq: HW interrupt number
  283. *
  284. * Return: Always returns 0.
  285. */
  286. static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
  287. irq_hw_number_t hwirq)
  288. {
  289. irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
  290. irq_set_chip_data(irq, domain->host_data);
  291. return 0;
  292. }
  293. /* IRQ Domain operations */
  294. static const struct irq_domain_ops msi_domain_ops = {
  295. .map = xilinx_pcie_msi_map,
  296. };
  297. /**
  298. * xilinx_pcie_enable_msi - Enable MSI support
  299. * @port: PCIe port information
  300. */
  301. static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
  302. {
  303. phys_addr_t msg_addr;
  304. port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
  305. msg_addr = virt_to_phys((void *)port->msi_pages);
  306. pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
  307. pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
  308. }
  309. /* INTx Functions */
  310. /**
  311. * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
  312. * @domain: IRQ domain
  313. * @irq: Virtual IRQ number
  314. * @hwirq: HW interrupt number
  315. *
  316. * Return: Always returns 0.
  317. */
  318. static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  319. irq_hw_number_t hwirq)
  320. {
  321. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  322. irq_set_chip_data(irq, domain->host_data);
  323. return 0;
  324. }
  325. /* INTx IRQ Domain operations */
  326. static const struct irq_domain_ops intx_domain_ops = {
  327. .map = xilinx_pcie_intx_map,
  328. };
  329. /* PCIe HW Functions */
  330. /**
  331. * xilinx_pcie_intr_handler - Interrupt Service Handler
  332. * @irq: IRQ number
  333. * @data: PCIe port information
  334. *
  335. * Return: IRQ_HANDLED on success and IRQ_NONE on failure
  336. */
  337. static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
  338. {
  339. struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
  340. u32 val, mask, status, msi_data;
  341. /* Read interrupt decode and mask registers */
  342. val = pcie_read(port, XILINX_PCIE_REG_IDR);
  343. mask = pcie_read(port, XILINX_PCIE_REG_IMR);
  344. status = val & mask;
  345. if (!status)
  346. return IRQ_NONE;
  347. if (status & XILINX_PCIE_INTR_LINK_DOWN)
  348. dev_warn(port->dev, "Link Down\n");
  349. if (status & XILINX_PCIE_INTR_ECRC_ERR)
  350. dev_warn(port->dev, "ECRC failed\n");
  351. if (status & XILINX_PCIE_INTR_STR_ERR)
  352. dev_warn(port->dev, "Streaming error\n");
  353. if (status & XILINX_PCIE_INTR_HOT_RESET)
  354. dev_info(port->dev, "Hot reset\n");
  355. if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
  356. dev_warn(port->dev, "ECAM access timeout\n");
  357. if (status & XILINX_PCIE_INTR_CORRECTABLE) {
  358. dev_warn(port->dev, "Correctable error message\n");
  359. xilinx_pcie_clear_err_interrupts(port);
  360. }
  361. if (status & XILINX_PCIE_INTR_NONFATAL) {
  362. dev_warn(port->dev, "Non fatal error message\n");
  363. xilinx_pcie_clear_err_interrupts(port);
  364. }
  365. if (status & XILINX_PCIE_INTR_FATAL) {
  366. dev_warn(port->dev, "Fatal error message\n");
  367. xilinx_pcie_clear_err_interrupts(port);
  368. }
  369. if (status & XILINX_PCIE_INTR_INTX) {
  370. /* INTx interrupt received */
  371. val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
  372. /* Check whether interrupt valid */
  373. if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
  374. dev_warn(port->dev, "RP Intr FIFO1 read error\n");
  375. return IRQ_HANDLED;
  376. }
  377. if (!(val & XILINX_PCIE_RPIFR1_MSI_INTR)) {
  378. /* Clear interrupt FIFO register 1 */
  379. pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
  380. XILINX_PCIE_REG_RPIFR1);
  381. /* Handle INTx Interrupt */
  382. val = ((val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
  383. XILINX_PCIE_RPIFR1_INTR_SHIFT) + 1;
  384. generic_handle_irq(irq_find_mapping(port->irq_domain,
  385. val));
  386. }
  387. }
  388. if (status & XILINX_PCIE_INTR_MSI) {
  389. /* MSI Interrupt */
  390. val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
  391. if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
  392. dev_warn(port->dev, "RP Intr FIFO1 read error\n");
  393. return IRQ_HANDLED;
  394. }
  395. if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
  396. msi_data = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
  397. XILINX_PCIE_RPIFR2_MSG_DATA;
  398. /* Clear interrupt FIFO register 1 */
  399. pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
  400. XILINX_PCIE_REG_RPIFR1);
  401. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  402. /* Handle MSI Interrupt */
  403. generic_handle_irq(msi_data);
  404. }
  405. }
  406. }
  407. if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
  408. dev_warn(port->dev, "Slave unsupported request\n");
  409. if (status & XILINX_PCIE_INTR_SLV_UNEXP)
  410. dev_warn(port->dev, "Slave unexpected completion\n");
  411. if (status & XILINX_PCIE_INTR_SLV_COMPL)
  412. dev_warn(port->dev, "Slave completion timeout\n");
  413. if (status & XILINX_PCIE_INTR_SLV_ERRP)
  414. dev_warn(port->dev, "Slave Error Poison\n");
  415. if (status & XILINX_PCIE_INTR_SLV_CMPABT)
  416. dev_warn(port->dev, "Slave Completer Abort\n");
  417. if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
  418. dev_warn(port->dev, "Slave Illegal Burst\n");
  419. if (status & XILINX_PCIE_INTR_MST_DECERR)
  420. dev_warn(port->dev, "Master decode error\n");
  421. if (status & XILINX_PCIE_INTR_MST_SLVERR)
  422. dev_warn(port->dev, "Master slave error\n");
  423. if (status & XILINX_PCIE_INTR_MST_ERRP)
  424. dev_warn(port->dev, "Master error poison\n");
  425. /* Clear the Interrupt Decode register */
  426. pcie_write(port, status, XILINX_PCIE_REG_IDR);
  427. return IRQ_HANDLED;
  428. }
  429. /**
  430. * xilinx_pcie_free_irq_domain - Free IRQ domain
  431. * @port: PCIe port information
  432. */
  433. static void xilinx_pcie_free_irq_domain(struct xilinx_pcie_port *port)
  434. {
  435. int i;
  436. u32 irq, num_irqs;
  437. /* Free IRQ Domain */
  438. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  439. free_pages(port->msi_pages, 0);
  440. num_irqs = XILINX_NUM_MSI_IRQS;
  441. } else {
  442. /* INTx */
  443. num_irqs = 4;
  444. }
  445. for (i = 0; i < num_irqs; i++) {
  446. irq = irq_find_mapping(port->irq_domain, i);
  447. if (irq > 0)
  448. irq_dispose_mapping(irq);
  449. }
  450. irq_domain_remove(port->irq_domain);
  451. }
  452. /**
  453. * xilinx_pcie_init_irq_domain - Initialize IRQ domain
  454. * @port: PCIe port information
  455. *
  456. * Return: '0' on success and error value on failure
  457. */
  458. static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
  459. {
  460. struct device *dev = port->dev;
  461. struct device_node *node = dev->of_node;
  462. struct device_node *pcie_intc_node;
  463. /* Setup INTx */
  464. pcie_intc_node = of_get_next_child(node, NULL);
  465. if (!pcie_intc_node) {
  466. dev_err(dev, "No PCIe Intc node found\n");
  467. return PTR_ERR(pcie_intc_node);
  468. }
  469. port->irq_domain = irq_domain_add_linear(pcie_intc_node, 4,
  470. &intx_domain_ops,
  471. port);
  472. if (!port->irq_domain) {
  473. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  474. return PTR_ERR(port->irq_domain);
  475. }
  476. /* Setup MSI */
  477. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  478. port->irq_domain = irq_domain_add_linear(node,
  479. XILINX_NUM_MSI_IRQS,
  480. &msi_domain_ops,
  481. &xilinx_pcie_msi_chip);
  482. if (!port->irq_domain) {
  483. dev_err(dev, "Failed to get a MSI IRQ domain\n");
  484. return PTR_ERR(port->irq_domain);
  485. }
  486. xilinx_pcie_enable_msi(port);
  487. }
  488. return 0;
  489. }
  490. /**
  491. * xilinx_pcie_init_port - Initialize hardware
  492. * @port: PCIe port information
  493. */
  494. static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
  495. {
  496. if (xilinx_pcie_link_is_up(port))
  497. dev_info(port->dev, "PCIe Link is UP\n");
  498. else
  499. dev_info(port->dev, "PCIe Link is DOWN\n");
  500. /* Disable all interrupts */
  501. pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
  502. XILINX_PCIE_REG_IMR);
  503. /* Clear pending interrupts */
  504. pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
  505. XILINX_PCIE_IMR_ALL_MASK,
  506. XILINX_PCIE_REG_IDR);
  507. /* Enable all interrupts */
  508. pcie_write(port, XILINX_PCIE_IMR_ALL_MASK, XILINX_PCIE_REG_IMR);
  509. /* Enable the Bridge enable bit */
  510. pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
  511. XILINX_PCIE_REG_RPSC_BEN,
  512. XILINX_PCIE_REG_RPSC);
  513. }
  514. /**
  515. * xilinx_pcie_setup - Setup memory resources
  516. * @nr: Bus number
  517. * @sys: Per controller structure
  518. *
  519. * Return: '1' on success and error value on failure
  520. */
  521. static int xilinx_pcie_setup(int nr, struct pci_sys_data *sys)
  522. {
  523. struct xilinx_pcie_port *port = sys_to_pcie(sys);
  524. list_splice_init(&port->resources, &sys->resources);
  525. return 1;
  526. }
  527. /**
  528. * xilinx_pcie_scan_bus - Scan PCIe bus for devices
  529. * @nr: Bus number
  530. * @sys: Per controller structure
  531. *
  532. * Return: Valid Bus pointer on success and NULL on failure
  533. */
  534. static struct pci_bus *xilinx_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  535. {
  536. struct xilinx_pcie_port *port = sys_to_pcie(sys);
  537. struct pci_bus *bus;
  538. port->root_busno = sys->busnr;
  539. if (IS_ENABLED(CONFIG_PCI_MSI))
  540. bus = pci_scan_root_bus_msi(port->dev, sys->busnr,
  541. &xilinx_pcie_ops, sys,
  542. &sys->resources,
  543. &xilinx_pcie_msi_chip);
  544. else
  545. bus = pci_scan_root_bus(port->dev, sys->busnr,
  546. &xilinx_pcie_ops, sys, &sys->resources);
  547. return bus;
  548. }
  549. /**
  550. * xilinx_pcie_parse_and_add_res - Add resources by parsing ranges
  551. * @port: PCIe port information
  552. *
  553. * Return: '0' on success and error value on failure
  554. */
  555. static int xilinx_pcie_parse_and_add_res(struct xilinx_pcie_port *port)
  556. {
  557. struct device *dev = port->dev;
  558. struct device_node *node = dev->of_node;
  559. struct resource *mem;
  560. resource_size_t offset;
  561. struct of_pci_range_parser parser;
  562. struct of_pci_range range;
  563. struct resource_entry *win;
  564. int err = 0, mem_resno = 0;
  565. /* Get the ranges */
  566. if (of_pci_range_parser_init(&parser, node)) {
  567. dev_err(dev, "missing \"ranges\" property\n");
  568. return -EINVAL;
  569. }
  570. /* Parse the ranges and add the resources found to the list */
  571. for_each_of_pci_range(&parser, &range) {
  572. if (mem_resno >= XILINX_MAX_NUM_RESOURCES) {
  573. dev_err(dev, "Maximum memory resources exceeded\n");
  574. return -EINVAL;
  575. }
  576. mem = devm_kmalloc(dev, sizeof(*mem), GFP_KERNEL);
  577. if (!mem) {
  578. err = -ENOMEM;
  579. goto free_resources;
  580. }
  581. of_pci_range_to_resource(&range, node, mem);
  582. switch (mem->flags & IORESOURCE_TYPE_BITS) {
  583. case IORESOURCE_MEM:
  584. offset = range.cpu_addr - range.pci_addr;
  585. mem_resno++;
  586. break;
  587. default:
  588. err = -EINVAL;
  589. break;
  590. }
  591. if (err < 0) {
  592. dev_warn(dev, "Invalid resource found %pR\n", mem);
  593. continue;
  594. }
  595. err = request_resource(&iomem_resource, mem);
  596. if (err)
  597. goto free_resources;
  598. pci_add_resource_offset(&port->resources, mem, offset);
  599. }
  600. /* Get the bus range */
  601. if (of_pci_parse_bus_range(node, &port->bus_range)) {
  602. u32 val = pcie_read(port, XILINX_PCIE_REG_BIR);
  603. u8 last;
  604. last = (val & XILINX_PCIE_BIR_ECAM_SZ_MASK) >>
  605. XILINX_PCIE_BIR_ECAM_SZ_SHIFT;
  606. port->bus_range = (struct resource) {
  607. .name = node->name,
  608. .start = 0,
  609. .end = last,
  610. .flags = IORESOURCE_BUS,
  611. };
  612. }
  613. /* Register bus resource */
  614. pci_add_resource(&port->resources, &port->bus_range);
  615. return 0;
  616. free_resources:
  617. release_child_resources(&iomem_resource);
  618. resource_list_for_each_entry(win, &port->resources)
  619. devm_kfree(dev, win->res);
  620. pci_free_resource_list(&port->resources);
  621. return err;
  622. }
  623. /**
  624. * xilinx_pcie_parse_dt - Parse Device tree
  625. * @port: PCIe port information
  626. *
  627. * Return: '0' on success and error value on failure
  628. */
  629. static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
  630. {
  631. struct device *dev = port->dev;
  632. struct device_node *node = dev->of_node;
  633. struct resource regs;
  634. const char *type;
  635. int err;
  636. type = of_get_property(node, "device_type", NULL);
  637. if (!type || strcmp(type, "pci")) {
  638. dev_err(dev, "invalid \"device_type\" %s\n", type);
  639. return -EINVAL;
  640. }
  641. err = of_address_to_resource(node, 0, &regs);
  642. if (err) {
  643. dev_err(dev, "missing \"reg\" property\n");
  644. return err;
  645. }
  646. port->reg_base = devm_ioremap_resource(dev, &regs);
  647. if (IS_ERR(port->reg_base))
  648. return PTR_ERR(port->reg_base);
  649. port->irq = irq_of_parse_and_map(node, 0);
  650. err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
  651. IRQF_SHARED | IRQF_NO_THREAD,
  652. "xilinx-pcie", port);
  653. if (err) {
  654. dev_err(dev, "unable to request irq %d\n", port->irq);
  655. return err;
  656. }
  657. return 0;
  658. }
  659. /**
  660. * xilinx_pcie_probe - Probe function
  661. * @pdev: Platform device pointer
  662. *
  663. * Return: '0' on success and error value on failure
  664. */
  665. static int xilinx_pcie_probe(struct platform_device *pdev)
  666. {
  667. struct xilinx_pcie_port *port;
  668. struct hw_pci hw;
  669. struct device *dev = &pdev->dev;
  670. int err;
  671. if (!dev->of_node)
  672. return -ENODEV;
  673. port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  674. if (!port)
  675. return -ENOMEM;
  676. port->dev = dev;
  677. err = xilinx_pcie_parse_dt(port);
  678. if (err) {
  679. dev_err(dev, "Parsing DT failed\n");
  680. return err;
  681. }
  682. xilinx_pcie_init_port(port);
  683. err = xilinx_pcie_init_irq_domain(port);
  684. if (err) {
  685. dev_err(dev, "Failed creating IRQ Domain\n");
  686. return err;
  687. }
  688. /*
  689. * Parse PCI ranges, configuration bus range and
  690. * request their resources
  691. */
  692. INIT_LIST_HEAD(&port->resources);
  693. err = xilinx_pcie_parse_and_add_res(port);
  694. if (err) {
  695. dev_err(dev, "Failed adding resources\n");
  696. return err;
  697. }
  698. platform_set_drvdata(pdev, port);
  699. /* Register the device */
  700. memset(&hw, 0, sizeof(hw));
  701. hw = (struct hw_pci) {
  702. .nr_controllers = 1,
  703. .private_data = (void **)&port,
  704. .setup = xilinx_pcie_setup,
  705. .map_irq = of_irq_parse_and_map_pci,
  706. .scan = xilinx_pcie_scan_bus,
  707. .ops = &xilinx_pcie_ops,
  708. };
  709. #ifdef CONFIG_PCI_MSI
  710. xilinx_pcie_msi_chip.dev = port->dev;
  711. #endif
  712. pci_common_init_dev(dev, &hw);
  713. return 0;
  714. }
  715. /**
  716. * xilinx_pcie_remove - Remove function
  717. * @pdev: Platform device pointer
  718. *
  719. * Return: '0' always
  720. */
  721. static int xilinx_pcie_remove(struct platform_device *pdev)
  722. {
  723. struct xilinx_pcie_port *port = platform_get_drvdata(pdev);
  724. xilinx_pcie_free_irq_domain(port);
  725. return 0;
  726. }
  727. static struct of_device_id xilinx_pcie_of_match[] = {
  728. { .compatible = "xlnx,axi-pcie-host-1.00.a", },
  729. {}
  730. };
  731. static struct platform_driver xilinx_pcie_driver = {
  732. .driver = {
  733. .name = "xilinx-pcie",
  734. .of_match_table = xilinx_pcie_of_match,
  735. .suppress_bind_attrs = true,
  736. },
  737. .probe = xilinx_pcie_probe,
  738. .remove = xilinx_pcie_remove,
  739. };
  740. module_platform_driver(xilinx_pcie_driver);
  741. MODULE_AUTHOR("Xilinx Inc");
  742. MODULE_DESCRIPTION("Xilinx AXI PCIe driver");
  743. MODULE_LICENSE("GPL v2");