blob: 3aea3add09545062e4edd3b8c4ae0b76827b7824 [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{
Kevin O'Connor1fcf1442008-03-11 19:42:41 -040031 u32 time=0, last=0;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050032 // 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
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500128
129// ---------------------------------------------------------------------------
Kevin O'Connor3a049632008-03-11 11:48:04 -0400130// ATA/ATAPI driver : execute a data command
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500131// ---------------------------------------------------------------------------
132 // returns
133 // 0 : no error
134 // 1 : BUSY bit set
135 // 2 : read error
136 // 3 : expected DRQ=1
137 // 4 : no sectors left to read/verify
138 // 5 : more sectors to read/verify
139 // 6 : no sectors left to write
140 // 7 : more sectors to write
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400141
142static int
143send_cmd(struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500144{
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400145 u16 biosid = cmd->biosid;
146 u8 channel = biosid / 2;
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500147 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
148 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500149
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500150 u8 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500151 if (status & ATA_CB_STAT_BSY)
152 return 1;
153
154 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400155 if (cmd->command & 0x04) {
156 outb(0x00, iobase1 + ATA_CB_FR);
157 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
158 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
159 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
160 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500161 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400162 outb(cmd->feature, iobase1 + ATA_CB_FR);
163 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
164 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
165 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
166 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
167 outb(cmd->device, iobase1 + ATA_CB_DH);
168 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500169
170 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500171
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400172 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500173 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400174 DEBUGF("send_cmd : read error\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500175 return 2;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400176 }
177 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400178 DEBUGF("send_cmd : DRQ not set (status %02x)\n"
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500179 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500180 return 3;
181 }
182
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400183 return 0;
184}
185
186int
187ata_transfer(struct ata_pio_command *cmd)
188{
189 DEBUGF("ata_transfer id=%d cmd=%d lba=%d count=%d seg=%x off=%x\n"
190 , cmd->biosid, cmd->command
191 , (cmd->lba_high << 16) | (cmd->lba_mid << 8) | cmd->lba_low
192 , cmd->sector_count, cmd->segment, cmd->offset);
193
194 // Reset count of transferred data
195 SET_EBDA(ata.trsfsectors,0);
196 SET_EBDA(ata.trsfbytes,0L);
197
198 int ret = send_cmd(cmd);
199 if (ret)
200 return ret;
201
202 u16 biosid = cmd->biosid;
203 u8 channel = biosid / 2;
204 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
205 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
206 u8 mode = GET_EBDA(ata.devices[biosid].mode);
207 int iswrite = (cmd->command & ~0x40) == ATA_CMD_WRITE_SECTORS;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500208
209 irq_enable();
210
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400211 u16 segment = cmd->segment;
212 u16 offset = cmd->offset;
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400213 u8 current = 0;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400214 u16 count = cmd->sector_count;
215 u8 status;
216 for (;;) {
217 if (offset >= 0xf800) {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500218 offset -= 0x800;
219 segment += 0x80;
220 }
221
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400222 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400223 // Write data to controller
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400224 DEBUGF("Write sector id=%d dest=%x:%x\n", biosid, segment, offset);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400225 if (mode == ATA_MODE_PIO32)
226 outsl_seg(iobase1, segment, offset, 512 / 4);
227 else
228 outsw_seg(iobase1, segment, offset, 512 / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500229 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400230 // Read data from controller
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400231 DEBUGF("Read sector id=%d dest=%x:%x\n", biosid, segment, offset);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400232 if (mode == ATA_MODE_PIO32)
233 insl_seg(iobase1, segment, offset, 512 / 4);
234 else
235 insw_seg(iobase1, segment, offset, 512 / 2);
236 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
237 }
238 offset += 512;
239
240 current++;
241 SET_EBDA(ata.trsfsectors,current);
242 count--;
243 status = inb(iobase1 + ATA_CB_STAT);
244 if (count == 0)
245 break;
246 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
247 | ATA_CB_STAT_ERR);
248 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400249 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400250 , (unsigned) status);
251 return 5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500252 }
253 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400254
255 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
256 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400257 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400258 status &= ~ATA_CB_STAT_DF;
259 if (status != ATA_CB_STAT_RDY ) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400260 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400261 , (unsigned) status);
262 return 4;
263 }
264
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400265 irq_disable();
266
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500267 // Enable interrupts
268 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
269 return 0;
270}
271
272// ---------------------------------------------------------------------------
273// ATA/ATAPI driver : execute a packet command
274// ---------------------------------------------------------------------------
275 // returns
276 // 0 : no error
277 // 1 : error in parameters
278 // 2 : BUSY bit set
279 // 3 : error
280 // 4 : not ready
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400281int
282ata_cmd_packet(u16 biosid, u8 *cmdbuf, u8 cmdlen
283 , u16 header, u32 length, u16 bufseg, u16 bufoff)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500284{
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400285 DEBUGF("ata_cmd_packet d=%d cmdlen=%d h=%d l=%d"
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400286 " seg=%x off=%x\n"
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400287 , biosid, cmdlen, header, length
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400288 , bufseg, bufoff);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500289
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400290 u8 channel = biosid / 2;
291 u8 slave = biosid % 2;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500292
293 // The header length must be even
294 if (header & 1) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500295 DEBUGF("ata_cmd_packet : header must be even (%04x)\n", header);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500296 return 1;
297 }
298
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400299 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
300 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400301 u8 mode = GET_EBDA(ata.devices[biosid].mode);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500302
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400303 struct ata_pio_command cmd;
304 cmd.sector_count = 0;
305 cmd.feature = 0;
306 cmd.lba_low = 0;
307 cmd.lba_mid = 0xf0;
308 cmd.lba_high = 0xff;
309 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
310 cmd.command = ATA_CMD_PACKET;
311
312 cmd.biosid = biosid;
313 int ret = send_cmd(&cmd);
314 if (ret)
315 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500316
317 // Reset count of transferred data
318 SET_EBDA(ata.trsfsectors,0);
319 SET_EBDA(ata.trsfbytes,0L);
320
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500321 irq_enable();
322
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400323 // Send command to device
324 outsw_seg(iobase1, GET_SEG(SS), (u32)cmdbuf, cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500325
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400326 u8 status;
327 u16 loops = 0;
328 for (;;) {
329 if (loops == 0) {//first time through
330 status = inb(iobase2 + ATA_CB_ASTAT);
331 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
332 } else
333 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
334 loops++;
335
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500336 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400337 inb(iobase1 + ATA_CB_SC);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500338
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400339 // Check if command completed
340 if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
341 ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY))
342 break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500343
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400344 if (status & ATA_CB_STAT_ERR) {
345 DEBUGF("ata_cmd_packet : error (status %02x)\n", status);
346 return 3;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500347 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400348
349 // Normalize address
350 bufseg += (bufoff / 16);
351 bufoff %= 16;
352
353 // Get the byte count
354 u16 lcount = (((u16)(inb(iobase1 + ATA_CB_CH))<<8)
355 + inb(iobase1 + ATA_CB_CL));
356
357 // adjust to read what we want
358 u16 lbefore, lafter;
359 if (header > lcount) {
360 lbefore=lcount;
361 header-=lcount;
362 lcount=0;
363 } else {
364 lbefore=header;
365 header=0;
366 lcount-=lbefore;
367 }
368
369 if (lcount > length) {
370 lafter=lcount-length;
371 lcount=length;
372 length=0;
373 } else {
374 lafter=0;
375 length-=lcount;
376 }
377
378 // Save byte count
379 u16 count = lcount;
380
381 DEBUGF("Trying to read %04x bytes (%04x %04x %04x) "
382 , lbefore+lcount+lafter, lbefore, lcount, lafter);
383 DEBUGF("to 0x%04x:0x%04x\n", bufseg, bufoff);
384
385 // If counts not dividable by 4, use 16bits mode
386 u8 lmode = mode;
387 if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
388 if (lcount & 0x03) lmode=ATA_MODE_PIO16;
389 if (lafter & 0x03) lmode=ATA_MODE_PIO16;
390
391 // adds an extra byte if count are odd. before is always even
392 if (lcount & 0x01) {
393 lcount+=1;
394 if ((lafter > 0) && (lafter & 0x01)) {
395 lafter-=1;
396 }
397 }
398
399 if (lmode == ATA_MODE_PIO32) {
400 lcount>>=2; lbefore>>=2; lafter>>=2;
401 } else {
402 lcount>>=1; lbefore>>=1; lafter>>=1;
403 }
404
405 int i;
406 for (i=0; i<lbefore; i++)
407 if (lmode == ATA_MODE_PIO32)
408 inl(iobase1);
409 else
410 inw(iobase1);
411
412 if (lmode == ATA_MODE_PIO32)
413 insl_seg(iobase1, bufseg, bufoff, lcount);
414 else
415 insw_seg(iobase1, bufseg, bufoff, lcount);
416
417 for (i=0; i<lafter; i++)
418 if (lmode == ATA_MODE_PIO32)
419 inl(iobase1);
420 else
421 inw(iobase1);
422
423 // Compute new buffer address
424 bufoff += count;
425
426 // Save transferred bytes count
427 SET_EBDA(ata.trsfsectors, loops);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500428 }
429
430 // Final check, device must be ready
431 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
432 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
433 != ATA_CB_STAT_RDY ) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500434 DEBUGF("ata_cmd_packet : not ready (status %02x)\n"
435 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500436 return 4;
437 }
438
439 // Enable interrupts
440 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
441 return 0;
442}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500443
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400444int
445cdrom_read(u16 biosid, u32 lba, u32 count, u16 segment, u16 offset, u16 skip)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500446{
447 u16 sectors = (count + 2048 - 1) / 2048;
448
449 u8 atacmd[12];
450 memset(atacmd, 0, sizeof(atacmd));
451 atacmd[0]=0x28; // READ command
452 atacmd[7]=(sectors & 0xff00) >> 8; // Sectors
453 atacmd[8]=(sectors & 0x00ff); // Sectors
454 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
455 atacmd[3]=(lba & 0x00ff0000) >> 16;
456 atacmd[4]=(lba & 0x0000ff00) >> 8;
457 atacmd[5]=(lba & 0x000000ff);
458
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400459 return ata_cmd_packet(biosid, atacmd, sizeof(atacmd)
460 , skip, count, segment, offset);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500461}
462
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500463// ---------------------------------------------------------------------------
464// ATA/ATAPI driver : device detection
465// ---------------------------------------------------------------------------
466
467void
468ata_detect()
469{
470 u8 hdcount, cdcount, device, type;
471 u8 buffer[0x0200];
472 memset(buffer, 0, sizeof(buffer));
473
474#if CONFIG_MAX_ATA_INTERFACES > 0
475 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
476 SET_EBDA(ata.channels[0].iobase1,0x1f0);
477 SET_EBDA(ata.channels[0].iobase2,0x3f0);
478 SET_EBDA(ata.channels[0].irq,14);
479#endif
480#if CONFIG_MAX_ATA_INTERFACES > 1
481 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
482 SET_EBDA(ata.channels[1].iobase1,0x170);
483 SET_EBDA(ata.channels[1].iobase2,0x370);
484 SET_EBDA(ata.channels[1].irq,15);
485#endif
486#if CONFIG_MAX_ATA_INTERFACES > 2
487 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
488 SET_EBDA(ata.channels[2].iobase1,0x1e8);
489 SET_EBDA(ata.channels[2].iobase2,0x3e0);
490 SET_EBDA(ata.channels[2].irq,12);
491#endif
492#if CONFIG_MAX_ATA_INTERFACES > 3
493 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
494 SET_EBDA(ata.channels[3].iobase1,0x168);
495 SET_EBDA(ata.channels[3].iobase2,0x360);
496 SET_EBDA(ata.channels[3].irq,11);
497#endif
498#if CONFIG_MAX_ATA_INTERFACES > 4
499#error Please fill the ATA interface informations
500#endif
501
502 // Device detection
503 hdcount=cdcount=0;
504
505 for(device=0; device<CONFIG_MAX_ATA_DEVICES; device++) {
506 u16 iobase1, iobase2;
507 u8 channel, slave, shift;
508 u8 sc, sn, cl, ch, st;
509
510 channel = device / 2;
511 slave = device % 2;
512
513 iobase1 =GET_EBDA(ata.channels[channel].iobase1);
514 iobase2 =GET_EBDA(ata.channels[channel].iobase2);
515
516 // Disable interrupts
517 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
518
519 // Look for device
520 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
521 outb(0x55, iobase1+ATA_CB_SC);
522 outb(0xaa, iobase1+ATA_CB_SN);
523 outb(0xaa, iobase1+ATA_CB_SC);
524 outb(0x55, iobase1+ATA_CB_SN);
525 outb(0x55, iobase1+ATA_CB_SC);
526 outb(0xaa, iobase1+ATA_CB_SN);
527
528 // If we found something
529 sc = inb(iobase1+ATA_CB_SC);
530 sn = inb(iobase1+ATA_CB_SN);
531
532 if ( (sc == 0x55) && (sn == 0xaa) ) {
533 SET_EBDA(ata.devices[device].type,ATA_TYPE_UNKNOWN);
534
535 // reset the channel
536 ata_reset(device);
537
538 // check for ATA or ATAPI
539 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
540 sc = inb(iobase1+ATA_CB_SC);
541 sn = inb(iobase1+ATA_CB_SN);
542 if ((sc==0x01) && (sn==0x01)) {
543 cl = inb(iobase1+ATA_CB_CL);
544 ch = inb(iobase1+ATA_CB_CH);
545 st = inb(iobase1+ATA_CB_STAT);
546
547 if ((cl==0x14) && (ch==0xeb)) {
548 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATAPI);
549 } else if ((cl==0x00) && (ch==0x00) && (st!=0x00)) {
550 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATA);
551 } else if ((cl==0xff) && (ch==0xff)) {
552 SET_EBDA(ata.devices[device].type,ATA_TYPE_NONE);
553 }
554 }
555 }
556
557 type=GET_EBDA(ata.devices[device].type);
558
559 // Now we send a IDENTIFY command to ATA device
560 if(type == ATA_TYPE_ATA) {
561 u32 sectors;
562 u16 cylinders, heads, spt, blksize;
563 u8 translation, removable, mode;
564
565 //Temporary values to do the transfer
566 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
567 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
568
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400569 u16 ret = ata_cmd_data_chs(device, ATA_CMD_IDENTIFY_DEVICE
570 , 0, 0, 1, 1
571 , GET_SEG(SS), (u32)buffer);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500572 if (ret)
573 BX_PANIC("ata-detect: Failed to detect ATA device\n");
574
575 removable = (buffer[0] & 0x80) ? 1 : 0;
576 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
577 blksize = *(u16*)&buffer[10];
578
579 cylinders = *(u16*)&buffer[1*2]; // word 1
580 heads = *(u16*)&buffer[3*2]; // word 3
581 spt = *(u16*)&buffer[6*2]; // word 6
582
583 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
584
585 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
586 SET_EBDA(ata.devices[device].removable, removable);
587 SET_EBDA(ata.devices[device].mode, mode);
588 SET_EBDA(ata.devices[device].blksize, blksize);
589 SET_EBDA(ata.devices[device].pchs.heads, heads);
590 SET_EBDA(ata.devices[device].pchs.cylinders, cylinders);
591 SET_EBDA(ata.devices[device].pchs.spt, spt);
592 SET_EBDA(ata.devices[device].sectors, sectors);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400593 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation="
594 , channel, slave,cylinders, heads, spt);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500595
596 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
597 for (shift=device%4; shift>0; shift--)
598 translation >>= 2;
599 translation &= 0x03;
600
601 SET_EBDA(ata.devices[device].translation, translation);
602
603 switch (translation) {
604 case ATA_TRANSLATION_NONE:
605 BX_INFO("none");
606 break;
607 case ATA_TRANSLATION_LBA:
608 BX_INFO("lba");
609 break;
610 case ATA_TRANSLATION_LARGE:
611 BX_INFO("large");
612 break;
613 case ATA_TRANSLATION_RECHS:
614 BX_INFO("r-echs");
615 break;
616 }
617 switch (translation) {
618 case ATA_TRANSLATION_NONE:
619 break;
620 case ATA_TRANSLATION_LBA:
621 spt = 63;
622 sectors /= 63;
623 heads = sectors / 1024;
624 if (heads>128) heads = 255;
625 else if (heads>64) heads = 128;
626 else if (heads>32) heads = 64;
627 else if (heads>16) heads = 32;
628 else heads=16;
629 cylinders = sectors / heads;
630 break;
631 case ATA_TRANSLATION_RECHS:
632 // Take care not to overflow
633 if (heads==16) {
634 if(cylinders>61439) cylinders=61439;
635 heads=15;
636 cylinders = (u16)((u32)(cylinders)*16/15);
637 }
638 // then go through the large bitshift process
639 case ATA_TRANSLATION_LARGE:
640 while(cylinders > 1024) {
641 cylinders >>= 1;
642 heads <<= 1;
643
644 // If we max out the head count
645 if (heads > 127) break;
646 }
647 break;
648 }
649 // clip to 1024 cylinders in lchs
650 if (cylinders > 1024)
651 cylinders=1024;
652 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
653
654 SET_EBDA(ata.devices[device].lchs.heads, heads);
655 SET_EBDA(ata.devices[device].lchs.cylinders, cylinders);
656 SET_EBDA(ata.devices[device].lchs.spt, spt);
657
658 // fill hdidmap
Kevin O'Connor180a9592008-03-04 22:50:53 -0500659 SET_EBDA(ata.idmap[0][hdcount], device);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500660 hdcount++;
661 }
662
663 // Now we send a IDENTIFY command to ATAPI device
664 if(type == ATA_TYPE_ATAPI) {
665
666 u8 type, removable, mode;
667 u16 blksize;
668
669 //Temporary values to do the transfer
670 SET_EBDA(ata.devices[device].device,ATA_DEVICE_CDROM);
671 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
672
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400673 u16 ret = ata_cmd_data_chs(device, ATA_CMD_IDENTIFY_DEVICE_PACKET
674 , 0, 0, 1, 1
675 , GET_SEG(SS), (u32)buffer);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500676 if (ret != 0)
677 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
678
679 type = buffer[1] & 0x1f;
680 removable = (buffer[0] & 0x80) ? 1 : 0;
681 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
682 blksize = 2048;
683
684 SET_EBDA(ata.devices[device].device, type);
685 SET_EBDA(ata.devices[device].removable, removable);
686 SET_EBDA(ata.devices[device].mode, mode);
687 SET_EBDA(ata.devices[device].blksize, blksize);
688
689 // fill cdidmap
Kevin O'Connor180a9592008-03-04 22:50:53 -0500690 SET_EBDA(ata.idmap[1][cdcount], device);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500691 cdcount++;
692 }
693
694 u32 sizeinmb = 0;
695 u16 ataversion;
696 u8 c, i, version=0, model[41];
697
698 switch (type) {
699 case ATA_TYPE_ATA:
700 sizeinmb = GET_EBDA(ata.devices[device].sectors);
701 sizeinmb >>= 11;
702 case ATA_TYPE_ATAPI:
703 // Read ATA/ATAPI version
704 ataversion=((u16)(buffer[161])<<8) | buffer[160];
705 for(version=15;version>0;version--) {
706 if ((ataversion&(1<<version))!=0)
707 break;
708 }
709
710 // Read model name
711 for (i=0;i<20;i++) {
712 model[i*2] = buffer[(i*2)+54+1];
713 model[(i*2)+1] = buffer[(i*2)+54];
714 }
715
716 // Reformat
717 model[40] = 0x00;
718 for (i=39;i>0;i--) {
719 if (model[i]==0x20)
720 model[i] = 0x00;
721 else
722 break;
723 }
724 break;
725 }
726
727 switch (type) {
728 case ATA_TYPE_ATA:
729 printf("ata%d %s: ",channel,slave?" slave":"master");
730 i=0;
731 while ((c=model[i++]))
732 printf("%c",c);
733 if (sizeinmb < (1UL<<16))
734 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u16)sizeinmb);
735 else
736 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, (u16)(sizeinmb>>10));
737 break;
738 case ATA_TYPE_ATAPI:
739 printf("ata%d %s: ",channel,slave?" slave":"master");
740 i=0;
741 while ((c=model[i++]))
742 printf("%c",c);
743 if (GET_EBDA(ata.devices[device].device)==ATA_DEVICE_CDROM)
744 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
745 else
746 printf(" ATAPI-%d Device\n",version);
747 break;
748 case ATA_TYPE_UNKNOWN:
749 printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
750 break;
751 }
752 }
753
754 // Store the devices counts
755 SET_EBDA(ata.hdcount, hdcount);
756 SET_EBDA(ata.cdcount, cdcount);
757 SET_BDA(disk_count, hdcount);
758
759 printf("\n");
760
761 // FIXME : should use bios=cmos|auto|disable bits
762 // FIXME : should know about translation bits
763 // FIXME : move hard_drive_post here
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500764}