aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mcb
diff options
context:
space:
mode:
authorJohannes Thumshirn <johannes.thumshirn@men.de>2014-02-26 11:29:05 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2014-02-28 18:36:37 -0500
commit3764e82e5150d87b205c10cd78a9c9ab86fbfa51 (patch)
tree07e523fb9cf879b88f43953ec05f982194c512b2 /drivers/mcb
parente5bb7425e5bae76c8950089a946edcba3e0540ab (diff)
drivers: Introduce MEN Chameleon Bus
The MCB (MEN Chameleon Bus) is a Bus specific to MEN Mikroelektronik FPGA based devices. It is used to identify MCB based IP-Cores within an FPGA and provide the necessary framework for instantiating drivers for these devices. Signed-off-by: Johannes Thumshirn <johannes.thumshirn@men.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/mcb')
-rw-r--r--drivers/mcb/Kconfig15
-rw-r--r--drivers/mcb/Makefile5
-rw-r--r--drivers/mcb/mcb-core.c414
-rw-r--r--drivers/mcb/mcb-internal.h116
-rw-r--r--drivers/mcb/mcb-parse.c159
5 files changed, 709 insertions, 0 deletions
diff --git a/drivers/mcb/Kconfig b/drivers/mcb/Kconfig
new file mode 100644
index 000000000000..44c82d68c563
--- /dev/null
+++ b/drivers/mcb/Kconfig
@@ -0,0 +1,15 @@
1#
2# MEN Chameleon Bus (MCB) support
3#
4
5menuconfig MCB
6 tristate "MCB support"
7 default m
8 help
9
10 The MCB (MEN Chameleon Bus) is a Bus specific to MEN Mikroelektronik
11 FPGA based devices. It is used to identify MCB based IP-Cores within
12 an FPGA and provide the necessary framework for instantiating drivers
13 for these devices.
14
15 If build as a module, the module is called mcb.ko
diff --git a/drivers/mcb/Makefile b/drivers/mcb/Makefile
new file mode 100644
index 000000000000..2d9a751e93bd
--- /dev/null
+++ b/drivers/mcb/Makefile
@@ -0,0 +1,5 @@
1
2obj-$(CONFIG_MCB) += mcb.o
3
4mcb-y += mcb-core.o
5mcb-y += mcb-parse.o
diff --git a/drivers/mcb/mcb-core.c b/drivers/mcb/mcb-core.c
new file mode 100644
index 000000000000..bbe12932d404
--- /dev/null
+++ b/drivers/mcb/mcb-core.c
@@ -0,0 +1,414 @@
1/*
2 * MEN Chameleon Bus.
3 *
4 * Copyright (C) 2013 MEN Mikroelektronik GmbH (www.men.de)
5 * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; version 2 of the License.
10 */
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/types.h>
15#include <linux/idr.h>
16#include <linux/mcb.h>
17
18static DEFINE_IDA(mcb_ida);
19
20static const struct mcb_device_id *mcb_match_id(const struct mcb_device_id *ids,
21 struct mcb_device *dev)
22{
23 if (ids) {
24 while (ids->device) {
25 if (ids->device == dev->id)
26 return ids;
27 ids++;
28 }
29 }
30
31 return NULL;
32}
33
34static int mcb_match(struct device *dev, struct device_driver *drv)
35{
36 struct mcb_driver *mdrv = to_mcb_driver(drv);
37 struct mcb_device *mdev = to_mcb_device(dev);
38 const struct mcb_device_id *found_id;
39
40 found_id = mcb_match_id(mdrv->id_table, mdev);
41 if (found_id)
42 return 1;
43
44 return 0;
45}
46
47static int mcb_uevent(struct device *dev, struct kobj_uevent_env *env)
48{
49 struct mcb_device *mdev = to_mcb_device(dev);
50 int ret;
51
52 ret = add_uevent_var(env, "MODALIAS=mcb:16z%03d", mdev->id);
53 if (ret)
54 return -ENOMEM;
55
56 return 0;
57}
58
59static int mcb_probe(struct device *dev)
60{
61 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
62 struct mcb_device *mdev = to_mcb_device(dev);
63 const struct mcb_device_id *found_id;
64
65 found_id = mcb_match_id(mdrv->id_table, mdev);
66 if (!found_id)
67 return -ENODEV;
68
69 return mdrv->probe(mdev, found_id);
70}
71
72static int mcb_remove(struct device *dev)
73{
74 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
75 struct mcb_device *mdev = to_mcb_device(dev);
76
77 mdrv->remove(mdev);
78
79 put_device(&mdev->dev);
80
81 return 0;
82}
83
84static void mcb_shutdown(struct device *dev)
85{
86 struct mcb_device *mdev = to_mcb_device(dev);
87 struct mcb_driver *mdrv = mdev->driver;
88
89 if (mdrv && mdrv->shutdown)
90 mdrv->shutdown(mdev);
91}
92
93static struct bus_type mcb_bus_type = {
94 .name = "mcb",
95 .match = mcb_match,
96 .uevent = mcb_uevent,
97 .probe = mcb_probe,
98 .remove = mcb_remove,
99 .shutdown = mcb_shutdown,
100};
101
102/**
103 * __mcb_register_driver() - Register a @mcb_driver at the system
104 * @drv: The @mcb_driver
105 * @owner: The @mcb_driver's module
106 * @mod_name: The name of the @mcb_driver's module
107 *
108 * Register a @mcb_driver at the system. Perform some sanity checks, if
109 * the .probe and .remove methods are provided by the driver.
110 */
111int __mcb_register_driver(struct mcb_driver *drv, struct module *owner,
112 const char *mod_name)
113{
114 if (!drv->probe || !drv->remove)
115 return -EINVAL;
116
117 drv->driver.owner = owner;
118 drv->driver.bus = &mcb_bus_type;
119 drv->driver.mod_name = mod_name;
120
121 return driver_register(&drv->driver);
122}
123EXPORT_SYMBOL_GPL(__mcb_register_driver);
124
125/**
126 * mcb_unregister_driver() - Unregister a @mcb_driver from the system
127 * @drv: The @mcb_driver
128 *
129 * Unregister a @mcb_driver from the system.
130 */
131void mcb_unregister_driver(struct mcb_driver *drv)
132{
133 driver_unregister(&drv->driver);
134}
135EXPORT_SYMBOL_GPL(mcb_unregister_driver);
136
137static void mcb_release_dev(struct device *dev)
138{
139 struct mcb_device *mdev = to_mcb_device(dev);
140
141 mcb_bus_put(mdev->bus);
142 kfree(mdev);
143}
144
145/**
146 * mcb_device_register() - Register a mcb_device
147 * @bus: The @mcb_bus of the device
148 * @dev: The @mcb_device
149 *
150 * Register a specific @mcb_device at a @mcb_bus and the system itself.
151 */
152int mcb_device_register(struct mcb_bus *bus, struct mcb_device *dev)
153{
154 int ret;
155 int device_id;
156
157 device_initialize(&dev->dev);
158 dev->dev.bus = &mcb_bus_type;
159 dev->dev.parent = bus->dev.parent;
160 dev->dev.release = mcb_release_dev;
161
162 device_id = dev->id;
163 dev_set_name(&dev->dev, "mcb%d-16z%03d-%d:%d:%d",
164 bus->bus_nr, device_id, dev->inst, dev->group, dev->var);
165
166 ret = device_add(&dev->dev);
167 if (ret < 0) {
168 pr_err("Failed registering device 16z%03d on bus mcb%d (%d)\n",
169 device_id, bus->bus_nr, ret);
170 goto out;
171 }
172
173 return 0;
174
175out:
176
177 return ret;
178}
179EXPORT_SYMBOL_GPL(mcb_device_register);
180
181/**
182 * mcb_alloc_bus() - Allocate a new @mcb_bus
183 *
184 * Allocate a new @mcb_bus.
185 */
186struct mcb_bus *mcb_alloc_bus(void)
187{
188 struct mcb_bus *bus;
189 int bus_nr;
190
191 bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL);
192 if (!bus)
193 return NULL;
194
195 bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL);
196 if (bus_nr < 0) {
197 kfree(bus);
198 return ERR_PTR(bus_nr);
199 }
200
201 INIT_LIST_HEAD(&bus->children);
202 bus->bus_nr = bus_nr;
203
204 return bus;
205}
206EXPORT_SYMBOL_GPL(mcb_alloc_bus);
207
208static int __mcb_devices_unregister(struct device *dev, void *data)
209{
210 device_unregister(dev);
211 return 0;
212}
213
214static void mcb_devices_unregister(struct mcb_bus *bus)
215{
216 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister);
217}
218/**
219 * mcb_release_bus() - Free a @mcb_bus
220 * @bus: The @mcb_bus to release
221 *
222 * Release an allocated @mcb_bus from the system.
223 */
224void mcb_release_bus(struct mcb_bus *bus)
225{
226 mcb_devices_unregister(bus);
227
228 ida_simple_remove(&mcb_ida, bus->bus_nr);
229
230 kfree(bus);
231}
232EXPORT_SYMBOL_GPL(mcb_release_bus);
233
234/**
235 * mcb_bus_put() - Increment refcnt
236 * @bus: The @mcb_bus
237 *
238 * Get a @mcb_bus' ref
239 */
240struct mcb_bus *mcb_bus_get(struct mcb_bus *bus)
241{
242 if (bus)
243 get_device(&bus->dev);
244
245 return bus;
246}
247EXPORT_SYMBOL_GPL(mcb_bus_get);
248
249/**
250 * mcb_bus_put() - Decrement refcnt
251 * @bus: The @mcb_bus
252 *
253 * Release a @mcb_bus' ref
254 */
255void mcb_bus_put(struct mcb_bus *bus)
256{
257 if (bus)
258 put_device(&bus->dev);
259}
260EXPORT_SYMBOL_GPL(mcb_bus_put);
261
262/**
263 * mcb_alloc_dev() - Allocate a device
264 * @bus: The @mcb_bus the device is part of
265 *
266 * Allocate a @mcb_device and add bus.
267 */
268struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus)
269{
270 struct mcb_device *dev;
271
272 dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL);
273 if (!dev)
274 return NULL;
275
276 INIT_LIST_HEAD(&dev->bus_list);
277 dev->bus = bus;
278
279 return dev;
280}
281EXPORT_SYMBOL_GPL(mcb_alloc_dev);
282
283/**
284 * mcb_free_dev() - Free @mcb_device
285 * @dev: The device to free
286 *
287 * Free a @mcb_device
288 */
289void mcb_free_dev(struct mcb_device *dev)
290{
291 kfree(dev);
292}
293EXPORT_SYMBOL_GPL(mcb_free_dev);
294
295static int __mcb_bus_add_devices(struct device *dev, void *data)
296{
297 struct mcb_device *mdev = to_mcb_device(dev);
298 int retval;
299
300 if (mdev->is_added)
301 return 0;
302
303 retval = device_attach(dev);
304 if (retval < 0)
305 dev_err(dev, "Error adding device (%d)\n", retval);
306
307 mdev->is_added = true;
308
309 return 0;
310}
311
312static int __mcb_bus_add_child(struct device *dev, void *data)
313{
314 struct mcb_device *mdev = to_mcb_device(dev);
315 struct mcb_bus *child;
316
317 BUG_ON(!mdev->is_added);
318 child = mdev->subordinate;
319
320 if (child)
321 mcb_bus_add_devices(child);
322
323 return 0;
324}
325
326/**
327 * mcb_bus_add_devices() - Add devices in the bus' internal device list
328 * @bus: The @mcb_bus we add the devices
329 *
330 * Add devices in the bus' internal device list to the system.
331 */
332void mcb_bus_add_devices(const struct mcb_bus *bus)
333{
334 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices);
335 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_child);
336
337}
338EXPORT_SYMBOL_GPL(mcb_bus_add_devices);
339
340/**
341 * mcb_request_mem() - Request memory
342 * @dev: The @mcb_device the memory is for
343 * @name: The name for the memory reference.
344 *
345 * Request memory for a @mcb_device. If @name is NULL the driver name will
346 * be used.
347 */
348struct resource *mcb_request_mem(struct mcb_device *dev, const char *name)
349{
350 struct resource *mem;
351 u32 size;
352
353 if (!name)
354 name = dev->dev.driver->name;
355
356 size = resource_size(&dev->mem);
357
358 mem = request_mem_region(dev->mem.start, size, name);
359 if (!mem)
360 return ERR_PTR(-EBUSY);
361
362 return mem;
363}
364EXPORT_SYMBOL_GPL(mcb_request_mem);
365
366/**
367 * mcb_release_mem() - Release memory requested by device
368 * @dev: The @mcb_device that requested the memory
369 *
370 * Release memory that was prior requested via @mcb_request_mem().
371 */
372void mcb_release_mem(struct resource *mem)
373{
374 u32 size;
375
376 size = resource_size(mem);
377 release_mem_region(mem->start, size);
378}
379EXPORT_SYMBOL_GPL(mcb_release_mem);
380
381/**
382 * mcb_get_irq() - Get device's IRQ number
383 * @dev: The @mcb_device the IRQ is for
384 *
385 * Get the IRQ number of a given @mcb_device.
386 */
387int mcb_get_irq(struct mcb_device *dev)
388{
389 struct resource *irq = &dev->irq;
390
391 return irq->start;
392}
393EXPORT_SYMBOL_GPL(mcb_get_irq);
394
395static int mcb_init(void)
396{
397 return bus_register(&mcb_bus_type);
398}
399
400static void mcb_exit(void)
401{
402 bus_unregister(&mcb_bus_type);
403}
404
405/* mcb must be initialized after PCI but before the chameleon drivers.
406 * That means we must use some initcall between subsys_initcall and
407 * device_initcall.
408 */
409fs_initcall(mcb_init);
410module_exit(mcb_exit);
411
412MODULE_DESCRIPTION("MEN Chameleon Bus Driver");
413MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
414MODULE_LICENSE("GPL v2");
diff --git a/drivers/mcb/mcb-internal.h b/drivers/mcb/mcb-internal.h
new file mode 100644
index 000000000000..db22777d0ac8
--- /dev/null
+++ b/drivers/mcb/mcb-internal.h
@@ -0,0 +1,116 @@
1#ifndef __MCB_INTERNAL
2#define __MCB_INTERNAL
3
4#include <linux/types.h>
5
6#define CHAMELEON_FILENAME_LEN 12
7#define CHAMELEONV2_MAGIC 0xabce
8
9enum chameleon_descriptor_type {
10 CHAMELEON_DTYPE_GENERAL = 0x0,
11 CHAMELEON_DTYPE_BRIDGE = 0x1,
12 CHAMELEON_DTYPE_CPU = 0x2,
13 CHAMELEON_DTYPE_BAR = 0x3,
14 CHAMELEON_DTYPE_END = 0xf,
15};
16
17enum chameleon_bus_type {
18 CHAMELEON_BUS_WISHBONE,
19 CHAMELEON_BUS_AVALON,
20 CHAMELEON_BUS_LPC,
21 CHAMELEON_BUS_ISA,
22};
23
24/**
25 * struct chameleon_fpga_header
26 *
27 * @revision: Revison of Chameleon table in FPGA
28 * @model: Chameleon table model ASCII char
29 * @minor: Revision minor
30 * @bus_type: Bus type (usually %CHAMELEON_BUS_WISHBONE)
31 * @magic: Chameleon header magic number (0xabce for version 2)
32 * @reserved: Reserved
33 * @filename: Filename of FPGA bitstream
34 */
35struct chameleon_fpga_header {
36 u8 revision;
37 char model;
38 u8 minor;
39 u8 bus_type;
40 u16 magic;
41 u16 reserved;
42 /* This one has no '\0' at the end!!! */
43 char filename[CHAMELEON_FILENAME_LEN];
44} __packed;
45#define HEADER_MAGIC_OFFSET 0x4
46
47/**
48 * struct chameleon_gdd - Chameleon General Device Descriptor
49 *
50 * @irq: the position in the FPGA's IRQ controller vector
51 * @rev: the revision of the variant's implementation
52 * @var: the variant of the IP core
53 * @dev: the device the IP core is
54 * @dtype: device descriptor type
55 * @bar: BAR offset that must be added to module offset
56 * @inst: the instance number of the device, 0 is first instance
57 * @group: the group the device belongs to (0 = no group)
58 * @reserved: reserved
59 * @offset: beginning of the address window of desired module
60 * @size: size of the module's address window
61 */
62struct chameleon_gdd {
63 __le32 reg1;
64 __le32 reg2;
65 __le32 offset;
66 __le32 size;
67
68} __packed;
69
70/* GDD Register 1 fields */
71#define GDD_IRQ(x) ((x) & 0x1f)
72#define GDD_REV(x) (((x) >> 5) & 0x3f)
73#define GDD_VAR(x) (((x) >> 11) & 0x3f)
74#define GDD_DEV(x) (((x) >> 18) & 0x3ff)
75#define GDD_DTY(x) (((x) >> 28) & 0xf)
76
77/* GDD Register 2 fields */
78#define GDD_BAR(x) ((x) & 0x7)
79#define GDD_INS(x) (((x) >> 3) & 0x3f)
80#define GDD_GRP(x) (((x) >> 9) & 0x3f)
81
82/**
83 * struct chameleon_bdd - Chameleon Bridge Device Descriptor
84 *
85 * @irq: the position in the FPGA's IRQ controller vector
86 * @rev: the revision of the variant's implementation
87 * @var: the variant of the IP core
88 * @dev: the device the IP core is
89 * @dtype: device descriptor type
90 * @bar: BAR offset that must be added to module offset
91 * @inst: the instance number of the device, 0 is first instance
92 * @dbar: destination bar from the bus _behind_ the bridge
93 * @chamoff: offset within the BAR of the source bus
94 * @offset:
95 * @size:
96 */
97struct chameleon_bdd {
98 unsigned int irq:6;
99 unsigned int rev:6;
100 unsigned int var:6;
101 unsigned int dev:10;
102 unsigned int dtype:4;
103 unsigned int bar:3;
104 unsigned int inst:6;
105 unsigned int dbar:3;
106 unsigned int group:6;
107 unsigned int reserved:14;
108 u32 chamoff;
109 u32 offset;
110 u32 size;
111} __packed;
112
113int chameleon_parse_cells(struct mcb_bus *bus, phys_addr_t mapbase,
114 void __iomem *base);
115
116#endif
diff --git a/drivers/mcb/mcb-parse.c b/drivers/mcb/mcb-parse.c
new file mode 100644
index 000000000000..d1278b5f3028
--- /dev/null
+++ b/drivers/mcb/mcb-parse.c
@@ -0,0 +1,159 @@
1#include <linux/types.h>
2#include <linux/ioport.h>
3#include <linux/slab.h>
4#include <linux/export.h>
5#include <linux/io.h>
6#include <linux/mcb.h>
7
8#include "mcb-internal.h"
9
10struct mcb_parse_priv {
11 phys_addr_t mapbase;
12 void __iomem *base;
13};
14
15#define for_each_chameleon_cell(dtype, p) \
16 for ((dtype) = get_next_dtype((p)); \
17 (dtype) != CHAMELEON_DTYPE_END; \
18 (dtype) = get_next_dtype((p)))
19
20static inline uint32_t get_next_dtype(void __iomem *p)
21{
22 uint32_t dtype;
23
24 dtype = readl(p);
25 return dtype >> 28;
26}
27
28static int chameleon_parse_bdd(struct mcb_bus *bus,
29 phys_addr_t mapbase,
30 void __iomem *base)
31{
32 return 0;
33}
34
35static int chameleon_parse_gdd(struct mcb_bus *bus,
36 phys_addr_t mapbase,
37 void __iomem *base)
38{
39 struct chameleon_gdd __iomem *gdd =
40 (struct chameleon_gdd __iomem *) base;
41 struct mcb_device *mdev;
42 u32 offset;
43 u32 size;
44 int ret;
45 __le32 reg1;
46 __le32 reg2;
47
48 mdev = mcb_alloc_dev(bus);
49 if (!mdev)
50 return -ENOMEM;
51
52 reg1 = readl(&gdd->reg1);
53 reg2 = readl(&gdd->reg2);
54 offset = readl(&gdd->offset);
55 size = readl(&gdd->size);
56
57 mdev->id = GDD_DEV(reg1);
58 mdev->rev = GDD_REV(reg1);
59 mdev->var = GDD_VAR(reg1);
60 mdev->bar = GDD_BAR(reg1);
61 mdev->group = GDD_GRP(reg2);
62 mdev->inst = GDD_INS(reg2);
63
64 pr_debug("Found a 16z%03d\n", mdev->id);
65
66 mdev->irq.start = GDD_IRQ(reg1);
67 mdev->irq.end = GDD_IRQ(reg1);
68 mdev->irq.flags = IORESOURCE_IRQ;
69
70 mdev->mem.start = mapbase + offset;
71 mdev->mem.end = mdev->mem.start + size - 1;
72 mdev->mem.flags = IORESOURCE_MEM;
73
74 mdev->is_added = false;
75
76 ret = mcb_device_register(bus, mdev);
77 if (ret < 0)
78 goto err;
79
80 return 0;
81
82err:
83 mcb_free_dev(mdev);
84
85 return ret;
86}
87
88int chameleon_parse_cells(struct mcb_bus *bus, phys_addr_t mapbase,
89 void __iomem *base)
90{
91 char __iomem *p = base;
92 struct chameleon_fpga_header *header;
93 uint32_t dtype;
94 int num_cells = 0;
95 int ret = 0;
96 u32 hsize;
97
98 hsize = sizeof(struct chameleon_fpga_header);
99
100 header = kzalloc(hsize, GFP_KERNEL);
101 if (!header)
102 return -ENOMEM;
103
104 /* Extract header information */
105 memcpy_fromio(header, p, hsize);
106 /* We only support chameleon v2 at the moment */
107 header->magic = le16_to_cpu(header->magic);
108 if (header->magic != CHAMELEONV2_MAGIC) {
109 pr_err("Unsupported chameleon version 0x%x\n",
110 header->magic);
111 kfree(header);
112 return -ENODEV;
113 }
114 p += hsize;
115
116 pr_debug("header->revision = %d\n", header->revision);
117 pr_debug("header->model = 0x%x ('%c')\n", header->model,
118 header->model);
119 pr_debug("header->minor = %d\n", header->minor);
120 pr_debug("header->bus_type = 0x%x\n", header->bus_type);
121
122
123 pr_debug("header->magic = 0x%x\n", header->magic);
124 pr_debug("header->filename = \"%.*s\"\n", CHAMELEON_FILENAME_LEN,
125 header->filename);
126
127 for_each_chameleon_cell(dtype, p) {
128 switch (dtype) {
129 case CHAMELEON_DTYPE_GENERAL:
130 ret = chameleon_parse_gdd(bus, mapbase, p);
131 if (ret < 0)
132 goto out;
133 p += sizeof(struct chameleon_gdd);
134 break;
135 case CHAMELEON_DTYPE_BRIDGE:
136 chameleon_parse_bdd(bus, mapbase, p);
137 p += sizeof(struct chameleon_bdd);
138 break;
139 case CHAMELEON_DTYPE_END:
140 break;
141 default:
142 pr_err("Invalid chameleon descriptor type 0x%x\n",
143 dtype);
144 return -EINVAL;
145 }
146 num_cells++;
147 }
148
149 if (num_cells == 0)
150 num_cells = -EINVAL;
151
152 kfree(header);
153 return num_cells;
154
155out:
156 kfree(header);
157 return ret;
158}
159EXPORT_SYMBOL_GPL(chameleon_parse_cells);