pcie-xilinx-nwl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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. #define INTX_NUM 4
  122. /* Readin the PS_LINKUP */
  123. #define PS_LINKUP_OFFSET 0x00000238
  124. #define PCIE_PHY_LINKUP_BIT BIT(0)
  125. #define PHY_RDY_LINKUP_BIT BIT(1)
  126. /* Parameters for the waiting for link up routine */
  127. #define LINK_WAIT_MAX_RETRIES 10
  128. #define LINK_WAIT_USLEEP_MIN 90000
  129. #define LINK_WAIT_USLEEP_MAX 100000
  130. struct nwl_msi { /* MSI information */
  131. struct irq_domain *msi_domain;
  132. unsigned long *bitmap;
  133. struct irq_domain *dev_domain;
  134. struct mutex lock; /* protect bitmap variable */
  135. int irq_msi0;
  136. int irq_msi1;
  137. };
  138. struct nwl_pcie {
  139. struct device *dev;
  140. void __iomem *breg_base;
  141. void __iomem *pcireg_base;
  142. void __iomem *ecam_base;
  143. phys_addr_t phys_breg_base; /* Physical Bridge Register Base */
  144. phys_addr_t phys_pcie_reg_base; /* Physical PCIe Controller Base */
  145. phys_addr_t phys_ecam_base; /* Physical Configuration Base */
  146. u32 breg_size;
  147. u32 pcie_reg_size;
  148. u32 ecam_size;
  149. int irq_intx;
  150. int irq_misc;
  151. u32 ecam_value;
  152. u8 last_busno;
  153. u8 root_busno;
  154. struct nwl_msi msi;
  155. struct irq_domain *legacy_irq_domain;
  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, INTX_NUM) {
  282. virq = irq_find_mapping(pcie->legacy_irq_domain,
  283. bit + 1);
  284. if (virq)
  285. generic_handle_irq(virq);
  286. }
  287. }
  288. chained_irq_exit(chip, desc);
  289. }
  290. static void nwl_pcie_handle_msi_irq(struct nwl_pcie *pcie, u32 status_reg)
  291. {
  292. struct nwl_msi *msi;
  293. unsigned long status;
  294. u32 bit;
  295. u32 virq;
  296. msi = &pcie->msi;
  297. while ((status = nwl_bridge_readl(pcie, status_reg)) != 0) {
  298. for_each_set_bit(bit, &status, 32) {
  299. nwl_bridge_writel(pcie, 1 << bit, status_reg);
  300. virq = irq_find_mapping(msi->dev_domain, bit);
  301. if (virq)
  302. generic_handle_irq(virq);
  303. }
  304. }
  305. }
  306. static void nwl_pcie_msi_handler_high(struct irq_desc *desc)
  307. {
  308. struct irq_chip *chip = irq_desc_get_chip(desc);
  309. struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
  310. chained_irq_enter(chip, desc);
  311. nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_HI);
  312. chained_irq_exit(chip, desc);
  313. }
  314. static void nwl_pcie_msi_handler_low(struct irq_desc *desc)
  315. {
  316. struct irq_chip *chip = irq_desc_get_chip(desc);
  317. struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
  318. chained_irq_enter(chip, desc);
  319. nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_LO);
  320. chained_irq_exit(chip, desc);
  321. }
  322. static int nwl_legacy_map(struct irq_domain *domain, unsigned int irq,
  323. irq_hw_number_t hwirq)
  324. {
  325. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  326. irq_set_chip_data(irq, domain->host_data);
  327. return 0;
  328. }
  329. static const struct irq_domain_ops legacy_domain_ops = {
  330. .map = nwl_legacy_map,
  331. };
  332. #ifdef CONFIG_PCI_MSI
  333. static struct irq_chip nwl_msi_irq_chip = {
  334. .name = "nwl_pcie:msi",
  335. .irq_enable = unmask_msi_irq,
  336. .irq_disable = mask_msi_irq,
  337. .irq_mask = mask_msi_irq,
  338. .irq_unmask = unmask_msi_irq,
  339. };
  340. static struct msi_domain_info nwl_msi_domain_info = {
  341. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  342. MSI_FLAG_MULTI_PCI_MSI),
  343. .chip = &nwl_msi_irq_chip,
  344. };
  345. #endif
  346. static void nwl_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  347. {
  348. struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
  349. phys_addr_t msi_addr = pcie->phys_pcie_reg_base;
  350. msg->address_lo = lower_32_bits(msi_addr);
  351. msg->address_hi = upper_32_bits(msi_addr);
  352. msg->data = data->hwirq;
  353. }
  354. static int nwl_msi_set_affinity(struct irq_data *irq_data,
  355. const struct cpumask *mask, bool force)
  356. {
  357. return -EINVAL;
  358. }
  359. static struct irq_chip nwl_irq_chip = {
  360. .name = "Xilinx MSI",
  361. .irq_compose_msi_msg = nwl_compose_msi_msg,
  362. .irq_set_affinity = nwl_msi_set_affinity,
  363. };
  364. static int nwl_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  365. unsigned int nr_irqs, void *args)
  366. {
  367. struct nwl_pcie *pcie = domain->host_data;
  368. struct nwl_msi *msi = &pcie->msi;
  369. int bit;
  370. int i;
  371. mutex_lock(&msi->lock);
  372. bit = bitmap_find_next_zero_area(msi->bitmap, INT_PCI_MSI_NR, 0,
  373. nr_irqs, 0);
  374. if (bit >= INT_PCI_MSI_NR) {
  375. mutex_unlock(&msi->lock);
  376. return -ENOSPC;
  377. }
  378. bitmap_set(msi->bitmap, bit, nr_irqs);
  379. for (i = 0; i < nr_irqs; i++) {
  380. irq_domain_set_info(domain, virq + i, bit + i, &nwl_irq_chip,
  381. domain->host_data, handle_simple_irq,
  382. NULL, NULL);
  383. }
  384. mutex_unlock(&msi->lock);
  385. return 0;
  386. }
  387. static void nwl_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  388. unsigned int nr_irqs)
  389. {
  390. struct irq_data *data = irq_domain_get_irq_data(domain, virq);
  391. struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
  392. struct nwl_msi *msi = &pcie->msi;
  393. mutex_lock(&msi->lock);
  394. bitmap_clear(msi->bitmap, data->hwirq, nr_irqs);
  395. mutex_unlock(&msi->lock);
  396. }
  397. static const struct irq_domain_ops dev_msi_domain_ops = {
  398. .alloc = nwl_irq_domain_alloc,
  399. .free = nwl_irq_domain_free,
  400. };
  401. static int nwl_pcie_init_msi_irq_domain(struct nwl_pcie *pcie)
  402. {
  403. #ifdef CONFIG_PCI_MSI
  404. struct device *dev = pcie->dev;
  405. struct fwnode_handle *fwnode = of_node_to_fwnode(dev->of_node);
  406. struct nwl_msi *msi = &pcie->msi;
  407. msi->dev_domain = irq_domain_add_linear(NULL, INT_PCI_MSI_NR,
  408. &dev_msi_domain_ops, pcie);
  409. if (!msi->dev_domain) {
  410. dev_err(dev, "failed to create dev IRQ domain\n");
  411. return -ENOMEM;
  412. }
  413. msi->msi_domain = pci_msi_create_irq_domain(fwnode,
  414. &nwl_msi_domain_info,
  415. msi->dev_domain);
  416. if (!msi->msi_domain) {
  417. dev_err(dev, "failed to create msi IRQ domain\n");
  418. irq_domain_remove(msi->dev_domain);
  419. return -ENOMEM;
  420. }
  421. #endif
  422. return 0;
  423. }
  424. static int nwl_pcie_init_irq_domain(struct nwl_pcie *pcie)
  425. {
  426. struct device *dev = pcie->dev;
  427. struct device_node *node = dev->of_node;
  428. struct device_node *legacy_intc_node;
  429. legacy_intc_node = of_get_next_child(node, NULL);
  430. if (!legacy_intc_node) {
  431. dev_err(dev, "No legacy intc node found\n");
  432. return -EINVAL;
  433. }
  434. pcie->legacy_irq_domain = irq_domain_add_linear(legacy_intc_node,
  435. INTX_NUM,
  436. &legacy_domain_ops,
  437. pcie);
  438. if (!pcie->legacy_irq_domain) {
  439. dev_err(dev, "failed to create IRQ domain\n");
  440. return -ENOMEM;
  441. }
  442. nwl_pcie_init_msi_irq_domain(pcie);
  443. return 0;
  444. }
  445. static int nwl_pcie_enable_msi(struct nwl_pcie *pcie, struct pci_bus *bus)
  446. {
  447. struct device *dev = pcie->dev;
  448. struct platform_device *pdev = to_platform_device(dev);
  449. struct nwl_msi *msi = &pcie->msi;
  450. unsigned long base;
  451. int ret;
  452. int size = BITS_TO_LONGS(INT_PCI_MSI_NR) * sizeof(long);
  453. mutex_init(&msi->lock);
  454. msi->bitmap = kzalloc(size, GFP_KERNEL);
  455. if (!msi->bitmap)
  456. return -ENOMEM;
  457. /* Get msi_1 IRQ number */
  458. msi->irq_msi1 = platform_get_irq_byname(pdev, "msi1");
  459. if (msi->irq_msi1 < 0) {
  460. dev_err(dev, "failed to get IRQ#%d\n", msi->irq_msi1);
  461. ret = -EINVAL;
  462. goto err;
  463. }
  464. irq_set_chained_handler_and_data(msi->irq_msi1,
  465. nwl_pcie_msi_handler_high, pcie);
  466. /* Get msi_0 IRQ number */
  467. msi->irq_msi0 = platform_get_irq_byname(pdev, "msi0");
  468. if (msi->irq_msi0 < 0) {
  469. dev_err(dev, "failed to get IRQ#%d\n", msi->irq_msi0);
  470. ret = -EINVAL;
  471. goto err;
  472. }
  473. irq_set_chained_handler_and_data(msi->irq_msi0,
  474. nwl_pcie_msi_handler_low, pcie);
  475. /* Check for msii_present bit */
  476. ret = nwl_bridge_readl(pcie, I_MSII_CAPABILITIES) & MSII_PRESENT;
  477. if (!ret) {
  478. dev_err(dev, "MSI not present\n");
  479. ret = -EIO;
  480. goto err;
  481. }
  482. /* Enable MSII */
  483. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
  484. MSII_ENABLE, I_MSII_CONTROL);
  485. /* Enable MSII status */
  486. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
  487. MSII_STATUS_ENABLE, I_MSII_CONTROL);
  488. /* setup AFI/FPCI range */
  489. base = pcie->phys_pcie_reg_base;
  490. nwl_bridge_writel(pcie, lower_32_bits(base), I_MSII_BASE_LO);
  491. nwl_bridge_writel(pcie, upper_32_bits(base), I_MSII_BASE_HI);
  492. /*
  493. * For high range MSI interrupts: disable, clear any pending,
  494. * and enable
  495. */
  496. nwl_bridge_writel(pcie, (u32)~MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
  497. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_HI) &
  498. MSGF_MSI_SR_HI_MASK, MSGF_MSI_STATUS_HI);
  499. nwl_bridge_writel(pcie, MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
  500. /*
  501. * For low range MSI interrupts: disable, clear any pending,
  502. * and enable
  503. */
  504. nwl_bridge_writel(pcie, (u32)~MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
  505. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_LO) &
  506. MSGF_MSI_SR_LO_MASK, MSGF_MSI_STATUS_LO);
  507. nwl_bridge_writel(pcie, MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
  508. return 0;
  509. err:
  510. kfree(msi->bitmap);
  511. msi->bitmap = NULL;
  512. return ret;
  513. }
  514. static int nwl_pcie_bridge_init(struct nwl_pcie *pcie)
  515. {
  516. struct device *dev = pcie->dev;
  517. struct platform_device *pdev = to_platform_device(dev);
  518. u32 breg_val, ecam_val, first_busno = 0;
  519. int err;
  520. breg_val = nwl_bridge_readl(pcie, E_BREG_CAPABILITIES) & BREG_PRESENT;
  521. if (!breg_val) {
  522. dev_err(dev, "BREG is not present\n");
  523. return breg_val;
  524. }
  525. /* Write bridge_off to breg base */
  526. nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_breg_base),
  527. E_BREG_BASE_LO);
  528. nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_breg_base),
  529. E_BREG_BASE_HI);
  530. /* Enable BREG */
  531. nwl_bridge_writel(pcie, ~BREG_ENABLE_FORCE & BREG_ENABLE,
  532. E_BREG_CONTROL);
  533. /* Disable DMA channel registers */
  534. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_PCIE_RX0) |
  535. CFG_DMA_REG_BAR, BRCFG_PCIE_RX0);
  536. /* Enable Ingress subtractive decode translation */
  537. nwl_bridge_writel(pcie, SET_ISUB_CONTROL, I_ISUB_CONTROL);
  538. /* Enable msg filtering details */
  539. nwl_bridge_writel(pcie, CFG_ENABLE_MSG_FILTER_MASK,
  540. BRCFG_PCIE_RX_MSG_FILTER);
  541. err = nwl_wait_for_link(pcie);
  542. if (err)
  543. return err;
  544. ecam_val = nwl_bridge_readl(pcie, E_ECAM_CAPABILITIES) & E_ECAM_PRESENT;
  545. if (!ecam_val) {
  546. dev_err(dev, "ECAM is not present\n");
  547. return ecam_val;
  548. }
  549. /* Enable ECAM */
  550. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
  551. E_ECAM_CR_ENABLE, E_ECAM_CONTROL);
  552. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
  553. (pcie->ecam_value << E_ECAM_SIZE_SHIFT),
  554. E_ECAM_CONTROL);
  555. nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_ecam_base),
  556. E_ECAM_BASE_LO);
  557. nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_ecam_base),
  558. E_ECAM_BASE_HI);
  559. /* Get bus range */
  560. ecam_val = nwl_bridge_readl(pcie, E_ECAM_CONTROL);
  561. pcie->last_busno = (ecam_val & E_ECAM_SIZE_LOC) >> E_ECAM_SIZE_SHIFT;
  562. /* Write primary, secondary and subordinate bus numbers */
  563. ecam_val = first_busno;
  564. ecam_val |= (first_busno + 1) << 8;
  565. ecam_val |= (pcie->last_busno << E_ECAM_SIZE_SHIFT);
  566. writel(ecam_val, (pcie->ecam_base + PCI_PRIMARY_BUS));
  567. if (nwl_pcie_link_up(pcie))
  568. dev_info(dev, "Link is UP\n");
  569. else
  570. dev_info(dev, "Link is DOWN\n");
  571. /* Get misc IRQ number */
  572. pcie->irq_misc = platform_get_irq_byname(pdev, "misc");
  573. if (pcie->irq_misc < 0) {
  574. dev_err(dev, "failed to get misc IRQ %d\n",
  575. pcie->irq_misc);
  576. return -EINVAL;
  577. }
  578. err = devm_request_irq(dev, pcie->irq_misc,
  579. nwl_pcie_misc_handler, IRQF_SHARED,
  580. "nwl_pcie:misc", pcie);
  581. if (err) {
  582. dev_err(dev, "fail to register misc IRQ#%d\n",
  583. pcie->irq_misc);
  584. return err;
  585. }
  586. /* Disable all misc interrupts */
  587. nwl_bridge_writel(pcie, (u32)~MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
  588. /* Clear pending misc interrupts */
  589. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
  590. MSGF_MISC_SR_MASKALL, MSGF_MISC_STATUS);
  591. /* Enable all misc interrupts */
  592. nwl_bridge_writel(pcie, MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
  593. /* Disable all legacy interrupts */
  594. nwl_bridge_writel(pcie, (u32)~MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
  595. /* Clear pending legacy interrupts */
  596. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
  597. MSGF_LEG_SR_MASKALL, MSGF_LEG_STATUS);
  598. /* Enable all legacy interrupts */
  599. nwl_bridge_writel(pcie, MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
  600. /* Enable the bridge config interrupt */
  601. nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_INTERRUPT) |
  602. BRCFG_INTERRUPT_MASK, BRCFG_INTERRUPT);
  603. return 0;
  604. }
  605. static int nwl_pcie_parse_dt(struct nwl_pcie *pcie,
  606. struct platform_device *pdev)
  607. {
  608. struct device *dev = pcie->dev;
  609. struct device_node *node = dev->of_node;
  610. struct resource *res;
  611. const char *type;
  612. /* Check for device type */
  613. type = of_get_property(node, "device_type", NULL);
  614. if (!type || strcmp(type, "pci")) {
  615. dev_err(dev, "invalid \"device_type\" %s\n", type);
  616. return -EINVAL;
  617. }
  618. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "breg");
  619. pcie->breg_base = devm_ioremap_resource(dev, res);
  620. if (IS_ERR(pcie->breg_base))
  621. return PTR_ERR(pcie->breg_base);
  622. pcie->phys_breg_base = res->start;
  623. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcireg");
  624. pcie->pcireg_base = devm_ioremap_resource(dev, res);
  625. if (IS_ERR(pcie->pcireg_base))
  626. return PTR_ERR(pcie->pcireg_base);
  627. pcie->phys_pcie_reg_base = res->start;
  628. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
  629. pcie->ecam_base = devm_pci_remap_cfg_resource(dev, res);
  630. if (IS_ERR(pcie->ecam_base))
  631. return PTR_ERR(pcie->ecam_base);
  632. pcie->phys_ecam_base = res->start;
  633. /* Get intx IRQ number */
  634. pcie->irq_intx = platform_get_irq_byname(pdev, "intx");
  635. if (pcie->irq_intx < 0) {
  636. dev_err(dev, "failed to get intx IRQ %d\n", pcie->irq_intx);
  637. return -EINVAL;
  638. }
  639. irq_set_chained_handler_and_data(pcie->irq_intx,
  640. nwl_pcie_leg_handler, pcie);
  641. return 0;
  642. }
  643. static const struct of_device_id nwl_pcie_of_match[] = {
  644. { .compatible = "xlnx,nwl-pcie-2.11", },
  645. {}
  646. };
  647. static int nwl_pcie_probe(struct platform_device *pdev)
  648. {
  649. struct device *dev = &pdev->dev;
  650. struct device_node *node = dev->of_node;
  651. struct nwl_pcie *pcie;
  652. struct pci_bus *bus;
  653. struct pci_bus *child;
  654. int err;
  655. resource_size_t iobase = 0;
  656. LIST_HEAD(res);
  657. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  658. if (!pcie)
  659. return -ENOMEM;
  660. pcie->dev = dev;
  661. pcie->ecam_value = NWL_ECAM_VALUE_DEFAULT;
  662. err = nwl_pcie_parse_dt(pcie, pdev);
  663. if (err) {
  664. dev_err(dev, "Parsing DT failed\n");
  665. return err;
  666. }
  667. err = nwl_pcie_bridge_init(pcie);
  668. if (err) {
  669. dev_err(dev, "HW Initialization failed\n");
  670. return err;
  671. }
  672. err = of_pci_get_host_bridge_resources(node, 0, 0xff, &res, &iobase);
  673. if (err) {
  674. dev_err(dev, "Getting bridge resources failed\n");
  675. return err;
  676. }
  677. err = devm_request_pci_bus_resources(dev, &res);
  678. if (err)
  679. goto error;
  680. err = nwl_pcie_init_irq_domain(pcie);
  681. if (err) {
  682. dev_err(dev, "Failed creating IRQ Domain\n");
  683. goto error;
  684. }
  685. bus = pci_create_root_bus(dev, pcie->root_busno,
  686. &nwl_pcie_ops, pcie, &res);
  687. if (!bus) {
  688. err = -ENOMEM;
  689. goto error;
  690. }
  691. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  692. err = nwl_pcie_enable_msi(pcie, bus);
  693. if (err < 0) {
  694. dev_err(dev, "failed to enable MSI support: %d\n", err);
  695. goto error;
  696. }
  697. }
  698. pci_scan_child_bus(bus);
  699. pci_assign_unassigned_bus_resources(bus);
  700. list_for_each_entry(child, &bus->children, node)
  701. pcie_bus_configure_settings(child);
  702. pci_bus_add_devices(bus);
  703. return 0;
  704. error:
  705. pci_free_resource_list(&res);
  706. return err;
  707. }
  708. static struct platform_driver nwl_pcie_driver = {
  709. .driver = {
  710. .name = "nwl-pcie",
  711. .suppress_bind_attrs = true,
  712. .of_match_table = nwl_pcie_of_match,
  713. },
  714. .probe = nwl_pcie_probe,
  715. };
  716. builtin_platform_driver(nwl_pcie_driver);