mmcard.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #ifndef _DEV_MMCARD_H_
  2. #define _DEV_MMCARD_H_
  3. /*
  4. * Copyright (C) 2005 by egnite Software GmbH. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file dev/mmcard.h
  36. * \brief Header file for basic multimedia card driver.
  37. *
  38. * \verbatim
  39. *
  40. * $Log$
  41. * Revision 1.5 2008/10/03 11:28:58 haraldkipp
  42. * Corrected and modified initialization of MultiMedia and SD Cards.
  43. *
  44. * Revision 1.4 2008/08/11 06:59:59 haraldkipp
  45. * BSD types replaced by stdint types (feature request #1282721).
  46. *
  47. * Revision 1.3 2006/10/08 16:48:09 haraldkipp
  48. * Documentation fixed
  49. *
  50. * Revision 1.2 2006/09/05 12:34:55 haraldkipp
  51. * Added commands for native MMC interface.
  52. *
  53. * Revision 1.1 2006/01/05 16:32:10 haraldkipp
  54. * First check-in.
  55. *
  56. *
  57. * \endverbatim
  58. */
  59. #include <sys/device.h>
  60. #include <sys/file.h>
  61. /*!
  62. * \addtogroup xgMmCard
  63. */
  64. /*@{*/
  65. #define NUTMC_SF_CD 0x01
  66. #define NUTMC_SF_WP 0x02
  67. #define NUTMC_SF_HC 0x04
  68. #define NUTMC_SF_ILL 0x80
  69. #define NUTMC_IND_OFF 0
  70. #define NUTMC_IND_READ 1
  71. #define NUTMC_IND_WRITE 2
  72. #define NUTMC_IND_ERROR 4
  73. /*!
  74. * \brief Memory card support structure.
  75. */
  76. typedef struct _MEMCARDSUPP {
  77. /*! \brief Status change flag. */
  78. uint8_t mcs_cf;
  79. /*! \brief Socket status. */
  80. uint8_t mcs_sf;
  81. /*! \brief Status reset. */
  82. int (*mcs_reset) (NUTDEVICE *);
  83. /*! \brief Set activity indicator. */
  84. void (*mcs_act) (int);
  85. /*! \brief Power up or down. */
  86. int (*mcs_power) (int);
  87. } MEMCARDSUPP;
  88. /*!
  89. * \brief Low level access information structure.
  90. */
  91. typedef struct _MMCIFC {
  92. /*! Initialize the card. */
  93. int (*mmcifc_in) (void);
  94. /*! Read received byte and transmit a new one. */
  95. uint8_t(*mmcifc_io) (uint8_t);
  96. /*! Select or deselect the card. */
  97. int (*mmcifc_cs) (int);
  98. /*! Query card detect. */
  99. int (*mmcifc_cd) (void);
  100. /*! Query write protect. */
  101. int (*mmcifc_wp) (void);
  102. /*! Set addressing mode. */
  103. int (*mmcifc_sm) (int);
  104. /*! Get addressing mode. */
  105. int (*mmcifc_gm) (void);
  106. } MMCIFC;
  107. #define MMCMD_HOST 0x40
  108. #define MMCMD_RESET_CRC 0x95
  109. #define MMCMD_IF_COND_CRC 0x87
  110. /*!
  111. * \brief Addressing mode selection flags
  112. * SD-HC uses byte addressing, SD-normal uses block addressing
  113. */
  114. #define MMC_BLOCK_MODE 0 // access card in block mode,
  115. #define MMC_BYTE_MODE 1 // acces card using byte-addresses iso sector addresses
  116. /*! \brief Reset card to idle state.
  117. *
  118. * In idle state the card will not accept any other commands than
  119. * MMCMD_SEND_OP_COND or MMCMD_READ_OCR.
  120. */
  121. #define MMCMD_GO_IDLE_STATE 0
  122. /*! \brief Activate card's initialization process. */
  123. #define MMCMD_SEND_OP_COND 1
  124. /*! \brief Send operation condition register.
  125. *
  126. * Queries the operation condition register content from all cards, which
  127. * are in idle state.
  128. */
  129. #define MMCMD_ALL_SEND_CID 2
  130. /*! \brief Assign relative card address. */
  131. #define MMCMD_SEND_RELATIVE_ADDR 3
  132. /*! \brief Assign relative card address. */
  133. #define MMCMD_SELECT_CARD 7
  134. /*! \brief negociate operating voltage (mandatory for 2.0). */
  135. // TODO: Double command? Same as extcsd?
  136. #define MMCMD_SEND_IF_COND 8
  137. /*! \brief Query card's extended CSD. */
  138. #define MMCMD_SEND_EXTCSD 8
  139. /*! \brief Query card's CSD. */
  140. #define MMCMD_SEND_CSD 9
  141. /*! \brief Query card's CID. */
  142. #define MMCMD_SEND_CID 10
  143. /*! \brief Stop multiple block transmission. */
  144. #define MMCMD_STOP_TRANSMISSION 12
  145. /*! \brief Query card's status register. */
  146. #define MMCMD_SEND_STATUS 13
  147. /*! \brief Select block length for following read/write commands. */
  148. #define MMCMD_SET_BLOCKLEN 16
  149. /*! \brief Initiate single block read. */
  150. #define MMCMD_READ_SINGLE_BLOCK 17
  151. /*! \brief Initiate continuous block read. */
  152. #define MMCMD_READ_MULTIPLE_BLOCK 18
  153. /*! \brief Initiate single block write. */
  154. #define MMCMD_WRITE_BLOCK 24
  155. /*! \brief Initiate continuous block write. */
  156. #define MMCMD_WRITE_MULTIPLE_BLOCK 25
  157. /*! \brief Initiate programming of programmable CSD bits. */
  158. #define MMCMD_PROGRAM_CSD 27
  159. /*! \brief Enable card's optional write protection. */
  160. #define MMCMD_SET_WRITE_PROTECT 28
  161. /*! \brief Disable card's write protection. */
  162. #define MMCMD_CLR_WRITE_PROTECT 29
  163. /*! \brief Query card's write protect status. */
  164. #define MMCMD_SEND_WRITE_PROTECT 30
  165. /*! \brief Set address of the first erase group. */
  166. #define MMCMD_TAG_ERASE_GROUP_START 35
  167. /*! \brief Set address of the last erase group. */
  168. #define MMCMD_TAG_ERASE_GROUP_END 36
  169. /*! \brief Erase previously selected sectors. */
  170. #define MMCMD_ERASE 38
  171. /*! \brief Activate SD card's initialization process. */
  172. #define MMCMD_SEND_APP_OP_COND 41
  173. /*! \brief Set/clear password or lock/unlock the card. */
  174. #define MMCMD_LOCK_UNLOCK 42
  175. /*! \brief Application command follows. */
  176. #define MMCMD_SEND_APP_CMD 55
  177. /*! \brief Query card's operating condition register. */
  178. #define MMCMD_READ_OCR 58
  179. /*! \brief Enable or disable CRC mode.
  180. *
  181. * In SPI mode CRC is disabled by default.
  182. */
  183. #define MMCMD_CRC_ON_OFF 59
  184. /*! \brief Card is idle. */
  185. #define MMR1_IDLE_STATE 0x00
  186. /*! \brief Card is busy. */
  187. #define MMR1_NOT_IDLE 0x01
  188. /*! \brief Erase sequence was cleared before execution. */
  189. #define MMR1_ERASE_RESET 0x02
  190. /*! \brief Illegal command code detected. */
  191. #define MMR1_ILLEGAL_COMMAND 0x04
  192. /*! \brief Bad command CRC detected. */
  193. #define MMR1_COM_CRC_ERROR 0x08
  194. /*! \brief Bad erase sequence. */
  195. #define MMR1_ERASE_SEQ_ERROR 0x10
  196. /*! \brief Misaligned address did not match block length. */
  197. #define MMR1_ADDRESS_ERROR 0x20
  198. /*! \brief Command parameter is out of range. */
  199. #define MMR1_PARAMETER_ERROR 0x40
  200. /*! \brief Card is locked. */
  201. #define MMR2_CARD_LOCKED 0x01
  202. /*! \brief Erasing write protected sector or password error. */
  203. #define MMR2_WP_ERASE_SKIP 0x02
  204. /*! \brief General or unknown error occured. */
  205. #define MMR2_ERROR 0x04
  206. /*! \brief Internal card controller error. */
  207. #define MMR2_CC_ERROR 0x08
  208. /*! \brief Bad internal ECC. */
  209. #define MMR2_ECC_FAILED 0x10
  210. /*! \brief Failed to write to protected block. */
  211. #define MMR2_WP_VIOLATION 0x20
  212. /*! \brief Invalid erase parameter. */
  213. #define MMR2_ERASE_PARAMETER 0x40
  214. /*! \brief Command parameter is out of range. */
  215. #define MMR2_OUT_OF_RANGE 0x80
  216. #define MMCSR_OUT_OF_RANGE 0x80000000
  217. #define MMCSR_ADDRESS_ERROR 0x40000000
  218. #define MMCSR_BLOCK_LEN_ERROR 0x20000000
  219. #define MMCSR_ERASE_SEQ_ERROR 0x10000000
  220. #define MMCSR_ERASE_PARAM 0x08000000
  221. #define MMCSR_WP_VIOLATION 0x04000000
  222. #define MMCSR_COM_CRC_ERROR 0x00800000
  223. #define MMCSR_ILLEGAL_COMMAND 0x00400000
  224. #define MMCSR_ERROR 0x00080000
  225. #define MMCSR_CIDCSD_OVERWRITE 0x00010000
  226. #define MMCSR_WP_ERASE_SKIP 0x00008000
  227. #define MMCSR_CARD_ECC_DISABLED 0x00004000
  228. #define MMCSR_ERASE_RESET 0x00002000
  229. #define MMCSR_STATE_MASK 0x00001E00
  230. #define MMCSR_READY_FOR_DATA 0x00000100
  231. #define MMCSR_IS_IDLE 0x00000000
  232. #define MMCSR_IS_READY 0x00000200
  233. #define MMCSR_IS_IDENT 0x00000400
  234. #define MMCSR_IS_STBY 0x00000600
  235. #define MMCSR_IS_TRAN 0x00000800
  236. #define MMCSR_IS_DATA 0x00000A00
  237. #define MMCSR_IS_RCV 0x00000C00
  238. #define MMCSR_IS_PRG 0x00000E00
  239. #define MMCSR_IS_DIS 0x00001000
  240. #define MMDR_ACCEPTED
  241. #define MMDR_CRC_ERROR
  242. #define MMDR_WRITE_ERROR
  243. #define MMCERR_TIMEOUT 0x00000001
  244. #define MMCOP_NBUSY 0x80000000
  245. /*! \brief Number of bytes in the CID register. */
  246. #define MMCARD_CIDR_SIZE 16
  247. /*! \brief Number of bytes in the CSD register. */
  248. #define MMCARD_CSDR_SIZE 16
  249. /*! \brief Number of bytes in the operating condition register. */
  250. #define MMCARD_OCR_SIZE 32
  251. /*!
  252. * \name Voltage Ranges
  253. */
  254. /*@{*/
  255. #define MMCARD_165_195V 0x00000080
  256. #define MMCARD_20_21V 0x00000100
  257. #define MMCARD_21_22V 0x00000200
  258. #define MMCARD_22_23V 0x00000400
  259. #define MMCARD_23_24V 0x00000800
  260. #define MMCARD_24_25V 0x00001000
  261. #define MMCARD_25_26V 0x00002000
  262. #define MMCARD_26_27V 0x00004000
  263. #define MMCARD_27_28V 0x00008000
  264. #define MMCARD_28_29V 0x00010000
  265. #define MMCARD_29_30V 0x00020000
  266. #define MMCARD_30_31V 0x00040000
  267. #define MMCARD_31_32V 0x00080000
  268. #define MMCARD_32_33V 0x00100000
  269. #define MMCARD_33_34V 0x00200000
  270. #define MMCARD_34_35V 0x00400000
  271. #define MMCARD_35_36V 0x00800000
  272. /*@}*/
  273. /*!
  274. * \name Control Codes
  275. */
  276. /*@{*/
  277. /*! \brief Retrieve card status. */
  278. #define MMCARD_GETSTATUS 0x2001
  279. /*! \brief Retrieve operation condition register. */
  280. #define MMCARD_GETOCR 0x2002
  281. /*! \brief Retrieve card identification. */
  282. #define MMCARD_GETCID 0x2003
  283. /*! \brief Retrieve card specific data. */
  284. #define MMCARD_GETCSD 0x2004
  285. /*! \brief Retrieve extended card specific data. */
  286. #define MMCARD_GETEXTCSD 0x2005
  287. /*@}*/
  288. /*!
  289. * \brief Multimedia card identification register.
  290. */
  291. typedef struct NUT_PACKED_TYPE _MMC_CID {
  292. /*! \brief Manufacturer identifier. */
  293. uint8_t mmcid_mid;
  294. /*! \brief OEM/Application identifier. */
  295. uint16_t mmcid_oid;
  296. /*! \brief Product name. */
  297. uint8_t mmcid_pnm[6];
  298. /*! \brief Product revision. */
  299. uint8_t mmcid_rev;
  300. /*! \brief Serial number. */
  301. uint32_t mmcid_psn;
  302. /*! \brief Manufacturing date code. */
  303. uint8_t mmcid_mdt;
  304. /*! \brief CRC7 checksum. */
  305. uint8_t mmcid_crc;
  306. } MMC_CID;
  307. /*!
  308. * \brief Multimedia card identification register.
  309. */
  310. typedef struct NUT_PACKED_TYPE _MMC_CSD {
  311. /*! \brief Card specification. */
  312. uint8_t mmcsd_spec;
  313. /*! \brief Data read access time. */
  314. uint8_t mmcsd_taac;
  315. /*! \brief Data read access time 2. */
  316. uint8_t mmcsd_nsac;
  317. /*! \brief Maximum data transfer rate. */
  318. uint8_t mmcsd_speed;
  319. /*! \brief Card command classes and max. read block length. */
  320. uint8_t mmcsd_ccc_bl[2];
  321. /*! \brief Read-only fields.
  322. *
  323. * - [0] 0..1 Device size bits 10..11.
  324. * - [0] 2..3 Reserved.
  325. * - [0] 4 DSR implemented.
  326. * - [0] 5 Read block misalignment.
  327. * - [0] 6 Write block misalignment.
  328. * - [0] 7 Partial blocks for read allowed.
  329. * - [1] 0..7 Device size bits 2..9.
  330. * - [2] 0..2 Max. read current at VDD max.
  331. * - [2] 3..5 Max. read current at VDD min.
  332. * - [2] 6..7 Device size bits 0..1.
  333. * - [3] 0..1 Device size multiplier bits 1..2.
  334. * - [3] 2..4 Max. write current at VDD max.
  335. * - [3] 5..7 Max. write current at VDD min.
  336. * - [4] 0..1 Erase group size multiplier bits 3..4.
  337. * - [4] 2..6 Erase group size.
  338. * - [4] 7 Device size multiplier bit 0.
  339. * - [5] 0..4 Write protect group size.
  340. * - [5] 5..7 Erase group size multiplier bits 0..2.
  341. * - [6] 0..1 Max. write data block length bits 2..3.
  342. * - [6] 2..4 Read to write speed factor.
  343. * - [6] 5..6 Reserved.
  344. * - [6] 7 Write protect group enable.
  345. * - [7] 0 Content protection application.
  346. * - [7] 1..4 Reserved.
  347. * - [7] 5 Partial blocks for write allowed.
  348. * - [7] 6..7 Max. write data block length bits 0..1.
  349. */
  350. uint8_t mmcsd_rfld[8];
  351. /*! \brief Programmable field. */
  352. uint8_t mmcsd_pfld;
  353. /*! \brief Checksum. */
  354. uint8_t mmcsd_crc;
  355. } MMC_CSD;
  356. /*@}*/
  357. extern int MmCardDevInit(NUTDEVICE * dev);
  358. extern int MmCardIOCtl(NUTDEVICE * dev, int req, void *conf);
  359. extern int MmCardBlockRead(NUTFILE * nfp, void *buffer, int len);
  360. extern int MmCardBlockWrite(NUTFILE * nfp, const void *buffer, int len);
  361. #ifdef __HARVARD_ARCH__
  362. extern int MmCardBlockWrite_P(NUTFILE * nfp, PGM_P buffer, int len);
  363. #endif
  364. extern NUTFILE *MmCardMount(NUTDEVICE * dev, const char *name, int mode, int acc);
  365. extern int MmCardUnmount(NUTFILE * nfp);
  366. extern int SpiMmcInit(NUTDEVICE * dev);
  367. extern int SpiMmcIOCtl(NUTDEVICE * dev, int req, void *conf);
  368. extern int SpiMmcBlockRead(NUTFILE * nfp, void *buffer, int num);
  369. extern int SpiMmcBlockWrite(NUTFILE * nfp, const void *buffer, int num);
  370. #ifdef __HARVARD_ARCH__
  371. extern int SpiMmcBlockWrite_P(NUTFILE * nfp, PGM_P buffer, int len);
  372. #endif
  373. extern NUTFILE *SpiMmcMount(NUTDEVICE * dev, const char *name, int mode, int acc);
  374. extern int SpiMmcUnmount(NUTFILE * nfp);
  375. #endif