blob: 62f55d1f0fac9faa84e8f039f191ca1d914bd631 [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'Connor3491e8b2008-02-29 00:22:27 -0500135
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400136 // 8.2.1 (h) -- wait for not BSY
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400137 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
138 dprintf(6, "ata_reset(2) status=%x\n", status);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400139
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500140 // Enable interrupts
141 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
142}
143
Kevin O'Connoree55c762008-05-13 00:18:20 -0400144
145/****************************************************************
146 * ATA send command
147 ****************************************************************/
148
149struct ata_op_s {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400150 u64 lba;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400151 void *far_buffer;
152 u16 driveid;
153 u16 count;
154};
155
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400156struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400157 u8 feature;
158 u8 sector_count;
159 u8 lba_low;
160 u8 lba_mid;
161 u8 lba_high;
162 u8 device;
163 u8 command;
164
165 u8 sector_count2;
166 u8 lba_low2;
167 u8 lba_mid2;
168 u8 lba_high2;
169};
170
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400171// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400172static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400173send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500174{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400175 u8 channel = driveid / 2;
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500176 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
177 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500178
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400179 int status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500180 if (status & ATA_CB_STAT_BSY)
181 return 1;
182
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400183 // Disable interrupts
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500184 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400185
186 // Select device
187 u8 device = inb(iobase1 + ATA_CB_DH);
188 outb(cmd->device, iobase1 + ATA_CB_DH);
189 if ((device ^ cmd->device) & (1UL << 4))
190 // Wait for device to become active.
191 msleep(50);
192
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400193 if (cmd->command & 0x04) {
194 outb(0x00, iobase1 + ATA_CB_FR);
195 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
196 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
197 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
198 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500199 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400200 outb(cmd->feature, iobase1 + ATA_CB_FR);
201 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
202 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
203 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
204 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400205 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500206
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400207 status = ndelay_await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400208 if (status < 0)
209 return status;
210
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500211 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400212 DEBUGF("send_cmd : read error\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500213 return 2;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400214 }
215 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400216 DEBUGF("send_cmd : DRQ not set (status %02x)\n"
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500217 , (unsigned) status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500218 return 3;
219 }
220
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400221 return 0;
222}
223
Kevin O'Connoree55c762008-05-13 00:18:20 -0400224
225/****************************************************************
226 * ATA transfers
227 ****************************************************************/
228
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400229// Read and discard x number of bytes from an io channel.
230static void
231insx_discard(int mode, int iobase1, int bytes)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400232{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400233 int count, i;
234 if (mode == ATA_MODE_PIO32) {
235 count = bytes / 4;
236 for (i=0; i<count; i++)
237 inl(iobase1);
238 } else {
239 count = bytes / 2;
240 for (i=0; i<count; i++)
241 inw(iobase1);
242 }
243}
244
245// Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
246// 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
247// and/or last block may be partially transferred. This function is
248// inlined because all the callers use different forms and because the
249// large number of parameters would consume a lot of stack space.
Kevin O'Connor74f9c082008-04-13 16:57:16 -0400250static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400251ata_transfer(int driveid, int iswrite, int count, int blocksize
252 , int skipfirst, int skiplast, void *far_buffer)
253{
254 DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d"
255 " skipf=%d skipl=%d buf=%p\n"
256 , driveid, iswrite, count, blocksize
257 , skipfirst, skiplast, far_buffer);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400258
259 // Reset count of transferred data
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400260 SET_EBDA(ata.trsfsectors, 0);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400261
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400262 u8 channel = driveid / 2;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400263 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
264 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400265 u8 mode = GET_EBDA(ata.devices[driveid].mode);
266 int current = 0;
267 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400268 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400269 int bsize = blocksize;
270 if (skipfirst && current == 0) {
271 insx_discard(mode, iobase1, skipfirst);
272 bsize -= skipfirst;
273 }
274 if (skiplast && current == count-1)
275 bsize -= skiplast;
276
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400277 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400278 // Write data to controller
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400279 DEBUGF("Write 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 outsl_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 outsw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500284 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400285 // Read data from controller
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400286 DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400287 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400288 insl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400289 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400290 insw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400291 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400292 far_buffer += bsize;
293
294 if (skiplast && current == count-1)
295 insx_discard(mode, iobase1, skiplast);
296
297 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
298 if (status < 0)
299 // Error
300 return status;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400301
302 current++;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400303 SET_EBDA(ata.trsfsectors, current);
304 if (current == count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400305 break;
306 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
307 | ATA_CB_STAT_ERR);
308 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400309 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400310 , (unsigned) status);
311 return 5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500312 }
313 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400314
315 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
316 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400317 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400318 status &= ~ATA_CB_STAT_DF;
319 if (status != ATA_CB_STAT_RDY ) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400320 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
Kevin O'Connor3a049632008-03-11 11:48:04 -0400321 , (unsigned) status);
322 return 4;
323 }
324
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500325 // Enable interrupts
326 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
327 return 0;
328}
329
Kevin O'Connoree55c762008-05-13 00:18:20 -0400330static noinline int
331ata_transfer_disk(const struct ata_op_s *op, int iswrite)
332{
333 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
334 , 0, 0, op->far_buffer);
335}
336
337static noinline int
338ata_transfer_cdrom(const struct ata_op_s *op)
339{
340 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
341 , 0, 0, op->far_buffer);
342}
343
344static noinline int
345ata_transfer_emu(const struct ata_op_s *op, int before, int after)
346{
347 int vcount = op->count * 4 - before - after;
348 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
349 , before*512, after*512, op->far_buffer);
350 if (ret) {
351 SET_EBDA(ata.trsfsectors, 0);
352 return ret;
353 }
354 SET_EBDA(ata.trsfsectors, vcount);
355 return 0;
356}
357
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400358
359/****************************************************************
360 * ATA hard drive functions
361 ****************************************************************/
362
Kevin O'Connoree55c762008-05-13 00:18:20 -0400363static noinline int
364send_cmd_disk(const struct ata_op_s *op, u16 command)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400365{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400366 u8 slave = op->driveid % 2;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400367 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400368
369 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400370 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400371
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400372 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400373 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
374 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400375 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400376 cmd.lba_mid2 = lba >> 32;
377 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400378
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400379 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400380 lba &= 0xffffff;
381 }
382
383 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400384 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400385 cmd.lba_low = lba;
386 cmd.lba_mid = lba >> 8;
387 cmd.lba_high = lba >> 16;
388 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
389 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400390
Kevin O'Connoree55c762008-05-13 00:18:20 -0400391 return send_cmd(op->driveid, &cmd);
392}
393
394// Read/write count blocks from a harddrive.
395__always_inline int
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400396ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
Kevin O'Connoree55c762008-05-13 00:18:20 -0400397{
398 struct ata_op_s op;
399 op.driveid = driveid;
400 op.lba = lba;
401 op.count = count;
402 op.far_buffer = far_buffer;
403
404 int ret = send_cmd_disk(&op, command);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400405 if (ret)
406 return ret;
407
408 int iswrite = command == ATA_CMD_WRITE_SECTORS;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400409 return ata_transfer_disk(&op, iswrite);
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400410}
411
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400412
413/****************************************************************
414 * ATAPI functions
415 ****************************************************************/
416
417// Low-level atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400418static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400419send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500420{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400421 u8 channel = driveid / 2;
422 u8 slave = driveid % 2;
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400423 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
424 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500425
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400426 struct ata_pio_command cmd;
427 cmd.sector_count = 0;
428 cmd.feature = 0;
429 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400430 cmd.lba_mid = blocksize;
431 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400432 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
433 cmd.command = ATA_CMD_PACKET;
434
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400435 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400436 if (ret)
437 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500438
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400439 // Send command to device
Kevin O'Connor070231b2008-03-22 20:44:37 -0400440 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500441
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400442 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
443 if (status < 0)
444 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400445
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500446 return 0;
447}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500448
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400449// Low-level cdrom read atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400450static int
451send_cmd_cdrom(const struct ata_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500452{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400453 u8 atacmd[12];
454 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500455
Kevin O'Connoree55c762008-05-13 00:18:20 -0400456 atacmd[0]=0x28; // READ command
457 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
458 atacmd[8]=(op->count & 0x00ff);
459 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
460 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
461 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
462 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400463
Kevin O'Connoree55c762008-05-13 00:18:20 -0400464 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
465 , CDROM_SECTOR_SIZE);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500466}
467
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400468// Read sectors from the cdrom.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400469__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400470cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400471{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400472 struct ata_op_s op;
473 op.driveid = driveid;
474 op.lba = lba;
475 op.count = count;
476 op.far_buffer = far_buffer;
477
478 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400479 if (ret)
480 return ret;
481
Kevin O'Connoree55c762008-05-13 00:18:20 -0400482 return ata_transfer_cdrom(&op);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400483}
484
485// Pretend the cdrom has 512 byte sectors (instead of 2048) and read
486// sectors.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400487__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400488cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400489{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400490 u32 velba = vlba + vcount - 1;
491 u32 lba = vlba / 4;
492 u32 elba = velba / 4;
493 int count = elba - lba + 1;
494 int before = vlba % 4;
495 int after = 3 - (velba % 4);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400496
Kevin O'Connoree55c762008-05-13 00:18:20 -0400497 struct ata_op_s op;
498 op.driveid = driveid;
499 op.lba = lba;
500 op.count = count;
501 op.far_buffer = far_buffer;
502
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400503 DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
504 " count=%d before=%d after=%d\n"
505 , driveid, vlba, vcount, far_buffer, lba, elba
506 , count, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400507
Kevin O'Connoree55c762008-05-13 00:18:20 -0400508 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400509 if (ret)
510 return ret;
511
Kevin O'Connoree55c762008-05-13 00:18:20 -0400512 return ata_transfer_emu(&op, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400513}
514
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400515// Send a simple atapi command to a drive.
516int
517ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
518 , u32 length, void *far_buffer)
519{
520 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
521 if (ret)
522 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400523
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400524 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
525}
526
527
528/****************************************************************
529 * ATA detect and init
530 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500531
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400532static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400533report_model(int driveid, u8 *buffer)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400534{
535 u8 model[41];
536
537 // Read model name
538 int i;
539 for (i=0; i<40; i+=2) {
540 model[i] = buffer[i+54+1];
541 model[i+1] = buffer[i+54];
542 }
543
544 // Reformat
545 model[40] = 0x00;
546 for (i=39; i>0; i--) {
547 if (model[i] != 0x20)
548 break;
549 model[i] = 0x00;
550 }
551
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400552 u8 channel = driveid / 2;
553 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400554 // XXX - model on stack not %cs
555 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
556}
557
558static u8
559get_ata_version(u8 *buffer)
560{
561 u16 ataversion = *(u16*)&buffer[160];
562 u8 version;
563 for (version=15; version>0; version--)
564 if (ataversion & (1<<version))
565 break;
566 return version;
567}
568
569static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400570init_drive_atapi(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400571{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400572 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400573
574 // Temporary values to do the transfer
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400575 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
576 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400577
578 // Now we send a IDENTIFY command to ATAPI device
579 u8 buffer[0x0200];
580 memset(buffer, 0, sizeof(buffer));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400581 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400582 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400583 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400584 if (ret != 0)
585 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
586
587 u8 type = buffer[1] & 0x1f;
588 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
589 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400590 u16 blksize = CDROM_SECTOR_SIZE;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400591
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400592 SET_EBDA(ata.devices[driveid].device, type);
593 SET_EBDA(ata.devices[driveid].removable, removable);
594 SET_EBDA(ata.devices[driveid].mode, mode);
595 SET_EBDA(ata.devices[driveid].blksize, blksize);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400596
597 // fill cdidmap
598 u8 cdcount = GET_EBDA(ata.cdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400599 SET_EBDA(ata.idmap[1][cdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400600 SET_EBDA(ata.cdcount, ++cdcount);
601
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400602 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400603 u8 version = get_ata_version(buffer);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400604 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400605 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
606 else
607 printf(" ATAPI-%d Device\n", version);
608}
609
610static void
Kevin O'Connorc1437612008-05-18 01:43:07 -0400611fill_fdpt(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400612{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400613 if (driveid > 1)
614 return;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400615
Kevin O'Connorc1437612008-05-18 01:43:07 -0400616 u16 nlc = GET_EBDA(ata.devices[driveid].lchs.cylinders);
617 u16 nlh = GET_EBDA(ata.devices[driveid].lchs.heads);
618 u16 nlspt = GET_EBDA(ata.devices[driveid].lchs.spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400619
Kevin O'Connorc1437612008-05-18 01:43:07 -0400620 u16 npc = GET_EBDA(ata.devices[driveid].pchs.cylinders);
621 u16 nph = GET_EBDA(ata.devices[driveid].pchs.heads);
622 u16 npspt = GET_EBDA(ata.devices[driveid].pchs.spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400623
Kevin O'Connorc1437612008-05-18 01:43:07 -0400624 SET_EBDA(fdpt[driveid].precompensation, 0xffff);
625 SET_EBDA(fdpt[driveid].drive_control_byte, 0xc0 | ((nph > 8) << 3));
626 SET_EBDA(fdpt[driveid].landing_zone, npc);
627 SET_EBDA(fdpt[driveid].cylinders, nlc);
628 SET_EBDA(fdpt[driveid].heads, nlh);
629 SET_EBDA(fdpt[driveid].sectors, nlspt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400630
Kevin O'Connorc1437612008-05-18 01:43:07 -0400631 if (nlc == npc && nlh == nph && nlspt == npspt)
632 // no logical CHS mapping used, just physical CHS
633 // use Standard Fixed Disk Parameter Table (FDPT)
634 return;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400635
Kevin O'Connorc1437612008-05-18 01:43:07 -0400636 // complies with Phoenix style Translated Fixed Disk Parameter
637 // Table (FDPT)
638 SET_EBDA(fdpt[driveid].phys_cylinders, npc);
639 SET_EBDA(fdpt[driveid].phys_heads, nph);
640 SET_EBDA(fdpt[driveid].phys_sectors, npspt);
641 SET_EBDA(fdpt[driveid].a0h_signature, 0xa0);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400642
Kevin O'Connorc1437612008-05-18 01:43:07 -0400643 // Checksum structure.
644 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
645 , fdpt[driveid]));
646 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
647 , fdpt[driveid]) - 1);
648 SET_EBDA(fdpt[driveid].checksum, -sum);
649}
650
651static u8
652get_translation(int driveid)
653{
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400654 if (! CONFIG_COREBOOT) {
655 // Emulators pass in the translation info via nvram.
656 u8 channel = driveid / 2;
657 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
658 translation >>= 2 * (driveid % 4);
659 translation &= 0x03;
660 return translation;
661 }
662
663 // On COREBOOT, use a heuristic to determine translation type.
664 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
665 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
666 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
667
668 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
669 return ATA_TRANSLATION_NONE;
670 if (cylinders * heads <= 131072)
671 return ATA_TRANSLATION_LARGE;
672 return ATA_TRANSLATION_LBA;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400673}
674
675static void
676setup_translation(int driveid)
677{
678 u8 translation = get_translation(driveid);
679 SET_EBDA(ata.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400680
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400681 u8 channel = driveid / 2;
682 u8 slave = driveid % 2;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400683 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
684 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
685 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
686 u64 sectors = GET_EBDA(ata.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400687
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400688 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400689 , channel, slave, cylinders, heads, spt);
690 switch (translation) {
691 case ATA_TRANSLATION_NONE:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400692 dprintf(1, "none");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400693 break;
694 case ATA_TRANSLATION_LBA:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400695 dprintf(1, "lba");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400696 spt = 63;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400697 if (sectors > 63*255*1024) {
698 heads = 255;
699 cylinders = 1024;
700 break;
701 }
702 u32 sect = (u32)sectors / 63;
703 heads = sect / 1024;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400704 if (heads>128)
705 heads = 255;
706 else if (heads>64)
707 heads = 128;
708 else if (heads>32)
709 heads = 64;
710 else if (heads>16)
711 heads = 32;
712 else
713 heads = 16;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400714 cylinders = sect / heads;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400715 break;
716 case ATA_TRANSLATION_RECHS:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400717 dprintf(1, "r-echs");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400718 // Take care not to overflow
719 if (heads==16) {
720 if (cylinders>61439)
721 cylinders=61439;
722 heads=15;
723 cylinders = (u16)((u32)(cylinders)*16/15);
724 }
725 // then go through the large bitshift process
726 case ATA_TRANSLATION_LARGE:
727 if (translation == ATA_TRANSLATION_LARGE)
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400728 dprintf(1, "large");
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400729 while (cylinders > 1024) {
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400730 cylinders >>= 1;
731 heads <<= 1;
732
733 // If we max out the head count
734 if (heads > 127)
735 break;
736 }
737 break;
738 }
739 // clip to 1024 cylinders in lchs
740 if (cylinders > 1024)
741 cylinders = 1024;
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400742 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400743
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400744 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
745 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
746 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400747}
748
749static void
750init_drive_ata(int driveid)
751{
752 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA);
753
754 // Temporary values to do the transfer
755 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
756 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
757
758 // Now we send a IDENTIFY command to ATA device
759 u8 buffer[0x0200];
760 memset(buffer, 0, sizeof(buffer));
761 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
762 , 1, 1
763 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
764 if (ret)
765 BX_PANIC("ata-detect: Failed to detect ATA device\n");
766
767 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
768 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
769 u16 blksize = *(u16*)&buffer[10];
770
771 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
772 u16 heads = *(u16*)&buffer[3*2]; // word 3
773 u16 spt = *(u16*)&buffer[6*2]; // word 6
774
775 u64 sectors;
776 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
777 sectors = *(u64*)&buffer[100*2]; // word 100-103
778 else
779 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
780
781 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
782 SET_EBDA(ata.devices[driveid].removable, removable);
783 SET_EBDA(ata.devices[driveid].mode, mode);
784 SET_EBDA(ata.devices[driveid].blksize, blksize);
785 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
786 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
787 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
788 SET_EBDA(ata.devices[driveid].sectors, sectors);
789
790 // Setup disk geometry translation.
791 setup_translation(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400792
793 // fill hdidmap
794 u8 hdcount = GET_EBDA(ata.hdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400795 SET_EBDA(ata.idmap[0][hdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400796 SET_EBDA(ata.hdcount, ++hdcount);
797
Kevin O'Connorc1437612008-05-18 01:43:07 -0400798 // Fill "fdpt" structure.
799 fill_fdpt(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400800
Kevin O'Connorc1437612008-05-18 01:43:07 -0400801 // Report drive info to user.
802 u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400803 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400804 u8 version = get_ata_version(buffer);
805 if (sizeinmb < (1 << 16))
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400806 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u32)sizeinmb);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400807 else
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400808 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version
809 , (u32)(sizeinmb >> 10));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400810}
811
812static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400813init_drive_unknown(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400814{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400815 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400816
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400817 u8 channel = driveid / 2;
818 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400819 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
820}
821
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400822static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500823ata_detect()
824{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500825 // Device detection
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400826 int driveid;
827 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
828 u8 channel = driveid / 2;
829 u8 slave = driveid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500830
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400831 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
832 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500833
834 // Disable interrupts
835 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
836
837 // Look for device
838 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400839 msleep(50);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500840 outb(0x55, iobase1+ATA_CB_SC);
841 outb(0xaa, iobase1+ATA_CB_SN);
842 outb(0xaa, iobase1+ATA_CB_SC);
843 outb(0x55, iobase1+ATA_CB_SN);
844 outb(0x55, iobase1+ATA_CB_SC);
845 outb(0xaa, iobase1+ATA_CB_SN);
846
847 // If we found something
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400848 u8 sc = inb(iobase1+ATA_CB_SC);
849 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400850 dprintf(6, "ata_detect(1) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400851
852 if (sc != 0x55 || sn != 0xaa)
853 continue;
854
855 // reset the channel
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400856 ata_reset(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400857
858 // check for ATA or ATAPI
859 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400860 msleep(50);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500861 sc = inb(iobase1+ATA_CB_SC);
862 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400863 dprintf(6, "ata_detect(2) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400864 if (sc!=0x01 || sn!=0x01) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400865 init_drive_unknown(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400866 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500867 }
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400868 u8 cl = inb(iobase1+ATA_CB_CL);
869 u8 ch = inb(iobase1+ATA_CB_CH);
870 u8 st = inb(iobase1+ATA_CB_STAT);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400871 dprintf(6, "ata_detect(3) drive=%d sc=%x sn=%x cl=%x ch=%x st=%x\n"
872 , driveid, sc, sn, cl, ch, st);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500873
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400874 if (cl==0x14 && ch==0xeb)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400875 init_drive_atapi(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400876 else if (cl==0x00 && ch==0x00 && st!=0x00)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400877 init_drive_ata(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400878 else if (cl==0xff && ch==0xff)
879 // None
880 continue;
881 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400882 init_drive_unknown(driveid);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500883 }
884
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500885 printf("\n");
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500886}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400887
888static void
889ata_init()
890{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400891 // hdidmap and cdidmap init.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400892 u8 device;
893 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
894 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
895 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
896 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400897
Kevin O'Connorc1437612008-05-18 01:43:07 -0400898#if CONFIG_MAX_ATA_INTERFACES > 0
899 SET_EBDA(ata.channels[0].iface, ATA_IFACE_ISA);
900 SET_EBDA(ata.channels[0].iobase1, 0x1f0);
901 SET_EBDA(ata.channels[0].iobase2, 0x3f0);
902 SET_EBDA(ata.channels[0].irq, 14);
903#endif
904#if CONFIG_MAX_ATA_INTERFACES > 1
905 SET_EBDA(ata.channels[1].iface, ATA_IFACE_ISA);
906 SET_EBDA(ata.channels[1].iobase1, 0x170);
907 SET_EBDA(ata.channels[1].iobase2, 0x370);
908 SET_EBDA(ata.channels[1].irq, 15);
909#endif
910#if CONFIG_MAX_ATA_INTERFACES > 2
911 SET_EBDA(ata.channels[2].iface, ATA_IFACE_ISA);
912 SET_EBDA(ata.channels[2].iobase1, 0x1e8);
913 SET_EBDA(ata.channels[2].iobase2, 0x3e0);
914 SET_EBDA(ata.channels[2].irq, 12);
915#endif
916#if CONFIG_MAX_ATA_INTERFACES > 3
917 SET_EBDA(ata.channels[3].iface, ATA_IFACE_ISA);
918 SET_EBDA(ata.channels[3].iobase1, 0x168);
919 SET_EBDA(ata.channels[3].iobase2, 0x360);
920 SET_EBDA(ata.channels[3].irq, 11);
921#endif
922#if CONFIG_MAX_ATA_INTERFACES > 4
923#error Please fill the ATA interface informations
924#endif
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400925}
926
927void
928hard_drive_setup()
929{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400930 if (!CONFIG_ATA)
931 return;
932
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400933 dprintf(3, "init hard drives\n");
Kevin O'Connorc1437612008-05-18 01:43:07 -0400934 ata_init();
935 ata_detect();
936
937 // Store the device count
938 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
939
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400940 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400941}