blob: 2e44d41c36a8ea95c4d56641002ea733fe8840c5 [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'Connor3491e8b2008-02-29 00:22:27 -0500208
209 irq_enable();
210
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400211 u8 current = 0;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400212 u16 count = cmd->sector_count;
213 u8 status;
Kevin O'Connorefde6092008-03-12 20:33:15 -0400214 void *far_buffer = cmd->far_buffer;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400215 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400216 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400217 // Write data to controller
Kevin O'Connorefde6092008-03-12 20:33:15 -0400218 DEBUGF("Write sector id=%d dest=%p\n", biosid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400219 if (mode == ATA_MODE_PIO32)
Kevin O'Connorefde6092008-03-12 20:33:15 -0400220 outsl_far(iobase1, far_buffer, 512 / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400221 else
Kevin O'Connorefde6092008-03-12 20:33:15 -0400222 outsw_far(iobase1, far_buffer, 512 / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500223 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400224 // Read data from controller
Kevin O'Connorefde6092008-03-12 20:33:15 -0400225 DEBUGF("Read sector id=%d dest=%p\n", biosid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400226 if (mode == ATA_MODE_PIO32)
Kevin O'Connorefde6092008-03-12 20:33:15 -0400227 insl_far(iobase1, far_buffer, 512 / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400228 else
Kevin O'Connorefde6092008-03-12 20:33:15 -0400229 insw_far(iobase1, far_buffer, 512 / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400230 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
231 }
Kevin O'Connorefde6092008-03-12 20:33:15 -0400232 far_buffer += 512;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400233
234 current++;
235 SET_EBDA(ata.trsfsectors,current);
236 count--;
237 status = inb(iobase1 + ATA_CB_STAT);
238 if (count == 0)
239 break;
240 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
241 | ATA_CB_STAT_ERR);
242 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400243 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400244 , (unsigned) status);
245 return 5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500246 }
247 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400248
249 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
250 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400251 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400252 status &= ~ATA_CB_STAT_DF;
253 if (status != ATA_CB_STAT_RDY ) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400254 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400255 , (unsigned) status);
256 return 4;
257 }
258
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400259 irq_disable();
260
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500261 // Enable interrupts
262 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
263 return 0;
264}
265
266// ---------------------------------------------------------------------------
267// ATA/ATAPI driver : execute a packet command
268// ---------------------------------------------------------------------------
269 // returns
270 // 0 : no error
271 // 1 : error in parameters
272 // 2 : BUSY bit set
273 // 3 : error
274 // 4 : not ready
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400275int
276ata_cmd_packet(u16 biosid, u8 *cmdbuf, u8 cmdlen
Kevin O'Connorefde6092008-03-12 20:33:15 -0400277 , u16 header, u32 length, void *far_buffer)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500278{
Kevin O'Connorefde6092008-03-12 20:33:15 -0400279 DEBUGF("ata_cmd_packet d=%d cmdlen=%d h=%d l=%d buf=%p\n"
280 , biosid, cmdlen, header, length, far_buffer);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500281
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400282 u8 channel = biosid / 2;
283 u8 slave = biosid % 2;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500284
285 // The header length must be even
286 if (header & 1) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500287 DEBUGF("ata_cmd_packet : header must be even (%04x)\n", header);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500288 return 1;
289 }
290
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400291 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
292 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400293 u8 mode = GET_EBDA(ata.devices[biosid].mode);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500294
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400295 struct ata_pio_command cmd;
296 cmd.sector_count = 0;
297 cmd.feature = 0;
298 cmd.lba_low = 0;
299 cmd.lba_mid = 0xf0;
300 cmd.lba_high = 0xff;
301 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
302 cmd.command = ATA_CMD_PACKET;
303
304 cmd.biosid = biosid;
305 int ret = send_cmd(&cmd);
306 if (ret)
307 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500308
309 // Reset count of transferred data
310 SET_EBDA(ata.trsfsectors,0);
311 SET_EBDA(ata.trsfbytes,0L);
312
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500313 irq_enable();
314
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400315 // Send command to device
Kevin O'Connorefde6092008-03-12 20:33:15 -0400316 outsw_far(iobase1, MAKE_32_PTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500317
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400318 u8 status;
319 u16 loops = 0;
320 for (;;) {
321 if (loops == 0) {//first time through
322 status = inb(iobase2 + ATA_CB_ASTAT);
323 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
324 } else
325 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
326 loops++;
327
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500328 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400329 inb(iobase1 + ATA_CB_SC);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500330
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400331 // Check if command completed
332 if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
333 ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY))
334 break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500335
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400336 if (status & ATA_CB_STAT_ERR) {
337 DEBUGF("ata_cmd_packet : error (status %02x)\n", status);
338 return 3;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500339 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400340
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400341 // Get the byte count
342 u16 lcount = (((u16)(inb(iobase1 + ATA_CB_CH))<<8)
343 + inb(iobase1 + ATA_CB_CL));
344
345 // adjust to read what we want
346 u16 lbefore, lafter;
347 if (header > lcount) {
348 lbefore=lcount;
349 header-=lcount;
350 lcount=0;
351 } else {
352 lbefore=header;
353 header=0;
354 lcount-=lbefore;
355 }
356
357 if (lcount > length) {
358 lafter=lcount-length;
359 lcount=length;
360 length=0;
361 } else {
362 lafter=0;
363 length-=lcount;
364 }
365
366 // Save byte count
367 u16 count = lcount;
368
Kevin O'Connorefde6092008-03-12 20:33:15 -0400369 DEBUGF("Trying to read %04x bytes (%04x %04x %04x) to %p\n"
370 , lbefore+lcount+lafter, lbefore, lcount, lafter, far_buffer);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400371
372 // If counts not dividable by 4, use 16bits mode
373 u8 lmode = mode;
374 if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
375 if (lcount & 0x03) lmode=ATA_MODE_PIO16;
376 if (lafter & 0x03) lmode=ATA_MODE_PIO16;
377
378 // adds an extra byte if count are odd. before is always even
379 if (lcount & 0x01) {
380 lcount+=1;
381 if ((lafter > 0) && (lafter & 0x01)) {
382 lafter-=1;
383 }
384 }
385
386 if (lmode == ATA_MODE_PIO32) {
387 lcount>>=2; lbefore>>=2; lafter>>=2;
388 } else {
389 lcount>>=1; lbefore>>=1; lafter>>=1;
390 }
391
392 int i;
393 for (i=0; i<lbefore; i++)
394 if (lmode == ATA_MODE_PIO32)
395 inl(iobase1);
396 else
397 inw(iobase1);
398
399 if (lmode == ATA_MODE_PIO32)
Kevin O'Connorefde6092008-03-12 20:33:15 -0400400 insl_far(iobase1, far_buffer, lcount);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400401 else
Kevin O'Connorefde6092008-03-12 20:33:15 -0400402 insw_far(iobase1, far_buffer, lcount);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400403
404 for (i=0; i<lafter; i++)
405 if (lmode == ATA_MODE_PIO32)
406 inl(iobase1);
407 else
408 inw(iobase1);
409
410 // Compute new buffer address
Kevin O'Connorefde6092008-03-12 20:33:15 -0400411 far_buffer += count;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400412
413 // Save transferred bytes count
414 SET_EBDA(ata.trsfsectors, loops);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500415 }
416
417 // Final check, device must be ready
418 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
419 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
420 != ATA_CB_STAT_RDY ) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500421 DEBUGF("ata_cmd_packet : not ready (status %02x)\n"
422 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500423 return 4;
424 }
425
426 // Enable interrupts
427 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
428 return 0;
429}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500430
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400431int
Kevin O'Connorefde6092008-03-12 20:33:15 -0400432cdrom_read(u16 biosid, u32 lba, u32 count, void *far_buffer, u16 skip)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500433{
434 u16 sectors = (count + 2048 - 1) / 2048;
435
436 u8 atacmd[12];
437 memset(atacmd, 0, sizeof(atacmd));
438 atacmd[0]=0x28; // READ command
439 atacmd[7]=(sectors & 0xff00) >> 8; // Sectors
440 atacmd[8]=(sectors & 0x00ff); // Sectors
441 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
442 atacmd[3]=(lba & 0x00ff0000) >> 16;
443 atacmd[4]=(lba & 0x0000ff00) >> 8;
444 atacmd[5]=(lba & 0x000000ff);
445
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400446 return ata_cmd_packet(biosid, atacmd, sizeof(atacmd)
Kevin O'Connorefde6092008-03-12 20:33:15 -0400447 , skip, count, far_buffer);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500448}
449
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500450// ---------------------------------------------------------------------------
451// ATA/ATAPI driver : device detection
452// ---------------------------------------------------------------------------
453
454void
455ata_detect()
456{
457 u8 hdcount, cdcount, device, type;
458 u8 buffer[0x0200];
459 memset(buffer, 0, sizeof(buffer));
460
461#if CONFIG_MAX_ATA_INTERFACES > 0
462 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
463 SET_EBDA(ata.channels[0].iobase1,0x1f0);
464 SET_EBDA(ata.channels[0].iobase2,0x3f0);
465 SET_EBDA(ata.channels[0].irq,14);
466#endif
467#if CONFIG_MAX_ATA_INTERFACES > 1
468 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
469 SET_EBDA(ata.channels[1].iobase1,0x170);
470 SET_EBDA(ata.channels[1].iobase2,0x370);
471 SET_EBDA(ata.channels[1].irq,15);
472#endif
473#if CONFIG_MAX_ATA_INTERFACES > 2
474 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
475 SET_EBDA(ata.channels[2].iobase1,0x1e8);
476 SET_EBDA(ata.channels[2].iobase2,0x3e0);
477 SET_EBDA(ata.channels[2].irq,12);
478#endif
479#if CONFIG_MAX_ATA_INTERFACES > 3
480 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
481 SET_EBDA(ata.channels[3].iobase1,0x168);
482 SET_EBDA(ata.channels[3].iobase2,0x360);
483 SET_EBDA(ata.channels[3].irq,11);
484#endif
485#if CONFIG_MAX_ATA_INTERFACES > 4
486#error Please fill the ATA interface informations
487#endif
488
489 // Device detection
490 hdcount=cdcount=0;
491
492 for(device=0; device<CONFIG_MAX_ATA_DEVICES; device++) {
493 u16 iobase1, iobase2;
494 u8 channel, slave, shift;
495 u8 sc, sn, cl, ch, st;
496
497 channel = device / 2;
498 slave = device % 2;
499
500 iobase1 =GET_EBDA(ata.channels[channel].iobase1);
501 iobase2 =GET_EBDA(ata.channels[channel].iobase2);
502
503 // Disable interrupts
504 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
505
506 // Look for device
507 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
508 outb(0x55, iobase1+ATA_CB_SC);
509 outb(0xaa, iobase1+ATA_CB_SN);
510 outb(0xaa, iobase1+ATA_CB_SC);
511 outb(0x55, iobase1+ATA_CB_SN);
512 outb(0x55, iobase1+ATA_CB_SC);
513 outb(0xaa, iobase1+ATA_CB_SN);
514
515 // If we found something
516 sc = inb(iobase1+ATA_CB_SC);
517 sn = inb(iobase1+ATA_CB_SN);
518
519 if ( (sc == 0x55) && (sn == 0xaa) ) {
520 SET_EBDA(ata.devices[device].type,ATA_TYPE_UNKNOWN);
521
522 // reset the channel
523 ata_reset(device);
524
525 // check for ATA or ATAPI
526 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
527 sc = inb(iobase1+ATA_CB_SC);
528 sn = inb(iobase1+ATA_CB_SN);
529 if ((sc==0x01) && (sn==0x01)) {
530 cl = inb(iobase1+ATA_CB_CL);
531 ch = inb(iobase1+ATA_CB_CH);
532 st = inb(iobase1+ATA_CB_STAT);
533
534 if ((cl==0x14) && (ch==0xeb)) {
535 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATAPI);
536 } else if ((cl==0x00) && (ch==0x00) && (st!=0x00)) {
537 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATA);
538 } else if ((cl==0xff) && (ch==0xff)) {
539 SET_EBDA(ata.devices[device].type,ATA_TYPE_NONE);
540 }
541 }
542 }
543
544 type=GET_EBDA(ata.devices[device].type);
545
546 // Now we send a IDENTIFY command to ATA device
547 if(type == ATA_TYPE_ATA) {
548 u32 sectors;
549 u16 cylinders, heads, spt, blksize;
550 u8 translation, removable, mode;
551
552 //Temporary values to do the transfer
553 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
554 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
555
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400556 u16 ret = ata_cmd_data_chs(device, ATA_CMD_IDENTIFY_DEVICE
557 , 0, 0, 1, 1
Kevin O'Connorefde6092008-03-12 20:33:15 -0400558 , MAKE_32_PTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500559 if (ret)
560 BX_PANIC("ata-detect: Failed to detect ATA device\n");
561
562 removable = (buffer[0] & 0x80) ? 1 : 0;
563 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
564 blksize = *(u16*)&buffer[10];
565
566 cylinders = *(u16*)&buffer[1*2]; // word 1
567 heads = *(u16*)&buffer[3*2]; // word 3
568 spt = *(u16*)&buffer[6*2]; // word 6
569
570 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
571
572 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
573 SET_EBDA(ata.devices[device].removable, removable);
574 SET_EBDA(ata.devices[device].mode, mode);
575 SET_EBDA(ata.devices[device].blksize, blksize);
576 SET_EBDA(ata.devices[device].pchs.heads, heads);
577 SET_EBDA(ata.devices[device].pchs.cylinders, cylinders);
578 SET_EBDA(ata.devices[device].pchs.spt, spt);
579 SET_EBDA(ata.devices[device].sectors, sectors);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400580 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation="
581 , channel, slave,cylinders, heads, spt);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500582
583 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
584 for (shift=device%4; shift>0; shift--)
585 translation >>= 2;
586 translation &= 0x03;
587
588 SET_EBDA(ata.devices[device].translation, translation);
589
590 switch (translation) {
591 case ATA_TRANSLATION_NONE:
592 BX_INFO("none");
593 break;
594 case ATA_TRANSLATION_LBA:
595 BX_INFO("lba");
596 break;
597 case ATA_TRANSLATION_LARGE:
598 BX_INFO("large");
599 break;
600 case ATA_TRANSLATION_RECHS:
601 BX_INFO("r-echs");
602 break;
603 }
604 switch (translation) {
605 case ATA_TRANSLATION_NONE:
606 break;
607 case ATA_TRANSLATION_LBA:
608 spt = 63;
609 sectors /= 63;
610 heads = sectors / 1024;
611 if (heads>128) heads = 255;
612 else if (heads>64) heads = 128;
613 else if (heads>32) heads = 64;
614 else if (heads>16) heads = 32;
615 else heads=16;
616 cylinders = sectors / heads;
617 break;
618 case ATA_TRANSLATION_RECHS:
619 // Take care not to overflow
620 if (heads==16) {
621 if(cylinders>61439) cylinders=61439;
622 heads=15;
623 cylinders = (u16)((u32)(cylinders)*16/15);
624 }
625 // then go through the large bitshift process
626 case ATA_TRANSLATION_LARGE:
627 while(cylinders > 1024) {
628 cylinders >>= 1;
629 heads <<= 1;
630
631 // If we max out the head count
632 if (heads > 127) break;
633 }
634 break;
635 }
636 // clip to 1024 cylinders in lchs
637 if (cylinders > 1024)
638 cylinders=1024;
639 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
640
641 SET_EBDA(ata.devices[device].lchs.heads, heads);
642 SET_EBDA(ata.devices[device].lchs.cylinders, cylinders);
643 SET_EBDA(ata.devices[device].lchs.spt, spt);
644
645 // fill hdidmap
Kevin O'Connor180a9592008-03-04 22:50:53 -0500646 SET_EBDA(ata.idmap[0][hdcount], device);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500647 hdcount++;
648 }
649
650 // Now we send a IDENTIFY command to ATAPI device
651 if(type == ATA_TYPE_ATAPI) {
652
653 u8 type, removable, mode;
654 u16 blksize;
655
656 //Temporary values to do the transfer
657 SET_EBDA(ata.devices[device].device,ATA_DEVICE_CDROM);
658 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
659
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400660 u16 ret = ata_cmd_data_chs(device, ATA_CMD_IDENTIFY_DEVICE_PACKET
661 , 0, 0, 1, 1
Kevin O'Connorefde6092008-03-12 20:33:15 -0400662 , MAKE_32_PTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500663 if (ret != 0)
664 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
665
666 type = buffer[1] & 0x1f;
667 removable = (buffer[0] & 0x80) ? 1 : 0;
668 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
669 blksize = 2048;
670
671 SET_EBDA(ata.devices[device].device, type);
672 SET_EBDA(ata.devices[device].removable, removable);
673 SET_EBDA(ata.devices[device].mode, mode);
674 SET_EBDA(ata.devices[device].blksize, blksize);
675
676 // fill cdidmap
Kevin O'Connor180a9592008-03-04 22:50:53 -0500677 SET_EBDA(ata.idmap[1][cdcount], device);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500678 cdcount++;
679 }
680
681 u32 sizeinmb = 0;
682 u16 ataversion;
683 u8 c, i, version=0, model[41];
684
685 switch (type) {
686 case ATA_TYPE_ATA:
687 sizeinmb = GET_EBDA(ata.devices[device].sectors);
688 sizeinmb >>= 11;
689 case ATA_TYPE_ATAPI:
690 // Read ATA/ATAPI version
691 ataversion=((u16)(buffer[161])<<8) | buffer[160];
692 for(version=15;version>0;version--) {
693 if ((ataversion&(1<<version))!=0)
694 break;
695 }
696
697 // Read model name
698 for (i=0;i<20;i++) {
699 model[i*2] = buffer[(i*2)+54+1];
700 model[(i*2)+1] = buffer[(i*2)+54];
701 }
702
703 // Reformat
704 model[40] = 0x00;
705 for (i=39;i>0;i--) {
706 if (model[i]==0x20)
707 model[i] = 0x00;
708 else
709 break;
710 }
711 break;
712 }
713
714 switch (type) {
715 case ATA_TYPE_ATA:
716 printf("ata%d %s: ",channel,slave?" slave":"master");
717 i=0;
718 while ((c=model[i++]))
719 printf("%c",c);
720 if (sizeinmb < (1UL<<16))
721 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u16)sizeinmb);
722 else
723 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, (u16)(sizeinmb>>10));
724 break;
725 case ATA_TYPE_ATAPI:
726 printf("ata%d %s: ",channel,slave?" slave":"master");
727 i=0;
728 while ((c=model[i++]))
729 printf("%c",c);
730 if (GET_EBDA(ata.devices[device].device)==ATA_DEVICE_CDROM)
731 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
732 else
733 printf(" ATAPI-%d Device\n",version);
734 break;
735 case ATA_TYPE_UNKNOWN:
736 printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
737 break;
738 }
739 }
740
741 // Store the devices counts
742 SET_EBDA(ata.hdcount, hdcount);
743 SET_EBDA(ata.cdcount, cdcount);
744 SET_BDA(disk_count, hdcount);
745
746 printf("\n");
747
748 // FIXME : should use bios=cmos|auto|disable bits
749 // FIXME : should know about translation bits
750 // FIXME : move hard_drive_post here
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500751}