pcie-xilinx.c 23 KB

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