blob: 705e106ea02454ffadae73f5ce571ceca51c05fb [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'Connorb3064592012-08-14 21:20:10 -040011#include "byteorder.h" // be16_to_cpu
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050012#include "cmos.h" // inb_cmos
Kevin O'Connord21c0892008-11-26 17:02:43 -050013#include "pic.h" // enable_hwirq
Kevin O'Connor46b82622012-05-13 12:10:30 -040014#include "biosvar.h" // GET_GLOBAL
Kevin O'Connor3f3e58d2011-06-20 22:20:43 -040015#include "pci.h" // foreachpci
Kevin O'Connor2ed2f582008-11-08 15:53:36 -050016#include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
17#include "pci_regs.h" // PCI_INTERRUPT_LINE
Kevin O'Connor72eee3e2010-12-27 19:07:49 -050018#include "boot.h" // boot_add_hd
Kevin O'Connor609da232008-12-28 23:18:57 -050019#include "disk.h" // struct ata_s
Kevin O'Connorc892b132009-08-11 21:59:37 -040020#include "ata.h" // ATA_CB_STAT
Kevin O'Connor7d700252010-02-15 11:56:07 -050021#include "blockcmd.h" // CDB_CMD_READ_10
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040022#include "string.h" // memset
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050023
Kevin O'Connor425f2122009-04-18 12:23:00 -040024#define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050025
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050026
Kevin O'Connora6b9f712008-03-29 12:53:57 -040027/****************************************************************
28 * Helper functions
29 ****************************************************************/
30
31// Wait for the specified ide state
Kevin O'Connor580e3322009-02-06 22:36:53 -050032static inline int
33await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050034{
Kevin O'Connor018bdd72013-07-20 18:22:57 -040035 u32 end = timer_calc(timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050036 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -040037 u8 status = inb(base+ATA_CB_STAT);
Kevin O'Connor580e3322009-02-06 22:36:53 -050038 if ((status & mask) == flags)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040039 return status;
Kevin O'Connor018bdd72013-07-20 18:22:57 -040040 if (timer_check(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050041 warn_timeout();
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050042 return -1;
43 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -040044 yield();
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050045 }
Kevin O'Connor580e3322009-02-06 22:36:53 -050046}
47
48// Wait for the device to be not-busy.
49static int
50await_not_bsy(u16 base)
51{
52 return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
53}
54
55// Wait for the device to be ready.
56static int
57await_rdy(u16 base)
58{
59 return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050060}
61
Kevin O'Connora6b9f712008-03-29 12:53:57 -040062// Wait for ide state - pauses for one ata cycle first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050063static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050064pause_await_not_bsy(u16 iobase1, u16 iobase2)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040065{
66 // Wait one PIO transfer cycle.
67 inb(iobase2 + ATA_CB_ASTAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050068
Kevin O'Connor580e3322009-02-06 22:36:53 -050069 return await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -040070}
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050071
Kevin O'Connoref2822a2008-06-07 15:23:11 -040072// Wait for ide state - pause for 400ns first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050073static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050074ndelay_await_not_bsy(u16 iobase1)
Kevin O'Connoref2822a2008-06-07 15:23:11 -040075{
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050076 ndelay(400);
Kevin O'Connor580e3322009-02-06 22:36:53 -050077 return await_not_bsy(iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -040078}
79
Kevin O'Connora6b9f712008-03-29 12:53:57 -040080// Reset a drive
Kevin O'Connorb1144362009-08-11 20:43:38 -040081static void
Kevin O'Connor8f469b92010-02-28 01:28:11 -050082ata_reset(struct atadrive_s *adrive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050083{
Kevin O'Connor8f469b92010-02-28 01:28:11 -050084 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
85 u8 slave = GET_GLOBAL(adrive_g->slave);
86 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
87 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050088
Kevin O'Connor8f469b92010-02-28 01:28:11 -050089 dprintf(6, "ata_reset drive=%p\n", &adrive_g->drive);
Kevin O'Connor580e3322009-02-06 22:36:53 -050090 // Pulse SRST
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050091 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 -050092 udelay(5);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050093 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
Kevin O'Connor10ad7992009-10-24 11:06:08 -040094 msleep(2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050095
Kevin O'Connor580e3322009-02-06 22:36:53 -050096 // wait for device to become not busy.
97 int status = await_not_bsy(iobase1);
98 if (status < 0)
99 goto done;
100 if (slave) {
101 // Change device.
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400102 u32 end = timer_calc(IDE_TIMEOUT);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500103 for (;;) {
104 outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400105 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500106 if (status < 0)
107 goto done;
108 if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
109 break;
110 // Change drive request failed to take effect - retry.
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400111 if (timer_check(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500112 warn_timeout();
Kevin O'Connor580e3322009-02-06 22:36:53 -0500113 goto done;
114 }
115 }
Kevin O'Connorf5624d22009-08-18 22:17:57 -0400116 } else {
117 // QEMU doesn't reset dh on reset, so set it explicitly.
118 outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
Kevin O'Connor2e3eeeb2008-06-12 22:29:30 -0400119 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500120
Kevin O'Connor580e3322009-02-06 22:36:53 -0500121 // On a user-reset request, wait for RDY if it is an ATA device.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500122 u8 type=GET_GLOBAL(adrive_g->drive.type);
Kevin O'Connor42337662009-08-10 00:06:37 -0400123 if (type == DTYPE_ATA)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500124 status = await_rdy(iobase1);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400125
Kevin O'Connor580e3322009-02-06 22:36:53 -0500126done:
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500127 // Enable interrupts
128 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500129
130 dprintf(6, "ata_reset exit status=%x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500131}
132
Kevin O'Connor14021f22009-12-26 23:21:38 -0500133// Check for drive RDY for 16bit interface command.
Kevin O'Connor42337662009-08-10 00:06:37 -0400134static int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500135isready(struct atadrive_s *adrive_g)
Kevin O'Connor42337662009-08-10 00:06:37 -0400136{
137 // Read the status from controller
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500138 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
139 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400140 u8 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400141 if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY)
142 return DISK_RET_SUCCESS;
143 return DISK_RET_ENOTREADY;
Kevin O'Connor42337662009-08-10 00:06:37 -0400144}
145
Kevin O'Connoree55c762008-05-13 00:18:20 -0400146
147/****************************************************************
148 * ATA send command
149 ****************************************************************/
150
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400151struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400152 u8 feature;
153 u8 sector_count;
154 u8 lba_low;
155 u8 lba_mid;
156 u8 lba_high;
157 u8 device;
158 u8 command;
159
Kevin O'Connor14021f22009-12-26 23:21:38 -0500160 u8 feature2;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400161 u8 sector_count2;
162 u8 lba_low2;
163 u8 lba_mid2;
164 u8 lba_high2;
165};
166
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400167// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400168static int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500169send_cmd(struct atadrive_s *adrive_g, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500170{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500171 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
172 u8 slave = GET_GLOBAL(adrive_g->slave);
173 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400174
175 // Select device
Kevin O'Connor580e3322009-02-06 22:36:53 -0500176 int status = await_not_bsy(iobase1);
177 if (status < 0)
178 return status;
179 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
180 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
181 u8 olddh = inb(iobase1 + ATA_CB_DH);
182 outb(newdh, iobase1 + ATA_CB_DH);
183 if ((olddh ^ newdh) & (1<<4)) {
184 // Was a device change - wait for device to become not busy.
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400185 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500186 if (status < 0)
187 return status;
188 }
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400189
Kevin O'Connor14021f22009-12-26 23:21:38 -0500190 // Check for ATA_CMD_(READ|WRITE)_(SECTORS|DMA)_EXT commands.
191 if ((cmd->command & ~0x11) == ATA_CMD_READ_SECTORS_EXT) {
192 outb(cmd->feature2, iobase1 + ATA_CB_FR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400193 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
194 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
195 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
196 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500197 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400198 outb(cmd->feature, iobase1 + ATA_CB_FR);
199 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
200 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
201 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
202 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400203 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500204
Kevin O'Connor14021f22009-12-26 23:21:38 -0500205 return 0;
206}
207
208// Wait for data after calling 'send_cmd'.
209static int
210ata_wait_data(u16 iobase1)
211{
212 int status = ndelay_await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400213 if (status < 0)
214 return status;
215
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500216 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500217 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
218 , status, inb(iobase1 + ATA_CB_ERR));
Kevin O'Connora05223c2008-06-28 12:15:57 -0400219 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400220 }
221 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500222 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400223 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500224 }
225
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400226 return 0;
227}
228
Kevin O'Connor14021f22009-12-26 23:21:38 -0500229// Send an ata command that does not transfer any further data.
230int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500231ata_cmd_nondata(struct atadrive_s *adrive_g, struct ata_pio_command *cmd)
Kevin O'Connor14021f22009-12-26 23:21:38 -0500232{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500233 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
234 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
235 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500236
237 // Disable interrupts
238 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
239
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500240 int ret = send_cmd(adrive_g, cmd);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500241 if (ret)
242 goto fail;
243 ret = ndelay_await_not_bsy(iobase1);
244 if (ret < 0)
245 goto fail;
246
247 if (ret & ATA_CB_STAT_ERR) {
248 dprintf(6, "nondata cmd : read error (status=%02x err=%02x)\n"
249 , ret, inb(iobase1 + ATA_CB_ERR));
250 ret = -4;
251 goto fail;
252 }
253 if (ret & ATA_CB_STAT_DRQ) {
254 dprintf(6, "nondata cmd : DRQ set (status %02x)\n", ret);
255 ret = -5;
256 goto fail;
257 }
258
259fail:
260 // Enable interrupts
261 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
262
263 return ret;
264}
265
Kevin O'Connoree55c762008-05-13 00:18:20 -0400266
267/****************************************************************
Kevin O'Connor14021f22009-12-26 23:21:38 -0500268 * ATA PIO transfers
Kevin O'Connoree55c762008-05-13 00:18:20 -0400269 ****************************************************************/
270
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400271// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400272// 'op->drive_g'.
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400273static int
Kevin O'Connor14021f22009-12-26 23:21:38 -0500274ata_pio_transfer(struct disk_op_s *op, int iswrite, int blocksize)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400275{
Kevin O'Connor14021f22009-12-26 23:21:38 -0500276 dprintf(16, "ata_pio_transfer id=%p write=%d count=%d bs=%d buf=%p\n"
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400277 , op->drive_g, iswrite, op->count, blocksize, op->buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400278
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500279 struct atadrive_s *adrive_g = container_of(
280 op->drive_g, struct atadrive_s, drive);
281 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
282 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
283 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400284 int count = op->count;
285 void *buf_fl = op->buf_fl;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400286 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400287 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400288 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400289 // Write data to controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400290 dprintf(16, "Write sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500291 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400292 outsl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400293 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400294 outsw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500295 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400296 // Read data from controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400297 dprintf(16, "Read sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500298 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400299 insl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400300 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400301 insw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400302 }
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400303 buf_fl += blocksize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400304
Kevin O'Connor580e3322009-02-06 22:36:53 -0500305 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400306 if (status < 0) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400307 // Error
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400308 op->count -= count;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400309 return status;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400310 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400311
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400312 count--;
313 if (!count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400314 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500315 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
316 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500317 dprintf(6, "ata_pio_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500318 , status);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400319 op->count -= count;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400320 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500321 }
322 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400323
Kevin O'Connor580e3322009-02-06 22:36:53 -0500324 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
325 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400326 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400327 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500328 if (status != 0) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500329 dprintf(6, "ata_pio_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400330 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400331 }
332
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500333 return 0;
334}
335
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400336
337/****************************************************************
Kevin O'Connor14021f22009-12-26 23:21:38 -0500338 * ATA DMA transfers
339 ****************************************************************/
340
341#define BM_CMD 0
342#define BM_CMD_MEMWRITE 0x08
343#define BM_CMD_START 0x01
344#define BM_STATUS 2
345#define BM_STATUS_IRQ 0x04
346#define BM_STATUS_ERROR 0x02
347#define BM_STATUS_ACTIVE 0x01
348#define BM_TABLE 4
349
350struct sff_dma_prd {
351 u32 buf_fl;
352 u32 count;
353};
354
355// Check if DMA available and setup transfer if so.
356static int
357ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize)
358{
Kevin O'Connor46b82622012-05-13 12:10:30 -0400359 ASSERT16();
Kevin O'Connor4d079022010-01-17 12:58:47 -0500360 if (! CONFIG_ATA_DMA)
361 return -1;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500362 u32 dest = (u32)op->buf_fl;
363 if (dest & 1)
364 // Need minimum alignment of 1.
365 return -1;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500366 struct atadrive_s *adrive_g = container_of(
367 op->drive_g, struct atadrive_s, drive);
368 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
369 u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500370 if (! iomaster)
371 return -1;
372 u32 bytes = op->count * blocksize;
373 if (! bytes)
374 return -1;
375
376 // Build PRD dma structure.
Kevin O'Connor46b82622012-05-13 12:10:30 -0400377 struct sff_dma_prd *dma = MAKE_FLATPTR(SEG_LOW, ExtraStack);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500378 struct sff_dma_prd *origdma = dma;
379 while (bytes) {
380 if (dma >= &origdma[16])
381 // Too many descriptors..
382 return -1;
383 u32 count = bytes;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500384 u32 max = 0x10000 - (dest & 0xffff);
385 if (count > max)
386 count = max;
387
Kevin O'Connor1e3bd4f2012-05-24 23:56:19 -0400388 SET_LOWFLAT(dma->buf_fl, dest);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500389 bytes -= count;
390 if (!bytes)
391 // Last descriptor.
392 count |= 1<<31;
393 dprintf(16, "dma@%p: %08x %08x\n", dma, dest, count);
394 dest += count;
Kevin O'Connor1e3bd4f2012-05-24 23:56:19 -0400395 SET_LOWFLAT(dma->count, count);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500396 dma++;
397 }
398
399 // Program bus-master controller.
400 outl((u32)origdma, iomaster + BM_TABLE);
401 u8 oldcmd = inb(iomaster + BM_CMD) & ~(BM_CMD_MEMWRITE|BM_CMD_START);
402 outb(oldcmd | (iswrite ? 0x00 : BM_CMD_MEMWRITE), iomaster + BM_CMD);
403 outb(BM_STATUS_ERROR|BM_STATUS_IRQ, iomaster + BM_STATUS);
404
405 return 0;
406}
407
408// Transfer data using DMA.
409static int
410ata_dma_transfer(struct disk_op_s *op)
411{
Kevin O'Connor4d079022010-01-17 12:58:47 -0500412 if (! CONFIG_ATA_DMA)
413 return -1;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500414 dprintf(16, "ata_dma_transfer id=%p buf=%p\n", op->drive_g, op->buf_fl);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500415
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500416 struct atadrive_s *adrive_g = container_of(
417 op->drive_g, struct atadrive_s, drive);
418 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
419 u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500420
421 // Start bus-master controller.
422 u8 oldcmd = inb(iomaster + BM_CMD);
423 outb(oldcmd | BM_CMD_START, iomaster + BM_CMD);
424
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400425 u32 end = timer_calc(IDE_TIMEOUT);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500426 u8 status;
427 for (;;) {
428 status = inb(iomaster + BM_STATUS);
429 if (status & BM_STATUS_IRQ)
430 break;
431 // Transfer in progress
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400432 if (timer_check(end)) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500433 // Timeout.
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500434 warn_timeout();
Kevin O'Connor14021f22009-12-26 23:21:38 -0500435 break;
436 }
437 yield();
438 }
439 outb(oldcmd & ~BM_CMD_START, iomaster + BM_CMD);
440
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500441 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
442 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500443 int idestatus = pause_await_not_bsy(iobase1, iobase2);
444
445 if ((status & (BM_STATUS_IRQ|BM_STATUS_ACTIVE)) == BM_STATUS_IRQ
446 && idestatus >= 0x00
447 && (idestatus & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
448 | ATA_CB_STAT_ERR)) == 0x00)
449 // Success.
450 return 0;
451
452 dprintf(6, "IDE DMA error (dma=%x ide=%x/%x/%x)\n", status, idestatus
453 , inb(iobase2 + ATA_CB_ASTAT), inb(iobase1 + ATA_CB_ERR));
454 op->count = 0;
455 return -1;
456}
457
458
459/****************************************************************
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400460 * ATA hard drive functions
461 ****************************************************************/
462
Kevin O'Connor14021f22009-12-26 23:21:38 -0500463// Transfer data to harddrive using PIO protocol.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400464static int
Kevin O'Connor14021f22009-12-26 23:21:38 -0500465ata_pio_cmd_data(struct disk_op_s *op, int iswrite, struct ata_pio_command *cmd)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400466{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500467 struct atadrive_s *adrive_g = container_of(
468 op->drive_g, struct atadrive_s, drive);
469 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
470 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
471 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400472
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500473 // Disable interrupts
474 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
475
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500476 int ret = send_cmd(adrive_g, cmd);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400477 if (ret)
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500478 goto fail;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500479 ret = ata_wait_data(iobase1);
480 if (ret)
481 goto fail;
482 ret = ata_pio_transfer(op, iswrite, DISK_SECTOR_SIZE);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500483
484fail:
485 // Enable interrupts
486 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
487 return ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400488}
489
Kevin O'Connor14021f22009-12-26 23:21:38 -0500490// Transfer data to harddrive using DMA protocol.
491static int
492ata_dma_cmd_data(struct disk_op_s *op, struct ata_pio_command *cmd)
493{
Kevin O'Connor4d079022010-01-17 12:58:47 -0500494 if (! CONFIG_ATA_DMA)
495 return -1;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500496 struct atadrive_s *adrive_g = container_of(
497 op->drive_g, struct atadrive_s, drive);
498 int ret = send_cmd(adrive_g, cmd);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500499 if (ret)
500 return ret;
501 return ata_dma_transfer(op);
502}
503
504// Read/write count blocks from a harddrive.
505static int
506ata_readwrite(struct disk_op_s *op, int iswrite)
507{
508 u64 lba = op->lba;
509
510 int usepio = ata_try_dma(op, iswrite, DISK_SECTOR_SIZE);
511
512 struct ata_pio_command cmd;
513 memset(&cmd, 0, sizeof(cmd));
514
515 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
516 cmd.sector_count2 = op->count >> 8;
517 cmd.lba_low2 = lba >> 24;
518 cmd.lba_mid2 = lba >> 32;
519 cmd.lba_high2 = lba >> 40;
520 lba &= 0xffffff;
521
522 if (usepio)
523 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS_EXT
524 : ATA_CMD_READ_SECTORS_EXT);
525 else
526 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
527 : ATA_CMD_READ_DMA_EXT);
528 } else {
529 if (usepio)
530 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS
531 : ATA_CMD_READ_SECTORS);
532 else
533 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA
534 : ATA_CMD_READ_DMA);
535 }
536
537 cmd.sector_count = op->count;
538 cmd.lba_low = lba;
539 cmd.lba_mid = lba >> 8;
540 cmd.lba_high = lba >> 16;
541 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
542
543 int ret;
544 if (usepio)
545 ret = ata_pio_cmd_data(op, iswrite, &cmd);
546 else
547 ret = ata_dma_cmd_data(op, &cmd);
548 if (ret)
549 return DISK_RET_EBADTRACK;
550 return DISK_RET_SUCCESS;
551}
552
553// 16bit command demuxer for ATA harddrives.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400554int
555process_ata_op(struct disk_op_s *op)
556{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400557 if (!CONFIG_ATA)
558 return 0;
559
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400560 struct atadrive_s *adrive_g = container_of(
561 op->drive_g, struct atadrive_s, drive);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400562 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400563 case CMD_READ:
Kevin O'Connor14021f22009-12-26 23:21:38 -0500564 return ata_readwrite(op, 0);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400565 case CMD_WRITE:
Kevin O'Connor14021f22009-12-26 23:21:38 -0500566 return ata_readwrite(op, 1);
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400567 case CMD_RESET:
568 ata_reset(adrive_g);
569 return DISK_RET_SUCCESS;
570 case CMD_ISREADY:
571 return isready(adrive_g);
572 case CMD_FORMAT:
573 case CMD_VERIFY:
574 case CMD_SEEK:
575 return DISK_RET_SUCCESS;
Kevin O'Connor42337662009-08-10 00:06:37 -0400576 default:
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400577 op->count = 0;
578 return DISK_RET_EPARAM;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400579 }
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400580}
581
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400582
583/****************************************************************
584 * ATAPI functions
585 ****************************************************************/
586
Kevin O'Connor7d700252010-02-15 11:56:07 -0500587#define CDROM_CDB_SIZE 12
588
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400589// Low-level atapi command transmit function.
Kevin O'Connor7d700252010-02-15 11:56:07 -0500590int
591atapi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500592{
Kevin O'Connor80c2b6e2010-12-05 12:52:02 -0500593 if (! CONFIG_ATA)
594 return 0;
595
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500596 struct atadrive_s *adrive_g = container_of(
597 op->drive_g, struct atadrive_s, drive);
598 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
599 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
600 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500601
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400602 struct ata_pio_command cmd;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500603 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400604 cmd.lba_mid = blocksize;
605 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400606 cmd.command = ATA_CMD_PACKET;
607
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500608 // Disable interrupts
609 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
610
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500611 int ret = send_cmd(adrive_g, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400612 if (ret)
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500613 goto fail;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500614 ret = ata_wait_data(iobase1);
615 if (ret)
616 goto fail;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500617
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400618 // Send command to device
Kevin O'Connor7d700252010-02-15 11:56:07 -0500619 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cdbcmd), CDROM_CDB_SIZE / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500620
Kevin O'Connor580e3322009-02-06 22:36:53 -0500621 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500622 if (status < 0) {
623 ret = status;
624 goto fail;
625 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400626
Kevin O'Connor580e3322009-02-06 22:36:53 -0500627 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connorb30c4002009-05-05 21:47:20 -0400628 u8 err = inb(iobase1 + ATA_CB_ERR);
629 // skip "Not Ready"
630 if (err != 0x20)
631 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
632 , status, err);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500633 ret = -2;
634 goto fail;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500635 }
Paolo Bonzini39d59892012-03-19 11:41:09 +0100636 if (blocksize) {
637 if (!(status & ATA_CB_STAT_DRQ)) {
638 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
639 ret = -3;
640 goto fail;
641 }
Kevin O'Connor580e3322009-02-06 22:36:53 -0500642
Paolo Bonzini39d59892012-03-19 11:41:09 +0100643 ret = ata_pio_transfer(op, 0, blocksize);
644 }
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500645
646fail:
647 // Enable interrupts
648 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor76977b22010-02-17 01:01:32 -0500649 if (ret)
650 return DISK_RET_EBADTRACK;
651 return DISK_RET_SUCCESS;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400652}
653
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400654
655/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500656 * ATA detect and init
657 ****************************************************************/
658
Kevin O'Connor14021f22009-12-26 23:21:38 -0500659// Send an identify device or identify device packet command.
660static int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500661send_ata_identity(struct atadrive_s *adrive_g, u16 *buffer, int command)
Kevin O'Connor14021f22009-12-26 23:21:38 -0500662{
663 memset(buffer, 0, DISK_SECTOR_SIZE);
664
665 struct disk_op_s dop;
666 memset(&dop, 0, sizeof(dop));
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500667 dop.drive_g = &adrive_g->drive;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500668 dop.count = 1;
669 dop.lba = 1;
670 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
671
672 struct ata_pio_command cmd;
673 memset(&cmd, 0, sizeof(cmd));
674 cmd.command = command;
675
676 return ata_pio_cmd_data(&dop, 0, &cmd);
677}
678
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400679// Extract the ATA/ATAPI version info.
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100680int
681ata_extract_version(u16 *buffer)
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400682{
683 // Extract ATA/ATAPI version.
684 u16 ataversion = buffer[80];
685 u8 version;
686 for (version=15; version>0; version--)
687 if (ataversion & (1<<version))
688 break;
689 return version;
690}
691
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500692#define MAXMODEL 40
Kevin O'Connor0a924122009-02-08 19:43:47 -0500693
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500694// Extract the ATA/ATAPI model info.
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100695char *
696ata_extract_model(char *model, u32 size, u16 *buffer)
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500697{
Kevin O'Connor0a924122009-02-08 19:43:47 -0500698 // Read model name
699 int i;
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100700 for (i=0; i<size/2; i++)
Kevin O'Connorb3064592012-08-14 21:20:10 -0400701 *(u16*)&model[i*2] = be16_to_cpu(buffer[27+i]);
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100702 model[size] = 0x00;
Kevin O'Connor9e881a32011-01-08 12:06:54 -0500703 nullTrailingSpace(model);
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500704 return model;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500705}
706
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500707// Common init code between ata and atapi
708static struct atadrive_s *
709init_atadrive(struct atadrive_s *dummy, u16 *buffer)
710{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500711 struct atadrive_s *adrive_g = malloc_fseg(sizeof(*adrive_g));
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500712 if (!adrive_g) {
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500713 warn_noalloc();
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500714 return NULL;
715 }
716 memset(adrive_g, 0, sizeof(*adrive_g));
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500717 adrive_g->chan_gf = dummy->chan_gf;
718 adrive_g->slave = dummy->slave;
719 adrive_g->drive.cntl_id = adrive_g->chan_gf->chanid * 2 + dummy->slave;
720 adrive_g->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
721 return adrive_g;
722}
723
Kevin O'Connor14021f22009-12-26 23:21:38 -0500724// Detect if the given drive is an atapi - initialize it if so.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500725static struct atadrive_s *
726init_drive_atapi(struct atadrive_s *dummy, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500727{
728 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connor14021f22009-12-26 23:21:38 -0500729 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_PACKET_DEVICE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500730 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400731 return NULL;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500732
733 // Success - setup as ATAPI.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500734 struct atadrive_s *adrive_g = init_atadrive(dummy, buffer);
735 if (!adrive_g)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400736 return NULL;
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400737 adrive_g->drive.type = DTYPE_ATA_ATAPI;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500738 adrive_g->drive.blksize = CDROM_SECTOR_SIZE;
739 adrive_g->drive.sectors = (u64)-1;
Kevin O'Connor42337662009-08-10 00:06:37 -0400740 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500741 char model[MAXMODEL+1];
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500742 char *desc = znprintf(MAXDESCSIZE
743 , "DVD/CD [ata%d-%d: %s ATAPI-%d %s]"
744 , adrive_g->chan_gf->chanid, adrive_g->slave
745 , ata_extract_model(model, MAXMODEL, buffer)
746 , ata_extract_version(buffer)
747 , (iscd ? "DVD/CD" : "Device"));
748 dprintf(1, "%s\n", desc);
Kevin O'Connor42337662009-08-10 00:06:37 -0400749
750 // fill cdidmap
Kevin O'Connor031ef552010-12-27 19:26:57 -0500751 if (iscd) {
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400752 int prio = bootprio_find_ata_device(adrive_g->chan_gf->pci_tmp,
Kevin O'Connor031ef552010-12-27 19:26:57 -0500753 adrive_g->chan_gf->chanid,
754 adrive_g->slave);
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500755 boot_add_cd(&adrive_g->drive, desc, prio);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500756 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500757
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500758 return adrive_g;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500759}
760
Kevin O'Connor14021f22009-12-26 23:21:38 -0500761// Detect if the given drive is a regular ata drive - initialize it if so.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500762static struct atadrive_s *
763init_drive_ata(struct atadrive_s *dummy, u16 *buffer)
Kevin O'Connorc1437612008-05-18 01:43:07 -0400764{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500765 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connor14021f22009-12-26 23:21:38 -0500766 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_DEVICE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400767 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400768 return NULL;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500769
770 // Success - setup as ATA.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500771 struct atadrive_s *adrive_g = init_atadrive(dummy, buffer);
772 if (!adrive_g)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400773 return NULL;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500774 adrive_g->drive.type = DTYPE_ATA;
775 adrive_g->drive.blksize = DISK_SECTOR_SIZE;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400776
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500777 adrive_g->drive.pchs.cylinders = buffer[1];
778 adrive_g->drive.pchs.heads = buffer[3];
779 adrive_g->drive.pchs.spt = buffer[6];
Kevin O'Connorc1437612008-05-18 01:43:07 -0400780
781 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500782 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
783 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400784 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500785 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500786 adrive_g->drive.sectors = sectors;
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500787 u64 adjsize = sectors >> 11;
788 char adjprefix = 'M';
789 if (adjsize >= (1 << 16)) {
790 adjsize >>= 10;
791 adjprefix = 'G';
792 }
793 char model[MAXMODEL+1];
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500794 char *desc = znprintf(MAXDESCSIZE
795 , "ata%d-%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
796 , adrive_g->chan_gf->chanid, adrive_g->slave
797 , ata_extract_model(model, MAXMODEL, buffer)
798 , ata_extract_version(buffer)
799 , (u32)adjsize, adjprefix);
800 dprintf(1, "%s\n", desc);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400801
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400802 int prio = bootprio_find_ata_device(adrive_g->chan_gf->pci_tmp,
Kevin O'Connor031ef552010-12-27 19:26:57 -0500803 adrive_g->chan_gf->chanid,
804 adrive_g->slave);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500805 // Register with bcv system.
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500806 boot_add_hd(&adrive_g->drive, desc, prio);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400807
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500808 return adrive_g;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400809}
810
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400811static u32 SpinupEnd;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400812
Kevin O'Connor14021f22009-12-26 23:21:38 -0500813// Wait for non-busy status and check for "floating bus" condition.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400814static int
Kevin O'Connora5826b52009-10-24 17:57:29 -0400815powerup_await_non_bsy(u16 base)
Kevin O'Connor425f2122009-04-18 12:23:00 -0400816{
817 u8 orstatus = 0;
818 u8 status;
819 for (;;) {
820 status = inb(base+ATA_CB_STAT);
821 if (!(status & ATA_CB_STAT_BSY))
822 break;
823 orstatus |= status;
824 if (orstatus == 0xff) {
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400825 dprintf(4, "powerup IDE floating\n");
Kevin O'Connor425f2122009-04-18 12:23:00 -0400826 return orstatus;
827 }
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400828 if (timer_check(SpinupEnd)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500829 warn_timeout();
Kevin O'Connor425f2122009-04-18 12:23:00 -0400830 return -1;
831 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400832 yield();
Kevin O'Connor425f2122009-04-18 12:23:00 -0400833 }
834 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
835 return status;
836}
837
Kevin O'Connor14021f22009-12-26 23:21:38 -0500838// Detect any drives attached to a given controller.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400839static void
Kevin O'Connora5826b52009-10-24 17:57:29 -0400840ata_detect(void *data)
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500841{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500842 struct ata_channel_s *chan_gf = data;
843 struct atadrive_s dummy;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400844 memset(&dummy, 0, sizeof(dummy));
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500845 dummy.chan_gf = chan_gf;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500846 // Device detection
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500847 int didreset = 0;
848 u8 slave;
849 for (slave=0; slave<=1; slave++) {
Kevin O'Connor425f2122009-04-18 12:23:00 -0400850 // Wait for not-bsy.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500851 u16 iobase1 = chan_gf->iobase1;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400852 int status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400853 if (status < 0)
854 continue;
855 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
856 outb(newdh, iobase1+ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400857 ndelay(400);
Kevin O'Connora5826b52009-10-24 17:57:29 -0400858 status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400859 if (status < 0)
860 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500861
Kevin O'Connor580e3322009-02-06 22:36:53 -0500862 // Check if ioport registers look valid.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400863 outb(newdh, iobase1+ATA_CB_DH);
864 u8 dh = inb(iobase1+ATA_CB_DH);
865 outb(0x55, iobase1+ATA_CB_SC);
866 outb(0xaa, iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400867 u8 sc = inb(iobase1+ATA_CB_SC);
868 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500869 dprintf(6, "ata_detect ata%d-%d: sc=%x sn=%x dh=%x\n"
870 , chan_gf->chanid, slave, sc, sn, dh);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400871 if (sc != 0x55 || sn != 0xaa || dh != newdh)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400872 continue;
873
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400874 // Prepare new drive.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500875 dummy.slave = slave;
Kevin O'Connorb1144362009-08-11 20:43:38 -0400876
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400877 // reset the channel
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500878 if (!didreset) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400879 ata_reset(&dummy);
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500880 didreset = 1;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500881 }
882
Kevin O'Connor580e3322009-02-06 22:36:53 -0500883 // check for ATAPI
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400884 u16 buffer[256];
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500885 struct atadrive_s *adrive_g = init_drive_atapi(&dummy, buffer);
886 if (!adrive_g) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400887 // Didn't find an ATAPI drive - look for ATA drive.
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400888 u8 st = inb(iobase1+ATA_CB_STAT);
889 if (!st)
890 // Status not set - can't be a valid drive.
891 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500892
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400893 // Wait for RDY.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400894 int ret = await_rdy(iobase1);
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400895 if (ret < 0)
896 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500897
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400898 // check for ATA.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500899 adrive_g = init_drive_ata(&dummy, buffer);
900 if (!adrive_g)
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400901 // No ATA drive found
902 continue;
903 }
Kevin O'Connor580e3322009-02-06 22:36:53 -0500904
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400905 u16 resetresult = buffer[93];
906 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
907 if (!slave && (resetresult & 0xdf61) == 0x4041)
908 // resetresult looks valid and device 0 is responding to
909 // device 1 requests - device 1 must not be present - skip
910 // detection.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500911 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500912 }
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500913}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400914
Kevin O'Connor14021f22009-12-26 23:21:38 -0500915// Initialize an ata controller and detect its drives.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400916static void
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400917init_controller(struct pci_device *pci, int irq
918 , u32 port1, u32 port2, u32 master)
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500919{
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400920 static int chanid = 0;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500921 struct ata_channel_s *chan_gf = malloc_fseg(sizeof(*chan_gf));
922 if (!chan_gf) {
923 warn_noalloc();
924 return;
925 }
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400926 chan_gf->chanid = chanid++;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500927 chan_gf->irq = irq;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400928 chan_gf->pci_bdf = pci ? pci->bdf : -1;
929 chan_gf->pci_tmp = pci;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500930 chan_gf->iobase1 = port1;
931 chan_gf->iobase2 = port2;
932 chan_gf->iomaster = master;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500933 dprintf(1, "ATA controller %d at %x/%x/%x (irq %d dev %x)\n"
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400934 , chanid, port1, port2, master, irq, chan_gf->pci_bdf);
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500935 run_thread(ata_detect, chan_gf);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500936}
937
Kevin O'Connor525219b2009-12-05 13:36:18 -0500938#define IRQ_ATA1 14
939#define IRQ_ATA2 15
940
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400941// Handle controllers on an ATA PCI device.
942static void
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400943init_pciata(struct pci_device *pci, u8 prog_if)
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400944{
Kevin O'Connor76b5e712011-06-21 22:52:51 -0400945 pci->have_driver = 1;
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400946 u16 bdf = pci->bdf;
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400947 u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400948 int master = 0;
949 if (CONFIG_ATA_DMA && prog_if & 0x80) {
950 // Check for bus-mastering.
951 u32 bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_4);
952 if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
953 master = bar & PCI_BASE_ADDRESS_IO_MASK;
954 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
955 }
956 }
957
958 u32 port1, port2, irq;
959 if (prog_if & 1) {
960 port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_0)
961 & PCI_BASE_ADDRESS_IO_MASK);
962 port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_1)
963 & PCI_BASE_ADDRESS_IO_MASK);
964 irq = pciirq;
965 } else {
966 port1 = PORT_ATA1_CMD_BASE;
967 port2 = PORT_ATA1_CTRL_BASE;
968 irq = IRQ_ATA1;
969 }
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400970 init_controller(pci, irq, port1, port2, master);
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400971
972 if (prog_if & 4) {
973 port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_2)
974 & PCI_BASE_ADDRESS_IO_MASK);
975 port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_3)
976 & PCI_BASE_ADDRESS_IO_MASK);
977 irq = pciirq;
978 } else {
979 port1 = PORT_ATA2_CMD_BASE;
980 port2 = PORT_ATA2_CTRL_BASE;
981 irq = IRQ_ATA2;
982 }
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400983 init_controller(pci, irq, port1, port2, master ? master + 8 : 0);
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400984}
985
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400986static void
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400987found_genericata(struct pci_device *pci, void *arg)
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400988{
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400989 init_pciata(pci, pci->prog_if);
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400990}
991
992static void
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400993found_compatibleahci(struct pci_device *pci, void *arg)
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400994{
995 if (CONFIG_AHCI)
996 // Already handled directly via native ahci interface.
997 return;
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400998 init_pciata(pci, 0x8f);
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400999}
1000
Kevin O'Connor927d16e2011-06-19 09:43:20 -04001001static const struct pci_device_id pci_ata_tbl[] = {
Kevin O'Connorb9457ec2011-06-19 10:03:08 -04001002 PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE
1003 , found_genericata),
1004 PCI_DEVICE(PCI_VENDOR_ID_ATI, 0x4391, found_compatibleahci),
Kevin O'Connor927d16e2011-06-19 09:43:20 -04001005 PCI_DEVICE_END,
1006};
1007
Kevin O'Connor14021f22009-12-26 23:21:38 -05001008// Locate and init ata controllers.
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001009static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -05001010ata_scan(void)
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001011{
Kevin O'Connorb98a4b12013-06-08 21:53:36 -04001012 if (CONFIG_QEMU && hlist_empty(&PCIDevices)) {
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001013 // No PCI devices found - probably a QEMU "-M isapc" machine.
1014 // Try using ISA ports for ATA controllers.
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -04001015 init_controller(NULL, IRQ_ATA1
Kevin O'Connor14021f22009-12-26 23:21:38 -05001016 , PORT_ATA1_CMD_BASE, PORT_ATA1_CTRL_BASE, 0);
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -04001017 init_controller(NULL, IRQ_ATA2
Kevin O'Connor14021f22009-12-26 23:21:38 -05001018 , PORT_ATA2_CMD_BASE, PORT_ATA2_CTRL_BASE, 0);
Kevin O'Connor3f3e58d2011-06-20 22:20:43 -04001019 return;
1020 }
1021
1022 // Scan PCI bus for ATA adapters
1023 struct pci_device *pci;
1024 foreachpci(pci) {
Kevin O'Connor278b19f2011-06-21 22:41:15 -04001025 pci_init_device(pci_ata_tbl, pci, NULL);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001026 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001027}
1028
1029void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001030ata_setup(void)
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001031{
Kevin O'Connor59c75742010-02-13 18:49:24 -05001032 ASSERT32FLAT();
Kevin O'Connorc1437612008-05-18 01:43:07 -04001033 if (!CONFIG_ATA)
1034 return;
1035
Kevin O'Connor35192dd2008-06-08 19:18:33 -04001036 dprintf(3, "init hard drives\n");
Kevin O'Connora5826b52009-10-24 17:57:29 -04001037
Kevin O'Connor018bdd72013-07-20 18:22:57 -04001038 SpinupEnd = timer_calc(IDE_TIMEOUT);
Kevin O'Connord83c87b2013-01-21 01:14:12 -05001039 ata_scan();
Kevin O'Connorc1437612008-05-18 01:43:07 -04001040
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001041 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -04001042
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -04001043 enable_hwirq(14, FUNC16(entry_76));
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001044}