irq-imx-mu-msi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Freescale MU used as MSI controller
  4. *
  5. * Copyright (c) 2018 Pengutronix, Oleksij Rempel <[email protected]>
  6. * Copyright 2022 NXP
  7. * Frank Li <[email protected]>
  8. * Peng Fan <[email protected]>
  9. *
  10. * Based on drivers/mailbox/imx-mailbox.c
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/irq.h>
  14. #include <linux/irqchip.h>
  15. #include <linux/irqchip/chained_irq.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/msi.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/pm_domain.h>
  24. #include <linux/spinlock.h>
  25. #define IMX_MU_CHANS 4
  26. enum imx_mu_xcr {
  27. IMX_MU_GIER,
  28. IMX_MU_GCR,
  29. IMX_MU_TCR,
  30. IMX_MU_RCR,
  31. IMX_MU_xCR_MAX,
  32. };
  33. enum imx_mu_xsr {
  34. IMX_MU_SR,
  35. IMX_MU_GSR,
  36. IMX_MU_TSR,
  37. IMX_MU_RSR,
  38. IMX_MU_xSR_MAX
  39. };
  40. enum imx_mu_type {
  41. IMX_MU_V2 = BIT(1),
  42. };
  43. /* Receive Interrupt Enable */
  44. #define IMX_MU_xCR_RIEn(data, x) ((data->cfg->type) & IMX_MU_V2 ? BIT(x) : BIT(24 + (3 - (x))))
  45. #define IMX_MU_xSR_RFn(data, x) ((data->cfg->type) & IMX_MU_V2 ? BIT(x) : BIT(24 + (3 - (x))))
  46. struct imx_mu_dcfg {
  47. enum imx_mu_type type;
  48. u32 xTR; /* Transmit Register0 */
  49. u32 xRR; /* Receive Register0 */
  50. u32 xSR[IMX_MU_xSR_MAX]; /* Status Registers */
  51. u32 xCR[IMX_MU_xCR_MAX]; /* Control Registers */
  52. };
  53. struct imx_mu_msi {
  54. raw_spinlock_t lock;
  55. struct irq_domain *msi_domain;
  56. void __iomem *regs;
  57. phys_addr_t msiir_addr;
  58. const struct imx_mu_dcfg *cfg;
  59. unsigned long used;
  60. struct clk *clk;
  61. };
  62. static void imx_mu_write(struct imx_mu_msi *msi_data, u32 val, u32 offs)
  63. {
  64. iowrite32(val, msi_data->regs + offs);
  65. }
  66. static u32 imx_mu_read(struct imx_mu_msi *msi_data, u32 offs)
  67. {
  68. return ioread32(msi_data->regs + offs);
  69. }
  70. static u32 imx_mu_xcr_rmw(struct imx_mu_msi *msi_data, enum imx_mu_xcr type, u32 set, u32 clr)
  71. {
  72. unsigned long flags;
  73. u32 val;
  74. raw_spin_lock_irqsave(&msi_data->lock, flags);
  75. val = imx_mu_read(msi_data, msi_data->cfg->xCR[type]);
  76. val &= ~clr;
  77. val |= set;
  78. imx_mu_write(msi_data, val, msi_data->cfg->xCR[type]);
  79. raw_spin_unlock_irqrestore(&msi_data->lock, flags);
  80. return val;
  81. }
  82. static void imx_mu_msi_parent_mask_irq(struct irq_data *data)
  83. {
  84. struct imx_mu_msi *msi_data = irq_data_get_irq_chip_data(data);
  85. imx_mu_xcr_rmw(msi_data, IMX_MU_RCR, 0, IMX_MU_xCR_RIEn(msi_data, data->hwirq));
  86. }
  87. static void imx_mu_msi_parent_unmask_irq(struct irq_data *data)
  88. {
  89. struct imx_mu_msi *msi_data = irq_data_get_irq_chip_data(data);
  90. imx_mu_xcr_rmw(msi_data, IMX_MU_RCR, IMX_MU_xCR_RIEn(msi_data, data->hwirq), 0);
  91. }
  92. static void imx_mu_msi_parent_ack_irq(struct irq_data *data)
  93. {
  94. struct imx_mu_msi *msi_data = irq_data_get_irq_chip_data(data);
  95. imx_mu_read(msi_data, msi_data->cfg->xRR + data->hwirq * 4);
  96. }
  97. static struct irq_chip imx_mu_msi_irq_chip = {
  98. .name = "MU-MSI",
  99. .irq_ack = irq_chip_ack_parent,
  100. };
  101. static struct msi_domain_ops imx_mu_msi_irq_ops = {
  102. };
  103. static struct msi_domain_info imx_mu_msi_domain_info = {
  104. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS),
  105. .ops = &imx_mu_msi_irq_ops,
  106. .chip = &imx_mu_msi_irq_chip,
  107. };
  108. static void imx_mu_msi_parent_compose_msg(struct irq_data *data,
  109. struct msi_msg *msg)
  110. {
  111. struct imx_mu_msi *msi_data = irq_data_get_irq_chip_data(data);
  112. u64 addr = msi_data->msiir_addr + 4 * data->hwirq;
  113. msg->address_hi = upper_32_bits(addr);
  114. msg->address_lo = lower_32_bits(addr);
  115. msg->data = data->hwirq;
  116. }
  117. static int imx_mu_msi_parent_set_affinity(struct irq_data *irq_data,
  118. const struct cpumask *mask, bool force)
  119. {
  120. return -EINVAL;
  121. }
  122. static struct irq_chip imx_mu_msi_parent_chip = {
  123. .name = "MU",
  124. .irq_mask = imx_mu_msi_parent_mask_irq,
  125. .irq_unmask = imx_mu_msi_parent_unmask_irq,
  126. .irq_ack = imx_mu_msi_parent_ack_irq,
  127. .irq_compose_msi_msg = imx_mu_msi_parent_compose_msg,
  128. .irq_set_affinity = imx_mu_msi_parent_set_affinity,
  129. };
  130. static int imx_mu_msi_domain_irq_alloc(struct irq_domain *domain,
  131. unsigned int virq,
  132. unsigned int nr_irqs,
  133. void *args)
  134. {
  135. struct imx_mu_msi *msi_data = domain->host_data;
  136. unsigned long flags;
  137. int pos, err = 0;
  138. WARN_ON(nr_irqs != 1);
  139. raw_spin_lock_irqsave(&msi_data->lock, flags);
  140. pos = find_first_zero_bit(&msi_data->used, IMX_MU_CHANS);
  141. if (pos < IMX_MU_CHANS)
  142. __set_bit(pos, &msi_data->used);
  143. else
  144. err = -ENOSPC;
  145. raw_spin_unlock_irqrestore(&msi_data->lock, flags);
  146. if (err)
  147. return err;
  148. irq_domain_set_info(domain, virq, pos,
  149. &imx_mu_msi_parent_chip, msi_data,
  150. handle_edge_irq, NULL, NULL);
  151. return 0;
  152. }
  153. static void imx_mu_msi_domain_irq_free(struct irq_domain *domain,
  154. unsigned int virq, unsigned int nr_irqs)
  155. {
  156. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  157. struct imx_mu_msi *msi_data = irq_data_get_irq_chip_data(d);
  158. unsigned long flags;
  159. raw_spin_lock_irqsave(&msi_data->lock, flags);
  160. __clear_bit(d->hwirq, &msi_data->used);
  161. raw_spin_unlock_irqrestore(&msi_data->lock, flags);
  162. }
  163. static const struct irq_domain_ops imx_mu_msi_domain_ops = {
  164. .alloc = imx_mu_msi_domain_irq_alloc,
  165. .free = imx_mu_msi_domain_irq_free,
  166. };
  167. static void imx_mu_msi_irq_handler(struct irq_desc *desc)
  168. {
  169. struct imx_mu_msi *msi_data = irq_desc_get_handler_data(desc);
  170. struct irq_chip *chip = irq_desc_get_chip(desc);
  171. u32 status;
  172. int i;
  173. status = imx_mu_read(msi_data, msi_data->cfg->xSR[IMX_MU_RSR]);
  174. chained_irq_enter(chip, desc);
  175. for (i = 0; i < IMX_MU_CHANS; i++) {
  176. if (status & IMX_MU_xSR_RFn(msi_data, i))
  177. generic_handle_domain_irq(msi_data->msi_domain, i);
  178. }
  179. chained_irq_exit(chip, desc);
  180. }
  181. static int imx_mu_msi_domains_init(struct imx_mu_msi *msi_data, struct device *dev)
  182. {
  183. struct fwnode_handle *fwnodes = dev_fwnode(dev);
  184. struct irq_domain *parent;
  185. /* Initialize MSI domain parent */
  186. parent = irq_domain_create_linear(fwnodes,
  187. IMX_MU_CHANS,
  188. &imx_mu_msi_domain_ops,
  189. msi_data);
  190. if (!parent) {
  191. dev_err(dev, "failed to create IRQ domain\n");
  192. return -ENOMEM;
  193. }
  194. irq_domain_update_bus_token(parent, DOMAIN_BUS_NEXUS);
  195. msi_data->msi_domain = platform_msi_create_irq_domain(fwnodes,
  196. &imx_mu_msi_domain_info,
  197. parent);
  198. if (!msi_data->msi_domain) {
  199. dev_err(dev, "failed to create MSI domain\n");
  200. irq_domain_remove(parent);
  201. return -ENOMEM;
  202. }
  203. irq_domain_set_pm_device(msi_data->msi_domain, dev);
  204. return 0;
  205. }
  206. /* Register offset of different version MU IP */
  207. static const struct imx_mu_dcfg imx_mu_cfg_imx6sx = {
  208. .type = 0,
  209. .xTR = 0x0,
  210. .xRR = 0x10,
  211. .xSR = {
  212. [IMX_MU_SR] = 0x20,
  213. [IMX_MU_GSR] = 0x20,
  214. [IMX_MU_TSR] = 0x20,
  215. [IMX_MU_RSR] = 0x20,
  216. },
  217. .xCR = {
  218. [IMX_MU_GIER] = 0x24,
  219. [IMX_MU_GCR] = 0x24,
  220. [IMX_MU_TCR] = 0x24,
  221. [IMX_MU_RCR] = 0x24,
  222. },
  223. };
  224. static const struct imx_mu_dcfg imx_mu_cfg_imx7ulp = {
  225. .type = 0,
  226. .xTR = 0x20,
  227. .xRR = 0x40,
  228. .xSR = {
  229. [IMX_MU_SR] = 0x60,
  230. [IMX_MU_GSR] = 0x60,
  231. [IMX_MU_TSR] = 0x60,
  232. [IMX_MU_RSR] = 0x60,
  233. },
  234. .xCR = {
  235. [IMX_MU_GIER] = 0x64,
  236. [IMX_MU_GCR] = 0x64,
  237. [IMX_MU_TCR] = 0x64,
  238. [IMX_MU_RCR] = 0x64,
  239. },
  240. };
  241. static const struct imx_mu_dcfg imx_mu_cfg_imx8ulp = {
  242. .type = IMX_MU_V2,
  243. .xTR = 0x200,
  244. .xRR = 0x280,
  245. .xSR = {
  246. [IMX_MU_SR] = 0xC,
  247. [IMX_MU_GSR] = 0x118,
  248. [IMX_MU_TSR] = 0x124,
  249. [IMX_MU_RSR] = 0x12C,
  250. },
  251. .xCR = {
  252. [IMX_MU_GIER] = 0x110,
  253. [IMX_MU_GCR] = 0x114,
  254. [IMX_MU_TCR] = 0x120,
  255. [IMX_MU_RCR] = 0x128
  256. },
  257. };
  258. static int __init imx_mu_of_init(struct device_node *dn,
  259. struct device_node *parent,
  260. const struct imx_mu_dcfg *cfg)
  261. {
  262. struct platform_device *pdev = of_find_device_by_node(dn);
  263. struct device_link *pd_link_a;
  264. struct device_link *pd_link_b;
  265. struct imx_mu_msi *msi_data;
  266. struct resource *res;
  267. struct device *pd_a;
  268. struct device *pd_b;
  269. struct device *dev;
  270. int ret;
  271. int irq;
  272. dev = &pdev->dev;
  273. msi_data = devm_kzalloc(&pdev->dev, sizeof(*msi_data), GFP_KERNEL);
  274. if (!msi_data)
  275. return -ENOMEM;
  276. msi_data->cfg = cfg;
  277. msi_data->regs = devm_platform_ioremap_resource_byname(pdev, "processor-a-side");
  278. if (IS_ERR(msi_data->regs)) {
  279. dev_err(&pdev->dev, "failed to initialize 'regs'\n");
  280. return PTR_ERR(msi_data->regs);
  281. }
  282. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "processor-b-side");
  283. if (!res)
  284. return -EIO;
  285. msi_data->msiir_addr = res->start + msi_data->cfg->xTR;
  286. irq = platform_get_irq(pdev, 0);
  287. if (irq <= 0)
  288. return -ENODEV;
  289. platform_set_drvdata(pdev, msi_data);
  290. msi_data->clk = devm_clk_get(dev, NULL);
  291. if (IS_ERR(msi_data->clk))
  292. return PTR_ERR(msi_data->clk);
  293. pd_a = dev_pm_domain_attach_by_name(dev, "processor-a-side");
  294. if (IS_ERR(pd_a))
  295. return PTR_ERR(pd_a);
  296. pd_b = dev_pm_domain_attach_by_name(dev, "processor-b-side");
  297. if (IS_ERR(pd_b))
  298. return PTR_ERR(pd_b);
  299. pd_link_a = device_link_add(dev, pd_a,
  300. DL_FLAG_STATELESS |
  301. DL_FLAG_PM_RUNTIME |
  302. DL_FLAG_RPM_ACTIVE);
  303. if (!pd_link_a) {
  304. dev_err(dev, "Failed to add device_link to mu a.\n");
  305. goto err_pd_a;
  306. }
  307. pd_link_b = device_link_add(dev, pd_b,
  308. DL_FLAG_STATELESS |
  309. DL_FLAG_PM_RUNTIME |
  310. DL_FLAG_RPM_ACTIVE);
  311. if (!pd_link_b) {
  312. dev_err(dev, "Failed to add device_link to mu a.\n");
  313. goto err_pd_b;
  314. }
  315. ret = imx_mu_msi_domains_init(msi_data, dev);
  316. if (ret)
  317. goto err_dm_init;
  318. pm_runtime_enable(dev);
  319. irq_set_chained_handler_and_data(irq,
  320. imx_mu_msi_irq_handler,
  321. msi_data);
  322. return 0;
  323. err_dm_init:
  324. device_link_remove(dev, pd_b);
  325. err_pd_b:
  326. device_link_remove(dev, pd_a);
  327. err_pd_a:
  328. return -EINVAL;
  329. }
  330. static int __maybe_unused imx_mu_runtime_suspend(struct device *dev)
  331. {
  332. struct imx_mu_msi *priv = dev_get_drvdata(dev);
  333. clk_disable_unprepare(priv->clk);
  334. return 0;
  335. }
  336. static int __maybe_unused imx_mu_runtime_resume(struct device *dev)
  337. {
  338. struct imx_mu_msi *priv = dev_get_drvdata(dev);
  339. int ret;
  340. ret = clk_prepare_enable(priv->clk);
  341. if (ret)
  342. dev_err(dev, "failed to enable clock\n");
  343. return ret;
  344. }
  345. static const struct dev_pm_ops imx_mu_pm_ops = {
  346. SET_RUNTIME_PM_OPS(imx_mu_runtime_suspend,
  347. imx_mu_runtime_resume, NULL)
  348. };
  349. static int __init imx_mu_imx7ulp_of_init(struct device_node *dn,
  350. struct device_node *parent)
  351. {
  352. return imx_mu_of_init(dn, parent, &imx_mu_cfg_imx7ulp);
  353. }
  354. static int __init imx_mu_imx6sx_of_init(struct device_node *dn,
  355. struct device_node *parent)
  356. {
  357. return imx_mu_of_init(dn, parent, &imx_mu_cfg_imx6sx);
  358. }
  359. static int __init imx_mu_imx8ulp_of_init(struct device_node *dn,
  360. struct device_node *parent)
  361. {
  362. return imx_mu_of_init(dn, parent, &imx_mu_cfg_imx8ulp);
  363. }
  364. IRQCHIP_PLATFORM_DRIVER_BEGIN(imx_mu_msi)
  365. IRQCHIP_MATCH("fsl,imx7ulp-mu-msi", imx_mu_imx7ulp_of_init)
  366. IRQCHIP_MATCH("fsl,imx6sx-mu-msi", imx_mu_imx6sx_of_init)
  367. IRQCHIP_MATCH("fsl,imx8ulp-mu-msi", imx_mu_imx8ulp_of_init)
  368. IRQCHIP_PLATFORM_DRIVER_END(imx_mu_msi, .pm = &imx_mu_pm_ops)
  369. MODULE_AUTHOR("Frank Li <[email protected]>");
  370. MODULE_DESCRIPTION("Freescale MU MSI controller driver");
  371. MODULE_LICENSE("GPL");