drivers/mailbox/sun6i-msgbox.c

Source file repositories/reference/linux-study-clean/drivers/mailbox/sun6i-msgbox.c

File Facts

System
Linux kernel
Corpus path
drivers/mailbox/sun6i-msgbox.c
Extension
.c
Size
8788 bytes
Lines
318
Domain
Driver Families
Bucket
drivers/mailbox
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct sun6i_msgbox {
	struct mbox_controller controller;
	struct clk *clk;
	spinlock_t lock;
	void __iomem *regs;
};

static bool sun6i_msgbox_last_tx_done(struct mbox_chan *chan);
static bool sun6i_msgbox_peek_data(struct mbox_chan *chan);

static inline int channel_number(struct mbox_chan *chan)
{
	return chan - chan->mbox->chans;
}

static inline struct sun6i_msgbox *to_sun6i_msgbox(struct mbox_chan *chan)
{
	return chan->con_priv;
}

static irqreturn_t sun6i_msgbox_irq(int irq, void *dev_id)
{
	struct sun6i_msgbox *mbox = dev_id;
	uint32_t status;
	int n;

	/* Only examine channels that are currently enabled. */
	status = readl(mbox->regs + LOCAL_IRQ_EN_REG) &
		 readl(mbox->regs + LOCAL_IRQ_STAT_REG);

	if (!(status & RX_IRQ_MASK))
		return IRQ_NONE;

	for (n = 0; n < NUM_CHANS; ++n) {
		struct mbox_chan *chan = &mbox->controller.chans[n];

		if (!(status & RX_IRQ(n)))
			continue;

		while (sun6i_msgbox_peek_data(chan)) {
			uint32_t msg = readl(mbox->regs + MSG_DATA_REG(n));

			mbox_dbg(mbox, "Channel %d received 0x%08x\n", n, msg);
			mbox_chan_received_data(chan, &msg);
		}

		/* The IRQ can be cleared only once the FIFO is empty. */
		writel(RX_IRQ(n), mbox->regs + LOCAL_IRQ_STAT_REG);
	}

	return IRQ_HANDLED;
}

static int sun6i_msgbox_send_data(struct mbox_chan *chan, void *data)
{
	struct sun6i_msgbox *mbox = to_sun6i_msgbox(chan);
	int n = channel_number(chan);
	uint32_t msg = *(uint32_t *)data;

	/* Using a channel backwards gets the hardware into a bad state. */
	if (WARN_ON_ONCE(!(readl(mbox->regs + CTRL_REG(n)) & CTRL_TX(n))))
		return 0;

	writel(msg, mbox->regs + MSG_DATA_REG(n));
	mbox_dbg(mbox, "Channel %d sent 0x%08x\n", n, msg);

	return 0;
}

static int sun6i_msgbox_startup(struct mbox_chan *chan)
{
	struct sun6i_msgbox *mbox = to_sun6i_msgbox(chan);
	int n = channel_number(chan);

	/* The coprocessor is responsible for setting channel directions. */
	if (readl(mbox->regs + CTRL_REG(n)) & CTRL_RX(n)) {
		/* Flush the receive FIFO. */
		while (sun6i_msgbox_peek_data(chan))
			readl(mbox->regs + MSG_DATA_REG(n));
		writel(RX_IRQ(n), mbox->regs + LOCAL_IRQ_STAT_REG);

		/* Enable the receive IRQ. */
		spin_lock(&mbox->lock);
		writel(readl(mbox->regs + LOCAL_IRQ_EN_REG) | RX_IRQ(n),
		       mbox->regs + LOCAL_IRQ_EN_REG);
		spin_unlock(&mbox->lock);
	}

	mbox_dbg(mbox, "Channel %d startup complete\n", n);

Annotation

Implementation Notes