blob: 76e4f20d2f9cd6b35a078d9e2200a8ef21969c7d [file] [log] [blame]
Kevin O'Connorc09492e2008-03-01 13:38:07 -05001// Low level ATA disk access
2//
Kevin O'Connorc892b132009-08-11 21:59:37 -04003// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorc09492e2008-03-01 13:38:07 -05004// 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'Connor3f3e58d2011-06-20 22:20:43 -040014#include "pci.h" // foreachpci
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'Connor72eee3e2010-12-27 19:07:49 -050017#include "boot.h" // boot_add_hd
Kevin O'Connor609da232008-12-28 23:18:57 -050018#include "disk.h" // struct ata_s
Kevin O'Connorc892b132009-08-11 21:59:37 -040019#include "ata.h" // ATA_CB_STAT
Kevin O'Connor7d700252010-02-15 11:56:07 -050020#include "blockcmd.h" // CDB_CMD_READ_10
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050021
Kevin O'Connor425f2122009-04-18 12:23:00 -040022#define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050023
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050024
Kevin O'Connora6b9f712008-03-29 12:53:57 -040025/****************************************************************
26 * Helper functions
27 ****************************************************************/
28
29// Wait for the specified ide state
Kevin O'Connor580e3322009-02-06 22:36:53 -050030static inline int
31await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050032{
Kevin O'Connor4e6c9702008-12-13 10:45:50 -050033 u64 end = calc_future_tsc(timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050034 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -040035 u8 status = inb(base+ATA_CB_STAT);
Kevin O'Connor580e3322009-02-06 22:36:53 -050036 if ((status & mask) == flags)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040037 return status;
Kevin O'Connor144817b2010-05-23 10:46:49 -040038 if (check_tsc(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050039 warn_timeout();
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050040 return -1;
41 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -040042 yield();
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050043 }
Kevin O'Connor580e3322009-02-06 22:36:53 -050044}
45
46// Wait for the device to be not-busy.
47static int
48await_not_bsy(u16 base)
49{
50 return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
51}
52
53// Wait for the device to be ready.
54static int
55await_rdy(u16 base)
56{
57 return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050058}
59
Kevin O'Connora6b9f712008-03-29 12:53:57 -040060// Wait for ide state - pauses for one ata cycle first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050061static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050062pause_await_not_bsy(u16 iobase1, u16 iobase2)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040063{
64 // Wait one PIO transfer cycle.
65 inb(iobase2 + ATA_CB_ASTAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050066
Kevin O'Connor580e3322009-02-06 22:36:53 -050067 return await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -040068}
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050069
Kevin O'Connoref2822a2008-06-07 15:23:11 -040070// Wait for ide state - pause for 400ns first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050071static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050072ndelay_await_not_bsy(u16 iobase1)
Kevin O'Connoref2822a2008-06-07 15:23:11 -040073{
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050074 ndelay(400);
Kevin O'Connor580e3322009-02-06 22:36:53 -050075 return await_not_bsy(iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -040076}
77
Kevin O'Connora6b9f712008-03-29 12:53:57 -040078// Reset a drive
Kevin O'Connorb1144362009-08-11 20:43:38 -040079static void
Kevin O'Connor8f469b92010-02-28 01:28:11 -050080ata_reset(struct atadrive_s *adrive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050081{
Kevin O'Connor8f469b92010-02-28 01:28:11 -050082 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
83 u8 slave = GET_GLOBAL(adrive_g->slave);
84 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
85 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050086
Kevin O'Connor8f469b92010-02-28 01:28:11 -050087 dprintf(6, "ata_reset drive=%p\n", &adrive_g->drive);
Kevin O'Connor580e3322009-02-06 22:36:53 -050088 // Pulse SRST
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050089 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 -050090 udelay(5);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050091 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
Kevin O'Connor10ad7992009-10-24 11:06:08 -040092 msleep(2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050093
Kevin O'Connor580e3322009-02-06 22:36:53 -050094 // wait for device to become not busy.
95 int status = await_not_bsy(iobase1);
96 if (status < 0)
97 goto done;
98 if (slave) {
99 // Change device.
100 u64 end = calc_future_tsc(IDE_TIMEOUT);
101 for (;;) {
102 outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400103 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500104 if (status < 0)
105 goto done;
106 if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
107 break;
108 // Change drive request failed to take effect - retry.
Kevin O'Connor144817b2010-05-23 10:46:49 -0400109 if (check_tsc(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500110 warn_timeout();
Kevin O'Connor580e3322009-02-06 22:36:53 -0500111 goto done;
112 }
113 }
Kevin O'Connorf5624d22009-08-18 22:17:57 -0400114 } else {
115 // QEMU doesn't reset dh on reset, so set it explicitly.
116 outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
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.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500120 u8 type=GET_GLOBAL(adrive_g->drive.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'Connor14021f22009-12-26 23:21:38 -0500131// Check for drive RDY for 16bit interface command.
Kevin O'Connor42337662009-08-10 00:06:37 -0400132static int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500133isready(struct atadrive_s *adrive_g)
Kevin O'Connor42337662009-08-10 00:06:37 -0400134{
135 // Read the status from controller
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500136 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
137 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400138 u8 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400139 if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY)
140 return DISK_RET_SUCCESS;
141 return DISK_RET_ENOTREADY;
Kevin O'Connor42337662009-08-10 00:06:37 -0400142}
143
Kevin O'Connor14021f22009-12-26 23:21:38 -0500144// Default 16bit command demuxer for ATA and ATAPI devices.
Kevin O'Connor42337662009-08-10 00:06:37 -0400145static int
146process_ata_misc_op(struct disk_op_s *op)
147{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400148 if (!CONFIG_ATA)
149 return 0;
150
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500151 struct atadrive_s *adrive_g = container_of(
152 op->drive_g, struct atadrive_s, drive);
Kevin O'Connor42337662009-08-10 00:06:37 -0400153 switch (op->command) {
Kevin O'Connor42337662009-08-10 00:06:37 -0400154 case CMD_RESET:
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500155 ata_reset(adrive_g);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400156 return DISK_RET_SUCCESS;
Kevin O'Connor42337662009-08-10 00:06:37 -0400157 case CMD_ISREADY:
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500158 return isready(adrive_g);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400159 case CMD_FORMAT:
160 case CMD_VERIFY:
161 case CMD_SEEK:
162 return DISK_RET_SUCCESS;
163 default:
164 op->count = 0;
165 return DISK_RET_EPARAM;
Kevin O'Connor42337662009-08-10 00:06:37 -0400166 }
167}
168
Kevin O'Connoree55c762008-05-13 00:18:20 -0400169
170/****************************************************************
171 * ATA send command
172 ****************************************************************/
173
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400174struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400175 u8 feature;
176 u8 sector_count;
177 u8 lba_low;
178 u8 lba_mid;
179 u8 lba_high;
180 u8 device;
181 u8 command;
182
Kevin O'Connor14021f22009-12-26 23:21:38 -0500183 u8 feature2;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400184 u8 sector_count2;
185 u8 lba_low2;
186 u8 lba_mid2;
187 u8 lba_high2;
188};
189
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400190// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400191static int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500192send_cmd(struct atadrive_s *adrive_g, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500193{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500194 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
195 u8 slave = GET_GLOBAL(adrive_g->slave);
196 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400197
198 // Select device
Kevin O'Connor580e3322009-02-06 22:36:53 -0500199 int status = await_not_bsy(iobase1);
200 if (status < 0)
201 return status;
202 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
203 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
204 u8 olddh = inb(iobase1 + ATA_CB_DH);
205 outb(newdh, iobase1 + ATA_CB_DH);
206 if ((olddh ^ newdh) & (1<<4)) {
207 // Was a device change - wait for device to become not busy.
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400208 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500209 if (status < 0)
210 return status;
211 }
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400212
Kevin O'Connor14021f22009-12-26 23:21:38 -0500213 // Check for ATA_CMD_(READ|WRITE)_(SECTORS|DMA)_EXT commands.
214 if ((cmd->command & ~0x11) == ATA_CMD_READ_SECTORS_EXT) {
215 outb(cmd->feature2, iobase1 + ATA_CB_FR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400216 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
217 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
218 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
219 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500220 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400221 outb(cmd->feature, iobase1 + ATA_CB_FR);
222 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
223 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
224 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
225 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400226 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500227
Kevin O'Connor14021f22009-12-26 23:21:38 -0500228 return 0;
229}
230
231// Wait for data after calling 'send_cmd'.
232static int
233ata_wait_data(u16 iobase1)
234{
235 int status = ndelay_await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400236 if (status < 0)
237 return status;
238
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500239 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500240 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
241 , status, inb(iobase1 + ATA_CB_ERR));
Kevin O'Connora05223c2008-06-28 12:15:57 -0400242 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400243 }
244 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500245 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400246 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500247 }
248
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400249 return 0;
250}
251
Kevin O'Connor14021f22009-12-26 23:21:38 -0500252// Send an ata command that does not transfer any further data.
253int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500254ata_cmd_nondata(struct atadrive_s *adrive_g, struct ata_pio_command *cmd)
Kevin O'Connor14021f22009-12-26 23:21:38 -0500255{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500256 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
257 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
258 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500259
260 // Disable interrupts
261 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
262
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500263 int ret = send_cmd(adrive_g, cmd);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500264 if (ret)
265 goto fail;
266 ret = ndelay_await_not_bsy(iobase1);
267 if (ret < 0)
268 goto fail;
269
270 if (ret & ATA_CB_STAT_ERR) {
271 dprintf(6, "nondata cmd : read error (status=%02x err=%02x)\n"
272 , ret, inb(iobase1 + ATA_CB_ERR));
273 ret = -4;
274 goto fail;
275 }
276 if (ret & ATA_CB_STAT_DRQ) {
277 dprintf(6, "nondata cmd : DRQ set (status %02x)\n", ret);
278 ret = -5;
279 goto fail;
280 }
281
282fail:
283 // Enable interrupts
284 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
285
286 return ret;
287}
288
Kevin O'Connoree55c762008-05-13 00:18:20 -0400289
290/****************************************************************
Kevin O'Connor14021f22009-12-26 23:21:38 -0500291 * ATA PIO transfers
Kevin O'Connoree55c762008-05-13 00:18:20 -0400292 ****************************************************************/
293
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400294// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400295// 'op->drive_g'.
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400296static int
Kevin O'Connor14021f22009-12-26 23:21:38 -0500297ata_pio_transfer(struct disk_op_s *op, int iswrite, int blocksize)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400298{
Kevin O'Connor14021f22009-12-26 23:21:38 -0500299 dprintf(16, "ata_pio_transfer id=%p write=%d count=%d bs=%d buf=%p\n"
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400300 , op->drive_g, iswrite, op->count, blocksize, op->buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400301
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500302 struct atadrive_s *adrive_g = container_of(
303 op->drive_g, struct atadrive_s, drive);
304 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
305 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
306 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400307 int count = op->count;
308 void *buf_fl = op->buf_fl;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400309 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400310 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400311 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400312 // Write data to controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400313 dprintf(16, "Write sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500314 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400315 outsl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400316 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400317 outsw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500318 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400319 // Read data from controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400320 dprintf(16, "Read sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500321 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400322 insl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400323 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400324 insw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400325 }
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400326 buf_fl += blocksize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400327
Kevin O'Connor580e3322009-02-06 22:36:53 -0500328 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400329 if (status < 0) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400330 // Error
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400331 op->count -= count;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400332 return status;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400333 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400334
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400335 count--;
336 if (!count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400337 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500338 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
339 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500340 dprintf(6, "ata_pio_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500341 , status);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400342 op->count -= count;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400343 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500344 }
345 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400346
Kevin O'Connor580e3322009-02-06 22:36:53 -0500347 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
348 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400349 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400350 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500351 if (status != 0) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500352 dprintf(6, "ata_pio_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400353 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400354 }
355
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500356 return 0;
357}
358
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400359
360/****************************************************************
Kevin O'Connor14021f22009-12-26 23:21:38 -0500361 * ATA DMA transfers
362 ****************************************************************/
363
364#define BM_CMD 0
365#define BM_CMD_MEMWRITE 0x08
366#define BM_CMD_START 0x01
367#define BM_STATUS 2
368#define BM_STATUS_IRQ 0x04
369#define BM_STATUS_ERROR 0x02
370#define BM_STATUS_ACTIVE 0x01
371#define BM_TABLE 4
372
373struct sff_dma_prd {
374 u32 buf_fl;
375 u32 count;
376};
377
378// Check if DMA available and setup transfer if so.
379static int
380ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize)
381{
Kevin O'Connor4d079022010-01-17 12:58:47 -0500382 if (! CONFIG_ATA_DMA)
383 return -1;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500384 u32 dest = (u32)op->buf_fl;
385 if (dest & 1)
386 // Need minimum alignment of 1.
387 return -1;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500388 struct atadrive_s *adrive_g = container_of(
389 op->drive_g, struct atadrive_s, drive);
390 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
391 u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500392 if (! iomaster)
393 return -1;
394 u32 bytes = op->count * blocksize;
395 if (! bytes)
396 return -1;
397
398 // Build PRD dma structure.
399 struct sff_dma_prd *dma = MAKE_FLATPTR(
400 get_ebda_seg()
401 , (void*)offsetof(struct extended_bios_data_area_s, extra_stack));
402 struct sff_dma_prd *origdma = dma;
403 while (bytes) {
404 if (dma >= &origdma[16])
405 // Too many descriptors..
406 return -1;
407 u32 count = bytes;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500408 u32 max = 0x10000 - (dest & 0xffff);
409 if (count > max)
410 count = max;
411
412 SET_FLATPTR(dma->buf_fl, dest);
413 bytes -= count;
414 if (!bytes)
415 // Last descriptor.
416 count |= 1<<31;
417 dprintf(16, "dma@%p: %08x %08x\n", dma, dest, count);
418 dest += count;
419 SET_FLATPTR(dma->count, count);
420 dma++;
421 }
422
423 // Program bus-master controller.
424 outl((u32)origdma, iomaster + BM_TABLE);
425 u8 oldcmd = inb(iomaster + BM_CMD) & ~(BM_CMD_MEMWRITE|BM_CMD_START);
426 outb(oldcmd | (iswrite ? 0x00 : BM_CMD_MEMWRITE), iomaster + BM_CMD);
427 outb(BM_STATUS_ERROR|BM_STATUS_IRQ, iomaster + BM_STATUS);
428
429 return 0;
430}
431
432// Transfer data using DMA.
433static int
434ata_dma_transfer(struct disk_op_s *op)
435{
Kevin O'Connor4d079022010-01-17 12:58:47 -0500436 if (! CONFIG_ATA_DMA)
437 return -1;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500438 dprintf(16, "ata_dma_transfer id=%p buf=%p\n", op->drive_g, op->buf_fl);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500439
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500440 struct atadrive_s *adrive_g = container_of(
441 op->drive_g, struct atadrive_s, drive);
442 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
443 u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500444
445 // Start bus-master controller.
446 u8 oldcmd = inb(iomaster + BM_CMD);
447 outb(oldcmd | BM_CMD_START, iomaster + BM_CMD);
448
449 u64 end = calc_future_tsc(IDE_TIMEOUT);
450 u8 status;
451 for (;;) {
452 status = inb(iomaster + BM_STATUS);
453 if (status & BM_STATUS_IRQ)
454 break;
455 // Transfer in progress
Kevin O'Connor144817b2010-05-23 10:46:49 -0400456 if (check_tsc(end)) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500457 // Timeout.
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500458 warn_timeout();
Kevin O'Connor14021f22009-12-26 23:21:38 -0500459 break;
460 }
461 yield();
462 }
463 outb(oldcmd & ~BM_CMD_START, iomaster + BM_CMD);
464
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500465 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
466 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500467 int idestatus = pause_await_not_bsy(iobase1, iobase2);
468
469 if ((status & (BM_STATUS_IRQ|BM_STATUS_ACTIVE)) == BM_STATUS_IRQ
470 && idestatus >= 0x00
471 && (idestatus & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
472 | ATA_CB_STAT_ERR)) == 0x00)
473 // Success.
474 return 0;
475
476 dprintf(6, "IDE DMA error (dma=%x ide=%x/%x/%x)\n", status, idestatus
477 , inb(iobase2 + ATA_CB_ASTAT), inb(iobase1 + ATA_CB_ERR));
478 op->count = 0;
479 return -1;
480}
481
482
483/****************************************************************
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400484 * ATA hard drive functions
485 ****************************************************************/
486
Kevin O'Connor14021f22009-12-26 23:21:38 -0500487// Transfer data to harddrive using PIO protocol.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400488static int
Kevin O'Connor14021f22009-12-26 23:21:38 -0500489ata_pio_cmd_data(struct disk_op_s *op, int iswrite, struct ata_pio_command *cmd)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400490{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500491 struct atadrive_s *adrive_g = container_of(
492 op->drive_g, struct atadrive_s, drive);
493 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
494 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
495 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400496
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500497 // Disable interrupts
498 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
499
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500500 int ret = send_cmd(adrive_g, cmd);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400501 if (ret)
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500502 goto fail;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500503 ret = ata_wait_data(iobase1);
504 if (ret)
505 goto fail;
506 ret = ata_pio_transfer(op, iswrite, DISK_SECTOR_SIZE);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500507
508fail:
509 // Enable interrupts
510 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
511 return ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400512}
513
Kevin O'Connor14021f22009-12-26 23:21:38 -0500514// Transfer data to harddrive using DMA protocol.
515static int
516ata_dma_cmd_data(struct disk_op_s *op, struct ata_pio_command *cmd)
517{
Kevin O'Connor4d079022010-01-17 12:58:47 -0500518 if (! CONFIG_ATA_DMA)
519 return -1;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500520 struct atadrive_s *adrive_g = container_of(
521 op->drive_g, struct atadrive_s, drive);
522 int ret = send_cmd(adrive_g, cmd);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500523 if (ret)
524 return ret;
525 return ata_dma_transfer(op);
526}
527
528// Read/write count blocks from a harddrive.
529static int
530ata_readwrite(struct disk_op_s *op, int iswrite)
531{
532 u64 lba = op->lba;
533
534 int usepio = ata_try_dma(op, iswrite, DISK_SECTOR_SIZE);
535
536 struct ata_pio_command cmd;
537 memset(&cmd, 0, sizeof(cmd));
538
539 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
540 cmd.sector_count2 = op->count >> 8;
541 cmd.lba_low2 = lba >> 24;
542 cmd.lba_mid2 = lba >> 32;
543 cmd.lba_high2 = lba >> 40;
544 lba &= 0xffffff;
545
546 if (usepio)
547 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS_EXT
548 : ATA_CMD_READ_SECTORS_EXT);
549 else
550 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
551 : ATA_CMD_READ_DMA_EXT);
552 } else {
553 if (usepio)
554 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS
555 : ATA_CMD_READ_SECTORS);
556 else
557 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA
558 : ATA_CMD_READ_DMA);
559 }
560
561 cmd.sector_count = op->count;
562 cmd.lba_low = lba;
563 cmd.lba_mid = lba >> 8;
564 cmd.lba_high = lba >> 16;
565 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
566
567 int ret;
568 if (usepio)
569 ret = ata_pio_cmd_data(op, iswrite, &cmd);
570 else
571 ret = ata_dma_cmd_data(op, &cmd);
572 if (ret)
573 return DISK_RET_EBADTRACK;
574 return DISK_RET_SUCCESS;
575}
576
577// 16bit command demuxer for ATA harddrives.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400578int
579process_ata_op(struct disk_op_s *op)
580{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400581 if (!CONFIG_ATA)
582 return 0;
583
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400584 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400585 case CMD_READ:
Kevin O'Connor14021f22009-12-26 23:21:38 -0500586 return ata_readwrite(op, 0);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400587 case CMD_WRITE:
Kevin O'Connor14021f22009-12-26 23:21:38 -0500588 return ata_readwrite(op, 1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400589 default:
590 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400591 }
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400592}
593
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400594
595/****************************************************************
596 * ATAPI functions
597 ****************************************************************/
598
Kevin O'Connor7d700252010-02-15 11:56:07 -0500599#define CDROM_CDB_SIZE 12
600
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400601// Low-level atapi command transmit function.
Kevin O'Connor7d700252010-02-15 11:56:07 -0500602int
603atapi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500604{
Kevin O'Connor80c2b6e2010-12-05 12:52:02 -0500605 if (! CONFIG_ATA)
606 return 0;
607
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500608 struct atadrive_s *adrive_g = container_of(
609 op->drive_g, struct atadrive_s, drive);
610 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
611 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
612 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500613
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400614 struct ata_pio_command cmd;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500615 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400616 cmd.lba_mid = blocksize;
617 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400618 cmd.command = ATA_CMD_PACKET;
619
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500620 // Disable interrupts
621 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
622
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500623 int ret = send_cmd(adrive_g, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400624 if (ret)
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500625 goto fail;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500626 ret = ata_wait_data(iobase1);
627 if (ret)
628 goto fail;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500629
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400630 // Send command to device
Kevin O'Connor7d700252010-02-15 11:56:07 -0500631 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cdbcmd), CDROM_CDB_SIZE / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500632
Kevin O'Connor580e3322009-02-06 22:36:53 -0500633 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500634 if (status < 0) {
635 ret = status;
636 goto fail;
637 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400638
Kevin O'Connor580e3322009-02-06 22:36:53 -0500639 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connorb30c4002009-05-05 21:47:20 -0400640 u8 err = inb(iobase1 + ATA_CB_ERR);
641 // skip "Not Ready"
642 if (err != 0x20)
643 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
644 , status, err);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500645 ret = -2;
646 goto fail;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500647 }
648 if (!(status & ATA_CB_STAT_DRQ)) {
649 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500650 ret = -3;
651 goto fail;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500652 }
653
Kevin O'Connor14021f22009-12-26 23:21:38 -0500654 ret = ata_pio_transfer(op, 0, blocksize);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500655
656fail:
657 // Enable interrupts
658 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor76977b22010-02-17 01:01:32 -0500659 if (ret)
660 return DISK_RET_EBADTRACK;
661 return DISK_RET_SUCCESS;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400662}
663
Kevin O'Connor14021f22009-12-26 23:21:38 -0500664// 16bit command demuxer for ATAPI cdroms.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400665int
666process_atapi_op(struct disk_op_s *op)
667{
Kevin O'Connor456479e2010-05-23 10:20:04 -0400668 if (!CONFIG_ATA)
669 return 0;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400670 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400671 case CMD_READ:
Kevin O'Connor76977b22010-02-17 01:01:32 -0500672 return cdb_read(op);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400673 case CMD_FORMAT:
674 case CMD_WRITE:
675 return DISK_RET_EWRITEPROTECT;
Kevin O'Connor42337662009-08-10 00:06:37 -0400676 default:
677 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400678 }
679}
680
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400681
682/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500683 * ATA detect and init
684 ****************************************************************/
685
Kevin O'Connor14021f22009-12-26 23:21:38 -0500686// Send an identify device or identify device packet command.
687static int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500688send_ata_identity(struct atadrive_s *adrive_g, u16 *buffer, int command)
Kevin O'Connor14021f22009-12-26 23:21:38 -0500689{
690 memset(buffer, 0, DISK_SECTOR_SIZE);
691
692 struct disk_op_s dop;
693 memset(&dop, 0, sizeof(dop));
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500694 dop.drive_g = &adrive_g->drive;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500695 dop.count = 1;
696 dop.lba = 1;
697 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
698
699 struct ata_pio_command cmd;
700 memset(&cmd, 0, sizeof(cmd));
701 cmd.command = command;
702
703 return ata_pio_cmd_data(&dop, 0, &cmd);
704}
705
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400706// Extract the ATA/ATAPI version info.
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100707int
708ata_extract_version(u16 *buffer)
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400709{
710 // Extract ATA/ATAPI version.
711 u16 ataversion = buffer[80];
712 u8 version;
713 for (version=15; version>0; version--)
714 if (ataversion & (1<<version))
715 break;
716 return version;
717}
718
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500719#define MAXMODEL 40
Kevin O'Connor0a924122009-02-08 19:43:47 -0500720
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500721// Extract the ATA/ATAPI model info.
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100722char *
723ata_extract_model(char *model, u32 size, u16 *buffer)
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500724{
Kevin O'Connor0a924122009-02-08 19:43:47 -0500725 // Read model name
726 int i;
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100727 for (i=0; i<size/2; i++)
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500728 *(u16*)&model[i*2] = ntohs(buffer[27+i]);
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100729 model[size] = 0x00;
Kevin O'Connor9e881a32011-01-08 12:06:54 -0500730 nullTrailingSpace(model);
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500731 return model;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500732}
733
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500734// Common init code between ata and atapi
735static struct atadrive_s *
736init_atadrive(struct atadrive_s *dummy, u16 *buffer)
737{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500738 struct atadrive_s *adrive_g = malloc_fseg(sizeof(*adrive_g));
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500739 if (!adrive_g) {
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500740 warn_noalloc();
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500741 return NULL;
742 }
743 memset(adrive_g, 0, sizeof(*adrive_g));
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500744 adrive_g->chan_gf = dummy->chan_gf;
745 adrive_g->slave = dummy->slave;
746 adrive_g->drive.cntl_id = adrive_g->chan_gf->chanid * 2 + dummy->slave;
747 adrive_g->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
748 return adrive_g;
749}
750
Kevin O'Connor14021f22009-12-26 23:21:38 -0500751// Detect if the given drive is an atapi - initialize it if so.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500752static struct atadrive_s *
753init_drive_atapi(struct atadrive_s *dummy, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500754{
755 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connor14021f22009-12-26 23:21:38 -0500756 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_PACKET_DEVICE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500757 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400758 return NULL;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500759
760 // Success - setup as ATAPI.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500761 struct atadrive_s *adrive_g = init_atadrive(dummy, buffer);
762 if (!adrive_g)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400763 return NULL;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500764 adrive_g->drive.type = DTYPE_ATAPI;
765 adrive_g->drive.blksize = CDROM_SECTOR_SIZE;
766 adrive_g->drive.sectors = (u64)-1;
Kevin O'Connor42337662009-08-10 00:06:37 -0400767 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500768 char model[MAXMODEL+1];
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500769 char *desc = znprintf(MAXDESCSIZE
770 , "DVD/CD [ata%d-%d: %s ATAPI-%d %s]"
771 , adrive_g->chan_gf->chanid, adrive_g->slave
772 , ata_extract_model(model, MAXMODEL, buffer)
773 , ata_extract_version(buffer)
774 , (iscd ? "DVD/CD" : "Device"));
775 dprintf(1, "%s\n", desc);
Kevin O'Connor42337662009-08-10 00:06:37 -0400776
777 // fill cdidmap
Kevin O'Connor031ef552010-12-27 19:26:57 -0500778 if (iscd) {
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400779 int prio = bootprio_find_ata_device(adrive_g->chan_gf->pci_tmp,
Kevin O'Connor031ef552010-12-27 19:26:57 -0500780 adrive_g->chan_gf->chanid,
781 adrive_g->slave);
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500782 boot_add_cd(&adrive_g->drive, desc, prio);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500783 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500784
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500785 return adrive_g;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500786}
787
Kevin O'Connor14021f22009-12-26 23:21:38 -0500788// Detect if the given drive is a regular ata drive - initialize it if so.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500789static struct atadrive_s *
790init_drive_ata(struct atadrive_s *dummy, u16 *buffer)
Kevin O'Connorc1437612008-05-18 01:43:07 -0400791{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500792 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connor14021f22009-12-26 23:21:38 -0500793 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_DEVICE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400794 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400795 return NULL;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500796
797 // Success - setup as ATA.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500798 struct atadrive_s *adrive_g = init_atadrive(dummy, buffer);
799 if (!adrive_g)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400800 return NULL;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500801 adrive_g->drive.type = DTYPE_ATA;
802 adrive_g->drive.blksize = DISK_SECTOR_SIZE;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400803
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500804 adrive_g->drive.pchs.cylinders = buffer[1];
805 adrive_g->drive.pchs.heads = buffer[3];
806 adrive_g->drive.pchs.spt = buffer[6];
Kevin O'Connorc1437612008-05-18 01:43:07 -0400807
808 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500809 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
810 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400811 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500812 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500813 adrive_g->drive.sectors = sectors;
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500814 u64 adjsize = sectors >> 11;
815 char adjprefix = 'M';
816 if (adjsize >= (1 << 16)) {
817 adjsize >>= 10;
818 adjprefix = 'G';
819 }
820 char model[MAXMODEL+1];
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500821 char *desc = znprintf(MAXDESCSIZE
822 , "ata%d-%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
823 , adrive_g->chan_gf->chanid, adrive_g->slave
824 , ata_extract_model(model, MAXMODEL, buffer)
825 , ata_extract_version(buffer)
826 , (u32)adjsize, adjprefix);
827 dprintf(1, "%s\n", desc);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400828
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400829 int prio = bootprio_find_ata_device(adrive_g->chan_gf->pci_tmp,
Kevin O'Connor031ef552010-12-27 19:26:57 -0500830 adrive_g->chan_gf->chanid,
831 adrive_g->slave);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500832 // Register with bcv system.
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500833 boot_add_hd(&adrive_g->drive, desc, prio);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400834
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500835 return adrive_g;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400836}
837
Kevin O'Connora5826b52009-10-24 17:57:29 -0400838static u64 SpinupEnd;
839
Kevin O'Connor14021f22009-12-26 23:21:38 -0500840// Wait for non-busy status and check for "floating bus" condition.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400841static int
Kevin O'Connora5826b52009-10-24 17:57:29 -0400842powerup_await_non_bsy(u16 base)
Kevin O'Connor425f2122009-04-18 12:23:00 -0400843{
844 u8 orstatus = 0;
845 u8 status;
846 for (;;) {
847 status = inb(base+ATA_CB_STAT);
848 if (!(status & ATA_CB_STAT_BSY))
849 break;
850 orstatus |= status;
851 if (orstatus == 0xff) {
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400852 dprintf(4, "powerup IDE floating\n");
Kevin O'Connor425f2122009-04-18 12:23:00 -0400853 return orstatus;
854 }
Kevin O'Connor144817b2010-05-23 10:46:49 -0400855 if (check_tsc(SpinupEnd)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500856 warn_timeout();
Kevin O'Connor425f2122009-04-18 12:23:00 -0400857 return -1;
858 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400859 yield();
Kevin O'Connor425f2122009-04-18 12:23:00 -0400860 }
861 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
862 return status;
863}
864
Kevin O'Connor14021f22009-12-26 23:21:38 -0500865// Detect any drives attached to a given controller.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400866static void
Kevin O'Connora5826b52009-10-24 17:57:29 -0400867ata_detect(void *data)
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500868{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500869 struct ata_channel_s *chan_gf = data;
870 struct atadrive_s dummy;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400871 memset(&dummy, 0, sizeof(dummy));
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500872 dummy.chan_gf = chan_gf;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500873 // Device detection
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500874 int didreset = 0;
875 u8 slave;
876 for (slave=0; slave<=1; slave++) {
Kevin O'Connor425f2122009-04-18 12:23:00 -0400877 // Wait for not-bsy.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500878 u16 iobase1 = chan_gf->iobase1;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400879 int status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400880 if (status < 0)
881 continue;
882 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
883 outb(newdh, iobase1+ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400884 ndelay(400);
Kevin O'Connora5826b52009-10-24 17:57:29 -0400885 status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400886 if (status < 0)
887 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500888
Kevin O'Connor580e3322009-02-06 22:36:53 -0500889 // Check if ioport registers look valid.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400890 outb(newdh, iobase1+ATA_CB_DH);
891 u8 dh = inb(iobase1+ATA_CB_DH);
892 outb(0x55, iobase1+ATA_CB_SC);
893 outb(0xaa, iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400894 u8 sc = inb(iobase1+ATA_CB_SC);
895 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500896 dprintf(6, "ata_detect ata%d-%d: sc=%x sn=%x dh=%x\n"
897 , chan_gf->chanid, slave, sc, sn, dh);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400898 if (sc != 0x55 || sn != 0xaa || dh != newdh)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400899 continue;
900
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400901 // Prepare new drive.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500902 dummy.slave = slave;
Kevin O'Connorb1144362009-08-11 20:43:38 -0400903
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400904 // reset the channel
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500905 if (!didreset) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400906 ata_reset(&dummy);
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500907 didreset = 1;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500908 }
909
Kevin O'Connor580e3322009-02-06 22:36:53 -0500910 // check for ATAPI
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400911 u16 buffer[256];
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500912 struct atadrive_s *adrive_g = init_drive_atapi(&dummy, buffer);
913 if (!adrive_g) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400914 // Didn't find an ATAPI drive - look for ATA drive.
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400915 u8 st = inb(iobase1+ATA_CB_STAT);
916 if (!st)
917 // Status not set - can't be a valid drive.
918 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500919
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400920 // Wait for RDY.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400921 int ret = await_rdy(iobase1);
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400922 if (ret < 0)
923 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500924
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400925 // check for ATA.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500926 adrive_g = init_drive_ata(&dummy, buffer);
927 if (!adrive_g)
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400928 // No ATA drive found
929 continue;
930 }
Kevin O'Connor580e3322009-02-06 22:36:53 -0500931
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400932 u16 resetresult = buffer[93];
933 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
934 if (!slave && (resetresult & 0xdf61) == 0x4041)
935 // resetresult looks valid and device 0 is responding to
936 // device 1 requests - device 1 must not be present - skip
937 // detection.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500938 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500939 }
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500940}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400941
Kevin O'Connor14021f22009-12-26 23:21:38 -0500942// Initialize an ata controller and detect its drives.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400943static void
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400944init_controller(struct pci_device *pci, int irq
945 , u32 port1, u32 port2, u32 master)
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500946{
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400947 static int chanid = 0;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500948 struct ata_channel_s *chan_gf = malloc_fseg(sizeof(*chan_gf));
949 if (!chan_gf) {
950 warn_noalloc();
951 return;
952 }
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400953 chan_gf->chanid = chanid++;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500954 chan_gf->irq = irq;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400955 chan_gf->pci_bdf = pci ? pci->bdf : -1;
956 chan_gf->pci_tmp = pci;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500957 chan_gf->iobase1 = port1;
958 chan_gf->iobase2 = port2;
959 chan_gf->iomaster = master;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500960 dprintf(1, "ATA controller %d at %x/%x/%x (irq %d dev %x)\n"
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400961 , chanid, port1, port2, master, irq, chan_gf->pci_bdf);
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500962 run_thread(ata_detect, chan_gf);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500963}
964
Kevin O'Connor525219b2009-12-05 13:36:18 -0500965#define IRQ_ATA1 14
966#define IRQ_ATA2 15
967
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400968// Handle controllers on an ATA PCI device.
969static void
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400970init_pciata(struct pci_device *pci, u8 prog_if)
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400971{
Kevin O'Connor76b5e712011-06-21 22:52:51 -0400972 pci->have_driver = 1;
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400973 u16 bdf = pci->bdf;
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400974 u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400975 int master = 0;
976 if (CONFIG_ATA_DMA && prog_if & 0x80) {
977 // Check for bus-mastering.
978 u32 bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_4);
979 if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
980 master = bar & PCI_BASE_ADDRESS_IO_MASK;
981 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
982 }
983 }
984
985 u32 port1, port2, irq;
986 if (prog_if & 1) {
987 port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_0)
988 & PCI_BASE_ADDRESS_IO_MASK);
989 port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_1)
990 & PCI_BASE_ADDRESS_IO_MASK);
991 irq = pciirq;
992 } else {
993 port1 = PORT_ATA1_CMD_BASE;
994 port2 = PORT_ATA1_CTRL_BASE;
995 irq = IRQ_ATA1;
996 }
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400997 init_controller(pci, irq, port1, port2, master);
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400998
999 if (prog_if & 4) {
1000 port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_2)
1001 & PCI_BASE_ADDRESS_IO_MASK);
1002 port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_3)
1003 & PCI_BASE_ADDRESS_IO_MASK);
1004 irq = pciirq;
1005 } else {
1006 port1 = PORT_ATA2_CMD_BASE;
1007 port2 = PORT_ATA2_CTRL_BASE;
1008 irq = IRQ_ATA2;
1009 }
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -04001010 init_controller(pci, irq, port1, port2, master ? master + 8 : 0);
Kevin O'Connor927d16e2011-06-19 09:43:20 -04001011}
1012
Kevin O'Connorb9457ec2011-06-19 10:03:08 -04001013static void
Kevin O'Connor278b19f2011-06-21 22:41:15 -04001014found_genericata(struct pci_device *pci, void *arg)
Kevin O'Connorb9457ec2011-06-19 10:03:08 -04001015{
Kevin O'Connor278b19f2011-06-21 22:41:15 -04001016 init_pciata(pci, pci->prog_if);
Kevin O'Connorb9457ec2011-06-19 10:03:08 -04001017}
1018
1019static void
Kevin O'Connor278b19f2011-06-21 22:41:15 -04001020found_compatibleahci(struct pci_device *pci, void *arg)
Kevin O'Connorb9457ec2011-06-19 10:03:08 -04001021{
1022 if (CONFIG_AHCI)
1023 // Already handled directly via native ahci interface.
1024 return;
Kevin O'Connor278b19f2011-06-21 22:41:15 -04001025 init_pciata(pci, 0x8f);
Kevin O'Connorb9457ec2011-06-19 10:03:08 -04001026}
1027
Kevin O'Connor927d16e2011-06-19 09:43:20 -04001028static const struct pci_device_id pci_ata_tbl[] = {
Kevin O'Connorb9457ec2011-06-19 10:03:08 -04001029 PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE
1030 , found_genericata),
1031 PCI_DEVICE(PCI_VENDOR_ID_ATI, 0x4391, found_compatibleahci),
Kevin O'Connor927d16e2011-06-19 09:43:20 -04001032 PCI_DEVICE_END,
1033};
1034
Kevin O'Connor14021f22009-12-26 23:21:38 -05001035// Locate and init ata controllers.
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001036static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001037ata_init(void)
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001038{
Kevin O'Connor3f3e58d2011-06-20 22:20:43 -04001039 if (!CONFIG_COREBOOT && !PCIDevices) {
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001040 // No PCI devices found - probably a QEMU "-M isapc" machine.
1041 // Try using ISA ports for ATA controllers.
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -04001042 init_controller(NULL, IRQ_ATA1
Kevin O'Connor14021f22009-12-26 23:21:38 -05001043 , PORT_ATA1_CMD_BASE, PORT_ATA1_CTRL_BASE, 0);
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -04001044 init_controller(NULL, IRQ_ATA2
Kevin O'Connor14021f22009-12-26 23:21:38 -05001045 , PORT_ATA2_CMD_BASE, PORT_ATA2_CTRL_BASE, 0);
Kevin O'Connor3f3e58d2011-06-20 22:20:43 -04001046 return;
1047 }
1048
1049 // Scan PCI bus for ATA adapters
1050 struct pci_device *pci;
1051 foreachpci(pci) {
Kevin O'Connor278b19f2011-06-21 22:41:15 -04001052 pci_init_device(pci_ata_tbl, pci, NULL);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001053 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001054}
1055
1056void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001057ata_setup(void)
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001058{
Kevin O'Connor59c75742010-02-13 18:49:24 -05001059 ASSERT32FLAT();
Kevin O'Connorc1437612008-05-18 01:43:07 -04001060 if (!CONFIG_ATA)
1061 return;
1062
Kevin O'Connor35192dd2008-06-08 19:18:33 -04001063 dprintf(3, "init hard drives\n");
Kevin O'Connora5826b52009-10-24 17:57:29 -04001064
1065 SpinupEnd = calc_future_tsc(IDE_TIMEOUT);
Kevin O'Connorc1437612008-05-18 01:43:07 -04001066 ata_init();
Kevin O'Connorc1437612008-05-18 01:43:07 -04001067
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001068 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -04001069
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -04001070 enable_hwirq(14, FUNC16(entry_76));
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001071}