light-mailbox.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 Alibaba Group Holding Limited.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/io.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mailbox_controller.h>
  10. #include <linux/module.h>
  11. #include <linux/of_device.h>
  12. #include <linux/slab.h>
  13. /* Status Register */
  14. #define LIGHT_MBOX_STA 0x0
  15. #define LIGHT_MBOX_CLR 0x4
  16. #define LIGHT_MBOX_MASK 0xc
  17. /* Transmit/receive data register:
  18. * INFO0 ~ INFO6
  19. */
  20. #define LIGHT_MBOX_INFO_NUM 8
  21. #define LIGHT_MBOX_DATA_INFO_NUM 7
  22. #define LIGHT_MBOX_INFO0 0x14
  23. /* Transmit ack register: INFO7 */
  24. #define LIGHT_MBOX_INFO7 0x30
  25. /* Generate remote icu IRQ Register */
  26. #define LIGHT_MBOX_GEN 0x10
  27. #define LIGHT_MBOX_GEN_RX_DATA BIT(6)
  28. #define LIGHT_MBOX_GEN_TX_ACK BIT(7)
  29. #define LIGHT_MBOX_CHAN_RES_SIZE 0x1000
  30. #define LIGHT_MBOX_CHANS 4
  31. #define LIGHT_MBOX_CHAN_NAME_SIZE 20
  32. #define LIGHT_MBOX_ACK_MAGIC 0xdeadbeaf
  33. enum light_mbox_chan_type {
  34. LIGHT_MBOX_TYPE_TXRX, /* Tx & Rx chan */
  35. LIGHT_MBOX_TYPE_DB, /* Tx & Rx doorbell */
  36. };
  37. enum light_mbox_icu_cpu_id {
  38. LIGHT_MBOX_ICU_CPU0, /* 910T */
  39. LIGHT_MBOX_ICU_CPU1, /* 902 */
  40. LIGHT_MBOX_ICU_CPU2, /* 906 */
  41. LIGHT_MBOX_ICU_CPU3, /* 910R */
  42. };
  43. struct light_mbox_con_priv {
  44. enum light_mbox_icu_cpu_id idx;
  45. enum light_mbox_chan_type type;
  46. void __iomem *comm_local_base;
  47. void __iomem *comm_remote_base;
  48. char irq_desc[LIGHT_MBOX_CHAN_NAME_SIZE];
  49. struct mbox_chan *chan;
  50. struct tasklet_struct txdb_tasklet;
  51. };
  52. struct light_mbox_priv {
  53. struct device *dev;
  54. void __iomem *local_icu[LIGHT_MBOX_CHANS];
  55. void __iomem *remote_icu[LIGHT_MBOX_CHANS - 1];
  56. void __iomem *cur_cpu_ch_base;
  57. enum light_mbox_icu_cpu_id cur_icu_cpu_id;
  58. spinlock_t mbox_lock; /* control register lock */
  59. struct mbox_controller mbox;
  60. struct mbox_chan mbox_chans[LIGHT_MBOX_CHANS];
  61. struct light_mbox_con_priv con_priv[LIGHT_MBOX_CHANS];
  62. struct clk *clk;
  63. int irq;
  64. };
  65. static struct light_mbox_priv *to_light_mbox_priv(struct mbox_controller *mbox)
  66. {
  67. return container_of(mbox, struct light_mbox_priv, mbox);
  68. }
  69. static void light_mbox_write(struct light_mbox_priv *priv, u32 val, u32 offs)
  70. {
  71. iowrite32(val, priv->cur_cpu_ch_base + offs);
  72. }
  73. static u32 light_mbox_read(struct light_mbox_priv *priv, u32 offs)
  74. {
  75. return ioread32(priv->cur_cpu_ch_base + offs);
  76. }
  77. static u32 light_mbox_rmw(struct light_mbox_priv *priv, u32 off, u32 set, u32 clr)
  78. {
  79. unsigned long flags;
  80. u32 val;
  81. spin_lock_irqsave(&priv->mbox_lock, flags);
  82. val = light_mbox_read(priv, off);
  83. val &= ~clr;
  84. val |= set;
  85. light_mbox_write(priv, val, off);
  86. spin_unlock_irqrestore(&priv->mbox_lock, flags);
  87. return val;
  88. }
  89. static void light_mbox_chan_write(struct light_mbox_con_priv *cp, u32 val, u32 offs, bool is_remote)
  90. {
  91. if (is_remote)
  92. iowrite32(val, cp->comm_remote_base + offs);
  93. else
  94. iowrite32(val, cp->comm_local_base + offs);
  95. }
  96. static u32 light_mbox_chan_read(struct light_mbox_con_priv *cp, u32 offs, bool is_remote)
  97. {
  98. if (is_remote)
  99. return ioread32(cp->comm_remote_base + offs);
  100. else
  101. return ioread32(cp->comm_local_base + offs);
  102. }
  103. static void light_mbox_chan_rmw(struct light_mbox_con_priv *cp, u32 off, u32 set, u32 clr, bool is_remote)
  104. {
  105. struct light_mbox_priv *priv = to_light_mbox_priv(cp->chan->mbox);
  106. unsigned long flags;
  107. u32 val;
  108. spin_lock_irqsave(&priv->mbox_lock, flags);
  109. val = light_mbox_chan_read(cp, off, is_remote);
  110. val &= ~clr;
  111. val |= set;
  112. light_mbox_chan_write(cp, val, off, is_remote);
  113. spin_unlock_irqrestore(&priv->mbox_lock, flags);
  114. }
  115. static void light_mbox_chan_rd_data(struct light_mbox_con_priv *cp, void *data, bool is_remote)
  116. {
  117. u32 off = LIGHT_MBOX_INFO0;
  118. u32 *arg = data;
  119. u32 i;
  120. /* read info0 ~ info6, totally 28 bytes
  121. * requires data memory size is 28 bytes
  122. */
  123. for (i = 0; i < LIGHT_MBOX_DATA_INFO_NUM; i++) {
  124. *arg = light_mbox_chan_read(cp, off, is_remote);
  125. off += 4;
  126. arg++;
  127. }
  128. }
  129. static void light_mbox_chan_wr_data(struct light_mbox_con_priv *cp, void *data, bool is_remote)
  130. {
  131. u32 off = LIGHT_MBOX_INFO0;
  132. u32 *arg = data;
  133. u32 i;
  134. /* write info0 ~ info6, totally 28 bytes
  135. * requires data memory is 28 bytes valid data
  136. */
  137. for (i = 0; i < LIGHT_MBOX_DATA_INFO_NUM; i++) {
  138. light_mbox_chan_write(cp, *arg, off, is_remote);
  139. off += 4;
  140. arg++;
  141. }
  142. }
  143. static void light_mbox_chan_wr_ack(struct light_mbox_con_priv *cp, void *data, bool is_remote)
  144. {
  145. u32 off = LIGHT_MBOX_INFO7;
  146. u32 *arg = data;
  147. light_mbox_chan_write(cp, *arg, off, is_remote);
  148. }
  149. static int light_mbox_chan_id_to_mapbit(struct light_mbox_con_priv *cp)
  150. {
  151. struct light_mbox_priv *priv = to_light_mbox_priv(cp->chan->mbox);
  152. int mapbit = 0;
  153. int i;
  154. for (i = 0; i < LIGHT_MBOX_CHANS; i++) {
  155. if (i == cp->idx)
  156. return mapbit;
  157. if (i != priv->cur_icu_cpu_id)
  158. mapbit++;
  159. }
  160. if (i == LIGHT_MBOX_CHANS)
  161. dev_err(cp->chan->mbox->dev, "convert to mapbit failed\n");
  162. return 0;
  163. }
  164. static void light_mbox_txdb_tasklet(unsigned long data)
  165. {
  166. struct light_mbox_con_priv *cp = (struct light_mbox_con_priv *)data;
  167. mbox_chan_txdone(cp->chan, 0);
  168. }
  169. static irqreturn_t light_mbox_isr(int irq, void *p)
  170. {
  171. struct mbox_chan *chan = p;
  172. struct light_mbox_priv *priv = to_light_mbox_priv(chan->mbox);
  173. struct light_mbox_con_priv *cp = chan->con_priv;
  174. int mapbit = light_mbox_chan_id_to_mapbit(cp);
  175. u32 sta, dat[LIGHT_MBOX_DATA_INFO_NUM];
  176. u32 ack_magic = LIGHT_MBOX_ACK_MAGIC;
  177. u32 info0_data, info7_data;
  178. sta = light_mbox_read(priv, LIGHT_MBOX_STA);
  179. if (!(sta & BIT(mapbit)))
  180. return IRQ_NONE;
  181. /* clear chan irq bit in STA register */
  182. light_mbox_rmw(priv, LIGHT_MBOX_CLR, BIT(mapbit), 0);
  183. /* rx doorbell */
  184. if (cp->type == LIGHT_MBOX_TYPE_DB) {
  185. mbox_chan_received_data(cp->chan, NULL);
  186. return IRQ_HANDLED;
  187. }
  188. /* info0 is the protocol word, shoud not be zero! */
  189. info0_data = light_mbox_chan_read(cp, LIGHT_MBOX_INFO0, false);
  190. if (info0_data) {
  191. /* read info0~info6 data */
  192. light_mbox_chan_rd_data(cp, dat, false);
  193. /* clear local info0 */
  194. light_mbox_chan_write(cp, 0x0, LIGHT_MBOX_INFO0, false);
  195. /* notify remote cpu */
  196. light_mbox_chan_wr_ack(cp, &ack_magic, true);
  197. /* CPU1 902/906 use polling mode to monitor info7 */
  198. if (cp->idx != LIGHT_MBOX_ICU_CPU1 && cp->idx != LIGHT_MBOX_ICU_CPU2)
  199. light_mbox_chan_rmw(cp, LIGHT_MBOX_GEN, LIGHT_MBOX_GEN_TX_ACK, 0, true);
  200. /* transfer the data to client */
  201. mbox_chan_received_data(chan, (void *)dat);
  202. }
  203. /* info7 magic value mean the real ack signal, not generate bit7 */
  204. info7_data = light_mbox_chan_read(cp, LIGHT_MBOX_INFO7, false);
  205. if (info7_data == LIGHT_MBOX_ACK_MAGIC) {
  206. /* clear local info7 */
  207. light_mbox_chan_write(cp, 0x0, LIGHT_MBOX_INFO7, false);
  208. /* notify framework the last TX has completed */
  209. mbox_chan_txdone(chan, 0);
  210. }
  211. if (!info0_data && !info7_data)
  212. return IRQ_NONE;
  213. return IRQ_HANDLED;
  214. }
  215. static int light_mbox_send_data(struct mbox_chan *chan, void *data)
  216. {
  217. struct light_mbox_con_priv *cp = chan->con_priv;
  218. if (cp->type == LIGHT_MBOX_TYPE_DB)
  219. tasklet_schedule(&cp->txdb_tasklet);
  220. else
  221. light_mbox_chan_wr_data(cp, data, true);
  222. light_mbox_chan_rmw(cp, LIGHT_MBOX_GEN, LIGHT_MBOX_GEN_RX_DATA, 0, true);
  223. return 0;
  224. }
  225. static int light_mbox_startup(struct mbox_chan *chan)
  226. {
  227. struct light_mbox_priv *priv = to_light_mbox_priv(chan->mbox);
  228. struct light_mbox_con_priv *cp = chan->con_priv;
  229. u32 data[8] = {0};
  230. int mask_bit;
  231. int ret;
  232. /* clear local and remote generate and info0~info7 */
  233. light_mbox_chan_rmw(cp, LIGHT_MBOX_GEN, 0x0, 0xff, true);
  234. light_mbox_chan_rmw(cp, LIGHT_MBOX_GEN, 0x0, 0xff, false);
  235. light_mbox_chan_wr_ack(cp, &data[7], true);
  236. light_mbox_chan_wr_ack(cp, &data[7], false);
  237. light_mbox_chan_wr_data(cp, &data[0], true);
  238. light_mbox_chan_wr_data(cp, &data[0], false);
  239. /* enable the chan mask */
  240. mask_bit = light_mbox_chan_id_to_mapbit(cp);
  241. light_mbox_rmw(priv, LIGHT_MBOX_MASK, BIT(mask_bit), 0);
  242. if (cp->type == LIGHT_MBOX_TYPE_DB)
  243. /* tx doorbell doesn't have ACK, rx doorbell requires isr */
  244. tasklet_init(&cp->txdb_tasklet, light_mbox_txdb_tasklet,
  245. (unsigned long)cp);
  246. ret = request_irq(priv->irq, light_mbox_isr, IRQF_SHARED |
  247. IRQF_NO_SUSPEND, cp->irq_desc, chan);
  248. if (ret) {
  249. dev_err(priv->dev,
  250. "Unable to acquire IRQ %d\n", priv->irq);
  251. return ret;
  252. }
  253. return 0;
  254. }
  255. static void light_mbox_shutdown(struct mbox_chan *chan)
  256. {
  257. struct light_mbox_priv *priv = to_light_mbox_priv(chan->mbox);
  258. struct light_mbox_con_priv *cp = chan->con_priv;
  259. int mask_bit;
  260. /* clear the chan mask */
  261. mask_bit = light_mbox_chan_id_to_mapbit(cp);
  262. light_mbox_rmw(priv, LIGHT_MBOX_MASK, 0, BIT(mask_bit));
  263. free_irq(priv->irq, chan);
  264. }
  265. static const struct mbox_chan_ops light_mbox_ops = {
  266. .send_data = light_mbox_send_data,
  267. .startup = light_mbox_startup,
  268. .shutdown = light_mbox_shutdown,
  269. };
  270. static void light_mbox_init_generic(struct light_mbox_priv *priv)
  271. {
  272. /* Set default configuration */
  273. light_mbox_write(priv, 0xff, LIGHT_MBOX_CLR);
  274. light_mbox_write(priv, 0x0, LIGHT_MBOX_MASK);
  275. }
  276. static struct mbox_chan *light_mbox_xlate(struct mbox_controller *mbox,
  277. const struct of_phandle_args *sp)
  278. {
  279. struct light_mbox_priv *priv = to_light_mbox_priv(mbox);
  280. struct light_mbox_con_priv *cp;
  281. u32 chan, type;
  282. if (sp->args_count != 2) {
  283. dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count);
  284. return ERR_PTR(-EINVAL);
  285. }
  286. chan = sp->args[0]; /* comm remote channel */
  287. type = sp->args[1]; /* comm channel type */
  288. if (chan >= mbox->num_chans) {
  289. dev_err(mbox->dev, "Not supported channel number: %d\n", chan);
  290. return ERR_PTR(-EINVAL);
  291. }
  292. if (chan == priv->cur_icu_cpu_id) {
  293. dev_err(mbox->dev, "Cannot communicate with yourself\n");
  294. return ERR_PTR(-EINVAL);
  295. }
  296. if (type > LIGHT_MBOX_TYPE_DB) {
  297. dev_err(mbox->dev, "Not supported the type for channel[%d]\n", chan);
  298. return ERR_PTR(-EINVAL);
  299. }
  300. cp = mbox->chans[chan].con_priv;
  301. cp->type = type;
  302. return &mbox->chans[chan];
  303. }
  304. static int light_mbox_probe(struct platform_device *pdev)
  305. {
  306. struct device *dev = &pdev->dev;
  307. struct device_node *np = dev->of_node;
  308. struct light_mbox_priv *priv;
  309. struct resource *res;
  310. unsigned int remote_idx = 0;
  311. unsigned int i;
  312. int ret;
  313. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  314. if (!priv)
  315. return -ENOMEM;
  316. if (of_property_read_u32(np, "icu_cpu_id", &priv->cur_icu_cpu_id)) {
  317. dev_err(dev, "icu_cpu_id is missing\n");
  318. return -EINVAL;
  319. }
  320. if (priv->cur_icu_cpu_id != LIGHT_MBOX_ICU_CPU0 &&
  321. priv->cur_icu_cpu_id != LIGHT_MBOX_ICU_CPU3) {
  322. dev_err(dev, "icu_cpu_id is invalid\n");
  323. return -EINVAL;
  324. }
  325. priv->dev = dev;
  326. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "local_base");
  327. priv->local_icu[LIGHT_MBOX_ICU_CPU0] = devm_ioremap_resource(dev, res);
  328. if (IS_ERR(priv->local_icu[LIGHT_MBOX_ICU_CPU0]))
  329. return PTR_ERR(priv->local_icu[LIGHT_MBOX_ICU_CPU0]);
  330. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "remote_icu0");
  331. priv->remote_icu[0] = devm_ioremap_resource(dev, res);
  332. if (IS_ERR(priv->remote_icu[0]))
  333. return PTR_ERR(priv->remote_icu[0]);
  334. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "remote_icu1");
  335. priv->remote_icu[1] = devm_ioremap_resource(dev, res);
  336. if (IS_ERR(priv->remote_icu[1]))
  337. return PTR_ERR(priv->remote_icu[1]);
  338. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "remote_icu2");
  339. priv->remote_icu[2] = devm_ioremap_resource(dev, res);
  340. if (IS_ERR(priv->remote_icu[2]))
  341. return PTR_ERR(priv->remote_icu[2]);
  342. priv->local_icu[LIGHT_MBOX_ICU_CPU1] = priv->local_icu[LIGHT_MBOX_ICU_CPU0] +
  343. LIGHT_MBOX_CHAN_RES_SIZE;
  344. priv->local_icu[LIGHT_MBOX_ICU_CPU2] = priv->local_icu[LIGHT_MBOX_ICU_CPU1] +
  345. LIGHT_MBOX_CHAN_RES_SIZE;
  346. priv->local_icu[LIGHT_MBOX_ICU_CPU3] = priv->local_icu[LIGHT_MBOX_ICU_CPU2] +
  347. LIGHT_MBOX_CHAN_RES_SIZE;
  348. priv->cur_cpu_ch_base = priv->local_icu[priv->cur_icu_cpu_id];
  349. priv->irq = platform_get_irq(pdev, 0);
  350. if (priv->irq < 0)
  351. return priv->irq;
  352. priv->clk = devm_clk_get(dev, NULL);
  353. if (IS_ERR(priv->clk)) {
  354. if (PTR_ERR(priv->clk) != -ENOENT)
  355. return PTR_ERR(priv->clk);
  356. priv->clk = NULL;
  357. }
  358. ret = clk_prepare_enable(priv->clk);
  359. if (ret) {
  360. dev_err(dev, "Failed to enable clock\n");
  361. return ret;
  362. }
  363. /* init the chans */
  364. for (i = 0; i < LIGHT_MBOX_CHANS; i++) {
  365. struct light_mbox_con_priv *cp = &priv->con_priv[i];
  366. cp->idx = i;
  367. cp->chan = &priv->mbox_chans[i];
  368. priv->mbox_chans[i].con_priv = cp;
  369. snprintf(cp->irq_desc, sizeof(cp->irq_desc),
  370. "light_mbox_chan[%i]", cp->idx);
  371. cp->comm_local_base = priv->local_icu[i];
  372. if (i != priv->cur_icu_cpu_id) {
  373. cp->comm_remote_base = priv->remote_icu[remote_idx];
  374. remote_idx++;
  375. }
  376. }
  377. spin_lock_init(&priv->mbox_lock);
  378. priv->mbox.dev = dev;
  379. priv->mbox.ops = &light_mbox_ops;
  380. priv->mbox.chans = priv->mbox_chans;
  381. priv->mbox.num_chans = LIGHT_MBOX_CHANS;
  382. priv->mbox.of_xlate = light_mbox_xlate;
  383. priv->mbox.txdone_irq = true;
  384. platform_set_drvdata(pdev, priv);
  385. light_mbox_init_generic(priv);
  386. return devm_mbox_controller_register(dev, &priv->mbox);
  387. }
  388. static int light_mbox_remove(struct platform_device *pdev)
  389. {
  390. struct light_mbox_priv *priv = platform_get_drvdata(pdev);
  391. clk_disable_unprepare(priv->clk);
  392. return 0;
  393. }
  394. static const struct of_device_id light_mbox_dt_ids[] = {
  395. { .compatible = "thead,light-mbox" },
  396. { },
  397. };
  398. MODULE_DEVICE_TABLE(of, light_mbox_dt_ids);
  399. static struct platform_driver light_mbox_driver = {
  400. .probe = light_mbox_probe,
  401. .remove = light_mbox_remove,
  402. .driver = {
  403. .name = "light_mbox",
  404. .of_match_table = light_mbox_dt_ids,
  405. },
  406. };
  407. module_platform_driver(light_mbox_driver);
  408. MODULE_AUTHOR("fugang.duan <duanfugang.dfg@linux.alibaba.com>");
  409. MODULE_DESCRIPTION("Thead Light mailbox IPC driver");
  410. MODULE_LICENSE("GPL v2");