Commit | Line | Data |
---|---|---|
5c4e6f13 PO |
1 | /* |
2 | * linux/drivers/mmc/sdio_ops.c | |
3 | * | |
4 | * Copyright 2006-2007 Pierre Ossman | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or (at | |
9 | * your option) any later version. | |
10 | */ | |
11 | ||
112c9db9 PO |
12 | #include <linux/scatterlist.h> |
13 | ||
5c4e6f13 | 14 | #include <linux/mmc/host.h> |
b2bcc798 | 15 | #include <linux/mmc/card.h> |
5c4e6f13 PO |
16 | #include <linux/mmc/mmc.h> |
17 | #include <linux/mmc/sdio.h> | |
18 | ||
19 | #include "core.h" | |
e70aa3fa | 20 | #include "sdio_ops.h" |
5c4e6f13 PO |
21 | |
22 | int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) | |
23 | { | |
24 | struct mmc_command cmd; | |
25 | int i, err = 0; | |
26 | ||
27 | BUG_ON(!host); | |
28 | ||
29 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
30 | ||
31 | cmd.opcode = SD_IO_SEND_OP_COND; | |
32 | cmd.arg = ocr; | |
af517150 | 33 | cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR; |
5c4e6f13 PO |
34 | |
35 | for (i = 100; i; i--) { | |
36 | err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); | |
37 | if (err) | |
38 | break; | |
39 | ||
af517150 DB |
40 | /* if we're just probing, do a single pass */ |
41 | if (ocr == 0) | |
5c4e6f13 PO |
42 | break; |
43 | ||
af517150 DB |
44 | /* otherwise wait until reset completes */ |
45 | if (mmc_host_is_spi(host)) { | |
46 | /* | |
47 | * Both R1_SPI_IDLE and MMC_CARD_BUSY indicate | |
48 | * an initialized card under SPI, but some cards | |
49 | * (Marvell's) only behave when looking at this | |
50 | * one. | |
51 | */ | |
52 | if (cmd.resp[1] & MMC_CARD_BUSY) | |
53 | break; | |
54 | } else { | |
55 | if (cmd.resp[0] & MMC_CARD_BUSY) | |
56 | break; | |
57 | } | |
58 | ||
5c4e6f13 PO |
59 | err = -ETIMEDOUT; |
60 | ||
61 | mmc_delay(10); | |
62 | } | |
63 | ||
64 | if (rocr) | |
af517150 | 65 | *rocr = cmd.resp[mmc_host_is_spi(host) ? 1 : 0]; |
5c4e6f13 PO |
66 | |
67 | return err; | |
68 | } | |
69 | ||
b2bcc798 PO |
70 | int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn, |
71 | unsigned addr, u8 in, u8* out) | |
72 | { | |
73 | struct mmc_command cmd; | |
74 | int err; | |
75 | ||
76 | BUG_ON(!card); | |
77 | BUG_ON(fn > 7); | |
78 | ||
79 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
80 | ||
81 | cmd.opcode = SD_IO_RW_DIRECT; | |
82 | cmd.arg = write ? 0x80000000 : 0x00000000; | |
83 | cmd.arg |= fn << 28; | |
84 | cmd.arg |= (write && out) ? 0x08000000 : 0x00000000; | |
85 | cmd.arg |= addr << 9; | |
86 | cmd.arg |= in; | |
af517150 | 87 | cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC; |
b2bcc798 PO |
88 | |
89 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | |
90 | if (err) | |
91 | return err; | |
92 | ||
af517150 DB |
93 | if (mmc_host_is_spi(card->host)) { |
94 | /* host driver already reported errors */ | |
95 | } else { | |
96 | if (cmd.resp[0] & R5_ERROR) | |
97 | return -EIO; | |
98 | if (cmd.resp[0] & R5_FUNCTION_NUMBER) | |
99 | return -EINVAL; | |
100 | if (cmd.resp[0] & R5_OUT_OF_RANGE) | |
101 | return -ERANGE; | |
102 | } | |
b2bcc798 | 103 | |
af517150 DB |
104 | if (out) { |
105 | if (mmc_host_is_spi(card->host)) | |
106 | *out = (cmd.resp[0] >> 8) & 0xFF; | |
107 | else | |
108 | *out = cmd.resp[0] & 0xFF; | |
109 | } | |
b2bcc798 PO |
110 | |
111 | return 0; | |
112 | } | |
113 | ||
112c9db9 | 114 | int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn, |
eb659468 | 115 | unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz) |
112c9db9 PO |
116 | { |
117 | struct mmc_request mrq; | |
118 | struct mmc_command cmd; | |
119 | struct mmc_data data; | |
120 | struct scatterlist sg; | |
121 | ||
122 | BUG_ON(!card); | |
123 | BUG_ON(fn > 7); | |
eb659468 DV |
124 | BUG_ON(blocks == 1 && blksz > 512); |
125 | WARN_ON(blocks == 0); | |
126 | WARN_ON(blksz == 0); | |
112c9db9 PO |
127 | |
128 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
129 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
130 | memset(&data, 0, sizeof(struct mmc_data)); | |
131 | ||
132 | mrq.cmd = &cmd; | |
133 | mrq.data = &data; | |
134 | ||
135 | cmd.opcode = SD_IO_RW_EXTENDED; | |
136 | cmd.arg = write ? 0x80000000 : 0x00000000; | |
137 | cmd.arg |= fn << 28; | |
eb659468 | 138 | cmd.arg |= incr_addr ? 0x04000000 : 0x00000000; |
112c9db9 | 139 | cmd.arg |= addr << 9; |
eb659468 DV |
140 | if (blocks == 1 && blksz <= 512) |
141 | cmd.arg |= (blksz == 512) ? 0 : blksz; /* byte mode */ | |
142 | else | |
143 | cmd.arg |= 0x08000000 | blocks; /* block mode */ | |
af517150 | 144 | cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC; |
112c9db9 | 145 | |
eb659468 DV |
146 | data.blksz = blksz; |
147 | data.blocks = blocks; | |
112c9db9 PO |
148 | data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ; |
149 | data.sg = &sg; | |
150 | data.sg_len = 1; | |
151 | ||
eb659468 | 152 | sg_init_one(&sg, buf, blksz * blocks); |
112c9db9 PO |
153 | |
154 | mmc_set_data_timeout(&data, card); | |
155 | ||
156 | mmc_wait_for_req(card->host, &mrq); | |
157 | ||
158 | if (cmd.error) | |
159 | return cmd.error; | |
160 | if (data.error) | |
161 | return data.error; | |
162 | ||
af517150 DB |
163 | if (mmc_host_is_spi(card->host)) { |
164 | /* host driver already reported errors */ | |
165 | } else { | |
166 | if (cmd.resp[0] & R5_ERROR) | |
167 | return -EIO; | |
168 | if (cmd.resp[0] & R5_FUNCTION_NUMBER) | |
169 | return -EINVAL; | |
170 | if (cmd.resp[0] & R5_OUT_OF_RANGE) | |
171 | return -ERANGE; | |
172 | } | |
112c9db9 PO |
173 | |
174 | return 0; | |
175 | } | |
176 |