blob: 2996728a672ff81a5c5a286021488c1b76c7b04b [file] [log] [blame]
Kevin O'Connorc09492e2008-03-01 13:38:07 -05001// Low level ATA disk access
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// 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'Connor4524bf72008-12-31 00:31:03 -050019#include "atabits.h" // ATA_CB_STAT
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050020
21#define TIMEOUT 0
22#define BSY 1
23#define NOT_BSY 2
24#define NOT_BSY_DRQ 3
25#define NOT_BSY_NOT_DRQ 4
26#define NOT_BSY_RDY 5
27
Kevin O'Connora6b9f712008-03-29 12:53:57 -040028#define IDE_SECTOR_SIZE 512
29#define CDROM_SECTOR_SIZE 2048
30
Kevin O'Connor425f2122009-04-18 12:23:00 -040031#define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050032
Kevin O'Connor92f95b02008-12-29 20:42:40 -050033struct ata_s ATA VAR16_32;
Kevin O'Connor609da232008-12-28 23:18:57 -050034
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050035
Kevin O'Connora6b9f712008-03-29 12:53:57 -040036/****************************************************************
37 * Helper functions
38 ****************************************************************/
39
40// Wait for the specified ide state
Kevin O'Connor580e3322009-02-06 22:36:53 -050041static inline int
42await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050043{
Kevin O'Connor4e6c9702008-12-13 10:45:50 -050044 u64 end = calc_future_tsc(timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050045 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -040046 u8 status = inb(base+ATA_CB_STAT);
Kevin O'Connor580e3322009-02-06 22:36:53 -050047 if ((status & mask) == flags)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040048 return status;
Kevin O'Connorf3587592009-02-15 13:02:56 -050049 if (rdtscll() > end) {
Kevin O'Connor580e3322009-02-06 22:36:53 -050050 dprintf(1, "IDE time out\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050051 return -1;
52 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050053 }
Kevin O'Connor580e3322009-02-06 22:36:53 -050054}
55
56// Wait for the device to be not-busy.
57static int
58await_not_bsy(u16 base)
59{
60 return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
61}
62
63// Wait for the device to be ready.
64static int
65await_rdy(u16 base)
66{
67 return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050068}
69
Kevin O'Connora6b9f712008-03-29 12:53:57 -040070// Wait for ide state - pauses for one ata cycle first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050071static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050072pause_await_not_bsy(u16 iobase1, u16 iobase2)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040073{
74 // Wait one PIO transfer cycle.
75 inb(iobase2 + ATA_CB_ASTAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050076
Kevin O'Connor580e3322009-02-06 22:36:53 -050077 return await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -040078}
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050079
Kevin O'Connoref2822a2008-06-07 15:23:11 -040080// Wait for ide state - pause for 400ns first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050081static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050082ndelay_await_not_bsy(u16 iobase1)
Kevin O'Connoref2822a2008-06-07 15:23:11 -040083{
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050084 ndelay(400);
Kevin O'Connor580e3322009-02-06 22:36:53 -050085 return await_not_bsy(iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -040086}
87
Kevin O'Connora6b9f712008-03-29 12:53:57 -040088// Reset a drive
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050089void
Kevin O'Connora6b9f712008-03-29 12:53:57 -040090ata_reset(int driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050091{
Kevin O'Connoref2822a2008-06-07 15:23:11 -040092 u8 channel = driveid / 2;
93 u8 slave = driveid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -050094 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
95 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050096
Kevin O'Connor580e3322009-02-06 22:36:53 -050097 dprintf(6, "ata_reset driveid=%d\n", driveid);
98 // Pulse SRST
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050099 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 -0500100 udelay(5);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500101 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500102 mdelay(2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500103
Kevin O'Connor580e3322009-02-06 22:36:53 -0500104 // wait for device to become not busy.
105 int status = await_not_bsy(iobase1);
106 if (status < 0)
107 goto done;
108 if (slave) {
109 // Change device.
110 u64 end = calc_future_tsc(IDE_TIMEOUT);
111 for (;;) {
112 outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400113 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500114 if (status < 0)
115 goto done;
116 if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
117 break;
118 // Change drive request failed to take effect - retry.
Kevin O'Connorf3587592009-02-15 13:02:56 -0500119 if (rdtscll() > end) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500120 dprintf(1, "ata_reset slave time out\n");
121 goto done;
122 }
123 }
Kevin O'Connor2e3eeeb2008-06-12 22:29:30 -0400124 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500125
Kevin O'Connor580e3322009-02-06 22:36:53 -0500126 // On a user-reset request, wait for RDY if it is an ATA device.
127 u8 type=GET_GLOBAL(ATA.devices[driveid].type);
128 if (type == ATA_TYPE_ATA)
129 status = await_rdy(iobase1);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400130
Kevin O'Connor580e3322009-02-06 22:36:53 -0500131done:
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500132 // Enable interrupts
133 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500134
135 dprintf(6, "ata_reset exit status=%x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500136}
137
Kevin O'Connoree55c762008-05-13 00:18:20 -0400138
139/****************************************************************
140 * ATA send command
141 ****************************************************************/
142
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400143struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400144 u8 feature;
145 u8 sector_count;
146 u8 lba_low;
147 u8 lba_mid;
148 u8 lba_high;
149 u8 device;
150 u8 command;
151
152 u8 sector_count2;
153 u8 lba_low2;
154 u8 lba_mid2;
155 u8 lba_high2;
156};
157
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400158// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400159static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400160send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500161{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400162 u8 channel = driveid / 2;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500163 u8 slave = driveid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500164 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
165 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500166
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400167 // Disable interrupts
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500168 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400169
170 // Select device
Kevin O'Connor580e3322009-02-06 22:36:53 -0500171 int status = await_not_bsy(iobase1);
172 if (status < 0)
173 return status;
174 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
175 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
176 u8 olddh = inb(iobase1 + ATA_CB_DH);
177 outb(newdh, iobase1 + ATA_CB_DH);
178 if ((olddh ^ newdh) & (1<<4)) {
179 // Was a device change - wait for device to become not busy.
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400180 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500181 if (status < 0)
182 return status;
183 }
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400184
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400185 if (cmd->command & 0x04) {
186 outb(0x00, iobase1 + ATA_CB_FR);
187 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
188 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
189 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
190 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500191 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400192 outb(cmd->feature, iobase1 + ATA_CB_FR);
193 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
194 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
195 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
196 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400197 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500198
Kevin O'Connor580e3322009-02-06 22:36:53 -0500199 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400200 if (status < 0)
201 return status;
202
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500203 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500204 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
205 , status, inb(iobase1 + ATA_CB_ERR));
Kevin O'Connora05223c2008-06-28 12:15:57 -0400206 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400207 }
208 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500209 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400210 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500211 }
212
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400213 return 0;
214}
215
Kevin O'Connoree55c762008-05-13 00:18:20 -0400216
217/****************************************************************
218 * ATA transfers
219 ****************************************************************/
220
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400221// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
222// 'op->driveid'.
223static int
224ata_transfer(struct disk_op_s *op, int iswrite, int blocksize)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400225{
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400226 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d buf=%p\n"
227 , op->driveid, iswrite, op->count, blocksize, op->buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400228
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400229 u8 channel = op->driveid / 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500230 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
231 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400232 int count = op->count;
233 void *buf_fl = op->buf_fl;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400234 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400235 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400236 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400237 // Write data to controller
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400238 dprintf(16, "Write sector id=%d dest=%p\n", op->driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500239 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400240 outsl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400241 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400242 outsw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500243 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400244 // Read data from controller
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400245 dprintf(16, "Read sector id=%d dest=%p\n", op->driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500246 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400247 insl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400248 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400249 insw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400250 }
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400251 buf_fl += blocksize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400252
Kevin O'Connor580e3322009-02-06 22:36:53 -0500253 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400254 if (status < 0) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400255 // Error
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400256 op->count -= count;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400257 return status;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400258 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400259
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400260 count--;
261 if (!count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400262 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500263 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
264 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400265 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500266 , status);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400267 op->count -= count;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400268 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500269 }
270 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400271
Kevin O'Connor580e3322009-02-06 22:36:53 -0500272 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
273 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400274 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400275 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500276 if (status != 0) {
277 dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400278 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400279 }
280
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500281 // Enable interrupts
282 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
283 return 0;
284}
285
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400286
287/****************************************************************
288 * ATA hard drive functions
289 ****************************************************************/
290
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400291// Read/write count blocks from a harddrive.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400292static int
293ata_cmd_data(struct disk_op_s *op, int iswrite, int command)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400294{
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400295 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400296
297 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400298 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400299
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400300 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400301 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
302 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400303 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400304 cmd.lba_mid2 = lba >> 32;
305 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400306
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400307 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400308 lba &= 0xffffff;
309 }
310
311 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400312 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400313 cmd.lba_low = lba;
314 cmd.lba_mid = lba >> 8;
315 cmd.lba_high = lba >> 16;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500316 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400317
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400318 int ret = send_cmd(op->driveid, &cmd);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400319 if (ret)
320 return ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400321 return ata_transfer(op, iswrite, IDE_SECTOR_SIZE);
322}
323
324int
325process_ata_op(struct disk_op_s *op)
326{
327 switch (op->command) {
328 default:
329 return 0;
330 case CMD_RESET:
331 ata_reset(op->driveid);
332 return 0;
333 case CMD_READ:
334 return ata_cmd_data(op, 0, ATA_CMD_READ_SECTORS);
335 case CMD_WRITE:
336 return ata_cmd_data(op, 1, ATA_CMD_WRITE_SECTORS);
337 }
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400338}
339
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400340
341/****************************************************************
342 * ATAPI functions
343 ****************************************************************/
344
345// Low-level atapi command transmit function.
Kevin O'Connora9caeae2009-03-07 00:09:52 -0500346static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400347send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500348{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400349 u8 channel = driveid / 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500350 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
351 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500352
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400353 struct ata_pio_command cmd;
354 cmd.sector_count = 0;
355 cmd.feature = 0;
356 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400357 cmd.lba_mid = blocksize;
358 cmd.lba_high = blocksize >> 8;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500359 cmd.device = 0;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400360 cmd.command = ATA_CMD_PACKET;
361
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400362 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400363 if (ret)
364 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500365
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400366 // Send command to device
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500367 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500368
Kevin O'Connor580e3322009-02-06 22:36:53 -0500369 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400370 if (status < 0)
371 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400372
Kevin O'Connor580e3322009-02-06 22:36:53 -0500373 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connorb30c4002009-05-05 21:47:20 -0400374 u8 err = inb(iobase1 + ATA_CB_ERR);
375 // skip "Not Ready"
376 if (err != 0x20)
377 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
378 , status, err);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500379 return -2;
380 }
381 if (!(status & ATA_CB_STAT_DRQ)) {
382 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
383 return -3;
384 }
385
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500386 return 0;
387}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500388
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400389// Read sectors from the cdrom.
390int
391cdrom_read(struct disk_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500392{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400393 u8 atacmd[12];
394 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connoree55c762008-05-13 00:18:20 -0400395 atacmd[0]=0x28; // READ command
396 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
397 atacmd[8]=(op->count & 0x00ff);
398 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
399 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
400 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
401 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400402
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400403 int ret = send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
404 , CDROM_SECTOR_SIZE);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400405 if (ret)
406 return ret;
407
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400408 return ata_transfer(op, 0, CDROM_SECTOR_SIZE);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400409}
410
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400411int
412process_atapi_op(struct disk_op_s *op)
413{
414 switch (op->command) {
415 default:
416 return 0;
417 case CMD_RESET:
418 ata_reset(op->driveid);
419 return 0;
420 case CMD_READ:
421 return cdrom_read(op);
422 }
423}
424
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400425// Send a simple atapi command to a drive.
426int
427ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500428 , u32 length, void *buf_fl)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400429{
430 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
431 if (ret)
432 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400433
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400434 struct disk_op_s dop;
435 memset(&dop, 0, sizeof(dop));
436 dop.driveid = driveid;
437 dop.count = 1;
438 dop.buf_fl = buf_fl;
439
440 return ata_transfer(&dop, 0, length);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400441}
442
443
444/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500445 * Disk geometry translation
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400446 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500447
Kevin O'Connorc1437612008-05-18 01:43:07 -0400448static u8
449get_translation(int driveid)
450{
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400451 if (! CONFIG_COREBOOT) {
452 // Emulators pass in the translation info via nvram.
453 u8 channel = driveid / 2;
454 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
455 translation >>= 2 * (driveid % 4);
456 translation &= 0x03;
457 return translation;
458 }
459
460 // On COREBOOT, use a heuristic to determine translation type.
Kevin O'Connor609da232008-12-28 23:18:57 -0500461 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
462 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
463 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400464
465 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
466 return ATA_TRANSLATION_NONE;
467 if (cylinders * heads <= 131072)
468 return ATA_TRANSLATION_LARGE;
469 return ATA_TRANSLATION_LBA;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400470}
471
472static void
473setup_translation(int driveid)
474{
475 u8 translation = get_translation(driveid);
Kevin O'Connor609da232008-12-28 23:18:57 -0500476 SET_GLOBAL(ATA.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400477
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400478 u8 channel = driveid / 2;
479 u8 slave = driveid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500480 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
481 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
482 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
483 u64 sectors = GET_GLOBAL(ATA.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400484
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400485 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400486 , channel, slave, cylinders, heads, spt);
487 switch (translation) {
488 case ATA_TRANSLATION_NONE:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400489 dprintf(1, "none");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400490 break;
491 case ATA_TRANSLATION_LBA:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400492 dprintf(1, "lba");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400493 spt = 63;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400494 if (sectors > 63*255*1024) {
495 heads = 255;
496 cylinders = 1024;
497 break;
498 }
499 u32 sect = (u32)sectors / 63;
500 heads = sect / 1024;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400501 if (heads>128)
502 heads = 255;
503 else if (heads>64)
504 heads = 128;
505 else if (heads>32)
506 heads = 64;
507 else if (heads>16)
508 heads = 32;
509 else
510 heads = 16;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400511 cylinders = sect / heads;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400512 break;
513 case ATA_TRANSLATION_RECHS:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400514 dprintf(1, "r-echs");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400515 // Take care not to overflow
516 if (heads==16) {
517 if (cylinders>61439)
518 cylinders=61439;
519 heads=15;
520 cylinders = (u16)((u32)(cylinders)*16/15);
521 }
522 // then go through the large bitshift process
523 case ATA_TRANSLATION_LARGE:
524 if (translation == ATA_TRANSLATION_LARGE)
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400525 dprintf(1, "large");
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400526 while (cylinders > 1024) {
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400527 cylinders >>= 1;
528 heads <<= 1;
529
530 // If we max out the head count
531 if (heads > 127)
532 break;
533 }
534 break;
535 }
536 // clip to 1024 cylinders in lchs
537 if (cylinders > 1024)
538 cylinders = 1024;
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400539 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400540
Kevin O'Connor609da232008-12-28 23:18:57 -0500541 SET_GLOBAL(ATA.devices[driveid].lchs.heads, heads);
542 SET_GLOBAL(ATA.devices[driveid].lchs.cylinders, cylinders);
543 SET_GLOBAL(ATA.devices[driveid].lchs.spt, spt);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400544}
545
Kevin O'Connor0a924122009-02-08 19:43:47 -0500546
547/****************************************************************
548 * ATA detect and init
549 ****************************************************************/
550
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500551// Extract common information from IDENTIFY commands.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500552static void
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500553extract_identify(int driveid, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500554{
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500555 dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500556
557 // Read model name
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500558 char *model = ATA.devices[driveid].model;
559 int maxsize = ARRAY_SIZE(ATA.devices[driveid].model);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500560 int i;
Kevin O'Connorab515602009-02-11 22:22:15 -0500561 for (i=0; i<maxsize/2; i++) {
562 u16 v = buffer[27+i];
563 model[i*2] = v >> 8;
564 model[i*2+1] = v & 0xff;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500565 }
566 model[maxsize-1] = 0x00;
567
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500568 // Trim trailing spaces from model name.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500569 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
570 model[i] = 0x00;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500571
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500572 // Extract ATA/ATAPI version.
Kevin O'Connorab515602009-02-11 22:22:15 -0500573 u16 ataversion = buffer[80];
Kevin O'Connor0a924122009-02-08 19:43:47 -0500574 u8 version;
575 for (version=15; version>0; version--)
576 if (ataversion & (1<<version))
577 break;
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500578 ATA.devices[driveid].version = version;
579
580 // Common flags.
581 SET_GLOBAL(ATA.devices[driveid].removable, (buffer[0] & 0x80) ? 1 : 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500582}
583
584static int
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400585init_drive_atapi(int driveid, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500586{
587 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400588 memset(buffer, 0, IDE_SECTOR_SIZE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500589 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400590 memset(&dop, 0, sizeof(dop));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500591 dop.driveid = driveid;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500592 dop.count = 1;
593 dop.lba = 1;
594 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400595 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE_PACKET);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500596 if (ret)
597 return ret;
598
599 // Success - setup as ATAPI.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500600 extract_identify(driveid, buffer);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500601 SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATAPI);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500602 SET_GLOBAL(ATA.devices[driveid].device, (buffer[0] >> 8) & 0x1f);
603 SET_GLOBAL(ATA.devices[driveid].blksize, CDROM_SECTOR_SIZE);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400604 SET_GLOBAL(ATA.devices[driveid].sectors, (u64)-1);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500605
606 // fill cdidmap
607 u8 cdcount = GET_GLOBAL(ATA.cdcount);
608 SET_GLOBAL(ATA.idmap[1][cdcount], driveid);
609 SET_GLOBAL(ATA.cdcount, cdcount+1);
610
611 // Report drive info to user.
612 u8 channel = driveid / 2;
613 u8 slave = driveid % 2;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500614 printf("ata%d-%d: %s ATAPI-%d %s\n", channel, slave
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500615 , ATA.devices[driveid].model, ATA.devices[driveid].version
Kevin O'Connor6a46a422009-02-17 22:57:37 -0500616 , (ATA.devices[driveid].device == ATA_DEVICE_CDROM
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500617 ? "CD-Rom/DVD-Rom" : "Device"));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500618
619 return 0;
620}
621
Kevin O'Connor580e3322009-02-06 22:36:53 -0500622static int
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400623init_drive_ata(int driveid, u16 *buffer)
Kevin O'Connorc1437612008-05-18 01:43:07 -0400624{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500625 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400626 memset(buffer, 0, IDE_SECTOR_SIZE);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500627 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400628 memset(&dop, 0, sizeof(dop));
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500629 dop.driveid = driveid;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500630 dop.count = 1;
631 dop.lba = 1;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500632 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400633 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400634 if (ret)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500635 return ret;
636
637 // Success - setup as ATA.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500638 extract_identify(driveid, buffer);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500639 SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATA);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500640 SET_GLOBAL(ATA.devices[driveid].device, ATA_DEVICE_HD);
641 SET_GLOBAL(ATA.devices[driveid].blksize, IDE_SECTOR_SIZE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400642
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500643 SET_GLOBAL(ATA.devices[driveid].pchs.cylinders, buffer[1]);
644 SET_GLOBAL(ATA.devices[driveid].pchs.heads, buffer[3]);
645 SET_GLOBAL(ATA.devices[driveid].pchs.spt, buffer[6]);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400646
647 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500648 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
649 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400650 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500651 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connor609da232008-12-28 23:18:57 -0500652 SET_GLOBAL(ATA.devices[driveid].sectors, sectors);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400653
654 // Setup disk geometry translation.
655 setup_translation(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400656
Kevin O'Connorc1437612008-05-18 01:43:07 -0400657 // Report drive info to user.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500658 u8 channel = driveid / 2;
659 u8 slave = driveid % 2;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500660 char *model = ATA.devices[driveid].model;
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500661 printf("ata%d-%d: %s ATA-%d Hard-Disk ", channel, slave, model
662 , ATA.devices[driveid].version);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500663 u64 sizeinmb = sectors >> 11;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400664 if (sizeinmb < (1 << 16))
Kevin O'Connor0a924122009-02-08 19:43:47 -0500665 printf("(%u MiBytes)\n", (u32)sizeinmb);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400666 else
Kevin O'Connor0a924122009-02-08 19:43:47 -0500667 printf("(%u GiBytes)\n", (u32)(sizeinmb >> 10));
668
669 // Register with bcv system.
670 add_bcv_hd(driveid, model);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400671
Kevin O'Connor580e3322009-02-06 22:36:53 -0500672 return 0;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400673}
674
Kevin O'Connor425f2122009-04-18 12:23:00 -0400675static int
676powerup_await_non_bsy(u16 base, u64 end)
677{
678 u8 orstatus = 0;
679 u8 status;
680 for (;;) {
681 status = inb(base+ATA_CB_STAT);
682 if (!(status & ATA_CB_STAT_BSY))
683 break;
684 orstatus |= status;
685 if (orstatus == 0xff) {
686 dprintf(1, "powerup IDE floating\n");
687 return orstatus;
688 }
689 if (rdtscll() > end) {
690 dprintf(1, "powerup IDE time out\n");
691 return -1;
692 }
693 }
694 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
695 return status;
696}
697
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400698static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500699ata_detect()
700{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500701 // Device detection
Kevin O'Connor425f2122009-04-18 12:23:00 -0400702 u64 end = calc_future_tsc(IDE_TIMEOUT);
Kevin O'Connor5eac1dd2009-02-07 00:03:11 -0500703 int driveid, last_reset_driveid=-1;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400704 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
705 u8 channel = driveid / 2;
706 u8 slave = driveid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500707
Kevin O'Connor609da232008-12-28 23:18:57 -0500708 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400709 if (!iobase1)
710 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500711
Kevin O'Connor425f2122009-04-18 12:23:00 -0400712 // Wait for not-bsy.
713 int status = powerup_await_non_bsy(iobase1, end);
714 if (status < 0)
715 continue;
716 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
717 outb(newdh, iobase1+ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400718 ndelay(400);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400719 status = powerup_await_non_bsy(iobase1, end);
720 if (status < 0)
721 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500722
Kevin O'Connor580e3322009-02-06 22:36:53 -0500723 // Check if ioport registers look valid.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400724 outb(newdh, iobase1+ATA_CB_DH);
725 u8 dh = inb(iobase1+ATA_CB_DH);
726 outb(0x55, iobase1+ATA_CB_SC);
727 outb(0xaa, iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400728 u8 sc = inb(iobase1+ATA_CB_SC);
729 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400730 dprintf(6, "ata_detect drive=%d sc=%x sn=%x dh=%x\n"
731 , driveid, sc, sn, dh);
732 if (sc != 0x55 || sn != 0xaa || dh != newdh)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400733 continue;
734
735 // reset the channel
Kevin O'Connor580e3322009-02-06 22:36:53 -0500736 if (slave && driveid == last_reset_driveid + 1) {
737 // The drive was just reset - no need to reset it again.
738 } else {
739 ata_reset(driveid);
740 last_reset_driveid = driveid;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500741 }
742
Kevin O'Connor580e3322009-02-06 22:36:53 -0500743 // check for ATAPI
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400744 u16 buffer[256];
745 int ret = init_drive_atapi(driveid, buffer);
746 if (!ret) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500747 // Found an ATAPI drive.
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400748 } else {
749 u8 st = inb(iobase1+ATA_CB_STAT);
750 if (!st)
751 // Status not set - can't be a valid drive.
752 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500753
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400754 // Wait for RDY.
755 ret = await_rdy(iobase1);
756 if (ret < 0)
757 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500758
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400759 // check for ATA.
760 ret = init_drive_ata(driveid, buffer);
761 if (ret)
762 // No ATA drive found
763 continue;
764 }
Kevin O'Connor580e3322009-02-06 22:36:53 -0500765
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400766 u16 resetresult = buffer[93];
767 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
768 if (!slave && (resetresult & 0xdf61) == 0x4041)
769 // resetresult looks valid and device 0 is responding to
770 // device 1 requests - device 1 must not be present - skip
771 // detection.
772 driveid++;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500773 }
774
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500775 printf("\n");
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500776}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400777
778static void
779ata_init()
780{
Kevin O'Connoref3d8822009-02-05 19:51:12 -0500781 memset(&ATA, 0, sizeof(ATA));
782
Kevin O'Connorc1437612008-05-18 01:43:07 -0400783 // hdidmap and cdidmap init.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400784 u8 device;
785 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
Kevin O'Connor609da232008-12-28 23:18:57 -0500786 SET_GLOBAL(ATA.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
787 SET_GLOBAL(ATA.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400788 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400789
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400790 // Scan PCI bus for ATA adapters
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500791 int count=0;
792 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -0500793 foreachpci(bdf, max) {
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500794 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400795 continue;
Kevin O'Connor89f67632009-02-11 20:16:49 -0500796 if (count >= ARRAY_SIZE(ATA.channels))
797 break;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400798
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500799 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor609da232008-12-28 23:18:57 -0500800 SET_GLOBAL(ATA.channels[count].irq, irq);
801 SET_GLOBAL(ATA.channels[count].pci_bdf, bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400802
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500803 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400804 u32 port1, port2;
805
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500806 if (prog_if & 1) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500807 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
808 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400809 } else {
810 port1 = 0x1f0;
811 port2 = 0x3f0;
812 }
Kevin O'Connor609da232008-12-28 23:18:57 -0500813 SET_GLOBAL(ATA.channels[count].iobase1, port1);
814 SET_GLOBAL(ATA.channels[count].iobase2, port2);
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500815 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
816 , count, port1, port2, bdf, prog_if);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400817 count++;
818
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500819 if (prog_if & 4) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500820 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
821 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400822 } else {
823 port1 = 0x170;
824 port2 = 0x370;
825 }
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500826 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
827 , count, port1, port2, bdf, prog_if);
Kevin O'Connor609da232008-12-28 23:18:57 -0500828 SET_GLOBAL(ATA.channels[count].iobase1, port1);
829 SET_GLOBAL(ATA.channels[count].iobase2, port2);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400830 count++;
831 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400832}
833
834void
835hard_drive_setup()
836{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400837 if (!CONFIG_ATA)
838 return;
839
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400840 dprintf(3, "init hard drives\n");
Kevin O'Connorc1437612008-05-18 01:43:07 -0400841 ata_init();
842 ata_detect();
843
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400844 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400845
Kevin O'Connord21c0892008-11-26 17:02:43 -0500846 enable_hwirq(14, entry_76);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400847}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500848
849
850/****************************************************************
851 * Drive mapping
852 ****************************************************************/
853
854// Fill in Fixed Disk Parameter Table (located in ebda).
855static void
856fill_fdpt(int driveid)
857{
858 if (driveid > 1)
859 return;
860
861 u16 nlc = GET_GLOBAL(ATA.devices[driveid].lchs.cylinders);
862 u16 nlh = GET_GLOBAL(ATA.devices[driveid].lchs.heads);
863 u16 nlspt = GET_GLOBAL(ATA.devices[driveid].lchs.spt);
864
865 u16 npc = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
866 u16 nph = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
867 u16 npspt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
868
869 struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[driveid];
870 fdpt->precompensation = 0xffff;
871 fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
872 fdpt->landing_zone = npc;
873 fdpt->cylinders = nlc;
874 fdpt->heads = nlh;
875 fdpt->sectors = nlspt;
876
877 if (nlc == npc && nlh == nph && nlspt == npspt)
878 // no logical CHS mapping used, just physical CHS
879 // use Standard Fixed Disk Parameter Table (FDPT)
880 return;
881
882 // complies with Phoenix style Translated Fixed Disk Parameter
883 // Table (FDPT)
884 fdpt->phys_cylinders = npc;
885 fdpt->phys_heads = nph;
886 fdpt->phys_sectors = npspt;
887 fdpt->a0h_signature = 0xa0;
888
889 // Checksum structure.
Kevin O'Connor523e5a92009-07-04 13:46:33 -0400890 fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
Kevin O'Connor9becbf12009-07-12 09:29:46 -0400891
892 if (driveid == 0)
893 SET_IVT(0x41, get_ebda_seg()
894 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
895 else
896 SET_IVT(0x46, get_ebda_seg()
897 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500898}
899
900// Map a drive (that was registered via add_bcv_hd)
901void
902map_drive(int driveid)
903{
904 // fill hdidmap
905 u8 hdcount = GET_BDA(hdcount);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500906 dprintf(3, "Mapping driveid %d to %d\n", driveid, hdcount);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500907 SET_GLOBAL(ATA.idmap[0][hdcount], driveid);
908 SET_BDA(hdcount, hdcount + 1);
909
910 // Fill "fdpt" structure.
911 fill_fdpt(hdcount);
912}