Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * EZ-USB specific functions used by some of the USB to Serial drivers.
0004  *
0005  * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/slab.h>
0010 #include <linux/module.h>
0011 #include <linux/usb.h>
0012 #include <linux/firmware.h>
0013 #include <linux/ihex.h>
0014 #include <linux/usb/ezusb.h>
0015 
0016 struct ezusb_fx_type {
0017     /* EZ-USB Control and Status Register.  Bit 0 controls 8051 reset */
0018     unsigned short cpucs_reg;
0019     unsigned short max_internal_adress;
0020 };
0021 
0022 static const struct ezusb_fx_type ezusb_fx1 = {
0023     .cpucs_reg = 0x7F92,
0024     .max_internal_adress = 0x1B3F,
0025 };
0026 
0027 /* Commands for writing to memory */
0028 #define WRITE_INT_RAM 0xA0
0029 #define WRITE_EXT_RAM 0xA3
0030 
0031 static int ezusb_writememory(struct usb_device *dev, int address,
0032                 unsigned char *data, int length, __u8 request)
0033 {
0034     if (!dev)
0035         return -ENODEV;
0036 
0037     return usb_control_msg_send(dev, 0, request,
0038                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0039                  address, 0, data, length, 3000, GFP_KERNEL);
0040 }
0041 
0042 static int ezusb_set_reset(struct usb_device *dev, unsigned short cpucs_reg,
0043                unsigned char reset_bit)
0044 {
0045     int response = ezusb_writememory(dev, cpucs_reg, &reset_bit, 1, WRITE_INT_RAM);
0046     if (response < 0)
0047         dev_err(&dev->dev, "%s-%d failed: %d\n",
0048                         __func__, reset_bit, response);
0049     return response;
0050 }
0051 
0052 int ezusb_fx1_set_reset(struct usb_device *dev, unsigned char reset_bit)
0053 {
0054     return ezusb_set_reset(dev, ezusb_fx1.cpucs_reg, reset_bit);
0055 }
0056 EXPORT_SYMBOL_GPL(ezusb_fx1_set_reset);
0057 
0058 static int ezusb_ihex_firmware_download(struct usb_device *dev,
0059                     struct ezusb_fx_type fx,
0060                     const char *firmware_path)
0061 {
0062     int ret = -ENOENT;
0063     const struct firmware *firmware = NULL;
0064     const struct ihex_binrec *record;
0065 
0066     if (request_ihex_firmware(&firmware, firmware_path,
0067                   &dev->dev)) {
0068         dev_err(&dev->dev,
0069             "%s - request \"%s\" failed\n",
0070             __func__, firmware_path);
0071         goto out;
0072     }
0073 
0074     ret = ezusb_set_reset(dev, fx.cpucs_reg, 0);
0075     if (ret < 0)
0076         goto out;
0077 
0078     record = (const struct ihex_binrec *)firmware->data;
0079     for (; record; record = ihex_next_binrec(record)) {
0080         if (be32_to_cpu(record->addr) > fx.max_internal_adress) {
0081             ret = ezusb_writememory(dev, be32_to_cpu(record->addr),
0082                         (unsigned char *)record->data,
0083                         be16_to_cpu(record->len), WRITE_EXT_RAM);
0084             if (ret < 0) {
0085                 dev_err(&dev->dev, "%s - ezusb_writememory "
0086                     "failed writing internal memory "
0087                     "(%d %04X %p %d)\n", __func__, ret,
0088                     be32_to_cpu(record->addr), record->data,
0089                     be16_to_cpu(record->len));
0090                 goto out;
0091             }
0092         }
0093     }
0094 
0095     ret = ezusb_set_reset(dev, fx.cpucs_reg, 1);
0096     if (ret < 0)
0097         goto out;
0098     record = (const struct ihex_binrec *)firmware->data;
0099     for (; record; record = ihex_next_binrec(record)) {
0100         if (be32_to_cpu(record->addr) <= fx.max_internal_adress) {
0101             ret = ezusb_writememory(dev, be32_to_cpu(record->addr),
0102                         (unsigned char *)record->data,
0103                         be16_to_cpu(record->len), WRITE_INT_RAM);
0104             if (ret < 0) {
0105                 dev_err(&dev->dev, "%s - ezusb_writememory "
0106                     "failed writing external memory "
0107                     "(%d %04X %p %d)\n", __func__, ret,
0108                     be32_to_cpu(record->addr), record->data,
0109                     be16_to_cpu(record->len));
0110                 goto out;
0111             }
0112         }
0113     }
0114     ret = ezusb_set_reset(dev, fx.cpucs_reg, 0);
0115 out:
0116     release_firmware(firmware);
0117     return ret;
0118 }
0119 
0120 int ezusb_fx1_ihex_firmware_download(struct usb_device *dev,
0121                      const char *firmware_path)
0122 {
0123     return ezusb_ihex_firmware_download(dev, ezusb_fx1, firmware_path);
0124 }
0125 EXPORT_SYMBOL_GPL(ezusb_fx1_ihex_firmware_download);
0126 
0127 #if 0
0128 /*
0129  * Once someone one needs these fx2 functions, uncomment them
0130  * and add them to ezusb.h and all should be good.
0131  */
0132 static struct ezusb_fx_type ezusb_fx2 = {
0133     .cpucs_reg = 0xE600,
0134     .max_internal_adress = 0x3FFF,
0135 };
0136 
0137 int ezusb_fx2_set_reset(struct usb_device *dev, unsigned char reset_bit)
0138 {
0139     return ezusb_set_reset(dev, ezusb_fx2.cpucs_reg, reset_bit);
0140 }
0141 EXPORT_SYMBOL_GPL(ezusb_fx2_set_reset);
0142 
0143 int ezusb_fx2_ihex_firmware_download(struct usb_device *dev,
0144                      const char *firmware_path)
0145 {
0146     return ezusb_ihex_firmware_download(dev, ezusb_fx2, firmware_path);
0147 }
0148 EXPORT_SYMBOL_GPL(ezusb_fx2_ihex_firmware_download);
0149 #endif
0150 
0151 MODULE_LICENSE("GPL");