2 * Copyright (C) 2012 CERN (www.cern.ch)
3 * Author: Alessandro Rubini <rubini@gnudd.com>
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * The software is provided "as is"; the copyright holders disclaim
10 * all warranties and liabilities, to the extent permitted by
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/string.h>
16 #include <linux/device.h>
17 #include <linux/slab.h>
18 #include <linux/firmware.h>
19 #include <linux/workqueue.h>
20 #include <linux/err.h>
21 #include <linux/fmc.h>
23 #define FF_EEPROM_SIZE 8192 /* The standard eeprom size */
24 #define FF_MAX_MEZZANINES 4 /* Fakes a multi-mezzanine carrier */
26 /* The user can pass up to 4 names of eeprom images to load */
27 static char *ff_eeprom[FF_MAX_MEZZANINES];
28 static int ff_nr_eeprom;
29 module_param_array_named(eeprom, ff_eeprom, charp, &ff_nr_eeprom, 0444);
31 /* The user can ask for a multi-mezzanine carrier, with the default eeprom */
32 static int ff_nr_dev = 1;
33 module_param_named(ndev, ff_nr_dev, int, 0444);
36 /* Lazily, don't support the "standard" module parameters */
39 * Eeprom built from these commands:
41 ../fru-generator -v fake-vendor -n fake-design-for-testing \
42 -s 01234 -p none > IPMI-FRU
44 gensdbfs . ../fake-eeprom.bin
46 static char ff_eeimg[FF_MAX_MEZZANINES][FF_EEPROM_SIZE] = {
48 0x01, 0x00, 0x00, 0x01, 0x00, 0x0c, 0x00, 0xf2, 0x01, 0x0b, 0x00, 0xb2,
49 0x86, 0x87, 0xcb, 0x66, 0x61, 0x6b, 0x65, 0x2d, 0x76, 0x65, 0x6e, 0x64,
50 0x6f, 0x72, 0xd7, 0x66, 0x61, 0x6b, 0x65, 0x2d, 0x64, 0x65, 0x73, 0x69,
51 0x67, 0x6e, 0x2d, 0x66, 0x6f, 0x72, 0x2d, 0x74, 0x65, 0x73, 0x74, 0x69,
52 0x6e, 0x67, 0xc5, 0x30, 0x31, 0x32, 0x33, 0x34, 0xc4, 0x6e, 0x6f, 0x6e,
53 0x65, 0xda, 0x32, 0x30, 0x31, 0x32, 0x2d, 0x31, 0x31, 0x2d, 0x31, 0x39,
54 0x20, 0x32, 0x32, 0x3a, 0x34, 0x32, 0x3a, 0x33, 0x30, 0x2e, 0x30, 0x37,
55 0x34, 0x30, 0x35, 0x35, 0xc1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87,
56 0x02, 0x02, 0x0d, 0xf7, 0xf8, 0x02, 0xb0, 0x04, 0x74, 0x04, 0xec, 0x04,
57 0x00, 0x00, 0x00, 0x00, 0xe8, 0x03, 0x02, 0x02, 0x0d, 0x5c, 0x93, 0x01,
58 0x4a, 0x01, 0x39, 0x01, 0x5a, 0x01, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x0b,
59 0x02, 0x02, 0x0d, 0x63, 0x8c, 0x00, 0xfa, 0x00, 0xed, 0x00, 0x06, 0x01,
60 0x00, 0x00, 0x00, 0x00, 0xa0, 0x0f, 0x01, 0x02, 0x0d, 0xfb, 0xf5, 0x05,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 0x01, 0x02, 0x0d, 0xfc, 0xf4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x0d, 0xfd, 0xf3, 0x03,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0xfa, 0x82, 0x0b, 0xea, 0x8f, 0xa2, 0x12, 0x00, 0x00, 0x1e, 0x44, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x53, 0x44, 0x42, 0x2d, 0x00, 0x03, 0x01, 0x01,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x01, 0xc4, 0x46, 0x69, 0x6c, 0x65, 0x44, 0x61, 0x74, 0x61,
72 0x2e, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
73 0x2e, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
74 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0,
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc4, 0x46, 0x69, 0x6c, 0x65,
77 0x44, 0x61, 0x74, 0x61, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x00, 0x00, 0x01,
78 0x00, 0x00, 0x00, 0x00, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x20, 0x20, 0x20,
79 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x01,
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xdf,
82 0x46, 0x69, 0x6c, 0x65, 0x44, 0x61, 0x74, 0x61, 0x49, 0x50, 0x4d, 0x49,
83 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x49, 0x50, 0x4d, 0x49,
84 0x2d, 0x46, 0x52, 0x55, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
85 0x20, 0x20, 0x20, 0x01, 0x66, 0x61, 0x6b, 0x65, 0x0a,
90 struct fmc_device *fmc[FF_MAX_MEZZANINES];
94 static struct ff_dev *ff_current_dev; /* We have 1 carrier, 1 slot */
96 static int ff_reprogram(struct fmc_device *fmc, struct fmc_driver *drv,
99 const struct firmware *fw;
103 /* program golden: success */
104 fmc->flags &= ~FMC_DEVICE_HAS_CUSTOM;
105 fmc->flags |= FMC_DEVICE_HAS_GOLDEN;
109 dev_info(&fmc->dev, "reprogramming with %s\n", gw);
110 ret = request_firmware(&fw, gw, &fmc->dev);
112 dev_warn(&fmc->dev, "request firmware \"%s\": error %i\n",
116 fmc->flags &= ~FMC_DEVICE_HAS_GOLDEN;
117 fmc->flags |= FMC_DEVICE_HAS_CUSTOM;
120 release_firmware(fw);
124 static int ff_irq_request(struct fmc_device *fmc, irq_handler_t handler,
125 char *name, int flags)
130 /* FIXME: should also have some fake FMC GPIO mapping */
134 * This work function is called when we changed the eeprom. It removes the
135 * current fmc device and registers a new one, with different identifiers.
137 static struct ff_dev *ff_dev_create(void); /* defined later */
139 static void ff_work_fn(struct work_struct *work)
141 struct ff_dev *ff = ff_current_dev;
144 fmc_device_unregister_n(ff->fmc, ff_nr_dev);
145 device_unregister(&ff->dev);
146 ff_current_dev = NULL;
148 ff = ff_dev_create();
150 pr_warning("%s: can't re-create FMC devices\n", __func__);
153 ret = fmc_device_register_n(ff->fmc, ff_nr_dev);
155 dev_warn(&ff->dev, "can't re-register FMC devices\n");
156 device_unregister(&ff->dev);
163 static DECLARE_DELAYED_WORK(ff_work, ff_work_fn);
167 static int ff_eeprom_read(struct fmc_device *fmc, uint32_t offset,
168 void *buf, size_t size)
170 if (offset > FF_EEPROM_SIZE)
172 if (offset + size > FF_EEPROM_SIZE)
173 size = FF_EEPROM_SIZE - offset;
174 memcpy(buf, fmc->eeprom + offset, size);
178 static int ff_eeprom_write(struct fmc_device *fmc, uint32_t offset,
179 const void *buf, size_t size)
181 if (offset > FF_EEPROM_SIZE)
183 if (offset + size > FF_EEPROM_SIZE)
184 size = FF_EEPROM_SIZE - offset;
185 dev_info(&fmc->dev, "write_eeprom: offset %i, size %zi\n",
187 memcpy(fmc->eeprom + offset, buf, size);
188 schedule_delayed_work(&ff_work, HZ * 2); /* remove, replug, in 2s */
192 /* i2c operations for fmc */
193 static int ff_read_ee(struct fmc_device *fmc, int pos, void *data, int len)
195 if (!(fmc->flags & FMC_DEVICE_HAS_GOLDEN))
197 return ff_eeprom_read(fmc, pos, data, len);
200 static int ff_write_ee(struct fmc_device *fmc, int pos,
201 const void *data, int len)
203 if (!(fmc->flags & FMC_DEVICE_HAS_GOLDEN))
205 return ff_eeprom_write(fmc, pos, data, len);
208 /* readl and writel do not do anything. Don't waste RAM with "base" */
209 static uint32_t ff_readl(struct fmc_device *fmc, int offset)
214 static void ff_writel(struct fmc_device *fmc, uint32_t value, int offset)
219 /* validate is useful so fmc-write-eeprom will not reprogram every 2 seconds */
220 static int ff_validate(struct fmc_device *fmc, struct fmc_driver *drv)
225 return 0; /* everyhing is valid */
226 for (i = 0; i < drv->busid_n; i++)
227 if (drv->busid_val[i] == fmc->device_id)
234 static struct fmc_operations ff_fmc_operations = {
236 .write32 = ff_writel,
237 .reprogram = ff_reprogram,
238 .irq_request = ff_irq_request,
239 .read_ee = ff_read_ee,
240 .write_ee = ff_write_ee,
241 .validate = ff_validate,
244 /* This device is kmalloced: release it */
245 static void ff_dev_release(struct device *dev)
247 struct ff_dev *ff = container_of(dev, struct ff_dev, dev);
251 static struct fmc_device ff_template_fmc = {
252 .version = FMC_VERSION,
253 .owner = THIS_MODULE,
254 .carrier_name = "fake-fmc-carrier",
255 .device_id = 0xf001, /* fool */
256 .eeprom_len = sizeof(ff_eeimg[0]),
257 .memlen = 0x1000, /* 4k, to show something */
258 .op = &ff_fmc_operations,
259 .hwdev = NULL, /* filled at creation time */
260 .flags = FMC_DEVICE_HAS_GOLDEN,
263 static struct ff_dev *ff_dev_create(void)
266 struct fmc_device *fmc;
269 ff = kzalloc(sizeof(*ff), GFP_KERNEL);
271 return ERR_PTR(-ENOMEM);
272 dev_set_name(&ff->dev, "fake-fmc-carrier");
273 ff->dev.release = ff_dev_release;
275 ret = device_register(&ff->dev);
277 put_device(&ff->dev);
281 /* Create fmc structures that refer to this new "hw" device */
282 for (i = 0; i < ff_nr_dev; i++) {
283 fmc = kmemdup(&ff_template_fmc, sizeof(ff_template_fmc),
285 fmc->hwdev = &ff->dev;
286 fmc->carrier_data = ff;
287 fmc->nr_slots = ff_nr_dev;
288 /* the following fields are different for each slot */
289 fmc->eeprom = ff_eeimg[i];
290 fmc->eeprom_addr = 0x50 + 2 * i;
293 /* increment the identifier, each must be different */
294 ff_template_fmc.device_id++;
300 static int ff_init(void)
303 const struct firmware *fw;
306 /* Replicate the default eeprom for the max number of mezzanines */
307 for (i = 1; i < FF_MAX_MEZZANINES; i++)
308 memcpy(ff_eeimg[i], ff_eeimg[0], sizeof(ff_eeimg[0]));
310 if (ff_nr_eeprom > ff_nr_dev)
311 ff_nr_dev = ff_nr_eeprom;
313 ff = ff_dev_create();
317 /* If the user passed "eeprom=" as a parameter, fetch them */
318 for (i = 0; i < ff_nr_eeprom; i++) {
319 if (!strlen(ff_eeprom[i]))
321 ret = request_firmware(&fw, ff_eeprom[i], &ff->dev);
323 dev_err(&ff->dev, "Mezzanine %i: can't load \"%s\" "
324 "(error %i)\n", i, ff_eeprom[i], -ret);
326 len = min_t(size_t, fw->size, (size_t)FF_EEPROM_SIZE);
327 memcpy(ff_eeimg[i], fw->data, len);
328 release_firmware(fw);
329 dev_info(&ff->dev, "Mezzanine %i: eeprom \"%s\"\n", i,
334 ret = fmc_device_register_n(ff->fmc, ff_nr_dev);
336 device_unregister(&ff->dev);
343 static void ff_exit(void)
345 if (ff_current_dev) {
346 fmc_device_unregister_n(ff_current_dev->fmc, ff_nr_dev);
347 device_unregister(&ff_current_dev->dev);
349 cancel_delayed_work_sync(&ff_work);
352 module_init(ff_init);
353 module_exit(ff_exit);
355 MODULE_LICENSE("Dual BSD/GPL");