Skip to content

Commit 112c9db

Browse files
author
Pierre Ossman
committed
sdio: support IO_RW_EXTENDED
Support the multi-byte transfer operation, including handlers for common operations like writel()/readl(). Signed-off-by: Pierre Ossman <drzeus@drzeus.cx>
1 parent 5ed334a commit 112c9db

5 files changed

Lines changed: 275 additions & 0 deletions

File tree

‎drivers/mmc/core/sdio_io.c‎

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,187 @@ void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
196196
}
197197
EXPORT_SYMBOL_GPL(sdio_writeb);
198198

199+
/**
200+
* sdio_memcpy_fromio - read a chunk of memory from a SDIO function
201+
* @func: SDIO function to access
202+
* @dst: buffer to store the data
203+
* @addr: address to begin reading from
204+
* @count: number of bytes to read
205+
*
206+
* Reads up to 512 bytes from the address space of a given SDIO
207+
* function. Return value indicates if the transfer succeeded or
208+
* not.
209+
*/
210+
int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
211+
unsigned int addr, int count)
212+
{
213+
return mmc_io_rw_extended(func->card, 0, func->num, addr, 0, dst,
214+
count);
215+
}
216+
EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
217+
218+
/**
219+
* sdio_memcpy_toio - write a chunk of memory to a SDIO function
220+
* @func: SDIO function to access
221+
* @addr: address to start writing to
222+
* @src: buffer that contains the data to write
223+
* @count: number of bytes to write
224+
*
225+
* Writes up to 512 bytes to the address space of a given SDIO
226+
* function. Return value indicates if the transfer succeeded or
227+
* not.
228+
*/
229+
int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
230+
void *src, int count)
231+
{
232+
return mmc_io_rw_extended(func->card, 1, func->num, addr, 0, src,
233+
count);
234+
}
235+
EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
236+
237+
/**
238+
* sdio_readsb - read from a FIFO on a SDIO function
239+
* @func: SDIO function to access
240+
* @dst: buffer to store the data
241+
* @addr: address of (single byte) FIFO
242+
* @count: number of bytes to read
243+
*
244+
* Reads up to 512 bytes from the specified FIFO of a given SDIO
245+
* function. Return value indicates if the transfer succeeded or
246+
* not.
247+
*/
248+
int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
249+
int count)
250+
{
251+
return mmc_io_rw_extended(func->card, 0, func->num, addr, 1, dst,
252+
count);
253+
}
254+
255+
EXPORT_SYMBOL_GPL(sdio_readsb);
256+
257+
/**
258+
* sdio_writesb - write to a FIFO of a SDIO function
259+
* @func: SDIO function to access
260+
* @addr: address of (single byte) FIFO
261+
* @src: buffer that contains the data to write
262+
* @count: number of bytes to write
263+
*
264+
* Writes up to 512 bytes to the specified FIFO of a given SDIO
265+
* function. Return value indicates if the transfer succeeded or
266+
* not.
267+
*/
268+
int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
269+
int count)
270+
{
271+
return mmc_io_rw_extended(func->card, 1, func->num, addr, 1, src,
272+
count);
273+
}
274+
EXPORT_SYMBOL_GPL(sdio_writesb);
275+
276+
/**
277+
* sdio_readw - read a 16 bit integer from a SDIO function
278+
* @func: SDIO function to access
279+
* @addr: address to read
280+
* @err_ret: optional status value from transfer
281+
*
282+
* Reads a 16 bit integer from the address space of a given SDIO
283+
* function. If there is a problem reading the address, 0xffff
284+
* is returned and @err_ret will contain the error code.
285+
*/
286+
unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
287+
int *err_ret)
288+
{
289+
int ret;
290+
291+
if (err_ret)
292+
*err_ret = 0;
293+
294+
ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
295+
if (ret) {
296+
if (err_ret)
297+
*err_ret = ret;
298+
return 0xFFFF;
299+
}
300+
301+
return le16_to_cpu(*(u16*)func->tmpbuf);
302+
}
303+
EXPORT_SYMBOL_GPL(sdio_readw);
304+
305+
/**
306+
* sdio_writew - write a 16 bit integer to a SDIO function
307+
* @func: SDIO function to access
308+
* @b: integer to write
309+
* @addr: address to write to
310+
* @err_ret: optional status value from transfer
311+
*
312+
* Writes a 16 bit integer to the address space of a given SDIO
313+
* function. @err_ret will contain the status of the actual
314+
* transfer.
315+
*/
316+
void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr,
317+
int *err_ret)
318+
{
319+
int ret;
320+
321+
*(u16*)func->tmpbuf = cpu_to_le16(b);
322+
323+
ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
324+
if (err_ret)
325+
*err_ret = ret;
326+
}
327+
EXPORT_SYMBOL_GPL(sdio_writew);
328+
329+
/**
330+
* sdio_readl - read a 32 bit integer from a SDIO function
331+
* @func: SDIO function to access
332+
* @addr: address to read
333+
* @err_ret: optional status value from transfer
334+
*
335+
* Reads a 32 bit integer from the address space of a given SDIO
336+
* function. If there is a problem reading the address,
337+
* 0xffffffff is returned and @err_ret will contain the error
338+
* code.
339+
*/
340+
unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
341+
int *err_ret)
342+
{
343+
int ret;
344+
345+
if (err_ret)
346+
*err_ret = 0;
347+
348+
ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
349+
if (ret) {
350+
if (err_ret)
351+
*err_ret = ret;
352+
return 0xFFFFFFFF;
353+
}
354+
355+
return le32_to_cpu(*(u32*)func->tmpbuf);
356+
}
357+
EXPORT_SYMBOL_GPL(sdio_readl);
358+
359+
/**
360+
* sdio_writel - write a 32 bit integer to a SDIO function
361+
* @func: SDIO function to access
362+
* @b: integer to write
363+
* @addr: address to write to
364+
* @err_ret: optional status value from transfer
365+
*
366+
* Writes a 32 bit integer to the address space of a given SDIO
367+
* function. @err_ret will contain the status of the actual
368+
* transfer.
369+
*/
370+
void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr,
371+
int *err_ret)
372+
{
373+
int ret;
374+
375+
*(u32*)func->tmpbuf = cpu_to_le32(b);
376+
377+
ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
378+
if (err_ret)
379+
*err_ret = ret;
380+
}
381+
EXPORT_SYMBOL_GPL(sdio_writel);
382+

‎drivers/mmc/core/sdio_ops.c‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
* your option) any later version.
1010
*/
1111

12+
#include <asm/scatterlist.h>
13+
#include <linux/scatterlist.h>
14+
1215
#include <linux/mmc/host.h>
1316
#include <linux/mmc/card.h>
1417
#include <linux/mmc/mmc.h>
@@ -84,3 +87,57 @@ int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
8487
return 0;
8588
}
8689

90+
int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
91+
unsigned addr, int bang, u8 *buf, unsigned size)
92+
{
93+
struct mmc_request mrq;
94+
struct mmc_command cmd;
95+
struct mmc_data data;
96+
struct scatterlist sg;
97+
98+
BUG_ON(!card);
99+
BUG_ON(fn > 7);
100+
BUG_ON(size > 512);
101+
102+
memset(&mrq, 0, sizeof(struct mmc_request));
103+
memset(&cmd, 0, sizeof(struct mmc_command));
104+
memset(&data, 0, sizeof(struct mmc_data));
105+
106+
mrq.cmd = &cmd;
107+
mrq.data = &data;
108+
109+
cmd.opcode = SD_IO_RW_EXTENDED;
110+
cmd.arg = write ? 0x80000000 : 0x00000000;
111+
cmd.arg |= fn << 28;
112+
cmd.arg |= bang ? 0x00000000 : 0x04000000;
113+
cmd.arg |= addr << 9;
114+
cmd.arg |= (size == 512) ? 0 : size;
115+
cmd.flags = MMC_RSP_R5 | MMC_CMD_ADTC;
116+
117+
data.blksz = size;
118+
data.blocks = 1;
119+
data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
120+
data.sg = &sg;
121+
data.sg_len = 1;
122+
123+
sg_init_one(&sg, buf, size);
124+
125+
mmc_set_data_timeout(&data, card);
126+
127+
mmc_wait_for_req(card->host, &mrq);
128+
129+
if (cmd.error)
130+
return cmd.error;
131+
if (data.error)
132+
return data.error;
133+
134+
if (cmd.resp[0] & R5_ERROR)
135+
return -EIO;
136+
if (cmd.resp[0] & R5_FUNCTION_NUMBER)
137+
return -EINVAL;
138+
if (cmd.resp[0] & R5_OUT_OF_RANGE)
139+
return -ERANGE;
140+
141+
return 0;
142+
}
143+

‎drivers/mmc/core/sdio_ops.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr);
1616
int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
1717
unsigned addr, u8 in, u8* out);
18+
int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
19+
unsigned addr, int bang, u8 *data, unsigned size);
1820

1921
#endif
2022

‎include/linux/mmc/sdio.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
/* SDIO commands type argument response */
1616
#define SD_IO_SEND_OP_COND 5 /* bcr [23:0] OCR R4 */
1717
#define SD_IO_RW_DIRECT 52 /* ac [31:0] See below R5 */
18+
#define SD_IO_RW_EXTENDED 53 /* adtc [31:0] See below R5 */
1819

1920
/*
2021
* SD_IO_RW_DIRECT argument format:
@@ -26,6 +27,17 @@
2627
* [7:0] Data
2728
*/
2829

30+
/*
31+
* SD_IO_RW_EXTENDED argument format:
32+
*
33+
* [31] R/W flag
34+
* [30:28] Function number
35+
* [27] Block mode
36+
* [26] Increment address
37+
* [25:9] Register address
38+
* [8:0] Byte/block count
39+
*/
40+
2941
/*
3042
SDIO status in R5
3143
Type

‎include/linux/mmc/sdio_func.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ struct sdio_func {
4848
unsigned int state; /* function state */
4949
#define SDIO_STATE_PRESENT (1<<0) /* present in sysfs */
5050

51+
u8 tmpbuf[4]; /* DMA:able scratch buffer */
52+
5153
struct sdio_func_tuple *tuples;
5254
};
5355

@@ -114,9 +116,27 @@ extern int sdio_release_irq(struct sdio_func *func);
114116

115117
extern unsigned char sdio_readb(struct sdio_func *func,
116118
unsigned int addr, int *err_ret);
119+
extern unsigned short sdio_readw(struct sdio_func *func,
120+
unsigned int addr, int *err_ret);
121+
extern unsigned long sdio_readl(struct sdio_func *func,
122+
unsigned int addr, int *err_ret);
123+
124+
extern int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
125+
unsigned int addr, int count);
126+
extern int sdio_readsb(struct sdio_func *func, void *dst,
127+
unsigned int addr, int count);
117128

118129
extern void sdio_writeb(struct sdio_func *func, unsigned char b,
119130
unsigned int addr, int *err_ret);
131+
extern void sdio_writew(struct sdio_func *func, unsigned short b,
132+
unsigned int addr, int *err_ret);
133+
extern void sdio_writel(struct sdio_func *func, unsigned long b,
134+
unsigned int addr, int *err_ret);
135+
136+
extern int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
137+
void *src, int count);
138+
extern int sdio_writesb(struct sdio_func *func, unsigned int addr,
139+
void *src, int count);
120140

121141
#endif
122142

0 commit comments

Comments
 (0)