pcie-xilinx-nwl.c 23 KB

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