pcie-xilinx-nwl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCIe host controller driver for NWL PCIe Bridge
  4. * Based on pcie-xilinx.c, pci-tegra.c
  5. *
  6. * (C) Copyright 2014 - 2015, Xilinx, Inc.
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/irq.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/msi.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_pci.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/pci.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/irqchip/chained_irq.h>
  22. #include "../pci.h"
  23. /* Bridge core config registers */
  24. #define BRCFG_PCIE_RX0 0x00000000
  25. #define BRCFG_INTERRUPT 0x00000010
  26. #define BRCFG_PCIE_RX_MSG_FILTER 0x00000020
  27. /* Egress - Bridge translation registers */
  28. #define E_BREG_CAPABILITIES 0x00000200
  29. #define E_BREG_CONTROL 0x00000208
  30. #define E_BREG_BASE_LO 0x00000210
  31. #define E_BREG_BASE_HI 0x00000214
  32. #define E_ECAM_CAPABILITIES 0x00000220
  33. #define E_ECAM_CONTROL 0x00000228
  34. #define E_ECAM_BASE_LO 0x00000230
  35. #define E_ECAM_BASE_HI 0x00000234
  36. /* Ingress - address translations */
  37. #define I_MSII_CAPABILITIES 0x00000300
  38. #define I_MSII_CONTROL 0x00000308
  39. #define I_MSII_BASE_LO 0x00000310
  40. #define I_MSII_BASE_HI 0x00000314
  41. #define I_ISUB_CONTROL 0x000003E8
  42. #define SET_ISUB_CONTROL BIT(0)
  43. /* Rxed msg fifo - Interrupt status registers */
  44. #define MSGF_MISC_STATUS 0x00000400
  45. #define MSGF_MISC_MASK 0x00000404
  46. #define MSGF_LEG_STATUS 0x00000420
  47. #define MSGF_LEG_MASK 0x00000424
  48. #define MSGF_MSI_STATUS_LO 0x00000440
  49. #define MSGF_MSI_STATUS_HI 0x00000444
  50. #define MSGF_MSI_MASK_LO 0x00000448
  51. #define MSGF_MSI_MASK_HI 0x0000044C
  52. /* Msg filter mask bits */
  53. #define CFG_ENABLE_PM_MSG_FWD BIT(1)
  54. #define CFG_ENABLE_INT_MSG_FWD BIT(2)
  55. #define CFG_ENABLE_ERR_MSG_FWD BIT(3)
  56. #define CFG_ENABLE_MSG_FILTER_MASK (CFG_ENABLE_PM_MSG_FWD | \
  57. CFG_ENABLE_INT_MSG_FWD | \
  58. CFG_ENABLE_ERR_MSG_FWD)
  59. /* Misc interrupt status mask bits */
  60. #define MSGF_MISC_SR_RXMSG_AVAIL BIT(0)
  61. #define MSGF_MISC_SR_RXMSG_OVER BIT(1)
  62. #define MSGF_MISC_SR_SLAVE_ERR BIT(4)
  63. #define MSGF_MISC_SR_MASTER_ERR BIT(5)
  64. #define MSGF_MISC_SR_I_ADDR_ERR BIT(6)
  65. #define MSGF_MISC_SR_E_ADDR_ERR BIT(7)
  66. #define MSGF_MISC_SR_FATAL_AER BIT(16)
  67. #define MSGF_MISC_SR_NON_FATAL_AER BIT(17)
  68. #define MSGF_MISC_SR_CORR_AER BIT(18)
  69. #define MSGF_MISC_SR_UR_DETECT BIT(20)
  70. #define MSGF_MISC_SR_NON_FATAL_DEV BIT(22)
  71. #define MSGF_MISC_SR_FATAL_DEV BIT(23)
  72. #define MSGF_MISC_SR_LINK_DOWN BIT(24)
  73. #define MSGF_MSIC_SR_LINK_AUTO_BWIDTH BIT(25)
  74. #define MSGF_MSIC_SR_LINK_BWIDTH BIT(26)
  75. #define MSGF_MISC_SR_MASKALL (MSGF_MISC_SR_RXMSG_AVAIL | \
  76. MSGF_MISC_SR_RXMSG_OVER | \
  77. MSGF_MISC_SR_SLAVE_ERR | \
  78. MSGF_MISC_SR_MASTER_ERR | \
  79. MSGF_MISC_SR_I_ADDR_ERR | \
  80. MSGF_MISC_SR_E_ADDR_ERR | \
  81. MSGF_MISC_SR_FATAL_AER | \
  82. MSGF_MISC_SR_NON_FATAL_AER | \
  83. MSGF_MISC_SR_CORR_AER | \
  84. MSGF_MISC_SR_UR_DETECT | \
  85. MSGF_MISC_SR_NON_FATAL_DEV | \
  86. MSGF_MISC_SR_FATAL_DEV | \
  87. MSGF_MISC_SR_LINK_DOWN | \
  88. MSGF_MSIC_SR_LINK_AUTO_BWIDTH | \
  89. MSGF_MSIC_SR_LINK_BWIDTH)
  90. /* Legacy interrupt status mask bits */
  91. #define MSGF_LEG_SR_INTA BIT(0)
  92. #define MSGF_LEG_SR_INTB BIT(1)
  93. #define MSGF_LEG_SR_INTC BIT(2)
  94. #define MSGF_LEG_SR_INTD BIT(3)
  95. #define MSGF_LEG_SR_MASKALL (MSGF_LEG_SR_INTA | MSGF_LEG_SR_INTB | \
  96. MSGF_LEG_SR_INTC | MSGF_LEG_SR_INTD)
  97. /* MSI interrupt status mask bits */
  98. #define MSGF_MSI_SR_LO_MASK GENMASK(31, 0)
  99. #define MSGF_MSI_SR_HI_MASK GENMASK(31, 0)
  100. #define MSII_PRESENT BIT(0)
  101. #define MSII_ENABLE BIT(0)
  102. #define MSII_STATUS_ENABLE BIT(15)
  103. /* Bridge config interrupt mask */
  104. #define BRCFG_INTERRUPT_MASK BIT(0)
  105. #define BREG_PRESENT BIT(0)
  106. #define BREG_ENABLE BIT(0)
  107. #define BREG_ENABLE_FORCE BIT(1)
  108. /* E_ECAM status mask bits */
  109. #define E_ECAM_PRESENT BIT(0)
  110. #define E_ECAM_CR_ENABLE BIT(0)
  111. #define E_ECAM_SIZE_LOC GENMASK(20, 16)
  112. #define E_ECAM_SIZE_SHIFT 16
  113. #define ECAM_BUS_LOC_SHIFT 20
  114. #define ECAM_DEV_LOC_SHIFT 12
  115. #define NWL_ECAM_VALUE_DEFAULT 12
  116. #define CFG_DMA_REG_BAR GENMASK(2, 0)
  117. #define INT_PCI_MSI_NR (2 * 32)
  118. /* Readin the PS_LINKUP */
  119. #define PS_LINKUP_OFFSET 0x00000238
  120. #define PCIE_PHY_LINKUP_BIT BIT(0)
  121. #define PHY_RDY_LINKUP_BIT BIT(1)
  122. /* Parameters for the waiting for link up routine */
  123. #define LINK_WAIT_MAX_RETRIES 10
  124. #define LINK_WAIT_USLEEP_MIN 90000
  125. #define LINK_WAIT_USLEEP_MAX 100000
  126. struct nwl_msi { /* MSI information */
  127. struct irq_domain *msi_domain;
  128. unsigned long *bitmap;
  129. struct irq_domain *dev_domain;
  130. struct mutex lock; /* protect bitmap variable */
  131. int irq_msi0;
  132. int irq_msi1;
  133. };
  134. struct nwl_pcie {
  135. struct device *dev;
  136. void __iomem *breg_base;
  137. void __iomem *pcireg_base;
  138. void __iomem *ecam_base;
  139. phys_addr_t phys_breg_base; /* Physical Bridge Register Base */
  140. phys_addr_t phys_pcie_reg_base; /* Physical PCIe Controller Base */
  141. phys_addr_t phys_ecam_base; /* Physical Configuration Base */
  142. u32 breg_size;
  143. u32 pcie_reg_size;
  144. u32 ecam_size;
  145. int irq_intx;
  146. int irq_misc;
  147. u32 ecam_value;
  148. u8 last_busno;
  149. u8 root_busno;
  150. struct nwl_msi msi;
  151. struct irq_domain *legacy_irq_domain;
  152. raw_spinlock_t leg_mask_lock;
  153. };
  154. static inline u32 nwl_bridge_readl(struct nwl_pcie *pcie, u32 off)
  155. {
  156. return readl(pcie->breg_base + off);
  157. }
  158. static inline void nwl_bridge_writel(struct nwl_pcie *pcie, u32 val, u32 off)
  159. {
  160. writel(val, pcie->breg_base + off);
  161. }
  162. static bool nwl_pcie_link_up(struct nwl_pcie *pcie)
  163. {
  164. if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PCIE_PHY_LINKUP_BIT)
  165. return true;
  166. return false;
  167. }
  168. static bool nwl_phy_link_up(struct nwl_pcie *pcie)
  169. {
  170. if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PHY_RDY_LINKUP_BIT)
  171. return true;
  172. return false;
  173. }
  174. static int nwl_wait_for_link(struct nwl_pcie *pcie)
  175. {
  176. struct device *dev = pcie->dev;
  177. int retries;
  178. /* check if the link is up or not */
  179. for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
  180. if (nwl_phy_link_up(pcie))
  181. return 0;
  182. usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
  183. }
  184. dev_err(dev, "PHY link never came up\n");
  185. return -ETIMEDOUT;
  186. }
  187. static bool nwl_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
  188. {
  189. struct nwl_pcie *pcie = bus->sysdata;
  190. /* Check link before accessing downstream ports */
  191. if (bus->number != pcie->root_busno) {
  192. if (!nwl_pcie_link_up(pcie))
  193. return false;
  194. }
  195. /* Only one device down on each root port */
  196. if (bus->number == pcie->root_busno && devfn > 0)
  197. return false;
  198. return true;
  199. }
  200. /**
  201. * nwl_pcie_map_bus - Get configuration base
  202. *
  203. * @bus: Bus structure of current bus
  204. * @devfn: Device/function
  205. * @where: Offset from base
  206. *
  207. * Return: Base address of the configuration space needed to be
  208. * accessed.
  209. */
  210. static void __iomem *nwl_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
  211. int where)
  212. {
  213. struct nwl_pcie *pcie = bus->sysdata;
  214. int relbus;
  215. if (!nwl_pcie_valid_device(bus, devfn))
  216. return NULL;
  217. relbus = (bus->number << ECAM_BUS_LOC_SHIFT) |
  218. (devfn << ECAM_DEV_LOC_SHIFT);
  219. return pcie->ecam_base + relbus + where;
  220. }
  221. /* PCIe operations */
  222. static struct pci_ops nwl_pcie_ops = {
  223. .map_bus = nwl_pcie_map_bus,
  224. .read = pci_generic_config_read,
  225. .write = pci_generic_config_write,
  226. };
  227. static irqreturn_t nwl_pcie_misc_handler(int irq, void *data)
  228. {
  229. struct nwl_pcie *pcie = data;
  230. struct device *dev = pcie->dev;
  231. u32 misc_stat;
  232. /* Checking for misc interrupts */
  233. misc_stat = nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
  234. MSGF_MISC_SR_MASKALL;
  235. if (!misc_stat)
  236. return IRQ_NONE;
  237. if (misc_stat & MSGF_MISC_SR_RXMSG_OVER)
  238. dev_err(dev, "Received Message FIFO Overflow\n");
  239. if (misc_stat & MSGF_MISC_SR_SLAVE_ERR)
  240. dev_err(dev, "Slave error\n");
  241. if (misc_stat & MSGF_MISC_SR_MASTER_ERR)
  242. dev_err(dev, "Master error\n");
  243. if (misc_stat & MSGF_MISC_SR_I_ADDR_ERR)
  244. dev_err(dev, "In Misc Ingress address translation error\n");
  245. if (misc_stat & MSGF_MISC_SR_E_ADDR_ERR)
  246. dev_err(dev, "In Misc Egress address translation error\n");
  247. if (misc_stat & MSGF_MISC_SR_FATAL_AER)
  248. dev_err(dev, "Fatal Error in AER Capability\n");
  249. if (misc_stat & MSGF_MISC_SR_NON_FATAL_AER)
  250. dev_err(dev, "Non-Fatal Error in AER Capability\n");
  251. if (misc_stat & MSGF_MISC_SR_CORR_AER)
  252. dev_err(dev, "Correctable Error in AER Capability\n");
  253. if (misc_stat & MSGF_MISC_SR_UR_DETECT)
  254. dev_err(dev, "Unsupported request Detected\n");
  255. if (misc_stat & MSGF_MISC_SR_NON_FATAL_DEV)
  256. dev_err(dev, "Non-Fatal Error Detected\n");
  257. if (misc_stat & MSGF_MISC_SR_FATAL_DEV)
  258. dev_err(dev, "Fatal Error Detected\n");
  259. if (misc_stat & MSGF_MSIC_SR_LINK_AUTO_BWIDTH)
  260. dev_info(dev, "Link Autonomous Bandwidth Management Status bit set\n");
  261. if (misc_stat & MSGF_MSIC_SR_LINK_BWIDTH)
  262. dev_info(dev, "Link Bandwidth Management Status bit set\n");
  263. /* Clear misc interrupt status */
  264. nwl_bridge_writel(pcie, misc_stat, MSGF_MISC_STATUS);
  265. return IRQ_HANDLED;
  266. }
  267. static void nwl_pcie_leg_handler(struct irq_desc *desc)
  268. {
  269. struct irq_chip *chip = irq_desc_get_chip(desc);
  270. struct nwl_pcie *pcie;
  271. unsigned long status;
  272. u32 bit;
  273. u32 virq;
  274. chained_irq_enter(chip, desc);
  275. pcie = irq_desc_get_handler_data(desc);
  276. while ((status = nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
  277. MSGF_LEG_SR_MASKALL) != 0) {
  278. for_each_set_bit(bit, &status, PCI_NUM_INTX) {
  279. virq = irq_find_mapping(pcie->legacy_irq_domain, bit);
  280. if (virq)
  281. generic_handle_irq(virq);
  282. }
  283. }
  284. chained_irq_exit(chip, desc);
  285. }
  286. static void nwl_pcie_handle_msi_irq(struct nwl_pcie *pcie, u32 status_reg)
  287. {
  288. struct nwl_msi *msi;
  289. unsigned long status;
  290. u32 bit;
  291. u32 virq;
  292. msi = &pcie->msi;
  293. while ((status = nwl_bridge_readl(pcie, status_reg)) != 0) {
  294. for_each_set_bit(bit, &status, 32) {
  295. nwl_bridge_writel(pcie, 1 << bit, status_reg);
  296. virq = irq_find_mapping(msi->dev_domain, bit);
  297. if (virq)
  298. generic_handle_irq(virq);
  299. }
  300. }
  301. }
  302. static void nwl_pcie_msi_handler_high(struct irq_desc *desc)
  303. {
  304. struct irq_chip *chip = irq_desc_get_chip(desc);
  305. struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
  306. chained_irq_enter(chip, desc);
  307. nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_HI);
  308. chained_irq_exit(chip, desc);
  309. }
  310. static void nwl_pcie_msi_handler_low(struct irq_desc *desc)
  311. {
  312. struct irq_chip *chip = irq_desc_get_chip(desc);
  313. struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
  314. chained_irq_enter(chip, desc);
  315. nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_LO);
  316. chained_irq_exit(chip, desc);
  317. }
  318. static void nwl_mask_leg_irq(struct irq_data *data)
  319. {
  320. struct irq_desc *desc = irq_to_desc(data->irq);
  321. struct nwl_pcie *pcie;
  322. unsigned long flags;
  323. u32 mask;
  324. u32 val;
  325. pcie = irq_desc_get_chip_data(desc);
  326. mask = 1 << (data->hwirq - 1);
  327. raw_spin_lock_irqsave(&pcie->leg_mask_lock, flags);
  328. val = nwl_bridge_readl(pcie, MSGF_LEG_MASK);
  329. nwl_bridge_writel(pcie, (val & (~mask)), MSGF_LEG_MASK);
  330. raw_spin_unlock_irqrestore(&pcie->leg_mask_lock, flags);
  331. }
  332. static void nwl_unmask_leg_irq(struct irq_data *data)
  333. {
  334. struct irq_desc *desc = irq_to_desc(data->irq);
  335. struct nwl_pcie *pcie;
  336. unsigned long flags;
  337. u32 mask;
  338. u32 val;
  339. pcie = irq_desc_get_chip_data(desc);
  340. mask = 1 << (data->hwirq - 1);
  341. raw_spin_lock_irqsave(&pcie->leg_mask_lock, flags);
  342. val = nwl_bridge_readl(pcie, MSGF_LEG_MASK);
  343. nwl_bridge_writel(pcie, (val | mask), MSGF_LEG_MASK);
  344. raw_spin_unlock_irqrestore(&pcie->leg_mask_lock, flags);
  345. }
  346. static struct irq_chip nwl_leg_irq_chip = {
  347. .name = "nwl_pcie:legacy",
  348. .irq_enable = nwl_unmask_leg_irq,
  349. .irq_disable = nwl_mask_leg_irq,
  350. .irq_mask = nwl_mask_leg_irq,
  351. .irq_unmask = nwl_unmask_leg_irq,
  352. };
  353. static int nwl_legacy_map(struct irq_domain *domain, unsigned int irq,
  354. irq_hw_number_t hwirq)
  355. {
  356. irq_set_chip_and_handler(irq, &nwl_leg_irq_chip, handle_level_irq);
  357. irq_set_chip_data(irq, domain->host_data);
  358. irq_set_status_flags(irq, IRQ_LEVEL);
  359. return 0;
  360. }
  361. static const struct irq_domain_ops legacy_domain_ops = {
  362. .map = nwl_legacy_map,
  363. .xlate = pci_irqd_intx_xlate,
  364. };
  365. #ifdef CONFIG_PCI_MSI
  366. static struct irq_chip nwl_msi_irq_chip = {
  367. .name = "nwl_pcie:msi",
  368. .irq_enable = unmask_msi_irq,
  369. .irq_disable = mask_msi_irq,
  370. .irq_mask = mask_msi_irq,
  371. .irq_unmask = unmask_msi_irq,
  372. };
  373. static struct msi_domain_info nwl_msi_domain_info = {
  374. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  375. MSI_FLAG_MULTI_PCI_MSI),
  376. .chip = &nwl_msi_irq_chip,
  377. };
  378. #endif
  379. static void nwl_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  380. {
  381. struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
  382. phys_addr_t msi_addr = pcie->phys_pcie_reg_base;
  383. msg->address_lo = lower_32_bits(msi_addr);
  384. msg->address_hi = upper_32_bits(msi_addr);
  385. msg->data = data->hwirq;
  386. }
  387. static int nwl_msi_set_affinity(struct irq_data *irq_data,
  388. const struct cpumask *mask, bool force)
  389. {
  390. return -EINVAL;
  391. }
  392. static struct irq_chip nwl_irq_chip = {
  393. .name = "Xilinx MSI",
  394. .irq_compose_msi_msg = nwl_compose_msi_msg,
  395. .irq_set_affinity = nwl_msi_set_affinity,
  396. };
  397. static int nwl_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  398. unsigned int nr_irqs, void *args)
  399. {
  400. struct nwl_pcie *pcie = domain->host_data;
  401. struct nwl_msi *msi = &pcie->msi;
  402. int bit;
  403. int i;
  404. mutex_lock(&msi->lock);
  405. bit = bitmap_find_free_region(msi->bitmap, INT_PCI_MSI_NR,
  406. get_count_order(nr_irqs));
  407. if (bit < 0) {
  408. mutex_unlock(&msi->lock);
  409. return -ENOSPC;
  410. }
  411. for (i = 0; i < nr_irqs; i++) {
  412. irq_domain_set_info(domain, virq + i, bit + i, &nwl_irq_chip,
  413. domain->host_data, handle_simple_irq,
  414. NULL, NULL);
  415. }
  416. mutex_unlock(&msi->lock);
  417. return 0;
  418. }
  419. static void nwl_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  420. unsigned int nr_irqs)
  421. {
  422. struct irq_data *data = irq_domain_get_irq_data(domain, virq);
  423. struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
  424. struct nwl_msi *msi = &pcie->msi;
  425. mutex_lock(&msi->lock);
  426. bitmap_release_region(msi->bitmap, data->hwirq,
  427. get_count_order(nr_irqs));
  428. mutex_unlock(&msi->lock);
  429. }
  430. static const struct irq_domain_ops dev_msi_domain_ops = {
  431. .alloc = nwl_irq_domain_alloc,
  432. .free = nwl_irq_domain_free,
  433. };
  434. static int nwl_pcie_init_msi_irq_domain(struct nwl_pcie *pcie)
  435. {
  436. #ifdef CONFIG_PCI_MSI
  437. struct device *dev = pcie->dev;
  438. struct fwnode_handle *fwnode = of_node_to_fwnode(dev->of_node);
  439. struct nwl_msi *msi = &pcie->msi;
  440. msi->dev_domain = irq_domain_add_linear(NULL, INT_PCI_MSI_NR,
  441. &dev_msi_domain_ops, pcie);
  442. if (!msi->dev_domain) {
  443. dev_err(dev, "failed to create dev IRQ domain\n");
  444. return -ENOMEM;
  445. }
  446. msi->msi_domain = pci_msi_create_irq_domain(fwnode,
  447. &nwl_msi_domain_info,
  448. msi->dev_domain);
  449. if (!msi->msi_domain) {
  450. dev_err(dev, "failed to create msi IRQ domain\n");
  451. irq_domain_remove(msi->dev_domain);
  452. return -ENOMEM;
  453. }
  454. #endif
  455. return 0;
  456. }
  457. static int nwl_pcie_init_irq_domain(struct nwl_pcie *pcie)
  458. {
  459. struct device *dev = pcie->dev;
  460. struct device_node *node = dev->of_node;
  461. struct device_node *legacy_intc_node;
  462. legacy_intc_node = of_get_next_child(node, NULL);
  463. if (!legacy_intc_node) {
  464. dev_err(dev, "No legacy intc node found\n");
  465. return -EINVAL;
  466. }
  467. pcie->legacy_irq_domain = irq_domain_add_linear(legacy_intc_node,
  468. PCI_NUM_INTX,
  469. &legacy_domain_ops,
  470. pcie);
  471. of_node_put(legacy_intc_node);
  472. if (!pcie->legacy_irq_domain) {
  473. dev_err(dev, "failed to create IRQ domain\n");
  474. return -ENOMEM;
  475. }
  476. raw_spin_lock_init(&pcie->leg_mask_lock);
  477. nwl_pcie_init_msi_irq_domain(pcie);
  478. return 0;
  479. }
  480. static int nwl_pcie_enable_msi(struct nwl_pcie *pcie)
  481. {
  482. struct device *dev = pcie->dev;
  483. struct platform_device *pdev = to_platform_device(dev);
  484. struct nwl_msi *msi = &pcie->msi;
  485. unsigned long base;
  486. int ret;
  487. int size = BITS_TO_LONGS(INT_PCI_MSI_NR) * sizeof(long);
  488. mutex_init(&msi->lock);
  489. msi->bitmap = kzalloc(size, GFP_KERNEL);
  490. if (!msi->bitmap)
  491. return -ENOMEM;
  492. /* Get msi_1 IRQ number */
  493. msi->irq_msi1 = platform_get_irq_byname(pdev, "msi1");
  494. if (msi->irq_msi1 < 0) {
  495. dev_err(dev, "failed to get IRQ#%d\n", msi->irq_msi1);
  496. ret = -EINVAL;
  497. goto err;
  498. }
  499. irq_set_chained_handler_and_data(msi->irq_msi1,
  500. nwl_pcie_msi_handler_high, pcie);
  501. /* Get msi_0 IRQ number */
  502. msi->irq_msi0 = platform_get_irq_byname(pdev, "msi0");
  503. if (msi->irq_msi0 < 0) {
  504. dev_err(dev, "failed to get IRQ#%d\n", msi->irq_msi0);
  505. ret = -EINVAL;
  506. goto err;
  507. }
  508. irq_set_chained_handler_and_data(msi->irq_msi0,
  509. nwl_pcie_msi_handler_low, pcie);
  510. /* Check for msii_present bit */
  511. ret = nwl_bridge_readl(pcie, I_MSII_CAPABILITIES) & MSII_PRESENT;
  512. if (!ret) {
  513. dev_err(dev, "MSI not present\n");
  514. ret = -EIO;
  515. goto err;
  516. }
  517. /* Enable MSII */
  518. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
  519. MSII_ENABLE, I_MSII_CONTROL);
  520. /* Enable MSII status */
  521. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
  522. MSII_STATUS_ENABLE, I_MSII_CONTROL);
  523. /* setup AFI/FPCI range */
  524. base = pcie->phys_pcie_reg_base;
  525. nwl_bridge_writel(pcie, lower_32_bits(base), I_MSII_BASE_LO);
  526. nwl_bridge_writel(pcie, upper_32_bits(base), I_MSII_BASE_HI);
  527. /*
  528. * For high range MSI interrupts: disable, clear any pending,
  529. * and enable
  530. */
  531. nwl_bridge_writel(pcie, 0, MSGF_MSI_MASK_HI);
  532. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_HI) &
  533. MSGF_MSI_SR_HI_MASK, MSGF_MSI_STATUS_HI);
  534. nwl_bridge_writel(pcie, MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
  535. /*
  536. * For low range MSI interrupts: disable, clear any pending,
  537. * and enable
  538. */
  539. nwl_bridge_writel(pcie, 0, MSGF_MSI_MASK_LO);
  540. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_LO) &
  541. MSGF_MSI_SR_LO_MASK, MSGF_MSI_STATUS_LO);
  542. nwl_bridge_writel(pcie, MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
  543. return 0;
  544. err:
  545. kfree(msi->bitmap);
  546. msi->bitmap = NULL;
  547. return ret;
  548. }
  549. static int nwl_pcie_bridge_init(struct nwl_pcie *pcie)
  550. {
  551. struct device *dev = pcie->dev;
  552. struct platform_device *pdev = to_platform_device(dev);
  553. u32 breg_val, ecam_val, first_busno = 0;
  554. int err;
  555. breg_val = nwl_bridge_readl(pcie, E_BREG_CAPABILITIES) & BREG_PRESENT;
  556. if (!breg_val) {
  557. dev_err(dev, "BREG is not present\n");
  558. return breg_val;
  559. }
  560. /* Write bridge_off to breg base */
  561. nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_breg_base),
  562. E_BREG_BASE_LO);
  563. nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_breg_base),
  564. E_BREG_BASE_HI);
  565. /* Enable BREG */
  566. nwl_bridge_writel(pcie, ~BREG_ENABLE_FORCE & BREG_ENABLE,
  567. E_BREG_CONTROL);
  568. /* Disable DMA channel registers */
  569. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_PCIE_RX0) |
  570. CFG_DMA_REG_BAR, BRCFG_PCIE_RX0);
  571. /* Enable Ingress subtractive decode translation */
  572. nwl_bridge_writel(pcie, SET_ISUB_CONTROL, I_ISUB_CONTROL);
  573. /* Enable msg filtering details */
  574. nwl_bridge_writel(pcie, CFG_ENABLE_MSG_FILTER_MASK,
  575. BRCFG_PCIE_RX_MSG_FILTER);
  576. err = nwl_wait_for_link(pcie);
  577. if (err)
  578. return err;
  579. ecam_val = nwl_bridge_readl(pcie, E_ECAM_CAPABILITIES) & E_ECAM_PRESENT;
  580. if (!ecam_val) {
  581. dev_err(dev, "ECAM is not present\n");
  582. return ecam_val;
  583. }
  584. /* Enable ECAM */
  585. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
  586. E_ECAM_CR_ENABLE, E_ECAM_CONTROL);
  587. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
  588. (pcie->ecam_value << E_ECAM_SIZE_SHIFT),
  589. E_ECAM_CONTROL);
  590. nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_ecam_base),
  591. E_ECAM_BASE_LO);
  592. nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_ecam_base),
  593. E_ECAM_BASE_HI);
  594. /* Get bus range */
  595. ecam_val = nwl_bridge_readl(pcie, E_ECAM_CONTROL);
  596. pcie->last_busno = (ecam_val & E_ECAM_SIZE_LOC) >> E_ECAM_SIZE_SHIFT;
  597. /* Write primary, secondary and subordinate bus numbers */
  598. ecam_val = first_busno;
  599. ecam_val |= (first_busno + 1) << 8;
  600. ecam_val |= (pcie->last_busno << E_ECAM_SIZE_SHIFT);
  601. writel(ecam_val, (pcie->ecam_base + PCI_PRIMARY_BUS));
  602. if (nwl_pcie_link_up(pcie))
  603. dev_info(dev, "Link is UP\n");
  604. else
  605. dev_info(dev, "Link is DOWN\n");
  606. /* Get misc IRQ number */
  607. pcie->irq_misc = platform_get_irq_byname(pdev, "misc");
  608. if (pcie->irq_misc < 0) {
  609. dev_err(dev, "failed to get misc IRQ %d\n",
  610. pcie->irq_misc);
  611. return -EINVAL;
  612. }
  613. err = devm_request_irq(dev, pcie->irq_misc,
  614. nwl_pcie_misc_handler, IRQF_SHARED,
  615. "nwl_pcie:misc", pcie);
  616. if (err) {
  617. dev_err(dev, "fail to register misc IRQ#%d\n",
  618. pcie->irq_misc);
  619. return err;
  620. }
  621. /* Disable all misc interrupts */
  622. nwl_bridge_writel(pcie, (u32)~MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
  623. /* Clear pending misc interrupts */
  624. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
  625. MSGF_MISC_SR_MASKALL, MSGF_MISC_STATUS);
  626. /* Enable all misc interrupts */
  627. nwl_bridge_writel(pcie, MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
  628. /* Disable all legacy interrupts */
  629. nwl_bridge_writel(pcie, (u32)~MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
  630. /* Clear pending legacy interrupts */
  631. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
  632. MSGF_LEG_SR_MASKALL, MSGF_LEG_STATUS);
  633. /* Enable all legacy interrupts */
  634. nwl_bridge_writel(pcie, MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
  635. /* Enable the bridge config interrupt */
  636. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_INTERRUPT) |
  637. BRCFG_INTERRUPT_MASK, BRCFG_INTERRUPT);
  638. return 0;
  639. }
  640. static int nwl_pcie_parse_dt(struct nwl_pcie *pcie,
  641. struct platform_device *pdev)
  642. {
  643. struct device *dev = pcie->dev;
  644. struct device_node *node = dev->of_node;
  645. struct resource *res;
  646. const char *type;
  647. /* Check for device type */
  648. type = of_get_property(node, "device_type", NULL);
  649. if (!type || strcmp(type, "pci")) {
  650. dev_err(dev, "invalid \"device_type\" %s\n", type);
  651. return -EINVAL;
  652. }
  653. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "breg");
  654. pcie->breg_base = devm_ioremap_resource(dev, res);
  655. if (IS_ERR(pcie->breg_base))
  656. return PTR_ERR(pcie->breg_base);
  657. pcie->phys_breg_base = res->start;
  658. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcireg");
  659. pcie->pcireg_base = devm_ioremap_resource(dev, res);
  660. if (IS_ERR(pcie->pcireg_base))
  661. return PTR_ERR(pcie->pcireg_base);
  662. pcie->phys_pcie_reg_base = res->start;
  663. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
  664. pcie->ecam_base = devm_pci_remap_cfg_resource(dev, res);
  665. if (IS_ERR(pcie->ecam_base))
  666. return PTR_ERR(pcie->ecam_base);
  667. pcie->phys_ecam_base = res->start;
  668. /* Get intx IRQ number */
  669. pcie->irq_intx = platform_get_irq_byname(pdev, "intx");
  670. if (pcie->irq_intx < 0) {
  671. dev_err(dev, "failed to get intx IRQ %d\n", pcie->irq_intx);
  672. return pcie->irq_intx;
  673. }
  674. irq_set_chained_handler_and_data(pcie->irq_intx,
  675. nwl_pcie_leg_handler, pcie);
  676. return 0;
  677. }
  678. static const struct of_device_id nwl_pcie_of_match[] = {
  679. { .compatible = "xlnx,nwl-pcie-2.11", },
  680. {}
  681. };
  682. static int nwl_pcie_probe(struct platform_device *pdev)
  683. {
  684. struct device *dev = &pdev->dev;
  685. struct nwl_pcie *pcie;
  686. struct pci_bus *bus;
  687. struct pci_bus *child;
  688. struct pci_host_bridge *bridge;
  689. int err;
  690. resource_size_t iobase = 0;
  691. LIST_HEAD(res);
  692. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  693. if (!bridge)
  694. return -ENODEV;
  695. pcie = pci_host_bridge_priv(bridge);
  696. pcie->dev = dev;
  697. pcie->ecam_value = NWL_ECAM_VALUE_DEFAULT;
  698. err = nwl_pcie_parse_dt(pcie, pdev);
  699. if (err) {
  700. dev_err(dev, "Parsing DT failed\n");
  701. return err;
  702. }
  703. err = nwl_pcie_bridge_init(pcie);
  704. if (err) {
  705. dev_err(dev, "HW Initialization failed\n");
  706. return err;
  707. }
  708. err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, &res,
  709. &iobase);
  710. if (err) {
  711. dev_err(dev, "Getting bridge resources failed\n");
  712. return err;
  713. }
  714. err = devm_request_pci_bus_resources(dev, &res);
  715. if (err)
  716. goto error;
  717. err = nwl_pcie_init_irq_domain(pcie);
  718. if (err) {
  719. dev_err(dev, "Failed creating IRQ Domain\n");
  720. goto error;
  721. }
  722. list_splice_init(&res, &bridge->windows);
  723. bridge->dev.parent = dev;
  724. bridge->sysdata = pcie;
  725. bridge->busnr = pcie->root_busno;
  726. bridge->ops = &nwl_pcie_ops;
  727. bridge->map_irq = of_irq_parse_and_map_pci;
  728. bridge->swizzle_irq = pci_common_swizzle;
  729. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  730. err = nwl_pcie_enable_msi(pcie);
  731. if (err < 0) {
  732. dev_err(dev, "failed to enable MSI support: %d\n", err);
  733. goto error;
  734. }
  735. }
  736. err = pci_scan_root_bus_bridge(bridge);
  737. if (err)
  738. goto error;
  739. bus = bridge->bus;
  740. pci_assign_unassigned_bus_resources(bus);
  741. list_for_each_entry(child, &bus->children, node)
  742. pcie_bus_configure_settings(child);
  743. pci_bus_add_devices(bus);
  744. return 0;
  745. error:
  746. pci_free_resource_list(&res);
  747. return err;
  748. }
  749. static struct platform_driver nwl_pcie_driver = {
  750. .driver = {
  751. .name = "nwl-pcie",
  752. .suppress_bind_attrs = true,
  753. .of_match_table = nwl_pcie_of_match,
  754. },
  755. .probe = nwl_pcie_probe,
  756. };
  757. builtin_platform_driver(nwl_pcie_driver);