| 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 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef __ATA_BD_H__
|
|---|
| 36 | #define __ATA_BD_H__
|
|---|
| 37 |
|
|---|
| 38 | #include <sys/types.h>
|
|---|
| 39 | #include <fibril_sync.h>
|
|---|
| 40 | #include <string.h>
|
|---|
| 41 |
|
|---|
| 42 | enum {
|
|---|
| 43 | CTL_READ_START = 0,
|
|---|
| 44 | CTL_WRITE_START = 1,
|
|---|
| 45 | };
|
|---|
| 46 |
|
|---|
| 47 | enum {
|
|---|
| 48 | STATUS_FAILURE = 0
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | enum {
|
|---|
| 52 | MAX_DISKS = 2
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | /** ATA Command Register Block. */
|
|---|
| 56 | typedef union {
|
|---|
| 57 | /* Read/Write */
|
|---|
| 58 | struct {
|
|---|
| 59 | uint16_t data_port;
|
|---|
| 60 | uint8_t sector_count;
|
|---|
| 61 | uint8_t sector_number;
|
|---|
| 62 | uint8_t cylinder_low;
|
|---|
| 63 | uint8_t cylinder_high;
|
|---|
| 64 | uint8_t drive_head;
|
|---|
| 65 | uint8_t pad_rw0;
|
|---|
| 66 | };
|
|---|
| 67 |
|
|---|
| 68 | /* Read Only */
|
|---|
| 69 | struct {
|
|---|
| 70 | uint8_t pad_ro0;
|
|---|
| 71 | uint8_t error;
|
|---|
| 72 | uint8_t pad_ro1[5];
|
|---|
| 73 | uint8_t status;
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | /* Write Only */
|
|---|
| 77 | struct {
|
|---|
| 78 | uint8_t pad_wo0;
|
|---|
| 79 | uint8_t features;
|
|---|
| 80 | uint8_t pad_wo1[5];
|
|---|
| 81 | uint8_t command;
|
|---|
| 82 | };
|
|---|
| 83 | } ata_cmd_t;
|
|---|
| 84 |
|
|---|
| 85 | typedef union {
|
|---|
| 86 | /* Read */
|
|---|
| 87 | struct {
|
|---|
| 88 | uint8_t pad0[6];
|
|---|
| 89 | uint8_t alt_status;
|
|---|
| 90 | uint8_t drive_address;
|
|---|
| 91 | };
|
|---|
| 92 |
|
|---|
| 93 | /* Write */
|
|---|
| 94 | struct {
|
|---|
| 95 | uint8_t pad1[6];
|
|---|
| 96 | uint8_t device_control;
|
|---|
| 97 | uint8_t pad2;
|
|---|
| 98 | };
|
|---|
| 99 | } ata_ctl_t;
|
|---|
| 100 |
|
|---|
| 101 | enum devctl_bits {
|
|---|
| 102 | DCR_SRST = 0x04, /**< Software Reset */
|
|---|
| 103 | DCR_nIEN = 0x02 /**< Interrupt Enable (negated) */
|
|---|
| 104 | };
|
|---|
| 105 |
|
|---|
| 106 | enum status_bits {
|
|---|
| 107 | SR_BSY = 0x80, /**< Busy */
|
|---|
| 108 | SR_DRDY = 0x40, /**< Drive Ready */
|
|---|
| 109 | SR_DWF = 0x20, /**< Drive Write Fault */
|
|---|
| 110 | SR_DSC = 0x10, /**< Drive Seek Complete */
|
|---|
| 111 | SR_DRQ = 0x08, /**< Data Request */
|
|---|
| 112 | SR_CORR = 0x04, /**< Corrected Data */
|
|---|
| 113 | SR_IDX = 0x02, /**< Index */
|
|---|
| 114 | SR_ERR = 0x01 /**< Error */
|
|---|
| 115 | };
|
|---|
| 116 |
|
|---|
| 117 | enum drive_head_bits {
|
|---|
| 118 | DHR_DRV = 0x10
|
|---|
| 119 | };
|
|---|
| 120 |
|
|---|
| 121 | enum error_bits {
|
|---|
| 122 | ER_BBK = 0x80, /**< Bad Block Detected */
|
|---|
| 123 | ER_UNC = 0x40, /**< Uncorrectable Data Error */
|
|---|
| 124 | ER_MC = 0x20, /**< Media Changed */
|
|---|
| 125 | ER_IDNF = 0x10, /**< ID Not Found */
|
|---|
| 126 | ER_MCR = 0x08, /**< Media Change Request */
|
|---|
| 127 | ER_ABRT = 0x04, /**< Aborted Command */
|
|---|
| 128 | ER_TK0NF = 0x02, /**< Track 0 Not Found */
|
|---|
| 129 | ER_AMNF = 0x01 /**< Address Mark Not Found */
|
|---|
| 130 | };
|
|---|
| 131 |
|
|---|
| 132 | enum ata_command {
|
|---|
| 133 | CMD_IDENTIFY_DRIVE = 0xEC,
|
|---|
| 134 | CMD_READ_SECTORS = 0x20,
|
|---|
| 135 | CMD_WRITE_SECTORS = 0x30
|
|---|
| 136 | };
|
|---|
| 137 |
|
|---|
| 138 | /** Timeout definitions. Unit is 10 ms. */
|
|---|
| 139 | enum ata_timeout {
|
|---|
| 140 | TIMEOUT_PROBE = 100, /* 1 s */
|
|---|
| 141 | TIMEOUT_BSY = 100, /* 1 s */
|
|---|
| 142 | TIMEOUT_DRDY = 1000 /* 10 s */
|
|---|
| 143 | };
|
|---|
| 144 |
|
|---|
| 145 | typedef struct {
|
|---|
| 146 | bool present;
|
|---|
| 147 | unsigned heads;
|
|---|
| 148 | unsigned cylinders;
|
|---|
| 149 | unsigned sectors;
|
|---|
| 150 | uint64_t blocks;
|
|---|
| 151 |
|
|---|
| 152 | char model[STR_BOUNDS(40) + 1];
|
|---|
| 153 |
|
|---|
| 154 | fibril_mutex_t lock;
|
|---|
| 155 | dev_handle_t dev_handle;
|
|---|
| 156 | } disk_t;
|
|---|
| 157 |
|
|---|
| 158 | #endif
|
|---|
| 159 |
|
|---|
| 160 | /** @}
|
|---|
| 161 | */
|
|---|