blob: f7f4a57c332dc2e27c84bae70bac4aa00da9b58e [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++;
42 u8 result;
43 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);
53 else if (when_done == TIMEOUT)
54 result = 0;
55
56 if (result)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040057 return status;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050058 // mod 2048 each 16 ms
59 if (time>>16 != last) {
60 last = time >>16;
Kevin O'Connor127cbd72008-03-08 11:34:28 -050061 DEBUGF("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY)"
62 " %d time= %d timeout= %d\n"
63 , when_done, time>>11, timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050064 }
65 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor127cbd72008-03-08 11:34:28 -050066 DEBUGF("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
67 ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
68 , when_done, time>>11, timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050069 return -1;
70 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -040071 if (timeout == 0 || (time>>11) > timeout)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050072 break;
73 }
74 BX_INFO("IDE time out\n");
75 return -1;
76}
77
Kevin O'Connora6b9f712008-03-29 12:53:57 -040078// Wait for ide state - pauses for one ata cycle first.
79static inline int
80pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout)
81{
82 // Wait one PIO transfer cycle.
83 inb(iobase2 + ATA_CB_ASTAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050084
Kevin O'Connora6b9f712008-03-29 12:53:57 -040085 return await_ide(when_done, iobase1, timeout);
86}
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050087
Kevin O'Connora6b9f712008-03-29 12:53:57 -040088// Delay for x nanoseconds
89static void
90nsleep(u32 delay)
91{
92 // XXX - how to implement ndelay?
93 while (delay--)
94 nop();
95}
96
97// Reset a drive
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050098void
Kevin O'Connora6b9f712008-03-29 12:53:57 -040099ata_reset(int driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500100{
101 u16 iobase1, iobase2;
102 u8 channel, slave, sn, sc;
103 u8 type;
104
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400105 channel = driveid / 2;
106 slave = driveid % 2;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500107
108 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
109 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
110
111 // Reset
112
113 // 8.2.1 (a) -- set SRST in DC
114 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
115
116 // 8.2.1 (b) -- wait for BSY
117 await_ide(BSY, iobase1, 20);
118
119 // 8.2.1 (f) -- clear SRST
120 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
121
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400122 type=GET_EBDA(ata.devices[driveid].type);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500123 if (type != ATA_TYPE_NONE) {
124
125 // 8.2.1 (g) -- check for sc==sn==0x01
126 // select device
127 outb(slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
128 sc = inb(iobase1+ATA_CB_SC);
129 sn = inb(iobase1+ATA_CB_SN);
130
131 if ( (sc==0x01) && (sn==0x01) ) {
132 if (type == ATA_TYPE_ATA) //ATA
133 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
134 else //ATAPI
135 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
136 }
137
138 // 8.2.1 (h) -- wait for not BSY
139 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
140 }
141
142 // Enable interrupts
143 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
144}
145
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400146struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400147 u8 feature;
148 u8 sector_count;
149 u8 lba_low;
150 u8 lba_mid;
151 u8 lba_high;
152 u8 device;
153 u8 command;
154
155 u8 sector_count2;
156 u8 lba_low2;
157 u8 lba_mid2;
158 u8 lba_high2;
159};
160
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400161// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400162static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400163send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500164{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400165 u8 channel = driveid / 2;
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500166 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
167 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500168
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400169 int status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500170 if (status & ATA_CB_STAT_BSY)
171 return 1;
172
173 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400174 if (cmd->command & 0x04) {
175 outb(0x00, iobase1 + ATA_CB_FR);
176 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
177 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
178 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
179 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500180 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400181 outb(cmd->feature, iobase1 + ATA_CB_FR);
182 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
183 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
184 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
185 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
186 outb(cmd->device, iobase1 + ATA_CB_DH);
187 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500188
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400189 nsleep(400);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500190
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400191 status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
192 if (status < 0)
193 return status;
194
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500195 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400196 DEBUGF("send_cmd : read error\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500197 return 2;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400198 }
199 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400200 DEBUGF("send_cmd : DRQ not set (status %02x)\n"
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500201 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500202 return 3;
203 }
204
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400205 return 0;
206}
207
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400208// Read and discard x number of bytes from an io channel.
209static void
210insx_discard(int mode, int iobase1, int bytes)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400211{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400212 int count, i;
213 if (mode == ATA_MODE_PIO32) {
214 count = bytes / 4;
215 for (i=0; i<count; i++)
216 inl(iobase1);
217 } else {
218 count = bytes / 2;
219 for (i=0; i<count; i++)
220 inw(iobase1);
221 }
222}
223
224// Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
225// 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
226// and/or last block may be partially transferred. This function is
227// inlined because all the callers use different forms and because the
228// large number of parameters would consume a lot of stack space.
229static inline int
230ata_transfer(int driveid, int iswrite, int count, int blocksize
231 , int skipfirst, int skiplast, void *far_buffer)
232{
233 DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d"
234 " skipf=%d skipl=%d buf=%p\n"
235 , driveid, iswrite, count, blocksize
236 , skipfirst, skiplast, far_buffer);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400237
238 // Reset count of transferred data
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400239 SET_EBDA(ata.trsfsectors, 0);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400240
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400241 u8 channel = driveid / 2;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400242 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
243 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400244 u8 mode = GET_EBDA(ata.devices[driveid].mode);
245 int current = 0;
246 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400247 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400248 int bsize = blocksize;
249 if (skipfirst && current == 0) {
250 insx_discard(mode, iobase1, skipfirst);
251 bsize -= skipfirst;
252 }
253 if (skiplast && current == count-1)
254 bsize -= skiplast;
255
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400256 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400257 // Write data to controller
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400258 DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400259 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400260 outsl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400261 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400262 outsw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500263 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400264 // Read data from controller
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400265 DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400266 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400267 insl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400268 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400269 insw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400270 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400271 far_buffer += bsize;
272
273 if (skiplast && current == count-1)
274 insx_discard(mode, iobase1, skiplast);
275
276 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
277 if (status < 0)
278 // Error
279 return status;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400280
281 current++;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400282 SET_EBDA(ata.trsfsectors, current);
283 if (current == count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400284 break;
285 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
286 | ATA_CB_STAT_ERR);
287 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400288 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400289 , (unsigned) status);
290 return 5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500291 }
292 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400293
294 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
295 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400296 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400297 status &= ~ATA_CB_STAT_DF;
298 if (status != ATA_CB_STAT_RDY ) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400299 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400300 , (unsigned) status);
301 return 4;
302 }
303
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500304 // Enable interrupts
305 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
306 return 0;
307}
308
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400309
310/****************************************************************
311 * ATA hard drive functions
312 ****************************************************************/
313
314// Read/write count blocks from a harddrive.
315int
316ata_cmd_data(int driveid, u16 command, u32 lba, u16 count, void *far_buffer)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400317{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400318 u8 slave = driveid % 2;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400319
320 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400321 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400322
323 if (count >= (1<<8) || lba + count >= (1<<28)) {
324 cmd.sector_count2 = count >> 8;
325 cmd.lba_low2 = lba >> 24;
326 cmd.lba_mid2 = 0;
327 cmd.lba_high2 = 0;
328
329 command |= 0x04;
330 lba &= 0xffffff;
331 }
332
333 cmd.feature = 0;
334 cmd.sector_count = count;
335 cmd.lba_low = lba;
336 cmd.lba_mid = lba >> 8;
337 cmd.lba_high = lba >> 16;
338 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
339 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
340 cmd.command = command;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400341
342 int ret = send_cmd(driveid, &cmd);
343 if (ret)
344 return ret;
345
346 int iswrite = command == ATA_CMD_WRITE_SECTORS;
347 return ata_transfer(driveid, iswrite, count, IDE_SECTOR_SIZE
348 , 0, 0, far_buffer);
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400349}
350
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400351
352/****************************************************************
353 * ATAPI functions
354 ****************************************************************/
355
356// Low-level atapi command transmit function.
357static int
358send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500359{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400360 u8 channel = driveid / 2;
361 u8 slave = driveid % 2;
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400362 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
363 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500364
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400365 struct ata_pio_command cmd;
366 cmd.sector_count = 0;
367 cmd.feature = 0;
368 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400369 cmd.lba_mid = blocksize;
370 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400371 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
372 cmd.command = ATA_CMD_PACKET;
373
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400374 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400375 if (ret)
376 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500377
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400378 // Send command to device
Kevin O'Connor070231b2008-03-22 20:44:37 -0400379 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500380
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400381 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
382 if (status < 0)
383 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400384
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500385 return 0;
386}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500387
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400388// Low-level cdrom read atapi command transmit function.
389static inline int
390send_cdrom_cmd(int driveid, u32 lba, u16 count)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500391{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400392 u8 atacmd[12];
393 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500394
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400395 atacmd[0]=0x28; // READ command
396 atacmd[7]=(count & 0xff00) >> 8; // Sectors
397 atacmd[8]=(count & 0x00ff); // Sectors
398 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
Kevin O'Connor180a9592008-03-04 22:50:53 -0500399 atacmd[3]=(lba & 0x00ff0000) >> 16;
400 atacmd[4]=(lba & 0x0000ff00) >> 8;
401 atacmd[5]=(lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400402
403 return send_atapi_cmd(driveid, atacmd, sizeof(atacmd), CDROM_SECTOR_SIZE);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500404}
405
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400406// Read sectors from the cdrom.
407int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400408cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400409{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400410 int ret = send_cdrom_cmd(driveid, lba, count);
411 if (ret)
412 return ret;
413
414 return ata_transfer(driveid, 0, count, CDROM_SECTOR_SIZE, 0, 0, far_buffer);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400415}
416
417// Pretend the cdrom has 512 byte sectors (instead of 2048) and read
418// sectors.
419int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400420cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400421{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400422 u32 velba = vlba + vcount - 1;
423 u32 lba = vlba / 4;
424 u32 elba = velba / 4;
425 int count = elba - lba + 1;
426 int before = vlba % 4;
427 int after = 3 - (velba % 4);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400428
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400429 DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
430 " count=%d before=%d after=%d\n"
431 , driveid, vlba, vcount, far_buffer, lba, elba
432 , count, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400433
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400434 int ret = send_cdrom_cmd(driveid, lba, count);
435 if (ret)
436 return ret;
437
438 ret = ata_transfer(driveid, 0, count, CDROM_SECTOR_SIZE
439 , before*512, after*512, far_buffer);
440 if (ret) {
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400441 SET_EBDA(ata.trsfsectors, 0);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400442 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400443 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400444 SET_EBDA(ata.trsfsectors, vcount);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400445 return 0;
446}
447
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400448// Send a simple atapi command to a drive.
449int
450ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
451 , u32 length, void *far_buffer)
452{
453 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
454 if (ret)
455 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400456
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400457 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
458}
459
460
461/****************************************************************
462 * ATA detect and init
463 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500464
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400465static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400466report_model(int driveid, u8 *buffer)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400467{
468 u8 model[41];
469
470 // Read model name
471 int i;
472 for (i=0; i<40; i+=2) {
473 model[i] = buffer[i+54+1];
474 model[i+1] = buffer[i+54];
475 }
476
477 // Reformat
478 model[40] = 0x00;
479 for (i=39; i>0; i--) {
480 if (model[i] != 0x20)
481 break;
482 model[i] = 0x00;
483 }
484
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400485 u8 channel = driveid / 2;
486 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400487 // XXX - model on stack not %cs
488 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
489}
490
491static u8
492get_ata_version(u8 *buffer)
493{
494 u16 ataversion = *(u16*)&buffer[160];
495 u8 version;
496 for (version=15; version>0; version--)
497 if (ataversion & (1<<version))
498 break;
499 return version;
500}
501
502static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400503init_drive_atapi(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400504{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400505 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400506
507 // Temporary values to do the transfer
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400508 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
509 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400510
511 // Now we send a IDENTIFY command to ATAPI device
512 u8 buffer[0x0200];
513 memset(buffer, 0, sizeof(buffer));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400514 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400515 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400516 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400517 if (ret != 0)
518 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
519
520 u8 type = buffer[1] & 0x1f;
521 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
522 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400523 u16 blksize = CDROM_SECTOR_SIZE;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400524
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400525 SET_EBDA(ata.devices[driveid].device, type);
526 SET_EBDA(ata.devices[driveid].removable, removable);
527 SET_EBDA(ata.devices[driveid].mode, mode);
528 SET_EBDA(ata.devices[driveid].blksize, blksize);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400529
530 // fill cdidmap
531 u8 cdcount = GET_EBDA(ata.cdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400532 SET_EBDA(ata.idmap[1][cdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400533 SET_EBDA(ata.cdcount, ++cdcount);
534
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400535 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400536 u8 version = get_ata_version(buffer);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400537 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400538 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
539 else
540 printf(" ATAPI-%d Device\n", version);
541}
542
543static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400544init_drive_ata(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400545{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400546 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATA);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400547
548 // Temporary values to do the transfer
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400549 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
550 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400551
552 // Now we send a IDENTIFY command to ATA device
553 u8 buffer[0x0200];
554 memset(buffer, 0, sizeof(buffer));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400555 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400556 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400557 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400558 if (ret)
559 BX_PANIC("ata-detect: Failed to detect ATA device\n");
560
561 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
562 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
563 u16 blksize = *(u16*)&buffer[10];
564
565 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
566 u16 heads = *(u16*)&buffer[3*2]; // word 3
567 u16 spt = *(u16*)&buffer[6*2]; // word 6
568
569 u32 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
570
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400571 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_HD);
572 SET_EBDA(ata.devices[driveid].removable, removable);
573 SET_EBDA(ata.devices[driveid].mode, mode);
574 SET_EBDA(ata.devices[driveid].blksize, blksize);
575 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
576 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
577 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
578 SET_EBDA(ata.devices[driveid].sectors, sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400579
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400580 u8 channel = driveid / 2;
581 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400582 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
583 u8 shift;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400584 for (shift=driveid%4; shift>0; shift--)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400585 translation >>= 2;
586 translation &= 0x03;
587
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400588 SET_EBDA(ata.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400589
590 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation="
591 , channel, slave, cylinders, heads, spt);
592 switch (translation) {
593 case ATA_TRANSLATION_NONE:
594 BX_INFO("none");
595 break;
596 case ATA_TRANSLATION_LBA:
597 BX_INFO("lba");
598 spt = 63;
599 sectors /= 63;
600 heads = sectors / 1024;
601 if (heads>128)
602 heads = 255;
603 else if (heads>64)
604 heads = 128;
605 else if (heads>32)
606 heads = 64;
607 else if (heads>16)
608 heads = 32;
609 else
610 heads = 16;
611 cylinders = sectors / heads;
612 break;
613 case ATA_TRANSLATION_RECHS:
614 BX_INFO("r-echs");
615 // Take care not to overflow
616 if (heads==16) {
617 if (cylinders>61439)
618 cylinders=61439;
619 heads=15;
620 cylinders = (u16)((u32)(cylinders)*16/15);
621 }
622 // then go through the large bitshift process
623 case ATA_TRANSLATION_LARGE:
624 if (translation == ATA_TRANSLATION_LARGE)
625 BX_INFO("large");
626 while(cylinders > 1024) {
627 cylinders >>= 1;
628 heads <<= 1;
629
630 // If we max out the head count
631 if (heads > 127)
632 break;
633 }
634 break;
635 }
636 // clip to 1024 cylinders in lchs
637 if (cylinders > 1024)
638 cylinders = 1024;
639 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
640
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400641 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
642 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
643 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400644
645 // fill hdidmap
646 u8 hdcount = GET_EBDA(ata.hdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400647 SET_EBDA(ata.idmap[0][hdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400648 SET_EBDA(ata.hdcount, ++hdcount);
649
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400650 u32 sizeinmb = GET_EBDA(ata.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400651 sizeinmb >>= 11;
652
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400653 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400654 u8 version = get_ata_version(buffer);
655 if (sizeinmb < (1 << 16))
656 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, sizeinmb);
657 else
658 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, sizeinmb >> 10);
659}
660
661static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400662init_drive_unknown(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400663{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400664 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400665
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400666 u8 channel = driveid / 2;
667 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400668 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
669}
670
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500671void
672ata_detect()
673{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500674#if CONFIG_MAX_ATA_INTERFACES > 0
675 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
676 SET_EBDA(ata.channels[0].iobase1,0x1f0);
677 SET_EBDA(ata.channels[0].iobase2,0x3f0);
678 SET_EBDA(ata.channels[0].irq,14);
679#endif
680#if CONFIG_MAX_ATA_INTERFACES > 1
681 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
682 SET_EBDA(ata.channels[1].iobase1,0x170);
683 SET_EBDA(ata.channels[1].iobase2,0x370);
684 SET_EBDA(ata.channels[1].irq,15);
685#endif
686#if CONFIG_MAX_ATA_INTERFACES > 2
687 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
688 SET_EBDA(ata.channels[2].iobase1,0x1e8);
689 SET_EBDA(ata.channels[2].iobase2,0x3e0);
690 SET_EBDA(ata.channels[2].irq,12);
691#endif
692#if CONFIG_MAX_ATA_INTERFACES > 3
693 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
694 SET_EBDA(ata.channels[3].iobase1,0x168);
695 SET_EBDA(ata.channels[3].iobase2,0x360);
696 SET_EBDA(ata.channels[3].irq,11);
697#endif
698#if CONFIG_MAX_ATA_INTERFACES > 4
699#error Please fill the ATA interface informations
700#endif
701
702 // Device detection
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400703 int driveid;
704 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
705 u8 channel = driveid / 2;
706 u8 slave = driveid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500707
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400708 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
709 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500710
711 // Disable interrupts
712 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
713
714 // Look for device
715 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
716 outb(0x55, iobase1+ATA_CB_SC);
717 outb(0xaa, iobase1+ATA_CB_SN);
718 outb(0xaa, iobase1+ATA_CB_SC);
719 outb(0x55, iobase1+ATA_CB_SN);
720 outb(0x55, iobase1+ATA_CB_SC);
721 outb(0xaa, iobase1+ATA_CB_SN);
722
723 // If we found something
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400724 u8 sc = inb(iobase1+ATA_CB_SC);
725 u8 sn = inb(iobase1+ATA_CB_SN);
726
727 if (sc != 0x55 || sn != 0xaa)
728 continue;
729
730 // reset the channel
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400731 ata_reset(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400732
733 // check for ATA or ATAPI
734 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500735 sc = inb(iobase1+ATA_CB_SC);
736 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400737 if (sc!=0x01 || sn!=0x01) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400738 init_drive_unknown(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400739 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500740 }
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400741 u8 cl = inb(iobase1+ATA_CB_CL);
742 u8 ch = inb(iobase1+ATA_CB_CH);
743 u8 st = inb(iobase1+ATA_CB_STAT);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500744
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400745 if (cl==0x14 && ch==0xeb)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400746 init_drive_atapi(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400747 else if (cl==0x00 && ch==0x00 && st!=0x00)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400748 init_drive_ata(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400749 else if (cl==0xff && ch==0xff)
750 // None
751 continue;
752 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400753 init_drive_unknown(driveid);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500754 }
755
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400756 // Store the device count
757 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500758
759 printf("\n");
760
761 // FIXME : should use bios=cmos|auto|disable bits
762 // FIXME : should know about translation bits
763 // FIXME : move hard_drive_post here
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500764}