summaryrefslogtreecommitdiff
path: root/target/linux/ubicom32/files/drivers/uio/uio_ubicom32ring.c
blob: 654ac4ced86e4394f75769e4db93c063cb9a0b67 (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
/*
 * drivers/uio/uio_ubicom32ring.c
 *
 * Userspace I/O platform driver for Ubicom32 ring buffers
 *
 * (C) Copyright 2009, Ubicom, Inc.
 *
 * This file is part of the Ubicom32 Linux Kernel Port.
 *
 * Based on uio_ubicom32ring.c by Magnus Damm
 *
 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Ubicom32 Linux Kernel Port.  If not,
 * see <http://www.gnu.org/licenses/>.
 */

#include <linux/platform_device.h>
#include <linux/uio_driver.h>
#include <linux/spinlock.h>
#include <linux/bitops.h>
#include <linux/interrupt.h>
#include <linux/stringify.h>

#include <asm/ip5000.h>
#include <asm/ubicom32ring.h>

#define DRIVER_NAME "uio_ubicom32ring"

struct uio_ubicom32ring_data {
	struct uio_info			*uioinfo;

	struct uio_ubicom32ring_regs	*regs;

	/*
	 * IRQ used to kick the ring buffer
	 */
	int				irq_tx;
	int				irq_rx;

	spinlock_t			lock;

	unsigned long			flags;

	char				name[0];
};

static irqreturn_t uio_ubicom32ring_handler(int irq, struct uio_info *dev_info)
{
	struct uio_ubicom32ring_data *priv = dev_info->priv;

	/* Just disable the interrupt in the interrupt controller, and
	 * remember the state so we can allow user space to enable it later.
	 */

	if (!test_and_set_bit(0, &priv->flags))
		disable_irq_nosync(irq);

	return IRQ_HANDLED;
}

static int uio_ubicom32ring_irqcontrol(struct uio_info *dev_info, s32 irq_on)
{
	struct uio_ubicom32ring_data *priv = dev_info->priv;
	unsigned long flags;

	/* Allow user space to enable and disable the interrupt
	 * in the interrupt controller, but keep track of the
	 * state to prevent per-irq depth damage.
	 *
	 * Serialize this operation to support multiple tasks.
	 */

	spin_lock_irqsave(&priv->lock, flags);

	if (irq_on & 2) {
		/*
		 * Kick the ring buffer (if we can)
		 */
		if (priv->irq_tx != 0xFF) {
			ubicom32_set_interrupt(priv->irq_tx);
		}
	}

	if (priv->irq_rx != 0xFF) {
		if (irq_on & 1) {
			if (test_and_clear_bit(0, &priv->flags))
				enable_irq(dev_info->irq);
		} else {
			if (!test_and_set_bit(0, &priv->flags))
				disable_irq(dev_info->irq);
		}
	}

	spin_unlock_irqrestore(&priv->lock, flags);

	return 0;
}

static int uio_ubicom32ring_probe(struct platform_device *pdev)
{
	struct uio_info *uioinfo;
	struct uio_mem *uiomem;
	struct uio_ubicom32ring_data *priv;
	struct uio_ubicom32ring_regs *regs;
	struct resource *mem_resource;
	struct resource *irqtx_resource;
	struct resource *irqrx_resource;
	int ret = -EINVAL;
	int i;

	uioinfo = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
	if (!uioinfo) {
		dev_err(&pdev->dev, "unable to kmalloc\n");
		return -ENOMEM;
	}

	/*
	 * Allocate private data with some string space after
	 */
	i = sizeof(DRIVER_NAME) + 1;
	i += pdev->dev.platform_data ? strlen(pdev->dev.platform_data) : 0;
	priv = kzalloc(sizeof(struct uio_ubicom32ring_data) + i, GFP_KERNEL);
	if (!priv) {
		dev_err(&pdev->dev, "unable to kmalloc\n");
		kfree(uioinfo);
		return -ENOMEM;
	}

	strcpy(priv->name, DRIVER_NAME ":");
	if (pdev->dev.platform_data) {
		strcat(priv->name, pdev->dev.platform_data);
	}
	uioinfo->priv = priv;
	uioinfo->name = priv->name;
	uioinfo->version = "0.1";

	priv->uioinfo = uioinfo;
	spin_lock_init(&priv->lock);
	priv->flags = 0; /* interrupt is enabled to begin with */

	/*
	 * Get our resources, the IRQ_TX and IRQ_RX are optional.
	 */
	priv->irq_tx = 0xFF;
	irqtx_resource = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (irqtx_resource) {
		priv->irq_tx = irqtx_resource->start;
	}

	uioinfo->irq = -1;
	priv->irq_rx = 0xFF;
	irqrx_resource = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
	if (irqrx_resource) {
		priv->irq_rx = irqrx_resource->start;
		uioinfo->irq = priv->irq_rx;
		uioinfo->handler = uio_ubicom32ring_handler;
	}

	mem_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!mem_resource || !mem_resource->start) {
		dev_err(&pdev->dev, "No valid memory resource found\n");
		ret = -ENODEV;
		goto fail;
	}
	regs = (struct uio_ubicom32ring_regs *)mem_resource->start;
	priv->regs = regs;

	if (regs->version != UIO_UBICOM32RING_REG_VERSION) {
		dev_err(&pdev->dev, "version %d not supported\n", regs->version);
		ret = -ENODEV;
		goto fail;
	}

	/*
	 * First range is the shared register space, if we have any
	 */
	uiomem = &uioinfo->mem[0];
	if (regs->regs_size) {
		uiomem->memtype = UIO_MEM_PHYS;
		uiomem->addr = (u32_t)regs->regs;
		uiomem->size = regs->regs_size;
		++uiomem;
		dev_info(&pdev->dev, "regs:%p (%u) / rings: %d found\n", regs->regs, regs->regs_size, regs->num_rings);
	} else {
		dev_info(&pdev->dev, "rings: %d found\n", regs->num_rings);
	}

	/*
	 * The rest of the range correspond to the rings
	 */
	for (i = 0; i < regs->num_rings; i++) {
		dev_info(&pdev->dev, "\t%d: entries:%d ring:%p\n",
			 i, regs->rings[i]->entries, &(regs->rings[i]->ring));
		if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
			dev_warn(&pdev->dev, "device has more than "
					__stringify(MAX_UIO_MAPS)
					" I/O memory resources.\n");
			break;
		}

		uiomem->memtype = UIO_MEM_PHYS;
		uiomem->addr = (u32_t)&(regs->rings[i]->head);
		uiomem->size = (regs->rings[i]->entries * sizeof(u32_t)) +
				sizeof(struct uio_ubicom32ring_desc);
		++uiomem;
	}

	while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
		uiomem->size = 0;
		++uiomem;
	}

	/* This driver requires no hardware specific kernel code to handle
	 * interrupts. Instead, the interrupt handler simply disables the
	 * interrupt in the interrupt controller. User space is responsible
	 * for performing hardware specific acknowledge and re-enabling of
	 * the interrupt in the interrupt controller.
	 *
	 * Interrupt sharing is not supported.
	 */
	uioinfo->irq_flags = IRQF_DISABLED;
	uioinfo->irqcontrol = uio_ubicom32ring_irqcontrol;

	ret = uio_register_device(&pdev->dev, priv->uioinfo);
	if (ret) {
		dev_err(&pdev->dev, "unable to register uio device\n");
		goto fail;
	}

	platform_set_drvdata(pdev, priv);

	dev_info(&pdev->dev, "'%s' using irq: rx %d tx %d, regs %p\n",
		 priv->name, priv->irq_rx, priv->irq_tx, priv->regs);

	return 0;

fail:
	kfree(uioinfo);
	kfree(priv);
	return ret;
}

static int uio_ubicom32ring_remove(struct platform_device *pdev)
{
	struct uio_ubicom32ring_data *priv = platform_get_drvdata(pdev);

	uio_unregister_device(priv->uioinfo);
	kfree(priv->uioinfo);
	kfree(priv);
	return 0;
}

static struct platform_driver uio_ubicom32ring = {
	.probe = uio_ubicom32ring_probe,
	.remove = uio_ubicom32ring_remove,
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
	},
};

static int __init uio_ubicom32ring_init(void)
{
	return platform_driver_register(&uio_ubicom32ring);
}

static void __exit uio_ubicom32ring_exit(void)
{
	platform_driver_unregister(&uio_ubicom32ring);
}

module_init(uio_ubicom32ring_init);
module_exit(uio_ubicom32ring_exit);

MODULE_AUTHOR("Patrick Tjin");
MODULE_DESCRIPTION("Userspace I/O driver for Ubicom32 ring buffers");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:" DRIVER_NAME);