source: mainline/uspace/srv/bd/ata_bd/ata_bd.c@ 7300c37

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7300c37 was 7ea7db31, checked in by Jakub Jermar <jakub@…>, 15 years ago

Cease using devmap_get_phone() and devmap_hangup_phone() in drivers.

  • Property mode set to 100644
File size: 18.3 KB
Line 
1/*
2 * Copyright (c) 2009 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup bd
30 * @{
31 */
32
33/**
34 * @file
35 * @brief ATA disk driver
36 *
37 * This driver supports CHS, 28-bit and 48-bit LBA addressing. It only uses
38 * PIO transfers. There is no support DMA, the PACKET feature set or any other
39 * fancy features such as S.M.A.R.T, removable devices, etc.
40 *
41 * This driver is based on the ATA-1, ATA-2, ATA-3 and ATA/ATAPI-4 through 7
42 * standards, as published by the ANSI, NCITS and INCITS standards bodies,
43 * which are freely available. This driver contains no vendor-specific
44 * code at this moment.
45 *
46 * The driver services a single controller which can have up to two disks
47 * attached.
48 */
49
50#include <stdio.h>
51#include <libarch/ddi.h>
52#include <ddi.h>
53#include <ipc/ipc.h>
54#include <ipc/bd.h>
55#include <async.h>
56#include <as.h>
57#include <fibril_synch.h>
58#include <str.h>
59#include <devmap.h>
60#include <sys/types.h>
61#include <inttypes.h>
62#include <errno.h>
63#include <bool.h>
64#include <task.h>
65#include <macros.h>
66
67#include "ata_hw.h"
68#include "ata_bd.h"
69
70#define NAME "ata_bd"
71#define NAMESPACE "bd"
72
73/** Number of defined legacy controller base addresses. */
74#define LEGACY_CTLS 4
75
76/** Physical block size. Should be always 512. */
77static const size_t block_size = 512;
78
79/** Size of the communication area. */
80static size_t comm_size;
81
82/** I/O base address of the command registers. */
83static uintptr_t cmd_physical;
84/** I/O base address of the control registers. */
85static uintptr_t ctl_physical;
86
87/** I/O base addresses for legacy (ISA-compatible) controllers. */
88static ata_base_t legacy_base[LEGACY_CTLS] = {
89 { 0x1f0, 0x3f0 },
90 { 0x170, 0x370 },
91 { 0x1e8, 0x3e8 },
92 { 0x168, 0x368 }
93};
94
95static ata_cmd_t *cmd;
96static ata_ctl_t *ctl;
97
98/** Per-disk state. */
99static disk_t disk[MAX_DISKS];
100
101static void print_syntax(void);
102static int ata_bd_init(void);
103static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
104static int ata_bd_read_blocks(int disk_id, uint64_t ba, size_t cnt,
105 void *buf);
106static int ata_bd_write_blocks(int disk_id, uint64_t ba, size_t cnt,
107 const void *buf);
108static int ata_bd_read_block(int disk_id, uint64_t ba, size_t cnt,
109 void *buf);
110static int ata_bd_write_block(int disk_id, uint64_t ba, size_t cnt,
111 const void *buf);
112static int disk_init(disk_t *d, int disk_id);
113static int drive_identify(int drive_id, void *buf);
114static void disk_print_summary(disk_t *d);
115static int coord_calc(disk_t *d, uint64_t ba, block_coord_t *bc);
116static void coord_sc_program(const block_coord_t *bc, uint16_t scnt);
117static int wait_status(unsigned set, unsigned n_reset, uint8_t *pstatus,
118 unsigned timeout);
119
120int main(int argc, char **argv)
121{
122 char name[16];
123 int i, rc;
124 int n_disks;
125 unsigned ctl_num;
126 char *eptr;
127
128 printf(NAME ": ATA disk driver\n");
129
130 if (argc > 1) {
131 ctl_num = strtoul(argv[1], &eptr, 0);
132 if (*eptr != '\0' || ctl_num == 0 || ctl_num > 4) {
133 printf("Invalid argument.\n");
134 print_syntax();
135 return -1;
136 }
137 } else {
138 ctl_num = 1;
139 }
140
141 cmd_physical = legacy_base[ctl_num - 1].cmd;
142 ctl_physical = legacy_base[ctl_num - 1].ctl;
143
144 printf("I/O address %p/%p\n", (void *) cmd_physical,
145 (void *) ctl_physical);
146
147 if (ata_bd_init() != EOK)
148 return -1;
149
150 for (i = 0; i < MAX_DISKS; i++) {
151 printf("Identify drive %d... ", i);
152 fflush(stdout);
153
154 rc = disk_init(&disk[i], i);
155
156 if (rc == EOK) {
157 disk_print_summary(&disk[i]);
158 } else {
159 printf("Not found.\n");
160 }
161 }
162
163 n_disks = 0;
164
165 for (i = 0; i < MAX_DISKS; i++) {
166 /* Skip unattached drives. */
167 if (disk[i].present == false)
168 continue;
169
170 snprintf(name, 16, "%s/ata%udisk%d", NAMESPACE, ctl_num, i);
171 rc = devmap_device_register(name, &disk[i].devmap_handle);
172 if (rc != EOK) {
173 printf(NAME ": Unable to register device %s.\n", name);
174 return rc;
175 }
176 ++n_disks;
177 }
178
179 if (n_disks == 0) {
180 printf("No disks detected.\n");
181 return -1;
182 }
183
184 printf(NAME ": Accepting connections\n");
185 task_retval(0);
186 async_manager();
187
188 /* Not reached */
189 return 0;
190}
191
192
193static void print_syntax(void)
194{
195 printf("Syntax: " NAME " <controller_number>\n");
196 printf("Controller number = 1..4\n");
197}
198
199/** Print one-line device summary. */
200static void disk_print_summary(disk_t *d)
201{
202 uint64_t mbytes;
203
204 printf("%s: ", d->model);
205
206 switch (d->amode) {
207 case am_chs:
208 printf("CHS %u cylinders, %u heads, %u sectors",
209 disk->geom.cylinders, disk->geom.heads, disk->geom.sectors);
210 break;
211 case am_lba28:
212 printf("LBA-28");
213 break;
214 case am_lba48:
215 printf("LBA-48");
216 break;
217 }
218
219 printf(" %" PRIu64 " blocks", d->blocks);
220
221 mbytes = d->blocks / (2 * 1024);
222 if (mbytes > 0)
223 printf(" %" PRIu64 " MB.", mbytes);
224
225 printf("\n");
226}
227
228/** Register driver and enable device I/O. */
229static int ata_bd_init(void)
230{
231 void *vaddr;
232 int rc;
233
234 rc = devmap_driver_register(NAME, ata_bd_connection);
235 if (rc < 0) {
236 printf(NAME ": Unable to register driver.\n");
237 return rc;
238 }
239
240 rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr);
241 if (rc != EOK) {
242 printf(NAME ": Could not initialize device I/O space.\n");
243 return rc;
244 }
245
246 cmd = vaddr;
247
248 rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr);
249 if (rc != EOK) {
250 printf(NAME ": Could not initialize device I/O space.\n");
251 return rc;
252 }
253
254 ctl = vaddr;
255
256
257 return EOK;
258}
259
260/** Block device connection handler */
261static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
262{
263 void *fs_va = NULL;
264 ipc_callid_t callid;
265 ipc_call_t call;
266 sysarg_t method;
267 devmap_handle_t dh;
268 int flags;
269 int retval;
270 uint64_t ba;
271 size_t cnt;
272 int disk_id, i;
273
274 /* Get the device handle. */
275 dh = IPC_GET_ARG1(*icall);
276
277 /* Determine which disk device is the client connecting to. */
278 disk_id = -1;
279 for (i = 0; i < MAX_DISKS; i++)
280 if (disk[i].devmap_handle == dh)
281 disk_id = i;
282
283 if (disk_id < 0 || disk[disk_id].present == false) {
284 ipc_answer_0(iid, EINVAL);
285 return;
286 }
287
288 /* Answer the IPC_M_CONNECT_ME_TO call. */
289 ipc_answer_0(iid, EOK);
290
291 if (!async_share_out_receive(&callid, &comm_size, &flags)) {
292 ipc_answer_0(callid, EHANGUP);
293 return;
294 }
295
296 fs_va = as_get_mappable_page(comm_size);
297 if (fs_va == NULL) {
298 ipc_answer_0(callid, EHANGUP);
299 return;
300 }
301
302 (void) async_share_out_finalize(callid, fs_va);
303
304 while (1) {
305 callid = async_get_call(&call);
306 method = IPC_GET_IMETHOD(call);
307 switch (method) {
308 case IPC_M_PHONE_HUNGUP:
309 /* The other side has hung up. */
310 ipc_answer_0(callid, EOK);
311 return;
312 case BD_READ_BLOCKS:
313 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
314 IPC_GET_ARG2(call));
315 cnt = IPC_GET_ARG3(call);
316 if (cnt * block_size > comm_size) {
317 retval = ELIMIT;
318 break;
319 }
320 retval = ata_bd_read_blocks(disk_id, ba, cnt, fs_va);
321 break;
322 case BD_WRITE_BLOCKS:
323 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
324 IPC_GET_ARG2(call));
325 cnt = IPC_GET_ARG3(call);
326 if (cnt * block_size > comm_size) {
327 retval = ELIMIT;
328 break;
329 }
330 retval = ata_bd_write_blocks(disk_id, ba, cnt, fs_va);
331 break;
332 case BD_GET_BLOCK_SIZE:
333 ipc_answer_1(callid, EOK, block_size);
334 continue;
335 case BD_GET_NUM_BLOCKS:
336 ipc_answer_2(callid, EOK, LOWER32(disk[disk_id].blocks),
337 UPPER32(disk[disk_id].blocks));
338 continue;
339 default:
340 retval = EINVAL;
341 break;
342 }
343 ipc_answer_0(callid, retval);
344 }
345}
346
347/** Initialize a disk.
348 *
349 * Probes for a disk, determines its parameters and initializes
350 * the disk structure.
351 */
352static int disk_init(disk_t *d, int disk_id)
353{
354 identify_data_t idata;
355 uint8_t model[40];
356 uint16_t w;
357 uint8_t c;
358 size_t pos, len;
359 int rc;
360 unsigned i;
361
362 rc = drive_identify(disk_id, &idata);
363 if (rc != EOK) {
364 d->present = false;
365 return rc;
366 }
367
368 if ((idata.caps & cap_lba) == 0) {
369 /* Device only supports CHS addressing. */
370 d->amode = am_chs;
371
372 d->geom.cylinders = idata.cylinders;
373 d->geom.heads = idata.heads;
374 d->geom.sectors = idata.sectors;
375
376 d->blocks = d->geom.cylinders * d->geom.heads * d->geom.sectors;
377 } else if ((idata.cmd_set1 & cs1_addr48) == 0) {
378 /* Device only supports LBA-28 addressing. */
379 d->amode = am_lba28;
380
381 d->geom.cylinders = 0;
382 d->geom.heads = 0;
383 d->geom.sectors = 0;
384
385 d->blocks =
386 (uint32_t) idata.total_lba28_0 |
387 ((uint32_t) idata.total_lba28_1 << 16);
388 } else {
389 /* Device supports LBA-48 addressing. */
390 d->amode = am_lba48;
391
392 d->geom.cylinders = 0;
393 d->geom.heads = 0;
394 d->geom.sectors = 0;
395
396 d->blocks =
397 (uint64_t) idata.total_lba48_0 |
398 ((uint64_t) idata.total_lba48_1 << 16) |
399 ((uint64_t) idata.total_lba48_2 << 32) |
400 ((uint64_t) idata.total_lba48_3 << 48);
401 }
402
403 /*
404 * Convert model name to string representation.
405 */
406 for (i = 0; i < 20; i++) {
407 w = idata.model_name[i];
408 model[2 * i] = w >> 8;
409 model[2 * i + 1] = w & 0x00ff;
410 }
411
412 len = 40;
413 while (len > 0 && model[len - 1] == 0x20)
414 --len;
415
416 pos = 0;
417 for (i = 0; i < len; ++i) {
418 c = model[i];
419 if (c >= 0x80) c = '?';
420
421 chr_encode(c, d->model, &pos, 40);
422 }
423 d->model[pos] = '\0';
424
425 d->present = true;
426 fibril_mutex_initialize(&d->lock);
427
428 return EOK;
429}
430
431/** Read multiple blocks from the device. */
432static int ata_bd_read_blocks(int disk_id, uint64_t ba, size_t cnt,
433 void *buf) {
434
435 int rc;
436
437 while (cnt > 0) {
438 rc = ata_bd_read_block(disk_id, ba, 1, buf);
439 if (rc != EOK)
440 return rc;
441
442 ++ba;
443 --cnt;
444 buf += block_size;
445 }
446
447 return EOK;
448}
449
450/** Write multiple blocks to the device. */
451static int ata_bd_write_blocks(int disk_id, uint64_t ba, size_t cnt,
452 const void *buf) {
453
454 int rc;
455
456 while (cnt > 0) {
457 rc = ata_bd_write_block(disk_id, ba, 1, buf);
458 if (rc != EOK)
459 return rc;
460
461 ++ba;
462 --cnt;
463 buf += block_size;
464 }
465
466 return EOK;
467}
468
469/** Issue IDENTIFY command.
470 *
471 * Reads @c identify data into the provided buffer. This is used to detect
472 * whether an ATA device is present and if so, to determine its parameters.
473 *
474 * @param disk_id Device ID, 0 or 1.
475 * @param buf Pointer to a 512-byte buffer.
476 */
477static int drive_identify(int disk_id, void *buf)
478{
479 uint16_t data;
480 uint8_t status;
481 uint8_t drv_head;
482 size_t i;
483
484 drv_head = ((disk_id != 0) ? DHR_DRV : 0);
485
486 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
487 return EIO;
488
489 pio_write_8(&cmd->drive_head, drv_head);
490
491 /*
492 * This is where we would most likely expect a non-existing device to
493 * show up by not setting SR_DRDY.
494 */
495 if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
496 return EIO;
497
498 pio_write_8(&cmd->command, CMD_IDENTIFY_DRIVE);
499
500 if (wait_status(0, ~SR_BSY, &status, TIMEOUT_PROBE) != EOK)
501 return EIO;
502
503 /* Read data from the disk buffer. */
504
505 if ((status & SR_DRQ) != 0) {
506 for (i = 0; i < block_size / 2; i++) {
507 data = pio_read_16(&cmd->data_port);
508 ((uint16_t *) buf)[i] = data;
509 }
510 }
511
512 if ((status & SR_ERR) != 0)
513 return EIO;
514
515 return EOK;
516}
517
518/** Read a physical from the device.
519 *
520 * @param disk_id Device index (0 or 1)
521 * @param ba Address the first block.
522 * @param cnt Number of blocks to transfer.
523 * @param buf Buffer for holding the data.
524 *
525 * @return EOK on success, EIO on error.
526 */
527static int ata_bd_read_block(int disk_id, uint64_t ba, size_t blk_cnt,
528 void *buf)
529{
530 size_t i;
531 uint16_t data;
532 uint8_t status;
533 uint8_t drv_head;
534 disk_t *d;
535 block_coord_t bc;
536
537 d = &disk[disk_id];
538
539 /* Silence warning. */
540 memset(&bc, 0, sizeof(bc));
541
542 /* Compute block coordinates. */
543 if (coord_calc(d, ba, &bc) != EOK)
544 return EINVAL;
545
546 /* New value for Drive/Head register */
547 drv_head =
548 ((disk_id != 0) ? DHR_DRV : 0) |
549 ((d->amode != am_chs) ? DHR_LBA : 0) |
550 (bc.h & 0x0f);
551
552 fibril_mutex_lock(&d->lock);
553
554 /* Program a Read Sectors operation. */
555
556 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
557 fibril_mutex_unlock(&d->lock);
558 return EIO;
559 }
560
561 pio_write_8(&cmd->drive_head, drv_head);
562
563 if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
564 fibril_mutex_unlock(&d->lock);
565 return EIO;
566 }
567
568 /* Program block coordinates into the device. */
569 coord_sc_program(&bc, 1);
570
571 pio_write_8(&cmd->command, d->amode == am_lba48 ?
572 CMD_READ_SECTORS_EXT : CMD_READ_SECTORS);
573
574 if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
575 fibril_mutex_unlock(&d->lock);
576 return EIO;
577 }
578
579 if ((status & SR_DRQ) != 0) {
580 /* Read data from the device buffer. */
581
582 for (i = 0; i < block_size / 2; i++) {
583 data = pio_read_16(&cmd->data_port);
584 ((uint16_t *) buf)[i] = data;
585 }
586 }
587
588 if ((status & SR_ERR) != 0)
589 return EIO;
590
591 fibril_mutex_unlock(&d->lock);
592 return EOK;
593}
594
595/** Write a physical block to the device.
596 *
597 * @param disk_id Device index (0 or 1)
598 * @param ba Address of the first block.
599 * @param cnt Number of blocks to transfer.
600 * @param buf Buffer holding the data to write.
601 *
602 * @return EOK on success, EIO on error.
603 */
604static int ata_bd_write_block(int disk_id, uint64_t ba, size_t cnt,
605 const void *buf)
606{
607 size_t i;
608 uint8_t status;
609 uint8_t drv_head;
610 disk_t *d;
611 block_coord_t bc;
612
613 d = &disk[disk_id];
614
615 /* Silence warning. */
616 memset(&bc, 0, sizeof(bc));
617
618 /* Compute block coordinates. */
619 if (coord_calc(d, ba, &bc) != EOK)
620 return EINVAL;
621
622 /* New value for Drive/Head register */
623 drv_head =
624 ((disk_id != 0) ? DHR_DRV : 0) |
625 ((d->amode != am_chs) ? DHR_LBA : 0) |
626 (bc.h & 0x0f);
627
628 fibril_mutex_lock(&d->lock);
629
630 /* Program a Write Sectors operation. */
631
632 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
633 fibril_mutex_unlock(&d->lock);
634 return EIO;
635 }
636
637 pio_write_8(&cmd->drive_head, drv_head);
638
639 if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
640 fibril_mutex_unlock(&d->lock);
641 return EIO;
642 }
643
644 /* Program block coordinates into the device. */
645 coord_sc_program(&bc, 1);
646
647 pio_write_8(&cmd->command, d->amode == am_lba48 ?
648 CMD_WRITE_SECTORS_EXT : CMD_WRITE_SECTORS);
649
650 if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
651 fibril_mutex_unlock(&d->lock);
652 return EIO;
653 }
654
655 if ((status & SR_DRQ) != 0) {
656 /* Write data to the device buffer. */
657
658 for (i = 0; i < block_size / 2; i++) {
659 pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]);
660 }
661 }
662
663 fibril_mutex_unlock(&d->lock);
664
665 if (status & SR_ERR)
666 return EIO;
667
668 return EOK;
669}
670
671/** Calculate block coordinates.
672 *
673 * Calculates block coordinates in the best coordinate system supported
674 * by the device. These can be later programmed into the device using
675 * @c coord_sc_program().
676 *
677 * @return EOK on success or EINVAL if block index is past end of device.
678 */
679static int coord_calc(disk_t *d, uint64_t ba, block_coord_t *bc)
680{
681 uint64_t c;
682 uint64_t idx;
683
684 /* Check device bounds. */
685 if (ba >= d->blocks)
686 return EINVAL;
687
688 bc->amode = d->amode;
689
690 switch (d->amode) {
691 case am_chs:
692 /* Compute CHS coordinates. */
693 c = ba / (d->geom.heads * d->geom.sectors);
694 idx = ba % (d->geom.heads * d->geom.sectors);
695
696 bc->cyl_lo = c & 0xff;
697 bc->cyl_hi = (c >> 8) & 0xff;
698 bc->h = (idx / d->geom.sectors) & 0x0f;
699 bc->sector = (1 + (idx % d->geom.sectors)) & 0xff;
700 break;
701
702 case am_lba28:
703 /* Compute LBA-28 coordinates. */
704 bc->c0 = ba & 0xff; /* bits 0-7 */
705 bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */
706 bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */
707 bc->h = (ba >> 24) & 0x0f; /* bits 24-27 */
708 break;
709
710 case am_lba48:
711 /* Compute LBA-48 coordinates. */
712 bc->c0 = ba & 0xff; /* bits 0-7 */
713 bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */
714 bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */
715 bc->c3 = (ba >> 24) & 0xff; /* bits 24-31 */
716 bc->c4 = (ba >> 32) & 0xff; /* bits 32-39 */
717 bc->c5 = (ba >> 40) & 0xff; /* bits 40-47 */
718 bc->h = 0;
719 break;
720 }
721
722 return EOK;
723}
724
725/** Program block coordinates and sector count into ATA registers.
726 *
727 * Note that bc->h must be programmed separately into the device/head register.
728 */
729static void coord_sc_program(const block_coord_t *bc, uint16_t scnt)
730{
731 if (bc->amode == am_lba48) {
732 /* Write high-order bits. */
733 pio_write_8(&cmd->sector_count, scnt >> 8);
734 pio_write_8(&cmd->sector_number, bc->c3);
735 pio_write_8(&cmd->cylinder_low, bc->c4);
736 pio_write_8(&cmd->cylinder_high, bc->c5);
737 }
738
739 /* Write low-order bits. */
740 pio_write_8(&cmd->sector_count, scnt & 0x00ff);
741 pio_write_8(&cmd->sector_number, bc->c0);
742 pio_write_8(&cmd->cylinder_low, bc->c1);
743 pio_write_8(&cmd->cylinder_high, bc->c2);
744}
745
746/** Wait until some status bits are set and some are reset.
747 *
748 * Example: wait_status(SR_DRDY, ~SR_BSY) waits for SR_DRDY to become
749 * set and SR_BSY to become reset.
750 *
751 * @param set Combination if bits which must be all set.
752 * @param n_reset Negated combination of bits which must be all reset.
753 * @param pstatus Pointer where to store last read status or NULL.
754 * @param timeout Timeout in 10ms units.
755 *
756 * @return EOK on success, EIO on timeout.
757 */
758static int wait_status(unsigned set, unsigned n_reset, uint8_t *pstatus,
759 unsigned timeout)
760{
761 uint8_t status;
762 int cnt;
763
764 status = pio_read_8(&cmd->status);
765
766 /*
767 * This is crude, yet simple. First try with 1us delays
768 * (most likely the device will respond very fast). If not,
769 * start trying every 10 ms.
770 */
771
772 cnt = 100;
773 while ((status & ~n_reset) != 0 || (status & set) != set) {
774 async_usleep(1);
775 --cnt;
776 if (cnt <= 0) break;
777
778 status = pio_read_8(&cmd->status);
779 }
780
781 cnt = timeout;
782 while ((status & ~n_reset) != 0 || (status & set) != set) {
783 async_usleep(10000);
784 --cnt;
785 if (cnt <= 0) break;
786
787 status = pio_read_8(&cmd->status);
788 }
789
790 if (pstatus)
791 *pstatus = status;
792
793 if (cnt == 0)
794 return EIO;
795
796 return EOK;
797}
798
799/**
800 * @}
801 */
Note: See TracBrowser for help on using the repository browser.