0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/errno.h>
0011 #include <linux/slab.h>
0012 #include <linux/module.h>
0013 #include <linux/usb.h>
0014 #include <linux/delay.h>
0015 #include <linux/firmware.h>
0016 #include <linux/ihex.h>
0017
0018 #define EMI26_VENDOR_ID 0x086a
0019 #define EMI26_PRODUCT_ID 0x0100
0020 #define EMI26B_PRODUCT_ID 0x0102
0021
0022 #define ANCHOR_LOAD_INTERNAL 0xA0
0023 #define ANCHOR_LOAD_EXTERNAL 0xA3
0024 #define ANCHOR_LOAD_FPGA 0xA5
0025 #define MAX_INTERNAL_ADDRESS 0x1B3F
0026 #define CPUCS_REG 0x7F92
0027 #define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
0028
0029 static int emi26_writememory( struct usb_device *dev, int address,
0030 const unsigned char *data, int length,
0031 __u8 bRequest);
0032 static int emi26_set_reset(struct usb_device *dev, unsigned char reset_bit);
0033 static int emi26_load_firmware (struct usb_device *dev);
0034 static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id);
0035 static void emi26_disconnect(struct usb_interface *intf);
0036
0037
0038 static int emi26_writememory (struct usb_device *dev, int address,
0039 const unsigned char *data, int length,
0040 __u8 request)
0041 {
0042 int result;
0043 unsigned char *buffer = kmemdup(data, length, GFP_KERNEL);
0044
0045 if (!buffer) {
0046 dev_err(&dev->dev, "kmalloc(%d) failed.\n", length);
0047 return -ENOMEM;
0048 }
0049
0050
0051 result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
0052 kfree (buffer);
0053 return result;
0054 }
0055
0056
0057 static int emi26_set_reset (struct usb_device *dev, unsigned char reset_bit)
0058 {
0059 int response;
0060 dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit);
0061
0062 response = emi26_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
0063 if (response < 0) {
0064 dev_err(&dev->dev, "set_reset (%d) failed\n", reset_bit);
0065 }
0066 return response;
0067 }
0068
0069 #define FW_LOAD_SIZE 1023
0070
0071 static int emi26_load_firmware (struct usb_device *dev)
0072 {
0073 const struct firmware *loader_fw = NULL;
0074 const struct firmware *bitstream_fw = NULL;
0075 const struct firmware *firmware_fw = NULL;
0076 const struct ihex_binrec *rec;
0077 int err = -ENOMEM;
0078 int i;
0079 __u32 addr;
0080 __u8 *buf;
0081
0082 buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
0083 if (!buf)
0084 goto wraperr;
0085
0086 err = request_ihex_firmware(&loader_fw, "emi26/loader.fw", &dev->dev);
0087 if (err)
0088 goto nofw;
0089
0090 err = request_ihex_firmware(&bitstream_fw, "emi26/bitstream.fw",
0091 &dev->dev);
0092 if (err)
0093 goto nofw;
0094
0095 err = request_ihex_firmware(&firmware_fw, "emi26/firmware.fw",
0096 &dev->dev);
0097 if (err) {
0098 nofw:
0099 dev_err(&dev->dev, "%s - request_firmware() failed\n",
0100 __func__);
0101 goto wraperr;
0102 }
0103
0104
0105 err = emi26_set_reset(dev,1);
0106 if (err < 0)
0107 goto wraperr;
0108
0109 rec = (const struct ihex_binrec *)loader_fw->data;
0110
0111 while (rec) {
0112 err = emi26_writememory(dev, be32_to_cpu(rec->addr),
0113 rec->data, be16_to_cpu(rec->len),
0114 ANCHOR_LOAD_INTERNAL);
0115 if (err < 0)
0116 goto wraperr;
0117 rec = ihex_next_binrec(rec);
0118 }
0119
0120
0121 err = emi26_set_reset(dev,0);
0122 if (err < 0)
0123 goto wraperr;
0124 msleep(250);
0125
0126
0127
0128
0129 rec = (const struct ihex_binrec *)bitstream_fw->data;
0130 do {
0131 i = 0;
0132 addr = be32_to_cpu(rec->addr);
0133
0134
0135 while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
0136 memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
0137 i += be16_to_cpu(rec->len);
0138 rec = ihex_next_binrec(rec);
0139 }
0140 err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
0141 if (err < 0)
0142 goto wraperr;
0143 } while (rec);
0144
0145
0146 err = emi26_set_reset(dev,1);
0147 if (err < 0)
0148 goto wraperr;
0149
0150
0151 for (rec = (const struct ihex_binrec *)loader_fw->data;
0152 rec; rec = ihex_next_binrec(rec)) {
0153 err = emi26_writememory(dev, be32_to_cpu(rec->addr),
0154 rec->data, be16_to_cpu(rec->len),
0155 ANCHOR_LOAD_INTERNAL);
0156 if (err < 0)
0157 goto wraperr;
0158 }
0159 msleep(250);
0160
0161
0162 err = emi26_set_reset(dev,0);
0163 if (err < 0)
0164 goto wraperr;
0165
0166
0167
0168 for (rec = (const struct ihex_binrec *)firmware_fw->data;
0169 rec; rec = ihex_next_binrec(rec)) {
0170 if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
0171 err = emi26_writememory(dev, be32_to_cpu(rec->addr),
0172 rec->data, be16_to_cpu(rec->len),
0173 ANCHOR_LOAD_EXTERNAL);
0174 if (err < 0)
0175 goto wraperr;
0176 }
0177 }
0178
0179
0180 err = emi26_set_reset(dev,1);
0181 if (err < 0)
0182 goto wraperr;
0183
0184 for (rec = (const struct ihex_binrec *)firmware_fw->data;
0185 rec; rec = ihex_next_binrec(rec)) {
0186 if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
0187 err = emi26_writememory(dev, be32_to_cpu(rec->addr),
0188 rec->data, be16_to_cpu(rec->len),
0189 ANCHOR_LOAD_INTERNAL);
0190 if (err < 0)
0191 goto wraperr;
0192 }
0193 }
0194
0195
0196 err = emi26_set_reset(dev,0);
0197 if (err < 0)
0198 goto wraperr;
0199 msleep(250);
0200
0201
0202
0203 err = 1;
0204
0205 wraperr:
0206 if (err < 0)
0207 dev_err(&dev->dev,"%s - error loading firmware: error = %d\n",
0208 __func__, err);
0209
0210 release_firmware(loader_fw);
0211 release_firmware(bitstream_fw);
0212 release_firmware(firmware_fw);
0213
0214 kfree(buf);
0215 return err;
0216 }
0217
0218 static const struct usb_device_id id_table[] = {
0219 { USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) },
0220 { USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) },
0221 { }
0222 };
0223
0224 MODULE_DEVICE_TABLE (usb, id_table);
0225
0226 static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id)
0227 {
0228 struct usb_device *dev = interface_to_usbdev(intf);
0229
0230 dev_info(&intf->dev, "%s start\n", __func__);
0231
0232 emi26_load_firmware(dev);
0233
0234
0235 return -EIO;
0236 }
0237
0238 static void emi26_disconnect(struct usb_interface *intf)
0239 {
0240 }
0241
0242 static struct usb_driver emi26_driver = {
0243 .name = "emi26 - firmware loader",
0244 .probe = emi26_probe,
0245 .disconnect = emi26_disconnect,
0246 .id_table = id_table,
0247 };
0248
0249 module_usb_driver(emi26_driver);
0250
0251 MODULE_AUTHOR("Tapio Laxström");
0252 MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
0253 MODULE_LICENSE("GPL");
0254
0255 MODULE_FIRMWARE("emi26/loader.fw");
0256 MODULE_FIRMWARE("emi26/bitstream.fw");
0257 MODULE_FIRMWARE("emi26/firmware.fw");
0258
0259