1 // SPDX-License-Identifier: GPL-2.0
3 * EZ-USB specific functions used by some of the USB to Serial drivers.
5 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/firmware.h>
13 #include <linux/ihex.h>
14 #include <linux/usb/ezusb.h>
16 struct ezusb_fx_type
{
17 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
18 unsigned short cpucs_reg
;
19 unsigned short max_internal_adress
;
22 static const struct ezusb_fx_type ezusb_fx1
= {
24 .max_internal_adress
= 0x1B3F,
27 /* Commands for writing to memory */
28 #define WRITE_INT_RAM 0xA0
29 #define WRITE_EXT_RAM 0xA3
31 static int ezusb_writememory(struct usb_device
*dev
, int address
,
32 unsigned char *data
, int length
, __u8 request
)
37 return usb_control_msg_send(dev
, 0, request
,
38 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
39 address
, 0, data
, length
, 3000, GFP_KERNEL
);
42 static int ezusb_set_reset(struct usb_device
*dev
, unsigned short cpucs_reg
,
43 unsigned char reset_bit
)
45 int response
= ezusb_writememory(dev
, cpucs_reg
, &reset_bit
, 1, WRITE_INT_RAM
);
47 dev_err(&dev
->dev
, "%s-%d failed: %d\n",
48 __func__
, reset_bit
, response
);
52 int ezusb_fx1_set_reset(struct usb_device
*dev
, unsigned char reset_bit
)
54 return ezusb_set_reset(dev
, ezusb_fx1
.cpucs_reg
, reset_bit
);
56 EXPORT_SYMBOL_GPL(ezusb_fx1_set_reset
);
58 static int ezusb_ihex_firmware_download(struct usb_device
*dev
,
59 struct ezusb_fx_type fx
,
60 const char *firmware_path
)
63 const struct firmware
*firmware
= NULL
;
64 const struct ihex_binrec
*record
;
66 if (request_ihex_firmware(&firmware
, firmware_path
,
69 "%s - request \"%s\" failed\n",
70 __func__
, firmware_path
);
74 ret
= ezusb_set_reset(dev
, fx
.cpucs_reg
, 0);
78 record
= (const struct ihex_binrec
*)firmware
->data
;
79 for (; record
; record
= ihex_next_binrec(record
)) {
80 if (be32_to_cpu(record
->addr
) > fx
.max_internal_adress
) {
81 ret
= ezusb_writememory(dev
, be32_to_cpu(record
->addr
),
82 (unsigned char *)record
->data
,
83 be16_to_cpu(record
->len
), WRITE_EXT_RAM
);
85 dev_err(&dev
->dev
, "%s - ezusb_writememory "
86 "failed writing internal memory "
87 "(%d %04X %p %d)\n", __func__
, ret
,
88 be32_to_cpu(record
->addr
), record
->data
,
89 be16_to_cpu(record
->len
));
95 ret
= ezusb_set_reset(dev
, fx
.cpucs_reg
, 1);
98 record
= (const struct ihex_binrec
*)firmware
->data
;
99 for (; record
; record
= ihex_next_binrec(record
)) {
100 if (be32_to_cpu(record
->addr
) <= fx
.max_internal_adress
) {
101 ret
= ezusb_writememory(dev
, be32_to_cpu(record
->addr
),
102 (unsigned char *)record
->data
,
103 be16_to_cpu(record
->len
), WRITE_INT_RAM
);
105 dev_err(&dev
->dev
, "%s - ezusb_writememory "
106 "failed writing external memory "
107 "(%d %04X %p %d)\n", __func__
, ret
,
108 be32_to_cpu(record
->addr
), record
->data
,
109 be16_to_cpu(record
->len
));
114 ret
= ezusb_set_reset(dev
, fx
.cpucs_reg
, 0);
116 release_firmware(firmware
);
120 int ezusb_fx1_ihex_firmware_download(struct usb_device
*dev
,
121 const char *firmware_path
)
123 return ezusb_ihex_firmware_download(dev
, ezusb_fx1
, firmware_path
);
125 EXPORT_SYMBOL_GPL(ezusb_fx1_ihex_firmware_download
);
129 * Once someone one needs these fx2 functions, uncomment them
130 * and add them to ezusb.h and all should be good.
132 static struct ezusb_fx_type ezusb_fx2
= {
134 .max_internal_adress
= 0x3FFF,
137 int ezusb_fx2_set_reset(struct usb_device
*dev
, unsigned char reset_bit
)
139 return ezusb_set_reset(dev
, ezusb_fx2
.cpucs_reg
, reset_bit
);
141 EXPORT_SYMBOL_GPL(ezusb_fx2_set_reset
);
143 int ezusb_fx2_ihex_firmware_download(struct usb_device
*dev
,
144 const char *firmware_path
)
146 return ezusb_ihex_firmware_download(dev
, ezusb_fx2
, firmware_path
);
148 EXPORT_SYMBOL_GPL(ezusb_fx2_ihex_firmware_download
);
151 MODULE_DESCRIPTION("EZUSB device support");
152 MODULE_LICENSE("GPL");