blob: 947811bc8126a086e84c7856eca80b70fa4e3121 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * EZ-USB specific functions used by some of the USB to Serial drivers.
3 *
4 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/usb.h>
Rene Buergel8d733e22012-09-18 09:02:01 +020015#include <linux/firmware.h>
16#include <linux/ihex.h>
Greg Kroah-Hartmanc30186e2012-10-24 14:19:16 -070017#include <linux/usb/ezusb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Rene Buergelcc183e22012-09-18 09:00:41 +020019struct ezusb_fx_type {
20 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
21 unsigned short cpucs_reg;
22 unsigned short max_internal_adress;
23};
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Greg Kroah-Hartmanc30186e2012-10-24 14:19:16 -070025static struct ezusb_fx_type ezusb_fx1 = {
Rene Buergelcc183e22012-09-18 09:00:41 +020026 .cpucs_reg = 0x7F92,
27 .max_internal_adress = 0x1B3F,
28};
29
Rene Buergelcc183e22012-09-18 09:00:41 +020030/* Commands for writing to memory */
Rene Buergel99495c72012-09-13 22:14:38 +020031#define WRITE_INT_RAM 0xA0
Rene Buergelcc183e22012-09-18 09:00:41 +020032#define WRITE_EXT_RAM 0xA3
Rene Buergel99495c72012-09-13 22:14:38 +020033
Greg Kroah-Hartmanc30186e2012-10-24 14:19:16 -070034static int ezusb_writememory(struct usb_device *dev, int address,
Alan Cox80359a92008-07-22 11:09:16 +010035 unsigned char *data, int length, __u8 request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 int result;
38 unsigned char *transfer_buffer;
39
Greg Kroah-Hartmanc6ab0152012-09-18 16:55:48 +010040 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Eric Sesterhenn5d7efe52006-10-26 21:06:24 +020043 transfer_buffer = kmemdup(data, length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 if (!transfer_buffer) {
Rene Buergel99495c72012-09-13 22:14:38 +020045 dev_err(&dev->dev, "%s - kmalloc(%d) failed.\n",
Alan Cox80359a92008-07-22 11:09:16 +010046 __func__, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return -ENOMEM;
48 }
Rene Buergel99495c72012-09-13 22:14:38 +020049 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
50 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
51 address, 0, transfer_buffer, length, 3000);
52
Alan Cox80359a92008-07-22 11:09:16 +010053 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 return result;
55}
56
Greg Kroah-Hartmanc30186e2012-10-24 14:19:16 -070057static int ezusb_set_reset(struct usb_device *dev, unsigned short cpucs_reg,
58 unsigned char reset_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Rene Buergelcc183e22012-09-18 09:00:41 +020060 int response = ezusb_writememory(dev, cpucs_reg, &reset_bit, 1, WRITE_INT_RAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (response < 0)
Rene Buergel99495c72012-09-13 22:14:38 +020062 dev_err(&dev->dev, "%s-%d failed: %d\n",
63 __func__, reset_bit, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return response;
65}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Rene Buergelcc183e22012-09-18 09:00:41 +020067int ezusb_fx1_set_reset(struct usb_device *dev, unsigned char reset_bit)
68{
69 return ezusb_set_reset(dev, ezusb_fx1.cpucs_reg, reset_bit);
70}
71EXPORT_SYMBOL_GPL(ezusb_fx1_set_reset);
72
Rene Buergel8d733e22012-09-18 09:02:01 +020073static int ezusb_ihex_firmware_download(struct usb_device *dev,
74 struct ezusb_fx_type fx,
75 const char *firmware_path)
76{
77 int ret = -ENOENT;
78 const struct firmware *firmware = NULL;
79 const struct ihex_binrec *record;
80
81 if (request_ihex_firmware(&firmware, firmware_path,
82 &dev->dev)) {
83 dev_err(&dev->dev,
84 "%s - request \"%s\" failed\n",
85 __func__, firmware_path);
86 goto out;
87 }
88
89 ret = ezusb_set_reset(dev, fx.cpucs_reg, 0);
90 if (ret < 0)
91 goto out;
92
93 record = (const struct ihex_binrec *)firmware->data;
94 for (; record; record = ihex_next_binrec(record)) {
95 if (be32_to_cpu(record->addr) > fx.max_internal_adress) {
96 ret = ezusb_writememory(dev, be32_to_cpu(record->addr),
97 (unsigned char *)record->data,
98 be16_to_cpu(record->len), WRITE_EXT_RAM);
99 if (ret < 0) {
100 dev_err(&dev->dev, "%s - ezusb_writememory "
101 "failed writing internal memory "
102 "(%d %04X %p %d)\n", __func__, ret,
103 be32_to_cpu(record->addr), record->data,
104 be16_to_cpu(record->len));
105 goto out;
106 }
107 }
108 }
109
110 ret = ezusb_set_reset(dev, fx.cpucs_reg, 1);
111 if (ret < 0)
112 goto out;
113 record = (const struct ihex_binrec *)firmware->data;
114 for (; record; record = ihex_next_binrec(record)) {
115 if (be32_to_cpu(record->addr) <= fx.max_internal_adress) {
116 ret = ezusb_writememory(dev, be32_to_cpu(record->addr),
117 (unsigned char *)record->data,
118 be16_to_cpu(record->len), WRITE_INT_RAM);
119 if (ret < 0) {
120 dev_err(&dev->dev, "%s - ezusb_writememory "
121 "failed writing external memory "
122 "(%d %04X %p %d)\n", __func__, ret,
123 be32_to_cpu(record->addr), record->data,
124 be16_to_cpu(record->len));
125 goto out;
126 }
127 }
128 }
129 ret = ezusb_set_reset(dev, fx.cpucs_reg, 0);
130out:
131 release_firmware(firmware);
132 return ret;
133}
134
135int ezusb_fx1_ihex_firmware_download(struct usb_device *dev,
136 const char *firmware_path)
137{
138 return ezusb_ihex_firmware_download(dev, ezusb_fx1, firmware_path);
139}
140EXPORT_SYMBOL_GPL(ezusb_fx1_ihex_firmware_download);
141
Greg Kroah-Hartmanc30186e2012-10-24 14:19:16 -0700142#if 0
143/*
144 * Once someone one needs these fx2 functions, uncomment them
145 * and add them to ezusb.h and all should be good.
146 */
147static struct ezusb_fx_type ezusb_fx2 = {
148 .cpucs_reg = 0xE600,
149 .max_internal_adress = 0x3FFF,
150};
151
152int ezusb_fx2_set_reset(struct usb_device *dev, unsigned char reset_bit)
153{
154 return ezusb_set_reset(dev, ezusb_fx2.cpucs_reg, reset_bit);
155}
156EXPORT_SYMBOL_GPL(ezusb_fx2_set_reset);
157
Rene Buergel8d733e22012-09-18 09:02:01 +0200158int ezusb_fx2_ihex_firmware_download(struct usb_device *dev,
159 const char *firmware_path)
160{
161 return ezusb_ihex_firmware_download(dev, ezusb_fx2, firmware_path);
162}
163EXPORT_SYMBOL_GPL(ezusb_fx2_ihex_firmware_download);
Greg Kroah-Hartmanc30186e2012-10-24 14:19:16 -0700164#endif
Rene Buergel8d733e22012-09-18 09:02:01 +0200165
Dave Jones197ef5ef2012-10-23 20:29:15 -0400166MODULE_LICENSE("GPL");