blob: 510951b54756eb508c65e0ae24fed5799a98ab5c [file] [log] [blame]
Kevin O'Connorc09492e2008-03-01 13:38:07 -05001// Low level ATA disk access
2//
Kevin O'Connorc892b132009-08-11 21:59:37 -04003// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorc09492e2008-03-01 13:38:07 -05004// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorc09492e2008-03-01 13:38:07 -05007
Kevin O'Connor3491e8b2008-02-29 00:22:27 -05008#include "types.h" // u8
9#include "ioport.h" // inb
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040010#include "util.h" // dprintf
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050011#include "cmos.h" // inb_cmos
Kevin O'Connord21c0892008-11-26 17:02:43 -050012#include "pic.h" // enable_hwirq
Kevin O'Connor9521e262008-07-04 13:04:29 -040013#include "biosvar.h" // GET_EBDA
Kevin O'Connor53236cc2008-08-31 11:16:33 -040014#include "pci.h" // pci_find_class
Kevin O'Connor2ed2f582008-11-08 15:53:36 -050015#include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
16#include "pci_regs.h" // PCI_INTERRUPT_LINE
Kevin O'Connor0a924122009-02-08 19:43:47 -050017#include "boot.h" // add_bcv_hd
Kevin O'Connor609da232008-12-28 23:18:57 -050018#include "disk.h" // struct ata_s
Kevin O'Connorc892b132009-08-11 21:59:37 -040019#include "ata.h" // ATA_CB_STAT
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050020
Kevin O'Connora6b9f712008-03-29 12:53:57 -040021#define IDE_SECTOR_SIZE 512
22#define CDROM_SECTOR_SIZE 2048
23
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'Connorc892b132009-08-11 21:59:37 -040026struct ata_channel_s ATA_channels[CONFIG_MAX_ATA_INTERFACES] VAR16_32;
Kevin O'Connor609da232008-12-28 23:18:57 -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'Connor4e6c9702008-12-13 10:45:50 -050037 u64 end = calc_future_tsc(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'Connorf3587592009-02-15 13:02:56 -050042 if (rdtscll() > end) {
Kevin O'Connor580e3322009-02-06 22:36:53 -050043 dprintf(1, "IDE time out\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050044 return -1;
45 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050046 }
Kevin O'Connor580e3322009-02-06 22:36:53 -050047}
48
49// Wait for the device to be not-busy.
50static int
51await_not_bsy(u16 base)
52{
53 return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
54}
55
56// Wait for the device to be ready.
57static int
58await_rdy(u16 base)
59{
60 return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050061}
62
Kevin O'Connora6b9f712008-03-29 12:53:57 -040063// Wait for ide state - pauses for one ata cycle first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050064static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050065pause_await_not_bsy(u16 iobase1, u16 iobase2)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040066{
67 // Wait one PIO transfer cycle.
68 inb(iobase2 + ATA_CB_ASTAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050069
Kevin O'Connor580e3322009-02-06 22:36:53 -050070 return await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -040071}
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050072
Kevin O'Connoref2822a2008-06-07 15:23:11 -040073// Wait for ide state - pause for 400ns first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050074static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050075ndelay_await_not_bsy(u16 iobase1)
Kevin O'Connoref2822a2008-06-07 15:23:11 -040076{
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050077 ndelay(400);
Kevin O'Connor580e3322009-02-06 22:36:53 -050078 return await_not_bsy(iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -040079}
80
Kevin O'Connora6b9f712008-03-29 12:53:57 -040081// Reset a drive
Kevin O'Connorb1144362009-08-11 20:43:38 -040082static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -040083ata_reset(int driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050084{
Kevin O'Connorc892b132009-08-11 21:59:37 -040085 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -040086 u8 channel = ataid / 2;
87 u8 slave = ataid % 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -040088 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
89 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050090
Kevin O'Connor580e3322009-02-06 22:36:53 -050091 dprintf(6, "ata_reset driveid=%d\n", driveid);
92 // 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'Connor580e3322009-02-06 22:36:53 -050096 mdelay(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.
104 u64 end = calc_future_tsc(IDE_TIMEOUT);
105 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'Connorf3587592009-02-15 13:02:56 -0500113 if (rdtscll() > end) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500114 dprintf(1, "ata_reset slave time out\n");
115 goto done;
116 }
117 }
Kevin O'Connor2e3eeeb2008-06-12 22:29:30 -0400118 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500119
Kevin O'Connor580e3322009-02-06 22:36:53 -0500120 // On a user-reset request, wait for RDY if it is an ATA device.
Kevin O'Connorc892b132009-08-11 21:59:37 -0400121 u8 type=GET_GLOBAL(Drives.drives[driveid].type);
Kevin O'Connor42337662009-08-10 00:06:37 -0400122 if (type == DTYPE_ATA)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500123 status = await_rdy(iobase1);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400124
Kevin O'Connor580e3322009-02-06 22:36:53 -0500125done:
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500126 // Enable interrupts
127 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500128
129 dprintf(6, "ata_reset exit status=%x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500130}
131
Kevin O'Connor42337662009-08-10 00:06:37 -0400132static int
133isready(int driveid)
134{
135 // Read the status from controller
Kevin O'Connorc892b132009-08-11 21:59:37 -0400136 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400137 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400138 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400139 u8 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400140 if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY)
141 return DISK_RET_SUCCESS;
142 return DISK_RET_ENOTREADY;
Kevin O'Connor42337662009-08-10 00:06:37 -0400143}
144
145static int
146process_ata_misc_op(struct disk_op_s *op)
147{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400148 if (!CONFIG_ATA)
149 return 0;
150
Kevin O'Connor42337662009-08-10 00:06:37 -0400151 switch (op->command) {
152 default:
153 return 0;
154 case CMD_RESET:
155 ata_reset(op->driveid);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400156 return DISK_RET_SUCCESS;
Kevin O'Connor42337662009-08-10 00:06:37 -0400157 case CMD_ISREADY:
158 return isready(op->driveid);
159 }
160}
161
Kevin O'Connoree55c762008-05-13 00:18:20 -0400162
163/****************************************************************
164 * ATA send command
165 ****************************************************************/
166
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400167struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400168 u8 feature;
169 u8 sector_count;
170 u8 lba_low;
171 u8 lba_mid;
172 u8 lba_high;
173 u8 device;
174 u8 command;
175
176 u8 sector_count2;
177 u8 lba_low2;
178 u8 lba_mid2;
179 u8 lba_high2;
180};
181
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400182// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400183static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400184send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500185{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400186 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400187 u8 channel = ataid / 2;
188 u8 slave = ataid % 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400189 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
190 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500191
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400192 // Disable interrupts
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500193 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400194
195 // Select device
Kevin O'Connor580e3322009-02-06 22:36:53 -0500196 int status = await_not_bsy(iobase1);
197 if (status < 0)
198 return status;
199 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
200 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
201 u8 olddh = inb(iobase1 + ATA_CB_DH);
202 outb(newdh, iobase1 + ATA_CB_DH);
203 if ((olddh ^ newdh) & (1<<4)) {
204 // Was a device change - wait for device to become not busy.
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400205 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500206 if (status < 0)
207 return status;
208 }
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400209
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400210 if (cmd->command & 0x04) {
211 outb(0x00, iobase1 + ATA_CB_FR);
212 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
213 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
214 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
215 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500216 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400217 outb(cmd->feature, iobase1 + ATA_CB_FR);
218 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
219 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
220 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
221 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400222 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500223
Kevin O'Connor580e3322009-02-06 22:36:53 -0500224 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400225 if (status < 0)
226 return status;
227
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500228 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500229 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
230 , status, inb(iobase1 + ATA_CB_ERR));
Kevin O'Connora05223c2008-06-28 12:15:57 -0400231 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400232 }
233 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500234 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400235 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500236 }
237
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400238 return 0;
239}
240
Kevin O'Connoree55c762008-05-13 00:18:20 -0400241
242/****************************************************************
243 * ATA transfers
244 ****************************************************************/
245
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400246// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
247// 'op->driveid'.
248static int
249ata_transfer(struct disk_op_s *op, int iswrite, int blocksize)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400250{
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400251 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d buf=%p\n"
252 , op->driveid, iswrite, op->count, blocksize, op->buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400253
Kevin O'Connorc892b132009-08-11 21:59:37 -0400254 u8 ataid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400255 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400256 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
257 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400258 int count = op->count;
259 void *buf_fl = op->buf_fl;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400260 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400261 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400262 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400263 // Write data to controller
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400264 dprintf(16, "Write sector id=%d dest=%p\n", op->driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500265 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400266 outsl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400267 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400268 outsw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500269 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400270 // Read data from controller
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400271 dprintf(16, "Read sector id=%d dest=%p\n", op->driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500272 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400273 insl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400274 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400275 insw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400276 }
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400277 buf_fl += blocksize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400278
Kevin O'Connor580e3322009-02-06 22:36:53 -0500279 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400280 if (status < 0) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400281 // Error
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400282 op->count -= count;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400283 return status;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400284 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400285
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400286 count--;
287 if (!count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400288 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500289 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
290 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400291 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500292 , status);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400293 op->count -= count;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400294 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500295 }
296 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400297
Kevin O'Connor580e3322009-02-06 22:36:53 -0500298 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
299 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400300 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400301 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500302 if (status != 0) {
303 dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400304 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400305 }
306
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500307 // Enable interrupts
308 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
309 return 0;
310}
311
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400312
313/****************************************************************
314 * ATA hard drive functions
315 ****************************************************************/
316
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400317// Read/write count blocks from a harddrive.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400318static int
319ata_cmd_data(struct disk_op_s *op, int iswrite, int command)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400320{
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400321 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400322
323 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400324 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400325
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400326 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400327 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
328 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400329 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400330 cmd.lba_mid2 = lba >> 32;
331 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400332
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400333 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400334 lba &= 0xffffff;
335 }
336
337 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400338 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400339 cmd.lba_low = lba;
340 cmd.lba_mid = lba >> 8;
341 cmd.lba_high = lba >> 16;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500342 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400343
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400344 int ret = send_cmd(op->driveid, &cmd);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400345 if (ret)
346 return ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400347 return ata_transfer(op, iswrite, IDE_SECTOR_SIZE);
348}
349
350int
351process_ata_op(struct disk_op_s *op)
352{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400353 if (!CONFIG_ATA)
354 return 0;
355
Kevin O'Connor126eac62009-08-16 13:32:24 -0400356 int ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400357 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400358 case CMD_READ:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400359 ret = ata_cmd_data(op, 0, ATA_CMD_READ_SECTORS);
360 break;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400361 case CMD_WRITE:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400362 ret = ata_cmd_data(op, 1, ATA_CMD_WRITE_SECTORS);
363 break;
Kevin O'Connor42337662009-08-10 00:06:37 -0400364 default:
365 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400366 }
Kevin O'Connor126eac62009-08-16 13:32:24 -0400367 if (ret)
368 return DISK_RET_EBADTRACK;
369 return DISK_RET_SUCCESS;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400370}
371
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400372
373/****************************************************************
374 * ATAPI functions
375 ****************************************************************/
376
377// Low-level atapi command transmit function.
Kevin O'Connora9caeae2009-03-07 00:09:52 -0500378static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400379send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500380{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400381 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400382 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400383 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
384 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500385
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400386 struct ata_pio_command cmd;
387 cmd.sector_count = 0;
388 cmd.feature = 0;
389 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400390 cmd.lba_mid = blocksize;
391 cmd.lba_high = blocksize >> 8;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500392 cmd.device = 0;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400393 cmd.command = ATA_CMD_PACKET;
394
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400395 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400396 if (ret)
397 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500398
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400399 // Send command to device
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500400 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500401
Kevin O'Connor580e3322009-02-06 22:36:53 -0500402 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400403 if (status < 0)
404 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400405
Kevin O'Connor580e3322009-02-06 22:36:53 -0500406 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connorb30c4002009-05-05 21:47:20 -0400407 u8 err = inb(iobase1 + ATA_CB_ERR);
408 // skip "Not Ready"
409 if (err != 0x20)
410 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
411 , status, err);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500412 return -2;
413 }
414 if (!(status & ATA_CB_STAT_DRQ)) {
415 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
416 return -3;
417 }
418
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500419 return 0;
420}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500421
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400422// Read sectors from the cdrom.
423int
424cdrom_read(struct disk_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500425{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400426 u8 atacmd[12];
427 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connoree55c762008-05-13 00:18:20 -0400428 atacmd[0]=0x28; // READ command
429 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
430 atacmd[8]=(op->count & 0x00ff);
431 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
432 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
433 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
434 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400435
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400436 int ret = send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
437 , CDROM_SECTOR_SIZE);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400438 if (ret)
439 return ret;
440
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400441 return ata_transfer(op, 0, CDROM_SECTOR_SIZE);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400442}
443
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400444int
445process_atapi_op(struct disk_op_s *op)
446{
Kevin O'Connor126eac62009-08-16 13:32:24 -0400447 int ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400448 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400449 case CMD_READ:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400450 ret = cdrom_read(op);
451 break;
Kevin O'Connor42337662009-08-10 00:06:37 -0400452 default:
453 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400454 }
Kevin O'Connor126eac62009-08-16 13:32:24 -0400455 if (ret)
456 return DISK_RET_EBADTRACK;
457 return DISK_RET_SUCCESS;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400458}
459
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400460// Send a simple atapi command to a drive.
461int
462ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500463 , u32 length, void *buf_fl)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400464{
465 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
466 if (ret)
467 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400468
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400469 struct disk_op_s dop;
470 memset(&dop, 0, sizeof(dop));
471 dop.driveid = driveid;
472 dop.count = 1;
473 dop.buf_fl = buf_fl;
474
475 return ata_transfer(&dop, 0, length);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400476}
477
478
479/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500480 * ATA detect and init
481 ****************************************************************/
482
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400483// Extract the ATA/ATAPI version info.
484static int
485extract_version(u16 *buffer)
486{
487 // Extract ATA/ATAPI version.
488 u16 ataversion = buffer[80];
489 u8 version;
490 for (version=15; version>0; version--)
491 if (ataversion & (1<<version))
492 break;
493 return version;
494}
495
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500496// Extract common information from IDENTIFY commands.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500497static void
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500498extract_identify(int driveid, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500499{
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500500 dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500501
502 // Read model name
Kevin O'Connorc892b132009-08-11 21:59:37 -0400503 char *model = Drives.drives[driveid].model;
504 int maxsize = ARRAY_SIZE(Drives.drives[driveid].model);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500505 int i;
Kevin O'Connorab515602009-02-11 22:22:15 -0500506 for (i=0; i<maxsize/2; i++) {
507 u16 v = buffer[27+i];
508 model[i*2] = v >> 8;
509 model[i*2+1] = v & 0xff;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500510 }
511 model[maxsize-1] = 0x00;
512
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500513 // Trim trailing spaces from model name.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500514 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
515 model[i] = 0x00;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500516
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500517 // Common flags.
Kevin O'Connorc892b132009-08-11 21:59:37 -0400518 SET_GLOBAL(Drives.drives[driveid].removable, (buffer[0] & 0x80) ? 1 : 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500519}
520
521static int
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400522init_drive_atapi(int driveid, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500523{
524 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400525 memset(buffer, 0, IDE_SECTOR_SIZE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500526 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400527 memset(&dop, 0, sizeof(dop));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500528 dop.driveid = driveid;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500529 dop.count = 1;
530 dop.lba = 1;
531 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400532 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE_PACKET);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500533 if (ret)
534 return ret;
535
536 // Success - setup as ATAPI.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500537 extract_identify(driveid, buffer);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400538 SET_GLOBAL(Drives.drives[driveid].type, DTYPE_ATAPI);
539 SET_GLOBAL(Drives.drives[driveid].blksize, CDROM_SECTOR_SIZE);
540 SET_GLOBAL(Drives.drives[driveid].sectors, (u64)-1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400541 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500542
543 // Report drive info to user.
Kevin O'Connorc892b132009-08-11 21:59:37 -0400544 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400545 u8 channel = ataid / 2;
546 u8 slave = ataid % 2;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500547 printf("ata%d-%d: %s ATAPI-%d %s\n", channel, slave
Kevin O'Connorc892b132009-08-11 21:59:37 -0400548 , Drives.drives[driveid].model, extract_version(buffer)
Kevin O'Connor42337662009-08-10 00:06:37 -0400549 , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
550
551 // fill cdidmap
Kevin O'Connorc892b132009-08-11 21:59:37 -0400552 if (iscd)
553 map_cd_drive(driveid);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500554
555 return 0;
556}
557
Kevin O'Connor580e3322009-02-06 22:36:53 -0500558static int
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400559init_drive_ata(int driveid, u16 *buffer)
Kevin O'Connorc1437612008-05-18 01:43:07 -0400560{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500561 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400562 memset(buffer, 0, IDE_SECTOR_SIZE);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500563 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400564 memset(&dop, 0, sizeof(dop));
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500565 dop.driveid = driveid;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500566 dop.count = 1;
567 dop.lba = 1;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500568 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400569 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400570 if (ret)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500571 return ret;
572
573 // Success - setup as ATA.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500574 extract_identify(driveid, buffer);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400575 SET_GLOBAL(Drives.drives[driveid].type, DTYPE_ATA);
576 SET_GLOBAL(Drives.drives[driveid].blksize, IDE_SECTOR_SIZE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400577
Kevin O'Connorc892b132009-08-11 21:59:37 -0400578 SET_GLOBAL(Drives.drives[driveid].pchs.cylinders, buffer[1]);
579 SET_GLOBAL(Drives.drives[driveid].pchs.heads, buffer[3]);
580 SET_GLOBAL(Drives.drives[driveid].pchs.spt, buffer[6]);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400581
582 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500583 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
584 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400585 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500586 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connorc892b132009-08-11 21:59:37 -0400587 SET_GLOBAL(Drives.drives[driveid].sectors, sectors);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400588
589 // Setup disk geometry translation.
590 setup_translation(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400591
Kevin O'Connorc1437612008-05-18 01:43:07 -0400592 // Report drive info to user.
Kevin O'Connorc892b132009-08-11 21:59:37 -0400593 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400594 u8 channel = ataid / 2;
595 u8 slave = ataid % 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400596 char *model = Drives.drives[driveid].model;
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500597 printf("ata%d-%d: %s ATA-%d Hard-Disk ", channel, slave, model
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400598 , extract_version(buffer));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500599 u64 sizeinmb = sectors >> 11;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400600 if (sizeinmb < (1 << 16))
Kevin O'Connor0a924122009-02-08 19:43:47 -0500601 printf("(%u MiBytes)\n", (u32)sizeinmb);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400602 else
Kevin O'Connor0a924122009-02-08 19:43:47 -0500603 printf("(%u GiBytes)\n", (u32)(sizeinmb >> 10));
604
605 // Register with bcv system.
606 add_bcv_hd(driveid, model);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400607
Kevin O'Connor580e3322009-02-06 22:36:53 -0500608 return 0;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400609}
610
Kevin O'Connor425f2122009-04-18 12:23:00 -0400611static int
612powerup_await_non_bsy(u16 base, u64 end)
613{
614 u8 orstatus = 0;
615 u8 status;
616 for (;;) {
617 status = inb(base+ATA_CB_STAT);
618 if (!(status & ATA_CB_STAT_BSY))
619 break;
620 orstatus |= status;
621 if (orstatus == 0xff) {
622 dprintf(1, "powerup IDE floating\n");
623 return orstatus;
624 }
625 if (rdtscll() > end) {
626 dprintf(1, "powerup IDE time out\n");
627 return -1;
628 }
629 }
630 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
631 return status;
632}
633
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400634static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500635ata_detect()
636{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500637 // Device detection
Kevin O'Connor425f2122009-04-18 12:23:00 -0400638 u64 end = calc_future_tsc(IDE_TIMEOUT);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400639 int ataid, last_reset_ataid=-1;
Kevin O'Connorb1144362009-08-11 20:43:38 -0400640 for (ataid=0; ataid<CONFIG_MAX_ATA_INTERFACES*2; ataid++) {
641 u8 channel = ataid / 2;
642 u8 slave = ataid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500643
Kevin O'Connorc892b132009-08-11 21:59:37 -0400644 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400645 if (!iobase1)
646 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500647
Kevin O'Connor425f2122009-04-18 12:23:00 -0400648 // Wait for not-bsy.
649 int status = powerup_await_non_bsy(iobase1, end);
650 if (status < 0)
651 continue;
652 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
653 outb(newdh, iobase1+ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400654 ndelay(400);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400655 status = powerup_await_non_bsy(iobase1, end);
656 if (status < 0)
657 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500658
Kevin O'Connor580e3322009-02-06 22:36:53 -0500659 // Check if ioport registers look valid.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400660 outb(newdh, iobase1+ATA_CB_DH);
661 u8 dh = inb(iobase1+ATA_CB_DH);
662 outb(0x55, iobase1+ATA_CB_SC);
663 outb(0xaa, iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400664 u8 sc = inb(iobase1+ATA_CB_SC);
665 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400666 dprintf(6, "ata_detect ataid=%d sc=%x sn=%x dh=%x\n"
667 , ataid, sc, sn, dh);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400668 if (sc != 0x55 || sn != 0xaa || dh != newdh)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400669 continue;
670
Kevin O'Connorb1144362009-08-11 20:43:38 -0400671 // Prepare new driveid.
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400672 u8 driveid = GET_GLOBAL(Drives.drivecount);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400673 if (driveid >= ARRAY_SIZE(Drives.drives))
Kevin O'Connorb1144362009-08-11 20:43:38 -0400674 break;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400675 memset(&Drives.drives[driveid], 0, sizeof(Drives.drives[0]));
676 Drives.drives[driveid].cntl_id = ataid;
Kevin O'Connorb1144362009-08-11 20:43:38 -0400677
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400678 // reset the channel
Kevin O'Connorb1144362009-08-11 20:43:38 -0400679 if (slave && ataid == last_reset_ataid + 1) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500680 // The drive was just reset - no need to reset it again.
681 } else {
682 ata_reset(driveid);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400683 last_reset_ataid = ataid;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500684 }
685
Kevin O'Connor580e3322009-02-06 22:36:53 -0500686 // check for ATAPI
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400687 u16 buffer[256];
688 int ret = init_drive_atapi(driveid, buffer);
689 if (!ret) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500690 // Found an ATAPI drive.
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400691 } else {
692 u8 st = inb(iobase1+ATA_CB_STAT);
693 if (!st)
694 // Status not set - can't be a valid drive.
695 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500696
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400697 // Wait for RDY.
698 ret = await_rdy(iobase1);
699 if (ret < 0)
700 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500701
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400702 // check for ATA.
703 ret = init_drive_ata(driveid, buffer);
704 if (ret)
705 // No ATA drive found
706 continue;
707 }
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400708 SET_GLOBAL(Drives.drivecount, driveid+1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500709
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400710 u16 resetresult = buffer[93];
711 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
712 if (!slave && (resetresult & 0xdf61) == 0x4041)
713 // resetresult looks valid and device 0 is responding to
714 // device 1 requests - device 1 must not be present - skip
715 // detection.
Kevin O'Connorb1144362009-08-11 20:43:38 -0400716 ataid++;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500717 }
718
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500719 printf("\n");
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500720}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400721
722static void
723ata_init()
724{
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400725 // Scan PCI bus for ATA adapters
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500726 int count=0;
727 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -0500728 foreachpci(bdf, max) {
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500729 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400730 continue;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400731 if (count >= ARRAY_SIZE(ATA_channels))
Kevin O'Connor89f67632009-02-11 20:16:49 -0500732 break;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400733
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500734 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400735 SET_GLOBAL(ATA_channels[count].irq, irq);
736 SET_GLOBAL(ATA_channels[count].pci_bdf, bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400737
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500738 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400739 u32 port1, port2;
740
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500741 if (prog_if & 1) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500742 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
743 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400744 } else {
745 port1 = 0x1f0;
746 port2 = 0x3f0;
747 }
Kevin O'Connorc892b132009-08-11 21:59:37 -0400748 SET_GLOBAL(ATA_channels[count].iobase1, port1);
749 SET_GLOBAL(ATA_channels[count].iobase2, port2);
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500750 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
751 , count, port1, port2, bdf, prog_if);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400752 count++;
753
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500754 if (prog_if & 4) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500755 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
756 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400757 } else {
758 port1 = 0x170;
759 port2 = 0x370;
760 }
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500761 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
762 , count, port1, port2, bdf, prog_if);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400763 SET_GLOBAL(ATA_channels[count].iobase1, port1);
764 SET_GLOBAL(ATA_channels[count].iobase2, port2);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400765 count++;
766 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400767}
768
769void
Kevin O'Connorc892b132009-08-11 21:59:37 -0400770ata_setup()
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400771{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400772 if (!CONFIG_ATA)
773 return;
774
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400775 dprintf(3, "init hard drives\n");
Kevin O'Connorc1437612008-05-18 01:43:07 -0400776 ata_init();
777 ata_detect();
778
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400779 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400780
Kevin O'Connord21c0892008-11-26 17:02:43 -0500781 enable_hwirq(14, entry_76);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400782}