mailbox: Add Altera mailbox driver
Subsystems (3)
PATCH v2 1/1 mailbox: Add Altera mailbox driver
The Altera mailbox allows for interprocessor communication. It supports only one channel and work as either sender or receiver.
---
v2:
Resolved comment from Dinh and Suman.
- Alphabetize the headers.
- Removed WARN_ON() checking
- Use devm_kzalloc to allocate struct mbox_chan
- Change __raw_readl/__raw_writel to readl/writel
- Check interrupt mode before enable interrupt
- Change to use module_platform_driver()
- Remove platform_set_drvdata(pdev, NULL);
- Fix "Alignment should match open parenthesis" warning
- Update MAINTAINERS file
--- .../devicetree/bindings/mailbox/altera-mailbox.txt | 49 +++ MAINTAINERS | 6 + drivers/mailbox/Kconfig | 6 + drivers/mailbox/Makefile | 2 + drivers/mailbox/mailbox-altera.c | 385 +++++++++++++++++++++ 5 files changed, 448 insertions(+) create mode 100644 Documentation/devicetree/bindings/mailbox/altera-mailbox.txt create mode 100644 drivers/mailbox/mailbox-altera.c
Documentation/devicetree/bindings/mailbox/altera-mailbox.txt new file +49 -0
@@ -0,0 +1,49 @@ +Altera Mailbox Driver +===================== + +Required properties: +- compatible : "altr,mailbox-1.0". +- reg : physical base address of the mailbox and length of + memory mapped region. +- #mbox-cells: Common mailbox binding property to identify the number + of cells required for the mailbox specifier. Should be 1. + +Optional properties: +- interrupt-parent : interrupt source phandle. +- interrupts : interrupt number. The interrupt specifier format + depends on the interrupt controller parent. + +Example: + mbox_tx: mailbox@0x100 { + compatible = "altr,mailbox-1.0"; + reg = <0x100 0x8>; + interrupt-parent = < &gic_0 >; + interrupts = <5>; + #mbox-cells = <1>; + }; + + mbox_rx: mailbox@0x200 { + compatible = "altr,mailbox-1.0"; + reg = <0x200 0x8>; + interrupt-parent = < &gic_0 >; + interrupts = <6>; + #mbox-cells = <1>; + }; + +Mailbox client +=============== +"mboxes" and the optional "mbox-names" (please see +Documentation/devicetree/bindings/mailbox/mailbox.txt for details). Each value +of the mboxes property should contain a phandle to the mailbox controller +device node and second argument is the channel index. It must be 0 (hardware +support only one channel).The equivalent "mbox-names" property value can be +used to give a name to the communication channel to be used by the client user. + +Example: + mclient0: mclient0@0x400 { + compatible = "client-1.0"; + reg = <0x400 0x10>; + mbox-names = "mbox-tx", "mbox-rx"; + mboxes = <&mbox_tx 0>, + <&mbox_rx 0>; + };
MAINTAINERS +6 -0
@@ -563,6 +563,12 @@ S: Odd Fixes L: [email protected] F: arch/alpha/ +ALTERA MAILBOX DRIVER +M: Ley Foon Tan <[email protected]> +L: nios2-dev-g9ZBwUv/Ih/[email protected] (moderated for non-subscribers) +S: Maintained +F: drivers/mailbox/mailbox-altera.c + ALTERA TRIPLE SPEED ETHERNET DRIVER M: Vince Bridgers <[email protected]> L: [email protected]
drivers/mailbox/Kconfig +6 -0
@@ -45,4 +45,10 @@ config PCC states). Select this driver if your platform implements the PCC clients mentioned above. +config ALTERA_MBOX + tristate "Altera Mailbox" + help + An implementation of the Altera Mailbox soft core. It is used + to send message between processors. Say Y here if you want to use the + Altera mailbox support. endif
drivers/mailbox/Makefile +2 -0
@@ -7,3 +7,5 @@ obj-$(CONFIG_PL320_MBOX) += pl320-ipc.o obj-$(CONFIG_OMAP2PLUS_MBOX) += omap-mailbox.o obj-$(CONFIG_PCC) += pcc.o + +obj-$(CONFIG_ALTERA_MBOX) += mailbox-altera.o
drivers/mailbox/mailbox-altera.c new file +385 -0
385 lines not on this page · raw message
Signature · 6 lines
1.8.2.1 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 5 lines · Ley Foon Tan
The Altera mailbox allows for interprocessor communication. It supports only one channel and work as either sender or receiver.
Signed-off-by Ley Foon Tan <[email protected]>
Quoted 13 lines
---
v2:
Resolved comment from Dinh and Suman.
- Alphabetize the headers.
- RemovedWARN_ON()checking
- Usedevm_kzallocto allocate structmbox_chan
- Change __raw_readl/__raw_writel to readl/writel
- Check interrupt mode before enable interrupt
- Change to usemodule_platform_driver()
- Removeplatform_set_drvdata(pdev, NULL);
- Fix "Alignment should match open parenthesis" warning
- UpdateMAINTAINERSfile
---
Any further feedback for this v2 patch?
Thanks.
Regards.
Jassi Brar
maintainerOn Mon, Dec 22, 2014 at 3:14 PM, Ley Foon Tan <[email protected]> wrote:
...
Quoted 45 lines
drivers/mailbox/mailbox-altera.c new file +39
-0@@ -0,0 +1,385 @@ +/* +* Copyright Altera Corporation (C) 2013-2014. All rights reserved +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <linux/device.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/kernel.h> +#include <linux/mailbox_controller.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/platform_device.h> + +#define DRIVER_NAME "altera-mailbox" + +#define MAILBOX_CMD_REG 0x00 +#define MAILBOX_PTR_REG 0x04 +#define MAILBOX_STS_REG 0x08 +#define MAILBOX_INTMASK_REG 0x0C + +#define INT_PENDING_MSK 0x1 +#define INT_SPACE_MSK 0x2 + +#define STS_PENDING_MSK 0x1 +#define STS_FULL_MSK 0x2 +#define STS_FULL_OFT 0x1 +
drivers/mailbox/mailbox-altera.c line 49
Quoted 10 lines
drivers/mailbox/mailbox-altera.c new file +10
-0+#define MBOX_PENDING (status) (((status) & STS_PENDING_MSK)) +#define MBOX_FULL (status) (((status) & STS_FULL_MSK) >> STS_FULL_OFT) + +enum altera_mbox_msg { +MBOX_CMD =0 , +MBOX_PTR , +}; + +#define MBOX_POLLING_MS 1/* polling interval 1ms */ +
The intention is too aggressive - though poll won't be before next jiffy (>1ms). And if you are not expecting a reply, it should be even bigger. Say, 50ms default and 5ms when you expect RX?
Quoted 18 lines
drivers/mailbox/mailbox-altera.c new file +17
-0+struct altera_mbox { +bool is_sender;/* 1-sender, 0-receiver */ +bool intr_mode; +int irq; +void __iomem *mbox_base; +struct device *dev; +struct mbox_controller controller; + +/* If the controller supports only RX polling mode */ +struct timer_list rxpoll_timer; +}; + +static struct altera_mbox *mbox_chan_to_altera_mbox (struct mbox_chan *chan) +{ +if (!chan || !chan->con_priv) +return NULL ; +
drivers/mailbox/mailbox-altera.c line 76
Quoted 10 lines
drivers/mailbox/mailbox-altera.c new file +10
-0+return (struct altera_mbox *)chan->con_priv; +} + +static inline int altera_mbox_full (struct altera_mbox *mbox) +{ +u32 status; + + status =readl (mbox->mbox_base +MAILBOX_STS_REG ); +return MBOX_FULL (status); +}
s/readl/readl_relaxed and s/writel/writel_relaxed everywhere
Quoted 115 lines
drivers/mailbox/mailbox-altera.c new file +114
-0+ +static inline int altera_mbox_pending (struct altera_mbox *mbox) +{ +u32 status; + + status =readl (mbox->mbox_base +MAILBOX_STS_REG ); +return MBOX_PENDING (status); +} + +static void altera_mbox_rx_intmask (struct altera_mbox *mbox,bool enable) +{ +u32 mask; + + mask =readl (mbox->mbox_base +MAILBOX_INTMASK_REG ); +if (enable) + mask |=INT_PENDING_MSK ; +else + mask &= ~INT_PENDING_MSK ; +writel (mask, mbox->mbox_base +MAILBOX_INTMASK_REG ); +} + +static void altera_mbox_tx_intmask (struct altera_mbox *mbox,bool enable) +{ +u32 mask; + + mask =readl (mbox->mbox_base +MAILBOX_INTMASK_REG ); +if (enable) + mask |=INT_SPACE_MSK ; +else + mask &= ~INT_SPACE_MSK ; +writel (mask, mbox->mbox_base +MAILBOX_INTMASK_REG ); +} + +static bool altera_mbox_is_sender (struct altera_mbox *mbox) +{ +u32 reg; +/* Write a magic number to PTR register and read back this register. +* This register is read-write if it is a sender. +*/ +#define MBOX_MAGIC 0xA5A5AA55 +writel (MBOX_MAGIC , mbox->mbox_base +MAILBOX_PTR_REG ); + reg =readl (mbox->mbox_base +MAILBOX_PTR_REG ); +if (reg ==MBOX_MAGIC ) { +/* Clear to 0 */ +writel (0 , mbox->mbox_base +MAILBOX_PTR_REG ); +return true; + } +return false; +} + +static void altera_mbox_rx_data (struct mbox_chan *chan) +{ +struct altera_mbox *mbox =mbox_chan_to_altera_mbox (chan); +u32 data[2 ]; + +if (altera_mbox_pending (mbox)) { + data[MBOX_PTR ] =readl (mbox->mbox_base +MAILBOX_PTR_REG ); + data[MBOX_CMD ] =readl (mbox->mbox_base +MAILBOX_CMD_REG ); +mbox_chan_received_data (chan, (void *)data); + } +} + +static void altera_mbox_poll_rx (unsigned long data) +{ +struct mbox_chan *chan = (struct mbox_chan *)data; +struct altera_mbox *mbox =mbox_chan_to_altera_mbox (chan); + +altera_mbox_rx_data (chan); + +mod_timer (&mbox->rxpoll_timer, + jiffies +msecs_to_jiffies (MBOX_POLLING_MS )); +} + +static irqreturn_t altera_mbox_tx_interrupt (int irq,void *p) +{ +struct mbox_chan *chan = (struct mbox_chan *)p; +struct altera_mbox *mbox =mbox_chan_to_altera_mbox (chan); + +altera_mbox_tx_intmask (mbox, false); +mbox_chan_txdone (chan,0 ); + +return IRQ_HANDLED ; +} + +static irqreturn_t altera_mbox_rx_interrupt (int irq,void *p) +{ +struct mbox_chan *chan = (struct mbox_chan *)p; + +altera_mbox_rx_data (chan); +return IRQ_HANDLED ; +} + +static int altera_mbox_startup_sender (struct mbox_chan *chan) +{ +int ret; +struct altera_mbox *mbox =mbox_chan_to_altera_mbox (chan); + +if (mbox->intr_mode) { + ret =request_irq (mbox->irq, altera_mbox_tx_interrupt,0 , +DRIVER_NAME , chan); +if (unlikely (ret)) { +dev_err (mbox->dev, +"failed to register mailbox interrupt:%d\n" , + ret); +return ret; + } + } + +return 0 ; +} + +static int altera_mbox_startup_receiver (struct mbox_chan *chan) +{ +int ret;
drivers/mailbox/mailbox-altera.c line 200
Quoted 10 lines
drivers/mailbox/mailbox-altera.c new file +10
-0+struct altera_mbox *mbox =mbox_chan_to_altera_mbox (chan); + +if (mbox->intr_mode) { + ret =request_irq (mbox->irq, altera_mbox_rx_interrupt,0 , +DRIVER_NAME , chan); +if (unlikely (ret)) { +dev_err (mbox->dev, +"failed to register mailbox interrupt:%d\n" , + ret); +return ret;
Is this really fatal? Maybe continue using the timer scheme below?
drivers/mailbox/mailbox-altera.c line 213
Quoted 13 lines
drivers/mailbox/mailbox-altera.c new file +13
-0+ } +altera_mbox_rx_intmask (mbox, true); + }else { +/* Setup polling timer */ +setup_timer (&mbox->rxpoll_timer, altera_mbox_poll_rx, + (unsigned long )chan); +mod_timer (&mbox->rxpoll_timer, + jiffies +msecs_to_jiffies (MBOX_POLLING_MS )); + } + +return 0 ; +} +
Just these minor comments.
-Jassi
Quoted 53 lines · Jassi Brar
On Mon, Dec 22, 2014 at 3:14 PM, Ley Foon Tan <[email protected]> wrote:
...Quoted 50 lines
drivers/mailbox/mailbox-altera.c new file +44
-0@@ -0,0 +1,385 @@ +/* +* Copyright Altera Corporation (C) 2013-2014. All rights reserved +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <linux/device.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/kernel.h> +#include <linux/mailbox_controller.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/platform_device.h> + +#define DRIVER_NAME "altera-mailbox" + +#define MAILBOX_CMD_REG 0x00 +#define MAILBOX_PTR_REG 0x04 +#define MAILBOX_STS_REG 0x08 +#define MAILBOX_INTMASK_REG 0x0C + +#define INT_PENDING_MSK 0x1 +#define INT_SPACE_MSK 0x2 + +#define STS_PENDING_MSK 0x1 +#define STS_FULL_MSK 0x2 +#define STS_FULL_OFT 0x1 + +#define MBOX_PENDING (status) (((status) & STS_PENDING_MSK)) +#define MBOX_FULL (status) (((status) & STS_FULL_MSK) >> STS_FULL_OFT) + +enum altera_mbox_msg { +MBOX_CMD =0 ,
Quoted 8 lines
drivers/mailbox/mailbox-altera.c line 49
Quoted 5 lines
drivers/mailbox/mailbox-altera.c new file +5
-0+ MBOX_PTR, +}; + +#define MBOX_POLLING_MS 1/* polling interval 1ms */ +The intention is too aggressive - though poll won't be before next jiffy (>1ms). And if you are not expecting a reply, it should be even bigger. Say, 50ms default and 5ms when you expect RX?
Currently MBOX_POLLING_MS is used for RX polling and TX polling. I think change to 5ms should be fine, 50ms is a bit too long. Do you okay with this?
Quoted 22 lines
Quoted 22 lines
drivers/mailbox/mailbox-altera.c new file +21
-0+struct altera_mbox { +bool is_sender;/* 1-sender, 0-receiver */ +bool intr_mode; +int irq; +void __iomem *mbox_base; +struct device *dev; +struct mbox_controller controller; + +/* If the controller supports only RX polling mode */ +struct timer_list rxpoll_timer; +}; + +static struct altera_mbox *mbox_chan_to_altera_mbox (struct mbox_chan *chan) +{ +if (!chan || !chan->con_priv) +return NULL ; + +return (struct altera_mbox *)chan->con_priv; +} + +static inline int altera_mbox_full (struct altera_mbox *mbox)
Quoted 8 lines
drivers/mailbox/mailbox-altera.c line 76
Quoted 7 lines
drivers/mailbox/mailbox-altera.c new file +6
-0+{ +u32 status; + + status =readl (mbox->mbox_base +MAILBOX_STS_REG ); +return MBOX_FULL (status); +}s/readl/readl_relaxed and s/writel/writel_relaxed everywhere
Noted.
Quoted 119 lines
Quoted 119 lines
drivers/mailbox/mailbox-altera.c new file +118
-0+ +static inline int altera_mbox_pending (struct altera_mbox *mbox) +{ +u32 status; + + status =readl (mbox->mbox_base +MAILBOX_STS_REG ); +return MBOX_PENDING (status); +} + +static void altera_mbox_rx_intmask (struct altera_mbox *mbox,bool enable) +{ +u32 mask; + + mask =readl (mbox->mbox_base +MAILBOX_INTMASK_REG ); +if (enable) + mask |=INT_PENDING_MSK ; +else + mask &= ~INT_PENDING_MSK ; +writel (mask, mbox->mbox_base +MAILBOX_INTMASK_REG ); +} + +static void altera_mbox_tx_intmask (struct altera_mbox *mbox,bool enable) +{ +u32 mask; + + mask =readl (mbox->mbox_base +MAILBOX_INTMASK_REG ); +if (enable) + mask |=INT_SPACE_MSK ; +else + mask &= ~INT_SPACE_MSK ; +writel (mask, mbox->mbox_base +MAILBOX_INTMASK_REG ); +} + +static bool altera_mbox_is_sender (struct altera_mbox *mbox) +{ +u32 reg; +/* Write a magic number to PTR register and read back this register. +* This register is read-write if it is a sender. +*/ +#define MBOX_MAGIC 0xA5A5AA55 +writel (MBOX_MAGIC , mbox->mbox_base +MAILBOX_PTR_REG ); + reg =readl (mbox->mbox_base +MAILBOX_PTR_REG ); +if (reg ==MBOX_MAGIC ) { +/* Clear to 0 */ +writel (0 , mbox->mbox_base +MAILBOX_PTR_REG ); +return true; + } +return false; +} + +static void altera_mbox_rx_data (struct mbox_chan *chan) +{ +struct altera_mbox *mbox =mbox_chan_to_altera_mbox (chan); +u32 data[2 ]; + +if (altera_mbox_pending (mbox)) { + data[MBOX_PTR ] =readl (mbox->mbox_base +MAILBOX_PTR_REG ); + data[MBOX_CMD ] =readl (mbox->mbox_base +MAILBOX_CMD_REG ); +mbox_chan_received_data (chan, (void *)data); + } +} + +static void altera_mbox_poll_rx (unsigned long data) +{ +struct mbox_chan *chan = (struct mbox_chan *)data; +struct altera_mbox *mbox =mbox_chan_to_altera_mbox (chan); + +altera_mbox_rx_data (chan); + +mod_timer (&mbox->rxpoll_timer, + jiffies +msecs_to_jiffies (MBOX_POLLING_MS )); +} + +static irqreturn_t altera_mbox_tx_interrupt (int irq,void *p) +{ +struct mbox_chan *chan = (struct mbox_chan *)p; +struct altera_mbox *mbox =mbox_chan_to_altera_mbox (chan); + +altera_mbox_tx_intmask (mbox, false); +mbox_chan_txdone (chan,0 ); + +return IRQ_HANDLED ; +} + +static irqreturn_t altera_mbox_rx_interrupt (int irq,void *p) +{ +struct mbox_chan *chan = (struct mbox_chan *)p; + +altera_mbox_rx_data (chan); +return IRQ_HANDLED ; +} + +static int altera_mbox_startup_sender (struct mbox_chan *chan) +{ +int ret; +struct altera_mbox *mbox =mbox_chan_to_altera_mbox (chan); + +if (mbox->intr_mode) { + ret =request_irq (mbox->irq, altera_mbox_tx_interrupt,0 , +DRIVER_NAME , chan); +if (unlikely (ret)) { +dev_err (mbox->dev, +"failed to register mailbox interrupt:%d\n" , + ret); +return ret; + } + } + +return 0 ; +} + +static int altera_mbox_startup_receiver (struct mbox_chan *chan) +{ +int ret; +struct altera_mbox *mbox =mbox_chan_to_altera_mbox (chan); + +if (mbox->intr_mode) { + ret = request_irq(mbox->irq, altera_mbox_rx_interrupt, 0,
Quoted 8 lines
drivers/mailbox/mailbox-altera.c line 181
Quoted 7 lines
drivers/mailbox/mailbox-altera.c new file +6
-0+ DRIVER_NAME, chan); +if (unlikely (ret)) { +dev_err (mbox->dev, +"failed to register mailbox interrupt:%d\n" , + ret); +return ret;Is this really fatal? Maybe continue using the timer scheme below?
Yes, can change to polling mode.
Quoted 15 lines
Quoted 4 lines
drivers/mailbox/mailbox-altera.c new file +3
-0+ } +altera_mbox_rx_intmask (mbox, true); + }else {drivers/mailbox/mailbox-altera.c line 213
Quoted 10 lines
drivers/mailbox/mailbox-altera.c new file +10
-0+/* Setup polling timer */ +setup_timer (&mbox->rxpoll_timer, altera_mbox_poll_rx, + (unsigned long )chan); +mod_timer (&mbox->rxpoll_timer, + jiffies +msecs_to_jiffies (MBOX_POLLING_MS )); + } + +return 0 ; +} +
Regards
Ley Foon
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html