pcie-xilinx.c 23 KB

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