blob: a33b83052d6b9f9e5ff437c99450717f4ee3b92b [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'Connord21c0892008-11-26 17:02:43 -050013#include "pic.h" // enable_hwirq
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'Connoref2822a2008-06-07 15:23:11 -040088// Wait for ide state - pause for 400ns first.
89static __always_inline int
90ndelay_await_ide(u8 when_done, u16 iobase1, u16 timeout)
91{
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050092 ndelay(400);
Kevin O'Connoref2822a2008-06-07 15:23:11 -040093 return await_ide(when_done, iobase1, timeout);
94}
95
Kevin O'Connora6b9f712008-03-29 12:53:57 -040096// Reset a drive
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050097void
Kevin O'Connora6b9f712008-03-29 12:53:57 -040098ata_reset(int driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050099{
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400100 u8 channel = driveid / 2;
101 u8 slave = driveid % 2;
102 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
103 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500104
105 // Reset
106
107 // 8.2.1 (a) -- set SRST in DC
108 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
109
110 // 8.2.1 (b) -- wait for BSY
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400111 int status = await_ide(BSY, iobase1, 20);
112 dprintf(6, "ata_reset(1) status=%x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500113
114 // 8.2.1 (f) -- clear SRST
115 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
116
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400117 // 8.2.1 (g) -- check for sc==sn==0x01
118 // select device
119 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500120 mdelay(50);
Kevin O'Connor2e3eeeb2008-06-12 22:29:30 -0400121 u8 sc = inb(iobase1+ATA_CB_SC);
122 u8 sn = inb(iobase1+ATA_CB_SN);
123
124 // For predetermined ATA drives - wait for ready.
125 if (sc==0x01 && sn==0x01) {
126 u8 type=GET_EBDA(ata.devices[driveid].type);
127 if (type == ATA_TYPE_ATA)
128 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
129 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500130
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400131 // 8.2.1 (h) -- wait for not BSY
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400132 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
133 dprintf(6, "ata_reset(2) status=%x\n", status);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400134
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500135 // Enable interrupts
136 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
137}
138
Kevin O'Connoree55c762008-05-13 00:18:20 -0400139
140/****************************************************************
141 * ATA send command
142 ****************************************************************/
143
144struct ata_op_s {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400145 u64 lba;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400146 void *far_buffer;
147 u16 driveid;
148 u16 count;
149};
150
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400151struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400152 u8 feature;
153 u8 sector_count;
154 u8 lba_low;
155 u8 lba_mid;
156 u8 lba_high;
157 u8 device;
158 u8 command;
159
160 u8 sector_count2;
161 u8 lba_low2;
162 u8 lba_mid2;
163 u8 lba_high2;
164};
165
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400166// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400167static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400168send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500169{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400170 u8 channel = driveid / 2;
Kevin O'Connor5b15fbf2008-03-08 23:20:41 -0500171 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
172 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500173
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400174 int status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500175 if (status & ATA_CB_STAT_BSY)
Kevin O'Connora05223c2008-06-28 12:15:57 -0400176 return -3;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500177
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400178 // Disable interrupts
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500179 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400180
181 // Select device
182 u8 device = inb(iobase1 + ATA_CB_DH);
183 outb(cmd->device, iobase1 + ATA_CB_DH);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400184 if ((device ^ cmd->device) & (1 << 4))
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400185 // Wait for device to become active.
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500186 mdelay(50);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400187
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400188 if (cmd->command & 0x04) {
189 outb(0x00, iobase1 + ATA_CB_FR);
190 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
191 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
192 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
193 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500194 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400195 outb(cmd->feature, iobase1 + ATA_CB_FR);
196 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
197 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
198 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
199 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400200 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500201
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400202 status = ndelay_await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400203 if (status < 0)
204 return status;
205
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500206 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400207 dprintf(6, "send_cmd : read error\n");
208 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400209 }
210 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400211 dprintf(6, "send_cmd : DRQ not set (status %02x)\n"
212 , (unsigned) status);
213 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500214 }
215
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400216 return 0;
217}
218
Kevin O'Connoree55c762008-05-13 00:18:20 -0400219
220/****************************************************************
221 * ATA transfers
222 ****************************************************************/
223
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400224// Read and discard x number of bytes from an io channel.
225static void
226insx_discard(int mode, int iobase1, int bytes)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400227{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400228 int count, i;
229 if (mode == ATA_MODE_PIO32) {
230 count = bytes / 4;
231 for (i=0; i<count; i++)
232 inl(iobase1);
233 } else {
234 count = bytes / 2;
235 for (i=0; i<count; i++)
236 inw(iobase1);
237 }
238}
239
240// Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
241// 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
242// and/or last block may be partially transferred. This function is
243// inlined because all the callers use different forms and because the
244// large number of parameters would consume a lot of stack space.
Kevin O'Connor74f9c082008-04-13 16:57:16 -0400245static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400246ata_transfer(int driveid, int iswrite, int count, int blocksize
247 , int skipfirst, int skiplast, void *far_buffer)
248{
Kevin O'Connora05223c2008-06-28 12:15:57 -0400249 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d"
250 " skipf=%d skipl=%d buf=%p\n"
251 , driveid, iswrite, count, blocksize
252 , skipfirst, skiplast, far_buffer);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400253
254 // Reset count of transferred data
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400255 SET_EBDA(ata.trsfsectors, 0);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400256
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400257 u8 channel = driveid / 2;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400258 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
259 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400260 u8 mode = GET_EBDA(ata.devices[driveid].mode);
261 int current = 0;
262 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400263 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400264 int bsize = blocksize;
265 if (skipfirst && current == 0) {
266 insx_discard(mode, iobase1, skipfirst);
267 bsize -= skipfirst;
268 }
269 if (skiplast && current == count-1)
270 bsize -= skiplast;
271
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400272 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400273 // Write data to controller
Kevin O'Connora05223c2008-06-28 12:15:57 -0400274 dprintf(16, "Write sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400275 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400276 outsl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400277 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400278 outsw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500279 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400280 // Read data from controller
Kevin O'Connora05223c2008-06-28 12:15:57 -0400281 dprintf(16, "Read sector id=%d dest=%p\n", driveid, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400282 if (mode == ATA_MODE_PIO32)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400283 insl_far(iobase1, far_buffer, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400284 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400285 insw_far(iobase1, far_buffer, bsize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400286 }
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400287 far_buffer += bsize;
288
289 if (skiplast && current == count-1)
290 insx_discard(mode, iobase1, skiplast);
291
292 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
293 if (status < 0)
294 // Error
295 return status;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400296
297 current++;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400298 SET_EBDA(ata.trsfsectors, current);
299 if (current == count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400300 break;
301 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
302 | ATA_CB_STAT_ERR);
303 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400304 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
305 , (unsigned) status);
306 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500307 }
308 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400309
310 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
311 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400312 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400313 status &= ~ATA_CB_STAT_DF;
314 if (status != ATA_CB_STAT_RDY ) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400315 dprintf(6, "ata_transfer : no sectors left (status %02x)\n"
316 , (unsigned) status);
317 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400318 }
319
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500320 // Enable interrupts
321 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
322 return 0;
323}
324
Kevin O'Connoree55c762008-05-13 00:18:20 -0400325static noinline int
326ata_transfer_disk(const struct ata_op_s *op, int iswrite)
327{
328 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
329 , 0, 0, op->far_buffer);
330}
331
332static noinline int
333ata_transfer_cdrom(const struct ata_op_s *op)
334{
335 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
336 , 0, 0, op->far_buffer);
337}
338
339static noinline int
340ata_transfer_emu(const struct ata_op_s *op, int before, int after)
341{
342 int vcount = op->count * 4 - before - after;
343 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
344 , before*512, after*512, op->far_buffer);
345 if (ret) {
346 SET_EBDA(ata.trsfsectors, 0);
347 return ret;
348 }
349 SET_EBDA(ata.trsfsectors, vcount);
350 return 0;
351}
352
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400353
354/****************************************************************
355 * ATA hard drive functions
356 ****************************************************************/
357
Kevin O'Connoree55c762008-05-13 00:18:20 -0400358static noinline int
359send_cmd_disk(const struct ata_op_s *op, u16 command)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400360{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400361 u8 slave = op->driveid % 2;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400362 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400363
364 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400365 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400366
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400367 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400368 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
369 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400370 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400371 cmd.lba_mid2 = lba >> 32;
372 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400373
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400374 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400375 lba &= 0xffffff;
376 }
377
378 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400379 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400380 cmd.lba_low = lba;
381 cmd.lba_mid = lba >> 8;
382 cmd.lba_high = lba >> 16;
383 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
384 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400385
Kevin O'Connoree55c762008-05-13 00:18:20 -0400386 return send_cmd(op->driveid, &cmd);
387}
388
389// Read/write count blocks from a harddrive.
390__always_inline int
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400391ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
Kevin O'Connoree55c762008-05-13 00:18:20 -0400392{
393 struct ata_op_s op;
394 op.driveid = driveid;
395 op.lba = lba;
396 op.count = count;
397 op.far_buffer = far_buffer;
398
399 int ret = send_cmd_disk(&op, command);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400400 if (ret)
401 return ret;
402
403 int iswrite = command == ATA_CMD_WRITE_SECTORS;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400404 return ata_transfer_disk(&op, iswrite);
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400405}
406
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400407
408/****************************************************************
409 * ATAPI functions
410 ****************************************************************/
411
412// Low-level atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400413static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400414send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500415{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400416 u8 channel = driveid / 2;
417 u8 slave = driveid % 2;
Kevin O'Connora20c4a52008-03-09 13:32:36 -0400418 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
419 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500420
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400421 struct ata_pio_command cmd;
422 cmd.sector_count = 0;
423 cmd.feature = 0;
424 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400425 cmd.lba_mid = blocksize;
426 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400427 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
428 cmd.command = ATA_CMD_PACKET;
429
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400430 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400431 if (ret)
432 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500433
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400434 // Send command to device
Kevin O'Connor070231b2008-03-22 20:44:37 -0400435 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500436
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400437 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
438 if (status < 0)
439 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400440
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500441 return 0;
442}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500443
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400444// Low-level cdrom read atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400445static int
446send_cmd_cdrom(const struct ata_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500447{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400448 u8 atacmd[12];
449 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500450
Kevin O'Connoree55c762008-05-13 00:18:20 -0400451 atacmd[0]=0x28; // READ command
452 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
453 atacmd[8]=(op->count & 0x00ff);
454 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
455 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
456 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
457 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400458
Kevin O'Connoree55c762008-05-13 00:18:20 -0400459 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
460 , CDROM_SECTOR_SIZE);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500461}
462
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400463// Read sectors from the cdrom.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400464__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400465cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400466{
Kevin O'Connoree55c762008-05-13 00:18:20 -0400467 struct ata_op_s op;
468 op.driveid = driveid;
469 op.lba = lba;
470 op.count = count;
471 op.far_buffer = far_buffer;
472
473 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400474 if (ret)
475 return ret;
476
Kevin O'Connoree55c762008-05-13 00:18:20 -0400477 return ata_transfer_cdrom(&op);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400478}
479
480// Pretend the cdrom has 512 byte sectors (instead of 2048) and read
481// sectors.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400482__always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400483cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400484{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400485 u32 velba = vlba + vcount - 1;
486 u32 lba = vlba / 4;
487 u32 elba = velba / 4;
488 int count = elba - lba + 1;
489 int before = vlba % 4;
490 int after = 3 - (velba % 4);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400491
Kevin O'Connoree55c762008-05-13 00:18:20 -0400492 struct ata_op_s op;
493 op.driveid = driveid;
494 op.lba = lba;
495 op.count = count;
496 op.far_buffer = far_buffer;
497
Kevin O'Connora05223c2008-06-28 12:15:57 -0400498 dprintf(16, "cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
499 " count=%d before=%d after=%d\n"
500 , driveid, vlba, vcount, far_buffer, lba, elba
501 , count, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400502
Kevin O'Connoree55c762008-05-13 00:18:20 -0400503 int ret = send_cmd_cdrom(&op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400504 if (ret)
505 return ret;
506
Kevin O'Connoree55c762008-05-13 00:18:20 -0400507 return ata_transfer_emu(&op, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400508}
509
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400510// Send a simple atapi command to a drive.
511int
512ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
513 , u32 length, void *far_buffer)
514{
515 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
516 if (ret)
517 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400518
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400519 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
520}
521
522
523/****************************************************************
524 * ATA detect and init
525 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500526
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400527static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400528report_model(int driveid, u8 *buffer)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400529{
530 u8 model[41];
531
532 // Read model name
533 int i;
534 for (i=0; i<40; i+=2) {
535 model[i] = buffer[i+54+1];
536 model[i+1] = buffer[i+54];
537 }
538
539 // Reformat
540 model[40] = 0x00;
541 for (i=39; i>0; i--) {
542 if (model[i] != 0x20)
543 break;
544 model[i] = 0x00;
545 }
546
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400547 u8 channel = driveid / 2;
548 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400549 // XXX - model on stack not %cs
550 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
551}
552
553static u8
554get_ata_version(u8 *buffer)
555{
556 u16 ataversion = *(u16*)&buffer[160];
557 u8 version;
558 for (version=15; version>0; version--)
559 if (ataversion & (1<<version))
560 break;
561 return version;
562}
563
564static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400565init_drive_atapi(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400566{
Kevin O'Connor970a0322008-10-26 12:01:21 -0400567 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATAPI);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400568
569 // Temporary values to do the transfer
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400570 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
571 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400572
573 // Now we send a IDENTIFY command to ATAPI device
574 u8 buffer[0x0200];
575 memset(buffer, 0, sizeof(buffer));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400576 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400577 , 1, 1
Kevin O'Connor070231b2008-03-22 20:44:37 -0400578 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400579 if (ret != 0)
580 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
581
582 u8 type = buffer[1] & 0x1f;
583 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
584 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400585 u16 blksize = CDROM_SECTOR_SIZE;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400586
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400587 SET_EBDA(ata.devices[driveid].device, type);
588 SET_EBDA(ata.devices[driveid].removable, removable);
589 SET_EBDA(ata.devices[driveid].mode, mode);
590 SET_EBDA(ata.devices[driveid].blksize, blksize);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400591
592 // fill cdidmap
593 u8 cdcount = GET_EBDA(ata.cdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400594 SET_EBDA(ata.idmap[1][cdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400595 SET_EBDA(ata.cdcount, ++cdcount);
596
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400597 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400598 u8 version = get_ata_version(buffer);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400599 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400600 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
601 else
602 printf(" ATAPI-%d Device\n", version);
603}
604
605static void
Kevin O'Connorc1437612008-05-18 01:43:07 -0400606fill_fdpt(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400607{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400608 if (driveid > 1)
609 return;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400610
Kevin O'Connorc1437612008-05-18 01:43:07 -0400611 u16 nlc = GET_EBDA(ata.devices[driveid].lchs.cylinders);
612 u16 nlh = GET_EBDA(ata.devices[driveid].lchs.heads);
613 u16 nlspt = GET_EBDA(ata.devices[driveid].lchs.spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400614
Kevin O'Connorc1437612008-05-18 01:43:07 -0400615 u16 npc = GET_EBDA(ata.devices[driveid].pchs.cylinders);
616 u16 nph = GET_EBDA(ata.devices[driveid].pchs.heads);
617 u16 npspt = GET_EBDA(ata.devices[driveid].pchs.spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400618
Kevin O'Connorc1437612008-05-18 01:43:07 -0400619 SET_EBDA(fdpt[driveid].precompensation, 0xffff);
620 SET_EBDA(fdpt[driveid].drive_control_byte, 0xc0 | ((nph > 8) << 3));
621 SET_EBDA(fdpt[driveid].landing_zone, npc);
622 SET_EBDA(fdpt[driveid].cylinders, nlc);
623 SET_EBDA(fdpt[driveid].heads, nlh);
624 SET_EBDA(fdpt[driveid].sectors, nlspt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400625
Kevin O'Connorc1437612008-05-18 01:43:07 -0400626 if (nlc == npc && nlh == nph && nlspt == npspt)
627 // no logical CHS mapping used, just physical CHS
628 // use Standard Fixed Disk Parameter Table (FDPT)
629 return;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400630
Kevin O'Connorc1437612008-05-18 01:43:07 -0400631 // complies with Phoenix style Translated Fixed Disk Parameter
632 // Table (FDPT)
633 SET_EBDA(fdpt[driveid].phys_cylinders, npc);
634 SET_EBDA(fdpt[driveid].phys_heads, nph);
635 SET_EBDA(fdpt[driveid].phys_sectors, npspt);
636 SET_EBDA(fdpt[driveid].a0h_signature, 0xa0);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400637
Kevin O'Connorc1437612008-05-18 01:43:07 -0400638 // Checksum structure.
639 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
640 , fdpt[driveid]));
641 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
642 , fdpt[driveid]) - 1);
643 SET_EBDA(fdpt[driveid].checksum, -sum);
644}
645
646static u8
647get_translation(int driveid)
648{
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400649 if (! CONFIG_COREBOOT) {
650 // Emulators pass in the translation info via nvram.
651 u8 channel = driveid / 2;
652 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
653 translation >>= 2 * (driveid % 4);
654 translation &= 0x03;
655 return translation;
656 }
657
658 // On COREBOOT, use a heuristic to determine translation type.
659 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
660 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
661 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
662
663 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
664 return ATA_TRANSLATION_NONE;
665 if (cylinders * heads <= 131072)
666 return ATA_TRANSLATION_LARGE;
667 return ATA_TRANSLATION_LBA;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400668}
669
670static void
671setup_translation(int driveid)
672{
673 u8 translation = get_translation(driveid);
674 SET_EBDA(ata.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400675
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400676 u8 channel = driveid / 2;
677 u8 slave = driveid % 2;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400678 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
679 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
680 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
681 u64 sectors = GET_EBDA(ata.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400682
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400683 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400684 , channel, slave, cylinders, heads, spt);
685 switch (translation) {
686 case ATA_TRANSLATION_NONE:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400687 dprintf(1, "none");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400688 break;
689 case ATA_TRANSLATION_LBA:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400690 dprintf(1, "lba");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400691 spt = 63;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400692 if (sectors > 63*255*1024) {
693 heads = 255;
694 cylinders = 1024;
695 break;
696 }
697 u32 sect = (u32)sectors / 63;
698 heads = sect / 1024;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400699 if (heads>128)
700 heads = 255;
701 else if (heads>64)
702 heads = 128;
703 else if (heads>32)
704 heads = 64;
705 else if (heads>16)
706 heads = 32;
707 else
708 heads = 16;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400709 cylinders = sect / heads;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400710 break;
711 case ATA_TRANSLATION_RECHS:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400712 dprintf(1, "r-echs");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400713 // Take care not to overflow
714 if (heads==16) {
715 if (cylinders>61439)
716 cylinders=61439;
717 heads=15;
718 cylinders = (u16)((u32)(cylinders)*16/15);
719 }
720 // then go through the large bitshift process
721 case ATA_TRANSLATION_LARGE:
722 if (translation == ATA_TRANSLATION_LARGE)
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400723 dprintf(1, "large");
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400724 while (cylinders > 1024) {
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400725 cylinders >>= 1;
726 heads <<= 1;
727
728 // If we max out the head count
729 if (heads > 127)
730 break;
731 }
732 break;
733 }
734 // clip to 1024 cylinders in lchs
735 if (cylinders > 1024)
736 cylinders = 1024;
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400737 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400738
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400739 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
740 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
741 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400742}
743
744static void
745init_drive_ata(int driveid)
746{
747 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA);
748
749 // Temporary values to do the transfer
750 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
751 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
752
753 // Now we send a IDENTIFY command to ATA device
754 u8 buffer[0x0200];
755 memset(buffer, 0, sizeof(buffer));
756 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
757 , 1, 1
758 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
759 if (ret)
760 BX_PANIC("ata-detect: Failed to detect ATA device\n");
761
762 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400763 u8 mode = buffer[48*2] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
764 u16 blksize = IDE_SECTOR_SIZE;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400765
766 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
767 u16 heads = *(u16*)&buffer[3*2]; // word 3
768 u16 spt = *(u16*)&buffer[6*2]; // word 6
769
770 u64 sectors;
771 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
772 sectors = *(u64*)&buffer[100*2]; // word 100-103
773 else
774 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
775
776 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
777 SET_EBDA(ata.devices[driveid].removable, removable);
778 SET_EBDA(ata.devices[driveid].mode, mode);
779 SET_EBDA(ata.devices[driveid].blksize, blksize);
780 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
781 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
782 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
783 SET_EBDA(ata.devices[driveid].sectors, sectors);
784
785 // Setup disk geometry translation.
786 setup_translation(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400787
788 // fill hdidmap
789 u8 hdcount = GET_EBDA(ata.hdcount);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400790 SET_EBDA(ata.idmap[0][hdcount], driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400791 SET_EBDA(ata.hdcount, ++hdcount);
792
Kevin O'Connorc1437612008-05-18 01:43:07 -0400793 // Fill "fdpt" structure.
794 fill_fdpt(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400795
Kevin O'Connorc1437612008-05-18 01:43:07 -0400796 // Report drive info to user.
797 u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400798 report_model(driveid, buffer);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400799 u8 version = get_ata_version(buffer);
800 if (sizeinmb < (1 << 16))
Kevin O'Connora05223c2008-06-28 12:15:57 -0400801 printf(" ATA-%d Hard-Disk (%u MiBytes)\n", version, (u32)sizeinmb);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400802 else
Kevin O'Connora05223c2008-06-28 12:15:57 -0400803 printf(" ATA-%d Hard-Disk (%u GiBytes)\n", version
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400804 , (u32)(sizeinmb >> 10));
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400805}
806
807static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400808init_drive_unknown(int driveid)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400809{
Kevin O'Connor970a0322008-10-26 12:01:21 -0400810 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_UNKNOWN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400811
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400812 u8 channel = driveid / 2;
813 u8 slave = driveid % 2;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400814 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
815}
816
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400817static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500818ata_detect()
819{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500820 // Device detection
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400821 int driveid;
822 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
823 u8 channel = driveid / 2;
824 u8 slave = driveid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500825
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400826 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
827 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400828 if (!iobase1)
829 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500830
831 // Disable interrupts
832 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
833
834 // Look for device
835 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500836 mdelay(50);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500837 outb(0x55, iobase1+ATA_CB_SC);
838 outb(0xaa, iobase1+ATA_CB_SN);
839 outb(0xaa, iobase1+ATA_CB_SC);
840 outb(0x55, iobase1+ATA_CB_SN);
841 outb(0x55, iobase1+ATA_CB_SC);
842 outb(0xaa, iobase1+ATA_CB_SN);
843
844 // If we found something
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400845 u8 sc = inb(iobase1+ATA_CB_SC);
846 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400847 dprintf(6, "ata_detect(1) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400848
849 if (sc != 0x55 || sn != 0xaa)
850 continue;
851
852 // reset the channel
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400853 ata_reset(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400854
855 // check for ATA or ATAPI
856 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500857 mdelay(50);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500858 sc = inb(iobase1+ATA_CB_SC);
859 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400860 dprintf(6, "ata_detect(2) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400861 if (sc!=0x01 || sn!=0x01) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400862 init_drive_unknown(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400863 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500864 }
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400865 u8 cl = inb(iobase1+ATA_CB_CL);
866 u8 ch = inb(iobase1+ATA_CB_CH);
867 u8 st = inb(iobase1+ATA_CB_STAT);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400868 dprintf(6, "ata_detect(3) drive=%d sc=%x sn=%x cl=%x ch=%x st=%x\n"
869 , driveid, sc, sn, cl, ch, st);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500870
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400871 if (cl==0x14 && ch==0xeb)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400872 init_drive_atapi(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400873 else if (cl==0x00 && ch==0x00 && st!=0x00)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400874 init_drive_ata(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400875 else if (cl==0xff && ch==0xff)
876 // None
877 continue;
878 else
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400879 init_drive_unknown(driveid);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500880 }
881
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500882 printf("\n");
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500883}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400884
885static void
886ata_init()
887{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400888 // hdidmap and cdidmap init.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400889 u8 device;
890 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
891 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
892 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
893 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400894
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400895 // Scan PCI bus for ATA adapters
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500896 int count=0;
897 int bdf, max;
898 foreachpci(bdf, max, 0) {
899 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400900 continue;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400901
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500902 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400903 SET_EBDA(ata.channels[count].irq, irq);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500904 SET_EBDA(ata.channels[count].pci_bdf, bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400905
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500906 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400907 u32 port1, port2;
908
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500909 if (prog_if & 1) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500910 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
911 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400912 } else {
913 port1 = 0x1f0;
914 port2 = 0x3f0;
915 }
916 SET_EBDA(ata.channels[count].iobase1, port1);
917 SET_EBDA(ata.channels[count].iobase2, port2);
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500918 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
919 , count, port1, port2, bdf, prog_if);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400920 count++;
921
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500922 if (prog_if & 4) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500923 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
924 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400925 } else {
926 port1 = 0x170;
927 port2 = 0x370;
928 }
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500929 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
930 , count, port1, port2, bdf, prog_if);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400931 SET_EBDA(ata.channels[count].iobase1, port1);
932 SET_EBDA(ata.channels[count].iobase2, port2);
933 count++;
934 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400935}
936
937void
938hard_drive_setup()
939{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400940 if (!CONFIG_ATA)
941 return;
942
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400943 dprintf(3, "init hard drives\n");
Kevin O'Connorc1437612008-05-18 01:43:07 -0400944 ata_init();
945 ata_detect();
946
947 // Store the device count
948 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
949
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400950 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400951
Kevin O'Connord21c0892008-11-26 17:02:43 -0500952 enable_hwirq(14, entry_76);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400953}