mailbox-altera.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. };
  51. static struct altera_mbox *mbox_chan_to_altera_mbox(struct mbox_chan *chan)
  52. {
  53. if (!chan || !chan->con_priv)
  54. return NULL;
  55. return (struct altera_mbox *)chan->con_priv;
  56. }
  57. static inline int altera_mbox_full(struct altera_mbox *mbox)
  58. {
  59. u32 status;
  60. status = readl_relaxed(mbox->mbox_base + MAILBOX_STS_REG);
  61. return MBOX_FULL(status);
  62. }
  63. static inline int altera_mbox_pending(struct altera_mbox *mbox)
  64. {
  65. u32 status;
  66. status = readl_relaxed(mbox->mbox_base + MAILBOX_STS_REG);
  67. return MBOX_PENDING(status);
  68. }
  69. static void altera_mbox_rx_intmask(struct altera_mbox *mbox, bool enable)
  70. {
  71. u32 mask;
  72. mask = readl_relaxed(mbox->mbox_base + MAILBOX_INTMASK_REG);
  73. if (enable)
  74. mask |= INT_PENDING_MSK;
  75. else
  76. mask &= ~INT_PENDING_MSK;
  77. writel_relaxed(mask, mbox->mbox_base + MAILBOX_INTMASK_REG);
  78. }
  79. static void altera_mbox_tx_intmask(struct altera_mbox *mbox, bool enable)
  80. {
  81. u32 mask;
  82. mask = readl_relaxed(mbox->mbox_base + MAILBOX_INTMASK_REG);
  83. if (enable)
  84. mask |= INT_SPACE_MSK;
  85. else
  86. mask &= ~INT_SPACE_MSK;
  87. writel_relaxed(mask, mbox->mbox_base + MAILBOX_INTMASK_REG);
  88. }
  89. static bool altera_mbox_is_sender(struct altera_mbox *mbox)
  90. {
  91. u32 reg;
  92. /* Write a magic number to PTR register and read back this register.
  93. * This register is read-write if it is a sender.
  94. */
  95. #define MBOX_MAGIC 0xA5A5AA55
  96. writel_relaxed(MBOX_MAGIC, mbox->mbox_base + MAILBOX_PTR_REG);
  97. reg = readl_relaxed(mbox->mbox_base + MAILBOX_PTR_REG);
  98. if (reg == MBOX_MAGIC) {
  99. /* Clear to 0 */
  100. writel_relaxed(0, mbox->mbox_base + MAILBOX_PTR_REG);
  101. return true;
  102. }
  103. return false;
  104. }
  105. static void altera_mbox_rx_data(struct mbox_chan *chan)
  106. {
  107. struct altera_mbox *mbox = mbox_chan_to_altera_mbox(chan);
  108. u32 data[2];
  109. if (altera_mbox_pending(mbox)) {
  110. data[MBOX_PTR] =
  111. readl_relaxed(mbox->mbox_base + MAILBOX_PTR_REG);
  112. data[MBOX_CMD] =
  113. readl_relaxed(mbox->mbox_base + MAILBOX_CMD_REG);
  114. mbox_chan_received_data(chan, (void *)data);
  115. }
  116. }
  117. static void altera_mbox_poll_rx(unsigned long data)
  118. {
  119. struct mbox_chan *chan = (struct mbox_chan *)data;
  120. struct altera_mbox *mbox = mbox_chan_to_altera_mbox(chan);
  121. altera_mbox_rx_data(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. setup_timer(&mbox->rxpoll_timer, altera_mbox_poll_rx,
  172. (unsigned long)chan);
  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");