Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * EZ-USB specific functions used by some of the USB to Serial drivers. | |
4 | * | |
5 | * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) | |
1da177e4 LT |
6 | */ |
7 | ||
1da177e4 | 8 | #include <linux/kernel.h> |
1da177e4 | 9 | #include <linux/slab.h> |
1da177e4 LT |
10 | #include <linux/module.h> |
11 | #include <linux/usb.h> | |
8d733e26 RB |
12 | #include <linux/firmware.h> |
13 | #include <linux/ihex.h> | |
c30186e5 | 14 | #include <linux/usb/ezusb.h> |
1da177e4 | 15 | |
cc183e2a RB |
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; | |
20 | }; | |
1da177e4 | 21 | |
36ae6776 | 22 | static const struct ezusb_fx_type ezusb_fx1 = { |
cc183e2a RB |
23 | .cpucs_reg = 0x7F92, |
24 | .max_internal_adress = 0x1B3F, | |
25 | }; | |
26 | ||
cc183e2a | 27 | /* Commands for writing to memory */ |
99495c70 | 28 | #define WRITE_INT_RAM 0xA0 |
cc183e2a | 29 | #define WRITE_EXT_RAM 0xA3 |
99495c70 | 30 | |
c30186e5 | 31 | static int ezusb_writememory(struct usb_device *dev, int address, |
80359a9c | 32 | unsigned char *data, int length, __u8 request) |
1da177e4 | 33 | { |
c6ab0158 | 34 | if (!dev) |
1da177e4 | 35 | return -ENODEV; |
1da177e4 | 36 | |
ced6a0ba | 37 | return usb_control_msg_send(dev, 0, request, |
99495c70 | 38 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
ced6a0ba | 39 | address, 0, data, length, 3000, GFP_KERNEL); |
1da177e4 LT |
40 | } |
41 | ||
c30186e5 GKH |
42 | static int ezusb_set_reset(struct usb_device *dev, unsigned short cpucs_reg, |
43 | unsigned char reset_bit) | |
1da177e4 | 44 | { |
cc183e2a | 45 | int response = ezusb_writememory(dev, cpucs_reg, &reset_bit, 1, WRITE_INT_RAM); |
1da177e4 | 46 | if (response < 0) |
99495c70 RB |
47 | dev_err(&dev->dev, "%s-%d failed: %d\n", |
48 | __func__, reset_bit, response); | |
1da177e4 LT |
49 | return response; |
50 | } | |
1da177e4 | 51 | |
cc183e2a RB |
52 | int ezusb_fx1_set_reset(struct usb_device *dev, unsigned char reset_bit) |
53 | { | |
54 | return ezusb_set_reset(dev, ezusb_fx1.cpucs_reg, reset_bit); | |
55 | } | |
56 | EXPORT_SYMBOL_GPL(ezusb_fx1_set_reset); | |
57 | ||
8d733e26 RB |
58 | static int ezusb_ihex_firmware_download(struct usb_device *dev, |
59 | struct ezusb_fx_type fx, | |
60 | const char *firmware_path) | |
61 | { | |
62 | int ret = -ENOENT; | |
63 | const struct firmware *firmware = NULL; | |
64 | const struct ihex_binrec *record; | |
65 | ||
66 | if (request_ihex_firmware(&firmware, firmware_path, | |
67 | &dev->dev)) { | |
68 | dev_err(&dev->dev, | |
69 | "%s - request \"%s\" failed\n", | |
70 | __func__, firmware_path); | |
71 | goto out; | |
72 | } | |
73 | ||
74 | ret = ezusb_set_reset(dev, fx.cpucs_reg, 0); | |
75 | if (ret < 0) | |
76 | goto out; | |
77 | ||
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); | |
84 | if (ret < 0) { | |
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)); | |
90 | goto out; | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | ret = ezusb_set_reset(dev, fx.cpucs_reg, 1); | |
96 | if (ret < 0) | |
97 | goto out; | |
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); | |
104 | if (ret < 0) { | |
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)); | |
110 | goto out; | |
111 | } | |
112 | } | |
113 | } | |
114 | ret = ezusb_set_reset(dev, fx.cpucs_reg, 0); | |
115 | out: | |
116 | release_firmware(firmware); | |
117 | return ret; | |
118 | } | |
119 | ||
120 | int ezusb_fx1_ihex_firmware_download(struct usb_device *dev, | |
121 | const char *firmware_path) | |
122 | { | |
123 | return ezusb_ihex_firmware_download(dev, ezusb_fx1, firmware_path); | |
124 | } | |
125 | EXPORT_SYMBOL_GPL(ezusb_fx1_ihex_firmware_download); | |
126 | ||
c30186e5 GKH |
127 | #if 0 |
128 | /* | |
129 | * Once someone one needs these fx2 functions, uncomment them | |
130 | * and add them to ezusb.h and all should be good. | |
131 | */ | |
132 | static struct ezusb_fx_type ezusb_fx2 = { | |
133 | .cpucs_reg = 0xE600, | |
134 | .max_internal_adress = 0x3FFF, | |
135 | }; | |
136 | ||
137 | int ezusb_fx2_set_reset(struct usb_device *dev, unsigned char reset_bit) | |
138 | { | |
139 | return ezusb_set_reset(dev, ezusb_fx2.cpucs_reg, reset_bit); | |
140 | } | |
141 | EXPORT_SYMBOL_GPL(ezusb_fx2_set_reset); | |
142 | ||
8d733e26 RB |
143 | int ezusb_fx2_ihex_firmware_download(struct usb_device *dev, |
144 | const char *firmware_path) | |
145 | { | |
146 | return ezusb_ihex_firmware_download(dev, ezusb_fx2, firmware_path); | |
147 | } | |
148 | EXPORT_SYMBOL_GPL(ezusb_fx2_ihex_firmware_download); | |
c30186e5 | 149 | #endif |
8d733e26 | 150 | |
197ef5ef | 151 | MODULE_LICENSE("GPL"); |