1 // SPDX-License-Identifier: GPL-2.0
3 * Emagic EMI 2|6 usb audio interface firmware loader.
5 * Tapio Laxström (tapio.laxstrom@iptime.fi)
7 * emi26.c,v 1.13 2002/03/08 13:10:26 tapio Exp
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/usb.h>
14 #include <linux/delay.h>
15 #include <linux/firmware.h>
16 #include <linux/ihex.h>
18 #define EMI26_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
19 #define EMI26_PRODUCT_ID 0x0100 /* EMI 2|6 without firmware */
20 #define EMI26B_PRODUCT_ID 0x0102 /* EMI 2|6 without firmware */
22 #define ANCHOR_LOAD_INTERNAL 0xA0 /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
23 #define ANCHOR_LOAD_EXTERNAL 0xA3 /* This command is not implemented in the core. Requires firmware */
24 #define ANCHOR_LOAD_FPGA 0xA5 /* This command is not implemented in the core. Requires firmware. Emagic extension */
25 #define MAX_INTERNAL_ADDRESS 0x1B3F /* This is the highest internal RAM address for the AN2131Q */
26 #define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
27 #define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
29 static int emi26_writememory( struct usb_device
*dev
, int address
,
30 const unsigned char *data
, int length
,
32 static int emi26_set_reset(struct usb_device
*dev
, unsigned char reset_bit
);
33 static int emi26_load_firmware (struct usb_device
*dev
);
34 static int emi26_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
);
35 static void emi26_disconnect(struct usb_interface
*intf
);
37 /* thanks to drivers/usb/serial/keyspan_pda.c code */
38 static int emi26_writememory (struct usb_device
*dev
, int address
,
39 const unsigned char *data
, int length
,
43 unsigned char *buffer
= kmemdup(data
, length
, GFP_KERNEL
);
46 dev_err(&dev
->dev
, "kmalloc(%d) failed.\n", length
);
49 /* Note: usb_control_msg returns negative value on error or length of the
50 * data that was written! */
51 result
= usb_control_msg (dev
, usb_sndctrlpipe(dev
, 0), request
, 0x40, address
, 0, buffer
, length
, 300);
56 /* thanks to drivers/usb/serial/keyspan_pda.c code */
57 static int emi26_set_reset (struct usb_device
*dev
, unsigned char reset_bit
)
60 dev_info(&dev
->dev
, "%s - %d\n", __func__
, reset_bit
);
61 /* printk(KERN_DEBUG "%s - %d", __func__, reset_bit); */
62 response
= emi26_writememory (dev
, CPUCS_REG
, &reset_bit
, 1, 0xa0);
64 dev_err(&dev
->dev
, "set_reset (%d) failed\n", reset_bit
);
69 #define FW_LOAD_SIZE 1023
71 static int emi26_load_firmware (struct usb_device
*dev
)
73 const struct firmware
*loader_fw
= NULL
;
74 const struct firmware
*bitstream_fw
= NULL
;
75 const struct firmware
*firmware_fw
= NULL
;
76 const struct ihex_binrec
*rec
;
79 __u32 addr
; /* Address to write */
82 buf
= kmalloc(FW_LOAD_SIZE
, GFP_KERNEL
);
86 err
= request_ihex_firmware(&loader_fw
, "emi26/loader.fw", &dev
->dev
);
90 err
= request_ihex_firmware(&bitstream_fw
, "emi26/bitstream.fw",
95 err
= request_ihex_firmware(&firmware_fw
, "emi26/firmware.fw",
99 dev_err(&dev
->dev
, "%s - request_firmware() failed\n",
104 /* Assert reset (stop the CPU in the EMI) */
105 err
= emi26_set_reset(dev
,1);
109 rec
= (const struct ihex_binrec
*)loader_fw
->data
;
110 /* 1. We need to put the loader for the FPGA into the EZ-USB */
112 err
= emi26_writememory(dev
, be32_to_cpu(rec
->addr
),
113 rec
->data
, be16_to_cpu(rec
->len
),
114 ANCHOR_LOAD_INTERNAL
);
117 rec
= ihex_next_binrec(rec
);
120 /* De-assert reset (let the CPU run) */
121 err
= emi26_set_reset(dev
,0);
124 msleep(250); /* let device settle */
126 /* 2. We upload the FPGA firmware into the EMI
127 * Note: collect up to 1023 (yes!) bytes and send them with
128 * a single request. This is _much_ faster! */
129 rec
= (const struct ihex_binrec
*)bitstream_fw
->data
;
132 addr
= be32_to_cpu(rec
->addr
);
134 /* intel hex records are terminated with type 0 element */
135 while (rec
&& (i
+ be16_to_cpu(rec
->len
) < FW_LOAD_SIZE
)) {
136 memcpy(buf
+ i
, rec
->data
, be16_to_cpu(rec
->len
));
137 i
+= be16_to_cpu(rec
->len
);
138 rec
= ihex_next_binrec(rec
);
140 err
= emi26_writememory(dev
, addr
, buf
, i
, ANCHOR_LOAD_FPGA
);
145 /* Assert reset (stop the CPU in the EMI) */
146 err
= emi26_set_reset(dev
,1);
150 /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
151 for (rec
= (const struct ihex_binrec
*)loader_fw
->data
;
152 rec
; rec
= ihex_next_binrec(rec
)) {
153 err
= emi26_writememory(dev
, be32_to_cpu(rec
->addr
),
154 rec
->data
, be16_to_cpu(rec
->len
),
155 ANCHOR_LOAD_INTERNAL
);
159 msleep(250); /* let device settle */
161 /* De-assert reset (let the CPU run) */
162 err
= emi26_set_reset(dev
,0);
166 /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
168 for (rec
= (const struct ihex_binrec
*)firmware_fw
->data
;
169 rec
; rec
= ihex_next_binrec(rec
)) {
170 if (!INTERNAL_RAM(be32_to_cpu(rec
->addr
))) {
171 err
= emi26_writememory(dev
, be32_to_cpu(rec
->addr
),
172 rec
->data
, be16_to_cpu(rec
->len
),
173 ANCHOR_LOAD_EXTERNAL
);
179 /* Assert reset (stop the CPU in the EMI) */
180 err
= emi26_set_reset(dev
,1);
184 for (rec
= (const struct ihex_binrec
*)firmware_fw
->data
;
185 rec
; rec
= ihex_next_binrec(rec
)) {
186 if (INTERNAL_RAM(be32_to_cpu(rec
->addr
))) {
187 err
= emi26_writememory(dev
, be32_to_cpu(rec
->addr
),
188 rec
->data
, be16_to_cpu(rec
->len
),
189 ANCHOR_LOAD_INTERNAL
);
195 /* De-assert reset (let the CPU run) */
196 err
= emi26_set_reset(dev
,0);
199 msleep(250); /* let device settle */
201 /* return 1 to fail the driver inialization
202 * and give real driver change to load */
207 dev_err(&dev
->dev
,"%s - error loading firmware: error = %d\n",
210 release_firmware(loader_fw
);
211 release_firmware(bitstream_fw
);
212 release_firmware(firmware_fw
);
218 static const struct usb_device_id id_table
[] = {
219 { USB_DEVICE(EMI26_VENDOR_ID
, EMI26_PRODUCT_ID
) },
220 { USB_DEVICE(EMI26_VENDOR_ID
, EMI26B_PRODUCT_ID
) },
221 { } /* Terminating entry */
224 MODULE_DEVICE_TABLE (usb
, id_table
);
226 static int emi26_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
228 struct usb_device
*dev
= interface_to_usbdev(intf
);
230 dev_info(&intf
->dev
, "%s start\n", __func__
);
232 emi26_load_firmware(dev
);
234 /* do not return the driver context, let real audio driver do that */
238 static void emi26_disconnect(struct usb_interface
*intf
)
242 static struct usb_driver emi26_driver
= {
243 .name
= "emi26 - firmware loader",
244 .probe
= emi26_probe
,
245 .disconnect
= emi26_disconnect
,
246 .id_table
= id_table
,
249 module_usb_driver(emi26_driver
);
251 MODULE_AUTHOR("Tapio Laxström");
252 MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
253 MODULE_LICENSE("GPL");
255 MODULE_FIRMWARE("emi26/loader.fw");
256 MODULE_FIRMWARE("emi26/bitstream.fw");
257 MODULE_FIRMWARE("emi26/firmware.fw");
258 /* vi:ai:syntax=c:sw=8:ts=8:tw=80