staging: erofs: integrate decompression inplace
[linux/fpc-iii.git] / drivers / fmc / fmc-write-eeprom.c
blob1c7826e3f526fd553cdf4c3b5cc98941652a1a64
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/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,
32 int write)
34 const uint8_t *p = fw->data;
35 int len = fw->size;
36 uint16_t thislen, thisaddr;
37 int err;
39 /* format is: 'w' addr16 len16 data... */
40 while (len > 5) {
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",
45 p - fw->data);
46 return -EINVAL;
48 err = 0;
49 if (write) {
50 dev_info(&fmc->dev, "write %i bytes at 0x%04x\n",
51 thislen, thisaddr);
52 err = fmc_write_ee(fmc, thisaddr, p + 5, thislen);
54 if (err < 0) {
55 dev_err(&fmc->dev, "write failure @0x%04x\n",
56 thisaddr);
57 return err;
59 p += 5 + thislen;
60 len -= 5 + thislen;
62 if (write)
63 dev_info(&fmc->dev, "write_eeprom: success\n");
64 return 0;
67 static int fwe_run_bin(struct fmc_device *fmc, const struct firmware *fw)
69 int ret;
71 dev_info(&fmc->dev, "programming %zi bytes\n", fw->size);
72 ret = fmc_write_ee(fmc, 0, (void *)fw->data, fw->size);
73 if (ret < 0) {
74 dev_info(&fmc->dev, "write_eeprom: error %i\n", ret);
75 return ret;
77 dev_info(&fmc->dev, "write_eeprom: success\n");
78 return 0;
81 static int fwe_run(struct fmc_device *fmc, const struct firmware *fw, char *s)
83 char *last4 = s + strlen(s) - 4;
84 int err;
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);
90 if (!err)
91 err = fwe_run_tlv(fmc, fw, 1);
92 return err;
94 dev_err(&fmc->dev, "invalid file name \"%s\"\n", s);
95 return -EINVAL;
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
103 * are there.
105 static int fwe_probe(struct fmc_device *fmc)
107 int err, index = 0;
108 const struct firmware *fw;
109 struct device *dev = &fmc->dev;
110 char *s;
112 if (!fwe_drv.busid_n) {
113 dev_err(dev, "%s: no busid passed, refusing all cards\n",
114 KBUILD_MODNAME);
115 return -ENODEV;
118 index = fmc_validate(fmc, &fwe_drv);
119 if (index < 0) {
120 pr_err("%s: refusing device \"%s\"\n", KBUILD_MODNAME,
121 dev_name(dev));
122 return -ENODEV;
124 if (index >= fwe_file_n) {
125 pr_err("%s: no filename for device index %i\n",
126 KBUILD_MODNAME, index);
127 return -ENODEV;
129 s = fwe_file[index];
130 if (!s) {
131 pr_err("%s: no filename for \"%s\" not programming\n",
132 KBUILD_MODNAME, dev_name(dev));
133 return -ENOENT;
135 err = request_firmware(&fw, s, dev);
136 if (err < 0) {
137 dev_err(&fmc->dev, "request firmware \"%s\": error %i\n",
138 s, err);
139 return err;
141 fwe_run(fmc, fw, s);
142 release_firmware(fw);
143 return 0;
146 static int fwe_remove(struct fmc_device *fmc)
148 return 0;
151 static struct fmc_driver fwe_drv = {
152 .version = FMC_VERSION,
153 .driver.name = KBUILD_MODNAME,
154 .probe = fwe_probe,
155 .remove = fwe_remove,
156 /* no table, as the current match just matches everything */
159 static int fwe_init(void)
161 int ret;
163 ret = fmc_driver_register(&fwe_drv);
164 return ret;
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");