source: mainline/uspace/srv/bd/ata_bd/ata_bd.c@ 74520daf

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 74520daf was 36e9cd1, checked in by Martin Decky <martin@…>, 16 years ago

silence compiler warnings (no change in actual functionality)

  • Property mode set to 100644
File size: 17.6 KB
RevLine 
[f8ef660]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 *
[a1f48f6]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.
[12956e57]45 *
46 * The driver services a single controller which can have up to two disks
47 * attached.
[f8ef660]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>
[1e4cada]57#include <fibril_synch.h>
[0e6dce8]58#include <string.h>
[f8ef660]59#include <devmap.h>
60#include <sys/types.h>
[fb6f1a5]61#include <inttypes.h>
[f8ef660]62#include <errno.h>
[1806e5d]63#include <bool.h>
[95bc57c]64#include <task.h>
[1ee00b7]65#include <macros.h>
[f8ef660]66
[4f5caea]67#include "ata_bd.h"
[f8ef660]68
[1313ee9]69#define NAME "ata_bd"
70#define NAMESPACE "bd"
[1806e5d]71
[54d0ddc]72/** Physical block size. Should be always 512. */
[f8ef660]73static const size_t block_size = 512;
[54d0ddc]74
75/** Size of the communication area. */
[f8ef660]76static size_t comm_size;
77
[54d0ddc]78/** I/O base address of the command registers. */
[f8ef660]79static uintptr_t cmd_physical = 0x1f0;
[54d0ddc]80/** I/O base address of the control registers. */
[f8ef660]81static uintptr_t ctl_physical = 0x170;
[54d0ddc]82
[f8ef660]83static ata_cmd_t *cmd;
84static ata_ctl_t *ctl;
85
[12956e57]86/** Per-disk state. */
[5481d1bb]87static disk_t disk[MAX_DISKS];
[f8ef660]88
89static int ata_bd_init(void);
90static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
[1ee00b7]91static int ata_bd_read_blocks(int disk_id, uint64_t ba, size_t cnt,
[f8ef660]92 void *buf);
[1ee00b7]93static int ata_bd_write_blocks(int disk_id, uint64_t ba, size_t cnt,
94 const void *buf);
95static int ata_bd_read_block(int disk_id, uint64_t ba, size_t cnt,
[f8ef660]96 void *buf);
[1ee00b7]97static int ata_bd_write_block(int disk_id, uint64_t ba, size_t cnt,
[d9f4c76]98 const void *buf);
[0e6dce8]99static int disk_init(disk_t *d, int disk_id);
100static int drive_identify(int drive_id, void *buf);
[a99cf073]101static void disk_print_summary(disk_t *d);
[1ee00b7]102static int coord_calc(disk_t *d, uint64_t ba, block_coord_t *bc);
[5048be7]103static void coord_sc_program(const block_coord_t *bc, uint16_t scnt);
[31de325]104static int wait_status(unsigned set, unsigned n_reset, uint8_t *pstatus,
105 unsigned timeout);
[f8ef660]106
107int main(int argc, char **argv)
108{
[1806e5d]109 char name[16];
110 int i, rc;
111 int n_disks;
[f8ef660]112
113 printf(NAME ": ATA disk driver\n");
114
[fb6f1a5]115 printf("I/O address %p/%p\n", ctl_physical, cmd_physical);
[f8ef660]116
117 if (ata_bd_init() != EOK)
118 return -1;
119
[31de325]120 for (i = 0; i < MAX_DISKS; i++) {
121 printf("Identify drive %d... ", i);
122 fflush(stdout);
123
[0e6dce8]124 rc = disk_init(&disk[i], i);
[31de325]125
126 if (rc == EOK) {
[a99cf073]127 disk_print_summary(&disk[i]);
[31de325]128 } else {
129 printf("Not found.\n");
130 }
131 }
[1806e5d]132
133 n_disks = 0;
134
135 for (i = 0; i < MAX_DISKS; i++) {
136 /* Skip unattached drives. */
137 if (disk[i].present == false)
138 continue;
[1313ee9]139
140 snprintf(name, 16, "%s/disk%d", NAMESPACE, i);
[12956e57]141 rc = devmap_device_register(name, &disk[i].dev_handle);
[1806e5d]142 if (rc != EOK) {
143 devmap_hangup_phone(DEVMAP_DRIVER);
[1313ee9]144 printf(NAME ": Unable to register device %s.\n", name);
[1806e5d]145 return rc;
146 }
147 ++n_disks;
148 }
149
150 if (n_disks == 0) {
151 printf("No disks detected.\n");
152 return -1;
153 }
154
155 printf(NAME ": Accepting connections\n");
[95bc57c]156 task_retval(0);
[1806e5d]157 async_manager();
158
159 /* Not reached */
160 return 0;
161}
162
[a99cf073]163/** Print one-line device summary. */
164static void disk_print_summary(disk_t *d)
165{
[4ef117f8]166 uint64_t mbytes;
167
[a99cf073]168 printf("%s: ", d->model);
169
[a1f48f6]170 switch (d->amode) {
171 case am_chs:
[a99cf073]172 printf("CHS %u cylinders, %u heads, %u sectors",
173 disk->geom.cylinders, disk->geom.heads, disk->geom.sectors);
[a1f48f6]174 break;
175 case am_lba28:
[a99cf073]176 printf("LBA-28");
[a1f48f6]177 break;
178 case am_lba48:
179 printf("LBA-48");
180 break;
[a99cf073]181 }
182
[fb6f1a5]183 printf(" %" PRIu64 " blocks", d->blocks, d->blocks / (2 * 1024));
[4ef117f8]184
185 mbytes = d->blocks / (2 * 1024);
186 if (mbytes > 0)
[fb6f1a5]187 printf(" %" PRIu64 " MB.", mbytes);
[4ef117f8]188
189 printf("\n");
[a99cf073]190}
[f8ef660]191
[54d0ddc]192/** Register driver and enable device I/O. */
[f8ef660]193static int ata_bd_init(void)
194{
195 void *vaddr;
[1806e5d]196 int rc;
[f8ef660]197
198 rc = devmap_driver_register(NAME, ata_bd_connection);
199 if (rc < 0) {
200 printf(NAME ": Unable to register driver.\n");
201 return rc;
202 }
203
204 rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr);
205 if (rc != EOK) {
206 printf(NAME ": Could not initialize device I/O space.\n");
207 return rc;
208 }
209
210 cmd = vaddr;
211
212 rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr);
213 if (rc != EOK) {
214 printf(NAME ": Could not initialize device I/O space.\n");
215 return rc;
216 }
217
218 ctl = vaddr;
219
220
221 return EOK;
222}
223
[54d0ddc]224/** Block device connection handler */
[f8ef660]225static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
226{
227 void *fs_va = NULL;
228 ipc_callid_t callid;
229 ipc_call_t call;
230 ipcarg_t method;
231 dev_handle_t dh;
232 int flags;
233 int retval;
[1ee00b7]234 uint64_t ba;
235 size_t cnt;
[f8ef660]236 int disk_id, i;
237
238 /* Get the device handle. */
239 dh = IPC_GET_ARG1(*icall);
240
241 /* Determine which disk device is the client connecting to. */
242 disk_id = -1;
243 for (i = 0; i < MAX_DISKS; i++)
[12956e57]244 if (disk[i].dev_handle == dh)
[f8ef660]245 disk_id = i;
246
[1806e5d]247 if (disk_id < 0 || disk[disk_id].present == false) {
[f8ef660]248 ipc_answer_0(iid, EINVAL);
249 return;
250 }
251
252 /* Answer the IPC_M_CONNECT_ME_TO call. */
253 ipc_answer_0(iid, EOK);
254
[0da4e41]255 if (!async_share_out_receive(&callid, &comm_size, &flags)) {
[f8ef660]256 ipc_answer_0(callid, EHANGUP);
257 return;
258 }
259
260 fs_va = as_get_mappable_page(comm_size);
261 if (fs_va == NULL) {
262 ipc_answer_0(callid, EHANGUP);
263 return;
264 }
265
[0da4e41]266 (void) async_share_out_finalize(callid, fs_va);
[f8ef660]267
268 while (1) {
269 callid = async_get_call(&call);
270 method = IPC_GET_METHOD(call);
271 switch (method) {
272 case IPC_M_PHONE_HUNGUP:
273 /* The other side has hung up. */
274 ipc_answer_0(callid, EOK);
275 return;
[1ee00b7]276 case BD_READ_BLOCKS:
277 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
278 IPC_GET_ARG2(call));
279 cnt = IPC_GET_ARG3(call);
280 if (cnt * block_size > comm_size) {
281 retval = ELIMIT;
282 break;
283 }
284 retval = ata_bd_read_blocks(disk_id, ba, cnt, fs_va);
285 break;
286 case BD_WRITE_BLOCKS:
287 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
288 IPC_GET_ARG2(call));
289 cnt = IPC_GET_ARG3(call);
290 if (cnt * block_size > comm_size) {
291 retval = ELIMIT;
[f8ef660]292 break;
293 }
[1ee00b7]294 retval = ata_bd_write_blocks(disk_id, ba, cnt, fs_va);
[f8ef660]295 break;
[1ee00b7]296 case BD_GET_BLOCK_SIZE:
297 ipc_answer_1(callid, EOK, block_size);
298 continue;
[08232ee]299 case BD_GET_NUM_BLOCKS:
300 ipc_answer_2(callid, EOK, LOWER32(disk[disk_id].blocks),
301 UPPER32(disk[disk_id].blocks));
302 continue;
[f8ef660]303 default:
304 retval = EINVAL;
305 break;
306 }
307 ipc_answer_0(callid, retval);
308 }
309}
310
[0e6dce8]311/** Initialize a disk.
312 *
313 * Probes for a disk, determines its parameters and initializes
314 * the disk structure.
315 */
316static int disk_init(disk_t *d, int disk_id)
317{
[b94334f]318 identify_data_t idata;
[0e6dce8]319 uint8_t model[40];
320 uint16_t w;
321 uint8_t c;
322 size_t pos, len;
323 int rc;
[a1f48f6]324 unsigned i;
[0e6dce8]325
[b94334f]326 rc = drive_identify(disk_id, &idata);
[0e6dce8]327 if (rc != EOK) {
328 d->present = false;
329 return rc;
330 }
331
[a99cf073]332 if ((idata.caps & cap_lba) == 0) {
333 /* Device only supports CHS addressing. */
334 d->amode = am_chs;
335
336 d->geom.cylinders = idata.cylinders;
337 d->geom.heads = idata.heads;
338 d->geom.sectors = idata.sectors;
339
340 d->blocks = d->geom.cylinders * d->geom.heads * d->geom.sectors;
[a1f48f6]341 } else if ((idata.cmd_set1 & cs1_addr48) == 0) {
342 /* Device only supports LBA-28 addressing. */
[a99cf073]343 d->amode = am_lba28;
[0e6dce8]344
[a99cf073]345 d->geom.cylinders = 0;
346 d->geom.heads = 0;
347 d->geom.sectors = 0;
348
349 d->blocks =
[a1f48f6]350 (uint32_t) idata.total_lba28_0 |
351 ((uint32_t) idata.total_lba28_1 << 16);
352 } else {
353 /* Device supports LBA-48 addressing. */
354 d->amode = am_lba48;
355
356 d->geom.cylinders = 0;
357 d->geom.heads = 0;
358 d->geom.sectors = 0;
359
360 d->blocks =
361 (uint64_t) idata.total_lba48_0 |
362 ((uint64_t) idata.total_lba48_1 << 16) |
363 ((uint64_t) idata.total_lba48_2 << 32) |
364 ((uint64_t) idata.total_lba48_3 << 48);
[a99cf073]365 }
[0e6dce8]366
367 /*
368 * Convert model name to string representation.
369 */
370 for (i = 0; i < 20; i++) {
[b94334f]371 w = idata.model_name[i];
[0e6dce8]372 model[2 * i] = w >> 8;
373 model[2 * i + 1] = w & 0x00ff;
374 }
375
376 len = 40;
377 while (len > 0 && model[len - 1] == 0x20)
378 --len;
379
380 pos = 0;
381 for (i = 0; i < len; ++i) {
382 c = model[i];
383 if (c >= 0x80) c = '?';
384
385 chr_encode(c, d->model, &pos, 40);
386 }
387 d->model[pos] = '\0';
388
389 d->present = true;
390 fibril_mutex_initialize(&d->lock);
391
392 return EOK;
393}
394
[1ee00b7]395/** Read multiple blocks from the device. */
396static int ata_bd_read_blocks(int disk_id, uint64_t ba, size_t cnt,
397 void *buf) {
398
[f8ef660]399 int rc;
400
[1ee00b7]401 while (cnt > 0) {
402 rc = ata_bd_read_block(disk_id, ba, 1, buf);
403 if (rc != EOK)
404 return rc;
405
406 ++ba;
407 --cnt;
408 buf += block_size;
409 }
410
411 return EOK;
412}
413
414/** Write multiple blocks to the device. */
415static int ata_bd_write_blocks(int disk_id, uint64_t ba, size_t cnt,
416 const void *buf) {
[f8ef660]417
[1ee00b7]418 int rc;
[f8ef660]419
[1ee00b7]420 while (cnt > 0) {
421 rc = ata_bd_write_block(disk_id, ba, 1, buf);
[f8ef660]422 if (rc != EOK)
423 return rc;
424
[1ee00b7]425 ++ba;
426 --cnt;
[f8ef660]427 buf += block_size;
428 }
429
430 return EOK;
431}
432
[54d0ddc]433/** Issue IDENTIFY command.
434 *
[0e6dce8]435 * Reads @c identify data into the provided buffer. This is used to detect
436 * whether an ATA device is present and if so, to determine its parameters.
[54d0ddc]437 *
438 * @param disk_id Device ID, 0 or 1.
[0e6dce8]439 * @param buf Pointer to a 512-byte buffer.
[54d0ddc]440 */
[0e6dce8]441static int drive_identify(int disk_id, void *buf)
[54d0ddc]442{
443 uint16_t data;
444 uint8_t status;
445 uint8_t drv_head;
446 size_t i;
[f8ef660]447
[54d0ddc]448 drv_head = ((disk_id != 0) ? DHR_DRV : 0);
449
[31de325]450 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
451 return EIO;
452
[54d0ddc]453 pio_write_8(&cmd->drive_head, drv_head);
454
455 /*
[31de325]456 * This is where we would most likely expect a non-existing device to
457 * show up by not setting SR_DRDY.
[54d0ddc]458 */
[31de325]459 if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
460 return EIO;
[54d0ddc]461
462 pio_write_8(&cmd->command, CMD_IDENTIFY_DRIVE);
463
[31de325]464 if (wait_status(0, ~SR_BSY, &status, TIMEOUT_PROBE) != EOK)
465 return EIO;
[54d0ddc]466
467 /* Read data from the disk buffer. */
468
469 if ((status & SR_DRQ) != 0) {
470 for (i = 0; i < block_size / 2; i++) {
471 data = pio_read_16(&cmd->data_port);
[0e6dce8]472 ((uint16_t *) buf)[i] = data;
[54d0ddc]473 }
474 }
475
476 if ((status & SR_ERR) != 0)
477 return EIO;
478
479 return EOK;
480}
481
482/** Read a physical from the device.
483 *
484 * @param disk_id Device index (0 or 1)
[1ee00b7]485 * @param ba Address the first block.
486 * @param cnt Number of blocks to transfer.
[54d0ddc]487 * @param buf Buffer for holding the data.
488 *
489 * @return EOK on success, EIO on error.
490 */
[1ee00b7]491static int ata_bd_read_block(int disk_id, uint64_t ba, size_t blk_cnt,
[f8ef660]492 void *buf)
493{
494 size_t i;
495 uint16_t data;
496 uint8_t status;
497 uint8_t drv_head;
[1806e5d]498 disk_t *d;
[5048be7]499 block_coord_t bc;
[1806e5d]500
501 d = &disk[disk_id];
[36e9cd1]502
503 /* Silence warning. */
504 memset(&bc, 0, sizeof(bc));
[f8ef660]505
[5048be7]506 /* Compute block coordinates. */
[1ee00b7]507 if (coord_calc(d, ba, &bc) != EOK)
[f8ef660]508 return EINVAL;
509
510 /* New value for Drive/Head register */
511 drv_head =
512 ((disk_id != 0) ? DHR_DRV : 0) |
[a99cf073]513 ((d->amode != am_chs) ? DHR_LBA : 0) |
[5048be7]514 (bc.h & 0x0f);
[f8ef660]515
[12956e57]516 fibril_mutex_lock(&d->lock);
[f8ef660]517
518 /* Program a Read Sectors operation. */
519
[31de325]520 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
521 fibril_mutex_unlock(&d->lock);
522 return EIO;
523 }
524
[f8ef660]525 pio_write_8(&cmd->drive_head, drv_head);
[a7de7907]526
[31de325]527 if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
528 fibril_mutex_unlock(&d->lock);
529 return EIO;
530 }
531
[5048be7]532 /* Program block coordinates into the device. */
533 coord_sc_program(&bc, 1);
[a7de7907]534
[1c1657c]535 pio_write_8(&cmd->command, d->amode == am_lba48 ?
536 CMD_READ_SECTORS_EXT : CMD_READ_SECTORS);
[f8ef660]537
[31de325]538 if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
539 fibril_mutex_unlock(&d->lock);
540 return EIO;
541 }
[f8ef660]542
[a7de7907]543 if ((status & SR_DRQ) != 0) {
[31de325]544 /* Read data from the device buffer. */
545
[a7de7907]546 for (i = 0; i < block_size / 2; i++) {
547 data = pio_read_16(&cmd->data_port);
548 ((uint16_t *) buf)[i] = data;
549 }
[f8ef660]550 }
551
[a7de7907]552 if ((status & SR_ERR) != 0)
553 return EIO;
554
[12956e57]555 fibril_mutex_unlock(&d->lock);
[f8ef660]556 return EOK;
557}
558
[54d0ddc]559/** Write a physical block to the device.
560 *
561 * @param disk_id Device index (0 or 1)
[1ee00b7]562 * @param ba Address of the first block.
563 * @param cnt Number of blocks to transfer.
[54d0ddc]564 * @param buf Buffer holding the data to write.
565 *
566 * @return EOK on success, EIO on error.
567 */
[1ee00b7]568static int ata_bd_write_block(int disk_id, uint64_t ba, size_t cnt,
[d9f4c76]569 const void *buf)
570{
571 size_t i;
572 uint8_t status;
573 uint8_t drv_head;
574 disk_t *d;
[5048be7]575 block_coord_t bc;
[d9f4c76]576
577 d = &disk[disk_id];
[36e9cd1]578
579 /* Silence warning. */
580 memset(&bc, 0, sizeof(bc));
[d9f4c76]581
[5048be7]582 /* Compute block coordinates. */
[1ee00b7]583 if (coord_calc(d, ba, &bc) != EOK)
[d9f4c76]584 return EINVAL;
585
586 /* New value for Drive/Head register */
587 drv_head =
588 ((disk_id != 0) ? DHR_DRV : 0) |
[a99cf073]589 ((d->amode != am_chs) ? DHR_LBA : 0) |
[5048be7]590 (bc.h & 0x0f);
[d9f4c76]591
[12956e57]592 fibril_mutex_lock(&d->lock);
[d9f4c76]593
[31de325]594 /* Program a Write Sectors operation. */
595
596 if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
597 fibril_mutex_unlock(&d->lock);
598 return EIO;
599 }
[d9f4c76]600
601 pio_write_8(&cmd->drive_head, drv_head);
[a7de7907]602
[31de325]603 if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
604 fibril_mutex_unlock(&d->lock);
605 return EIO;
606 }
607
[5048be7]608 /* Program block coordinates into the device. */
609 coord_sc_program(&bc, 1);
[a7de7907]610
[1c1657c]611 pio_write_8(&cmd->command, d->amode == am_lba48 ?
612 CMD_WRITE_SECTORS_EXT : CMD_WRITE_SECTORS);
[d9f4c76]613
[31de325]614 if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
615 fibril_mutex_unlock(&d->lock);
616 return EIO;
617 }
[d9f4c76]618
[a7de7907]619 if ((status & SR_DRQ) != 0) {
[31de325]620 /* Write data to the device buffer. */
621
[a7de7907]622 for (i = 0; i < block_size / 2; i++) {
623 pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]);
624 }
[d9f4c76]625 }
626
[12956e57]627 fibril_mutex_unlock(&d->lock);
[a7de7907]628
629 if (status & SR_ERR)
630 return EIO;
631
[d9f4c76]632 return EOK;
633}
634
[5048be7]635/** Calculate block coordinates.
636 *
637 * Calculates block coordinates in the best coordinate system supported
638 * by the device. These can be later programmed into the device using
639 * @c coord_sc_program().
640 *
641 * @return EOK on success or EINVAL if block index is past end of device.
642 */
[1ee00b7]643static int coord_calc(disk_t *d, uint64_t ba, block_coord_t *bc)
[5048be7]644{
645 uint64_t c;
646 uint64_t idx;
647
648 /* Check device bounds. */
[1ee00b7]649 if (ba >= d->blocks)
[5048be7]650 return EINVAL;
651
652 bc->amode = d->amode;
653
654 switch (d->amode) {
655 case am_chs:
656 /* Compute CHS coordinates. */
[1ee00b7]657 c = ba / (d->geom.heads * d->geom.sectors);
658 idx = ba % (d->geom.heads * d->geom.sectors);
[5048be7]659
660 bc->cyl_lo = c & 0xff;
661 bc->cyl_hi = (c >> 8) & 0xff;
662 bc->h = (idx / d->geom.sectors) & 0x0f;
663 bc->sector = (1 + (idx % d->geom.sectors)) & 0xff;
664 break;
665
666 case am_lba28:
667 /* Compute LBA-28 coordinates. */
[1ee00b7]668 bc->c0 = ba & 0xff; /* bits 0-7 */
669 bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */
670 bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */
671 bc->h = (ba >> 24) & 0x0f; /* bits 24-27 */
[5048be7]672 break;
673
674 case am_lba48:
675 /* Compute LBA-48 coordinates. */
[1ee00b7]676 bc->c0 = ba & 0xff; /* bits 0-7 */
677 bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */
678 bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */
679 bc->c3 = (ba >> 24) & 0xff; /* bits 24-31 */
680 bc->c4 = (ba >> 32) & 0xff; /* bits 32-39 */
681 bc->c5 = (ba >> 40) & 0xff; /* bits 40-47 */
[5048be7]682 bc->h = 0;
683 break;
684 }
685
686 return EOK;
687}
688
689/** Program block coordinates and sector count into ATA registers.
690 *
691 * Note that bc->h must be programmed separately into the device/head register.
692 */
693static void coord_sc_program(const block_coord_t *bc, uint16_t scnt)
694{
695 if (bc->amode == am_lba48) {
696 /* Write high-order bits. */
697 pio_write_8(&cmd->sector_count, scnt >> 8);
698 pio_write_8(&cmd->sector_number, bc->c3);
699 pio_write_8(&cmd->cylinder_low, bc->c4);
700 pio_write_8(&cmd->cylinder_high, bc->c5);
701 }
702
703 /* Write low-order bits. */
704 pio_write_8(&cmd->sector_count, scnt & 0x00ff);
705 pio_write_8(&cmd->sector_number, bc->c0);
706 pio_write_8(&cmd->cylinder_low, bc->c1);
707 pio_write_8(&cmd->cylinder_high, bc->c2);
708}
709
[31de325]710/** Wait until some status bits are set and some are reset.
[54d0ddc]711 *
712 * Example: wait_status(SR_DRDY, ~SR_BSY) waits for SR_DRDY to become
713 * set and SR_BSY to become reset.
714 *
715 * @param set Combination if bits which must be all set.
716 * @param n_reset Negated combination of bits which must be all reset.
[31de325]717 * @param pstatus Pointer where to store last read status or NULL.
718 * @param timeout Timeout in 10ms units.
719 *
720 * @return EOK on success, EIO on timeout.
[54d0ddc]721 */
[31de325]722static int wait_status(unsigned set, unsigned n_reset, uint8_t *pstatus,
723 unsigned timeout)
[54d0ddc]724{
725 uint8_t status;
[31de325]726 int cnt;
727
728 status = pio_read_8(&cmd->status);
729
730 /*
731 * This is crude, yet simple. First try with 1us delays
732 * (most likely the device will respond very fast). If not,
733 * start trying every 10 ms.
734 */
735
736 cnt = 100;
737 while ((status & ~n_reset) != 0 || (status & set) != set) {
738 async_usleep(1);
739 --cnt;
740 if (cnt <= 0) break;
741
742 status = pio_read_8(&cmd->status);
743 }
744
745 cnt = timeout;
746 while ((status & ~n_reset) != 0 || (status & set) != set) {
747 async_usleep(10000);
748 --cnt;
749 if (cnt <= 0) break;
[54d0ddc]750
751 status = pio_read_8(&cmd->status);
[31de325]752 }
753
754 if (pstatus)
755 *pstatus = status;
[54d0ddc]756
[31de325]757 if (cnt == 0)
758 return EIO;
759
760 return EOK;
[54d0ddc]761}
762
[f8ef660]763/**
764 * @}
765 */
Note: See TracBrowser for help on using the repository browser.