blob: e9f9c7cf855c0d01cf461d2517b0f41c37c948a8 [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'Connor3491e8b2008-02-29 00:22:27 -050031#define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
32
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'Connor74f9c082008-04-13 16:57:16 -040071static __always_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.
81static __always_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);
113 status = await_not_bsy(iobase1);
114 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.
180 status = await_not_bsy(iobase1);
181 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'Connora6b9f712008-03-29 12:53:57 -0400221// Read and discard x number of bytes from an io channel.
222static void
Kevin O'Connor32945af2009-02-27 21:23:01 -0500223insx_discard(int iobase1, int bytes)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400224{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400225 int count, i;
Kevin O'Connor32945af2009-02-27 21:23:01 -0500226 if (CONFIG_ATA_PIO32) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400227 count = bytes / 4;
228 for (i=0; i<count; i++)
229 inl(iobase1);
230 } else {
231 count = bytes / 2;
232 for (i=0; i<count; i++)
233 inw(iobase1);
234 }
235}
236
237// Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
238// 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
239// and/or last block may be partially transferred. This function is
240// inlined because all the callers use different forms and because the
241// large number of parameters would consume a lot of stack space.
Kevin O'Connor74f9c082008-04-13 16:57:16 -0400242static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400243ata_transfer(int driveid, int iswrite, int count, int blocksize
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500244 , int skipfirst, int skiplast, void *buf_fl)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400245{
Kevin O'Connora05223c2008-06-28 12:15:57 -0400246 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d"
247 " skipf=%d skipl=%d buf=%p\n"
248 , driveid, iswrite, count, blocksize
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500249 , skipfirst, skiplast, buf_fl);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400250
251 // Reset count of transferred data
Kevin O'Connor609da232008-12-28 23:18:57 -0500252 SET_EBDA(sector_count, 0);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400253
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400254 u8 channel = driveid / 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500255 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
256 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400257 int current = 0;
258 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400259 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400260 int bsize = blocksize;
261 if (skipfirst && current == 0) {
Kevin O'Connor32945af2009-02-27 21:23:01 -0500262 insx_discard(iobase1, skipfirst);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400263 bsize -= skipfirst;
264 }
265 if (skiplast && current == count-1)
266 bsize -= skiplast;
267
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'Connor35ae7262009-01-19 15:44:44 -0500270 dprintf(16, "Write sector id=%d dest=%p\n", driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500271 if (CONFIG_ATA_PIO32)
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500272 outsl_fl(iobase1, buf_fl, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400273 else
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500274 outsw_fl(iobase1, buf_fl, bsize / 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'Connor35ae7262009-01-19 15:44:44 -0500277 dprintf(16, "Read sector id=%d dest=%p\n", driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500278 if (CONFIG_ATA_PIO32)
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500279 insl_fl(iobase1, buf_fl, bsize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400280 else
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500281 insw_fl(iobase1, buf_fl, bsize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400282 }
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500283 buf_fl += bsize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400284
285 if (skiplast && current == count-1)
Kevin O'Connor32945af2009-02-27 21:23:01 -0500286 insx_discard(iobase1, skiplast);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400287
Kevin O'Connor580e3322009-02-06 22:36:53 -0500288 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400289 if (status < 0)
290 // Error
291 return status;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400292
293 current++;
Kevin O'Connor609da232008-12-28 23:18:57 -0500294 SET_EBDA(sector_count, current);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400295 if (current == count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400296 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500297 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
298 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400299 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500300 , status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400301 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500302 }
303 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400304
Kevin O'Connor580e3322009-02-06 22:36:53 -0500305 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
306 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400307 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400308 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500309 if (status != 0) {
310 dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400311 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400312 }
313
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500314 // Enable interrupts
315 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
316 return 0;
317}
318
Kevin O'Connoree55c762008-05-13 00:18:20 -0400319static noinline int
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500320ata_transfer_disk(const struct disk_op_s *op)
Kevin O'Connoree55c762008-05-13 00:18:20 -0400321{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500322 return ata_transfer(op->driveid, op->command == ATA_CMD_WRITE_SECTORS
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500323 , op->count, IDE_SECTOR_SIZE, 0, 0, op->buf_fl);
Kevin O'Connoree55c762008-05-13 00:18:20 -0400324}
325
326static noinline int
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500327ata_transfer_cdrom(const struct disk_op_s *op)
Kevin O'Connoree55c762008-05-13 00:18:20 -0400328{
329 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500330 , 0, 0, op->buf_fl);
Kevin O'Connoree55c762008-05-13 00:18:20 -0400331}
332
333static noinline int
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500334ata_transfer_cdemu(const struct disk_op_s *op, int before, int after)
Kevin O'Connoree55c762008-05-13 00:18:20 -0400335{
336 int vcount = op->count * 4 - before - after;
337 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500338 , before*512, after*512, op->buf_fl);
Kevin O'Connoree55c762008-05-13 00:18:20 -0400339 if (ret) {
Kevin O'Connor609da232008-12-28 23:18:57 -0500340 SET_EBDA(sector_count, 0);
Kevin O'Connoree55c762008-05-13 00:18:20 -0400341 return ret;
342 }
Kevin O'Connor609da232008-12-28 23:18:57 -0500343 SET_EBDA(sector_count, vcount);
Kevin O'Connoree55c762008-05-13 00:18:20 -0400344 return 0;
345}
346
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400347
348/****************************************************************
349 * ATA hard drive functions
350 ****************************************************************/
351
Kevin O'Connoree55c762008-05-13 00:18:20 -0400352static noinline int
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500353send_cmd_disk(const struct disk_op_s *op)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400354{
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400355 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400356
357 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400358 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400359
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500360 cmd.command = op->command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400361 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
362 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400363 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400364 cmd.lba_mid2 = lba >> 32;
365 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400366
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400367 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400368 lba &= 0xffffff;
369 }
370
371 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400372 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400373 cmd.lba_low = lba;
374 cmd.lba_mid = lba >> 8;
375 cmd.lba_high = lba >> 16;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500376 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400377
Kevin O'Connoree55c762008-05-13 00:18:20 -0400378 return send_cmd(op->driveid, &cmd);
379}
380
381// Read/write count blocks from a harddrive.
382__always_inline int
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500383ata_cmd_data(struct disk_op_s *op)
Kevin O'Connoree55c762008-05-13 00:18:20 -0400384{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500385 int ret = send_cmd_disk(op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400386 if (ret)
387 return ret;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500388 return ata_transfer_disk(op);
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400389}
390
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400391
392/****************************************************************
393 * ATAPI functions
394 ****************************************************************/
395
396// Low-level atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400397static __always_inline int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400398send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500399{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400400 u8 channel = driveid / 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500401 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
402 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500403
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400404 struct ata_pio_command cmd;
405 cmd.sector_count = 0;
406 cmd.feature = 0;
407 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400408 cmd.lba_mid = blocksize;
409 cmd.lba_high = blocksize >> 8;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500410 cmd.device = 0;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400411 cmd.command = ATA_CMD_PACKET;
412
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400413 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400414 if (ret)
415 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500416
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400417 // Send command to device
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500418 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500419
Kevin O'Connor580e3322009-02-06 22:36:53 -0500420 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400421 if (status < 0)
422 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400423
Kevin O'Connor580e3322009-02-06 22:36:53 -0500424 if (status & ATA_CB_STAT_ERR) {
425 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
426 , status, inb(iobase1 + ATA_CB_ERR));
427 return -2;
428 }
429 if (!(status & ATA_CB_STAT_DRQ)) {
430 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
431 return -3;
432 }
433
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500434 return 0;
435}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500436
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400437// Low-level cdrom read atapi command transmit function.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400438static int
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500439send_cmd_cdrom(const struct disk_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500440{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400441 u8 atacmd[12];
442 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connor180a9592008-03-04 22:50:53 -0500443
Kevin O'Connoree55c762008-05-13 00:18:20 -0400444 atacmd[0]=0x28; // READ command
445 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
446 atacmd[8]=(op->count & 0x00ff);
447 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
448 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
449 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
450 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400451
Kevin O'Connoree55c762008-05-13 00:18:20 -0400452 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
453 , CDROM_SECTOR_SIZE);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500454}
455
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400456// Read sectors from the cdrom.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400457__always_inline int
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500458cdrom_read(struct disk_op_s *op)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400459{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500460 int ret = send_cmd_cdrom(op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400461 if (ret)
462 return ret;
463
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500464 return ata_transfer_cdrom(op);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400465}
466
467// Pretend the cdrom has 512 byte sectors (instead of 2048) and read
468// sectors.
Kevin O'Connoree55c762008-05-13 00:18:20 -0400469__always_inline int
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500470cdrom_read_512(struct disk_op_s *op)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400471{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500472 u32 vlba = op->lba;
473 u32 vcount = op->count;
474 u32 lba = op->lba = vlba / 4;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400475 u32 velba = vlba + vcount - 1;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400476 u32 elba = velba / 4;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500477 op->count = elba - lba + 1;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400478 int before = vlba % 4;
479 int after = 3 - (velba % 4);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400480
Kevin O'Connora05223c2008-06-28 12:15:57 -0400481 dprintf(16, "cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
482 " count=%d before=%d after=%d\n"
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500483 , op->driveid, vlba, vcount, op->buf_fl, lba, elba
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500484 , op->count, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400485
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500486 int ret = send_cmd_cdrom(op);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400487 if (ret)
488 return ret;
489
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500490 return ata_transfer_cdemu(op, before, after);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400491}
492
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400493// Send a simple atapi command to a drive.
494int
495ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500496 , u32 length, void *buf_fl)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400497{
498 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
499 if (ret)
500 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400501
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500502 return ata_transfer(driveid, 0, 1, length, 0, 0, buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400503}
504
505
506/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500507 * Disk geometry translation
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400508 ****************************************************************/
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500509
Kevin O'Connorc1437612008-05-18 01:43:07 -0400510static u8
511get_translation(int driveid)
512{
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400513 if (! CONFIG_COREBOOT) {
514 // Emulators pass in the translation info via nvram.
515 u8 channel = driveid / 2;
516 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
517 translation >>= 2 * (driveid % 4);
518 translation &= 0x03;
519 return translation;
520 }
521
522 // On COREBOOT, use a heuristic to determine translation type.
Kevin O'Connor609da232008-12-28 23:18:57 -0500523 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
524 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
525 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400526
527 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
528 return ATA_TRANSLATION_NONE;
529 if (cylinders * heads <= 131072)
530 return ATA_TRANSLATION_LARGE;
531 return ATA_TRANSLATION_LBA;
Kevin O'Connorc1437612008-05-18 01:43:07 -0400532}
533
534static void
535setup_translation(int driveid)
536{
537 u8 translation = get_translation(driveid);
Kevin O'Connor609da232008-12-28 23:18:57 -0500538 SET_GLOBAL(ATA.devices[driveid].translation, translation);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400539
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400540 u8 channel = driveid / 2;
541 u8 slave = driveid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500542 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
543 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
544 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
545 u64 sectors = GET_GLOBAL(ATA.devices[driveid].sectors);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400546
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400547 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400548 , channel, slave, cylinders, heads, spt);
549 switch (translation) {
550 case ATA_TRANSLATION_NONE:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400551 dprintf(1, "none");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400552 break;
553 case ATA_TRANSLATION_LBA:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400554 dprintf(1, "lba");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400555 spt = 63;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400556 if (sectors > 63*255*1024) {
557 heads = 255;
558 cylinders = 1024;
559 break;
560 }
561 u32 sect = (u32)sectors / 63;
562 heads = sect / 1024;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400563 if (heads>128)
564 heads = 255;
565 else if (heads>64)
566 heads = 128;
567 else if (heads>32)
568 heads = 64;
569 else if (heads>16)
570 heads = 32;
571 else
572 heads = 16;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400573 cylinders = sect / heads;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400574 break;
575 case ATA_TRANSLATION_RECHS:
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400576 dprintf(1, "r-echs");
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400577 // Take care not to overflow
578 if (heads==16) {
579 if (cylinders>61439)
580 cylinders=61439;
581 heads=15;
582 cylinders = (u16)((u32)(cylinders)*16/15);
583 }
584 // then go through the large bitshift process
585 case ATA_TRANSLATION_LARGE:
586 if (translation == ATA_TRANSLATION_LARGE)
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400587 dprintf(1, "large");
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400588 while (cylinders > 1024) {
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400589 cylinders >>= 1;
590 heads <<= 1;
591
592 // If we max out the head count
593 if (heads > 127)
594 break;
595 }
596 break;
597 }
598 // clip to 1024 cylinders in lchs
599 if (cylinders > 1024)
600 cylinders = 1024;
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400601 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400602
Kevin O'Connor609da232008-12-28 23:18:57 -0500603 SET_GLOBAL(ATA.devices[driveid].lchs.heads, heads);
604 SET_GLOBAL(ATA.devices[driveid].lchs.cylinders, cylinders);
605 SET_GLOBAL(ATA.devices[driveid].lchs.spt, spt);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400606}
607
Kevin O'Connor0a924122009-02-08 19:43:47 -0500608
609/****************************************************************
610 * ATA detect and init
611 ****************************************************************/
612
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500613// Extract common information from IDENTIFY commands.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500614static void
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500615extract_identify(int driveid, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500616{
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500617 dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500618
619 // Read model name
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500620 char *model = ATA.devices[driveid].model;
621 int maxsize = ARRAY_SIZE(ATA.devices[driveid].model);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500622 int i;
Kevin O'Connorab515602009-02-11 22:22:15 -0500623 for (i=0; i<maxsize/2; i++) {
624 u16 v = buffer[27+i];
625 model[i*2] = v >> 8;
626 model[i*2+1] = v & 0xff;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500627 }
628 model[maxsize-1] = 0x00;
629
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500630 // Trim trailing spaces from model name.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500631 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
632 model[i] = 0x00;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500633
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500634 // Extract ATA/ATAPI version.
Kevin O'Connorab515602009-02-11 22:22:15 -0500635 u16 ataversion = buffer[80];
Kevin O'Connor0a924122009-02-08 19:43:47 -0500636 u8 version;
637 for (version=15; version>0; version--)
638 if (ataversion & (1<<version))
639 break;
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500640 ATA.devices[driveid].version = version;
641
642 // Common flags.
643 SET_GLOBAL(ATA.devices[driveid].removable, (buffer[0] & 0x80) ? 1 : 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500644}
645
646static int
647init_drive_atapi(int driveid)
648{
649 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connorab515602009-02-11 22:22:15 -0500650 u16 buffer[256];
Kevin O'Connor0a924122009-02-08 19:43:47 -0500651 memset(buffer, 0, sizeof(buffer));
652 struct disk_op_s dop;
653 dop.driveid = driveid;
654 dop.command = ATA_CMD_IDENTIFY_DEVICE_PACKET;
655 dop.count = 1;
656 dop.lba = 1;
657 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
658 int ret = ata_cmd_data(&dop);
659 if (ret)
660 return ret;
661
662 // Success - setup as ATAPI.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500663 extract_identify(driveid, buffer);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500664 SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATAPI);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500665 SET_GLOBAL(ATA.devices[driveid].device, (buffer[0] >> 8) & 0x1f);
666 SET_GLOBAL(ATA.devices[driveid].blksize, CDROM_SECTOR_SIZE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500667
668 // fill cdidmap
669 u8 cdcount = GET_GLOBAL(ATA.cdcount);
670 SET_GLOBAL(ATA.idmap[1][cdcount], driveid);
671 SET_GLOBAL(ATA.cdcount, cdcount+1);
672
673 // Report drive info to user.
674 u8 channel = driveid / 2;
675 u8 slave = driveid % 2;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500676 printf("ata%d-%d: %s ATAPI-%d %s\n", channel, slave
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500677 , ATA.devices[driveid].model, ATA.devices[driveid].version
Kevin O'Connor6a46a422009-02-17 22:57:37 -0500678 , (ATA.devices[driveid].device == ATA_DEVICE_CDROM
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500679 ? "CD-Rom/DVD-Rom" : "Device"));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500680
681 return 0;
682}
683
Kevin O'Connor580e3322009-02-06 22:36:53 -0500684static int
Kevin O'Connorc1437612008-05-18 01:43:07 -0400685init_drive_ata(int driveid)
686{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500687 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connorab515602009-02-11 22:22:15 -0500688 u16 buffer[256];
Kevin O'Connorc1437612008-05-18 01:43:07 -0400689 memset(buffer, 0, sizeof(buffer));
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500690 struct disk_op_s dop;
691 dop.driveid = driveid;
692 dop.command = ATA_CMD_IDENTIFY_DEVICE;
693 dop.count = 1;
694 dop.lba = 1;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500695 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500696 int ret = ata_cmd_data(&dop);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400697 if (ret)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500698 return ret;
699
700 // Success - setup as ATA.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500701 extract_identify(driveid, buffer);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500702 SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATA);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500703 SET_GLOBAL(ATA.devices[driveid].device, ATA_DEVICE_HD);
704 SET_GLOBAL(ATA.devices[driveid].blksize, IDE_SECTOR_SIZE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400705
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500706 SET_GLOBAL(ATA.devices[driveid].pchs.cylinders, buffer[1]);
707 SET_GLOBAL(ATA.devices[driveid].pchs.heads, buffer[3]);
708 SET_GLOBAL(ATA.devices[driveid].pchs.spt, buffer[6]);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400709
710 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500711 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
712 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400713 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500714 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connor609da232008-12-28 23:18:57 -0500715 SET_GLOBAL(ATA.devices[driveid].sectors, sectors);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400716
717 // Setup disk geometry translation.
718 setup_translation(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400719
Kevin O'Connorc1437612008-05-18 01:43:07 -0400720 // Report drive info to user.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500721 u8 channel = driveid / 2;
722 u8 slave = driveid % 2;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500723 char *model = ATA.devices[driveid].model;
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500724 printf("ata%d-%d: %s ATA-%d Hard-Disk ", channel, slave, model
725 , ATA.devices[driveid].version);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500726 u64 sizeinmb = sectors >> 11;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400727 if (sizeinmb < (1 << 16))
Kevin O'Connor0a924122009-02-08 19:43:47 -0500728 printf("(%u MiBytes)\n", (u32)sizeinmb);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400729 else
Kevin O'Connor0a924122009-02-08 19:43:47 -0500730 printf("(%u GiBytes)\n", (u32)(sizeinmb >> 10));
731
732 // Register with bcv system.
733 add_bcv_hd(driveid, model);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400734
Kevin O'Connor580e3322009-02-06 22:36:53 -0500735 return 0;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400736}
737
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400738static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500739ata_detect()
740{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500741 // Device detection
Kevin O'Connor5eac1dd2009-02-07 00:03:11 -0500742 int driveid, last_reset_driveid=-1;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400743 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
744 u8 channel = driveid / 2;
745 u8 slave = driveid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500746
Kevin O'Connor609da232008-12-28 23:18:57 -0500747 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400748 if (!iobase1)
749 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500750
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500751 // Look for device
752 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
753 outb(0x55, iobase1+ATA_CB_SC);
754 outb(0xaa, iobase1+ATA_CB_SN);
755 outb(0xaa, iobase1+ATA_CB_SC);
756 outb(0x55, iobase1+ATA_CB_SN);
757 outb(0x55, iobase1+ATA_CB_SC);
758 outb(0xaa, iobase1+ATA_CB_SN);
759
Kevin O'Connor580e3322009-02-06 22:36:53 -0500760 // Check if ioport registers look valid.
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400761 u8 sc = inb(iobase1+ATA_CB_SC);
762 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500763 dprintf(6, "ata_detect drive=%d sc=%x sn=%x\n", driveid, sc, sn);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400764 if (sc != 0x55 || sn != 0xaa)
765 continue;
766
767 // reset the channel
Kevin O'Connor580e3322009-02-06 22:36:53 -0500768 if (slave && driveid == last_reset_driveid + 1) {
769 // The drive was just reset - no need to reset it again.
770 } else {
771 ata_reset(driveid);
772 last_reset_driveid = driveid;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500773 }
774
Kevin O'Connor580e3322009-02-06 22:36:53 -0500775 // check for ATAPI
776 int ret = init_drive_atapi(driveid);
777 if (!ret)
778 // Found an ATAPI drive.
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400779 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500780
781 u8 st = inb(iobase1+ATA_CB_STAT);
782 if (!st)
783 // Status not set - can't be a valid drive.
784 continue;
785
786 // Wait for RDY.
787 ret = await_rdy(iobase1);
788 if (ret < 0)
789 continue;
790
791 // check for ATA.
792 init_drive_ata(driveid);
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500793 }
794
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500795 printf("\n");
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500796}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400797
798static void
799ata_init()
800{
Kevin O'Connoref3d8822009-02-05 19:51:12 -0500801 memset(&ATA, 0, sizeof(ATA));
802
Kevin O'Connorc1437612008-05-18 01:43:07 -0400803 // hdidmap and cdidmap init.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400804 u8 device;
805 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
Kevin O'Connor609da232008-12-28 23:18:57 -0500806 SET_GLOBAL(ATA.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
807 SET_GLOBAL(ATA.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400808 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400809
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400810 // Scan PCI bus for ATA adapters
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500811 int count=0;
812 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -0500813 foreachpci(bdf, max) {
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500814 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400815 continue;
Kevin O'Connor89f67632009-02-11 20:16:49 -0500816 if (count >= ARRAY_SIZE(ATA.channels))
817 break;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400818
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500819 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor609da232008-12-28 23:18:57 -0500820 SET_GLOBAL(ATA.channels[count].irq, irq);
821 SET_GLOBAL(ATA.channels[count].pci_bdf, bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400822
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500823 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400824 u32 port1, port2;
825
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500826 if (prog_if & 1) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500827 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
828 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400829 } else {
830 port1 = 0x1f0;
831 port2 = 0x3f0;
832 }
Kevin O'Connor609da232008-12-28 23:18:57 -0500833 SET_GLOBAL(ATA.channels[count].iobase1, port1);
834 SET_GLOBAL(ATA.channels[count].iobase2, port2);
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500835 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
836 , count, port1, port2, bdf, prog_if);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400837 count++;
838
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500839 if (prog_if & 4) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500840 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
841 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400842 } else {
843 port1 = 0x170;
844 port2 = 0x370;
845 }
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500846 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
847 , count, port1, port2, bdf, prog_if);
Kevin O'Connor609da232008-12-28 23:18:57 -0500848 SET_GLOBAL(ATA.channels[count].iobase1, port1);
849 SET_GLOBAL(ATA.channels[count].iobase2, port2);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400850 count++;
851 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400852}
853
854void
855hard_drive_setup()
856{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400857 if (!CONFIG_ATA)
858 return;
859
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400860 dprintf(3, "init hard drives\n");
Kevin O'Connorc1437612008-05-18 01:43:07 -0400861 ata_init();
862 ata_detect();
863
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400864 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400865
Kevin O'Connord21c0892008-11-26 17:02:43 -0500866 enable_hwirq(14, entry_76);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400867}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500868
869
870/****************************************************************
871 * Drive mapping
872 ****************************************************************/
873
874// Fill in Fixed Disk Parameter Table (located in ebda).
875static void
876fill_fdpt(int driveid)
877{
878 if (driveid > 1)
879 return;
880
881 u16 nlc = GET_GLOBAL(ATA.devices[driveid].lchs.cylinders);
882 u16 nlh = GET_GLOBAL(ATA.devices[driveid].lchs.heads);
883 u16 nlspt = GET_GLOBAL(ATA.devices[driveid].lchs.spt);
884
885 u16 npc = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
886 u16 nph = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
887 u16 npspt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
888
889 struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[driveid];
890 fdpt->precompensation = 0xffff;
891 fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
892 fdpt->landing_zone = npc;
893 fdpt->cylinders = nlc;
894 fdpt->heads = nlh;
895 fdpt->sectors = nlspt;
896
897 if (nlc == npc && nlh == nph && nlspt == npspt)
898 // no logical CHS mapping used, just physical CHS
899 // use Standard Fixed Disk Parameter Table (FDPT)
900 return;
901
902 // complies with Phoenix style Translated Fixed Disk Parameter
903 // Table (FDPT)
904 fdpt->phys_cylinders = npc;
905 fdpt->phys_heads = nph;
906 fdpt->phys_sectors = npspt;
907 fdpt->a0h_signature = 0xa0;
908
909 // Checksum structure.
Kevin O'Connor94fd47e2009-02-15 15:21:10 -0500910 u8 sum = checksum(fdpt, sizeof(*fdpt)-1);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500911 fdpt->checksum = -sum;
912}
913
914// Map a drive (that was registered via add_bcv_hd)
915void
916map_drive(int driveid)
917{
918 // fill hdidmap
919 u8 hdcount = GET_BDA(hdcount);
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500920 dprintf(3, "Mapping driveid %d to %d\n", driveid, hdcount);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500921 SET_GLOBAL(ATA.idmap[0][hdcount], driveid);
922 SET_BDA(hdcount, hdcount + 1);
923
924 // Fill "fdpt" structure.
925 fill_fdpt(hdcount);
926}