sdio: support IO_RW_EXTENDED
[safe/jmp/linux-2.6] / drivers / mmc / core / sdio_io.c
1 /*
2  *  linux/drivers/mmc/core/sdio_io.c
3  *
4  *  Copyright 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
12 #include <linux/mmc/host.h>
13 #include <linux/mmc/card.h>
14 #include <linux/mmc/sdio.h>
15 #include <linux/mmc/sdio_func.h>
16
17 #include "sdio_ops.h"
18
19 /**
20  *      sdio_claim_host - exclusively claim a bus for a certain SDIO function
21  *      @func: SDIO function that will be accessed
22  *
23  *      Claim a bus for a set of operations. The SDIO function given
24  *      is used to figure out which bus is relevant.
25  */
26 void sdio_claim_host(struct sdio_func *func)
27 {
28         BUG_ON(!func);
29         BUG_ON(!func->card);
30
31         mmc_claim_host(func->card->host);
32 }
33 EXPORT_SYMBOL_GPL(sdio_claim_host);
34
35 /**
36  *      sdio_release_host - release a bus for a certain SDIO function
37  *      @func: SDIO function that was accessed
38  *
39  *      Release a bus, allowing others to claim the bus for their
40  *      operations.
41  */
42 void sdio_release_host(struct sdio_func *func)
43 {
44         BUG_ON(!func);
45         BUG_ON(!func->card);
46
47         mmc_release_host(func->card->host);
48 }
49 EXPORT_SYMBOL_GPL(sdio_release_host);
50
51 /**
52  *      sdio_enable_func - enables a SDIO function for usage
53  *      @func: SDIO function to enable
54  *
55  *      Powers up and activates a SDIO function so that register
56  *      access is possible.
57  */
58 int sdio_enable_func(struct sdio_func *func)
59 {
60         int ret;
61         unsigned char reg;
62         unsigned long timeout;
63
64         BUG_ON(!func);
65         BUG_ON(!func->card);
66
67         pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));
68
69         ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
70         if (ret)
71                 goto err;
72
73         reg |= 1 << func->num;
74
75         ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
76         if (ret)
77                 goto err;
78
79         /*
80          * FIXME: This should timeout based on information in the CIS,
81          * but we don't have card to parse that yet.
82          */
83         timeout = jiffies + HZ;
84
85         while (1) {
86                 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, &reg);
87                 if (ret)
88                         goto err;
89                 if (reg & (1 << func->num))
90                         break;
91                 ret = -ETIME;
92                 if (time_after(jiffies, timeout))
93                         goto err;
94         }
95
96         pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));
97
98         return 0;
99
100 err:
101         pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
102         return ret;
103 }
104 EXPORT_SYMBOL_GPL(sdio_enable_func);
105
106 /**
107  *      sdio_disable_func - disable a SDIO function
108  *      @func: SDIO function to disable
109  *
110  *      Powers down and deactivates a SDIO function. Register access
111  *      to this function will fail until the function is reenabled.
112  */
113 int sdio_disable_func(struct sdio_func *func)
114 {
115         int ret;
116         unsigned char reg;
117
118         BUG_ON(!func);
119         BUG_ON(!func->card);
120
121         pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func));
122
123         ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
124         if (ret)
125                 goto err;
126
127         reg &= ~(1 << func->num);
128
129         ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
130         if (ret)
131                 goto err;
132
133         pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));
134
135         return 0;
136
137 err:
138         pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
139         return -EIO;
140 }
141 EXPORT_SYMBOL_GPL(sdio_disable_func);
142
143 /**
144  *      sdio_readb - read a single byte from a SDIO function
145  *      @func: SDIO function to access
146  *      @addr: address to read
147  *      @err_ret: optional status value from transfer
148  *
149  *      Reads a single byte from the address space of a given SDIO
150  *      function. If there is a problem reading the address, 0xff
151  *      is returned and @err_ret will contain the error code.
152  */
153 unsigned char sdio_readb(struct sdio_func *func, unsigned int addr,
154         int *err_ret)
155 {
156         int ret;
157         unsigned char val;
158
159         BUG_ON(!func);
160
161         if (err_ret)
162                 *err_ret = 0;
163
164         ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
165         if (ret) {
166                 if (err_ret)
167                         *err_ret = ret;
168                 return 0xFF;
169         }
170
171         return val;
172 }
173 EXPORT_SYMBOL_GPL(sdio_readb);
174
175 /**
176  *      sdio_writeb - write a single byte to a SDIO function
177  *      @func: SDIO function to access
178  *      @b: byte to write
179  *      @addr: address to write to
180  *      @err_ret: optional status value from transfer
181  *
182  *      Writes a single byte to the address space of a given SDIO
183  *      function. @err_ret will contain the status of the actual
184  *      transfer.
185  */
186 void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
187         int *err_ret)
188 {
189         int ret;
190
191         BUG_ON(!func);
192
193         ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
194         if (err_ret)
195                 *err_ret = ret;
196 }
197 EXPORT_SYMBOL_GPL(sdio_writeb);
198
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