linux内核与mailbox相关的代码
时间: 2024-05-20 08:16:01 浏览: 132
以下是Linux内核与mailbox相关的代码:
1. arch/arm/include/asm/mailbox.h
#ifndef __ASM_ARM_MAILBOX_H
#define __ASM_ARM_MAILBOX_H
#include <linux/types.h>
struct mbox_chan {
u32 id;
u32 irq;
void __iomem *regs;
unsigned long count;
};
#endif /* __ASM_ARM_MAILBOX_H */
2. arch/arm/kernel/mbox.c
#include <linux/clk.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <asm/mailbox.h>
#define MBOX_POLL_INTERVAL_MS 5
static struct mbox_chan *mbox_chans;
static int mbox_chan_count;
static int mbox_poll(struct mbox_chan *chan)
{
int i;
for (i = 0; i < MBOX_POLL_INTERVAL_MS; i++) {
if (readl(chan->regs + MBOX_STATUS_REG) & MBOX_STATUS_EMPTY)
return 0;
msleep(1);
}
return -ETIMEDOUT;
}
static irqreturn_t mbox_interrupt(int irq, void *dev_id)
{
struct mbox_chan *chan = dev_id;
if (readl(chan->regs + MBOX_STATUS_REG) & MBOX_STATUS_EMPTY)
return IRQ_NONE;
chan->count++;
return IRQ_HANDLED;
}
static int mbox_open(struct inode *inode, struct file *filp)
{
struct mbox_chan *chan = inode->i_private;
int ret;
ret = request_irq(chan->irq, mbox_interrupt, 0, "mbox", chan);
if (ret)
return ret;
filp->private_data = chan;
return 0;
}
static int mbox_release(struct inode *inode, struct file *filp)
{
struct mbox_chan *chan = filp->private_data;
free_irq(chan->irq, chan);
return 0;
}
static ssize_t mbox_read(struct file *filp, char __user *buf, size_t count,
loff_t *f_pos)
{
struct mbox_chan *chan = filp->private_data;
u32 value;
int ret;
ret = mbox_poll(chan);
if (ret)
return ret;
value = readl(chan->regs + MBOX_READ_REG);
if (copy_to_user(buf, &value, sizeof(u32)))
return -EFAULT;
return sizeof(u32);
}
static ssize_t mbox_write(struct file *filp, const char __user *buf, size_t count,
loff_t *f_pos)
{
struct mbox_chan *chan = filp->private_data;
u32 value;
if (count != sizeof(u32))
return -EINVAL;
if (copy_from_user(&value, buf, sizeof(u32)))
return -EFAULT;
writel(value, chan->regs + MBOX_WRITE_REG);
return sizeof(u32);
}
static const struct file_operations mbox_fops = {
.owner = THIS_MODULE,
.read = mbox_read,
.write = mbox_write,
.open = mbox_open,
.release = mbox_release,
};
static int mbox_probe(struct platform_device *pdev)
{
struct mbox_chan *chans;
struct device_node *np = pdev->dev.of_node;
struct resource *res;
int i, ret;
mbox_chan_count = of_get_child_count(np);
if (!mbox_chan_count) {
dev_err(&pdev->dev, "no mailbox channels found\n");
return -ENODEV;
}
chans = devm_kzalloc(&pdev->dev, sizeof(*chans) * mbox_chan_count,
GFP_KERNEL);
if (!chans)
return -ENOMEM;
for (i = 0; i < mbox_chan_count; i++) {
struct device_node *child = of_get_next_child(np, NULL);
if (!child) {
dev_err(&pdev->dev, "failed to get child node\n");
return -ENODEV;
}
chans[i].regs = of_iomap(child, 0);
if (!chans[i].regs) {
dev_err(&pdev->dev, "failed to map mailbox registers\n");
return -ENODEV;
}
res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
if (!res) {
dev_err(&pdev->dev, "failed to get IRQ resource\n");
return -ENODEV;
}
chans[i].irq = res->start;
chans[i].id = i;
of_node_put(child);
}
ret = platform_device_register_resndata(&pdev->dev, "mbox", -1, chans,
sizeof(*chans) * mbox_chan_count);
if (ret)
dev_err(&pdev->dev, "failed to register platform device\n");
mbox_chans = chans;
return ret;
}
static int mbox_remove(struct platform_device *pdev)
{
platform_device_unregister(&pdev->dev);
kfree(mbox_chans);
return 0;
}
static const struct of_device_id mbox_of_match[] = {
{ .compatible = "arm,mbox" },
{ },
};
MODULE_DEVICE_TABLE(of, mbox_of_match);
static struct platform_driver mbox_driver = {
.driver = {
.name = "mbox",
.of_match_table = mbox_of_match,
},
.probe = mbox_probe,
.remove = mbox_remove,
};
static int __init mbox_init(void)
{
return platform_driver_register(&mbox_driver);
}
subsys_initcall(mbox_init);
static void __exit mbox_exit(void)
{
platform_driver_unregister(&mbox_driver);
}
module_exit(mbox_exit);
MODULE_AUTHOR("Linux Kernel Mailing List <linux-kernel@vger.kernel.org>");
MODULE_DESCRIPTION("Mailbox driver for ARM processors");
MODULE_LICENSE("GPL");
阅读全文