sdio_io.c 16 KB

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