pcie-xilinx-nwl.c 24 KB

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