blob: d26eca9012a2abe770031a7a0f1edad2480dcc33 [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
Kevin O'Connor127cbd72008-03-08 11:34:28 -050023#define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
24#define DEBUGF(fmt, args...)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050025
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050026// XXX - lots of redundancy in this file.
27
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050028static int
29await_ide(u8 when_done, u16 base, u16 timeout)
30{
31 u32 time=0,last=0;
32 // for the times you're supposed to throw one away
33 u16 status = inb(base + ATA_CB_STAT);
34 for (;;) {
35 status = inb(base+ATA_CB_STAT);
36 time++;
37 u8 result;
38 if (when_done == BSY)
39 result = status & ATA_CB_STAT_BSY;
40 else if (when_done == NOT_BSY)
41 result = !(status & ATA_CB_STAT_BSY);
42 else if (when_done == NOT_BSY_DRQ)
43 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
44 else if (when_done == NOT_BSY_NOT_DRQ)
45 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
46 else if (when_done == NOT_BSY_RDY)
47 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
48 else if (when_done == TIMEOUT)
49 result = 0;
50
51 if (result)
52 return 0;
53 // mod 2048 each 16 ms
54 if (time>>16 != last) {
55 last = time >>16;
Kevin O'Connor127cbd72008-03-08 11:34:28 -050056 DEBUGF("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY)"
57 " %d time= %d timeout= %d\n"
58 , when_done, time>>11, timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050059 }
60 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -050061 DEBUGF("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
62 ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
63 , when_done, time>>11, timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050064 return -1;
65 }
66 if ((timeout == 0) || ((time>>11) > timeout))
67 break;
68 }
69 BX_INFO("IDE time out\n");
70 return -1;
71}
72
73
74// ---------------------------------------------------------------------------
75// ATA/ATAPI driver : software reset
76// ---------------------------------------------------------------------------
77// ATA-3
78// 8.2.1 Software reset - Device 0
79
80void
81ata_reset(u16 device)
82{
83 u16 iobase1, iobase2;
84 u8 channel, slave, sn, sc;
85 u8 type;
86
87 channel = device / 2;
88 slave = device % 2;
89
90 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
91 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
92
93 // Reset
94
95 // 8.2.1 (a) -- set SRST in DC
96 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
97
98 // 8.2.1 (b) -- wait for BSY
99 await_ide(BSY, iobase1, 20);
100
101 // 8.2.1 (f) -- clear SRST
102 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
103
104 type=GET_EBDA(ata.devices[device].type);
105 if (type != ATA_TYPE_NONE) {
106
107 // 8.2.1 (g) -- check for sc==sn==0x01
108 // select device
109 outb(slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
110 sc = inb(iobase1+ATA_CB_SC);
111 sn = inb(iobase1+ATA_CB_SN);
112
113 if ( (sc==0x01) && (sn==0x01) ) {
114 if (type == ATA_TYPE_ATA) //ATA
115 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
116 else //ATAPI
117 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
118 }
119
120 // 8.2.1 (h) -- wait for not BSY
121 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
122 }
123
124 // Enable interrupts
125 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
126}
127
128static void
129insw(u16 port, u16 segment, u16 offset, u16 count)
130{
131 u16 i;
132 for (i=0; i<count; i++) {
133 u16 d = inw(port);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500134 SET_FARVAR(segment, *(u16*)(offset + 2*i), d);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500135 }
136}
137
138static void
139insl(u16 port, u16 segment, u16 offset, u16 count)
140{
141 u16 i;
142 for (i=0; i<count; i++) {
143 u32 d = inl(port);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500144 SET_FARVAR(segment, *(u32*)(offset + 4*i), d);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500145 }
146}
147
148static void
149outsw(u16 port, u16 segment, u16 offset, u16 count)
150{
151 u16 i;
152 for (i=0; i<count; i++) {
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500153 u16 d = GET_FARVAR(segment, *(u16*)(offset + 2*i));
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500154 outw(d, port);
155 }
156}
157
158static void
159outsl(u16 port, u16 segment, u16 offset, u16 count)
160{
161 u16 i;
162 for (i=0; i<count; i++) {
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500163 u32 d = GET_FARVAR(segment, *(u32*)(offset + 4*i));
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500164 outl(d, port);
165 }
166}
167
168
169// ---------------------------------------------------------------------------
170// ATA/ATAPI driver : execute a data-in command
171// ---------------------------------------------------------------------------
172 // returns
173 // 0 : no error
174 // 1 : BUSY bit set
175 // 2 : read error
176 // 3 : expected DRQ=1
177 // 4 : no sectors left to read/verify
178 // 5 : more sectors to read/verify
179 // 6 : no sectors left to write
180 // 7 : more sectors to write
181u16
182ata_cmd_data_in(u16 device, u16 command, u16 count, u16 cylinder
183 , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
184{
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500185 DEBUGF("ata_cmd_data_in d=%d cmd=%d count=%d c=%d h=%d s=%d"
186 " lba=%d seg=%x off=%x\n"
187 , device, command, count, cylinder, head, sector
188 , lba, segment, offset);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500189
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500190 u8 channel = device / 2;
191 u8 slave = device % 2;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500192
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500193 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
194 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
195 u8 mode = GET_EBDA(ata.devices[device].mode);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500196
197 // Reset count of transferred data
198 SET_EBDA(ata.trsfsectors,0);
199 SET_EBDA(ata.trsfbytes,0L);
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500200 u8 current = 0;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500201
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500202 u8 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500203 if (status & ATA_CB_STAT_BSY)
204 return 1;
205
206 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
207
208 // sector will be 0 only on lba access. Convert to lba-chs
209 if (sector == 0) {
210 if ((count >= 1 << 8) || (lba + count >= 1UL << 28)) {
211 outb(0x00, iobase1 + ATA_CB_FR);
212 outb((count >> 8) & 0xff, iobase1 + ATA_CB_SC);
213 outb(lba >> 24, iobase1 + ATA_CB_SN);
214 outb(0, iobase1 + ATA_CB_CL);
215 outb(0, iobase1 + ATA_CB_CH);
216 command |= 0x04;
217 count &= (1UL << 8) - 1;
218 lba &= (1UL << 24) - 1;
219 }
220 sector = (u16) (lba & 0x000000ffL);
221 cylinder = (u16) ((lba>>8) & 0x0000ffffL);
222 head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
223 }
224
225 outb(0x00, iobase1 + ATA_CB_FR);
226 outb(count, iobase1 + ATA_CB_SC);
227 outb(sector, iobase1 + ATA_CB_SN);
228 outb(cylinder & 0x00ff, iobase1 + ATA_CB_CL);
229 outb(cylinder >> 8, iobase1 + ATA_CB_CH);
230 outb((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (u8) head
231 , iobase1 + ATA_CB_DH);
232 outb(command, iobase1 + ATA_CB_CMD);
233
234 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
235 status = inb(iobase1 + ATA_CB_STAT);
236
237 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500238 DEBUGF("ata_cmd_data_in : read error\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500239 return 2;
240 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500241 DEBUGF("ata_cmd_data_in : DRQ not set (status %02x)\n"
242 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500243 return 3;
244 }
245
246 // FIXME : move seg/off translation here
247
248 irq_enable();
249
250 while (1) {
251
252 if (offset > 0xf800) {
253 offset -= 0x800;
254 segment += 0x80;
255 }
256
257 if (mode == ATA_MODE_PIO32)
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500258 insl(iobase1, segment, offset, 512 / 4);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500259 else
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500260 insw(iobase1, segment, offset, 512 / 2);
261 offset += 512;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500262
263 current++;
264 SET_EBDA(ata.trsfsectors,current);
265 count--;
266 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
267 status = inb(iobase1 + ATA_CB_STAT);
268 if (count == 0) {
269 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
270 | ATA_CB_STAT_ERR) )
271 != ATA_CB_STAT_RDY ) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500272 DEBUGF("ata_cmd_data_in : no sectors left (status %02x)\n"
273 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500274 return 4;
275 }
276 break;
277 }
278 else {
279 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
280 | ATA_CB_STAT_ERR) )
281 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500282 DEBUGF("ata_cmd_data_in : more sectors left (status %02x)\n"
283 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500284 return 5;
285 }
286 continue;
287 }
288 }
289 // Enable interrupts
290 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
291 return 0;
292}
293
294// ---------------------------------------------------------------------------
295// ATA/ATAPI driver : execute a data-out command
296// ---------------------------------------------------------------------------
297 // returns
298 // 0 : no error
299 // 1 : BUSY bit set
300 // 2 : read error
301 // 3 : expected DRQ=1
302 // 4 : no sectors left to read/verify
303 // 5 : more sectors to read/verify
304 // 6 : no sectors left to write
305 // 7 : more sectors to write
306u16
307ata_cmd_data_out(u16 device, u16 command, u16 count, u16 cylinder
308 , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
309{
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500310 DEBUGF("ata_cmd_data_out d=%d cmd=%d count=%d c=%d h=%d s=%d"
311 " lba=%d seg=%x off=%x\n"
312 , device, command, count, cylinder, head, sector
313 , lba, segment, offset);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500314
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500315 u8 channel = device / 2;
316 u8 slave = device % 2;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500317
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500318 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
319 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
320 u8 mode = GET_EBDA(ata.devices[device].mode);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500321
322 // Reset count of transferred data
323 SET_EBDA(ata.trsfsectors,0);
324 SET_EBDA(ata.trsfbytes,0L);
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500325 u8 current = 0;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500326
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500327 u8 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500328 if (status & ATA_CB_STAT_BSY)
329 return 1;
330
331 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
332
333 // sector will be 0 only on lba access. Convert to lba-chs
334 if (sector == 0) {
335 if ((count >= 1 << 8) || (lba + count >= 1UL << 28)) {
336 outb(0x00, iobase1 + ATA_CB_FR);
337 outb((count >> 8) & 0xff, iobase1 + ATA_CB_SC);
338 outb(lba >> 24, iobase1 + ATA_CB_SN);
339 outb(0, iobase1 + ATA_CB_CL);
340 outb(0, iobase1 + ATA_CB_CH);
341 command |= 0x04;
342 count &= (1UL << 8) - 1;
343 lba &= (1UL << 24) - 1;
344 }
345 sector = (u16) (lba & 0x000000ffL);
346 cylinder = (u16) ((lba>>8) & 0x0000ffffL);
347 head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
348 }
349
350 outb(0x00, iobase1 + ATA_CB_FR);
351 outb(count, iobase1 + ATA_CB_SC);
352 outb(sector, iobase1 + ATA_CB_SN);
353 outb(cylinder & 0x00ff, iobase1 + ATA_CB_CL);
354 outb(cylinder >> 8, iobase1 + ATA_CB_CH);
355 outb((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (u8) head
356 , iobase1 + ATA_CB_DH);
357 outb(command, iobase1 + ATA_CB_CMD);
358
359 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
360 status = inb(iobase1 + ATA_CB_STAT);
361
362 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500363 DEBUGF("ata_cmd_data_out : read error\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500364 return 2;
365 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500366 DEBUGF("ata_cmd_data_out : DRQ not set (status %02x)\n"
367 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500368 return 3;
369 }
370
371 // FIXME : move seg/off translation here
372
373 irq_enable();
374
375 while (1) {
376
377 if (offset > 0xf800) {
378 offset -= 0x800;
379 segment += 0x80;
380 }
381
382 if (mode == ATA_MODE_PIO32)
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500383 outsl(iobase1, segment, offset, 512 / 4);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500384 else
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500385 outsw(iobase1, segment, offset, 512 / 2);
386 offset += 512;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500387
388 current++;
389 SET_EBDA(ata.trsfsectors,current);
390 count--;
391 status = inb(iobase1 + ATA_CB_STAT);
392 if (count == 0) {
393 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
394 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
395 != ATA_CB_STAT_RDY ) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500396 DEBUGF("ata_cmd_data_out : no sectors left (status %02x)\n"
397 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500398 return 6;
399 }
400 break;
401 } else {
402 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
403 | ATA_CB_STAT_ERR) )
404 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500405 DEBUGF("ata_cmd_data_out : more sectors left (status %02x)\n"
406 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500407 return 7;
408 }
409 continue;
410 }
411 }
412 // Enable interrupts
413 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
414 return 0;
415}
416
417// ---------------------------------------------------------------------------
418// ATA/ATAPI driver : execute a packet command
419// ---------------------------------------------------------------------------
420 // returns
421 // 0 : no error
422 // 1 : error in parameters
423 // 2 : BUSY bit set
424 // 3 : error
425 // 4 : not ready
426u16
Kevin O'Connor180a9592008-03-04 22:50:53 -0500427ata_cmd_packet(u16 device, u8 *cmdbuf, u8 cmdlen, u16 header
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500428 , u32 length, u8 inout, u16 bufseg, u16 bufoff)
429{
430 u16 iobase1, iobase2;
431 u16 lcount, lbefore, lafter, count;
432 u8 channel, slave;
433 u8 status, mode, lmode;
434 u32 transfer;
435
436 channel = device / 2;
437 slave = device % 2;
438
439 // Data out is not supported yet
440 if (inout == ATA_DATA_OUT) {
441 BX_INFO("ata_cmd_packet: DATA_OUT not supported yet\n");
442 return 1;
443 }
444
445 // The header length must be even
446 if (header & 1) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500447 DEBUGF("ata_cmd_packet : header must be even (%04x)\n", header);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500448 return 1;
449 }
450
451 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
452 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
453 mode = GET_EBDA(ata.devices[device].mode);
454 transfer= 0L;
455
456 if (cmdlen < 12)
457 cmdlen=12;
458 if (cmdlen > 12)
459 cmdlen=16;
460 cmdlen>>=1;
461
462 // Reset count of transferred data
463 SET_EBDA(ata.trsfsectors,0);
464 SET_EBDA(ata.trsfbytes,0L);
465
466 status = inb(iobase1 + ATA_CB_STAT);
467 if (status & ATA_CB_STAT_BSY)
468 return 2;
469
470 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
471 outb(0x00, iobase1 + ATA_CB_FR);
472 outb(0x00, iobase1 + ATA_CB_SC);
473 outb(0x00, iobase1 + ATA_CB_SN);
474 outb(0xfff0 & 0x00ff, iobase1 + ATA_CB_CL);
475 outb(0xfff0 >> 8, iobase1 + ATA_CB_CH);
476 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
477 outb(ATA_CMD_PACKET, iobase1 + ATA_CB_CMD);
478
479 // Device should ok to receive command
480 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
481 status = inb(iobase1 + ATA_CB_STAT);
482
483 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500484 DEBUGF("ata_cmd_packet : error, status is %02x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500485 return 3;
486 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500487 DEBUGF("ata_cmd_packet : DRQ not set (status %02x)\n"
488 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500489 return 4;
490 }
491
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500492 // Send command to device
493 irq_enable();
494
Kevin O'Connor180a9592008-03-04 22:50:53 -0500495 outsw(iobase1, GET_SEG(SS), (u32)cmdbuf, cmdlen);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500496
497 if (inout == ATA_DATA_NO) {
498 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
499 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500500 } else {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500501 u16 loops = 0;
502 u8 sc;
503 while (1) {
504
505 if (loops == 0) {//first time through
506 status = inb(iobase2 + ATA_CB_ASTAT);
507 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500508 } else
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500509 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
510 loops++;
511
512 status = inb(iobase1 + ATA_CB_STAT);
513 sc = inb(iobase1 + ATA_CB_SC);
514
515 // Check if command completed
516 if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
517 ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY))
518 break;
519
520 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500521 DEBUGF("ata_cmd_packet : error (status %02x)\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500522 return 3;
523 }
524
525 // Normalize address
526 bufseg += (bufoff / 16);
527 bufoff %= 16;
528
529 // Get the byte count
530 lcount = ((u16)(inb(iobase1 + ATA_CB_CH))<<8)+inb(iobase1 + ATA_CB_CL);
531
532 // adjust to read what we want
Kevin O'Connor180a9592008-03-04 22:50:53 -0500533 if (header > lcount) {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500534 lbefore=lcount;
535 header-=lcount;
536 lcount=0;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500537 } else {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500538 lbefore=header;
539 header=0;
540 lcount-=lbefore;
541 }
542
Kevin O'Connor180a9592008-03-04 22:50:53 -0500543 if (lcount > length) {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500544 lafter=lcount-length;
545 lcount=length;
546 length=0;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500547 } else {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500548 lafter=0;
549 length-=lcount;
550 }
551
552 // Save byte count
553 count = lcount;
554
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500555 DEBUGF("Trying to read %04x bytes (%04x %04x %04x) "
556 , lbefore+lcount+lafter, lbefore, lcount, lafter);
557 DEBUGF("to 0x%04x:0x%04x\n", bufseg, bufoff);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500558
559 // If counts not dividable by 4, use 16bits mode
560 lmode = mode;
561 if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
562 if (lcount & 0x03) lmode=ATA_MODE_PIO16;
563 if (lafter & 0x03) lmode=ATA_MODE_PIO16;
564
565 // adds an extra byte if count are odd. before is always even
566 if (lcount & 0x01) {
567 lcount+=1;
568 if ((lafter > 0) && (lafter & 0x01)) {
569 lafter-=1;
570 }
571 }
572
573 if (lmode == ATA_MODE_PIO32) {
574 lcount>>=2; lbefore>>=2; lafter>>=2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500575 } else {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500576 lcount>>=1; lbefore>>=1; lafter>>=1;
577 }
578
579 int i;
580 for (i=0; i<lbefore; i++)
581 if (lmode == ATA_MODE_PIO32)
582 inl(iobase1);
583 else
584 inw(iobase1);
585
586 if (lmode == ATA_MODE_PIO32)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500587 insl(iobase1, bufseg, bufoff, lcount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500588 else
Kevin O'Connor180a9592008-03-04 22:50:53 -0500589 insw(iobase1, bufseg, bufoff, lcount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500590
591 for (i=0; i<lafter; i++)
592 if (lmode == ATA_MODE_PIO32)
593 inl(iobase1);
594 else
595 inw(iobase1);
596
597 // Compute new buffer address
598 bufoff += count;
599
600 // Save transferred bytes count
601 transfer += count;
602 SET_EBDA(ata.trsfbytes,transfer);
603 }
604 }
605
606 // Final check, device must be ready
607 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
608 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
609 != ATA_CB_STAT_RDY ) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500610 DEBUGF("ata_cmd_packet : not ready (status %02x)\n"
611 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500612 return 4;
613 }
614
615 // Enable interrupts
616 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
617 return 0;
618}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500619
Kevin O'Connor180a9592008-03-04 22:50:53 -0500620u16
621cdrom_read(u16 device, u32 lba, u32 count, u16 segment, u16 offset, u16 skip)
622{
623 u16 sectors = (count + 2048 - 1) / 2048;
624
625 u8 atacmd[12];
626 memset(atacmd, 0, sizeof(atacmd));
627 atacmd[0]=0x28; // READ command
628 atacmd[7]=(sectors & 0xff00) >> 8; // Sectors
629 atacmd[8]=(sectors & 0x00ff); // Sectors
630 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
631 atacmd[3]=(lba & 0x00ff0000) >> 16;
632 atacmd[4]=(lba & 0x0000ff00) >> 8;
633 atacmd[5]=(lba & 0x000000ff);
634
635 return ata_cmd_packet(device, atacmd, sizeof(atacmd)
636 , skip, count, ATA_DATA_IN
637 , segment, offset);
638}
639
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500640// ---------------------------------------------------------------------------
641// ATA/ATAPI driver : device detection
642// ---------------------------------------------------------------------------
643
644void
645ata_detect()
646{
647 u8 hdcount, cdcount, device, type;
648 u8 buffer[0x0200];
649 memset(buffer, 0, sizeof(buffer));
650
651#if CONFIG_MAX_ATA_INTERFACES > 0
652 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
653 SET_EBDA(ata.channels[0].iobase1,0x1f0);
654 SET_EBDA(ata.channels[0].iobase2,0x3f0);
655 SET_EBDA(ata.channels[0].irq,14);
656#endif
657#if CONFIG_MAX_ATA_INTERFACES > 1
658 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
659 SET_EBDA(ata.channels[1].iobase1,0x170);
660 SET_EBDA(ata.channels[1].iobase2,0x370);
661 SET_EBDA(ata.channels[1].irq,15);
662#endif
663#if CONFIG_MAX_ATA_INTERFACES > 2
664 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
665 SET_EBDA(ata.channels[2].iobase1,0x1e8);
666 SET_EBDA(ata.channels[2].iobase2,0x3e0);
667 SET_EBDA(ata.channels[2].irq,12);
668#endif
669#if CONFIG_MAX_ATA_INTERFACES > 3
670 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
671 SET_EBDA(ata.channels[3].iobase1,0x168);
672 SET_EBDA(ata.channels[3].iobase2,0x360);
673 SET_EBDA(ata.channels[3].irq,11);
674#endif
675#if CONFIG_MAX_ATA_INTERFACES > 4
676#error Please fill the ATA interface informations
677#endif
678
679 // Device detection
680 hdcount=cdcount=0;
681
682 for(device=0; device<CONFIG_MAX_ATA_DEVICES; device++) {
683 u16 iobase1, iobase2;
684 u8 channel, slave, shift;
685 u8 sc, sn, cl, ch, st;
686
687 channel = device / 2;
688 slave = device % 2;
689
690 iobase1 =GET_EBDA(ata.channels[channel].iobase1);
691 iobase2 =GET_EBDA(ata.channels[channel].iobase2);
692
693 // Disable interrupts
694 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
695
696 // Look for device
697 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
698 outb(0x55, iobase1+ATA_CB_SC);
699 outb(0xaa, iobase1+ATA_CB_SN);
700 outb(0xaa, iobase1+ATA_CB_SC);
701 outb(0x55, iobase1+ATA_CB_SN);
702 outb(0x55, iobase1+ATA_CB_SC);
703 outb(0xaa, iobase1+ATA_CB_SN);
704
705 // If we found something
706 sc = inb(iobase1+ATA_CB_SC);
707 sn = inb(iobase1+ATA_CB_SN);
708
709 if ( (sc == 0x55) && (sn == 0xaa) ) {
710 SET_EBDA(ata.devices[device].type,ATA_TYPE_UNKNOWN);
711
712 // reset the channel
713 ata_reset(device);
714
715 // check for ATA or ATAPI
716 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
717 sc = inb(iobase1+ATA_CB_SC);
718 sn = inb(iobase1+ATA_CB_SN);
719 if ((sc==0x01) && (sn==0x01)) {
720 cl = inb(iobase1+ATA_CB_CL);
721 ch = inb(iobase1+ATA_CB_CH);
722 st = inb(iobase1+ATA_CB_STAT);
723
724 if ((cl==0x14) && (ch==0xeb)) {
725 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATAPI);
726 } else if ((cl==0x00) && (ch==0x00) && (st!=0x00)) {
727 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATA);
728 } else if ((cl==0xff) && (ch==0xff)) {
729 SET_EBDA(ata.devices[device].type,ATA_TYPE_NONE);
730 }
731 }
732 }
733
734 type=GET_EBDA(ata.devices[device].type);
735
736 // Now we send a IDENTIFY command to ATA device
737 if(type == ATA_TYPE_ATA) {
738 u32 sectors;
739 u16 cylinders, heads, spt, blksize;
740 u8 translation, removable, mode;
741
742 //Temporary values to do the transfer
743 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
744 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
745
746 u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE
747 , 1, 0, 0, 0, 0L
748 , GET_SEG(SS), (u32)buffer);
749 if (ret)
750 BX_PANIC("ata-detect: Failed to detect ATA device\n");
751
752 removable = (buffer[0] & 0x80) ? 1 : 0;
753 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
754 blksize = *(u16*)&buffer[10];
755
756 cylinders = *(u16*)&buffer[1*2]; // word 1
757 heads = *(u16*)&buffer[3*2]; // word 3
758 spt = *(u16*)&buffer[6*2]; // word 6
759
760 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
761
762 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
763 SET_EBDA(ata.devices[device].removable, removable);
764 SET_EBDA(ata.devices[device].mode, mode);
765 SET_EBDA(ata.devices[device].blksize, blksize);
766 SET_EBDA(ata.devices[device].pchs.heads, heads);
767 SET_EBDA(ata.devices[device].pchs.cylinders, cylinders);
768 SET_EBDA(ata.devices[device].pchs.spt, spt);
769 SET_EBDA(ata.devices[device].sectors, sectors);
770 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation=", channel, slave,cylinders, heads, spt);
771
772 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
773 for (shift=device%4; shift>0; shift--)
774 translation >>= 2;
775 translation &= 0x03;
776
777 SET_EBDA(ata.devices[device].translation, translation);
778
779 switch (translation) {
780 case ATA_TRANSLATION_NONE:
781 BX_INFO("none");
782 break;
783 case ATA_TRANSLATION_LBA:
784 BX_INFO("lba");
785 break;
786 case ATA_TRANSLATION_LARGE:
787 BX_INFO("large");
788 break;
789 case ATA_TRANSLATION_RECHS:
790 BX_INFO("r-echs");
791 break;
792 }
793 switch (translation) {
794 case ATA_TRANSLATION_NONE:
795 break;
796 case ATA_TRANSLATION_LBA:
797 spt = 63;
798 sectors /= 63;
799 heads = sectors / 1024;
800 if (heads>128) heads = 255;
801 else if (heads>64) heads = 128;
802 else if (heads>32) heads = 64;
803 else if (heads>16) heads = 32;
804 else heads=16;
805 cylinders = sectors / heads;
806 break;
807 case ATA_TRANSLATION_RECHS:
808 // Take care not to overflow
809 if (heads==16) {
810 if(cylinders>61439) cylinders=61439;
811 heads=15;
812 cylinders = (u16)((u32)(cylinders)*16/15);
813 }
814 // then go through the large bitshift process
815 case ATA_TRANSLATION_LARGE:
816 while(cylinders > 1024) {
817 cylinders >>= 1;
818 heads <<= 1;
819
820 // If we max out the head count
821 if (heads > 127) break;
822 }
823 break;
824 }
825 // clip to 1024 cylinders in lchs
826 if (cylinders > 1024)
827 cylinders=1024;
828 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
829
830 SET_EBDA(ata.devices[device].lchs.heads, heads);
831 SET_EBDA(ata.devices[device].lchs.cylinders, cylinders);
832 SET_EBDA(ata.devices[device].lchs.spt, spt);
833
834 // fill hdidmap
Kevin O'Connor180a9592008-03-04 22:50:53 -0500835 SET_EBDA(ata.idmap[0][hdcount], device);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500836 hdcount++;
837 }
838
839 // Now we send a IDENTIFY command to ATAPI device
840 if(type == ATA_TYPE_ATAPI) {
841
842 u8 type, removable, mode;
843 u16 blksize;
844
845 //Temporary values to do the transfer
846 SET_EBDA(ata.devices[device].device,ATA_DEVICE_CDROM);
847 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
848
849 u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE_PACKET
850 , 1, 0, 0, 0, 0L
851 , GET_SEG(SS), (u32)buffer);
852 if (ret != 0)
853 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
854
855 type = buffer[1] & 0x1f;
856 removable = (buffer[0] & 0x80) ? 1 : 0;
857 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
858 blksize = 2048;
859
860 SET_EBDA(ata.devices[device].device, type);
861 SET_EBDA(ata.devices[device].removable, removable);
862 SET_EBDA(ata.devices[device].mode, mode);
863 SET_EBDA(ata.devices[device].blksize, blksize);
864
865 // fill cdidmap
Kevin O'Connor180a9592008-03-04 22:50:53 -0500866 SET_EBDA(ata.idmap[1][cdcount], device);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500867 cdcount++;
868 }
869
870 u32 sizeinmb = 0;
871 u16 ataversion;
872 u8 c, i, version=0, model[41];
873
874 switch (type) {
875 case ATA_TYPE_ATA:
876 sizeinmb = GET_EBDA(ata.devices[device].sectors);
877 sizeinmb >>= 11;
878 case ATA_TYPE_ATAPI:
879 // Read ATA/ATAPI version
880 ataversion=((u16)(buffer[161])<<8) | buffer[160];
881 for(version=15;version>0;version--) {
882 if ((ataversion&(1<<version))!=0)
883 break;
884 }
885
886 // Read model name
887 for (i=0;i<20;i++) {
888 model[i*2] = buffer[(i*2)+54+1];
889 model[(i*2)+1] = buffer[(i*2)+54];
890 }
891
892 // Reformat
893 model[40] = 0x00;
894 for (i=39;i>0;i--) {
895 if (model[i]==0x20)
896 model[i] = 0x00;
897 else
898 break;
899 }
900 break;
901 }
902
903 switch (type) {
904 case ATA_TYPE_ATA:
905 printf("ata%d %s: ",channel,slave?" slave":"master");
906 i=0;
907 while ((c=model[i++]))
908 printf("%c",c);
909 if (sizeinmb < (1UL<<16))
910 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u16)sizeinmb);
911 else
912 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, (u16)(sizeinmb>>10));
913 break;
914 case ATA_TYPE_ATAPI:
915 printf("ata%d %s: ",channel,slave?" slave":"master");
916 i=0;
917 while ((c=model[i++]))
918 printf("%c",c);
919 if (GET_EBDA(ata.devices[device].device)==ATA_DEVICE_CDROM)
920 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
921 else
922 printf(" ATAPI-%d Device\n",version);
923 break;
924 case ATA_TYPE_UNKNOWN:
925 printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
926 break;
927 }
928 }
929
930 // Store the devices counts
931 SET_EBDA(ata.hdcount, hdcount);
932 SET_EBDA(ata.cdcount, cdcount);
933 SET_BDA(disk_count, hdcount);
934
935 printf("\n");
936
937 // FIXME : should use bios=cmos|auto|disable bits
938 // FIXME : should know about translation bits
939 // FIXME : move hard_drive_post here
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500940}