blob: e30f1860a5b19274750163b43180d6cf2bbe0e02 [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);
196 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500197
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400198 // Disable interrupts
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500199 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400200
201 // Select device
Kevin O'Connor580e3322009-02-06 22:36:53 -0500202 int status = await_not_bsy(iobase1);
203 if (status < 0)
204 return status;
205 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
206 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
207 u8 olddh = inb(iobase1 + ATA_CB_DH);
208 outb(newdh, iobase1 + ATA_CB_DH);
209 if ((olddh ^ newdh) & (1<<4)) {
210 // Was a device change - wait for device to become not busy.
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400211 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500212 if (status < 0)
213 return status;
214 }
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400215
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400216 if (cmd->command & 0x04) {
217 outb(0x00, iobase1 + ATA_CB_FR);
218 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
219 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
220 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
221 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500222 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400223 outb(cmd->feature, iobase1 + ATA_CB_FR);
224 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
225 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
226 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
227 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400228 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500229
Kevin O'Connor580e3322009-02-06 22:36:53 -0500230 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400231 if (status < 0)
232 return status;
233
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500234 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500235 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
236 , status, inb(iobase1 + ATA_CB_ERR));
Kevin O'Connora05223c2008-06-28 12:15:57 -0400237 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400238 }
239 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500240 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400241 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500242 }
243
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400244 return 0;
245}
246
Kevin O'Connoree55c762008-05-13 00:18:20 -0400247
248/****************************************************************
249 * ATA transfers
250 ****************************************************************/
251
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400252// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400253// 'op->drive_g'.
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400254static int
255ata_transfer(struct disk_op_s *op, int iswrite, int blocksize)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400256{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400257 dprintf(16, "ata_transfer id=%p write=%d count=%d bs=%d buf=%p\n"
258 , op->drive_g, iswrite, op->count, blocksize, op->buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400259
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400260 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400261 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400262 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
263 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400264 int count = op->count;
265 void *buf_fl = op->buf_fl;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400266 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400267 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400268 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400269 // Write data to controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400270 dprintf(16, "Write sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500271 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400272 outsl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400273 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400274 outsw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500275 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400276 // Read data from controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400277 dprintf(16, "Read sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500278 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400279 insl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400280 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400281 insw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400282 }
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400283 buf_fl += blocksize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400284
Kevin O'Connor580e3322009-02-06 22:36:53 -0500285 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400286 if (status < 0) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400287 // Error
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400288 op->count -= count;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400289 return status;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400290 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400291
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400292 count--;
293 if (!count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400294 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500295 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
296 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400297 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500298 , status);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400299 op->count -= count;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400300 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500301 }
302 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400303
Kevin O'Connor580e3322009-02-06 22:36:53 -0500304 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
305 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400306 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400307 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500308 if (status != 0) {
309 dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400310 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400311 }
312
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500313 // Enable interrupts
314 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
315 return 0;
316}
317
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400318
319/****************************************************************
320 * ATA hard drive functions
321 ****************************************************************/
322
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400323// Read/write count blocks from a harddrive.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400324static int
325ata_cmd_data(struct disk_op_s *op, int iswrite, int command)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400326{
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400327 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400328
329 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400330 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400331
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400332 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400333 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
334 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400335 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400336 cmd.lba_mid2 = lba >> 32;
337 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400338
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400339 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400340 lba &= 0xffffff;
341 }
342
343 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400344 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400345 cmd.lba_low = lba;
346 cmd.lba_mid = lba >> 8;
347 cmd.lba_high = lba >> 16;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500348 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400349
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)
352 return ret;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400353 return ata_transfer(op, iswrite, DISK_SECTOR_SIZE);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400354}
355
356int
357process_ata_op(struct disk_op_s *op)
358{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400359 if (!CONFIG_ATA)
360 return 0;
361
Kevin O'Connor126eac62009-08-16 13:32:24 -0400362 int ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400363 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400364 case CMD_READ:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400365 ret = ata_cmd_data(op, 0, ATA_CMD_READ_SECTORS);
366 break;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400367 case CMD_WRITE:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400368 ret = ata_cmd_data(op, 1, ATA_CMD_WRITE_SECTORS);
369 break;
Kevin O'Connor42337662009-08-10 00:06:37 -0400370 default:
371 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400372 }
Kevin O'Connor126eac62009-08-16 13:32:24 -0400373 if (ret)
374 return DISK_RET_EBADTRACK;
375 return DISK_RET_SUCCESS;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400376}
377
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400378
379/****************************************************************
380 * ATAPI functions
381 ****************************************************************/
382
383// Low-level atapi command transmit function.
Kevin O'Connora9caeae2009-03-07 00:09:52 -0500384static int
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400385send_atapi_cmd(struct drive_s *drive_g, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500386{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400387 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400388 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400389 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
390 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500391
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400392 struct ata_pio_command cmd;
393 cmd.sector_count = 0;
394 cmd.feature = 0;
395 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400396 cmd.lba_mid = blocksize;
397 cmd.lba_high = blocksize >> 8;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500398 cmd.device = 0;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400399 cmd.command = ATA_CMD_PACKET;
400
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400401 int ret = send_cmd(drive_g, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400402 if (ret)
403 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500404
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400405 // Send command to device
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500406 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500407
Kevin O'Connor580e3322009-02-06 22:36:53 -0500408 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400409 if (status < 0)
410 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400411
Kevin O'Connor580e3322009-02-06 22:36:53 -0500412 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connorb30c4002009-05-05 21:47:20 -0400413 u8 err = inb(iobase1 + ATA_CB_ERR);
414 // skip "Not Ready"
415 if (err != 0x20)
416 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
417 , status, err);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500418 return -2;
419 }
420 if (!(status & ATA_CB_STAT_DRQ)) {
421 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
422 return -3;
423 }
424
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500425 return 0;
426}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500427
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400428// Read sectors from the cdrom.
429int
430cdrom_read(struct disk_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500431{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400432 u8 atacmd[12];
433 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connoree55c762008-05-13 00:18:20 -0400434 atacmd[0]=0x28; // READ command
435 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
436 atacmd[8]=(op->count & 0x00ff);
437 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
438 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
439 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
440 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400441
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400442 int ret = send_atapi_cmd(op->drive_g, atacmd, sizeof(atacmd)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400443 , CDROM_SECTOR_SIZE);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400444 if (ret)
445 return ret;
446
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400447 return ata_transfer(op, 0, CDROM_SECTOR_SIZE);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400448}
449
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400450int
451process_atapi_op(struct disk_op_s *op)
452{
Kevin O'Connor126eac62009-08-16 13:32:24 -0400453 int ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400454 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400455 case CMD_READ:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400456 ret = cdrom_read(op);
457 break;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400458 case CMD_FORMAT:
459 case CMD_WRITE:
460 return DISK_RET_EWRITEPROTECT;
Kevin O'Connor42337662009-08-10 00:06:37 -0400461 default:
462 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400463 }
Kevin O'Connor126eac62009-08-16 13:32:24 -0400464 if (ret)
465 return DISK_RET_EBADTRACK;
466 return DISK_RET_SUCCESS;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400467}
468
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400469// Send a simple atapi command to a drive.
470int
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400471ata_cmd_packet(struct drive_s *drive_g, u8 *cmdbuf, u8 cmdlen
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500472 , u32 length, void *buf_fl)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400473{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400474 int ret = send_atapi_cmd(drive_g, cmdbuf, cmdlen, length);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400475 if (ret)
476 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400477
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400478 struct disk_op_s dop;
479 memset(&dop, 0, sizeof(dop));
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400480 dop.drive_g = drive_g;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400481 dop.count = 1;
482 dop.buf_fl = buf_fl;
483
484 return ata_transfer(&dop, 0, length);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400485}
486
487
488/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500489 * ATA detect and init
490 ****************************************************************/
491
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400492// Extract the ATA/ATAPI version info.
493static int
494extract_version(u16 *buffer)
495{
496 // Extract ATA/ATAPI version.
497 u16 ataversion = buffer[80];
498 u8 version;
499 for (version=15; version>0; version--)
500 if (ataversion & (1<<version))
501 break;
502 return version;
503}
504
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500505// Extract common information from IDENTIFY commands.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500506static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400507extract_identify(struct drive_s *drive_g, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500508{
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500509 dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500510
511 // Read model name
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400512 char *model = drive_g->model;
513 int maxsize = ARRAY_SIZE(drive_g->model);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500514 int i;
Kevin O'Connorab515602009-02-11 22:22:15 -0500515 for (i=0; i<maxsize/2; i++) {
516 u16 v = buffer[27+i];
517 model[i*2] = v >> 8;
518 model[i*2+1] = v & 0xff;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500519 }
520 model[maxsize-1] = 0x00;
521
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500522 // Trim trailing spaces from model name.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500523 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
524 model[i] = 0x00;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500525
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500526 // Common flags.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400527 SET_GLOBAL(drive_g->removable, (buffer[0] & 0x80) ? 1 : 0);
528 SET_GLOBAL(drive_g->cntl_info, extract_version(buffer));
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400529}
530
531void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400532describe_atapi(struct drive_s *drive_g)
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400533{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400534 u8 ataid = drive_g->cntl_id;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400535 u8 channel = ataid / 2;
536 u8 slave = ataid % 2;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400537 u8 version = drive_g->cntl_info;
538 int iscd = drive_g->floppy_type;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400539 printf("ata%d-%d: %s ATAPI-%d %s", channel, slave
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400540 , drive_g->model, version
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400541 , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500542}
543
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400544static struct drive_s *
545init_drive_atapi(struct drive_s *dummy, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500546{
547 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400548 memset(buffer, 0, DISK_SECTOR_SIZE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500549 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400550 memset(&dop, 0, sizeof(dop));
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400551 dop.drive_g = dummy;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500552 dop.count = 1;
553 dop.lba = 1;
554 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400555 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE_PACKET);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500556 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400557 return NULL;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500558
559 // Success - setup as ATAPI.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400560 struct drive_s *drive_g = allocDrive();
561 if (! drive_g)
562 return NULL;
563 SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
564 extract_identify(drive_g, buffer);
565 SET_GLOBAL(drive_g->type, DTYPE_ATAPI);
566 SET_GLOBAL(drive_g->blksize, CDROM_SECTOR_SIZE);
567 SET_GLOBAL(drive_g->sectors, (u64)-1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400568 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400569 SET_GLOBAL(drive_g->floppy_type, iscd);
Kevin O'Connor42337662009-08-10 00:06:37 -0400570
571 // fill cdidmap
Kevin O'Connorc892b132009-08-11 21:59:37 -0400572 if (iscd)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400573 map_cd_drive(drive_g);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500574
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400575 return drive_g;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500576}
577
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400578void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400579describe_ata(struct drive_s *drive_g)
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400580{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400581 u8 ataid = drive_g->cntl_id;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400582 u8 channel = ataid / 2;
583 u8 slave = ataid % 2;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400584 u64 sectors = drive_g->sectors;
585 u8 version = drive_g->cntl_info;
586 char *model = drive_g->model;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400587 printf("ata%d-%d: %s ATA-%d Hard-Disk", channel, slave, model, version);
588 u64 sizeinmb = sectors >> 11;
589 if (sizeinmb < (1 << 16))
590 printf(" (%u MiBytes)", (u32)sizeinmb);
591 else
592 printf(" (%u GiBytes)", (u32)(sizeinmb >> 10));
593}
594
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400595static struct drive_s *
596init_drive_ata(struct drive_s *dummy, u16 *buffer)
Kevin O'Connorc1437612008-05-18 01:43:07 -0400597{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500598 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400599 memset(buffer, 0, DISK_SECTOR_SIZE);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500600 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400601 memset(&dop, 0, sizeof(dop));
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400602 dop.drive_g = dummy;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500603 dop.count = 1;
604 dop.lba = 1;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500605 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400606 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400607 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400608 return NULL;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500609
610 // Success - setup as ATA.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400611 struct drive_s *drive_g = allocDrive();
612 if (! drive_g)
613 return NULL;
614 SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
615 extract_identify(drive_g, buffer);
616 SET_GLOBAL(drive_g->type, DTYPE_ATA);
617 SET_GLOBAL(drive_g->blksize, DISK_SECTOR_SIZE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400618
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400619 SET_GLOBAL(drive_g->pchs.cylinders, buffer[1]);
620 SET_GLOBAL(drive_g->pchs.heads, buffer[3]);
621 SET_GLOBAL(drive_g->pchs.spt, buffer[6]);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400622
623 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500624 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
625 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400626 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500627 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400628 SET_GLOBAL(drive_g->sectors, sectors);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400629
630 // Setup disk geometry translation.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400631 setup_translation(drive_g);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400632
Kevin O'Connor0a924122009-02-08 19:43:47 -0500633 // Register with bcv system.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400634 add_bcv_internal(drive_g);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400635
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400636 return drive_g;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400637}
638
Kevin O'Connora5826b52009-10-24 17:57:29 -0400639static u64 SpinupEnd;
640
Kevin O'Connor425f2122009-04-18 12:23:00 -0400641static int
Kevin O'Connora5826b52009-10-24 17:57:29 -0400642powerup_await_non_bsy(u16 base)
Kevin O'Connor425f2122009-04-18 12:23:00 -0400643{
644 u8 orstatus = 0;
645 u8 status;
646 for (;;) {
647 status = inb(base+ATA_CB_STAT);
648 if (!(status & ATA_CB_STAT_BSY))
649 break;
650 orstatus |= status;
651 if (orstatus == 0xff) {
652 dprintf(1, "powerup IDE floating\n");
653 return orstatus;
654 }
Kevin O'Connora5826b52009-10-24 17:57:29 -0400655 if (check_time(SpinupEnd)) {
Kevin O'Connor425f2122009-04-18 12:23:00 -0400656 dprintf(1, "powerup IDE time out\n");
657 return -1;
658 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400659 yield();
Kevin O'Connor425f2122009-04-18 12:23:00 -0400660 }
661 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
662 return status;
663}
664
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400665static void
Kevin O'Connora5826b52009-10-24 17:57:29 -0400666ata_detect(void *data)
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500667{
Kevin O'Connora5826b52009-10-24 17:57:29 -0400668 struct ata_channel_s *atachannel = data;
669 int startid = (atachannel - ATA_channels) * 2;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400670 struct drive_s dummy;
671 memset(&dummy, 0, sizeof(dummy));
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500672 // Device detection
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400673 int ataid, last_reset_ataid=-1;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400674 for (ataid=startid; ataid<startid+2; ataid++) {
Kevin O'Connorb1144362009-08-11 20:43:38 -0400675 u8 channel = ataid / 2;
676 u8 slave = ataid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500677
Kevin O'Connorc892b132009-08-11 21:59:37 -0400678 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400679 if (!iobase1)
680 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500681
Kevin O'Connor425f2122009-04-18 12:23:00 -0400682 // Wait for not-bsy.
Kevin O'Connora5826b52009-10-24 17:57:29 -0400683 int status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400684 if (status < 0)
685 continue;
686 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
687 outb(newdh, iobase1+ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400688 ndelay(400);
Kevin O'Connora5826b52009-10-24 17:57:29 -0400689 status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400690 if (status < 0)
691 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500692
Kevin O'Connor580e3322009-02-06 22:36:53 -0500693 // Check if ioport registers look valid.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400694 outb(newdh, iobase1+ATA_CB_DH);
695 u8 dh = inb(iobase1+ATA_CB_DH);
696 outb(0x55, iobase1+ATA_CB_SC);
697 outb(0xaa, iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400698 u8 sc = inb(iobase1+ATA_CB_SC);
699 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400700 dprintf(6, "ata_detect ataid=%d sc=%x sn=%x dh=%x\n"
701 , ataid, sc, sn, dh);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400702 if (sc != 0x55 || sn != 0xaa || dh != newdh)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400703 continue;
704
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400705 // Prepare new drive.
706 dummy.cntl_id = ataid;
Kevin O'Connorb1144362009-08-11 20:43:38 -0400707
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400708 // reset the channel
Kevin O'Connorb1144362009-08-11 20:43:38 -0400709 if (slave && ataid == last_reset_ataid + 1) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500710 // The drive was just reset - no need to reset it again.
711 } else {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400712 ata_reset(&dummy);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400713 last_reset_ataid = ataid;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500714 }
715
Kevin O'Connor580e3322009-02-06 22:36:53 -0500716 // check for ATAPI
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400717 u16 buffer[256];
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400718 struct drive_s *drive_g = init_drive_atapi(&dummy, buffer);
719 if (!drive_g) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400720 // Didn't find an ATAPI drive - look for ATA drive.
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400721 u8 st = inb(iobase1+ATA_CB_STAT);
722 if (!st)
723 // Status not set - can't be a valid drive.
724 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500725
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400726 // Wait for RDY.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400727 int ret = await_rdy(iobase1);
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400728 if (ret < 0)
729 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500730
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400731 // check for ATA.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400732 drive_g = init_drive_ata(&dummy, buffer);
733 if (!drive_g)
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400734 // No ATA drive found
735 continue;
736 }
Kevin O'Connor580e3322009-02-06 22:36:53 -0500737
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400738 u16 resetresult = buffer[93];
739 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
740 if (!slave && (resetresult & 0xdf61) == 0x4041)
741 // resetresult looks valid and device 0 is responding to
742 // device 1 requests - device 1 must not be present - skip
743 // detection.
Kevin O'Connorb1144362009-08-11 20:43:38 -0400744 ataid++;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500745 }
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500746}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400747
748static void
749ata_init()
750{
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400751 // Scan PCI bus for ATA adapters
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500752 int count=0;
753 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -0500754 foreachpci(bdf, max) {
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500755 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400756 continue;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400757 if (count >= ARRAY_SIZE(ATA_channels))
Kevin O'Connor89f67632009-02-11 20:16:49 -0500758 break;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400759
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500760 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400761 SET_GLOBAL(ATA_channels[count].irq, irq);
762 SET_GLOBAL(ATA_channels[count].pci_bdf, bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400763
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500764 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400765 u32 port1, port2;
766
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500767 if (prog_if & 1) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500768 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
769 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400770 } else {
771 port1 = 0x1f0;
772 port2 = 0x3f0;
773 }
Kevin O'Connorc892b132009-08-11 21:59:37 -0400774 SET_GLOBAL(ATA_channels[count].iobase1, port1);
775 SET_GLOBAL(ATA_channels[count].iobase2, port2);
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500776 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
777 , count, port1, port2, bdf, prog_if);
Kevin O'Connora5826b52009-10-24 17:57:29 -0400778 run_thread(ata_detect, &ATA_channels[count]);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400779 count++;
780
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500781 if (prog_if & 4) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500782 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
783 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400784 } else {
785 port1 = 0x170;
786 port2 = 0x370;
787 }
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500788 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
789 , count, port1, port2, bdf, prog_if);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400790 SET_GLOBAL(ATA_channels[count].iobase1, port1);
791 SET_GLOBAL(ATA_channels[count].iobase2, port2);
Kevin O'Connora5826b52009-10-24 17:57:29 -0400792 run_thread(ata_detect, &ATA_channels[count]);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400793 count++;
794 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400795}
796
797void
Kevin O'Connorc892b132009-08-11 21:59:37 -0400798ata_setup()
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400799{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400800 if (!CONFIG_ATA)
801 return;
802
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400803 dprintf(3, "init hard drives\n");
Kevin O'Connora5826b52009-10-24 17:57:29 -0400804
805 SpinupEnd = calc_future_tsc(IDE_TIMEOUT);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400806 ata_init();
Kevin O'Connorc1437612008-05-18 01:43:07 -0400807
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400808 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400809
Kevin O'Connord21c0892008-11-26 17:02:43 -0500810 enable_hwirq(14, entry_76);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400811}