Date:2010-01-08 03:35:59 (10 years 4 months ago)
Author:Lars C.
Commit:9aa52b794714b52a35bd414964cd6c99fca261b6
Message:more cleanup

Files: usbboot/src/cmd.c (7 diffs)
usbboot/src/command_line.c (5 diffs)
usbboot/src/ingenic_cfg.c (1 diff)
usbboot/src/ingenic_usb.c (4 diffs)
usbboot/src/ingenic_usb.h (4 diffs)
usbboot/src/main.c (2 diffs)
usbboot/src/nand.c (22 diffs)
usbboot/src/nand.h (1 diff)
usbboot/xburst_include/usb_boot.h (1 diff)
usbboot/xburst_stage1/board_4740.c (5 diffs)
usbboot/xburst_stage1/main.c (2 diffs)
usbboot/xburst_stage2/nandflash_4740.c (1 diff)

Change Details

usbboot/src/cmd.c
3131#include "ingenic_usb.h"
3232#include "usb_boot_defines.h"
3333
34struct hand hand;
3534struct sdram_in sdram_in;
3635
3736unsigned int total_size;
3837static char code_buf[4 * 512 * 1024];
3938static char ret[8];
4039
41
42static int load_file(struct ingenic_dev *ingenic_dev, const char *file_path)
40static int load_file(struct ingenic_dev *dev, const char *file_path, char *buf)
4341{
4442    struct stat fstat;
45    int fd, status, res = -1;
43    int fd, status;
4644
4745    status = stat(file_path, &fstat);
4846
4947    if (status < 0) {
5048        fprintf(stderr, "Error - can't get file size from '%s': %s\n",
5149            file_path, strerror(errno));
52        goto out;
50        return errno;
5351    }
5452
55    ingenic_dev->file_len = fstat.st_size;
56    ingenic_dev->file_buff = code_buf;
57
5853    fd = open(file_path, O_RDONLY);
5954
6055    if (fd < 0) {
61        fprintf(stderr, "Error - can't open file '%s': %s\n",
56        fprintf(stderr, "Error - can't open file '%s': %s\n",
6257            file_path, strerror(errno));
63        goto out;
58        return errno;
6459    }
6560
66    status = read(fd, ingenic_dev->file_buff, ingenic_dev->file_len);
61    status = read(fd, buf, fstat.st_size);
6762
68    if (status < (int)ingenic_dev->file_len) {
69        fprintf(stderr, "Error - can't read file '%s': %s\n",
63    if (status < (int)fstat.st_size) {
64        fprintf(stderr, "Error - can't read file '%s': %s\n",
7065            file_path, strerror(errno));
7166        goto close;
7267    }
7368
7469    /* write args to code */
75    memcpy(ingenic_dev->file_buff + 8, &hand.fw_args,
76           sizeof(struct fw_args));
77
78    res = 1;
70    memcpy(buf + 8, &dev->config.fw_args, sizeof(struct fw_args));
7971
8072close:
8173    close(fd);
82out:
83    return res;
74
75    return fstat.st_size;
8476}
8577
8678/* after upload stage2. must init device */
8779void init_cfg(struct ingenic_dev *dev)
8880{
8981    if (usb_get_ingenic_cpu(dev) < 3) {
90        printf(" XBurst CPU not booted yet, boot it first!\n");
82        printf("XBurst CPU not booted yet, boot it first!\n");
9183        return;
9284    }
9385
94    if (usb_send_data_to_ingenic(dev, (char*)&hand, sizeof(hand)) != 1)
86    if (usb_send_data_to_ingenic(dev, (char*)&dev->config, sizeof(dev->config))
87    < 0)
9588        goto xout;
9689
97    if (usb_ingenic_configration(dev, DS_hand) != 1)
90    if (usb_ingenic_configration(dev, DS_hand) < 0)
9891        goto xout;
9992
100    if (usb_read_data_from_ingenic(dev, ret, 8) != 1)
93    if (usb_read_data_from_ingenic(dev, ret, 8) < 0)
10194        goto xout;
10295
103    printf(" Configuring XBurst CPU succeeded.\n");
96    printf("Configuring XBurst CPU succeeded.\n");
10497    return;
10598xout:
10699    printf("Configuring XBurst CPU failed.\n");
...... 
109102int boot(struct ingenic_dev *dev, const char *stage1_path, const char *stage2_path)
110103{
111104    int status;
105    int size;
112106
113107    status = usb_get_ingenic_cpu(dev);
114108    switch (status) {
115    case 1: /* Jz4740v1 */
109    case INGENIC_CPU_JZ4740V1:
116110        status = 0;
117        hand.fw_args.cpu_id = 0x4740;
111        dev->config.fw_args.cpu_id = 0x4740;
118112        break;
119    case 2: /* Jz4750v1 */
113    case INGENIC_CPU_JZ4750V1:
120114        status = 0;
121        hand.fw_args.cpu_id = 0x4750;
115        dev->config.fw_args.cpu_id = 0x4750;
122116        break;
123    case 3: /* Boot4740 */
117    case INGENIC_CPU_JZ4740:
124118        status = 1;
125        hand.fw_args.cpu_id = 0x4740;
119        dev->config.fw_args.cpu_id = 0x4740;
126120        break;
127    case 4: /* Boot4750 */
121    case INGENIC_CPU_JZ4750:
128122        status = 1;
129        hand.fw_args.cpu_id = 0x4750;
123        dev->config.fw_args.cpu_id = 0x4750;
130124        break;
131125    default:
132        return 1;
126        return (status < 0) ? status : -1;
133127    }
134128
135129    if (status) {
136        printf(" Already booted.\n");
137        return 1;
130        printf("Already booted.\n");
131        return 0;
138132    } else {
139        printf(" CPU not yet booted, now booting...\n");
133        printf("CPU not yet booted, now booting...\n");
140134
141135        /* now we upload the boot stage1 */
142        printf(" Loading stage1 from '%s'\n", stage1_path);
143        if (load_file(dev, stage1_path) < 1)
144            return -1;
136        printf("Loading stage1 from '%s'\n", stage1_path);
137        size = load_file(dev, stage1_path, code_buf);
138        if (size < 0)
139            return size;
145140
146        if (usb_ingenic_upload(dev, 1) < 1)
141        if (usb_ingenic_upload(dev, 1, code_buf, size) < 0)
147142            return -1;
148143
149144        /* now we upload the boot stage2 */
150145        usleep(100);
151        printf(" Loading stage2 from '%s'\n", stage2_path);
152        if (load_file(dev, stage2_path) < 1)
153            return -1;
146        printf("Loading stage2 from '%s'\n", stage2_path);
147        size = load_file(dev, stage2_path, code_buf);
148        if (size < 0) {
149            printf("FOOBAR");
150            return size;
151        }
154152
155        if (usb_ingenic_upload(dev, 2) < 1)
153        if (usb_ingenic_upload(dev, 2, code_buf, size) < 0)
156154            return -1;
157155
158        printf(" Booted successfully!\n");
156        printf("Booted successfully!\n");
159157    }
160158    usleep(100);
161159    init_cfg(dev);
162    return 1;
160    return 0;
163161}
164162
165163
...... 
171169
172170    tmp = usb_get_ingenic_cpu(dev);
173171    if (tmp > 2) {
174        printf(" This command only run under UNBOOT state!\n");
172        printf("This command only run under UNBOOT state!\n");
175173        return -1;
176174    }
177175
178176    switch (tmp) {
179177    case 1:
180        tmp = 0;
181        hand.fw_args.cpu_id = 0x4740;
178        dev->config.fw_args.cpu_id = 0x4740;
182179        break;
183180    case 2:
184        tmp = 0;
185        hand.fw_args.cpu_id = 0x4750;
181        dev->config.fw_args.cpu_id = 0x4750;
186182        break;
187183    }
188184
189    hand.fw_args.debug_ops = 1;/* tell device it's memory debug */
190    hand.fw_args.start = start;
185    dev->config.fw_args.debug_ops = 1;/* tell device it's memory debug */
186    dev->config.fw_args.start = start;
191187
192188    if (size == 0)
193        hand.fw_args.size = total_size;
189        dev->config.fw_args.size = total_size;
194190    else
195        hand.fw_args.size = size;
191        dev->config.fw_args.size = size;
196192
197    printf(" Now test memory from 0x%x to 0x%x: \n",
198           start, start + hand.fw_args.size);
193    printf("Now test memory from 0x%x to 0x%x: \n",
194           start, start + dev->config.fw_args.size);
199195
200    if (load_file(dev, STAGE1_FILE_PATH) < 1)
201        return -1;
202    if (usb_ingenic_upload(dev, 1) < 1)
196    size = load_file(dev, STAGE1_FILE_PATH, code_buf);
197    if (size < 0)
198        return size;
199    if (usb_ingenic_upload(dev, 1, code_buf, size) < 1)
203200        return -1;
204201
205202    usleep(100);
206203    usb_read_data_from_ingenic(dev, buffer, 8);
207204    if (buffer[0] != 0)
208        printf(" Test memory fail! Last error address is 0x%x !\n",
205        printf("Test memory fail! Last error address is 0x%x !\n",
209206               buffer[0]);
210207    else
211        printf(" Test memory pass!\n");
208        printf("Test memory pass!\n");
212209
213210    return 1;
214211}
215212
216213int debug_go(struct ingenic_dev *dev, size_t argc, char *argv[])
217214{
218    unsigned int addr,obj;
215    unsigned int addr, obj;
219216    if (argc < 3) {
220217        printf(" Usage: go (1) (2) \n"
221218               " 1:start SDRAM address\n"
...... 
226223    addr = strtoul(argv[1], NULL, 0);
227224    obj = atoi(argv[2]);
228225
229    printf(" Executing No.%d device at address 0x%x\n", obj, addr);
226    printf("Executing No.%d device at address 0x%x\n", obj, addr);
230227
231228    if (usb_ingenic_start(dev, VR_PROGRAM_START2, addr) < 1)
232229        return -1;
...... 
237234int sdram_load(struct ingenic_dev *dev, struct sdram_in *sdram_in)
238235{
239236    if (usb_get_ingenic_cpu(dev) < 3) {
240        printf(" Device unboot! Boot it first!\n");
237        printf("Device unboot! Boot it first!\n");
241238        return -1;
242239    }
243240
244241    if (sdram_in->length > (unsigned int) MAX_LOAD_SIZE) {
245        printf(" Image length too long!\n");
242        printf("Image length too long!\n");
246243        return -1;
247244    }
248245
...... 
252249/* usb_ingenic_sdram_ops(dev, sdram_in);*/
253250
254251    usb_read_data_from_ingenic(dev, ret, 8);
255    printf(" Load last address at 0x%x\n",
252    printf("Load last address at 0x%x\n",
256253           ((ret[3]<<24)|(ret[2]<<16)|(ret[1]<<8)|(ret[0]<<0)));
257254
258255    return 1;
...... 
283280    j = flen % MAX_LOAD_SIZE;
284281    offset = 0;
285282
286    printf(" Total size to send in byte is :%d\n", flen);
287    printf(" Loading data to SDRAM :\n");
283    printf("Total size to send in byte is :%d\n", flen);
284    printf("Loading data to SDRAM :\n");
288285
289286    for (k = 0; k < m; k++) {
290287        status = read(fd, sdram_in->buf, MAX_LOAD_SIZE);
usbboot/src/command_line.c
2020#include <stdlib.h>
2121#include <string.h>
2222#include <ctype.h>
23#include <fcntl.h>
24#include <unistd.h>
25
2326#include "usb_boot_defines.h"
2427#include "ingenic_usb.h"
2528#include "cmd.h"
...... 
323326    return argc;
324327}
325328
329static int nand_read_callback(void *data, const char *buf, size_t size)
330{
331    return 0;
332}
333
326334static int handle_nand_read(size_t argc, char *argv[])
327335{
328336    int mode;
...... 
354362    if (err)
355363        return err;
356364
357    return nand_read(&ingenic_dev, nand_idx, mode, start_page, num_pages, 0);
365    return nand_read(&ingenic_dev, nand_idx, mode, start_page, num_pages, 0,
366                    nand_read_callback, NULL);
367}
368
369static int nand_dump_callback(void *data, const char *buf, size_t size)
370{
371    int fd = *(int*)(data);
372
373    write(fd, buf, size);
374
375    return 0;
358376}
359377
360378static int handle_nand_dump(size_t argc, char *argv[])
361379{
362380    int mode;
363381    uint32_t start_page, num_pages;
364    unsigned int device_idx;
365382    uint8_t nand_idx;
366383    int err = 0;
384    int fd;
367385
368386    if (argc != 5) {
369387        printf("Usage: %s <start page> <length> <filename> <mode>\n", argv[0]);
370388        return -1;
371389    }
372390
373    if (strcmp(argv[5], "-n") == 0) {
391    if (strcmp(argv[4], "-n") == 0) {
374392        mode = NAND_READ;
375    } else if (strcmp(argv[5], "-e") == 0) {
393    } else if (strcmp(argv[4], "-e") == 0) {
376394        mode = NAND_READ_RAW;
377    } else if (strcmp(argv[5], "-o") == 0) {
395    } else if (strcmp(argv[4], "-o") == 0) {
378396        mode = NAND_READ_OOB;
379397    } else {
380398        return -1;
...... 
382400
383401    start_page = parse_number_print_error(argv[1], &err);
384402    num_pages = parse_number_print_error(argv[2], &err);
385    device_idx = parse_number_print_error(argv[3], &err);
386    nand_idx = parse_number_print_error(argv[4], &err);
403/* device_idx = parse_number_print_error(argv[3], &err);*/
404    nand_idx = 0;
387405    if (err)
388406        return err;
389407
390    return nand_read(&ingenic_dev, nand_idx, mode, start_page, num_pages, 0);
408    fd = creat(argv[3], 0644);
409
410    err = nand_read(&ingenic_dev, nand_idx, mode, start_page, num_pages, 0,
411                    nand_dump_callback, &fd);
391412
413    close(fd);
414
415    return err;
392416}
393417
394418static int handle_nand_query(size_t argc, char *argv[])
...... 
417441    uint8_t nand_idx, mode = -1;
418442    int err = 0;
419443
420    if (argc != 5) {
444    if (argc != 6) {
421445        printf("Usage: %s <start page> <filename> <device index> <nand chip index> <mode>\n", argv[0]);
422446        return -1;
423447    }
usbboot/src/ingenic_cfg.c
213213    if (check_dump_cfg(hand) < 1)
214214        return -1;
215215
216    return 1;
216    return 0;
217217}
usbboot/src/ingenic_usb.c
4141                (usb_dev->descriptor.idProduct == PRODUCT_ID)) {
4242                ingenic_dev->usb_dev = usb_dev;
4343                count++;
44                break;
44                break;
4545            }
4646
4747        }
...... 
6363        usb_config_desc = &ingenic_dev->usb_dev->config[config_index];
6464
6565        if (!usb_config_desc)
66            return 0;
66            return -1;
6767
6868        for (if_index = 0; if_index < usb_config_desc->bNumInterfaces;
6969             if_index++) {
7070            usb_if = &usb_config_desc->interface[if_index];
7171
7272            if (!usb_if)
73                return 0;
73                return -1;
7474
7575            for (alt_index = 0; alt_index < usb_if->num_altsetting;
7676                 alt_index++) {
7777                usb_if_desc = &usb_if->altsetting[alt_index];
7878
7979                if (!usb_if_desc)
80                    return 0;
80                    return -1;
8181
8282                if ((usb_if_desc->bInterfaceClass == 0xff) &&
8383                    (usb_if_desc->bInterfaceSubClass == 0)) {
8484                    ingenic_dev->interface =
8585                        usb_if_desc->bInterfaceNumber;
86                    return 1;
86                    return 0;
8787                }
8888            }
8989        }
...... 
9494
9595int usb_ingenic_init(struct ingenic_dev *ingenic_dev)
9696{
97    int num_ingenic, status = -1;
97    int num_ingenic;
98    int ret = -1;
9899
99100    memset(ingenic_dev, 0, sizeof(struct ingenic_dev));
100101
...... 
107108
108109    if (num_ingenic == 0) {
109110        fprintf(stderr, "Error - no XBurst device found\n");
110        goto out;
111        goto err;
111112    }
112113
113114    if (num_ingenic > 1) {
114        fprintf(stderr, "Error - too many XBurst devices found: %i\n",
115        fprintf(stderr, "Error - too many XBurst devices found: %d\n",
115116            num_ingenic);
116        goto out;
117        goto err;
117118    }
118119
119120    ingenic_dev->usb_handle = usb_open(ingenic_dev->usb_dev);
120121    if (!ingenic_dev->usb_handle) {
121122        fprintf(stderr, "Error - can't open XBurst device: %s\n",
122123            usb_strerror());
123        goto out;
124        goto err;
124125    }
125126
126    if (get_ingenic_interface(ingenic_dev) < 1) {
127    ret = get_ingenic_interface(ingenic_dev);
128    if (ret < 0) {
127129        fprintf(stderr, "Error - can't find XBurst interface\n");
128        goto out;
130        goto err;
129131    }
130132
131    if (usb_claim_interface(ingenic_dev->usb_handle, ingenic_dev->interface)
132        < 0) {
133    ret = usb_claim_interface(ingenic_dev->usb_handle, ingenic_dev->interface);
134    if (ret < 0) {
133135        fprintf(stderr, "Error - can't claim XBurst interface: %s\n",
134136            usb_strerror());
135        goto out;
137        goto err;
136138    }
137139
138    status = 1;
139
140out:
141    return status;
140    return 0;
141err:
142    return ret;
142143}
143144
144145int usb_get_ingenic_cpu(struct ingenic_dev *ingenic_dev)
145146{
146    int status;
147
148    memset(&ingenic_dev->cpu_info_buff, 0,
149           ARRAY_SIZE(ingenic_dev->cpu_info_buff));
147    int ret;
148    char buf[9];
150149
151150    sleep(1);
152    status = usb_control_msg(ingenic_dev->usb_handle,
153          /* bmRequestType */ USB_ENDPOINT_IN | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
154          /* bRequest */ VR_GET_CPU_INFO,
155          /* wValue */ 0,
156          /* wIndex */ 0,
157          /* Data */ ingenic_dev->cpu_info_buff,
158          /* wLength */ 8,
159                              USB_TIMEOUT);
160
161    if (status != sizeof(ingenic_dev->cpu_info_buff) - 1 ) {
151    ret = usb_control_msg(ingenic_dev->usb_handle,
152      /* bmRequestType */ USB_ENDPOINT_IN | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
153      /* bRequest */ VR_GET_CPU_INFO,
154      /* wValue */ 0,
155      /* wIndex */ 0,
156      /* Data */ buf,
157      /* wLength */ 8,
158                  USB_TIMEOUT);
159
160    if (ret != 8) {
162161        fprintf(stderr, "Error - "
163            "can't retrieve XBurst CPU information: %i\n", status);
164        return status;
162            "can't retrieve XBurst CPU information: %d\n", ret);
163        return ret;
165164    }
166165
167    ingenic_dev->cpu_info_buff[8] = '\0';
168    printf(" CPU data: %s\n", ingenic_dev->cpu_info_buff);
166    buf[8] = '\0';
167    printf(" CPU data: %s\n", buf);
169168
170    if (!strcmp(ingenic_dev->cpu_info_buff,"JZ4740V1")) return 1;
171    if (!strcmp(ingenic_dev->cpu_info_buff,"JZ4750V1")) return 2;
172    if (!strcmp(ingenic_dev->cpu_info_buff,"Boot4740")) return 3;
173    if (!strcmp(ingenic_dev->cpu_info_buff,"Boot4750")) return 4;
169    if (!strcmp(buf, "JZ4740V1"))
170        return INGENIC_CPU_JZ4740V1;
171    if (!strcmp(buf, "JZ4750V1"))
172        return INGENIC_CPU_JZ4750V1;
173    if (!strcmp(buf, "Boot4740"))
174        return INGENIC_CPU_JZ4740;
175    if (!strcmp(buf, "Boot4750"))
176        return INGENIC_CPU_JZ4750;
174177
175    return 0;
178    return INGENIC_CPU_UNKOWN;
176179}
177180
178int usb_ingenic_flush_cache(struct ingenic_dev *ingenic_dev)
181int usb_send_data_to_ingenic(struct ingenic_dev *ingenic_dev, const char *data,
182                             int size)
179183{
180    int status;
181
182    status = usb_control_msg(ingenic_dev->usb_handle,
183          /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
184          /* bRequest */ VR_FLUSH_CACHES,
185          /* wValue */ 0,
186          /* wIndex */ 0,
187          /* Data */ 0,
188          /* wLength */ 0,
189                              USB_TIMEOUT);
190
191    if (status != 0) {
192        fprintf(stderr, "Error - can't flush cache: %i\n", status);
193        return status;
184    int ret;
185    ret = usb_bulk_write(ingenic_dev->usb_handle,
186    /* endpoint */ INGENIC_OUT_ENDPOINT,
187    /* bulk data */ data,
188    /* bulk data length */ size,
189                USB_TIMEOUT);
190
191    if (ret < 0) {
192        fprintf(stderr, "Error - "
193            "Can't send bulk data to Ingenic CPU: %d\n", ret);
194        return ret;
194195    }
195196
196    return 1;
197    return size;
197198}
198199
199int usb_send_data_length_to_ingenic(struct ingenic_dev *ingenic_dev, unsigned int len)
200int usb_read_data_from_ingenic(struct ingenic_dev *ingenic_dev,
201                   char *data, int size)
200202{
201    int status;
203    int ret;
204    ret = usb_bulk_read(ingenic_dev->usb_handle,
205    /* endpoint */ INGENIC_IN_ENDPOINT,
206    /* bulk data */ data,
207    /* bulk data length */ size,
208                USB_TIMEOUT);
202209
203    /* tell the device the length of the file to be uploaded */
204    status = usb_control_msg(ingenic_dev->usb_handle,
205          /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
206          /* bRequest */ VR_SET_DATA_LENGTH,
207          /* wValue */ STAGE_ADDR_MSB(len),
208          /* wIndex */ STAGE_ADDR_LSB(len),
209          /* Data */ 0,
210          /* wLength */ 0,
211                              USB_TIMEOUT);
212
213    if (status != 0) {
210    if (ret < 0) {
214211        fprintf(stderr, "Error - "
215            "can't set data length on Ingenic device: %i\n", status);
216        return -1;
212            "Can't read bulk data from Ingenic device: %d\n", ret);
213        return ret;
217214    }
218215
219    return 1;
216    return size;
220217}
221218
222int usb_send_data_address_to_ingenic(struct ingenic_dev *ingenic_dev, uint32_t addr)
219int usb_ingenic_start(struct ingenic_dev *ingenic_dev, int rqst, int stage_addr)
223220{
224    int status;
225    /* tell the device the RAM address to store the file */
226    status = usb_control_msg(ingenic_dev->usb_handle,
227          /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
228          /* bRequest */ VR_SET_DATA_ADDRESS,
229          /* wValue */ STAGE_ADDR_MSB(addr),
230          /* wIndex */ STAGE_ADDR_LSB(addr),
231          /* Data */ 0,
232          /* wLength */ 0,
233                              USB_TIMEOUT);
234
235    if (status != 0) {
236        fprintf(stderr, "Error - "
237            "can't set the address on Ingenic device: %i\n", status);
238        return -1;
239    }
221    int ret;
240222
241    return 1;
242}
223    /* tell the device to start the uploaded device */
224    ret = usb_control_msg(ingenic_dev->usb_handle,
225      /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
226      /* bRequest */ rqst,
227      /* wValue */ STAGE_ADDR_MSB(stage_addr),
228      /* wIndex */ STAGE_ADDR_LSB(stage_addr),
229      /* Data */ 0,
230      /* wLength */ 0,
231                          USB_TIMEOUT);
243232
244int usb_send_data_to_ingenic(struct ingenic_dev *ingenic_dev, const char *data,
245                             int size)
246{
247    int status;
248    status = usb_bulk_write(ingenic_dev->usb_handle,
249    /* endpoint */ INGENIC_OUT_ENDPOINT,
250    /* bulk data */ data,
251    /* bulk data length */ size,
252                USB_TIMEOUT);
253    if (status < size) {
254        fprintf(stderr, "Error - "
255            "can't send bulk data to Ingenic CPU: %i\n", status);
256        return -1;
233    if (ret != 0) {
234        fprintf(stderr, "Error - can't start the uploaded binary "
235            "on the Ingenic device: %d\n", ret);
257236    }
258
259    return 1;
237    return ret;
260238}
261239
262int usb_read_data_from_ingenic(struct ingenic_dev *ingenic_dev,
263                   char *data, int size)
240int usb_send_data_length_to_ingenic(struct ingenic_dev *ingenic_dev, unsigned int len)
264241{
265    int status;
266    status = usb_bulk_read(ingenic_dev->usb_handle,
267        /* endpoint */ INGENIC_IN_ENDPOINT,
268    /* bulk data */ data,
269    /* bulk data length */ size,
270                USB_TIMEOUT);
271    if (status < size) {
242    int ret;
243
244    /* tell the device the length of the file to be uploaded */
245    ret = usb_control_msg(ingenic_dev->usb_handle,
246      /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
247      /* bRequest */ VR_SET_DATA_LENGTH,
248      /* wValue */ STAGE_ADDR_MSB(len),
249      /* wIndex */ STAGE_ADDR_LSB(len),
250      /* Data */ 0,
251      /* wLength */ 0,
252                  USB_TIMEOUT);
253
254    if (ret != 0) {
272255        fprintf(stderr, "Error - "
273            "can't read bulk data from Ingenic device:%i\n", status);
274        return -1;
256            "can't set data length on Ingenic device: %d\n", ret);
275257    }
276258
277    return 1;
259    return ret;
278260}
279261
280int usb_ingenic_start(struct ingenic_dev *ingenic_dev, int rqst, int stage_addr)
262int usb_send_data_address_to_ingenic(struct ingenic_dev *ingenic_dev, uint32_t addr)
281263{
282    int status;
264    int ret;
283265
284    /* tell the device to start the uploaded device */
285    status = usb_control_msg(ingenic_dev->usb_handle,
286          /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
287      /* bRequest */ rqst,
288          /* wValue */ STAGE_ADDR_MSB(stage_addr),
289          /* wIndex */ STAGE_ADDR_LSB(stage_addr),
290          /* Data */ 0,
291          /* wLength */ 0,
292                              USB_TIMEOUT);
293
294    if (status != 0) {
295        fprintf(stderr, "Error - can't start the uploaded binary "
296            "on the Ingenic device: %i\n", status);
297        return status;
266    /* tell the device the RAM address to store the file */
267    ret = usb_control_msg(ingenic_dev->usb_handle,
268      /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
269      /* bRequest */ VR_SET_DATA_ADDRESS,
270      /* wValue */ STAGE_ADDR_MSB(addr),
271      /* wIndex */ STAGE_ADDR_LSB(addr),
272      /* Data */ 0,
273      /* wLength */ 0,
274                  USB_TIMEOUT);
275
276    if (ret != 0) {
277        fprintf(stderr, "Error - "
278            "Can't set the address on Ingenic device: %d\n", ret);
298279    }
299    return 1;
280
281    return ret;
300282}
301283
302int usb_ingenic_upload(struct ingenic_dev *ingenic_dev, int stage)
284
285int usb_ingenic_upload(struct ingenic_dev *ingenic_dev, int stage,
286                       const char *buf, int size)
303287{
304    unsigned int stage2_addr;
305    stage2_addr = total_size + 0x80000000;
306    stage2_addr -= CODE_SIZE;
288    uint32_t stage_addr;
289    int request;
290    int ret;
291
292    if (stage == 1) {
293        stage_addr = 0x80002000;
294        request = VR_PROGRAM_START1;
295    } else {
296        stage_addr = 0x80000000 + total_size - CODE_SIZE;
297        request = VR_PROGRAM_START2;
298    }
307299
308    int stage_addr = (stage == 1 ? 0x80002000 : stage2_addr);
309    int rqst = VR_PROGRAM_START1;
310300
311301    usb_send_data_address_to_ingenic(ingenic_dev, stage_addr);
312    printf(" Download stage %d program and execute at 0x%08x\n",
313           stage, (stage_addr));
314    usb_send_data_to_ingenic(ingenic_dev, ingenic_dev->file_buff,
315    ingenic_dev->file_len);
302    printf("Download stage %d program and execute at 0x%08x\n",
303           stage, stage_addr);
304    usb_send_data_to_ingenic(ingenic_dev, buf, size);
316305
317306    if (stage == 2) {
318        if (usb_get_ingenic_cpu(ingenic_dev) < 1)
319            return -1;
307        ret = usb_get_ingenic_cpu(ingenic_dev);
308        if (ret < 0)
309            return ret;
320310        usb_ingenic_flush_cache(ingenic_dev);
321        rqst = VR_PROGRAM_START2;
322311    }
323312
324    if (usb_ingenic_start(ingenic_dev, rqst, stage_addr) < 1)
325        return -1;
326    if (usb_get_ingenic_cpu(ingenic_dev) < 1)
327        return -1;
313    ret = usb_ingenic_start(ingenic_dev, request, stage_addr);
314    if (ret)
315        return ret;
316    ret = usb_get_ingenic_cpu(ingenic_dev);
317    if (ret)
318        return ret;
328319
329    return 1;
320    return 0;
330321}
331322
332323void usb_ingenic_cleanup(struct ingenic_dev *ingenic_dev)
333324{
334    if ((ingenic_dev->usb_handle) && (ingenic_dev->interface))
335        usb_release_interface(ingenic_dev->usb_handle,
336                      ingenic_dev->interface);
325    if (ingenic_dev->usb_handle) {
326        if (ingenic_dev->interface) {
327            usb_release_interface(ingenic_dev->usb_handle,
328                                  ingenic_dev->interface);
329        }
337330
338    if (ingenic_dev->usb_handle)
339331        usb_close(ingenic_dev->usb_handle);
332    }
333
340334}
341335
336static int usb_ingenic_ops(struct ingenic_dev *ingenic_dev, uint32_t type,
337                           uint32_t ops)
338{
339    int ret;
340    ret = usb_control_msg(ingenic_dev->usb_handle,
341      /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
342      /* bRequest */ type,
343      /* wValue */ ops,
344      /* wIndex */ 0,
345      /* Data */ 0,
346      /* wLength */ 0,
347                  USB_TIMEOUT);
348
349    return ret;
350}
351
352int usb_ingenic_flush_cache(struct ingenic_dev *ingenic_dev)
353{
354    int ret;
355    ret = usb_ingenic_ops(ingenic_dev, VR_FLUSH_CACHES, 0);
356
357    if (ret != 0) {
358        fprintf(stderr, "Error - can't flush cache: %d\n", ret);
359    }
360
361    return ret;
362}
363
364
342365int usb_ingenic_nand_ops(struct ingenic_dev *ingenic_dev, int ops)
343366{
344    int status;
345    status = usb_control_msg(ingenic_dev->usb_handle,
346          /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
347          /* bRequest */ VR_NAND_OPS,
348          /* wValue */ ops & 0xffff,
349          /* wIndex */ 0,
350          /* Data */ 0,
351          /* wLength */ 0,
352                              USB_TIMEOUT);
353
354    if (status != 0) {
367    int ret;
368    ret = usb_ingenic_ops(ingenic_dev, VR_NAND_OPS, ops);
369
370    if (ret != 0) {
355371        fprintf(stderr, "Error - "
356            "can't set Ingenic device nand ops: %i\n", status);
357        return -1;
372            "can't set Ingenic device nand ops: %d\n", ret);
358373    }
359374
360    return 1;
375    return ret;
361376}
362377
363378int usb_ingenic_mem_ops(struct ingenic_dev *ingenic_dev, int ops)
364379{
365    int status;
366    status = usb_control_msg(ingenic_dev->usb_handle,
367          /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
368          /* bRequest */ VR_MEM_OPS,
369          /* wValue */ ops & 0xffff,
370          /* wIndex */ 0,
371          /* Data */ 0,
372          /* wLength */ 0,
373                              USB_TIMEOUT);
374
375    if (status != 0) {
380    int ret;
381    ret = usb_ingenic_ops(ingenic_dev, VR_MEM_OPS, ops);
382
383    if (ret != 0) {
376384        fprintf(stderr, "Error - "
377            "can't set Ingenic device nand ops: %i\n", status);
378        return -1;
385            "can't set Ingenic device nand ops: %d\n", ret);
379386    }
380387
381    return 1;
388    return ret;
382389}
383390
384391
385392int usb_ingenic_configration(struct ingenic_dev *ingenic_dev, int ops)
386393{
387    int status;
388    status = usb_control_msg(ingenic_dev->usb_handle,
389          /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
390          /* bRequest */ VR_CONFIGRATION,
391          /* wValue */ ops,
392          /* wIndex */ 0,
393          /* Data */ 0,
394          /* wLength */ 0,
395                              USB_TIMEOUT);
396
397    if (status != 0) {
394    int ret;
395    ret = usb_ingenic_ops(ingenic_dev, VR_CONFIGURATION, ops);
396
397    if (ret != 0) {
398398        fprintf(stderr, "Error - "
399            "can't init Ingenic configration: %i\n", status);
400        return -1;
399            "can't init Ingenic configration: %d\n", ret);
401400    }
402401
403    return 1;
402    return ret;
404403}
405404
406405int usb_ingenic_sdram_ops(struct ingenic_dev *ingenic_dev, int ops)
407406{
408    int status;
409    status = usb_control_msg(ingenic_dev->usb_handle,
410          /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
411          /* bRequest */ VR_SDRAM_OPS,
412          /* wValue */ ops,
413          /* wIndex */ 0,
414          /* Data */ 0,
415          /* wLength */ 0,
416                              USB_TIMEOUT);
417
418    if (status != 0) {
407    int ret;
408
409    ret = usb_ingenic_ops(ingenic_dev, VR_SDRAM_OPS, ops);
410
411    if (ret != 0) {
419412        fprintf(stderr, "Error - "
420            "Device can't load file to sdram: %i\n", status);
421        return -1;
413            "Device can't load file to sdram: %d\n", ret);
422414    }
423415
424    return 1;
416    return ret;
425417}
usbboot/src/ingenic_usb.h
2121#define __INGENIC_USB_H__
2222
2323#include <stdint.h>
24#include "usb_boot_defines.h"
2425
2526#define INGENIC_OUT_ENDPOINT 0x01
2627#define INGENIC_IN_ENDPOINT 0x81
...... 
3435#define VR_NOR_OPS 0x06
3536#define VR_NAND_OPS 0x07
3637#define VR_SDRAM_OPS 0x08
37#define VR_CONFIGRATION 0x09
38#define VR_CONFIGURATION 0x09
3839#define VR_MEM_OPS 0x0a
3940#define VR_GET_NUM 0x0b
4041
...... 
5657    struct usb_device *usb_dev;
5758    struct usb_dev_handle *usb_handle;
5859    uint8_t interface;
59    char cpu_info_buff[9];
60    char *file_buff;
61    int file_len;
60
61    struct hand config;
62};
63
64enum ingenic_cpu_type {
65    INGENIC_CPU_UNKOWN,
66    INGENIC_CPU_JZ4740V1,
67    INGENIC_CPU_JZ4750V1,
68    INGENIC_CPU_JZ4740,
69    INGENIC_CPU_JZ4750,
6270};
6371
6472int usb_ingenic_init(struct ingenic_dev *ingenic_dev);
6573int usb_get_ingenic_cpu(struct ingenic_dev *ingenic_dev);
66int usb_ingenic_upload(struct ingenic_dev *ingenic_dev, int stage);
74int usb_ingenic_upload(struct ingenic_dev *ingenic_dev, int stage, const char *buf, int size);
6775void usb_ingenic_cleanup(struct ingenic_dev *ingenic_dev);
6876int usb_ingenic_nand_ops(struct ingenic_dev *ingenic_dev, int ops);
6977int usb_ingenic_mem_ops(struct ingenic_dev *ingenic_dev, int ops);
...... 
7886int usb_ingenic_start(struct ingenic_dev *ingenic_dev, int rqst, int stage_addr);
7987int usb_ingenic_sdram_ops(struct ingenic_dev *ingenic_dev, int ops);
8088int usb_ingenic_configration(struct ingenic_dev *ingenic_dev, int ops);
89int usb_ingenic_flush_cache(struct ingenic_dev *ingenic_dev);
8190
8291#endif /* __INGENIC_USB_H__ */
8392
usbboot/src/main.c
2929#define CONFIG_FILE_PATH "/etc/xburst-tools/usbboot.cfg"
3030
3131struct ingenic_dev ingenic_dev;
32extern struct hand hand;
3332
3433static void help(void)
3534{
...... 
9897        return EXIT_FAILURE;
9998    }*/
10099
101    if (usb_ingenic_init(&ingenic_dev) < 1)
100    if (usb_ingenic_init(&ingenic_dev)) {
101        printf("A\n");
102102         return EXIT_FAILURE;
103    }
103104
104    if (parse_configure(&hand, cfgpath) < 1)
105    if (parse_configure(&ingenic_dev.config, cfgpath)) {
106        printf("B\n");
105107        return EXIT_FAILURE;
108    }
106109
107110    if (cmdpt) { /* direct run command */
108111        char *delim=";";
usbboot/src/nand.c
1010#include "ingenic_usb.h"
1111#include "usb_boot_defines.h"
1212
13extern struct hand hand;
14
1513#define NAND_OP(idx, op, mode) (((mode << 12) & 0xf000) | ((idx << 4) & 0xff0) | op)
1614
1715#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
...... 
2220    "without oob",
2321};
2422
25static int error_check(const char *org, const char *obj, unsigned int size)
23static int error_check(const char *a, const char *b, unsigned int size)
2624{
2725    unsigned int i;
26    const unsigned char *org = (const unsigned char *)a;
27    const unsigned char *obj= (const unsigned char *)b;
28
29
2830    printf("Comparing %d bytes - ", size);
2931    for (i = 0; i < size; i++) {
3032        if (org[i] != obj[i]) {
...... 
6567    (void)nand_idx;
6668
6769    if (usb_get_ingenic_cpu(dev) < 3) {
68        printf("Device unboot! Boot it first!\n");
69        return -1;
70        fprintf(stderr, "Device unboot! Boot it first!\n");
71        return -ENODEV;
7072    }
7173    printf("Mark bad block : %d\n", block);
7274    usb_send_data_address_to_ingenic(dev, block);
...... 
7577    printf("Mark bad block at %d\n",((ret[3] << 24) |
7678                       (ret[2] << 16) |
7779                       (ret[1] << 8) |
78                       (ret[0] << 0)) / hand.nand_ppb);
80                       (ret[0] << 0)) / dev->config.nand_ppb);
7981
8082    return 0;
8183}
...... 
9193
9294    printf("Writing NAND page %u len %u...\n", start_page, length);
9395    if (length > (unsigned int)MAX_TRANSFER_SIZE) {
94        printf("Buffer size too long!\n");
95        return -ENOMEM;
96        fprintf(stderr, "Buffer size too long!\n");
97        return -EINVAL;
9698    }
9799
98100    if (usb_get_ingenic_cpu(dev) < 3) {
99        printf("Device unboot! Boot it first!\n");
101        fprintf(stderr, "Device unboot! Boot it first!\n");
100102        return -ENODEV;
101103    }
102104    usb_send_data_to_ingenic(dev, data, length);
103105
104106    if (mode == NO_OOB)
105        page_num = DIV_ROUND_UP(length, hand.nand_ps);
107        page_num = DIV_ROUND_UP(length, dev->config.nand_ps);
106108    else
107        page_num = DIV_ROUND_UP(length, hand.nand_ps + hand.nand_os);
109        page_num = DIV_ROUND_UP(length, dev->config.nand_ps + dev->config.nand_os);
108110
109111    op = NAND_OP(nand_idx, NAND_PROGRAM, mode);
110112
...... 
117119               length, start_page, page_num);
118120
119121    switch(mode) {
120    case NAND_READ:
122    case NO_OOB:
121123        op = NAND_OP(nand_idx, NAND_READ, NO_OOB);
122124        break;
123    case NAND_READ_OOB:
124        op = NAND_OP(nand_idx, NAND_READ_OOB, 0);
125        break;
126    case NAND_READ_RAW:
127        op = NAND_OP(nand_idx, NAND_READ_RAW, NO_OOB);
125    default:
126        op = NAND_OP(nand_idx, NAND_READ_RAW, OOB_ECC);
128127        break;
129128    }
130129
...... 
135134        (ret[0] << 0);
136135
137136    if (start_page < 1 &&
138        hand.nand_ps == 4096 &&
139        hand.fw_args.cpu_id == 0x4740) {
137        dev->config.nand_ps == 4096 &&
138        dev->config.fw_args.cpu_id == 0x4740) {
140139        printf("no check! End at Page: %d\n", cur_page);
141140    }
142141
143142    if (!error_check(data, read_back_buf, length)) {
144143        // tbd: doesn't the other side skip bad blocks too? Can we just deduct 1 from cur_page?
145144        // tbd: why do we only mark a block as bad if the last page in the block was written?
146        if (cur_page % hand.nand_ppb == 0)
147            nand_markbad(dev, nand_idx, (cur_page - 1) / hand.nand_ppb);
145        if (cur_page % dev->config.nand_ppb == 0)
146            nand_markbad(dev, nand_idx, (cur_page - 1) / dev->config.nand_ppb);
148147    }
149148
150149    printf("End at Page: %d\n", cur_page);
...... 
161160    static char ret[8];
162161
163162    if (start_block > (unsigned int)NAND_MAX_BLK_NUM) {
164        printf("Start block number overflow!\n");
165        return -1;
163        fprintf(stderr, "Start block number overflow!\n");
164        return -EINVAL;
166165    }
167166    if (num_blocks > (unsigned int)NAND_MAX_BLK_NUM) {
168        printf("Length block number overflow!\n");
169        return -1;
167        fprintf(stderr, "Length block number overflow!\n");
168        return -EINVAL;
170169    }
171170
172171    if (usb_get_ingenic_cpu(dev) < 3) {
173        printf("Device unboot! Boot it first!\n");
174        return -1;
172        fprintf(stderr, "Device unboot! Boot it first!\n");
173        return -ENODEV;
175174    }
176175
177176    printf("Erasing No.%d device No.%d flash (start_blk %u blk_num %u)......\n",
...... 
187186    printf("Finish!");
188187
189188    end_block = ((ret[3] << 24) | (ret[2] << 16) |
190             (ret[1] << 8) | (ret[0] << 0)) / hand.nand_ppb;
189             (ret[1] << 8) | (ret[0] << 0)) / dev->config.nand_ppb;
191190    printf("Return: %02x %02x %02x %02x %02x %02x %02x %02x (position %d)\n",
192191           ret[0], ret[1], ret[2], ret[3], ret[4], ret[5], ret[6], ret[7], end_block);
193    if (!hand.nand_force_erase) {
192    if (!dev->config.nand_force_erase) {
194193    /* not force erase, show bad block infomation */
195194        printf("There are marked bad blocks: %d\n",
196195               end_block - start_block - num_blocks );
...... 
217216    if (status < 0) {
218217        fprintf(stderr, "Error - can't get file size from '%s': %s\n",
219218            filename, strerror(errno));
220        return -1;
219        return -EEXIST;
221220    }
222221    flen = fstat.st_size;
223222
...... 
225224    if (fd < 0) {
226225        fprintf(stderr, "Error - can't open file '%s': %s\n",
227226            filename, strerror(errno));
228        return -1;
227        return errno;
229228    }
230229
231230    printf("Programing No.%d device, flen %d, start page %d...\n", 0,
...... 
233232
234233    /* printf("length %d flen %d\n", n_in.length, flen); */
235234    if (mode == NO_OOB)
236        transfer_size = (hand.nand_ppb * hand.nand_ps);
235        transfer_size = (dev->config.nand_ppb * dev->config.nand_ps);
237236    else
238        transfer_size = (hand.nand_ppb * (hand.nand_ps + hand.nand_os));
237        transfer_size = (dev->config.nand_ppb * (dev->config.nand_ps +
238        dev->config.nand_os));
239239
240    start_block = start_page / hand.nand_ppb;
240    start_block = start_page / dev->config.nand_ppb;
241241    num_blocks = flen / (transfer_size - 1) + 1;
242242
243243    if (nand_erase(dev, nand_idx, start_block, num_blocks))
...... 
247247    j = flen % transfer_size;
248248
249249    printf("Size to send %d, transfer_size %d\n", flen, transfer_size);
250    printf("Image type : %s\n", IMAGE_TYPE[mode]);
250/* printf("Image type : %s\n", IMAGE_TYPE[mode]);*/
251251    printf("It will cause %d times buffer transfer.\n", j == 0 ? m : m + 1);
252252
253253    if (mode == NO_OOB)
254        page_num = transfer_size / hand.nand_ps;
254        page_num = transfer_size / dev->config.nand_ps;
255255    else
256        page_num = transfer_size / (hand.nand_ps + hand.nand_os);
256        page_num = transfer_size / (dev->config.nand_ps + dev->config.nand_os);
257257
258258
259259    offset = 0;
...... 
269269        if (nand_program_check(dev, nand_idx, start_page, code_buf, code_len, mode) == -1)
270270            goto close;
271271
272/* if (start_page - nand_in->start > hand.nand_ppb)
272/* if (start_page - nand_in->start > dev->config.nand_ppb)
273273            printf("Skip a old bad block !\n");*/
274274
275275        offset += code_len ;
...... 
277277
278278    if (j) {
279279        code_len = j;
280        if (j % hand.nand_ps)
281            j += hand.nand_ps - (j % hand.nand_ps);
280        if (j % dev->config.nand_ps)
281            j += dev->config.nand_ps - (j % dev->config.nand_ps);
282282        memset(code_buf, 0, j); /* set all to null */
283283
284284        status = read(fd, code_buf, code_len);
...... 
293293            goto close;
294294
295295/*
296        if (start_page - nand_in->start > hand.nand_ppb)
296        if (start_page - nand_in->start > dev->config.nand_ppb)
297297            printf("Skip a old bad block !");
298298*/
299299    }
...... 
305305int nand_prog(struct ingenic_dev *dev, uint8_t nand_idx, uint32_t start_page,
306306            const char *filename, int mode)
307307{
308    if (hand.nand_plane > 1)
308    if (dev->config.nand_plane > 1)
309309        printf("ERROR");
310310    else
311311        nand_program_file(dev, nand_idx, start_page, filename, mode);
...... 
316316int nand_query(struct ingenic_dev *dev, uint8_t nand_idx)
317317{
318318    uint16_t op;
319    char ret[8];
319    char ret[8] = {0,0,0,0,0,0,0,0};
320    int ret2;
320321
321322    if (usb_get_ingenic_cpu(dev) < 3) {
322        printf("Device unboot! Boot it first!\n");
323        return -1;
323        fprintf(stderr, "Device unboot! Boot it first!\n");
324        return -ENODEV;
324325    }
325326
326327    printf("ID of No.%u device No.%u flash: \n", 0, nand_idx);
...... 
328329    op = NAND_OP(nand_idx, NAND_QUERY, 0);
329330
330331    usb_ingenic_nand_ops(dev, op);
331    usb_read_data_from_ingenic(dev, ret, ARRAY_SIZE(ret));
332    ret2 = usb_read_data_from_ingenic(dev, ret, ARRAY_SIZE(ret));
333
334    if (ret2 < 0)
335        return ret2;
336
332337    printf("Vendor ID :0x%x \n", (unsigned char)ret[0]);
333338    printf("Product ID :0x%x \n", (unsigned char)ret[1]);
334339    printf("Chip ID :0x%x \n", (unsigned char)ret[2]);
...... 
342347}
343348
344349int nand_read(struct ingenic_dev *dev, uint8_t nand_idx, int mode,
345                uint32_t start_page, uint32_t length, uint32_t ram_addr)
350                uint32_t start_page, uint32_t length, uint32_t ram_addr,
351                nand_read_cb_t callback, void *userdata)
346352{
347353    uint16_t op;
348354    uint32_t page;
...... 
350356    uint32_t pages_per_request;
351357    char ret[8];
352358    char *buf;
353    int fd;
359    int ret2;
354360
355361    if (start_page > NAND_MAX_PAGE_NUM) {
356        printf("Page number overflow!\n");
357        return -1;
362        fprintf(stderr, "Page number overflow!\n");
363        return -EINVAL;
358364    }
359365    if (usb_get_ingenic_cpu(dev) < 3) {
360        printf("Device unboot! Boot it first!\n");
361        return -1;
366        fprintf(stderr, "Device unboot! Boot it first!\n");
367        return -EINVAL;
362368    }
363369    if (nand_idx >= 16)
364        return -1;
370        return -EINVAL;
365371
366372    printf("Reading from No.%u device No.%u flash....\n", 0, nand_idx);
367373
...... 
374380        op = NAND_OP(nand_idx, NAND_READ_OOB, 0);
375381        break;
376382    case NAND_READ_RAW:
377        op = NAND_OP(nand_idx, NAND_READ_RAW, NO_OOB);
383        op = NAND_OP(nand_idx, NAND_READ_RAW, OOB_ECC);
378384        break;
379385    case NAND_READ_TO_RAM:
380386        op = NAND_OP(nand_idx, NAND_READ_TO_RAM, NO_OOB);
381        printf("Reading nand to RAM: 0x%x\n", ram_addr);
382387        usb_ingenic_start(dev, VR_PROGRAM_START1, ram_addr);
383388        break;
384389    default:
385        printf("unknow mode!\n");
386        return -1;
390        return -EINVAL;
387391    }
388392
389393    pages_per_request = 1;
390    request_length = hand.nand_ps * pages_per_request;
394    if (mode == NAND_READ_OOB || mode == NAND_READ_RAW)
395        request_length = (dev->config.nand_ps + dev->config.nand_os) * pages_per_request;
396    else
397        request_length = dev->config.nand_ps * pages_per_request;
391398
392399    page = start_page;
393400
394401    buf = malloc(request_length);
395402
396    fd = open("/tmp/dump.bin", O_WRONLY | O_TRUNC | O_CREAT);
397    if (fd < 0) {
398        printf("Failed to open file\n");
399        return errno;
400    }
401
402403    while (length > 0) {
403404        if (request_length > length)
404405            request_length = length;
405406
406407        nand_read_pages(dev, page, pages_per_request, buf, request_length, op, ret);
407408
408        write(fd, buf, request_length);
409        ret2 = callback(userdata, buf, request_length);
410        if (ret2)
411            return ret2;
409412
410413        length -= request_length;
411414        page += pages_per_request;
412415    }
413    close(fd);
416
414417    printf("Operation end position : %u \n",
415418           (ret[3]<<24)|(ret[2]<<16)|(ret[1]<<8)|(ret[0]<<0));
416419    free(buf);
417420
418    return 1;
421    return 0;
419422}
usbboot/src/nand.h
66
77struct ingenic_dev;
88
9typedef int (*nand_read_cb_t)(void *userdata, const char *buf, size_t size);
10
911int nand_query(struct ingenic_dev *dev, uint8_t nand_idx);
1012int nand_read(struct ingenic_dev *dev, uint8_t nand_idx, int mode,
11                uint32_t start_page, uint32_t length, uint32_t ram_addr);
12int nand_dump(struct ingenic_dev *dev, uint8_t nand_idx,
13                uint32_t start_page, uint32_t length, const char *filename);
13                uint32_t start_page, uint32_t length, uint32_t ram_addr,
14                nand_read_cb_t callbcallback, void *userdata);
1415int nand_erase(struct ingenic_dev *dev, uint8_t nand_idx,
1516                uint32_t start_block, uint32_t num_blocks);
1617int nand_prog(struct ingenic_dev *dev, uint8_t nand_idx,
usbboot/xburst_include/usb_boot.h
2222#ifndef __USB_BOOT_H__
2323#define __USB_BOOT_H__
2424
25#define BULK_BUF_SIZE (128 * 4096)
25#define BULK_BUF_SIZE (128 * (4096 + 128))
2626
2727enum UDC_STATE
2828{
usbboot/xburst_stage1/board_4740.c
121121
122122    int div[] = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32};
123123
124    if (SDRAM_BW16 == 0xff) {
125        serial_puts("hura");
126        return;
127    }
128
129    serial_put_hex(0xf00);
124130    cpu_clk = CFG_CPU_SPEED;
125131    mem_clk = cpu_clk * div[__cpm_get_cdiv()] / div[__cpm_get_mdiv()];
132    serial_put_hex(0xf01);
126133
127134    REG_EMC_BCR = 0; /* Disable bus release */
135    serial_put_hex(0xf02);
128136    REG_EMC_RTCSR = 0; /* Disable clock for counting */
137    serial_put_hex(0xf03);
129138
130139    /* Fault DMCR value for mode register setting*/
131140#define SDRAM_ROW0 11
132141#define SDRAM_COL0 8
133142#define SDRAM_BANK40 0
134143
144    serial_put_hex(0xf04);
135145    dmcr0 = ((SDRAM_ROW0-11)<<EMC_DMCR_RA_BIT) |
136146        ((SDRAM_COL0-8)<<EMC_DMCR_CA_BIT) |
137147        (SDRAM_BANK40<<EMC_DMCR_BA_BIT) |
...... 
139149        EMC_DMCR_EPIN |
140150        cas_latency_dmcr[((SDRAM_CASL == 3) ? 1 : 0)];
141151
152    serial_put_hex(0xf05);
142153    /* Basic DMCR value */
143154    dmcr = ((SDRAM_ROW-11)<<EMC_DMCR_RA_BIT) |
144155        ((SDRAM_COL-8)<<EMC_DMCR_CA_BIT) |
...... 
147158        EMC_DMCR_EPIN |
148159        cas_latency_dmcr[((SDRAM_CASL == 3) ? 1 : 0)];
149160
161    serial_put_hex(0xf06);
150162    /* SDRAM timimg */
151163    ns = 1000000000 / mem_clk;
152164    tmp = SDRAM_TRAS/ns;
...... 
172184         EMC_SDMR_BL_4 |
173185         cas_latency_sdmr[((SDRAM_CASL == 3) ? 1 : 0)];
174186
187    serial_put_hex(0xf07);
175188    /* Stage 1. Precharge all banks by writing SDMR with DMCR.MRSET=0 */
189    serial_put_hex(dmcr);
190    serial_put_hex(REG_EMC_DMCR);
176191    REG_EMC_DMCR = dmcr;
177192    REG8(EMC_SDMR0|sdmode) = 0;
178193
179194    /* Wait for precharge, > 200us */
180195    tmp = (cpu_clk / 1000000) * 1000;
181196    while (tmp--);
197    serial_put_hex(0xf08);
182198
183199    /* Stage 2. Enable auto-refresh */
184200    REG_EMC_DMCR = dmcr | EMC_DMCR_RFSH;
185
201    serial_put_hex(0xf09);
186202    tmp = SDRAM_TREF/ns;
203    serial_put_hex(0xf10);
187204    tmp = tmp/64 + 1;
205    serial_put_hex(0xf11);
188206    if (tmp > 0xff) tmp = 0xff;
189    REG_EMC_RTCOR = tmp;
207    serial_put_hex(tmp);
208/* REG_EMC_RTCOR = tmp;*/
209    serial_put_hex(0xf10);
190210    REG_EMC_RTCNT = 0;
211    serial_put_hex(0xf11);
191212    REG_EMC_RTCSR = EMC_RTCSR_CKS_64; /* Divisor is 64, CKO/64 */
213    serial_put_hex(0xf12);
192214
193215    /* Wait for number of auto-refresh cycles */
194216    tmp = (cpu_clk / 1000000) * 1000;
...... 
198220    REG_EMC_DMCR = dmcr0 | EMC_DMCR_RFSH | EMC_DMCR_MRSET;
199221    REG8(EMC_SDMR0|sdmode) = 0;
200222
223    serial_put_hex(0xf11);
201224        /* Set back to basic DMCR value */
202225    REG_EMC_DMCR = dmcr | EMC_DMCR_RFSH | EMC_DMCR_MRSET;
203
226    serial_put_hex(0xf12);
204227    /* everything is ok now */
205228}
206229
usbboot/xburst_stage1/main.c
6868        CFG_CPU_SPEED = 192000000;
6969    }
7070    PHM_DIV = fw_args->phm_div;
71    if (fw_args->use_uart > 3)
71    if (fw_args->use_uart > 3)
7272        fw_args->use_uart = 0;
7373    UART_BASE = UART0_BASE + fw_args->use_uart * 0x1000;
7474    CONFIG_BAUDRATE = fw_args->boudrate;
...... 
8888        do_debug();
8989        return ;
9090    }
91/* serial_put_hex(0xf00);*/
9192
9293    switch (CPU_ID) {
9394    case 0x4740:
9495        gpio_init_4740();
96/* serial_put_hex(0xf01);*/
9597        pll_init_4740();
9698        serial_init();
9799        sdram_init_4740();
usbboot/xburst_stage2/nandflash_4740.c
440440    tmpbuf = buf;
441441    handshake_PKT[3] = 0;
442442
443    return cur_page + pagecount;
444
443445    while (cnt < pagecount) {
444446        select_chip(cnt / ppb);
445447        /* If this is the first page of the block, check for bad. */

Archive Download the corresponding diff file



interactive