blob: e8a4a287df3d265673ebbf71763fd25c5fa7e305 [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
Kevin O'Connora6b9f712008-03-29 12:53:57 -040021#define IDE_SECTOR_SIZE 512
22#define CDROM_SECTOR_SIZE 2048
23
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050024#define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
25
Kevin O'Connor127cbd72008-03-08 11:34:28 -050026#define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
27#define DEBUGF(fmt, args...)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050028
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050029
Kevin O'Connora6b9f712008-03-29 12:53:57 -040030/****************************************************************
31 * Helper functions
32 ****************************************************************/
33
34// Wait for the specified ide state
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050035static int
36await_ide(u8 when_done, u16 base, u16 timeout)
37{
Kevin O'Connor1fcf1442008-03-11 19:42:41 -040038 u32 time=0, last=0;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050039 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -040040 u8 status = inb(base+ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050041 time++;
Kevin O'Connor117fc212008-04-13 18:17:02 -040042 u8 result = 0;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050043 if (when_done == BSY)
44 result = status & ATA_CB_STAT_BSY;
45 else if (when_done == NOT_BSY)
46 result = !(status & ATA_CB_STAT_BSY);
47 else if (when_done == NOT_BSY_DRQ)
48 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
49 else if (when_done == NOT_BSY_NOT_DRQ)
50 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
51 else if (when_done == NOT_BSY_RDY)
52 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050053
54 if (result)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040055 return status;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050056 // mod 2048 each 16 ms
57 if (time>>16 != last) {
58 last = time >>16;
Kevin O'Connor127cbd72008-03-08 11:34:28 -050059 DEBUGF("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY)"
60 " %d time= %d timeout= %d\n"
61 , when_done, time>>11, timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050062 }
63 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -050064 DEBUGF("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
65 ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
66 , when_done, time>>11, timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050067 return -1;
68 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -040069 if (timeout == 0 || (time>>11) > timeout)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050070 break;
71 }
72 BX_INFO("IDE time out\n");
73 return -1;
74}
75
Kevin O'Connora6b9f712008-03-29 12:53:57 -040076// Wait for ide state - pauses for one ata cycle first.
Kevin O'Connor74f9c082008-04-13 16:57:16 -040077static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -040078pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout)
79{
80 // Wait one PIO transfer cycle.
81 inb(iobase2 + ATA_CB_ASTAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050082
Kevin O'Connora6b9f712008-03-29 12:53:57 -040083 return await_ide(when_done, iobase1, timeout);
84}
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050085
Kevin O'Connora6b9f712008-03-29 12:53:57 -040086// Delay for x nanoseconds
87static void
88nsleep(u32 delay)
89{
90 // XXX - how to implement ndelay?
91 while (delay--)
92 nop();
93}
94
95// Reset a drive
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050096void
Kevin O'Connora6b9f712008-03-29 12:53:57 -040097ata_reset(int driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050098{
99 u16 iobase1, iobase2;
100 u8 channel, slave, sn, sc;
101 u8 type;
102
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400103 channel = driveid / 2;
104 slave = driveid % 2;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500105
106 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
107 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
108
109 // Reset
110
111 // 8.2.1 (a) -- set SRST in DC
112 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
113
114 // 8.2.1 (b) -- wait for BSY
115 await_ide(BSY, iobase1, 20);
116
117 // 8.2.1 (f) -- clear SRST
118 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
119
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400120 type=GET_EBDA(ata.devices[driveid].type);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500121 if (type != ATA_TYPE_NONE) {
122
123 // 8.2.1 (g) -- check for sc==sn==0x01
124 // select device
125 outb(slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
126 sc = inb(iobase1+ATA_CB_SC);
127 sn = inb(iobase1+ATA_CB_SN);
128
129 if ( (sc==0x01) && (sn==0x01) ) {
130 if (type == ATA_TYPE_ATA) //ATA
131 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
132 else //ATAPI
133 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
134 }
135
136 // 8.2.1 (h) -- wait for not BSY
137 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
138 }
139
140 // Enable interrupts
141 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
142}
143
Kevin O'Connoree55c762008-05-13 00:18:20 -0400144
145/****************************************************************
146 * ATA send command
147 ****************************************************************/
148
149struct ata_op_s {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400150 u64 lba;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400151 void *far_buffer;
152 u16 driveid;
153 u16 count;
154};
155
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400156struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400157 u8 feature;
158 u8 sector_count;
159 u8 lba_low;
160 u8 lba_mid;
161 u8 lba_high;
162 u8 device;
163 u8 command;
164
165 u8 sector_count2;
166 u8 lba_low2;
167 u8 lba_mid2;
168 u8 lba_high2;
169};
170
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400171// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400172static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400173send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500174{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400175 u8 channel = driveid / 2;
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500176 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
177 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500178
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400179 int status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500180 if (status & ATA_CB_STAT_BSY)
181 return 1;
182
183 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400184 if (cmd->command & 0x04) {
185 outb(0x00, iobase1 + ATA_CB_FR);
186 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
187 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
188 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
189 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500190 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400191 outb(cmd->feature, iobase1 + ATA_CB_FR);
192 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
193 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
194 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
195 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
196 outb(cmd->device, iobase1 + ATA_CB_DH);
197 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500198
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400199 nsleep(400);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500200
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400201 status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
202 if (status < 0)
203 return status;
204
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500205 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400206 DEBUGF("send_cmd : read error\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500207 return 2;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400208 }
209 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400210 DEBUGF("send_cmd : DRQ not set (status %02x)\n"
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500211 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500212 return 3;
213 }
214
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400215 return 0;
216}
217
Kevin O'Connoree55c762008-05-13 00:18:20 -0400218
219/****************************************************************
220 * ATA transfers
221 ****************************************************************/
222
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400223// Read and discard x number of bytes from an io channel.
224static void
225insx_discard(int mode, int iobase1, int bytes)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400226{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400227 int count, i;
228 if (mode == ATA_MODE_PIO32) {
229 count = bytes / 4;
230 for (i=0; i<count; i++)
231 inl(iobase1);
232 } else {
233 count = bytes / 2;
234 for (i=0; i<count; i++)
235 inw(iobase1);
236 }
237}
238
239// Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
240// 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
241// and/or last block may be partially transferred. This function is
242// inlined because all the callers use different forms and because the
243// large number of parameters would consume a lot of stack space.
Kevin O'Connor74f9c082008-04-13 16:57:16 -0400244static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400245ata_transfer(int driveid, int iswrite, int count, int blocksize
246 , int skipfirst, int skiplast, void *far_buffer)
247{
248 DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d"
249 " skipf=%d skipl=%d buf=%p\n"
250 , driveid, iswrite, count, blocksize
251 , skipfirst, skiplast, far_buffer);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400252
253 // Reset count of transferred data
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400254 SET_EBDA(ata.trsfsectors, 0);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400255
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400256 u8 channel = driveid / 2;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400257 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
258 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400259 u8 mode = GET_EBDA(ata.devices[driveid].mode);
260 int current = 0;
261 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400262 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400263 int bsize = blocksize;
264 if (skipfirst && current == 0) {
265 insx_discard(mode, iobase1, skipfirst);
266 bsize -= skipfirst;
267 }
268 if (skiplast && current == count-1)
269 bsize -= skiplast;
270
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400271 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400272 // Write data to controller
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400273 DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400274 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400275 outsl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400276 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400277 outsw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500278 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400279 // Read data from controller
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400280 DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400281 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400282 insl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400283 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400284 insw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400285 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400286 far_buffer += bsize;
287
288 if (skiplast && current == count-1)
289 insx_discard(mode, iobase1, skiplast);
290
291 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
292 if (status < 0)
293 // Error
294 return status;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400295
296 current++;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400297 SET_EBDA(ata.trsfsectors, current);
298 if (current == count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400299 break;
300 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
301 | ATA_CB_STAT_ERR);
302 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400303 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400304 , (unsigned) status);
305 return 5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500306 }
307 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400308
309 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
310 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400311 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400312 status &= ~ATA_CB_STAT_DF;
313 if (status != ATA_CB_STAT_RDY ) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400314 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400315 , (unsigned) status);
316 return 4;
317 }
318
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500319 // Enable interrupts
320 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
321 return 0;
322}
323
Kevin O'Connoree55c762008-05-13 00:18:20 -0400324static noinline int
325ata_transfer_disk(const struct ata_op_s *op, int iswrite)
326{
327 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
328 , 0, 0, op->far_buffer);
329}
330
331static noinline int
332ata_transfer_cdrom(const struct ata_op_s *op)
333{
334 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
335 , 0, 0, op->far_buffer);
336}
337
338static noinline int
339ata_transfer_emu(const struct ata_op_s *op, int before, int after)
340{
341 int vcount = op->count * 4 - before - after;
342 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
343 , before*512, after*512, op->far_buffer);
344 if (ret) {
345 SET_EBDA(ata.trsfsectors, 0);
346 return ret;
347 }
348 SET_EBDA(ata.trsfsectors, vcount);
349 return 0;
350}
351
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400352
353/****************************************************************
354 * ATA hard drive functions
355 ****************************************************************/
356
Kevin O'Connoree55c762008-05-13 00:18:20 -0400357static noinline int
358send_cmd_disk(const struct ata_op_s *op, u16 command)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400359{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400360 u8 slave = op->driveid % 2;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400361 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400362
363 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400364 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400365
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400366 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400367 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
368 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400369 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400370 cmd.lba_mid2 = lba >> 32;
371 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400372
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400373 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400374 lba &= 0xffffff;
375 }
376
377 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400378 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400379 cmd.lba_low = lba;
380 cmd.lba_mid = lba >> 8;
381 cmd.lba_high = lba >> 16;
382 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
383 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400384
Kevin O'Connoree55c762008-05-13 00:18:20 -0400385 return send_cmd(op->driveid, &cmd);
386}
387
388// Read/write count blocks from a harddrive.
389__always_inline int
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400390ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
Kevin O'Connoree55c762008-05-13 00:18:20 -0400391{
392 struct ata_op_s op;
393 op.driveid = driveid;
394 op.lba = lba;
395 op.count = count;
396 op.far_buffer = far_buffer;
397
398 int ret = send_cmd_disk(&op, command);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400399 if (ret)
400 return ret;
401
402 int iswrite = command == ATA_CMD_WRITE_SECTORS;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400403 return ata_transfer_disk(&op, iswrite);
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400404}
405
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400406
407/****************************************************************
408 * ATAPI functions
409 ****************************************************************/
410
411// Low-level atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400412static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400413send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500414{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400415 u8 channel = driveid / 2;
416 u8 slave = driveid % 2;
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400417 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
418 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500419
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400420 struct ata_pio_command cmd;
421 cmd.sector_count = 0;
422 cmd.feature = 0;
423 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400424 cmd.lba_mid = blocksize;
425 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400426 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
427 cmd.command = ATA_CMD_PACKET;
428
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400429 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400430 if (ret)
431 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500432
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400433 // Send command to device
Kevin O'Connor070231b2008-03-22 20:44:37 -0400434 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500435
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400436 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
437 if (status < 0)
438 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400439
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500440 return 0;
441}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500442
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400443// Low-level cdrom read atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400444static int
445send_cmd_cdrom(const struct ata_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500446{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400447 u8 atacmd[12];
448 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500449
Kevin O'Connoree55c762008-05-13 00:18:20 -0400450 atacmd[0]=0x28; // READ command
451 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
452 atacmd[8]=(op->count & 0x00ff);
453 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
454 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
455 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
456 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400457
Kevin O'Connoree55c762008-05-13 00:18:20 -0400458 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
459 , CDROM_SECTOR_SIZE);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500460}
461
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400462// Read sectors from the cdrom.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400463__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400464cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400465{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400466 struct ata_op_s op;
467 op.driveid = driveid;
468 op.lba = lba;
469 op.count = count;
470 op.far_buffer = far_buffer;
471
472 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400473 if (ret)
474 return ret;
475
Kevin O'Connoree55c762008-05-13 00:18:20 -0400476 return ata_transfer_cdrom(&op);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400477}
478
479// Pretend the cdrom has 512 byte sectors (instead of 2048) and read
480// sectors.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400481__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400482cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400483{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400484 u32 velba = vlba + vcount - 1;
485 u32 lba = vlba / 4;
486 u32 elba = velba / 4;
487 int count = elba - lba + 1;
488 int before = vlba % 4;
489 int after = 3 - (velba % 4);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400490
Kevin O'Connoree55c762008-05-13 00:18:20 -0400491 struct ata_op_s op;
492 op.driveid = driveid;
493 op.lba = lba;
494 op.count = count;
495 op.far_buffer = far_buffer;
496
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400497 DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
498 " count=%d before=%d after=%d\n"
499 , driveid, vlba, vcount, far_buffer, lba, elba
500 , count, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400501
Kevin O'Connoree55c762008-05-13 00:18:20 -0400502 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400503 if (ret)
504 return ret;
505
Kevin O'Connoree55c762008-05-13 00:18:20 -0400506 return ata_transfer_emu(&op, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400507}
508
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400509// Send a simple atapi command to a drive.
510int
511ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
512 , u32 length, void *far_buffer)
513{
514 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
515 if (ret)
516 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400517
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400518 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
519}
520
521
522/****************************************************************
523 * ATA detect and init
524 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500525
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400526static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400527report_model(int driveid, u8 *buffer)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400528{
529 u8 model[41];
530
531 // Read model name
532 int i;
533 for (i=0; i<40; i+=2) {
534 model[i] = buffer[i+54+1];
535 model[i+1] = buffer[i+54];
536 }
537
538 // Reformat
539 model[40] = 0x00;
540 for (i=39; i>0; i--) {
541 if (model[i] != 0x20)
542 break;
543 model[i] = 0x00;
544 }
545
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400546 u8 channel = driveid / 2;
547 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400548 // XXX - model on stack not %cs
549 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
550}
551
552static u8
553get_ata_version(u8 *buffer)
554{
555 u16 ataversion = *(u16*)&buffer[160];
556 u8 version;
557 for (version=15; version>0; version--)
558 if (ataversion & (1<<version))
559 break;
560 return version;
561}
562
563static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400564init_drive_atapi(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400565{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400566 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400567
568 // Temporary values to do the transfer
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400569 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
570 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400571
572 // Now we send a IDENTIFY command to ATAPI device
573 u8 buffer[0x0200];
574 memset(buffer, 0, sizeof(buffer));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400575 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400576 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400577 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400578 if (ret != 0)
579 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
580
581 u8 type = buffer[1] & 0x1f;
582 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
583 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400584 u16 blksize = CDROM_SECTOR_SIZE;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400585
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400586 SET_EBDA(ata.devices[driveid].device, type);
587 SET_EBDA(ata.devices[driveid].removable, removable);
588 SET_EBDA(ata.devices[driveid].mode, mode);
589 SET_EBDA(ata.devices[driveid].blksize, blksize);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400590
591 // fill cdidmap
592 u8 cdcount = GET_EBDA(ata.cdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400593 SET_EBDA(ata.idmap[1][cdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400594 SET_EBDA(ata.cdcount, ++cdcount);
595
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400596 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400597 u8 version = get_ata_version(buffer);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400598 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400599 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
600 else
601 printf(" ATAPI-%d Device\n", version);
602}
603
604static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400605init_drive_ata(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400606{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400607 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATA);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400608
609 // Temporary values to do the transfer
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400610 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
611 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400612
613 // Now we send a IDENTIFY command to ATA device
614 u8 buffer[0x0200];
615 memset(buffer, 0, sizeof(buffer));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400616 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400617 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400618 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400619 if (ret)
620 BX_PANIC("ata-detect: Failed to detect ATA device\n");
621
622 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
623 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
624 u16 blksize = *(u16*)&buffer[10];
625
626 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
627 u16 heads = *(u16*)&buffer[3*2]; // word 3
628 u16 spt = *(u16*)&buffer[6*2]; // word 6
629
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400630 u64 sectors;
631 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
632 sectors = *(u64*)&buffer[100*2]; // word 100-103
633 else
634 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400635
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400636 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_HD);
637 SET_EBDA(ata.devices[driveid].removable, removable);
638 SET_EBDA(ata.devices[driveid].mode, mode);
639 SET_EBDA(ata.devices[driveid].blksize, blksize);
640 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
641 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
642 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
643 SET_EBDA(ata.devices[driveid].sectors, sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400644
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400645 u8 channel = driveid / 2;
646 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400647 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
648 u8 shift;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400649 for (shift=driveid%4; shift>0; shift--)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400650 translation >>= 2;
651 translation &= 0x03;
652
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400653 SET_EBDA(ata.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400654
655 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation="
656 , channel, slave, cylinders, heads, spt);
657 switch (translation) {
658 case ATA_TRANSLATION_NONE:
659 BX_INFO("none");
660 break;
661 case ATA_TRANSLATION_LBA:
662 BX_INFO("lba");
663 spt = 63;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400664 if (sectors > 63*255*1024) {
665 heads = 255;
666 cylinders = 1024;
667 break;
668 }
669 u32 sect = (u32)sectors / 63;
670 heads = sect / 1024;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400671 if (heads>128)
672 heads = 255;
673 else if (heads>64)
674 heads = 128;
675 else if (heads>32)
676 heads = 64;
677 else if (heads>16)
678 heads = 32;
679 else
680 heads = 16;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400681 cylinders = sect / heads;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400682 break;
683 case ATA_TRANSLATION_RECHS:
684 BX_INFO("r-echs");
685 // Take care not to overflow
686 if (heads==16) {
687 if (cylinders>61439)
688 cylinders=61439;
689 heads=15;
690 cylinders = (u16)((u32)(cylinders)*16/15);
691 }
692 // then go through the large bitshift process
693 case ATA_TRANSLATION_LARGE:
694 if (translation == ATA_TRANSLATION_LARGE)
695 BX_INFO("large");
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400696 while (cylinders > 1024) {
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400697 cylinders >>= 1;
698 heads <<= 1;
699
700 // If we max out the head count
701 if (heads > 127)
702 break;
703 }
704 break;
705 }
706 // clip to 1024 cylinders in lchs
707 if (cylinders > 1024)
708 cylinders = 1024;
709 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
710
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400711 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
712 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
713 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400714
715 // fill hdidmap
716 u8 hdcount = GET_EBDA(ata.hdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400717 SET_EBDA(ata.idmap[0][hdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400718 SET_EBDA(ata.hdcount, ++hdcount);
719
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400720 u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400721 sizeinmb >>= 11;
722
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400723 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400724 u8 version = get_ata_version(buffer);
725 if (sizeinmb < (1 << 16))
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400726 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u32)sizeinmb);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400727 else
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400728 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version
729 , (u32)(sizeinmb >> 10));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400730}
731
732static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400733init_drive_unknown(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400734{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400735 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400736
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400737 u8 channel = driveid / 2;
738 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400739 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
740}
741
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400742static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500743ata_detect()
744{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500745#if CONFIG_MAX_ATA_INTERFACES > 0
746 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
747 SET_EBDA(ata.channels[0].iobase1,0x1f0);
748 SET_EBDA(ata.channels[0].iobase2,0x3f0);
749 SET_EBDA(ata.channels[0].irq,14);
750#endif
751#if CONFIG_MAX_ATA_INTERFACES > 1
752 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
753 SET_EBDA(ata.channels[1].iobase1,0x170);
754 SET_EBDA(ata.channels[1].iobase2,0x370);
755 SET_EBDA(ata.channels[1].irq,15);
756#endif
757#if CONFIG_MAX_ATA_INTERFACES > 2
758 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
759 SET_EBDA(ata.channels[2].iobase1,0x1e8);
760 SET_EBDA(ata.channels[2].iobase2,0x3e0);
761 SET_EBDA(ata.channels[2].irq,12);
762#endif
763#if CONFIG_MAX_ATA_INTERFACES > 3
764 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
765 SET_EBDA(ata.channels[3].iobase1,0x168);
766 SET_EBDA(ata.channels[3].iobase2,0x360);
767 SET_EBDA(ata.channels[3].irq,11);
768#endif
769#if CONFIG_MAX_ATA_INTERFACES > 4
770#error Please fill the ATA interface informations
771#endif
772
773 // Device detection
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400774 int driveid;
775 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
776 u8 channel = driveid / 2;
777 u8 slave = driveid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500778
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400779 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
780 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500781
782 // Disable interrupts
783 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
784
785 // Look for device
786 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
787 outb(0x55, iobase1+ATA_CB_SC);
788 outb(0xaa, iobase1+ATA_CB_SN);
789 outb(0xaa, iobase1+ATA_CB_SC);
790 outb(0x55, iobase1+ATA_CB_SN);
791 outb(0x55, iobase1+ATA_CB_SC);
792 outb(0xaa, iobase1+ATA_CB_SN);
793
794 // If we found something
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400795 u8 sc = inb(iobase1+ATA_CB_SC);
796 u8 sn = inb(iobase1+ATA_CB_SN);
797
798 if (sc != 0x55 || sn != 0xaa)
799 continue;
800
801 // reset the channel
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400802 ata_reset(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400803
804 // check for ATA or ATAPI
805 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500806 sc = inb(iobase1+ATA_CB_SC);
807 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400808 if (sc!=0x01 || sn!=0x01) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400809 init_drive_unknown(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400810 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500811 }
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400812 u8 cl = inb(iobase1+ATA_CB_CL);
813 u8 ch = inb(iobase1+ATA_CB_CH);
814 u8 st = inb(iobase1+ATA_CB_STAT);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500815
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400816 if (cl==0x14 && ch==0xeb)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400817 init_drive_atapi(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400818 else if (cl==0x00 && ch==0x00 && st!=0x00)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400819 init_drive_ata(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400820 else if (cl==0xff && ch==0xff)
821 // None
822 continue;
823 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400824 init_drive_unknown(driveid);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500825 }
826
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400827 // Store the device count
828 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500829
830 printf("\n");
831
832 // FIXME : should use bios=cmos|auto|disable bits
833 // FIXME : should know about translation bits
834 // FIXME : move hard_drive_post here
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500835}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400836
837static void
838ata_init()
839{
840 // hdidmap and cdidmap init.
841 u8 device;
842 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
843 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
844 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
845 }
846}
847
848static void
849fill_hdinfo(int hdnum, u8 typecmos, u8 basecmos)
850{
851 u8 type = inb_cmos(typecmos);
852 if (type != 47)
853 // XXX - halt
854 return;
855
856 SET_EBDA(fdpt[hdnum].precompensation, ((inb_cmos(basecmos+4) << 8)
857 | inb_cmos(basecmos+3)));
858 SET_EBDA(fdpt[hdnum].drive_control_byte, inb_cmos(basecmos+5));
859 SET_EBDA(fdpt[hdnum].landing_zone, ((inb_cmos(basecmos+7) << 8)
860 | inb_cmos(basecmos+6)));
861 u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0);
862 u8 heads = inb_cmos(basecmos+2);
863 u8 sectors = inb_cmos(basecmos+8);
864 if (cyl < 1024) {
865 // no logical CHS mapping used, just physical CHS
866 // use Standard Fixed Disk Parameter Table (FDPT)
867 SET_EBDA(fdpt[hdnum].cylinders, cyl);
868 SET_EBDA(fdpt[hdnum].heads, heads);
869 SET_EBDA(fdpt[hdnum].sectors, sectors);
870 return;
871 }
872
873 // complies with Phoenix style Translated Fixed Disk Parameter
874 // Table (FDPT)
875 SET_EBDA(fdpt[hdnum].phys_cylinders, cyl);
876 SET_EBDA(fdpt[hdnum].phys_heads, heads);
877 SET_EBDA(fdpt[hdnum].phys_sectors, sectors);
878 SET_EBDA(fdpt[hdnum].sectors, sectors);
879 SET_EBDA(fdpt[hdnum].a0h_signature, 0xa0);
880 if (cyl > 8192) {
881 cyl >>= 4;
882 heads <<= 4;
883 } else if (cyl > 4096) {
884 cyl >>= 3;
885 heads <<= 3;
886 } else if (cyl > 2048) {
887 cyl >>= 2;
888 heads <<= 2;
889 }
890 SET_EBDA(fdpt[hdnum].cylinders, cyl);
891 SET_EBDA(fdpt[hdnum].heads, heads);
892 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
893 , fdpt[hdnum]));
894 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
895 , fdpt[hdnum]) - 1);
896 SET_EBDA(fdpt[hdnum].checksum, -sum);
897}
898
899void
900hard_drive_setup()
901{
902 outb(0x0a, PORT_HD_DATA); // 0000 1010 = reserved, disable IRQ 14
903 SET_BDA(disk_count, 1);
904 SET_BDA(disk_control_byte, 0xc0);
905
906 // move disk geometry data from CMOS to EBDA disk parameter table(s)
907 u8 diskinfo = inb_cmos(CMOS_DISK_DATA);
908 if ((diskinfo & 0xf0) == 0xf0)
909 // Fill EBDA table for hard disk 0.
910 fill_hdinfo(0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL);
911 if ((diskinfo & 0x0f) == 0x0f)
912 // XXX - bochs halts on any other type
913 // Fill EBDA table for hard disk 1.
914 fill_hdinfo(1, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL);
915
916 if (CONFIG_ATA) {
917 ata_init();
918 ata_detect();
919 }
920}