blob: cfe4b1eee7e4357afc7c0609401b2535038f99f8 [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{
Kevin O'Connorefde6092008-03-12 20:33:15 -0400189 DEBUGF("ata_transfer id=%d cmd=%d lba=%d count=%d buf=%p\n"
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400190 , cmd->biosid, cmd->command
191 , (cmd->lba_high << 16) | (cmd->lba_mid << 8) | cmd->lba_low
Kevin O'Connorefde6092008-03-12 20:33:15 -0400192 , cmd->sector_count, cmd->far_buffer);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400193
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'Connora20c4a52008-03-09 13:32:36 -0400208 u8 current = 0;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400209 u16 count = cmd->sector_count;
210 u8 status;
Kevin O'Connorefde6092008-03-12 20:33:15 -0400211 void *far_buffer = cmd->far_buffer;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400212 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400213 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400214 // Write data to controller
Kevin O'Connorefde6092008-03-12 20:33:15 -0400215 DEBUGF("Write sector id=%d dest=%p\n", biosid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400216 if (mode == ATA_MODE_PIO32)
Kevin O'Connorefde6092008-03-12 20:33:15 -0400217 outsl_far(iobase1, far_buffer, 512 / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400218 else
Kevin O'Connorefde6092008-03-12 20:33:15 -0400219 outsw_far(iobase1, far_buffer, 512 / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500220 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400221 // Read data from controller
Kevin O'Connorefde6092008-03-12 20:33:15 -0400222 DEBUGF("Read sector id=%d dest=%p\n", biosid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400223 if (mode == ATA_MODE_PIO32)
Kevin O'Connorefde6092008-03-12 20:33:15 -0400224 insl_far(iobase1, far_buffer, 512 / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400225 else
Kevin O'Connorefde6092008-03-12 20:33:15 -0400226 insw_far(iobase1, far_buffer, 512 / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400227 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
228 }
Kevin O'Connorefde6092008-03-12 20:33:15 -0400229 far_buffer += 512;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400230
231 current++;
232 SET_EBDA(ata.trsfsectors,current);
233 count--;
234 status = inb(iobase1 + ATA_CB_STAT);
235 if (count == 0)
236 break;
237 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
238 | ATA_CB_STAT_ERR);
239 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400240 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400241 , (unsigned) status);
242 return 5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500243 }
244 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400245
246 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
247 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400248 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400249 status &= ~ATA_CB_STAT_DF;
250 if (status != ATA_CB_STAT_RDY ) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400251 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400252 , (unsigned) status);
253 return 4;
254 }
255
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500256 // Enable interrupts
257 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
258 return 0;
259}
260
261// ---------------------------------------------------------------------------
262// ATA/ATAPI driver : execute a packet command
263// ---------------------------------------------------------------------------
264 // returns
265 // 0 : no error
266 // 1 : error in parameters
267 // 2 : BUSY bit set
268 // 3 : error
269 // 4 : not ready
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400270static inline int
271__ata_cmd_packet(u16 biosid, u8 *cmdbuf, u8 cmdlen
272 , u16 header, u32 length, void *far_buffer)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500273{
Kevin O'Connorefde6092008-03-12 20:33:15 -0400274 DEBUGF("ata_cmd_packet d=%d cmdlen=%d h=%d l=%d buf=%p\n"
275 , biosid, cmdlen, header, length, far_buffer);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500276
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400277 u8 channel = biosid / 2;
278 u8 slave = biosid % 2;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500279
280 // The header length must be even
281 if (header & 1) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500282 DEBUGF("ata_cmd_packet : header must be even (%04x)\n", header);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500283 return 1;
284 }
285
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400286 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
287 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400288 u8 mode = GET_EBDA(ata.devices[biosid].mode);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500289
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400290 struct ata_pio_command cmd;
291 cmd.sector_count = 0;
292 cmd.feature = 0;
293 cmd.lba_low = 0;
294 cmd.lba_mid = 0xf0;
295 cmd.lba_high = 0xff;
296 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
297 cmd.command = ATA_CMD_PACKET;
298
299 cmd.biosid = biosid;
300 int ret = send_cmd(&cmd);
301 if (ret)
302 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500303
304 // Reset count of transferred data
305 SET_EBDA(ata.trsfsectors,0);
306 SET_EBDA(ata.trsfbytes,0L);
307
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400308 // Send command to device
Kevin O'Connor070231b2008-03-22 20:44:37 -0400309 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500310
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400311 u8 status;
312 u16 loops = 0;
313 for (;;) {
314 if (loops == 0) {//first time through
315 status = inb(iobase2 + ATA_CB_ASTAT);
316 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
317 } else
318 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
319 loops++;
320
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500321 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400322 inb(iobase1 + ATA_CB_SC);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500323
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400324 // Check if command completed
325 if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
326 ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY))
327 break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500328
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400329 if (status & ATA_CB_STAT_ERR) {
330 DEBUGF("ata_cmd_packet : error (status %02x)\n", status);
331 return 3;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500332 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400333
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400334 // Get the byte count
335 u16 lcount = (((u16)(inb(iobase1 + ATA_CB_CH))<<8)
336 + inb(iobase1 + ATA_CB_CL));
337
338 // adjust to read what we want
339 u16 lbefore, lafter;
340 if (header > lcount) {
341 lbefore=lcount;
342 header-=lcount;
343 lcount=0;
344 } else {
345 lbefore=header;
346 header=0;
347 lcount-=lbefore;
348 }
349
350 if (lcount > length) {
351 lafter=lcount-length;
352 lcount=length;
353 length=0;
354 } else {
355 lafter=0;
356 length-=lcount;
357 }
358
359 // Save byte count
360 u16 count = lcount;
361
Kevin O'Connorefde6092008-03-12 20:33:15 -0400362 DEBUGF("Trying to read %04x bytes (%04x %04x %04x) to %p\n"
363 , lbefore+lcount+lafter, lbefore, lcount, lafter, far_buffer);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400364
365 // If counts not dividable by 4, use 16bits mode
366 u8 lmode = mode;
367 if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
368 if (lcount & 0x03) lmode=ATA_MODE_PIO16;
369 if (lafter & 0x03) lmode=ATA_MODE_PIO16;
370
371 // adds an extra byte if count are odd. before is always even
372 if (lcount & 0x01) {
373 lcount+=1;
374 if ((lafter > 0) && (lafter & 0x01)) {
375 lafter-=1;
376 }
377 }
378
379 if (lmode == ATA_MODE_PIO32) {
380 lcount>>=2; lbefore>>=2; lafter>>=2;
381 } else {
382 lcount>>=1; lbefore>>=1; lafter>>=1;
383 }
384
385 int i;
386 for (i=0; i<lbefore; i++)
387 if (lmode == ATA_MODE_PIO32)
388 inl(iobase1);
389 else
390 inw(iobase1);
391
392 if (lmode == ATA_MODE_PIO32)
Kevin O'Connorefde6092008-03-12 20:33:15 -0400393 insl_far(iobase1, far_buffer, lcount);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400394 else
Kevin O'Connorefde6092008-03-12 20:33:15 -0400395 insw_far(iobase1, far_buffer, lcount);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400396
397 for (i=0; i<lafter; i++)
398 if (lmode == ATA_MODE_PIO32)
399 inl(iobase1);
400 else
401 inw(iobase1);
402
403 // Compute new buffer address
Kevin O'Connorefde6092008-03-12 20:33:15 -0400404 far_buffer += count;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400405
406 // Save transferred bytes count
407 SET_EBDA(ata.trsfsectors, loops);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500408 }
409
410 // Final check, device must be ready
411 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
412 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
413 != ATA_CB_STAT_RDY ) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500414 DEBUGF("ata_cmd_packet : not ready (status %02x)\n"
415 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500416 return 4;
417 }
418
419 // Enable interrupts
420 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
421 return 0;
422}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500423
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400424int
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400425ata_cmd_packet(u16 biosid, u8 *cmdbuf, u8 cmdlen
426 , u32 length, void *far_buffer)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500427{
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400428 return __ata_cmd_packet(biosid, cmdbuf, cmdlen, 0, length, far_buffer);
429}
Kevin O'Connor180a9592008-03-04 22:50:53 -0500430
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400431static void
432build_cdrom_cmd(u8 *atacmd, u32 lba, u16 count)
433{
434 memset(atacmd, 0, 12);
435 atacmd[0]=0x28; // READ command
436 atacmd[7]=(count & 0xff00) >> 8; // Sectors
437 atacmd[8]=(count & 0x00ff); // Sectors
438 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
Kevin O'Connor180a9592008-03-04 22:50:53 -0500439 atacmd[3]=(lba & 0x00ff0000) >> 16;
440 atacmd[4]=(lba & 0x0000ff00) >> 8;
441 atacmd[5]=(lba & 0x000000ff);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500442}
443
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400444// Read sectors from the cdrom.
445int
446cdrom_read(u16 biosid, u32 lba, u32 count, void *far_buffer)
447{
448 u8 atacmd[12];
449 build_cdrom_cmd(atacmd, lba, count);
450 return ata_cmd_packet(biosid, atacmd, sizeof(atacmd)
451 , count*2048, far_buffer);
452}
453
454// Pretend the cdrom has 512 byte sectors (instead of 2048) and read
455// sectors.
456int
457cdrom_read_512(u16 biosid, u32 vlba, u32 count, void *far_buffer)
458{
459 u32 slba = vlba / 4;
460 u16 before = vlba % 4;
461 u32 elba = (vlba + count - 1) / 4;
462
463 u8 atacmd[12];
464 build_cdrom_cmd(atacmd, slba, elba - slba + 1);
465
466 int status = __ata_cmd_packet(biosid, atacmd, sizeof(atacmd)
467 , before*512, count*512, far_buffer);
468 if (status) {
469 SET_EBDA(ata.trsfsectors, 0);
470 return status;
471 }
472 SET_EBDA(ata.trsfsectors, count);
473 return 0;
474}
475
476
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500477// ---------------------------------------------------------------------------
478// ATA/ATAPI driver : device detection
479// ---------------------------------------------------------------------------
480
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400481static void
482report_model(u8 devid, u8 *buffer)
483{
484 u8 model[41];
485
486 // Read model name
487 int i;
488 for (i=0; i<40; i+=2) {
489 model[i] = buffer[i+54+1];
490 model[i+1] = buffer[i+54];
491 }
492
493 // Reformat
494 model[40] = 0x00;
495 for (i=39; i>0; i--) {
496 if (model[i] != 0x20)
497 break;
498 model[i] = 0x00;
499 }
500
501 u8 channel = devid / 2;
502 u8 slave = devid % 2;
503 // XXX - model on stack not %cs
504 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
505}
506
507static u8
508get_ata_version(u8 *buffer)
509{
510 u16 ataversion = *(u16*)&buffer[160];
511 u8 version;
512 for (version=15; version>0; version--)
513 if (ataversion & (1<<version))
514 break;
515 return version;
516}
517
518static void
519init_drive_atapi(u8 devid)
520{
521 SET_EBDA(ata.devices[devid].type,ATA_TYPE_ATAPI);
522
523 // Temporary values to do the transfer
524 SET_EBDA(ata.devices[devid].device,ATA_DEVICE_CDROM);
525 SET_EBDA(ata.devices[devid].mode, ATA_MODE_PIO16);
526
527 // Now we send a IDENTIFY command to ATAPI device
528 u8 buffer[0x0200];
529 memset(buffer, 0, sizeof(buffer));
530 u16 ret = ata_cmd_data(devid, ATA_CMD_IDENTIFY_DEVICE_PACKET
531 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400532 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400533 if (ret != 0)
534 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
535
536 u8 type = buffer[1] & 0x1f;
537 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
538 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
539 u16 blksize = 2048;
540
541 SET_EBDA(ata.devices[devid].device, type);
542 SET_EBDA(ata.devices[devid].removable, removable);
543 SET_EBDA(ata.devices[devid].mode, mode);
544 SET_EBDA(ata.devices[devid].blksize, blksize);
545
546 // fill cdidmap
547 u8 cdcount = GET_EBDA(ata.cdcount);
548 SET_EBDA(ata.idmap[1][cdcount], devid);
549 SET_EBDA(ata.cdcount, ++cdcount);
550
551 report_model(devid, buffer);
552 u8 version = get_ata_version(buffer);
553 if (GET_EBDA(ata.devices[devid].device)==ATA_DEVICE_CDROM)
554 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
555 else
556 printf(" ATAPI-%d Device\n", version);
557}
558
559static void
560init_drive_ata(u8 devid)
561{
562 SET_EBDA(ata.devices[devid].type,ATA_TYPE_ATA);
563
564 // Temporary values to do the transfer
565 SET_EBDA(ata.devices[devid].device, ATA_DEVICE_HD);
566 SET_EBDA(ata.devices[devid].mode, ATA_MODE_PIO16);
567
568 // Now we send a IDENTIFY command to ATA device
569 u8 buffer[0x0200];
570 memset(buffer, 0, sizeof(buffer));
571 u16 ret = ata_cmd_data(devid, ATA_CMD_IDENTIFY_DEVICE
572 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400573 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400574 if (ret)
575 BX_PANIC("ata-detect: Failed to detect ATA device\n");
576
577 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
578 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
579 u16 blksize = *(u16*)&buffer[10];
580
581 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
582 u16 heads = *(u16*)&buffer[3*2]; // word 3
583 u16 spt = *(u16*)&buffer[6*2]; // word 6
584
585 u32 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
586
587 SET_EBDA(ata.devices[devid].device,ATA_DEVICE_HD);
588 SET_EBDA(ata.devices[devid].removable, removable);
589 SET_EBDA(ata.devices[devid].mode, mode);
590 SET_EBDA(ata.devices[devid].blksize, blksize);
591 SET_EBDA(ata.devices[devid].pchs.heads, heads);
592 SET_EBDA(ata.devices[devid].pchs.cylinders, cylinders);
593 SET_EBDA(ata.devices[devid].pchs.spt, spt);
594 SET_EBDA(ata.devices[devid].sectors, sectors);
595
596 u8 channel = devid / 2;
597 u8 slave = devid % 2;
598 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
599 u8 shift;
600 for (shift=devid%4; shift>0; shift--)
601 translation >>= 2;
602 translation &= 0x03;
603
604 SET_EBDA(ata.devices[devid].translation, translation);
605
606 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation="
607 , channel, slave, cylinders, heads, spt);
608 switch (translation) {
609 case ATA_TRANSLATION_NONE:
610 BX_INFO("none");
611 break;
612 case ATA_TRANSLATION_LBA:
613 BX_INFO("lba");
614 spt = 63;
615 sectors /= 63;
616 heads = sectors / 1024;
617 if (heads>128)
618 heads = 255;
619 else if (heads>64)
620 heads = 128;
621 else if (heads>32)
622 heads = 64;
623 else if (heads>16)
624 heads = 32;
625 else
626 heads = 16;
627 cylinders = sectors / heads;
628 break;
629 case ATA_TRANSLATION_RECHS:
630 BX_INFO("r-echs");
631 // Take care not to overflow
632 if (heads==16) {
633 if (cylinders>61439)
634 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 if (translation == ATA_TRANSLATION_LARGE)
641 BX_INFO("large");
642 while(cylinders > 1024) {
643 cylinders >>= 1;
644 heads <<= 1;
645
646 // If we max out the head count
647 if (heads > 127)
648 break;
649 }
650 break;
651 }
652 // clip to 1024 cylinders in lchs
653 if (cylinders > 1024)
654 cylinders = 1024;
655 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
656
657 SET_EBDA(ata.devices[devid].lchs.heads, heads);
658 SET_EBDA(ata.devices[devid].lchs.cylinders, cylinders);
659 SET_EBDA(ata.devices[devid].lchs.spt, spt);
660
661 // fill hdidmap
662 u8 hdcount = GET_EBDA(ata.hdcount);
663 SET_EBDA(ata.idmap[0][hdcount], devid);
664 SET_EBDA(ata.hdcount, ++hdcount);
665
666 u32 sizeinmb = GET_EBDA(ata.devices[devid].sectors);
667 sizeinmb >>= 11;
668
669 report_model(devid, buffer);
670 u8 version = get_ata_version(buffer);
671 if (sizeinmb < (1 << 16))
672 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, sizeinmb);
673 else
674 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, sizeinmb >> 10);
675}
676
677static void
678init_drive_unknown(u8 devid)
679{
680 SET_EBDA(ata.devices[devid].type,ATA_TYPE_UNKNOWN);
681
682 u8 channel = devid / 2;
683 u8 slave = devid % 2;
684 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
685}
686
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500687void
688ata_detect()
689{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500690#if CONFIG_MAX_ATA_INTERFACES > 0
691 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
692 SET_EBDA(ata.channels[0].iobase1,0x1f0);
693 SET_EBDA(ata.channels[0].iobase2,0x3f0);
694 SET_EBDA(ata.channels[0].irq,14);
695#endif
696#if CONFIG_MAX_ATA_INTERFACES > 1
697 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
698 SET_EBDA(ata.channels[1].iobase1,0x170);
699 SET_EBDA(ata.channels[1].iobase2,0x370);
700 SET_EBDA(ata.channels[1].irq,15);
701#endif
702#if CONFIG_MAX_ATA_INTERFACES > 2
703 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
704 SET_EBDA(ata.channels[2].iobase1,0x1e8);
705 SET_EBDA(ata.channels[2].iobase2,0x3e0);
706 SET_EBDA(ata.channels[2].irq,12);
707#endif
708#if CONFIG_MAX_ATA_INTERFACES > 3
709 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
710 SET_EBDA(ata.channels[3].iobase1,0x168);
711 SET_EBDA(ata.channels[3].iobase2,0x360);
712 SET_EBDA(ata.channels[3].irq,11);
713#endif
714#if CONFIG_MAX_ATA_INTERFACES > 4
715#error Please fill the ATA interface informations
716#endif
717
718 // Device detection
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400719 u8 devid;
720 for(devid=0; devid<CONFIG_MAX_ATA_DEVICES; devid++) {
721 u8 channel = devid / 2;
722 u8 slave = devid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500723
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400724 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
725 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500726
727 // Disable interrupts
728 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
729
730 // Look for device
731 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
732 outb(0x55, iobase1+ATA_CB_SC);
733 outb(0xaa, iobase1+ATA_CB_SN);
734 outb(0xaa, iobase1+ATA_CB_SC);
735 outb(0x55, iobase1+ATA_CB_SN);
736 outb(0x55, iobase1+ATA_CB_SC);
737 outb(0xaa, iobase1+ATA_CB_SN);
738
739 // If we found something
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400740 u8 sc = inb(iobase1+ATA_CB_SC);
741 u8 sn = inb(iobase1+ATA_CB_SN);
742
743 if (sc != 0x55 || sn != 0xaa)
744 continue;
745
746 // reset the channel
747 ata_reset(devid);
748
749 // check for ATA or ATAPI
750 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500751 sc = inb(iobase1+ATA_CB_SC);
752 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400753 if (sc!=0x01 || sn!=0x01) {
754 init_drive_unknown(devid);
755 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500756 }
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400757 u8 cl = inb(iobase1+ATA_CB_CL);
758 u8 ch = inb(iobase1+ATA_CB_CH);
759 u8 st = inb(iobase1+ATA_CB_STAT);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500760
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400761 if (cl==0x14 && ch==0xeb)
762 init_drive_atapi(devid);
763 else if (cl==0x00 && ch==0x00 && st!=0x00)
764 init_drive_ata(devid);
765 else if (cl==0xff && ch==0xff)
766 // None
767 continue;
768 else
769 init_drive_unknown(devid);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500770 }
771
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400772 // Store the device count
773 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500774
775 printf("\n");
776
777 // FIXME : should use bios=cmos|auto|disable bits
778 // FIXME : should know about translation bits
779 // FIXME : move hard_drive_post here
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500780}