2 * Copyright (c) 2011, GraÅžvydas Ignotas
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the organization nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * BASE+1 ~ status: BAOSEI??
41 * /BUSY, ACK, PE, SLCT, ERROR, IRQ
42 * BASE+2 ~ control: ??MISIAS
43 * bidirrectMODE, IRQ_EN, /SLCT_IN, INIT, /AUTO_FD_XT, /STROBE
47 * TH, TR, TL, D3, D2, D1, D0
55 * 6 TL <-- 14 /AUTO_FD_XT
58 * 9 TR <-- 17 /SLCT_IN
60 * start: TH low/high, TL high
62 * TH low - lower nibble: MD ready to recv | MD sent to PC
63 * TL low - lower niblle: sent to MD | ready to recv from MD
64 * TH high - upper nibble: MD ready to recv | MD sent to PC
65 * TL high - upper nibble: sent | ready to recv from MD
68 #define ACK_TIMEOUT 2000000
71 #define PORT_STATUS 889
72 #define PORT_CONTROL 890
74 #define timediff(now, start) \
75 ((now.tv_sec - start.tv_sec) * 1000000 + now.tv_usec - start.tv_usec)
77 #define PBE2(p) ((*(p) << 8) | *(p+1))
78 #define PBE3(p) ((*(p) << 16) | (*(p + 1) << 8) | *(p + 2))
79 #define PBE4(p) ((*(p) << 24) | (*(p + 1) << 16) | (*(p + 2) << 8) | *(p + 3))
81 static void do_exit(const char *msg, const char *where)
83 /* switch TL back to high */
84 outb(0xe0, PORT_CONTROL);
87 fprintf(stderr, "%s: ", where);
89 fprintf(stderr, "%s", msg);
93 static void inthandler(int u)
98 static void wait_th_low(const char *where)
100 struct timeval start, now;
102 gettimeofday(&start, NULL);
104 while (inb(PORT_STATUS) & 0x40) {
105 gettimeofday(&now, NULL);
106 if (timediff(now, start) > ACK_TIMEOUT)
107 do_exit("timeout waiting TH low\n", where);
111 static void wait_th_high(const char *where)
113 struct timeval start, now;
115 gettimeofday(&start, NULL);
117 while (!(inb(PORT_STATUS) & 0x40)) {
118 gettimeofday(&now, NULL);
119 if (timediff(now, start) > ACK_TIMEOUT)
120 do_exit("timeout waiting TH high\n", where);
124 static void output_to_input(void)
126 /* TL high, recv mode; also give time
127 * MD to see TL before we lower it in recv_byte */
128 outb(0xe0, PORT_CONTROL);
129 usleep(4*10); /* must be at least 12+8+8 M68k cycles, 28/7.67M */
132 static void input_to_output(void)
134 wait_th_low("input_to_output");
135 outb(0xc0, PORT_CONTROL); /* TL high, out mode */
138 static unsigned int recv_byte(void)
142 outb(0xe2, PORT_CONTROL); /* TL low */
144 wait_th_low("recv_byte");
146 byte = inb(PORT_DATA) & 0x0f;
148 outb(0xe0, PORT_CONTROL); /* TL high */
150 wait_th_high("recv_byte");
152 byte |= inb(PORT_DATA) << 4;
157 static void recv_bytes(unsigned char *b, size_t count)
163 static void send_byte(unsigned int byte)
165 wait_th_low("recv_bytes");
167 outb(byte & 0x0f, PORT_DATA);
168 outb(0xc2, PORT_CONTROL); /* TL low */
170 wait_th_high("recv_bytes");
172 outb((byte >> 4) & 0x0f, PORT_DATA);
173 outb(0xc0, PORT_CONTROL); /* TL high */
176 static void send_bytes(unsigned char *b, size_t count)
182 static void send_cmd(unsigned int cmd)
184 send_byte(CMD_PREFIX);
188 static void usage(const char *argv0)
190 fprintf(stderr, "usage:\n%s <cmd> [args]\n"
191 "\tsend <file> <addr> [size]\n"
192 "\trecv <file> <addr> <size>\n"
193 "\tjump <addr>\n", argv0);
197 static unsigned int atoi_or_die(const char *a)
202 i = strtoul(a, &p, 0);
203 if (p == NULL || *p != 0) {
204 fprintf(stderr, "atoi: can't convert: %s\n", a);
211 int main(int argc, char *argv[])
213 unsigned int addr = 0, size = 0;
214 unsigned int count = 0, i = 0;
222 data = malloc(0x1000000);
224 fprintf(stderr, "can't alloc %d bytes\n", 0x1000000);
228 /* parse args, read files.. */
229 if (strcmp(argv[1], "send") == 0)
231 if (argc != 4 && argc != 5)
234 file = fopen(argv[2], "rb");
236 fprintf(stderr, "can't open file: %s\n", argv[2]);
240 addr = atoi_or_die(argv[3]);
241 if (argv[4] == NULL) {
242 fseek(file, 0, SEEK_END);
244 fseek(file, 0, SEEK_SET);
247 size = atoi_or_die(argv[4]);
249 ret = fread(data, 1, size, file);
251 fprintf(stderr, "fread returned %d/%d\n", ret, size);
256 else if (strcmp(argv[1], "recv") == 0)
261 file = fopen(argv[2], "wb");
263 fprintf(stderr, "can't open file: %s\n", argv[2]);
267 addr = atoi_or_die(argv[3]);
268 size = atoi_or_die(argv[4]);
270 memset(data, 0, size);
272 else if (strcmp(argv[1], "jump") == 0)
277 addr = atoi_or_die(argv[2]);
279 else if (strcmp(argv[1], "io") == 0)
281 unsigned int cmd = 0, value, iosize;
282 unsigned char *p = data;
284 for (i = 2; i < argc; ) {
285 if (argv[i][0] == 'r')
287 else if (argv[i][0] == 'w')
292 iosize = atoi_or_die(&argv[i][1]);
295 else if (iosize == 16)
297 else if (iosize != 8)
302 addr = atoi_or_die(argv[i]);
308 if (cmd == IOSEQ_W8 || cmd == IOSEQ_W16 || cmd == IOSEQ_W32) {
309 value = atoi_or_die(argv[i]);
328 ret = ioperm(PORT_DATA, 3, 1);
334 signal(SIGINT, inthandler);
336 printf("regs: %02x %02x %02x\n",
337 inb(PORT_DATA), inb(PORT_STATUS), inb(PORT_CONTROL));
338 outb(0xe8, PORT_CONTROL); /* TR low - request for transfer */
340 if (inb(PORT_STATUS) & 0x40)
341 printf("waiting for TH low..\n");
342 while (inb(PORT_STATUS) & 0x40)
345 outb(0xe0, PORT_CONTROL);
347 if (strcmp(argv[1], "send") == 0)
349 send_cmd(CMD_MD_SEND);
350 send_byte((addr >> 16) & 0xff);
351 send_byte((addr >> 8) & 0xff);
352 send_byte((addr >> 0) & 0xff);
353 send_byte((size >> 16) & 0xff);
354 send_byte((size >> 8) & 0xff);
355 send_byte((size >> 0) & 0xff);
357 for (i = 0; i < size; i++)
359 if ((i & 0xff) == 0) {
360 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b");
361 printf("%06x/%06x", i, size);
368 else if (strcmp(argv[1], "recv") == 0)
370 send_cmd(CMD_MD_RECV);
371 send_byte((addr >> 16) & 0xff);
372 send_byte((addr >> 8) & 0xff);
373 send_byte((addr >> 0) & 0xff);
374 send_byte((size >> 16) & 0xff);
375 send_byte((size >> 8) & 0xff);
376 send_byte((size >> 0) & 0xff);
379 for (i = 0; i < size; i++)
381 if ((i & 0xff) == 0) {
382 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b");
383 printf("%06x/%06x", i, size);
387 data[i] = recv_byte();
390 fwrite(data, 1, size, file);
392 else if (strcmp(argv[1], "jump") == 0)
395 send_byte((addr >> 16) & 0xff);
396 send_byte((addr >> 8) & 0xff);
397 send_byte((addr >> 0) & 0xff);
399 else if (strcmp(argv[1], "io") == 0)
401 unsigned char *p = data;
402 unsigned char rdata[4];
404 send_byte((count >> 8) & 0xff);
405 send_byte((count >> 0) & 0xff);
407 for (; count > 0; count--) {
409 send_bytes(p, 4); /* cmd + addr */
414 recv_bytes(rdata, 1);
415 printf("r8 %06x %02x\n", PBE3(p + 1), rdata[0]);
420 recv_bytes(rdata, 2);
421 printf("r16 %06x %04x\n", PBE3(p + 1), PBE2(rdata));
426 recv_bytes(rdata, 4);
427 printf("r32 %06x %08x\n", PBE3(p + 1), PBE4(rdata));
431 send_bytes(&p[4], 1);
432 printf("w8 %06x %02x\n", PBE3(p + 1), p[4]);
436 send_bytes(&p[4], 2);
437 printf("w16 %06x %04x\n", PBE3(p + 1), PBE2(p + 4));
441 send_bytes(&p[4], 4);
442 printf("w32 %06x %08x\n", PBE3(p + 1), PBE4(p + 4));
446 do_exit("error in ioseq data\n", NULL);
453 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b");
454 printf("%06x/%06x\n", i, size);
458 /* switch TL back to high, disable outputs */
459 outb(0xe0, PORT_CONTROL);