pcie-xilinx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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 irq_desc *desc;
  199. struct msi_desc *msi;
  200. struct xilinx_pcie_port *port;
  201. desc = irq_to_desc(irq);
  202. msi = irq_desc_get_msi_desc(desc);
  203. port = sys_to_pcie(msi->dev->bus->sysdata);
  204. if (!test_bit(irq, msi_irq_in_use))
  205. dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
  206. else
  207. clear_bit(irq, msi_irq_in_use);
  208. }
  209. /**
  210. * xilinx_pcie_assign_msi - Allocate MSI number
  211. * @port: PCIe port structure
  212. *
  213. * Return: A valid IRQ on success and error value on failure.
  214. */
  215. static int xilinx_pcie_assign_msi(struct xilinx_pcie_port *port)
  216. {
  217. int pos;
  218. pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
  219. if (pos < XILINX_NUM_MSI_IRQS)
  220. set_bit(pos, msi_irq_in_use);
  221. else
  222. return -ENOSPC;
  223. return pos;
  224. }
  225. /**
  226. * xilinx_msi_teardown_irq - Destroy the MSI
  227. * @chip: MSI Chip descriptor
  228. * @irq: MSI IRQ to destroy
  229. */
  230. static void xilinx_msi_teardown_irq(struct msi_controller *chip,
  231. unsigned int irq)
  232. {
  233. xilinx_pcie_destroy_msi(irq);
  234. }
  235. /**
  236. * xilinx_pcie_msi_setup_irq - Setup MSI request
  237. * @chip: MSI chip pointer
  238. * @pdev: PCIe device pointer
  239. * @desc: MSI descriptor pointer
  240. *
  241. * Return: '0' on success and error value on failure
  242. */
  243. static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
  244. struct pci_dev *pdev,
  245. struct msi_desc *desc)
  246. {
  247. struct xilinx_pcie_port *port = sys_to_pcie(pdev->bus->sysdata);
  248. unsigned int irq;
  249. int hwirq;
  250. struct msi_msg msg;
  251. phys_addr_t msg_addr;
  252. hwirq = xilinx_pcie_assign_msi(port);
  253. if (hwirq < 0)
  254. return hwirq;
  255. irq = irq_create_mapping(port->irq_domain, hwirq);
  256. if (!irq)
  257. return -EINVAL;
  258. irq_set_msi_desc(irq, desc);
  259. msg_addr = virt_to_phys((void *)port->msi_pages);
  260. msg.address_hi = 0;
  261. msg.address_lo = msg_addr;
  262. msg.data = irq;
  263. pci_write_msi_msg(irq, &msg);
  264. return 0;
  265. }
  266. /* MSI Chip Descriptor */
  267. static struct msi_controller xilinx_pcie_msi_chip = {
  268. .setup_irq = xilinx_pcie_msi_setup_irq,
  269. .teardown_irq = xilinx_msi_teardown_irq,
  270. };
  271. /* HW Interrupt Chip Descriptor */
  272. static struct irq_chip xilinx_msi_irq_chip = {
  273. .name = "Xilinx PCIe MSI",
  274. .irq_enable = pci_msi_unmask_irq,
  275. .irq_disable = pci_msi_mask_irq,
  276. .irq_mask = pci_msi_mask_irq,
  277. .irq_unmask = pci_msi_unmask_irq,
  278. };
  279. /**
  280. * xilinx_pcie_msi_map - Set the handler for the MSI and mark IRQ as valid
  281. * @domain: IRQ domain
  282. * @irq: Virtual IRQ number
  283. * @hwirq: HW interrupt number
  284. *
  285. * Return: Always returns 0.
  286. */
  287. static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
  288. irq_hw_number_t hwirq)
  289. {
  290. irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
  291. irq_set_chip_data(irq, domain->host_data);
  292. set_irq_flags(irq, IRQF_VALID);
  293. return 0;
  294. }
  295. /* IRQ Domain operations */
  296. static const struct irq_domain_ops msi_domain_ops = {
  297. .map = xilinx_pcie_msi_map,
  298. };
  299. /**
  300. * xilinx_pcie_enable_msi - Enable MSI support
  301. * @port: PCIe port information
  302. */
  303. static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
  304. {
  305. phys_addr_t msg_addr;
  306. port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
  307. msg_addr = virt_to_phys((void *)port->msi_pages);
  308. pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
  309. pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
  310. }
  311. /* INTx Functions */
  312. /**
  313. * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
  314. * @domain: IRQ domain
  315. * @irq: Virtual IRQ number
  316. * @hwirq: HW interrupt number
  317. *
  318. * Return: Always returns 0.
  319. */
  320. static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  321. irq_hw_number_t hwirq)
  322. {
  323. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  324. irq_set_chip_data(irq, domain->host_data);
  325. set_irq_flags(irq, IRQF_VALID);
  326. return 0;
  327. }
  328. /* INTx IRQ Domain operations */
  329. static const struct irq_domain_ops intx_domain_ops = {
  330. .map = xilinx_pcie_intx_map,
  331. };
  332. /* PCIe HW Functions */
  333. /**
  334. * xilinx_pcie_intr_handler - Interrupt Service Handler
  335. * @irq: IRQ number
  336. * @data: PCIe port information
  337. *
  338. * Return: IRQ_HANDLED on success and IRQ_NONE on failure
  339. */
  340. static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
  341. {
  342. struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
  343. u32 val, mask, status, msi_data;
  344. /* Read interrupt decode and mask registers */
  345. val = pcie_read(port, XILINX_PCIE_REG_IDR);
  346. mask = pcie_read(port, XILINX_PCIE_REG_IMR);
  347. status = val & mask;
  348. if (!status)
  349. return IRQ_NONE;
  350. if (status & XILINX_PCIE_INTR_LINK_DOWN)
  351. dev_warn(port->dev, "Link Down\n");
  352. if (status & XILINX_PCIE_INTR_ECRC_ERR)
  353. dev_warn(port->dev, "ECRC failed\n");
  354. if (status & XILINX_PCIE_INTR_STR_ERR)
  355. dev_warn(port->dev, "Streaming error\n");
  356. if (status & XILINX_PCIE_INTR_HOT_RESET)
  357. dev_info(port->dev, "Hot reset\n");
  358. if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
  359. dev_warn(port->dev, "ECAM access timeout\n");
  360. if (status & XILINX_PCIE_INTR_CORRECTABLE) {
  361. dev_warn(port->dev, "Correctable error message\n");
  362. xilinx_pcie_clear_err_interrupts(port);
  363. }
  364. if (status & XILINX_PCIE_INTR_NONFATAL) {
  365. dev_warn(port->dev, "Non fatal error message\n");
  366. xilinx_pcie_clear_err_interrupts(port);
  367. }
  368. if (status & XILINX_PCIE_INTR_FATAL) {
  369. dev_warn(port->dev, "Fatal error message\n");
  370. xilinx_pcie_clear_err_interrupts(port);
  371. }
  372. if (status & XILINX_PCIE_INTR_INTX) {
  373. /* INTx interrupt received */
  374. val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
  375. /* Check whether interrupt valid */
  376. if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
  377. dev_warn(port->dev, "RP Intr FIFO1 read error\n");
  378. return IRQ_HANDLED;
  379. }
  380. /* Clear interrupt FIFO register 1 */
  381. pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
  382. XILINX_PCIE_REG_RPIFR1);
  383. /* Handle INTx Interrupt */
  384. val = ((val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
  385. XILINX_PCIE_RPIFR1_INTR_SHIFT) + 1;
  386. generic_handle_irq(irq_find_mapping(port->irq_domain, val));
  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. bus = pci_scan_root_bus(port->dev, sys->busnr, &xilinx_pcie_ops,
  540. sys, &sys->resources);
  541. return bus;
  542. }
  543. /**
  544. * xilinx_pcie_parse_and_add_res - Add resources by parsing ranges
  545. * @port: PCIe port information
  546. *
  547. * Return: '0' on success and error value on failure
  548. */
  549. static int xilinx_pcie_parse_and_add_res(struct xilinx_pcie_port *port)
  550. {
  551. struct device *dev = port->dev;
  552. struct device_node *node = dev->of_node;
  553. struct resource *mem;
  554. resource_size_t offset;
  555. struct of_pci_range_parser parser;
  556. struct of_pci_range range;
  557. struct resource_entry *win;
  558. int err = 0, mem_resno = 0;
  559. /* Get the ranges */
  560. if (of_pci_range_parser_init(&parser, node)) {
  561. dev_err(dev, "missing \"ranges\" property\n");
  562. return -EINVAL;
  563. }
  564. /* Parse the ranges and add the resources found to the list */
  565. for_each_of_pci_range(&parser, &range) {
  566. if (mem_resno >= XILINX_MAX_NUM_RESOURCES) {
  567. dev_err(dev, "Maximum memory resources exceeded\n");
  568. return -EINVAL;
  569. }
  570. mem = devm_kmalloc(dev, sizeof(*mem), GFP_KERNEL);
  571. if (!mem) {
  572. err = -ENOMEM;
  573. goto free_resources;
  574. }
  575. of_pci_range_to_resource(&range, node, mem);
  576. switch (mem->flags & IORESOURCE_TYPE_BITS) {
  577. case IORESOURCE_MEM:
  578. offset = range.cpu_addr - range.pci_addr;
  579. mem_resno++;
  580. break;
  581. default:
  582. err = -EINVAL;
  583. break;
  584. }
  585. if (err < 0) {
  586. dev_warn(dev, "Invalid resource found %pR\n", mem);
  587. continue;
  588. }
  589. err = request_resource(&iomem_resource, mem);
  590. if (err)
  591. goto free_resources;
  592. pci_add_resource_offset(&port->resources, mem, offset);
  593. }
  594. /* Get the bus range */
  595. if (of_pci_parse_bus_range(node, &port->bus_range)) {
  596. u32 val = pcie_read(port, XILINX_PCIE_REG_BIR);
  597. u8 last;
  598. last = (val & XILINX_PCIE_BIR_ECAM_SZ_MASK) >>
  599. XILINX_PCIE_BIR_ECAM_SZ_SHIFT;
  600. port->bus_range = (struct resource) {
  601. .name = node->name,
  602. .start = 0,
  603. .end = last,
  604. .flags = IORESOURCE_BUS,
  605. };
  606. }
  607. /* Register bus resource */
  608. pci_add_resource(&port->resources, &port->bus_range);
  609. return 0;
  610. free_resources:
  611. release_child_resources(&iomem_resource);
  612. resource_list_for_each_entry(win, &port->resources)
  613. devm_kfree(dev, win->res);
  614. pci_free_resource_list(&port->resources);
  615. return err;
  616. }
  617. /**
  618. * xilinx_pcie_parse_dt - Parse Device tree
  619. * @port: PCIe port information
  620. *
  621. * Return: '0' on success and error value on failure
  622. */
  623. static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
  624. {
  625. struct device *dev = port->dev;
  626. struct device_node *node = dev->of_node;
  627. struct resource regs;
  628. const char *type;
  629. int err;
  630. type = of_get_property(node, "device_type", NULL);
  631. if (!type || strcmp(type, "pci")) {
  632. dev_err(dev, "invalid \"device_type\" %s\n", type);
  633. return -EINVAL;
  634. }
  635. err = of_address_to_resource(node, 0, &regs);
  636. if (err) {
  637. dev_err(dev, "missing \"reg\" property\n");
  638. return err;
  639. }
  640. port->reg_base = devm_ioremap_resource(dev, &regs);
  641. if (IS_ERR(port->reg_base))
  642. return PTR_ERR(port->reg_base);
  643. port->irq = irq_of_parse_and_map(node, 0);
  644. err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
  645. IRQF_SHARED, "xilinx-pcie", port);
  646. if (err) {
  647. dev_err(dev, "unable to request irq %d\n", port->irq);
  648. return err;
  649. }
  650. return 0;
  651. }
  652. /**
  653. * xilinx_pcie_probe - Probe function
  654. * @pdev: Platform device pointer
  655. *
  656. * Return: '0' on success and error value on failure
  657. */
  658. static int xilinx_pcie_probe(struct platform_device *pdev)
  659. {
  660. struct xilinx_pcie_port *port;
  661. struct hw_pci hw;
  662. struct device *dev = &pdev->dev;
  663. int err;
  664. if (!dev->of_node)
  665. return -ENODEV;
  666. port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  667. if (!port)
  668. return -ENOMEM;
  669. port->dev = dev;
  670. err = xilinx_pcie_parse_dt(port);
  671. if (err) {
  672. dev_err(dev, "Parsing DT failed\n");
  673. return err;
  674. }
  675. xilinx_pcie_init_port(port);
  676. err = xilinx_pcie_init_irq_domain(port);
  677. if (err) {
  678. dev_err(dev, "Failed creating IRQ Domain\n");
  679. return err;
  680. }
  681. /*
  682. * Parse PCI ranges, configuration bus range and
  683. * request their resources
  684. */
  685. INIT_LIST_HEAD(&port->resources);
  686. err = xilinx_pcie_parse_and_add_res(port);
  687. if (err) {
  688. dev_err(dev, "Failed adding resources\n");
  689. return err;
  690. }
  691. platform_set_drvdata(pdev, port);
  692. /* Register the device */
  693. memset(&hw, 0, sizeof(hw));
  694. hw = (struct hw_pci) {
  695. .nr_controllers = 1,
  696. .private_data = (void **)&port,
  697. .setup = xilinx_pcie_setup,
  698. .map_irq = of_irq_parse_and_map_pci,
  699. .scan = xilinx_pcie_scan_bus,
  700. .ops = &xilinx_pcie_ops,
  701. };
  702. #ifdef CONFIG_PCI_MSI
  703. xilinx_pcie_msi_chip.dev = port->dev;
  704. hw.msi_ctrl = &xilinx_pcie_msi_chip;
  705. #endif
  706. pci_common_init_dev(dev, &hw);
  707. return 0;
  708. }
  709. /**
  710. * xilinx_pcie_remove - Remove function
  711. * @pdev: Platform device pointer
  712. *
  713. * Return: '0' always
  714. */
  715. static int xilinx_pcie_remove(struct platform_device *pdev)
  716. {
  717. struct xilinx_pcie_port *port = platform_get_drvdata(pdev);
  718. xilinx_pcie_free_irq_domain(port);
  719. return 0;
  720. }
  721. static struct of_device_id xilinx_pcie_of_match[] = {
  722. { .compatible = "xlnx,axi-pcie-host-1.00.a", },
  723. {}
  724. };
  725. static struct platform_driver xilinx_pcie_driver = {
  726. .driver = {
  727. .name = "xilinx-pcie",
  728. .of_match_table = xilinx_pcie_of_match,
  729. .suppress_bind_attrs = true,
  730. },
  731. .probe = xilinx_pcie_probe,
  732. .remove = xilinx_pcie_remove,
  733. };
  734. module_platform_driver(xilinx_pcie_driver);
  735. MODULE_AUTHOR("Xilinx Inc");
  736. MODULE_DESCRIPTION("Xilinx AXI PCIe driver");
  737. MODULE_LICENSE("GPL v2");