mailbox.c 4.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Mailbox reservation modules for DSP
 *
 * Copyright (C) 2006 Nokia Corporation
 * Written by: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */

#include <linux/kernel.h>
13 14 15
#include <linux/resource.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <asm/arch/mailbox.h>
#include <asm/arch/irqs.h>
#include <asm/io.h>

#define MAILBOX_ARM2DSP1		0x00
#define MAILBOX_ARM2DSP1b		0x04
#define MAILBOX_DSP2ARM1		0x08
#define MAILBOX_DSP2ARM1b		0x0c
#define MAILBOX_DSP2ARM2		0x10
#define MAILBOX_DSP2ARM2b		0x14
#define MAILBOX_ARM2DSP1_Flag		0x18
#define MAILBOX_DSP2ARM1_Flag		0x1c
#define MAILBOX_DSP2ARM2_Flag		0x20

unsigned long mbox_base;

struct omap_mbox1_fifo {
33 34 35
	unsigned long cmd;
	unsigned long data;
	unsigned long flag;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
};

struct omap_mbox1_priv {
	struct omap_mbox1_fifo tx_fifo;
	struct omap_mbox1_fifo rx_fifo;
};

static inline int mbox_read_reg(unsigned int reg)
{
	return __raw_readw(mbox_base + reg);
}

static inline void mbox_write_reg(unsigned int val, unsigned int reg)
{
	__raw_writew(val, mbox_base + reg);
}

/* msg */
54
static mbox_msg_t omap1_mbox_fifo_read(struct omap_mbox *mbox)
55
{
56 57
	struct omap_mbox1_fifo *fifo =
		&((struct omap_mbox1_priv *)mbox->priv)->rx_fifo;
58 59 60 61 62 63 64 65
	mbox_msg_t msg;

	msg = mbox_read_reg(fifo->data);
	msg |= ((mbox_msg_t) mbox_read_reg(fifo->cmd)) << 16;

	return msg;
}

66
static void
67
omap1_mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
68
{
69
	struct omap_mbox1_fifo *fifo =
70
		&((struct omap_mbox1_priv *)mbox->priv)->tx_fifo;
71 72 73 74 75

	mbox_write_reg(msg & 0xffff, fifo->data);
	mbox_write_reg(msg >> 16, fifo->cmd);
}

76
static int omap1_mbox_fifo_empty(struct omap_mbox *mbox)
77 78 79 80
{
	return 0;
}

81
static int omap1_mbox_fifo_full(struct omap_mbox *mbox)
82
{
83 84 85
	struct omap_mbox1_fifo *fifo =
		&((struct omap_mbox1_priv *)mbox->priv)->rx_fifo;

86 87 88 89
	return (mbox_read_reg(fifo->flag));
}

/* irq */
90
static void
91
omap1_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_type_t irq)
92 93 94 95 96
{
	if (irq == IRQ_RX)
		enable_irq(mbox->irq);
}

97
static void
98
omap1_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_type_t irq)
99 100
{
	if (irq == IRQ_RX)
101
		disable_irq(mbox->irq);
102 103
}

104
static int
105
omap1_mbox_is_irq(struct omap_mbox *mbox, omap_mbox_type_t irq)
106 107 108 109 110 111
{
	if (irq == IRQ_TX)
		return 0;
	return 1;
}

112
static struct omap_mbox_ops omap1_mbox_ops = {
113 114 115 116 117 118 119 120
	.type		= OMAP_MBOX_TYPE1,
	.fifo_read	= omap1_mbox_fifo_read,
	.fifo_write	= omap1_mbox_fifo_write,
	.fifo_empty	= omap1_mbox_fifo_empty,
	.fifo_full	= omap1_mbox_fifo_full,
	.enable_irq	= omap1_mbox_enable_irq,
	.disable_irq	= omap1_mbox_disable_irq,
	.is_irq		= omap1_mbox_is_irq,
121 122
};

123
/* FIXME: the following struct should be created automatically by the user id */
124 125

/* DSP */
126
static struct omap_mbox1_priv omap1_mbox_dsp_priv = {
127
	.tx_fifo = {
128 129 130
		.cmd	= MAILBOX_ARM2DSP1b,
		.data	= MAILBOX_ARM2DSP1,
		.flag	= MAILBOX_ARM2DSP1_Flag,
131 132
	},
	.rx_fifo = {
133 134 135
		.cmd	= MAILBOX_DSP2ARM1b,
		.data	= MAILBOX_DSP2ARM1,
		.flag	= MAILBOX_DSP2ARM1_Flag,
136 137 138 139
	},
};

struct omap_mbox mbox_dsp_info = {
140 141 142
	.name	= "dsp",
	.ops	= &omap1_mbox_ops,
	.priv	= &omap1_mbox_dsp_priv,
143
};
144
EXPORT_SYMBOL(mbox_dsp_info);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

static int __init omap1_mbox_probe(struct platform_device *pdev)
{
	struct resource *res;
	int ret = 0;

	if (pdev->num_resources != 2) {
		dev_err(&pdev->dev, "invalid number of resources: %d\n",
			pdev->num_resources);
		return -ENODEV;
	}

	/* MBOX base */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (unlikely(!res)) {
		dev_err(&pdev->dev, "invalid mem resource\n");
		return -ENODEV;
	}
	mbox_base = res->start;

	/* DSP IRQ */
	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (unlikely(!res)) {
		dev_err(&pdev->dev, "invalid irq resource\n");
		return -ENODEV;
	}
	mbox_dsp_info.irq = res->start;

	ret = omap_mbox_register(&mbox_dsp_info);

	return ret;
}

static int omap1_mbox_remove(struct platform_device *pdev)
{
	omap_mbox_unregister(&mbox_dsp_info);

	return 0;
}

static struct platform_driver omap1_mbox_driver = {
186
	.probe	= omap1_mbox_probe,
187 188 189
	.remove	= omap1_mbox_remove,
	.driver	= {
		.name	= "mailbox",
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	},
};

static int __init omap1_mbox_init(void)
{
	return platform_driver_register(&omap1_mbox_driver);
}

static void __exit omap1_mbox_exit(void)
{
	platform_driver_unregister(&omap1_mbox_driver);
}

module_init(omap1_mbox_init);
module_exit(omap1_mbox_exit);

MODULE_LICENSE("GPL");