Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012-2015 Google Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | #include "em100.h" |
| 17 | |
| 18 | /* SDRAM related operations */ |
| 19 | |
| 20 | int read_sdram(struct em100 *em100, void *data, int address, int length) |
| 21 | { |
| 22 | int actual; |
| 23 | int transfer_length = 0x200000; |
| 24 | int bytes_read=0; |
| 25 | int bytes_left; |
| 26 | int bytes_to_read; |
| 27 | unsigned char cmd[16]; |
| 28 | |
| 29 | memset(cmd, 0, 16); |
| 30 | cmd[0] = 0x41; /* em100-to-host eeprom data */ |
| 31 | cmd[1] = (address >> 24) & 0xff; |
| 32 | cmd[2] = (address >> 16) & 0xff; |
| 33 | cmd[3] = (address >> 8) & 0xff; |
| 34 | cmd[4] = address & 0xff; |
| 35 | cmd[5] = (length >> 24) & 0xff; |
| 36 | cmd[6] = (length >> 16) & 0xff; |
| 37 | cmd[7] = (length >> 8) & 0xff; |
| 38 | cmd[8] = length & 0xff; |
| 39 | |
| 40 | if (!send_cmd(em100->dev, cmd)) { |
| 41 | printf("error initiating host-to-em100 transfer.\n"); |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | while (bytes_read < length) { |
| 46 | actual = 0; |
| 47 | |
| 48 | bytes_left = length - bytes_read; |
| 49 | bytes_to_read = (bytes_left < transfer_length) ? |
| 50 | bytes_left : transfer_length; |
| 51 | |
| 52 | libusb_bulk_transfer(em100->dev, 2 | LIBUSB_ENDPOINT_IN, |
| 53 | data + bytes_read, bytes_to_read, |
| 54 | &actual, BULK_SEND_TIMEOUT); |
| 55 | |
| 56 | bytes_read += actual; |
| 57 | if (actual < bytes_to_read) { |
| 58 | printf("tried reading %d bytes, got %d\n", |
| 59 | bytes_to_read, actual); |
| 60 | break; |
| 61 | } |
| 62 | |
| 63 | printf("Read %d bytes of %d\n", bytes_read, length); |
| 64 | } |
| 65 | |
| 66 | return (bytes_read == length); |
| 67 | } |
| 68 | |
| 69 | int write_sdram(struct em100 *em100, unsigned char *data, int address, |
| 70 | int length) |
| 71 | { |
| 72 | int actual; |
| 73 | int transfer_length = 0x200000; |
| 74 | int bytes_sent=0; |
| 75 | int bytes_left; |
| 76 | int bytes_to_send; |
| 77 | unsigned char cmd[16]; |
| 78 | |
| 79 | memset(cmd, 0, 16); |
| 80 | cmd[0] = 0x40; /* host-to-em100 eeprom data */ |
| 81 | cmd[1] = (address >> 24) & 0xff; |
| 82 | cmd[2] = (address >> 16) & 0xff; |
| 83 | cmd[3] = (address >> 8) & 0xff; |
| 84 | cmd[4] = address & 0xff; |
| 85 | cmd[5] = (length >> 24) & 0xff; |
| 86 | cmd[6] = (length >> 16) & 0xff; |
| 87 | cmd[7] = (length >> 8) & 0xff; |
| 88 | cmd[8] = length & 0xff; |
| 89 | |
| 90 | if (!send_cmd(em100->dev, cmd)) { |
| 91 | printf("error initiating host-to-em100 transfer.\n"); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | while ( bytes_sent < length) { |
| 96 | actual = 0; |
| 97 | |
| 98 | bytes_left = length - bytes_sent; |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 99 | bytes_to_send = (bytes_left < transfer_length) ? bytes_left : |
| 100 | transfer_length; |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 101 | |
| 102 | libusb_bulk_transfer(em100->dev, 1 | LIBUSB_ENDPOINT_OUT, |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 103 | data + bytes_sent, bytes_to_send, &actual, |
| 104 | BULK_SEND_TIMEOUT); |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 105 | bytes_sent += actual; |
| 106 | if (actual < bytes_to_send) { |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 107 | printf("Tried sending %d bytes, sent %d\n", |
| 108 | bytes_to_send, actual); |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 109 | break; |
| 110 | } |
| 111 | |
| 112 | printf("Sent %d bytes of %d\n", bytes_sent, length); |
| 113 | } |
| 114 | |
| 115 | printf ("Transfer %s\n",bytes_sent == length ? "Succeeded" : "Failed"); |
| 116 | return (bytes_sent == length); |
| 117 | } |