blob: 94538303191ab29b80e1c34269cca7299e0ef69e [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'Connor51fd0a12009-09-12 13:20:14 -040014#include "pci.h" // foreachpci
Kevin O'Connor2ed2f582008-11-08 15:53:36 -050015#include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
16#include "pci_regs.h" // PCI_INTERRUPT_LINE
Kevin O'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'Connor425f2122009-04-18 12:23:00 -040021#define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050022
Kevin O'Connor372e0712009-09-09 09:51:31 -040023struct ata_channel_s ATA_channels[CONFIG_MAX_ATA_INTERFACES] VAR16VISIBLE;
Kevin O'Connor609da232008-12-28 23:18:57 -050024
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050025
Kevin O'Connora6b9f712008-03-29 12:53:57 -040026/****************************************************************
27 * Helper functions
28 ****************************************************************/
29
30// Wait for the specified ide state
Kevin O'Connor580e3322009-02-06 22:36:53 -050031static inline int
32await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050033{
Kevin O'Connor4e6c9702008-12-13 10:45:50 -050034 u64 end = calc_future_tsc(timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050035 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -040036 u8 status = inb(base+ATA_CB_STAT);
Kevin O'Connor580e3322009-02-06 22:36:53 -050037 if ((status & mask) == flags)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040038 return status;
Kevin O'Connor89eb6242009-10-22 22:30:37 -040039 if (check_time(end)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -050040 dprintf(1, "IDE time out\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050041 return -1;
42 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -040043 yield();
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050044 }
Kevin O'Connor580e3322009-02-06 22:36:53 -050045}
46
47// Wait for the device to be not-busy.
48static int
49await_not_bsy(u16 base)
50{
51 return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
52}
53
54// Wait for the device to be ready.
55static int
56await_rdy(u16 base)
57{
58 return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050059}
60
Kevin O'Connora6b9f712008-03-29 12:53:57 -040061// Wait for ide state - pauses for one ata cycle first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050062static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050063pause_await_not_bsy(u16 iobase1, u16 iobase2)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040064{
65 // Wait one PIO transfer cycle.
66 inb(iobase2 + ATA_CB_ASTAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050067
Kevin O'Connor580e3322009-02-06 22:36:53 -050068 return await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -040069}
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050070
Kevin O'Connoref2822a2008-06-07 15:23:11 -040071// Wait for ide state - pause for 400ns first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050072static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050073ndelay_await_not_bsy(u16 iobase1)
Kevin O'Connoref2822a2008-06-07 15:23:11 -040074{
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050075 ndelay(400);
Kevin O'Connor580e3322009-02-06 22:36:53 -050076 return await_not_bsy(iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -040077}
78
Kevin O'Connora6b9f712008-03-29 12:53:57 -040079// Reset a drive
Kevin O'Connorb1144362009-08-11 20:43:38 -040080static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -040081ata_reset(struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050082{
Kevin O'Connor77d227b2009-10-22 21:48:39 -040083 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -040084 u8 channel = ataid / 2;
85 u8 slave = ataid % 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -040086 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
87 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050088
Kevin O'Connor77d227b2009-10-22 21:48:39 -040089 dprintf(6, "ata_reset drive=%p\n", drive_g);
Kevin O'Connor580e3322009-02-06 22:36:53 -050090 // Pulse SRST
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050091 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -050092 udelay(5);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050093 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
Kevin O'Connor10ad7992009-10-24 11:06:08 -040094 msleep(2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050095
Kevin O'Connor580e3322009-02-06 22:36:53 -050096 // wait for device to become not busy.
97 int status = await_not_bsy(iobase1);
98 if (status < 0)
99 goto done;
100 if (slave) {
101 // Change device.
102 u64 end = calc_future_tsc(IDE_TIMEOUT);
103 for (;;) {
104 outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400105 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500106 if (status < 0)
107 goto done;
108 if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
109 break;
110 // Change drive request failed to take effect - retry.
Kevin O'Connor89eb6242009-10-22 22:30:37 -0400111 if (check_time(end)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500112 dprintf(1, "ata_reset slave time out\n");
113 goto done;
114 }
115 }
Kevin O'Connorf5624d22009-08-18 22:17:57 -0400116 } else {
117 // QEMU doesn't reset dh on reset, so set it explicitly.
118 outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
Kevin O'Connor2e3eeeb2008-06-12 22:29:30 -0400119 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500120
Kevin O'Connor580e3322009-02-06 22:36:53 -0500121 // On a user-reset request, wait for RDY if it is an ATA device.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400122 u8 type=GET_GLOBAL(drive_g->type);
Kevin O'Connor42337662009-08-10 00:06:37 -0400123 if (type == DTYPE_ATA)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500124 status = await_rdy(iobase1);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400125
Kevin O'Connor580e3322009-02-06 22:36:53 -0500126done:
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500127 // Enable interrupts
128 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500129
130 dprintf(6, "ata_reset exit status=%x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500131}
132
Kevin O'Connor42337662009-08-10 00:06:37 -0400133static int
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400134isready(struct drive_s *drive_g)
Kevin O'Connor42337662009-08-10 00:06:37 -0400135{
136 // Read the status from controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400137 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400138 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400139 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400140 u8 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400141 if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY)
142 return DISK_RET_SUCCESS;
143 return DISK_RET_ENOTREADY;
Kevin O'Connor42337662009-08-10 00:06:37 -0400144}
145
146static int
147process_ata_misc_op(struct disk_op_s *op)
148{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400149 if (!CONFIG_ATA)
150 return 0;
151
Kevin O'Connor42337662009-08-10 00:06:37 -0400152 switch (op->command) {
Kevin O'Connor42337662009-08-10 00:06:37 -0400153 case CMD_RESET:
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400154 ata_reset(op->drive_g);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400155 return DISK_RET_SUCCESS;
Kevin O'Connor42337662009-08-10 00:06:37 -0400156 case CMD_ISREADY:
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400157 return isready(op->drive_g);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400158 case CMD_FORMAT:
159 case CMD_VERIFY:
160 case CMD_SEEK:
161 return DISK_RET_SUCCESS;
162 default:
163 op->count = 0;
164 return DISK_RET_EPARAM;
Kevin O'Connor42337662009-08-10 00:06:37 -0400165 }
166}
167
Kevin O'Connoree55c762008-05-13 00:18:20 -0400168
169/****************************************************************
170 * ATA send command
171 ****************************************************************/
172
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400173struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400174 u8 feature;
175 u8 sector_count;
176 u8 lba_low;
177 u8 lba_mid;
178 u8 lba_high;
179 u8 device;
180 u8 command;
181
182 u8 sector_count2;
183 u8 lba_low2;
184 u8 lba_mid2;
185 u8 lba_high2;
186};
187
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400188// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400189static int
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400190send_cmd(struct drive_s *drive_g, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500191{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400192 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400193 u8 channel = ataid / 2;
194 u8 slave = ataid % 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400195 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400196
197 // Select device
Kevin O'Connor580e3322009-02-06 22:36:53 -0500198 int status = await_not_bsy(iobase1);
199 if (status < 0)
200 return status;
201 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
202 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
203 u8 olddh = inb(iobase1 + ATA_CB_DH);
204 outb(newdh, iobase1 + ATA_CB_DH);
205 if ((olddh ^ newdh) & (1<<4)) {
206 // Was a device change - wait for device to become not busy.
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400207 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500208 if (status < 0)
209 return status;
210 }
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400211
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400212 if (cmd->command & 0x04) {
213 outb(0x00, iobase1 + ATA_CB_FR);
214 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
215 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
216 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
217 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500218 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400219 outb(cmd->feature, iobase1 + ATA_CB_FR);
220 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
221 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
222 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
223 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400224 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500225
Kevin O'Connor580e3322009-02-06 22:36:53 -0500226 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400227 if (status < 0)
228 return status;
229
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500230 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500231 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
232 , status, inb(iobase1 + ATA_CB_ERR));
Kevin O'Connora05223c2008-06-28 12:15:57 -0400233 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400234 }
235 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500236 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400237 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500238 }
239
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400240 return 0;
241}
242
Kevin O'Connoree55c762008-05-13 00:18:20 -0400243
244/****************************************************************
245 * ATA transfers
246 ****************************************************************/
247
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400248// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400249// 'op->drive_g'.
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400250static int
251ata_transfer(struct disk_op_s *op, int iswrite, int blocksize)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400252{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400253 dprintf(16, "ata_transfer id=%p write=%d count=%d bs=%d buf=%p\n"
254 , op->drive_g, iswrite, op->count, blocksize, op->buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400255
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400256 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400257 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400258 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
259 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400260 int count = op->count;
261 void *buf_fl = op->buf_fl;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400262 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400263 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400264 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400265 // Write data to controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400266 dprintf(16, "Write sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500267 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400268 outsl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400269 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400270 outsw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500271 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400272 // Read data from controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400273 dprintf(16, "Read sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500274 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400275 insl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400276 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400277 insw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400278 }
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400279 buf_fl += blocksize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400280
Kevin O'Connor580e3322009-02-06 22:36:53 -0500281 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400282 if (status < 0) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400283 // Error
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400284 op->count -= count;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400285 return status;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400286 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400287
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400288 count--;
289 if (!count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400290 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500291 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
292 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400293 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500294 , status);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400295 op->count -= count;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400296 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500297 }
298 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400299
Kevin O'Connor580e3322009-02-06 22:36:53 -0500300 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
301 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400302 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400303 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500304 if (status != 0) {
305 dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400306 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400307 }
308
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500309 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'Connor42bc3942009-11-20 17:28:19 -0500321 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
322 u8 channel = ataid / 2;
323 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400324 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400325
326 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400327 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400328
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400329 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400330 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
331 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400332 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400333 cmd.lba_mid2 = lba >> 32;
334 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400335
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400336 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400337 lba &= 0xffffff;
338 }
339
340 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400341 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400342 cmd.lba_low = lba;
343 cmd.lba_mid = lba >> 8;
344 cmd.lba_high = lba >> 16;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500345 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400346
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500347 // Disable interrupts
348 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
349
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400350 int ret = send_cmd(op->drive_g, &cmd);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400351 if (ret)
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500352 goto fail;
353 ret = ata_transfer(op, iswrite, DISK_SECTOR_SIZE);
354
355fail:
356 // Enable interrupts
357 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
358 return ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400359}
360
361int
362process_ata_op(struct disk_op_s *op)
363{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400364 if (!CONFIG_ATA)
365 return 0;
366
Kevin O'Connor126eac62009-08-16 13:32:24 -0400367 int ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400368 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400369 case CMD_READ:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400370 ret = ata_cmd_data(op, 0, ATA_CMD_READ_SECTORS);
371 break;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400372 case CMD_WRITE:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400373 ret = ata_cmd_data(op, 1, ATA_CMD_WRITE_SECTORS);
374 break;
Kevin O'Connor42337662009-08-10 00:06:37 -0400375 default:
376 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400377 }
Kevin O'Connor126eac62009-08-16 13:32:24 -0400378 if (ret)
379 return DISK_RET_EBADTRACK;
380 return DISK_RET_SUCCESS;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400381}
382
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400383
384/****************************************************************
385 * ATAPI functions
386 ****************************************************************/
387
388// Low-level atapi command transmit function.
Kevin O'Connora9caeae2009-03-07 00:09:52 -0500389static int
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500390atapi_cmd_data(struct disk_op_s *op, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500391{
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500392 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400393 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400394 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
395 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500396
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400397 struct ata_pio_command cmd;
398 cmd.sector_count = 0;
399 cmd.feature = 0;
400 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400401 cmd.lba_mid = blocksize;
402 cmd.lba_high = blocksize >> 8;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500403 cmd.device = 0;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400404 cmd.command = ATA_CMD_PACKET;
405
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500406 // Disable interrupts
407 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
408
409 int ret = send_cmd(op->drive_g, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400410 if (ret)
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500411 goto fail;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500412
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400413 // Send command to device
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500414 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500415
Kevin O'Connor580e3322009-02-06 22:36:53 -0500416 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500417 if (status < 0) {
418 ret = status;
419 goto fail;
420 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400421
Kevin O'Connor580e3322009-02-06 22:36:53 -0500422 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connorb30c4002009-05-05 21:47:20 -0400423 u8 err = inb(iobase1 + ATA_CB_ERR);
424 // skip "Not Ready"
425 if (err != 0x20)
426 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
427 , status, err);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500428 ret = -2;
429 goto fail;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500430 }
431 if (!(status & ATA_CB_STAT_DRQ)) {
432 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500433 ret = -3;
434 goto fail;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500435 }
436
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500437 ret = ata_transfer(op, 0, blocksize);
438
439fail:
440 // Enable interrupts
441 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
442 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500443}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500444
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400445// Read sectors from the cdrom.
446int
447cdrom_read(struct disk_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500448{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400449 u8 atacmd[12];
450 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connoree55c762008-05-13 00:18:20 -0400451 atacmd[0]=0x28; // READ command
452 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
453 atacmd[8]=(op->count & 0x00ff);
454 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
455 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
456 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
457 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400458
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500459 return atapi_cmd_data(op, atacmd, sizeof(atacmd), CDROM_SECTOR_SIZE);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400460}
461
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400462int
463process_atapi_op(struct disk_op_s *op)
464{
Kevin O'Connor126eac62009-08-16 13:32:24 -0400465 int ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400466 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400467 case CMD_READ:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400468 ret = cdrom_read(op);
469 break;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400470 case CMD_FORMAT:
471 case CMD_WRITE:
472 return DISK_RET_EWRITEPROTECT;
Kevin O'Connor42337662009-08-10 00:06:37 -0400473 default:
474 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400475 }
Kevin O'Connor126eac62009-08-16 13:32:24 -0400476 if (ret)
477 return DISK_RET_EBADTRACK;
478 return DISK_RET_SUCCESS;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400479}
480
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400481// Send a simple atapi command to a drive.
482int
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400483ata_cmd_packet(struct drive_s *drive_g, u8 *cmdbuf, u8 cmdlen
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500484 , u32 length, void *buf_fl)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400485{
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400486 struct disk_op_s dop;
487 memset(&dop, 0, sizeof(dop));
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400488 dop.drive_g = drive_g;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400489 dop.count = 1;
490 dop.buf_fl = buf_fl;
491
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500492 return atapi_cmd_data(&dop, cmdbuf, cmdlen, length);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400493}
494
495
496/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500497 * ATA detect and init
498 ****************************************************************/
499
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400500// Extract the ATA/ATAPI version info.
501static int
502extract_version(u16 *buffer)
503{
504 // Extract ATA/ATAPI version.
505 u16 ataversion = buffer[80];
506 u8 version;
507 for (version=15; version>0; version--)
508 if (ataversion & (1<<version))
509 break;
510 return version;
511}
512
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500513// Extract common information from IDENTIFY commands.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500514static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400515extract_identify(struct drive_s *drive_g, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500516{
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500517 dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500518
519 // Read model name
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400520 char *model = drive_g->model;
521 int maxsize = ARRAY_SIZE(drive_g->model);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500522 int i;
Kevin O'Connorab515602009-02-11 22:22:15 -0500523 for (i=0; i<maxsize/2; i++) {
524 u16 v = buffer[27+i];
525 model[i*2] = v >> 8;
526 model[i*2+1] = v & 0xff;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500527 }
528 model[maxsize-1] = 0x00;
529
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500530 // Trim trailing spaces from model name.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500531 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
532 model[i] = 0x00;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500533
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500534 // Common flags.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400535 SET_GLOBAL(drive_g->removable, (buffer[0] & 0x80) ? 1 : 0);
536 SET_GLOBAL(drive_g->cntl_info, extract_version(buffer));
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400537}
538
539void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400540describe_atapi(struct drive_s *drive_g)
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400541{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400542 u8 ataid = drive_g->cntl_id;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400543 u8 channel = ataid / 2;
544 u8 slave = ataid % 2;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400545 u8 version = drive_g->cntl_info;
546 int iscd = drive_g->floppy_type;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400547 printf("ata%d-%d: %s ATAPI-%d %s", channel, slave
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400548 , drive_g->model, version
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400549 , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500550}
551
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400552static struct drive_s *
553init_drive_atapi(struct drive_s *dummy, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500554{
555 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400556 memset(buffer, 0, DISK_SECTOR_SIZE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500557 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400558 memset(&dop, 0, sizeof(dop));
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400559 dop.drive_g = dummy;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500560 dop.count = 1;
561 dop.lba = 1;
562 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400563 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE_PACKET);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500564 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400565 return NULL;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500566
567 // Success - setup as ATAPI.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400568 struct drive_s *drive_g = allocDrive();
569 if (! drive_g)
570 return NULL;
571 SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
572 extract_identify(drive_g, buffer);
573 SET_GLOBAL(drive_g->type, DTYPE_ATAPI);
574 SET_GLOBAL(drive_g->blksize, CDROM_SECTOR_SIZE);
575 SET_GLOBAL(drive_g->sectors, (u64)-1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400576 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400577 SET_GLOBAL(drive_g->floppy_type, iscd);
Kevin O'Connor42337662009-08-10 00:06:37 -0400578
579 // fill cdidmap
Kevin O'Connorc892b132009-08-11 21:59:37 -0400580 if (iscd)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400581 map_cd_drive(drive_g);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500582
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400583 return drive_g;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500584}
585
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400586void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400587describe_ata(struct drive_s *drive_g)
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400588{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400589 u8 ataid = drive_g->cntl_id;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400590 u8 channel = ataid / 2;
591 u8 slave = ataid % 2;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400592 u64 sectors = drive_g->sectors;
593 u8 version = drive_g->cntl_info;
594 char *model = drive_g->model;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400595 printf("ata%d-%d: %s ATA-%d Hard-Disk", channel, slave, model, version);
596 u64 sizeinmb = sectors >> 11;
597 if (sizeinmb < (1 << 16))
598 printf(" (%u MiBytes)", (u32)sizeinmb);
599 else
600 printf(" (%u GiBytes)", (u32)(sizeinmb >> 10));
601}
602
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400603static struct drive_s *
604init_drive_ata(struct drive_s *dummy, u16 *buffer)
Kevin O'Connorc1437612008-05-18 01:43:07 -0400605{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500606 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400607 memset(buffer, 0, DISK_SECTOR_SIZE);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500608 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400609 memset(&dop, 0, sizeof(dop));
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400610 dop.drive_g = dummy;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500611 dop.count = 1;
612 dop.lba = 1;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500613 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400614 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400615 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400616 return NULL;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500617
618 // Success - setup as ATA.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400619 struct drive_s *drive_g = allocDrive();
620 if (! drive_g)
621 return NULL;
622 SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
623 extract_identify(drive_g, buffer);
624 SET_GLOBAL(drive_g->type, DTYPE_ATA);
625 SET_GLOBAL(drive_g->blksize, DISK_SECTOR_SIZE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400626
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400627 SET_GLOBAL(drive_g->pchs.cylinders, buffer[1]);
628 SET_GLOBAL(drive_g->pchs.heads, buffer[3]);
629 SET_GLOBAL(drive_g->pchs.spt, buffer[6]);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400630
631 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500632 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
633 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400634 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500635 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400636 SET_GLOBAL(drive_g->sectors, sectors);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400637
638 // Setup disk geometry translation.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400639 setup_translation(drive_g);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400640
Kevin O'Connor0a924122009-02-08 19:43:47 -0500641 // Register with bcv system.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400642 add_bcv_internal(drive_g);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400643
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400644 return drive_g;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400645}
646
Kevin O'Connora5826b52009-10-24 17:57:29 -0400647static u64 SpinupEnd;
648
Kevin O'Connor425f2122009-04-18 12:23:00 -0400649static int
Kevin O'Connora5826b52009-10-24 17:57:29 -0400650powerup_await_non_bsy(u16 base)
Kevin O'Connor425f2122009-04-18 12:23:00 -0400651{
652 u8 orstatus = 0;
653 u8 status;
654 for (;;) {
655 status = inb(base+ATA_CB_STAT);
656 if (!(status & ATA_CB_STAT_BSY))
657 break;
658 orstatus |= status;
659 if (orstatus == 0xff) {
660 dprintf(1, "powerup IDE floating\n");
661 return orstatus;
662 }
Kevin O'Connora5826b52009-10-24 17:57:29 -0400663 if (check_time(SpinupEnd)) {
Kevin O'Connor425f2122009-04-18 12:23:00 -0400664 dprintf(1, "powerup IDE time out\n");
665 return -1;
666 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400667 yield();
Kevin O'Connor425f2122009-04-18 12:23:00 -0400668 }
669 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
670 return status;
671}
672
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400673static void
Kevin O'Connora5826b52009-10-24 17:57:29 -0400674ata_detect(void *data)
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500675{
Kevin O'Connora5826b52009-10-24 17:57:29 -0400676 struct ata_channel_s *atachannel = data;
677 int startid = (atachannel - ATA_channels) * 2;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400678 struct drive_s dummy;
679 memset(&dummy, 0, sizeof(dummy));
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500680 // Device detection
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400681 int ataid, last_reset_ataid=-1;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400682 for (ataid=startid; ataid<startid+2; ataid++) {
Kevin O'Connorb1144362009-08-11 20:43:38 -0400683 u8 channel = ataid / 2;
684 u8 slave = ataid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500685
Kevin O'Connorc892b132009-08-11 21:59:37 -0400686 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400687 if (!iobase1)
688 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500689
Kevin O'Connor425f2122009-04-18 12:23:00 -0400690 // Wait for not-bsy.
Kevin O'Connora5826b52009-10-24 17:57:29 -0400691 int status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400692 if (status < 0)
693 continue;
694 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
695 outb(newdh, iobase1+ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400696 ndelay(400);
Kevin O'Connora5826b52009-10-24 17:57:29 -0400697 status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400698 if (status < 0)
699 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500700
Kevin O'Connor580e3322009-02-06 22:36:53 -0500701 // Check if ioport registers look valid.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400702 outb(newdh, iobase1+ATA_CB_DH);
703 u8 dh = inb(iobase1+ATA_CB_DH);
704 outb(0x55, iobase1+ATA_CB_SC);
705 outb(0xaa, iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400706 u8 sc = inb(iobase1+ATA_CB_SC);
707 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400708 dprintf(6, "ata_detect ataid=%d sc=%x sn=%x dh=%x\n"
709 , ataid, sc, sn, dh);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400710 if (sc != 0x55 || sn != 0xaa || dh != newdh)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400711 continue;
712
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400713 // Prepare new drive.
714 dummy.cntl_id = ataid;
Kevin O'Connorb1144362009-08-11 20:43:38 -0400715
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400716 // reset the channel
Kevin O'Connorb1144362009-08-11 20:43:38 -0400717 if (slave && ataid == last_reset_ataid + 1) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500718 // The drive was just reset - no need to reset it again.
719 } else {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400720 ata_reset(&dummy);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400721 last_reset_ataid = ataid;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500722 }
723
Kevin O'Connor580e3322009-02-06 22:36:53 -0500724 // check for ATAPI
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400725 u16 buffer[256];
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400726 struct drive_s *drive_g = init_drive_atapi(&dummy, buffer);
727 if (!drive_g) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400728 // Didn't find an ATAPI drive - look for ATA drive.
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400729 u8 st = inb(iobase1+ATA_CB_STAT);
730 if (!st)
731 // Status not set - can't be a valid drive.
732 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500733
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400734 // Wait for RDY.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400735 int ret = await_rdy(iobase1);
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400736 if (ret < 0)
737 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500738
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400739 // check for ATA.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400740 drive_g = init_drive_ata(&dummy, buffer);
741 if (!drive_g)
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400742 // No ATA drive found
743 continue;
744 }
Kevin O'Connor580e3322009-02-06 22:36:53 -0500745
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400746 u16 resetresult = buffer[93];
747 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
748 if (!slave && (resetresult & 0xdf61) == 0x4041)
749 // resetresult looks valid and device 0 is responding to
750 // device 1 requests - device 1 must not be present - skip
751 // detection.
Kevin O'Connorb1144362009-08-11 20:43:38 -0400752 ataid++;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500753 }
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500754}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400755
756static void
757ata_init()
758{
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400759 // Scan PCI bus for ATA adapters
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500760 int count=0;
761 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -0500762 foreachpci(bdf, max) {
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500763 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400764 continue;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400765 if (count >= ARRAY_SIZE(ATA_channels))
Kevin O'Connor89f67632009-02-11 20:16:49 -0500766 break;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400767
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500768 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400769 SET_GLOBAL(ATA_channels[count].irq, irq);
770 SET_GLOBAL(ATA_channels[count].pci_bdf, bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400771
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500772 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400773 u32 port1, port2;
774
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500775 if (prog_if & 1) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500776 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
777 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400778 } else {
779 port1 = 0x1f0;
780 port2 = 0x3f0;
781 }
Kevin O'Connorc892b132009-08-11 21:59:37 -0400782 SET_GLOBAL(ATA_channels[count].iobase1, port1);
783 SET_GLOBAL(ATA_channels[count].iobase2, port2);
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500784 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
785 , count, port1, port2, bdf, prog_if);
Kevin O'Connora5826b52009-10-24 17:57:29 -0400786 run_thread(ata_detect, &ATA_channels[count]);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400787 count++;
788
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500789 if (prog_if & 4) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500790 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
791 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400792 } else {
793 port1 = 0x170;
794 port2 = 0x370;
795 }
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500796 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
797 , count, port1, port2, bdf, prog_if);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400798 SET_GLOBAL(ATA_channels[count].iobase1, port1);
799 SET_GLOBAL(ATA_channels[count].iobase2, port2);
Kevin O'Connora5826b52009-10-24 17:57:29 -0400800 run_thread(ata_detect, &ATA_channels[count]);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400801 count++;
802 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400803}
804
805void
Kevin O'Connorc892b132009-08-11 21:59:37 -0400806ata_setup()
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400807{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400808 if (!CONFIG_ATA)
809 return;
810
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400811 dprintf(3, "init hard drives\n");
Kevin O'Connora5826b52009-10-24 17:57:29 -0400812
813 SpinupEnd = calc_future_tsc(IDE_TIMEOUT);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400814 ata_init();
Kevin O'Connorc1437612008-05-18 01:43:07 -0400815
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400816 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400817
Kevin O'Connord21c0892008-11-26 17:02:43 -0500818 enable_hwirq(14, entry_76);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400819}