1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012 Robert N. M. Watson 5 * All rights reserved. 6 * 7 * This software was developed by SRI International and the University of 8 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 9 * ("CTSRD"), as part of the DARPA CRASH research programme. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 35 #ifndef _DEV_ALTERA_SDCARD_H_ 36 #define _DEV_ALTERA_SDCARD_H_ 37 38 #define ALTERA_SDCARD_CSD_SIZE 16 39 struct altera_sdcard_csd { 40 uint8_t csd_data[ALTERA_SDCARD_CSD_SIZE]; 41 } __aligned(2); /* CSD is read in 16-bit chunks, so align to match. */ 42 43 struct altera_sdcard_softc { 44 device_t as_dev; 45 int as_unit; 46 struct resource *as_res; 47 int as_rid; 48 struct mtx as_lock; 49 struct cv as_condvar; 50 int as_state; 51 int as_flags; 52 struct disk *as_disk; 53 struct taskqueue *as_taskqueue; 54 struct timeout_task as_task; 55 56 /* 57 * Fields relating to in-progress and pending I/O, if any. 58 */ 59 struct bio_queue_head as_bioq; 60 struct bio *as_currentbio; 61 u_int as_retriesleft; 62 63 /* 64 * Infrequently changing fields cached from the SD Card IP Core. 65 */ 66 struct altera_sdcard_csd as_csd; 67 uint8_t as_csd_structure; /* CSD version. */ 68 uint64_t as_mediasize; 69 }; 70 71 #define ALTERA_SDCARD_LOCK(sc) mtx_lock(&(sc)->as_lock) 72 #define ALTERA_SDCARD_LOCK_ASSERT(sc) mtx_assert(&(sc)->as_lock, MA_OWNED) 73 #define ALTERA_SDCARD_LOCK_DESTROY(sc) mtx_destroy(&(sc)->as_lock) 74 #define ALTERA_SDCARD_LOCK_INIT(sc) mtx_init(&(sc)->as_lock, \ 75 "altera_sdcard", NULL, MTX_DEF) 76 #define ALTERA_SDCARD_UNLOCK(sc) mtx_unlock(&(sc)->as_lock) 77 78 #define ALTERA_SDCARD_CONDVAR_DESTROY(sc) cv_destroy(&(sc)->as_condvar) 79 #define ALTERA_SDCARD_CONDVAR_INIT(sc) cv_init(&(sc)->as_condvar, \ 80 "altera_sdcard_detach_wait") 81 #define ALTERA_SDCARD_CONDVAR_SIGNAL(dc) cv_signal(&(sc)->as_condvar) 82 #define ALTERA_SDCARD_CONDVAR_WAIT(sc) cv_wait(&(sc)->as_condvar, \ 83 &(sc)->as_lock) 84 85 /* 86 * States an instance can be in at any given moment. 87 */ 88 #define ALTERA_SDCARD_STATE_NOCARD 1 /* No card inserted. */ 89 #define ALTERA_SDCARD_STATE_BADCARD 2 /* Card bad/not supported. */ 90 #define ALTERA_SDCARD_STATE_IDLE 3 /* Card present but idle. */ 91 #define ALTERA_SDCARD_STATE_IO 4 /* Card in I/O currently. */ 92 #define ALTERA_SDCARD_STATE_DETACHED 5 /* Driver is detaching. */ 93 94 /* 95 * Different timeout intervals based on state. When just looking for a card 96 * status change, check twice a second. When we're actively waiting on I/O 97 * completion, check every millisecond. 98 */ 99 #define ALTERA_SDCARD_TIMEOUT_NOCARD (hz/2) 100 #define ALTERA_SDCARD_TIMEOUT_IDLE (hz/2) 101 #define ALTERA_SDCARD_TIMEOUT_IO (1) 102 #define ALTERA_SDCARD_TIMEOUT_IOERROR (hz/5) 103 104 /* 105 * Maximum number of retries on an I/O. 106 */ 107 #define ALTERA_SDCARD_RETRY_LIMIT 10 108 109 /* 110 * Driver status flags. 111 */ 112 #define ALTERA_SDCARD_FLAG_DETACHREQ 0x00000001 /* Detach requested. */ 113 #define ALTERA_SDCARD_FLAG_IOERROR 0x00000002 /* Error in progress. */ 114 115 /* 116 * Functions for performing low-level register and memory I/O to/from the SD 117 * Card IP Core. In general, only code in altera_sdcard_io.c is aware of the 118 * hardware interface. 119 */ 120 uint16_t altera_sdcard_read_asr(struct altera_sdcard_softc *sc); 121 int altera_sdcard_read_csd(struct altera_sdcard_softc *sc); 122 123 int altera_sdcard_io_complete(struct altera_sdcard_softc *sc, 124 uint16_t asr); 125 void altera_sdcard_io_start(struct altera_sdcard_softc *sc, 126 struct bio *bp); 127 128 /* 129 * Constants for interpreting the SD Card Card Specific Data (CSD) register. 130 */ 131 #define ALTERA_SDCARD_CSD_STRUCTURE_BYTE 15 132 #define ALTERA_SDCARD_CSD_STRUCTURE_MASK 0xc0 /* 2 bits */ 133 #define ALTERA_SDCARD_CSD_STRUCTURE_RSHIFT 6 134 135 #define ALTERA_SDCARD_CSD_READ_BL_LEN_BYTE 10 136 #define ALTERA_SDCARD_CSD_READ_BL_LEN_MASK 0x0f /* 4 bits */ 137 138 /* 139 * C_SIZE is a 12-bit field helpfully split over three differe bytes of CSD 140 * data. Software ease of use was not a design consideration. 141 */ 142 #define ALTERA_SDCARD_CSD_C_SIZE_BYTE0 7 143 #define ALTERA_SDCARD_CSD_C_SIZE_MASK0 0xc0 /* top 2 bits */ 144 #define ALTERA_SDCARD_CSD_C_SIZE_RSHIFT0 6 145 146 #define ALTERA_SDCARD_CSD_C_SIZE_BYTE1 8 147 #define ALTERA_SDCARD_CSD_C_SIZE_MASK1 0xff /* 8 bits */ 148 #define ALTERA_SDCARD_CSD_C_SIZE_LSHIFT1 2 149 150 #define ALTERA_SDCARD_CSD_C_SIZE_BYTE2 9 151 #define ALTERA_SDCARD_CSD_C_SIZE_MASK2 0x03 /* bottom 2 bits */ 152 #define ALTERA_SDCARD_CSD_C_SIZE_LSHIFT2 10 153 154 #define ALTERA_SDCARD_CSD_C_SIZE_MULT_BYTE0 5 155 #define ALTERA_SDCARD_CSD_C_SIZE_MULT_MASK0 0x80 /* top 1 bit */ 156 #define ALTERA_SDCARD_CSD_C_SIZE_MULT_RSHIFT0 7 157 158 #define ALTERA_SDCARD_CSD_C_SIZE_MULT_BYTE1 6 159 #define ALTERA_SDCARD_CSD_C_SIZE_MULT_MASK1 0x03 /* bottom 2 bits */ 160 #define ALTERA_SDCARD_CSD_C_SIZE_MULT_LSHIFT1 1 161 162 /* 163 * I/O register/buffer offsets, from Table 4.1.1 in the Altera University 164 * Program SD Card IP Core specification. 165 */ 166 #define ALTERA_SDCARD_OFF_RXTX_BUFFER 0 /* 512-byte I/O buffer */ 167 #define ALTERA_SDCARD_OFF_CID 512 /* 16-byte Card ID number */ 168 #define ALTERA_SDCARD_OFF_CSD 528 /* 16-byte Card Specific Data */ 169 #define ALTERA_SDCARD_OFF_OCR 544 /* Operating Conditions Reg */ 170 #define ALTERA_SDCARD_OFF_SR 548 /* SD Card Status Register */ 171 #define ALTERA_SDCARD_OFF_RCA 552 /* Relative Card Address Reg */ 172 #define ALTERA_SDCARD_OFF_CMD_ARG 556 /* Command Argument Register */ 173 #define ALTERA_SDCARD_OFF_CMD 560 /* Command Register */ 174 #define ALTERA_SDCARD_OFF_ASR 564 /* Auxiliary Status Register */ 175 #define ALTERA_SDCARD_OFF_RR1 568 /* Response R1 */ 176 177 /* 178 * The Altera IP Core provides a 16-bit "Additional Status Register" (ASR) 179 * beyond those described in the SD Card specification that captures IP Core 180 * transaction state, such as whether the last command is in progress, the 181 * card has been removed, etc. 182 */ 183 #define ALTERA_SDCARD_ASR_CMDVALID 0x0001 184 #define ALTERA_SDCARD_ASR_CARDPRESENT 0x0002 185 #define ALTERA_SDCARD_ASR_CMDINPROGRESS 0x0004 186 #define ALTERA_SDCARD_ASR_SRVALID 0x0008 187 #define ALTERA_SDCARD_ASR_CMDTIMEOUT 0x0010 188 #define ALTERA_SDCARD_ASR_CMDDATAERROR 0x0020 189 190 /* 191 * The Altera IP Core claims to provide a 16-bit "Response R1" register (RR1) 192 * to provide more detailed error reporting when a read or write fails. 193 * 194 * XXXRW: The specification claims that this field is 16-bit, but then 195 * proceeds to define values as though it is 32-bit. In practice, 16-bit 196 * seems more likely as the register is not 32-bit aligned. 197 */ 198 #define ALTERA_SDCARD_RR1_INITPROCRUNNING 0x0100 199 #define ALTERA_SDCARD_RR1_ERASEINTERRUPTED 0x0200 200 #define ALTERA_SDCARD_RR1_ILLEGALCOMMAND 0x0400 201 #define ALTERA_SDCARD_RR1_COMMANDCRCFAILED 0x0800 202 #define ALTERA_SDCARD_RR1_ADDRESSMISALIGNED 0x1000 203 #define ALTERA_SDCARD_RR1_ADDRBLOCKRANGE 0x2000 204 205 /* 206 * Not all RR1 values are "errors" per se -- check only for the ones that are 207 * when performing error handling. 208 */ 209 #define ALTERA_SDCARD_RR1_ERRORMASK \ 210 (ALTERA_SDCARD_RR1_ERASEINTERRUPTED | ALTERA_SDCARD_RR1_ILLEGALCOMMAND | \ 211 ALTERA_SDCARD_RR1_COMMANDCRCFAILED | ALTERA_SDCARD_RR1_ADDRESSMISALIGNED |\ 212 ALTERA_SDCARD_RR1_ADDRBLOCKRANGE) 213 214 /* 215 * Although SD Cards may have various sector sizes, the Altera IP Core 216 * requires that I/O be done in 512-byte chunks. 217 */ 218 #define ALTERA_SDCARD_SECTORSIZE 512 219 220 /* 221 * SD Card commands used in this driver. 222 */ 223 #define ALTERA_SDCARD_CMD_SEND_RCA 0x03 /* Retrieve card RCA. */ 224 #define ALTERA_SDCARD_CMD_SEND_CSD 0x09 /* Retrieve CSD register. */ 225 #define ALTERA_SDCARD_CMD_SEND_CID 0x0A /* Retrieve CID register. */ 226 #define ALTERA_SDCARD_CMD_READ_BLOCK 0x11 /* Read block from disk. */ 227 #define ALTERA_SDCARD_CMD_WRITE_BLOCK 0x18 /* Write block to disk. */ 228 229 /* 230 * Functions exposed by the device driver core to newbus(9) bus attachment 231 * implementations. 232 */ 233 void altera_sdcard_attach(struct altera_sdcard_softc *sc); 234 void altera_sdcard_detach(struct altera_sdcard_softc *sc); 235 void altera_sdcard_task(void *arg, int pending); 236 237 /* 238 * Functions exposed by the device driver core to the disk(9) front-end. 239 */ 240 void altera_sdcard_start(struct altera_sdcard_softc *sc); 241 242 /* 243 * Functions relating to the implementation of disk(9) KPIs for the SD Card 244 * driver. 245 */ 246 void altera_sdcard_disk_insert(struct altera_sdcard_softc *sc); 247 void altera_sdcard_disk_remove(struct altera_sdcard_softc *sc); 248 249 extern devclass_t altera_sdcard_devclass; 250 251 #endif /* _DEV_ALTERA_SDCARD_H_ */ 252