sdio_io.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * linux/drivers/amlogic/cardreader/sdio_io.c
  3. *
  4. * Copyright 20108 Amlogic
  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/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/kthread.h>
  14. #include <linux/wait.h>
  15. #include <linux/delay.h>
  16. #include <linux/cardreader/sdio.h>
  17. #include <linux/cardreader/card_block.h>
  18. #include "sdio_ops.h"
  19. static int process_sdio_pending_irqs(struct memory_card *card)
  20. {
  21. int i, ret, count;
  22. unsigned char pending;
  23. ret = sdio_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
  24. if (ret) {
  25. printk(KERN_DEBUG "%s: error %d reading SDIO_CCCR_INTx\n",
  26. card_card_id(card), ret);
  27. return ret;
  28. }
  29. count = 0;
  30. for (i = 1; i <= 7; i++) {
  31. if (pending & (1 << i)) {
  32. struct sdio_func *func = card->sdio_func[i - 1];
  33. if (!func) {
  34. printk(KERN_WARNING "%s: pending IRQ for "
  35. "non-existant function,pending = %x\n",
  36. card_card_id(card), pending);
  37. ret = -EINVAL;
  38. } else if (func->irq_handler) {
  39. //printk("sdio irq received and handled at func %d\n", i);
  40. func->irq_handler(func);
  41. count++;
  42. } else {
  43. printk(KERN_WARNING "%s: pending IRQ with no handler fun_num: %d\n", sdio_func_id(func), i);
  44. ret = -EINVAL;
  45. }
  46. }
  47. }
  48. sdio_irq_handled = 1;
  49. if (count)
  50. return count;
  51. return ret;
  52. }
  53. static int sdio_irq_thread(void *_host)
  54. {
  55. struct card_host *host = _host;
  56. struct sched_param param = { .sched_priority = 1 };
  57. unsigned long period, idle_period;
  58. int ret;
  59. //set_cpus_allowed_ptr(current, cpumask_of(0));
  60. sched_setscheduler(current, SCHED_FIFO, &param);
  61. /*
  62. * We want to allow for SDIO cards to work even on non SDIO
  63. * aware hosts. One thing that non SDIO host cannot do is
  64. * asynchronous notification of pending SDIO card interrupts
  65. * hence we poll for them in that case.
  66. */
  67. idle_period = msecs_to_jiffies(10);
  68. period = (host->caps & CARD_CAP_SDIO_IRQ) ?
  69. MAX_SCHEDULE_TIMEOUT : idle_period;
  70. pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
  71. card_hostname(host), period);
  72. do {
  73. /*
  74. * We claim the host here on drivers behalf for a couple
  75. * reasons:
  76. *
  77. * 1) it is already needed to retrieve the CCCR_INTx;
  78. * 2) we want the driver(s) to clear the IRQ condition ASAP;
  79. * 3) we need to control the abort condition locally.
  80. *
  81. * Just like traditional hard IRQ handlers, we expect SDIO
  82. * IRQ handlers to be quick and to the point, so that the
  83. * holding of the host lock does not cover too much work
  84. * that doesn't require that lock to be held.
  85. */
  86. //ret = __card_claim_host(host, &host->sdio_irq_thread_abort);
  87. card_claim_host(host);
  88. //if (ret)
  89. //break;
  90. ret = process_sdio_pending_irqs(host->card);
  91. card_release_host(host);
  92. /*
  93. * Give other threads a chance to run in the presence of
  94. * errors.
  95. */
  96. if (ret < 0) {
  97. set_current_state(TASK_INTERRUPTIBLE);
  98. if (!kthread_should_stop())
  99. schedule_timeout(HZ);
  100. set_current_state(TASK_RUNNING);
  101. }
  102. /*
  103. * Adaptive polling frequency based on the assumption
  104. * that an interrupt will be closely followed by more.
  105. * This has a substantial benefit for network devices.
  106. */
  107. if (!(host->caps & CARD_CAP_SDIO_IRQ)) {
  108. if (ret > 0)
  109. period /= 2;
  110. else {
  111. period++;
  112. if (period > idle_period)
  113. period = idle_period;
  114. }
  115. }
  116. set_current_state(TASK_INTERRUPTIBLE);
  117. if (host->caps & CARD_CAP_SDIO_IRQ) {
  118. host->ops->enable_sdio_irq(host, 1);
  119. schedule();
  120. }
  121. else if (!kthread_should_stop())
  122. msleep(10);
  123. //schedule_timeout(period);
  124. set_current_state(TASK_INTERRUPTIBLE);
  125. if(host->sdio_task_state)
  126. {
  127. schedule();
  128. }
  129. set_current_state(TASK_RUNNING);
  130. } while (!kthread_should_stop());
  131. if (host->caps & CARD_CAP_SDIO_IRQ)
  132. host->ops->enable_sdio_irq(host, 0);
  133. pr_debug("%s: IRQ thread exiting with code %d\n",
  134. card_hostname(host), ret);
  135. return ret;
  136. }
  137. static int sdio_card_irq_get(struct memory_card *card)
  138. {
  139. struct card_host *host = card->host;
  140. WARN_ON(!host->claimed);
  141. if (!host->sdio_irqs++) {
  142. atomic_set(&host->sdio_irq_thread_abort, 0);
  143. host->sdio_irq_thread =
  144. kthread_run(sdio_irq_thread, host, "ksdioirqd/%s",
  145. card_hostname(host));
  146. if (IS_ERR(host->sdio_irq_thread)) {
  147. int err = PTR_ERR(host->sdio_irq_thread);
  148. host->sdio_irqs--;
  149. return err;
  150. }
  151. }
  152. return 0;
  153. }
  154. static int sdio_card_irq_put(struct memory_card *card)
  155. {
  156. struct card_host *host = card->host;
  157. WARN_ON(!host->claimed);
  158. BUG_ON(host->sdio_irqs < 1);
  159. if (!--host->sdio_irqs) {
  160. atomic_set(&host->sdio_irq_thread_abort, 1);
  161. kthread_stop(host->sdio_irq_thread);
  162. }
  163. return 0;
  164. }
  165. /**
  166. * sdio_claim_irq - claim the IRQ for a SDIO function
  167. * @func: SDIO function
  168. * @handler: IRQ handler callback
  169. *
  170. * Claim and activate the IRQ for the given SDIO function. The provided
  171. * handler will be called when that IRQ is asserted. The host is always
  172. * claimed already when the handler is called so the handler must not
  173. * call sdio_claim_host() nor sdio_release_host().
  174. */
  175. int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
  176. {
  177. int ret;
  178. unsigned char reg;
  179. BUG_ON(!func);
  180. BUG_ON(!func->card);
  181. pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
  182. if (func->irq_handler) {
  183. pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
  184. return -EBUSY;
  185. }
  186. ret = sdio_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
  187. if (ret)
  188. return ret;
  189. reg |= 1 << func->num;
  190. reg |= 1; /* Master interrupt enable */
  191. ret = sdio_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
  192. if (ret)
  193. return ret;
  194. func->irq_handler = handler;
  195. ret = sdio_card_irq_get(func->card);
  196. if (ret)
  197. func->irq_handler = NULL;
  198. return ret;
  199. }
  200. EXPORT_SYMBOL_GPL(sdio_claim_irq);
  201. /**
  202. * sdio_release_irq - release the IRQ for a SDIO function
  203. * @func: SDIO function
  204. *
  205. * Disable and release the IRQ for the given SDIO function.
  206. */
  207. int sdio_release_irq(struct sdio_func *func)
  208. {
  209. int ret;
  210. unsigned char reg;
  211. BUG_ON(!func);
  212. BUG_ON(!func->card);
  213. pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
  214. if (func->irq_handler) {
  215. func->irq_handler = NULL;
  216. sdio_card_irq_put(func->card);
  217. }
  218. ret = sdio_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
  219. if (ret)
  220. return ret;
  221. reg &= ~(1 << func->num);
  222. /* Disable master interrupt with the last function interrupt */
  223. if (!(reg & 0xFE))
  224. reg = 0;
  225. ret = sdio_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
  226. if (ret)
  227. return ret;
  228. return 0;
  229. }
  230. EXPORT_SYMBOL_GPL(sdio_release_irq);
  231. /**
  232. * sdio_claim_host - exclusively claim a bus for a certain SDIO function
  233. * @func: SDIO function that will be accessed
  234. *
  235. * Claim a bus for a set of operations. The SDIO function given
  236. * is used to figure out which bus is relevant.
  237. */
  238. void sdio_claim_host(struct sdio_func *func)
  239. {
  240. BUG_ON(!func);
  241. BUG_ON(!func->card);
  242. __card_claim_host(func->card->host, func->card);
  243. }
  244. EXPORT_SYMBOL_GPL(sdio_claim_host);
  245. /**
  246. * sdio_release_host - release a bus for a certain SDIO function
  247. * @func: SDIO function that was accessed
  248. *
  249. * Release a bus, allowing others to claim the bus for their
  250. * operations.
  251. */
  252. void sdio_release_host(struct sdio_func *func)
  253. {
  254. BUG_ON(!func);
  255. BUG_ON(!func->card);
  256. card_release_host(func->card->host);
  257. }
  258. EXPORT_SYMBOL_GPL(sdio_release_host);
  259. /**
  260. * sdio_enable_func - enables a SDIO function for usage
  261. * @func: SDIO function to enable
  262. *
  263. * Powers up and activates a SDIO function so that register
  264. * access is possible.
  265. */
  266. int sdio_enable_func(struct sdio_func *func)
  267. {
  268. int ret;
  269. unsigned char reg;
  270. unsigned long timeout;
  271. BUG_ON(!func);
  272. BUG_ON(!func->card);
  273. pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));
  274. ret = sdio_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
  275. if (ret)
  276. goto err;
  277. reg |= 1 << func->num;
  278. ret = sdio_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
  279. if (ret)
  280. goto err;
  281. timeout = jiffies + msecs_to_jiffies(func->enable_timeout);
  282. while (1) {
  283. ret = sdio_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, &reg);
  284. if (ret)
  285. goto err;
  286. if (reg & (1 << func->num))
  287. break;
  288. ret = -ETIME;
  289. if (time_after(jiffies, timeout))
  290. goto err;
  291. }
  292. pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));
  293. return 0;
  294. err:
  295. pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
  296. return ret;
  297. }
  298. EXPORT_SYMBOL_GPL(sdio_enable_func);
  299. /**
  300. * sdio_disable_func - disable a SDIO function
  301. * @func: SDIO function to disable
  302. *
  303. * Powers down and deactivates a SDIO function. Register access
  304. * to this function will fail until the function is reenabled.
  305. */
  306. int sdio_disable_func(struct sdio_func *func)
  307. {
  308. int ret;
  309. unsigned char reg;
  310. BUG_ON(!func);
  311. BUG_ON(!func->card);
  312. pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func));
  313. ret = sdio_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
  314. if (ret)
  315. goto err;
  316. reg &= ~(1 << func->num);
  317. ret = sdio_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
  318. if (ret)
  319. goto err;
  320. pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));
  321. return 0;
  322. err:
  323. pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
  324. return -EIO;
  325. }
  326. EXPORT_SYMBOL_GPL(sdio_disable_func);
  327. /**
  328. * sdio_set_block_size - set the block size of an SDIO function
  329. * @func: SDIO function to change
  330. * @blksz: new block size or 0 to use the default.
  331. *
  332. * The default block size is the largest supported by both the function
  333. * and the host, with a maximum of 512 to ensure that arbitrarily sized
  334. * data transfer use the optimal (least) number of commands.
  335. *
  336. * A driver may call this to override the default block size set by the
  337. * core. This can be used to set a block size greater than the maximum
  338. * that reported by the card; it is the driver's responsibility to ensure
  339. * it uses a value that the card supports.
  340. *
  341. * Returns 0 on success, -EINVAL if the host does not support the
  342. * requested block size, or -EIO (etc.) if one of the resultant FBR block
  343. * size register writes failed.
  344. *
  345. */
  346. int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
  347. {
  348. int ret;
  349. if (blksz > func->card->host->max_blk_size)
  350. return -EINVAL;
  351. if (blksz == 0) {
  352. blksz = min(func->max_blksize, func->card->host->max_blk_size);
  353. blksz = min(blksz, 512u);
  354. }
  355. ret = sdio_io_rw_direct(func->card, 1, 0,
  356. SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
  357. blksz & 0xff, NULL);
  358. if (ret)
  359. return ret;
  360. ret = sdio_io_rw_direct(func->card, 1, 0,
  361. SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,
  362. (blksz >> 8) & 0xff, NULL);
  363. if (ret)
  364. return ret;
  365. func->cur_blksize = blksz;
  366. return 0;
  367. }
  368. EXPORT_SYMBOL_GPL(sdio_set_block_size);
  369. /*
  370. * Calculate the maximum byte mode transfer size
  371. */
  372. static inline unsigned int sdio_max_byte_size(struct sdio_func *func)
  373. {
  374. unsigned mval = min(func->card->host->max_seg_size,
  375. func->card->host->max_blk_size);
  376. /*if (mmc_blksz_for_byte_mode(func->card))
  377. mval = min(mval, func->cur_blksize);
  378. else
  379. mval = min(mval, func->max_blksize);*/
  380. return min(mval, 512u); /* maximum size for byte mode */
  381. }
  382. /**
  383. * sdio_align_size - pads a transfer size to a more optimal value
  384. * @func: SDIO function
  385. * @sz: original transfer size
  386. *
  387. * Pads the original data size with a number of extra bytes in
  388. * order to avoid controller bugs and/or performance hits
  389. * (e.g. some controllers revert to PIO for certain sizes).
  390. *
  391. * If possible, it will also adjust the size so that it can be
  392. * handled in just a single request.
  393. *
  394. * Returns the improved size, which might be unmodified.
  395. */
  396. unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
  397. {
  398. unsigned int orig_sz;
  399. unsigned int blk_sz, byte_sz;
  400. unsigned chunk_sz;
  401. orig_sz = sz;
  402. /*
  403. * Do a first check with the controller, in case it
  404. * wants to increase the size up to a point where it
  405. * might need more than one block.
  406. */
  407. sz = card_align_data_size(func->card, sz);
  408. /*
  409. * If we can still do this with just a byte transfer, then
  410. * we're done.
  411. */
  412. if (sz <= sdio_max_byte_size(func))
  413. return sz;
  414. if (1) {
  415. /*
  416. * Check if the transfer is already block aligned
  417. */
  418. if ((sz % func->cur_blksize) == 0)
  419. return sz;
  420. /*
  421. * Realign it so that it can be done with one request,
  422. * and recheck if the controller still likes it.
  423. */
  424. blk_sz = ((sz + func->cur_blksize - 1) /
  425. func->cur_blksize) * func->cur_blksize;
  426. blk_sz = card_align_data_size(func->card, blk_sz);
  427. /*
  428. * This value is only good if it is still just
  429. * one request.
  430. */
  431. if ((blk_sz % func->cur_blksize) == 0)
  432. return blk_sz;
  433. /*
  434. * We failed to do one request, but at least try to
  435. * pad the remainder properly.
  436. */
  437. byte_sz = card_align_data_size(func->card,
  438. sz % func->cur_blksize);
  439. if (byte_sz <= sdio_max_byte_size(func)) {
  440. blk_sz = sz / func->cur_blksize;
  441. return blk_sz * func->cur_blksize + byte_sz;
  442. }
  443. } else {
  444. /*
  445. * We need multiple requests, so first check that the
  446. * controller can handle the chunk size;
  447. */
  448. chunk_sz = card_align_data_size(func->card,
  449. sdio_max_byte_size(func));
  450. if (chunk_sz == sdio_max_byte_size(func)) {
  451. /*
  452. * Fix up the size of the remainder (if any)
  453. */
  454. byte_sz = orig_sz % chunk_sz;
  455. if (byte_sz) {
  456. byte_sz = card_align_data_size(func->card,
  457. byte_sz);
  458. }
  459. return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
  460. }
  461. }
  462. /*
  463. * The controller is simply incapable of transferring the size
  464. * we want in decent manner, so just return the original size.
  465. */
  466. return orig_sz;
  467. }
  468. EXPORT_SYMBOL_GPL(sdio_align_size);
  469. /* Split an arbitrarily sized data transfer into several
  470. * IO_RW_EXTENDED commands. */
  471. static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
  472. unsigned addr, int incr_addr, u8 *buf, unsigned size)
  473. {
  474. unsigned remainder = size;
  475. unsigned max_blocks;
  476. int ret;
  477. //printk("sdio rw ext addr %x at fun %d cnt: %d read_or_write %d\n",
  478. //addr, func->num, size, write);
  479. /* Do the bulk of the transfer using block mode (if supported). */
  480. if ((size > sdio_max_byte_size(func))) {
  481. /* Blocks per command is limited by host count, host transfer
  482. * size (we only use a single sg entry) and the maximum for
  483. * IO_RW_EXTENDED of 511 blocks. */
  484. max_blocks = min(func->card->host->max_blk_count,
  485. func->card->host->max_seg_size / func->cur_blksize);
  486. max_blocks = min(max_blocks, 511u);
  487. while (remainder > func->cur_blksize) {
  488. unsigned blocks;
  489. blocks = remainder / func->cur_blksize;
  490. if (blocks > max_blocks)
  491. blocks = max_blocks;
  492. size = blocks * func->cur_blksize;
  493. ret = sdio_io_rw_extended(func->card, write,
  494. func->num, addr, incr_addr, buf,
  495. blocks, func->cur_blksize);
  496. if (ret)
  497. return ret;
  498. remainder -= size;
  499. buf += size;
  500. if (incr_addr)
  501. addr += size;
  502. }
  503. }
  504. /* Write the remainder using byte mode. */
  505. while (remainder > 0) {
  506. size = min(remainder, sdio_max_byte_size(func));
  507. ret = sdio_io_rw_extended(func->card, write, func->num, addr,
  508. incr_addr, buf, 1, size);
  509. if (ret)
  510. return ret;
  511. remainder -= size;
  512. buf += size;
  513. if (incr_addr)
  514. addr += size;
  515. }
  516. return 0;
  517. }
  518. /**
  519. * sdio_readb - read a single byte from a SDIO function
  520. * @func: SDIO function to access
  521. * @addr: address to read
  522. * @err_ret: optional status value from transfer
  523. *
  524. * Reads a single byte from the address space of a given SDIO
  525. * function. If there is a problem reading the address, 0xff
  526. * is returned and @err_ret will contain the error code.
  527. */
  528. u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
  529. {
  530. int ret;
  531. u8 val;
  532. BUG_ON(!func);
  533. if (err_ret)
  534. *err_ret = 0;
  535. ret = sdio_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
  536. if (ret) {
  537. if (err_ret)
  538. *err_ret = ret;
  539. return 0xFF;
  540. }
  541. return val;
  542. }
  543. EXPORT_SYMBOL_GPL(sdio_readb);
  544. /**
  545. * sdio_writeb - write a single byte to a SDIO function
  546. * @func: SDIO function to access
  547. * @b: byte to write
  548. * @addr: address to write to
  549. * @err_ret: optional status value from transfer
  550. *
  551. * Writes a single byte to the address space of a given SDIO
  552. * function. @err_ret will contain the status of the actual
  553. * transfer.
  554. */
  555. void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
  556. {
  557. int ret;
  558. BUG_ON(!func);
  559. ret = sdio_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
  560. if (err_ret)
  561. *err_ret = ret;
  562. }
  563. EXPORT_SYMBOL_GPL(sdio_writeb);
  564. /**
  565. * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
  566. * @func: SDIO function to access
  567. * @dst: buffer to store the data
  568. * @addr: address to begin reading from
  569. * @count: number of bytes to read
  570. *
  571. * Reads from the address space of a given SDIO function. Return
  572. * value indicates if the transfer succeeded or not.
  573. */
  574. int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
  575. unsigned int addr, int count)
  576. {
  577. return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
  578. }
  579. EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
  580. /**
  581. * sdio_memcpy_toio - write a chunk of memory to a SDIO function
  582. * @func: SDIO function to access
  583. * @addr: address to start writing to
  584. * @src: buffer that contains the data to write
  585. * @count: number of bytes to write
  586. *
  587. * Writes to the address space of a given SDIO function. Return
  588. * value indicates if the transfer succeeded or not.
  589. */
  590. int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
  591. void *src, int count)
  592. {
  593. return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
  594. }
  595. EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
  596. /**
  597. * sdio_readsb - read from a FIFO on a SDIO function
  598. * @func: SDIO function to access
  599. * @dst: buffer to store the data
  600. * @addr: address of (single byte) FIFO
  601. * @count: number of bytes to read
  602. *
  603. * Reads from the specified FIFO of a given SDIO function. Return
  604. * value indicates if the transfer succeeded or not.
  605. */
  606. int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
  607. int count)
  608. {
  609. return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
  610. }
  611. EXPORT_SYMBOL_GPL(sdio_readsb);
  612. /**
  613. * sdio_writesb - write to a FIFO of a SDIO function
  614. * @func: SDIO function to access
  615. * @addr: address of (single byte) FIFO
  616. * @src: buffer that contains the data to write
  617. * @count: number of bytes to write
  618. *
  619. * Writes to the specified FIFO of a given SDIO function. Return
  620. * value indicates if the transfer succeeded or not.
  621. */
  622. int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
  623. int count)
  624. {
  625. return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
  626. }
  627. EXPORT_SYMBOL_GPL(sdio_writesb);
  628. /**
  629. * sdio_readw - read a 16 bit integer from a SDIO function
  630. * @func: SDIO function to access
  631. * @addr: address to read
  632. * @err_ret: optional status value from transfer
  633. *
  634. * Reads a 16 bit integer from the address space of a given SDIO
  635. * function. If there is a problem reading the address, 0xffff
  636. * is returned and @err_ret will contain the error code.
  637. */
  638. u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
  639. {
  640. int ret;
  641. if (err_ret)
  642. *err_ret = 0;
  643. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
  644. if (ret) {
  645. if (err_ret)
  646. *err_ret = ret;
  647. return 0xFFFF;
  648. }
  649. return le16_to_cpup((__le16 *)func->tmpbuf);
  650. }
  651. EXPORT_SYMBOL_GPL(sdio_readw);
  652. /**
  653. * sdio_writew - write a 16 bit integer to a SDIO function
  654. * @func: SDIO function to access
  655. * @b: integer to write
  656. * @addr: address to write to
  657. * @err_ret: optional status value from transfer
  658. *
  659. * Writes a 16 bit integer to the address space of a given SDIO
  660. * function. @err_ret will contain the status of the actual
  661. * transfer.
  662. */
  663. void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
  664. {
  665. int ret;
  666. *(__le16 *)func->tmpbuf = cpu_to_le16(b);
  667. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
  668. if (err_ret)
  669. *err_ret = ret;
  670. }
  671. EXPORT_SYMBOL_GPL(sdio_writew);
  672. /**
  673. * sdio_readl - read a 32 bit integer from a SDIO function
  674. * @func: SDIO function to access
  675. * @addr: address to read
  676. * @err_ret: optional status value from transfer
  677. *
  678. * Reads a 32 bit integer from the address space of a given SDIO
  679. * function. If there is a problem reading the address,
  680. * 0xffffffff is returned and @err_ret will contain the error
  681. * code.
  682. */
  683. u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
  684. {
  685. int ret;
  686. if (err_ret)
  687. *err_ret = 0;
  688. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
  689. if (ret) {
  690. if (err_ret)
  691. *err_ret = ret;
  692. return 0xFFFFFFFF;
  693. }
  694. return le32_to_cpup((__le32 *)func->tmpbuf);
  695. }
  696. EXPORT_SYMBOL_GPL(sdio_readl);
  697. /**
  698. * sdio_writel - write a 32 bit integer to a SDIO function
  699. * @func: SDIO function to access
  700. * @b: integer to write
  701. * @addr: address to write to
  702. * @err_ret: optional status value from transfer
  703. *
  704. * Writes a 32 bit integer to the address space of a given SDIO
  705. * function. @err_ret will contain the status of the actual
  706. * transfer.
  707. */
  708. void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
  709. {
  710. int ret;
  711. *(__le32 *)func->tmpbuf = cpu_to_le32(b);
  712. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
  713. if (err_ret)
  714. *err_ret = ret;
  715. }
  716. EXPORT_SYMBOL_GPL(sdio_writel);
  717. /**
  718. * sdio_f0_readb - read a single byte from SDIO function 0
  719. * @func: an SDIO function of the card
  720. * @addr: address to read
  721. * @err_ret: optional status value from transfer
  722. *
  723. * Reads a single byte from the address space of SDIO function 0.
  724. * If there is a problem reading the address, 0xff is returned
  725. * and @err_ret will contain the error code.
  726. */
  727. unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
  728. int *err_ret)
  729. {
  730. int ret;
  731. unsigned char val;
  732. BUG_ON(!func);
  733. if (err_ret)
  734. *err_ret = 0;
  735. ret = sdio_io_rw_direct(func->card, 0, 0, addr, 0, &val);
  736. if (ret) {
  737. if (err_ret)
  738. *err_ret = ret;
  739. return 0xFF;
  740. }
  741. return val;
  742. }
  743. EXPORT_SYMBOL_GPL(sdio_f0_readb);
  744. /**
  745. * sdio_f0_writeb - write a single byte to SDIO function 0
  746. * @func: an SDIO function of the card
  747. * @b: byte to write
  748. * @addr: address to write to
  749. * @err_ret: optional status value from transfer
  750. *
  751. * Writes a single byte to the address space of SDIO function 0.
  752. * @err_ret will contain the status of the actual transfer.
  753. *
  754. * Only writes to the vendor specific CCCR registers (0xF0 -
  755. * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
  756. * writes outside this range.
  757. */
  758. void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
  759. int *err_ret)
  760. {
  761. int ret;
  762. BUG_ON(!func);
  763. if ((addr < 0xF0 || addr > 0xFF)) {
  764. if (err_ret)
  765. *err_ret = -EINVAL;
  766. return;
  767. }
  768. ret = sdio_io_rw_direct(func->card, 1, 0, addr, b, NULL);
  769. if (err_ret)
  770. *err_ret = ret;
  771. }
  772. EXPORT_SYMBOL_GPL(sdio_f0_writeb);
  773. /**
  774. * sdio_get_host_pm_caps - get host power management capabilities
  775. * @func: SDIO function attached to host
  776. *
  777. * Returns a capability bitmask corresponding to power management
  778. * features supported by the host controller that the card function
  779. * might rely upon during a system suspend. The host doesn't need
  780. * to be claimed, nor the function active, for this information to be
  781. * obtained.
  782. */
  783. card_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)
  784. {
  785. BUG_ON(!func);
  786. BUG_ON(!func->card);
  787. return func->card->host->pm_caps;
  788. }
  789. EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);
  790. /**
  791. * sdio_set_host_pm_flags - set wanted host power management capabilities
  792. * @func: SDIO function attached to host
  793. *
  794. * Set a capability bitmask corresponding to wanted host controller
  795. * power management features for the upcoming suspend state.
  796. * This must be called, if needed, each time the suspend method of
  797. * the function driver is called, and must contain only bits that
  798. * were returned by sdio_get_host_pm_caps().
  799. * The host doesn't need to be claimed, nor the function active,
  800. * for this information to be set.
  801. */
  802. int sdio_set_host_pm_flags(struct sdio_func *func, card_pm_flag_t flags)
  803. {
  804. struct card_host *host;
  805. BUG_ON(!func);
  806. BUG_ON(!func->card);
  807. host = func->card->host;
  808. if (flags & ~host->pm_caps)
  809. return -EINVAL;
  810. /* function suspend methods are serialized, hence no lock needed */
  811. host->pm_flags |= flags;
  812. return 0;
  813. }
  814. EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);