]>
Commit | Line | Data |
---|---|---|
4319c7f7 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
6007b1bd AR |
2 | /* |
3 | * Copyright (C) 2012 CERN (www.cern.ch) | |
4 | * Author: Alessandro Rubini <rubini@gnudd.com> | |
5 | * | |
6007b1bd AR |
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> | |
15 | ||
16 | /* | |
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 | |
21 | */ | |
22 | static struct fmc_driver fwe_drv; | |
23 | ||
24 | FMC_PARAM_BUSID(fwe_drv); | |
25 | ||
26 | /* The "file=" is like the generic "gateware=" used elsewhere */ | |
27 | static char *fwe_file[FMC_MAX_CARDS]; | |
28 | static int fwe_file_n; | |
01412886 | 29 | module_param_array_named(file, fwe_file, charp, &fwe_file_n, 0444); |
6007b1bd AR |
30 | |
31 | static int fwe_run_tlv(struct fmc_device *fmc, const struct firmware *fw, | |
32 | int write) | |
33 | { | |
34 | const uint8_t *p = fw->data; | |
35 | int len = fw->size; | |
36 | uint16_t thislen, thisaddr; | |
37 | int err; | |
38 | ||
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; | |
47 | } | |
48 | err = 0; | |
49 | if (write) { | |
50 | dev_info(&fmc->dev, "write %i bytes at 0x%04x\n", | |
51 | thislen, thisaddr); | |
9f757f41 | 52 | err = fmc_write_ee(fmc, thisaddr, p + 5, thislen); |
6007b1bd AR |
53 | } |
54 | if (err < 0) { | |
55 | dev_err(&fmc->dev, "write failure @0x%04x\n", | |
56 | thisaddr); | |
57 | return err; | |
58 | } | |
59 | p += 5 + thislen; | |
60 | len -= 5 + thislen; | |
61 | } | |
62 | if (write) | |
63 | dev_info(&fmc->dev, "write_eeprom: success\n"); | |
64 | return 0; | |
65 | } | |
66 | ||
67 | static int fwe_run_bin(struct fmc_device *fmc, const struct firmware *fw) | |
68 | { | |
69 | int ret; | |
70 | ||
71 | dev_info(&fmc->dev, "programming %zi bytes\n", fw->size); | |
9f757f41 | 72 | ret = fmc_write_ee(fmc, 0, (void *)fw->data, fw->size); |
6007b1bd AR |
73 | if (ret < 0) { |
74 | dev_info(&fmc->dev, "write_eeprom: error %i\n", ret); | |
75 | return ret; | |
76 | } | |
77 | dev_info(&fmc->dev, "write_eeprom: success\n"); | |
78 | return 0; | |
79 | } | |
80 | ||
81 | static int fwe_run(struct fmc_device *fmc, const struct firmware *fw, char *s) | |
82 | { | |
83 | char *last4 = s + strlen(s) - 4; | |
84 | int err; | |
85 | ||
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; | |
93 | } | |
94 | dev_err(&fmc->dev, "invalid file name \"%s\"\n", s); | |
95 | return -EINVAL; | |
96 | } | |
97 | ||
98 | /* | |
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. | |
104 | */ | |
3d04dd2f | 105 | static int fwe_probe(struct fmc_device *fmc) |
6007b1bd AR |
106 | { |
107 | int err, index = 0; | |
108 | const struct firmware *fw; | |
109 | struct device *dev = &fmc->dev; | |
110 | char *s; | |
111 | ||
112 | if (!fwe_drv.busid_n) { | |
113 | dev_err(dev, "%s: no busid passed, refusing all cards\n", | |
114 | KBUILD_MODNAME); | |
115 | return -ENODEV; | |
116 | } | |
9f757f41 FV |
117 | |
118 | index = fmc_validate(fmc, &fwe_drv); | |
6007b1bd AR |
119 | if (index < 0) { |
120 | pr_err("%s: refusing device \"%s\"\n", KBUILD_MODNAME, | |
121 | dev_name(dev)); | |
122 | return -ENODEV; | |
123 | } | |
124 | if (index >= fwe_file_n) { | |
125 | pr_err("%s: no filename for device index %i\n", | |
126 | KBUILD_MODNAME, index); | |
127 | return -ENODEV; | |
128 | } | |
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; | |
134 | } | |
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; | |
140 | } | |
141 | fwe_run(fmc, fw, s); | |
142 | release_firmware(fw); | |
143 | return 0; | |
144 | } | |
145 | ||
3d04dd2f | 146 | static int fwe_remove(struct fmc_device *fmc) |
6007b1bd AR |
147 | { |
148 | return 0; | |
149 | } | |
150 | ||
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 */ | |
157 | }; | |
158 | ||
159 | static int fwe_init(void) | |
160 | { | |
161 | int ret; | |
162 | ||
163 | ret = fmc_driver_register(&fwe_drv); | |
164 | return ret; | |
165 | } | |
166 | ||
167 | static void fwe_exit(void) | |
168 | { | |
169 | fmc_driver_unregister(&fwe_drv); | |
170 | } | |
171 | ||
172 | module_init(fwe_init); | |
173 | module_exit(fwe_exit); | |
174 | ||
175 | MODULE_LICENSE("GPL"); |