18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * EZ-USB specific functions used by some of the USB to Serial drivers.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/slab.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/usb.h>
128c2ecf20Sopenharmony_ci#include <linux/firmware.h>
138c2ecf20Sopenharmony_ci#include <linux/ihex.h>
148c2ecf20Sopenharmony_ci#include <linux/usb/ezusb.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistruct ezusb_fx_type {
178c2ecf20Sopenharmony_ci	/* EZ-USB Control and Status Register.  Bit 0 controls 8051 reset */
188c2ecf20Sopenharmony_ci	unsigned short cpucs_reg;
198c2ecf20Sopenharmony_ci	unsigned short max_internal_adress;
208c2ecf20Sopenharmony_ci};
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic const struct ezusb_fx_type ezusb_fx1 = {
238c2ecf20Sopenharmony_ci	.cpucs_reg = 0x7F92,
248c2ecf20Sopenharmony_ci	.max_internal_adress = 0x1B3F,
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* Commands for writing to memory */
288c2ecf20Sopenharmony_ci#define WRITE_INT_RAM 0xA0
298c2ecf20Sopenharmony_ci#define WRITE_EXT_RAM 0xA3
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic int ezusb_writememory(struct usb_device *dev, int address,
328c2ecf20Sopenharmony_ci				unsigned char *data, int length, __u8 request)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	int result;
358c2ecf20Sopenharmony_ci	unsigned char *transfer_buffer;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	if (!dev)
388c2ecf20Sopenharmony_ci		return -ENODEV;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	transfer_buffer = kmemdup(data, length, GFP_KERNEL);
418c2ecf20Sopenharmony_ci	if (!transfer_buffer) {
428c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "%s - kmalloc(%d) failed.\n",
438c2ecf20Sopenharmony_ci							__func__, length);
448c2ecf20Sopenharmony_ci		return -ENOMEM;
458c2ecf20Sopenharmony_ci	}
468c2ecf20Sopenharmony_ci	result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
478c2ecf20Sopenharmony_ci				 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
488c2ecf20Sopenharmony_ci				 address, 0, transfer_buffer, length, 3000);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	kfree(transfer_buffer);
518c2ecf20Sopenharmony_ci	return result;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic int ezusb_set_reset(struct usb_device *dev, unsigned short cpucs_reg,
558c2ecf20Sopenharmony_ci			   unsigned char reset_bit)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	int response = ezusb_writememory(dev, cpucs_reg, &reset_bit, 1, WRITE_INT_RAM);
588c2ecf20Sopenharmony_ci	if (response < 0)
598c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "%s-%d failed: %d\n",
608c2ecf20Sopenharmony_ci						__func__, reset_bit, response);
618c2ecf20Sopenharmony_ci	return response;
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ciint ezusb_fx1_set_reset(struct usb_device *dev, unsigned char reset_bit)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	return ezusb_set_reset(dev, ezusb_fx1.cpucs_reg, reset_bit);
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ezusb_fx1_set_reset);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic int ezusb_ihex_firmware_download(struct usb_device *dev,
718c2ecf20Sopenharmony_ci					struct ezusb_fx_type fx,
728c2ecf20Sopenharmony_ci					const char *firmware_path)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	int ret = -ENOENT;
758c2ecf20Sopenharmony_ci	const struct firmware *firmware = NULL;
768c2ecf20Sopenharmony_ci	const struct ihex_binrec *record;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (request_ihex_firmware(&firmware, firmware_path,
798c2ecf20Sopenharmony_ci				  &dev->dev)) {
808c2ecf20Sopenharmony_ci		dev_err(&dev->dev,
818c2ecf20Sopenharmony_ci			"%s - request \"%s\" failed\n",
828c2ecf20Sopenharmony_ci			__func__, firmware_path);
838c2ecf20Sopenharmony_ci		goto out;
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	ret = ezusb_set_reset(dev, fx.cpucs_reg, 0);
878c2ecf20Sopenharmony_ci	if (ret < 0)
888c2ecf20Sopenharmony_ci		goto out;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	record = (const struct ihex_binrec *)firmware->data;
918c2ecf20Sopenharmony_ci	for (; record; record = ihex_next_binrec(record)) {
928c2ecf20Sopenharmony_ci		if (be32_to_cpu(record->addr) > fx.max_internal_adress) {
938c2ecf20Sopenharmony_ci			ret = ezusb_writememory(dev, be32_to_cpu(record->addr),
948c2ecf20Sopenharmony_ci						(unsigned char *)record->data,
958c2ecf20Sopenharmony_ci						be16_to_cpu(record->len), WRITE_EXT_RAM);
968c2ecf20Sopenharmony_ci			if (ret < 0) {
978c2ecf20Sopenharmony_ci				dev_err(&dev->dev, "%s - ezusb_writememory "
988c2ecf20Sopenharmony_ci					"failed writing internal memory "
998c2ecf20Sopenharmony_ci					"(%d %04X %p %d)\n", __func__, ret,
1008c2ecf20Sopenharmony_ci					be32_to_cpu(record->addr), record->data,
1018c2ecf20Sopenharmony_ci					be16_to_cpu(record->len));
1028c2ecf20Sopenharmony_ci				goto out;
1038c2ecf20Sopenharmony_ci			}
1048c2ecf20Sopenharmony_ci		}
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	ret = ezusb_set_reset(dev, fx.cpucs_reg, 1);
1088c2ecf20Sopenharmony_ci	if (ret < 0)
1098c2ecf20Sopenharmony_ci		goto out;
1108c2ecf20Sopenharmony_ci	record = (const struct ihex_binrec *)firmware->data;
1118c2ecf20Sopenharmony_ci	for (; record; record = ihex_next_binrec(record)) {
1128c2ecf20Sopenharmony_ci		if (be32_to_cpu(record->addr) <= fx.max_internal_adress) {
1138c2ecf20Sopenharmony_ci			ret = ezusb_writememory(dev, be32_to_cpu(record->addr),
1148c2ecf20Sopenharmony_ci						(unsigned char *)record->data,
1158c2ecf20Sopenharmony_ci						be16_to_cpu(record->len), WRITE_INT_RAM);
1168c2ecf20Sopenharmony_ci			if (ret < 0) {
1178c2ecf20Sopenharmony_ci				dev_err(&dev->dev, "%s - ezusb_writememory "
1188c2ecf20Sopenharmony_ci					"failed writing external memory "
1198c2ecf20Sopenharmony_ci					"(%d %04X %p %d)\n", __func__, ret,
1208c2ecf20Sopenharmony_ci					be32_to_cpu(record->addr), record->data,
1218c2ecf20Sopenharmony_ci					be16_to_cpu(record->len));
1228c2ecf20Sopenharmony_ci				goto out;
1238c2ecf20Sopenharmony_ci			}
1248c2ecf20Sopenharmony_ci		}
1258c2ecf20Sopenharmony_ci	}
1268c2ecf20Sopenharmony_ci	ret = ezusb_set_reset(dev, fx.cpucs_reg, 0);
1278c2ecf20Sopenharmony_ciout:
1288c2ecf20Sopenharmony_ci	release_firmware(firmware);
1298c2ecf20Sopenharmony_ci	return ret;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ciint ezusb_fx1_ihex_firmware_download(struct usb_device *dev,
1338c2ecf20Sopenharmony_ci				     const char *firmware_path)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	return ezusb_ihex_firmware_download(dev, ezusb_fx1, firmware_path);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ezusb_fx1_ihex_firmware_download);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci#if 0
1408c2ecf20Sopenharmony_ci/*
1418c2ecf20Sopenharmony_ci * Once someone one needs these fx2 functions, uncomment them
1428c2ecf20Sopenharmony_ci * and add them to ezusb.h and all should be good.
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_cistatic struct ezusb_fx_type ezusb_fx2 = {
1458c2ecf20Sopenharmony_ci	.cpucs_reg = 0xE600,
1468c2ecf20Sopenharmony_ci	.max_internal_adress = 0x3FFF,
1478c2ecf20Sopenharmony_ci};
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ciint ezusb_fx2_set_reset(struct usb_device *dev, unsigned char reset_bit)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	return ezusb_set_reset(dev, ezusb_fx2.cpucs_reg, reset_bit);
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ezusb_fx2_set_reset);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ciint ezusb_fx2_ihex_firmware_download(struct usb_device *dev,
1568c2ecf20Sopenharmony_ci				     const char *firmware_path)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	return ezusb_ihex_firmware_download(dev, ezusb_fx2, firmware_path);
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ezusb_fx2_ihex_firmware_download);
1618c2ecf20Sopenharmony_ci#endif
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
164