blob: ecda021809501549e8706aeaaf2c879a8ff549bf [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'Connorf888f8c2008-03-23 00:04:54 -0400144struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400145 u8 feature;
146 u8 sector_count;
147 u8 lba_low;
148 u8 lba_mid;
149 u8 lba_high;
150 u8 device;
151 u8 command;
152
153 u8 sector_count2;
154 u8 lba_low2;
155 u8 lba_mid2;
156 u8 lba_high2;
157};
158
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400159// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400160static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400161send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500162{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400163 u8 channel = driveid / 2;
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500164 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
165 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500166
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400167 int status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500168 if (status & ATA_CB_STAT_BSY)
169 return 1;
170
171 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400172 if (cmd->command & 0x04) {
173 outb(0x00, iobase1 + ATA_CB_FR);
174 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
175 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
176 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
177 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500178 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400179 outb(cmd->feature, iobase1 + ATA_CB_FR);
180 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
181 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
182 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
183 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
184 outb(cmd->device, iobase1 + ATA_CB_DH);
185 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500186
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400187 nsleep(400);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500188
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400189 status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
190 if (status < 0)
191 return status;
192
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500193 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400194 DEBUGF("send_cmd : read error\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500195 return 2;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400196 }
197 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400198 DEBUGF("send_cmd : DRQ not set (status %02x)\n"
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500199 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500200 return 3;
201 }
202
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400203 return 0;
204}
205
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400206// Read and discard x number of bytes from an io channel.
207static void
208insx_discard(int mode, int iobase1, int bytes)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400209{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400210 int count, i;
211 if (mode == ATA_MODE_PIO32) {
212 count = bytes / 4;
213 for (i=0; i<count; i++)
214 inl(iobase1);
215 } else {
216 count = bytes / 2;
217 for (i=0; i<count; i++)
218 inw(iobase1);
219 }
220}
221
222// Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
223// 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
224// and/or last block may be partially transferred. This function is
225// inlined because all the callers use different forms and because the
226// large number of parameters would consume a lot of stack space.
Kevin O'Connor74f9c082008-04-13 16:57:16 -0400227static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400228ata_transfer(int driveid, int iswrite, int count, int blocksize
229 , int skipfirst, int skiplast, void *far_buffer)
230{
231 DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d"
232 " skipf=%d skipl=%d buf=%p\n"
233 , driveid, iswrite, count, blocksize
234 , skipfirst, skiplast, far_buffer);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400235
236 // Reset count of transferred data
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400237 SET_EBDA(ata.trsfsectors, 0);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400238
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400239 u8 channel = driveid / 2;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400240 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
241 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400242 u8 mode = GET_EBDA(ata.devices[driveid].mode);
243 int current = 0;
244 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400245 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400246 int bsize = blocksize;
247 if (skipfirst && current == 0) {
248 insx_discard(mode, iobase1, skipfirst);
249 bsize -= skipfirst;
250 }
251 if (skiplast && current == count-1)
252 bsize -= skiplast;
253
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400254 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400255 // Write data to controller
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400256 DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400257 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400258 outsl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400259 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400260 outsw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500261 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400262 // Read data from controller
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400263 DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400264 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400265 insl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400266 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400267 insw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400268 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400269 far_buffer += bsize;
270
271 if (skiplast && current == count-1)
272 insx_discard(mode, iobase1, skiplast);
273
274 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
275 if (status < 0)
276 // Error
277 return status;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400278
279 current++;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400280 SET_EBDA(ata.trsfsectors, current);
281 if (current == count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400282 break;
283 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
284 | ATA_CB_STAT_ERR);
285 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400286 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400287 , (unsigned) status);
288 return 5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500289 }
290 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400291
292 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
293 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400294 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400295 status &= ~ATA_CB_STAT_DF;
296 if (status != ATA_CB_STAT_RDY ) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400297 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400298 , (unsigned) status);
299 return 4;
300 }
301
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500302 // Enable interrupts
303 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
304 return 0;
305}
306
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400307
308/****************************************************************
309 * ATA hard drive functions
310 ****************************************************************/
311
312// Read/write count blocks from a harddrive.
313int
314ata_cmd_data(int driveid, u16 command, u32 lba, u16 count, void *far_buffer)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400315{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400316 u8 slave = driveid % 2;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400317
318 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400319 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400320
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400321 cmd.command = command;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400322 if (count >= (1<<8) || lba + count >= (1<<28)) {
323 cmd.sector_count2 = count >> 8;
324 cmd.lba_low2 = lba >> 24;
325 cmd.lba_mid2 = 0;
326 cmd.lba_high2 = 0;
327
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400328 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400329 lba &= 0xffffff;
330 }
331
332 cmd.feature = 0;
333 cmd.sector_count = count;
334 cmd.lba_low = lba;
335 cmd.lba_mid = lba >> 8;
336 cmd.lba_high = lba >> 16;
337 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
338 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400339
340 int ret = send_cmd(driveid, &cmd);
341 if (ret)
342 return ret;
343
344 int iswrite = command == ATA_CMD_WRITE_SECTORS;
345 return ata_transfer(driveid, iswrite, count, IDE_SECTOR_SIZE
346 , 0, 0, far_buffer);
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400347}
348
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400349
350/****************************************************************
351 * ATAPI functions
352 ****************************************************************/
353
354// Low-level atapi command transmit function.
355static int
356send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500357{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400358 u8 channel = driveid / 2;
359 u8 slave = driveid % 2;
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400360 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
361 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500362
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400363 struct ata_pio_command cmd;
364 cmd.sector_count = 0;
365 cmd.feature = 0;
366 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400367 cmd.lba_mid = blocksize;
368 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400369 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
370 cmd.command = ATA_CMD_PACKET;
371
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400372 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400373 if (ret)
374 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500375
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400376 // Send command to device
Kevin O'Connor070231b2008-03-22 20:44:37 -0400377 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500378
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400379 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
380 if (status < 0)
381 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400382
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500383 return 0;
384}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500385
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400386// Low-level cdrom read atapi command transmit function.
Kevin O'Connor74f9c082008-04-13 16:57:16 -0400387static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400388send_cdrom_cmd(int driveid, u32 lba, u16 count)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500389{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400390 u8 atacmd[12];
391 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500392
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400393 atacmd[0]=0x28; // READ command
394 atacmd[7]=(count & 0xff00) >> 8; // Sectors
395 atacmd[8]=(count & 0x00ff); // Sectors
396 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
Kevin O'Connor180a9592008-03-04 22:50:53 -0500397 atacmd[3]=(lba & 0x00ff0000) >> 16;
398 atacmd[4]=(lba & 0x0000ff00) >> 8;
399 atacmd[5]=(lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400400
401 return send_atapi_cmd(driveid, atacmd, sizeof(atacmd), CDROM_SECTOR_SIZE);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500402}
403
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400404// Read sectors from the cdrom.
405int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400406cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400407{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400408 int ret = send_cdrom_cmd(driveid, lba, count);
409 if (ret)
410 return ret;
411
412 return ata_transfer(driveid, 0, count, CDROM_SECTOR_SIZE, 0, 0, far_buffer);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400413}
414
415// Pretend the cdrom has 512 byte sectors (instead of 2048) and read
416// sectors.
417int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400418cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400419{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400420 u32 velba = vlba + vcount - 1;
421 u32 lba = vlba / 4;
422 u32 elba = velba / 4;
423 int count = elba - lba + 1;
424 int before = vlba % 4;
425 int after = 3 - (velba % 4);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400426
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400427 DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
428 " count=%d before=%d after=%d\n"
429 , driveid, vlba, vcount, far_buffer, lba, elba
430 , count, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400431
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400432 int ret = send_cdrom_cmd(driveid, lba, count);
433 if (ret)
434 return ret;
435
436 ret = ata_transfer(driveid, 0, count, CDROM_SECTOR_SIZE
437 , before*512, after*512, far_buffer);
438 if (ret) {
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400439 SET_EBDA(ata.trsfsectors, 0);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400440 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400441 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400442 SET_EBDA(ata.trsfsectors, vcount);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400443 return 0;
444}
445
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400446// Send a simple atapi command to a drive.
447int
448ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
449 , u32 length, void *far_buffer)
450{
451 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
452 if (ret)
453 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400454
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400455 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
456}
457
458
459/****************************************************************
460 * ATA detect and init
461 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500462
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400463static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400464report_model(int driveid, u8 *buffer)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400465{
466 u8 model[41];
467
468 // Read model name
469 int i;
470 for (i=0; i<40; i+=2) {
471 model[i] = buffer[i+54+1];
472 model[i+1] = buffer[i+54];
473 }
474
475 // Reformat
476 model[40] = 0x00;
477 for (i=39; i>0; i--) {
478 if (model[i] != 0x20)
479 break;
480 model[i] = 0x00;
481 }
482
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400483 u8 channel = driveid / 2;
484 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400485 // XXX - model on stack not %cs
486 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
487}
488
489static u8
490get_ata_version(u8 *buffer)
491{
492 u16 ataversion = *(u16*)&buffer[160];
493 u8 version;
494 for (version=15; version>0; version--)
495 if (ataversion & (1<<version))
496 break;
497 return version;
498}
499
500static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400501init_drive_atapi(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400502{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400503 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400504
505 // Temporary values to do the transfer
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400506 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
507 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400508
509 // Now we send a IDENTIFY command to ATAPI device
510 u8 buffer[0x0200];
511 memset(buffer, 0, sizeof(buffer));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400512 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400513 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400514 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400515 if (ret != 0)
516 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
517
518 u8 type = buffer[1] & 0x1f;
519 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
520 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400521 u16 blksize = CDROM_SECTOR_SIZE;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400522
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400523 SET_EBDA(ata.devices[driveid].device, type);
524 SET_EBDA(ata.devices[driveid].removable, removable);
525 SET_EBDA(ata.devices[driveid].mode, mode);
526 SET_EBDA(ata.devices[driveid].blksize, blksize);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400527
528 // fill cdidmap
529 u8 cdcount = GET_EBDA(ata.cdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400530 SET_EBDA(ata.idmap[1][cdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400531 SET_EBDA(ata.cdcount, ++cdcount);
532
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400533 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400534 u8 version = get_ata_version(buffer);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400535 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400536 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
537 else
538 printf(" ATAPI-%d Device\n", version);
539}
540
541static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400542init_drive_ata(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400543{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400544 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATA);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400545
546 // Temporary values to do the transfer
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400547 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
548 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400549
550 // Now we send a IDENTIFY command to ATA device
551 u8 buffer[0x0200];
552 memset(buffer, 0, sizeof(buffer));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400553 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400554 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400555 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400556 if (ret)
557 BX_PANIC("ata-detect: Failed to detect ATA device\n");
558
559 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
560 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
561 u16 blksize = *(u16*)&buffer[10];
562
563 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
564 u16 heads = *(u16*)&buffer[3*2]; // word 3
565 u16 spt = *(u16*)&buffer[6*2]; // word 6
566
567 u32 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
568
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400569 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_HD);
570 SET_EBDA(ata.devices[driveid].removable, removable);
571 SET_EBDA(ata.devices[driveid].mode, mode);
572 SET_EBDA(ata.devices[driveid].blksize, blksize);
573 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
574 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
575 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
576 SET_EBDA(ata.devices[driveid].sectors, sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400577
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400578 u8 channel = driveid / 2;
579 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400580 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
581 u8 shift;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400582 for (shift=driveid%4; shift>0; shift--)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400583 translation >>= 2;
584 translation &= 0x03;
585
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400586 SET_EBDA(ata.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400587
588 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation="
589 , channel, slave, cylinders, heads, spt);
590 switch (translation) {
591 case ATA_TRANSLATION_NONE:
592 BX_INFO("none");
593 break;
594 case ATA_TRANSLATION_LBA:
595 BX_INFO("lba");
596 spt = 63;
597 sectors /= 63;
598 heads = sectors / 1024;
599 if (heads>128)
600 heads = 255;
601 else if (heads>64)
602 heads = 128;
603 else if (heads>32)
604 heads = 64;
605 else if (heads>16)
606 heads = 32;
607 else
608 heads = 16;
609 cylinders = sectors / heads;
610 break;
611 case ATA_TRANSLATION_RECHS:
612 BX_INFO("r-echs");
613 // Take care not to overflow
614 if (heads==16) {
615 if (cylinders>61439)
616 cylinders=61439;
617 heads=15;
618 cylinders = (u16)((u32)(cylinders)*16/15);
619 }
620 // then go through the large bitshift process
621 case ATA_TRANSLATION_LARGE:
622 if (translation == ATA_TRANSLATION_LARGE)
623 BX_INFO("large");
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400624 while (cylinders > 1024) {
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400625 cylinders >>= 1;
626 heads <<= 1;
627
628 // If we max out the head count
629 if (heads > 127)
630 break;
631 }
632 break;
633 }
634 // clip to 1024 cylinders in lchs
635 if (cylinders > 1024)
636 cylinders = 1024;
637 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
638
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400639 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
640 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
641 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400642
643 // fill hdidmap
644 u8 hdcount = GET_EBDA(ata.hdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400645 SET_EBDA(ata.idmap[0][hdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400646 SET_EBDA(ata.hdcount, ++hdcount);
647
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400648 u32 sizeinmb = GET_EBDA(ata.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400649 sizeinmb >>= 11;
650
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400651 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400652 u8 version = get_ata_version(buffer);
653 if (sizeinmb < (1 << 16))
654 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, sizeinmb);
655 else
656 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, sizeinmb >> 10);
657}
658
659static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400660init_drive_unknown(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400661{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400662 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400663
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400664 u8 channel = driveid / 2;
665 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400666 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
667}
668
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400669static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500670ata_detect()
671{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500672#if CONFIG_MAX_ATA_INTERFACES > 0
673 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
674 SET_EBDA(ata.channels[0].iobase1,0x1f0);
675 SET_EBDA(ata.channels[0].iobase2,0x3f0);
676 SET_EBDA(ata.channels[0].irq,14);
677#endif
678#if CONFIG_MAX_ATA_INTERFACES > 1
679 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
680 SET_EBDA(ata.channels[1].iobase1,0x170);
681 SET_EBDA(ata.channels[1].iobase2,0x370);
682 SET_EBDA(ata.channels[1].irq,15);
683#endif
684#if CONFIG_MAX_ATA_INTERFACES > 2
685 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
686 SET_EBDA(ata.channels[2].iobase1,0x1e8);
687 SET_EBDA(ata.channels[2].iobase2,0x3e0);
688 SET_EBDA(ata.channels[2].irq,12);
689#endif
690#if CONFIG_MAX_ATA_INTERFACES > 3
691 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
692 SET_EBDA(ata.channels[3].iobase1,0x168);
693 SET_EBDA(ata.channels[3].iobase2,0x360);
694 SET_EBDA(ata.channels[3].irq,11);
695#endif
696#if CONFIG_MAX_ATA_INTERFACES > 4
697#error Please fill the ATA interface informations
698#endif
699
700 // Device detection
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400701 int driveid;
702 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
703 u8 channel = driveid / 2;
704 u8 slave = driveid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500705
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400706 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
707 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500708
709 // Disable interrupts
710 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
711
712 // Look for device
713 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
714 outb(0x55, iobase1+ATA_CB_SC);
715 outb(0xaa, iobase1+ATA_CB_SN);
716 outb(0xaa, iobase1+ATA_CB_SC);
717 outb(0x55, iobase1+ATA_CB_SN);
718 outb(0x55, iobase1+ATA_CB_SC);
719 outb(0xaa, iobase1+ATA_CB_SN);
720
721 // If we found something
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400722 u8 sc = inb(iobase1+ATA_CB_SC);
723 u8 sn = inb(iobase1+ATA_CB_SN);
724
725 if (sc != 0x55 || sn != 0xaa)
726 continue;
727
728 // reset the channel
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400729 ata_reset(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400730
731 // check for ATA or ATAPI
732 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500733 sc = inb(iobase1+ATA_CB_SC);
734 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400735 if (sc!=0x01 || sn!=0x01) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400736 init_drive_unknown(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400737 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500738 }
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400739 u8 cl = inb(iobase1+ATA_CB_CL);
740 u8 ch = inb(iobase1+ATA_CB_CH);
741 u8 st = inb(iobase1+ATA_CB_STAT);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500742
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400743 if (cl==0x14 && ch==0xeb)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400744 init_drive_atapi(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400745 else if (cl==0x00 && ch==0x00 && st!=0x00)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400746 init_drive_ata(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400747 else if (cl==0xff && ch==0xff)
748 // None
749 continue;
750 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400751 init_drive_unknown(driveid);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500752 }
753
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400754 // Store the device count
755 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500756
757 printf("\n");
758
759 // FIXME : should use bios=cmos|auto|disable bits
760 // FIXME : should know about translation bits
761 // FIXME : move hard_drive_post here
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500762}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400763
764static void
765ata_init()
766{
767 // hdidmap and cdidmap init.
768 u8 device;
769 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
770 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
771 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
772 }
773}
774
775static void
776fill_hdinfo(int hdnum, u8 typecmos, u8 basecmos)
777{
778 u8 type = inb_cmos(typecmos);
779 if (type != 47)
780 // XXX - halt
781 return;
782
783 SET_EBDA(fdpt[hdnum].precompensation, ((inb_cmos(basecmos+4) << 8)
784 | inb_cmos(basecmos+3)));
785 SET_EBDA(fdpt[hdnum].drive_control_byte, inb_cmos(basecmos+5));
786 SET_EBDA(fdpt[hdnum].landing_zone, ((inb_cmos(basecmos+7) << 8)
787 | inb_cmos(basecmos+6)));
788 u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0);
789 u8 heads = inb_cmos(basecmos+2);
790 u8 sectors = inb_cmos(basecmos+8);
791 if (cyl < 1024) {
792 // no logical CHS mapping used, just physical CHS
793 // use Standard Fixed Disk Parameter Table (FDPT)
794 SET_EBDA(fdpt[hdnum].cylinders, cyl);
795 SET_EBDA(fdpt[hdnum].heads, heads);
796 SET_EBDA(fdpt[hdnum].sectors, sectors);
797 return;
798 }
799
800 // complies with Phoenix style Translated Fixed Disk Parameter
801 // Table (FDPT)
802 SET_EBDA(fdpt[hdnum].phys_cylinders, cyl);
803 SET_EBDA(fdpt[hdnum].phys_heads, heads);
804 SET_EBDA(fdpt[hdnum].phys_sectors, sectors);
805 SET_EBDA(fdpt[hdnum].sectors, sectors);
806 SET_EBDA(fdpt[hdnum].a0h_signature, 0xa0);
807 if (cyl > 8192) {
808 cyl >>= 4;
809 heads <<= 4;
810 } else if (cyl > 4096) {
811 cyl >>= 3;
812 heads <<= 3;
813 } else if (cyl > 2048) {
814 cyl >>= 2;
815 heads <<= 2;
816 }
817 SET_EBDA(fdpt[hdnum].cylinders, cyl);
818 SET_EBDA(fdpt[hdnum].heads, heads);
819 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
820 , fdpt[hdnum]));
821 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
822 , fdpt[hdnum]) - 1);
823 SET_EBDA(fdpt[hdnum].checksum, -sum);
824}
825
826void
827hard_drive_setup()
828{
829 outb(0x0a, PORT_HD_DATA); // 0000 1010 = reserved, disable IRQ 14
830 SET_BDA(disk_count, 1);
831 SET_BDA(disk_control_byte, 0xc0);
832
833 // move disk geometry data from CMOS to EBDA disk parameter table(s)
834 u8 diskinfo = inb_cmos(CMOS_DISK_DATA);
835 if ((diskinfo & 0xf0) == 0xf0)
836 // Fill EBDA table for hard disk 0.
837 fill_hdinfo(0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL);
838 if ((diskinfo & 0x0f) == 0x0f)
839 // XXX - bochs halts on any other type
840 // Fill EBDA table for hard disk 1.
841 fill_hdinfo(1, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL);
842
843 if (CONFIG_ATA) {
844 ata_init();
845 ata_detect();
846 }
847}