drivers/mailbox/ast2700-mailbox.c

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

File Facts

System
Linux kernel
Corpus path
drivers/mailbox/ast2700-mailbox.c
Extension
.c
Size
6029 bytes
Lines
236
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 ast2700_mbox_data {
	u8 num_chans;
	u8 msg_size;
};

struct ast2700_mbox {
	struct mbox_controller mbox;
	u8 msg_size;
	void __iomem *tx_regs;
	void __iomem *rx_regs;
	spinlock_t lock;
};

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

static inline bool ast2700_mbox_tx_done(struct ast2700_mbox *mb, int idx)
{
	return !(readl(mb->tx_regs + IPCR_STATUS) & BIT(idx));
}

static irqreturn_t ast2700_mbox_irq(int irq, void *p)
{
	struct ast2700_mbox *mb = p;
	void __iomem *data_reg;
	int num_words = mb->msg_size / sizeof(u32);
	u32 *word_data;
	u32 status;
	int n, i;

	/* Only examine channels that are currently enabled. */
	status = readl(mb->rx_regs + IPCR_ENABLE) &
		 readl(mb->rx_regs + IPCR_STATUS);

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

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

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

		data_reg = mb->rx_regs + IPCR_DATA + mb->msg_size * n;
		word_data = chan->con_priv;
		/* Read the message data */
		for (i = 0; i < num_words; i++)
			word_data[i] = readl(data_reg + i * sizeof(u32));

		mbox_chan_received_data(chan, chan->con_priv);

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

	return IRQ_HANDLED;
}

static int ast2700_mbox_send_data(struct mbox_chan *chan, void *data)
{
	struct ast2700_mbox *mb = dev_get_drvdata(chan->mbox->dev);
	int idx = ch_num(chan);
	void __iomem *data_reg = mb->tx_regs + IPCR_DATA + mb->msg_size * idx;
	u32 *word_data = data;
	int num_words = mb->msg_size / sizeof(u32);
	int i;

	if (!(readl(mb->tx_regs + IPCR_ENABLE) & BIT(idx))) {
		dev_warn(mb->mbox.dev, "%s: Ch-%d not enabled yet\n", __func__, idx);
		return -ENODEV;
	}

	if (!(ast2700_mbox_tx_done(mb, idx))) {
		dev_warn(mb->mbox.dev, "%s: Ch-%d last data has not finished\n", __func__, idx);
		return -EBUSY;
	}

	/* Write the message data */
	for (i = 0 ; i < num_words; i++)
		writel(word_data[i], data_reg + i * sizeof(u32));

	writel(BIT(idx), mb->tx_regs + IPCR_TX_TRIG);
	dev_dbg(mb->mbox.dev, "%s: Ch-%d sent\n", __func__, idx);

	return 0;
}

static int ast2700_mbox_startup(struct mbox_chan *chan)

Annotation

Implementation Notes