blob: 0546dcd9f0cf2b128db8505cc2d3d43d8f89e68f [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
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040011#include "util.h" // dprintf
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 }
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040072 dprintf(1, "IDE time out\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050073 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
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400122 // 8.2.1 (g) -- check for sc==sn==0x01
123 // select device
124 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
125 sc = inb(iobase1+ATA_CB_SC);
126 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500127
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400128 // XXX - why special check for ATA and ready?
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);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500134 }
135
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400136 // 8.2.1 (h) -- wait for not BSY
137 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
138
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500139 // Enable interrupts
140 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
141}
142
Kevin O'Connoree55c762008-05-13 00:18:20 -0400143
144/****************************************************************
145 * ATA send command
146 ****************************************************************/
147
148struct ata_op_s {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400149 u64 lba;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400150 void *far_buffer;
151 u16 driveid;
152 u16 count;
153};
154
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400155struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400156 u8 feature;
157 u8 sector_count;
158 u8 lba_low;
159 u8 lba_mid;
160 u8 lba_high;
161 u8 device;
162 u8 command;
163
164 u8 sector_count2;
165 u8 lba_low2;
166 u8 lba_mid2;
167 u8 lba_high2;
168};
169
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400170// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400171static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400172send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500173{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400174 u8 channel = driveid / 2;
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500175 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
176 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500177
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400178 int status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500179 if (status & ATA_CB_STAT_BSY)
180 return 1;
181
182 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400183 if (cmd->command & 0x04) {
184 outb(0x00, iobase1 + ATA_CB_FR);
185 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
186 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
187 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
188 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500189 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400190 outb(cmd->feature, iobase1 + ATA_CB_FR);
191 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
192 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
193 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
194 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
195 outb(cmd->device, iobase1 + ATA_CB_DH);
196 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500197
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400198 nsleep(400);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500199
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400200 status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
201 if (status < 0)
202 return status;
203
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500204 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400205 DEBUGF("send_cmd : read error\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500206 return 2;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400207 }
208 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400209 DEBUGF("send_cmd : DRQ not set (status %02x)\n"
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500210 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500211 return 3;
212 }
213
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400214 return 0;
215}
216
Kevin O'Connoree55c762008-05-13 00:18:20 -0400217
218/****************************************************************
219 * ATA transfers
220 ****************************************************************/
221
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400222// Read and discard x number of bytes from an io channel.
223static void
224insx_discard(int mode, int iobase1, int bytes)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400225{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400226 int count, i;
227 if (mode == ATA_MODE_PIO32) {
228 count = bytes / 4;
229 for (i=0; i<count; i++)
230 inl(iobase1);
231 } else {
232 count = bytes / 2;
233 for (i=0; i<count; i++)
234 inw(iobase1);
235 }
236}
237
238// Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
239// 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
240// and/or last block may be partially transferred. This function is
241// inlined because all the callers use different forms and because the
242// large number of parameters would consume a lot of stack space.
Kevin O'Connor74f9c082008-04-13 16:57:16 -0400243static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400244ata_transfer(int driveid, int iswrite, int count, int blocksize
245 , int skipfirst, int skiplast, void *far_buffer)
246{
247 DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d"
248 " skipf=%d skipl=%d buf=%p\n"
249 , driveid, iswrite, count, blocksize
250 , skipfirst, skiplast, far_buffer);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400251
252 // Reset count of transferred data
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400253 SET_EBDA(ata.trsfsectors, 0);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400254
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400255 u8 channel = driveid / 2;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400256 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
257 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400258 u8 mode = GET_EBDA(ata.devices[driveid].mode);
259 int current = 0;
260 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400261 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400262 int bsize = blocksize;
263 if (skipfirst && current == 0) {
264 insx_discard(mode, iobase1, skipfirst);
265 bsize -= skipfirst;
266 }
267 if (skiplast && current == count-1)
268 bsize -= skiplast;
269
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400270 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400271 // Write data to controller
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400272 DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400273 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400274 outsl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400275 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400276 outsw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500277 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400278 // Read data from controller
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400279 DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400280 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400281 insl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400282 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400283 insw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400284 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400285 far_buffer += bsize;
286
287 if (skiplast && current == count-1)
288 insx_discard(mode, iobase1, skiplast);
289
290 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
291 if (status < 0)
292 // Error
293 return status;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400294
295 current++;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400296 SET_EBDA(ata.trsfsectors, current);
297 if (current == count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400298 break;
299 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
300 | ATA_CB_STAT_ERR);
301 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400302 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400303 , (unsigned) status);
304 return 5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500305 }
306 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400307
308 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
309 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400310 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400311 status &= ~ATA_CB_STAT_DF;
312 if (status != ATA_CB_STAT_RDY ) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400313 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400314 , (unsigned) status);
315 return 4;
316 }
317
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500318 // Enable interrupts
319 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
320 return 0;
321}
322
Kevin O'Connoree55c762008-05-13 00:18:20 -0400323static noinline int
324ata_transfer_disk(const struct ata_op_s *op, int iswrite)
325{
326 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
327 , 0, 0, op->far_buffer);
328}
329
330static noinline int
331ata_transfer_cdrom(const struct ata_op_s *op)
332{
333 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
334 , 0, 0, op->far_buffer);
335}
336
337static noinline int
338ata_transfer_emu(const struct ata_op_s *op, int before, int after)
339{
340 int vcount = op->count * 4 - before - after;
341 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
342 , before*512, after*512, op->far_buffer);
343 if (ret) {
344 SET_EBDA(ata.trsfsectors, 0);
345 return ret;
346 }
347 SET_EBDA(ata.trsfsectors, vcount);
348 return 0;
349}
350
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400351
352/****************************************************************
353 * ATA hard drive functions
354 ****************************************************************/
355
Kevin O'Connoree55c762008-05-13 00:18:20 -0400356static noinline int
357send_cmd_disk(const struct ata_op_s *op, u16 command)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400358{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400359 u8 slave = op->driveid % 2;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400360 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400361
362 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400363 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400364
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400365 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400366 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
367 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400368 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400369 cmd.lba_mid2 = lba >> 32;
370 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400371
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400372 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400373 lba &= 0xffffff;
374 }
375
376 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400377 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400378 cmd.lba_low = lba;
379 cmd.lba_mid = lba >> 8;
380 cmd.lba_high = lba >> 16;
381 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
382 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400383
Kevin O'Connoree55c762008-05-13 00:18:20 -0400384 return send_cmd(op->driveid, &cmd);
385}
386
387// Read/write count blocks from a harddrive.
388__always_inline int
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400389ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
Kevin O'Connoree55c762008-05-13 00:18:20 -0400390{
391 struct ata_op_s op;
392 op.driveid = driveid;
393 op.lba = lba;
394 op.count = count;
395 op.far_buffer = far_buffer;
396
397 int ret = send_cmd_disk(&op, command);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400398 if (ret)
399 return ret;
400
401 int iswrite = command == ATA_CMD_WRITE_SECTORS;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400402 return ata_transfer_disk(&op, iswrite);
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400403}
404
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400405
406/****************************************************************
407 * ATAPI functions
408 ****************************************************************/
409
410// Low-level atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400411static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400412send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500413{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400414 u8 channel = driveid / 2;
415 u8 slave = driveid % 2;
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400416 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
417 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500418
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400419 struct ata_pio_command cmd;
420 cmd.sector_count = 0;
421 cmd.feature = 0;
422 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400423 cmd.lba_mid = blocksize;
424 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400425 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
426 cmd.command = ATA_CMD_PACKET;
427
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400428 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400429 if (ret)
430 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500431
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400432 // Send command to device
Kevin O'Connor070231b2008-03-22 20:44:37 -0400433 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500434
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400435 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
436 if (status < 0)
437 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400438
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500439 return 0;
440}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500441
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400442// Low-level cdrom read atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400443static int
444send_cmd_cdrom(const struct ata_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500445{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400446 u8 atacmd[12];
447 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500448
Kevin O'Connoree55c762008-05-13 00:18:20 -0400449 atacmd[0]=0x28; // READ command
450 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
451 atacmd[8]=(op->count & 0x00ff);
452 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
453 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
454 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
455 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400456
Kevin O'Connoree55c762008-05-13 00:18:20 -0400457 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
458 , CDROM_SECTOR_SIZE);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500459}
460
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400461// Read sectors from the cdrom.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400462__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400463cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400464{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400465 struct ata_op_s op;
466 op.driveid = driveid;
467 op.lba = lba;
468 op.count = count;
469 op.far_buffer = far_buffer;
470
471 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400472 if (ret)
473 return ret;
474
Kevin O'Connoree55c762008-05-13 00:18:20 -0400475 return ata_transfer_cdrom(&op);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400476}
477
478// Pretend the cdrom has 512 byte sectors (instead of 2048) and read
479// sectors.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400480__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400481cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400482{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400483 u32 velba = vlba + vcount - 1;
484 u32 lba = vlba / 4;
485 u32 elba = velba / 4;
486 int count = elba - lba + 1;
487 int before = vlba % 4;
488 int after = 3 - (velba % 4);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400489
Kevin O'Connoree55c762008-05-13 00:18:20 -0400490 struct ata_op_s op;
491 op.driveid = driveid;
492 op.lba = lba;
493 op.count = count;
494 op.far_buffer = far_buffer;
495
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400496 DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
497 " count=%d before=%d after=%d\n"
498 , driveid, vlba, vcount, far_buffer, lba, elba
499 , count, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400500
Kevin O'Connoree55c762008-05-13 00:18:20 -0400501 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400502 if (ret)
503 return ret;
504
Kevin O'Connoree55c762008-05-13 00:18:20 -0400505 return ata_transfer_emu(&op, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400506}
507
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400508// Send a simple atapi command to a drive.
509int
510ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
511 , u32 length, void *far_buffer)
512{
513 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
514 if (ret)
515 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400516
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400517 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
518}
519
520
521/****************************************************************
522 * ATA detect and init
523 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500524
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400525static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400526report_model(int driveid, u8 *buffer)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400527{
528 u8 model[41];
529
530 // Read model name
531 int i;
532 for (i=0; i<40; i+=2) {
533 model[i] = buffer[i+54+1];
534 model[i+1] = buffer[i+54];
535 }
536
537 // Reformat
538 model[40] = 0x00;
539 for (i=39; i>0; i--) {
540 if (model[i] != 0x20)
541 break;
542 model[i] = 0x00;
543 }
544
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400545 u8 channel = driveid / 2;
546 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400547 // XXX - model on stack not %cs
548 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
549}
550
551static u8
552get_ata_version(u8 *buffer)
553{
554 u16 ataversion = *(u16*)&buffer[160];
555 u8 version;
556 for (version=15; version>0; version--)
557 if (ataversion & (1<<version))
558 break;
559 return version;
560}
561
562static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400563init_drive_atapi(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400564{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400565 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400566
567 // Temporary values to do the transfer
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400568 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
569 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400570
571 // Now we send a IDENTIFY command to ATAPI device
572 u8 buffer[0x0200];
573 memset(buffer, 0, sizeof(buffer));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400574 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400575 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400576 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400577 if (ret != 0)
578 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
579
580 u8 type = buffer[1] & 0x1f;
581 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
582 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400583 u16 blksize = CDROM_SECTOR_SIZE;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400584
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400585 SET_EBDA(ata.devices[driveid].device, type);
586 SET_EBDA(ata.devices[driveid].removable, removable);
587 SET_EBDA(ata.devices[driveid].mode, mode);
588 SET_EBDA(ata.devices[driveid].blksize, blksize);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400589
590 // fill cdidmap
591 u8 cdcount = GET_EBDA(ata.cdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400592 SET_EBDA(ata.idmap[1][cdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400593 SET_EBDA(ata.cdcount, ++cdcount);
594
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400595 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400596 u8 version = get_ata_version(buffer);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400597 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400598 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
599 else
600 printf(" ATAPI-%d Device\n", version);
601}
602
603static void
Kevin O'Connorc1437612008-05-18 01:43:07 -0400604fill_fdpt(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400605{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400606 if (driveid > 1)
607 return;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400608
Kevin O'Connorc1437612008-05-18 01:43:07 -0400609 u16 nlc = GET_EBDA(ata.devices[driveid].lchs.cylinders);
610 u16 nlh = GET_EBDA(ata.devices[driveid].lchs.heads);
611 u16 nlspt = GET_EBDA(ata.devices[driveid].lchs.spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400612
Kevin O'Connorc1437612008-05-18 01:43:07 -0400613 u16 npc = GET_EBDA(ata.devices[driveid].pchs.cylinders);
614 u16 nph = GET_EBDA(ata.devices[driveid].pchs.heads);
615 u16 npspt = GET_EBDA(ata.devices[driveid].pchs.spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400616
Kevin O'Connorc1437612008-05-18 01:43:07 -0400617 SET_EBDA(fdpt[driveid].precompensation, 0xffff);
618 SET_EBDA(fdpt[driveid].drive_control_byte, 0xc0 | ((nph > 8) << 3));
619 SET_EBDA(fdpt[driveid].landing_zone, npc);
620 SET_EBDA(fdpt[driveid].cylinders, nlc);
621 SET_EBDA(fdpt[driveid].heads, nlh);
622 SET_EBDA(fdpt[driveid].sectors, nlspt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400623
Kevin O'Connorc1437612008-05-18 01:43:07 -0400624 if (nlc == npc && nlh == nph && nlspt == npspt)
625 // no logical CHS mapping used, just physical CHS
626 // use Standard Fixed Disk Parameter Table (FDPT)
627 return;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400628
Kevin O'Connorc1437612008-05-18 01:43:07 -0400629 // complies with Phoenix style Translated Fixed Disk Parameter
630 // Table (FDPT)
631 SET_EBDA(fdpt[driveid].phys_cylinders, npc);
632 SET_EBDA(fdpt[driveid].phys_heads, nph);
633 SET_EBDA(fdpt[driveid].phys_sectors, npspt);
634 SET_EBDA(fdpt[driveid].a0h_signature, 0xa0);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400635
Kevin O'Connorc1437612008-05-18 01:43:07 -0400636 // Checksum structure.
637 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
638 , fdpt[driveid]));
639 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
640 , fdpt[driveid]) - 1);
641 SET_EBDA(fdpt[driveid].checksum, -sum);
642}
643
644static u8
645get_translation(int driveid)
646{
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400647 if (! CONFIG_COREBOOT) {
648 // Emulators pass in the translation info via nvram.
649 u8 channel = driveid / 2;
650 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
651 translation >>= 2 * (driveid % 4);
652 translation &= 0x03;
653 return translation;
654 }
655
656 // On COREBOOT, use a heuristic to determine translation type.
657 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
658 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
659 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
660
661 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
662 return ATA_TRANSLATION_NONE;
663 if (cylinders * heads <= 131072)
664 return ATA_TRANSLATION_LARGE;
665 return ATA_TRANSLATION_LBA;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400666}
667
668static void
669setup_translation(int driveid)
670{
671 u8 translation = get_translation(driveid);
672 SET_EBDA(ata.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400673
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400674 u8 channel = driveid / 2;
675 u8 slave = driveid % 2;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400676 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
677 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
678 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
679 u64 sectors = GET_EBDA(ata.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400680
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400681 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400682 , channel, slave, cylinders, heads, spt);
683 switch (translation) {
684 case ATA_TRANSLATION_NONE:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400685 dprintf(1, "none");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400686 break;
687 case ATA_TRANSLATION_LBA:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400688 dprintf(1, "lba");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400689 spt = 63;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400690 if (sectors > 63*255*1024) {
691 heads = 255;
692 cylinders = 1024;
693 break;
694 }
695 u32 sect = (u32)sectors / 63;
696 heads = sect / 1024;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400697 if (heads>128)
698 heads = 255;
699 else if (heads>64)
700 heads = 128;
701 else if (heads>32)
702 heads = 64;
703 else if (heads>16)
704 heads = 32;
705 else
706 heads = 16;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400707 cylinders = sect / heads;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400708 break;
709 case ATA_TRANSLATION_RECHS:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400710 dprintf(1, "r-echs");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400711 // Take care not to overflow
712 if (heads==16) {
713 if (cylinders>61439)
714 cylinders=61439;
715 heads=15;
716 cylinders = (u16)((u32)(cylinders)*16/15);
717 }
718 // then go through the large bitshift process
719 case ATA_TRANSLATION_LARGE:
720 if (translation == ATA_TRANSLATION_LARGE)
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400721 dprintf(1, "large");
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400722 while (cylinders > 1024) {
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400723 cylinders >>= 1;
724 heads <<= 1;
725
726 // If we max out the head count
727 if (heads > 127)
728 break;
729 }
730 break;
731 }
732 // clip to 1024 cylinders in lchs
733 if (cylinders > 1024)
734 cylinders = 1024;
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400735 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400736
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400737 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
738 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
739 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400740}
741
742static void
743init_drive_ata(int driveid)
744{
745 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA);
746
747 // Temporary values to do the transfer
748 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
749 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
750
751 // Now we send a IDENTIFY command to ATA device
752 u8 buffer[0x0200];
753 memset(buffer, 0, sizeof(buffer));
754 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
755 , 1, 1
756 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
757 if (ret)
758 BX_PANIC("ata-detect: Failed to detect ATA device\n");
759
760 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
761 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
762 u16 blksize = *(u16*)&buffer[10];
763
764 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
765 u16 heads = *(u16*)&buffer[3*2]; // word 3
766 u16 spt = *(u16*)&buffer[6*2]; // word 6
767
768 u64 sectors;
769 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
770 sectors = *(u64*)&buffer[100*2]; // word 100-103
771 else
772 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
773
774 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
775 SET_EBDA(ata.devices[driveid].removable, removable);
776 SET_EBDA(ata.devices[driveid].mode, mode);
777 SET_EBDA(ata.devices[driveid].blksize, blksize);
778 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
779 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
780 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
781 SET_EBDA(ata.devices[driveid].sectors, sectors);
782
783 // Setup disk geometry translation.
784 setup_translation(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400785
786 // fill hdidmap
787 u8 hdcount = GET_EBDA(ata.hdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400788 SET_EBDA(ata.idmap[0][hdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400789 SET_EBDA(ata.hdcount, ++hdcount);
790
Kevin O'Connorc1437612008-05-18 01:43:07 -0400791 // Fill "fdpt" structure.
792 fill_fdpt(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400793
Kevin O'Connorc1437612008-05-18 01:43:07 -0400794 // Report drive info to user.
795 u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400796 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400797 u8 version = get_ata_version(buffer);
798 if (sizeinmb < (1 << 16))
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400799 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u32)sizeinmb);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400800 else
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400801 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version
802 , (u32)(sizeinmb >> 10));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400803}
804
805static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400806init_drive_unknown(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400807{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400808 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400809
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400810 u8 channel = driveid / 2;
811 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400812 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
813}
814
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400815static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500816ata_detect()
817{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500818 // Device detection
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400819 int driveid;
820 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
821 u8 channel = driveid / 2;
822 u8 slave = driveid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500823
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400824 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
825 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500826
827 // Disable interrupts
828 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
829
830 // Look for device
831 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
832 outb(0x55, iobase1+ATA_CB_SC);
833 outb(0xaa, iobase1+ATA_CB_SN);
834 outb(0xaa, iobase1+ATA_CB_SC);
835 outb(0x55, iobase1+ATA_CB_SN);
836 outb(0x55, iobase1+ATA_CB_SC);
837 outb(0xaa, iobase1+ATA_CB_SN);
838
839 // If we found something
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400840 u8 sc = inb(iobase1+ATA_CB_SC);
841 u8 sn = inb(iobase1+ATA_CB_SN);
842
843 if (sc != 0x55 || sn != 0xaa)
844 continue;
845
846 // reset the channel
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400847 ata_reset(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400848
849 // check for ATA or ATAPI
850 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500851 sc = inb(iobase1+ATA_CB_SC);
852 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400853 if (sc!=0x01 || sn!=0x01) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400854 init_drive_unknown(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400855 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500856 }
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400857 u8 cl = inb(iobase1+ATA_CB_CL);
858 u8 ch = inb(iobase1+ATA_CB_CH);
859 u8 st = inb(iobase1+ATA_CB_STAT);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500860
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400861 if (cl==0x14 && ch==0xeb)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400862 init_drive_atapi(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400863 else if (cl==0x00 && ch==0x00 && st!=0x00)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400864 init_drive_ata(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400865 else if (cl==0xff && ch==0xff)
866 // None
867 continue;
868 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400869 init_drive_unknown(driveid);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500870 }
871
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500872 printf("\n");
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500873}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400874
875static void
876ata_init()
877{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400878 // hdidmap and cdidmap init.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400879 u8 device;
880 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
881 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
882 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
883 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400884
Kevin O'Connorc1437612008-05-18 01:43:07 -0400885#if CONFIG_MAX_ATA_INTERFACES > 0
886 SET_EBDA(ata.channels[0].iface, ATA_IFACE_ISA);
887 SET_EBDA(ata.channels[0].iobase1, 0x1f0);
888 SET_EBDA(ata.channels[0].iobase2, 0x3f0);
889 SET_EBDA(ata.channels[0].irq, 14);
890#endif
891#if CONFIG_MAX_ATA_INTERFACES > 1
892 SET_EBDA(ata.channels[1].iface, ATA_IFACE_ISA);
893 SET_EBDA(ata.channels[1].iobase1, 0x170);
894 SET_EBDA(ata.channels[1].iobase2, 0x370);
895 SET_EBDA(ata.channels[1].irq, 15);
896#endif
897#if CONFIG_MAX_ATA_INTERFACES > 2
898 SET_EBDA(ata.channels[2].iface, ATA_IFACE_ISA);
899 SET_EBDA(ata.channels[2].iobase1, 0x1e8);
900 SET_EBDA(ata.channels[2].iobase2, 0x3e0);
901 SET_EBDA(ata.channels[2].irq, 12);
902#endif
903#if CONFIG_MAX_ATA_INTERFACES > 3
904 SET_EBDA(ata.channels[3].iface, ATA_IFACE_ISA);
905 SET_EBDA(ata.channels[3].iobase1, 0x168);
906 SET_EBDA(ata.channels[3].iobase2, 0x360);
907 SET_EBDA(ata.channels[3].irq, 11);
908#endif
909#if CONFIG_MAX_ATA_INTERFACES > 4
910#error Please fill the ATA interface informations
911#endif
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400912}
913
914void
915hard_drive_setup()
916{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400917 if (!CONFIG_ATA)
918 return;
919
920 ata_init();
921 ata_detect();
922
923 // Store the device count
924 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
925
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400926 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400927}