fmc-write-eeprom.c 4.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) 2012 CERN (www.cern.ch)
 * Author: Alessandro Rubini <rubini@gnudd.com>
 *
 * Released according to the GNU GPL, version 2 or any later version.
 *
 * This work is part of the White Rabbit project, a research effort led
 * by CERN, the European Institute for Nuclear Research.
 */
#include <linux/module.h>
#include <linux/string.h>
#include <linux/firmware.h>
#include <linux/init.h>
#include <linux/fmc.h>
15
#include <asm/unaligned.h>
16 17 18 19

/*
 * This module uses the firmware loader to program the whole or part
 * of the FMC eeprom. The meat is in the _run functions.  However, no
20 21
 * default file name is provided, to avoid accidental mishaps. Also,
 * you must pass the busid argument
22
 */
23 24 25 26 27 28 29
static struct fmc_driver fwe_drv;

FMC_PARAM_BUSID(fwe_drv);

/* The "file=" is like the generic "gateware=" used elsewhere */
static char *fwe_file[FMC_MAX_CARDS];
static int fwe_file_n;
30
module_param_array_named(file, fwe_file, charp, &fwe_file_n, 0444);
31

32 33
static int fwe_run_tlv(struct fmc_device *fmc, const struct firmware *fw,
	int write)
34
{
35 36 37 38 39 40 41 42 43 44
	const uint8_t *p = fw->data;
	int len = fw->size;
	uint16_t thislen, thisaddr;
	int err;

	/* format is: 'w' addr16 len16 data... */
	while (len > 5) {
		thisaddr = get_unaligned_le16(p+1);
		thislen = get_unaligned_le16(p+3);
		if (p[0] != 'w' || thislen + 5 > len) {
45
			dev_err(&fmc->dev, "invalid tlv at offset %ti\n",
46 47 48 49 50
				p - fw->data);
			return -EINVAL;
		}
		err = 0;
		if (write) {
51
			dev_info(&fmc->dev, "write %i bytes at 0x%04x\n",
52 53 54 55
				 thislen, thisaddr);
			err = fmc->op->write_ee(fmc, thisaddr, p + 5, thislen);
		}
		if (err < 0) {
56
			dev_err(&fmc->dev, "write failure @0x%04x\n",
57 58 59 60 61 62 63
				thisaddr);
			return err;
		}
		p += 5 + thislen;
		len -= 5 + thislen;
	}
	if (write)
64
		dev_info(&fmc->dev, "write_eeprom: success\n");
65
	return 0;
66 67 68 69 70 71
}

static int fwe_run_bin(struct fmc_device *fmc, const struct firmware *fw)
{
	int ret;

72
	dev_info(&fmc->dev, "programming %zi bytes\n", fw->size);
73 74
	ret = fmc->op->write_ee(fmc, 0, (void *)fw->data, fw->size);
	if (ret < 0) {
75
		dev_info(&fmc->dev, "write_eeprom: error %i\n", ret);
76 77
		return ret;
	}
78
	dev_info(&fmc->dev, "write_eeprom: success\n");
79 80 81
	return 0;
}

82
static int fwe_run(struct fmc_device *fmc, const struct firmware *fw, char *s)
83
{
84
	char *last4 = s + strlen(s) - 4;
85
	int err;
86

87
	if (!strcmp(last4, ".bin"))
88
		return fwe_run_bin(fmc, fw);
89
	if (!strcmp(last4, ".tlv")) {
90 91 92 93 94
		err = fwe_run_tlv(fmc, fw, 0);
		if (!err)
			err = fwe_run_tlv(fmc, fw, 1);
		return err;
	}
95
	dev_err(&fmc->dev, "invalid file name \"%s\"\n", s);
96 97 98 99
	return -EINVAL;
}

/*
100 101
 * Programming is done at probe time. Morever, only those listed with
 * busid= are programmed.
102 103 104 105 106 107
 * card is probed for, only one is programmed. Unfortunately, it's
 * difficult to know in advance when probing the first card if others
 * are there.
 */
int fwe_probe(struct fmc_device *fmc)
{
108
	int err, index = 0;
109
	const struct firmware *fw;
110
	struct device *dev = &fmc->dev;
111
	char *s;
112

113 114
	if (!fwe_drv.busid_n) {
		dev_err(dev, "%s: no busid passed, refusing all cards\n",
115
			KBUILD_MODNAME);
116
		return -ENODEV;
117
	}
118 119
	if (fmc->op->validate)
		index = fmc->op->validate(fmc, &fwe_drv);
120 121 122 123 124
	if (index < 0) {
		pr_err("%s: refusing device \"%s\"\n", KBUILD_MODNAME,
		       dev_name(dev));
		return -ENODEV;
	}
125
	if (index >= fwe_file_n) {
126
		pr_err("%s: no filename for device index %i\n",
127 128 129
			KBUILD_MODNAME, index);
		return -ENODEV;
	}
130 131
	s = fwe_file[index];
	if (!s) {
132 133
		pr_err("%s: no filename for \"%s\" not programming\n",
		       KBUILD_MODNAME, dev_name(dev));
134
		return -ENOENT;
135
	}
136
	err = request_firmware(&fw, s, dev);
137
	if (err < 0) {
138 139
		dev_err(&fmc->dev, "request firmware \"%s\": error %i\n",
			s, err);
140 141
		return err;
	}
142
	fwe_run(fmc, fw, s);
143 144 145 146 147 148 149 150 151 152
	release_firmware(fw);
	return 0;
}

int fwe_remove(struct fmc_device *fmc)
{
	return 0;
}

static struct fmc_driver fwe_drv = {
153
	.version = FMC_VERSION,
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	.driver.name = KBUILD_MODNAME,
	.probe = fwe_probe,
	.remove = fwe_remove,
	/* no table, as the current match just matches everything */
};

static int fwe_init(void)
{
	int ret;

	ret = fmc_driver_register(&fwe_drv);
	return ret;
}

static void fwe_exit(void)
{
	fmc_driver_unregister(&fwe_drv);
}

module_init(fwe_init);
module_exit(fwe_exit);

176
MODULE_VERSION(GIT_VERSION);
177
MODULE_LICENSE("GPL");