pcie-xilinx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. return 0;
  293. }
  294. /* IRQ Domain operations */
  295. static const struct irq_domain_ops msi_domain_ops = {
  296. .map = xilinx_pcie_msi_map,
  297. };
  298. /**
  299. * xilinx_pcie_enable_msi - Enable MSI support
  300. * @port: PCIe port information
  301. */
  302. static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
  303. {
  304. phys_addr_t msg_addr;
  305. port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
  306. msg_addr = virt_to_phys((void *)port->msi_pages);
  307. pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
  308. pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
  309. }
  310. /* INTx Functions */
  311. /**
  312. * xilinx_pcie_intx_map - Set the handler for the INTx and mark IRQ as valid
  313. * @domain: IRQ domain
  314. * @irq: Virtual IRQ number
  315. * @hwirq: HW interrupt number
  316. *
  317. * Return: Always returns 0.
  318. */
  319. static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  320. irq_hw_number_t hwirq)
  321. {
  322. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  323. irq_set_chip_data(irq, domain->host_data);
  324. return 0;
  325. }
  326. /* INTx IRQ Domain operations */
  327. static const struct irq_domain_ops intx_domain_ops = {
  328. .map = xilinx_pcie_intx_map,
  329. };
  330. /* PCIe HW Functions */
  331. /**
  332. * xilinx_pcie_intr_handler - Interrupt Service Handler
  333. * @irq: IRQ number
  334. * @data: PCIe port information
  335. *
  336. * Return: IRQ_HANDLED on success and IRQ_NONE on failure
  337. */
  338. static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
  339. {
  340. struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
  341. u32 val, mask, status, msi_data;
  342. /* Read interrupt decode and mask registers */
  343. val = pcie_read(port, XILINX_PCIE_REG_IDR);
  344. mask = pcie_read(port, XILINX_PCIE_REG_IMR);
  345. status = val & mask;
  346. if (!status)
  347. return IRQ_NONE;
  348. if (status & XILINX_PCIE_INTR_LINK_DOWN)
  349. dev_warn(port->dev, "Link Down\n");
  350. if (status & XILINX_PCIE_INTR_ECRC_ERR)
  351. dev_warn(port->dev, "ECRC failed\n");
  352. if (status & XILINX_PCIE_INTR_STR_ERR)
  353. dev_warn(port->dev, "Streaming error\n");
  354. if (status & XILINX_PCIE_INTR_HOT_RESET)
  355. dev_info(port->dev, "Hot reset\n");
  356. if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
  357. dev_warn(port->dev, "ECAM access timeout\n");
  358. if (status & XILINX_PCIE_INTR_CORRECTABLE) {
  359. dev_warn(port->dev, "Correctable error message\n");
  360. xilinx_pcie_clear_err_interrupts(port);
  361. }
  362. if (status & XILINX_PCIE_INTR_NONFATAL) {
  363. dev_warn(port->dev, "Non fatal error message\n");
  364. xilinx_pcie_clear_err_interrupts(port);
  365. }
  366. if (status & XILINX_PCIE_INTR_FATAL) {
  367. dev_warn(port->dev, "Fatal error message\n");
  368. xilinx_pcie_clear_err_interrupts(port);
  369. }
  370. if (status & XILINX_PCIE_INTR_INTX) {
  371. /* INTx interrupt received */
  372. val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
  373. /* Check whether interrupt valid */
  374. if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
  375. dev_warn(port->dev, "RP Intr FIFO1 read error\n");
  376. return IRQ_HANDLED;
  377. }
  378. if (!(val & XILINX_PCIE_RPIFR1_MSI_INTR)) {
  379. /* Clear interrupt FIFO register 1 */
  380. pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
  381. XILINX_PCIE_REG_RPIFR1);
  382. /* Handle INTx Interrupt */
  383. val = ((val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
  384. XILINX_PCIE_RPIFR1_INTR_SHIFT) + 1;
  385. generic_handle_irq(irq_find_mapping(port->irq_domain,
  386. val));
  387. }
  388. }
  389. if (status & XILINX_PCIE_INTR_MSI) {
  390. /* MSI Interrupt */
  391. val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
  392. if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
  393. dev_warn(port->dev, "RP Intr FIFO1 read error\n");
  394. return IRQ_HANDLED;
  395. }
  396. if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
  397. msi_data = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
  398. XILINX_PCIE_RPIFR2_MSG_DATA;
  399. /* Clear interrupt FIFO register 1 */
  400. pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
  401. XILINX_PCIE_REG_RPIFR1);
  402. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  403. /* Handle MSI Interrupt */
  404. generic_handle_irq(msi_data);
  405. }
  406. }
  407. }
  408. if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
  409. dev_warn(port->dev, "Slave unsupported request\n");
  410. if (status & XILINX_PCIE_INTR_SLV_UNEXP)
  411. dev_warn(port->dev, "Slave unexpected completion\n");
  412. if (status & XILINX_PCIE_INTR_SLV_COMPL)
  413. dev_warn(port->dev, "Slave completion timeout\n");
  414. if (status & XILINX_PCIE_INTR_SLV_ERRP)
  415. dev_warn(port->dev, "Slave Error Poison\n");
  416. if (status & XILINX_PCIE_INTR_SLV_CMPABT)
  417. dev_warn(port->dev, "Slave Completer Abort\n");
  418. if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
  419. dev_warn(port->dev, "Slave Illegal Burst\n");
  420. if (status & XILINX_PCIE_INTR_MST_DECERR)
  421. dev_warn(port->dev, "Master decode error\n");
  422. if (status & XILINX_PCIE_INTR_MST_SLVERR)
  423. dev_warn(port->dev, "Master slave error\n");
  424. if (status & XILINX_PCIE_INTR_MST_ERRP)
  425. dev_warn(port->dev, "Master error poison\n");
  426. /* Clear the Interrupt Decode register */
  427. pcie_write(port, status, XILINX_PCIE_REG_IDR);
  428. return IRQ_HANDLED;
  429. }
  430. /**
  431. * xilinx_pcie_free_irq_domain - Free IRQ domain
  432. * @port: PCIe port information
  433. */
  434. static void xilinx_pcie_free_irq_domain(struct xilinx_pcie_port *port)
  435. {
  436. int i;
  437. u32 irq, num_irqs;
  438. /* Free IRQ Domain */
  439. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  440. free_pages(port->msi_pages, 0);
  441. num_irqs = XILINX_NUM_MSI_IRQS;
  442. } else {
  443. /* INTx */
  444. num_irqs = 4;
  445. }
  446. for (i = 0; i < num_irqs; i++) {
  447. irq = irq_find_mapping(port->irq_domain, i);
  448. if (irq > 0)
  449. irq_dispose_mapping(irq);
  450. }
  451. irq_domain_remove(port->irq_domain);
  452. }
  453. /**
  454. * xilinx_pcie_init_irq_domain - Initialize IRQ domain
  455. * @port: PCIe port information
  456. *
  457. * Return: '0' on success and error value on failure
  458. */
  459. static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
  460. {
  461. struct device *dev = port->dev;
  462. struct device_node *node = dev->of_node;
  463. struct device_node *pcie_intc_node;
  464. /* Setup INTx */
  465. pcie_intc_node = of_get_next_child(node, NULL);
  466. if (!pcie_intc_node) {
  467. dev_err(dev, "No PCIe Intc node found\n");
  468. return PTR_ERR(pcie_intc_node);
  469. }
  470. port->irq_domain = irq_domain_add_linear(pcie_intc_node, 4,
  471. &intx_domain_ops,
  472. port);
  473. if (!port->irq_domain) {
  474. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  475. return PTR_ERR(port->irq_domain);
  476. }
  477. /* Setup MSI */
  478. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  479. port->irq_domain = irq_domain_add_linear(node,
  480. XILINX_NUM_MSI_IRQS,
  481. &msi_domain_ops,
  482. &xilinx_pcie_msi_chip);
  483. if (!port->irq_domain) {
  484. dev_err(dev, "Failed to get a MSI IRQ domain\n");
  485. return PTR_ERR(port->irq_domain);
  486. }
  487. xilinx_pcie_enable_msi(port);
  488. }
  489. return 0;
  490. }
  491. /**
  492. * xilinx_pcie_init_port - Initialize hardware
  493. * @port: PCIe port information
  494. */
  495. static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
  496. {
  497. if (xilinx_pcie_link_is_up(port))
  498. dev_info(port->dev, "PCIe Link is UP\n");
  499. else
  500. dev_info(port->dev, "PCIe Link is DOWN\n");
  501. /* Disable all interrupts */
  502. pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
  503. XILINX_PCIE_REG_IMR);
  504. /* Clear pending interrupts */
  505. pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
  506. XILINX_PCIE_IMR_ALL_MASK,
  507. XILINX_PCIE_REG_IDR);
  508. /* Enable all interrupts */
  509. pcie_write(port, XILINX_PCIE_IMR_ALL_MASK, XILINX_PCIE_REG_IMR);
  510. /* Enable the Bridge enable bit */
  511. pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
  512. XILINX_PCIE_REG_RPSC_BEN,
  513. XILINX_PCIE_REG_RPSC);
  514. }
  515. /**
  516. * xilinx_pcie_setup - Setup memory resources
  517. * @nr: Bus number
  518. * @sys: Per controller structure
  519. *
  520. * Return: '1' on success and error value on failure
  521. */
  522. static int xilinx_pcie_setup(int nr, struct pci_sys_data *sys)
  523. {
  524. struct xilinx_pcie_port *port = sys_to_pcie(sys);
  525. list_splice_init(&port->resources, &sys->resources);
  526. return 1;
  527. }
  528. /**
  529. * xilinx_pcie_scan_bus - Scan PCIe bus for devices
  530. * @nr: Bus number
  531. * @sys: Per controller structure
  532. *
  533. * Return: Valid Bus pointer on success and NULL on failure
  534. */
  535. static struct pci_bus *xilinx_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  536. {
  537. struct xilinx_pcie_port *port = sys_to_pcie(sys);
  538. struct pci_bus *bus;
  539. port->root_busno = sys->busnr;
  540. if (IS_ENABLED(CONFIG_PCI_MSI))
  541. bus = pci_scan_root_bus_msi(port->dev, sys->busnr,
  542. &xilinx_pcie_ops, sys,
  543. &sys->resources,
  544. &xilinx_pcie_msi_chip);
  545. else
  546. bus = pci_scan_root_bus(port->dev, sys->busnr,
  547. &xilinx_pcie_ops, sys, &sys->resources);
  548. return bus;
  549. }
  550. /**
  551. * xilinx_pcie_parse_and_add_res - Add resources by parsing ranges
  552. * @port: PCIe port information
  553. *
  554. * Return: '0' on success and error value on failure
  555. */
  556. static int xilinx_pcie_parse_and_add_res(struct xilinx_pcie_port *port)
  557. {
  558. struct device *dev = port->dev;
  559. struct device_node *node = dev->of_node;
  560. struct resource *mem;
  561. resource_size_t offset;
  562. struct of_pci_range_parser parser;
  563. struct of_pci_range range;
  564. struct resource_entry *win;
  565. int err = 0, mem_resno = 0;
  566. /* Get the ranges */
  567. if (of_pci_range_parser_init(&parser, node)) {
  568. dev_err(dev, "missing \"ranges\" property\n");
  569. return -EINVAL;
  570. }
  571. /* Parse the ranges and add the resources found to the list */
  572. for_each_of_pci_range(&parser, &range) {
  573. if (mem_resno >= XILINX_MAX_NUM_RESOURCES) {
  574. dev_err(dev, "Maximum memory resources exceeded\n");
  575. return -EINVAL;
  576. }
  577. mem = devm_kmalloc(dev, sizeof(*mem), GFP_KERNEL);
  578. if (!mem) {
  579. err = -ENOMEM;
  580. goto free_resources;
  581. }
  582. of_pci_range_to_resource(&range, node, mem);
  583. switch (mem->flags & IORESOURCE_TYPE_BITS) {
  584. case IORESOURCE_MEM:
  585. offset = range.cpu_addr - range.pci_addr;
  586. mem_resno++;
  587. break;
  588. default:
  589. err = -EINVAL;
  590. break;
  591. }
  592. if (err < 0) {
  593. dev_warn(dev, "Invalid resource found %pR\n", mem);
  594. continue;
  595. }
  596. err = request_resource(&iomem_resource, mem);
  597. if (err)
  598. goto free_resources;
  599. pci_add_resource_offset(&port->resources, mem, offset);
  600. }
  601. /* Get the bus range */
  602. if (of_pci_parse_bus_range(node, &port->bus_range)) {
  603. u32 val = pcie_read(port, XILINX_PCIE_REG_BIR);
  604. u8 last;
  605. last = (val & XILINX_PCIE_BIR_ECAM_SZ_MASK) >>
  606. XILINX_PCIE_BIR_ECAM_SZ_SHIFT;
  607. port->bus_range = (struct resource) {
  608. .name = node->name,
  609. .start = 0,
  610. .end = last,
  611. .flags = IORESOURCE_BUS,
  612. };
  613. }
  614. /* Register bus resource */
  615. pci_add_resource(&port->resources, &port->bus_range);
  616. return 0;
  617. free_resources:
  618. release_child_resources(&iomem_resource);
  619. resource_list_for_each_entry(win, &port->resources)
  620. devm_kfree(dev, win->res);
  621. pci_free_resource_list(&port->resources);
  622. return err;
  623. }
  624. /**
  625. * xilinx_pcie_parse_dt - Parse Device tree
  626. * @port: PCIe port information
  627. *
  628. * Return: '0' on success and error value on failure
  629. */
  630. static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
  631. {
  632. struct device *dev = port->dev;
  633. struct device_node *node = dev->of_node;
  634. struct resource regs;
  635. const char *type;
  636. int err;
  637. type = of_get_property(node, "device_type", NULL);
  638. if (!type || strcmp(type, "pci")) {
  639. dev_err(dev, "invalid \"device_type\" %s\n", type);
  640. return -EINVAL;
  641. }
  642. err = of_address_to_resource(node, 0, &regs);
  643. if (err) {
  644. dev_err(dev, "missing \"reg\" property\n");
  645. return err;
  646. }
  647. port->reg_base = devm_ioremap_resource(dev, &regs);
  648. if (IS_ERR(port->reg_base))
  649. return PTR_ERR(port->reg_base);
  650. port->irq = irq_of_parse_and_map(node, 0);
  651. err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
  652. IRQF_SHARED, "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");