Initial commit
[wrt350n-kernel.git] / drivers / mmc / core / sdio_ops.c
blobe1fca588e3853b5a10c477b6bd55ee7078fd19b8
1 /*
2 * linux/drivers/mmc/sdio_ops.c
4 * Copyright 2006-2007 Pierre Ossman
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.
12 #include <linux/scatterlist.h>
14 #include <linux/mmc/host.h>
15 #include <linux/mmc/card.h>
16 #include <linux/mmc/mmc.h>
17 #include <linux/mmc/sdio.h>
19 #include "core.h"
21 int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
23 struct mmc_command cmd;
24 int i, err = 0;
26 BUG_ON(!host);
28 memset(&cmd, 0, sizeof(struct mmc_command));
30 cmd.opcode = SD_IO_SEND_OP_COND;
31 cmd.arg = ocr;
32 cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR;
34 for (i = 100; i; i--) {
35 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
36 if (err)
37 break;
39 /* if we're just probing, do a single pass */
40 if (ocr == 0)
41 break;
43 /* otherwise wait until reset completes */
44 if (mmc_host_is_spi(host)) {
46 * Both R1_SPI_IDLE and MMC_CARD_BUSY indicate
47 * an initialized card under SPI, but some cards
48 * (Marvell's) only behave when looking at this
49 * one.
51 if (cmd.resp[1] & MMC_CARD_BUSY)
52 break;
53 } else {
54 if (cmd.resp[0] & MMC_CARD_BUSY)
55 break;
58 err = -ETIMEDOUT;
60 mmc_delay(10);
63 if (rocr)
64 *rocr = cmd.resp[mmc_host_is_spi(host) ? 1 : 0];
66 return err;
69 int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
70 unsigned addr, u8 in, u8* out)
72 struct mmc_command cmd;
73 int err;
75 BUG_ON(!card);
76 BUG_ON(fn > 7);
78 memset(&cmd, 0, sizeof(struct mmc_command));
80 cmd.opcode = SD_IO_RW_DIRECT;
81 cmd.arg = write ? 0x80000000 : 0x00000000;
82 cmd.arg |= fn << 28;
83 cmd.arg |= (write && out) ? 0x08000000 : 0x00000000;
84 cmd.arg |= addr << 9;
85 cmd.arg |= in;
86 cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
88 err = mmc_wait_for_cmd(card->host, &cmd, 0);
89 if (err)
90 return err;
92 if (mmc_host_is_spi(card->host)) {
93 /* host driver already reported errors */
94 } else {
95 if (cmd.resp[0] & R5_ERROR)
96 return -EIO;
97 if (cmd.resp[0] & R5_FUNCTION_NUMBER)
98 return -EINVAL;
99 if (cmd.resp[0] & R5_OUT_OF_RANGE)
100 return -ERANGE;
103 if (out) {
104 if (mmc_host_is_spi(card->host))
105 *out = (cmd.resp[0] >> 8) & 0xFF;
106 else
107 *out = cmd.resp[0] & 0xFF;
110 return 0;
113 int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
114 unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz)
116 struct mmc_request mrq;
117 struct mmc_command cmd;
118 struct mmc_data data;
119 struct scatterlist sg;
121 BUG_ON(!card);
122 BUG_ON(fn > 7);
123 BUG_ON(blocks == 1 && blksz > 512);
124 WARN_ON(blocks == 0);
125 WARN_ON(blksz == 0);
127 memset(&mrq, 0, sizeof(struct mmc_request));
128 memset(&cmd, 0, sizeof(struct mmc_command));
129 memset(&data, 0, sizeof(struct mmc_data));
131 mrq.cmd = &cmd;
132 mrq.data = &data;
134 cmd.opcode = SD_IO_RW_EXTENDED;
135 cmd.arg = write ? 0x80000000 : 0x00000000;
136 cmd.arg |= fn << 28;
137 cmd.arg |= incr_addr ? 0x04000000 : 0x00000000;
138 cmd.arg |= addr << 9;
139 if (blocks == 1 && blksz <= 512)
140 cmd.arg |= (blksz == 512) ? 0 : blksz; /* byte mode */
141 else
142 cmd.arg |= 0x08000000 | blocks; /* block mode */
143 cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
145 data.blksz = blksz;
146 data.blocks = blocks;
147 data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
148 data.sg = &sg;
149 data.sg_len = 1;
151 sg_init_one(&sg, buf, blksz * blocks);
153 mmc_set_data_timeout(&data, card);
155 mmc_wait_for_req(card->host, &mrq);
157 if (cmd.error)
158 return cmd.error;
159 if (data.error)
160 return data.error;
162 if (mmc_host_is_spi(card->host)) {
163 /* host driver already reported errors */
164 } else {
165 if (cmd.resp[0] & R5_ERROR)
166 return -EIO;
167 if (cmd.resp[0] & R5_FUNCTION_NUMBER)
168 return -EINVAL;
169 if (cmd.resp[0] & R5_OUT_OF_RANGE)
170 return -ERANGE;
173 return 0;