blob: 6da1f1470be917cc58f35a909d4782122fb16bca [file] [log] [blame]
Stefan Reinauerc566d202015-08-25 09:52:42 -07001/*
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.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
Martin Rothb54d6ba2015-09-29 14:49:37 -060015 * Foundation, Inc.
Stefan Reinauerc566d202015-08-25 09:52:42 -070016 */
17
18#include <stdio.h>
19#include <string.h>
20#include "em100.h"
21
22/* SDRAM related operations */
23
24int read_sdram(struct em100 *em100, void *data, int address, int length)
25{
26 int actual;
27 int transfer_length = 0x200000;
28 int bytes_read=0;
29 int bytes_left;
30 int bytes_to_read;
31 unsigned char cmd[16];
32
33 memset(cmd, 0, 16);
34 cmd[0] = 0x41; /* em100-to-host eeprom data */
35 cmd[1] = (address >> 24) & 0xff;
36 cmd[2] = (address >> 16) & 0xff;
37 cmd[3] = (address >> 8) & 0xff;
38 cmd[4] = address & 0xff;
39 cmd[5] = (length >> 24) & 0xff;
40 cmd[6] = (length >> 16) & 0xff;
41 cmd[7] = (length >> 8) & 0xff;
42 cmd[8] = length & 0xff;
43
44 if (!send_cmd(em100->dev, cmd)) {
45 printf("error initiating host-to-em100 transfer.\n");
46 return 0;
47 }
48
49 while (bytes_read < length) {
50 actual = 0;
51
52 bytes_left = length - bytes_read;
53 bytes_to_read = (bytes_left < transfer_length) ?
54 bytes_left : transfer_length;
55
56 libusb_bulk_transfer(em100->dev, 2 | LIBUSB_ENDPOINT_IN,
57 data + bytes_read, bytes_to_read,
58 &actual, BULK_SEND_TIMEOUT);
59
60 bytes_read += actual;
61 if (actual < bytes_to_read) {
62 printf("tried reading %d bytes, got %d\n",
63 bytes_to_read, actual);
64 break;
65 }
66
67 printf("Read %d bytes of %d\n", bytes_read, length);
68 }
69
70 return (bytes_read == length);
71}
72
73int write_sdram(struct em100 *em100, unsigned char *data, int address,
74 int length)
75{
76 int actual;
77 int transfer_length = 0x200000;
78 int bytes_sent=0;
79 int bytes_left;
80 int bytes_to_send;
81 unsigned char cmd[16];
82
83 memset(cmd, 0, 16);
84 cmd[0] = 0x40; /* host-to-em100 eeprom data */
85 cmd[1] = (address >> 24) & 0xff;
86 cmd[2] = (address >> 16) & 0xff;
87 cmd[3] = (address >> 8) & 0xff;
88 cmd[4] = address & 0xff;
89 cmd[5] = (length >> 24) & 0xff;
90 cmd[6] = (length >> 16) & 0xff;
91 cmd[7] = (length >> 8) & 0xff;
92 cmd[8] = length & 0xff;
93
94 if (!send_cmd(em100->dev, cmd)) {
95 printf("error initiating host-to-em100 transfer.\n");
96 return 0;
97 }
98
99 while ( bytes_sent < length) {
100 actual = 0;
101
102 bytes_left = length - bytes_sent;
Martin Rothb54d6ba2015-09-29 14:49:37 -0600103 bytes_to_send = (bytes_left < transfer_length) ? bytes_left :
104 transfer_length;
Stefan Reinauerc566d202015-08-25 09:52:42 -0700105
106 libusb_bulk_transfer(em100->dev, 1 | LIBUSB_ENDPOINT_OUT,
Martin Rothb54d6ba2015-09-29 14:49:37 -0600107 data + bytes_sent, bytes_to_send, &actual,
108 BULK_SEND_TIMEOUT);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700109 bytes_sent += actual;
110 if (actual < bytes_to_send) {
Martin Rothb54d6ba2015-09-29 14:49:37 -0600111 printf("Tried sending %d bytes, sent %d\n",
112 bytes_to_send, actual);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700113 break;
114 }
115
116 printf("Sent %d bytes of %d\n", bytes_sent, length);
117 }
118
119 printf ("Transfer %s\n",bytes_sent == length ? "Succeeded" : "Failed");
120 return (bytes_sent == length);
121}