blob: 1cb51eff1a043be0bf7d71de0cd49bc18baaec03 [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'Connor9521e262008-07-04 13:04:29 -040014#include "biosvar.h" // GET_EBDA
Kevin O'Connor53236cc2008-08-31 11:16:33 -040015#include "pci.h" // pci_find_class
Kevin O'Connor2ed2f582008-11-08 15:53:36 -050016#include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
17#include "pci_regs.h" // PCI_INTERRUPT_LINE
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050018
19#define TIMEOUT 0
20#define BSY 1
21#define NOT_BSY 2
22#define NOT_BSY_DRQ 3
23#define NOT_BSY_NOT_DRQ 4
24#define NOT_BSY_RDY 5
25
Kevin O'Connora6b9f712008-03-29 12:53:57 -040026#define IDE_SECTOR_SIZE 512
27#define CDROM_SECTOR_SIZE 2048
28
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050029#define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
30
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050031
Kevin O'Connora6b9f712008-03-29 12:53:57 -040032/****************************************************************
33 * Helper functions
34 ****************************************************************/
35
36// Wait for the specified ide state
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050037static int
38await_ide(u8 when_done, u16 base, u16 timeout)
39{
Kevin O'Connor1fcf1442008-03-11 19:42:41 -040040 u32 time=0, last=0;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050041 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -040042 u8 status = inb(base+ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050043 time++;
Kevin O'Connor117fc212008-04-13 18:17:02 -040044 u8 result = 0;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050045 if (when_done == BSY)
46 result = status & ATA_CB_STAT_BSY;
47 else if (when_done == NOT_BSY)
48 result = !(status & ATA_CB_STAT_BSY);
49 else if (when_done == NOT_BSY_DRQ)
50 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
51 else if (when_done == NOT_BSY_NOT_DRQ)
52 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
53 else if (when_done == NOT_BSY_RDY)
54 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050055
56 if (result)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040057 return status;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050058 // mod 2048 each 16 ms
59 if (time>>16 != last) {
60 last = time >>16;
Kevin O'Connora05223c2008-06-28 12:15:57 -040061 dprintf(6, "await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ"
62 ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
63 , when_done, time>>11, timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050064 }
65 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connora05223c2008-06-28 12:15:57 -040066 dprintf(1, "await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
67 ",!BSY_!DRQ,!BSY_RDY) %d status=%x time= %d timeout= %d\n"
68 , when_done, status, time>>11, timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050069 return -1;
70 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -040071 if (timeout == 0 || (time>>11) > timeout)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050072 break;
73 }
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040074 dprintf(1, "IDE time out\n");
Kevin O'Connora05223c2008-06-28 12:15:57 -040075 return -2;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050076}
77
Kevin O'Connora6b9f712008-03-29 12:53:57 -040078// Wait for ide state - pauses for one ata cycle first.
Kevin O'Connor74f9c082008-04-13 16:57:16 -040079static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -040080pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout)
81{
82 // Wait one PIO transfer cycle.
83 inb(iobase2 + ATA_CB_ASTAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050084
Kevin O'Connora6b9f712008-03-29 12:53:57 -040085 return await_ide(when_done, iobase1, timeout);
86}
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050087
Kevin O'Connora6b9f712008-03-29 12:53:57 -040088// Delay for x nanoseconds
89static void
90nsleep(u32 delay)
91{
92 // XXX - how to implement ndelay?
93 while (delay--)
94 nop();
95}
96
Kevin O'Connoref2822a2008-06-07 15:23:11 -040097// Wait for ide state - pause for 400ns first.
98static __always_inline int
99ndelay_await_ide(u8 when_done, u16 iobase1, u16 timeout)
100{
101 nsleep(400);
102 return await_ide(when_done, iobase1, timeout);
103}
104
105// Delay for x milliseconds
106static void
107msleep(u32 delay)
108{
109 usleep(delay * 1000);
110}
111
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400112// Reset a drive
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500113void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400114ata_reset(int driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500115{
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400116 u8 channel = driveid / 2;
117 u8 slave = driveid % 2;
118 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
119 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500120
121 // Reset
122
123 // 8.2.1 (a) -- set SRST in DC
124 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
125
126 // 8.2.1 (b) -- wait for BSY
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400127 int status = await_ide(BSY, iobase1, 20);
128 dprintf(6, "ata_reset(1) status=%x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500129
130 // 8.2.1 (f) -- clear SRST
131 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
132
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400133 // 8.2.1 (g) -- check for sc==sn==0x01
134 // select device
135 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400136 msleep(50);
Kevin O'Connor2e3eeeb2008-06-12 22:29:30 -0400137 u8 sc = inb(iobase1+ATA_CB_SC);
138 u8 sn = inb(iobase1+ATA_CB_SN);
139
140 // For predetermined ATA drives - wait for ready.
141 if (sc==0x01 && sn==0x01) {
142 u8 type=GET_EBDA(ata.devices[driveid].type);
143 if (type == ATA_TYPE_ATA)
144 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
145 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500146
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400147 // 8.2.1 (h) -- wait for not BSY
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400148 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
149 dprintf(6, "ata_reset(2) status=%x\n", status);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400150
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500151 // Enable interrupts
152 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
153}
154
Kevin O'Connoree55c762008-05-13 00:18:20 -0400155
156/****************************************************************
157 * ATA send command
158 ****************************************************************/
159
160struct ata_op_s {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400161 u64 lba;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400162 void *far_buffer;
163 u16 driveid;
164 u16 count;
165};
166
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400167struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400168 u8 feature;
169 u8 sector_count;
170 u8 lba_low;
171 u8 lba_mid;
172 u8 lba_high;
173 u8 device;
174 u8 command;
175
176 u8 sector_count2;
177 u8 lba_low2;
178 u8 lba_mid2;
179 u8 lba_high2;
180};
181
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400182// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400183static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400184send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500185{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400186 u8 channel = driveid / 2;
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500187 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
188 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500189
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400190 int status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500191 if (status & ATA_CB_STAT_BSY)
Kevin O'Connora05223c2008-06-28 12:15:57 -0400192 return -3;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500193
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400194 // Disable interrupts
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500195 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400196
197 // Select device
198 u8 device = inb(iobase1 + ATA_CB_DH);
199 outb(cmd->device, iobase1 + ATA_CB_DH);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400200 if ((device ^ cmd->device) & (1 << 4))
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400201 // Wait for device to become active.
202 msleep(50);
203
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400204 if (cmd->command & 0x04) {
205 outb(0x00, iobase1 + ATA_CB_FR);
206 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
207 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
208 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
209 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500210 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400211 outb(cmd->feature, iobase1 + ATA_CB_FR);
212 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
213 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
214 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
215 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400216 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500217
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400218 status = ndelay_await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400219 if (status < 0)
220 return status;
221
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500222 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400223 dprintf(6, "send_cmd : read error\n");
224 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400225 }
226 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400227 dprintf(6, "send_cmd : DRQ not set (status %02x)\n"
228 , (unsigned) status);
229 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500230 }
231
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400232 return 0;
233}
234
Kevin O'Connoree55c762008-05-13 00:18:20 -0400235
236/****************************************************************
237 * ATA transfers
238 ****************************************************************/
239
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400240// Read and discard x number of bytes from an io channel.
241static void
242insx_discard(int mode, int iobase1, int bytes)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400243{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400244 int count, i;
245 if (mode == ATA_MODE_PIO32) {
246 count = bytes / 4;
247 for (i=0; i<count; i++)
248 inl(iobase1);
249 } else {
250 count = bytes / 2;
251 for (i=0; i<count; i++)
252 inw(iobase1);
253 }
254}
255
256// Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
257// 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
258// and/or last block may be partially transferred. This function is
259// inlined because all the callers use different forms and because the
260// large number of parameters would consume a lot of stack space.
Kevin O'Connor74f9c082008-04-13 16:57:16 -0400261static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400262ata_transfer(int driveid, int iswrite, int count, int blocksize
263 , int skipfirst, int skiplast, void *far_buffer)
264{
Kevin O'Connora05223c2008-06-28 12:15:57 -0400265 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d"
266 " skipf=%d skipl=%d buf=%p\n"
267 , driveid, iswrite, count, blocksize
268 , skipfirst, skiplast, far_buffer);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400269
270 // Reset count of transferred data
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400271 SET_EBDA(ata.trsfsectors, 0);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400272
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400273 u8 channel = driveid / 2;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400274 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
275 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400276 u8 mode = GET_EBDA(ata.devices[driveid].mode);
277 int current = 0;
278 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400279 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400280 int bsize = blocksize;
281 if (skipfirst && current == 0) {
282 insx_discard(mode, iobase1, skipfirst);
283 bsize -= skipfirst;
284 }
285 if (skiplast && current == count-1)
286 bsize -= skiplast;
287
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400288 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400289 // Write data to controller
Kevin O'Connora05223c2008-06-28 12:15:57 -0400290 dprintf(16, "Write sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400291 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400292 outsl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400293 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400294 outsw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500295 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400296 // Read data from controller
Kevin O'Connora05223c2008-06-28 12:15:57 -0400297 dprintf(16, "Read sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400298 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400299 insl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400300 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400301 insw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400302 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400303 far_buffer += bsize;
304
305 if (skiplast && current == count-1)
306 insx_discard(mode, iobase1, skiplast);
307
308 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
309 if (status < 0)
310 // Error
311 return status;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400312
313 current++;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400314 SET_EBDA(ata.trsfsectors, current);
315 if (current == count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400316 break;
317 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
318 | ATA_CB_STAT_ERR);
319 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400320 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
321 , (unsigned) status);
322 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500323 }
324 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400325
326 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
327 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400328 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400329 status &= ~ATA_CB_STAT_DF;
330 if (status != ATA_CB_STAT_RDY ) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400331 dprintf(6, "ata_transfer : no sectors left (status %02x)\n"
332 , (unsigned) status);
333 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400334 }
335
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500336 // Enable interrupts
337 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
338 return 0;
339}
340
Kevin O'Connoree55c762008-05-13 00:18:20 -0400341static noinline int
342ata_transfer_disk(const struct ata_op_s *op, int iswrite)
343{
344 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
345 , 0, 0, op->far_buffer);
346}
347
348static noinline int
349ata_transfer_cdrom(const struct ata_op_s *op)
350{
351 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
352 , 0, 0, op->far_buffer);
353}
354
355static noinline int
356ata_transfer_emu(const struct ata_op_s *op, int before, int after)
357{
358 int vcount = op->count * 4 - before - after;
359 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
360 , before*512, after*512, op->far_buffer);
361 if (ret) {
362 SET_EBDA(ata.trsfsectors, 0);
363 return ret;
364 }
365 SET_EBDA(ata.trsfsectors, vcount);
366 return 0;
367}
368
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400369
370/****************************************************************
371 * ATA hard drive functions
372 ****************************************************************/
373
Kevin O'Connoree55c762008-05-13 00:18:20 -0400374static noinline int
375send_cmd_disk(const struct ata_op_s *op, u16 command)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400376{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400377 u8 slave = op->driveid % 2;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400378 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400379
380 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400381 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400382
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400383 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400384 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
385 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400386 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400387 cmd.lba_mid2 = lba >> 32;
388 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400389
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400390 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400391 lba &= 0xffffff;
392 }
393
394 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400395 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400396 cmd.lba_low = lba;
397 cmd.lba_mid = lba >> 8;
398 cmd.lba_high = lba >> 16;
399 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
400 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400401
Kevin O'Connoree55c762008-05-13 00:18:20 -0400402 return send_cmd(op->driveid, &cmd);
403}
404
405// Read/write count blocks from a harddrive.
406__always_inline int
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400407ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
Kevin O'Connoree55c762008-05-13 00:18:20 -0400408{
409 struct ata_op_s op;
410 op.driveid = driveid;
411 op.lba = lba;
412 op.count = count;
413 op.far_buffer = far_buffer;
414
415 int ret = send_cmd_disk(&op, command);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400416 if (ret)
417 return ret;
418
419 int iswrite = command == ATA_CMD_WRITE_SECTORS;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400420 return ata_transfer_disk(&op, iswrite);
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400421}
422
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400423
424/****************************************************************
425 * ATAPI functions
426 ****************************************************************/
427
428// Low-level atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400429static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400430send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500431{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400432 u8 channel = driveid / 2;
433 u8 slave = driveid % 2;
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400434 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
435 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500436
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400437 struct ata_pio_command cmd;
438 cmd.sector_count = 0;
439 cmd.feature = 0;
440 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400441 cmd.lba_mid = blocksize;
442 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400443 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
444 cmd.command = ATA_CMD_PACKET;
445
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400446 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400447 if (ret)
448 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500449
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400450 // Send command to device
Kevin O'Connor070231b2008-03-22 20:44:37 -0400451 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500452
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400453 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
454 if (status < 0)
455 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400456
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500457 return 0;
458}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500459
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400460// Low-level cdrom read atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400461static int
462send_cmd_cdrom(const struct ata_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500463{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400464 u8 atacmd[12];
465 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500466
Kevin O'Connoree55c762008-05-13 00:18:20 -0400467 atacmd[0]=0x28; // READ command
468 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
469 atacmd[8]=(op->count & 0x00ff);
470 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
471 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
472 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
473 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400474
Kevin O'Connoree55c762008-05-13 00:18:20 -0400475 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
476 , CDROM_SECTOR_SIZE);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500477}
478
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400479// Read sectors from the cdrom.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400480__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400481cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400482{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400483 struct ata_op_s op;
484 op.driveid = driveid;
485 op.lba = lba;
486 op.count = count;
487 op.far_buffer = far_buffer;
488
489 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400490 if (ret)
491 return ret;
492
Kevin O'Connoree55c762008-05-13 00:18:20 -0400493 return ata_transfer_cdrom(&op);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400494}
495
496// Pretend the cdrom has 512 byte sectors (instead of 2048) and read
497// sectors.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400498__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400499cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400500{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400501 u32 velba = vlba + vcount - 1;
502 u32 lba = vlba / 4;
503 u32 elba = velba / 4;
504 int count = elba - lba + 1;
505 int before = vlba % 4;
506 int after = 3 - (velba % 4);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400507
Kevin O'Connoree55c762008-05-13 00:18:20 -0400508 struct ata_op_s op;
509 op.driveid = driveid;
510 op.lba = lba;
511 op.count = count;
512 op.far_buffer = far_buffer;
513
Kevin O'Connora05223c2008-06-28 12:15:57 -0400514 dprintf(16, "cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
515 " count=%d before=%d after=%d\n"
516 , driveid, vlba, vcount, far_buffer, lba, elba
517 , count, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400518
Kevin O'Connoree55c762008-05-13 00:18:20 -0400519 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400520 if (ret)
521 return ret;
522
Kevin O'Connoree55c762008-05-13 00:18:20 -0400523 return ata_transfer_emu(&op, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400524}
525
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400526// Send a simple atapi command to a drive.
527int
528ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
529 , u32 length, void *far_buffer)
530{
531 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
532 if (ret)
533 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400534
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400535 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
536}
537
538
539/****************************************************************
540 * ATA detect and init
541 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500542
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400543static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400544report_model(int driveid, u8 *buffer)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400545{
546 u8 model[41];
547
548 // Read model name
549 int i;
550 for (i=0; i<40; i+=2) {
551 model[i] = buffer[i+54+1];
552 model[i+1] = buffer[i+54];
553 }
554
555 // Reformat
556 model[40] = 0x00;
557 for (i=39; i>0; i--) {
558 if (model[i] != 0x20)
559 break;
560 model[i] = 0x00;
561 }
562
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400563 u8 channel = driveid / 2;
564 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400565 // XXX - model on stack not %cs
566 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
567}
568
569static u8
570get_ata_version(u8 *buffer)
571{
572 u16 ataversion = *(u16*)&buffer[160];
573 u8 version;
574 for (version=15; version>0; version--)
575 if (ataversion & (1<<version))
576 break;
577 return version;
578}
579
580static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400581init_drive_atapi(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400582{
Kevin O'Connor970a0322008-10-26 12:01:21 -0400583 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATAPI);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400584
585 // Temporary values to do the transfer
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400586 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
587 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400588
589 // Now we send a IDENTIFY command to ATAPI device
590 u8 buffer[0x0200];
591 memset(buffer, 0, sizeof(buffer));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400592 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400593 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400594 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400595 if (ret != 0)
596 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
597
598 u8 type = buffer[1] & 0x1f;
599 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
600 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400601 u16 blksize = CDROM_SECTOR_SIZE;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400602
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400603 SET_EBDA(ata.devices[driveid].device, type);
604 SET_EBDA(ata.devices[driveid].removable, removable);
605 SET_EBDA(ata.devices[driveid].mode, mode);
606 SET_EBDA(ata.devices[driveid].blksize, blksize);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400607
608 // fill cdidmap
609 u8 cdcount = GET_EBDA(ata.cdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400610 SET_EBDA(ata.idmap[1][cdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400611 SET_EBDA(ata.cdcount, ++cdcount);
612
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400613 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400614 u8 version = get_ata_version(buffer);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400615 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400616 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
617 else
618 printf(" ATAPI-%d Device\n", version);
619}
620
621static void
Kevin O'Connorc1437612008-05-18 01:43:07 -0400622fill_fdpt(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400623{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400624 if (driveid > 1)
625 return;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400626
Kevin O'Connorc1437612008-05-18 01:43:07 -0400627 u16 nlc = GET_EBDA(ata.devices[driveid].lchs.cylinders);
628 u16 nlh = GET_EBDA(ata.devices[driveid].lchs.heads);
629 u16 nlspt = GET_EBDA(ata.devices[driveid].lchs.spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400630
Kevin O'Connorc1437612008-05-18 01:43:07 -0400631 u16 npc = GET_EBDA(ata.devices[driveid].pchs.cylinders);
632 u16 nph = GET_EBDA(ata.devices[driveid].pchs.heads);
633 u16 npspt = GET_EBDA(ata.devices[driveid].pchs.spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400634
Kevin O'Connorc1437612008-05-18 01:43:07 -0400635 SET_EBDA(fdpt[driveid].precompensation, 0xffff);
636 SET_EBDA(fdpt[driveid].drive_control_byte, 0xc0 | ((nph > 8) << 3));
637 SET_EBDA(fdpt[driveid].landing_zone, npc);
638 SET_EBDA(fdpt[driveid].cylinders, nlc);
639 SET_EBDA(fdpt[driveid].heads, nlh);
640 SET_EBDA(fdpt[driveid].sectors, nlspt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400641
Kevin O'Connorc1437612008-05-18 01:43:07 -0400642 if (nlc == npc && nlh == nph && nlspt == npspt)
643 // no logical CHS mapping used, just physical CHS
644 // use Standard Fixed Disk Parameter Table (FDPT)
645 return;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400646
Kevin O'Connorc1437612008-05-18 01:43:07 -0400647 // complies with Phoenix style Translated Fixed Disk Parameter
648 // Table (FDPT)
649 SET_EBDA(fdpt[driveid].phys_cylinders, npc);
650 SET_EBDA(fdpt[driveid].phys_heads, nph);
651 SET_EBDA(fdpt[driveid].phys_sectors, npspt);
652 SET_EBDA(fdpt[driveid].a0h_signature, 0xa0);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400653
Kevin O'Connorc1437612008-05-18 01:43:07 -0400654 // Checksum structure.
655 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
656 , fdpt[driveid]));
657 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
658 , fdpt[driveid]) - 1);
659 SET_EBDA(fdpt[driveid].checksum, -sum);
660}
661
662static u8
663get_translation(int driveid)
664{
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400665 if (! CONFIG_COREBOOT) {
666 // Emulators pass in the translation info via nvram.
667 u8 channel = driveid / 2;
668 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
669 translation >>= 2 * (driveid % 4);
670 translation &= 0x03;
671 return translation;
672 }
673
674 // On COREBOOT, use a heuristic to determine translation type.
675 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
676 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
677 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
678
679 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
680 return ATA_TRANSLATION_NONE;
681 if (cylinders * heads <= 131072)
682 return ATA_TRANSLATION_LARGE;
683 return ATA_TRANSLATION_LBA;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400684}
685
686static void
687setup_translation(int driveid)
688{
689 u8 translation = get_translation(driveid);
690 SET_EBDA(ata.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400691
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400692 u8 channel = driveid / 2;
693 u8 slave = driveid % 2;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400694 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
695 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
696 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
697 u64 sectors = GET_EBDA(ata.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400698
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400699 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400700 , channel, slave, cylinders, heads, spt);
701 switch (translation) {
702 case ATA_TRANSLATION_NONE:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400703 dprintf(1, "none");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400704 break;
705 case ATA_TRANSLATION_LBA:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400706 dprintf(1, "lba");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400707 spt = 63;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400708 if (sectors > 63*255*1024) {
709 heads = 255;
710 cylinders = 1024;
711 break;
712 }
713 u32 sect = (u32)sectors / 63;
714 heads = sect / 1024;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400715 if (heads>128)
716 heads = 255;
717 else if (heads>64)
718 heads = 128;
719 else if (heads>32)
720 heads = 64;
721 else if (heads>16)
722 heads = 32;
723 else
724 heads = 16;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400725 cylinders = sect / heads;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400726 break;
727 case ATA_TRANSLATION_RECHS:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400728 dprintf(1, "r-echs");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400729 // Take care not to overflow
730 if (heads==16) {
731 if (cylinders>61439)
732 cylinders=61439;
733 heads=15;
734 cylinders = (u16)((u32)(cylinders)*16/15);
735 }
736 // then go through the large bitshift process
737 case ATA_TRANSLATION_LARGE:
738 if (translation == ATA_TRANSLATION_LARGE)
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400739 dprintf(1, "large");
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400740 while (cylinders > 1024) {
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400741 cylinders >>= 1;
742 heads <<= 1;
743
744 // If we max out the head count
745 if (heads > 127)
746 break;
747 }
748 break;
749 }
750 // clip to 1024 cylinders in lchs
751 if (cylinders > 1024)
752 cylinders = 1024;
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400753 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400754
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400755 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
756 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
757 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400758}
759
760static void
761init_drive_ata(int driveid)
762{
763 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA);
764
765 // Temporary values to do the transfer
766 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
767 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
768
769 // Now we send a IDENTIFY command to ATA device
770 u8 buffer[0x0200];
771 memset(buffer, 0, sizeof(buffer));
772 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
773 , 1, 1
774 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
775 if (ret)
776 BX_PANIC("ata-detect: Failed to detect ATA device\n");
777
778 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400779 u8 mode = buffer[48*2] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
780 u16 blksize = IDE_SECTOR_SIZE;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400781
782 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
783 u16 heads = *(u16*)&buffer[3*2]; // word 3
784 u16 spt = *(u16*)&buffer[6*2]; // word 6
785
786 u64 sectors;
787 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
788 sectors = *(u64*)&buffer[100*2]; // word 100-103
789 else
790 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
791
792 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
793 SET_EBDA(ata.devices[driveid].removable, removable);
794 SET_EBDA(ata.devices[driveid].mode, mode);
795 SET_EBDA(ata.devices[driveid].blksize, blksize);
796 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
797 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
798 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
799 SET_EBDA(ata.devices[driveid].sectors, sectors);
800
801 // Setup disk geometry translation.
802 setup_translation(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400803
804 // fill hdidmap
805 u8 hdcount = GET_EBDA(ata.hdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400806 SET_EBDA(ata.idmap[0][hdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400807 SET_EBDA(ata.hdcount, ++hdcount);
808
Kevin O'Connorc1437612008-05-18 01:43:07 -0400809 // Fill "fdpt" structure.
810 fill_fdpt(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400811
Kevin O'Connorc1437612008-05-18 01:43:07 -0400812 // Report drive info to user.
813 u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400814 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400815 u8 version = get_ata_version(buffer);
816 if (sizeinmb < (1 << 16))
Kevin O'Connora05223c2008-06-28 12:15:57 -0400817 printf(" ATA-%d Hard-Disk (%u MiBytes)\n", version, (u32)sizeinmb);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400818 else
Kevin O'Connora05223c2008-06-28 12:15:57 -0400819 printf(" ATA-%d Hard-Disk (%u GiBytes)\n", version
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400820 , (u32)(sizeinmb >> 10));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400821}
822
823static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400824init_drive_unknown(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400825{
Kevin O'Connor970a0322008-10-26 12:01:21 -0400826 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_UNKNOWN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400827
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400828 u8 channel = driveid / 2;
829 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400830 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
831}
832
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400833static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500834ata_detect()
835{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500836 // Device detection
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400837 int driveid;
838 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
839 u8 channel = driveid / 2;
840 u8 slave = driveid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500841
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400842 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
843 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400844 if (!iobase1)
845 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500846
847 // Disable interrupts
848 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
849
850 // Look for device
851 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400852 msleep(50);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500853 outb(0x55, iobase1+ATA_CB_SC);
854 outb(0xaa, iobase1+ATA_CB_SN);
855 outb(0xaa, iobase1+ATA_CB_SC);
856 outb(0x55, iobase1+ATA_CB_SN);
857 outb(0x55, iobase1+ATA_CB_SC);
858 outb(0xaa, iobase1+ATA_CB_SN);
859
860 // If we found something
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400861 u8 sc = inb(iobase1+ATA_CB_SC);
862 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400863 dprintf(6, "ata_detect(1) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400864
865 if (sc != 0x55 || sn != 0xaa)
866 continue;
867
868 // reset the channel
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400869 ata_reset(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400870
871 // check for ATA or ATAPI
872 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400873 msleep(50);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500874 sc = inb(iobase1+ATA_CB_SC);
875 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400876 dprintf(6, "ata_detect(2) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400877 if (sc!=0x01 || sn!=0x01) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400878 init_drive_unknown(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400879 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500880 }
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400881 u8 cl = inb(iobase1+ATA_CB_CL);
882 u8 ch = inb(iobase1+ATA_CB_CH);
883 u8 st = inb(iobase1+ATA_CB_STAT);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400884 dprintf(6, "ata_detect(3) drive=%d sc=%x sn=%x cl=%x ch=%x st=%x\n"
885 , driveid, sc, sn, cl, ch, st);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500886
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400887 if (cl==0x14 && ch==0xeb)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400888 init_drive_atapi(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400889 else if (cl==0x00 && ch==0x00 && st!=0x00)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400890 init_drive_ata(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400891 else if (cl==0xff && ch==0xff)
892 // None
893 continue;
894 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400895 init_drive_unknown(driveid);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500896 }
897
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500898 printf("\n");
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500899}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400900
901static void
902ata_init()
903{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400904 // hdidmap and cdidmap init.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400905 u8 device;
906 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
907 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
908 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
909 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400910
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400911 // Scan PCI bus for ATA adapters
912 int count=0, index=0;
Kevin O'Connor2ed2f582008-11-08 15:53:36 -0500913 u16 classid = PCI_CLASS_STORAGE_OTHER; // SATA first
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400914 while (count<CONFIG_MAX_ATA_INTERFACES-1) {
915 PCIDevice d;
916 int ret = pci_find_class(classid, index, &d);
917 if (ret) {
Kevin O'Connor2ed2f582008-11-08 15:53:36 -0500918 if (classid == PCI_CLASS_STORAGE_IDE)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400919 // Done
920 break;
921 classid = 0x0101; // PATA controllers
922 index = 0;
923 continue;
924 }
925 index++;
926
927 u8 irq = pci_config_readb(d, PCI_INTERRUPT_LINE);
928 SET_EBDA(ata.channels[count].irq, irq);
929 SET_EBDA(ata.channels[count].pci_bdf, pci_to_bdf(d));
930
931 u8 prog_if = pci_config_readb(d, PCI_CLASS_PROG);
932 u32 port1, port2;
933
934 if (classid != 0x0101 || prog_if & 1) {
Kevin O'Connor2ed2f582008-11-08 15:53:36 -0500935 port1 = pci_config_readl(d, PCI_BASE_ADDRESS_0) & ~3;
936 port2 = pci_config_readl(d, PCI_BASE_ADDRESS_1) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400937 } else {
938 port1 = 0x1f0;
939 port2 = 0x3f0;
940 }
941 SET_EBDA(ata.channels[count].iobase1, port1);
942 SET_EBDA(ata.channels[count].iobase2, port2);
943 dprintf(1, "ATA controller %d at %x/%x\n", count, port1, port2);
944 count++;
945
946 if (classid != 0x0101 || prog_if & 4) {
Kevin O'Connor2ed2f582008-11-08 15:53:36 -0500947 port1 = pci_config_readl(d, PCI_BASE_ADDRESS_2) & ~3;
948 port2 = pci_config_readl(d, PCI_BASE_ADDRESS_3) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400949 } else {
950 port1 = 0x170;
951 port2 = 0x370;
952 }
953 dprintf(1, "ATA controller %d at %x/%x\n", count, port1, port2);
954 SET_EBDA(ata.channels[count].iobase1, port1);
955 SET_EBDA(ata.channels[count].iobase2, port2);
956 count++;
957 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400958}
959
960void
961hard_drive_setup()
962{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400963 if (!CONFIG_ATA)
964 return;
965
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400966 dprintf(3, "init hard drives\n");
Kevin O'Connorc1437612008-05-18 01:43:07 -0400967 ata_init();
968 ata_detect();
969
970 // Store the device count
971 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
972
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400973 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400974
975 // Enable IRQ14 (handle_76)
976 unmask_pic2(PIC2_IRQ14);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400977}