mmcard.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. /*
  2. * Copyright (C) 2005-2007 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. /*!
  33. * \brief Basic block device driver for multimedia cards.
  34. *
  35. * The driver uses SPI mode, but doesn't include any low level hardware
  36. * access. This must be provided by some additional routines.
  37. *
  38. * \verbatim
  39. *
  40. * $Log$
  41. * Revision 1.15 2009/02/13 14:52:05 haraldkipp
  42. * Include memdebug.h for heap management debugging support.
  43. *
  44. * Revision 1.14 2009/02/06 15:40:29 haraldkipp
  45. * Using newly available strdup() and calloc().
  46. * Replaced NutHeap routines by standard malloc/free.
  47. * Replaced pointer value 0 by NULL.
  48. *
  49. * Revision 1.13 2009/01/17 11:26:46 haraldkipp
  50. * Getting rid of two remaining BSD types in favor of stdint.
  51. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  52. *
  53. * Revision 1.12 2008/08/11 06:59:42 haraldkipp
  54. * BSD types replaced by stdint types (feature request #1282721).
  55. *
  56. * Revision 1.11 2008/07/14 13:09:30 haraldkipp
  57. * Allow small MultiMedia Cards without partition table.
  58. *
  59. * Revision 1.10 2007/08/30 12:15:06 haraldkipp
  60. * Configurable MMC timings.
  61. *
  62. * Revision 1.9 2006/10/08 16:48:09 haraldkipp
  63. * Documentation fixed
  64. *
  65. * Revision 1.8 2006/07/05 08:03:12 haraldkipp
  66. * Bugfix. Trailing slash in mount path not properly handled.
  67. * Thanks to Michael Fischer.
  68. *
  69. * Revision 1.7 2006/06/18 16:34:46 haraldkipp
  70. * Mutex deadlock fixed.
  71. *
  72. * Revision 1.6 2006/05/25 09:34:21 haraldkipp
  73. * Added mutual exclusion lock for multithreaded access.
  74. *
  75. * Revision 1.5 2006/04/07 12:29:03 haraldkipp
  76. * Number of read retries increased. Memory hole fixed.
  77. * Added ioctl(NUTBLKDEV_MEDIAAVAIL).
  78. * Card change ioctl() will also return 1 if no card is available.
  79. *
  80. * Revision 1.4 2006/02/23 15:43:56 haraldkipp
  81. * Timeout value increased. Some cards have long write latencies.
  82. *
  83. * Revision 1.3 2006/01/22 17:36:31 haraldkipp
  84. * Some cards need more time to enter idle state.
  85. * Card access now returns an error after card change detection.
  86. *
  87. * Revision 1.2 2006/01/19 18:40:08 haraldkipp
  88. * Timeouts increased and long time sleeps decreased for better performance.
  89. *
  90. * Revision 1.1 2006/01/05 16:30:49 haraldkipp
  91. * First check-in.
  92. *
  93. *
  94. * \endverbatim
  95. */
  96. #include <cfg/os.h>
  97. #include <cfg/mmci.h>
  98. #if defined(NUTDEBUG)
  99. #include <stdio.h>
  100. #endif
  101. #include <errno.h>
  102. #include <string.h>
  103. #include <stdlib.h>
  104. #include <memdebug.h>
  105. #include <sys/heap.h>
  106. #include <sys/timer.h>
  107. #include <sys/event.h>
  108. #include <fs/dospart.h>
  109. #include <fs/fs.h>
  110. #include <dev/blockdev.h>
  111. #include <dev/mmcard.h>
  112. /*!
  113. * \addtogroup xgMmCard
  114. */
  115. /*@{*/
  116. #ifndef MMC_BLOCK_SIZE
  117. /*!
  118. * \brief Block size.
  119. *
  120. * Block size in bytes. Do not change unless you are sure that both,
  121. * the file system and the hardware support it.
  122. */
  123. #define MMC_BLOCK_SIZE 512
  124. #endif
  125. #ifndef MMC_MAX_INIT_POLLS
  126. /*!
  127. * \brief Card init timeout.
  128. *
  129. * Max. number of loops waiting for card's idle mode after initialization.
  130. * An additional delay of 1 ms is added to each loop after one quarter of
  131. * this value elapsed.
  132. */
  133. #define MMC_MAX_INIT_POLLS 512
  134. #endif
  135. #ifndef MMC_MAX_INIT_POLLS_SDHC
  136. /*!
  137. * \brief Card init timeout for SDHC cards.
  138. */
  139. #define MMC_MAX_INIT_POLLS_SDHC 4096
  140. #endif
  141. #ifndef MMC_MAX_RESET_POLLS
  142. /*!
  143. * \brief Card reset timeout.
  144. *
  145. * Max. number of loops waiting for card's idle mode after resetting it.
  146. */
  147. #define MMC_MAX_RESET_POLLS 255
  148. #endif
  149. #ifndef MMC_MAX_WRITE_POLLS
  150. /*!
  151. * \brief Card write timeout.
  152. *
  153. * Max. number of loops waiting for card's idle mode after resetting it.
  154. * An additional delay of 1 ms is added to each loop after 31/32 of this
  155. * value elapsed.
  156. */
  157. #define MMC_MAX_WRITE_POLLS 1024
  158. #endif
  159. #ifndef MMC_MAX_WRITE_RETRIES
  160. /*!
  161. * \brief Card write retries.
  162. *
  163. * Max. number of retries while writing.
  164. */
  165. #define MMC_MAX_WRITE_RETRIES 32
  166. #endif
  167. #ifndef MMC_MAX_READ_RETRIES
  168. /*!
  169. * \brief Card read retries.
  170. *
  171. * Max. number of retries while reading.
  172. */
  173. #define MMC_MAX_READ_RETRIES 8
  174. #endif
  175. #ifndef MMC_MAX_REG_POLLS
  176. /*!
  177. * \brief Register read timeout.
  178. *
  179. * Max. number of loops while reading a card's register.
  180. */
  181. #define MMC_MAX_REG_POLLS 512
  182. #endif
  183. #ifndef MMC_MAX_CMDACK_POLLS
  184. /*!
  185. * \brief Command acknowledge timeout.
  186. *
  187. * Max. number of loops waiting for card's acknowledge of a command.
  188. * An additional delay of 1 ms is added to each loop after three quarter
  189. * of this value elapsed.
  190. */
  191. #define MMC_MAX_CMDACK_POLLS 1024
  192. #endif
  193. #ifndef MMC_MAX_R1_POLLS
  194. /*!
  195. * \brief R1 response timeout.
  196. *
  197. * Max. number of loops waiting for card's R1 response.
  198. */
  199. #define MMC_MAX_R1_POLLS 1024
  200. #endif
  201. /*!
  202. * \brief Local multimedia card mount information.
  203. */
  204. typedef struct _MMCFCB {
  205. /*! \brief Attached file system device.
  206. */
  207. NUTDEVICE *fcb_fsdev;
  208. /*! \brief Partition table entry of the currently mounted partition.
  209. */
  210. DOSPART fcb_part;
  211. /*! \brief Next block number to read.
  212. *
  213. * The file system driver will send a NUTBLKDEV_SEEK control command
  214. * to set this value before calling the read or the write routine.
  215. *
  216. * The number is partition relative.
  217. */
  218. uint32_t fcb_blknum;
  219. /*! \brief Internal block buffer.
  220. *
  221. * A file system driver may use this one or optionally provide it's
  222. * own buffers.
  223. *
  224. * Minimal systems may share their external bus interface with
  225. * device I/O lines, in which case the buffer must be located
  226. * in internal memory.
  227. */
  228. uint8_t fcb_blkbuf[MMC_BLOCK_SIZE];
  229. } MMCFCB;
  230. /*
  231. * Several routines call NutSleep, which results in a context switch.
  232. * This mutual exclusion semaphore takes care, that multiple threads
  233. * do not interfere with each other.
  234. */
  235. static HANDLE mutex;
  236. /*!
  237. * \brief Send command to multimedia card.
  238. *
  239. * \param ifc Specifies the hardware interface.
  240. * \param cmd Command code. See MMCMD_ macros.
  241. * \param param Optional command parameter.
  242. */
  243. static void MmCardTxCmd(MMCIFC * ifc, uint8_t cmd, uint32_t param)
  244. {
  245. unsigned int tmo = MMC_MAX_CMDACK_POLLS;
  246. uint8_t ch;
  247. /* Enable card select. */
  248. (*ifc->mmcifc_cs) (1);
  249. /*
  250. * Repeat sending nothing until we receive nothing. Actually
  251. * it should be sufficient to send a single 0xFF value, but
  252. * running a loop seems to fix certain kind of sync problems.
  253. */
  254. while ((ch = (*ifc->mmcifc_io) (0xFF)) != 0xFF) {
  255. if (--tmo == 0) {
  256. #ifdef NUTDEBUG
  257. printf("[MMCmd%u Timeout %02X]\n", cmd, ch);
  258. #endif
  259. break;
  260. }
  261. if (tmo < MMC_MAX_CMDACK_POLLS / 4) {
  262. NutSleep(1);
  263. }
  264. }
  265. /* Send command and parameter. */
  266. (*ifc->mmcifc_io) (MMCMD_HOST | cmd);
  267. (*ifc->mmcifc_io) ((uint8_t) (param >> 24));
  268. (*ifc->mmcifc_io) ((uint8_t) (param >> 16));
  269. (*ifc->mmcifc_io) ((uint8_t) (param >> 8));
  270. (*ifc->mmcifc_io) ((uint8_t) param);
  271. /*
  272. * We are running with CRC disabled. However, the reset and excsd commands
  273. * must be send with a valid CRC. Fortunately these commands are sent with a
  274. * fixed parameter value of zero, which results in a fixed CRC value
  275. */
  276. if (cmd == MMCMD_SEND_IF_COND) {
  277. (*ifc->mmcifc_io) (MMCMD_IF_COND_CRC);
  278. } else {
  279. (*ifc->mmcifc_io) (MMCMD_RESET_CRC);
  280. }
  281. }
  282. /*!
  283. * \brief Receive an R1 response token from the card.
  284. *
  285. * In SPI mode, the card sends an R1 response after every command except
  286. * after the SEND_STATUS and the READ_OCR commands.
  287. *
  288. * \param ifc Specifies the hardware interface.
  289. *
  290. * \return R1 response token or 0xFF on timeout.
  291. */
  292. static uint8_t MmCardRxR1(MMCIFC * ifc)
  293. {
  294. uint8_t rc;
  295. int i;
  296. for (i = 0; i < MMC_MAX_R1_POLLS; i++) {
  297. if ((rc = (*ifc->mmcifc_io) (0xFF)) != 0xFF) {
  298. break;
  299. }
  300. }
  301. return rc;
  302. }
  303. /*!
  304. * \brief Receive an R2 response token from the card.
  305. *
  306. * In SPI mode the card sends this token in response to the SEND_STATUS
  307. * command.
  308. *
  309. * \param ifc Specifies the hardware interface.
  310. *
  311. * \return R2 response token or 0xFFFF on timeout.
  312. */
  313. static uint16_t MmCardRxR2(MMCIFC * ifc)
  314. {
  315. uint16_t rc;
  316. rc = MmCardRxR1(ifc);
  317. rc <<= 8;
  318. rc += (*ifc->mmcifc_io) (0xFF);
  319. return rc;
  320. }
  321. /*!
  322. * \brief Receive an R3 response token from the card.
  323. *
  324. * In SPI mode the card sends this token in response to the READ_OCR
  325. * command.
  326. *
  327. * \param ifc Specifies the hardware interface.
  328. * \param ocr Points to a buffer which receives the OCR register contents.
  329. *
  330. * \return R1 response token or 0xFF on timeout.
  331. */
  332. static uint8_t MmCardRxR3(MMCIFC * ifc, uint32_t * ocr)
  333. {
  334. uint8_t rc;
  335. int i;
  336. /* The first byte is equal to the R1 response. */
  337. rc = MmCardRxR1(ifc);
  338. /* Receive the operating condition. */
  339. for (i = 0; i < 4; i++) {
  340. *ocr <<= 8;
  341. *ocr |= (*ifc->mmcifc_io) (0xFF);
  342. }
  343. return rc;
  344. }
  345. /*!
  346. * \brief Configure card for SPI mode.
  347. *
  348. * \param ifc Specifies the hardware interface.
  349. *
  350. * \return 0 on success, -1 otherwise.
  351. */
  352. static int MmCardReset(MMCIFC * ifc)
  353. {
  354. int i;
  355. uint8_t rsp;
  356. /*
  357. * Initialize the low level card interface.
  358. */
  359. if ((*ifc->mmcifc_in) ()) {
  360. return -1;
  361. }
  362. /*
  363. * 80 bits of ones with deactivated chip select will put the card
  364. * in SPI mode.
  365. *
  366. * /note that spec 2.0 tells us that we need to send the CMD0 with CS inactive
  367. * to get the card in SPI-mode. This 'old' methode however still seems to work
  368. * with 2.0 cards
  369. */
  370. (*ifc->mmcifc_cs) (0);
  371. for (i = 0; i < 10; i++) {
  372. (*ifc->mmcifc_io) (0xFF);
  373. }
  374. /*
  375. * Switch to idle state and wait until initialization is running
  376. * or idle state is reached.
  377. */
  378. for (i = 0; i < MMC_MAX_RESET_POLLS; i++) {
  379. MmCardTxCmd(ifc, MMCMD_GO_IDLE_STATE, 0);
  380. rsp = MmCardRxR1(ifc);
  381. (*ifc->mmcifc_cs) (0);
  382. if (rsp == MMR1_IDLE_STATE || rsp == MMR1_NOT_IDLE) {
  383. return 0;
  384. }
  385. }
  386. return -1;
  387. }
  388. /*!
  389. * \brief Initialize the multimedia card.
  390. *
  391. * This routine will put a newly powered up card into SPI mode.
  392. * It is called by MmCardMount().
  393. *
  394. * \param ifc Specifies the hardware interface.
  395. *
  396. * \return 0 on success, -1 otherwise.
  397. */
  398. static int MmCardInit(MMCIFC * ifc)
  399. {
  400. int i;
  401. uint8_t rsp;
  402. uint8_t ocr[4];
  403. /*
  404. * assume BLOCK_MODE addressing for most cards.
  405. * if the cards turns out to be a SD_HC card, the mode will
  406. * be set to BYTE_MODE later on
  407. */
  408. (*ifc->mmcifc_sm) (MMC_BLOCK_MODE);
  409. /*
  410. * Try to switch to SPI mode. Looks like a retry helps to fix
  411. * certain synchronization problems.
  412. */
  413. if (MmCardReset(ifc)) {
  414. if (MmCardReset(ifc)) {
  415. #ifdef NUTDEBUG
  416. printf("[CardReset failed]");
  417. #endif
  418. return -1;
  419. }
  420. }
  421. /*
  422. * Let's check if we deal with a card that
  423. * support the 2.0 specs by sending the EXTCSD command.
  424. * If the card response is "illegal Command", we know
  425. * that the 2.0 specs are not supported.
  426. * Note that SD-HC cards always support the 2.0 specs
  427. */
  428. MmCardTxCmd(ifc, MMCMD_SEND_IF_COND, 0x1AA);
  429. rsp = MmCardRxR1(ifc);
  430. /* Receive the data. */
  431. for (i = 0; i < 4; i++) {
  432. ocr[i] = (*ifc->mmcifc_io) (0xFF);
  433. }
  434. if ((ocr[2] == 0x01) && (ocr[3] == 0xAA)) {
  435. // The card can work at vdd range of 2.7-3.6V
  436. #ifdef NUTDEBUG
  437. printf("[SD -> support for 2.0 specs]");
  438. #endif
  439. /*
  440. * Wait for a really long time until card is initialized
  441. * and enters idle state.
  442. */
  443. for (i = 0; i < MMC_MAX_INIT_POLLS_SDHC; i++) {
  444. MmCardTxCmd(ifc, MMCMD_SEND_APP_CMD, 0);
  445. if (MmCardRxR1(ifc) <= MMR1_NOT_IDLE) {
  446. /* some cards really need a lot of time for their initialisation... */
  447. NutSleep(10);
  448. /* ACMD41 with HCS bit */
  449. MmCardTxCmd(ifc, MMCMD_SEND_APP_OP_COND, 1UL << 30);
  450. /* check response */
  451. if (MmCardRxR1(ifc)==MMR1_IDLE_STATE) {
  452. #ifdef NUTDEBUG
  453. printf("[CardIdle]");
  454. #endif
  455. // Check the CSS bit that tells us if we are dealing with HC-cards
  456. MmCardTxCmd(ifc, MMCMD_READ_OCR, 0);
  457. rsp = MmCardRxR1(ifc);
  458. /* Receive the data. */
  459. for (i = 0; i < 4; i++) {
  460. ocr[i] = (*ifc->mmcifc_io) (0xFF);
  461. }
  462. if (ocr[0] & 0x40) {
  463. /* set byte addressing mode for SD-HC cards */
  464. (*ifc->mmcifc_sm) (MMC_BYTE_MODE);
  465. #ifdef NUTDEBUG
  466. printf("[Found SD-HC]");
  467. #endif
  468. }
  469. /* Initialize MMC access mutex semaphore. */
  470. NutEventPost(&mutex);
  471. return 0; /* initialisation finished */
  472. }
  473. }
  474. }
  475. /* Card stays in IDLE state for some reason... */
  476. #ifdef NUTDEBUG
  477. printf("[CardInit failed [%d]]", rsp);
  478. #endif
  479. return(-1);
  480. }
  481. else {
  482. /*
  483. * Wait for a really long time until card is initialized
  484. * and enters idle state.
  485. */
  486. for (i = 0; i < MMC_MAX_INIT_POLLS; i++) {
  487. /*
  488. * In SPI mode SEND_OP_COND is a dummy, used to poll the card
  489. * for initialization finished. Thus, there are no parameters
  490. * and no operation condition data is sent back.
  491. */
  492. MmCardTxCmd(ifc, MMCMD_SEND_OP_COND, 0);
  493. rsp = MmCardRxR1(ifc);
  494. (*ifc->mmcifc_cs) (0);
  495. if (rsp == MMR1_IDLE_STATE) {
  496. #ifdef NUTDEBUG
  497. printf("[CardIdle]");
  498. #endif
  499. /* Initialize MMC access mutex semaphore. */
  500. NutEventPost(&mutex);
  501. return 0;
  502. }
  503. if (i > MMC_MAX_INIT_POLLS / 4) {
  504. NutSleep(1);
  505. }
  506. }
  507. #ifdef NUTDEBUG
  508. printf("[CardInit failed [%d]]", rsp);
  509. #endif
  510. }
  511. return -1;
  512. }
  513. /*!
  514. * \brief Read or verify a single block.
  515. *
  516. * \param ifc Specifies the hardware interface.
  517. * \param blk Block number to read or verify.
  518. * \param buf Data buffer. Receives the data or is verified against the
  519. * data being read from the card.
  520. *
  521. * \return 0 on success, -1 otherwise.
  522. */
  523. static int MmCardReadOrVerify(MMCIFC * ifc, uint32_t blk, uint8_t * buf, int vflg)
  524. {
  525. int rc = -1;
  526. int retries = 64;
  527. int i;
  528. uint8_t rsp;
  529. /* Gain mutex access. */
  530. NutEventWait(&mutex, 0);
  531. /*
  532. * SD_HC cards use BYTE_ADDRESSING, which means that the partion info
  533. * that indicates the starting address of the BOOT_RECORD is in bytes (and
  534. * not in sectors like with non SD_HC cards).
  535. * Please note that the sector size is not officially known at this stage since
  536. * this code does NOT read the CDS register. However, for SD-HC cards fortunately
  537. * it is defined that the sectorsize is always 512.
  538. */
  539. if ((*ifc->mmcifc_gm) () == MMC_BLOCK_MODE) {
  540. blk <<= 9;
  541. }
  542. while (retries--) {
  543. MmCardTxCmd(ifc, MMCMD_READ_SINGLE_BLOCK, blk);
  544. if ((rsp = MmCardRxR1(ifc)) == 0x00) {
  545. if ((rsp = MmCardRxR1(ifc)) == 0xFE) {
  546. rc = 0;
  547. if (vflg) {
  548. for (i = 0; i < MMC_BLOCK_SIZE; i++) {
  549. if (*buf != (*ifc->mmcifc_io) (0xFF)) {
  550. rc = -1;
  551. }
  552. buf++;
  553. }
  554. } else {
  555. for (i = 0; i < MMC_BLOCK_SIZE; i++) {
  556. *buf = (*ifc->mmcifc_io) (0xFF);
  557. buf++;
  558. }
  559. }
  560. (*ifc->mmcifc_io) (0xff);
  561. (*ifc->mmcifc_io) (0xff);
  562. (*ifc->mmcifc_cs) (0);
  563. break;
  564. }
  565. }
  566. (*ifc->mmcifc_cs) (0);
  567. }
  568. /* Release mutex access. */
  569. NutEventPost(&mutex);
  570. return rc;
  571. }
  572. /*!
  573. * \brief Write a single block.
  574. *
  575. * \param ifc Specifies the hardware interface.
  576. * \param blk Block number to read or verify.
  577. * \param buf Pointer to a buffer which holds the data to write.
  578. *
  579. * \return 0 on success, -1 otherwise.
  580. */
  581. static int MmCardWrite(MMCIFC * ifc, uint32_t blk, const uint8_t * buf)
  582. {
  583. int rc = -1;
  584. int retries = MMC_MAX_WRITE_RETRIES;
  585. int tmo;
  586. int i;
  587. uint8_t rsp;
  588. /* Gain mutex access. */
  589. NutEventWait(&mutex, 0);
  590. /*
  591. * SD_HC cards use BYTE_ADDRESSING, which means that the partion info
  592. * that indicates the starting address of the BOOT_RECORD is in bytes (and
  593. * not in sectors like with non SD_HC cards).
  594. * Please note that the sector size is not officially known at this stage since
  595. * this code does NOT read the CDS register. However, for SD-HC cards fortunately
  596. * it is defined that the sectorsize is always 512.
  597. */
  598. if ((*ifc->mmcifc_gm) () == MMC_BLOCK_MODE) {
  599. blk <<= 9;
  600. }
  601. while (retries--) {
  602. MmCardTxCmd(ifc, MMCMD_WRITE_BLOCK, blk);
  603. if ((rsp = MmCardRxR1(ifc)) == 0x00) {
  604. (*ifc->mmcifc_io) (0xFF);
  605. (*ifc->mmcifc_io) (0xFE);
  606. for (i = 0; i < MMC_BLOCK_SIZE; i++) {
  607. (*ifc->mmcifc_io) (*buf);
  608. buf++;
  609. }
  610. // (*ifc->mmcifc_io)(0xFF);
  611. // (*ifc->mmcifc_io)(0xFF);
  612. if ((rsp = MmCardRxR1(ifc)) == 0xE5) {
  613. for (tmo = 0; tmo < MMC_MAX_WRITE_POLLS; tmo++) {
  614. if ((*ifc->mmcifc_io) (0xFF) == 0xFF) {
  615. break;
  616. }
  617. if (tmo > MMC_MAX_WRITE_POLLS - MMC_MAX_WRITE_POLLS / 32) {
  618. NutSleep(1);
  619. }
  620. }
  621. if (tmo) {
  622. rc = 0;
  623. break;
  624. }
  625. #ifdef NUTDEBUG
  626. printf("[MMCWR Timeout]\n");
  627. #endif
  628. }
  629. }
  630. (*ifc->mmcifc_cs) (0);
  631. }
  632. (*ifc->mmcifc_cs) (0);
  633. /* Release mutex access. */
  634. NutEventPost(&mutex);
  635. return rc;
  636. }
  637. /*!
  638. * \brief Read data blocks from a mounted partition.
  639. *
  640. * Applications should not call this function directly, but use the
  641. * stdio interface.
  642. *
  643. * \param nfp Pointer to a ::NUTFILE structure, obtained by a previous
  644. * call to MmCardMount().
  645. * \param buffer Pointer to the data buffer to fill.
  646. * \param num Maximum number of blocks to read. However, reading
  647. * multiple blocks is not yet supported by this driver.
  648. *
  649. * \return The number of blocks actually read. A return value of -1
  650. * indicates an error.
  651. */
  652. int MmCardBlockRead(NUTFILE * nfp, void *buffer, int num)
  653. {
  654. MMCFCB *fcb = (MMCFCB *) nfp->nf_fcb;
  655. uint32_t blk = fcb->fcb_blknum;
  656. NUTDEVICE *dev = (NUTDEVICE *) nfp->nf_dev;
  657. MMCIFC *ifc = (MMCIFC *) dev->dev_icb;
  658. if ((*ifc->mmcifc_cd) () != 1) {
  659. return -1;
  660. }
  661. if (buffer == 0) {
  662. buffer = fcb->fcb_blkbuf;
  663. }
  664. /*
  665. * when using the filesystem, the sectornumbering is different then when
  666. * directly accesing the card. For example, the MBR can be found at the
  667. * sector 0 of the card, but the filesystem's first sector is the sector
  668. * where the start is of the FAT VolumeID (also called the BOOT SECTOR).
  669. * This position (or offset) is indicated by reading the partion-table,
  670. * more specific: by reading the LBA begin info.
  671. * This offset we need to add here to the sector# we get in as
  672. * parameter. This way we acces the real sector on the card.
  673. *
  674. */
  675. blk += fcb->fcb_part.part_sect_offs;
  676. #ifdef MMC_VERIFY_AFTER
  677. {
  678. int i;
  679. /*
  680. * It would be much better to verify the checksum than to re-read
  681. * and verify the data block.
  682. */
  683. for (i = 0; i < MMC_MAX_READ_RETRIES; i++) {
  684. if (MmCardReadOrVerify(ifc, blk, buffer, 0) == 0) {
  685. if (MmCardReadOrVerify(ifc, blk, buffer, 1) == 0) {
  686. return 1;
  687. }
  688. }
  689. }
  690. }
  691. #else
  692. if (MmCardReadOrVerify(ifc, blk, buffer, 0) == 0) {
  693. return 1;
  694. }
  695. #endif
  696. return -1;
  697. }
  698. /*!
  699. * \brief Write data blocks to a mounted partition.
  700. *
  701. * Applications should not call this function directly, but use the
  702. * stdio interface.
  703. *
  704. * \param nfp Pointer to a \ref NUTFILE structure, obtained by a previous
  705. * call to MmCardMount().
  706. * \param buffer Pointer to the data to be written.
  707. * \param num Maximum number of blocks to write. However, writing
  708. * multiple blocks is not yet supported by this driver.
  709. *
  710. * \return The number of blocks written. A return value of -1 indicates an
  711. * error.
  712. */
  713. int MmCardBlockWrite(NUTFILE * nfp, const void *buffer, int num)
  714. {
  715. MMCFCB *fcb = (MMCFCB *) nfp->nf_fcb;
  716. uint32_t blk = fcb->fcb_blknum;
  717. NUTDEVICE *dev = (NUTDEVICE *) nfp->nf_dev;
  718. MMCIFC *ifc = (MMCIFC *) dev->dev_icb;
  719. if ((*ifc->mmcifc_cd) () != 1) {
  720. return -1;
  721. }
  722. if (buffer == 0) {
  723. buffer = fcb->fcb_blkbuf;
  724. }
  725. blk += fcb->fcb_part.part_sect_offs;
  726. #ifdef MMC_VERIFY_AFTER
  727. {
  728. int i;
  729. for (i = 0; i < MMC_MAX_READ_RETRIES; i++) {
  730. if (MmCardWrite(ifc, blk, buffer) == 0) {
  731. if (MmCardReadOrVerify(ifc, blk, (void *) buffer, 1) == 0) {
  732. return 1;
  733. }
  734. if (MmCardReadOrVerify(ifc, blk, (void *) buffer, 1) == 0) {
  735. return 1;
  736. }
  737. }
  738. }
  739. }
  740. #else
  741. if (MmCardWrite(ifc, blk, buffer) == 0) {
  742. return 1;
  743. }
  744. #endif
  745. return -1;
  746. }
  747. #ifdef __HARVARD_ARCH__
  748. /*!
  749. * \brief Write data blocks from program space to a mounted partition.
  750. *
  751. * This function is not yet implemented and will always return -1.
  752. *
  753. * Similar to MmCardBlockWrite() except that the data is located in
  754. * program memory.
  755. *
  756. * Applications should not call this function directly, but use the
  757. * stdio interface.
  758. *
  759. * \param nfp File pointer to a previously opened device.
  760. * \param buffer Pointer to the data bytes in program space to be written.
  761. * \param num Maximum number of blocks to write. However, writing
  762. * multiple blocks is not yet supported by this driver.
  763. *
  764. * \return The number of blocks written. A return value of -1 indicates an
  765. * error.
  766. */
  767. int MmCardBlockWrite_P(NUTFILE * nfp, PGM_P buffer, int num)
  768. {
  769. return -1;
  770. }
  771. #endif
  772. /*!
  773. * \brief Mount a partition.
  774. *
  775. * Nut/OS doesn't provide specific routines for mounting. Instead routines
  776. * for opening files are used.
  777. *
  778. * Applications should not directly call this function, but use the high
  779. * level stdio routines for opening a file.
  780. *
  781. * \param dev Pointer to the MMC device.
  782. * \param name Partition number followed by a slash followed by a name
  783. * of the file system device. Both items are optional. If no
  784. * file system driver name is given, the first file system
  785. * driver found in the list of registered devices will be
  786. * used. If no partition number is specified or if partition
  787. * zero is given, the first active primary partition will be
  788. * used.
  789. * \param mode Opening mode. Currently ignored, but
  790. * \code _O_RDWR | _O_BINARY \endcode should be used for
  791. * compatibility with future enhancements.
  792. * \param acc File attributes, ignored.
  793. *
  794. * \return Pointer to a newly created file pointer to the mounted
  795. * partition or NUTFILE_EOF in case of any error.
  796. */
  797. NUTFILE *MmCardMount(NUTDEVICE * dev, const char *name, int mode, int acc)
  798. {
  799. int partno = 0;
  800. int i;
  801. NUTDEVICE *fsdev;
  802. NUTFILE *nfp;
  803. MMCFCB *fcb;
  804. DOSPART *part;
  805. MMCIFC *ifc = (MMCIFC *) dev->dev_icb;
  806. FSCP_VOL_MOUNT mparm;
  807. /* Return an error if no card is detected. */
  808. if ((*ifc->mmcifc_cd) () == 0) {
  809. errno = ENODEV;
  810. return NUTFILE_EOF;
  811. }
  812. /* Set the card in SPI mode and check for SD-HC cards. */
  813. if (MmCardInit(ifc)) {
  814. errno = ENODEV;
  815. return NUTFILE_EOF;
  816. }
  817. /* Parse the name for a partition number and a file system driver. */
  818. if (*name) {
  819. partno = atoi(name);
  820. do {
  821. name++;
  822. } while (*name && *name != '/');
  823. if (*name == '/') {
  824. name++;
  825. }
  826. }
  827. /*
  828. * Check the list of registered devices for the given name of the
  829. * files system driver. If none has been specified, get the first
  830. * file system driver in the list. Hopefully the application
  831. * registered one only.
  832. */
  833. for (fsdev = nutDeviceList; fsdev; fsdev = fsdev->dev_next) {
  834. if (*name == 0) {
  835. if (fsdev->dev_type == IFTYP_FS) {
  836. break;
  837. }
  838. } else
  839. if (strcmp(fsdev->dev_name, name) == 0) {
  840. break;
  841. }
  842. }
  843. if (fsdev == 0) {
  844. #ifdef NUTDEBUG
  845. printf("[No FSDriver]");
  846. #endif
  847. errno = ENODEV;
  848. return NUTFILE_EOF;
  849. }
  850. if ((fcb = calloc(1, sizeof(MMCFCB))) == 0) {
  851. errno = ENOMEM;
  852. return NUTFILE_EOF;
  853. }
  854. fcb->fcb_fsdev = fsdev;
  855. /* Read MBR. */
  856. if (MmCardReadOrVerify(ifc, 0, fcb->fcb_blkbuf, 0)) {
  857. free(fcb);
  858. return NUTFILE_EOF;
  859. }
  860. /* Check for the cookie at the end of this sector. */
  861. if (fcb->fcb_blkbuf[DOSPART_MAGICPOS] != 0x55 || fcb->fcb_blkbuf[DOSPART_MAGICPOS + 1] != 0xAA) {
  862. #ifdef NUTDEBUG
  863. printf("[MBR corrupted]");
  864. #endif
  865. free(fcb);
  866. return NUTFILE_EOF;
  867. }
  868. /* Check for the partition table. */
  869. if(fcb->fcb_blkbuf[DOSPART_TYPEPOS] == 'F' &&
  870. fcb->fcb_blkbuf[DOSPART_TYPEPOS + 1] == 'A' &&
  871. fcb->fcb_blkbuf[DOSPART_TYPEPOS + 2] == 'T') {
  872. /* No partition table. Assume FAT12 and 32MB size. */
  873. fcb->fcb_part.part_type = PTYPE_FAT12;
  874. fcb->fcb_part.part_sect_offs = 0;
  875. fcb->fcb_part.part_sects = 65536; /* How to find out? */
  876. }
  877. else {
  878. /* Read partition table. */
  879. part = (DOSPART *) & fcb->fcb_blkbuf[DOSPART_SECTORPOS];
  880. for (i = 1; i <= 4; i++) {
  881. if (partno) {
  882. if (i == partno) {
  883. /* Found specified partition number. */
  884. fcb->fcb_part = *part;
  885. break;
  886. }
  887. } else if (part->part_state & 0x80) {
  888. /* Located first active partition. */
  889. fcb->fcb_part = *part;
  890. break;
  891. }
  892. part++;
  893. }
  894. if (fcb->fcb_part.part_type == PTYPE_EMPTY) {
  895. free(fcb);
  896. return NUTFILE_EOF;
  897. }
  898. }
  899. if ((nfp = NutHeapAlloc(sizeof(NUTFILE))) == 0) {
  900. free(fcb);
  901. errno = ENOMEM;
  902. return NUTFILE_EOF;
  903. }
  904. nfp->nf_dev = dev;
  905. nfp->nf_fcb = fcb;
  906. /*
  907. * Mount the file system volume.
  908. */
  909. mparm.fscp_bmnt = nfp;
  910. mparm.fscp_part_type = fcb->fcb_part.part_type;
  911. if (fsdev->dev_ioctl(fsdev, FS_VOL_MOUNT, &mparm)) {
  912. MmCardUnmount(nfp);
  913. return NUTFILE_EOF;
  914. }
  915. return nfp;
  916. }
  917. /*!
  918. * \brief Unmount a previously mounted partition.
  919. *
  920. * Applications should not directly call this function, but use the high
  921. * level stdio routines for closing a previously opened file.
  922. *
  923. * \return 0 on success, -1 otherwise.
  924. */
  925. int MmCardUnmount(NUTFILE * nfp)
  926. {
  927. int rc = -1;
  928. if (nfp) {
  929. MMCFCB *fcb = (MMCFCB *) nfp->nf_fcb;
  930. if (fcb) {
  931. NUTDEVICE *dev = (NUTDEVICE *) nfp->nf_dev;
  932. MMCIFC *ifc = (MMCIFC *) dev->dev_icb;
  933. if ((*ifc->mmcifc_cd) () == 1) {
  934. rc = fcb->fcb_fsdev->dev_ioctl(fcb->fcb_fsdev, FS_VOL_UNMOUNT, NULL);
  935. }
  936. free(fcb);
  937. }
  938. free(nfp);
  939. }
  940. return rc;
  941. }
  942. /*!
  943. * \brief Retrieve contents of a card register.
  944. *
  945. * \param ifc Specifies the hardware interface.
  946. * \param cmd This command is sent to the card to retrieve the contents
  947. * of a specific register.
  948. * \param rbp Pointer to the buffer that receives the register contents.
  949. * \param siz Size of the specified register.
  950. *
  951. * \return 0 on success, -1 otherwise.
  952. */
  953. static int MmCardGetReg(MMCIFC * ifc, uint8_t cmd, uint8_t * rbp, int siz)
  954. {
  955. int rc = -1;
  956. int retries = MMC_MAX_REG_POLLS;
  957. int i;
  958. /* Gain mutex access. */
  959. NutEventWait(&mutex, 0);
  960. while (retries--) {
  961. /* Send the command to the card. This will select the card. */
  962. MmCardTxCmd(ifc, cmd, 0);
  963. /* Wait for OK response from the card. */
  964. if (MmCardRxR1(ifc) == 0x00) {
  965. /* Wait for data from the card. */
  966. if (MmCardRxR1(ifc) == 0xFE) {
  967. for (i = 0; i < siz; i++) {
  968. *rbp++ = (*ifc->mmcifc_io) (0xFF);
  969. }
  970. /* Ignore the CRC. */
  971. (*ifc->mmcifc_io) (0xFF);
  972. (*ifc->mmcifc_io) (0xFF);
  973. /* De-activate card selection. */
  974. (*ifc->mmcifc_cs) (0);
  975. /* Return a positive result. */
  976. rc = 0;
  977. break;
  978. }
  979. }
  980. /* De-activate card selection. */
  981. (*ifc->mmcifc_cs) (0);
  982. }
  983. /* Release mutex access. */
  984. NutEventPost(&mutex);
  985. return rc;
  986. }
  987. /*!
  988. * \brief Perform MMC control functions.
  989. *
  990. * This function is called by the ioctl() function of the C runtime
  991. * library. Applications should not directly call this function.
  992. *
  993. * \todo Card change detection should verify the serial card number.
  994. *
  995. * \param dev Identifies the device that receives the device-control
  996. * function.
  997. * \param req Requested control function. May be set to one of the
  998. * following constants:
  999. * - \ref NUTBLKDEV_MEDIACHANGE
  1000. * - \ref NUTBLKDEV_INFO
  1001. * - \ref NUTBLKDEV_SEEK
  1002. * - \ref MMCARD_GETCID
  1003. * - \ref MMCARD_GETCSD
  1004. *
  1005. * \param conf Points to a buffer that contains any data required for
  1006. * the given control function or receives data from that
  1007. * function.
  1008. * \return 0 on success, -1 otherwise.
  1009. */
  1010. int MmCardIOCtl(NUTDEVICE * dev, int req, void *conf)
  1011. {
  1012. int rc = 0;
  1013. MMCIFC *ifc = (MMCIFC *) dev->dev_icb;
  1014. switch (req) {
  1015. case NUTBLKDEV_MEDIAAVAIL:
  1016. {
  1017. int *flg = (int *) conf;
  1018. *flg = (*ifc->mmcifc_cd) ();
  1019. }
  1020. break;
  1021. case NUTBLKDEV_MEDIACHANGE:
  1022. {
  1023. int *flg = (int *) conf;
  1024. if ((*ifc->mmcifc_cd) () != 1) {
  1025. *flg = 1;
  1026. } else {
  1027. *flg = 0;
  1028. }
  1029. }
  1030. break;
  1031. case NUTBLKDEV_INFO:
  1032. {
  1033. BLKPAR_INFO *par = (BLKPAR_INFO *) conf;
  1034. MMCFCB *fcb = (MMCFCB *) par->par_nfp->nf_fcb;
  1035. /*
  1036. * note that we don't read the card's CSD-register for this info,
  1037. * in stead, we use the info that we found in the partition table of
  1038. * the formatted card.
  1039. */
  1040. par->par_nblks = fcb->fcb_part.part_sects;
  1041. par->par_blksz = MMC_BLOCK_SIZE;
  1042. par->par_blkbp = fcb->fcb_blkbuf;
  1043. }
  1044. break;
  1045. case NUTBLKDEV_SEEK:
  1046. {
  1047. BLKPAR_SEEK *par = (BLKPAR_SEEK *) conf;
  1048. MMCFCB *fcb = (MMCFCB *) par->par_nfp->nf_fcb;
  1049. fcb->fcb_blknum = par->par_blknum;
  1050. }
  1051. break;
  1052. case MMCARD_GETSTATUS:
  1053. {
  1054. uint16_t *s = (uint16_t *) conf;
  1055. /* Gain mutex access. */
  1056. NutEventWait(&mutex, 0);
  1057. MmCardTxCmd(ifc, MMCMD_SEND_STATUS, 0);
  1058. *s = MmCardRxR2(ifc);
  1059. /* Release mutex access. */
  1060. NutEventPost(&mutex);
  1061. }
  1062. break;
  1063. case MMCARD_GETOCR:
  1064. /* Gain mutex access. */
  1065. NutEventWait(&mutex, 0);
  1066. MmCardTxCmd(ifc, MMCMD_READ_OCR, 0);
  1067. if (MmCardRxR3(ifc, (uint32_t *) conf) != MMR1_IDLE_STATE) {
  1068. rc = -1;
  1069. }
  1070. /* Release mutex access. */
  1071. NutEventPost(&mutex);
  1072. break;
  1073. case MMCARD_GETCID:
  1074. rc = MmCardGetReg(ifc, MMCMD_SEND_CID, (uint8_t *) conf, sizeof(MMC_CID));
  1075. break;
  1076. case MMCARD_GETCSD:
  1077. rc = MmCardGetReg(ifc, MMCMD_SEND_CSD, (uint8_t *) conf, sizeof(MMC_CSD));
  1078. break;
  1079. default:
  1080. rc = -1;
  1081. break;
  1082. }
  1083. return rc;
  1084. }
  1085. /*!
  1086. * \brief Initialize high level MMC driver.
  1087. *
  1088. * Applications should not directly call this function. It is
  1089. * automatically executed during during device registration by
  1090. * NutRegisterDevice().
  1091. *
  1092. * \param dev Identifies the device to initialize.
  1093. *
  1094. * \return Always zero.
  1095. */
  1096. int MmCardDevInit(NUTDEVICE * dev)
  1097. {
  1098. return 0;
  1099. }
  1100. /*@}*/