sdio_io.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * linux/drivers/mmc/core/sdio_io.c
  3. *
  4. * Copyright 2007-2008 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. #include <linux/mmc/host.h>
  12. #include <linux/mmc/card.h>
  13. #include <linux/mmc/sdio.h>
  14. #include <linux/mmc/sdio_func.h>
  15. #include "sdio_ops.h"
  16. /**
  17. * sdio_claim_host - exclusively claim a bus for a certain SDIO function
  18. * @func: SDIO function that will be accessed
  19. *
  20. * Claim a bus for a set of operations. The SDIO function given
  21. * is used to figure out which bus is relevant.
  22. */
  23. void sdio_claim_host(struct sdio_func *func)
  24. {
  25. BUG_ON(!func);
  26. BUG_ON(!func->card);
  27. mmc_claim_host(func->card->host);
  28. }
  29. EXPORT_SYMBOL_GPL(sdio_claim_host);
  30. /**
  31. * sdio_release_host - release a bus for a certain SDIO function
  32. * @func: SDIO function that was accessed
  33. *
  34. * Release a bus, allowing others to claim the bus for their
  35. * operations.
  36. */
  37. void sdio_release_host(struct sdio_func *func)
  38. {
  39. BUG_ON(!func);
  40. BUG_ON(!func->card);
  41. mmc_release_host(func->card->host);
  42. }
  43. EXPORT_SYMBOL_GPL(sdio_release_host);
  44. /**
  45. * sdio_enable_func - enables a SDIO function for usage
  46. * @func: SDIO function to enable
  47. *
  48. * Powers up and activates a SDIO function so that register
  49. * access is possible.
  50. */
  51. int sdio_enable_func(struct sdio_func *func)
  52. {
  53. int ret;
  54. unsigned char reg;
  55. unsigned long timeout;
  56. BUG_ON(!func);
  57. BUG_ON(!func->card);
  58. pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));
  59. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
  60. if (ret)
  61. goto err;
  62. reg |= 1 << func->num;
  63. ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
  64. if (ret)
  65. goto err;
  66. timeout = jiffies + msecs_to_jiffies(func->enable_timeout);
  67. while (1) {
  68. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, &reg);
  69. if (ret)
  70. goto err;
  71. if (reg & (1 << func->num))
  72. break;
  73. ret = -ETIME;
  74. if (time_after(jiffies, timeout))
  75. goto err;
  76. }
  77. pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));
  78. return 0;
  79. err:
  80. pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
  81. return ret;
  82. }
  83. EXPORT_SYMBOL_GPL(sdio_enable_func);
  84. /**
  85. * sdio_disable_func - disable a SDIO function
  86. * @func: SDIO function to disable
  87. *
  88. * Powers down and deactivates a SDIO function. Register access
  89. * to this function will fail until the function is reenabled.
  90. */
  91. int sdio_disable_func(struct sdio_func *func)
  92. {
  93. int ret;
  94. unsigned char reg;
  95. BUG_ON(!func);
  96. BUG_ON(!func->card);
  97. pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func));
  98. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
  99. if (ret)
  100. goto err;
  101. reg &= ~(1 << func->num);
  102. ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
  103. if (ret)
  104. goto err;
  105. pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));
  106. return 0;
  107. err:
  108. pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
  109. return -EIO;
  110. }
  111. EXPORT_SYMBOL_GPL(sdio_disable_func);
  112. /**
  113. * sdio_set_block_size - set the block size of an SDIO function
  114. * @func: SDIO function to change
  115. * @blksz: new block size or 0 to use the default.
  116. *
  117. * The default block size is the largest supported by both the function
  118. * and the host, with a maximum of 512 to ensure that arbitrarily sized
  119. * data transfer use the optimal (least) number of commands.
  120. *
  121. * A driver may call this to override the default block size set by the
  122. * core. This can be used to set a block size greater than the maximum
  123. * that reported by the card; it is the driver's responsibility to ensure
  124. * it uses a value that the card supports.
  125. *
  126. * Returns 0 on success, -EINVAL if the host does not support the
  127. * requested block size, or -EIO (etc.) if one of the resultant FBR block
  128. * size register writes failed.
  129. *
  130. */
  131. int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
  132. {
  133. int ret;
  134. if (blksz > func->card->host->max_blk_size)
  135. return -EINVAL;
  136. if (blksz == 0) {
  137. blksz = min(func->max_blksize, func->card->host->max_blk_size);
  138. blksz = min(blksz, 512u);
  139. }
  140. ret = mmc_io_rw_direct(func->card, 1, 0,
  141. SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
  142. blksz & 0xff, NULL);
  143. if (ret)
  144. return ret;
  145. ret = mmc_io_rw_direct(func->card, 1, 0,
  146. SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,
  147. (blksz >> 8) & 0xff, NULL);
  148. if (ret)
  149. return ret;
  150. func->cur_blksize = blksz;
  151. return 0;
  152. }
  153. EXPORT_SYMBOL_GPL(sdio_set_block_size);
  154. /*
  155. * Calculate the maximum byte mode transfer size
  156. */
  157. static inline unsigned int sdio_max_byte_size(struct sdio_func *func)
  158. {
  159. unsigned mval = min(func->card->host->max_seg_size,
  160. func->card->host->max_blk_size);
  161. mval = min(mval, func->max_blksize);
  162. return min(mval, 512u); /* maximum size for byte mode */
  163. }
  164. /**
  165. * sdio_align_size - pads a transfer size to a more optimal value
  166. * @func: SDIO function
  167. * @sz: original transfer size
  168. *
  169. * Pads the original data size with a number of extra bytes in
  170. * order to avoid controller bugs and/or performance hits
  171. * (e.g. some controllers revert to PIO for certain sizes).
  172. *
  173. * If possible, it will also adjust the size so that it can be
  174. * handled in just a single request.
  175. *
  176. * Returns the improved size, which might be unmodified.
  177. */
  178. unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
  179. {
  180. unsigned int orig_sz;
  181. unsigned int blk_sz, byte_sz;
  182. unsigned chunk_sz;
  183. orig_sz = sz;
  184. /*
  185. * Do a first check with the controller, in case it
  186. * wants to increase the size up to a point where it
  187. * might need more than one block.
  188. */
  189. sz = mmc_align_data_size(func->card, sz);
  190. /*
  191. * If we can still do this with just a byte transfer, then
  192. * we're done.
  193. */
  194. if (sz <= sdio_max_byte_size(func))
  195. return sz;
  196. if (func->card->cccr.multi_block) {
  197. /*
  198. * Check if the transfer is already block aligned
  199. */
  200. if ((sz % func->cur_blksize) == 0)
  201. return sz;
  202. /*
  203. * Realign it so that it can be done with one request,
  204. * and recheck if the controller still likes it.
  205. */
  206. blk_sz = ((sz + func->cur_blksize - 1) /
  207. func->cur_blksize) * func->cur_blksize;
  208. blk_sz = mmc_align_data_size(func->card, blk_sz);
  209. /*
  210. * This value is only good if it is still just
  211. * one request.
  212. */
  213. if ((blk_sz % func->cur_blksize) == 0)
  214. return blk_sz;
  215. /*
  216. * We failed to do one request, but at least try to
  217. * pad the remainder properly.
  218. */
  219. byte_sz = mmc_align_data_size(func->card,
  220. sz % func->cur_blksize);
  221. if (byte_sz <= sdio_max_byte_size(func)) {
  222. blk_sz = sz / func->cur_blksize;
  223. return blk_sz * func->cur_blksize + byte_sz;
  224. }
  225. } else {
  226. /*
  227. * We need multiple requests, so first check that the
  228. * controller can handle the chunk size;
  229. */
  230. chunk_sz = mmc_align_data_size(func->card,
  231. sdio_max_byte_size(func));
  232. if (chunk_sz == sdio_max_byte_size(func)) {
  233. /*
  234. * Fix up the size of the remainder (if any)
  235. */
  236. byte_sz = orig_sz % chunk_sz;
  237. if (byte_sz) {
  238. byte_sz = mmc_align_data_size(func->card,
  239. byte_sz);
  240. }
  241. return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
  242. }
  243. }
  244. /*
  245. * The controller is simply incapable of transferring the size
  246. * we want in decent manner, so just return the original size.
  247. */
  248. return orig_sz;
  249. }
  250. EXPORT_SYMBOL_GPL(sdio_align_size);
  251. /* Split an arbitrarily sized data transfer into several
  252. * IO_RW_EXTENDED commands. */
  253. static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
  254. unsigned addr, int incr_addr, u8 *buf, unsigned size)
  255. {
  256. unsigned remainder = size;
  257. unsigned max_blocks;
  258. int ret;
  259. /* Do the bulk of the transfer using block mode (if supported). */
  260. if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
  261. /* Blocks per command is limited by host count, host transfer
  262. * size (we only use a single sg entry) and the maximum for
  263. * IO_RW_EXTENDED of 511 blocks. */
  264. max_blocks = min(func->card->host->max_blk_count,
  265. func->card->host->max_seg_size / func->cur_blksize);
  266. max_blocks = min(max_blocks, 511u);
  267. while (remainder > func->cur_blksize) {
  268. unsigned blocks;
  269. blocks = remainder / func->cur_blksize;
  270. if (blocks > max_blocks)
  271. blocks = max_blocks;
  272. size = blocks * func->cur_blksize;
  273. ret = mmc_io_rw_extended(func->card, write,
  274. func->num, addr, incr_addr, buf,
  275. blocks, func->cur_blksize);
  276. if (ret)
  277. return ret;
  278. remainder -= size;
  279. buf += size;
  280. if (incr_addr)
  281. addr += size;
  282. }
  283. }
  284. /* Write the remainder using byte mode. */
  285. while (remainder > 0) {
  286. size = min(remainder, sdio_max_byte_size(func));
  287. ret = mmc_io_rw_extended(func->card, write, func->num, addr,
  288. incr_addr, buf, 1, size);
  289. if (ret)
  290. return ret;
  291. remainder -= size;
  292. buf += size;
  293. if (incr_addr)
  294. addr += size;
  295. }
  296. return 0;
  297. }
  298. /**
  299. * sdio_readb - read a single byte from a SDIO function
  300. * @func: SDIO function to access
  301. * @addr: address to read
  302. * @err_ret: optional status value from transfer
  303. *
  304. * Reads a single byte from the address space of a given SDIO
  305. * function. If there is a problem reading the address, 0xff
  306. * is returned and @err_ret will contain the error code.
  307. */
  308. u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
  309. {
  310. int ret;
  311. u8 val;
  312. BUG_ON(!func);
  313. if (err_ret)
  314. *err_ret = 0;
  315. ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
  316. if (ret) {
  317. if (err_ret)
  318. *err_ret = ret;
  319. return 0xFF;
  320. }
  321. return val;
  322. }
  323. EXPORT_SYMBOL_GPL(sdio_readb);
  324. /**
  325. * sdio_writeb - write a single byte to a SDIO function
  326. * @func: SDIO function to access
  327. * @b: byte to write
  328. * @addr: address to write to
  329. * @err_ret: optional status value from transfer
  330. *
  331. * Writes a single byte to the address space of a given SDIO
  332. * function. @err_ret will contain the status of the actual
  333. * transfer.
  334. */
  335. void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
  336. {
  337. int ret;
  338. BUG_ON(!func);
  339. ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
  340. if (err_ret)
  341. *err_ret = ret;
  342. }
  343. EXPORT_SYMBOL_GPL(sdio_writeb);
  344. /**
  345. * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
  346. * @func: SDIO function to access
  347. * @dst: buffer to store the data
  348. * @addr: address to begin reading from
  349. * @count: number of bytes to read
  350. *
  351. * Reads from the address space of a given SDIO function. Return
  352. * value indicates if the transfer succeeded or not.
  353. */
  354. int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
  355. unsigned int addr, int count)
  356. {
  357. return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
  358. }
  359. EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
  360. /**
  361. * sdio_memcpy_toio - write a chunk of memory to a SDIO function
  362. * @func: SDIO function to access
  363. * @addr: address to start writing to
  364. * @src: buffer that contains the data to write
  365. * @count: number of bytes to write
  366. *
  367. * Writes to the address space of a given SDIO function. Return
  368. * value indicates if the transfer succeeded or not.
  369. */
  370. int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
  371. void *src, int count)
  372. {
  373. return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
  374. }
  375. EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
  376. /**
  377. * sdio_readsb - read from a FIFO on a SDIO function
  378. * @func: SDIO function to access
  379. * @dst: buffer to store the data
  380. * @addr: address of (single byte) FIFO
  381. * @count: number of bytes to read
  382. *
  383. * Reads from the specified FIFO of a given SDIO function. Return
  384. * value indicates if the transfer succeeded or not.
  385. */
  386. int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
  387. int count)
  388. {
  389. return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
  390. }
  391. EXPORT_SYMBOL_GPL(sdio_readsb);
  392. /**
  393. * sdio_writesb - write to a FIFO of a SDIO function
  394. * @func: SDIO function to access
  395. * @addr: address of (single byte) FIFO
  396. * @src: buffer that contains the data to write
  397. * @count: number of bytes to write
  398. *
  399. * Writes to the specified FIFO of a given SDIO function. Return
  400. * value indicates if the transfer succeeded or not.
  401. */
  402. int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
  403. int count)
  404. {
  405. return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
  406. }
  407. EXPORT_SYMBOL_GPL(sdio_writesb);
  408. /**
  409. * sdio_readw - read a 16 bit integer from a SDIO function
  410. * @func: SDIO function to access
  411. * @addr: address to read
  412. * @err_ret: optional status value from transfer
  413. *
  414. * Reads a 16 bit integer from the address space of a given SDIO
  415. * function. If there is a problem reading the address, 0xffff
  416. * is returned and @err_ret will contain the error code.
  417. */
  418. u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
  419. {
  420. int ret;
  421. if (err_ret)
  422. *err_ret = 0;
  423. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
  424. if (ret) {
  425. if (err_ret)
  426. *err_ret = ret;
  427. return 0xFFFF;
  428. }
  429. return le16_to_cpup((__le16 *)func->tmpbuf);
  430. }
  431. EXPORT_SYMBOL_GPL(sdio_readw);
  432. /**
  433. * sdio_writew - write a 16 bit integer to a SDIO function
  434. * @func: SDIO function to access
  435. * @b: integer to write
  436. * @addr: address to write to
  437. * @err_ret: optional status value from transfer
  438. *
  439. * Writes a 16 bit integer to the address space of a given SDIO
  440. * function. @err_ret will contain the status of the actual
  441. * transfer.
  442. */
  443. void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
  444. {
  445. int ret;
  446. *(__le16 *)func->tmpbuf = cpu_to_le16(b);
  447. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
  448. if (err_ret)
  449. *err_ret = ret;
  450. }
  451. EXPORT_SYMBOL_GPL(sdio_writew);
  452. /**
  453. * sdio_readl - read a 32 bit integer from a SDIO function
  454. * @func: SDIO function to access
  455. * @addr: address to read
  456. * @err_ret: optional status value from transfer
  457. *
  458. * Reads a 32 bit integer from the address space of a given SDIO
  459. * function. If there is a problem reading the address,
  460. * 0xffffffff is returned and @err_ret will contain the error
  461. * code.
  462. */
  463. u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
  464. {
  465. int ret;
  466. if (err_ret)
  467. *err_ret = 0;
  468. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
  469. if (ret) {
  470. if (err_ret)
  471. *err_ret = ret;
  472. return 0xFFFFFFFF;
  473. }
  474. return le32_to_cpup((__le32 *)func->tmpbuf);
  475. }
  476. EXPORT_SYMBOL_GPL(sdio_readl);
  477. /**
  478. * sdio_writel - write a 32 bit integer to a SDIO function
  479. * @func: SDIO function to access
  480. * @b: integer to write
  481. * @addr: address to write to
  482. * @err_ret: optional status value from transfer
  483. *
  484. * Writes a 32 bit integer to the address space of a given SDIO
  485. * function. @err_ret will contain the status of the actual
  486. * transfer.
  487. */
  488. void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
  489. {
  490. int ret;
  491. *(__le32 *)func->tmpbuf = cpu_to_le32(b);
  492. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
  493. if (err_ret)
  494. *err_ret = ret;
  495. }
  496. EXPORT_SYMBOL_GPL(sdio_writel);
  497. /**
  498. * sdio_f0_readb - read a single byte from SDIO function 0
  499. * @func: an SDIO function of the card
  500. * @addr: address to read
  501. * @err_ret: optional status value from transfer
  502. *
  503. * Reads a single byte from the address space of SDIO function 0.
  504. * If there is a problem reading the address, 0xff is returned
  505. * and @err_ret will contain the error code.
  506. */
  507. unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
  508. int *err_ret)
  509. {
  510. int ret;
  511. unsigned char val;
  512. BUG_ON(!func);
  513. if (err_ret)
  514. *err_ret = 0;
  515. ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
  516. if (ret) {
  517. if (err_ret)
  518. *err_ret = ret;
  519. return 0xFF;
  520. }
  521. return val;
  522. }
  523. EXPORT_SYMBOL_GPL(sdio_f0_readb);
  524. /**
  525. * sdio_f0_writeb - write a single byte to SDIO function 0
  526. * @func: an SDIO function of the card
  527. * @b: byte to write
  528. * @addr: address to write to
  529. * @err_ret: optional status value from transfer
  530. *
  531. * Writes a single byte to the address space of SDIO function 0.
  532. * @err_ret will contain the status of the actual transfer.
  533. *
  534. * Only writes to the vendor specific CCCR registers (0xF0 -
  535. * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
  536. * writes outside this range.
  537. */
  538. void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
  539. int *err_ret)
  540. {
  541. int ret;
  542. BUG_ON(!func);
  543. if (addr < 0xF0 || addr > 0xFF) {
  544. if (err_ret)
  545. *err_ret = -EINVAL;
  546. return;
  547. }
  548. ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
  549. if (err_ret)
  550. *err_ret = ret;
  551. }
  552. EXPORT_SYMBOL_GPL(sdio_f0_writeb);