blob: 20d360ab5e560607395c75ffa62205046f1b178d [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'Connor3491e8b2008-02-29 00:22:27 -050082void
Kevin O'Connora6b9f712008-03-29 12:53:57 -040083ata_reset(int driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050084{
Kevin O'Connoref2822a2008-06-07 15:23:11 -040085 u8 channel = driveid / 2;
86 u8 slave = driveid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -050087 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
88 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050089
Kevin O'Connor580e3322009-02-06 22:36:53 -050090 dprintf(6, "ata_reset driveid=%d\n", driveid);
91 // Pulse SRST
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050092 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 -050093 udelay(5);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050094 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -050095 mdelay(2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050096
Kevin O'Connor580e3322009-02-06 22:36:53 -050097 // wait for device to become not busy.
98 int status = await_not_bsy(iobase1);
99 if (status < 0)
100 goto done;
101 if (slave) {
102 // Change device.
103 u64 end = calc_future_tsc(IDE_TIMEOUT);
104 for (;;) {
105 outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400106 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500107 if (status < 0)
108 goto done;
109 if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
110 break;
111 // Change drive request failed to take effect - retry.
Kevin O'Connorf3587592009-02-15 13:02:56 -0500112 if (rdtscll() > end) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500113 dprintf(1, "ata_reset slave time out\n");
114 goto done;
115 }
116 }
Kevin O'Connor2e3eeeb2008-06-12 22:29:30 -0400117 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500118
Kevin O'Connor580e3322009-02-06 22:36:53 -0500119 // On a user-reset request, wait for RDY if it is an ATA device.
120 u8 type=GET_GLOBAL(ATA.devices[driveid].type);
Kevin O'Connor42337662009-08-10 00:06:37 -0400121 if (type == DTYPE_ATA)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500122 status = await_rdy(iobase1);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400123
Kevin O'Connor580e3322009-02-06 22:36:53 -0500124done:
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500125 // Enable interrupts
126 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500127
128 dprintf(6, "ata_reset exit status=%x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500129}
130
Kevin O'Connor42337662009-08-10 00:06:37 -0400131static int
132isready(int driveid)
133{
134 // Read the status from controller
135 u8 channel = driveid / 2;
136 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
137 u8 status = inb(iobase1 + ATA_CB_STAT);
138 return (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY;
139}
140
141static int
142process_ata_misc_op(struct disk_op_s *op)
143{
144 switch (op->command) {
145 default:
146 return 0;
147 case CMD_RESET:
148 ata_reset(op->driveid);
149 return 0;
150 case CMD_ISREADY:
151 return isready(op->driveid);
152 }
153}
154
Kevin O'Connoree55c762008-05-13 00:18:20 -0400155
156/****************************************************************
157 * ATA send command
158 ****************************************************************/
159
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400160struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400161 u8 feature;
162 u8 sector_count;
163 u8 lba_low;
164 u8 lba_mid;
165 u8 lba_high;
166 u8 device;
167 u8 command;
168
169 u8 sector_count2;
170 u8 lba_low2;
171 u8 lba_mid2;
172 u8 lba_high2;
173};
174
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400175// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400176static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400177send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500178{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400179 u8 channel = driveid / 2;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500180 u8 slave = driveid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500181 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
182 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500183
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400184 // Disable interrupts
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500185 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400186
187 // Select device
Kevin O'Connor580e3322009-02-06 22:36:53 -0500188 int status = await_not_bsy(iobase1);
189 if (status < 0)
190 return status;
191 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
192 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
193 u8 olddh = inb(iobase1 + ATA_CB_DH);
194 outb(newdh, iobase1 + ATA_CB_DH);
195 if ((olddh ^ newdh) & (1<<4)) {
196 // Was a device change - wait for device to become not busy.
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400197 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500198 if (status < 0)
199 return status;
200 }
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400201
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400202 if (cmd->command & 0x04) {
203 outb(0x00, iobase1 + ATA_CB_FR);
204 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
205 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
206 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
207 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500208 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400209 outb(cmd->feature, iobase1 + ATA_CB_FR);
210 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
211 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
212 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
213 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400214 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500215
Kevin O'Connor580e3322009-02-06 22:36:53 -0500216 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400217 if (status < 0)
218 return status;
219
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500220 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500221 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
222 , status, inb(iobase1 + ATA_CB_ERR));
Kevin O'Connora05223c2008-06-28 12:15:57 -0400223 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400224 }
225 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500226 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400227 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500228 }
229
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400230 return 0;
231}
232
Kevin O'Connoree55c762008-05-13 00:18:20 -0400233
234/****************************************************************
235 * ATA transfers
236 ****************************************************************/
237
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400238// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
239// 'op->driveid'.
240static int
241ata_transfer(struct disk_op_s *op, int iswrite, int blocksize)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400242{
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400243 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d buf=%p\n"
244 , op->driveid, iswrite, op->count, blocksize, op->buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400245
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400246 u8 channel = op->driveid / 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500247 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
248 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400249 int count = op->count;
250 void *buf_fl = op->buf_fl;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400251 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400252 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400253 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400254 // Write data to controller
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400255 dprintf(16, "Write sector id=%d dest=%p\n", op->driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500256 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400257 outsl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400258 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400259 outsw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500260 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400261 // Read data from controller
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400262 dprintf(16, "Read sector id=%d dest=%p\n", op->driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500263 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400264 insl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400265 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400266 insw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400267 }
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400268 buf_fl += blocksize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400269
Kevin O'Connor580e3322009-02-06 22:36:53 -0500270 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400271 if (status < 0) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400272 // Error
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400273 op->count -= count;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400274 return status;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400275 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400276
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400277 count--;
278 if (!count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400279 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500280 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
281 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400282 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500283 , status);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400284 op->count -= count;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400285 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500286 }
287 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400288
Kevin O'Connor580e3322009-02-06 22:36:53 -0500289 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
290 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400291 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400292 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500293 if (status != 0) {
294 dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400295 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400296 }
297
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500298 // Enable interrupts
299 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
300 return 0;
301}
302
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400303
304/****************************************************************
305 * ATA hard drive functions
306 ****************************************************************/
307
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400308// Read/write count blocks from a harddrive.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400309static int
310ata_cmd_data(struct disk_op_s *op, int iswrite, int command)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400311{
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400312 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400313
314 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400315 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400316
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400317 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400318 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
319 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400320 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400321 cmd.lba_mid2 = lba >> 32;
322 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400323
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400324 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400325 lba &= 0xffffff;
326 }
327
328 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400329 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400330 cmd.lba_low = lba;
331 cmd.lba_mid = lba >> 8;
332 cmd.lba_high = lba >> 16;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500333 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400334
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400335 int ret = send_cmd(op->driveid, &cmd);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400336 if (ret)
337 return ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400338 return ata_transfer(op, iswrite, IDE_SECTOR_SIZE);
339}
340
341int
342process_ata_op(struct disk_op_s *op)
343{
344 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400345 case CMD_READ:
346 return ata_cmd_data(op, 0, ATA_CMD_READ_SECTORS);
347 case CMD_WRITE:
348 return ata_cmd_data(op, 1, ATA_CMD_WRITE_SECTORS);
Kevin O'Connor42337662009-08-10 00:06:37 -0400349 default:
350 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400351 }
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400352}
353
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400354
355/****************************************************************
356 * ATAPI functions
357 ****************************************************************/
358
359// Low-level atapi command transmit function.
Kevin O'Connora9caeae2009-03-07 00:09:52 -0500360static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400361send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500362{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400363 u8 channel = driveid / 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500364 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
365 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500366
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400367 struct ata_pio_command cmd;
368 cmd.sector_count = 0;
369 cmd.feature = 0;
370 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400371 cmd.lba_mid = blocksize;
372 cmd.lba_high = blocksize >> 8;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500373 cmd.device = 0;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400374 cmd.command = ATA_CMD_PACKET;
375
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400376 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400377 if (ret)
378 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500379
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400380 // Send command to device
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500381 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500382
Kevin O'Connor580e3322009-02-06 22:36:53 -0500383 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400384 if (status < 0)
385 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400386
Kevin O'Connor580e3322009-02-06 22:36:53 -0500387 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connorb30c4002009-05-05 21:47:20 -0400388 u8 err = inb(iobase1 + ATA_CB_ERR);
389 // skip "Not Ready"
390 if (err != 0x20)
391 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
392 , status, err);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500393 return -2;
394 }
395 if (!(status & ATA_CB_STAT_DRQ)) {
396 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
397 return -3;
398 }
399
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500400 return 0;
401}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500402
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400403// Read sectors from the cdrom.
404int
405cdrom_read(struct disk_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500406{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400407 u8 atacmd[12];
408 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connoree55c762008-05-13 00:18:20 -0400409 atacmd[0]=0x28; // READ command
410 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
411 atacmd[8]=(op->count & 0x00ff);
412 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
413 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
414 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
415 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400416
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400417 int ret = send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
418 , CDROM_SECTOR_SIZE);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400419 if (ret)
420 return ret;
421
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400422 return ata_transfer(op, 0, CDROM_SECTOR_SIZE);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400423}
424
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400425int
426process_atapi_op(struct disk_op_s *op)
427{
428 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400429 case CMD_READ:
430 return cdrom_read(op);
Kevin O'Connor42337662009-08-10 00:06:37 -0400431 default:
432 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400433 }
434}
435
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400436// Send a simple atapi command to a drive.
437int
438ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500439 , u32 length, void *buf_fl)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400440{
441 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
442 if (ret)
443 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400444
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400445 struct disk_op_s dop;
446 memset(&dop, 0, sizeof(dop));
447 dop.driveid = driveid;
448 dop.count = 1;
449 dop.buf_fl = buf_fl;
450
451 return ata_transfer(&dop, 0, length);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400452}
453
454
455/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500456 * Disk geometry translation
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400457 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500458
Kevin O'Connorc1437612008-05-18 01:43:07 -0400459static u8
460get_translation(int driveid)
461{
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400462 if (! CONFIG_COREBOOT) {
463 // Emulators pass in the translation info via nvram.
464 u8 channel = driveid / 2;
465 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
466 translation >>= 2 * (driveid % 4);
467 translation &= 0x03;
468 return translation;
469 }
470
471 // On COREBOOT, use a heuristic to determine translation type.
Kevin O'Connor609da232008-12-28 23:18:57 -0500472 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
473 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
474 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400475
476 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
Kevin O'Connor42337662009-08-10 00:06:37 -0400477 return TRANSLATION_NONE;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400478 if (cylinders * heads <= 131072)
Kevin O'Connor42337662009-08-10 00:06:37 -0400479 return TRANSLATION_LARGE;
480 return TRANSLATION_LBA;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400481}
482
483static void
484setup_translation(int driveid)
485{
486 u8 translation = get_translation(driveid);
Kevin O'Connor609da232008-12-28 23:18:57 -0500487 SET_GLOBAL(ATA.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400488
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400489 u8 channel = driveid / 2;
490 u8 slave = driveid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500491 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
492 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
493 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
494 u64 sectors = GET_GLOBAL(ATA.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400495
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400496 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400497 , channel, slave, cylinders, heads, spt);
498 switch (translation) {
Kevin O'Connor42337662009-08-10 00:06:37 -0400499 case TRANSLATION_NONE:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400500 dprintf(1, "none");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400501 break;
Kevin O'Connor42337662009-08-10 00:06:37 -0400502 case TRANSLATION_LBA:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400503 dprintf(1, "lba");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400504 spt = 63;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400505 if (sectors > 63*255*1024) {
506 heads = 255;
507 cylinders = 1024;
508 break;
509 }
510 u32 sect = (u32)sectors / 63;
511 heads = sect / 1024;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400512 if (heads>128)
513 heads = 255;
514 else if (heads>64)
515 heads = 128;
516 else if (heads>32)
517 heads = 64;
518 else if (heads>16)
519 heads = 32;
520 else
521 heads = 16;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400522 cylinders = sect / heads;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400523 break;
Kevin O'Connor42337662009-08-10 00:06:37 -0400524 case TRANSLATION_RECHS:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400525 dprintf(1, "r-echs");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400526 // Take care not to overflow
527 if (heads==16) {
528 if (cylinders>61439)
529 cylinders=61439;
530 heads=15;
531 cylinders = (u16)((u32)(cylinders)*16/15);
532 }
533 // then go through the large bitshift process
Kevin O'Connor42337662009-08-10 00:06:37 -0400534 case TRANSLATION_LARGE:
535 if (translation == TRANSLATION_LARGE)
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400536 dprintf(1, "large");
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400537 while (cylinders > 1024) {
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400538 cylinders >>= 1;
539 heads <<= 1;
540
541 // If we max out the head count
542 if (heads > 127)
543 break;
544 }
545 break;
546 }
547 // clip to 1024 cylinders in lchs
548 if (cylinders > 1024)
549 cylinders = 1024;
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400550 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400551
Kevin O'Connor609da232008-12-28 23:18:57 -0500552 SET_GLOBAL(ATA.devices[driveid].lchs.heads, heads);
553 SET_GLOBAL(ATA.devices[driveid].lchs.cylinders, cylinders);
554 SET_GLOBAL(ATA.devices[driveid].lchs.spt, spt);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400555}
556
Kevin O'Connor0a924122009-02-08 19:43:47 -0500557
558/****************************************************************
559 * ATA detect and init
560 ****************************************************************/
561
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500562// Extract common information from IDENTIFY commands.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500563static void
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500564extract_identify(int driveid, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500565{
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500566 dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500567
568 // Read model name
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500569 char *model = ATA.devices[driveid].model;
570 int maxsize = ARRAY_SIZE(ATA.devices[driveid].model);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500571 int i;
Kevin O'Connorab515602009-02-11 22:22:15 -0500572 for (i=0; i<maxsize/2; i++) {
573 u16 v = buffer[27+i];
574 model[i*2] = v >> 8;
575 model[i*2+1] = v & 0xff;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500576 }
577 model[maxsize-1] = 0x00;
578
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500579 // Trim trailing spaces from model name.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500580 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
581 model[i] = 0x00;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500582
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500583 // Extract ATA/ATAPI version.
Kevin O'Connorab515602009-02-11 22:22:15 -0500584 u16 ataversion = buffer[80];
Kevin O'Connor0a924122009-02-08 19:43:47 -0500585 u8 version;
586 for (version=15; version>0; version--)
587 if (ataversion & (1<<version))
588 break;
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500589 ATA.devices[driveid].version = version;
590
591 // Common flags.
592 SET_GLOBAL(ATA.devices[driveid].removable, (buffer[0] & 0x80) ? 1 : 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500593}
594
595static int
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400596init_drive_atapi(int driveid, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500597{
598 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400599 memset(buffer, 0, IDE_SECTOR_SIZE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500600 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400601 memset(&dop, 0, sizeof(dop));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500602 dop.driveid = driveid;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500603 dop.count = 1;
604 dop.lba = 1;
605 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400606 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE_PACKET);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500607 if (ret)
608 return ret;
609
610 // Success - setup as ATAPI.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500611 extract_identify(driveid, buffer);
Kevin O'Connor42337662009-08-10 00:06:37 -0400612 SET_GLOBAL(ATA.devices[driveid].type, DTYPE_ATAPI);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500613 SET_GLOBAL(ATA.devices[driveid].blksize, CDROM_SECTOR_SIZE);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400614 SET_GLOBAL(ATA.devices[driveid].sectors, (u64)-1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400615 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500616
617 // Report drive info to user.
618 u8 channel = driveid / 2;
619 u8 slave = driveid % 2;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500620 printf("ata%d-%d: %s ATAPI-%d %s\n", channel, slave
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500621 , ATA.devices[driveid].model, ATA.devices[driveid].version
Kevin O'Connor42337662009-08-10 00:06:37 -0400622 , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
623
624 // fill cdidmap
625 if (iscd) {
626 u8 cdcount = GET_GLOBAL(ATA.cdcount);
627 SET_GLOBAL(ATA.idmap[1][cdcount], driveid);
628 SET_GLOBAL(ATA.cdcount, cdcount+1);
629 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500630
631 return 0;
632}
633
Kevin O'Connor580e3322009-02-06 22:36:53 -0500634static int
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400635init_drive_ata(int driveid, u16 *buffer)
Kevin O'Connorc1437612008-05-18 01:43:07 -0400636{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500637 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400638 memset(buffer, 0, IDE_SECTOR_SIZE);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500639 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400640 memset(&dop, 0, sizeof(dop));
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500641 dop.driveid = driveid;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500642 dop.count = 1;
643 dop.lba = 1;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500644 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400645 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400646 if (ret)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500647 return ret;
648
649 // Success - setup as ATA.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500650 extract_identify(driveid, buffer);
Kevin O'Connor42337662009-08-10 00:06:37 -0400651 SET_GLOBAL(ATA.devices[driveid].type, DTYPE_ATA);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500652 SET_GLOBAL(ATA.devices[driveid].blksize, IDE_SECTOR_SIZE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400653
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500654 SET_GLOBAL(ATA.devices[driveid].pchs.cylinders, buffer[1]);
655 SET_GLOBAL(ATA.devices[driveid].pchs.heads, buffer[3]);
656 SET_GLOBAL(ATA.devices[driveid].pchs.spt, buffer[6]);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400657
658 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500659 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
660 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400661 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500662 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connor609da232008-12-28 23:18:57 -0500663 SET_GLOBAL(ATA.devices[driveid].sectors, sectors);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400664
665 // Setup disk geometry translation.
666 setup_translation(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400667
Kevin O'Connorc1437612008-05-18 01:43:07 -0400668 // Report drive info to user.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500669 u8 channel = driveid / 2;
670 u8 slave = driveid % 2;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500671 char *model = ATA.devices[driveid].model;
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500672 printf("ata%d-%d: %s ATA-%d Hard-Disk ", channel, slave, model
673 , ATA.devices[driveid].version);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500674 u64 sizeinmb = sectors >> 11;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400675 if (sizeinmb < (1 << 16))
Kevin O'Connor0a924122009-02-08 19:43:47 -0500676 printf("(%u MiBytes)\n", (u32)sizeinmb);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400677 else
Kevin O'Connor0a924122009-02-08 19:43:47 -0500678 printf("(%u GiBytes)\n", (u32)(sizeinmb >> 10));
679
680 // Register with bcv system.
681 add_bcv_hd(driveid, model);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400682
Kevin O'Connor580e3322009-02-06 22:36:53 -0500683 return 0;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400684}
685
Kevin O'Connor425f2122009-04-18 12:23:00 -0400686static int
687powerup_await_non_bsy(u16 base, u64 end)
688{
689 u8 orstatus = 0;
690 u8 status;
691 for (;;) {
692 status = inb(base+ATA_CB_STAT);
693 if (!(status & ATA_CB_STAT_BSY))
694 break;
695 orstatus |= status;
696 if (orstatus == 0xff) {
697 dprintf(1, "powerup IDE floating\n");
698 return orstatus;
699 }
700 if (rdtscll() > end) {
701 dprintf(1, "powerup IDE time out\n");
702 return -1;
703 }
704 }
705 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
706 return status;
707}
708
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400709static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500710ata_detect()
711{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500712 // Device detection
Kevin O'Connor425f2122009-04-18 12:23:00 -0400713 u64 end = calc_future_tsc(IDE_TIMEOUT);
Kevin O'Connor5eac1dd2009-02-07 00:03:11 -0500714 int driveid, last_reset_driveid=-1;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400715 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
716 u8 channel = driveid / 2;
717 u8 slave = driveid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500718
Kevin O'Connor609da232008-12-28 23:18:57 -0500719 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400720 if (!iobase1)
721 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500722
Kevin O'Connor425f2122009-04-18 12:23:00 -0400723 // Wait for not-bsy.
724 int status = powerup_await_non_bsy(iobase1, end);
725 if (status < 0)
726 continue;
727 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
728 outb(newdh, iobase1+ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400729 ndelay(400);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400730 status = powerup_await_non_bsy(iobase1, end);
731 if (status < 0)
732 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500733
Kevin O'Connor580e3322009-02-06 22:36:53 -0500734 // Check if ioport registers look valid.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400735 outb(newdh, iobase1+ATA_CB_DH);
736 u8 dh = inb(iobase1+ATA_CB_DH);
737 outb(0x55, iobase1+ATA_CB_SC);
738 outb(0xaa, iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400739 u8 sc = inb(iobase1+ATA_CB_SC);
740 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400741 dprintf(6, "ata_detect drive=%d sc=%x sn=%x dh=%x\n"
742 , driveid, sc, sn, dh);
743 if (sc != 0x55 || sn != 0xaa || dh != newdh)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400744 continue;
745
746 // reset the channel
Kevin O'Connor580e3322009-02-06 22:36:53 -0500747 if (slave && driveid == last_reset_driveid + 1) {
748 // The drive was just reset - no need to reset it again.
749 } else {
750 ata_reset(driveid);
751 last_reset_driveid = driveid;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500752 }
753
Kevin O'Connor580e3322009-02-06 22:36:53 -0500754 // check for ATAPI
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400755 u16 buffer[256];
756 int ret = init_drive_atapi(driveid, buffer);
757 if (!ret) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500758 // Found an ATAPI drive.
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400759 } else {
760 u8 st = inb(iobase1+ATA_CB_STAT);
761 if (!st)
762 // Status not set - can't be a valid drive.
763 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500764
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400765 // Wait for RDY.
766 ret = await_rdy(iobase1);
767 if (ret < 0)
768 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500769
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400770 // check for ATA.
771 ret = init_drive_ata(driveid, buffer);
772 if (ret)
773 // No ATA drive found
774 continue;
775 }
Kevin O'Connor580e3322009-02-06 22:36:53 -0500776
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400777 u16 resetresult = buffer[93];
778 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
779 if (!slave && (resetresult & 0xdf61) == 0x4041)
780 // resetresult looks valid and device 0 is responding to
781 // device 1 requests - device 1 must not be present - skip
782 // detection.
783 driveid++;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500784 }
785
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500786 printf("\n");
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500787}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400788
789static void
790ata_init()
791{
Kevin O'Connoref3d8822009-02-05 19:51:12 -0500792 memset(&ATA, 0, sizeof(ATA));
793
Kevin O'Connorc1437612008-05-18 01:43:07 -0400794 // hdidmap and cdidmap init.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400795 u8 device;
796 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
Kevin O'Connor609da232008-12-28 23:18:57 -0500797 SET_GLOBAL(ATA.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
798 SET_GLOBAL(ATA.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400799 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400800
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400801 // Scan PCI bus for ATA adapters
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500802 int count=0;
803 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -0500804 foreachpci(bdf, max) {
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500805 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400806 continue;
Kevin O'Connor89f67632009-02-11 20:16:49 -0500807 if (count >= ARRAY_SIZE(ATA.channels))
808 break;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400809
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500810 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor609da232008-12-28 23:18:57 -0500811 SET_GLOBAL(ATA.channels[count].irq, irq);
812 SET_GLOBAL(ATA.channels[count].pci_bdf, bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400813
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500814 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400815 u32 port1, port2;
816
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500817 if (prog_if & 1) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500818 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
819 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400820 } else {
821 port1 = 0x1f0;
822 port2 = 0x3f0;
823 }
Kevin O'Connor609da232008-12-28 23:18:57 -0500824 SET_GLOBAL(ATA.channels[count].iobase1, port1);
825 SET_GLOBAL(ATA.channels[count].iobase2, port2);
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500826 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
827 , count, port1, port2, bdf, prog_if);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400828 count++;
829
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500830 if (prog_if & 4) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500831 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
832 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400833 } else {
834 port1 = 0x170;
835 port2 = 0x370;
836 }
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500837 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
838 , count, port1, port2, bdf, prog_if);
Kevin O'Connor609da232008-12-28 23:18:57 -0500839 SET_GLOBAL(ATA.channels[count].iobase1, port1);
840 SET_GLOBAL(ATA.channels[count].iobase2, port2);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400841 count++;
842 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400843}
844
845void
846hard_drive_setup()
847{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400848 if (!CONFIG_ATA)
849 return;
850
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400851 dprintf(3, "init hard drives\n");
Kevin O'Connorc1437612008-05-18 01:43:07 -0400852 ata_init();
853 ata_detect();
854
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400855 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400856
Kevin O'Connord21c0892008-11-26 17:02:43 -0500857 enable_hwirq(14, entry_76);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400858}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500859
860
861/****************************************************************
862 * Drive mapping
863 ****************************************************************/
864
865// Fill in Fixed Disk Parameter Table (located in ebda).
866static void
867fill_fdpt(int driveid)
868{
869 if (driveid > 1)
870 return;
871
872 u16 nlc = GET_GLOBAL(ATA.devices[driveid].lchs.cylinders);
873 u16 nlh = GET_GLOBAL(ATA.devices[driveid].lchs.heads);
874 u16 nlspt = GET_GLOBAL(ATA.devices[driveid].lchs.spt);
875
876 u16 npc = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
877 u16 nph = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
878 u16 npspt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
879
880 struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[driveid];
881 fdpt->precompensation = 0xffff;
882 fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
883 fdpt->landing_zone = npc;
884 fdpt->cylinders = nlc;
885 fdpt->heads = nlh;
886 fdpt->sectors = nlspt;
887
888 if (nlc == npc && nlh == nph && nlspt == npspt)
889 // no logical CHS mapping used, just physical CHS
890 // use Standard Fixed Disk Parameter Table (FDPT)
891 return;
892
893 // complies with Phoenix style Translated Fixed Disk Parameter
894 // Table (FDPT)
895 fdpt->phys_cylinders = npc;
896 fdpt->phys_heads = nph;
897 fdpt->phys_sectors = npspt;
898 fdpt->a0h_signature = 0xa0;
899
900 // Checksum structure.
Kevin O'Connor523e5a92009-07-04 13:46:33 -0400901 fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
Kevin O'Connor9becbf12009-07-12 09:29:46 -0400902
903 if (driveid == 0)
904 SET_IVT(0x41, get_ebda_seg()
905 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
906 else
907 SET_IVT(0x46, get_ebda_seg()
908 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500909}
910
911// Map a drive (that was registered via add_bcv_hd)
912void
913map_drive(int driveid)
914{
915 // fill hdidmap
916 u8 hdcount = GET_BDA(hdcount);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500917 dprintf(3, "Mapping driveid %d to %d\n", driveid, hdcount);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500918 SET_GLOBAL(ATA.idmap[0][hdcount], driveid);
919 SET_BDA(hdcount, hdcount + 1);
920
921 // Fill "fdpt" structure.
922 fill_fdpt(hdcount);
923}