pcie-xilinx-nwl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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/irqchip/irq-msi-lib.h>
  13. #include <linux/irqdomain.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/msi.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_pci.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/pci.h>
  21. #include <linux/pci-ecam.h>
  22. #include <linux/phy/phy.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/irqchip/chained_irq.h>
  25. #include "../pci.h"
  26. /* Bridge core config registers */
  27. #define BRCFG_PCIE_RX0 0x00000000
  28. #define BRCFG_PCIE_RX1 0x00000004
  29. #define BRCFG_INTERRUPT 0x00000010
  30. #define BRCFG_PCIE_RX_MSG_FILTER 0x00000020
  31. /* Egress - Bridge translation registers */
  32. #define E_BREG_CAPABILITIES 0x00000200
  33. #define E_BREG_CONTROL 0x00000208
  34. #define E_BREG_BASE_LO 0x00000210
  35. #define E_BREG_BASE_HI 0x00000214
  36. #define E_ECAM_CAPABILITIES 0x00000220
  37. #define E_ECAM_CONTROL 0x00000228
  38. #define E_ECAM_BASE_LO 0x00000230
  39. #define E_ECAM_BASE_HI 0x00000234
  40. /* Ingress - address translations */
  41. #define I_MSII_CAPABILITIES 0x00000300
  42. #define I_MSII_CONTROL 0x00000308
  43. #define I_MSII_BASE_LO 0x00000310
  44. #define I_MSII_BASE_HI 0x00000314
  45. #define I_ISUB_CONTROL 0x000003E8
  46. #define SET_ISUB_CONTROL BIT(0)
  47. /* Rxed msg fifo - Interrupt status registers */
  48. #define MSGF_MISC_STATUS 0x00000400
  49. #define MSGF_MISC_MASK 0x00000404
  50. #define MSGF_LEG_STATUS 0x00000420
  51. #define MSGF_LEG_MASK 0x00000424
  52. #define MSGF_MSI_STATUS_LO 0x00000440
  53. #define MSGF_MSI_STATUS_HI 0x00000444
  54. #define MSGF_MSI_MASK_LO 0x00000448
  55. #define MSGF_MSI_MASK_HI 0x0000044C
  56. /* Msg filter mask bits */
  57. #define CFG_ENABLE_PM_MSG_FWD BIT(1)
  58. #define CFG_ENABLE_INT_MSG_FWD BIT(2)
  59. #define CFG_ENABLE_ERR_MSG_FWD BIT(3)
  60. #define CFG_ENABLE_MSG_FILTER_MASK (CFG_ENABLE_PM_MSG_FWD | \
  61. CFG_ENABLE_INT_MSG_FWD | \
  62. CFG_ENABLE_ERR_MSG_FWD)
  63. /* Misc interrupt status mask bits */
  64. #define MSGF_MISC_SR_RXMSG_AVAIL BIT(0)
  65. #define MSGF_MISC_SR_RXMSG_OVER BIT(1)
  66. #define MSGF_MISC_SR_SLAVE_ERR BIT(4)
  67. #define MSGF_MISC_SR_MASTER_ERR BIT(5)
  68. #define MSGF_MISC_SR_I_ADDR_ERR BIT(6)
  69. #define MSGF_MISC_SR_E_ADDR_ERR BIT(7)
  70. #define MSGF_MISC_SR_FATAL_AER BIT(16)
  71. #define MSGF_MISC_SR_NON_FATAL_AER BIT(17)
  72. #define MSGF_MISC_SR_CORR_AER BIT(18)
  73. #define MSGF_MISC_SR_UR_DETECT BIT(20)
  74. #define MSGF_MISC_SR_NON_FATAL_DEV BIT(22)
  75. #define MSGF_MISC_SR_FATAL_DEV BIT(23)
  76. #define MSGF_MISC_SR_LINK_DOWN BIT(24)
  77. #define MSGF_MISC_SR_LINK_AUTO_BWIDTH BIT(25)
  78. #define MSGF_MISC_SR_LINK_BWIDTH BIT(26)
  79. #define MSGF_MISC_SR_MASKALL (MSGF_MISC_SR_RXMSG_AVAIL | \
  80. MSGF_MISC_SR_RXMSG_OVER | \
  81. MSGF_MISC_SR_SLAVE_ERR | \
  82. MSGF_MISC_SR_MASTER_ERR | \
  83. MSGF_MISC_SR_I_ADDR_ERR | \
  84. MSGF_MISC_SR_E_ADDR_ERR | \
  85. MSGF_MISC_SR_FATAL_AER | \
  86. MSGF_MISC_SR_NON_FATAL_AER | \
  87. MSGF_MISC_SR_CORR_AER | \
  88. MSGF_MISC_SR_UR_DETECT | \
  89. MSGF_MISC_SR_NON_FATAL_DEV | \
  90. MSGF_MISC_SR_FATAL_DEV | \
  91. MSGF_MISC_SR_LINK_DOWN | \
  92. MSGF_MISC_SR_LINK_AUTO_BWIDTH | \
  93. MSGF_MISC_SR_LINK_BWIDTH)
  94. /* Legacy interrupt status mask bits */
  95. #define MSGF_LEG_SR_INTA BIT(0)
  96. #define MSGF_LEG_SR_INTB BIT(1)
  97. #define MSGF_LEG_SR_INTC BIT(2)
  98. #define MSGF_LEG_SR_INTD BIT(3)
  99. #define MSGF_LEG_SR_MASKALL (MSGF_LEG_SR_INTA | MSGF_LEG_SR_INTB | \
  100. MSGF_LEG_SR_INTC | MSGF_LEG_SR_INTD)
  101. /* MSI interrupt status mask bits */
  102. #define MSGF_MSI_SR_LO_MASK GENMASK(31, 0)
  103. #define MSGF_MSI_SR_HI_MASK GENMASK(31, 0)
  104. #define MSII_PRESENT BIT(0)
  105. #define MSII_ENABLE BIT(0)
  106. #define MSII_STATUS_ENABLE BIT(15)
  107. /* Bridge config interrupt mask */
  108. #define BRCFG_INTERRUPT_MASK BIT(0)
  109. #define BREG_PRESENT BIT(0)
  110. #define BREG_ENABLE BIT(0)
  111. #define BREG_ENABLE_FORCE BIT(1)
  112. /* E_ECAM status mask bits */
  113. #define E_ECAM_PRESENT BIT(0)
  114. #define E_ECAM_CR_ENABLE BIT(0)
  115. #define E_ECAM_SIZE_LOC GENMASK(20, 16)
  116. #define E_ECAM_SIZE_SHIFT 16
  117. #define NWL_ECAM_MAX_SIZE 16
  118. #define CFG_DMA_REG_BAR GENMASK(2, 0)
  119. #define CFG_PCIE_CACHE GENMASK(7, 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. 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. #define NWL_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
  352. MSI_FLAG_USE_DEF_CHIP_OPS | \
  353. MSI_FLAG_NO_AFFINITY)
  354. #define NWL_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK | \
  355. MSI_FLAG_MULTI_PCI_MSI)
  356. static const struct msi_parent_ops nwl_msi_parent_ops = {
  357. .required_flags = NWL_MSI_FLAGS_REQUIRED,
  358. .supported_flags = NWL_MSI_FLAGS_SUPPORTED,
  359. .bus_select_token = DOMAIN_BUS_PCI_MSI,
  360. .prefix = "nwl-",
  361. .init_dev_msi_info = msi_lib_init_dev_msi_info,
  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 nwl_msi *msi = &pcie->msi;
  418. struct irq_domain_info info = {
  419. .fwnode = dev_fwnode(dev),
  420. .ops = &dev_msi_domain_ops,
  421. .host_data = pcie,
  422. .size = INT_PCI_MSI_NR,
  423. };
  424. msi->dev_domain = msi_create_parent_irq_domain(&info, &nwl_msi_parent_ops);
  425. if (!msi->dev_domain) {
  426. dev_err(dev, "failed to create dev IRQ domain\n");
  427. return -ENOMEM;
  428. }
  429. #endif
  430. return 0;
  431. }
  432. static void nwl_pcie_phy_power_off(struct nwl_pcie *pcie, int i)
  433. {
  434. int err = phy_power_off(pcie->phy[i]);
  435. if (err)
  436. dev_err(pcie->dev, "could not power off phy %d (err=%d)\n", i,
  437. err);
  438. }
  439. static void nwl_pcie_phy_exit(struct nwl_pcie *pcie, int i)
  440. {
  441. int err = phy_exit(pcie->phy[i]);
  442. if (err)
  443. dev_err(pcie->dev, "could not exit phy %d (err=%d)\n", i, err);
  444. }
  445. static int nwl_pcie_phy_enable(struct nwl_pcie *pcie)
  446. {
  447. int i, ret;
  448. for (i = 0; i < ARRAY_SIZE(pcie->phy); i++) {
  449. ret = phy_init(pcie->phy[i]);
  450. if (ret)
  451. goto err;
  452. ret = phy_power_on(pcie->phy[i]);
  453. if (ret) {
  454. nwl_pcie_phy_exit(pcie, i);
  455. goto err;
  456. }
  457. }
  458. return 0;
  459. err:
  460. while (i--) {
  461. nwl_pcie_phy_power_off(pcie, i);
  462. nwl_pcie_phy_exit(pcie, i);
  463. }
  464. return ret;
  465. }
  466. static void nwl_pcie_phy_disable(struct nwl_pcie *pcie)
  467. {
  468. int i;
  469. for (i = ARRAY_SIZE(pcie->phy); i--;) {
  470. nwl_pcie_phy_power_off(pcie, i);
  471. nwl_pcie_phy_exit(pcie, i);
  472. }
  473. }
  474. static int nwl_pcie_init_irq_domain(struct nwl_pcie *pcie)
  475. {
  476. struct device *dev = pcie->dev;
  477. struct device_node *node = dev->of_node;
  478. struct device_node *intc_node;
  479. intc_node = of_get_next_child(node, NULL);
  480. if (!intc_node) {
  481. dev_err(dev, "No legacy intc node found\n");
  482. return -EINVAL;
  483. }
  484. pcie->intx_irq_domain = irq_domain_create_linear(of_fwnode_handle(intc_node), PCI_NUM_INTX,
  485. &intx_domain_ops, pcie);
  486. of_node_put(intc_node);
  487. if (!pcie->intx_irq_domain) {
  488. dev_err(dev, "failed to create IRQ domain\n");
  489. return -ENOMEM;
  490. }
  491. raw_spin_lock_init(&pcie->leg_mask_lock);
  492. nwl_pcie_init_msi_irq_domain(pcie);
  493. return 0;
  494. }
  495. static int nwl_pcie_enable_msi(struct nwl_pcie *pcie)
  496. {
  497. struct device *dev = pcie->dev;
  498. struct platform_device *pdev = to_platform_device(dev);
  499. struct nwl_msi *msi = &pcie->msi;
  500. unsigned long base;
  501. int ret;
  502. mutex_init(&msi->lock);
  503. /* Get msi_1 IRQ number */
  504. msi->irq_msi1 = platform_get_irq_byname(pdev, "msi1");
  505. if (msi->irq_msi1 < 0)
  506. return -EINVAL;
  507. irq_set_chained_handler_and_data(msi->irq_msi1,
  508. nwl_pcie_msi_handler_high, pcie);
  509. /* Get msi_0 IRQ number */
  510. msi->irq_msi0 = platform_get_irq_byname(pdev, "msi0");
  511. if (msi->irq_msi0 < 0)
  512. return -EINVAL;
  513. irq_set_chained_handler_and_data(msi->irq_msi0,
  514. nwl_pcie_msi_handler_low, pcie);
  515. /* Check for msii_present bit */
  516. ret = nwl_bridge_readl(pcie, I_MSII_CAPABILITIES) & MSII_PRESENT;
  517. if (!ret) {
  518. dev_err(dev, "MSI not present\n");
  519. return -EIO;
  520. }
  521. /* Enable MSII */
  522. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
  523. MSII_ENABLE, I_MSII_CONTROL);
  524. /* Enable MSII status */
  525. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
  526. MSII_STATUS_ENABLE, I_MSII_CONTROL);
  527. /* setup AFI/FPCI range */
  528. base = pcie->phys_pcie_reg_base;
  529. nwl_bridge_writel(pcie, lower_32_bits(base), I_MSII_BASE_LO);
  530. nwl_bridge_writel(pcie, upper_32_bits(base), I_MSII_BASE_HI);
  531. /*
  532. * For high range MSI interrupts: disable, clear any pending,
  533. * and enable
  534. */
  535. nwl_bridge_writel(pcie, 0, MSGF_MSI_MASK_HI);
  536. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_HI) &
  537. MSGF_MSI_SR_HI_MASK, MSGF_MSI_STATUS_HI);
  538. nwl_bridge_writel(pcie, MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
  539. /*
  540. * For low range MSI interrupts: disable, clear any pending,
  541. * and enable
  542. */
  543. nwl_bridge_writel(pcie, 0, MSGF_MSI_MASK_LO);
  544. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_LO) &
  545. MSGF_MSI_SR_LO_MASK, MSGF_MSI_STATUS_LO);
  546. nwl_bridge_writel(pcie, MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
  547. return 0;
  548. }
  549. static int nwl_pcie_bridge_init(struct nwl_pcie *pcie)
  550. {
  551. struct device *dev = pcie->dev;
  552. struct platform_device *pdev = to_platform_device(dev);
  553. u32 breg_val, ecam_val;
  554. int err;
  555. breg_val = nwl_bridge_readl(pcie, E_BREG_CAPABILITIES) & BREG_PRESENT;
  556. if (!breg_val) {
  557. dev_err(dev, "BREG is not present\n");
  558. return breg_val;
  559. }
  560. /* Write bridge_off to breg base */
  561. nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_breg_base),
  562. E_BREG_BASE_LO);
  563. nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_breg_base),
  564. E_BREG_BASE_HI);
  565. /* Enable BREG */
  566. nwl_bridge_writel(pcie, ~BREG_ENABLE_FORCE & BREG_ENABLE,
  567. E_BREG_CONTROL);
  568. /* Disable DMA channel registers */
  569. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_PCIE_RX0) |
  570. CFG_DMA_REG_BAR, BRCFG_PCIE_RX0);
  571. /* Enable Ingress subtractive decode translation */
  572. nwl_bridge_writel(pcie, SET_ISUB_CONTROL, I_ISUB_CONTROL);
  573. /* Enable msg filtering details */
  574. nwl_bridge_writel(pcie, CFG_ENABLE_MSG_FILTER_MASK,
  575. BRCFG_PCIE_RX_MSG_FILTER);
  576. /* This routes the PCIe DMA traffic to go through CCI path */
  577. if (of_dma_is_coherent(dev->of_node))
  578. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_PCIE_RX1) |
  579. CFG_PCIE_CACHE, BRCFG_PCIE_RX1);
  580. err = nwl_wait_for_link(pcie);
  581. if (err)
  582. return err;
  583. ecam_val = nwl_bridge_readl(pcie, E_ECAM_CAPABILITIES) & E_ECAM_PRESENT;
  584. if (!ecam_val) {
  585. dev_err(dev, "ECAM is not present\n");
  586. return ecam_val;
  587. }
  588. /* Enable ECAM */
  589. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
  590. E_ECAM_CR_ENABLE, E_ECAM_CONTROL);
  591. ecam_val = nwl_bridge_readl(pcie, E_ECAM_CONTROL);
  592. ecam_val &= ~E_ECAM_SIZE_LOC;
  593. ecam_val |= NWL_ECAM_MAX_SIZE << E_ECAM_SIZE_SHIFT;
  594. nwl_bridge_writel(pcie, ecam_val, E_ECAM_CONTROL);
  595. nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_ecam_base),
  596. E_ECAM_BASE_LO);
  597. nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_ecam_base),
  598. E_ECAM_BASE_HI);
  599. if (nwl_pcie_link_up(pcie))
  600. dev_info(dev, "Link is UP\n");
  601. else
  602. dev_info(dev, "Link is DOWN\n");
  603. /* Get misc IRQ number */
  604. pcie->irq_misc = platform_get_irq_byname(pdev, "misc");
  605. if (pcie->irq_misc < 0)
  606. return -EINVAL;
  607. err = devm_request_irq(dev, pcie->irq_misc,
  608. nwl_pcie_misc_handler, IRQF_SHARED,
  609. "nwl_pcie:misc", pcie);
  610. if (err) {
  611. dev_err(dev, "fail to register misc IRQ#%d\n",
  612. pcie->irq_misc);
  613. return err;
  614. }
  615. /* Disable all misc interrupts */
  616. nwl_bridge_writel(pcie, (u32)~MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
  617. /* Clear pending misc interrupts */
  618. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
  619. MSGF_MISC_SR_MASKALL, MSGF_MISC_STATUS);
  620. /* Enable all misc interrupts */
  621. nwl_bridge_writel(pcie, MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
  622. /* Disable all INTX interrupts */
  623. nwl_bridge_writel(pcie, (u32)~MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
  624. /* Clear pending INTX interrupts */
  625. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
  626. MSGF_LEG_SR_MASKALL, MSGF_LEG_STATUS);
  627. /* Enable all INTX interrupts */
  628. nwl_bridge_writel(pcie, MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
  629. /* Enable the bridge config interrupt */
  630. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_INTERRUPT) |
  631. BRCFG_INTERRUPT_MASK, BRCFG_INTERRUPT);
  632. return 0;
  633. }
  634. static int nwl_pcie_parse_dt(struct nwl_pcie *pcie,
  635. struct platform_device *pdev)
  636. {
  637. struct device *dev = pcie->dev;
  638. struct resource *res;
  639. int i;
  640. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "breg");
  641. pcie->breg_base = devm_ioremap_resource(dev, res);
  642. if (IS_ERR(pcie->breg_base))
  643. return PTR_ERR(pcie->breg_base);
  644. pcie->phys_breg_base = res->start;
  645. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcireg");
  646. pcie->pcireg_base = devm_ioremap_resource(dev, res);
  647. if (IS_ERR(pcie->pcireg_base))
  648. return PTR_ERR(pcie->pcireg_base);
  649. pcie->phys_pcie_reg_base = res->start;
  650. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
  651. pcie->ecam_base = devm_pci_remap_cfg_resource(dev, res);
  652. if (IS_ERR(pcie->ecam_base))
  653. return PTR_ERR(pcie->ecam_base);
  654. pcie->phys_ecam_base = res->start;
  655. /* Get intx IRQ number */
  656. pcie->irq_intx = platform_get_irq_byname(pdev, "intx");
  657. if (pcie->irq_intx < 0)
  658. return pcie->irq_intx;
  659. irq_set_chained_handler_and_data(pcie->irq_intx,
  660. nwl_pcie_leg_handler, pcie);
  661. for (i = 0; i < ARRAY_SIZE(pcie->phy); i++) {
  662. pcie->phy[i] = devm_of_phy_get_by_index(dev, dev->of_node, i);
  663. if (PTR_ERR(pcie->phy[i]) == -ENODEV) {
  664. pcie->phy[i] = NULL;
  665. break;
  666. }
  667. if (IS_ERR(pcie->phy[i]))
  668. return PTR_ERR(pcie->phy[i]);
  669. }
  670. return 0;
  671. }
  672. static const struct of_device_id nwl_pcie_of_match[] = {
  673. { .compatible = "xlnx,nwl-pcie-2.11", },
  674. {}
  675. };
  676. static int nwl_pcie_probe(struct platform_device *pdev)
  677. {
  678. struct device *dev = &pdev->dev;
  679. struct nwl_pcie *pcie;
  680. struct pci_host_bridge *bridge;
  681. int err;
  682. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  683. if (!bridge)
  684. return -ENODEV;
  685. pcie = pci_host_bridge_priv(bridge);
  686. platform_set_drvdata(pdev, pcie);
  687. pcie->dev = dev;
  688. err = nwl_pcie_parse_dt(pcie, pdev);
  689. if (err) {
  690. dev_err(dev, "Parsing DT failed\n");
  691. return err;
  692. }
  693. pcie->clk = devm_clk_get(dev, NULL);
  694. if (IS_ERR(pcie->clk))
  695. return PTR_ERR(pcie->clk);
  696. err = clk_prepare_enable(pcie->clk);
  697. if (err) {
  698. dev_err(dev, "can't enable PCIe ref clock\n");
  699. return err;
  700. }
  701. err = nwl_pcie_phy_enable(pcie);
  702. if (err) {
  703. dev_err(dev, "could not enable PHYs\n");
  704. goto err_clk;
  705. }
  706. err = nwl_pcie_bridge_init(pcie);
  707. if (err) {
  708. dev_err(dev, "HW Initialization failed\n");
  709. goto err_phy;
  710. }
  711. err = nwl_pcie_init_irq_domain(pcie);
  712. if (err) {
  713. dev_err(dev, "Failed creating IRQ Domain\n");
  714. goto err_phy;
  715. }
  716. bridge->sysdata = pcie;
  717. bridge->ops = &nwl_pcie_ops;
  718. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  719. err = nwl_pcie_enable_msi(pcie);
  720. if (err < 0) {
  721. dev_err(dev, "failed to enable MSI support: %d\n", err);
  722. goto err_phy;
  723. }
  724. }
  725. err = pci_host_probe(bridge);
  726. if (!err)
  727. return 0;
  728. err_phy:
  729. nwl_pcie_phy_disable(pcie);
  730. err_clk:
  731. clk_disable_unprepare(pcie->clk);
  732. return err;
  733. }
  734. static void nwl_pcie_remove(struct platform_device *pdev)
  735. {
  736. struct nwl_pcie *pcie = platform_get_drvdata(pdev);
  737. nwl_pcie_phy_disable(pcie);
  738. clk_disable_unprepare(pcie->clk);
  739. }
  740. static struct platform_driver nwl_pcie_driver = {
  741. .driver = {
  742. .name = "nwl-pcie",
  743. .suppress_bind_attrs = true,
  744. .of_match_table = nwl_pcie_of_match,
  745. },
  746. .probe = nwl_pcie_probe,
  747. .remove = nwl_pcie_remove,
  748. };
  749. builtin_platform_driver(nwl_pcie_driver);