blob: 440feab6b8a7931afb39cb323f4e2a7d9bd42307 [file] [log] [blame]
Kevin O'Connorc09492e2008-03-01 13:38:07 -05001// Low level ATA disk access
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2002 MandrakeSoft S.A.
5//
6// This file may be distributed under the terms of the GNU GPLv3 license.
7
Kevin O'Connor3491e8b2008-02-29 00:22:27 -05008#include "ata.h" // ATA_*
9#include "types.h" // u8
10#include "ioport.h" // inb
11#include "util.h" // BX_INFO
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050012#include "cmos.h" // inb_cmos
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050013
14#define TIMEOUT 0
15#define BSY 1
16#define NOT_BSY 2
17#define NOT_BSY_DRQ 3
18#define NOT_BSY_NOT_DRQ 4
19#define NOT_BSY_RDY 5
20
21#define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
22
23#define BX_DEBUG_ATA BX_INFO
24
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050025// XXX - lots of redundancy in this file.
26
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050027static int
28await_ide(u8 when_done, u16 base, u16 timeout)
29{
30 u32 time=0,last=0;
31 // for the times you're supposed to throw one away
32 u16 status = inb(base + ATA_CB_STAT);
33 for (;;) {
34 status = inb(base+ATA_CB_STAT);
35 time++;
36 u8 result;
37 if (when_done == BSY)
38 result = status & ATA_CB_STAT_BSY;
39 else if (when_done == NOT_BSY)
40 result = !(status & ATA_CB_STAT_BSY);
41 else if (when_done == NOT_BSY_DRQ)
42 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
43 else if (when_done == NOT_BSY_NOT_DRQ)
44 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
45 else if (when_done == NOT_BSY_RDY)
46 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
47 else if (when_done == TIMEOUT)
48 result = 0;
49
50 if (result)
51 return 0;
52 // mod 2048 each 16 ms
53 if (time>>16 != last) {
54 last = time >>16;
55 BX_DEBUG_ATA("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n",when_done,time>>11, timeout);
56 }
57 if (status & ATA_CB_STAT_ERR) {
58 BX_DEBUG_ATA("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n",when_done,time>>11, timeout);
59 return -1;
60 }
61 if ((timeout == 0) || ((time>>11) > timeout))
62 break;
63 }
64 BX_INFO("IDE time out\n");
65 return -1;
66}
67
68
69// ---------------------------------------------------------------------------
70// ATA/ATAPI driver : software reset
71// ---------------------------------------------------------------------------
72// ATA-3
73// 8.2.1 Software reset - Device 0
74
75void
76ata_reset(u16 device)
77{
78 u16 iobase1, iobase2;
79 u8 channel, slave, sn, sc;
80 u8 type;
81
82 channel = device / 2;
83 slave = device % 2;
84
85 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
86 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
87
88 // Reset
89
90 // 8.2.1 (a) -- set SRST in DC
91 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
92
93 // 8.2.1 (b) -- wait for BSY
94 await_ide(BSY, iobase1, 20);
95
96 // 8.2.1 (f) -- clear SRST
97 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
98
99 type=GET_EBDA(ata.devices[device].type);
100 if (type != ATA_TYPE_NONE) {
101
102 // 8.2.1 (g) -- check for sc==sn==0x01
103 // select device
104 outb(slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
105 sc = inb(iobase1+ATA_CB_SC);
106 sn = inb(iobase1+ATA_CB_SN);
107
108 if ( (sc==0x01) && (sn==0x01) ) {
109 if (type == ATA_TYPE_ATA) //ATA
110 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
111 else //ATAPI
112 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
113 }
114
115 // 8.2.1 (h) -- wait for not BSY
116 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
117 }
118
119 // Enable interrupts
120 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
121}
122
123static void
124insw(u16 port, u16 segment, u16 offset, u16 count)
125{
126 u16 i;
127 for (i=0; i<count; i++) {
128 u16 d = inw(port);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500129 SET_FARVAR(segment, *(u16*)(offset + 2*i), d);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500130 }
131}
132
133static void
134insl(u16 port, u16 segment, u16 offset, u16 count)
135{
136 u16 i;
137 for (i=0; i<count; i++) {
138 u32 d = inl(port);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500139 SET_FARVAR(segment, *(u32*)(offset + 4*i), d);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500140 }
141}
142
143static void
144outsw(u16 port, u16 segment, u16 offset, u16 count)
145{
146 u16 i;
147 for (i=0; i<count; i++) {
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500148 u16 d = GET_FARVAR(segment, *(u16*)(offset + 2*i));
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500149 outw(d, port);
150 }
151}
152
153static void
154outsl(u16 port, u16 segment, u16 offset, u16 count)
155{
156 u16 i;
157 for (i=0; i<count; i++) {
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500158 u32 d = GET_FARVAR(segment, *(u32*)(offset + 4*i));
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500159 outl(d, port);
160 }
161}
162
163
164// ---------------------------------------------------------------------------
165// ATA/ATAPI driver : execute a data-in command
166// ---------------------------------------------------------------------------
167 // returns
168 // 0 : no error
169 // 1 : BUSY bit set
170 // 2 : read error
171 // 3 : expected DRQ=1
172 // 4 : no sectors left to read/verify
173 // 5 : more sectors to read/verify
174 // 6 : no sectors left to write
175 // 7 : more sectors to write
176u16
177ata_cmd_data_in(u16 device, u16 command, u16 count, u16 cylinder
178 , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
179{
180 u16 iobase1, iobase2, blksize;
181 u8 channel, slave;
182 u8 status, current, mode;
183
184 channel = device / 2;
185 slave = device % 2;
186
187 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
188 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
189 mode = GET_EBDA(ata.devices[device].mode);
190 blksize = 0x200;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500191 if (mode == ATA_MODE_PIO32)
192 blksize>>=2;
193 else
194 blksize>>=1;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500195
196 // Reset count of transferred data
197 SET_EBDA(ata.trsfsectors,0);
198 SET_EBDA(ata.trsfbytes,0L);
199 current = 0;
200
201 status = inb(iobase1 + ATA_CB_STAT);
202 if (status & ATA_CB_STAT_BSY)
203 return 1;
204
205 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
206
207 // sector will be 0 only on lba access. Convert to lba-chs
208 if (sector == 0) {
209 if ((count >= 1 << 8) || (lba + count >= 1UL << 28)) {
210 outb(0x00, iobase1 + ATA_CB_FR);
211 outb((count >> 8) & 0xff, iobase1 + ATA_CB_SC);
212 outb(lba >> 24, iobase1 + ATA_CB_SN);
213 outb(0, iobase1 + ATA_CB_CL);
214 outb(0, iobase1 + ATA_CB_CH);
215 command |= 0x04;
216 count &= (1UL << 8) - 1;
217 lba &= (1UL << 24) - 1;
218 }
219 sector = (u16) (lba & 0x000000ffL);
220 cylinder = (u16) ((lba>>8) & 0x0000ffffL);
221 head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
222 }
223
224 outb(0x00, iobase1 + ATA_CB_FR);
225 outb(count, iobase1 + ATA_CB_SC);
226 outb(sector, iobase1 + ATA_CB_SN);
227 outb(cylinder & 0x00ff, iobase1 + ATA_CB_CL);
228 outb(cylinder >> 8, iobase1 + ATA_CB_CH);
229 outb((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (u8) head
230 , iobase1 + ATA_CB_DH);
231 outb(command, iobase1 + ATA_CB_CMD);
232
233 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
234 status = inb(iobase1 + ATA_CB_STAT);
235
236 if (status & ATA_CB_STAT_ERR) {
237 BX_DEBUG_ATA("ata_cmd_data_in : read error\n");
238 return 2;
239 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
240 BX_DEBUG_ATA("ata_cmd_data_in : DRQ not set (status %02x)\n"
241 , (unsigned) status);
242 return 3;
243 }
244
245 // FIXME : move seg/off translation here
246
247 irq_enable();
248
249 while (1) {
250
251 if (offset > 0xf800) {
252 offset -= 0x800;
253 segment += 0x80;
254 }
255
256 if (mode == ATA_MODE_PIO32)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500257 insl(iobase1, segment, offset, blksize);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500258 else
259 insw(iobase1, segment, offset, blksize);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500260
261 current++;
262 SET_EBDA(ata.trsfsectors,current);
263 count--;
264 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
265 status = inb(iobase1 + ATA_CB_STAT);
266 if (count == 0) {
267 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
268 | ATA_CB_STAT_ERR) )
269 != ATA_CB_STAT_RDY ) {
270 BX_DEBUG_ATA("ata_cmd_data_in : no sectors left (status %02x)\n"
271 , (unsigned) status);
272 return 4;
273 }
274 break;
275 }
276 else {
277 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
278 | ATA_CB_STAT_ERR) )
279 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
280 BX_DEBUG_ATA("ata_cmd_data_in : more sectors left (status %02x)\n"
281 , (unsigned) status);
282 return 5;
283 }
284 continue;
285 }
286 }
287 // Enable interrupts
288 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
289 return 0;
290}
291
292// ---------------------------------------------------------------------------
293// ATA/ATAPI driver : execute a data-out command
294// ---------------------------------------------------------------------------
295 // returns
296 // 0 : no error
297 // 1 : BUSY bit set
298 // 2 : read error
299 // 3 : expected DRQ=1
300 // 4 : no sectors left to read/verify
301 // 5 : more sectors to read/verify
302 // 6 : no sectors left to write
303 // 7 : more sectors to write
304u16
305ata_cmd_data_out(u16 device, u16 command, u16 count, u16 cylinder
306 , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
307{
308 u16 iobase1, iobase2, blksize;
309 u8 channel, slave;
310 u8 status, current, mode;
311
312 channel = device / 2;
313 slave = device % 2;
314
315 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
316 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
317 mode = GET_EBDA(ata.devices[device].mode);
318 blksize = 0x200;
319 if (mode == ATA_MODE_PIO32)
320 blksize>>=2;
321 else
322 blksize>>=1;
323
324 // Reset count of transferred data
325 SET_EBDA(ata.trsfsectors,0);
326 SET_EBDA(ata.trsfbytes,0L);
327 current = 0;
328
329 status = inb(iobase1 + ATA_CB_STAT);
330 if (status & ATA_CB_STAT_BSY)
331 return 1;
332
333 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
334
335 // sector will be 0 only on lba access. Convert to lba-chs
336 if (sector == 0) {
337 if ((count >= 1 << 8) || (lba + count >= 1UL << 28)) {
338 outb(0x00, iobase1 + ATA_CB_FR);
339 outb((count >> 8) & 0xff, iobase1 + ATA_CB_SC);
340 outb(lba >> 24, iobase1 + ATA_CB_SN);
341 outb(0, iobase1 + ATA_CB_CL);
342 outb(0, iobase1 + ATA_CB_CH);
343 command |= 0x04;
344 count &= (1UL << 8) - 1;
345 lba &= (1UL << 24) - 1;
346 }
347 sector = (u16) (lba & 0x000000ffL);
348 cylinder = (u16) ((lba>>8) & 0x0000ffffL);
349 head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
350 }
351
352 outb(0x00, iobase1 + ATA_CB_FR);
353 outb(count, iobase1 + ATA_CB_SC);
354 outb(sector, iobase1 + ATA_CB_SN);
355 outb(cylinder & 0x00ff, iobase1 + ATA_CB_CL);
356 outb(cylinder >> 8, iobase1 + ATA_CB_CH);
357 outb((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (u8) head
358 , iobase1 + ATA_CB_DH);
359 outb(command, iobase1 + ATA_CB_CMD);
360
361 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
362 status = inb(iobase1 + ATA_CB_STAT);
363
364 if (status & ATA_CB_STAT_ERR) {
365 BX_DEBUG_ATA("ata_cmd_data_out : read error\n");
366 return 2;
367 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
368 BX_DEBUG_ATA("ata_cmd_data_out : DRQ not set (status %02x)\n"
369 , (unsigned) status);
370 return 3;
371 }
372
373 // FIXME : move seg/off translation here
374
375 irq_enable();
376
377 while (1) {
378
379 if (offset > 0xf800) {
380 offset -= 0x800;
381 segment += 0x80;
382 }
383
384 if (mode == ATA_MODE_PIO32)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500385 outsl(iobase1, segment, offset, blksize);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500386 else
387 outsw(iobase1, segment, offset, blksize);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500388
389 current++;
390 SET_EBDA(ata.trsfsectors,current);
391 count--;
392 status = inb(iobase1 + ATA_CB_STAT);
393 if (count == 0) {
394 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
395 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
396 != ATA_CB_STAT_RDY ) {
397 BX_DEBUG_ATA("ata_cmd_data_out : no sectors left (status %02x)\n"
398 , (unsigned) status);
399 return 6;
400 }
401 break;
402 } else {
403 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
404 | ATA_CB_STAT_ERR) )
405 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
406 BX_DEBUG_ATA("ata_cmd_data_out : more sectors left (status %02x)\n"
407 , (unsigned) status);
408 return 7;
409 }
410 continue;
411 }
412 }
413 // Enable interrupts
414 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
415 return 0;
416}
417
418// ---------------------------------------------------------------------------
419// ATA/ATAPI driver : execute a packet command
420// ---------------------------------------------------------------------------
421 // returns
422 // 0 : no error
423 // 1 : error in parameters
424 // 2 : BUSY bit set
425 // 3 : error
426 // 4 : not ready
427u16
428ata_cmd_packet(u16 device, u8 cmdlen, u16 cmdseg, u16 cmdoff, u16 header
429 , u32 length, u8 inout, u16 bufseg, u16 bufoff)
430{
431 u16 iobase1, iobase2;
432 u16 lcount, lbefore, lafter, count;
433 u8 channel, slave;
434 u8 status, mode, lmode;
435 u32 transfer;
436
437 channel = device / 2;
438 slave = device % 2;
439
440 // Data out is not supported yet
441 if (inout == ATA_DATA_OUT) {
442 BX_INFO("ata_cmd_packet: DATA_OUT not supported yet\n");
443 return 1;
444 }
445
446 // The header length must be even
447 if (header & 1) {
448 BX_DEBUG_ATA("ata_cmd_packet : header must be even (%04x)\n",header);
449 return 1;
450 }
451
452 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
453 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
454 mode = GET_EBDA(ata.devices[device].mode);
455 transfer= 0L;
456
457 if (cmdlen < 12)
458 cmdlen=12;
459 if (cmdlen > 12)
460 cmdlen=16;
461 cmdlen>>=1;
462
463 // Reset count of transferred data
464 SET_EBDA(ata.trsfsectors,0);
465 SET_EBDA(ata.trsfbytes,0L);
466
467 status = inb(iobase1 + ATA_CB_STAT);
468 if (status & ATA_CB_STAT_BSY)
469 return 2;
470
471 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
472 outb(0x00, iobase1 + ATA_CB_FR);
473 outb(0x00, iobase1 + ATA_CB_SC);
474 outb(0x00, iobase1 + ATA_CB_SN);
475 outb(0xfff0 & 0x00ff, iobase1 + ATA_CB_CL);
476 outb(0xfff0 >> 8, iobase1 + ATA_CB_CH);
477 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
478 outb(ATA_CMD_PACKET, iobase1 + ATA_CB_CMD);
479
480 // Device should ok to receive command
481 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
482 status = inb(iobase1 + ATA_CB_STAT);
483
484 if (status & ATA_CB_STAT_ERR) {
485 BX_DEBUG_ATA("ata_cmd_packet : error, status is %02x\n",status);
486 return 3;
487 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
488 BX_DEBUG_ATA("ata_cmd_packet : DRQ not set (status %02x)\n"
489 , (unsigned) status);
490 return 4;
491 }
492
493 // Normalize address
494 cmdseg += (cmdoff / 16);
495 cmdoff %= 16;
496
497 // Send command to device
498 irq_enable();
499
500 outsw(iobase1, cmdseg, cmdoff, cmdlen);
501
502 if (inout == ATA_DATA_NO) {
503 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
504 status = inb(iobase1 + ATA_CB_STAT);
505 }
506 else {
507 u16 loops = 0;
508 u8 sc;
509 while (1) {
510
511 if (loops == 0) {//first time through
512 status = inb(iobase2 + ATA_CB_ASTAT);
513 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
514 }
515 else
516 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
517 loops++;
518
519 status = inb(iobase1 + ATA_CB_STAT);
520 sc = inb(iobase1 + ATA_CB_SC);
521
522 // Check if command completed
523 if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
524 ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY))
525 break;
526
527 if (status & ATA_CB_STAT_ERR) {
528 BX_DEBUG_ATA("ata_cmd_packet : error (status %02x)\n",status);
529 return 3;
530 }
531
532 // Normalize address
533 bufseg += (bufoff / 16);
534 bufoff %= 16;
535
536 // Get the byte count
537 lcount = ((u16)(inb(iobase1 + ATA_CB_CH))<<8)+inb(iobase1 + ATA_CB_CL);
538
539 // adjust to read what we want
540 if(header>lcount) {
541 lbefore=lcount;
542 header-=lcount;
543 lcount=0;
544 }
545 else {
546 lbefore=header;
547 header=0;
548 lcount-=lbefore;
549 }
550
551 if(lcount>length) {
552 lafter=lcount-length;
553 lcount=length;
554 length=0;
555 }
556 else {
557 lafter=0;
558 length-=lcount;
559 }
560
561 // Save byte count
562 count = lcount;
563
564 BX_DEBUG_ATA("Trying to read %04x bytes (%04x %04x %04x) "
565 ,lbefore+lcount+lafter,lbefore,lcount,lafter);
566 BX_DEBUG_ATA("to 0x%04x:0x%04x\n",bufseg,bufoff);
567
568 // If counts not dividable by 4, use 16bits mode
569 lmode = mode;
570 if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
571 if (lcount & 0x03) lmode=ATA_MODE_PIO16;
572 if (lafter & 0x03) lmode=ATA_MODE_PIO16;
573
574 // adds an extra byte if count are odd. before is always even
575 if (lcount & 0x01) {
576 lcount+=1;
577 if ((lafter > 0) && (lafter & 0x01)) {
578 lafter-=1;
579 }
580 }
581
582 if (lmode == ATA_MODE_PIO32) {
583 lcount>>=2; lbefore>>=2; lafter>>=2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500584 } else {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500585 lcount>>=1; lbefore>>=1; lafter>>=1;
586 }
587
588 int i;
589 for (i=0; i<lbefore; i++)
590 if (lmode == ATA_MODE_PIO32)
591 inl(iobase1);
592 else
593 inw(iobase1);
594
595 if (lmode == ATA_MODE_PIO32)
596 insl(iobase1, bufoff, bufseg, lcount);
597 else
598 insw(iobase1, bufoff, bufseg, lcount);
599
600 for (i=0; i<lafter; i++)
601 if (lmode == ATA_MODE_PIO32)
602 inl(iobase1);
603 else
604 inw(iobase1);
605
606 // Compute new buffer address
607 bufoff += count;
608
609 // Save transferred bytes count
610 transfer += count;
611 SET_EBDA(ata.trsfbytes,transfer);
612 }
613 }
614
615 // Final check, device must be ready
616 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
617 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
618 != ATA_CB_STAT_RDY ) {
619 BX_DEBUG_ATA("ata_cmd_packet : not ready (status %02x)\n"
620 , (unsigned) status);
621 return 4;
622 }
623
624 // Enable interrupts
625 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
626 return 0;
627}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500628
629// ---------------------------------------------------------------------------
630// ATA/ATAPI driver : device detection
631// ---------------------------------------------------------------------------
632
633void
634ata_detect()
635{
636 u8 hdcount, cdcount, device, type;
637 u8 buffer[0x0200];
638 memset(buffer, 0, sizeof(buffer));
639
640#if CONFIG_MAX_ATA_INTERFACES > 0
641 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
642 SET_EBDA(ata.channels[0].iobase1,0x1f0);
643 SET_EBDA(ata.channels[0].iobase2,0x3f0);
644 SET_EBDA(ata.channels[0].irq,14);
645#endif
646#if CONFIG_MAX_ATA_INTERFACES > 1
647 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
648 SET_EBDA(ata.channels[1].iobase1,0x170);
649 SET_EBDA(ata.channels[1].iobase2,0x370);
650 SET_EBDA(ata.channels[1].irq,15);
651#endif
652#if CONFIG_MAX_ATA_INTERFACES > 2
653 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
654 SET_EBDA(ata.channels[2].iobase1,0x1e8);
655 SET_EBDA(ata.channels[2].iobase2,0x3e0);
656 SET_EBDA(ata.channels[2].irq,12);
657#endif
658#if CONFIG_MAX_ATA_INTERFACES > 3
659 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
660 SET_EBDA(ata.channels[3].iobase1,0x168);
661 SET_EBDA(ata.channels[3].iobase2,0x360);
662 SET_EBDA(ata.channels[3].irq,11);
663#endif
664#if CONFIG_MAX_ATA_INTERFACES > 4
665#error Please fill the ATA interface informations
666#endif
667
668 // Device detection
669 hdcount=cdcount=0;
670
671 for(device=0; device<CONFIG_MAX_ATA_DEVICES; device++) {
672 u16 iobase1, iobase2;
673 u8 channel, slave, shift;
674 u8 sc, sn, cl, ch, st;
675
676 channel = device / 2;
677 slave = device % 2;
678
679 iobase1 =GET_EBDA(ata.channels[channel].iobase1);
680 iobase2 =GET_EBDA(ata.channels[channel].iobase2);
681
682 // Disable interrupts
683 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
684
685 // Look for device
686 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
687 outb(0x55, iobase1+ATA_CB_SC);
688 outb(0xaa, iobase1+ATA_CB_SN);
689 outb(0xaa, iobase1+ATA_CB_SC);
690 outb(0x55, iobase1+ATA_CB_SN);
691 outb(0x55, iobase1+ATA_CB_SC);
692 outb(0xaa, iobase1+ATA_CB_SN);
693
694 // If we found something
695 sc = inb(iobase1+ATA_CB_SC);
696 sn = inb(iobase1+ATA_CB_SN);
697
698 if ( (sc == 0x55) && (sn == 0xaa) ) {
699 SET_EBDA(ata.devices[device].type,ATA_TYPE_UNKNOWN);
700
701 // reset the channel
702 ata_reset(device);
703
704 // check for ATA or ATAPI
705 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
706 sc = inb(iobase1+ATA_CB_SC);
707 sn = inb(iobase1+ATA_CB_SN);
708 if ((sc==0x01) && (sn==0x01)) {
709 cl = inb(iobase1+ATA_CB_CL);
710 ch = inb(iobase1+ATA_CB_CH);
711 st = inb(iobase1+ATA_CB_STAT);
712
713 if ((cl==0x14) && (ch==0xeb)) {
714 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATAPI);
715 } else if ((cl==0x00) && (ch==0x00) && (st!=0x00)) {
716 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATA);
717 } else if ((cl==0xff) && (ch==0xff)) {
718 SET_EBDA(ata.devices[device].type,ATA_TYPE_NONE);
719 }
720 }
721 }
722
723 type=GET_EBDA(ata.devices[device].type);
724
725 // Now we send a IDENTIFY command to ATA device
726 if(type == ATA_TYPE_ATA) {
727 u32 sectors;
728 u16 cylinders, heads, spt, blksize;
729 u8 translation, removable, mode;
730
731 //Temporary values to do the transfer
732 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
733 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
734
735 u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE
736 , 1, 0, 0, 0, 0L
737 , GET_SEG(SS), (u32)buffer);
738 if (ret)
739 BX_PANIC("ata-detect: Failed to detect ATA device\n");
740
741 removable = (buffer[0] & 0x80) ? 1 : 0;
742 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
743 blksize = *(u16*)&buffer[10];
744
745 cylinders = *(u16*)&buffer[1*2]; // word 1
746 heads = *(u16*)&buffer[3*2]; // word 3
747 spt = *(u16*)&buffer[6*2]; // word 6
748
749 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
750
751 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
752 SET_EBDA(ata.devices[device].removable, removable);
753 SET_EBDA(ata.devices[device].mode, mode);
754 SET_EBDA(ata.devices[device].blksize, blksize);
755 SET_EBDA(ata.devices[device].pchs.heads, heads);
756 SET_EBDA(ata.devices[device].pchs.cylinders, cylinders);
757 SET_EBDA(ata.devices[device].pchs.spt, spt);
758 SET_EBDA(ata.devices[device].sectors, sectors);
759 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation=", channel, slave,cylinders, heads, spt);
760
761 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
762 for (shift=device%4; shift>0; shift--)
763 translation >>= 2;
764 translation &= 0x03;
765
766 SET_EBDA(ata.devices[device].translation, translation);
767
768 switch (translation) {
769 case ATA_TRANSLATION_NONE:
770 BX_INFO("none");
771 break;
772 case ATA_TRANSLATION_LBA:
773 BX_INFO("lba");
774 break;
775 case ATA_TRANSLATION_LARGE:
776 BX_INFO("large");
777 break;
778 case ATA_TRANSLATION_RECHS:
779 BX_INFO("r-echs");
780 break;
781 }
782 switch (translation) {
783 case ATA_TRANSLATION_NONE:
784 break;
785 case ATA_TRANSLATION_LBA:
786 spt = 63;
787 sectors /= 63;
788 heads = sectors / 1024;
789 if (heads>128) heads = 255;
790 else if (heads>64) heads = 128;
791 else if (heads>32) heads = 64;
792 else if (heads>16) heads = 32;
793 else heads=16;
794 cylinders = sectors / heads;
795 break;
796 case ATA_TRANSLATION_RECHS:
797 // Take care not to overflow
798 if (heads==16) {
799 if(cylinders>61439) cylinders=61439;
800 heads=15;
801 cylinders = (u16)((u32)(cylinders)*16/15);
802 }
803 // then go through the large bitshift process
804 case ATA_TRANSLATION_LARGE:
805 while(cylinders > 1024) {
806 cylinders >>= 1;
807 heads <<= 1;
808
809 // If we max out the head count
810 if (heads > 127) break;
811 }
812 break;
813 }
814 // clip to 1024 cylinders in lchs
815 if (cylinders > 1024)
816 cylinders=1024;
817 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
818
819 SET_EBDA(ata.devices[device].lchs.heads, heads);
820 SET_EBDA(ata.devices[device].lchs.cylinders, cylinders);
821 SET_EBDA(ata.devices[device].lchs.spt, spt);
822
823 // fill hdidmap
824 SET_EBDA(ata.hdidmap[hdcount], device);
825 hdcount++;
826 }
827
828 // Now we send a IDENTIFY command to ATAPI device
829 if(type == ATA_TYPE_ATAPI) {
830
831 u8 type, removable, mode;
832 u16 blksize;
833
834 //Temporary values to do the transfer
835 SET_EBDA(ata.devices[device].device,ATA_DEVICE_CDROM);
836 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
837
838 u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE_PACKET
839 , 1, 0, 0, 0, 0L
840 , GET_SEG(SS), (u32)buffer);
841 if (ret != 0)
842 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
843
844 type = buffer[1] & 0x1f;
845 removable = (buffer[0] & 0x80) ? 1 : 0;
846 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
847 blksize = 2048;
848
849 SET_EBDA(ata.devices[device].device, type);
850 SET_EBDA(ata.devices[device].removable, removable);
851 SET_EBDA(ata.devices[device].mode, mode);
852 SET_EBDA(ata.devices[device].blksize, blksize);
853
854 // fill cdidmap
855 SET_EBDA(ata.cdidmap[cdcount], device);
856 cdcount++;
857 }
858
859 u32 sizeinmb = 0;
860 u16 ataversion;
861 u8 c, i, version=0, model[41];
862
863 switch (type) {
864 case ATA_TYPE_ATA:
865 sizeinmb = GET_EBDA(ata.devices[device].sectors);
866 sizeinmb >>= 11;
867 case ATA_TYPE_ATAPI:
868 // Read ATA/ATAPI version
869 ataversion=((u16)(buffer[161])<<8) | buffer[160];
870 for(version=15;version>0;version--) {
871 if ((ataversion&(1<<version))!=0)
872 break;
873 }
874
875 // Read model name
876 for (i=0;i<20;i++) {
877 model[i*2] = buffer[(i*2)+54+1];
878 model[(i*2)+1] = buffer[(i*2)+54];
879 }
880
881 // Reformat
882 model[40] = 0x00;
883 for (i=39;i>0;i--) {
884 if (model[i]==0x20)
885 model[i] = 0x00;
886 else
887 break;
888 }
889 break;
890 }
891
892 switch (type) {
893 case ATA_TYPE_ATA:
894 printf("ata%d %s: ",channel,slave?" slave":"master");
895 i=0;
896 while ((c=model[i++]))
897 printf("%c",c);
898 if (sizeinmb < (1UL<<16))
899 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u16)sizeinmb);
900 else
901 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, (u16)(sizeinmb>>10));
902 break;
903 case ATA_TYPE_ATAPI:
904 printf("ata%d %s: ",channel,slave?" slave":"master");
905 i=0;
906 while ((c=model[i++]))
907 printf("%c",c);
908 if (GET_EBDA(ata.devices[device].device)==ATA_DEVICE_CDROM)
909 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
910 else
911 printf(" ATAPI-%d Device\n",version);
912 break;
913 case ATA_TYPE_UNKNOWN:
914 printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
915 break;
916 }
917 }
918
919 // Store the devices counts
920 SET_EBDA(ata.hdcount, hdcount);
921 SET_EBDA(ata.cdcount, cdcount);
922 SET_BDA(disk_count, hdcount);
923
924 printf("\n");
925
926 // FIXME : should use bios=cmos|auto|disable bits
927 // FIXME : should know about translation bits
928 // FIXME : move hard_drive_post here
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500929}