2 * Copyright (c) 2012-2014 Robert N. M. Watson
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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/types.h>
34 #include <sys/endian.h>
40 * Altera University Program SD Card micro-driver for boot2 and loader.
42 * XXXRW: It might be nice to add 'unit' arguments to all APIs to allow
43 * multiple instances to be addressed.
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 */
58 #define ALTERA_SDCARD_SECTORSIZE 512
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. */
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
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
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
103 * Not all RR1 values are "errors" per se -- check only for the ones that are
104 * when performing error handling.
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)
111 extern uint8_t __cheri_sdcard_vaddr__[];
113 #define ALTERA_SDCARD_PTR(type, offset) \
114 (volatile type *)(&__cheri_sdcard_vaddr__[(offset)])
116 static __inline uint16_t
117 altera_sdcard_read_uint16(u_int offset)
119 volatile uint16_t *p;
121 p = ALTERA_SDCARD_PTR(uint16_t, offset);
122 return (le16toh(*p));
126 altera_sdcard_write_uint16(u_int offset, uint16_t v)
128 volatile uint16_t *p;
130 p = ALTERA_SDCARD_PTR(uint16_t, offset);
135 altera_sdcard_write_uint32(u_int offset, uint32_t v)
137 volatile uint32_t *p;
139 p = ALTERA_SDCARD_PTR(uint32_t, offset);
143 static __inline uint16_t
144 altera_sdcard_read_asr(void)
147 return (altera_sdcard_read_uint16(ALTERA_SDCARD_OFF_ASR));
150 static __inline uint16_t
151 altera_sdcard_read_rr1(void)
154 return (altera_sdcard_read_uint16(ALTERA_SDCARD_OFF_RR1));
158 altera_sdcard_write_cmd(uint16_t cmd)
161 altera_sdcard_write_uint16(ALTERA_SDCARD_OFF_CMD, cmd);
165 altera_sdcard_write_cmd_arg(uint32_t cmd_arg)
168 altera_sdcard_write_uint32(ALTERA_SDCARD_OFF_CMD_ARG, cmd_arg);
171 /* NB: Use 16-bit aligned buffer due to hardware features, so 16-bit type. */
173 altera_sdcard_read_csd(uint16_t *csdp)
175 volatile uint16_t *hw_csdp;
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];
184 * Private interface: load exactly one block of size ALTERA_SDCARD_SECTORSIZE
188 altera_sdcard_read_block(void *buf, unsigned lba)
190 volatile uint32_t *rxtxp;
195 if (!(altera_sdcard_read_asr() & ALTERA_SDCARD_ASR_CARDPRESENT)) {
196 printf("SD Card: card not present\n");
200 bufp = (uint32_t *)buf;
201 rxtxp = ALTERA_SDCARD_PTR(uint32_t, ALTERA_SDCARD_OFF_RXTX_BUFFER);
204 * Issue read block command.
206 altera_sdcard_write_cmd_arg(lba * ALTERA_SDCARD_SECTORSIZE);
207 altera_sdcard_write_cmd(ALTERA_SDCARD_CMD_READ_BLOCK);
210 * Wait for device to signal completion of command.
212 while ((asr = altera_sdcard_read_asr()) &
213 ALTERA_SDCARD_ASR_CMDINPROGRESS);
216 * Due to hardware bugs/features, interpretting this field is messy.
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");
224 if ((asr & ALTERA_SDCARD_ASR_CMDDATAERROR) &&
225 (rr1 & ALTERA_SDCARD_RR1_ERRORMASK)) {
226 printf("SD Card: asr %u rr1 %u\n", asr, rr1);
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.
234 for (i = 0; i < ALTERA_SDCARD_SECTORSIZE/sizeof(uint32_t); i++)
240 * Public interface: load 'nblk' blocks from block #lba into *buf.
243 altera_sdcard_read(void *buf, unsigned lba, unsigned nblk)
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);
259 * Public interface: query (current) media size.
262 altera_sdcard_get_mediasize(void)
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;
270 altera_sdcard_read_csd(csd16); /* Provide 16-bit alignment. */
272 read_bl_len = csd8p[ALTERA_SDCARD_CSD_READ_BL_LEN_BYTE];
273 read_bl_len &= ALTERA_SDCARD_CSD_READ_BL_LEN_MASK;
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);
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);
291 mediasize = (c_size + 1) * (1 << (c_size_mult + 2)) *
297 * Public interface: is media present / supported?
300 altera_sdcard_get_present(void)
302 uint16_t csd16[ALTERA_SDCARD_CSD_SIZE/sizeof(uint16_t)];
303 uint8_t *csd8p = (uint8_t *)&csd16;
304 uint8_t csd_structure;
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");
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);
326 * Public interface: query sector size.
329 altera_sdcard_get_sectorsize(void)
332 return (ALTERA_SDCARD_SECTORSIZE);