summaryrefslogtreecommitdiff
path: root/mailbox-wc.c
blob: 3c7e380818d2a7a2ef26498248329e8629ef8c42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Google Whitechapel Mailbox Driver
 *
 * Copyright (c) 2019 Google LLC
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#define pr_fmt(fmt) "wc-mbox: " fmt

#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/mailbox_controller.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>

/* Register offsets */
#define MB_MCUCTLR 0x0000
#define MB_INTGR0 0x0020
#define MB_INTCR0 0x0024
#define MB_INTMR0 0x0028
#define MB_INTSR0 0x002C
#define MB_INTMSR0 0x0030
#define MB_INTGR1 0x0040
#define MB_INTCR1 0x0044
#define MB_INTMR1 0x0048
#define MB_INTSR1 0x004C
#define MB_INTMSR1 0x0050
#define MB_SHARED(i) (0x0080 + (i * 4))

/* General defines */
#define MBOX_WC_INTERRUPTS 16

static int wc_mbox_probe(struct platform_device *dev);
static int wc_mbox_remove(struct platform_device *dev);

/* Channel ops */
static int wc_mbox_send_data(struct mbox_chan *chan, void *data);
static int wc_mbox_startup(struct mbox_chan *chan);
static void wc_mbox_shutdown(struct mbox_chan *chan);
static bool wc_mbox_last_tx_done(struct mbox_chan *chan);
static bool wc_mbox_peek_data(struct mbox_chan *chan);

struct wc_mbox_prvdata {
	void __iomem *base;
	struct mbox_controller *mbox;
	int interrupt;
	int shared_registers;
};

static const struct of_device_id wc_mbox_match[] = {
	{
		.compatible = "google,mailbox-whitechapel",
	},
	{},
};

static struct platform_driver wc_mbox_driver = {
	.probe = wc_mbox_probe,
	.remove = wc_mbox_remove,
	.driver = {
		.name = "wc-mbox",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(wc_mbox_match),
	},
};

static struct mbox_chan_ops wc_mbox_chan_ops = {
	.send_data = wc_mbox_send_data,
	.startup = wc_mbox_startup,
	.shutdown = wc_mbox_shutdown,
	.last_tx_done = wc_mbox_last_tx_done,
	.peek_data = wc_mbox_peek_data,
};

/* Helper functions */
static int wc_mbox_int_generate(void *base, u8 i)
{
	if (i > MBOX_WC_INTERRUPTS)
		return -EINVAL;

	iowrite32((1 << i), base + MB_INTGR0);
	return 0;
}

static int wc_mbox_int_clear(void *base, u8 i)
{
	if (i > MBOX_WC_INTERRUPTS)
		return -EINVAL;

	iowrite32((1 << i), base + MB_INTCR1);
	return 0;
}

static int wc_mbox_set_interrupt_mask(void *base, u16 mask)
{
	u32 value = mask;

	iowrite32(value, base + MB_INTMR1);
	return 0;
}

static irqreturn_t wc_int_handler(int irq, void *dev)
{
	struct wc_mbox_prvdata *prvdata = dev_get_drvdata(dev);
	struct mbox_controller *mbox = prvdata->mbox;
	struct mbox_chan *chan = &mbox->chans[0];
	u32 data[8];
	u32 status;
	int i;

	for (i = 0; i < min(prvdata->shared_registers, 8); i++)
		data[i] = ioread32(prvdata->base + MB_SHARED(i));

	status = ioread32(prvdata->base + MB_INTSR1);

	for (i = 0; i < MBOX_WC_INTERRUPTS; i++) {
		if ((status & (1 << i)) != 0) {
			if (chan->cl != NULL)
				mbox_chan_received_data(chan, &data);

			wc_mbox_int_clear(prvdata->base, i);
		}
	}

	return IRQ_HANDLED;
}

static int wc_mbox_send_data(struct mbox_chan *chan, void *data)
{
	struct wc_mbox_prvdata *prvdata = chan->con_priv;
	u32 *regs = data;
	u32 status;
	int i;

	/* Wait if the remote has not finished processing the last message */
	if (data) {
		status = ioread32(prvdata->base + MB_INTSR0);
		if (status != 0) {
			pr_debug("Busy with status %x\n", status);
			return -EBUSY;
		}

		for (i = 0; i < prvdata->shared_registers; i++)
			iowrite32(regs[i], prvdata->base + MB_SHARED(i));
	}

	wc_mbox_int_generate(prvdata->base, 0);

	return 0;
}

static int wc_mbox_startup(struct mbox_chan *chan)
{
	struct wc_mbox_prvdata *prvdata = chan->con_priv;

	wc_mbox_set_interrupt_mask(prvdata->base, 0x0000);

	pr_debug("startup for mbox_chan %p\n", chan);

	return 0;
}

static void wc_mbox_shutdown(struct mbox_chan *chan)
{
	struct wc_mbox_prvdata *prvdata = chan->con_priv;

	wc_mbox_set_interrupt_mask(prvdata->base, 0xffff);

	pr_debug("shutdown for mbox_chan %p\n", chan);
}

static bool wc_mbox_last_tx_done(struct mbox_chan *chan)
{
	struct wc_mbox_prvdata *prvdata = chan->con_priv;
	u32 status;

	status = ioread32(prvdata->base + MB_INTSR0);
	if (status != 0)
		pr_debug("Pending status %x\n", status);

	return status == 0;
}

static bool wc_mbox_peek_data(struct mbox_chan *chan)
{
	struct wc_mbox_prvdata *prvdata = chan->con_priv;
	u32 status;

	status = ioread32(prvdata->base + MB_INTSR1);
	return status != 0;
}

static int wc_mbox_probe(struct platform_device *pdev)
{
	struct mbox_controller *mbox;
	struct mbox_chan *channels;
	struct wc_mbox_prvdata *prvdata;
	struct device *dev = &pdev->dev;

	struct resource *base;
	size_t region_size;
	int interrupt;
	int ret;

	/* Check for device tree properties */
	interrupt = platform_get_irq(pdev, 0);
	if (interrupt <= 0) {
		dev_err(dev, "No interrupt for device\n");
		return -EINVAL;
	}

	base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!base) {
		dev_err(dev, "No base address for device\n");
		return -EINVAL;
	}

	mbox = devm_kzalloc(dev, sizeof(*mbox), GFP_KERNEL);
	channels = devm_kzalloc(dev, sizeof(struct mbox_chan), GFP_KERNEL);
	prvdata = devm_kzalloc(dev, sizeof(*prvdata), GFP_KERNEL);

	if (!mbox || !channels || !prvdata)
		return -ENOMEM;

	region_size = ((base->end - base->start) + 1);
	prvdata->base = devm_ioremap_nocache(dev, base->start, region_size);
	prvdata->interrupt = interrupt;
	prvdata->shared_registers = 8;
	prvdata->mbox = mbox;

	mbox->num_chans = 1;
	mbox->txpoll_period = 2;
	mbox->txdone_poll = true;
	mbox->dev = dev;
	mbox->ops = &wc_mbox_chan_ops;

	channels[0].mbox = mbox;
	channels[0].con_priv = prvdata;
	mbox->chans = channels;

	platform_set_drvdata(pdev, prvdata);

	/* Start with interrupts masked */
	wc_mbox_set_interrupt_mask(prvdata->base, 0xffff);

	ret = devm_request_irq(dev, interrupt, wc_int_handler,
			       IRQF_TRIGGER_HIGH, dev_name(dev), dev);
	if (ret != 0) {
		dev_err(dev, "failed to register interrupt handler: %d\n", ret);
		return -ENXIO;
	}

	ret = mbox_controller_register(mbox);
	if (ret != 0) {
		dev_err(dev, "failed to register mailbox controller: %d\n",
			ret);
		return -EINVAL;
	}

	dev_dbg(dev, "probe completed successfully\n");

	return 0;
}

static int wc_mbox_remove(struct platform_device *pdev)
{
	struct wc_mbox_prvdata *prvdata = platform_get_drvdata(pdev);
	struct device *dev = &(pdev->dev);

	if (!prvdata)
		return -EINVAL;

	dev_dbg(dev, "removing mailbox\n");

	mbox_controller_unregister(prvdata->mbox);

	devm_free_irq(dev, prvdata->interrupt, dev);

	devm_iounmap(dev, prvdata->base);

	return 0;
}

/* Module functions */
static int __init mbox_wc_init(void)
{
	pr_debug("init\n");
	platform_driver_register(&wc_mbox_driver);

	return 0;
}

static void __exit mbox_wc_exit(void)
{
	pr_debug("exit\n");
	platform_driver_unregister(&wc_mbox_driver);
}

module_init(mbox_wc_init);
module_exit(mbox_wc_exit);

MODULE_DESCRIPTION("Whitechapel Mailbox Driver");
MODULE_AUTHOR("Craig Dooley (Google)");
MODULE_LICENSE("GPL v2");