1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
9 #include <linux/module.h>
10 #include <linux/string.h>
11 #include <linux/firmware.h>
12 #include <linux/init.h>
13 #include <linux/fmc.h>
14 #include <asm/unaligned.h>
17 * This module uses the firmware loader to program the whole or part
18 * of the FMC eeprom. The meat is in the _run functions. However, no
19 * default file name is provided, to avoid accidental mishaps. Also,
20 * you must pass the busid argument
22 static struct fmc_driver fwe_drv
;
24 FMC_PARAM_BUSID(fwe_drv
);
26 /* The "file=" is like the generic "gateware=" used elsewhere */
27 static char *fwe_file
[FMC_MAX_CARDS
];
28 static int fwe_file_n
;
29 module_param_array_named(file
, fwe_file
, charp
, &fwe_file_n
, 0444);
31 static int fwe_run_tlv(struct fmc_device
*fmc
, const struct firmware
*fw
,
34 const uint8_t *p
= fw
->data
;
36 uint16_t thislen
, thisaddr
;
39 /* format is: 'w' addr16 len16 data... */
41 thisaddr
= get_unaligned_le16(p
+1);
42 thislen
= get_unaligned_le16(p
+3);
43 if (p
[0] != 'w' || thislen
+ 5 > len
) {
44 dev_err(&fmc
->dev
, "invalid tlv at offset %ti\n",
50 dev_info(&fmc
->dev
, "write %i bytes at 0x%04x\n",
52 err
= fmc_write_ee(fmc
, thisaddr
, p
+ 5, thislen
);
55 dev_err(&fmc
->dev
, "write failure @0x%04x\n",
63 dev_info(&fmc
->dev
, "write_eeprom: success\n");
67 static int fwe_run_bin(struct fmc_device
*fmc
, const struct firmware
*fw
)
71 dev_info(&fmc
->dev
, "programming %zi bytes\n", fw
->size
);
72 ret
= fmc_write_ee(fmc
, 0, (void *)fw
->data
, fw
->size
);
74 dev_info(&fmc
->dev
, "write_eeprom: error %i\n", ret
);
77 dev_info(&fmc
->dev
, "write_eeprom: success\n");
81 static int fwe_run(struct fmc_device
*fmc
, const struct firmware
*fw
, char *s
)
83 char *last4
= s
+ strlen(s
) - 4;
86 if (!strcmp(last4
, ".bin"))
87 return fwe_run_bin(fmc
, fw
);
88 if (!strcmp(last4
, ".tlv")) {
89 err
= fwe_run_tlv(fmc
, fw
, 0);
91 err
= fwe_run_tlv(fmc
, fw
, 1);
94 dev_err(&fmc
->dev
, "invalid file name \"%s\"\n", s
);
99 * Programming is done at probe time. Morever, only those listed with
100 * busid= are programmed.
101 * card is probed for, only one is programmed. Unfortunately, it's
102 * difficult to know in advance when probing the first card if others
105 static int fwe_probe(struct fmc_device
*fmc
)
108 const struct firmware
*fw
;
109 struct device
*dev
= &fmc
->dev
;
112 if (!fwe_drv
.busid_n
) {
113 dev_err(dev
, "%s: no busid passed, refusing all cards\n",
118 index
= fmc_validate(fmc
, &fwe_drv
);
120 pr_err("%s: refusing device \"%s\"\n", KBUILD_MODNAME
,
124 if (index
>= fwe_file_n
) {
125 pr_err("%s: no filename for device index %i\n",
126 KBUILD_MODNAME
, index
);
131 pr_err("%s: no filename for \"%s\" not programming\n",
132 KBUILD_MODNAME
, dev_name(dev
));
135 err
= request_firmware(&fw
, s
, dev
);
137 dev_err(&fmc
->dev
, "request firmware \"%s\": error %i\n",
142 release_firmware(fw
);
146 static int fwe_remove(struct fmc_device
*fmc
)
151 static struct fmc_driver fwe_drv
= {
152 .version
= FMC_VERSION
,
153 .driver
.name
= KBUILD_MODNAME
,
155 .remove
= fwe_remove
,
156 /* no table, as the current match just matches everything */
159 static int fwe_init(void)
163 ret
= fmc_driver_register(&fwe_drv
);
167 static void fwe_exit(void)
169 fmc_driver_unregister(&fwe_drv
);
172 module_init(fwe_init
);
173 module_exit(fwe_exit
);
175 MODULE_LICENSE("GPL");