mailbox-altera.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright Altera Corporation (C) 2013-2014. All rights reserved
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/device.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/platform_device.h>
  24. #define DRIVER_NAME "altera-mailbox"
  25. #define MAILBOX_CMD_REG 0x00
  26. #define MAILBOX_PTR_REG 0x04
  27. #define MAILBOX_STS_REG 0x08
  28. #define MAILBOX_INTMASK_REG 0x0C
  29. #define INT_PENDING_MSK 0x1
  30. #define INT_SPACE_MSK 0x2
  31. #define STS_PENDING_MSK 0x1
  32. #define STS_FULL_MSK 0x2
  33. #define STS_FULL_OFT 0x1
  34. #define MBOX_PENDING(status) (((status) & STS_PENDING_MSK))
  35. #define MBOX_FULL(status) (((status) & STS_FULL_MSK) >> STS_FULL_OFT)
  36. enum altera_mbox_msg {
  37. MBOX_CMD = 0,
  38. MBOX_PTR,
  39. };
  40. #define MBOX_POLLING_MS 5 /* polling interval 5ms */
  41. struct altera_mbox {
  42. bool is_sender; /* 1-sender, 0-receiver */
  43. bool intr_mode;
  44. int irq;
  45. void __iomem *mbox_base;
  46. struct device *dev;
  47. struct mbox_controller controller;
  48. /* If the controller supports only RX polling mode */
  49. struct timer_list rxpoll_timer;
  50. struct mbox_chan *chan;
  51. };
  52. static struct altera_mbox *mbox_chan_to_altera_mbox(struct mbox_chan *chan)
  53. {
  54. if (!chan || !chan->con_priv)
  55. return NULL;
  56. return (struct altera_mbox *)chan->con_priv;
  57. }
  58. static inline int altera_mbox_full(struct altera_mbox *mbox)
  59. {
  60. u32 status;
  61. status = readl_relaxed(mbox->mbox_base + MAILBOX_STS_REG);
  62. return MBOX_FULL(status);
  63. }
  64. static inline int altera_mbox_pending(struct altera_mbox *mbox)
  65. {
  66. u32 status;
  67. status = readl_relaxed(mbox->mbox_base + MAILBOX_STS_REG);
  68. return MBOX_PENDING(status);
  69. }
  70. static void altera_mbox_rx_intmask(struct altera_mbox *mbox, bool enable)
  71. {
  72. u32 mask;
  73. mask = readl_relaxed(mbox->mbox_base + MAILBOX_INTMASK_REG);
  74. if (enable)
  75. mask |= INT_PENDING_MSK;
  76. else
  77. mask &= ~INT_PENDING_MSK;
  78. writel_relaxed(mask, mbox->mbox_base + MAILBOX_INTMASK_REG);
  79. }
  80. static void altera_mbox_tx_intmask(struct altera_mbox *mbox, bool enable)
  81. {
  82. u32 mask;
  83. mask = readl_relaxed(mbox->mbox_base + MAILBOX_INTMASK_REG);
  84. if (enable)
  85. mask |= INT_SPACE_MSK;
  86. else
  87. mask &= ~INT_SPACE_MSK;
  88. writel_relaxed(mask, mbox->mbox_base + MAILBOX_INTMASK_REG);
  89. }
  90. static bool altera_mbox_is_sender(struct altera_mbox *mbox)
  91. {
  92. u32 reg;
  93. /* Write a magic number to PTR register and read back this register.
  94. * This register is read-write if it is a sender.
  95. */
  96. #define MBOX_MAGIC 0xA5A5AA55
  97. writel_relaxed(MBOX_MAGIC, mbox->mbox_base + MAILBOX_PTR_REG);
  98. reg = readl_relaxed(mbox->mbox_base + MAILBOX_PTR_REG);
  99. if (reg == MBOX_MAGIC) {
  100. /* Clear to 0 */
  101. writel_relaxed(0, mbox->mbox_base + MAILBOX_PTR_REG);
  102. return true;
  103. }
  104. return false;
  105. }
  106. static void altera_mbox_rx_data(struct mbox_chan *chan)
  107. {
  108. struct altera_mbox *mbox = mbox_chan_to_altera_mbox(chan);
  109. u32 data[2];
  110. if (altera_mbox_pending(mbox)) {
  111. data[MBOX_PTR] =
  112. readl_relaxed(mbox->mbox_base + MAILBOX_PTR_REG);
  113. data[MBOX_CMD] =
  114. readl_relaxed(mbox->mbox_base + MAILBOX_CMD_REG);
  115. mbox_chan_received_data(chan, (void *)data);
  116. }
  117. }
  118. static void altera_mbox_poll_rx(struct timer_list *t)
  119. {
  120. struct altera_mbox *mbox = from_timer(mbox, t, rxpoll_timer);
  121. altera_mbox_rx_data(mbox->chan);
  122. mod_timer(&mbox->rxpoll_timer,
  123. jiffies + msecs_to_jiffies(MBOX_POLLING_MS));
  124. }
  125. static irqreturn_t altera_mbox_tx_interrupt(int irq, void *p)
  126. {
  127. struct mbox_chan *chan = (struct mbox_chan *)p;
  128. struct altera_mbox *mbox = mbox_chan_to_altera_mbox(chan);
  129. altera_mbox_tx_intmask(mbox, false);
  130. mbox_chan_txdone(chan, 0);
  131. return IRQ_HANDLED;
  132. }
  133. static irqreturn_t altera_mbox_rx_interrupt(int irq, void *p)
  134. {
  135. struct mbox_chan *chan = (struct mbox_chan *)p;
  136. altera_mbox_rx_data(chan);
  137. return IRQ_HANDLED;
  138. }
  139. static int altera_mbox_startup_sender(struct mbox_chan *chan)
  140. {
  141. int ret;
  142. struct altera_mbox *mbox = mbox_chan_to_altera_mbox(chan);
  143. if (mbox->intr_mode) {
  144. ret = request_irq(mbox->irq, altera_mbox_tx_interrupt, 0,
  145. DRIVER_NAME, chan);
  146. if (unlikely(ret)) {
  147. dev_err(mbox->dev,
  148. "failed to register mailbox interrupt:%d\n",
  149. ret);
  150. return ret;
  151. }
  152. }
  153. return 0;
  154. }
  155. static int altera_mbox_startup_receiver(struct mbox_chan *chan)
  156. {
  157. int ret;
  158. struct altera_mbox *mbox = mbox_chan_to_altera_mbox(chan);
  159. if (mbox->intr_mode) {
  160. ret = request_irq(mbox->irq, altera_mbox_rx_interrupt, 0,
  161. DRIVER_NAME, chan);
  162. if (unlikely(ret)) {
  163. mbox->intr_mode = false;
  164. goto polling; /* use polling if failed */
  165. }
  166. altera_mbox_rx_intmask(mbox, true);
  167. return 0;
  168. }
  169. polling:
  170. /* Setup polling timer */
  171. mbox->chan = chan;
  172. timer_setup(&mbox->rxpoll_timer, altera_mbox_poll_rx, 0);
  173. mod_timer(&mbox->rxpoll_timer,
  174. jiffies + msecs_to_jiffies(MBOX_POLLING_MS));
  175. return 0;
  176. }
  177. static int altera_mbox_send_data(struct mbox_chan *chan, void *data)
  178. {
  179. struct altera_mbox *mbox = mbox_chan_to_altera_mbox(chan);
  180. u32 *udata = (u32 *)data;
  181. if (!mbox || !data)
  182. return -EINVAL;
  183. if (!mbox->is_sender) {
  184. dev_warn(mbox->dev,
  185. "failed to send. This is receiver mailbox.\n");
  186. return -EINVAL;
  187. }
  188. if (altera_mbox_full(mbox))
  189. return -EBUSY;
  190. /* Enable interrupt before send */
  191. if (mbox->intr_mode)
  192. altera_mbox_tx_intmask(mbox, true);
  193. /* Pointer register must write before command register */
  194. writel_relaxed(udata[MBOX_PTR], mbox->mbox_base + MAILBOX_PTR_REG);
  195. writel_relaxed(udata[MBOX_CMD], mbox->mbox_base + MAILBOX_CMD_REG);
  196. return 0;
  197. }
  198. static bool altera_mbox_last_tx_done(struct mbox_chan *chan)
  199. {
  200. struct altera_mbox *mbox = mbox_chan_to_altera_mbox(chan);
  201. /* Return false if mailbox is full */
  202. return altera_mbox_full(mbox) ? false : true;
  203. }
  204. static bool altera_mbox_peek_data(struct mbox_chan *chan)
  205. {
  206. struct altera_mbox *mbox = mbox_chan_to_altera_mbox(chan);
  207. return altera_mbox_pending(mbox) ? true : false;
  208. }
  209. static int altera_mbox_startup(struct mbox_chan *chan)
  210. {
  211. struct altera_mbox *mbox = mbox_chan_to_altera_mbox(chan);
  212. int ret = 0;
  213. if (!mbox)
  214. return -EINVAL;
  215. if (mbox->is_sender)
  216. ret = altera_mbox_startup_sender(chan);
  217. else
  218. ret = altera_mbox_startup_receiver(chan);
  219. return ret;
  220. }
  221. static void altera_mbox_shutdown(struct mbox_chan *chan)
  222. {
  223. struct altera_mbox *mbox = mbox_chan_to_altera_mbox(chan);
  224. if (mbox->intr_mode) {
  225. /* Unmask all interrupt masks */
  226. writel_relaxed(~0, mbox->mbox_base + MAILBOX_INTMASK_REG);
  227. free_irq(mbox->irq, chan);
  228. } else if (!mbox->is_sender) {
  229. del_timer_sync(&mbox->rxpoll_timer);
  230. }
  231. }
  232. static const struct mbox_chan_ops altera_mbox_ops = {
  233. .send_data = altera_mbox_send_data,
  234. .startup = altera_mbox_startup,
  235. .shutdown = altera_mbox_shutdown,
  236. .last_tx_done = altera_mbox_last_tx_done,
  237. .peek_data = altera_mbox_peek_data,
  238. };
  239. static int altera_mbox_probe(struct platform_device *pdev)
  240. {
  241. struct altera_mbox *mbox;
  242. struct resource *regs;
  243. struct mbox_chan *chans;
  244. int ret;
  245. mbox = devm_kzalloc(&pdev->dev, sizeof(*mbox),
  246. GFP_KERNEL);
  247. if (!mbox)
  248. return -ENOMEM;
  249. /* Allocated one channel */
  250. chans = devm_kzalloc(&pdev->dev, sizeof(*chans), GFP_KERNEL);
  251. if (!chans)
  252. return -ENOMEM;
  253. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  254. mbox->mbox_base = devm_ioremap_resource(&pdev->dev, regs);
  255. if (IS_ERR(mbox->mbox_base))
  256. return PTR_ERR(mbox->mbox_base);
  257. /* Check is it a sender or receiver? */
  258. mbox->is_sender = altera_mbox_is_sender(mbox);
  259. mbox->irq = platform_get_irq(pdev, 0);
  260. if (mbox->irq >= 0)
  261. mbox->intr_mode = true;
  262. mbox->dev = &pdev->dev;
  263. /* Hardware supports only one channel. */
  264. chans[0].con_priv = mbox;
  265. mbox->controller.dev = mbox->dev;
  266. mbox->controller.num_chans = 1;
  267. mbox->controller.chans = chans;
  268. mbox->controller.ops = &altera_mbox_ops;
  269. if (mbox->is_sender) {
  270. if (mbox->intr_mode) {
  271. mbox->controller.txdone_irq = true;
  272. } else {
  273. mbox->controller.txdone_poll = true;
  274. mbox->controller.txpoll_period = MBOX_POLLING_MS;
  275. }
  276. }
  277. ret = mbox_controller_register(&mbox->controller);
  278. if (ret) {
  279. dev_err(&pdev->dev, "Register mailbox failed\n");
  280. goto err;
  281. }
  282. platform_set_drvdata(pdev, mbox);
  283. err:
  284. return ret;
  285. }
  286. static int altera_mbox_remove(struct platform_device *pdev)
  287. {
  288. struct altera_mbox *mbox = platform_get_drvdata(pdev);
  289. if (!mbox)
  290. return -EINVAL;
  291. mbox_controller_unregister(&mbox->controller);
  292. return 0;
  293. }
  294. static const struct of_device_id altera_mbox_match[] = {
  295. { .compatible = "altr,mailbox-1.0" },
  296. { /* Sentinel */ }
  297. };
  298. MODULE_DEVICE_TABLE(of, altera_mbox_match);
  299. static struct platform_driver altera_mbox_driver = {
  300. .probe = altera_mbox_probe,
  301. .remove = altera_mbox_remove,
  302. .driver = {
  303. .name = DRIVER_NAME,
  304. .of_match_table = altera_mbox_match,
  305. },
  306. };
  307. module_platform_driver(altera_mbox_driver);
  308. MODULE_LICENSE("GPL v2");
  309. MODULE_DESCRIPTION("Altera mailbox specific functions");
  310. MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
  311. MODULE_ALIAS("platform:altera-mailbox");