pcie-xilinx-nwl.c 23 KB

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