treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 60
[linux-2.6-microblaze.git] / drivers / fmc / fmc-core.c
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>
5  *
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>
16
17 #include "fmc-private.h"
18
19 static int fmc_check_version(unsigned long version, const char *name)
20 {
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;
25         }
26
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;
31 }
32
33 static int fmc_uevent(struct device *dev, struct kobj_uevent_env *env)
34 {
35         /* struct fmc_device *fdev = to_fmc_device(dev); */
36
37         /* FIXME: The MODALIAS */
38         add_uevent_var(env, "MODALIAS=%s", "fmc");
39         return 0;
40 }
41
42 static int fmc_probe(struct device *dev)
43 {
44         struct fmc_driver *fdrv = to_fmc_driver(dev->driver);
45         struct fmc_device *fdev = to_fmc_device(dev);
46
47         return fdrv->probe(fdev);
48 }
49
50 static int fmc_remove(struct device *dev)
51 {
52         struct fmc_driver *fdrv = to_fmc_driver(dev->driver);
53         struct fmc_device *fdev = to_fmc_device(dev);
54
55         return fdrv->remove(fdev);
56 }
57
58 static void fmc_shutdown(struct device *dev)
59 {
60         /* not implemented but mandatory */
61 }
62
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,
70 };
71
72 static void fmc_release(struct device *dev)
73 {
74         struct fmc_device *fmc = container_of(dev, struct fmc_device, dev);
75
76         kfree(fmc);
77 }
78
79 /*
80  * The eeprom is exported in sysfs, through a binary attribute
81  */
82
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)
86 {
87         struct device *dev;
88         struct fmc_device *fmc;
89         int eelen;
90
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;
102 }
103
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)
107 {
108         struct device *dev;
109         struct fmc_device *fmc;
110
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);
114 }
115
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,
121 };
122
123 int fmc_irq_request(struct fmc_device *fmc, irq_handler_t h,
124                     char *name, int flags)
125 {
126         if (fmc->op->irq_request)
127                 return fmc->op->irq_request(fmc, h, name, flags);
128         return -EPERM;
129 }
130 EXPORT_SYMBOL(fmc_irq_request);
131
132 void fmc_irq_free(struct fmc_device *fmc)
133 {
134         if (fmc->op->irq_free)
135                 fmc->op->irq_free(fmc);
136 }
137 EXPORT_SYMBOL(fmc_irq_free);
138
139 void fmc_irq_ack(struct fmc_device *fmc)
140 {
141         if (likely(fmc->op->irq_ack))
142                 fmc->op->irq_ack(fmc);
143 }
144 EXPORT_SYMBOL(fmc_irq_ack);
145
146 int fmc_validate(struct fmc_device *fmc, struct fmc_driver *drv)
147 {
148         if (fmc->op->validate)
149                 return fmc->op->validate(fmc, drv);
150         return -EPERM;
151 }
152 EXPORT_SYMBOL(fmc_validate);
153
154 int fmc_gpio_config(struct fmc_device *fmc, struct fmc_gpio *gpio, int ngpio)
155 {
156         if (fmc->op->gpio_config)
157                 return fmc->op->gpio_config(fmc, gpio, ngpio);
158         return -EPERM;
159 }
160 EXPORT_SYMBOL(fmc_gpio_config);
161
162 int fmc_read_ee(struct fmc_device *fmc, int pos, void *d, int l)
163 {
164         if (fmc->op->read_ee)
165                 return fmc->op->read_ee(fmc, pos, d, l);
166         return -EPERM;
167 }
168 EXPORT_SYMBOL(fmc_read_ee);
169
170 int fmc_write_ee(struct fmc_device *fmc, int pos, const void *d, int l)
171 {
172         if (fmc->op->write_ee)
173                 return fmc->op->write_ee(fmc, pos, d, l);
174         return -EPERM;
175 }
176 EXPORT_SYMBOL(fmc_write_ee);
177
178 /*
179  * Functions for client modules follow
180  */
181
182 int fmc_driver_register(struct fmc_driver *drv)
183 {
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);
188 }
189 EXPORT_SYMBOL(fmc_driver_register);
190
191 void fmc_driver_unregister(struct fmc_driver *drv)
192 {
193         driver_unregister(&drv->driver);
194 }
195 EXPORT_SYMBOL(fmc_driver_unregister);
196
197 /*
198  * When a device set is registered, all eeproms must be read
199  * and all FRUs must be parsed
200  */
201 int fmc_device_register_n_gw(struct fmc_device **devs, int n,
202                           struct fmc_gateware *gw)
203 {
204         struct fmc_device *fmc, **devarray;
205         uint32_t device_id;
206         int i, ret = 0;
207
208         if (n < 1)
209                 return 0;
210
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;
214
215         devarray = kmemdup(devs, n * sizeof(*devs), GFP_KERNEL);
216         if (!devarray)
217                 return -ENOMEM;
218
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;
227                 }
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;
232                 }
233                 if (!fmc->eeprom) {
234                         dev_err(fmc->hwdev, "no eeprom provided for slot %i\n",
235                                 fmc->slot_id);
236                         ret = -EINVAL;
237                 }
238                 if (!fmc->eeprom_addr) {
239                         dev_err(fmc->hwdev, "no eeprom_addr for slot %i\n",
240                                 fmc->slot_id);
241                         ret = -EINVAL;
242                 }
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;
249                 }
250                 if (ret)
251                         break;
252
253         }
254         if (ret) {
255                 kfree(devarray);
256                 return ret;
257         }
258
259         /* Validation is ok. Now init and register the devices */
260         for (i = 0; i < n; i++) {
261                 fmc = devarray[i];
262
263                 fmc->nr_slots = n; /* each slot must know how many are there */
264                 fmc->devarray = devarray;
265
266                 device_initialize(&fmc->dev);
267                 fmc->dev.release = fmc_release;
268                 fmc->dev.parent = fmc->hwdev;
269
270                 /* Fill the identification stuff (may fail) */
271                 fmc_fill_id_info(fmc);
272
273                 fmc->dev.bus = &fmc_bus_type;
274
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);
282
283                 if (gw) {
284                         /*
285                          * The carrier already know the bitstream to load
286                          * for this set of FMC mezzanines.
287                          */
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;
294                         }
295                 }
296
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;
302                 }
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;
307                 }
308                 /* This device went well, give information to the user */
309                 fmc_dump_eeprom(fmc);
310                 fmc_debug_init(fmc);
311         }
312         return 0;
313
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);
324         }
325         return ret;
326
327 }
328 EXPORT_SYMBOL(fmc_device_register_n_gw);
329
330 int fmc_device_register_n(struct fmc_device **devs, int n)
331 {
332         return fmc_device_register_n_gw(devs, n, NULL);
333 }
334 EXPORT_SYMBOL(fmc_device_register_n);
335
336 int fmc_device_register_gw(struct fmc_device *fmc, struct fmc_gateware *gw)
337 {
338         return fmc_device_register_n_gw(&fmc, 1, gw);
339 }
340 EXPORT_SYMBOL(fmc_device_register_gw);
341
342 int fmc_device_register(struct fmc_device *fmc)
343 {
344         return fmc_device_register_n(&fmc, 1);
345 }
346 EXPORT_SYMBOL(fmc_device_register);
347
348 void fmc_device_unregister_n(struct fmc_device **devs, int n)
349 {
350         int i;
351
352         if (n < 1)
353                 return;
354
355         /* Free devarray first, not used by the later loop */
356         kfree(devs[0]->devarray);
357
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);
364         }
365 }
366 EXPORT_SYMBOL(fmc_device_unregister_n);
367
368 void fmc_device_unregister(struct fmc_device *fmc)
369 {
370         fmc_device_unregister_n(&fmc, 1);
371 }
372 EXPORT_SYMBOL(fmc_device_unregister);
373
374 /* Init and exit are trivial */
375 static int fmc_init(void)
376 {
377         return bus_register(&fmc_bus_type);
378 }
379
380 static void fmc_exit(void)
381 {
382         bus_unregister(&fmc_bus_type);
383 }
384
385 module_init(fmc_init);
386 module_exit(fmc_exit);
387
388 MODULE_LICENSE("GPL");