blob: a833c1d46aa632cddb90880f06fa0700bb1d19aa [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//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorc09492e2008-03-01 13:38:07 -05007
Kevin O'Connor3491e8b2008-02-29 00:22:27 -05008#include "types.h" // u8
9#include "ioport.h" // inb
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040010#include "util.h" // dprintf
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050011#include "cmos.h" // inb_cmos
Kevin O'Connord21c0892008-11-26 17:02:43 -050012#include "pic.h" // enable_hwirq
Kevin O'Connor9521e262008-07-04 13:04:29 -040013#include "biosvar.h" // GET_EBDA
Kevin O'Connor53236cc2008-08-31 11:16:33 -040014#include "pci.h" // pci_find_class
Kevin O'Connor2ed2f582008-11-08 15:53:36 -050015#include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
16#include "pci_regs.h" // PCI_INTERRUPT_LINE
Kevin O'Connor0a924122009-02-08 19:43:47 -050017#include "boot.h" // add_bcv_hd
Kevin O'Connor609da232008-12-28 23:18:57 -050018#include "disk.h" // struct ata_s
Kevin O'Connor4524bf72008-12-31 00:31:03 -050019#include "atabits.h" // ATA_CB_STAT
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050020
Kevin O'Connora6b9f712008-03-29 12:53:57 -040021#define IDE_SECTOR_SIZE 512
22#define CDROM_SECTOR_SIZE 2048
23
Kevin O'Connor425f2122009-04-18 12:23:00 -040024#define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050025
Kevin O'Connor92f95b02008-12-29 20:42:40 -050026struct ata_s ATA VAR16_32;
Kevin O'Connor609da232008-12-28 23:18:57 -050027
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050028
Kevin O'Connora6b9f712008-03-29 12:53:57 -040029/****************************************************************
30 * Helper functions
31 ****************************************************************/
32
33// Wait for the specified ide state
Kevin O'Connor580e3322009-02-06 22:36:53 -050034static inline int
35await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050036{
Kevin O'Connor4e6c9702008-12-13 10:45:50 -050037 u64 end = calc_future_tsc(timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050038 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -040039 u8 status = inb(base+ATA_CB_STAT);
Kevin O'Connor580e3322009-02-06 22:36:53 -050040 if ((status & mask) == flags)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040041 return status;
Kevin O'Connorf3587592009-02-15 13:02:56 -050042 if (rdtscll() > end) {
Kevin O'Connor580e3322009-02-06 22:36:53 -050043 dprintf(1, "IDE time out\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050044 return -1;
45 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050046 }
Kevin O'Connor580e3322009-02-06 22:36:53 -050047}
48
49// Wait for the device to be not-busy.
50static int
51await_not_bsy(u16 base)
52{
53 return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
54}
55
56// Wait for the device to be ready.
57static int
58await_rdy(u16 base)
59{
60 return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050061}
62
Kevin O'Connora6b9f712008-03-29 12:53:57 -040063// Wait for ide state - pauses for one ata cycle first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050064static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050065pause_await_not_bsy(u16 iobase1, u16 iobase2)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040066{
67 // Wait one PIO transfer cycle.
68 inb(iobase2 + ATA_CB_ASTAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050069
Kevin O'Connor580e3322009-02-06 22:36:53 -050070 return await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -040071}
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050072
Kevin O'Connoref2822a2008-06-07 15:23:11 -040073// Wait for ide state - pause for 400ns first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050074static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050075ndelay_await_not_bsy(u16 iobase1)
Kevin O'Connoref2822a2008-06-07 15:23:11 -040076{
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050077 ndelay(400);
Kevin O'Connor580e3322009-02-06 22:36:53 -050078 return await_not_bsy(iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -040079}
80
Kevin O'Connora6b9f712008-03-29 12:53:57 -040081// Reset a drive
Kevin O'Connorb1144362009-08-11 20:43:38 -040082static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -040083ata_reset(int driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050084{
Kevin O'Connorb1144362009-08-11 20:43:38 -040085 u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
86 u8 channel = ataid / 2;
87 u8 slave = ataid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -050088 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
89 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050090
Kevin O'Connor580e3322009-02-06 22:36:53 -050091 dprintf(6, "ata_reset driveid=%d\n", driveid);
92 // Pulse SRST
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050093 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -050094 udelay(5);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050095 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -050096 mdelay(2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050097
Kevin O'Connor580e3322009-02-06 22:36:53 -050098 // wait for device to become not busy.
99 int status = await_not_bsy(iobase1);
100 if (status < 0)
101 goto done;
102 if (slave) {
103 // Change device.
104 u64 end = calc_future_tsc(IDE_TIMEOUT);
105 for (;;) {
106 outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400107 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500108 if (status < 0)
109 goto done;
110 if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
111 break;
112 // Change drive request failed to take effect - retry.
Kevin O'Connorf3587592009-02-15 13:02:56 -0500113 if (rdtscll() > end) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500114 dprintf(1, "ata_reset slave time out\n");
115 goto done;
116 }
117 }
Kevin O'Connor2e3eeeb2008-06-12 22:29:30 -0400118 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500119
Kevin O'Connor580e3322009-02-06 22:36:53 -0500120 // On a user-reset request, wait for RDY if it is an ATA device.
121 u8 type=GET_GLOBAL(ATA.devices[driveid].type);
Kevin O'Connor42337662009-08-10 00:06:37 -0400122 if (type == DTYPE_ATA)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500123 status = await_rdy(iobase1);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400124
Kevin O'Connor580e3322009-02-06 22:36:53 -0500125done:
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500126 // Enable interrupts
127 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500128
129 dprintf(6, "ata_reset exit status=%x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500130}
131
Kevin O'Connor42337662009-08-10 00:06:37 -0400132static int
133isready(int driveid)
134{
135 // Read the status from controller
Kevin O'Connorb1144362009-08-11 20:43:38 -0400136 u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
137 u8 channel = ataid / 2;
Kevin O'Connor42337662009-08-10 00:06:37 -0400138 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
139 u8 status = inb(iobase1 + ATA_CB_STAT);
140 return (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY;
141}
142
143static int
144process_ata_misc_op(struct disk_op_s *op)
145{
146 switch (op->command) {
147 default:
148 return 0;
149 case CMD_RESET:
150 ata_reset(op->driveid);
151 return 0;
152 case CMD_ISREADY:
153 return isready(op->driveid);
154 }
155}
156
Kevin O'Connoree55c762008-05-13 00:18:20 -0400157
158/****************************************************************
159 * ATA send command
160 ****************************************************************/
161
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400162struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400163 u8 feature;
164 u8 sector_count;
165 u8 lba_low;
166 u8 lba_mid;
167 u8 lba_high;
168 u8 device;
169 u8 command;
170
171 u8 sector_count2;
172 u8 lba_low2;
173 u8 lba_mid2;
174 u8 lba_high2;
175};
176
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400177// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400178static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400179send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500180{
Kevin O'Connorb1144362009-08-11 20:43:38 -0400181 u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
182 u8 channel = ataid / 2;
183 u8 slave = ataid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500184 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
185 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500186
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400187 // Disable interrupts
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500188 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400189
190 // Select device
Kevin O'Connor580e3322009-02-06 22:36:53 -0500191 int status = await_not_bsy(iobase1);
192 if (status < 0)
193 return status;
194 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
195 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
196 u8 olddh = inb(iobase1 + ATA_CB_DH);
197 outb(newdh, iobase1 + ATA_CB_DH);
198 if ((olddh ^ newdh) & (1<<4)) {
199 // Was a device change - wait for device to become not busy.
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400200 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500201 if (status < 0)
202 return status;
203 }
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400204
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400205 if (cmd->command & 0x04) {
206 outb(0x00, iobase1 + ATA_CB_FR);
207 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
208 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
209 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
210 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500211 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400212 outb(cmd->feature, iobase1 + ATA_CB_FR);
213 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
214 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
215 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
216 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400217 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500218
Kevin O'Connor580e3322009-02-06 22:36:53 -0500219 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400220 if (status < 0)
221 return status;
222
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500223 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500224 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
225 , status, inb(iobase1 + ATA_CB_ERR));
Kevin O'Connora05223c2008-06-28 12:15:57 -0400226 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400227 }
228 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500229 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400230 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500231 }
232
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400233 return 0;
234}
235
Kevin O'Connoree55c762008-05-13 00:18:20 -0400236
237/****************************************************************
238 * ATA transfers
239 ****************************************************************/
240
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400241// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
242// 'op->driveid'.
243static int
244ata_transfer(struct disk_op_s *op, int iswrite, int blocksize)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400245{
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400246 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d buf=%p\n"
247 , op->driveid, iswrite, op->count, blocksize, op->buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400248
Kevin O'Connorb1144362009-08-11 20:43:38 -0400249 u8 ataid = GET_GLOBAL(ATA.devices[op->driveid].cntl_id);
250 u8 channel = ataid / 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500251 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
252 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400253 int count = op->count;
254 void *buf_fl = op->buf_fl;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400255 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400256 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400257 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400258 // Write data to controller
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400259 dprintf(16, "Write sector id=%d dest=%p\n", op->driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500260 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400261 outsl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400262 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400263 outsw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500264 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400265 // Read data from controller
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400266 dprintf(16, "Read sector id=%d dest=%p\n", op->driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500267 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400268 insl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400269 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400270 insw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400271 }
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400272 buf_fl += blocksize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400273
Kevin O'Connor580e3322009-02-06 22:36:53 -0500274 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400275 if (status < 0) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400276 // Error
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400277 op->count -= count;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400278 return status;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400279 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400280
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400281 count--;
282 if (!count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400283 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500284 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
285 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400286 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500287 , status);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400288 op->count -= count;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400289 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500290 }
291 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400292
Kevin O'Connor580e3322009-02-06 22:36:53 -0500293 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
294 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400295 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400296 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500297 if (status != 0) {
298 dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400299 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400300 }
301
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500302 // Enable interrupts
303 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
304 return 0;
305}
306
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400307
308/****************************************************************
309 * ATA hard drive functions
310 ****************************************************************/
311
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400312// Read/write count blocks from a harddrive.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400313static int
314ata_cmd_data(struct disk_op_s *op, int iswrite, int command)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400315{
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400316 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400317
318 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400319 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400320
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400321 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400322 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
323 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400324 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400325 cmd.lba_mid2 = lba >> 32;
326 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400327
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400328 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400329 lba &= 0xffffff;
330 }
331
332 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400333 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400334 cmd.lba_low = lba;
335 cmd.lba_mid = lba >> 8;
336 cmd.lba_high = lba >> 16;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500337 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400338
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400339 int ret = send_cmd(op->driveid, &cmd);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400340 if (ret)
341 return ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400342 return ata_transfer(op, iswrite, IDE_SECTOR_SIZE);
343}
344
345int
346process_ata_op(struct disk_op_s *op)
347{
348 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400349 case CMD_READ:
350 return ata_cmd_data(op, 0, ATA_CMD_READ_SECTORS);
351 case CMD_WRITE:
352 return ata_cmd_data(op, 1, ATA_CMD_WRITE_SECTORS);
Kevin O'Connor42337662009-08-10 00:06:37 -0400353 default:
354 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400355 }
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400356}
357
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400358
359/****************************************************************
360 * ATAPI functions
361 ****************************************************************/
362
363// Low-level atapi command transmit function.
Kevin O'Connora9caeae2009-03-07 00:09:52 -0500364static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400365send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500366{
Kevin O'Connorb1144362009-08-11 20:43:38 -0400367 u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
368 u8 channel = ataid / 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500369 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
370 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500371
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400372 struct ata_pio_command cmd;
373 cmd.sector_count = 0;
374 cmd.feature = 0;
375 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400376 cmd.lba_mid = blocksize;
377 cmd.lba_high = blocksize >> 8;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500378 cmd.device = 0;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400379 cmd.command = ATA_CMD_PACKET;
380
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400381 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400382 if (ret)
383 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500384
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400385 // Send command to device
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500386 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500387
Kevin O'Connor580e3322009-02-06 22:36:53 -0500388 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400389 if (status < 0)
390 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400391
Kevin O'Connor580e3322009-02-06 22:36:53 -0500392 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connorb30c4002009-05-05 21:47:20 -0400393 u8 err = inb(iobase1 + ATA_CB_ERR);
394 // skip "Not Ready"
395 if (err != 0x20)
396 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
397 , status, err);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500398 return -2;
399 }
400 if (!(status & ATA_CB_STAT_DRQ)) {
401 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
402 return -3;
403 }
404
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500405 return 0;
406}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500407
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400408// Read sectors from the cdrom.
409int
410cdrom_read(struct disk_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500411{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400412 u8 atacmd[12];
413 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connoree55c762008-05-13 00:18:20 -0400414 atacmd[0]=0x28; // READ command
415 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
416 atacmd[8]=(op->count & 0x00ff);
417 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
418 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
419 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
420 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400421
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400422 int ret = send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
423 , CDROM_SECTOR_SIZE);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400424 if (ret)
425 return ret;
426
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400427 return ata_transfer(op, 0, CDROM_SECTOR_SIZE);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400428}
429
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400430int
431process_atapi_op(struct disk_op_s *op)
432{
433 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400434 case CMD_READ:
435 return cdrom_read(op);
Kevin O'Connor42337662009-08-10 00:06:37 -0400436 default:
437 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400438 }
439}
440
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400441// Send a simple atapi command to a drive.
442int
443ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500444 , u32 length, void *buf_fl)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400445{
446 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
447 if (ret)
448 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400449
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400450 struct disk_op_s dop;
451 memset(&dop, 0, sizeof(dop));
452 dop.driveid = driveid;
453 dop.count = 1;
454 dop.buf_fl = buf_fl;
455
456 return ata_transfer(&dop, 0, length);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400457}
458
459
460/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500461 * Disk geometry translation
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400462 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500463
Kevin O'Connorc1437612008-05-18 01:43:07 -0400464static u8
465get_translation(int driveid)
466{
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400467 if (! CONFIG_COREBOOT) {
468 // Emulators pass in the translation info via nvram.
Kevin O'Connorb1144362009-08-11 20:43:38 -0400469 u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
470 u8 channel = ataid / 2;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400471 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400472 translation >>= 2 * (ataid % 4);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400473 translation &= 0x03;
474 return translation;
475 }
476
477 // On COREBOOT, use a heuristic to determine translation type.
Kevin O'Connor609da232008-12-28 23:18:57 -0500478 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
479 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
480 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400481
482 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
Kevin O'Connor42337662009-08-10 00:06:37 -0400483 return TRANSLATION_NONE;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400484 if (cylinders * heads <= 131072)
Kevin O'Connor42337662009-08-10 00:06:37 -0400485 return TRANSLATION_LARGE;
486 return TRANSLATION_LBA;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400487}
488
489static void
490setup_translation(int driveid)
491{
492 u8 translation = get_translation(driveid);
Kevin O'Connor609da232008-12-28 23:18:57 -0500493 SET_GLOBAL(ATA.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400494
Kevin O'Connorb1144362009-08-11 20:43:38 -0400495 u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
496 u8 channel = ataid / 2;
497 u8 slave = ataid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500498 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
499 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
500 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
501 u64 sectors = GET_GLOBAL(ATA.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400502
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400503 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400504 , channel, slave, cylinders, heads, spt);
505 switch (translation) {
Kevin O'Connor42337662009-08-10 00:06:37 -0400506 case TRANSLATION_NONE:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400507 dprintf(1, "none");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400508 break;
Kevin O'Connor42337662009-08-10 00:06:37 -0400509 case TRANSLATION_LBA:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400510 dprintf(1, "lba");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400511 spt = 63;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400512 if (sectors > 63*255*1024) {
513 heads = 255;
514 cylinders = 1024;
515 break;
516 }
517 u32 sect = (u32)sectors / 63;
518 heads = sect / 1024;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400519 if (heads>128)
520 heads = 255;
521 else if (heads>64)
522 heads = 128;
523 else if (heads>32)
524 heads = 64;
525 else if (heads>16)
526 heads = 32;
527 else
528 heads = 16;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400529 cylinders = sect / heads;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400530 break;
Kevin O'Connor42337662009-08-10 00:06:37 -0400531 case TRANSLATION_RECHS:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400532 dprintf(1, "r-echs");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400533 // Take care not to overflow
534 if (heads==16) {
535 if (cylinders>61439)
536 cylinders=61439;
537 heads=15;
538 cylinders = (u16)((u32)(cylinders)*16/15);
539 }
540 // then go through the large bitshift process
Kevin O'Connor42337662009-08-10 00:06:37 -0400541 case TRANSLATION_LARGE:
542 if (translation == TRANSLATION_LARGE)
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400543 dprintf(1, "large");
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400544 while (cylinders > 1024) {
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400545 cylinders >>= 1;
546 heads <<= 1;
547
548 // If we max out the head count
549 if (heads > 127)
550 break;
551 }
552 break;
553 }
554 // clip to 1024 cylinders in lchs
555 if (cylinders > 1024)
556 cylinders = 1024;
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400557 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400558
Kevin O'Connor609da232008-12-28 23:18:57 -0500559 SET_GLOBAL(ATA.devices[driveid].lchs.heads, heads);
560 SET_GLOBAL(ATA.devices[driveid].lchs.cylinders, cylinders);
561 SET_GLOBAL(ATA.devices[driveid].lchs.spt, spt);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400562}
563
Kevin O'Connor0a924122009-02-08 19:43:47 -0500564
565/****************************************************************
566 * ATA detect and init
567 ****************************************************************/
568
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500569// Extract common information from IDENTIFY commands.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500570static void
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500571extract_identify(int driveid, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500572{
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500573 dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500574
575 // Read model name
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500576 char *model = ATA.devices[driveid].model;
577 int maxsize = ARRAY_SIZE(ATA.devices[driveid].model);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500578 int i;
Kevin O'Connorab515602009-02-11 22:22:15 -0500579 for (i=0; i<maxsize/2; i++) {
580 u16 v = buffer[27+i];
581 model[i*2] = v >> 8;
582 model[i*2+1] = v & 0xff;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500583 }
584 model[maxsize-1] = 0x00;
585
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500586 // Trim trailing spaces from model name.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500587 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
588 model[i] = 0x00;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500589
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500590 // Extract ATA/ATAPI version.
Kevin O'Connorab515602009-02-11 22:22:15 -0500591 u16 ataversion = buffer[80];
Kevin O'Connor0a924122009-02-08 19:43:47 -0500592 u8 version;
593 for (version=15; version>0; version--)
594 if (ataversion & (1<<version))
595 break;
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500596 ATA.devices[driveid].version = version;
597
598 // Common flags.
599 SET_GLOBAL(ATA.devices[driveid].removable, (buffer[0] & 0x80) ? 1 : 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500600}
601
602static int
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400603init_drive_atapi(int driveid, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500604{
605 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400606 memset(buffer, 0, IDE_SECTOR_SIZE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500607 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400608 memset(&dop, 0, sizeof(dop));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500609 dop.driveid = driveid;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500610 dop.count = 1;
611 dop.lba = 1;
612 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400613 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE_PACKET);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500614 if (ret)
615 return ret;
616
617 // Success - setup as ATAPI.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500618 extract_identify(driveid, buffer);
Kevin O'Connor42337662009-08-10 00:06:37 -0400619 SET_GLOBAL(ATA.devices[driveid].type, DTYPE_ATAPI);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500620 SET_GLOBAL(ATA.devices[driveid].blksize, CDROM_SECTOR_SIZE);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400621 SET_GLOBAL(ATA.devices[driveid].sectors, (u64)-1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400622 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500623
624 // Report drive info to user.
Kevin O'Connorb1144362009-08-11 20:43:38 -0400625 u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
626 u8 channel = ataid / 2;
627 u8 slave = ataid % 2;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500628 printf("ata%d-%d: %s ATAPI-%d %s\n", channel, slave
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500629 , ATA.devices[driveid].model, ATA.devices[driveid].version
Kevin O'Connor42337662009-08-10 00:06:37 -0400630 , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
631
632 // fill cdidmap
633 if (iscd) {
634 u8 cdcount = GET_GLOBAL(ATA.cdcount);
635 SET_GLOBAL(ATA.idmap[1][cdcount], driveid);
636 SET_GLOBAL(ATA.cdcount, cdcount+1);
637 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500638
639 return 0;
640}
641
Kevin O'Connor580e3322009-02-06 22:36:53 -0500642static int
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400643init_drive_ata(int driveid, u16 *buffer)
Kevin O'Connorc1437612008-05-18 01:43:07 -0400644{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500645 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400646 memset(buffer, 0, IDE_SECTOR_SIZE);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500647 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400648 memset(&dop, 0, sizeof(dop));
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500649 dop.driveid = driveid;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500650 dop.count = 1;
651 dop.lba = 1;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500652 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400653 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400654 if (ret)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500655 return ret;
656
657 // Success - setup as ATA.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500658 extract_identify(driveid, buffer);
Kevin O'Connor42337662009-08-10 00:06:37 -0400659 SET_GLOBAL(ATA.devices[driveid].type, DTYPE_ATA);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500660 SET_GLOBAL(ATA.devices[driveid].blksize, IDE_SECTOR_SIZE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400661
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500662 SET_GLOBAL(ATA.devices[driveid].pchs.cylinders, buffer[1]);
663 SET_GLOBAL(ATA.devices[driveid].pchs.heads, buffer[3]);
664 SET_GLOBAL(ATA.devices[driveid].pchs.spt, buffer[6]);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400665
666 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500667 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
668 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400669 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500670 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connor609da232008-12-28 23:18:57 -0500671 SET_GLOBAL(ATA.devices[driveid].sectors, sectors);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400672
673 // Setup disk geometry translation.
674 setup_translation(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400675
Kevin O'Connorc1437612008-05-18 01:43:07 -0400676 // Report drive info to user.
Kevin O'Connorb1144362009-08-11 20:43:38 -0400677 u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
678 u8 channel = ataid / 2;
679 u8 slave = ataid % 2;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500680 char *model = ATA.devices[driveid].model;
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500681 printf("ata%d-%d: %s ATA-%d Hard-Disk ", channel, slave, model
682 , ATA.devices[driveid].version);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500683 u64 sizeinmb = sectors >> 11;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400684 if (sizeinmb < (1 << 16))
Kevin O'Connor0a924122009-02-08 19:43:47 -0500685 printf("(%u MiBytes)\n", (u32)sizeinmb);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400686 else
Kevin O'Connor0a924122009-02-08 19:43:47 -0500687 printf("(%u GiBytes)\n", (u32)(sizeinmb >> 10));
688
689 // Register with bcv system.
690 add_bcv_hd(driveid, model);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400691
Kevin O'Connor580e3322009-02-06 22:36:53 -0500692 return 0;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400693}
694
Kevin O'Connor425f2122009-04-18 12:23:00 -0400695static int
696powerup_await_non_bsy(u16 base, u64 end)
697{
698 u8 orstatus = 0;
699 u8 status;
700 for (;;) {
701 status = inb(base+ATA_CB_STAT);
702 if (!(status & ATA_CB_STAT_BSY))
703 break;
704 orstatus |= status;
705 if (orstatus == 0xff) {
706 dprintf(1, "powerup IDE floating\n");
707 return orstatus;
708 }
709 if (rdtscll() > end) {
710 dprintf(1, "powerup IDE time out\n");
711 return -1;
712 }
713 }
714 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
715 return status;
716}
717
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400718static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500719ata_detect()
720{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500721 // Device detection
Kevin O'Connor425f2122009-04-18 12:23:00 -0400722 u64 end = calc_future_tsc(IDE_TIMEOUT);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400723 int ataid, last_reset_ataid=-1, driveid=0;
724 for (ataid=0; ataid<CONFIG_MAX_ATA_INTERFACES*2; ataid++) {
725 u8 channel = ataid / 2;
726 u8 slave = ataid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500727
Kevin O'Connor609da232008-12-28 23:18:57 -0500728 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400729 if (!iobase1)
730 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500731
Kevin O'Connor425f2122009-04-18 12:23:00 -0400732 // Wait for not-bsy.
733 int status = powerup_await_non_bsy(iobase1, end);
734 if (status < 0)
735 continue;
736 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
737 outb(newdh, iobase1+ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400738 ndelay(400);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400739 status = powerup_await_non_bsy(iobase1, end);
740 if (status < 0)
741 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500742
Kevin O'Connor580e3322009-02-06 22:36:53 -0500743 // Check if ioport registers look valid.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400744 outb(newdh, iobase1+ATA_CB_DH);
745 u8 dh = inb(iobase1+ATA_CB_DH);
746 outb(0x55, iobase1+ATA_CB_SC);
747 outb(0xaa, iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400748 u8 sc = inb(iobase1+ATA_CB_SC);
749 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400750 dprintf(6, "ata_detect ataid=%d sc=%x sn=%x dh=%x\n"
751 , ataid, sc, sn, dh);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400752 if (sc != 0x55 || sn != 0xaa || dh != newdh)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400753 continue;
754
Kevin O'Connorb1144362009-08-11 20:43:38 -0400755 // Prepare new driveid.
756 if (driveid >= ARRAY_SIZE(ATA.devices))
757 break;
758 memset(&ATA.devices[driveid], 0, sizeof(ATA.devices[0]));
759 ATA.devices[driveid].cntl_id = ataid;
760
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400761 // reset the channel
Kevin O'Connorb1144362009-08-11 20:43:38 -0400762 if (slave && ataid == last_reset_ataid + 1) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500763 // The drive was just reset - no need to reset it again.
764 } else {
765 ata_reset(driveid);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400766 last_reset_ataid = ataid;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500767 }
768
Kevin O'Connor580e3322009-02-06 22:36:53 -0500769 // check for ATAPI
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400770 u16 buffer[256];
771 int ret = init_drive_atapi(driveid, buffer);
772 if (!ret) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500773 // Found an ATAPI drive.
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400774 } else {
775 u8 st = inb(iobase1+ATA_CB_STAT);
776 if (!st)
777 // Status not set - can't be a valid drive.
778 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500779
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400780 // Wait for RDY.
781 ret = await_rdy(iobase1);
782 if (ret < 0)
783 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500784
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400785 // check for ATA.
786 ret = init_drive_ata(driveid, buffer);
787 if (ret)
788 // No ATA drive found
789 continue;
790 }
Kevin O'Connorb1144362009-08-11 20:43:38 -0400791 driveid++;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500792
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400793 u16 resetresult = buffer[93];
794 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
795 if (!slave && (resetresult & 0xdf61) == 0x4041)
796 // resetresult looks valid and device 0 is responding to
797 // device 1 requests - device 1 must not be present - skip
798 // detection.
Kevin O'Connorb1144362009-08-11 20:43:38 -0400799 ataid++;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500800 }
801
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500802 printf("\n");
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500803}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400804
805static void
806ata_init()
807{
Kevin O'Connoref3d8822009-02-05 19:51:12 -0500808 memset(&ATA, 0, sizeof(ATA));
809
Kevin O'Connorc1437612008-05-18 01:43:07 -0400810 // hdidmap and cdidmap init.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400811 u8 device;
812 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
Kevin O'Connor609da232008-12-28 23:18:57 -0500813 SET_GLOBAL(ATA.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
814 SET_GLOBAL(ATA.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400815 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400816
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400817 // Scan PCI bus for ATA adapters
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500818 int count=0;
819 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -0500820 foreachpci(bdf, max) {
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500821 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400822 continue;
Kevin O'Connor89f67632009-02-11 20:16:49 -0500823 if (count >= ARRAY_SIZE(ATA.channels))
824 break;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400825
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500826 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor609da232008-12-28 23:18:57 -0500827 SET_GLOBAL(ATA.channels[count].irq, irq);
828 SET_GLOBAL(ATA.channels[count].pci_bdf, bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400829
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500830 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400831 u32 port1, port2;
832
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500833 if (prog_if & 1) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500834 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
835 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400836 } else {
837 port1 = 0x1f0;
838 port2 = 0x3f0;
839 }
Kevin O'Connor609da232008-12-28 23:18:57 -0500840 SET_GLOBAL(ATA.channels[count].iobase1, port1);
841 SET_GLOBAL(ATA.channels[count].iobase2, port2);
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500842 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
843 , count, port1, port2, bdf, prog_if);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400844 count++;
845
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500846 if (prog_if & 4) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500847 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
848 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400849 } else {
850 port1 = 0x170;
851 port2 = 0x370;
852 }
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500853 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
854 , count, port1, port2, bdf, prog_if);
Kevin O'Connor609da232008-12-28 23:18:57 -0500855 SET_GLOBAL(ATA.channels[count].iobase1, port1);
856 SET_GLOBAL(ATA.channels[count].iobase2, port2);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400857 count++;
858 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400859}
860
861void
862hard_drive_setup()
863{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400864 if (!CONFIG_ATA)
865 return;
866
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400867 dprintf(3, "init hard drives\n");
Kevin O'Connorc1437612008-05-18 01:43:07 -0400868 ata_init();
869 ata_detect();
870
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400871 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400872
Kevin O'Connord21c0892008-11-26 17:02:43 -0500873 enable_hwirq(14, entry_76);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400874}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500875
876
877/****************************************************************
878 * Drive mapping
879 ****************************************************************/
880
881// Fill in Fixed Disk Parameter Table (located in ebda).
882static void
883fill_fdpt(int driveid)
884{
885 if (driveid > 1)
886 return;
887
888 u16 nlc = GET_GLOBAL(ATA.devices[driveid].lchs.cylinders);
889 u16 nlh = GET_GLOBAL(ATA.devices[driveid].lchs.heads);
890 u16 nlspt = GET_GLOBAL(ATA.devices[driveid].lchs.spt);
891
892 u16 npc = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
893 u16 nph = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
894 u16 npspt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
895
896 struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[driveid];
897 fdpt->precompensation = 0xffff;
898 fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
899 fdpt->landing_zone = npc;
900 fdpt->cylinders = nlc;
901 fdpt->heads = nlh;
902 fdpt->sectors = nlspt;
903
904 if (nlc == npc && nlh == nph && nlspt == npspt)
905 // no logical CHS mapping used, just physical CHS
906 // use Standard Fixed Disk Parameter Table (FDPT)
907 return;
908
909 // complies with Phoenix style Translated Fixed Disk Parameter
910 // Table (FDPT)
911 fdpt->phys_cylinders = npc;
912 fdpt->phys_heads = nph;
913 fdpt->phys_sectors = npspt;
914 fdpt->a0h_signature = 0xa0;
915
916 // Checksum structure.
Kevin O'Connor523e5a92009-07-04 13:46:33 -0400917 fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
Kevin O'Connor9becbf12009-07-12 09:29:46 -0400918
919 if (driveid == 0)
920 SET_IVT(0x41, get_ebda_seg()
921 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
922 else
923 SET_IVT(0x46, get_ebda_seg()
924 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500925}
926
927// Map a drive (that was registered via add_bcv_hd)
928void
929map_drive(int driveid)
930{
931 // fill hdidmap
932 u8 hdcount = GET_BDA(hdcount);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500933 dprintf(3, "Mapping driveid %d to %d\n", driveid, hdcount);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500934 SET_GLOBAL(ATA.idmap[0][hdcount], driveid);
935 SET_BDA(hdcount, hdcount + 1);
936
937 // Fill "fdpt" structure.
938 fill_fdpt(hdcount);
939}