xref: /openbmc/linux/drivers/usb/misc/ezusb.c (revision ced6a0ba)
1 // SPDX-License-Identifier: GPL-2.0
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)
6  */
7 
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>
15 
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 };
21 
22 static const struct ezusb_fx_type ezusb_fx1 = {
23 	.cpucs_reg = 0x7F92,
24 	.max_internal_adress = 0x1B3F,
25 };
26 
27 /* Commands for writing to memory */
28 #define WRITE_INT_RAM 0xA0
29 #define WRITE_EXT_RAM 0xA3
30 
ezusb_writememory(struct usb_device * dev,int address,unsigned char * data,int length,__u8 request)31 static int ezusb_writememory(struct usb_device *dev, int address,
32 				unsigned char *data, int length, __u8 request)
33 {
34 	if (!dev)
35 		return -ENODEV;
36 
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);
40 }
41 
ezusb_set_reset(struct usb_device * dev,unsigned short cpucs_reg,unsigned char reset_bit)42 static int ezusb_set_reset(struct usb_device *dev, unsigned short cpucs_reg,
43 			   unsigned char reset_bit)
44 {
45 	int response = ezusb_writememory(dev, cpucs_reg, &reset_bit, 1, WRITE_INT_RAM);
46 	if (response < 0)
47 		dev_err(&dev->dev, "%s-%d failed: %d\n",
48 						__func__, reset_bit, response);
49 	return response;
50 }
51 
ezusb_fx1_set_reset(struct usb_device * dev,unsigned char reset_bit)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 
ezusb_ihex_firmware_download(struct usb_device * dev,struct ezusb_fx_type fx,const char * firmware_path)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 
ezusb_fx1_ihex_firmware_download(struct usb_device * dev,const char * firmware_path)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 
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 
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);
149 #endif
150 
151 MODULE_LICENSE("GPL");
152