summaryrefslogtreecommitdiff
path: root/drivers/uio/fsl_sec_uio.c
blob: 34f088de1c21525617e9c52edf639d3056bb756d (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * Copyright 2012-2013 Freescale Semiconductor, Inc.
 *
 * Author: Po Liu <po.liu@freescale.com>
 *
 * This program 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.
 *
 * This program 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 this program; if not, write  to the Free Software Foundation, Inc.,
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of_platform.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/io.h>
#include <linux/uio_driver.h>
#include <linux/slab.h>
#include <linux/list.h>

static const char sec_uio_version[] = "fsl SEC UIO driver v1.0";

#define NAME_LENGTH 30
#define JR_INDEX_OFFSET 12

static const char uio_device_name[NAME_LENGTH] = "fsl-sec";
static LIST_HEAD(sec_list);

struct sec_uio_info {
	atomic_t ref; /* exclusive, only one open() at a time */
	struct uio_info uio;
	char name[NAME_LENGTH];
};

struct sec_dev {
	u32 revision;
	u32 index;
	u32 irq;
	void __iomem *global_regs;
	struct device *dev;
	struct resource res;
	struct sec_uio_info info;
	struct list_head node;
	struct list_head jr_list;
};

struct sec_job_ring {
	struct list_head list_node;
	u32 index;
	u32 irq;
	struct device *dev;
	struct sec_uio_info info;
	struct resource *res;
};

static int sec_uio_open(struct uio_info *info, struct inode *inode)
{
	struct sec_uio_info *uio_info = container_of(info,
					struct sec_uio_info, uio);

	if (!atomic_dec_and_test(&uio_info->ref)) {
		pr_err("%s: failing non-exclusive open()\n", uio_info->name);
		atomic_inc(&uio_info->ref);
		return -EBUSY;
	}

	return 0;
}

static int sec_uio_release(struct uio_info *info, struct inode *inode)
{
	struct sec_uio_info *uio_info = container_of(info,
					struct sec_uio_info, uio);
	atomic_inc(&uio_info->ref);

	return 0;
}

static irqreturn_t sec_uio_irq_handler(int irq, struct uio_info *dev_info)
{
	return IRQ_NONE;
}

static int sec_uio_irqcontrol(struct uio_info *dev_info, int irqon)
{
	return 0;
}

static irqreturn_t sec_jr_irq_handler(int irq, struct uio_info *dev_info)
{
	return IRQ_NONE;
}

static int sec_jr_irqcontrol(struct uio_info *dev_info, int irqon)
{
	return 0;
}

static int __init sec_uio_init(struct sec_dev *uio_dev)
{
	int ret;
	struct sec_uio_info *info;

	info = &uio_dev->info;
	atomic_set(&info->ref, 1);
	info->uio.version = sec_uio_version;
	info->uio.name = uio_dev->info.name;
	info->uio.mem[0].name = "sec config space";
	info->uio.mem[0].addr = uio_dev->res.start;
	info->uio.mem[0].size = uio_dev->res.end - uio_dev->res.start + 1;
	info->uio.mem[0].internal_addr = uio_dev->global_regs;
	info->uio.mem[0].memtype = UIO_MEM_PHYS;
	info->uio.irq = uio_dev->irq;
	info->uio.irq_flags = IRQF_SHARED;
	info->uio.handler = sec_uio_irq_handler;
	info->uio.irqcontrol = sec_uio_irqcontrol;
	info->uio.open = sec_uio_open;
	info->uio.release = sec_uio_release;
	info->uio.priv = uio_dev;

	ret = uio_register_device(uio_dev->dev, &info->uio);
	if (ret) {
		pr_err("sec_uio: UIO registration failed\n");
		return ret;
	}

	return 0;
}

static int __init sec_jr_uio_init(struct sec_job_ring *jr)
{
	int ret;
	struct sec_uio_info *info;

	info = &jr->info;
	atomic_set(&info->ref, 1);
	info->uio.version = sec_uio_version;
	info->uio.name = jr->info.name;
	info->uio.mem[0].name = "sec job ring";
	info->uio.mem[0].addr = jr->res->start;
	info->uio.mem[0].size = jr->res->end - jr->res->start + 1;
	info->uio.mem[0].memtype = UIO_MEM_PHYS;
	info->uio.irq = jr->irq;
	info->uio.irq_flags = IRQF_SHARED;
	info->uio.handler = sec_jr_irq_handler;
	info->uio.irqcontrol = sec_jr_irqcontrol;
	info->uio.open = sec_uio_open;
	info->uio.release = sec_uio_release;
	info->uio.priv = jr;

	ret = uio_register_device(jr->dev, &info->uio);
	if (ret) {
		pr_err("sec_jr_uio: UIO registration failed\n");
		return ret;
	}

	return 0;
}

static int fsl_sec_jr_probe(struct device_node *jr_node,
			     struct sec_dev *scdev)
{
	struct sec_job_ring *jr;
	struct resource regs;
	int ret;

	if (!jr_node || !scdev)
		return -EINVAL;

	jr = kzalloc(sizeof(*jr), GFP_KERNEL);
	if (!jr) {
		dev_err(scdev->dev, "Can't alloc memory for job ring\n");
		return -ENOMEM;
	}

	jr->dev = scdev->dev;

	ret = of_address_to_resource(jr_node, 0, &regs);
	if (unlikely(ret < 0)) {
		dev_err(jr->dev, "Can't get property 'reg'\n");
		ret = -EFAULT;
		goto abort_jr;
	}

	jr->index = (regs.start >> JR_INDEX_OFFSET) & 0xf;
	snprintf(jr->info.name, sizeof(jr->info.name)-1,
		 "sec_job_ring%d-%d", scdev->index, jr->index);

	jr->res = devm_request_mem_region(scdev->dev, regs.start,
					  regs.end + 1 - regs.start,
					  jr->info.name);
	if (unlikely(!jr->res)) {
		dev_err(jr->dev, "devm_request_mem_region failed\n");
		ret = -ENOMEM;
		goto abort_jr;
	}

	dev_dbg(jr->dev,
		 "sec_job_ring%d-%d reg start 0x%016llx, size 0x%016llx.\n",
		 scdev->index, jr->index, jr->res->start,
		 jr->res->end + 1 - jr->res->start);

	jr->irq = irq_of_parse_and_map(jr_node, 0);
	dev_dbg(jr->dev, "errirq: %d\n", jr->irq);

	ret = sec_jr_uio_init(jr);
	if (ret)
		goto abort_jr;

	list_add(&jr->list_node, &scdev->jr_list);
	dev_info(jr->dev, "sec_job_ring%d-%d initialized.\n",
			scdev->index, jr->index);

	return 0;
abort_jr:
	kfree(jr);
	return ret;
}

static int fsl_sec_jr_remove(struct sec_job_ring *jr)
{
	if (!jr)
		return 0;
	uio_unregister_device(&jr->info.uio);
	kfree(jr);

	return 0;
}

static const struct of_device_id jr_ids[] = {
	{ .compatible = "fsl,sec-v4.0-job-ring", },
	{ .compatible = "fsl,sec-v4.4-job-ring", },
	{ .compatible = "fsl,sec-v5.0-job-ring", },
	{ .compatible = "fsl,sec-v6.0-job-ring", },
	{},
};

static int fsl_sec_probe(struct platform_device *dev)
{
	struct resource regs;
	struct sec_dev *sec_dev;
	struct device_node *sec_node, *child;
	struct sec_job_ring *jr, *tmp;
	int ret, count = 0;
	struct list_head *p;

	sec_node = dev->dev.of_node;
	if (!sec_node) {
		dev_err(&dev->dev, "Device OF-Node is NULL");
		return -EFAULT;
	}

	sec_dev = kzalloc(sizeof(struct sec_dev), GFP_KERNEL);
	if (!sec_dev)
		return -ENOMEM;

	/* Creat name and index */
	list_for_each(p, &sec_list) {
		count++;
	}
	sec_dev->index = count;

	snprintf(sec_dev->info.name, sizeof(sec_dev->info.name) - 1,
			"%s%d", uio_device_name, sec_dev->index);

	sec_dev->dev = &dev->dev;
	platform_set_drvdata(dev, sec_dev);

	dev_info(sec_dev->dev, "UIO device full name %s initialized\n",
			sec_dev->info.name);

	/* Create each jr under this sec node */
	INIT_LIST_HEAD(&sec_dev->jr_list);
	for_each_child_of_node(sec_node, child) {
		if (of_match_node(jr_ids, child))
			fsl_sec_jr_probe(child, sec_dev);
	}

	/* Get the resource from dtb node */
	ret = of_address_to_resource(sec_node, 0, &regs);
	if (unlikely(ret < 0)) {
		ret = -EFAULT;
		goto abort;
	}

	sec_dev->res = regs;

	sec_dev->global_regs = of_iomap(sec_node, 0);

	sec_dev->irq = irq_of_parse_and_map(sec_node, 0);
	dev_dbg(sec_dev->dev, "errirq: %d\n", sec_dev->irq);

	/* Register UIO */
	ret = sec_uio_init(sec_dev);
	if (ret)
		goto abort_iounmap;

	list_add_tail(&sec_dev->node, &sec_list);

	return 0;

abort_iounmap:
	iounmap(sec_dev->global_regs);
abort:
	list_for_each_entry_safe(jr, tmp, &sec_dev->jr_list, list_node) {
		list_del(&jr->list_node);
		fsl_sec_jr_remove(jr);
	}
	kfree(sec_dev);
	return ret;
}

static int fsl_sec_remove(struct platform_device *dev)
{
	struct sec_dev *sec_dev = platform_get_drvdata(dev);
	struct sec_job_ring *jr, *tmp;

	if (!sec_dev)
		return 0;

	list_for_each_entry_safe(jr, tmp, &sec_dev->jr_list, list_node) {
		list_del(&jr->list_node);
		fsl_sec_jr_remove(jr);
	}

	list_del(&sec_dev->node);
	uio_unregister_device(&sec_dev->info.uio);
	platform_set_drvdata(dev, NULL);
	iounmap(sec_dev->global_regs);
	kfree(sec_dev);

	return 0;
}

static const struct of_device_id sec_ids[] = {
	{ .compatible = "fsl,sec-v4.0", },
	{ .compatible = "fsl,sec-v4.4", },
	{ .compatible = "fsl,sec-v5.0", },
	{ .compatible = "fsl,sec-v6.0", },
	{},
};

static struct platform_driver fsl_sec_driver = {
	.driver = {
		.name = "fsl-sec-uio",
		.owner = THIS_MODULE,
		.of_match_table = sec_ids,
	},
	.probe = fsl_sec_probe,
	.remove = fsl_sec_remove,
};

static __init int fsl_sec_init(void)
{
	int ret;

	ret = platform_driver_register(&fsl_sec_driver);
	if (unlikely(ret < 0))
		pr_warn(": %s:%hu:%s(): platform_driver_register() = %d\n",
			__FILE__, __LINE__, __func__, ret);

	return ret;
}

static void __exit fsl_sec_exit(void)
{
	platform_driver_unregister(&fsl_sec_driver);
}

module_init(fsl_sec_init);
module_exit(fsl_sec_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Liu Po <po.liu@freescale.com>");
MODULE_DESCRIPTION("FSL SEC UIO Driver");