blob: ed7ef28d01682e7b9e3a9e98d854f5d626b7a021 [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'Connorcfdc13f2010-02-14 13:07:54 -050040 warn_timeout();
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'Connorcfdc13f2010-02-14 13:07:54 -0500112 warn_timeout();
Kevin O'Connor580e3322009-02-06 22:36:53 -0500113 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'Connor14021f22009-12-26 23:21:38 -0500133// Check for drive RDY for 16bit interface command.
Kevin O'Connor42337662009-08-10 00:06:37 -0400134static int
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400135isready(struct drive_s *drive_g)
Kevin O'Connor42337662009-08-10 00:06:37 -0400136{
137 // Read the status from controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400138 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400139 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400140 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400141 u8 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400142 if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY)
143 return DISK_RET_SUCCESS;
144 return DISK_RET_ENOTREADY;
Kevin O'Connor42337662009-08-10 00:06:37 -0400145}
146
Kevin O'Connor14021f22009-12-26 23:21:38 -0500147// Default 16bit command demuxer for ATA and ATAPI devices.
Kevin O'Connor42337662009-08-10 00:06:37 -0400148static int
149process_ata_misc_op(struct disk_op_s *op)
150{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400151 if (!CONFIG_ATA)
152 return 0;
153
Kevin O'Connor42337662009-08-10 00:06:37 -0400154 switch (op->command) {
Kevin O'Connor42337662009-08-10 00:06:37 -0400155 case CMD_RESET:
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400156 ata_reset(op->drive_g);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400157 return DISK_RET_SUCCESS;
Kevin O'Connor42337662009-08-10 00:06:37 -0400158 case CMD_ISREADY:
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400159 return isready(op->drive_g);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400160 case CMD_FORMAT:
161 case CMD_VERIFY:
162 case CMD_SEEK:
163 return DISK_RET_SUCCESS;
164 default:
165 op->count = 0;
166 return DISK_RET_EPARAM;
Kevin O'Connor42337662009-08-10 00:06:37 -0400167 }
168}
169
Kevin O'Connoree55c762008-05-13 00:18:20 -0400170
171/****************************************************************
172 * ATA send command
173 ****************************************************************/
174
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400175struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400176 u8 feature;
177 u8 sector_count;
178 u8 lba_low;
179 u8 lba_mid;
180 u8 lba_high;
181 u8 device;
182 u8 command;
183
Kevin O'Connor14021f22009-12-26 23:21:38 -0500184 u8 feature2;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400185 u8 sector_count2;
186 u8 lba_low2;
187 u8 lba_mid2;
188 u8 lba_high2;
189};
190
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400191// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400192static int
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400193send_cmd(struct drive_s *drive_g, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500194{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400195 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400196 u8 channel = ataid / 2;
197 u8 slave = ataid % 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400198 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400199
200 // Select device
Kevin O'Connor580e3322009-02-06 22:36:53 -0500201 int status = await_not_bsy(iobase1);
202 if (status < 0)
203 return status;
204 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
205 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
206 u8 olddh = inb(iobase1 + ATA_CB_DH);
207 outb(newdh, iobase1 + ATA_CB_DH);
208 if ((olddh ^ newdh) & (1<<4)) {
209 // Was a device change - wait for device to become not busy.
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400210 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500211 if (status < 0)
212 return status;
213 }
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400214
Kevin O'Connor14021f22009-12-26 23:21:38 -0500215 // Check for ATA_CMD_(READ|WRITE)_(SECTORS|DMA)_EXT commands.
216 if ((cmd->command & ~0x11) == ATA_CMD_READ_SECTORS_EXT) {
217 outb(cmd->feature2, iobase1 + ATA_CB_FR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400218 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'Connor14021f22009-12-26 23:21:38 -0500230 return 0;
231}
232
233// Wait for data after calling 'send_cmd'.
234static int
235ata_wait_data(u16 iobase1)
236{
237 int status = ndelay_await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400238 if (status < 0)
239 return status;
240
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500241 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500242 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
243 , status, inb(iobase1 + ATA_CB_ERR));
Kevin O'Connora05223c2008-06-28 12:15:57 -0400244 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400245 }
246 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500247 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400248 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500249 }
250
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400251 return 0;
252}
253
Kevin O'Connor14021f22009-12-26 23:21:38 -0500254// Send an ata command that does not transfer any further data.
255int
256ata_cmd_nondata(struct drive_s *drive_g, struct ata_pio_command *cmd)
257{
258 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
259 u8 channel = ataid / 2;
260 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
261 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
262
263 // Disable interrupts
264 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
265
266 int ret = send_cmd(drive_g, cmd);
267 if (ret)
268 goto fail;
269 ret = ndelay_await_not_bsy(iobase1);
270 if (ret < 0)
271 goto fail;
272
273 if (ret & ATA_CB_STAT_ERR) {
274 dprintf(6, "nondata cmd : read error (status=%02x err=%02x)\n"
275 , ret, inb(iobase1 + ATA_CB_ERR));
276 ret = -4;
277 goto fail;
278 }
279 if (ret & ATA_CB_STAT_DRQ) {
280 dprintf(6, "nondata cmd : DRQ set (status %02x)\n", ret);
281 ret = -5;
282 goto fail;
283 }
284
285fail:
286 // Enable interrupts
287 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
288
289 return ret;
290}
291
Kevin O'Connoree55c762008-05-13 00:18:20 -0400292
293/****************************************************************
Kevin O'Connor14021f22009-12-26 23:21:38 -0500294 * ATA PIO transfers
Kevin O'Connoree55c762008-05-13 00:18:20 -0400295 ****************************************************************/
296
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400297// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400298// 'op->drive_g'.
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400299static int
Kevin O'Connor14021f22009-12-26 23:21:38 -0500300ata_pio_transfer(struct disk_op_s *op, int iswrite, int blocksize)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400301{
Kevin O'Connor14021f22009-12-26 23:21:38 -0500302 dprintf(16, "ata_pio_transfer id=%p write=%d count=%d bs=%d buf=%p\n"
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400303 , op->drive_g, iswrite, op->count, blocksize, op->buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400304
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400305 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400306 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400307 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
308 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400309 int count = op->count;
310 void *buf_fl = op->buf_fl;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400311 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400312 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400313 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400314 // Write data to controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400315 dprintf(16, "Write sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500316 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400317 outsl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400318 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400319 outsw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500320 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400321 // Read data from controller
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400322 dprintf(16, "Read sector id=%p dest=%p\n", op->drive_g, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500323 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400324 insl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400325 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400326 insw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400327 }
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400328 buf_fl += blocksize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400329
Kevin O'Connor580e3322009-02-06 22:36:53 -0500330 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400331 if (status < 0) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400332 // Error
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400333 op->count -= count;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400334 return status;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400335 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400336
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400337 count--;
338 if (!count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400339 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500340 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
341 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500342 dprintf(6, "ata_pio_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500343 , status);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400344 op->count -= count;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400345 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500346 }
347 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400348
Kevin O'Connor580e3322009-02-06 22:36:53 -0500349 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
350 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400351 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400352 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500353 if (status != 0) {
Kevin O'Connor14021f22009-12-26 23:21:38 -0500354 dprintf(6, "ata_pio_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400355 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400356 }
357
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500358 return 0;
359}
360
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400361
362/****************************************************************
Kevin O'Connor14021f22009-12-26 23:21:38 -0500363 * ATA DMA transfers
364 ****************************************************************/
365
366#define BM_CMD 0
367#define BM_CMD_MEMWRITE 0x08
368#define BM_CMD_START 0x01
369#define BM_STATUS 2
370#define BM_STATUS_IRQ 0x04
371#define BM_STATUS_ERROR 0x02
372#define BM_STATUS_ACTIVE 0x01
373#define BM_TABLE 4
374
375struct sff_dma_prd {
376 u32 buf_fl;
377 u32 count;
378};
379
380// Check if DMA available and setup transfer if so.
381static int
382ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize)
383{
Kevin O'Connor4d079022010-01-17 12:58:47 -0500384 if (! CONFIG_ATA_DMA)
385 return -1;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500386 u32 dest = (u32)op->buf_fl;
387 if (dest & 1)
388 // Need minimum alignment of 1.
389 return -1;
390 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
391 u8 channel = ataid / 2;
392 u16 iomaster = GET_GLOBAL(ATA_channels[channel].iomaster);
393 if (! iomaster)
394 return -1;
395 u32 bytes = op->count * blocksize;
396 if (! bytes)
397 return -1;
398
399 // Build PRD dma structure.
400 struct sff_dma_prd *dma = MAKE_FLATPTR(
401 get_ebda_seg()
402 , (void*)offsetof(struct extended_bios_data_area_s, extra_stack));
403 struct sff_dma_prd *origdma = dma;
404 while (bytes) {
405 if (dma >= &origdma[16])
406 // Too many descriptors..
407 return -1;
408 u32 count = bytes;
409 if (count > 0x10000)
410 count = 0x10000;
411 u32 max = 0x10000 - (dest & 0xffff);
412 if (count > max)
413 count = max;
414
415 SET_FLATPTR(dma->buf_fl, dest);
416 bytes -= count;
417 if (!bytes)
418 // Last descriptor.
419 count |= 1<<31;
420 dprintf(16, "dma@%p: %08x %08x\n", dma, dest, count);
421 dest += count;
422 SET_FLATPTR(dma->count, count);
423 dma++;
424 }
425
426 // Program bus-master controller.
427 outl((u32)origdma, iomaster + BM_TABLE);
428 u8 oldcmd = inb(iomaster + BM_CMD) & ~(BM_CMD_MEMWRITE|BM_CMD_START);
429 outb(oldcmd | (iswrite ? 0x00 : BM_CMD_MEMWRITE), iomaster + BM_CMD);
430 outb(BM_STATUS_ERROR|BM_STATUS_IRQ, iomaster + BM_STATUS);
431
432 return 0;
433}
434
435// Transfer data using DMA.
436static int
437ata_dma_transfer(struct disk_op_s *op)
438{
Kevin O'Connor4d079022010-01-17 12:58:47 -0500439 if (! CONFIG_ATA_DMA)
440 return -1;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500441 dprintf(16, "ata_dma_transfer id=%p buf=%p\n"
442 , op->drive_g, op->buf_fl);
443
444 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
445 u8 channel = ataid / 2;
446 u16 iomaster = GET_GLOBAL(ATA_channels[channel].iomaster);
447
448 // Start bus-master controller.
449 u8 oldcmd = inb(iomaster + BM_CMD);
450 outb(oldcmd | BM_CMD_START, iomaster + BM_CMD);
451
452 u64 end = calc_future_tsc(IDE_TIMEOUT);
453 u8 status;
454 for (;;) {
455 status = inb(iomaster + BM_STATUS);
456 if (status & BM_STATUS_IRQ)
457 break;
458 // Transfer in progress
459 if (check_time(end)) {
460 // Timeout.
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500461 warn_timeout();
Kevin O'Connor14021f22009-12-26 23:21:38 -0500462 break;
463 }
464 yield();
465 }
466 outb(oldcmd & ~BM_CMD_START, iomaster + BM_CMD);
467
468 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
469 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
470 int idestatus = pause_await_not_bsy(iobase1, iobase2);
471
472 if ((status & (BM_STATUS_IRQ|BM_STATUS_ACTIVE)) == BM_STATUS_IRQ
473 && idestatus >= 0x00
474 && (idestatus & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
475 | ATA_CB_STAT_ERR)) == 0x00)
476 // Success.
477 return 0;
478
479 dprintf(6, "IDE DMA error (dma=%x ide=%x/%x/%x)\n", status, idestatus
480 , inb(iobase2 + ATA_CB_ASTAT), inb(iobase1 + ATA_CB_ERR));
481 op->count = 0;
482 return -1;
483}
484
485
486/****************************************************************
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400487 * ATA hard drive functions
488 ****************************************************************/
489
Kevin O'Connor14021f22009-12-26 23:21:38 -0500490// Transfer data to harddrive using PIO protocol.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400491static int
Kevin O'Connor14021f22009-12-26 23:21:38 -0500492ata_pio_cmd_data(struct disk_op_s *op, int iswrite, struct ata_pio_command *cmd)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400493{
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500494 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
495 u8 channel = ataid / 2;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500496 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500497 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400498
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500499 // Disable interrupts
500 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
501
Kevin O'Connor14021f22009-12-26 23:21:38 -0500502 int ret = send_cmd(op->drive_g, cmd);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400503 if (ret)
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500504 goto fail;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500505 ret = ata_wait_data(iobase1);
506 if (ret)
507 goto fail;
508 ret = ata_pio_transfer(op, iswrite, DISK_SECTOR_SIZE);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500509
510fail:
511 // Enable interrupts
512 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
513 return ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400514}
515
Kevin O'Connor14021f22009-12-26 23:21:38 -0500516// Transfer data to harddrive using DMA protocol.
517static int
518ata_dma_cmd_data(struct disk_op_s *op, struct ata_pio_command *cmd)
519{
Kevin O'Connor4d079022010-01-17 12:58:47 -0500520 if (! CONFIG_ATA_DMA)
521 return -1;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500522 int ret = send_cmd(op->drive_g, cmd);
523 if (ret)
524 return ret;
525 return ata_dma_transfer(op);
526}
527
528// Read/write count blocks from a harddrive.
529static int
530ata_readwrite(struct disk_op_s *op, int iswrite)
531{
532 u64 lba = op->lba;
533
534 int usepio = ata_try_dma(op, iswrite, DISK_SECTOR_SIZE);
535
536 struct ata_pio_command cmd;
537 memset(&cmd, 0, sizeof(cmd));
538
539 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
540 cmd.sector_count2 = op->count >> 8;
541 cmd.lba_low2 = lba >> 24;
542 cmd.lba_mid2 = lba >> 32;
543 cmd.lba_high2 = lba >> 40;
544 lba &= 0xffffff;
545
546 if (usepio)
547 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS_EXT
548 : ATA_CMD_READ_SECTORS_EXT);
549 else
550 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
551 : ATA_CMD_READ_DMA_EXT);
552 } else {
553 if (usepio)
554 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS
555 : ATA_CMD_READ_SECTORS);
556 else
557 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA
558 : ATA_CMD_READ_DMA);
559 }
560
561 cmd.sector_count = op->count;
562 cmd.lba_low = lba;
563 cmd.lba_mid = lba >> 8;
564 cmd.lba_high = lba >> 16;
565 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
566
567 int ret;
568 if (usepio)
569 ret = ata_pio_cmd_data(op, iswrite, &cmd);
570 else
571 ret = ata_dma_cmd_data(op, &cmd);
572 if (ret)
573 return DISK_RET_EBADTRACK;
574 return DISK_RET_SUCCESS;
575}
576
577// 16bit command demuxer for ATA harddrives.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400578int
579process_ata_op(struct disk_op_s *op)
580{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400581 if (!CONFIG_ATA)
582 return 0;
583
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400584 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400585 case CMD_READ:
Kevin O'Connor14021f22009-12-26 23:21:38 -0500586 return ata_readwrite(op, 0);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400587 case CMD_WRITE:
Kevin O'Connor14021f22009-12-26 23:21:38 -0500588 return ata_readwrite(op, 1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400589 default:
590 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400591 }
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400592}
593
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400594
595/****************************************************************
596 * ATAPI functions
597 ****************************************************************/
598
599// Low-level atapi command transmit function.
Kevin O'Connora9caeae2009-03-07 00:09:52 -0500600static int
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500601atapi_cmd_data(struct disk_op_s *op, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500602{
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500603 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400604 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400605 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
606 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500607
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400608 struct ata_pio_command cmd;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500609 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400610 cmd.lba_mid = blocksize;
611 cmd.lba_high = blocksize >> 8;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400612 cmd.command = ATA_CMD_PACKET;
613
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500614 // Disable interrupts
615 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
616
617 int ret = send_cmd(op->drive_g, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400618 if (ret)
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500619 goto fail;
Kevin O'Connor14021f22009-12-26 23:21:38 -0500620 ret = ata_wait_data(iobase1);
621 if (ret)
622 goto fail;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500623
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400624 // Send command to device
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500625 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500626
Kevin O'Connor580e3322009-02-06 22:36:53 -0500627 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500628 if (status < 0) {
629 ret = status;
630 goto fail;
631 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400632
Kevin O'Connor580e3322009-02-06 22:36:53 -0500633 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connorb30c4002009-05-05 21:47:20 -0400634 u8 err = inb(iobase1 + ATA_CB_ERR);
635 // skip "Not Ready"
636 if (err != 0x20)
637 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
638 , status, err);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500639 ret = -2;
640 goto fail;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500641 }
642 if (!(status & ATA_CB_STAT_DRQ)) {
643 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500644 ret = -3;
645 goto fail;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500646 }
647
Kevin O'Connor14021f22009-12-26 23:21:38 -0500648 ret = ata_pio_transfer(op, 0, blocksize);
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500649
650fail:
651 // Enable interrupts
652 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
653 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500654}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500655
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400656// Read sectors from the cdrom.
657int
658cdrom_read(struct disk_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500659{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400660 u8 atacmd[12];
661 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connoree55c762008-05-13 00:18:20 -0400662 atacmd[0]=0x28; // READ command
663 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
664 atacmd[8]=(op->count & 0x00ff);
665 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
666 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
667 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
668 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400669
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500670 return atapi_cmd_data(op, atacmd, sizeof(atacmd), CDROM_SECTOR_SIZE);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400671}
672
Kevin O'Connor14021f22009-12-26 23:21:38 -0500673// 16bit command demuxer for ATAPI cdroms.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400674int
675process_atapi_op(struct disk_op_s *op)
676{
Kevin O'Connor126eac62009-08-16 13:32:24 -0400677 int ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400678 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400679 case CMD_READ:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400680 ret = cdrom_read(op);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500681 if (ret)
682 return DISK_RET_EBADTRACK;
683 return DISK_RET_SUCCESS;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400684 case CMD_FORMAT:
685 case CMD_WRITE:
686 return DISK_RET_EWRITEPROTECT;
Kevin O'Connor42337662009-08-10 00:06:37 -0400687 default:
688 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400689 }
690}
691
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400692// Send a simple atapi command to a drive.
693int
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400694ata_cmd_packet(struct drive_s *drive_g, u8 *cmdbuf, u8 cmdlen
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500695 , u32 length, void *buf_fl)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400696{
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400697 struct disk_op_s dop;
698 memset(&dop, 0, sizeof(dop));
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400699 dop.drive_g = drive_g;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400700 dop.count = 1;
701 dop.buf_fl = buf_fl;
702
Kevin O'Connor42bc3942009-11-20 17:28:19 -0500703 return atapi_cmd_data(&dop, cmdbuf, cmdlen, length);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400704}
705
706
707/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500708 * ATA detect and init
709 ****************************************************************/
710
Kevin O'Connor14021f22009-12-26 23:21:38 -0500711// Send an identify device or identify device packet command.
712static int
713send_ata_identity(struct drive_s *drive_g, u16 *buffer, int command)
714{
715 memset(buffer, 0, DISK_SECTOR_SIZE);
716
717 struct disk_op_s dop;
718 memset(&dop, 0, sizeof(dop));
719 dop.drive_g = drive_g;
720 dop.count = 1;
721 dop.lba = 1;
722 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
723
724 struct ata_pio_command cmd;
725 memset(&cmd, 0, sizeof(cmd));
726 cmd.command = command;
727
728 return ata_pio_cmd_data(&dop, 0, &cmd);
729}
730
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400731// Extract the ATA/ATAPI version info.
732static int
733extract_version(u16 *buffer)
734{
735 // Extract ATA/ATAPI version.
736 u16 ataversion = buffer[80];
737 u8 version;
738 for (version=15; version>0; version--)
739 if (ataversion & (1<<version))
740 break;
741 return version;
742}
743
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500744// Extract common information from IDENTIFY commands.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500745static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400746extract_identify(struct drive_s *drive_g, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500747{
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500748 dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500749
750 // Read model name
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400751 char *model = drive_g->model;
752 int maxsize = ARRAY_SIZE(drive_g->model);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500753 int i;
Kevin O'Connorab515602009-02-11 22:22:15 -0500754 for (i=0; i<maxsize/2; i++) {
755 u16 v = buffer[27+i];
756 model[i*2] = v >> 8;
757 model[i*2+1] = v & 0xff;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500758 }
759 model[maxsize-1] = 0x00;
760
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500761 // Trim trailing spaces from model name.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500762 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
763 model[i] = 0x00;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500764
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500765 // Common flags.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400766 SET_GLOBAL(drive_g->removable, (buffer[0] & 0x80) ? 1 : 0);
767 SET_GLOBAL(drive_g->cntl_info, extract_version(buffer));
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400768}
769
Kevin O'Connor14021f22009-12-26 23:21:38 -0500770// Print out a description of the given atapi drive.
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400771void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400772describe_atapi(struct drive_s *drive_g)
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400773{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400774 u8 ataid = drive_g->cntl_id;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400775 u8 channel = ataid / 2;
776 u8 slave = ataid % 2;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400777 u8 version = drive_g->cntl_info;
778 int iscd = drive_g->floppy_type;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400779 printf("ata%d-%d: %s ATAPI-%d %s", channel, slave
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400780 , drive_g->model, version
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400781 , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500782}
783
Kevin O'Connor14021f22009-12-26 23:21:38 -0500784// Detect if the given drive is an atapi - initialize it if so.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400785static struct drive_s *
786init_drive_atapi(struct drive_s *dummy, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500787{
788 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connor14021f22009-12-26 23:21:38 -0500789 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_PACKET_DEVICE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500790 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400791 return NULL;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500792
793 // Success - setup as ATAPI.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400794 struct drive_s *drive_g = allocDrive();
795 if (! drive_g)
796 return NULL;
797 SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
798 extract_identify(drive_g, buffer);
799 SET_GLOBAL(drive_g->type, DTYPE_ATAPI);
800 SET_GLOBAL(drive_g->blksize, CDROM_SECTOR_SIZE);
801 SET_GLOBAL(drive_g->sectors, (u64)-1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400802 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400803 SET_GLOBAL(drive_g->floppy_type, iscd);
Kevin O'Connor42337662009-08-10 00:06:37 -0400804
805 // fill cdidmap
Kevin O'Connorc892b132009-08-11 21:59:37 -0400806 if (iscd)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400807 map_cd_drive(drive_g);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500808
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400809 return drive_g;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500810}
811
Kevin O'Connor14021f22009-12-26 23:21:38 -0500812// Print out a description of the given ata drive.
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400813void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400814describe_ata(struct drive_s *drive_g)
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400815{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400816 u8 ataid = drive_g->cntl_id;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400817 u8 channel = ataid / 2;
818 u8 slave = ataid % 2;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400819 u64 sectors = drive_g->sectors;
820 u8 version = drive_g->cntl_info;
821 char *model = drive_g->model;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400822 printf("ata%d-%d: %s ATA-%d Hard-Disk", channel, slave, model, version);
823 u64 sizeinmb = sectors >> 11;
824 if (sizeinmb < (1 << 16))
825 printf(" (%u MiBytes)", (u32)sizeinmb);
826 else
827 printf(" (%u GiBytes)", (u32)(sizeinmb >> 10));
828}
829
Kevin O'Connor14021f22009-12-26 23:21:38 -0500830// Detect if the given drive is a regular ata drive - initialize it if so.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400831static struct drive_s *
832init_drive_ata(struct drive_s *dummy, u16 *buffer)
Kevin O'Connorc1437612008-05-18 01:43:07 -0400833{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500834 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connor14021f22009-12-26 23:21:38 -0500835 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_DEVICE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400836 if (ret)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400837 return NULL;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500838
839 // Success - setup as ATA.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400840 struct drive_s *drive_g = allocDrive();
841 if (! drive_g)
842 return NULL;
843 SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
844 extract_identify(drive_g, buffer);
845 SET_GLOBAL(drive_g->type, DTYPE_ATA);
846 SET_GLOBAL(drive_g->blksize, DISK_SECTOR_SIZE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400847
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400848 SET_GLOBAL(drive_g->pchs.cylinders, buffer[1]);
849 SET_GLOBAL(drive_g->pchs.heads, buffer[3]);
850 SET_GLOBAL(drive_g->pchs.spt, buffer[6]);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400851
852 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500853 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
854 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400855 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500856 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400857 SET_GLOBAL(drive_g->sectors, sectors);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400858
859 // Setup disk geometry translation.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400860 setup_translation(drive_g);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400861
Kevin O'Connor0a924122009-02-08 19:43:47 -0500862 // Register with bcv system.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400863 add_bcv_internal(drive_g);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400864
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400865 return drive_g;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400866}
867
Kevin O'Connora5826b52009-10-24 17:57:29 -0400868static u64 SpinupEnd;
869
Kevin O'Connor14021f22009-12-26 23:21:38 -0500870// Wait for non-busy status and check for "floating bus" condition.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400871static int
Kevin O'Connora5826b52009-10-24 17:57:29 -0400872powerup_await_non_bsy(u16 base)
Kevin O'Connor425f2122009-04-18 12:23:00 -0400873{
874 u8 orstatus = 0;
875 u8 status;
876 for (;;) {
877 status = inb(base+ATA_CB_STAT);
878 if (!(status & ATA_CB_STAT_BSY))
879 break;
880 orstatus |= status;
881 if (orstatus == 0xff) {
882 dprintf(1, "powerup IDE floating\n");
883 return orstatus;
884 }
Kevin O'Connora5826b52009-10-24 17:57:29 -0400885 if (check_time(SpinupEnd)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500886 warn_timeout();
Kevin O'Connor425f2122009-04-18 12:23:00 -0400887 return -1;
888 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400889 yield();
Kevin O'Connor425f2122009-04-18 12:23:00 -0400890 }
891 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
892 return status;
893}
894
Kevin O'Connor14021f22009-12-26 23:21:38 -0500895// Detect any drives attached to a given controller.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400896static void
Kevin O'Connora5826b52009-10-24 17:57:29 -0400897ata_detect(void *data)
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500898{
Kevin O'Connora5826b52009-10-24 17:57:29 -0400899 struct ata_channel_s *atachannel = data;
900 int startid = (atachannel - ATA_channels) * 2;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400901 struct drive_s dummy;
902 memset(&dummy, 0, sizeof(dummy));
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500903 // Device detection
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400904 int ataid, last_reset_ataid=-1;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400905 for (ataid=startid; ataid<startid+2; ataid++) {
Kevin O'Connorb1144362009-08-11 20:43:38 -0400906 u8 channel = ataid / 2;
907 u8 slave = ataid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500908
Kevin O'Connorc892b132009-08-11 21:59:37 -0400909 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400910 if (!iobase1)
911 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500912
Kevin O'Connor425f2122009-04-18 12:23:00 -0400913 // Wait for not-bsy.
Kevin O'Connora5826b52009-10-24 17:57:29 -0400914 int status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400915 if (status < 0)
916 continue;
917 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
918 outb(newdh, iobase1+ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400919 ndelay(400);
Kevin O'Connora5826b52009-10-24 17:57:29 -0400920 status = powerup_await_non_bsy(iobase1);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400921 if (status < 0)
922 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500923
Kevin O'Connor580e3322009-02-06 22:36:53 -0500924 // Check if ioport registers look valid.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400925 outb(newdh, iobase1+ATA_CB_DH);
926 u8 dh = inb(iobase1+ATA_CB_DH);
927 outb(0x55, iobase1+ATA_CB_SC);
928 outb(0xaa, iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400929 u8 sc = inb(iobase1+ATA_CB_SC);
930 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400931 dprintf(6, "ata_detect ataid=%d sc=%x sn=%x dh=%x\n"
932 , ataid, sc, sn, dh);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400933 if (sc != 0x55 || sn != 0xaa || dh != newdh)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400934 continue;
935
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400936 // Prepare new drive.
937 dummy.cntl_id = ataid;
Kevin O'Connorb1144362009-08-11 20:43:38 -0400938
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400939 // reset the channel
Kevin O'Connorb1144362009-08-11 20:43:38 -0400940 if (slave && ataid == last_reset_ataid + 1) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500941 // The drive was just reset - no need to reset it again.
942 } else {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400943 ata_reset(&dummy);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400944 last_reset_ataid = ataid;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500945 }
946
Kevin O'Connor580e3322009-02-06 22:36:53 -0500947 // check for ATAPI
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400948 u16 buffer[256];
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400949 struct drive_s *drive_g = init_drive_atapi(&dummy, buffer);
950 if (!drive_g) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400951 // Didn't find an ATAPI drive - look for ATA drive.
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400952 u8 st = inb(iobase1+ATA_CB_STAT);
953 if (!st)
954 // Status not set - can't be a valid drive.
955 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500956
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400957 // Wait for RDY.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400958 int ret = await_rdy(iobase1);
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400959 if (ret < 0)
960 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500961
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400962 // check for ATA.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400963 drive_g = init_drive_ata(&dummy, buffer);
964 if (!drive_g)
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400965 // No ATA drive found
966 continue;
967 }
Kevin O'Connor580e3322009-02-06 22:36:53 -0500968
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400969 u16 resetresult = buffer[93];
970 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
971 if (!slave && (resetresult & 0xdf61) == 0x4041)
972 // resetresult looks valid and device 0 is responding to
973 // device 1 requests - device 1 must not be present - skip
974 // detection.
Kevin O'Connorb1144362009-08-11 20:43:38 -0400975 ataid++;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500976 }
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500977}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400978
Kevin O'Connor14021f22009-12-26 23:21:38 -0500979// Initialize an ata controller and detect its drives.
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400980static void
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500981init_controller(struct ata_channel_s *atachannel
Kevin O'Connor14021f22009-12-26 23:21:38 -0500982 , int bdf, int irq, u32 port1, u32 port2, u32 master)
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500983{
984 SET_GLOBAL(atachannel->irq, irq);
985 SET_GLOBAL(atachannel->pci_bdf, bdf);
986 SET_GLOBAL(atachannel->iobase1, port1);
987 SET_GLOBAL(atachannel->iobase2, port2);
Kevin O'Connor14021f22009-12-26 23:21:38 -0500988 SET_GLOBAL(atachannel->iomaster, master);
989 dprintf(1, "ATA controller %d at %x/%x/%x (irq %d dev %x)\n"
990 , atachannel - ATA_channels, port1, port2, master, irq, bdf);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500991 run_thread(ata_detect, atachannel);
992}
993
Kevin O'Connor525219b2009-12-05 13:36:18 -0500994#define IRQ_ATA1 14
995#define IRQ_ATA2 15
996
Kevin O'Connor14021f22009-12-26 23:21:38 -0500997// Locate and init ata controllers.
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500998static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500999ata_init(void)
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001000{
Kevin O'Connor53236cc2008-08-31 11:16:33 -04001001 // Scan PCI bus for ATA adapters
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001002 int count=0, pcicount=0;
Kevin O'Connor53ab0b62008-12-04 19:22:49 -05001003 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -05001004 foreachpci(bdf, max) {
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001005 pcicount++;
Kevin O'Connor53ab0b62008-12-04 19:22:49 -05001006 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
Kevin O'Connor53236cc2008-08-31 11:16:33 -04001007 continue;
Kevin O'Connorc892b132009-08-11 21:59:37 -04001008 if (count >= ARRAY_SIZE(ATA_channels))
Kevin O'Connor89f67632009-02-11 20:16:49 -05001009 break;
Kevin O'Connor53236cc2008-08-31 11:16:33 -04001010
Kevin O'Connor525219b2009-12-05 13:36:18 -05001011 u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -05001012 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
Kevin O'Connor14021f22009-12-26 23:21:38 -05001013 int master = 0;
Kevin O'Connor4d079022010-01-17 12:58:47 -05001014 if (CONFIG_ATA_DMA && prog_if & 0x80) {
Kevin O'Connor14021f22009-12-26 23:21:38 -05001015 // Check for bus-mastering.
1016 u32 bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_4);
1017 if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
1018 master = bar & PCI_BASE_ADDRESS_IO_MASK;
1019 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
1020 }
1021 }
1022
Kevin O'Connor525219b2009-12-05 13:36:18 -05001023 u32 port1, port2, irq;
Kevin O'Connor53ab0b62008-12-04 19:22:49 -05001024 if (prog_if & 1) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -05001025 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
1026 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
Kevin O'Connor525219b2009-12-05 13:36:18 -05001027 irq = pciirq;
Kevin O'Connor53236cc2008-08-31 11:16:33 -04001028 } else {
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001029 port1 = PORT_ATA1_CMD_BASE;
1030 port2 = PORT_ATA1_CTRL_BASE;
Kevin O'Connor525219b2009-12-05 13:36:18 -05001031 irq = IRQ_ATA1;
Kevin O'Connor53236cc2008-08-31 11:16:33 -04001032 }
Kevin O'Connor14021f22009-12-26 23:21:38 -05001033 init_controller(&ATA_channels[count], bdf, irq, port1, port2, master);
Kevin O'Connor53236cc2008-08-31 11:16:33 -04001034 count++;
1035
Kevin O'Connor53ab0b62008-12-04 19:22:49 -05001036 if (prog_if & 4) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -05001037 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
1038 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
Kevin O'Connor525219b2009-12-05 13:36:18 -05001039 irq = pciirq;
Kevin O'Connor53236cc2008-08-31 11:16:33 -04001040 } else {
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001041 port1 = PORT_ATA2_CMD_BASE;
1042 port2 = PORT_ATA2_CTRL_BASE;
Kevin O'Connor525219b2009-12-05 13:36:18 -05001043 irq = IRQ_ATA2;
Kevin O'Connor53236cc2008-08-31 11:16:33 -04001044 }
Kevin O'Connor14021f22009-12-26 23:21:38 -05001045 init_controller(&ATA_channels[count], bdf, irq, port1, port2
1046 , master ? master + 8 : 0);
Kevin O'Connor53236cc2008-08-31 11:16:33 -04001047 count++;
1048 }
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001049
1050 if (!CONFIG_COREBOOT && !pcicount && ARRAY_SIZE(ATA_channels) >= 2) {
1051 // No PCI devices found - probably a QEMU "-M isapc" machine.
1052 // Try using ISA ports for ATA controllers.
Kevin O'Connor14021f22009-12-26 23:21:38 -05001053 init_controller(&ATA_channels[0], -1, IRQ_ATA1
1054 , PORT_ATA1_CMD_BASE, PORT_ATA1_CTRL_BASE, 0);
1055 init_controller(&ATA_channels[1], -1, IRQ_ATA2
1056 , PORT_ATA2_CMD_BASE, PORT_ATA2_CTRL_BASE, 0);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -05001057 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001058}
1059
1060void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001061ata_setup(void)
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001062{
Kevin O'Connor59c75742010-02-13 18:49:24 -05001063 ASSERT32FLAT();
Kevin O'Connorc1437612008-05-18 01:43:07 -04001064 if (!CONFIG_ATA)
1065 return;
1066
Kevin O'Connor35192dd2008-06-08 19:18:33 -04001067 dprintf(3, "init hard drives\n");
Kevin O'Connora5826b52009-10-24 17:57:29 -04001068
1069 SpinupEnd = calc_future_tsc(IDE_TIMEOUT);
Kevin O'Connorc1437612008-05-18 01:43:07 -04001070 ata_init();
Kevin O'Connorc1437612008-05-18 01:43:07 -04001071
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001072 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -04001073
Kevin O'Connord21c0892008-11-26 17:02:43 -05001074 enable_hwirq(14, entry_76);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -04001075}