blob: 724ad6252a2082e9b059f60fc296d237bdbc3cea [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'Connorf54c1502008-06-14 15:56:16 -040013#include "pic.h" // unmask_pic2
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050014
15#define TIMEOUT 0
16#define BSY 1
17#define NOT_BSY 2
18#define NOT_BSY_DRQ 3
19#define NOT_BSY_NOT_DRQ 4
20#define NOT_BSY_RDY 5
21
Kevin O'Connora6b9f712008-03-29 12:53:57 -040022#define IDE_SECTOR_SIZE 512
23#define CDROM_SECTOR_SIZE 2048
24
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050025#define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
26
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050027
Kevin O'Connora6b9f712008-03-29 12:53:57 -040028/****************************************************************
29 * Helper functions
30 ****************************************************************/
31
32// Wait for the specified ide state
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050033static int
34await_ide(u8 when_done, u16 base, u16 timeout)
35{
Kevin O'Connor1fcf1442008-03-11 19:42:41 -040036 u32 time=0, last=0;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050037 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -040038 u8 status = inb(base+ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050039 time++;
Kevin O'Connor117fc212008-04-13 18:17:02 -040040 u8 result = 0;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050041 if (when_done == BSY)
42 result = status & ATA_CB_STAT_BSY;
43 else if (when_done == NOT_BSY)
44 result = !(status & ATA_CB_STAT_BSY);
45 else if (when_done == NOT_BSY_DRQ)
46 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
47 else if (when_done == NOT_BSY_NOT_DRQ)
48 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
49 else if (when_done == NOT_BSY_RDY)
50 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050051
52 if (result)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040053 return status;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050054 // mod 2048 each 16 ms
55 if (time>>16 != last) {
56 last = time >>16;
Kevin O'Connora05223c2008-06-28 12:15:57 -040057 dprintf(6, "await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ"
58 ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
59 , when_done, time>>11, timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050060 }
61 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connora05223c2008-06-28 12:15:57 -040062 dprintf(1, "await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
63 ",!BSY_!DRQ,!BSY_RDY) %d status=%x time= %d timeout= %d\n"
64 , when_done, status, time>>11, timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050065 return -1;
66 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -040067 if (timeout == 0 || (time>>11) > timeout)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050068 break;
69 }
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040070 dprintf(1, "IDE time out\n");
Kevin O'Connora05223c2008-06-28 12:15:57 -040071 return -2;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050072}
73
Kevin O'Connora6b9f712008-03-29 12:53:57 -040074// Wait for ide state - pauses for one ata cycle first.
Kevin O'Connor74f9c082008-04-13 16:57:16 -040075static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -040076pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout)
77{
78 // Wait one PIO transfer cycle.
79 inb(iobase2 + ATA_CB_ASTAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050080
Kevin O'Connora6b9f712008-03-29 12:53:57 -040081 return await_ide(when_done, iobase1, timeout);
82}
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050083
Kevin O'Connora6b9f712008-03-29 12:53:57 -040084// Delay for x nanoseconds
85static void
86nsleep(u32 delay)
87{
88 // XXX - how to implement ndelay?
89 while (delay--)
90 nop();
91}
92
Kevin O'Connoref2822a2008-06-07 15:23:11 -040093// Wait for ide state - pause for 400ns first.
94static __always_inline int
95ndelay_await_ide(u8 when_done, u16 iobase1, u16 timeout)
96{
97 nsleep(400);
98 return await_ide(when_done, iobase1, timeout);
99}
100
101// Delay for x milliseconds
102static void
103msleep(u32 delay)
104{
105 usleep(delay * 1000);
106}
107
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400108// Reset a drive
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500109void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400110ata_reset(int driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500111{
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400112 u8 channel = driveid / 2;
113 u8 slave = driveid % 2;
114 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
115 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500116
117 // Reset
118
119 // 8.2.1 (a) -- set SRST in DC
120 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
121
122 // 8.2.1 (b) -- wait for BSY
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400123 int status = await_ide(BSY, iobase1, 20);
124 dprintf(6, "ata_reset(1) status=%x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500125
126 // 8.2.1 (f) -- clear SRST
127 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
128
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400129 // 8.2.1 (g) -- check for sc==sn==0x01
130 // select device
131 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400132 msleep(50);
Kevin O'Connor2e3eeeb2008-06-12 22:29:30 -0400133 u8 sc = inb(iobase1+ATA_CB_SC);
134 u8 sn = inb(iobase1+ATA_CB_SN);
135
136 // For predetermined ATA drives - wait for ready.
137 if (sc==0x01 && sn==0x01) {
138 u8 type=GET_EBDA(ata.devices[driveid].type);
139 if (type == ATA_TYPE_ATA)
140 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
141 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500142
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400143 // 8.2.1 (h) -- wait for not BSY
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400144 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
145 dprintf(6, "ata_reset(2) status=%x\n", status);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400146
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500147 // Enable interrupts
148 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
149}
150
Kevin O'Connoree55c762008-05-13 00:18:20 -0400151
152/****************************************************************
153 * ATA send command
154 ****************************************************************/
155
156struct ata_op_s {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400157 u64 lba;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400158 void *far_buffer;
159 u16 driveid;
160 u16 count;
161};
162
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400163struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400164 u8 feature;
165 u8 sector_count;
166 u8 lba_low;
167 u8 lba_mid;
168 u8 lba_high;
169 u8 device;
170 u8 command;
171
172 u8 sector_count2;
173 u8 lba_low2;
174 u8 lba_mid2;
175 u8 lba_high2;
176};
177
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400178// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400179static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400180send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500181{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400182 u8 channel = driveid / 2;
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500183 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
184 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500185
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400186 int status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500187 if (status & ATA_CB_STAT_BSY)
Kevin O'Connora05223c2008-06-28 12:15:57 -0400188 return -3;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500189
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400190 // Disable interrupts
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500191 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400192
193 // Select device
194 u8 device = inb(iobase1 + ATA_CB_DH);
195 outb(cmd->device, iobase1 + ATA_CB_DH);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400196 if ((device ^ cmd->device) & (1 << 4))
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400197 // Wait for device to become active.
198 msleep(50);
199
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400200 if (cmd->command & 0x04) {
201 outb(0x00, iobase1 + ATA_CB_FR);
202 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
203 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
204 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
205 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500206 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400207 outb(cmd->feature, iobase1 + ATA_CB_FR);
208 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
209 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
210 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
211 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400212 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500213
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400214 status = ndelay_await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400215 if (status < 0)
216 return status;
217
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500218 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400219 dprintf(6, "send_cmd : read error\n");
220 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400221 }
222 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400223 dprintf(6, "send_cmd : DRQ not set (status %02x)\n"
224 , (unsigned) status);
225 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500226 }
227
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400228 return 0;
229}
230
Kevin O'Connoree55c762008-05-13 00:18:20 -0400231
232/****************************************************************
233 * ATA transfers
234 ****************************************************************/
235
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400236// Read and discard x number of bytes from an io channel.
237static void
238insx_discard(int mode, int iobase1, int bytes)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400239{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400240 int count, i;
241 if (mode == ATA_MODE_PIO32) {
242 count = bytes / 4;
243 for (i=0; i<count; i++)
244 inl(iobase1);
245 } else {
246 count = bytes / 2;
247 for (i=0; i<count; i++)
248 inw(iobase1);
249 }
250}
251
252// Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
253// 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
254// and/or last block may be partially transferred. This function is
255// inlined because all the callers use different forms and because the
256// large number of parameters would consume a lot of stack space.
Kevin O'Connor74f9c082008-04-13 16:57:16 -0400257static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400258ata_transfer(int driveid, int iswrite, int count, int blocksize
259 , int skipfirst, int skiplast, void *far_buffer)
260{
Kevin O'Connora05223c2008-06-28 12:15:57 -0400261 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d"
262 " skipf=%d skipl=%d buf=%p\n"
263 , driveid, iswrite, count, blocksize
264 , skipfirst, skiplast, far_buffer);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400265
266 // Reset count of transferred data
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400267 SET_EBDA(ata.trsfsectors, 0);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400268
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400269 u8 channel = driveid / 2;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400270 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
271 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400272 u8 mode = GET_EBDA(ata.devices[driveid].mode);
273 int current = 0;
274 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400275 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400276 int bsize = blocksize;
277 if (skipfirst && current == 0) {
278 insx_discard(mode, iobase1, skipfirst);
279 bsize -= skipfirst;
280 }
281 if (skiplast && current == count-1)
282 bsize -= skiplast;
283
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400284 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400285 // Write data to controller
Kevin O'Connora05223c2008-06-28 12:15:57 -0400286 dprintf(16, "Write 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 outsl_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 outsw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500291 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400292 // Read data from controller
Kevin O'Connora05223c2008-06-28 12:15:57 -0400293 dprintf(16, "Read sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400294 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400295 insl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400296 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400297 insw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400298 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400299 far_buffer += bsize;
300
301 if (skiplast && current == count-1)
302 insx_discard(mode, iobase1, skiplast);
303
304 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
305 if (status < 0)
306 // Error
307 return status;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400308
309 current++;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400310 SET_EBDA(ata.trsfsectors, current);
311 if (current == count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400312 break;
313 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
314 | ATA_CB_STAT_ERR);
315 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400316 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
317 , (unsigned) status);
318 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500319 }
320 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400321
322 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
323 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400324 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400325 status &= ~ATA_CB_STAT_DF;
326 if (status != ATA_CB_STAT_RDY ) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400327 dprintf(6, "ata_transfer : no sectors left (status %02x)\n"
328 , (unsigned) status);
329 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400330 }
331
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500332 // Enable interrupts
333 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
334 return 0;
335}
336
Kevin O'Connoree55c762008-05-13 00:18:20 -0400337static noinline int
338ata_transfer_disk(const struct ata_op_s *op, int iswrite)
339{
340 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
341 , 0, 0, op->far_buffer);
342}
343
344static noinline int
345ata_transfer_cdrom(const struct ata_op_s *op)
346{
347 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
348 , 0, 0, op->far_buffer);
349}
350
351static noinline int
352ata_transfer_emu(const struct ata_op_s *op, int before, int after)
353{
354 int vcount = op->count * 4 - before - after;
355 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
356 , before*512, after*512, op->far_buffer);
357 if (ret) {
358 SET_EBDA(ata.trsfsectors, 0);
359 return ret;
360 }
361 SET_EBDA(ata.trsfsectors, vcount);
362 return 0;
363}
364
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400365
366/****************************************************************
367 * ATA hard drive functions
368 ****************************************************************/
369
Kevin O'Connoree55c762008-05-13 00:18:20 -0400370static noinline int
371send_cmd_disk(const struct ata_op_s *op, u16 command)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400372{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400373 u8 slave = op->driveid % 2;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400374 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400375
376 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400377 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400378
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400379 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400380 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
381 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400382 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400383 cmd.lba_mid2 = lba >> 32;
384 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400385
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400386 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400387 lba &= 0xffffff;
388 }
389
390 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400391 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400392 cmd.lba_low = lba;
393 cmd.lba_mid = lba >> 8;
394 cmd.lba_high = lba >> 16;
395 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
396 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400397
Kevin O'Connoree55c762008-05-13 00:18:20 -0400398 return send_cmd(op->driveid, &cmd);
399}
400
401// Read/write count blocks from a harddrive.
402__always_inline int
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400403ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
Kevin O'Connoree55c762008-05-13 00:18:20 -0400404{
405 struct ata_op_s op;
406 op.driveid = driveid;
407 op.lba = lba;
408 op.count = count;
409 op.far_buffer = far_buffer;
410
411 int ret = send_cmd_disk(&op, command);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400412 if (ret)
413 return ret;
414
415 int iswrite = command == ATA_CMD_WRITE_SECTORS;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400416 return ata_transfer_disk(&op, iswrite);
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400417}
418
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400419
420/****************************************************************
421 * ATAPI functions
422 ****************************************************************/
423
424// Low-level atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400425static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400426send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500427{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400428 u8 channel = driveid / 2;
429 u8 slave = driveid % 2;
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400430 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
431 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500432
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400433 struct ata_pio_command cmd;
434 cmd.sector_count = 0;
435 cmd.feature = 0;
436 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400437 cmd.lba_mid = blocksize;
438 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400439 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
440 cmd.command = ATA_CMD_PACKET;
441
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400442 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400443 if (ret)
444 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500445
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400446 // Send command to device
Kevin O'Connor070231b2008-03-22 20:44:37 -0400447 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500448
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400449 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
450 if (status < 0)
451 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400452
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500453 return 0;
454}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500455
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400456// Low-level cdrom read atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400457static int
458send_cmd_cdrom(const struct ata_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500459{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400460 u8 atacmd[12];
461 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500462
Kevin O'Connoree55c762008-05-13 00:18:20 -0400463 atacmd[0]=0x28; // READ command
464 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
465 atacmd[8]=(op->count & 0x00ff);
466 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
467 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
468 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
469 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400470
Kevin O'Connoree55c762008-05-13 00:18:20 -0400471 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
472 , CDROM_SECTOR_SIZE);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500473}
474
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400475// Read sectors from the cdrom.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400476__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400477cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400478{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400479 struct ata_op_s op;
480 op.driveid = driveid;
481 op.lba = lba;
482 op.count = count;
483 op.far_buffer = far_buffer;
484
485 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400486 if (ret)
487 return ret;
488
Kevin O'Connoree55c762008-05-13 00:18:20 -0400489 return ata_transfer_cdrom(&op);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400490}
491
492// Pretend the cdrom has 512 byte sectors (instead of 2048) and read
493// sectors.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400494__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400495cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400496{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400497 u32 velba = vlba + vcount - 1;
498 u32 lba = vlba / 4;
499 u32 elba = velba / 4;
500 int count = elba - lba + 1;
501 int before = vlba % 4;
502 int after = 3 - (velba % 4);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400503
Kevin O'Connoree55c762008-05-13 00:18:20 -0400504 struct ata_op_s op;
505 op.driveid = driveid;
506 op.lba = lba;
507 op.count = count;
508 op.far_buffer = far_buffer;
509
Kevin O'Connora05223c2008-06-28 12:15:57 -0400510 dprintf(16, "cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
511 " count=%d before=%d after=%d\n"
512 , driveid, vlba, vcount, far_buffer, lba, elba
513 , count, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400514
Kevin O'Connoree55c762008-05-13 00:18:20 -0400515 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400516 if (ret)
517 return ret;
518
Kevin O'Connoree55c762008-05-13 00:18:20 -0400519 return ata_transfer_emu(&op, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400520}
521
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400522// Send a simple atapi command to a drive.
523int
524ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
525 , u32 length, void *far_buffer)
526{
527 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
528 if (ret)
529 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400530
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400531 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
532}
533
534
535/****************************************************************
536 * ATA detect and init
537 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500538
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400539static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400540report_model(int driveid, u8 *buffer)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400541{
542 u8 model[41];
543
544 // Read model name
545 int i;
546 for (i=0; i<40; i+=2) {
547 model[i] = buffer[i+54+1];
548 model[i+1] = buffer[i+54];
549 }
550
551 // Reformat
552 model[40] = 0x00;
553 for (i=39; i>0; i--) {
554 if (model[i] != 0x20)
555 break;
556 model[i] = 0x00;
557 }
558
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400559 u8 channel = driveid / 2;
560 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400561 // XXX - model on stack not %cs
562 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
563}
564
565static u8
566get_ata_version(u8 *buffer)
567{
568 u16 ataversion = *(u16*)&buffer[160];
569 u8 version;
570 for (version=15; version>0; version--)
571 if (ataversion & (1<<version))
572 break;
573 return version;
574}
575
576static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400577init_drive_atapi(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400578{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400579 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400580
581 // Temporary values to do the transfer
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400582 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
583 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400584
585 // Now we send a IDENTIFY command to ATAPI device
586 u8 buffer[0x0200];
587 memset(buffer, 0, sizeof(buffer));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400588 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400589 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400590 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400591 if (ret != 0)
592 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
593
594 u8 type = buffer[1] & 0x1f;
595 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
596 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400597 u16 blksize = CDROM_SECTOR_SIZE;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400598
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400599 SET_EBDA(ata.devices[driveid].device, type);
600 SET_EBDA(ata.devices[driveid].removable, removable);
601 SET_EBDA(ata.devices[driveid].mode, mode);
602 SET_EBDA(ata.devices[driveid].blksize, blksize);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400603
604 // fill cdidmap
605 u8 cdcount = GET_EBDA(ata.cdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400606 SET_EBDA(ata.idmap[1][cdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400607 SET_EBDA(ata.cdcount, ++cdcount);
608
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400609 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400610 u8 version = get_ata_version(buffer);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400611 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400612 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
613 else
614 printf(" ATAPI-%d Device\n", version);
615}
616
617static void
Kevin O'Connorc1437612008-05-18 01:43:07 -0400618fill_fdpt(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400619{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400620 if (driveid > 1)
621 return;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400622
Kevin O'Connorc1437612008-05-18 01:43:07 -0400623 u16 nlc = GET_EBDA(ata.devices[driveid].lchs.cylinders);
624 u16 nlh = GET_EBDA(ata.devices[driveid].lchs.heads);
625 u16 nlspt = GET_EBDA(ata.devices[driveid].lchs.spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400626
Kevin O'Connorc1437612008-05-18 01:43:07 -0400627 u16 npc = GET_EBDA(ata.devices[driveid].pchs.cylinders);
628 u16 nph = GET_EBDA(ata.devices[driveid].pchs.heads);
629 u16 npspt = GET_EBDA(ata.devices[driveid].pchs.spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400630
Kevin O'Connorc1437612008-05-18 01:43:07 -0400631 SET_EBDA(fdpt[driveid].precompensation, 0xffff);
632 SET_EBDA(fdpt[driveid].drive_control_byte, 0xc0 | ((nph > 8) << 3));
633 SET_EBDA(fdpt[driveid].landing_zone, npc);
634 SET_EBDA(fdpt[driveid].cylinders, nlc);
635 SET_EBDA(fdpt[driveid].heads, nlh);
636 SET_EBDA(fdpt[driveid].sectors, nlspt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400637
Kevin O'Connorc1437612008-05-18 01:43:07 -0400638 if (nlc == npc && nlh == nph && nlspt == npspt)
639 // no logical CHS mapping used, just physical CHS
640 // use Standard Fixed Disk Parameter Table (FDPT)
641 return;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400642
Kevin O'Connorc1437612008-05-18 01:43:07 -0400643 // complies with Phoenix style Translated Fixed Disk Parameter
644 // Table (FDPT)
645 SET_EBDA(fdpt[driveid].phys_cylinders, npc);
646 SET_EBDA(fdpt[driveid].phys_heads, nph);
647 SET_EBDA(fdpt[driveid].phys_sectors, npspt);
648 SET_EBDA(fdpt[driveid].a0h_signature, 0xa0);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400649
Kevin O'Connorc1437612008-05-18 01:43:07 -0400650 // Checksum structure.
651 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
652 , fdpt[driveid]));
653 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
654 , fdpt[driveid]) - 1);
655 SET_EBDA(fdpt[driveid].checksum, -sum);
656}
657
658static u8
659get_translation(int driveid)
660{
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400661 if (! CONFIG_COREBOOT) {
662 // Emulators pass in the translation info via nvram.
663 u8 channel = driveid / 2;
664 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
665 translation >>= 2 * (driveid % 4);
666 translation &= 0x03;
667 return translation;
668 }
669
670 // On COREBOOT, use a heuristic to determine translation type.
671 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
672 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
673 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
674
675 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
676 return ATA_TRANSLATION_NONE;
677 if (cylinders * heads <= 131072)
678 return ATA_TRANSLATION_LARGE;
679 return ATA_TRANSLATION_LBA;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400680}
681
682static void
683setup_translation(int driveid)
684{
685 u8 translation = get_translation(driveid);
686 SET_EBDA(ata.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400687
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400688 u8 channel = driveid / 2;
689 u8 slave = driveid % 2;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400690 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
691 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
692 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
693 u64 sectors = GET_EBDA(ata.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400694
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400695 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400696 , channel, slave, cylinders, heads, spt);
697 switch (translation) {
698 case ATA_TRANSLATION_NONE:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400699 dprintf(1, "none");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400700 break;
701 case ATA_TRANSLATION_LBA:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400702 dprintf(1, "lba");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400703 spt = 63;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400704 if (sectors > 63*255*1024) {
705 heads = 255;
706 cylinders = 1024;
707 break;
708 }
709 u32 sect = (u32)sectors / 63;
710 heads = sect / 1024;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400711 if (heads>128)
712 heads = 255;
713 else if (heads>64)
714 heads = 128;
715 else if (heads>32)
716 heads = 64;
717 else if (heads>16)
718 heads = 32;
719 else
720 heads = 16;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400721 cylinders = sect / heads;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400722 break;
723 case ATA_TRANSLATION_RECHS:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400724 dprintf(1, "r-echs");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400725 // Take care not to overflow
726 if (heads==16) {
727 if (cylinders>61439)
728 cylinders=61439;
729 heads=15;
730 cylinders = (u16)((u32)(cylinders)*16/15);
731 }
732 // then go through the large bitshift process
733 case ATA_TRANSLATION_LARGE:
734 if (translation == ATA_TRANSLATION_LARGE)
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400735 dprintf(1, "large");
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400736 while (cylinders > 1024) {
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400737 cylinders >>= 1;
738 heads <<= 1;
739
740 // If we max out the head count
741 if (heads > 127)
742 break;
743 }
744 break;
745 }
746 // clip to 1024 cylinders in lchs
747 if (cylinders > 1024)
748 cylinders = 1024;
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400749 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400750
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400751 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
752 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
753 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400754}
755
756static void
757init_drive_ata(int driveid)
758{
759 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA);
760
761 // Temporary values to do the transfer
762 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
763 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
764
765 // Now we send a IDENTIFY command to ATA device
766 u8 buffer[0x0200];
767 memset(buffer, 0, sizeof(buffer));
768 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
769 , 1, 1
770 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
771 if (ret)
772 BX_PANIC("ata-detect: Failed to detect ATA device\n");
773
774 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
775 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
776 u16 blksize = *(u16*)&buffer[10];
777
778 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
779 u16 heads = *(u16*)&buffer[3*2]; // word 3
780 u16 spt = *(u16*)&buffer[6*2]; // word 6
781
782 u64 sectors;
783 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
784 sectors = *(u64*)&buffer[100*2]; // word 100-103
785 else
786 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
787
788 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
789 SET_EBDA(ata.devices[driveid].removable, removable);
790 SET_EBDA(ata.devices[driveid].mode, mode);
791 SET_EBDA(ata.devices[driveid].blksize, blksize);
792 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
793 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
794 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
795 SET_EBDA(ata.devices[driveid].sectors, sectors);
796
797 // Setup disk geometry translation.
798 setup_translation(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400799
800 // fill hdidmap
801 u8 hdcount = GET_EBDA(ata.hdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400802 SET_EBDA(ata.idmap[0][hdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400803 SET_EBDA(ata.hdcount, ++hdcount);
804
Kevin O'Connorc1437612008-05-18 01:43:07 -0400805 // Fill "fdpt" structure.
806 fill_fdpt(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400807
Kevin O'Connorc1437612008-05-18 01:43:07 -0400808 // Report drive info to user.
809 u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400810 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400811 u8 version = get_ata_version(buffer);
812 if (sizeinmb < (1 << 16))
Kevin O'Connora05223c2008-06-28 12:15:57 -0400813 printf(" ATA-%d Hard-Disk (%u MiBytes)\n", version, (u32)sizeinmb);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400814 else
Kevin O'Connora05223c2008-06-28 12:15:57 -0400815 printf(" ATA-%d Hard-Disk (%u GiBytes)\n", version
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400816 , (u32)(sizeinmb >> 10));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400817}
818
819static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400820init_drive_unknown(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400821{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400822 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400823
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400824 u8 channel = driveid / 2;
825 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400826 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
827}
828
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400829static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500830ata_detect()
831{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500832 // Device detection
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400833 int driveid;
834 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
835 u8 channel = driveid / 2;
836 u8 slave = driveid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500837
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400838 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
839 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500840
841 // Disable interrupts
842 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
843
844 // Look for device
845 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400846 msleep(50);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500847 outb(0x55, iobase1+ATA_CB_SC);
848 outb(0xaa, iobase1+ATA_CB_SN);
849 outb(0xaa, iobase1+ATA_CB_SC);
850 outb(0x55, iobase1+ATA_CB_SN);
851 outb(0x55, iobase1+ATA_CB_SC);
852 outb(0xaa, iobase1+ATA_CB_SN);
853
854 // If we found something
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400855 u8 sc = inb(iobase1+ATA_CB_SC);
856 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400857 dprintf(6, "ata_detect(1) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400858
859 if (sc != 0x55 || sn != 0xaa)
860 continue;
861
862 // reset the channel
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400863 ata_reset(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400864
865 // check for ATA or ATAPI
866 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400867 msleep(50);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500868 sc = inb(iobase1+ATA_CB_SC);
869 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400870 dprintf(6, "ata_detect(2) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400871 if (sc!=0x01 || sn!=0x01) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400872 init_drive_unknown(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400873 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500874 }
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400875 u8 cl = inb(iobase1+ATA_CB_CL);
876 u8 ch = inb(iobase1+ATA_CB_CH);
877 u8 st = inb(iobase1+ATA_CB_STAT);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400878 dprintf(6, "ata_detect(3) drive=%d sc=%x sn=%x cl=%x ch=%x st=%x\n"
879 , driveid, sc, sn, cl, ch, st);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500880
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400881 if (cl==0x14 && ch==0xeb)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400882 init_drive_atapi(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400883 else if (cl==0x00 && ch==0x00 && st!=0x00)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400884 init_drive_ata(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400885 else if (cl==0xff && ch==0xff)
886 // None
887 continue;
888 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400889 init_drive_unknown(driveid);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500890 }
891
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500892 printf("\n");
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500893}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400894
895static void
896ata_init()
897{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400898 // hdidmap and cdidmap init.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400899 u8 device;
900 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
901 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
902 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
903 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400904
Kevin O'Connorc1437612008-05-18 01:43:07 -0400905#if CONFIG_MAX_ATA_INTERFACES > 0
906 SET_EBDA(ata.channels[0].iface, ATA_IFACE_ISA);
907 SET_EBDA(ata.channels[0].iobase1, 0x1f0);
908 SET_EBDA(ata.channels[0].iobase2, 0x3f0);
909 SET_EBDA(ata.channels[0].irq, 14);
910#endif
911#if CONFIG_MAX_ATA_INTERFACES > 1
912 SET_EBDA(ata.channels[1].iface, ATA_IFACE_ISA);
913 SET_EBDA(ata.channels[1].iobase1, 0x170);
914 SET_EBDA(ata.channels[1].iobase2, 0x370);
915 SET_EBDA(ata.channels[1].irq, 15);
916#endif
917#if CONFIG_MAX_ATA_INTERFACES > 2
918 SET_EBDA(ata.channels[2].iface, ATA_IFACE_ISA);
919 SET_EBDA(ata.channels[2].iobase1, 0x1e8);
920 SET_EBDA(ata.channels[2].iobase2, 0x3e0);
921 SET_EBDA(ata.channels[2].irq, 12);
922#endif
923#if CONFIG_MAX_ATA_INTERFACES > 3
924 SET_EBDA(ata.channels[3].iface, ATA_IFACE_ISA);
925 SET_EBDA(ata.channels[3].iobase1, 0x168);
926 SET_EBDA(ata.channels[3].iobase2, 0x360);
927 SET_EBDA(ata.channels[3].irq, 11);
928#endif
929#if CONFIG_MAX_ATA_INTERFACES > 4
930#error Please fill the ATA interface informations
931#endif
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400932}
933
934void
935hard_drive_setup()
936{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400937 if (!CONFIG_ATA)
938 return;
939
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400940 dprintf(3, "init hard drives\n");
Kevin O'Connorc1437612008-05-18 01:43:07 -0400941 ata_init();
942 ata_detect();
943
944 // Store the device count
945 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
946
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400947 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400948
949 // Enable IRQ14 (handle_76)
950 unmask_pic2(PIC2_IRQ14);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400951}