Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / fmc / fmc-core.c
blob573f5471f680148c331c1612503ccbdba880adeb
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2012 CERN (www.cern.ch)
4 * Author: Alessandro Rubini <rubini@gnudd.com>
6 * This work is part of the White Rabbit project, a research effort led
7 * by CERN, the European Institute for Nuclear Research.
8 */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <linux/fmc.h>
15 #include <linux/fmc-sdb.h>
17 #include "fmc-private.h"
19 static int fmc_check_version(unsigned long version, const char *name)
21 if (__FMC_MAJOR(version) != FMC_MAJOR) {
22 pr_err("%s: \"%s\" has wrong major (has %li, expected %i)\n",
23 __func__, name, __FMC_MAJOR(version), FMC_MAJOR);
24 return -EINVAL;
27 if (__FMC_MINOR(version) != FMC_MINOR)
28 pr_info("%s: \"%s\" has wrong minor (has %li, expected %i)\n",
29 __func__, name, __FMC_MINOR(version), FMC_MINOR);
30 return 0;
33 static int fmc_uevent(struct device *dev, struct kobj_uevent_env *env)
35 /* struct fmc_device *fdev = to_fmc_device(dev); */
37 /* FIXME: The MODALIAS */
38 add_uevent_var(env, "MODALIAS=%s", "fmc");
39 return 0;
42 static int fmc_probe(struct device *dev)
44 struct fmc_driver *fdrv = to_fmc_driver(dev->driver);
45 struct fmc_device *fdev = to_fmc_device(dev);
47 return fdrv->probe(fdev);
50 static int fmc_remove(struct device *dev)
52 struct fmc_driver *fdrv = to_fmc_driver(dev->driver);
53 struct fmc_device *fdev = to_fmc_device(dev);
55 return fdrv->remove(fdev);
58 static void fmc_shutdown(struct device *dev)
60 /* not implemented but mandatory */
63 static struct bus_type fmc_bus_type = {
64 .name = "fmc",
65 .match = fmc_match,
66 .uevent = fmc_uevent,
67 .probe = fmc_probe,
68 .remove = fmc_remove,
69 .shutdown = fmc_shutdown,
72 static void fmc_release(struct device *dev)
74 struct fmc_device *fmc = container_of(dev, struct fmc_device, dev);
76 kfree(fmc);
80 * The eeprom is exported in sysfs, through a binary attribute
83 static ssize_t fmc_read_eeprom(struct file *file, struct kobject *kobj,
84 struct bin_attribute *bin_attr,
85 char *buf, loff_t off, size_t count)
87 struct device *dev;
88 struct fmc_device *fmc;
89 int eelen;
91 dev = container_of(kobj, struct device, kobj);
92 fmc = container_of(dev, struct fmc_device, dev);
93 eelen = fmc->eeprom_len;
94 if (off > eelen)
95 return -ESPIPE;
96 if (off == eelen)
97 return 0; /* EOF */
98 if (off + count > eelen)
99 count = eelen - off;
100 memcpy(buf, fmc->eeprom + off, count);
101 return count;
104 static ssize_t fmc_write_eeprom(struct file *file, struct kobject *kobj,
105 struct bin_attribute *bin_attr,
106 char *buf, loff_t off, size_t count)
108 struct device *dev;
109 struct fmc_device *fmc;
111 dev = container_of(kobj, struct device, kobj);
112 fmc = container_of(dev, struct fmc_device, dev);
113 return fmc->op->write_ee(fmc, off, buf, count);
116 static struct bin_attribute fmc_eeprom_attr = {
117 .attr = { .name = "eeprom", .mode = S_IRUGO | S_IWUSR, },
118 .size = 8192, /* more or less standard */
119 .read = fmc_read_eeprom,
120 .write = fmc_write_eeprom,
123 int fmc_irq_request(struct fmc_device *fmc, irq_handler_t h,
124 char *name, int flags)
126 if (fmc->op->irq_request)
127 return fmc->op->irq_request(fmc, h, name, flags);
128 return -EPERM;
130 EXPORT_SYMBOL(fmc_irq_request);
132 void fmc_irq_free(struct fmc_device *fmc)
134 if (fmc->op->irq_free)
135 fmc->op->irq_free(fmc);
137 EXPORT_SYMBOL(fmc_irq_free);
139 void fmc_irq_ack(struct fmc_device *fmc)
141 if (likely(fmc->op->irq_ack))
142 fmc->op->irq_ack(fmc);
144 EXPORT_SYMBOL(fmc_irq_ack);
146 int fmc_validate(struct fmc_device *fmc, struct fmc_driver *drv)
148 if (fmc->op->validate)
149 return fmc->op->validate(fmc, drv);
150 return -EPERM;
152 EXPORT_SYMBOL(fmc_validate);
154 int fmc_gpio_config(struct fmc_device *fmc, struct fmc_gpio *gpio, int ngpio)
156 if (fmc->op->gpio_config)
157 return fmc->op->gpio_config(fmc, gpio, ngpio);
158 return -EPERM;
160 EXPORT_SYMBOL(fmc_gpio_config);
162 int fmc_read_ee(struct fmc_device *fmc, int pos, void *d, int l)
164 if (fmc->op->read_ee)
165 return fmc->op->read_ee(fmc, pos, d, l);
166 return -EPERM;
168 EXPORT_SYMBOL(fmc_read_ee);
170 int fmc_write_ee(struct fmc_device *fmc, int pos, const void *d, int l)
172 if (fmc->op->write_ee)
173 return fmc->op->write_ee(fmc, pos, d, l);
174 return -EPERM;
176 EXPORT_SYMBOL(fmc_write_ee);
179 * Functions for client modules follow
182 int fmc_driver_register(struct fmc_driver *drv)
184 if (fmc_check_version(drv->version, drv->driver.name))
185 return -EINVAL;
186 drv->driver.bus = &fmc_bus_type;
187 return driver_register(&drv->driver);
189 EXPORT_SYMBOL(fmc_driver_register);
191 void fmc_driver_unregister(struct fmc_driver *drv)
193 driver_unregister(&drv->driver);
195 EXPORT_SYMBOL(fmc_driver_unregister);
198 * When a device set is registered, all eeproms must be read
199 * and all FRUs must be parsed
201 int fmc_device_register_n_gw(struct fmc_device **devs, int n,
202 struct fmc_gateware *gw)
204 struct fmc_device *fmc, **devarray;
205 uint32_t device_id;
206 int i, ret = 0;
208 if (n < 1)
209 return 0;
211 /* Check the version of the first data structure (function prints) */
212 if (fmc_check_version(devs[0]->version, devs[0]->carrier_name))
213 return -EINVAL;
215 devarray = kmemdup(devs, n * sizeof(*devs), GFP_KERNEL);
216 if (!devarray)
217 return -ENOMEM;
219 /* Make all other checks before continuing, for all devices */
220 for (i = 0; i < n; i++) {
221 fmc = devarray[i];
222 if (!fmc->hwdev) {
223 pr_err("%s: device nr. %i has no hwdev pointer\n",
224 __func__, i);
225 ret = -EINVAL;
226 break;
228 if (fmc->flags & FMC_DEVICE_NO_MEZZANINE) {
229 dev_info(fmc->hwdev, "absent mezzanine in slot %d\n",
230 fmc->slot_id);
231 continue;
233 if (!fmc->eeprom) {
234 dev_err(fmc->hwdev, "no eeprom provided for slot %i\n",
235 fmc->slot_id);
236 ret = -EINVAL;
238 if (!fmc->eeprom_addr) {
239 dev_err(fmc->hwdev, "no eeprom_addr for slot %i\n",
240 fmc->slot_id);
241 ret = -EINVAL;
243 if (!fmc->carrier_name || !fmc->carrier_data ||
244 !fmc->device_id) {
245 dev_err(fmc->hwdev,
246 "device nr %i: carrier name, "
247 "data or dev_id not set\n", i);
248 ret = -EINVAL;
250 if (ret)
251 break;
254 if (ret) {
255 kfree(devarray);
256 return ret;
259 /* Validation is ok. Now init and register the devices */
260 for (i = 0; i < n; i++) {
261 fmc = devarray[i];
263 fmc->nr_slots = n; /* each slot must know how many are there */
264 fmc->devarray = devarray;
266 device_initialize(&fmc->dev);
267 fmc->dev.release = fmc_release;
268 fmc->dev.parent = fmc->hwdev;
270 /* Fill the identification stuff (may fail) */
271 fmc_fill_id_info(fmc);
273 fmc->dev.bus = &fmc_bus_type;
275 /* Name from mezzanine info or carrier info. Or 0,1,2.. */
276 device_id = fmc->device_id;
277 if (!fmc->mezzanine_name)
278 dev_set_name(&fmc->dev, "fmc-%04x", device_id);
279 else
280 dev_set_name(&fmc->dev, "%s-%04x", fmc->mezzanine_name,
281 device_id);
283 if (gw) {
285 * The carrier already know the bitstream to load
286 * for this set of FMC mezzanines.
288 ret = fmc->op->reprogram_raw(fmc, NULL,
289 gw->bitstream, gw->len);
290 if (ret) {
291 dev_warn(fmc->hwdev,
292 "Invalid gateware for FMC mezzanine\n");
293 goto out;
297 ret = device_add(&fmc->dev);
298 if (ret < 0) {
299 dev_err(fmc->hwdev, "Slot %i: Failed in registering "
300 "\"%s\"\n", fmc->slot_id, fmc->dev.kobj.name);
301 goto out;
303 ret = sysfs_create_bin_file(&fmc->dev.kobj, &fmc_eeprom_attr);
304 if (ret < 0) {
305 dev_err(&fmc->dev, "Failed in registering eeprom\n");
306 goto out1;
308 /* This device went well, give information to the user */
309 fmc_dump_eeprom(fmc);
310 fmc_debug_init(fmc);
312 return 0;
314 out1:
315 device_del(&fmc->dev);
316 out:
317 kfree(devarray);
318 for (i--; i >= 0; i--) {
319 fmc_debug_exit(devs[i]);
320 sysfs_remove_bin_file(&devs[i]->dev.kobj, &fmc_eeprom_attr);
321 device_del(&devs[i]->dev);
322 fmc_free_id_info(devs[i]);
323 put_device(&devs[i]->dev);
325 return ret;
328 EXPORT_SYMBOL(fmc_device_register_n_gw);
330 int fmc_device_register_n(struct fmc_device **devs, int n)
332 return fmc_device_register_n_gw(devs, n, NULL);
334 EXPORT_SYMBOL(fmc_device_register_n);
336 int fmc_device_register_gw(struct fmc_device *fmc, struct fmc_gateware *gw)
338 return fmc_device_register_n_gw(&fmc, 1, gw);
340 EXPORT_SYMBOL(fmc_device_register_gw);
342 int fmc_device_register(struct fmc_device *fmc)
344 return fmc_device_register_n(&fmc, 1);
346 EXPORT_SYMBOL(fmc_device_register);
348 void fmc_device_unregister_n(struct fmc_device **devs, int n)
350 int i;
352 if (n < 1)
353 return;
355 /* Free devarray first, not used by the later loop */
356 kfree(devs[0]->devarray);
358 for (i = 0; i < n; i++) {
359 fmc_debug_exit(devs[i]);
360 sysfs_remove_bin_file(&devs[i]->dev.kobj, &fmc_eeprom_attr);
361 device_del(&devs[i]->dev);
362 fmc_free_id_info(devs[i]);
363 put_device(&devs[i]->dev);
366 EXPORT_SYMBOL(fmc_device_unregister_n);
368 void fmc_device_unregister(struct fmc_device *fmc)
370 fmc_device_unregister_n(&fmc, 1);
372 EXPORT_SYMBOL(fmc_device_unregister);
374 /* Init and exit are trivial */
375 static int fmc_init(void)
377 return bus_register(&fmc_bus_type);
380 static void fmc_exit(void)
382 bus_unregister(&fmc_bus_type);
385 module_init(fmc_init);
386 module_exit(fmc_exit);
388 MODULE_LICENSE("GPL");