11#include "acata_internal.h"
13static int atapi_dma_xfer(
acDmaT dma,
int intr, acDmaOp op);
14static void atapi_dma_done(
acDmaT dma);
15static void atapi_dma_error(
acDmaT dma,
int intr, acDmaState state,
int result);
16static int atapi_ops_command(
struct ac_ata_h *atah,
int cmdpri,
int pri);
17static void atapi_ops_done(
struct ac_ata_h *atah,
int result);
18static int atapi_ops_error(
struct ac_ata_h *atah,
int ret);
20static acDmaOpsData ops_8_0 = {&atapi_dma_xfer, &atapi_dma_done, &atapi_dma_error};
21static struct ac_ata_ops ops_48 = {&atapi_ops_command, &atapi_ops_done, &atapi_ops_error};
23static int atapi_dma_xfer(
acDmaT dma,
int intr, acDmaOp op)
29 if ( dmatmp->ad_state == 31 )
31 dmatmp->ad_result = dmatmp->ad_atapi->ap_h.a_size;
32 return op(dma, (
void *)0xB6000000, dmatmp->ad_atapi->ap_h.a_buf, dmatmp->ad_atapi->ap_h.a_size);
34 thid = dmatmp->ad_thid;
43static void atapi_dma_done(
acDmaT dma)
49 thid = dmatmp->ad_thid;
50 dmatmp->ad_state = 127;
55static void atapi_dma_error(
acDmaT dma,
int intr, acDmaState state,
int result)
61 thid = dmatmp->ad_thid;
62 dmatmp->ad_state = 1023;
63 dmatmp->ad_result = result;
71 Kprintf(
"acata:P:dma_error: state=%d ret=%d\n", state, result);
82 *((
volatile acUint16 *)0xB6050000) = 0;
83 *((
volatile acUint16 *)0xB6040000) = 64;
84 *((
volatile acUint16 *)0xB6060000) = flag & 0x10;
85 *((
volatile acUint16 *)0xB6160000) = (flag & 2) ^ 2;
86 *((
volatile acUint16 *)0xB6010000) = flag & 1;
87 *((
volatile acUint16 *)0xB6070000) = 160;
90 while ( (*((
volatile acUint16 *)0xB6070000) & 0x80) != 0 )
94 printf(
"acata:P:wait: TIMEDOUT\n");
103 printf(
"acata:P:packet_send: TIMEDOUT\n");
107 while ( (*((
volatile acUint16 *)0xB6070000) & 8) != 0 )
112 *((
volatile acUint16 *)0xB6000000) = pkt->u_h[0];
118static int atapi_pio_read(acAtaReg atareg, acUint16 *buf,
int count,
int flag)
140 sr_v5 = *((
volatile acUint16 *)0xB6070000);
141 while ( (*((
volatile acUint16 *)0xB6070000) & 0x80) != 0 )
143 xlen = *((
volatile acUint16 *)0xB6070000);
149 xlen = *((
volatile acUint16 *)0xB6160000);
151 while ( (*((
volatile acUint16 *)0xB6160000) & 0x80) != 0 )
154 if ( SleepThread() != 0 )
156 sr_v5 = *((
volatile acUint16 *)0xB6160000);
161 if ( (sr_v5 & 8) == 0 )
163 xlen_v6 = (*((
volatile acUint16 *)0xB6050000) << 8) + *((
volatile acUint16 *)0xB6040000);
164 drop = xlen_v6 - rest;
165 if ( rest >= xlen_v6 )
170 xlen_v8 = (xlen_v6 + 1) / 2 - 1;
171 while ( xlen_v8 >= 0 )
174 *buf++ = *((
volatile acUint16 *)0xB6000000);
177 for ( drop_v10 = sr_v9 / 2 - 1; drop_v10 >= 0; --drop_v10 )
180 if ( (*((
volatile acUint16 *)0xB6070000) & 0x80) == 0 )
188static int atapi_ops_command(
struct ac_ata_h *atah,
int cmdpri,
int pri)
198 if ( (flag & 1) == 0 )
208 dma = acDmaSetup(&dma_data.ad_dma, &ops_8_0, 4, 64, flag & 4);
209 dma_data.ad_result = 0;
211 dma_data.ad_thid = GetThreadId();
213 dma_data.ad_atapi = atapi;
214 dma_data.ad_state = 0;
215 v8 = acDmaRequest(dma);
219 printf(
"acata:P:dma_wait: error %d\n", v8);
229 ret_v5 = dma_data.ad_state;
232 if ( (
int)ret_v5 > 0 )
239 if ( flg == 0 && SleepThread() )
243 printf(
"acata:P:dma_wait: TIMEDOUT %d\n", ret_v5);
244 acDmaCancel(&dma_data.ad_dma, -116);
256 ChangeThreadPriority(0, cmdpri);
257 ret_v5 = atapi_packet_send((acAtaReg)0xB6000000, &atapi->ap_packet, flag);
261 if ( atah->a_state < 0x1FFu )
275 a_buf = (acUint16 *)atah->a_buf;
280 else if ( (flag & 1) != 0 )
282 dma_data.ad_state = 31;
283 ret_v5 = acDmaRequest(&dma_data.ad_dma);
291 v15 = (acUint16 *)atah->a_buf;
292 if ( (flag & 4) == 0 )
294 ret_v5 = atapi_pio_read((acAtaReg)0xB6000000, a_buf, atah->a_size, flag);
301 a_size = atah->a_size;
315 xlen = *((
volatile acUint16 *)0xB6160000);
316 sr_v14 = xlen & 0xFF;
317 while ( (*((
volatile acUint16 *)0xB6160000) & 0x80) != 0 )
320 if ( SleepThread() != 0 )
322 sr_v14 = *((
volatile acUint16 *)0xB6160000);
327 sr_v14 = *((
volatile acUint16 *)0xB6070000);
328 while ( (*((
volatile acUint16 *)0xB6070000) & 0x80) != 0 )
330 xlen = *((
volatile acUint16 *)0xB6070000);
331 sr_v14 = xlen & 0xFF;
337 if ( (sr_v14 & 8) == 0 )
339 ret_v5 = size - a_size;
342 xlen_v15 = (*((
volatile acUint16 *)0xB6050000) << 8) + *((
volatile acUint16 *)0xB6040000);
343 drop = xlen_v15 - a_size;
344 if ( a_size >= xlen_v15 )
349 xlen_v17 = (xlen_v15 + 1) / 2 - 1;
350 for ( sr_v18 = drop + 1; xlen_v17 >= 0; sr_v18 = drop + 1 )
352 *((
volatile acUint16 *)0xB6000000) = *v15;
356 for ( drop_v20 = sr_v18 / 2 - 1; drop_v20 >= 0; --drop_v20 )
357 *((
volatile acUint16 *)0xB6000000) = 0;
359 if ( (*((
volatile acUint16 *)0xB6070000) & 0x80) == 0 )
361 ret_v5 = size - a_size;
369 ChangeThreadPriority(0, pri);
372 if ( atah->a_state < 0x1FFu )
383 if ( (flag & 1) == 0 )
388 if ( (flag & 2) != 0 )
390 while ( (*((
volatile acUint16 *)0xB6160000) & 0x81) == 128 )
404 while ( (*((
volatile acUint16 *)0xB6070000) & 0x81) == 128 )
416 printf(
"acata:C:io_done: TIMEDOUT\n");
430 v30 = *((
volatile acUint16 *)0xB6160000) & 1;
431 if ( v30 || ret_v23 >= 511 )
434 "acata:P:dma_iowait: TIMEDOUT %04x:%02x:%02x\n",
436 *((
volatile acUint16 *)0xB6160000),
437 *((
volatile acUint16 *)0xB6010000));
438 if ( ret_v23 < 1023 )
439 acDmaCancel(&dma_data.ad_dma, -116);
445 ret_v23 = dma_data.ad_state;
446 if ( (*((
volatile acUint16 *)0xB6160000) & 0x80) == 0 && (
int)dma_data.ad_state >= 64 )
448 ad_result = dma_data.ad_result;
454 if ( dma_data.ad_state == 31 )
462 if ( (*((
volatile acUint16 *)0xB6070000) & 1) != 0 )
463 return -((*((
volatile acUint16 *)0xB6070000) << 8) + *((
volatile acUint16 *)0xB6010000));
464 if ( atah->a_state >= 0x1FFu )
472static void atapi_ops_done(
struct ac_ata_h *atah,
int result)
478 ap_done = atapi->ap_done;
480 ap_done(atapi, atah->a_arg, result);
483static int atapi_ops_error(
struct ac_ata_h *atah,
int ret)
505 if ( (*((
volatile acUint16 *)0xB6070000) & 1) == 0 )
507 memset(&sense, 0,
sizeof(sense));
508 memset(&u, 0,
sizeof(u));
512 u.lun = atapi->ap_packet.u_b[1];
513 *((
volatile acUint16 *)0xB6160000) = (flag & 2) ^ 2;
514 *((
volatile acUint16 *)0xB6010000) = 0;
515 v3 = atapi_packet_send((acAtaReg)0xB6000000, &u.pkt, flag);
525 v4 = atapi_pio_read((acAtaReg)0xB6000000, (acUint16 *)&sense,
sizeof(sense), flag);
532 if ( (flag & 2) != 0 )
534 while ( (*((
volatile acUint16 *)0xB6160000) & 0x81) == 128 )
548 while ( (*((
volatile acUint16 *)0xB6070000) & 0x81) == 128 )
560 printf(
"acata:C:io_done: TIMEDOUT\n");
579 return -((sense.s_key << 16) | (sense.s_asc << 8) | sense.s_ascq);
589 atapi->ap_h.a_ops = &ops_48;
590 atapi->ap_h.a_arg = arg;
591 atapi->ap_h.a_tmout = tmout;
592 atapi->ap_h.a_state = 0;
593 atapi->ap_h.a_flag = 0;
594 atapi->ap_done = done;
602 if ( !atapi || !pkt )
606 atapi->ap_h.a_flag = flag;
607 atapi->ap_packet.u_w[0] = pkt->u_w[0];
608 atapi->ap_packet.u_w[1] = pkt->u_w[1];
609 atapi->ap_packet.u_w[2] = pkt->u_w[2];
610 atapi->ap_h.a_buf = buf;
611 atapi->ap_h.a_size = size;
612 return ata_request(&atapi->ap_h, WakeupThread);
618 if ( !atapi || !pkt )
622 atapi->ap_h.a_flag = flag;
623 atapi->ap_packet.u_w[0] = pkt->u_w[0];
624 atapi->ap_packet.u_w[1] = pkt->u_w[1];
625 atapi->ap_packet.u_w[2] = pkt->u_w[2];
626 atapi->ap_h.a_buf = buf;
627 atapi->ap_h.a_size = size;
628 return ata_request(&atapi->ap_h, iWakeupThread);
639 state = atapi->ap_h.a_state;
640 if ( (
unsigned int)(state - 1) >= 0x7E )
u32 count
start sector of fragmented bd/file