blob: 944f74cf9da3fd3b0bfeb1080e70e269480178eb [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
21#define TIMEOUT 0
22#define BSY 1
23#define NOT_BSY 2
24#define NOT_BSY_DRQ 3
25#define NOT_BSY_NOT_DRQ 4
26#define NOT_BSY_RDY 5
27
Kevin O'Connora6b9f712008-03-29 12:53:57 -040028#define IDE_SECTOR_SIZE 512
29#define CDROM_SECTOR_SIZE 2048
30
Kevin O'Connor425f2122009-04-18 12:23:00 -040031#define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050032
Kevin O'Connor92f95b02008-12-29 20:42:40 -050033struct ata_s ATA VAR16_32;
Kevin O'Connor609da232008-12-28 23:18:57 -050034
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050035
Kevin O'Connora6b9f712008-03-29 12:53:57 -040036/****************************************************************
37 * Helper functions
38 ****************************************************************/
39
40// Wait for the specified ide state
Kevin O'Connor580e3322009-02-06 22:36:53 -050041static inline int
42await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050043{
Kevin O'Connor4e6c9702008-12-13 10:45:50 -050044 u64 end = calc_future_tsc(timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050045 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -040046 u8 status = inb(base+ATA_CB_STAT);
Kevin O'Connor580e3322009-02-06 22:36:53 -050047 if ((status & mask) == flags)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040048 return status;
Kevin O'Connorf3587592009-02-15 13:02:56 -050049 if (rdtscll() > end) {
Kevin O'Connor580e3322009-02-06 22:36:53 -050050 dprintf(1, "IDE time out\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050051 return -1;
52 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050053 }
Kevin O'Connor580e3322009-02-06 22:36:53 -050054}
55
56// Wait for the device to be not-busy.
57static int
58await_not_bsy(u16 base)
59{
60 return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
61}
62
63// Wait for the device to be ready.
64static int
65await_rdy(u16 base)
66{
67 return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050068}
69
Kevin O'Connora6b9f712008-03-29 12:53:57 -040070// Wait for ide state - pauses for one ata cycle first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050071static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050072pause_await_not_bsy(u16 iobase1, u16 iobase2)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040073{
74 // Wait one PIO transfer cycle.
75 inb(iobase2 + ATA_CB_ASTAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050076
Kevin O'Connor580e3322009-02-06 22:36:53 -050077 return await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -040078}
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050079
Kevin O'Connoref2822a2008-06-07 15:23:11 -040080// Wait for ide state - pause for 400ns first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050081static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050082ndelay_await_not_bsy(u16 iobase1)
Kevin O'Connoref2822a2008-06-07 15:23:11 -040083{
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050084 ndelay(400);
Kevin O'Connor580e3322009-02-06 22:36:53 -050085 return await_not_bsy(iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -040086}
87
Kevin O'Connora6b9f712008-03-29 12:53:57 -040088// Reset a drive
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050089void
Kevin O'Connora6b9f712008-03-29 12:53:57 -040090ata_reset(int driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050091{
Kevin O'Connoref2822a2008-06-07 15:23:11 -040092 u8 channel = driveid / 2;
93 u8 slave = driveid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -050094 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
95 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050096
Kevin O'Connor580e3322009-02-06 22:36:53 -050097 dprintf(6, "ata_reset driveid=%d\n", driveid);
98 // Pulse SRST
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050099 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 -0500100 udelay(5);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500101 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500102 mdelay(2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500103
Kevin O'Connor580e3322009-02-06 22:36:53 -0500104 // wait for device to become not busy.
105 int status = await_not_bsy(iobase1);
106 if (status < 0)
107 goto done;
108 if (slave) {
109 // Change device.
110 u64 end = calc_future_tsc(IDE_TIMEOUT);
111 for (;;) {
112 outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400113 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500114 if (status < 0)
115 goto done;
116 if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
117 break;
118 // Change drive request failed to take effect - retry.
Kevin O'Connorf3587592009-02-15 13:02:56 -0500119 if (rdtscll() > end) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500120 dprintf(1, "ata_reset slave time out\n");
121 goto done;
122 }
123 }
Kevin O'Connor2e3eeeb2008-06-12 22:29:30 -0400124 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500125
Kevin O'Connor580e3322009-02-06 22:36:53 -0500126 // On a user-reset request, wait for RDY if it is an ATA device.
127 u8 type=GET_GLOBAL(ATA.devices[driveid].type);
128 if (type == ATA_TYPE_ATA)
129 status = await_rdy(iobase1);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400130
Kevin O'Connor580e3322009-02-06 22:36:53 -0500131done:
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500132 // Enable interrupts
133 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500134
135 dprintf(6, "ata_reset exit status=%x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500136}
137
Kevin O'Connoree55c762008-05-13 00:18:20 -0400138
139/****************************************************************
140 * ATA send command
141 ****************************************************************/
142
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400143struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400144 u8 feature;
145 u8 sector_count;
146 u8 lba_low;
147 u8 lba_mid;
148 u8 lba_high;
149 u8 device;
150 u8 command;
151
152 u8 sector_count2;
153 u8 lba_low2;
154 u8 lba_mid2;
155 u8 lba_high2;
156};
157
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400158// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400159static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400160send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500161{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400162 u8 channel = driveid / 2;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500163 u8 slave = driveid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500164 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
165 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500166
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400167 // Disable interrupts
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500168 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400169
170 // Select device
Kevin O'Connor580e3322009-02-06 22:36:53 -0500171 int status = await_not_bsy(iobase1);
172 if (status < 0)
173 return status;
174 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
175 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
176 u8 olddh = inb(iobase1 + ATA_CB_DH);
177 outb(newdh, iobase1 + ATA_CB_DH);
178 if ((olddh ^ newdh) & (1<<4)) {
179 // Was a device change - wait for device to become not busy.
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400180 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500181 if (status < 0)
182 return status;
183 }
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400184
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400185 if (cmd->command & 0x04) {
186 outb(0x00, iobase1 + ATA_CB_FR);
187 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
188 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
189 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
190 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500191 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400192 outb(cmd->feature, iobase1 + ATA_CB_FR);
193 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
194 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
195 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
196 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400197 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500198
Kevin O'Connor580e3322009-02-06 22:36:53 -0500199 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400200 if (status < 0)
201 return status;
202
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500203 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500204 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
205 , status, inb(iobase1 + ATA_CB_ERR));
Kevin O'Connora05223c2008-06-28 12:15:57 -0400206 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400207 }
208 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500209 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400210 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500211 }
212
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400213 return 0;
214}
215
Kevin O'Connoree55c762008-05-13 00:18:20 -0400216
217/****************************************************************
218 * ATA transfers
219 ****************************************************************/
220
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400221// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
222// 'op->driveid'.
223static int
224ata_transfer(struct disk_op_s *op, int iswrite, int blocksize)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400225{
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400226 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d buf=%p\n"
227 , op->driveid, iswrite, op->count, blocksize, op->buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400228
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400229 u8 channel = op->driveid / 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500230 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
231 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400232 int count = op->count;
233 void *buf_fl = op->buf_fl;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400234 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400235 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400236 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400237 // Write data to controller
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400238 dprintf(16, "Write sector id=%d dest=%p\n", op->driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500239 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400240 outsl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400241 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400242 outsw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500243 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400244 // Read data from controller
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400245 dprintf(16, "Read sector id=%d dest=%p\n", op->driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500246 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400247 insl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400248 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400249 insw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400250 }
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400251 buf_fl += blocksize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400252
Kevin O'Connor580e3322009-02-06 22:36:53 -0500253 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400254 if (status < 0) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400255 // Error
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400256 op->count -= count;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400257 return status;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400258 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400259
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400260 count--;
261 if (!count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400262 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500263 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
264 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400265 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500266 , status);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400267 op->count -= count;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400268 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500269 }
270 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400271
Kevin O'Connor580e3322009-02-06 22:36:53 -0500272 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
273 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400274 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400275 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500276 if (status != 0) {
277 dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400278 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400279 }
280
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500281 // Enable interrupts
282 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
283 return 0;
284}
285
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400286
287/****************************************************************
288 * ATA hard drive functions
289 ****************************************************************/
290
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400291// Read/write count blocks from a harddrive.
292int
293ata_cmd_data(struct disk_op_s *op)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400294{
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400295 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400296
297 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400298 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400299
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500300 cmd.command = op->command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400301 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
302 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400303 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400304 cmd.lba_mid2 = lba >> 32;
305 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400306
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400307 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400308 lba &= 0xffffff;
309 }
310
311 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400312 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400313 cmd.lba_low = lba;
314 cmd.lba_mid = lba >> 8;
315 cmd.lba_high = lba >> 16;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500316 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400317
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400318 int ret = send_cmd(op->driveid, &cmd);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400319 if (ret)
320 return ret;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400321 return ata_transfer(op, op->command == ATA_CMD_WRITE_SECTORS
322 , IDE_SECTOR_SIZE);
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400323}
324
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400325
326/****************************************************************
327 * ATAPI functions
328 ****************************************************************/
329
330// Low-level atapi command transmit function.
Kevin O'Connora9caeae2009-03-07 00:09:52 -0500331static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400332send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500333{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400334 u8 channel = driveid / 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500335 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
336 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500337
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400338 struct ata_pio_command cmd;
339 cmd.sector_count = 0;
340 cmd.feature = 0;
341 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400342 cmd.lba_mid = blocksize;
343 cmd.lba_high = blocksize >> 8;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500344 cmd.device = 0;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400345 cmd.command = ATA_CMD_PACKET;
346
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400347 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400348 if (ret)
349 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500350
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400351 // Send command to device
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500352 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500353
Kevin O'Connor580e3322009-02-06 22:36:53 -0500354 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400355 if (status < 0)
356 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400357
Kevin O'Connor580e3322009-02-06 22:36:53 -0500358 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connorb30c4002009-05-05 21:47:20 -0400359 u8 err = inb(iobase1 + ATA_CB_ERR);
360 // skip "Not Ready"
361 if (err != 0x20)
362 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
363 , status, err);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500364 return -2;
365 }
366 if (!(status & ATA_CB_STAT_DRQ)) {
367 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
368 return -3;
369 }
370
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500371 return 0;
372}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500373
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400374// Read sectors from the cdrom.
375int
376cdrom_read(struct disk_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500377{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400378 u8 atacmd[12];
379 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connoree55c762008-05-13 00:18:20 -0400380 atacmd[0]=0x28; // READ command
381 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
382 atacmd[8]=(op->count & 0x00ff);
383 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
384 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
385 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
386 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400387
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400388 int ret = send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
389 , CDROM_SECTOR_SIZE);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400390 if (ret)
391 return ret;
392
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400393 return ata_transfer(op, 0, CDROM_SECTOR_SIZE);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400394}
395
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400396// Send a simple atapi command to a drive.
397int
398ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500399 , u32 length, void *buf_fl)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400400{
401 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
402 if (ret)
403 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400404
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400405 struct disk_op_s dop;
406 memset(&dop, 0, sizeof(dop));
407 dop.driveid = driveid;
408 dop.count = 1;
409 dop.buf_fl = buf_fl;
410
411 return ata_transfer(&dop, 0, length);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400412}
413
414
415/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500416 * Disk geometry translation
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400417 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500418
Kevin O'Connorc1437612008-05-18 01:43:07 -0400419static u8
420get_translation(int driveid)
421{
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400422 if (! CONFIG_COREBOOT) {
423 // Emulators pass in the translation info via nvram.
424 u8 channel = driveid / 2;
425 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
426 translation >>= 2 * (driveid % 4);
427 translation &= 0x03;
428 return translation;
429 }
430
431 // On COREBOOT, use a heuristic to determine translation type.
Kevin O'Connor609da232008-12-28 23:18:57 -0500432 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
433 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
434 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400435
436 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
437 return ATA_TRANSLATION_NONE;
438 if (cylinders * heads <= 131072)
439 return ATA_TRANSLATION_LARGE;
440 return ATA_TRANSLATION_LBA;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400441}
442
443static void
444setup_translation(int driveid)
445{
446 u8 translation = get_translation(driveid);
Kevin O'Connor609da232008-12-28 23:18:57 -0500447 SET_GLOBAL(ATA.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400448
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400449 u8 channel = driveid / 2;
450 u8 slave = driveid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500451 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
452 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
453 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
454 u64 sectors = GET_GLOBAL(ATA.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400455
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400456 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400457 , channel, slave, cylinders, heads, spt);
458 switch (translation) {
459 case ATA_TRANSLATION_NONE:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400460 dprintf(1, "none");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400461 break;
462 case ATA_TRANSLATION_LBA:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400463 dprintf(1, "lba");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400464 spt = 63;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400465 if (sectors > 63*255*1024) {
466 heads = 255;
467 cylinders = 1024;
468 break;
469 }
470 u32 sect = (u32)sectors / 63;
471 heads = sect / 1024;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400472 if (heads>128)
473 heads = 255;
474 else if (heads>64)
475 heads = 128;
476 else if (heads>32)
477 heads = 64;
478 else if (heads>16)
479 heads = 32;
480 else
481 heads = 16;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400482 cylinders = sect / heads;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400483 break;
484 case ATA_TRANSLATION_RECHS:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400485 dprintf(1, "r-echs");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400486 // Take care not to overflow
487 if (heads==16) {
488 if (cylinders>61439)
489 cylinders=61439;
490 heads=15;
491 cylinders = (u16)((u32)(cylinders)*16/15);
492 }
493 // then go through the large bitshift process
494 case ATA_TRANSLATION_LARGE:
495 if (translation == ATA_TRANSLATION_LARGE)
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400496 dprintf(1, "large");
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400497 while (cylinders > 1024) {
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400498 cylinders >>= 1;
499 heads <<= 1;
500
501 // If we max out the head count
502 if (heads > 127)
503 break;
504 }
505 break;
506 }
507 // clip to 1024 cylinders in lchs
508 if (cylinders > 1024)
509 cylinders = 1024;
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400510 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400511
Kevin O'Connor609da232008-12-28 23:18:57 -0500512 SET_GLOBAL(ATA.devices[driveid].lchs.heads, heads);
513 SET_GLOBAL(ATA.devices[driveid].lchs.cylinders, cylinders);
514 SET_GLOBAL(ATA.devices[driveid].lchs.spt, spt);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400515}
516
Kevin O'Connor0a924122009-02-08 19:43:47 -0500517
518/****************************************************************
519 * ATA detect and init
520 ****************************************************************/
521
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500522// Extract common information from IDENTIFY commands.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500523static void
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500524extract_identify(int driveid, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500525{
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500526 dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500527
528 // Read model name
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500529 char *model = ATA.devices[driveid].model;
530 int maxsize = ARRAY_SIZE(ATA.devices[driveid].model);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500531 int i;
Kevin O'Connorab515602009-02-11 22:22:15 -0500532 for (i=0; i<maxsize/2; i++) {
533 u16 v = buffer[27+i];
534 model[i*2] = v >> 8;
535 model[i*2+1] = v & 0xff;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500536 }
537 model[maxsize-1] = 0x00;
538
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500539 // Trim trailing spaces from model name.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500540 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
541 model[i] = 0x00;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500542
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500543 // Extract ATA/ATAPI version.
Kevin O'Connorab515602009-02-11 22:22:15 -0500544 u16 ataversion = buffer[80];
Kevin O'Connor0a924122009-02-08 19:43:47 -0500545 u8 version;
546 for (version=15; version>0; version--)
547 if (ataversion & (1<<version))
548 break;
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500549 ATA.devices[driveid].version = version;
550
551 // Common flags.
552 SET_GLOBAL(ATA.devices[driveid].removable, (buffer[0] & 0x80) ? 1 : 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500553}
554
555static int
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400556init_drive_atapi(int driveid, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500557{
558 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400559 memset(buffer, 0, IDE_SECTOR_SIZE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500560 struct disk_op_s dop;
561 dop.driveid = driveid;
562 dop.command = ATA_CMD_IDENTIFY_DEVICE_PACKET;
563 dop.count = 1;
564 dop.lba = 1;
565 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
566 int ret = ata_cmd_data(&dop);
567 if (ret)
568 return ret;
569
570 // Success - setup as ATAPI.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500571 extract_identify(driveid, buffer);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500572 SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATAPI);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500573 SET_GLOBAL(ATA.devices[driveid].device, (buffer[0] >> 8) & 0x1f);
574 SET_GLOBAL(ATA.devices[driveid].blksize, CDROM_SECTOR_SIZE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500575
576 // fill cdidmap
577 u8 cdcount = GET_GLOBAL(ATA.cdcount);
578 SET_GLOBAL(ATA.idmap[1][cdcount], driveid);
579 SET_GLOBAL(ATA.cdcount, cdcount+1);
580
581 // Report drive info to user.
582 u8 channel = driveid / 2;
583 u8 slave = driveid % 2;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500584 printf("ata%d-%d: %s ATAPI-%d %s\n", channel, slave
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500585 , ATA.devices[driveid].model, ATA.devices[driveid].version
Kevin O'Connor6a46a422009-02-17 22:57:37 -0500586 , (ATA.devices[driveid].device == ATA_DEVICE_CDROM
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500587 ? "CD-Rom/DVD-Rom" : "Device"));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500588
589 return 0;
590}
591
Kevin O'Connor580e3322009-02-06 22:36:53 -0500592static int
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400593init_drive_ata(int driveid, u16 *buffer)
Kevin O'Connorc1437612008-05-18 01:43:07 -0400594{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500595 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400596 memset(buffer, 0, IDE_SECTOR_SIZE);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500597 struct disk_op_s dop;
598 dop.driveid = driveid;
599 dop.command = ATA_CMD_IDENTIFY_DEVICE;
600 dop.count = 1;
601 dop.lba = 1;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500602 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500603 int ret = ata_cmd_data(&dop);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400604 if (ret)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500605 return ret;
606
607 // Success - setup as ATA.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500608 extract_identify(driveid, buffer);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500609 SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATA);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500610 SET_GLOBAL(ATA.devices[driveid].device, ATA_DEVICE_HD);
611 SET_GLOBAL(ATA.devices[driveid].blksize, IDE_SECTOR_SIZE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400612
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500613 SET_GLOBAL(ATA.devices[driveid].pchs.cylinders, buffer[1]);
614 SET_GLOBAL(ATA.devices[driveid].pchs.heads, buffer[3]);
615 SET_GLOBAL(ATA.devices[driveid].pchs.spt, buffer[6]);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400616
617 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500618 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
619 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400620 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500621 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connor609da232008-12-28 23:18:57 -0500622 SET_GLOBAL(ATA.devices[driveid].sectors, sectors);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400623
624 // Setup disk geometry translation.
625 setup_translation(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400626
Kevin O'Connorc1437612008-05-18 01:43:07 -0400627 // Report drive info to user.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500628 u8 channel = driveid / 2;
629 u8 slave = driveid % 2;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500630 char *model = ATA.devices[driveid].model;
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500631 printf("ata%d-%d: %s ATA-%d Hard-Disk ", channel, slave, model
632 , ATA.devices[driveid].version);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500633 u64 sizeinmb = sectors >> 11;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400634 if (sizeinmb < (1 << 16))
Kevin O'Connor0a924122009-02-08 19:43:47 -0500635 printf("(%u MiBytes)\n", (u32)sizeinmb);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400636 else
Kevin O'Connor0a924122009-02-08 19:43:47 -0500637 printf("(%u GiBytes)\n", (u32)(sizeinmb >> 10));
638
639 // Register with bcv system.
640 add_bcv_hd(driveid, model);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400641
Kevin O'Connor580e3322009-02-06 22:36:53 -0500642 return 0;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400643}
644
Kevin O'Connor425f2122009-04-18 12:23:00 -0400645static int
646powerup_await_non_bsy(u16 base, u64 end)
647{
648 u8 orstatus = 0;
649 u8 status;
650 for (;;) {
651 status = inb(base+ATA_CB_STAT);
652 if (!(status & ATA_CB_STAT_BSY))
653 break;
654 orstatus |= status;
655 if (orstatus == 0xff) {
656 dprintf(1, "powerup IDE floating\n");
657 return orstatus;
658 }
659 if (rdtscll() > end) {
660 dprintf(1, "powerup IDE time out\n");
661 return -1;
662 }
663 }
664 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
665 return status;
666}
667
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400668static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500669ata_detect()
670{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500671 // Device detection
Kevin O'Connor425f2122009-04-18 12:23:00 -0400672 u64 end = calc_future_tsc(IDE_TIMEOUT);
Kevin O'Connor5eac1dd2009-02-07 00:03:11 -0500673 int driveid, last_reset_driveid=-1;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400674 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
675 u8 channel = driveid / 2;
676 u8 slave = driveid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500677
Kevin O'Connor609da232008-12-28 23:18:57 -0500678 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400679 if (!iobase1)
680 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500681
Kevin O'Connor425f2122009-04-18 12:23:00 -0400682 // Wait for not-bsy.
683 int status = powerup_await_non_bsy(iobase1, end);
684 if (status < 0)
685 continue;
686 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
687 outb(newdh, iobase1+ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400688 ndelay(400);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400689 status = powerup_await_non_bsy(iobase1, end);
690 if (status < 0)
691 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500692
Kevin O'Connor580e3322009-02-06 22:36:53 -0500693 // Check if ioport registers look valid.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400694 outb(newdh, iobase1+ATA_CB_DH);
695 u8 dh = inb(iobase1+ATA_CB_DH);
696 outb(0x55, iobase1+ATA_CB_SC);
697 outb(0xaa, iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400698 u8 sc = inb(iobase1+ATA_CB_SC);
699 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400700 dprintf(6, "ata_detect drive=%d sc=%x sn=%x dh=%x\n"
701 , driveid, sc, sn, dh);
702 if (sc != 0x55 || sn != 0xaa || dh != newdh)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400703 continue;
704
705 // reset the channel
Kevin O'Connor580e3322009-02-06 22:36:53 -0500706 if (slave && driveid == last_reset_driveid + 1) {
707 // The drive was just reset - no need to reset it again.
708 } else {
709 ata_reset(driveid);
710 last_reset_driveid = driveid;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500711 }
712
Kevin O'Connor580e3322009-02-06 22:36:53 -0500713 // check for ATAPI
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400714 u16 buffer[256];
715 int ret = init_drive_atapi(driveid, buffer);
716 if (!ret) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500717 // Found an ATAPI drive.
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400718 } else {
719 u8 st = inb(iobase1+ATA_CB_STAT);
720 if (!st)
721 // Status not set - can't be a valid drive.
722 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500723
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400724 // Wait for RDY.
725 ret = await_rdy(iobase1);
726 if (ret < 0)
727 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500728
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400729 // check for ATA.
730 ret = init_drive_ata(driveid, buffer);
731 if (ret)
732 // No ATA drive found
733 continue;
734 }
Kevin O'Connor580e3322009-02-06 22:36:53 -0500735
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400736 u16 resetresult = buffer[93];
737 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
738 if (!slave && (resetresult & 0xdf61) == 0x4041)
739 // resetresult looks valid and device 0 is responding to
740 // device 1 requests - device 1 must not be present - skip
741 // detection.
742 driveid++;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500743 }
744
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500745 printf("\n");
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500746}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400747
748static void
749ata_init()
750{
Kevin O'Connoref3d8822009-02-05 19:51:12 -0500751 memset(&ATA, 0, sizeof(ATA));
752
Kevin O'Connorc1437612008-05-18 01:43:07 -0400753 // hdidmap and cdidmap init.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400754 u8 device;
755 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
Kevin O'Connor609da232008-12-28 23:18:57 -0500756 SET_GLOBAL(ATA.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
757 SET_GLOBAL(ATA.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400758 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400759
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400760 // Scan PCI bus for ATA adapters
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500761 int count=0;
762 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -0500763 foreachpci(bdf, max) {
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500764 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400765 continue;
Kevin O'Connor89f67632009-02-11 20:16:49 -0500766 if (count >= ARRAY_SIZE(ATA.channels))
767 break;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400768
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500769 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor609da232008-12-28 23:18:57 -0500770 SET_GLOBAL(ATA.channels[count].irq, irq);
771 SET_GLOBAL(ATA.channels[count].pci_bdf, bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400772
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500773 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400774 u32 port1, port2;
775
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500776 if (prog_if & 1) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500777 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
778 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400779 } else {
780 port1 = 0x1f0;
781 port2 = 0x3f0;
782 }
Kevin O'Connor609da232008-12-28 23:18:57 -0500783 SET_GLOBAL(ATA.channels[count].iobase1, port1);
784 SET_GLOBAL(ATA.channels[count].iobase2, port2);
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500785 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
786 , count, port1, port2, bdf, prog_if);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400787 count++;
788
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500789 if (prog_if & 4) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500790 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
791 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400792 } else {
793 port1 = 0x170;
794 port2 = 0x370;
795 }
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500796 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
797 , count, port1, port2, bdf, prog_if);
Kevin O'Connor609da232008-12-28 23:18:57 -0500798 SET_GLOBAL(ATA.channels[count].iobase1, port1);
799 SET_GLOBAL(ATA.channels[count].iobase2, port2);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400800 count++;
801 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400802}
803
804void
805hard_drive_setup()
806{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400807 if (!CONFIG_ATA)
808 return;
809
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400810 dprintf(3, "init hard drives\n");
Kevin O'Connorc1437612008-05-18 01:43:07 -0400811 ata_init();
812 ata_detect();
813
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400814 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400815
Kevin O'Connord21c0892008-11-26 17:02:43 -0500816 enable_hwirq(14, entry_76);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400817}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500818
819
820/****************************************************************
821 * Drive mapping
822 ****************************************************************/
823
824// Fill in Fixed Disk Parameter Table (located in ebda).
825static void
826fill_fdpt(int driveid)
827{
828 if (driveid > 1)
829 return;
830
831 u16 nlc = GET_GLOBAL(ATA.devices[driveid].lchs.cylinders);
832 u16 nlh = GET_GLOBAL(ATA.devices[driveid].lchs.heads);
833 u16 nlspt = GET_GLOBAL(ATA.devices[driveid].lchs.spt);
834
835 u16 npc = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
836 u16 nph = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
837 u16 npspt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
838
839 struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[driveid];
840 fdpt->precompensation = 0xffff;
841 fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
842 fdpt->landing_zone = npc;
843 fdpt->cylinders = nlc;
844 fdpt->heads = nlh;
845 fdpt->sectors = nlspt;
846
847 if (nlc == npc && nlh == nph && nlspt == npspt)
848 // no logical CHS mapping used, just physical CHS
849 // use Standard Fixed Disk Parameter Table (FDPT)
850 return;
851
852 // complies with Phoenix style Translated Fixed Disk Parameter
853 // Table (FDPT)
854 fdpt->phys_cylinders = npc;
855 fdpt->phys_heads = nph;
856 fdpt->phys_sectors = npspt;
857 fdpt->a0h_signature = 0xa0;
858
859 // Checksum structure.
Kevin O'Connor523e5a92009-07-04 13:46:33 -0400860 fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
Kevin O'Connor9becbf12009-07-12 09:29:46 -0400861
862 if (driveid == 0)
863 SET_IVT(0x41, get_ebda_seg()
864 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
865 else
866 SET_IVT(0x46, get_ebda_seg()
867 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500868}
869
870// Map a drive (that was registered via add_bcv_hd)
871void
872map_drive(int driveid)
873{
874 // fill hdidmap
875 u8 hdcount = GET_BDA(hdcount);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500876 dprintf(3, "Mapping driveid %d to %d\n", driveid, hdcount);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500877 SET_GLOBAL(ATA.idmap[0][hdcount], driveid);
878 SET_BDA(hdcount, hdcount + 1);
879
880 // Fill "fdpt" structure.
881 fill_fdpt(hdcount);
882}