sdcard.c revision 329176
1/*- 2 * Copyright (c) 2012-2014 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * This software was developed by SRI International and the University of 6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7 * ("CTSRD"), as part of the DARPA CRASH research programme. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD: stable/11/stand/mips/beri/common/sdcard.c 329176 2018-02-12 18:30:20Z kevans $ 31 */ 32 33#include <sys/types.h> 34#include <sys/endian.h> 35 36#include <stand.h> 37 38 39/* 40 * Altera University Program SD Card micro-driver for boot2 and loader. 41 * 42 * XXXRW: It might be nice to add 'unit' arguments to all APIs to allow 43 * multiple instances to be addressed. 44 */ 45 46/* Constants lifted from altera_sdcard.h -- possibly we should share headers? */ 47#define ALTERA_SDCARD_OFF_RXTX_BUFFER 0 /* 512-byte I/O buffer */ 48#define ALTERA_SDCARD_OFF_CID 512 /* 16-byte Card ID number */ 49#define ALTERA_SDCARD_OFF_CSD 528 /* 16-byte Card Specific Data */ 50#define ALTERA_SDCARD_OFF_OCR 544 /* Operating Conditions Reg */ 51#define ALTERA_SDCARD_OFF_SR 548 /* SD Card Status Register */ 52#define ALTERA_SDCARD_OFF_RCA 552 /* Relative Card Address Reg */ 53#define ALTERA_SDCARD_OFF_CMD_ARG 556 /* Command Argument Register */ 54#define ALTERA_SDCARD_OFF_CMD 560 /* Command Register */ 55#define ALTERA_SDCARD_OFF_ASR 564 /* Auxiliary Status Register */ 56#define ALTERA_SDCARD_OFF_RR1 568 /* Response R1 */ 57 58#define ALTERA_SDCARD_SECTORSIZE 512 59 60#define ALTERA_SDCARD_CMD_SEND_RCA 0x03 /* Retrieve card RCA. */ 61#define ALTERA_SDCARD_CMD_SEND_CSD 0x09 /* Retrieve CSD register. */ 62#define ALTERA_SDCARD_CMD_SEND_CID 0x0A /* Retrieve CID register. */ 63#define ALTERA_SDCARD_CMD_READ_BLOCK 0x11 /* Read block from disk. */ 64#define ALTERA_SDCARD_CMD_WRITE_BLOCK 0x18 /* Write block to disk. */ 65 66#define ALTERA_SDCARD_ASR_CMDVALID 0x0001 67#define ALTERA_SDCARD_ASR_CARDPRESENT 0x0002 68#define ALTERA_SDCARD_ASR_CMDINPROGRESS 0x0004 69#define ALTERA_SDCARD_ASR_SRVALID 0x0008 70#define ALTERA_SDCARD_ASR_CMDTIMEOUT 0x0010 71#define ALTERA_SDCARD_ASR_CMDDATAERROR 0x0020 72 73#define ALTERA_SDCARD_RR1_INITPROCRUNNING 0x0100 74#define ALTERA_SDCARD_RR1_ERASEINTERRUPTED 0x0200 75#define ALTERA_SDCARD_RR1_ILLEGALCOMMAND 0x0400 76#define ALTERA_SDCARD_RR1_COMMANDCRCFAILED 0x0800 77#define ALTERA_SDCARD_RR1_ADDRESSMISALIGNED 0x1000 78#define ALTERA_SDCARD_RR1_ADDRBLOCKRANGE 0x2000 79 80#define ALTERA_SDCARD_CSD_STRUCTURE_BYTE 15 81#define ALTERA_SDCARD_CSD_STRUCTURE_MASK 0xc0 /* 2 bits */ 82#define ALTERA_SDCARD_CSD_STRUCTURE_RSHIFT 6 83#define ALTERA_SDCARD_CSD_SIZE 16 84#define ALTERA_SDCARD_CSD_READ_BL_LEN_BYTE 10 85#define ALTERA_SDCARD_CSD_READ_BL_LEN_MASK 0x0f /* 4 bits */ 86#define ALTERA_SDCARD_CSD_C_SIZE_BYTE0 7 87#define ALTERA_SDCARD_CSD_C_SIZE_MASK0 0xc0 /* top 2 bits */ 88#define ALTERA_SDCARD_CSD_C_SIZE_RSHIFT0 6 89#define ALTERA_SDCARD_CSD_C_SIZE_BYTE1 8 90#define ALTERA_SDCARD_CSD_C_SIZE_MASK1 0xff /* 8 bits */ 91#define ALTERA_SDCARD_CSD_C_SIZE_LSHIFT1 2 92#define ALTERA_SDCARD_CSD_C_SIZE_BYTE2 9 93#define ALTERA_SDCARD_CSD_C_SIZE_MASK2 0x03 /* bottom 2 bits */ 94#define ALTERA_SDCARD_CSD_C_SIZE_LSHIFT2 10 95#define ALTERA_SDCARD_CSD_C_SIZE_MULT_BYTE0 5 96#define ALTERA_SDCARD_CSD_C_SIZE_MULT_MASK0 0x80 /* top 1 bit */ 97#define ALTERA_SDCARD_CSD_C_SIZE_MULT_RSHIFT0 7 98#define ALTERA_SDCARD_CSD_C_SIZE_MULT_BYTE1 6 99#define ALTERA_SDCARD_CSD_C_SIZE_MULT_MASK1 0x03 /* bottom 2 bits */ 100#define ALTERA_SDCARD_CSD_C_SIZE_MULT_LSHIFT1 1 101 102/* 103 * Not all RR1 values are "errors" per se -- check only for the ones that are 104 * when performing error handling. 105 */ 106#define ALTERA_SDCARD_RR1_ERRORMASK \ 107 (ALTERA_SDCARD_RR1_ERASEINTERRUPTED | ALTERA_SDCARD_RR1_ILLEGALCOMMAND | \ 108 ALTERA_SDCARD_RR1_COMMANDCRCFAILED | ALTERA_SDCARD_RR1_ADDRESSMISALIGNED |\ 109 ALTERA_SDCARD_RR1_ADDRBLOCKRANGE) 110 111extern uint8_t __cheri_sdcard_vaddr__[]; 112 113#define ALTERA_SDCARD_PTR(type, offset) \ 114 (volatile type *)(&__cheri_sdcard_vaddr__[(offset)]) 115 116static __inline uint16_t 117altera_sdcard_read_uint16(u_int offset) 118{ 119 volatile uint16_t *p; 120 121 p = ALTERA_SDCARD_PTR(uint16_t, offset); 122 return (le16toh(*p)); 123} 124 125static __inline void 126altera_sdcard_write_uint16(u_int offset, uint16_t v) 127{ 128 volatile uint16_t *p; 129 130 p = ALTERA_SDCARD_PTR(uint16_t, offset); 131 *p = htole16(v); 132} 133 134static __inline void 135altera_sdcard_write_uint32(u_int offset, uint32_t v) 136{ 137 volatile uint32_t *p; 138 139 p = ALTERA_SDCARD_PTR(uint32_t, offset); 140 *p = htole32(v); 141} 142 143static __inline uint16_t 144altera_sdcard_read_asr(void) 145{ 146 147 return (altera_sdcard_read_uint16(ALTERA_SDCARD_OFF_ASR)); 148} 149 150static __inline uint16_t 151altera_sdcard_read_rr1(void) 152{ 153 154 return (altera_sdcard_read_uint16(ALTERA_SDCARD_OFF_RR1)); 155} 156 157static __inline void 158altera_sdcard_write_cmd(uint16_t cmd) 159{ 160 161 altera_sdcard_write_uint16(ALTERA_SDCARD_OFF_CMD, cmd); 162} 163 164static __inline void 165altera_sdcard_write_cmd_arg(uint32_t cmd_arg) 166{ 167 168 altera_sdcard_write_uint32(ALTERA_SDCARD_OFF_CMD_ARG, cmd_arg); 169} 170 171/* NB: Use 16-bit aligned buffer due to hardware features, so 16-bit type. */ 172static __inline void 173altera_sdcard_read_csd(uint16_t *csdp) 174{ 175 volatile uint16_t *hw_csdp; 176 u_int i; 177 178 hw_csdp = ALTERA_SDCARD_PTR(uint16_t, ALTERA_SDCARD_OFF_CSD); 179 for (i = 0; i < ALTERA_SDCARD_CSD_SIZE / sizeof(uint16_t); i++) 180 csdp[i] = hw_csdp[i]; 181} 182 183/* 184 * Private interface: load exactly one block of size ALTERA_SDCARD_SECTORSIZE 185 * from block #lba. 186 */ 187static int 188altera_sdcard_read_block(void *buf, unsigned lba) 189{ 190 volatile uint32_t *rxtxp; 191 uint32_t *bufp; 192 uint16_t asr, rr1; 193 int i; 194 195 if (!(altera_sdcard_read_asr() & ALTERA_SDCARD_ASR_CARDPRESENT)) { 196 printf("SD Card: card not present\n"); 197 return (-1); 198 } 199 200 bufp = (uint32_t *)buf; 201 rxtxp = ALTERA_SDCARD_PTR(uint32_t, ALTERA_SDCARD_OFF_RXTX_BUFFER); 202 203 /* 204 * Issue read block command. 205 */ 206 altera_sdcard_write_cmd_arg(lba * ALTERA_SDCARD_SECTORSIZE); 207 altera_sdcard_write_cmd(ALTERA_SDCARD_CMD_READ_BLOCK); 208 209 /* 210 * Wait for device to signal completion of command. 211 */ 212 while ((asr = altera_sdcard_read_asr()) & 213 ALTERA_SDCARD_ASR_CMDINPROGRESS); 214 215 /* 216 * Due to hardware bugs/features, interpretting this field is messy. 217 */ 218 rr1 = altera_sdcard_read_rr1(); 219 rr1 &= ~ALTERA_SDCARD_RR1_COMMANDCRCFAILED; /* HW bug. */ 220 if (asr & ALTERA_SDCARD_ASR_CMDTIMEOUT) { 221 printf("SD Card: timeout\n"); 222 return (-1); 223 } 224 if ((asr & ALTERA_SDCARD_ASR_CMDDATAERROR) && 225 (rr1 & ALTERA_SDCARD_RR1_ERRORMASK)) { 226 printf("SD Card: asr %u rr1 %u\n", asr, rr1); 227 return (-1); 228 } 229 230 /* 231 * We can't use a regular memcpy() due to byte-enable bugs in the 232 * Altera IP core: instead copy in 32-bit units. 233 */ 234 for (i = 0; i < ALTERA_SDCARD_SECTORSIZE/sizeof(uint32_t); i++) 235 bufp[i] = rxtxp[i]; 236 return (0); 237} 238 239/* 240 * Public interface: load 'nblk' blocks from block #lba into *buf. 241 */ 242int 243altera_sdcard_read(void *buf, unsigned lba, unsigned nblk) 244{ 245 uint8_t *bufp = buf; 246 int i; 247 248 for (i = 0; i < nblk; i++) { 249 if (altera_sdcard_read_block(bufp + i * 250 ALTERA_SDCARD_SECTORSIZE, lba + i) < 0) { 251 printf("SD Card: block read %u failed\n", i); 252 return (-1); 253 } 254 } 255 return (0); 256} 257 258/* 259 * Public interface: query (current) media size. 260 */ 261uint64_t 262altera_sdcard_get_mediasize(void) 263{ 264 uint64_t mediasize; 265 uint64_t c_size, c_size_mult, read_bl_len; 266 uint16_t csd16[ALTERA_SDCARD_CSD_SIZE/sizeof(uint16_t)]; 267 uint8_t *csd8p = (uint8_t *)&csd16; 268 uint8_t byte0, byte1, byte2; 269 270 altera_sdcard_read_csd(csd16); /* Provide 16-bit alignment. */ 271 272 read_bl_len = csd8p[ALTERA_SDCARD_CSD_READ_BL_LEN_BYTE]; 273 read_bl_len &= ALTERA_SDCARD_CSD_READ_BL_LEN_MASK; 274 275 byte0 = csd8p[ALTERA_SDCARD_CSD_C_SIZE_BYTE0]; 276 byte0 &= ALTERA_SDCARD_CSD_C_SIZE_MASK0; 277 byte1 = csd8p[ALTERA_SDCARD_CSD_C_SIZE_BYTE1]; 278 byte2 = csd8p[ALTERA_SDCARD_CSD_C_SIZE_BYTE2]; 279 byte2 &= ALTERA_SDCARD_CSD_C_SIZE_MASK2; 280 c_size = (byte0 >> ALTERA_SDCARD_CSD_C_SIZE_RSHIFT0) | 281 (byte1 << ALTERA_SDCARD_CSD_C_SIZE_LSHIFT1) | 282 (byte2 << ALTERA_SDCARD_CSD_C_SIZE_LSHIFT2); 283 284 byte0 = csd8p[ALTERA_SDCARD_CSD_C_SIZE_MULT_BYTE0]; 285 byte0 &= ALTERA_SDCARD_CSD_C_SIZE_MULT_MASK0; 286 byte1 = csd8p[ALTERA_SDCARD_CSD_C_SIZE_MULT_BYTE1]; 287 byte1 &= ALTERA_SDCARD_CSD_C_SIZE_MULT_MASK1; 288 c_size_mult = (byte0 >> ALTERA_SDCARD_CSD_C_SIZE_MULT_RSHIFT0) | 289 (byte1 << ALTERA_SDCARD_CSD_C_SIZE_MULT_LSHIFT1); 290 291 mediasize = (c_size + 1) * (1 << (c_size_mult + 2)) * 292 (1 << read_bl_len); 293 return (mediasize); 294} 295 296/* 297 * Public interface: is media present / supported? 298 */ 299int 300altera_sdcard_get_present(void) 301{ 302 uint16_t csd16[ALTERA_SDCARD_CSD_SIZE/sizeof(uint16_t)]; 303 uint8_t *csd8p = (uint8_t *)&csd16; 304 uint8_t csd_structure; 305 306 /* First: does status bit think it is there? */ 307 if (!(altera_sdcard_read_asr() & ALTERA_SDCARD_ASR_CARDPRESENT)) { 308 printf("SD Card: not present\n"); 309 return (0); 310 } 311 312 /* Second: do we understand the CSD structure version? */ 313 altera_sdcard_read_csd(csd16); /* Provide 16-bit alignment. */ 314 csd_structure = csd8p[ALTERA_SDCARD_CSD_STRUCTURE_BYTE]; 315 csd_structure &= ALTERA_SDCARD_CSD_STRUCTURE_MASK; 316 csd_structure >>= ALTERA_SDCARD_CSD_STRUCTURE_RSHIFT; 317 if (csd_structure != 0) { 318 printf("SD Card: unrecognised csd %u\n", csd_structure); 319 return (0); 320 } 321 322 return (1); 323} 324 325/* 326 * Public interface: query sector size. 327 */ 328uint64_t 329altera_sdcard_get_sectorsize(void) 330{ 331 332 return (ALTERA_SDCARD_SECTORSIZE); 333} 334