mailbox-sti.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * STi Mailbox
  3. *
  4. * Copyright (C) 2015 ST Microelectronics
  5. *
  6. * Author: Lee Jones <lee.jones@linaro.org> for ST Microelectronics
  7. *
  8. * Based on the original driver written by;
  9. * Alexandre Torgue, Olivier Lebreton and Loic Pallardy
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mailbox_controller.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include "mailbox.h"
  27. #define STI_MBOX_INST_MAX 4 /* RAM saving: Max supported instances */
  28. #define STI_MBOX_CHAN_MAX 20 /* RAM saving: Max supported channels */
  29. #define STI_IRQ_VAL_OFFSET 0x04 /* Read interrupt status */
  30. #define STI_IRQ_SET_OFFSET 0x24 /* Generate a Tx channel interrupt */
  31. #define STI_IRQ_CLR_OFFSET 0x44 /* Clear pending Rx interrupts */
  32. #define STI_ENA_VAL_OFFSET 0x64 /* Read enable status */
  33. #define STI_ENA_SET_OFFSET 0x84 /* Enable a channel */
  34. #define STI_ENA_CLR_OFFSET 0xa4 /* Disable a channel */
  35. #define MBOX_BASE(mdev, inst) ((mdev)->base + ((inst) * 4))
  36. /**
  37. * STi Mailbox device data
  38. *
  39. * An IP Mailbox is currently composed of 4 instances
  40. * Each instance is currently composed of 32 channels
  41. * This means that we have 128 channels per Mailbox
  42. * A channel an be used for TX or RX
  43. *
  44. * @dev: Device to which it is attached
  45. * @mbox: Representation of a communication channel controller
  46. * @base: Base address of the register mapping region
  47. * @name: Name of the mailbox
  48. * @enabled: Local copy of enabled channels
  49. * @lock: Mutex protecting enabled status
  50. */
  51. struct sti_mbox_device {
  52. struct device *dev;
  53. struct mbox_controller *mbox;
  54. void __iomem *base;
  55. const char *name;
  56. u32 enabled[STI_MBOX_INST_MAX];
  57. spinlock_t lock;
  58. };
  59. /**
  60. * STi Mailbox platform specific configuration
  61. *
  62. * @num_inst: Maximum number of instances in one HW Mailbox
  63. * @num_chan: Maximum number of channel per instance
  64. */
  65. struct sti_mbox_pdata {
  66. unsigned int num_inst;
  67. unsigned int num_chan;
  68. };
  69. /**
  70. * STi Mailbox allocated channel information
  71. *
  72. * @mdev: Pointer to parent Mailbox device
  73. * @instance: Instance number channel resides in
  74. * @channel: Channel number pertaining to this container
  75. */
  76. struct sti_channel {
  77. struct sti_mbox_device *mdev;
  78. unsigned int instance;
  79. unsigned int channel;
  80. };
  81. static inline bool sti_mbox_channel_is_enabled(struct mbox_chan *chan)
  82. {
  83. struct sti_channel *chan_info = chan->con_priv;
  84. struct sti_mbox_device *mdev = chan_info->mdev;
  85. unsigned int instance = chan_info->instance;
  86. unsigned int channel = chan_info->channel;
  87. return mdev->enabled[instance] & BIT(channel);
  88. }
  89. static inline
  90. struct mbox_chan *sti_mbox_to_channel(struct mbox_controller *mbox,
  91. unsigned int instance,
  92. unsigned int channel)
  93. {
  94. struct sti_channel *chan_info;
  95. int i;
  96. for (i = 0; i < mbox->num_chans; i++) {
  97. chan_info = mbox->chans[i].con_priv;
  98. if (chan_info &&
  99. chan_info->instance == instance &&
  100. chan_info->channel == channel)
  101. return &mbox->chans[i];
  102. }
  103. dev_err(mbox->dev,
  104. "Channel not registered: instance: %d channel: %d\n",
  105. instance, channel);
  106. return NULL;
  107. }
  108. static void sti_mbox_enable_channel(struct mbox_chan *chan)
  109. {
  110. struct sti_channel *chan_info = chan->con_priv;
  111. struct sti_mbox_device *mdev = chan_info->mdev;
  112. unsigned int instance = chan_info->instance;
  113. unsigned int channel = chan_info->channel;
  114. unsigned long flags;
  115. void __iomem *base = MBOX_BASE(mdev, instance);
  116. spin_lock_irqsave(&mdev->lock, flags);
  117. mdev->enabled[instance] |= BIT(channel);
  118. writel_relaxed(BIT(channel), base + STI_ENA_SET_OFFSET);
  119. spin_unlock_irqrestore(&mdev->lock, flags);
  120. }
  121. static void sti_mbox_disable_channel(struct mbox_chan *chan)
  122. {
  123. struct sti_channel *chan_info = chan->con_priv;
  124. struct sti_mbox_device *mdev = chan_info->mdev;
  125. unsigned int instance = chan_info->instance;
  126. unsigned int channel = chan_info->channel;
  127. unsigned long flags;
  128. void __iomem *base = MBOX_BASE(mdev, instance);
  129. spin_lock_irqsave(&mdev->lock, flags);
  130. mdev->enabled[instance] &= ~BIT(channel);
  131. writel_relaxed(BIT(channel), base + STI_ENA_CLR_OFFSET);
  132. spin_unlock_irqrestore(&mdev->lock, flags);
  133. }
  134. static void sti_mbox_clear_irq(struct mbox_chan *chan)
  135. {
  136. struct sti_channel *chan_info = chan->con_priv;
  137. struct sti_mbox_device *mdev = chan_info->mdev;
  138. unsigned int instance = chan_info->instance;
  139. unsigned int channel = chan_info->channel;
  140. void __iomem *base = MBOX_BASE(mdev, instance);
  141. writel_relaxed(BIT(channel), base + STI_IRQ_CLR_OFFSET);
  142. }
  143. static struct mbox_chan *sti_mbox_irq_to_channel(struct sti_mbox_device *mdev,
  144. unsigned int instance)
  145. {
  146. struct mbox_controller *mbox = mdev->mbox;
  147. struct mbox_chan *chan = NULL;
  148. unsigned int channel;
  149. unsigned long bits;
  150. void __iomem *base = MBOX_BASE(mdev, instance);
  151. bits = readl_relaxed(base + STI_IRQ_VAL_OFFSET);
  152. if (!bits)
  153. /* No IRQs fired in specified instance */
  154. return NULL;
  155. /* An IRQ has fired, find the associated channel */
  156. for (channel = 0; bits; channel++) {
  157. if (!test_and_clear_bit(channel, &bits))
  158. continue;
  159. chan = sti_mbox_to_channel(mbox, instance, channel);
  160. if (chan) {
  161. dev_dbg(mbox->dev,
  162. "IRQ fired on instance: %d channel: %d\n",
  163. instance, channel);
  164. break;
  165. }
  166. }
  167. return chan;
  168. }
  169. static irqreturn_t sti_mbox_thread_handler(int irq, void *data)
  170. {
  171. struct sti_mbox_device *mdev = data;
  172. struct sti_mbox_pdata *pdata = dev_get_platdata(mdev->dev);
  173. struct mbox_chan *chan;
  174. unsigned int instance;
  175. for (instance = 0; instance < pdata->num_inst; instance++) {
  176. keep_looking:
  177. chan = sti_mbox_irq_to_channel(mdev, instance);
  178. if (!chan)
  179. continue;
  180. mbox_chan_received_data(chan, NULL);
  181. sti_mbox_clear_irq(chan);
  182. sti_mbox_enable_channel(chan);
  183. goto keep_looking;
  184. }
  185. return IRQ_HANDLED;
  186. }
  187. static irqreturn_t sti_mbox_irq_handler(int irq, void *data)
  188. {
  189. struct sti_mbox_device *mdev = data;
  190. struct sti_mbox_pdata *pdata = dev_get_platdata(mdev->dev);
  191. struct sti_channel *chan_info;
  192. struct mbox_chan *chan;
  193. unsigned int instance;
  194. int ret = IRQ_NONE;
  195. for (instance = 0; instance < pdata->num_inst; instance++) {
  196. chan = sti_mbox_irq_to_channel(mdev, instance);
  197. if (!chan)
  198. continue;
  199. chan_info = chan->con_priv;
  200. if (!sti_mbox_channel_is_enabled(chan)) {
  201. dev_warn(mdev->dev,
  202. "Unexpected IRQ: %s\n"
  203. " instance: %d: channel: %d [enabled: %x]\n",
  204. mdev->name, chan_info->instance,
  205. chan_info->channel, mdev->enabled[instance]);
  206. /* Only handle IRQ if no other valid IRQs were found */
  207. if (ret == IRQ_NONE)
  208. ret = IRQ_HANDLED;
  209. continue;
  210. }
  211. sti_mbox_disable_channel(chan);
  212. ret = IRQ_WAKE_THREAD;
  213. }
  214. if (ret == IRQ_NONE)
  215. dev_err(mdev->dev, "Spurious IRQ - was a channel requested?\n");
  216. return ret;
  217. }
  218. static bool sti_mbox_tx_is_ready(struct mbox_chan *chan)
  219. {
  220. struct sti_channel *chan_info = chan->con_priv;
  221. struct sti_mbox_device *mdev = chan_info->mdev;
  222. unsigned int instance = chan_info->instance;
  223. unsigned int channel = chan_info->channel;
  224. void __iomem *base = MBOX_BASE(mdev, instance);
  225. if (!(readl_relaxed(base + STI_ENA_VAL_OFFSET) & BIT(channel))) {
  226. dev_dbg(mdev->dev, "Mbox: %s: inst: %d, chan: %d disabled\n",
  227. mdev->name, instance, channel);
  228. return false;
  229. }
  230. if (readl_relaxed(base + STI_IRQ_VAL_OFFSET) & BIT(channel)) {
  231. dev_dbg(mdev->dev, "Mbox: %s: inst: %d, chan: %d not ready\n",
  232. mdev->name, instance, channel);
  233. return false;
  234. }
  235. return true;
  236. }
  237. static int sti_mbox_send_data(struct mbox_chan *chan, void *data)
  238. {
  239. struct sti_channel *chan_info = chan->con_priv;
  240. struct sti_mbox_device *mdev = chan_info->mdev;
  241. unsigned int instance = chan_info->instance;
  242. unsigned int channel = chan_info->channel;
  243. void __iomem *base = MBOX_BASE(mdev, instance);
  244. /* Send event to co-processor */
  245. writel_relaxed(BIT(channel), base + STI_IRQ_SET_OFFSET);
  246. dev_dbg(mdev->dev,
  247. "Sent via Mailbox %s: instance: %d channel: %d\n",
  248. mdev->name, instance, channel);
  249. return 0;
  250. }
  251. static int sti_mbox_startup_chan(struct mbox_chan *chan)
  252. {
  253. sti_mbox_clear_irq(chan);
  254. sti_mbox_enable_channel(chan);
  255. return 0;
  256. }
  257. static void sti_mbox_shutdown_chan(struct mbox_chan *chan)
  258. {
  259. struct sti_channel *chan_info = chan->con_priv;
  260. struct mbox_controller *mbox = chan_info->mdev->mbox;
  261. int i;
  262. for (i = 0; i < mbox->num_chans; i++)
  263. if (chan == &mbox->chans[i])
  264. break;
  265. if (mbox->num_chans == i) {
  266. dev_warn(mbox->dev, "Request to free non-existent channel\n");
  267. return;
  268. }
  269. /* Reset channel */
  270. sti_mbox_disable_channel(chan);
  271. sti_mbox_clear_irq(chan);
  272. chan->con_priv = NULL;
  273. }
  274. static struct mbox_chan *sti_mbox_xlate(struct mbox_controller *mbox,
  275. const struct of_phandle_args *spec)
  276. {
  277. struct sti_mbox_device *mdev = dev_get_drvdata(mbox->dev);
  278. struct sti_mbox_pdata *pdata = dev_get_platdata(mdev->dev);
  279. struct sti_channel *chan_info;
  280. struct mbox_chan *chan = NULL;
  281. unsigned int instance = spec->args[0];
  282. unsigned int channel = spec->args[1];
  283. int i;
  284. /* Bounds checking */
  285. if (instance >= pdata->num_inst || channel >= pdata->num_chan) {
  286. dev_err(mbox->dev,
  287. "Invalid channel requested instance: %d channel: %d\n",
  288. instance, channel);
  289. return ERR_PTR(-EINVAL);
  290. }
  291. for (i = 0; i < mbox->num_chans; i++) {
  292. chan_info = mbox->chans[i].con_priv;
  293. /* Is requested channel free? */
  294. if (chan_info &&
  295. mbox->dev == chan_info->mdev->dev &&
  296. instance == chan_info->instance &&
  297. channel == chan_info->channel) {
  298. dev_err(mbox->dev, "Channel in use\n");
  299. return ERR_PTR(-EBUSY);
  300. }
  301. /*
  302. * Find the first free slot, then continue checking
  303. * to see if requested channel is in use
  304. */
  305. if (!chan && !chan_info)
  306. chan = &mbox->chans[i];
  307. }
  308. if (!chan) {
  309. dev_err(mbox->dev, "No free channels left\n");
  310. return ERR_PTR(-EBUSY);
  311. }
  312. chan_info = devm_kzalloc(mbox->dev, sizeof(*chan_info), GFP_KERNEL);
  313. if (!chan_info)
  314. return ERR_PTR(-ENOMEM);
  315. chan_info->mdev = mdev;
  316. chan_info->instance = instance;
  317. chan_info->channel = channel;
  318. chan->con_priv = chan_info;
  319. dev_info(mbox->dev,
  320. "Mbox: %s: Created channel: instance: %d channel: %d\n",
  321. mdev->name, instance, channel);
  322. return chan;
  323. }
  324. static struct mbox_chan_ops sti_mbox_ops = {
  325. .startup = sti_mbox_startup_chan,
  326. .shutdown = sti_mbox_shutdown_chan,
  327. .send_data = sti_mbox_send_data,
  328. .last_tx_done = sti_mbox_tx_is_ready,
  329. };
  330. static const struct sti_mbox_pdata mbox_stih407_pdata = {
  331. .num_inst = 4,
  332. .num_chan = 32,
  333. };
  334. static const struct of_device_id sti_mailbox_match[] = {
  335. {
  336. .compatible = "st,stih407-mailbox",
  337. .data = (void *)&mbox_stih407_pdata
  338. },
  339. { }
  340. };
  341. static int sti_mbox_probe(struct platform_device *pdev)
  342. {
  343. const struct of_device_id *match;
  344. struct mbox_controller *mbox;
  345. struct sti_mbox_device *mdev;
  346. struct device_node *np = pdev->dev.of_node;
  347. struct mbox_chan *chans;
  348. struct resource *res;
  349. int irq;
  350. int ret;
  351. match = of_match_device(sti_mailbox_match, &pdev->dev);
  352. if (!match) {
  353. dev_err(&pdev->dev, "No configuration found\n");
  354. return -ENODEV;
  355. }
  356. pdev->dev.platform_data = (struct sti_mbox_pdata *) match->data;
  357. mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL);
  358. if (!mdev)
  359. return -ENOMEM;
  360. platform_set_drvdata(pdev, mdev);
  361. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  362. mdev->base = devm_ioremap_resource(&pdev->dev, res);
  363. if (!mdev->base)
  364. return -ENOMEM;
  365. ret = of_property_read_string(np, "mbox-name", &mdev->name);
  366. if (ret)
  367. mdev->name = np->full_name;
  368. mbox = devm_kzalloc(&pdev->dev, sizeof(*mbox), GFP_KERNEL);
  369. if (!mbox)
  370. return -ENOMEM;
  371. chans = devm_kzalloc(&pdev->dev,
  372. sizeof(*chans) * STI_MBOX_CHAN_MAX, GFP_KERNEL);
  373. if (!chans)
  374. return -ENOMEM;
  375. mdev->dev = &pdev->dev;
  376. mdev->mbox = mbox;
  377. spin_lock_init(&mdev->lock);
  378. /* STi Mailbox does not have a Tx-Done or Tx-Ready IRQ */
  379. mbox->txdone_irq = false;
  380. mbox->txdone_poll = true;
  381. mbox->txpoll_period = 100;
  382. mbox->ops = &sti_mbox_ops;
  383. mbox->dev = mdev->dev;
  384. mbox->of_xlate = sti_mbox_xlate;
  385. mbox->chans = chans;
  386. mbox->num_chans = STI_MBOX_CHAN_MAX;
  387. ret = mbox_controller_register(mbox);
  388. if (ret)
  389. return ret;
  390. /* It's okay for Tx Mailboxes to not supply IRQs */
  391. irq = platform_get_irq(pdev, 0);
  392. if (irq < 0) {
  393. dev_info(&pdev->dev,
  394. "%s: Registered Tx only Mailbox\n", mdev->name);
  395. return 0;
  396. }
  397. ret = devm_request_threaded_irq(&pdev->dev, irq,
  398. sti_mbox_irq_handler,
  399. sti_mbox_thread_handler,
  400. IRQF_ONESHOT, mdev->name, mdev);
  401. if (ret) {
  402. dev_err(&pdev->dev, "Can't claim IRQ %d\n", irq);
  403. mbox_controller_unregister(mbox);
  404. return -EINVAL;
  405. }
  406. dev_info(&pdev->dev, "%s: Registered Tx/Rx Mailbox\n", mdev->name);
  407. return 0;
  408. }
  409. static int sti_mbox_remove(struct platform_device *pdev)
  410. {
  411. struct sti_mbox_device *mdev = platform_get_drvdata(pdev);
  412. mbox_controller_unregister(mdev->mbox);
  413. return 0;
  414. }
  415. static struct platform_driver sti_mbox_driver = {
  416. .probe = sti_mbox_probe,
  417. .remove = sti_mbox_remove,
  418. .driver = {
  419. .name = "sti-mailbox",
  420. .of_match_table = sti_mailbox_match,
  421. },
  422. };
  423. module_platform_driver(sti_mbox_driver);
  424. MODULE_LICENSE("GPL");
  425. MODULE_DESCRIPTION("STMicroelectronics Mailbox Controller");
  426. MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
  427. MODULE_ALIAS("platform:mailbox-sti");