blob: 34ba119d7d1ce67bad8b4db3b53a97bf6b23b896 [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'Connor2d2fa312013-09-14 21:55:26 -04008#include "ata.h" // ATA_CB_STAT
9#include "biosvar.h" // GET_GLOBAL
Kevin O'Connor135f3f62013-09-14 23:57:26 -040010#include "block.h" // struct drive_s
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040011#include "blockcmd.h" // CDB_CMD_READ_10
Kevin O'Connorb3064592012-08-14 21:20:10 -040012#include "byteorder.h" // be16_to_cpu
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050013#include "cmos.h" // inb_cmos
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040014#include "ioport.h" // inb
15#include "malloc.h" // malloc_fseg
16#include "output.h" // dprintf
Kevin O'Connor3f3e58d2011-06-20 22:20:43 -040017#include "pci.h" // foreachpci
Kevin O'Connor2ed2f582008-11-08 15:53:36 -050018#include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
19#include "pci_regs.h" // PCI_INTERRUPT_LINE
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040020#include "pic.h" // enable_hwirq
Kevin O'Connor3df600b2013-09-14 19:28:55 -040021#include "stacks.h" // yield
Kevin O'Connor135f3f62013-09-14 23:57:26 -040022#include "std/disk.h" // DISK_RET_SUCCESS
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040023#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040024#include "util.h" // timer_calc
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050025
Kevin O'Connor425f2122009-04-18 12:23:00 -040026#define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050027
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050028
Kevin O'Connora6b9f712008-03-29 12:53:57 -040029/****************************************************************
30 * Helper functions
31 ****************************************************************/
32
33// Wait for the specified ide state
Kevin O'Connor580e3322009-02-06 22:36:53 -050034static inline int
35await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050036{
Kevin O'Connor018bdd72013-07-20 18:22:57 -040037 u32 end = timer_calc(timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050038 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -040039 u8 status = inb(base+ATA_CB_STAT);
Kevin O'Connor580e3322009-02-06 22:36:53 -050040 if ((status & mask) == flags)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040041 return status;
Kevin O'Connor018bdd72013-07-20 18:22:57 -040042 if (timer_check(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050043 warn_timeout();
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050044 return -1;
45 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -040046 yield();
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050047 }
Kevin O'Connor580e3322009-02-06 22:36:53 -050048}
49
50// Wait for the device to be not-busy.
51static int
52await_not_bsy(u16 base)
53{
54 return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
55}
56
57// Wait for the device to be ready.
58static int
59await_rdy(u16 base)
60{
61 return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050062}
63
Kevin O'Connora6b9f712008-03-29 12:53:57 -040064// Wait for ide state - pauses for one ata cycle first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050065static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050066pause_await_not_bsy(u16 iobase1, u16 iobase2)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040067{
68 // Wait one PIO transfer cycle.
69 inb(iobase2 + ATA_CB_ASTAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050070
Kevin O'Connor580e3322009-02-06 22:36:53 -050071 return await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -040072}
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050073
Kevin O'Connoref2822a2008-06-07 15:23:11 -040074// Wait for ide state - pause for 400ns first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050075static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050076ndelay_await_not_bsy(u16 iobase1)
Kevin O'Connoref2822a2008-06-07 15:23:11 -040077{
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050078 ndelay(400);
Kevin O'Connor580e3322009-02-06 22:36:53 -050079 return await_not_bsy(iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -040080}
81
Kevin O'Connora6b9f712008-03-29 12:53:57 -040082// Reset a drive
Kevin O'Connorb1144362009-08-11 20:43:38 -040083static void
Kevin O'Connor8f469b92010-02-28 01:28:11 -050084ata_reset(struct atadrive_s *adrive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050085{
Kevin O'Connor8f469b92010-02-28 01:28:11 -050086 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
87 u8 slave = GET_GLOBAL(adrive_g->slave);
88 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
89 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050090
Kevin O'Connor8f469b92010-02-28 01:28:11 -050091 dprintf(6, "ata_reset drive=%p\n", &adrive_g->drive);
Kevin O'Connor580e3322009-02-06 22:36:53 -050092 // Pulse SRST
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050093 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 -050094 udelay(5);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050095 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
Kevin O'Connor10ad7992009-10-24 11:06:08 -040096 msleep(2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050097
Kevin O'Connor580e3322009-02-06 22:36:53 -050098 // wait for device to become not busy.
99 int status = await_not_bsy(iobase1);
100 if (status < 0)
101 goto done;
102 if (slave) {
103 // Change device.
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400104 u32 end = timer_calc(IDE_TIMEOUT);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500105 for (;;) {
106 outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400107 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500108 if (status < 0)
109 goto done;
110 if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
111 break;
112 // Change drive request failed to take effect - retry.
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400113 if (timer_check(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500114 warn_timeout();
Kevin O'Connor580e3322009-02-06 22:36:53 -0500115 goto done;
116 }
117 }
Kevin O'Connorf5624d22009-08-18 22:17:57 -0400118 } else {
119 // QEMU doesn't reset dh on reset, so set it explicitly.
120 outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
Kevin O'Connor2e3eeeb2008-06-12 22:29:30 -0400121 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500122
Kevin O'Connor580e3322009-02-06 22:36:53 -0500123 // On a user-reset request, wait for RDY if it is an ATA device.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500124 u8 type=GET_GLOBAL(adrive_g->drive.type);
Kevin O'Connor42337662009-08-10 00:06:37 -0400125 if (type == DTYPE_ATA)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500126 status = await_rdy(iobase1);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400127
Kevin O'Connor580e3322009-02-06 22:36:53 -0500128done:
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500129 // Enable interrupts
130 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500131
132 dprintf(6, "ata_reset exit status=%x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500133}
134
Kevin O'Connor14021f22009-12-26 23:21:38 -0500135// Check for drive RDY for 16bit interface command.
Kevin O'Connor42337662009-08-10 00:06:37 -0400136static int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500137isready(struct atadrive_s *adrive_g)
Kevin O'Connor42337662009-08-10 00:06:37 -0400138{
139 // Read the status from controller
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500140 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
141 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400142 u8 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400143 if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY)
144 return DISK_RET_SUCCESS;
145 return DISK_RET_ENOTREADY;
Kevin O'Connor42337662009-08-10 00:06:37 -0400146}
147
Kevin O'Connoree55c762008-05-13 00:18:20 -0400148
149/****************************************************************
150 * ATA send command
151 ****************************************************************/
152
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400153struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400154 u8 feature;
155 u8 sector_count;
156 u8 lba_low;
157 u8 lba_mid;
158 u8 lba_high;
159 u8 device;
160 u8 command;
161
Kevin O'Connor14021f22009-12-26 23:21:38 -0500162 u8 feature2;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400163 u8 sector_count2;
164 u8 lba_low2;
165 u8 lba_mid2;
166 u8 lba_high2;
167};
168
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400169// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400170static int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500171send_cmd(struct atadrive_s *adrive_g, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500172{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500173 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
174 u8 slave = GET_GLOBAL(adrive_g->slave);
175 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400176
177 // Select device
Kevin O'Connor580e3322009-02-06 22:36:53 -0500178 int status = await_not_bsy(iobase1);
179 if (status < 0)
180 return status;
181 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
182 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
183 u8 olddh = inb(iobase1 + ATA_CB_DH);
184 outb(newdh, iobase1 + ATA_CB_DH);
185 if ((olddh ^ newdh) & (1<<4)) {
186 // Was a device change - wait for device to become not busy.
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400187 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500188 if (status < 0)
189 return status;
190 }
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400191
Kevin O'Connor14021f22009-12-26 23:21:38 -0500192 // Check for ATA_CMD_(READ|WRITE)_(SECTORS|DMA)_EXT commands.
193 if ((cmd->command & ~0x11) == ATA_CMD_READ_SECTORS_EXT) {
194 outb(cmd->feature2, iobase1 + ATA_CB_FR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400195 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
196 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
197 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
198 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500199 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400200 outb(cmd->feature, iobase1 + ATA_CB_FR);
201 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
202 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
203 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
204 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400205 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500206
Kevin O'Connor14021f22009-12-26 23:21:38 -0500207 return 0;
208}
209
210// Wait for data after calling 'send_cmd'.
211static int
212ata_wait_data(u16 iobase1)
213{
214 int status = ndelay_await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400215 if (status < 0)
216 return status;
217
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500218 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500219 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
220 , status, inb(iobase1 + ATA_CB_ERR));
Kevin O'Connora05223c2008-06-28 12:15:57 -0400221 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400222 }
223 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500224 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400225 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500226 }
227
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400228 return 0;
229}
230
Kevin O'Connor14021f22009-12-26 23:21:38 -0500231// Send an ata command that does not transfer any further data.
232int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500233ata_cmd_nondata(struct atadrive_s *adrive_g, struct ata_pio_command *cmd)
Kevin O'Connor14021f22009-12-26 23:21:38 -0500234{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500235 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
236 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
237 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500238
239 // Disable interrupts
240 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
241
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500242 int ret = send_cmd(adrive_g, cmd);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500243 if (ret)
244 goto fail;
245 ret = ndelay_await_not_bsy(iobase1);
246 if (ret < 0)
247 goto fail;
248
249 if (ret & ATA_CB_STAT_ERR) {
250 dprintf(6, "nondata cmd : read error (status=%02x err=%02x)\n"
251 , ret, inb(iobase1 + ATA_CB_ERR));
252 ret = -4;
253 goto fail;
254 }
255 if (ret & ATA_CB_STAT_DRQ) {
256 dprintf(6, "nondata cmd : DRQ set (status %02x)\n", ret);
257 ret = -5;
258 goto fail;
259 }
260
261fail:
262 // Enable interrupts
263 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
264
265 return ret;
266}
267
Kevin O'Connoree55c762008-05-13 00:18:20 -0400268
269/****************************************************************
Kevin O'Connor14021f22009-12-26 23:21:38 -0500270 * ATA PIO transfers
Kevin O'Connoree55c762008-05-13 00:18:20 -0400271 ****************************************************************/
272
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400273// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400274// 'op->drive_g'.
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400275static int
Kevin O'Connor14021f22009-12-26 23:21:38 -0500276ata_pio_transfer(struct disk_op_s *op, int iswrite, int blocksize)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400277{
Kevin O'Connor14021f22009-12-26 23:21:38 -0500278 dprintf(16, "ata_pio_transfer id=%p write=%d count=%d bs=%d buf=%p\n"
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400279 , op->drive_g, iswrite, op->count, blocksize, op->buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400280
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500281 struct atadrive_s *adrive_g = container_of(
282 op->drive_g, struct atadrive_s, drive);
283 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
284 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
285 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400286 int count = op->count;
287 void *buf_fl = op->buf_fl;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400288 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400289 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400290 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400291 // Write data to controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400292 dprintf(16, "Write sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500293 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400294 outsl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400295 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400296 outsw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500297 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400298 // Read data from controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400299 dprintf(16, "Read sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500300 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400301 insl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400302 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400303 insw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400304 }
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400305 buf_fl += blocksize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400306
Kevin O'Connor580e3322009-02-06 22:36:53 -0500307 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400308 if (status < 0) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400309 // Error
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400310 op->count -= count;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400311 return status;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400312 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400313
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400314 count--;
315 if (!count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400316 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500317 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
318 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500319 dprintf(6, "ata_pio_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500320 , status);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400321 op->count -= count;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400322 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500323 }
324 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400325
Kevin O'Connor580e3322009-02-06 22:36:53 -0500326 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
327 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400328 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400329 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500330 if (status != 0) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500331 dprintf(6, "ata_pio_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400332 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400333 }
334
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500335 return 0;
336}
337
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400338
339/****************************************************************
Kevin O'Connor14021f22009-12-26 23:21:38 -0500340 * ATA DMA transfers
341 ****************************************************************/
342
343#define BM_CMD 0
344#define BM_CMD_MEMWRITE 0x08
345#define BM_CMD_START 0x01
346#define BM_STATUS 2
347#define BM_STATUS_IRQ 0x04
348#define BM_STATUS_ERROR 0x02
349#define BM_STATUS_ACTIVE 0x01
350#define BM_TABLE 4
351
352struct sff_dma_prd {
353 u32 buf_fl;
354 u32 count;
355};
356
357// Check if DMA available and setup transfer if so.
358static int
359ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize)
360{
Kevin O'Connor46b82622012-05-13 12:10:30 -0400361 ASSERT16();
Kevin O'Connor4d079022010-01-17 12:58:47 -0500362 if (! CONFIG_ATA_DMA)
363 return -1;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500364 u32 dest = (u32)op->buf_fl;
365 if (dest & 1)
366 // Need minimum alignment of 1.
367 return -1;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500368 struct atadrive_s *adrive_g = container_of(
369 op->drive_g, struct atadrive_s, drive);
370 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
371 u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500372 if (! iomaster)
373 return -1;
374 u32 bytes = op->count * blocksize;
375 if (! bytes)
376 return -1;
377
378 // Build PRD dma structure.
Kevin O'Connor46b82622012-05-13 12:10:30 -0400379 struct sff_dma_prd *dma = MAKE_FLATPTR(SEG_LOW, ExtraStack);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500380 struct sff_dma_prd *origdma = dma;
381 while (bytes) {
382 if (dma >= &origdma[16])
383 // Too many descriptors..
384 return -1;
385 u32 count = bytes;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500386 u32 max = 0x10000 - (dest & 0xffff);
387 if (count > max)
388 count = max;
389
Kevin O'Connor1e3bd4f2012-05-24 23:56:19 -0400390 SET_LOWFLAT(dma->buf_fl, dest);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500391 bytes -= count;
392 if (!bytes)
393 // Last descriptor.
394 count |= 1<<31;
395 dprintf(16, "dma@%p: %08x %08x\n", dma, dest, count);
396 dest += count;
Kevin O'Connor1e3bd4f2012-05-24 23:56:19 -0400397 SET_LOWFLAT(dma->count, count);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500398 dma++;
399 }
400
401 // Program bus-master controller.
402 outl((u32)origdma, iomaster + BM_TABLE);
403 u8 oldcmd = inb(iomaster + BM_CMD) & ~(BM_CMD_MEMWRITE|BM_CMD_START);
404 outb(oldcmd | (iswrite ? 0x00 : BM_CMD_MEMWRITE), iomaster + BM_CMD);
405 outb(BM_STATUS_ERROR|BM_STATUS_IRQ, iomaster + BM_STATUS);
406
407 return 0;
408}
409
410// Transfer data using DMA.
411static int
412ata_dma_transfer(struct disk_op_s *op)
413{
Kevin O'Connor4d079022010-01-17 12:58:47 -0500414 if (! CONFIG_ATA_DMA)
415 return -1;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500416 dprintf(16, "ata_dma_transfer id=%p buf=%p\n", op->drive_g, op->buf_fl);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500417
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500418 struct atadrive_s *adrive_g = container_of(
419 op->drive_g, struct atadrive_s, drive);
420 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
421 u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500422
423 // Start bus-master controller.
424 u8 oldcmd = inb(iomaster + BM_CMD);
425 outb(oldcmd | BM_CMD_START, iomaster + BM_CMD);
426
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400427 u32 end = timer_calc(IDE_TIMEOUT);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500428 u8 status;
429 for (;;) {
430 status = inb(iomaster + BM_STATUS);
431 if (status & BM_STATUS_IRQ)
432 break;
433 // Transfer in progress
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400434 if (timer_check(end)) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500435 // Timeout.
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500436 warn_timeout();
Kevin O'Connor14021f22009-12-26 23:21:38 -0500437 break;
438 }
439 yield();
440 }
441 outb(oldcmd & ~BM_CMD_START, iomaster + BM_CMD);
442
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500443 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
444 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500445 int idestatus = pause_await_not_bsy(iobase1, iobase2);
446
447 if ((status & (BM_STATUS_IRQ|BM_STATUS_ACTIVE)) == BM_STATUS_IRQ
448 && idestatus >= 0x00
449 && (idestatus & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
450 | ATA_CB_STAT_ERR)) == 0x00)
451 // Success.
452 return 0;
453
454 dprintf(6, "IDE DMA error (dma=%x ide=%x/%x/%x)\n", status, idestatus
455 , inb(iobase2 + ATA_CB_ASTAT), inb(iobase1 + ATA_CB_ERR));
456 op->count = 0;
457 return -1;
458}
459
460
461/****************************************************************
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400462 * ATA hard drive functions
463 ****************************************************************/
464
Kevin O'Connor14021f22009-12-26 23:21:38 -0500465// Transfer data to harddrive using PIO protocol.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400466static int
Kevin O'Connor14021f22009-12-26 23:21:38 -0500467ata_pio_cmd_data(struct disk_op_s *op, int iswrite, struct ata_pio_command *cmd)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400468{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500469 struct atadrive_s *adrive_g = container_of(
470 op->drive_g, struct atadrive_s, drive);
471 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
472 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
473 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400474
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500475 // Disable interrupts
476 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
477
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500478 int ret = send_cmd(adrive_g, cmd);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400479 if (ret)
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500480 goto fail;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500481 ret = ata_wait_data(iobase1);
482 if (ret)
483 goto fail;
484 ret = ata_pio_transfer(op, iswrite, DISK_SECTOR_SIZE);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500485
486fail:
487 // Enable interrupts
488 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
489 return ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400490}
491
Kevin O'Connor14021f22009-12-26 23:21:38 -0500492// Transfer data to harddrive using DMA protocol.
493static int
494ata_dma_cmd_data(struct disk_op_s *op, struct ata_pio_command *cmd)
495{
Kevin O'Connor4d079022010-01-17 12:58:47 -0500496 if (! CONFIG_ATA_DMA)
497 return -1;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500498 struct atadrive_s *adrive_g = container_of(
499 op->drive_g, struct atadrive_s, drive);
500 int ret = send_cmd(adrive_g, cmd);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500501 if (ret)
502 return ret;
503 return ata_dma_transfer(op);
504}
505
506// Read/write count blocks from a harddrive.
507static int
508ata_readwrite(struct disk_op_s *op, int iswrite)
509{
510 u64 lba = op->lba;
511
512 int usepio = ata_try_dma(op, iswrite, DISK_SECTOR_SIZE);
513
514 struct ata_pio_command cmd;
515 memset(&cmd, 0, sizeof(cmd));
516
517 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
518 cmd.sector_count2 = op->count >> 8;
519 cmd.lba_low2 = lba >> 24;
520 cmd.lba_mid2 = lba >> 32;
521 cmd.lba_high2 = lba >> 40;
522 lba &= 0xffffff;
523
524 if (usepio)
525 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS_EXT
526 : ATA_CMD_READ_SECTORS_EXT);
527 else
528 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
529 : ATA_CMD_READ_DMA_EXT);
530 } else {
531 if (usepio)
532 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS
533 : ATA_CMD_READ_SECTORS);
534 else
535 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA
536 : ATA_CMD_READ_DMA);
537 }
538
539 cmd.sector_count = op->count;
540 cmd.lba_low = lba;
541 cmd.lba_mid = lba >> 8;
542 cmd.lba_high = lba >> 16;
543 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
544
545 int ret;
546 if (usepio)
547 ret = ata_pio_cmd_data(op, iswrite, &cmd);
548 else
549 ret = ata_dma_cmd_data(op, &cmd);
550 if (ret)
551 return DISK_RET_EBADTRACK;
552 return DISK_RET_SUCCESS;
553}
554
555// 16bit command demuxer for ATA harddrives.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400556int
557process_ata_op(struct disk_op_s *op)
558{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400559 if (!CONFIG_ATA)
560 return 0;
561
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400562 struct atadrive_s *adrive_g = container_of(
563 op->drive_g, struct atadrive_s, drive);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400564 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400565 case CMD_READ:
Kevin O'Connor14021f22009-12-26 23:21:38 -0500566 return ata_readwrite(op, 0);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400567 case CMD_WRITE:
Kevin O'Connor14021f22009-12-26 23:21:38 -0500568 return ata_readwrite(op, 1);
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400569 case CMD_RESET:
570 ata_reset(adrive_g);
571 return DISK_RET_SUCCESS;
572 case CMD_ISREADY:
573 return isready(adrive_g);
574 case CMD_FORMAT:
575 case CMD_VERIFY:
576 case CMD_SEEK:
577 return DISK_RET_SUCCESS;
Kevin O'Connor42337662009-08-10 00:06:37 -0400578 default:
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400579 op->count = 0;
580 return DISK_RET_EPARAM;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400581 }
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400582}
583
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400584
585/****************************************************************
586 * ATAPI functions
587 ****************************************************************/
588
Kevin O'Connor7d700252010-02-15 11:56:07 -0500589#define CDROM_CDB_SIZE 12
590
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400591// Low-level atapi command transmit function.
Kevin O'Connor7d700252010-02-15 11:56:07 -0500592int
593atapi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500594{
Kevin O'Connor80c2b6e2010-12-05 12:52:02 -0500595 if (! CONFIG_ATA)
596 return 0;
597
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500598 struct atadrive_s *adrive_g = container_of(
599 op->drive_g, struct atadrive_s, drive);
600 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
601 u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
602 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500603
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400604 struct ata_pio_command cmd;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500605 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400606 cmd.lba_mid = blocksize;
607 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400608 cmd.command = ATA_CMD_PACKET;
609
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500610 // Disable interrupts
611 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
612
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500613 int ret = send_cmd(adrive_g, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400614 if (ret)
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500615 goto fail;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500616 ret = ata_wait_data(iobase1);
617 if (ret)
618 goto fail;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500619
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400620 // Send command to device
Kevin O'Connor7d700252010-02-15 11:56:07 -0500621 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cdbcmd), CDROM_CDB_SIZE / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500622
Kevin O'Connor580e3322009-02-06 22:36:53 -0500623 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500624 if (status < 0) {
625 ret = status;
626 goto fail;
627 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400628
Kevin O'Connor580e3322009-02-06 22:36:53 -0500629 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connorb30c4002009-05-05 21:47:20 -0400630 u8 err = inb(iobase1 + ATA_CB_ERR);
631 // skip "Not Ready"
632 if (err != 0x20)
633 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
634 , status, err);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500635 ret = -2;
636 goto fail;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500637 }
Paolo Bonzini39d59892012-03-19 11:41:09 +0100638 if (blocksize) {
639 if (!(status & ATA_CB_STAT_DRQ)) {
640 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
641 ret = -3;
642 goto fail;
643 }
Kevin O'Connor580e3322009-02-06 22:36:53 -0500644
Paolo Bonzini39d59892012-03-19 11:41:09 +0100645 ret = ata_pio_transfer(op, 0, blocksize);
646 }
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500647
648fail:
649 // Enable interrupts
650 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor76977b22010-02-17 01:01:32 -0500651 if (ret)
652 return DISK_RET_EBADTRACK;
653 return DISK_RET_SUCCESS;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400654}
655
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400656
657/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500658 * ATA detect and init
659 ****************************************************************/
660
Kevin O'Connor14021f22009-12-26 23:21:38 -0500661// Send an identify device or identify device packet command.
662static int
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500663send_ata_identity(struct atadrive_s *adrive_g, u16 *buffer, int command)
Kevin O'Connor14021f22009-12-26 23:21:38 -0500664{
665 memset(buffer, 0, DISK_SECTOR_SIZE);
666
667 struct disk_op_s dop;
668 memset(&dop, 0, sizeof(dop));
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500669 dop.drive_g = &adrive_g->drive;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500670 dop.count = 1;
671 dop.lba = 1;
672 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
673
674 struct ata_pio_command cmd;
675 memset(&cmd, 0, sizeof(cmd));
676 cmd.command = command;
677
678 return ata_pio_cmd_data(&dop, 0, &cmd);
679}
680
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400681// Extract the ATA/ATAPI version info.
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100682int
683ata_extract_version(u16 *buffer)
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400684{
685 // Extract ATA/ATAPI version.
686 u16 ataversion = buffer[80];
687 u8 version;
688 for (version=15; version>0; version--)
689 if (ataversion & (1<<version))
690 break;
691 return version;
692}
693
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500694#define MAXMODEL 40
Kevin O'Connor0a924122009-02-08 19:43:47 -0500695
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500696// Extract the ATA/ATAPI model info.
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100697char *
698ata_extract_model(char *model, u32 size, u16 *buffer)
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500699{
Kevin O'Connor0a924122009-02-08 19:43:47 -0500700 // Read model name
701 int i;
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100702 for (i=0; i<size/2; i++)
Kevin O'Connorb3064592012-08-14 21:20:10 -0400703 *(u16*)&model[i*2] = be16_to_cpu(buffer[27+i]);
Gerd Hoffmann54fa8ec2010-11-29 09:42:12 +0100704 model[size] = 0x00;
Kevin O'Connor9e881a32011-01-08 12:06:54 -0500705 nullTrailingSpace(model);
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500706 return model;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500707}
708
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500709// Common init code between ata and atapi
710static struct atadrive_s *
711init_atadrive(struct atadrive_s *dummy, u16 *buffer)
712{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500713 struct atadrive_s *adrive_g = malloc_fseg(sizeof(*adrive_g));
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500714 if (!adrive_g) {
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500715 warn_noalloc();
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500716 return NULL;
717 }
718 memset(adrive_g, 0, sizeof(*adrive_g));
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500719 adrive_g->chan_gf = dummy->chan_gf;
720 adrive_g->slave = dummy->slave;
721 adrive_g->drive.cntl_id = adrive_g->chan_gf->chanid * 2 + dummy->slave;
722 adrive_g->drive.removable = (buffer[0] & 0x80) ? 1 : 0;
723 return adrive_g;
724}
725
Kevin O'Connor14021f22009-12-26 23:21:38 -0500726// Detect if the given drive is an atapi - initialize it if so.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500727static struct atadrive_s *
728init_drive_atapi(struct atadrive_s *dummy, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500729{
730 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connor14021f22009-12-26 23:21:38 -0500731 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_PACKET_DEVICE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500732 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400733 return NULL;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500734
735 // Success - setup as ATAPI.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500736 struct atadrive_s *adrive_g = init_atadrive(dummy, buffer);
737 if (!adrive_g)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400738 return NULL;
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400739 adrive_g->drive.type = DTYPE_ATA_ATAPI;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500740 adrive_g->drive.blksize = CDROM_SECTOR_SIZE;
741 adrive_g->drive.sectors = (u64)-1;
Kevin O'Connor42337662009-08-10 00:06:37 -0400742 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500743 char model[MAXMODEL+1];
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500744 char *desc = znprintf(MAXDESCSIZE
745 , "DVD/CD [ata%d-%d: %s ATAPI-%d %s]"
746 , adrive_g->chan_gf->chanid, adrive_g->slave
747 , ata_extract_model(model, MAXMODEL, buffer)
748 , ata_extract_version(buffer)
749 , (iscd ? "DVD/CD" : "Device"));
750 dprintf(1, "%s\n", desc);
Kevin O'Connor42337662009-08-10 00:06:37 -0400751
752 // fill cdidmap
Kevin O'Connor031ef552010-12-27 19:26:57 -0500753 if (iscd) {
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400754 int prio = bootprio_find_ata_device(adrive_g->chan_gf->pci_tmp,
Kevin O'Connor031ef552010-12-27 19:26:57 -0500755 adrive_g->chan_gf->chanid,
756 adrive_g->slave);
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500757 boot_add_cd(&adrive_g->drive, desc, prio);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500758 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500759
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500760 return adrive_g;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500761}
762
Kevin O'Connor14021f22009-12-26 23:21:38 -0500763// Detect if the given drive is a regular ata drive - initialize it if so.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500764static struct atadrive_s *
765init_drive_ata(struct atadrive_s *dummy, u16 *buffer)
Kevin O'Connorc1437612008-05-18 01:43:07 -0400766{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500767 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connor14021f22009-12-26 23:21:38 -0500768 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_DEVICE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400769 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400770 return NULL;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500771
772 // Success - setup as ATA.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500773 struct atadrive_s *adrive_g = init_atadrive(dummy, buffer);
774 if (!adrive_g)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400775 return NULL;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500776 adrive_g->drive.type = DTYPE_ATA;
777 adrive_g->drive.blksize = DISK_SECTOR_SIZE;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400778
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500779 adrive_g->drive.pchs.cylinders = buffer[1];
780 adrive_g->drive.pchs.heads = buffer[3];
781 adrive_g->drive.pchs.spt = buffer[6];
Kevin O'Connorc1437612008-05-18 01:43:07 -0400782
783 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500784 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
785 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400786 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500787 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500788 adrive_g->drive.sectors = sectors;
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500789 u64 adjsize = sectors >> 11;
790 char adjprefix = 'M';
791 if (adjsize >= (1 << 16)) {
792 adjsize >>= 10;
793 adjprefix = 'G';
794 }
795 char model[MAXMODEL+1];
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500796 char *desc = znprintf(MAXDESCSIZE
797 , "ata%d-%d: %s ATA-%d Hard-Disk (%u %ciBytes)"
798 , adrive_g->chan_gf->chanid, adrive_g->slave
799 , ata_extract_model(model, MAXMODEL, buffer)
800 , ata_extract_version(buffer)
801 , (u32)adjsize, adjprefix);
802 dprintf(1, "%s\n", desc);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400803
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400804 int prio = bootprio_find_ata_device(adrive_g->chan_gf->pci_tmp,
Kevin O'Connor031ef552010-12-27 19:26:57 -0500805 adrive_g->chan_gf->chanid,
806 adrive_g->slave);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500807 // Register with bcv system.
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500808 boot_add_hd(&adrive_g->drive, desc, prio);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400809
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500810 return adrive_g;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400811}
812
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400813static u32 SpinupEnd;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400814
Kevin O'Connor14021f22009-12-26 23:21:38 -0500815// Wait for non-busy status and check for "floating bus" condition.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400816static int
Kevin O'Connora5826b52009-10-24 17:57:29 -0400817powerup_await_non_bsy(u16 base)
Kevin O'Connor425f2122009-04-18 12:23:00 -0400818{
819 u8 orstatus = 0;
820 u8 status;
821 for (;;) {
822 status = inb(base+ATA_CB_STAT);
823 if (!(status & ATA_CB_STAT_BSY))
824 break;
825 orstatus |= status;
826 if (orstatus == 0xff) {
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400827 dprintf(4, "powerup IDE floating\n");
Kevin O'Connor425f2122009-04-18 12:23:00 -0400828 return orstatus;
829 }
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400830 if (timer_check(SpinupEnd)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500831 warn_timeout();
Kevin O'Connor425f2122009-04-18 12:23:00 -0400832 return -1;
833 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400834 yield();
Kevin O'Connor425f2122009-04-18 12:23:00 -0400835 }
836 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
837 return status;
838}
839
Kevin O'Connor14021f22009-12-26 23:21:38 -0500840// Detect any drives attached to a given controller.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400841static void
Kevin O'Connora5826b52009-10-24 17:57:29 -0400842ata_detect(void *data)
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500843{
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500844 struct ata_channel_s *chan_gf = data;
845 struct atadrive_s dummy;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400846 memset(&dummy, 0, sizeof(dummy));
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500847 dummy.chan_gf = chan_gf;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500848 // Device detection
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500849 int didreset = 0;
850 u8 slave;
851 for (slave=0; slave<=1; slave++) {
Kevin O'Connor425f2122009-04-18 12:23:00 -0400852 // Wait for not-bsy.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500853 u16 iobase1 = chan_gf->iobase1;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400854 int status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400855 if (status < 0)
856 continue;
857 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
858 outb(newdh, iobase1+ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400859 ndelay(400);
Kevin O'Connora5826b52009-10-24 17:57:29 -0400860 status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400861 if (status < 0)
862 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500863
Kevin O'Connor580e3322009-02-06 22:36:53 -0500864 // Check if ioport registers look valid.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400865 outb(newdh, iobase1+ATA_CB_DH);
866 u8 dh = inb(iobase1+ATA_CB_DH);
867 outb(0x55, iobase1+ATA_CB_SC);
868 outb(0xaa, iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400869 u8 sc = inb(iobase1+ATA_CB_SC);
870 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500871 dprintf(6, "ata_detect ata%d-%d: sc=%x sn=%x dh=%x\n"
872 , chan_gf->chanid, slave, sc, sn, dh);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400873 if (sc != 0x55 || sn != 0xaa || dh != newdh)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400874 continue;
875
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400876 // Prepare new drive.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500877 dummy.slave = slave;
Kevin O'Connorb1144362009-08-11 20:43:38 -0400878
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400879 // reset the channel
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500880 if (!didreset) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400881 ata_reset(&dummy);
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500882 didreset = 1;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500883 }
884
Kevin O'Connor580e3322009-02-06 22:36:53 -0500885 // check for ATAPI
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400886 u16 buffer[256];
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500887 struct atadrive_s *adrive_g = init_drive_atapi(&dummy, buffer);
888 if (!adrive_g) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400889 // Didn't find an ATAPI drive - look for ATA drive.
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400890 u8 st = inb(iobase1+ATA_CB_STAT);
891 if (!st)
892 // Status not set - can't be a valid drive.
893 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500894
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400895 // Wait for RDY.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400896 int ret = await_rdy(iobase1);
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400897 if (ret < 0)
898 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500899
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400900 // check for ATA.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500901 adrive_g = init_drive_ata(&dummy, buffer);
902 if (!adrive_g)
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400903 // No ATA drive found
904 continue;
905 }
Kevin O'Connor580e3322009-02-06 22:36:53 -0500906
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400907 u16 resetresult = buffer[93];
908 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
909 if (!slave && (resetresult & 0xdf61) == 0x4041)
910 // resetresult looks valid and device 0 is responding to
911 // device 1 requests - device 1 must not be present - skip
912 // detection.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500913 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500914 }
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500915}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400916
Kevin O'Connor14021f22009-12-26 23:21:38 -0500917// Initialize an ata controller and detect its drives.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400918static void
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400919init_controller(struct pci_device *pci, int irq
920 , u32 port1, u32 port2, u32 master)
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500921{
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400922 static int chanid = 0;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500923 struct ata_channel_s *chan_gf = malloc_fseg(sizeof(*chan_gf));
924 if (!chan_gf) {
925 warn_noalloc();
926 return;
927 }
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400928 chan_gf->chanid = chanid++;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500929 chan_gf->irq = irq;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400930 chan_gf->pci_bdf = pci ? pci->bdf : -1;
931 chan_gf->pci_tmp = pci;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500932 chan_gf->iobase1 = port1;
933 chan_gf->iobase2 = port2;
934 chan_gf->iomaster = master;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500935 dprintf(1, "ATA controller %d at %x/%x/%x (irq %d dev %x)\n"
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400936 , chanid, port1, port2, master, irq, chan_gf->pci_bdf);
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500937 run_thread(ata_detect, chan_gf);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500938}
939
Kevin O'Connor525219b2009-12-05 13:36:18 -0500940#define IRQ_ATA1 14
941#define IRQ_ATA2 15
942
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400943// Handle controllers on an ATA PCI device.
944static void
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400945init_pciata(struct pci_device *pci, u8 prog_if)
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400946{
Kevin O'Connor76b5e712011-06-21 22:52:51 -0400947 pci->have_driver = 1;
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400948 u16 bdf = pci->bdf;
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400949 u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400950 int master = 0;
951 if (CONFIG_ATA_DMA && prog_if & 0x80) {
952 // Check for bus-mastering.
953 u32 bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_4);
954 if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
955 master = bar & PCI_BASE_ADDRESS_IO_MASK;
956 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
957 }
958 }
959
960 u32 port1, port2, irq;
961 if (prog_if & 1) {
962 port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_0)
963 & PCI_BASE_ADDRESS_IO_MASK);
964 port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_1)
965 & PCI_BASE_ADDRESS_IO_MASK);
966 irq = pciirq;
967 } else {
968 port1 = PORT_ATA1_CMD_BASE;
969 port2 = PORT_ATA1_CTRL_BASE;
970 irq = IRQ_ATA1;
971 }
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400972 init_controller(pci, irq, port1, port2, master);
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400973
974 if (prog_if & 4) {
975 port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_2)
976 & PCI_BASE_ADDRESS_IO_MASK);
977 port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_3)
978 & PCI_BASE_ADDRESS_IO_MASK);
979 irq = pciirq;
980 } else {
981 port1 = PORT_ATA2_CMD_BASE;
982 port2 = PORT_ATA2_CTRL_BASE;
983 irq = IRQ_ATA2;
984 }
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400985 init_controller(pci, irq, port1, port2, master ? master + 8 : 0);
Kevin O'Connor927d16e2011-06-19 09:43:20 -0400986}
987
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400988static void
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400989found_genericata(struct pci_device *pci, void *arg)
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400990{
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400991 init_pciata(pci, pci->prog_if);
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400992}
993
994static void
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400995found_compatibleahci(struct pci_device *pci, void *arg)
Kevin O'Connorb9457ec2011-06-19 10:03:08 -0400996{
997 if (CONFIG_AHCI)
998 // Already handled directly via native ahci interface.
999 return;
Kevin O'Connor278b19f2011-06-21 22:41:15 -04001000 init_pciata(pci, 0x8f);
Kevin O'Connorb9457ec2011-06-19 10:03:08 -04001001}
1002
Kevin O'Connor927d16e2011-06-19 09:43:20 -04001003static const struct pci_device_id pci_ata_tbl[] = {
Kevin O'Connorb9457ec2011-06-19 10:03:08 -04001004 PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE
1005 , found_genericata),
1006 PCI_DEVICE(PCI_VENDOR_ID_ATI, 0x4391, found_compatibleahci),
Kevin O'Connor927d16e2011-06-19 09:43:20 -04001007 PCI_DEVICE_END,
1008};
1009
Kevin O'Connor14021f22009-12-26 23:21:38 -05001010// Locate and init ata controllers.
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001011static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -05001012ata_scan(void)
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001013{
Kevin O'Connorb98a4b12013-06-08 21:53:36 -04001014 if (CONFIG_QEMU && hlist_empty(&PCIDevices)) {
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001015 // No PCI devices found - probably a QEMU "-M isapc" machine.
1016 // Try using ISA ports for ATA controllers.
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -04001017 init_controller(NULL, IRQ_ATA1
Kevin O'Connor14021f22009-12-26 23:21:38 -05001018 , PORT_ATA1_CMD_BASE, PORT_ATA1_CTRL_BASE, 0);
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -04001019 init_controller(NULL, IRQ_ATA2
Kevin O'Connor14021f22009-12-26 23:21:38 -05001020 , PORT_ATA2_CMD_BASE, PORT_ATA2_CTRL_BASE, 0);
Kevin O'Connor3f3e58d2011-06-20 22:20:43 -04001021 return;
1022 }
1023
1024 // Scan PCI bus for ATA adapters
1025 struct pci_device *pci;
1026 foreachpci(pci) {
Kevin O'Connor278b19f2011-06-21 22:41:15 -04001027 pci_init_device(pci_ata_tbl, pci, NULL);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001028 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001029}
1030
1031void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001032ata_setup(void)
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001033{
Kevin O'Connor59c75742010-02-13 18:49:24 -05001034 ASSERT32FLAT();
Kevin O'Connorc1437612008-05-18 01:43:07 -04001035 if (!CONFIG_ATA)
1036 return;
1037
Kevin O'Connor35192dd2008-06-08 19:18:33 -04001038 dprintf(3, "init hard drives\n");
Kevin O'Connora5826b52009-10-24 17:57:29 -04001039
Kevin O'Connor018bdd72013-07-20 18:22:57 -04001040 SpinupEnd = timer_calc(IDE_TIMEOUT);
Kevin O'Connord83c87b2013-01-21 01:14:12 -05001041 ata_scan();
Kevin O'Connorc1437612008-05-18 01:43:07 -04001042
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001043 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -04001044
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -04001045 enable_hwirq(14, FUNC16(entry_76));
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001046}