sdio_io.c 16 KB

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