2009-04-26 13:37:31 +03:00
|
|
|
/*
|
2009-06-25 07:33:46 +03:00
|
|
|
* Authors: Marek Lindner <lindner_marek@yahoo.de>
|
|
|
|
* Xiangfu Liu <xiangfu.z@gmail.com>
|
2009-04-26 13:37:31 +03:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2009-06-25 07:33:46 +03:00
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 3 of the License, or (at your option) any later version.
|
2009-04-26 13:37:31 +03:00
|
|
|
*/
|
|
|
|
|
2009-04-28 20:07:43 +03:00
|
|
|
#include "ingenic_usb.h"
|
2009-04-27 11:54:10 +03:00
|
|
|
#include "usb_boot_defines.h"
|
2009-04-26 13:37:31 +03:00
|
|
|
#include <usb.h>
|
|
|
|
#include <stdio.h>
|
2009-04-28 21:38:26 +03:00
|
|
|
#include <string.h>
|
2009-04-26 13:37:31 +03:00
|
|
|
|
2009-04-28 18:57:53 +03:00
|
|
|
extern unsigned int total_size;
|
2009-04-26 13:37:31 +03:00
|
|
|
|
|
|
|
static int get_ingenic_device(struct ingenic_dev *ingenic_dev)
|
|
|
|
{
|
|
|
|
struct usb_bus *usb_busses, *usb_bus;
|
|
|
|
struct usb_device *usb_dev;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
usb_busses = usb_get_busses();
|
|
|
|
|
|
|
|
for (usb_bus = usb_busses; usb_bus != NULL; usb_bus = usb_bus->next) {
|
2009-05-14 20:11:03 +03:00
|
|
|
for (usb_dev = usb_bus->devices; usb_dev != NULL;
|
|
|
|
usb_dev = usb_dev->next) {
|
2009-04-26 13:37:31 +03:00
|
|
|
|
|
|
|
if ((usb_dev->descriptor.idVendor == VENDOR_ID) &&
|
|
|
|
(usb_dev->descriptor.idProduct == PRODUCT_ID)) {
|
|
|
|
ingenic_dev->usb_dev = usb_dev;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_ingenic_interface(struct ingenic_dev *ingenic_dev)
|
|
|
|
{
|
|
|
|
struct usb_config_descriptor *usb_config_desc;
|
|
|
|
struct usb_interface_descriptor *usb_if_desc;
|
|
|
|
struct usb_interface *usb_if;
|
|
|
|
int config_index, if_index, alt_index;
|
|
|
|
|
2009-05-14 12:15:11 +03:00
|
|
|
for (config_index = 0;
|
|
|
|
config_index < ingenic_dev->usb_dev->descriptor.bNumConfigurations;
|
|
|
|
config_index++) {
|
2009-04-26 13:37:31 +03:00
|
|
|
usb_config_desc = &ingenic_dev->usb_dev->config[config_index];
|
|
|
|
|
|
|
|
if (!usb_config_desc)
|
|
|
|
return 0;
|
|
|
|
|
2009-05-14 20:11:03 +03:00
|
|
|
for (if_index = 0; if_index < usb_config_desc->bNumInterfaces;
|
|
|
|
if_index++) {
|
2009-04-26 13:37:31 +03:00
|
|
|
usb_if = &usb_config_desc->interface[if_index];
|
|
|
|
|
|
|
|
if (!usb_if)
|
|
|
|
return 0;
|
|
|
|
|
2009-05-14 20:11:03 +03:00
|
|
|
for (alt_index = 0; alt_index < usb_if->num_altsetting;
|
|
|
|
alt_index++) {
|
2009-04-26 13:37:31 +03:00
|
|
|
usb_if_desc = &usb_if->altsetting[alt_index];
|
|
|
|
|
|
|
|
if (!usb_if_desc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((usb_if_desc->bInterfaceClass == 0xff) &&
|
|
|
|
(usb_if_desc->bInterfaceSubClass == 0)) {
|
2009-05-14 20:11:03 +03:00
|
|
|
ingenic_dev->interface =
|
|
|
|
usb_if_desc->bInterfaceNumber;
|
2009-04-26 13:37:31 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_ingenic_init(struct ingenic_dev *ingenic_dev)
|
|
|
|
{
|
|
|
|
int num_ingenic, status = -1;
|
|
|
|
|
2009-04-30 20:26:14 +03:00
|
|
|
memset(ingenic_dev, 0, sizeof(struct ingenic_dev));
|
|
|
|
|
2009-04-26 13:37:31 +03:00
|
|
|
usb_init();
|
2009-04-26 23:08:14 +03:00
|
|
|
/* usb_set_debug(255); */
|
2009-04-26 13:37:31 +03:00
|
|
|
usb_find_busses();
|
|
|
|
usb_find_devices();
|
|
|
|
|
|
|
|
num_ingenic = get_ingenic_device(ingenic_dev);
|
|
|
|
|
|
|
|
if (num_ingenic == 0) {
|
2009-06-25 08:37:17 +03:00
|
|
|
fprintf(stderr, "Error - no XBurst device found\n");
|
2009-04-26 13:37:31 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_ingenic > 1) {
|
2009-06-25 08:37:17 +03:00
|
|
|
fprintf(stderr, "Error - too many XBurst devices found: %i\n",
|
2009-05-14 20:11:03 +03:00
|
|
|
num_ingenic);
|
2009-04-26 13:37:31 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ingenic_dev->usb_handle = usb_open(ingenic_dev->usb_dev);
|
|
|
|
if (!ingenic_dev->usb_handle) {
|
2009-06-25 08:37:17 +03:00
|
|
|
fprintf(stderr, "Error - can't open XBurst device: %s\n",
|
2009-05-14 20:11:03 +03:00
|
|
|
usb_strerror());
|
2009-04-26 13:37:31 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_ingenic_interface(ingenic_dev) < 1) {
|
2009-06-25 08:37:17 +03:00
|
|
|
fprintf(stderr, "Error - can't find XBurst interface\n");
|
2009-04-26 13:37:31 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-05-14 20:11:03 +03:00
|
|
|
if (usb_claim_interface(ingenic_dev->usb_handle, ingenic_dev->interface)
|
|
|
|
< 0) {
|
2009-06-25 08:37:17 +03:00
|
|
|
fprintf(stderr, "Error - can't claim XBurst interface: %s\n",
|
2009-05-14 20:11:03 +03:00
|
|
|
usb_strerror());
|
2009-04-26 13:37:31 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = 1;
|
|
|
|
|
|
|
|
out:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_get_ingenic_cpu(struct ingenic_dev *ingenic_dev)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
2009-05-01 19:14:55 +03:00
|
|
|
memset(&ingenic_dev->cpu_info_buff, 0,
|
|
|
|
ARRAY_SIZE(ingenic_dev->cpu_info_buff));
|
|
|
|
|
2009-05-07 06:07:52 +03:00
|
|
|
sleep(1);
|
2009-04-26 13:37:31 +03:00
|
|
|
status = usb_control_msg(ingenic_dev->usb_handle,
|
2009-05-14 20:11:03 +03:00
|
|
|
/* bmRequestType */ USB_ENDPOINT_IN | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
|
2009-04-26 13:37:31 +03:00
|
|
|
/* bRequest */ VR_GET_CPU_INFO,
|
|
|
|
/* wValue */ 0,
|
|
|
|
/* wIndex */ 0,
|
|
|
|
/* Data */ ingenic_dev->cpu_info_buff,
|
|
|
|
/* wLength */ 8,
|
|
|
|
USB_TIMEOUT);
|
|
|
|
|
2009-04-27 19:08:41 +03:00
|
|
|
if (status != sizeof(ingenic_dev->cpu_info_buff) - 1 ) {
|
2009-05-14 20:11:03 +03:00
|
|
|
fprintf(stderr, "Error - "
|
2009-06-25 08:37:17 +03:00
|
|
|
"can't retrieve XBurst CPU information: %i\n", status);
|
2009-04-27 11:54:10 +03:00
|
|
|
return status;
|
2009-04-26 13:37:31 +03:00
|
|
|
}
|
|
|
|
|
2009-04-27 19:08:41 +03:00
|
|
|
ingenic_dev->cpu_info_buff[8] = '\0';
|
2009-06-25 08:37:17 +03:00
|
|
|
printf(" CPU data: "
|
2009-05-14 20:11:03 +03:00
|
|
|
"%02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x : %s\n",
|
2009-04-26 13:37:31 +03:00
|
|
|
ingenic_dev->cpu_info_buff[0], ingenic_dev->cpu_info_buff[1],
|
|
|
|
ingenic_dev->cpu_info_buff[2], ingenic_dev->cpu_info_buff[3],
|
|
|
|
ingenic_dev->cpu_info_buff[4], ingenic_dev->cpu_info_buff[5],
|
2009-04-27 09:42:15 +03:00
|
|
|
ingenic_dev->cpu_info_buff[6], ingenic_dev->cpu_info_buff[7],
|
|
|
|
ingenic_dev->cpu_info_buff);
|
2009-04-26 13:37:31 +03:00
|
|
|
|
2009-04-27 19:08:41 +03:00
|
|
|
if (!strcmp(ingenic_dev->cpu_info_buff,"JZ4740V1")) return 1;
|
|
|
|
if (!strcmp(ingenic_dev->cpu_info_buff,"JZ4750V1")) return 2;
|
|
|
|
if (!strcmp(ingenic_dev->cpu_info_buff,"Boot4740")) return 3;
|
|
|
|
if (!strcmp(ingenic_dev->cpu_info_buff,"Boot4750")) return 4;
|
|
|
|
|
|
|
|
return 0;
|
2009-04-27 11:54:10 +03:00
|
|
|
}
|
2009-04-26 13:37:31 +03:00
|
|
|
|
2009-04-27 11:54:10 +03:00
|
|
|
int usb_ingenic_flush_cache(struct ingenic_dev *ingenic_dev)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
status = usb_control_msg(ingenic_dev->usb_handle,
|
|
|
|
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
|
|
|
|
/* bRequest */ VR_FLUSH_CACHES,
|
|
|
|
/* wValue */ 0,
|
|
|
|
/* wIndex */ 0,
|
|
|
|
/* Data */ 0,
|
|
|
|
/* wLength */ 0,
|
|
|
|
USB_TIMEOUT);
|
|
|
|
|
2009-04-27 19:08:41 +03:00
|
|
|
if (status != 0) {
|
2009-04-27 11:54:10 +03:00
|
|
|
fprintf(stderr, "Error - can't flush cache: %i\n", status);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2009-04-26 13:37:31 +03:00
|
|
|
}
|
|
|
|
|
2009-05-03 18:33:56 +03:00
|
|
|
int usb_send_data_length_to_ingenic(struct ingenic_dev *ingenic_dev, int len)
|
2009-04-26 13:37:31 +03:00
|
|
|
{
|
|
|
|
int status;
|
2009-05-01 19:14:55 +03:00
|
|
|
/* tell the device the length of the file to be uploaded */
|
|
|
|
status = usb_control_msg(ingenic_dev->usb_handle,
|
|
|
|
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
|
|
|
|
/* bRequest */ VR_SET_DATA_LENGTH,
|
2009-05-08 19:14:14 +03:00
|
|
|
/* wValue */ STAGE_ADDR_MSB(len),
|
|
|
|
/* wIndex */ STAGE_ADDR_LSB(len),
|
2009-05-01 19:14:55 +03:00
|
|
|
/* Data */ 0,
|
|
|
|
/* wLength */ 0,
|
|
|
|
USB_TIMEOUT);
|
2009-04-26 13:37:31 +03:00
|
|
|
|
2009-05-12 17:41:52 +03:00
|
|
|
if (status != 0) {
|
2009-05-14 20:11:03 +03:00
|
|
|
fprintf(stderr, "Error - "
|
|
|
|
"can't set data length on Ingenic device: %i\n", status);
|
2009-05-12 17:41:52 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2009-04-27 11:54:10 +03:00
|
|
|
|
2009-05-12 17:41:52 +03:00
|
|
|
return 1;
|
2009-05-01 19:14:55 +03:00
|
|
|
}
|
|
|
|
|
2009-05-14 20:11:03 +03:00
|
|
|
int usb_send_data_address_to_ingenic(struct ingenic_dev *ingenic_dev,
|
|
|
|
unsigned int stage_addr)
|
2009-05-01 19:14:55 +03:00
|
|
|
{
|
|
|
|
int status;
|
2009-04-26 13:37:31 +03:00
|
|
|
/* tell the device the RAM address to store the file */
|
|
|
|
status = usb_control_msg(ingenic_dev->usb_handle,
|
|
|
|
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
|
|
|
|
/* bRequest */ VR_SET_DATA_ADDRESS,
|
2009-04-27 19:08:41 +03:00
|
|
|
/* wValue */ STAGE_ADDR_MSB(stage_addr),
|
|
|
|
/* wIndex */ STAGE_ADDR_LSB(stage_addr),
|
2009-04-26 13:37:31 +03:00
|
|
|
/* Data */ 0,
|
|
|
|
/* wLength */ 0,
|
|
|
|
USB_TIMEOUT);
|
|
|
|
|
|
|
|
if (status != 0) {
|
2009-05-14 20:11:03 +03:00
|
|
|
fprintf(stderr, "Error - "
|
|
|
|
"can't set the address on Ingenic device: %i\n", status);
|
2009-05-01 19:14:55 +03:00
|
|
|
return -1;
|
2009-04-26 13:37:31 +03:00
|
|
|
}
|
|
|
|
|
2009-05-12 17:41:52 +03:00
|
|
|
return 1;
|
2009-05-01 19:14:55 +03:00
|
|
|
}
|
2009-04-26 21:11:19 +03:00
|
|
|
|
2009-05-01 19:14:55 +03:00
|
|
|
int usb_send_data_to_ingenic(struct ingenic_dev *ingenic_dev)
|
|
|
|
{
|
|
|
|
int status;
|
2009-04-26 23:08:14 +03:00
|
|
|
status = usb_bulk_write(ingenic_dev->usb_handle,
|
|
|
|
/* endpoint */ INGENIC_OUT_ENDPOINT,
|
2009-04-26 21:11:19 +03:00
|
|
|
/* bulk data */ ingenic_dev->file_buff,
|
|
|
|
/* bulk data length */ ingenic_dev->file_len,
|
|
|
|
USB_TIMEOUT);
|
|
|
|
if (status < ingenic_dev->file_len) {
|
2009-05-14 20:11:03 +03:00
|
|
|
fprintf(stderr, "Error - "
|
|
|
|
"can't send bulk data to Ingenic CPU: %i\n", status);
|
2009-04-26 21:11:19 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2009-05-14 12:15:11 +03:00
|
|
|
|
2009-05-01 19:14:55 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-05-14 20:11:03 +03:00
|
|
|
int usb_read_data_from_ingenic(struct ingenic_dev *ingenic_dev,
|
|
|
|
unsigned char *buff, unsigned int len)
|
2009-05-04 17:36:51 +03:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
status = usb_bulk_read(ingenic_dev->usb_handle,
|
2009-05-14 20:11:03 +03:00
|
|
|
/* endpoint */ INGENIC_IN_ENDPOINT,
|
2009-05-14 12:15:11 +03:00
|
|
|
/* bulk data */ buff,
|
|
|
|
/* bulk data length */ len,
|
2009-05-04 17:36:51 +03:00
|
|
|
USB_TIMEOUT);
|
2009-05-14 12:15:11 +03:00
|
|
|
if (status < len) {
|
2009-05-14 20:11:03 +03:00
|
|
|
fprintf(stderr, "Error - "
|
|
|
|
"can't read bulk data from Ingenic device:%i\n", status);
|
2009-05-04 17:36:51 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2009-05-12 17:41:52 +03:00
|
|
|
|
2009-05-04 17:36:51 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-05-01 19:14:55 +03:00
|
|
|
int usb_ingenic_upload(struct ingenic_dev *ingenic_dev, int stage)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
unsigned int stage2_addr;
|
|
|
|
stage2_addr = total_size + 0x80000000;
|
|
|
|
stage2_addr -= CODE_SIZE;
|
2009-05-14 12:15:11 +03:00
|
|
|
|
2009-05-01 19:14:55 +03:00
|
|
|
int stage_addr = (stage == 1 ? 0x80002000 : stage2_addr);
|
|
|
|
|
|
|
|
usb_send_data_address_to_ingenic(ingenic_dev, stage_addr);
|
2009-06-25 08:37:17 +03:00
|
|
|
printf(" Download stage %d program and execute at 0x%08x\n",
|
2009-05-14 20:11:03 +03:00
|
|
|
stage, (stage_addr));
|
2009-05-01 19:14:55 +03:00
|
|
|
usb_send_data_to_ingenic(ingenic_dev);
|
2009-04-26 13:37:31 +03:00
|
|
|
|
2009-04-27 12:37:05 +03:00
|
|
|
if (stage == 2) {
|
2009-05-26 20:05:54 +03:00
|
|
|
if (usb_get_ingenic_cpu(ingenic_dev) < 1)
|
|
|
|
return -1;
|
2009-04-27 11:54:10 +03:00
|
|
|
usb_ingenic_flush_cache(ingenic_dev);
|
|
|
|
}
|
|
|
|
|
2009-04-26 13:37:31 +03:00
|
|
|
/* tell the device to start the uploaded device */
|
|
|
|
status = usb_control_msg(ingenic_dev->usb_handle,
|
|
|
|
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
|
|
|
|
/* bRequest */ (stage == 1 ? VR_PROGRAM_START1 : VR_PROGRAM_START2),
|
2009-04-27 19:08:41 +03:00
|
|
|
/* wValue */ STAGE_ADDR_MSB(stage_addr),
|
|
|
|
/* wIndex */ STAGE_ADDR_LSB(stage_addr),
|
2009-04-26 13:37:31 +03:00
|
|
|
/* Data */ 0,
|
|
|
|
/* wLength */ 0,
|
|
|
|
USB_TIMEOUT);
|
|
|
|
|
|
|
|
if (status != 0) {
|
2009-05-14 20:11:03 +03:00
|
|
|
fprintf(stderr, "Error - can't start the uploaded binary "
|
|
|
|
"on the Ingenic device: %i\n", status);
|
2009-05-14 12:15:11 +03:00
|
|
|
return status;
|
2009-04-26 13:37:31 +03:00
|
|
|
}
|
|
|
|
|
2009-05-26 20:05:54 +03:00
|
|
|
if (usb_get_ingenic_cpu(ingenic_dev) < 1)
|
|
|
|
return -1;
|
2009-04-26 13:37:31 +03:00
|
|
|
|
2009-05-14 12:15:11 +03:00
|
|
|
return 1;
|
2009-04-26 13:37:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_ingenic_cleanup(struct ingenic_dev *ingenic_dev)
|
|
|
|
{
|
|
|
|
if ((ingenic_dev->usb_handle) && (ingenic_dev->interface))
|
2009-05-14 20:11:03 +03:00
|
|
|
usb_release_interface(ingenic_dev->usb_handle,
|
|
|
|
ingenic_dev->interface);
|
2009-04-26 13:37:31 +03:00
|
|
|
|
|
|
|
if (ingenic_dev->usb_handle)
|
|
|
|
usb_close(ingenic_dev->usb_handle);
|
|
|
|
}
|
2009-05-03 18:06:23 +03:00
|
|
|
|
|
|
|
int usb_ingenic_nand_ops(struct ingenic_dev *ingenic_dev, int ops)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
status = usb_control_msg(ingenic_dev->usb_handle,
|
|
|
|
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
|
|
|
|
/* bRequest */ VR_NAND_OPS,
|
|
|
|
/* wValue */ ops & 0xffff,
|
|
|
|
/* wIndex */ 0,
|
|
|
|
/* Data */ 0,
|
|
|
|
/* wLength */ 0,
|
|
|
|
USB_TIMEOUT);
|
|
|
|
|
|
|
|
if (status != 0) {
|
2009-05-14 20:11:03 +03:00
|
|
|
fprintf(stderr, "Error - "
|
|
|
|
"can't set Ingenic device nand ops: %i\n", status);
|
2009-05-03 18:06:23 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-05-12 17:41:52 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_ingenic_configration(struct ingenic_dev *ingenic_dev, int ops)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
status = usb_control_msg(ingenic_dev->usb_handle,
|
|
|
|
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
|
|
|
|
/* bRequest */ VR_CONFIGRATION,
|
|
|
|
/* wValue */ ops,
|
|
|
|
/* wIndex */ 0,
|
|
|
|
/* Data */ 0,
|
|
|
|
/* wLength */ 0,
|
|
|
|
USB_TIMEOUT);
|
|
|
|
|
|
|
|
if (status != 0) {
|
2009-05-14 20:11:03 +03:00
|
|
|
fprintf(stderr, "Error - "
|
|
|
|
"can't init Ingenic configration: %i\n", status);
|
2009-05-12 17:41:52 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2009-05-03 18:06:23 +03:00
|
|
|
}
|