sdio_io.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. return min(min(min(
  160. func->card->host->max_seg_size,
  161. func->card->host->max_blk_size),
  162. func->max_blksize),
  163. 512u); /* maximum size for byte mode */
  164. }
  165. /**
  166. * sdio_align_size - pads a transfer size to a more optimal value
  167. * @func: SDIO function
  168. * @sz: original transfer size
  169. *
  170. * Pads the original data size with a number of extra bytes in
  171. * order to avoid controller bugs and/or performance hits
  172. * (e.g. some controllers revert to PIO for certain sizes).
  173. *
  174. * If possible, it will also adjust the size so that it can be
  175. * handled in just a single request.
  176. *
  177. * Returns the improved size, which might be unmodified.
  178. */
  179. unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
  180. {
  181. unsigned int orig_sz;
  182. unsigned int blk_sz, byte_sz;
  183. unsigned chunk_sz;
  184. orig_sz = sz;
  185. /*
  186. * Do a first check with the controller, in case it
  187. * wants to increase the size up to a point where it
  188. * might need more than one block.
  189. */
  190. sz = mmc_align_data_size(func->card, sz);
  191. /*
  192. * If we can still do this with just a byte transfer, then
  193. * we're done.
  194. */
  195. if (sz <= sdio_max_byte_size(func))
  196. return sz;
  197. if (func->card->cccr.multi_block) {
  198. /*
  199. * Check if the transfer is already block aligned
  200. */
  201. if ((sz % func->cur_blksize) == 0)
  202. return sz;
  203. /*
  204. * Realign it so that it can be done with one request,
  205. * and recheck if the controller still likes it.
  206. */
  207. blk_sz = ((sz + func->cur_blksize - 1) /
  208. func->cur_blksize) * func->cur_blksize;
  209. blk_sz = mmc_align_data_size(func->card, blk_sz);
  210. /*
  211. * This value is only good if it is still just
  212. * one request.
  213. */
  214. if ((blk_sz % func->cur_blksize) == 0)
  215. return blk_sz;
  216. /*
  217. * We failed to do one request, but at least try to
  218. * pad the remainder properly.
  219. */
  220. byte_sz = mmc_align_data_size(func->card,
  221. sz % func->cur_blksize);
  222. if (byte_sz <= sdio_max_byte_size(func)) {
  223. blk_sz = sz / func->cur_blksize;
  224. return blk_sz * func->cur_blksize + byte_sz;
  225. }
  226. } else {
  227. /*
  228. * We need multiple requests, so first check that the
  229. * controller can handle the chunk size;
  230. */
  231. chunk_sz = mmc_align_data_size(func->card,
  232. sdio_max_byte_size(func));
  233. if (chunk_sz == sdio_max_byte_size(func)) {
  234. /*
  235. * Fix up the size of the remainder (if any)
  236. */
  237. byte_sz = orig_sz % chunk_sz;
  238. if (byte_sz) {
  239. byte_sz = mmc_align_data_size(func->card,
  240. byte_sz);
  241. }
  242. return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
  243. }
  244. }
  245. /*
  246. * The controller is simply incapable of transferring the size
  247. * we want in decent manner, so just return the original size.
  248. */
  249. return orig_sz;
  250. }
  251. EXPORT_SYMBOL_GPL(sdio_align_size);
  252. /* Split an arbitrarily sized data transfer into several
  253. * IO_RW_EXTENDED commands. */
  254. static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
  255. unsigned addr, int incr_addr, u8 *buf, unsigned size)
  256. {
  257. unsigned remainder = size;
  258. unsigned max_blocks;
  259. int ret;
  260. /* Do the bulk of the transfer using block mode (if supported). */
  261. if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
  262. /* Blocks per command is limited by host count, host transfer
  263. * size (we only use a single sg entry) and the maximum for
  264. * IO_RW_EXTENDED of 511 blocks. */
  265. max_blocks = min(func->card->host->max_blk_count,
  266. func->card->host->max_seg_size / func->cur_blksize);
  267. max_blocks = min(max_blocks, 511u);
  268. while (remainder > func->cur_blksize) {
  269. unsigned blocks;
  270. blocks = remainder / func->cur_blksize;
  271. if (blocks > max_blocks)
  272. blocks = max_blocks;
  273. size = blocks * func->cur_blksize;
  274. ret = mmc_io_rw_extended(func->card, write,
  275. func->num, addr, incr_addr, buf,
  276. blocks, func->cur_blksize);
  277. if (ret)
  278. return ret;
  279. remainder -= size;
  280. buf += size;
  281. if (incr_addr)
  282. addr += size;
  283. }
  284. }
  285. /* Write the remainder using byte mode. */
  286. while (remainder > 0) {
  287. size = min(remainder, sdio_max_byte_size(func));
  288. ret = mmc_io_rw_extended(func->card, write, func->num, addr,
  289. incr_addr, buf, 1, size);
  290. if (ret)
  291. return ret;
  292. remainder -= size;
  293. buf += size;
  294. if (incr_addr)
  295. addr += size;
  296. }
  297. return 0;
  298. }
  299. /**
  300. * sdio_readb - read a single byte from a SDIO function
  301. * @func: SDIO function to access
  302. * @addr: address to read
  303. * @err_ret: optional status value from transfer
  304. *
  305. * Reads a single byte from the address space of a given SDIO
  306. * function. If there is a problem reading the address, 0xff
  307. * is returned and @err_ret will contain the error code.
  308. */
  309. u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
  310. {
  311. int ret;
  312. u8 val;
  313. BUG_ON(!func);
  314. if (err_ret)
  315. *err_ret = 0;
  316. ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
  317. if (ret) {
  318. if (err_ret)
  319. *err_ret = ret;
  320. return 0xFF;
  321. }
  322. return val;
  323. }
  324. EXPORT_SYMBOL_GPL(sdio_readb);
  325. /**
  326. * sdio_writeb - write a single byte to a SDIO function
  327. * @func: SDIO function to access
  328. * @b: byte to write
  329. * @addr: address to write to
  330. * @err_ret: optional status value from transfer
  331. *
  332. * Writes a single byte to the address space of a given SDIO
  333. * function. @err_ret will contain the status of the actual
  334. * transfer.
  335. */
  336. void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
  337. {
  338. int ret;
  339. BUG_ON(!func);
  340. ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
  341. if (err_ret)
  342. *err_ret = ret;
  343. }
  344. EXPORT_SYMBOL_GPL(sdio_writeb);
  345. /**
  346. * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
  347. * @func: SDIO function to access
  348. * @dst: buffer to store the data
  349. * @addr: address to begin reading from
  350. * @count: number of bytes to read
  351. *
  352. * Reads from the address space of a given SDIO function. Return
  353. * value indicates if the transfer succeeded or not.
  354. */
  355. int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
  356. unsigned int addr, int count)
  357. {
  358. return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
  359. }
  360. EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
  361. /**
  362. * sdio_memcpy_toio - write a chunk of memory to a SDIO function
  363. * @func: SDIO function to access
  364. * @addr: address to start writing to
  365. * @src: buffer that contains the data to write
  366. * @count: number of bytes to write
  367. *
  368. * Writes to the address space of a given SDIO function. Return
  369. * value indicates if the transfer succeeded or not.
  370. */
  371. int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
  372. void *src, int count)
  373. {
  374. return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
  375. }
  376. EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
  377. /**
  378. * sdio_readsb - read from a FIFO on a SDIO function
  379. * @func: SDIO function to access
  380. * @dst: buffer to store the data
  381. * @addr: address of (single byte) FIFO
  382. * @count: number of bytes to read
  383. *
  384. * Reads from the specified FIFO of a given SDIO function. Return
  385. * value indicates if the transfer succeeded or not.
  386. */
  387. int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
  388. int count)
  389. {
  390. return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
  391. }
  392. EXPORT_SYMBOL_GPL(sdio_readsb);
  393. /**
  394. * sdio_writesb - write to a FIFO of a SDIO function
  395. * @func: SDIO function to access
  396. * @addr: address of (single byte) FIFO
  397. * @src: buffer that contains the data to write
  398. * @count: number of bytes to write
  399. *
  400. * Writes to the specified FIFO of a given SDIO function. Return
  401. * value indicates if the transfer succeeded or not.
  402. */
  403. int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
  404. int count)
  405. {
  406. return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
  407. }
  408. EXPORT_SYMBOL_GPL(sdio_writesb);
  409. /**
  410. * sdio_readw - read a 16 bit integer from a SDIO function
  411. * @func: SDIO function to access
  412. * @addr: address to read
  413. * @err_ret: optional status value from transfer
  414. *
  415. * Reads a 16 bit integer from the address space of a given SDIO
  416. * function. If there is a problem reading the address, 0xffff
  417. * is returned and @err_ret will contain the error code.
  418. */
  419. u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
  420. {
  421. int ret;
  422. if (err_ret)
  423. *err_ret = 0;
  424. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
  425. if (ret) {
  426. if (err_ret)
  427. *err_ret = ret;
  428. return 0xFFFF;
  429. }
  430. return le16_to_cpup((__le16 *)func->tmpbuf);
  431. }
  432. EXPORT_SYMBOL_GPL(sdio_readw);
  433. /**
  434. * sdio_writew - write a 16 bit integer to a SDIO function
  435. * @func: SDIO function to access
  436. * @b: integer to write
  437. * @addr: address to write to
  438. * @err_ret: optional status value from transfer
  439. *
  440. * Writes a 16 bit integer to the address space of a given SDIO
  441. * function. @err_ret will contain the status of the actual
  442. * transfer.
  443. */
  444. void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
  445. {
  446. int ret;
  447. *(__le16 *)func->tmpbuf = cpu_to_le16(b);
  448. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
  449. if (err_ret)
  450. *err_ret = ret;
  451. }
  452. EXPORT_SYMBOL_GPL(sdio_writew);
  453. /**
  454. * sdio_readl - read a 32 bit integer from a SDIO function
  455. * @func: SDIO function to access
  456. * @addr: address to read
  457. * @err_ret: optional status value from transfer
  458. *
  459. * Reads a 32 bit integer from the address space of a given SDIO
  460. * function. If there is a problem reading the address,
  461. * 0xffffffff is returned and @err_ret will contain the error
  462. * code.
  463. */
  464. u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
  465. {
  466. int ret;
  467. if (err_ret)
  468. *err_ret = 0;
  469. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
  470. if (ret) {
  471. if (err_ret)
  472. *err_ret = ret;
  473. return 0xFFFFFFFF;
  474. }
  475. return le32_to_cpup((__le32 *)func->tmpbuf);
  476. }
  477. EXPORT_SYMBOL_GPL(sdio_readl);
  478. /**
  479. * sdio_writel - write a 32 bit integer to a SDIO function
  480. * @func: SDIO function to access
  481. * @b: integer to write
  482. * @addr: address to write to
  483. * @err_ret: optional status value from transfer
  484. *
  485. * Writes a 32 bit integer to the address space of a given SDIO
  486. * function. @err_ret will contain the status of the actual
  487. * transfer.
  488. */
  489. void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
  490. {
  491. int ret;
  492. *(__le32 *)func->tmpbuf = cpu_to_le32(b);
  493. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
  494. if (err_ret)
  495. *err_ret = ret;
  496. }
  497. EXPORT_SYMBOL_GPL(sdio_writel);
  498. /**
  499. * sdio_f0_readb - read a single byte from SDIO function 0
  500. * @func: an SDIO function of the card
  501. * @addr: address to read
  502. * @err_ret: optional status value from transfer
  503. *
  504. * Reads a single byte from the address space of SDIO function 0.
  505. * If there is a problem reading the address, 0xff is returned
  506. * and @err_ret will contain the error code.
  507. */
  508. unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
  509. int *err_ret)
  510. {
  511. int ret;
  512. unsigned char val;
  513. BUG_ON(!func);
  514. if (err_ret)
  515. *err_ret = 0;
  516. ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
  517. if (ret) {
  518. if (err_ret)
  519. *err_ret = ret;
  520. return 0xFF;
  521. }
  522. return val;
  523. }
  524. EXPORT_SYMBOL_GPL(sdio_f0_readb);
  525. /**
  526. * sdio_f0_writeb - write a single byte to SDIO function 0
  527. * @func: an SDIO function of the card
  528. * @b: byte to write
  529. * @addr: address to write to
  530. * @err_ret: optional status value from transfer
  531. *
  532. * Writes a single byte to the address space of SDIO function 0.
  533. * @err_ret will contain the status of the actual transfer.
  534. *
  535. * Only writes to the vendor specific CCCR registers (0xF0 -
  536. * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
  537. * writes outside this range.
  538. */
  539. void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
  540. int *err_ret)
  541. {
  542. int ret;
  543. BUG_ON(!func);
  544. if (addr < 0xF0 || addr > 0xFF) {
  545. if (err_ret)
  546. *err_ret = -EINVAL;
  547. return;
  548. }
  549. ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
  550. if (err_ret)
  551. *err_ret = ret;
  552. }
  553. EXPORT_SYMBOL_GPL(sdio_f0_writeb);