2 * $Id: testide.c,v 1.5 2006-12-29 00:23:16 nkeynes Exp $
4 * IDE interface test cases. Covers all (known) IDE registers in the
5 * 5F7000 - 5F74FF range including DMA, but does not cover any GD-Rom
6 * device behaviour (ie packet comands).
8 * These tests should be run with the drive empty.
10 * Copyright (c) 2006 Nathan Keynes.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
29 unsigned int test_count = 0, test_failures = 0;
31 #define IDE_BASE 0xA05F7000
33 #define IDE_ALTSTATUS IDE_BASE+0x018
34 #define IDE_UNKNOWN IDE_BASE+0x01C
35 #define IDE_DATA IDE_BASE+0x080 /* 16 bits */
36 #define IDE_FEATURE IDE_BASE+0x084
37 #define IDE_COUNT IDE_BASE+0x088
38 #define IDE_LBA0 IDE_BASE+0x08C
39 #define IDE_LBA1 IDE_BASE+0x090
40 #define IDE_LBA2 IDE_BASE+0x094
41 #define IDE_DEVICE IDE_BASE+0x098
42 #define IDE_COMMAND IDE_BASE+0x09C
43 #define IDE_ACTIVATE IDE_BASE+0x4E4
45 #define IDE_DISC IDE_LBA0
46 #define IDE_DEVCONTROL IDE_ALTSTATUS
47 #define IDE_ERROR IDE_FEATURE
48 #define IDE_STATUS IDE_COMMAND
50 #define IDE_DMA_ADDR IDE_BASE+0x404
51 #define IDE_DMA_SIZE IDE_BASE+0x408
52 #define IDE_DMA_DIR IDE_BASE+0x40C
53 #define IDE_DMA_CTL1 IDE_BASE+0x414
54 #define IDE_DMA_CTL2 IDE_BASE+0x418
55 #define IDE_DMA_MAGIC IDE_BASE+0x4B8
56 #define IDE_DMA_STATUS IDE_BASE+0x4F8
58 #define CHECK_REG_EQUALS( a, b, c ) if( b != c ) { fprintf(stderr, "Assertion failed at %s:%d %s(): expected %08X from register %08X, but was %08X\n", __FILE__, __LINE__, __func__, b, a, c ); return -1; }
60 /* Wait for the standard timeout for an INTRQ. If none is received, print an
63 #define EXPECT_INTRQ() if( ide_wait_irq() != 0 ) { fprintf(stderr, "Timeout at %s:%d %s(): waiting for INTRQ\n", __FILE__, __LINE__, __func__ ); return -1; }
65 /* Check if the INTRQ line is currently cleared (ie inactive) */
66 #define CHECK_INTRQ_CLEAR() if ( (long_read( ASIC_STATUS1 ) & 1) != 0 ) { fprintf(stderr, "Assertion failed at %s:%d %s(): expected INTRQ to be cleared, but was raised.\n", __FILE__, __LINE__, __func__ ); return -1; }
68 #define EXPECT_READY() if( ide_wait_ready() != 0 ) { fprintf(stderr, "Timeout at %s:%d %s(): waiting for BSY flag to clear\n", __FILE__, __LINE__, __func__ ); return -1; }
70 int check_regs( uint32_t *regs,const char *file, int line, const char *fn )
74 for( i=0; regs[i] != 0; i+=2 ) {
75 uint32_t addr = regs[i];
76 uint32_t val = regs[i+1];
78 if( addr == IDE_DATA ) {
79 actual = (uint32_t)word_read(addr);
81 fprintf(stderr, "Assertion failed at %s:%d %s(): expected %04X from register %08X, but was %04X\n", file, line, fn, val, addr, actual );
84 } else if( addr <= IDE_COMMAND ) {
85 actual = (uint32_t)byte_read(addr);
87 fprintf(stderr, "Assertion failed at %s:%d %s(): expected %02X from register %08X, but was %02X\n", file, line, fn, val, addr, actual );
91 actual = long_read(addr);
93 fprintf(stderr, "Assertion failed at %s:%d %s(): expected %08X from register %08X, but was %08X\n", file, line, fn, val, addr, actual );
101 #define CHECK_REGS( r ) if( check_regs(r, __FILE__, __LINE__, __func__) != 0 ) { return -1; }
104 uint32_t post_packet_ready_regs[] =
105 { IDE_ALTSTATUS, 0x58,
110 IDE_STATUS, 0x58, 0, 0 };
112 uint32_t post_packet_cmd_regs[] =
113 { IDE_ALTSTATUS, 0xD0,
119 IDE_STATUS, 0xD0, 0, 0 };
121 uint32_t packet_cmd_error6_regs[] =
122 { IDE_ALTSTATUS, 0x51,
128 IDE_STATUS, 0x51, 0, 0 };
130 uint32_t packet_data_ready_regs[] =
131 { IDE_ALTSTATUS, 0x58,
137 IDE_STATUS, 0x58, 0, 0 };
140 uint32_t post_packet_data_regs[] =
141 { IDE_ALTSTATUS, 0xD0,
147 IDE_STATUS, 0xD0, 0, 0 };
149 uint32_t packet_complete_regs[] =
150 { IDE_ALTSTATUS, 0x50,
156 IDE_STATUS, 0x50, 0, 0 };
158 int send_packet_command( const char *cmd )
160 unsigned short *spkt = (unsigned short *)cmd;
164 byte_write( IDE_FEATURE, 0 );
165 byte_write( IDE_COUNT, 0 );
166 byte_write( IDE_LBA0, 0 );
167 byte_write( IDE_LBA1, 8 );
168 byte_write( IDE_LBA2, 0 );
169 byte_write( IDE_DEVICE, 0 );
170 byte_write( IDE_COMMAND, 0xA0 );
171 byte_read(IDE_ALTSTATUS); /* delay 1 PIO cycle */
172 EXPECT_READY(); /* Wait until device is ready to accept command (usually immediate) */
174 CHECK_REGS( post_packet_ready_regs );
176 /* Write the command */
177 for( i=0; i<6; i++ ) {
178 word_write( IDE_DATA, spkt[i] );
181 byte_read(IDE_ALTSTATUS);
183 // CHECK_REGS( post_packet_cmd_regs );
189 int read_pio( char *buf, int expected_length ) {
190 uint32_t ready_regs[] = {
194 IDE_LBA1, expected_length & 0xFF,
195 IDE_LBA2, (expected_length >> 8),
201 unsigned short *bufptr = (unsigned short *)buf;
202 unsigned int length = 0, avail;
205 CHECK_REGS( ready_regs );
206 for( i=0; i<expected_length; i+=2 ) {
207 *bufptr++ = word_read(IDE_DATA);
212 ready_regs[1] = 0x50;
213 ready_regs[5] = 0x03;
214 ready_regs[13] = 0x50;
215 CHECK_REGS( ready_regs );
219 #define IDE_TEST_PACKET_OK( c,e,l ) if( ide_test_packet_ok( __FILE__, __LINE__, __func__, c, e, l ) != 0 ) { return -1; }
220 int ide_test_packet_ok( const char *file, int line, const char *func,
221 const char *cmd, char *expect, int expect_len )
223 char buf[expect_len];
224 int status = send_packet_command(cmd);
228 status = byte_read( IDE_ALTSTATUS );
229 if( status & 1 ) { /* Error */
230 status = ide_get_sense_code();
231 fprintf( stderr, "Assertion failed at %s:%d %s(): Unexpected error %04X\n",
232 file, line, func, status );
236 status = read_pio( buf, expect_len );
240 if( expect != NULL && memcmp( expect, buf, expect_len ) != 0 ) {
241 fprintf(stderr, "Assertion failed at %s:%d %s(): Results differ from expected:\n",file,line,func );
242 fwrite_diff( stderr, expect, expect_len, buf, expect_len );
248 #define IDE_TEST_PACKET_ERROR( c,e ) if( ide_test_packet_error( __FILE__, __LINE__, __func__, c, e ) != 0 ) { return -1; }
249 int ide_test_packet_error( char *file, int line, char *func,
250 char *cmd, int expect_error )
252 uint32_t error_regs[] =
253 { IDE_ALTSTATUS, 0x51,
254 IDE_ERROR, (expect_error & 0x0F)<<4,
257 IDE_STATUS, 0x51, 0, 0 };
259 int status = send_packet_command(cmd);
263 CHECK_REGS(error_regs);
264 error_code = ide_get_sense_code();
265 CHECK_IEQUALS( expect_error, error_code );
271 uint32_t abort_regs[] = {
282 uint32_t post_reset_regs[] = {
293 uint32_t post_set_feature_regs[] = {
304 uint32_t post_set_feature2_regs[] = {
315 /************************** Interface Tests *******************************/
318 * Test enable/disable of the IDE interface via port
325 /* ensure deactivated */
326 long_write( IDE_ACTIVATE, 0x00042FE );
328 /* test registers to ensure all return 0xFF (need to wait a few cycles?) */
329 for( i= IDE_BASE; i< IDE_BASE+0x400; i+= 4 ) {
330 CHECK_REG_EQUALS( i, 0xFFFFFFFF, long_read( i ) );
333 /* enable interface */
336 /* test registers have default settings */
337 // CHECK_REGS( post_reset_regs );
340 /* disable interface and re-test */
341 long_write( IDE_ACTIVATE, 0x00042FE );
343 /* Test registers all 0xFF */
344 for( i= IDE_BASE; i< IDE_BASE+0x400; i+= 4 ) {
345 CHECK_REG_EQUALS( i, 0xFFFFFFFF, long_read( i ) );
348 /* Finally leave the interface in an enabled state */
354 uint32_t drive_ready_regs[] = {
366 * Test the reset command
370 byte_write( IDE_COMMAND, 0x08 );
373 CHECK_REGS( post_reset_regs );
375 /** Set Default PIO mode */
376 byte_write( IDE_FEATURE, 0x03 );
377 byte_write( IDE_COUNT, 0x0B );
378 byte_write( IDE_COMMAND, 0xEF );
380 CHECK_REGS( post_set_feature_regs );
382 /** Set Multi-word DMA mode 2 */
383 long_write( 0xA05F7490, 0x222 );
384 long_write( 0xA05F7494, 0x222 );
385 byte_write( IDE_FEATURE, 0x03 );
386 byte_write( IDE_COUNT, 0x22 );
387 byte_write( IDE_COMMAND, 0xEF );
390 CHECK_REGS( post_set_feature2_regs );
392 char test_ready_cmd[12] = { 0,0,0,0, 0,0,0,0, 0,0,0,0 };
393 if( send_packet_command(test_ready_cmd) != 0 ) {
397 CHECK_REGS( packet_cmd_error6_regs );
398 int sense = ide_get_sense_code();
399 CHECK_IEQUALS( 0x2906, sense );
401 if( send_packet_command(test_ready_cmd) != 0 ) {
404 CHECK_REGS( drive_ready_regs );
408 char expect_ident[] = { 0x00, 0xb4, 0x19, 0x00,
409 0x00, 0x08, 0x53, 0x45, 0x20, 0x20, 0x20, 0x20 };
412 * Test the PACKET command (using the Inquiry command)
417 char cmd[12] = { 0x11, 0, 4, 0, 12, 0, 0, 0, 0, 0, 0, 0 };
418 // char cmd[12] = { 0x00,0,0,0, 0,0,0,0, 0,0,0,0 };
419 unsigned short *spkt;
422 send_packet_command( cmd );
423 CHECK_REGS( packet_data_ready_regs );
424 spkt = (unsigned short *)result;
425 *spkt++ = word_read(IDE_DATA);
426 *spkt++ = word_read(IDE_DATA);
427 *spkt++ = word_read(IDE_DATA);
428 *spkt++ = word_read(IDE_DATA);
429 CHECK_REGS( packet_data_ready_regs );
430 *spkt++ = word_read(IDE_DATA);
431 *spkt++ = word_read(IDE_DATA);
432 // CHECK_REGS( post_packet_data_regs );
435 CHECK_REGS( packet_complete_regs );
437 if( memcmp( result, expect_ident, 12 ) != 0 ) {
438 fwrite_diff( stderr, expect_ident, 12, result, 12 );
445 * Test the SET FEATURE command
447 int test_set_feature()
455 * Test DMA transfer (using the Inquiry packet comand)
470 /***************************** GD-Rom Tests **********************************/
474 char cmd[12] = { 0x14,0,0,0x00, 0x0C,0,0,0, 0,0,0,0 };
475 char expect[12] = { 0x41, 0,0, 0x96, 0x41, 0, 0x2E, 0x4C, 0xFF, 0xFF, 0xFF, 0xFF };
477 IDE_TEST_PACKET_OK( cmd, expect, 12 );
484 char cmd[12] = {0x30, 0x28, 0, 0x2E, 0x4C, 0, 0, 0, 0, 0, 7, 0 };
485 uint32_t read_pio_ready_regs[] =
486 { IDE_ALTSTATUS, 0x58,
492 IDE_STATUS, 0x58, 0, 0 };
494 if( send_packet_command(cmd) != 0 ) {
498 for( j=0; j<7; j++ ) {
499 CHECK_REGS(read_pio_ready_regs);
501 for( i=0; i<0x0800; i+=2 ) {
502 word_read(IDE_DATA); // throw away for now.
509 read_pio_ready_regs[1] = 0x50;
510 read_pio_ready_regs[5] = 0x03;
511 read_pio_ready_regs[13] = 0x50;
512 CHECK_REGS( read_pio_ready_regs );
517 * Test interaction of Read CD (0x30) with Status (0x40,1)
521 char cmd[12] = { 0x40, 0x01, 0, 0, 16,0,0,0, 0,0,0,0 };
522 char read1cmd[12] = { 0x30, 0x28, 0, 0x2E, 0x4C, 0, 0, 0, 0, 0,1,0 };
523 char expect1[16] = { 0,0x15,0,0x0E, 0x41,2,1,0, 0,1,0,0, 0x2E,0x4D,0,0 };
524 char read2cmd[12] = { 0x30, 0x28, 0, 0x2E, 0x4D, 0, 0, 0, 0, 0,1,0 };
525 char expect2[16] = { 0,0x15,0,0x0E, 0x41,2,1,0, 0,4,0,0, 0x2E,0x50,0,0 };
526 char read3cmd[12] = { 0x30, 0x28, 0, 0x2E, 0x4E, 0, 0, 0, 0, 0,1,0 };
527 char expect3[16] = { 0,0x15,0,0x0E, 0x41,2,1,0, 0,5,0,0, 0x2E,0x51,0,0 };
528 char expect4[16] = { 0,0x15,0,0x0E, 0x41,2,1,0, 0,2,0,0, 0x2E,0x4E,0,0 };
529 char read5cmd[12] = { 0x30, 0x28, 0, 0x2F, 0x01, 0, 0, 0, 0, 0,1,0 };
530 char expect5[16] = { 0,0x15,0,0x0E, 0x41,2,1,0, 0,0xB6,0,0, 0x2F,0x02,0,0 };
531 char read6cmd[12] = { 0x30, 0x28, 0, 0x2F, 0x50, 0, 0, 0, 0, 0,1,0 };
532 char expect6[16] = { 0,0x15,0,0x0E, 0x41,2,1,0, 0x01,0x05,0,0, 0x2F,0x51,0,0 };
533 char read7cmd[12] = { 0x30, 0x28, 0, 0x2F, 0x51, 0, 0, 0, 0, 0,1,0 };
534 char expect7[16] = { 0,0x15,0,0x0E, 0x41,2,1,0, 0x01,0x06,0,0, 0x2F,0x52,0,0 };
537 IDE_TEST_PACKET_OK(read1cmd, NULL, 2048);
538 IDE_TEST_PACKET_OK(cmd, expect1, 14 );
539 IDE_TEST_PACKET_OK(read2cmd, NULL, 2048);
540 IDE_TEST_PACKET_OK(cmd, expect2, 14 );
541 IDE_TEST_PACKET_OK(read3cmd, NULL, 2048);
542 IDE_TEST_PACKET_OK(cmd, expect3, 14 );
543 IDE_TEST_PACKET_OK(read2cmd, NULL, 2048);
544 IDE_TEST_PACKET_OK(cmd, expect4, 14 );
545 IDE_TEST_PACKET_OK(read5cmd, NULL, 2048);
546 IDE_TEST_PACKET_OK(cmd, expect5, 14 );
547 IDE_TEST_PACKET_OK(read6cmd, NULL, 2048);
548 IDE_TEST_PACKET_OK(cmd, expect6, 14 );
553 /********************************* Main **************************************/
555 typedef int (*test_func_t)();
557 test_func_t test_fns[] = { test_enable, test_reset, test_packet,
558 test_dma, test_dma_abort, test_read_pio,
560 test_status1, NULL };
568 for( i=0; test_fns[i] != NULL; i++ ) {
570 if( test_fns[i]() != 0 ) {
571 fprintf( stderr, "Test %d failed\n", i+1 );
577 fprintf( stderr, "%d/%d tests passed!\n", test_count - test_failures, test_count );
578 return test_failures;
.