sdmmcvar.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* $OpenBSD: sdmmcvar.h,v 1.22 2013/09/12 11:54:04 rapha Exp $ */
  2. /*
  3. * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #ifndef _SDMMCVAR_H_
  18. #define _SDMMCVAR_H_
  19. #include <sys/queue.h>
  20. #include <sys/rwlock.h>
  21. #include <scsi/scsi_all.h>
  22. #include <scsi/scsiconf.h>
  23. #include <dev/sdmmc/sdmmcchip.h>
  24. #include <dev/sdmmc/sdmmcreg.h>
  25. struct sdmmc_csd {
  26. int csdver; /* CSD structure format */
  27. int mmcver; /* MMC version (for CID format) */
  28. int capacity; /* total number of sectors */
  29. int sector_size; /* sector size in bytes */
  30. int read_bl_len; /* block length for reads */
  31. /* ... */
  32. };
  33. struct sdmmc_cid {
  34. int mid; /* manufacturer identification number */
  35. int oid; /* OEM/product identification number */
  36. char pnm[8]; /* product name (MMC v1 has the longest) */
  37. int rev; /* product revision */
  38. int psn; /* product serial number */
  39. int mdt; /* manufacturing date */
  40. };
  41. typedef u_int32_t sdmmc_response[4];
  42. struct sdmmc_softc;
  43. struct sdmmc_task {
  44. void (*func)(void *arg);
  45. void *arg;
  46. int onqueue;
  47. struct sdmmc_softc *sc;
  48. TAILQ_ENTRY(sdmmc_task) next;
  49. };
  50. #define sdmmc_init_task(xtask, xfunc, xarg) do { \
  51. (xtask)->func = (xfunc); \
  52. (xtask)->arg = (xarg); \
  53. (xtask)->onqueue = 0; \
  54. (xtask)->sc = NULL; \
  55. } while (0)
  56. #define sdmmc_task_pending(xtask) ((xtask)->onqueue)
  57. struct sdmmc_command {
  58. struct sdmmc_task c_task; /* task queue entry */
  59. u_int16_t c_opcode; /* SD or MMC command index */
  60. u_int32_t c_arg; /* SD/MMC command argument */
  61. sdmmc_response c_resp; /* response buffer */
  62. void *c_data; /* buffer to send or read into */
  63. int c_datalen; /* length of data buffer */
  64. int c_blklen; /* block length */
  65. int c_flags; /* see below */
  66. #define SCF_ITSDONE 0x0001 /* command is complete */
  67. #define SCF_CMD(flags) ((flags) & 0x00f0)
  68. #define SCF_CMD_AC 0x0000
  69. #define SCF_CMD_ADTC 0x0010
  70. #define SCF_CMD_BC 0x0020
  71. #define SCF_CMD_BCR 0x0030
  72. #define SCF_CMD_READ 0x0040 /* read command (data expected) */
  73. #define SCF_RSP_BSY 0x0100
  74. #define SCF_RSP_136 0x0200
  75. #define SCF_RSP_CRC 0x0400
  76. #define SCF_RSP_IDX 0x0800
  77. #define SCF_RSP_PRESENT 0x1000
  78. /* response types */
  79. #define SCF_RSP_R0 0 /* none */
  80. #define SCF_RSP_R1 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
  81. #define SCF_RSP_R1B (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX|SCF_RSP_BSY)
  82. #define SCF_RSP_R2 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_136)
  83. #define SCF_RSP_R3 (SCF_RSP_PRESENT)
  84. #define SCF_RSP_R4 (SCF_RSP_PRESENT)
  85. #define SCF_RSP_R5 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
  86. #define SCF_RSP_R5B (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX|SCF_RSP_BSY)
  87. #define SCF_RSP_R6 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
  88. #define SCF_RSP_R7 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
  89. int c_error; /* errno value on completion */
  90. /* Host controller owned fields for data xfer in progress */
  91. int c_resid; /* remaining I/O */
  92. u_char *c_buf; /* remaining data */
  93. };
  94. /*
  95. * Decoded PC Card 16 based Card Information Structure (CIS),
  96. * per card (function 0) and per function (1 and greater).
  97. */
  98. struct sdmmc_cis {
  99. u_int16_t manufacturer;
  100. #define SDMMC_VENDOR_INVALID 0xffff
  101. u_int16_t product;
  102. #define SDMMC_PRODUCT_INVALID 0xffff
  103. u_int8_t function;
  104. #define SDMMC_FUNCTION_INVALID 0xff
  105. u_char cis1_major;
  106. u_char cis1_minor;
  107. char cis1_info_buf[256];
  108. char *cis1_info[4];
  109. };
  110. /*
  111. * Structure describing either an SD card I/O function or a SD/MMC
  112. * memory card from a "stack of cards" that responded to CMD2. For a
  113. * combo card with one I/O function and one memory card, there will be
  114. * two of these structures allocated. Each card slot has such a list
  115. * of sdmmc_function structures.
  116. */
  117. struct sdmmc_function {
  118. /* common members */
  119. struct sdmmc_softc *sc; /* card slot softc */
  120. u_int16_t rca; /* relative card address */
  121. int flags;
  122. #define SFF_ERROR 0x0001 /* function is poo; ignore it */
  123. #define SFF_SDHC 0x0002 /* SD High Capacity card */
  124. SIMPLEQ_ENTRY(sdmmc_function) sf_list;
  125. /* SD card I/O function members */
  126. int number; /* I/O function number or -1 */
  127. struct device *child; /* function driver */
  128. struct sdmmc_cis cis; /* decoded CIS */
  129. /* SD/MMC memory card members */
  130. struct sdmmc_csd csd; /* decoded CSD value */
  131. struct sdmmc_cid cid; /* decoded CID value */
  132. sdmmc_response raw_cid; /* temp. storage for decoding */
  133. };
  134. /*
  135. * Structure describing a single SD/MMC/SDIO card slot.
  136. */
  137. struct sdmmc_softc {
  138. struct device sc_dev; /* base device */
  139. #define DEVNAME(sc) ((sc)->sc_dev.dv_xname)
  140. sdmmc_chipset_tag_t sct; /* host controller chipset tag */
  141. sdmmc_chipset_handle_t sch; /* host controller chipset handle */
  142. int sc_flags;
  143. #define SMF_SD_MODE 0x0001 /* host in SD mode (MMC otherwise) */
  144. #define SMF_IO_MODE 0x0002 /* host in I/O mode (SD mode only) */
  145. #define SMF_MEM_MODE 0x0004 /* host in memory mode (SD or MMC) */
  146. #define SMF_CARD_PRESENT 0x0010 /* card presence noticed */
  147. #define SMF_CARD_ATTACHED 0x0020 /* card driver(s) attached */
  148. #define SMF_STOP_AFTER_MULTIPLE 0x0040 /* send a stop after a multiple cmd */
  149. #define SMF_CONFIG_PENDING 0x0080 /* config_pending_incr() called */
  150. uint32_t sc_caps; /* host capability */
  151. #define SMC_CAPS_AUTO_STOP 0x0001 /* send CMD12 automagically by host */
  152. #define SMC_CAPS_4BIT_MODE 0x0002 /* 4-bits data bus width */
  153. #define SMC_CAPS_DMA 0x0004 /* DMA transfer */
  154. #define SMC_CAPS_SPI_MODE 0x0008 /* SPI mode */
  155. #define SMC_CAPS_POLL_CARD_DET 0x0010 /* Polling card detect */
  156. #define SMC_CAPS_SINGLE_ONLY 0x0020 /* only single read/write */
  157. #define SMC_CAPS_8BIT_MODE 0x0040 /* 8-bits data bus width */
  158. #define SMC_CAPS_MULTI_SEG_DMA 0x0080 /* multiple segment DMA transfer */
  159. #define SMC_CAPS_SD_HIGHSPEED 0x0100 /* SD high-speed timing */
  160. #define SMC_CAPS_MMC_HIGHSPEED 0x0200 /* MMC high-speed timing */
  161. int sc_function_count; /* number of I/O functions (SDIO) */
  162. struct sdmmc_function *sc_card; /* selected card */
  163. struct sdmmc_function *sc_fn0; /* function 0, the card itself */
  164. SIMPLEQ_HEAD(, sdmmc_function) sf_head; /* list of card functions */
  165. int sc_dying; /* bus driver is shutting down */
  166. struct proc *sc_task_thread; /* asynchronous tasks */
  167. TAILQ_HEAD(, sdmmc_task) sc_tskq; /* task thread work queue */
  168. struct sdmmc_task sc_discover_task; /* card attach/detach task */
  169. struct sdmmc_task sc_intr_task; /* card interrupt task */
  170. struct rwlock sc_lock; /* lock around host controller */
  171. void *sc_scsibus; /* SCSI bus emulation softc */
  172. TAILQ_HEAD(, sdmmc_intr_handler) sc_intrq; /* interrupt handlers */
  173. long sc_max_xfer; /* maximum transfer size */
  174. };
  175. /*
  176. * Attach devices at the sdmmc bus.
  177. */
  178. struct sdmmc_attach_args {
  179. struct scsi_link *scsi_link; /* XXX */
  180. struct sdmmc_function *sf;
  181. };
  182. #define IPL_SDMMC IPL_BIO
  183. #define splsdmmc() splbio()
  184. #define SDMMC_ASSERT_LOCKED(sc) \
  185. rw_assert_wrlock(&(sc)->sc_lock)
  186. void sdmmc_add_task(struct sdmmc_softc *, struct sdmmc_task *);
  187. void sdmmc_del_task(struct sdmmc_task *);
  188. struct sdmmc_function *sdmmc_function_alloc(struct sdmmc_softc *);
  189. void sdmmc_function_free(struct sdmmc_function *);
  190. int sdmmc_set_bus_power(struct sdmmc_softc *, u_int32_t, u_int32_t);
  191. int sdmmc_mmc_command(struct sdmmc_softc *, struct sdmmc_command *);
  192. int sdmmc_app_command(struct sdmmc_softc *, struct sdmmc_command *);
  193. void sdmmc_go_idle_state(struct sdmmc_softc *);
  194. int sdmmc_select_card(struct sdmmc_softc *, struct sdmmc_function *);
  195. int sdmmc_set_relative_addr(struct sdmmc_softc *,
  196. struct sdmmc_function *);
  197. int sdmmc_send_if_cond(struct sdmmc_softc *, uint32_t);
  198. void sdmmc_intr_enable(struct sdmmc_function *);
  199. void sdmmc_intr_disable(struct sdmmc_function *);
  200. void *sdmmc_intr_establish(struct device *, int (*)(void *),
  201. void *, const char *);
  202. void sdmmc_intr_disestablish(void *);
  203. void sdmmc_intr_task(void *);
  204. int sdmmc_io_enable(struct sdmmc_softc *);
  205. void sdmmc_io_scan(struct sdmmc_softc *);
  206. int sdmmc_io_init(struct sdmmc_softc *, struct sdmmc_function *);
  207. void sdmmc_io_attach(struct sdmmc_softc *);
  208. void sdmmc_io_detach(struct sdmmc_softc *);
  209. u_int8_t sdmmc_io_read_1(struct sdmmc_function *, int);
  210. u_int16_t sdmmc_io_read_2(struct sdmmc_function *, int);
  211. u_int32_t sdmmc_io_read_4(struct sdmmc_function *, int);
  212. int sdmmc_io_read_multi_1(struct sdmmc_function *, int, u_char *, int);
  213. void sdmmc_io_write_1(struct sdmmc_function *, int, u_int8_t);
  214. void sdmmc_io_write_2(struct sdmmc_function *, int, u_int16_t);
  215. void sdmmc_io_write_4(struct sdmmc_function *, int, u_int32_t);
  216. int sdmmc_io_write_multi_1(struct sdmmc_function *, int, u_char *, int);
  217. int sdmmc_io_function_ready(struct sdmmc_function *);
  218. int sdmmc_io_function_enable(struct sdmmc_function *);
  219. void sdmmc_io_function_disable(struct sdmmc_function *);
  220. int sdmmc_read_cis(struct sdmmc_function *, struct sdmmc_cis *);
  221. void sdmmc_print_cis(struct sdmmc_function *);
  222. void sdmmc_check_cis_quirks(struct sdmmc_function *);
  223. int sdmmc_mem_enable(struct sdmmc_softc *);
  224. void sdmmc_mem_scan(struct sdmmc_softc *);
  225. int sdmmc_mem_init(struct sdmmc_softc *, struct sdmmc_function *);
  226. int sdmmc_mem_read_block(struct sdmmc_function *, int, u_char *, size_t);
  227. int sdmmc_mem_write_block(struct sdmmc_function *, int, u_char *, size_t);
  228. /* ioctls */
  229. #include <sys/ioccom.h>
  230. struct bio_sdmmc_command {
  231. void *cookie;
  232. struct sdmmc_command cmd;
  233. };
  234. struct bio_sdmmc_debug {
  235. void *cookie;
  236. int debug;
  237. };
  238. #define SDIOCEXECMMC _IOWR('S',0, struct bio_sdmmc_command)
  239. #define SDIOCEXECAPP _IOWR('S',1, struct bio_sdmmc_command)
  240. #define SDIOCSETDEBUG _IOWR('S',2, struct bio_sdmmc_debug)
  241. #endif