blob: 998576cd519c773140549f99bd236705ad0a5c28 [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
Kevin O'Connoref2822a2008-06-07 15:23:11 -040095// Wait for ide state - pause for 400ns first.
96static __always_inline int
97ndelay_await_ide(u8 when_done, u16 iobase1, u16 timeout)
98{
99 nsleep(400);
100 return await_ide(when_done, iobase1, timeout);
101}
102
103// Delay for x milliseconds
104static void
105msleep(u32 delay)
106{
107 usleep(delay * 1000);
108}
109
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400110// Reset a drive
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500111void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400112ata_reset(int driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500113{
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400114 u8 channel = driveid / 2;
115 u8 slave = driveid % 2;
116 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
117 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500118
119 // Reset
120
121 // 8.2.1 (a) -- set SRST in DC
122 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
123
124 // 8.2.1 (b) -- wait for BSY
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400125 int status = await_ide(BSY, iobase1, 20);
126 dprintf(6, "ata_reset(1) status=%x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500127
128 // 8.2.1 (f) -- clear SRST
129 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
130
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400131 // 8.2.1 (g) -- check for sc==sn==0x01
132 // select device
133 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400134 msleep(50);
Kevin O'Connor2e3eeeb2008-06-12 22:29:30 -0400135 u8 sc = inb(iobase1+ATA_CB_SC);
136 u8 sn = inb(iobase1+ATA_CB_SN);
137
138 // For predetermined ATA drives - wait for ready.
139 if (sc==0x01 && sn==0x01) {
140 u8 type=GET_EBDA(ata.devices[driveid].type);
141 if (type == ATA_TYPE_ATA)
142 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
143 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500144
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400145 // 8.2.1 (h) -- wait for not BSY
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400146 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
147 dprintf(6, "ata_reset(2) status=%x\n", status);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400148
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500149 // Enable interrupts
150 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
151}
152
Kevin O'Connoree55c762008-05-13 00:18:20 -0400153
154/****************************************************************
155 * ATA send command
156 ****************************************************************/
157
158struct ata_op_s {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400159 u64 lba;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400160 void *far_buffer;
161 u16 driveid;
162 u16 count;
163};
164
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400165struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400166 u8 feature;
167 u8 sector_count;
168 u8 lba_low;
169 u8 lba_mid;
170 u8 lba_high;
171 u8 device;
172 u8 command;
173
174 u8 sector_count2;
175 u8 lba_low2;
176 u8 lba_mid2;
177 u8 lba_high2;
178};
179
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400180// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400181static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400182send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500183{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400184 u8 channel = driveid / 2;
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500185 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
186 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500187
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400188 int status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500189 if (status & ATA_CB_STAT_BSY)
190 return 1;
191
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400192 // Disable interrupts
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500193 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400194
195 // Select device
196 u8 device = inb(iobase1 + ATA_CB_DH);
197 outb(cmd->device, iobase1 + ATA_CB_DH);
198 if ((device ^ cmd->device) & (1UL << 4))
199 // Wait for device to become active.
200 msleep(50);
201
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400202 if (cmd->command & 0x04) {
203 outb(0x00, iobase1 + ATA_CB_FR);
204 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
205 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
206 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
207 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500208 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400209 outb(cmd->feature, iobase1 + ATA_CB_FR);
210 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
211 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
212 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
213 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400214 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500215
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400216 status = ndelay_await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400217 if (status < 0)
218 return status;
219
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500220 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400221 DEBUGF("send_cmd : read error\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500222 return 2;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400223 }
224 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400225 DEBUGF("send_cmd : DRQ not set (status %02x)\n"
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500226 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500227 return 3;
228 }
229
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400230 return 0;
231}
232
Kevin O'Connoree55c762008-05-13 00:18:20 -0400233
234/****************************************************************
235 * ATA transfers
236 ****************************************************************/
237
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400238// Read and discard x number of bytes from an io channel.
239static void
240insx_discard(int mode, int iobase1, int bytes)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400241{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400242 int count, i;
243 if (mode == ATA_MODE_PIO32) {
244 count = bytes / 4;
245 for (i=0; i<count; i++)
246 inl(iobase1);
247 } else {
248 count = bytes / 2;
249 for (i=0; i<count; i++)
250 inw(iobase1);
251 }
252}
253
254// Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
255// 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
256// and/or last block may be partially transferred. This function is
257// inlined because all the callers use different forms and because the
258// large number of parameters would consume a lot of stack space.
Kevin O'Connor74f9c082008-04-13 16:57:16 -0400259static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400260ata_transfer(int driveid, int iswrite, int count, int blocksize
261 , int skipfirst, int skiplast, void *far_buffer)
262{
263 DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d"
264 " skipf=%d skipl=%d buf=%p\n"
265 , driveid, iswrite, count, blocksize
266 , skipfirst, skiplast, far_buffer);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400267
268 // Reset count of transferred data
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400269 SET_EBDA(ata.trsfsectors, 0);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400270
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400271 u8 channel = driveid / 2;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400272 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
273 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400274 u8 mode = GET_EBDA(ata.devices[driveid].mode);
275 int current = 0;
276 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400277 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400278 int bsize = blocksize;
279 if (skipfirst && current == 0) {
280 insx_discard(mode, iobase1, skipfirst);
281 bsize -= skipfirst;
282 }
283 if (skiplast && current == count-1)
284 bsize -= skiplast;
285
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400286 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400287 // Write data to controller
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400288 DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400289 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400290 outsl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400291 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400292 outsw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500293 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400294 // Read data from controller
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400295 DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400296 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400297 insl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400298 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400299 insw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400300 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400301 far_buffer += bsize;
302
303 if (skiplast && current == count-1)
304 insx_discard(mode, iobase1, skiplast);
305
306 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
307 if (status < 0)
308 // Error
309 return status;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400310
311 current++;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400312 SET_EBDA(ata.trsfsectors, current);
313 if (current == count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400314 break;
315 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
316 | ATA_CB_STAT_ERR);
317 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400318 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400319 , (unsigned) status);
320 return 5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500321 }
322 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400323
324 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
325 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400326 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400327 status &= ~ATA_CB_STAT_DF;
328 if (status != ATA_CB_STAT_RDY ) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400329 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400330 , (unsigned) status);
331 return 4;
332 }
333
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500334 // Enable interrupts
335 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
336 return 0;
337}
338
Kevin O'Connoree55c762008-05-13 00:18:20 -0400339static noinline int
340ata_transfer_disk(const struct ata_op_s *op, int iswrite)
341{
342 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
343 , 0, 0, op->far_buffer);
344}
345
346static noinline int
347ata_transfer_cdrom(const struct ata_op_s *op)
348{
349 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
350 , 0, 0, op->far_buffer);
351}
352
353static noinline int
354ata_transfer_emu(const struct ata_op_s *op, int before, int after)
355{
356 int vcount = op->count * 4 - before - after;
357 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
358 , before*512, after*512, op->far_buffer);
359 if (ret) {
360 SET_EBDA(ata.trsfsectors, 0);
361 return ret;
362 }
363 SET_EBDA(ata.trsfsectors, vcount);
364 return 0;
365}
366
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400367
368/****************************************************************
369 * ATA hard drive functions
370 ****************************************************************/
371
Kevin O'Connoree55c762008-05-13 00:18:20 -0400372static noinline int
373send_cmd_disk(const struct ata_op_s *op, u16 command)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400374{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400375 u8 slave = op->driveid % 2;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400376 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400377
378 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400379 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400380
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400381 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400382 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
383 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400384 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400385 cmd.lba_mid2 = lba >> 32;
386 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400387
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400388 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400389 lba &= 0xffffff;
390 }
391
392 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400393 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400394 cmd.lba_low = lba;
395 cmd.lba_mid = lba >> 8;
396 cmd.lba_high = lba >> 16;
397 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
398 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400399
Kevin O'Connoree55c762008-05-13 00:18:20 -0400400 return send_cmd(op->driveid, &cmd);
401}
402
403// Read/write count blocks from a harddrive.
404__always_inline int
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400405ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
Kevin O'Connoree55c762008-05-13 00:18:20 -0400406{
407 struct ata_op_s op;
408 op.driveid = driveid;
409 op.lba = lba;
410 op.count = count;
411 op.far_buffer = far_buffer;
412
413 int ret = send_cmd_disk(&op, command);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400414 if (ret)
415 return ret;
416
417 int iswrite = command == ATA_CMD_WRITE_SECTORS;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400418 return ata_transfer_disk(&op, iswrite);
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400419}
420
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400421
422/****************************************************************
423 * ATAPI functions
424 ****************************************************************/
425
426// Low-level atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400427static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400428send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500429{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400430 u8 channel = driveid / 2;
431 u8 slave = driveid % 2;
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400432 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
433 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500434
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400435 struct ata_pio_command cmd;
436 cmd.sector_count = 0;
437 cmd.feature = 0;
438 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400439 cmd.lba_mid = blocksize;
440 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400441 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
442 cmd.command = ATA_CMD_PACKET;
443
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400444 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400445 if (ret)
446 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500447
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400448 // Send command to device
Kevin O'Connor070231b2008-03-22 20:44:37 -0400449 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500450
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400451 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
452 if (status < 0)
453 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400454
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500455 return 0;
456}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500457
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400458// Low-level cdrom read atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400459static int
460send_cmd_cdrom(const struct ata_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500461{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400462 u8 atacmd[12];
463 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500464
Kevin O'Connoree55c762008-05-13 00:18:20 -0400465 atacmd[0]=0x28; // READ command
466 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
467 atacmd[8]=(op->count & 0x00ff);
468 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
469 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
470 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
471 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400472
Kevin O'Connoree55c762008-05-13 00:18:20 -0400473 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
474 , CDROM_SECTOR_SIZE);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500475}
476
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400477// Read sectors from the cdrom.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400478__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400479cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400480{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400481 struct ata_op_s op;
482 op.driveid = driveid;
483 op.lba = lba;
484 op.count = count;
485 op.far_buffer = far_buffer;
486
487 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400488 if (ret)
489 return ret;
490
Kevin O'Connoree55c762008-05-13 00:18:20 -0400491 return ata_transfer_cdrom(&op);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400492}
493
494// Pretend the cdrom has 512 byte sectors (instead of 2048) and read
495// sectors.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400496__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400497cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400498{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400499 u32 velba = vlba + vcount - 1;
500 u32 lba = vlba / 4;
501 u32 elba = velba / 4;
502 int count = elba - lba + 1;
503 int before = vlba % 4;
504 int after = 3 - (velba % 4);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400505
Kevin O'Connoree55c762008-05-13 00:18:20 -0400506 struct ata_op_s op;
507 op.driveid = driveid;
508 op.lba = lba;
509 op.count = count;
510 op.far_buffer = far_buffer;
511
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400512 DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
513 " count=%d before=%d after=%d\n"
514 , driveid, vlba, vcount, far_buffer, lba, elba
515 , count, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400516
Kevin O'Connoree55c762008-05-13 00:18:20 -0400517 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400518 if (ret)
519 return ret;
520
Kevin O'Connoree55c762008-05-13 00:18:20 -0400521 return ata_transfer_emu(&op, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400522}
523
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400524// Send a simple atapi command to a drive.
525int
526ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
527 , u32 length, void *far_buffer)
528{
529 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
530 if (ret)
531 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400532
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400533 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
534}
535
536
537/****************************************************************
538 * ATA detect and init
539 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500540
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400541static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400542report_model(int driveid, u8 *buffer)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400543{
544 u8 model[41];
545
546 // Read model name
547 int i;
548 for (i=0; i<40; i+=2) {
549 model[i] = buffer[i+54+1];
550 model[i+1] = buffer[i+54];
551 }
552
553 // Reformat
554 model[40] = 0x00;
555 for (i=39; i>0; i--) {
556 if (model[i] != 0x20)
557 break;
558 model[i] = 0x00;
559 }
560
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400561 u8 channel = driveid / 2;
562 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400563 // XXX - model on stack not %cs
564 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
565}
566
567static u8
568get_ata_version(u8 *buffer)
569{
570 u16 ataversion = *(u16*)&buffer[160];
571 u8 version;
572 for (version=15; version>0; version--)
573 if (ataversion & (1<<version))
574 break;
575 return version;
576}
577
578static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400579init_drive_atapi(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400580{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400581 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400582
583 // Temporary values to do the transfer
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400584 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
585 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400586
587 // Now we send a IDENTIFY command to ATAPI device
588 u8 buffer[0x0200];
589 memset(buffer, 0, sizeof(buffer));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400590 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400591 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400592 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400593 if (ret != 0)
594 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
595
596 u8 type = buffer[1] & 0x1f;
597 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
598 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400599 u16 blksize = CDROM_SECTOR_SIZE;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400600
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400601 SET_EBDA(ata.devices[driveid].device, type);
602 SET_EBDA(ata.devices[driveid].removable, removable);
603 SET_EBDA(ata.devices[driveid].mode, mode);
604 SET_EBDA(ata.devices[driveid].blksize, blksize);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400605
606 // fill cdidmap
607 u8 cdcount = GET_EBDA(ata.cdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400608 SET_EBDA(ata.idmap[1][cdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400609 SET_EBDA(ata.cdcount, ++cdcount);
610
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400611 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400612 u8 version = get_ata_version(buffer);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400613 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400614 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
615 else
616 printf(" ATAPI-%d Device\n", version);
617}
618
619static void
Kevin O'Connorc1437612008-05-18 01:43:07 -0400620fill_fdpt(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400621{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400622 if (driveid > 1)
623 return;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400624
Kevin O'Connorc1437612008-05-18 01:43:07 -0400625 u16 nlc = GET_EBDA(ata.devices[driveid].lchs.cylinders);
626 u16 nlh = GET_EBDA(ata.devices[driveid].lchs.heads);
627 u16 nlspt = GET_EBDA(ata.devices[driveid].lchs.spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400628
Kevin O'Connorc1437612008-05-18 01:43:07 -0400629 u16 npc = GET_EBDA(ata.devices[driveid].pchs.cylinders);
630 u16 nph = GET_EBDA(ata.devices[driveid].pchs.heads);
631 u16 npspt = GET_EBDA(ata.devices[driveid].pchs.spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400632
Kevin O'Connorc1437612008-05-18 01:43:07 -0400633 SET_EBDA(fdpt[driveid].precompensation, 0xffff);
634 SET_EBDA(fdpt[driveid].drive_control_byte, 0xc0 | ((nph > 8) << 3));
635 SET_EBDA(fdpt[driveid].landing_zone, npc);
636 SET_EBDA(fdpt[driveid].cylinders, nlc);
637 SET_EBDA(fdpt[driveid].heads, nlh);
638 SET_EBDA(fdpt[driveid].sectors, nlspt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400639
Kevin O'Connorc1437612008-05-18 01:43:07 -0400640 if (nlc == npc && nlh == nph && nlspt == npspt)
641 // no logical CHS mapping used, just physical CHS
642 // use Standard Fixed Disk Parameter Table (FDPT)
643 return;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400644
Kevin O'Connorc1437612008-05-18 01:43:07 -0400645 // complies with Phoenix style Translated Fixed Disk Parameter
646 // Table (FDPT)
647 SET_EBDA(fdpt[driveid].phys_cylinders, npc);
648 SET_EBDA(fdpt[driveid].phys_heads, nph);
649 SET_EBDA(fdpt[driveid].phys_sectors, npspt);
650 SET_EBDA(fdpt[driveid].a0h_signature, 0xa0);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400651
Kevin O'Connorc1437612008-05-18 01:43:07 -0400652 // Checksum structure.
653 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
654 , fdpt[driveid]));
655 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
656 , fdpt[driveid]) - 1);
657 SET_EBDA(fdpt[driveid].checksum, -sum);
658}
659
660static u8
661get_translation(int driveid)
662{
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400663 if (! CONFIG_COREBOOT) {
664 // Emulators pass in the translation info via nvram.
665 u8 channel = driveid / 2;
666 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
667 translation >>= 2 * (driveid % 4);
668 translation &= 0x03;
669 return translation;
670 }
671
672 // On COREBOOT, use a heuristic to determine translation type.
673 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
674 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
675 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
676
677 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
678 return ATA_TRANSLATION_NONE;
679 if (cylinders * heads <= 131072)
680 return ATA_TRANSLATION_LARGE;
681 return ATA_TRANSLATION_LBA;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400682}
683
684static void
685setup_translation(int driveid)
686{
687 u8 translation = get_translation(driveid);
688 SET_EBDA(ata.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400689
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400690 u8 channel = driveid / 2;
691 u8 slave = driveid % 2;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400692 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
693 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
694 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
695 u64 sectors = GET_EBDA(ata.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400696
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400697 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400698 , channel, slave, cylinders, heads, spt);
699 switch (translation) {
700 case ATA_TRANSLATION_NONE:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400701 dprintf(1, "none");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400702 break;
703 case ATA_TRANSLATION_LBA:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400704 dprintf(1, "lba");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400705 spt = 63;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400706 if (sectors > 63*255*1024) {
707 heads = 255;
708 cylinders = 1024;
709 break;
710 }
711 u32 sect = (u32)sectors / 63;
712 heads = sect / 1024;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400713 if (heads>128)
714 heads = 255;
715 else if (heads>64)
716 heads = 128;
717 else if (heads>32)
718 heads = 64;
719 else if (heads>16)
720 heads = 32;
721 else
722 heads = 16;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400723 cylinders = sect / heads;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400724 break;
725 case ATA_TRANSLATION_RECHS:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400726 dprintf(1, "r-echs");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400727 // Take care not to overflow
728 if (heads==16) {
729 if (cylinders>61439)
730 cylinders=61439;
731 heads=15;
732 cylinders = (u16)((u32)(cylinders)*16/15);
733 }
734 // then go through the large bitshift process
735 case ATA_TRANSLATION_LARGE:
736 if (translation == ATA_TRANSLATION_LARGE)
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400737 dprintf(1, "large");
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400738 while (cylinders > 1024) {
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400739 cylinders >>= 1;
740 heads <<= 1;
741
742 // If we max out the head count
743 if (heads > 127)
744 break;
745 }
746 break;
747 }
748 // clip to 1024 cylinders in lchs
749 if (cylinders > 1024)
750 cylinders = 1024;
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400751 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400752
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400753 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
754 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
755 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400756}
757
758static void
759init_drive_ata(int driveid)
760{
761 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA);
762
763 // Temporary values to do the transfer
764 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
765 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
766
767 // Now we send a IDENTIFY command to ATA device
768 u8 buffer[0x0200];
769 memset(buffer, 0, sizeof(buffer));
770 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
771 , 1, 1
772 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
773 if (ret)
774 BX_PANIC("ata-detect: Failed to detect ATA device\n");
775
776 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
777 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
778 u16 blksize = *(u16*)&buffer[10];
779
780 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
781 u16 heads = *(u16*)&buffer[3*2]; // word 3
782 u16 spt = *(u16*)&buffer[6*2]; // word 6
783
784 u64 sectors;
785 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
786 sectors = *(u64*)&buffer[100*2]; // word 100-103
787 else
788 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
789
790 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
791 SET_EBDA(ata.devices[driveid].removable, removable);
792 SET_EBDA(ata.devices[driveid].mode, mode);
793 SET_EBDA(ata.devices[driveid].blksize, blksize);
794 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
795 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
796 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
797 SET_EBDA(ata.devices[driveid].sectors, sectors);
798
799 // Setup disk geometry translation.
800 setup_translation(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400801
802 // fill hdidmap
803 u8 hdcount = GET_EBDA(ata.hdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400804 SET_EBDA(ata.idmap[0][hdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400805 SET_EBDA(ata.hdcount, ++hdcount);
806
Kevin O'Connorc1437612008-05-18 01:43:07 -0400807 // Fill "fdpt" structure.
808 fill_fdpt(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400809
Kevin O'Connorc1437612008-05-18 01:43:07 -0400810 // Report drive info to user.
811 u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400812 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400813 u8 version = get_ata_version(buffer);
814 if (sizeinmb < (1 << 16))
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400815 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u32)sizeinmb);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400816 else
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400817 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version
818 , (u32)(sizeinmb >> 10));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400819}
820
821static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400822init_drive_unknown(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400823{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400824 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400825
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400826 u8 channel = driveid / 2;
827 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400828 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
829}
830
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400831static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500832ata_detect()
833{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500834 // Device detection
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400835 int driveid;
836 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
837 u8 channel = driveid / 2;
838 u8 slave = driveid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500839
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400840 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
841 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500842
843 // Disable interrupts
844 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
845
846 // Look for device
847 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400848 msleep(50);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500849 outb(0x55, iobase1+ATA_CB_SC);
850 outb(0xaa, iobase1+ATA_CB_SN);
851 outb(0xaa, iobase1+ATA_CB_SC);
852 outb(0x55, iobase1+ATA_CB_SN);
853 outb(0x55, iobase1+ATA_CB_SC);
854 outb(0xaa, iobase1+ATA_CB_SN);
855
856 // If we found something
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400857 u8 sc = inb(iobase1+ATA_CB_SC);
858 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400859 dprintf(6, "ata_detect(1) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400860
861 if (sc != 0x55 || sn != 0xaa)
862 continue;
863
864 // reset the channel
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400865 ata_reset(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400866
867 // check for ATA or ATAPI
868 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400869 msleep(50);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500870 sc = inb(iobase1+ATA_CB_SC);
871 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400872 dprintf(6, "ata_detect(2) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400873 if (sc!=0x01 || sn!=0x01) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400874 init_drive_unknown(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400875 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500876 }
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400877 u8 cl = inb(iobase1+ATA_CB_CL);
878 u8 ch = inb(iobase1+ATA_CB_CH);
879 u8 st = inb(iobase1+ATA_CB_STAT);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400880 dprintf(6, "ata_detect(3) drive=%d sc=%x sn=%x cl=%x ch=%x st=%x\n"
881 , driveid, sc, sn, cl, ch, st);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500882
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400883 if (cl==0x14 && ch==0xeb)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400884 init_drive_atapi(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400885 else if (cl==0x00 && ch==0x00 && st!=0x00)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400886 init_drive_ata(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400887 else if (cl==0xff && ch==0xff)
888 // None
889 continue;
890 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400891 init_drive_unknown(driveid);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500892 }
893
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500894 printf("\n");
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500895}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400896
897static void
898ata_init()
899{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400900 // hdidmap and cdidmap init.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400901 u8 device;
902 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
903 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
904 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
905 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400906
Kevin O'Connorc1437612008-05-18 01:43:07 -0400907#if CONFIG_MAX_ATA_INTERFACES > 0
908 SET_EBDA(ata.channels[0].iface, ATA_IFACE_ISA);
909 SET_EBDA(ata.channels[0].iobase1, 0x1f0);
910 SET_EBDA(ata.channels[0].iobase2, 0x3f0);
911 SET_EBDA(ata.channels[0].irq, 14);
912#endif
913#if CONFIG_MAX_ATA_INTERFACES > 1
914 SET_EBDA(ata.channels[1].iface, ATA_IFACE_ISA);
915 SET_EBDA(ata.channels[1].iobase1, 0x170);
916 SET_EBDA(ata.channels[1].iobase2, 0x370);
917 SET_EBDA(ata.channels[1].irq, 15);
918#endif
919#if CONFIG_MAX_ATA_INTERFACES > 2
920 SET_EBDA(ata.channels[2].iface, ATA_IFACE_ISA);
921 SET_EBDA(ata.channels[2].iobase1, 0x1e8);
922 SET_EBDA(ata.channels[2].iobase2, 0x3e0);
923 SET_EBDA(ata.channels[2].irq, 12);
924#endif
925#if CONFIG_MAX_ATA_INTERFACES > 3
926 SET_EBDA(ata.channels[3].iface, ATA_IFACE_ISA);
927 SET_EBDA(ata.channels[3].iobase1, 0x168);
928 SET_EBDA(ata.channels[3].iobase2, 0x360);
929 SET_EBDA(ata.channels[3].irq, 11);
930#endif
931#if CONFIG_MAX_ATA_INTERFACES > 4
932#error Please fill the ATA interface informations
933#endif
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400934}
935
936void
937hard_drive_setup()
938{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400939 if (!CONFIG_ATA)
940 return;
941
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400942 dprintf(3, "init hard drives\n");
Kevin O'Connorc1437612008-05-18 01:43:07 -0400943 ata_init();
944 ata_detect();
945
946 // Store the device count
947 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
948
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400949 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400950}