aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/microblaze/lib/sd.h
blob: e2d0ae38e7576dea0e11c054ad4968ecb63d1e89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -*- c -*- */
/*
 * Copyright 2008 Ettus Research LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_SD_H
#define INCLUDED_SD_H

#include "memory_map.h"

#define SD_READY 1
#define SD_IDLE_WAIT_MAX 100
#define SD_CMD_TIMEOUT 100
#define SD_RD_TIMEOUT 1000

#define SD_CMD0 0
#define SD_CMD1 1
#define SD_CMD9 9
#define SD_CMD10 10
#define SD_CMD12 12
#define SD_CMD13 13
#define SD_CMD16 16
#define SD_CMD17 17 
#define SD_CMD18 18
#define SD_CMD24 24
#define SD_CMD25 25
#define SD_CMD27 27
#define SD_CMD28 28
#define SD_CMD29 29
#define SD_CMD30 30
#define SD_CMD32 32
#define SD_CMD33 33
#define SD_CMD38 38
#define SD_CMD55 55
#define SD_CMD58 58
#define SD_CMD59 59
#define SD_ACMD41 41
#define SD_IDLE 0xFF
#define SD_CRC 0x95

#define SD_R1 1
#define SD_R1B 2
#define SD_R2 3
#define SD_R3 4

#define SD_CMD0_R SD_R1
#define SD_CMD16_R SD_R1
#define SD_CMD17_R SD_R1
#define SD_CMD55_R SD_R1
#define SD_ACMD41_R SD_R1
#define SD_CMD58_R SD_R3

#define SD_BLOCKLEN 512
#define SD_BLOCKLEN_NBITS 9

#define SD_MSK_IDLE 0x01
#define SD_MSK_OCR_33 0xC0
#define SD_MSK_TOK_DATAERROR 0xE0


int sd_init(void);

static inline void
sd_assert_cs(void)
{
  // Wait for idle before doing anything
  while(sdspi_regs->status != SD_READY)
    ;
  sdspi_regs->status = 1;
}

static inline void
sd_deassert_cs(void)
{
  // Wait for idle before doing anything
  while(sdspi_regs->status != SD_READY)
    ;
  sdspi_regs->status = 0;
}

static inline char
sd_rcv_byte(void)
{
  // Wait for idle before doing anything
  while(sdspi_regs->status != SD_READY)
    ;
  sdspi_regs->send_dat = SD_IDLE;
  while(sdspi_regs->status != SD_READY)
    ;
  return sdspi_regs-> receive_dat;
}

static inline void
sd_send_byte(char dat)
{
  // Wait for idle before doing anything
  while(sdspi_regs->status != SD_READY)
    ;      // Wait for status = 1 (ready)
  sdspi_regs->send_dat = dat;
}


int sd_send_command(unsigned char cmd,unsigned char response_type,
		    unsigned char *response,unsigned char *argument);

int sd_read_block (unsigned int blockaddr, unsigned char *buf);
int sd_write_block(unsigned int blockaddr, const unsigned char *buf);

#endif /* INCLUDED_SD_H */