blob: 7ff5f86c5df4061217e316fa03f82781ec406812 [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'Connor46b82622012-05-13 12:10:30 -040013#include "biosvar.h" // GET_GLOBAL
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'Connoree55c762008-05-13 00:18:20 -0400144
145/****************************************************************
146 * ATA send command
147 ****************************************************************/
148
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400149struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400150 u8 feature;
151 u8 sector_count;
152 u8 lba_low;
153 u8 lba_mid;
154 u8 lba_high;
155 u8 device;
156 u8 command;
157
Kevin O'Connor14021f22009-12-26 23:21:38 -0500158 u8 feature2;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400159 u8 sector_count2;
160 u8 lba_low2;
161 u8 lba_mid2;
162 u8 lba_high2;
163};
164
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400165// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400166static int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500167send_cmd(struct atadrive_s *adrive_g, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500168{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500169 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
170 u8 slave = GET_GLOBAL(adrive_g->slave);
171 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400172
173 // Select device
Kevin O'Connor580e3322009-02-06 22:36:53 -0500174 int status = await_not_bsy(iobase1);
175 if (status < 0)
176 return status;
177 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
178 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
179 u8 olddh = inb(iobase1 + ATA_CB_DH);
180 outb(newdh, iobase1 + ATA_CB_DH);
181 if ((olddh ^ newdh) & (1<<4)) {
182 // Was a device change - wait for device to become not busy.
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400183 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500184 if (status < 0)
185 return status;
186 }
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400187
Kevin O'Connor14021f22009-12-26 23:21:38 -0500188 // Check for ATA_CMD_(READ|WRITE)_(SECTORS|DMA)_EXT commands.
189 if ((cmd->command & ~0x11) == ATA_CMD_READ_SECTORS_EXT) {
190 outb(cmd->feature2, iobase1 + ATA_CB_FR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400191 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
192 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
193 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
194 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500195 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400196 outb(cmd->feature, iobase1 + ATA_CB_FR);
197 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
198 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
199 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
200 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400201 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500202
Kevin O'Connor14021f22009-12-26 23:21:38 -0500203 return 0;
204}
205
206// Wait for data after calling 'send_cmd'.
207static int
208ata_wait_data(u16 iobase1)
209{
210 int status = ndelay_await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400211 if (status < 0)
212 return status;
213
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500214 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500215 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
216 , status, inb(iobase1 + ATA_CB_ERR));
Kevin O'Connora05223c2008-06-28 12:15:57 -0400217 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400218 }
219 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500220 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400221 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500222 }
223
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400224 return 0;
225}
226
Kevin O'Connor14021f22009-12-26 23:21:38 -0500227// Send an ata command that does not transfer any further data.
228int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500229ata_cmd_nondata(struct atadrive_s *adrive_g, struct ata_pio_command *cmd)
Kevin O'Connor14021f22009-12-26 23:21:38 -0500230{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500231 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
232 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
233 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500234
235 // Disable interrupts
236 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
237
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500238 int ret = send_cmd(adrive_g, cmd);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500239 if (ret)
240 goto fail;
241 ret = ndelay_await_not_bsy(iobase1);
242 if (ret < 0)
243 goto fail;
244
245 if (ret & ATA_CB_STAT_ERR) {
246 dprintf(6, "nondata cmd : read error (status=%02x err=%02x)\n"
247 , ret, inb(iobase1 + ATA_CB_ERR));
248 ret = -4;
249 goto fail;
250 }
251 if (ret & ATA_CB_STAT_DRQ) {
252 dprintf(6, "nondata cmd : DRQ set (status %02x)\n", ret);
253 ret = -5;
254 goto fail;
255 }
256
257fail:
258 // Enable interrupts
259 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
260
261 return ret;
262}
263
Kevin O'Connoree55c762008-05-13 00:18:20 -0400264
265/****************************************************************
Kevin O'Connor14021f22009-12-26 23:21:38 -0500266 * ATA PIO transfers
Kevin O'Connoree55c762008-05-13 00:18:20 -0400267 ****************************************************************/
268
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400269// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400270// 'op->drive_g'.
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400271static int
Kevin O'Connor14021f22009-12-26 23:21:38 -0500272ata_pio_transfer(struct disk_op_s *op, int iswrite, int blocksize)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400273{
Kevin O'Connor14021f22009-12-26 23:21:38 -0500274 dprintf(16, "ata_pio_transfer id=%p write=%d count=%d bs=%d buf=%p\n"
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400275 , op->drive_g, iswrite, op->count, blocksize, op->buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400276
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500277 struct atadrive_s *adrive_g = container_of(
278 op->drive_g, struct atadrive_s, drive);
279 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
280 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
281 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400282 int count = op->count;
283 void *buf_fl = op->buf_fl;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400284 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400285 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400286 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400287 // Write data to controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400288 dprintf(16, "Write sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500289 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400290 outsl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400291 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400292 outsw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500293 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400294 // Read data from controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400295 dprintf(16, "Read sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500296 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400297 insl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400298 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400299 insw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400300 }
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400301 buf_fl += blocksize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400302
Kevin O'Connor580e3322009-02-06 22:36:53 -0500303 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400304 if (status < 0) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400305 // Error
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400306 op->count -= count;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400307 return status;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400308 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400309
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400310 count--;
311 if (!count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400312 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500313 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
314 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500315 dprintf(6, "ata_pio_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500316 , status);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400317 op->count -= count;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400318 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500319 }
320 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400321
Kevin O'Connor580e3322009-02-06 22:36:53 -0500322 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
323 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400324 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400325 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500326 if (status != 0) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500327 dprintf(6, "ata_pio_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400328 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400329 }
330
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500331 return 0;
332}
333
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400334
335/****************************************************************
Kevin O'Connor14021f22009-12-26 23:21:38 -0500336 * ATA DMA transfers
337 ****************************************************************/
338
339#define BM_CMD 0
340#define BM_CMD_MEMWRITE 0x08
341#define BM_CMD_START 0x01
342#define BM_STATUS 2
343#define BM_STATUS_IRQ 0x04
344#define BM_STATUS_ERROR 0x02
345#define BM_STATUS_ACTIVE 0x01
346#define BM_TABLE 4
347
348struct sff_dma_prd {
349 u32 buf_fl;
350 u32 count;
351};
352
353// Check if DMA available and setup transfer if so.
354static int
355ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize)
356{
Kevin O'Connor46b82622012-05-13 12:10:30 -0400357 ASSERT16();
Kevin O'Connor4d079022010-01-17 12:58:47 -0500358 if (! CONFIG_ATA_DMA)
359 return -1;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500360 u32 dest = (u32)op->buf_fl;
361 if (dest & 1)
362 // Need minimum alignment of 1.
363 return -1;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500364 struct atadrive_s *adrive_g = container_of(
365 op->drive_g, struct atadrive_s, drive);
366 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
367 u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500368 if (! iomaster)
369 return -1;
370 u32 bytes = op->count * blocksize;
371 if (! bytes)
372 return -1;
373
374 // Build PRD dma structure.
Kevin O'Connor46b82622012-05-13 12:10:30 -0400375 struct sff_dma_prd *dma = MAKE_FLATPTR(SEG_LOW, ExtraStack);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500376 struct sff_dma_prd *origdma = dma;
377 while (bytes) {
378 if (dma >= &origdma[16])
379 // Too many descriptors..
380 return -1;
381 u32 count = bytes;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500382 u32 max = 0x10000 - (dest & 0xffff);
383 if (count > max)
384 count = max;
385
Kevin O'Connor1e3bd4f2012-05-24 23:56:19 -0400386 SET_LOWFLAT(dma->buf_fl, dest);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500387 bytes -= count;
388 if (!bytes)
389 // Last descriptor.
390 count |= 1<<31;
391 dprintf(16, "dma@%p: %08x %08x\n", dma, dest, count);
392 dest += count;
Kevin O'Connor1e3bd4f2012-05-24 23:56:19 -0400393 SET_LOWFLAT(dma->count, count);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500394 dma++;
395 }
396
397 // Program bus-master controller.
398 outl((u32)origdma, iomaster + BM_TABLE);
399 u8 oldcmd = inb(iomaster + BM_CMD) & ~(BM_CMD_MEMWRITE|BM_CMD_START);
400 outb(oldcmd | (iswrite ? 0x00 : BM_CMD_MEMWRITE), iomaster + BM_CMD);
401 outb(BM_STATUS_ERROR|BM_STATUS_IRQ, iomaster + BM_STATUS);
402
403 return 0;
404}
405
406// Transfer data using DMA.
407static int
408ata_dma_transfer(struct disk_op_s *op)
409{
Kevin O'Connor4d079022010-01-17 12:58:47 -0500410 if (! CONFIG_ATA_DMA)
411 return -1;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500412 dprintf(16, "ata_dma_transfer id=%p buf=%p\n", op->drive_g, op->buf_fl);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500413
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500414 struct atadrive_s *adrive_g = container_of(
415 op->drive_g, struct atadrive_s, drive);
416 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
417 u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500418
419 // Start bus-master controller.
420 u8 oldcmd = inb(iomaster + BM_CMD);
421 outb(oldcmd | BM_CMD_START, iomaster + BM_CMD);
422
423 u64 end = calc_future_tsc(IDE_TIMEOUT);
424 u8 status;
425 for (;;) {
426 status = inb(iomaster + BM_STATUS);
427 if (status & BM_STATUS_IRQ)
428 break;
429 // Transfer in progress
Kevin O'Connor144817b2010-05-23 10:46:49 -0400430 if (check_tsc(end)) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500431 // Timeout.
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500432 warn_timeout();
Kevin O'Connor14021f22009-12-26 23:21:38 -0500433 break;
434 }
435 yield();
436 }
437 outb(oldcmd & ~BM_CMD_START, iomaster + BM_CMD);
438
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500439 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
440 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500441 int idestatus = pause_await_not_bsy(iobase1, iobase2);
442
443 if ((status & (BM_STATUS_IRQ|BM_STATUS_ACTIVE)) == BM_STATUS_IRQ
444 && idestatus >= 0x00
445 && (idestatus & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
446 | ATA_CB_STAT_ERR)) == 0x00)
447 // Success.
448 return 0;
449
450 dprintf(6, "IDE DMA error (dma=%x ide=%x/%x/%x)\n", status, idestatus
451 , inb(iobase2 + ATA_CB_ASTAT), inb(iobase1 + ATA_CB_ERR));
452 op->count = 0;
453 return -1;
454}
455
456
457/****************************************************************
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400458 * ATA hard drive functions
459 ****************************************************************/
460
Kevin O'Connor14021f22009-12-26 23:21:38 -0500461// Transfer data to harddrive using PIO protocol.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400462static int
Kevin O'Connor14021f22009-12-26 23:21:38 -0500463ata_pio_cmd_data(struct disk_op_s *op, int iswrite, struct ata_pio_command *cmd)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400464{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500465 struct atadrive_s *adrive_g = container_of(
466 op->drive_g, struct atadrive_s, drive);
467 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
468 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
469 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400470
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500471 // Disable interrupts
472 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
473
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500474 int ret = send_cmd(adrive_g, cmd);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400475 if (ret)
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500476 goto fail;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500477 ret = ata_wait_data(iobase1);
478 if (ret)
479 goto fail;
480 ret = ata_pio_transfer(op, iswrite, DISK_SECTOR_SIZE);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500481
482fail:
483 // Enable interrupts
484 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
485 return ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400486}
487
Kevin O'Connor14021f22009-12-26 23:21:38 -0500488// Transfer data to harddrive using DMA protocol.
489static int
490ata_dma_cmd_data(struct disk_op_s *op, struct ata_pio_command *cmd)
491{
Kevin O'Connor4d079022010-01-17 12:58:47 -0500492 if (! CONFIG_ATA_DMA)
493 return -1;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500494 struct atadrive_s *adrive_g = container_of(
495 op->drive_g, struct atadrive_s, drive);
496 int ret = send_cmd(adrive_g, cmd);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500497 if (ret)
498 return ret;
499 return ata_dma_transfer(op);
500}
501
502// Read/write count blocks from a harddrive.
503static int
504ata_readwrite(struct disk_op_s *op, int iswrite)
505{
506 u64 lba = op->lba;
507
508 int usepio = ata_try_dma(op, iswrite, DISK_SECTOR_SIZE);
509
510 struct ata_pio_command cmd;
511 memset(&cmd, 0, sizeof(cmd));
512
513 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
514 cmd.sector_count2 = op->count >> 8;
515 cmd.lba_low2 = lba >> 24;
516 cmd.lba_mid2 = lba >> 32;
517 cmd.lba_high2 = lba >> 40;
518 lba &= 0xffffff;
519
520 if (usepio)
521 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS_EXT
522 : ATA_CMD_READ_SECTORS_EXT);
523 else
524 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
525 : ATA_CMD_READ_DMA_EXT);
526 } else {
527 if (usepio)
528 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS
529 : ATA_CMD_READ_SECTORS);
530 else
531 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA
532 : ATA_CMD_READ_DMA);
533 }
534
535 cmd.sector_count = op->count;
536 cmd.lba_low = lba;
537 cmd.lba_mid = lba >> 8;
538 cmd.lba_high = lba >> 16;
539 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
540
541 int ret;
542 if (usepio)
543 ret = ata_pio_cmd_data(op, iswrite, &cmd);
544 else
545 ret = ata_dma_cmd_data(op, &cmd);
546 if (ret)
547 return DISK_RET_EBADTRACK;
548 return DISK_RET_SUCCESS;
549}
550
551// 16bit command demuxer for ATA harddrives.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400552int
553process_ata_op(struct disk_op_s *op)
554{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400555 if (!CONFIG_ATA)
556 return 0;
557
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400558 struct atadrive_s *adrive_g = container_of(
559 op->drive_g, struct atadrive_s, drive);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400560 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400561 case CMD_READ:
Kevin O'Connor14021f22009-12-26 23:21:38 -0500562 return ata_readwrite(op, 0);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400563 case CMD_WRITE:
Kevin O'Connor14021f22009-12-26 23:21:38 -0500564 return ata_readwrite(op, 1);
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400565 case CMD_RESET:
566 ata_reset(adrive_g);
567 return DISK_RET_SUCCESS;
568 case CMD_ISREADY:
569 return isready(adrive_g);
570 case CMD_FORMAT:
571 case CMD_VERIFY:
572 case CMD_SEEK:
573 return DISK_RET_SUCCESS;
Kevin O'Connor42337662009-08-10 00:06:37 -0400574 default:
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400575 op->count = 0;
576 return DISK_RET_EPARAM;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400577 }
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400578}
579
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400580
581/****************************************************************
582 * ATAPI functions
583 ****************************************************************/
584
Kevin O'Connor7d700252010-02-15 11:56:07 -0500585#define CDROM_CDB_SIZE 12
586
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400587// Low-level atapi command transmit function.
Kevin O'Connor7d700252010-02-15 11:56:07 -0500588int
589atapi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500590{
Kevin O'Connor80c2b6e2010-12-05 12:52:02 -0500591 if (! CONFIG_ATA)
592 return 0;
593
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500594 struct atadrive_s *adrive_g = container_of(
595 op->drive_g, struct atadrive_s, drive);
596 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
597 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
598 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500599
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400600 struct ata_pio_command cmd;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500601 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400602 cmd.lba_mid = blocksize;
603 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400604 cmd.command = ATA_CMD_PACKET;
605
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500606 // Disable interrupts
607 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
608
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500609 int ret = send_cmd(adrive_g, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400610 if (ret)
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500611 goto fail;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500612 ret = ata_wait_data(iobase1);
613 if (ret)
614 goto fail;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500615
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400616 // Send command to device
Kevin O'Connor7d700252010-02-15 11:56:07 -0500617 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cdbcmd), CDROM_CDB_SIZE / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500618
Kevin O'Connor580e3322009-02-06 22:36:53 -0500619 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500620 if (status < 0) {
621 ret = status;
622 goto fail;
623 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400624
Kevin O'Connor580e3322009-02-06 22:36:53 -0500625 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connorb30c4002009-05-05 21:47:20 -0400626 u8 err = inb(iobase1 + ATA_CB_ERR);
627 // skip "Not Ready"
628 if (err != 0x20)
629 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
630 , status, err);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500631 ret = -2;
632 goto fail;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500633 }
Paolo Bonzini39d59892012-03-19 11:41:09 +0100634 if (blocksize) {
635 if (!(status & ATA_CB_STAT_DRQ)) {
636 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
637 ret = -3;
638 goto fail;
639 }
Kevin O'Connor580e3322009-02-06 22:36:53 -0500640
Paolo Bonzini39d59892012-03-19 11:41:09 +0100641 ret = ata_pio_transfer(op, 0, blocksize);
642 }
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500643
644fail:
645 // Enable interrupts
646 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor76977b22010-02-17 01:01:32 -0500647 if (ret)
648 return DISK_RET_EBADTRACK;
649 return DISK_RET_SUCCESS;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400650}
651
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400652
653/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500654 * ATA detect and init
655 ****************************************************************/
656
Kevin O'Connor14021f22009-12-26 23:21:38 -0500657// Send an identify device or identify device packet command.
658static int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500659send_ata_identity(struct atadrive_s *adrive_g, u16 *buffer, int command)
Kevin O'Connor14021f22009-12-26 23:21:38 -0500660{
661 memset(buffer, 0, DISK_SECTOR_SIZE);
662
663 struct disk_op_s dop;
664 memset(&dop, 0, sizeof(dop));
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500665 dop.drive_g = &adrive_g->drive;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500666 dop.count = 1;
667 dop.lba = 1;
668 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
669
670 struct ata_pio_command cmd;
671 memset(&cmd, 0, sizeof(cmd));
672 cmd.command = command;
673
674 return ata_pio_cmd_data(&dop, 0, &cmd);
675}
676
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400677// Extract the ATA/ATAPI version info.
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100678int
679ata_extract_version(u16 *buffer)
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400680{
681 // Extract ATA/ATAPI version.
682 u16 ataversion = buffer[80];
683 u8 version;
684 for (version=15; version>0; version--)
685 if (ataversion & (1<<version))
686 break;
687 return version;
688}
689
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500690#define MAXMODEL 40
Kevin O'Connor0a924122009-02-08 19:43:47 -0500691
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500692// Extract the ATA/ATAPI model info.
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100693char *
694ata_extract_model(char *model, u32 size, u16 *buffer)
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500695{
Kevin O'Connor0a924122009-02-08 19:43:47 -0500696 // Read model name
697 int i;
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100698 for (i=0; i<size/2; i++)
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500699 *(u16*)&model[i*2] = ntohs(buffer[27+i]);
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100700 model[size] = 0x00;
Kevin O'Connor9e881a32011-01-08 12:06:54 -0500701 nullTrailingSpace(model);
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500702 return model;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500703}
704
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500705// Common init code between ata and atapi
706static struct atadrive_s *
707init_atadrive(struct atadrive_s *dummy, u16 *buffer)
708{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500709 struct atadrive_s *adrive_g = malloc_fseg(sizeof(*adrive_g));
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500710 if (!adrive_g) {
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500711 warn_noalloc();
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500712 return NULL;
713 }
714 memset(adrive_g, 0, sizeof(*adrive_g));
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500715 adrive_g->chan_gf = dummy->chan_gf;
716 adrive_g->slave = dummy->slave;
717 adrive_g->drive.cntl_id = adrive_g->chan_gf->chanid * 2 + dummy->slave;
718 adrive_g->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
719 return adrive_g;
720}
721
Kevin O'Connor14021f22009-12-26 23:21:38 -0500722// Detect if the given drive is an atapi - initialize it if so.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500723static struct atadrive_s *
724init_drive_atapi(struct atadrive_s *dummy, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500725{
726 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connor14021f22009-12-26 23:21:38 -0500727 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_PACKET_DEVICE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500728 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400729 return NULL;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500730
731 // Success - setup as ATAPI.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500732 struct atadrive_s *adrive_g = init_atadrive(dummy, buffer);
733 if (!adrive_g)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400734 return NULL;
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400735 adrive_g->drive.type = DTYPE_ATA_ATAPI;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500736 adrive_g->drive.blksize = CDROM_SECTOR_SIZE;
737 adrive_g->drive.sectors = (u64)-1;
Kevin O'Connor42337662009-08-10 00:06:37 -0400738 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500739 char model[MAXMODEL+1];
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500740 char *desc = znprintf(MAXDESCSIZE
741 , "DVD/CD [ata%d-%d: %s ATAPI-%d %s]"
742 , adrive_g->chan_gf->chanid, adrive_g->slave
743 , ata_extract_model(model, MAXMODEL, buffer)
744 , ata_extract_version(buffer)
745 , (iscd ? "DVD/CD" : "Device"));
746 dprintf(1, "%s\n", desc);
Kevin O'Connor42337662009-08-10 00:06:37 -0400747
748 // fill cdidmap
Kevin O'Connor031ef552010-12-27 19:26:57 -0500749 if (iscd) {
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400750 int prio = bootprio_find_ata_device(adrive_g->chan_gf->pci_tmp,
Kevin O'Connor031ef552010-12-27 19:26:57 -0500751 adrive_g->chan_gf->chanid,
752 adrive_g->slave);
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500753 boot_add_cd(&adrive_g->drive, desc, prio);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500754 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500755
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500756 return adrive_g;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500757}
758
Kevin O'Connor14021f22009-12-26 23:21:38 -0500759// Detect if the given drive is a regular ata drive - initialize it if so.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500760static struct atadrive_s *
761init_drive_ata(struct atadrive_s *dummy, u16 *buffer)
Kevin O'Connorc1437612008-05-18 01:43:07 -0400762{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500763 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connor14021f22009-12-26 23:21:38 -0500764 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_DEVICE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400765 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400766 return NULL;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500767
768 // Success - setup as ATA.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500769 struct atadrive_s *adrive_g = init_atadrive(dummy, buffer);
770 if (!adrive_g)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400771 return NULL;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500772 adrive_g->drive.type = DTYPE_ATA;
773 adrive_g->drive.blksize = DISK_SECTOR_SIZE;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400774
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500775 adrive_g->drive.pchs.cylinders = buffer[1];
776 adrive_g->drive.pchs.heads = buffer[3];
777 adrive_g->drive.pchs.spt = buffer[6];
Kevin O'Connorc1437612008-05-18 01:43:07 -0400778
779 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500780 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
781 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400782 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500783 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500784 adrive_g->drive.sectors = sectors;
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500785 u64 adjsize = sectors >> 11;
786 char adjprefix = 'M';
787 if (adjsize >= (1 << 16)) {
788 adjsize >>= 10;
789 adjprefix = 'G';
790 }
791 char model[MAXMODEL+1];
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500792 char *desc = znprintf(MAXDESCSIZE
793 , "ata%d-%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
794 , adrive_g->chan_gf->chanid, adrive_g->slave
795 , ata_extract_model(model, MAXMODEL, buffer)
796 , ata_extract_version(buffer)
797 , (u32)adjsize, adjprefix);
798 dprintf(1, "%s\n", desc);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400799
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400800 int prio = bootprio_find_ata_device(adrive_g->chan_gf->pci_tmp,
Kevin O'Connor031ef552010-12-27 19:26:57 -0500801 adrive_g->chan_gf->chanid,
802 adrive_g->slave);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500803 // Register with bcv system.
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500804 boot_add_hd(&adrive_g->drive, desc, prio);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400805
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500806 return adrive_g;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400807}
808
Kevin O'Connora5826b52009-10-24 17:57:29 -0400809static u64 SpinupEnd;
810
Kevin O'Connor14021f22009-12-26 23:21:38 -0500811// Wait for non-busy status and check for "floating bus" condition.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400812static int
Kevin O'Connora5826b52009-10-24 17:57:29 -0400813powerup_await_non_bsy(u16 base)
Kevin O'Connor425f2122009-04-18 12:23:00 -0400814{
815 u8 orstatus = 0;
816 u8 status;
817 for (;;) {
818 status = inb(base+ATA_CB_STAT);
819 if (!(status & ATA_CB_STAT_BSY))
820 break;
821 orstatus |= status;
822 if (orstatus == 0xff) {
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400823 dprintf(4, "powerup IDE floating\n");
Kevin O'Connor425f2122009-04-18 12:23:00 -0400824 return orstatus;
825 }
Kevin O'Connor144817b2010-05-23 10:46:49 -0400826 if (check_tsc(SpinupEnd)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500827 warn_timeout();
Kevin O'Connor425f2122009-04-18 12:23:00 -0400828 return -1;
829 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400830 yield();
Kevin O'Connor425f2122009-04-18 12:23:00 -0400831 }
832 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
833 return status;
834}
835
Kevin O'Connor14021f22009-12-26 23:21:38 -0500836// Detect any drives attached to a given controller.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400837static void
Kevin O'Connora5826b52009-10-24 17:57:29 -0400838ata_detect(void *data)
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500839{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500840 struct ata_channel_s *chan_gf = data;
841 struct atadrive_s dummy;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400842 memset(&dummy, 0, sizeof(dummy));
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500843 dummy.chan_gf = chan_gf;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500844 // Device detection
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500845 int didreset = 0;
846 u8 slave;
847 for (slave=0; slave<=1; slave++) {
Kevin O'Connor425f2122009-04-18 12:23:00 -0400848 // Wait for not-bsy.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500849 u16 iobase1 = chan_gf->iobase1;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400850 int status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400851 if (status < 0)
852 continue;
853 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
854 outb(newdh, iobase1+ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400855 ndelay(400);
Kevin O'Connora5826b52009-10-24 17:57:29 -0400856 status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400857 if (status < 0)
858 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500859
Kevin O'Connor580e3322009-02-06 22:36:53 -0500860 // Check if ioport registers look valid.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400861 outb(newdh, iobase1+ATA_CB_DH);
862 u8 dh = inb(iobase1+ATA_CB_DH);
863 outb(0x55, iobase1+ATA_CB_SC);
864 outb(0xaa, iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400865 u8 sc = inb(iobase1+ATA_CB_SC);
866 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500867 dprintf(6, "ata_detect ata%d-%d: sc=%x sn=%x dh=%x\n"
868 , chan_gf->chanid, slave, sc, sn, dh);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400869 if (sc != 0x55 || sn != 0xaa || dh != newdh)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400870 continue;
871
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400872 // Prepare new drive.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500873 dummy.slave = slave;
Kevin O'Connorb1144362009-08-11 20:43:38 -0400874
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400875 // reset the channel
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500876 if (!didreset) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400877 ata_reset(&dummy);
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500878 didreset = 1;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500879 }
880
Kevin O'Connor580e3322009-02-06 22:36:53 -0500881 // check for ATAPI
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400882 u16 buffer[256];
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500883 struct atadrive_s *adrive_g = init_drive_atapi(&dummy, buffer);
884 if (!adrive_g) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400885 // Didn't find an ATAPI drive - look for ATA drive.
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400886 u8 st = inb(iobase1+ATA_CB_STAT);
887 if (!st)
888 // Status not set - can't be a valid drive.
889 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500890
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400891 // Wait for RDY.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400892 int ret = await_rdy(iobase1);
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400893 if (ret < 0)
894 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500895
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400896 // check for ATA.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500897 adrive_g = init_drive_ata(&dummy, buffer);
898 if (!adrive_g)
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400899 // No ATA drive found
900 continue;
901 }
Kevin O'Connor580e3322009-02-06 22:36:53 -0500902
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400903 u16 resetresult = buffer[93];
904 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
905 if (!slave && (resetresult & 0xdf61) == 0x4041)
906 // resetresult looks valid and device 0 is responding to
907 // device 1 requests - device 1 must not be present - skip
908 // detection.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500909 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500910 }
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500911}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400912
Kevin O'Connor14021f22009-12-26 23:21:38 -0500913// Initialize an ata controller and detect its drives.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400914static void
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400915init_controller(struct pci_device *pci, int irq
916 , u32 port1, u32 port2, u32 master)
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500917{
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400918 static int chanid = 0;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500919 struct ata_channel_s *chan_gf = malloc_fseg(sizeof(*chan_gf));
920 if (!chan_gf) {
921 warn_noalloc();
922 return;
923 }
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400924 chan_gf->chanid = chanid++;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500925 chan_gf->irq = irq;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400926 chan_gf->pci_bdf = pci ? pci->bdf : -1;
927 chan_gf->pci_tmp = pci;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500928 chan_gf->iobase1 = port1;
929 chan_gf->iobase2 = port2;
930 chan_gf->iomaster = master;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500931 dprintf(1, "ATA controller %d at %x/%x/%x (irq %d dev %x)\n"
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400932 , chanid, port1, port2, master, irq, chan_gf->pci_bdf);
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500933 run_thread(ata_detect, chan_gf);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500934}
935
Kevin O'Connor525219b2009-12-05 13:36:18 -0500936#define IRQ_ATA1 14
937#define IRQ_ATA2 15
938
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400939// Handle controllers on an ATA PCI device.
940static void
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400941init_pciata(struct pci_device *pci, u8 prog_if)
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400942{
Kevin O'Connor76b5e712011-06-21 22:52:51 -0400943 pci->have_driver = 1;
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400944 u16 bdf = pci->bdf;
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400945 u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400946 int master = 0;
947 if (CONFIG_ATA_DMA && prog_if & 0x80) {
948 // Check for bus-mastering.
949 u32 bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_4);
950 if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
951 master = bar & PCI_BASE_ADDRESS_IO_MASK;
952 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
953 }
954 }
955
956 u32 port1, port2, irq;
957 if (prog_if & 1) {
958 port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_0)
959 & PCI_BASE_ADDRESS_IO_MASK);
960 port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_1)
961 & PCI_BASE_ADDRESS_IO_MASK);
962 irq = pciirq;
963 } else {
964 port1 = PORT_ATA1_CMD_BASE;
965 port2 = PORT_ATA1_CTRL_BASE;
966 irq = IRQ_ATA1;
967 }
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400968 init_controller(pci, irq, port1, port2, master);
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400969
970 if (prog_if & 4) {
971 port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_2)
972 & PCI_BASE_ADDRESS_IO_MASK);
973 port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_3)
974 & PCI_BASE_ADDRESS_IO_MASK);
975 irq = pciirq;
976 } else {
977 port1 = PORT_ATA2_CMD_BASE;
978 port2 = PORT_ATA2_CTRL_BASE;
979 irq = IRQ_ATA2;
980 }
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400981 init_controller(pci, irq, port1, port2, master ? master + 8 : 0);
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400982}
983
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400984static void
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400985found_genericata(struct pci_device *pci, void *arg)
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400986{
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400987 init_pciata(pci, pci->prog_if);
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400988}
989
990static void
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400991found_compatibleahci(struct pci_device *pci, void *arg)
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400992{
993 if (CONFIG_AHCI)
994 // Already handled directly via native ahci interface.
995 return;
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400996 init_pciata(pci, 0x8f);
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400997}
998
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400999static const struct pci_device_id pci_ata_tbl[] = {
Kevin O'Connorb9457ec2011-06-19 10:03:08 -04001000 PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE
1001 , found_genericata),
1002 PCI_DEVICE(PCI_VENDOR_ID_ATI, 0x4391, found_compatibleahci),
Kevin O'Connor927d16e2011-06-19 09:43:20 -04001003 PCI_DEVICE_END,
1004};
1005
Kevin O'Connor14021f22009-12-26 23:21:38 -05001006// Locate and init ata controllers.
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001007static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001008ata_init(void)
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001009{
Kevin O'Connor3f3e58d2011-06-20 22:20:43 -04001010 if (!CONFIG_COREBOOT && !PCIDevices) {
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001011 // No PCI devices found - probably a QEMU "-M isapc" machine.
1012 // Try using ISA ports for ATA controllers.
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -04001013 init_controller(NULL, IRQ_ATA1
Kevin O'Connor14021f22009-12-26 23:21:38 -05001014 , PORT_ATA1_CMD_BASE, PORT_ATA1_CTRL_BASE, 0);
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -04001015 init_controller(NULL, IRQ_ATA2
Kevin O'Connor14021f22009-12-26 23:21:38 -05001016 , PORT_ATA2_CMD_BASE, PORT_ATA2_CTRL_BASE, 0);
Kevin O'Connor3f3e58d2011-06-20 22:20:43 -04001017 return;
1018 }
1019
1020 // Scan PCI bus for ATA adapters
1021 struct pci_device *pci;
1022 foreachpci(pci) {
Kevin O'Connor278b19f2011-06-21 22:41:15 -04001023 pci_init_device(pci_ata_tbl, pci, NULL);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001024 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001025}
1026
1027void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001028ata_setup(void)
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001029{
Kevin O'Connor59c75742010-02-13 18:49:24 -05001030 ASSERT32FLAT();
Kevin O'Connorc1437612008-05-18 01:43:07 -04001031 if (!CONFIG_ATA)
1032 return;
1033
Kevin O'Connor35192dd2008-06-08 19:18:33 -04001034 dprintf(3, "init hard drives\n");
Kevin O'Connora5826b52009-10-24 17:57:29 -04001035
1036 SpinupEnd = calc_future_tsc(IDE_TIMEOUT);
Kevin O'Connorc1437612008-05-18 01:43:07 -04001037 ata_init();
Kevin O'Connorc1437612008-05-18 01:43:07 -04001038
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001039 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -04001040
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -04001041 enable_hwirq(14, FUNC16(entry_76));
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001042}