blob: 78701f6dce1c2beccc3cd16a2e016ce400205dec [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
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
141u16
Kevin O'Connor3a049632008-03-11 11:48:04 -0400142ata_cmd_data(u16 device, u16 command, u16 count, u16 cylinder
143 , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500144{
Kevin O'Connor3a049632008-03-11 11:48:04 -0400145 DEBUGF("ata_cmd_data d=%d cmd=%d count=%d c=%d h=%d s=%d"
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500146 " lba=%d seg=%x off=%x\n"
147 , device, command, count, cylinder, head, sector
148 , lba, segment, offset);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500149
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500150 u8 channel = device / 2;
151 u8 slave = device % 2;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500152
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500153 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
154 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
155 u8 mode = GET_EBDA(ata.devices[device].mode);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500156
157 // Reset count of transferred data
158 SET_EBDA(ata.trsfsectors,0);
159 SET_EBDA(ata.trsfbytes,0L);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500160
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500161 u8 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500162 if (status & ATA_CB_STAT_BSY)
163 return 1;
164
165 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
166
167 // sector will be 0 only on lba access. Convert to lba-chs
168 if (sector == 0) {
169 if ((count >= 1 << 8) || (lba + count >= 1UL << 28)) {
170 outb(0x00, iobase1 + ATA_CB_FR);
171 outb((count >> 8) & 0xff, iobase1 + ATA_CB_SC);
172 outb(lba >> 24, iobase1 + ATA_CB_SN);
173 outb(0, iobase1 + ATA_CB_CL);
174 outb(0, iobase1 + ATA_CB_CH);
175 command |= 0x04;
176 count &= (1UL << 8) - 1;
177 lba &= (1UL << 24) - 1;
178 }
179 sector = (u16) (lba & 0x000000ffL);
180 cylinder = (u16) ((lba>>8) & 0x0000ffffL);
181 head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
182 }
183
184 outb(0x00, iobase1 + ATA_CB_FR);
185 outb(count, iobase1 + ATA_CB_SC);
186 outb(sector, iobase1 + ATA_CB_SN);
187 outb(cylinder & 0x00ff, iobase1 + ATA_CB_CL);
188 outb(cylinder >> 8, iobase1 + ATA_CB_CH);
189 outb((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (u8) head
190 , iobase1 + ATA_CB_DH);
191 outb(command, iobase1 + ATA_CB_CMD);
192
193 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
194 status = inb(iobase1 + ATA_CB_STAT);
195
196 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400197 DEBUGF("ata_cmd_data : read error\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500198 return 2;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400199 }
200 if (!(status & ATA_CB_STAT_DRQ)) {
201 DEBUGF("ata_cmd_data : DRQ not set (status %02x)\n"
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500202 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500203 return 3;
204 }
205
206 // FIXME : move seg/off translation here
207
208 irq_enable();
209
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400210 u8 current = 0;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500211 while (1) {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500212 if (offset > 0xf800) {
213 offset -= 0x800;
214 segment += 0x80;
215 }
216
Kevin O'Connor3a049632008-03-11 11:48:04 -0400217 if (command == ATA_CMD_WRITE_SECTORS) {
218 // Write data to controller
219 if (mode == ATA_MODE_PIO32)
220 outsl_seg(iobase1, segment, offset, 512 / 4);
221 else
222 outsw_seg(iobase1, segment, offset, 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
225 if (mode == ATA_MODE_PIO32)
226 insl_seg(iobase1, segment, offset, 512 / 4);
227 else
228 insw_seg(iobase1, segment, offset, 512 / 2);
229 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
230 }
231 offset += 512;
232
233 current++;
234 SET_EBDA(ata.trsfsectors,current);
235 count--;
236 status = inb(iobase1 + ATA_CB_STAT);
237 if (count == 0)
238 break;
239 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
240 | ATA_CB_STAT_ERR);
241 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
242 DEBUGF("ata_cmd_data : more sectors left (status %02x)\n"
243 , (unsigned) status);
244 return 5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500245 }
246 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400247
248 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
249 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
250 if (command != ATA_CMD_WRITE_SECTORS)
251 status &= ~ATA_CB_STAT_DF;
252 if (status != ATA_CB_STAT_RDY ) {
253 DEBUGF("ata_cmd_data : no sectors left (status %02x)\n"
254 , (unsigned) status);
255 return 4;
256 }
257
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500258 // Enable interrupts
259 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400260 irq_disable();
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500261 return 0;
262}
263
264// ---------------------------------------------------------------------------
265// ATA/ATAPI driver : execute a packet command
266// ---------------------------------------------------------------------------
267 // returns
268 // 0 : no error
269 // 1 : error in parameters
270 // 2 : BUSY bit set
271 // 3 : error
272 // 4 : not ready
273u16
Kevin O'Connor180a9592008-03-04 22:50:53 -0500274ata_cmd_packet(u16 device, u8 *cmdbuf, u8 cmdlen, u16 header
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500275 , u32 length, u8 inout, u16 bufseg, u16 bufoff)
276{
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400277 DEBUGF("ata_cmd_packet d=%d cmdlen=%d h=%d l=%d inout=%d"
278 " seg=%x off=%x\n"
279 , device, cmdlen, header, length, inout
280 , bufseg, bufoff);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500281
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400282 u8 channel = device / 2;
283 u8 slave = device % 2;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500284
285 // Data out is not supported yet
286 if (inout == ATA_DATA_OUT) {
287 BX_INFO("ata_cmd_packet: DATA_OUT not supported yet\n");
288 return 1;
289 }
290
291 // The header length must be even
292 if (header & 1) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500293 DEBUGF("ata_cmd_packet : header must be even (%04x)\n", header);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500294 return 1;
295 }
296
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400297 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
298 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
299 u8 mode = GET_EBDA(ata.devices[device].mode);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500300
301 if (cmdlen < 12)
302 cmdlen=12;
303 if (cmdlen > 12)
304 cmdlen=16;
305 cmdlen>>=1;
306
307 // Reset count of transferred data
308 SET_EBDA(ata.trsfsectors,0);
309 SET_EBDA(ata.trsfbytes,0L);
310
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400311 u8 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500312 if (status & ATA_CB_STAT_BSY)
313 return 2;
314
315 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
316 outb(0x00, iobase1 + ATA_CB_FR);
317 outb(0x00, iobase1 + ATA_CB_SC);
318 outb(0x00, iobase1 + ATA_CB_SN);
319 outb(0xfff0 & 0x00ff, iobase1 + ATA_CB_CL);
320 outb(0xfff0 >> 8, iobase1 + ATA_CB_CH);
321 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
322 outb(ATA_CMD_PACKET, iobase1 + ATA_CB_CMD);
323
324 // Device should ok to receive command
325 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
326 status = inb(iobase1 + ATA_CB_STAT);
327
328 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500329 DEBUGF("ata_cmd_packet : error, status is %02x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500330 return 3;
331 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500332 DEBUGF("ata_cmd_packet : DRQ not set (status %02x)\n"
333 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500334 return 4;
335 }
336
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500337 // Send command to device
338 irq_enable();
339
Kevin O'Connor843a62c2008-03-09 00:59:58 -0500340 outsw_seg(iobase1, GET_SEG(SS), (u32)cmdbuf, cmdlen);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500341
342 if (inout == ATA_DATA_NO) {
343 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
344 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500345 } else {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500346 u16 loops = 0;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500347 while (1) {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500348 if (loops == 0) {//first time through
349 status = inb(iobase2 + ATA_CB_ASTAT);
350 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500351 } else
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500352 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
353 loops++;
354
355 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400356 inb(iobase1 + ATA_CB_SC);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500357
358 // Check if command completed
359 if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
360 ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY))
361 break;
362
363 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500364 DEBUGF("ata_cmd_packet : error (status %02x)\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500365 return 3;
366 }
367
368 // Normalize address
369 bufseg += (bufoff / 16);
370 bufoff %= 16;
371
372 // Get the byte count
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400373 u16 lcount = (((u16)(inb(iobase1 + ATA_CB_CH))<<8)
374 + inb(iobase1 + ATA_CB_CL));
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500375
376 // adjust to read what we want
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400377 u16 lbefore, lafter;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500378 if (header > lcount) {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500379 lbefore=lcount;
380 header-=lcount;
381 lcount=0;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500382 } else {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500383 lbefore=header;
384 header=0;
385 lcount-=lbefore;
386 }
387
Kevin O'Connor180a9592008-03-04 22:50:53 -0500388 if (lcount > length) {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500389 lafter=lcount-length;
390 lcount=length;
391 length=0;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500392 } else {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500393 lafter=0;
394 length-=lcount;
395 }
396
397 // Save byte count
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400398 u16 count = lcount;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500399
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500400 DEBUGF("Trying to read %04x bytes (%04x %04x %04x) "
401 , lbefore+lcount+lafter, lbefore, lcount, lafter);
402 DEBUGF("to 0x%04x:0x%04x\n", bufseg, bufoff);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500403
404 // If counts not dividable by 4, use 16bits mode
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400405 u8 lmode = mode;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500406 if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
407 if (lcount & 0x03) lmode=ATA_MODE_PIO16;
408 if (lafter & 0x03) lmode=ATA_MODE_PIO16;
409
410 // adds an extra byte if count are odd. before is always even
411 if (lcount & 0x01) {
412 lcount+=1;
413 if ((lafter > 0) && (lafter & 0x01)) {
414 lafter-=1;
415 }
416 }
417
418 if (lmode == ATA_MODE_PIO32) {
419 lcount>>=2; lbefore>>=2; lafter>>=2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500420 } else {
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500421 lcount>>=1; lbefore>>=1; lafter>>=1;
422 }
423
424 int i;
425 for (i=0; i<lbefore; i++)
426 if (lmode == ATA_MODE_PIO32)
427 inl(iobase1);
428 else
429 inw(iobase1);
430
431 if (lmode == ATA_MODE_PIO32)
Kevin O'Connor843a62c2008-03-09 00:59:58 -0500432 insl_seg(iobase1, bufseg, bufoff, lcount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500433 else
Kevin O'Connor843a62c2008-03-09 00:59:58 -0500434 insw_seg(iobase1, bufseg, bufoff, lcount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500435
436 for (i=0; i<lafter; i++)
437 if (lmode == ATA_MODE_PIO32)
438 inl(iobase1);
439 else
440 inw(iobase1);
441
442 // Compute new buffer address
443 bufoff += count;
444
445 // Save transferred bytes count
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400446 SET_EBDA(ata.trsfsectors, loops);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500447 }
448 }
449
450 // Final check, device must be ready
451 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
452 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
453 != ATA_CB_STAT_RDY ) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500454 DEBUGF("ata_cmd_packet : not ready (status %02x)\n"
455 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500456 return 4;
457 }
458
459 // Enable interrupts
460 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
461 return 0;
462}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500463
Kevin O'Connor180a9592008-03-04 22:50:53 -0500464u16
465cdrom_read(u16 device, u32 lba, u32 count, u16 segment, u16 offset, u16 skip)
466{
467 u16 sectors = (count + 2048 - 1) / 2048;
468
469 u8 atacmd[12];
470 memset(atacmd, 0, sizeof(atacmd));
471 atacmd[0]=0x28; // READ command
472 atacmd[7]=(sectors & 0xff00) >> 8; // Sectors
473 atacmd[8]=(sectors & 0x00ff); // Sectors
474 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
475 atacmd[3]=(lba & 0x00ff0000) >> 16;
476 atacmd[4]=(lba & 0x0000ff00) >> 8;
477 atacmd[5]=(lba & 0x000000ff);
478
479 return ata_cmd_packet(device, atacmd, sizeof(atacmd)
480 , skip, count, ATA_DATA_IN
481 , segment, offset);
482}
483
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500484// ---------------------------------------------------------------------------
485// ATA/ATAPI driver : device detection
486// ---------------------------------------------------------------------------
487
488void
489ata_detect()
490{
491 u8 hdcount, cdcount, device, type;
492 u8 buffer[0x0200];
493 memset(buffer, 0, sizeof(buffer));
494
495#if CONFIG_MAX_ATA_INTERFACES > 0
496 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
497 SET_EBDA(ata.channels[0].iobase1,0x1f0);
498 SET_EBDA(ata.channels[0].iobase2,0x3f0);
499 SET_EBDA(ata.channels[0].irq,14);
500#endif
501#if CONFIG_MAX_ATA_INTERFACES > 1
502 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
503 SET_EBDA(ata.channels[1].iobase1,0x170);
504 SET_EBDA(ata.channels[1].iobase2,0x370);
505 SET_EBDA(ata.channels[1].irq,15);
506#endif
507#if CONFIG_MAX_ATA_INTERFACES > 2
508 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
509 SET_EBDA(ata.channels[2].iobase1,0x1e8);
510 SET_EBDA(ata.channels[2].iobase2,0x3e0);
511 SET_EBDA(ata.channels[2].irq,12);
512#endif
513#if CONFIG_MAX_ATA_INTERFACES > 3
514 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
515 SET_EBDA(ata.channels[3].iobase1,0x168);
516 SET_EBDA(ata.channels[3].iobase2,0x360);
517 SET_EBDA(ata.channels[3].irq,11);
518#endif
519#if CONFIG_MAX_ATA_INTERFACES > 4
520#error Please fill the ATA interface informations
521#endif
522
523 // Device detection
524 hdcount=cdcount=0;
525
526 for(device=0; device<CONFIG_MAX_ATA_DEVICES; device++) {
527 u16 iobase1, iobase2;
528 u8 channel, slave, shift;
529 u8 sc, sn, cl, ch, st;
530
531 channel = device / 2;
532 slave = device % 2;
533
534 iobase1 =GET_EBDA(ata.channels[channel].iobase1);
535 iobase2 =GET_EBDA(ata.channels[channel].iobase2);
536
537 // Disable interrupts
538 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
539
540 // Look for device
541 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
542 outb(0x55, iobase1+ATA_CB_SC);
543 outb(0xaa, iobase1+ATA_CB_SN);
544 outb(0xaa, iobase1+ATA_CB_SC);
545 outb(0x55, iobase1+ATA_CB_SN);
546 outb(0x55, iobase1+ATA_CB_SC);
547 outb(0xaa, iobase1+ATA_CB_SN);
548
549 // If we found something
550 sc = inb(iobase1+ATA_CB_SC);
551 sn = inb(iobase1+ATA_CB_SN);
552
553 if ( (sc == 0x55) && (sn == 0xaa) ) {
554 SET_EBDA(ata.devices[device].type,ATA_TYPE_UNKNOWN);
555
556 // reset the channel
557 ata_reset(device);
558
559 // check for ATA or ATAPI
560 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
561 sc = inb(iobase1+ATA_CB_SC);
562 sn = inb(iobase1+ATA_CB_SN);
563 if ((sc==0x01) && (sn==0x01)) {
564 cl = inb(iobase1+ATA_CB_CL);
565 ch = inb(iobase1+ATA_CB_CH);
566 st = inb(iobase1+ATA_CB_STAT);
567
568 if ((cl==0x14) && (ch==0xeb)) {
569 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATAPI);
570 } else if ((cl==0x00) && (ch==0x00) && (st!=0x00)) {
571 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATA);
572 } else if ((cl==0xff) && (ch==0xff)) {
573 SET_EBDA(ata.devices[device].type,ATA_TYPE_NONE);
574 }
575 }
576 }
577
578 type=GET_EBDA(ata.devices[device].type);
579
580 // Now we send a IDENTIFY command to ATA device
581 if(type == ATA_TYPE_ATA) {
582 u32 sectors;
583 u16 cylinders, heads, spt, blksize;
584 u8 translation, removable, mode;
585
586 //Temporary values to do the transfer
587 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
588 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
589
Kevin O'Connor3a049632008-03-11 11:48:04 -0400590 u16 ret = ata_cmd_data(device,ATA_CMD_IDENTIFY_DEVICE
591 , 1, 0, 0, 0, 0L
592 , GET_SEG(SS), (u32)buffer);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500593 if (ret)
594 BX_PANIC("ata-detect: Failed to detect ATA device\n");
595
596 removable = (buffer[0] & 0x80) ? 1 : 0;
597 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
598 blksize = *(u16*)&buffer[10];
599
600 cylinders = *(u16*)&buffer[1*2]; // word 1
601 heads = *(u16*)&buffer[3*2]; // word 3
602 spt = *(u16*)&buffer[6*2]; // word 6
603
604 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
605
606 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
607 SET_EBDA(ata.devices[device].removable, removable);
608 SET_EBDA(ata.devices[device].mode, mode);
609 SET_EBDA(ata.devices[device].blksize, blksize);
610 SET_EBDA(ata.devices[device].pchs.heads, heads);
611 SET_EBDA(ata.devices[device].pchs.cylinders, cylinders);
612 SET_EBDA(ata.devices[device].pchs.spt, spt);
613 SET_EBDA(ata.devices[device].sectors, sectors);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400614 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation="
615 , channel, slave,cylinders, heads, spt);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500616
617 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
618 for (shift=device%4; shift>0; shift--)
619 translation >>= 2;
620 translation &= 0x03;
621
622 SET_EBDA(ata.devices[device].translation, translation);
623
624 switch (translation) {
625 case ATA_TRANSLATION_NONE:
626 BX_INFO("none");
627 break;
628 case ATA_TRANSLATION_LBA:
629 BX_INFO("lba");
630 break;
631 case ATA_TRANSLATION_LARGE:
632 BX_INFO("large");
633 break;
634 case ATA_TRANSLATION_RECHS:
635 BX_INFO("r-echs");
636 break;
637 }
638 switch (translation) {
639 case ATA_TRANSLATION_NONE:
640 break;
641 case ATA_TRANSLATION_LBA:
642 spt = 63;
643 sectors /= 63;
644 heads = sectors / 1024;
645 if (heads>128) heads = 255;
646 else if (heads>64) heads = 128;
647 else if (heads>32) heads = 64;
648 else if (heads>16) heads = 32;
649 else heads=16;
650 cylinders = sectors / heads;
651 break;
652 case ATA_TRANSLATION_RECHS:
653 // Take care not to overflow
654 if (heads==16) {
655 if(cylinders>61439) cylinders=61439;
656 heads=15;
657 cylinders = (u16)((u32)(cylinders)*16/15);
658 }
659 // then go through the large bitshift process
660 case ATA_TRANSLATION_LARGE:
661 while(cylinders > 1024) {
662 cylinders >>= 1;
663 heads <<= 1;
664
665 // If we max out the head count
666 if (heads > 127) break;
667 }
668 break;
669 }
670 // clip to 1024 cylinders in lchs
671 if (cylinders > 1024)
672 cylinders=1024;
673 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
674
675 SET_EBDA(ata.devices[device].lchs.heads, heads);
676 SET_EBDA(ata.devices[device].lchs.cylinders, cylinders);
677 SET_EBDA(ata.devices[device].lchs.spt, spt);
678
679 // fill hdidmap
Kevin O'Connor180a9592008-03-04 22:50:53 -0500680 SET_EBDA(ata.idmap[0][hdcount], device);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500681 hdcount++;
682 }
683
684 // Now we send a IDENTIFY command to ATAPI device
685 if(type == ATA_TYPE_ATAPI) {
686
687 u8 type, removable, mode;
688 u16 blksize;
689
690 //Temporary values to do the transfer
691 SET_EBDA(ata.devices[device].device,ATA_DEVICE_CDROM);
692 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
693
Kevin O'Connor3a049632008-03-11 11:48:04 -0400694 u16 ret = ata_cmd_data(device,ATA_CMD_IDENTIFY_DEVICE_PACKET
695 , 1, 0, 0, 0, 0L
696 , GET_SEG(SS), (u32)buffer);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500697 if (ret != 0)
698 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
699
700 type = buffer[1] & 0x1f;
701 removable = (buffer[0] & 0x80) ? 1 : 0;
702 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
703 blksize = 2048;
704
705 SET_EBDA(ata.devices[device].device, type);
706 SET_EBDA(ata.devices[device].removable, removable);
707 SET_EBDA(ata.devices[device].mode, mode);
708 SET_EBDA(ata.devices[device].blksize, blksize);
709
710 // fill cdidmap
Kevin O'Connor180a9592008-03-04 22:50:53 -0500711 SET_EBDA(ata.idmap[1][cdcount], device);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500712 cdcount++;
713 }
714
715 u32 sizeinmb = 0;
716 u16 ataversion;
717 u8 c, i, version=0, model[41];
718
719 switch (type) {
720 case ATA_TYPE_ATA:
721 sizeinmb = GET_EBDA(ata.devices[device].sectors);
722 sizeinmb >>= 11;
723 case ATA_TYPE_ATAPI:
724 // Read ATA/ATAPI version
725 ataversion=((u16)(buffer[161])<<8) | buffer[160];
726 for(version=15;version>0;version--) {
727 if ((ataversion&(1<<version))!=0)
728 break;
729 }
730
731 // Read model name
732 for (i=0;i<20;i++) {
733 model[i*2] = buffer[(i*2)+54+1];
734 model[(i*2)+1] = buffer[(i*2)+54];
735 }
736
737 // Reformat
738 model[40] = 0x00;
739 for (i=39;i>0;i--) {
740 if (model[i]==0x20)
741 model[i] = 0x00;
742 else
743 break;
744 }
745 break;
746 }
747
748 switch (type) {
749 case ATA_TYPE_ATA:
750 printf("ata%d %s: ",channel,slave?" slave":"master");
751 i=0;
752 while ((c=model[i++]))
753 printf("%c",c);
754 if (sizeinmb < (1UL<<16))
755 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u16)sizeinmb);
756 else
757 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, (u16)(sizeinmb>>10));
758 break;
759 case ATA_TYPE_ATAPI:
760 printf("ata%d %s: ",channel,slave?" slave":"master");
761 i=0;
762 while ((c=model[i++]))
763 printf("%c",c);
764 if (GET_EBDA(ata.devices[device].device)==ATA_DEVICE_CDROM)
765 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
766 else
767 printf(" ATAPI-%d Device\n",version);
768 break;
769 case ATA_TYPE_UNKNOWN:
770 printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
771 break;
772 }
773 }
774
775 // Store the devices counts
776 SET_EBDA(ata.hdcount, hdcount);
777 SET_EBDA(ata.cdcount, cdcount);
778 SET_BDA(disk_count, hdcount);
779
780 printf("\n");
781
782 // FIXME : should use bios=cmos|auto|disable bits
783 // FIXME : should know about translation bits
784 // FIXME : move hard_drive_post here
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500785}