pcie-xilinx-nwl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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/pci.h>
  20. #include <linux/pci-ecam.h>
  21. #include <linux/phy/phy.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_MISC_SR_LINK_AUTO_BWIDTH BIT(25)
  77. #define MSGF_MISC_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_MISC_SR_LINK_AUTO_BWIDTH | \
  92. MSGF_MISC_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_MAX_SIZE 16
  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. struct phy *phy[4];
  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. struct nwl_msi msi;
  151. struct irq_domain *intx_irq_domain;
  152. struct clk *clk;
  153. raw_spinlock_t leg_mask_lock;
  154. };
  155. static inline u32 nwl_bridge_readl(struct nwl_pcie *pcie, u32 off)
  156. {
  157. return readl(pcie->breg_base + off);
  158. }
  159. static inline void nwl_bridge_writel(struct nwl_pcie *pcie, u32 val, u32 off)
  160. {
  161. writel(val, pcie->breg_base + off);
  162. }
  163. static bool nwl_pcie_link_up(struct nwl_pcie *pcie)
  164. {
  165. if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PCIE_PHY_LINKUP_BIT)
  166. return true;
  167. return false;
  168. }
  169. static bool nwl_phy_link_up(struct nwl_pcie *pcie)
  170. {
  171. if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PHY_RDY_LINKUP_BIT)
  172. return true;
  173. return false;
  174. }
  175. static int nwl_wait_for_link(struct nwl_pcie *pcie)
  176. {
  177. struct device *dev = pcie->dev;
  178. int retries;
  179. /* check if the link is up or not */
  180. for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
  181. if (nwl_phy_link_up(pcie))
  182. return 0;
  183. usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
  184. }
  185. dev_err(dev, "PHY link never came up\n");
  186. return -ETIMEDOUT;
  187. }
  188. static bool nwl_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
  189. {
  190. struct nwl_pcie *pcie = bus->sysdata;
  191. /* Check link before accessing downstream ports */
  192. if (!pci_is_root_bus(bus)) {
  193. if (!nwl_pcie_link_up(pcie))
  194. return false;
  195. } else if (devfn > 0)
  196. /* Only one device down on each root port */
  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. if (!nwl_pcie_valid_device(bus, devfn))
  215. return NULL;
  216. return pcie->ecam_base + PCIE_ECAM_OFFSET(bus->number, devfn, where);
  217. }
  218. /* PCIe operations */
  219. static struct pci_ops nwl_pcie_ops = {
  220. .map_bus = nwl_pcie_map_bus,
  221. .read = pci_generic_config_read,
  222. .write = pci_generic_config_write,
  223. };
  224. static irqreturn_t nwl_pcie_misc_handler(int irq, void *data)
  225. {
  226. struct nwl_pcie *pcie = data;
  227. struct device *dev = pcie->dev;
  228. u32 misc_stat;
  229. /* Checking for misc interrupts */
  230. misc_stat = nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
  231. MSGF_MISC_SR_MASKALL;
  232. if (!misc_stat)
  233. return IRQ_NONE;
  234. if (misc_stat & MSGF_MISC_SR_RXMSG_OVER)
  235. dev_err_ratelimited(dev, "Received Message FIFO Overflow\n");
  236. if (misc_stat & MSGF_MISC_SR_SLAVE_ERR)
  237. dev_err_ratelimited(dev, "Slave error\n");
  238. if (misc_stat & MSGF_MISC_SR_MASTER_ERR)
  239. dev_err_ratelimited(dev, "Master error\n");
  240. if (misc_stat & MSGF_MISC_SR_I_ADDR_ERR)
  241. dev_err_ratelimited(dev, "In Misc Ingress address translation error\n");
  242. if (misc_stat & MSGF_MISC_SR_E_ADDR_ERR)
  243. dev_err_ratelimited(dev, "In Misc Egress address translation error\n");
  244. if (misc_stat & MSGF_MISC_SR_FATAL_AER)
  245. dev_err_ratelimited(dev, "Fatal Error in AER Capability\n");
  246. if (misc_stat & MSGF_MISC_SR_NON_FATAL_AER)
  247. dev_err_ratelimited(dev, "Non-Fatal Error in AER Capability\n");
  248. if (misc_stat & MSGF_MISC_SR_CORR_AER)
  249. dev_err_ratelimited(dev, "Correctable Error in AER Capability\n");
  250. if (misc_stat & MSGF_MISC_SR_UR_DETECT)
  251. dev_err_ratelimited(dev, "Unsupported request Detected\n");
  252. if (misc_stat & MSGF_MISC_SR_NON_FATAL_DEV)
  253. dev_err_ratelimited(dev, "Non-Fatal Error Detected\n");
  254. if (misc_stat & MSGF_MISC_SR_FATAL_DEV)
  255. dev_err_ratelimited(dev, "Fatal Error Detected\n");
  256. if (misc_stat & MSGF_MISC_SR_LINK_AUTO_BWIDTH)
  257. dev_info(dev, "Link Autonomous Bandwidth Management Status bit set\n");
  258. if (misc_stat & MSGF_MISC_SR_LINK_BWIDTH)
  259. dev_info(dev, "Link Bandwidth Management Status bit set\n");
  260. /* Clear misc interrupt status */
  261. nwl_bridge_writel(pcie, misc_stat, MSGF_MISC_STATUS);
  262. return IRQ_HANDLED;
  263. }
  264. static void nwl_pcie_leg_handler(struct irq_desc *desc)
  265. {
  266. struct irq_chip *chip = irq_desc_get_chip(desc);
  267. struct nwl_pcie *pcie;
  268. unsigned long status;
  269. u32 bit;
  270. chained_irq_enter(chip, desc);
  271. pcie = irq_desc_get_handler_data(desc);
  272. while ((status = nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
  273. MSGF_LEG_SR_MASKALL) != 0) {
  274. for_each_set_bit(bit, &status, PCI_NUM_INTX)
  275. generic_handle_domain_irq(pcie->intx_irq_domain, bit);
  276. }
  277. chained_irq_exit(chip, desc);
  278. }
  279. static void nwl_pcie_handle_msi_irq(struct nwl_pcie *pcie, u32 status_reg)
  280. {
  281. struct nwl_msi *msi = &pcie->msi;
  282. unsigned long status;
  283. u32 bit;
  284. while ((status = nwl_bridge_readl(pcie, status_reg)) != 0) {
  285. for_each_set_bit(bit, &status, 32) {
  286. nwl_bridge_writel(pcie, 1 << bit, status_reg);
  287. generic_handle_domain_irq(msi->dev_domain, bit);
  288. }
  289. }
  290. }
  291. static void nwl_pcie_msi_handler_high(struct irq_desc *desc)
  292. {
  293. struct irq_chip *chip = irq_desc_get_chip(desc);
  294. struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
  295. chained_irq_enter(chip, desc);
  296. nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_HI);
  297. chained_irq_exit(chip, desc);
  298. }
  299. static void nwl_pcie_msi_handler_low(struct irq_desc *desc)
  300. {
  301. struct irq_chip *chip = irq_desc_get_chip(desc);
  302. struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
  303. chained_irq_enter(chip, desc);
  304. nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_LO);
  305. chained_irq_exit(chip, desc);
  306. }
  307. static void nwl_mask_intx_irq(struct irq_data *data)
  308. {
  309. struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
  310. unsigned long flags;
  311. u32 mask;
  312. u32 val;
  313. mask = 1 << data->hwirq;
  314. raw_spin_lock_irqsave(&pcie->leg_mask_lock, flags);
  315. val = nwl_bridge_readl(pcie, MSGF_LEG_MASK);
  316. nwl_bridge_writel(pcie, (val & (~mask)), MSGF_LEG_MASK);
  317. raw_spin_unlock_irqrestore(&pcie->leg_mask_lock, flags);
  318. }
  319. static void nwl_unmask_intx_irq(struct irq_data *data)
  320. {
  321. struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
  322. unsigned long flags;
  323. u32 mask;
  324. u32 val;
  325. mask = 1 << data->hwirq;
  326. raw_spin_lock_irqsave(&pcie->leg_mask_lock, flags);
  327. val = nwl_bridge_readl(pcie, MSGF_LEG_MASK);
  328. nwl_bridge_writel(pcie, (val | mask), MSGF_LEG_MASK);
  329. raw_spin_unlock_irqrestore(&pcie->leg_mask_lock, flags);
  330. }
  331. static struct irq_chip nwl_intx_irq_chip = {
  332. .name = "nwl_pcie:legacy",
  333. .irq_enable = nwl_unmask_intx_irq,
  334. .irq_disable = nwl_mask_intx_irq,
  335. .irq_mask = nwl_mask_intx_irq,
  336. .irq_unmask = nwl_unmask_intx_irq,
  337. };
  338. static int nwl_intx_map(struct irq_domain *domain, unsigned int irq,
  339. irq_hw_number_t hwirq)
  340. {
  341. irq_set_chip_and_handler(irq, &nwl_intx_irq_chip, handle_level_irq);
  342. irq_set_chip_data(irq, domain->host_data);
  343. irq_set_status_flags(irq, IRQ_LEVEL);
  344. return 0;
  345. }
  346. static const struct irq_domain_ops intx_domain_ops = {
  347. .map = nwl_intx_map,
  348. .xlate = pci_irqd_intx_xlate,
  349. };
  350. #ifdef CONFIG_PCI_MSI
  351. static struct irq_chip nwl_msi_irq_chip = {
  352. .name = "nwl_pcie:msi",
  353. .irq_enable = pci_msi_unmask_irq,
  354. .irq_disable = pci_msi_mask_irq,
  355. .irq_mask = pci_msi_mask_irq,
  356. .irq_unmask = pci_msi_unmask_irq,
  357. };
  358. static struct msi_domain_info nwl_msi_domain_info = {
  359. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  360. MSI_FLAG_NO_AFFINITY | MSI_FLAG_MULTI_PCI_MSI,
  361. .chip = &nwl_msi_irq_chip,
  362. };
  363. #endif
  364. static void nwl_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  365. {
  366. struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
  367. phys_addr_t msi_addr = pcie->phys_pcie_reg_base;
  368. msg->address_lo = lower_32_bits(msi_addr);
  369. msg->address_hi = upper_32_bits(msi_addr);
  370. msg->data = data->hwirq;
  371. }
  372. static struct irq_chip nwl_irq_chip = {
  373. .name = "Xilinx MSI",
  374. .irq_compose_msi_msg = nwl_compose_msi_msg,
  375. };
  376. static int nwl_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  377. unsigned int nr_irqs, void *args)
  378. {
  379. struct nwl_pcie *pcie = domain->host_data;
  380. struct nwl_msi *msi = &pcie->msi;
  381. int bit;
  382. int i;
  383. mutex_lock(&msi->lock);
  384. bit = bitmap_find_free_region(msi->bitmap, INT_PCI_MSI_NR,
  385. get_count_order(nr_irqs));
  386. if (bit < 0) {
  387. mutex_unlock(&msi->lock);
  388. return -ENOSPC;
  389. }
  390. for (i = 0; i < nr_irqs; i++) {
  391. irq_domain_set_info(domain, virq + i, bit + i, &nwl_irq_chip,
  392. domain->host_data, handle_simple_irq,
  393. NULL, NULL);
  394. }
  395. mutex_unlock(&msi->lock);
  396. return 0;
  397. }
  398. static void nwl_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  399. unsigned int nr_irqs)
  400. {
  401. struct irq_data *data = irq_domain_get_irq_data(domain, virq);
  402. struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
  403. struct nwl_msi *msi = &pcie->msi;
  404. mutex_lock(&msi->lock);
  405. bitmap_release_region(msi->bitmap, data->hwirq,
  406. get_count_order(nr_irqs));
  407. mutex_unlock(&msi->lock);
  408. }
  409. static const struct irq_domain_ops dev_msi_domain_ops = {
  410. .alloc = nwl_irq_domain_alloc,
  411. .free = nwl_irq_domain_free,
  412. };
  413. static int nwl_pcie_init_msi_irq_domain(struct nwl_pcie *pcie)
  414. {
  415. #ifdef CONFIG_PCI_MSI
  416. struct device *dev = pcie->dev;
  417. struct fwnode_handle *fwnode = of_node_to_fwnode(dev->of_node);
  418. struct nwl_msi *msi = &pcie->msi;
  419. msi->dev_domain = irq_domain_add_linear(NULL, INT_PCI_MSI_NR,
  420. &dev_msi_domain_ops, pcie);
  421. if (!msi->dev_domain) {
  422. dev_err(dev, "failed to create dev IRQ domain\n");
  423. return -ENOMEM;
  424. }
  425. msi->msi_domain = pci_msi_create_irq_domain(fwnode,
  426. &nwl_msi_domain_info,
  427. msi->dev_domain);
  428. if (!msi->msi_domain) {
  429. dev_err(dev, "failed to create msi IRQ domain\n");
  430. irq_domain_remove(msi->dev_domain);
  431. return -ENOMEM;
  432. }
  433. #endif
  434. return 0;
  435. }
  436. static void nwl_pcie_phy_power_off(struct nwl_pcie *pcie, int i)
  437. {
  438. int err = phy_power_off(pcie->phy[i]);
  439. if (err)
  440. dev_err(pcie->dev, "could not power off phy %d (err=%d)\n", i,
  441. err);
  442. }
  443. static void nwl_pcie_phy_exit(struct nwl_pcie *pcie, int i)
  444. {
  445. int err = phy_exit(pcie->phy[i]);
  446. if (err)
  447. dev_err(pcie->dev, "could not exit phy %d (err=%d)\n", i, err);
  448. }
  449. static int nwl_pcie_phy_enable(struct nwl_pcie *pcie)
  450. {
  451. int i, ret;
  452. for (i = 0; i < ARRAY_SIZE(pcie->phy); i++) {
  453. ret = phy_init(pcie->phy[i]);
  454. if (ret)
  455. goto err;
  456. ret = phy_power_on(pcie->phy[i]);
  457. if (ret) {
  458. nwl_pcie_phy_exit(pcie, i);
  459. goto err;
  460. }
  461. }
  462. return 0;
  463. err:
  464. while (i--) {
  465. nwl_pcie_phy_power_off(pcie, i);
  466. nwl_pcie_phy_exit(pcie, i);
  467. }
  468. return ret;
  469. }
  470. static void nwl_pcie_phy_disable(struct nwl_pcie *pcie)
  471. {
  472. int i;
  473. for (i = ARRAY_SIZE(pcie->phy); i--;) {
  474. nwl_pcie_phy_power_off(pcie, i);
  475. nwl_pcie_phy_exit(pcie, i);
  476. }
  477. }
  478. static int nwl_pcie_init_irq_domain(struct nwl_pcie *pcie)
  479. {
  480. struct device *dev = pcie->dev;
  481. struct device_node *node = dev->of_node;
  482. struct device_node *intc_node;
  483. intc_node = of_get_next_child(node, NULL);
  484. if (!intc_node) {
  485. dev_err(dev, "No legacy intc node found\n");
  486. return -EINVAL;
  487. }
  488. pcie->intx_irq_domain = irq_domain_add_linear(intc_node,
  489. PCI_NUM_INTX,
  490. &intx_domain_ops,
  491. pcie);
  492. of_node_put(intc_node);
  493. if (!pcie->intx_irq_domain) {
  494. dev_err(dev, "failed to create IRQ domain\n");
  495. return -ENOMEM;
  496. }
  497. raw_spin_lock_init(&pcie->leg_mask_lock);
  498. nwl_pcie_init_msi_irq_domain(pcie);
  499. return 0;
  500. }
  501. static int nwl_pcie_enable_msi(struct nwl_pcie *pcie)
  502. {
  503. struct device *dev = pcie->dev;
  504. struct platform_device *pdev = to_platform_device(dev);
  505. struct nwl_msi *msi = &pcie->msi;
  506. unsigned long base;
  507. int ret;
  508. mutex_init(&msi->lock);
  509. /* Get msi_1 IRQ number */
  510. msi->irq_msi1 = platform_get_irq_byname(pdev, "msi1");
  511. if (msi->irq_msi1 < 0)
  512. return -EINVAL;
  513. irq_set_chained_handler_and_data(msi->irq_msi1,
  514. nwl_pcie_msi_handler_high, pcie);
  515. /* Get msi_0 IRQ number */
  516. msi->irq_msi0 = platform_get_irq_byname(pdev, "msi0");
  517. if (msi->irq_msi0 < 0)
  518. return -EINVAL;
  519. irq_set_chained_handler_and_data(msi->irq_msi0,
  520. nwl_pcie_msi_handler_low, pcie);
  521. /* Check for msii_present bit */
  522. ret = nwl_bridge_readl(pcie, I_MSII_CAPABILITIES) & MSII_PRESENT;
  523. if (!ret) {
  524. dev_err(dev, "MSI not present\n");
  525. return -EIO;
  526. }
  527. /* Enable MSII */
  528. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
  529. MSII_ENABLE, I_MSII_CONTROL);
  530. /* Enable MSII status */
  531. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
  532. MSII_STATUS_ENABLE, I_MSII_CONTROL);
  533. /* setup AFI/FPCI range */
  534. base = pcie->phys_pcie_reg_base;
  535. nwl_bridge_writel(pcie, lower_32_bits(base), I_MSII_BASE_LO);
  536. nwl_bridge_writel(pcie, upper_32_bits(base), I_MSII_BASE_HI);
  537. /*
  538. * For high range MSI interrupts: disable, clear any pending,
  539. * and enable
  540. */
  541. nwl_bridge_writel(pcie, 0, MSGF_MSI_MASK_HI);
  542. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_HI) &
  543. MSGF_MSI_SR_HI_MASK, MSGF_MSI_STATUS_HI);
  544. nwl_bridge_writel(pcie, MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
  545. /*
  546. * For low range MSI interrupts: disable, clear any pending,
  547. * and enable
  548. */
  549. nwl_bridge_writel(pcie, 0, MSGF_MSI_MASK_LO);
  550. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_LO) &
  551. MSGF_MSI_SR_LO_MASK, MSGF_MSI_STATUS_LO);
  552. nwl_bridge_writel(pcie, MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
  553. return 0;
  554. }
  555. static int nwl_pcie_bridge_init(struct nwl_pcie *pcie)
  556. {
  557. struct device *dev = pcie->dev;
  558. struct platform_device *pdev = to_platform_device(dev);
  559. u32 breg_val, ecam_val;
  560. int err;
  561. breg_val = nwl_bridge_readl(pcie, E_BREG_CAPABILITIES) & BREG_PRESENT;
  562. if (!breg_val) {
  563. dev_err(dev, "BREG is not present\n");
  564. return breg_val;
  565. }
  566. /* Write bridge_off to breg base */
  567. nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_breg_base),
  568. E_BREG_BASE_LO);
  569. nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_breg_base),
  570. E_BREG_BASE_HI);
  571. /* Enable BREG */
  572. nwl_bridge_writel(pcie, ~BREG_ENABLE_FORCE & BREG_ENABLE,
  573. E_BREG_CONTROL);
  574. /* Disable DMA channel registers */
  575. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_PCIE_RX0) |
  576. CFG_DMA_REG_BAR, BRCFG_PCIE_RX0);
  577. /* Enable Ingress subtractive decode translation */
  578. nwl_bridge_writel(pcie, SET_ISUB_CONTROL, I_ISUB_CONTROL);
  579. /* Enable msg filtering details */
  580. nwl_bridge_writel(pcie, CFG_ENABLE_MSG_FILTER_MASK,
  581. BRCFG_PCIE_RX_MSG_FILTER);
  582. /* This routes the PCIe DMA traffic to go through CCI path */
  583. if (of_dma_is_coherent(dev->of_node))
  584. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_PCIE_RX1) |
  585. CFG_PCIE_CACHE, BRCFG_PCIE_RX1);
  586. err = nwl_wait_for_link(pcie);
  587. if (err)
  588. return err;
  589. ecam_val = nwl_bridge_readl(pcie, E_ECAM_CAPABILITIES) & E_ECAM_PRESENT;
  590. if (!ecam_val) {
  591. dev_err(dev, "ECAM is not present\n");
  592. return ecam_val;
  593. }
  594. /* Enable ECAM */
  595. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
  596. E_ECAM_CR_ENABLE, E_ECAM_CONTROL);
  597. ecam_val = nwl_bridge_readl(pcie, E_ECAM_CONTROL);
  598. ecam_val &= ~E_ECAM_SIZE_LOC;
  599. ecam_val |= NWL_ECAM_MAX_SIZE << E_ECAM_SIZE_SHIFT;
  600. nwl_bridge_writel(pcie, ecam_val, E_ECAM_CONTROL);
  601. nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_ecam_base),
  602. E_ECAM_BASE_LO);
  603. nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_ecam_base),
  604. E_ECAM_BASE_HI);
  605. if (nwl_pcie_link_up(pcie))
  606. dev_info(dev, "Link is UP\n");
  607. else
  608. dev_info(dev, "Link is DOWN\n");
  609. /* Get misc IRQ number */
  610. pcie->irq_misc = platform_get_irq_byname(pdev, "misc");
  611. if (pcie->irq_misc < 0)
  612. return -EINVAL;
  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 INTX interrupts */
  629. nwl_bridge_writel(pcie, (u32)~MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
  630. /* Clear pending INTX interrupts */
  631. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
  632. MSGF_LEG_SR_MASKALL, MSGF_LEG_STATUS);
  633. /* Enable all INTX 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 resource *res;
  645. int i;
  646. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "breg");
  647. pcie->breg_base = devm_ioremap_resource(dev, res);
  648. if (IS_ERR(pcie->breg_base))
  649. return PTR_ERR(pcie->breg_base);
  650. pcie->phys_breg_base = res->start;
  651. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcireg");
  652. pcie->pcireg_base = devm_ioremap_resource(dev, res);
  653. if (IS_ERR(pcie->pcireg_base))
  654. return PTR_ERR(pcie->pcireg_base);
  655. pcie->phys_pcie_reg_base = res->start;
  656. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
  657. pcie->ecam_base = devm_pci_remap_cfg_resource(dev, res);
  658. if (IS_ERR(pcie->ecam_base))
  659. return PTR_ERR(pcie->ecam_base);
  660. pcie->phys_ecam_base = res->start;
  661. /* Get intx IRQ number */
  662. pcie->irq_intx = platform_get_irq_byname(pdev, "intx");
  663. if (pcie->irq_intx < 0)
  664. return pcie->irq_intx;
  665. irq_set_chained_handler_and_data(pcie->irq_intx,
  666. nwl_pcie_leg_handler, pcie);
  667. for (i = 0; i < ARRAY_SIZE(pcie->phy); i++) {
  668. pcie->phy[i] = devm_of_phy_get_by_index(dev, dev->of_node, i);
  669. if (PTR_ERR(pcie->phy[i]) == -ENODEV) {
  670. pcie->phy[i] = NULL;
  671. break;
  672. }
  673. if (IS_ERR(pcie->phy[i]))
  674. return PTR_ERR(pcie->phy[i]);
  675. }
  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_host_bridge *bridge;
  687. int err;
  688. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  689. if (!bridge)
  690. return -ENODEV;
  691. pcie = pci_host_bridge_priv(bridge);
  692. platform_set_drvdata(pdev, pcie);
  693. pcie->dev = dev;
  694. err = nwl_pcie_parse_dt(pcie, pdev);
  695. if (err) {
  696. dev_err(dev, "Parsing DT failed\n");
  697. return err;
  698. }
  699. pcie->clk = devm_clk_get(dev, NULL);
  700. if (IS_ERR(pcie->clk))
  701. return PTR_ERR(pcie->clk);
  702. err = clk_prepare_enable(pcie->clk);
  703. if (err) {
  704. dev_err(dev, "can't enable PCIe ref clock\n");
  705. return err;
  706. }
  707. err = nwl_pcie_phy_enable(pcie);
  708. if (err) {
  709. dev_err(dev, "could not enable PHYs\n");
  710. goto err_clk;
  711. }
  712. err = nwl_pcie_bridge_init(pcie);
  713. if (err) {
  714. dev_err(dev, "HW Initialization failed\n");
  715. goto err_phy;
  716. }
  717. err = nwl_pcie_init_irq_domain(pcie);
  718. if (err) {
  719. dev_err(dev, "Failed creating IRQ Domain\n");
  720. goto err_phy;
  721. }
  722. bridge->sysdata = pcie;
  723. bridge->ops = &nwl_pcie_ops;
  724. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  725. err = nwl_pcie_enable_msi(pcie);
  726. if (err < 0) {
  727. dev_err(dev, "failed to enable MSI support: %d\n", err);
  728. goto err_phy;
  729. }
  730. }
  731. err = pci_host_probe(bridge);
  732. if (!err)
  733. return 0;
  734. err_phy:
  735. nwl_pcie_phy_disable(pcie);
  736. err_clk:
  737. clk_disable_unprepare(pcie->clk);
  738. return err;
  739. }
  740. static void nwl_pcie_remove(struct platform_device *pdev)
  741. {
  742. struct nwl_pcie *pcie = platform_get_drvdata(pdev);
  743. nwl_pcie_phy_disable(pcie);
  744. clk_disable_unprepare(pcie->clk);
  745. }
  746. static struct platform_driver nwl_pcie_driver = {
  747. .driver = {
  748. .name = "nwl-pcie",
  749. .suppress_bind_attrs = true,
  750. .of_match_table = nwl_pcie_of_match,
  751. },
  752. .probe = nwl_pcie_probe,
  753. .remove_new = nwl_pcie_remove,
  754. };
  755. builtin_platform_driver(nwl_pcie_driver);