mmcsd_spi.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068
  1. /****************************************************************************
  2. * drivers/mmcsd/mmcsd_spi.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #if defined (CONFIG_MMCSD) && defined (CONFIG_MMCSD_SPI)
  25. #include <sys/types.h>
  26. #include <inttypes.h>
  27. #include <stdint.h>
  28. #include <stdbool.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <assert.h>
  33. #include <errno.h>
  34. #include <debug.h>
  35. #include <unistd.h>
  36. #include <nuttx/arch.h>
  37. #include <nuttx/clock.h>
  38. #include <nuttx/signal.h>
  39. #include <nuttx/spi/spi.h>
  40. #include <nuttx/fs/fs.h>
  41. #include <nuttx/mmcsd.h>
  42. #include "mmcsd_spi.h"
  43. #include "mmcsd_csd.h"
  44. #include "mmcsd.h"
  45. /****************************************************************************
  46. * Pre-processor Definitions
  47. ****************************************************************************/
  48. /* Configuration ************************************************************/
  49. #if !defined(CONFIG_MMCSD_READONLY)
  50. # define MMCSD_MODE 0666
  51. #else
  52. # define MMCSD_MODE 0444
  53. #endif
  54. #ifndef CONFIG_MMCSD_SECTOR512
  55. # define CONFIG_MMCSD_SECTOR512 /* Force 512 byte sectors on all cards */
  56. #endif
  57. /* Slot struct info *********************************************************/
  58. /* Slot status definitions */
  59. #define MMCSD_SLOTSTATUS_NOTREADY 0x01 /* Card not initialized */
  60. #define MMCSD_SLOTSTATUS_NODISK 0x02 /* No card in the slot */
  61. #define MMCSD_SLOTSTATUS_WRPROTECT 0x04 /* Card is write protected */
  62. #define MMCSD_SLOTSTATUS_MEDIACHGD 0x08 /* Media changed in slot */
  63. /* Values in the MMC/SD command table ***************************************/
  64. /* These define the value returned by the MMC/SD command */
  65. #define MMCSD_CMDRESP_R1 0
  66. #define MMCSD_CMDRESP_R1B 1
  67. #define MMCSD_CMDRESP_R2 2
  68. #define MMCSD_CMDRESP_R3 3
  69. #define MMCSD_CMDRESP_R7 4
  70. #ifdef CONFIG_MMCSD_SECTOR512
  71. # define SECTORSIZE(s) (512)
  72. #else
  73. # define SECTORSIZE(s) ((s)->sectorsize)
  74. #endif
  75. /* Time delays in units of the system clock. CLK_TCK is the number of clock
  76. * ticks per second.
  77. */
  78. #define MMCSD_DELAY_10MS (CLK_TCK/100 + 1)
  79. #define MMCSD_DELAY_50MS (CLK_TCK/20 + 1)
  80. #define MMCSD_DELAY_100MS (CLK_TCK/10 + 1)
  81. #define MMCSD_DELAY_250MS (CLK_TCK/4 + 1)
  82. #define MMCSD_DELAY_500MS (CLK_TCK/2 + 1)
  83. #define MMCSD_DELAY_1SEC (CLK_TCK + 1)
  84. #define MMCSD_DELAY_10SEC (10 * CLK_TCK + 1)
  85. #define ELAPSED_TIME(t) (clock_systime_ticks()-(t))
  86. #define START_TIME (clock_systime_ticks())
  87. /* SD read timeout: ~100msec, Write Time out ~250ms. Units of clock ticks */
  88. #define SD_READACCESS MMCSD_DELAY_100MS
  89. #define SD_WRITEACCESS MMCSD_DELAY_250MS
  90. /****************************************************************************
  91. * Private Types
  92. ****************************************************************************/
  93. /* This structure represents the state of one card slot */
  94. struct mmcsd_slot_s
  95. {
  96. FAR struct spi_dev_s *spi; /* SPI port bound to this slot */
  97. sem_t sem; /* Assures mutually exclusive access to card and SPI */
  98. uint8_t state; /* State of the slot (see MMCSD_SLOTSTATUS_* definitions) */
  99. uint8_t type; /* Disk type */
  100. uint8_t csd[16]; /* Copy of card CSD */
  101. #ifndef CONFIG_MMCSD_SECTOR512
  102. uint16_t sectorsize; /* Media block size (in bytes) */
  103. #endif
  104. uint32_t nsectors; /* Number of blocks on the media */
  105. uint32_t taccess; /* Card access time */
  106. uint32_t twrite; /* Card write time */
  107. uint32_t ocr; /* Last 4 bytes of OCR (R3) */
  108. uint32_t r7; /* Last 4 bytes of R7 */
  109. uint32_t spispeed; /* Speed to use for SPI in data mode */
  110. };
  111. struct mmcsd_cmdinfo_s
  112. {
  113. uint8_t cmd;
  114. uint8_t resp;
  115. uint8_t chksum;
  116. };
  117. /****************************************************************************
  118. * Private Function Prototypes
  119. ****************************************************************************/
  120. /* Misc *********************************************************************/
  121. static int mmcsd_semtake(FAR struct mmcsd_slot_s *slot);
  122. static void mmcsd_semgive(FAR struct mmcsd_slot_s *slot);
  123. /* Card SPI interface *******************************************************/
  124. static int mmcsd_waitready(FAR struct mmcsd_slot_s *slot);
  125. static uint32_t mmcsd_sendcmd(FAR struct mmcsd_slot_s *slot,
  126. const struct mmcsd_cmdinfo_s *cmd, uint32_t arg);
  127. static void mmcsd_setblklen(FAR struct mmcsd_slot_s *slot,
  128. uint32_t length);
  129. static uint32_t mmcsd_nsac(FAR struct mmcsd_slot_s *slot, uint8_t *csd,
  130. uint32_t frequency);
  131. static uint32_t mmcsd_taac(FAR struct mmcsd_slot_s *slot, uint8_t *csd);
  132. static void mmcsd_decodecsd(FAR struct mmcsd_slot_s *slot, uint8_t *csd);
  133. static void mmcsd_checkwrprotect(FAR struct mmcsd_slot_s *slot,
  134. uint8_t *csd);
  135. static int mmcsd_getcardinfo(FAR struct mmcsd_slot_s *slot,
  136. uint8_t *buffer, const struct mmcsd_cmdinfo_s *cmd);
  137. #define mmcsd_getcsd(slot, csd) mmcsd_getcardinfo(slot, csd, &g_cmd9);
  138. #define mmcsd_getcid(slot, cid) mmcsd_getcardinfo(slot, cid, &g_cmd10);
  139. static int mmcsd_recvblock(FAR struct mmcsd_slot_s *slot,
  140. uint8_t *buffer, int nbytes);
  141. #if !defined(CONFIG_MMCSD_READONLY)
  142. static int mmcsd_xmitblock(FAR struct mmcsd_slot_s *slot,
  143. const uint8_t *buffer, int nbytes, uint8_t token);
  144. #endif
  145. /* Block driver interfaces **************************************************/
  146. static int mmcsd_open(FAR struct inode *inode);
  147. static int mmcsd_close(FAR struct inode *inode);
  148. static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer,
  149. blkcnt_t start_sector, unsigned int nsectors);
  150. #if !defined(CONFIG_MMCSD_READONLY)
  151. static ssize_t mmcsd_write(FAR struct inode *inode,
  152. const unsigned char *buffer, blkcnt_t start_sector,
  153. unsigned int nsectors);
  154. #endif
  155. static int mmcsd_geometry(FAR struct inode *inode,
  156. struct geometry *geometry);
  157. /* Initialization ***********************************************************/
  158. static int mmcsd_mediainitialize(FAR struct mmcsd_slot_s *slot);
  159. static void mmcsd_mediachanged(void *arg);
  160. /****************************************************************************
  161. * Private Data
  162. ****************************************************************************/
  163. /* Driver state *************************************************************/
  164. /* These are the lock driver methods supported by this file */
  165. static const struct block_operations g_bops =
  166. {
  167. mmcsd_open, /* open */
  168. mmcsd_close, /* close */
  169. mmcsd_read, /* read */
  170. #if !defined(CONFIG_MMCSD_READONLY)
  171. mmcsd_write, /* write */
  172. #else
  173. NULL, /* write */
  174. #endif
  175. mmcsd_geometry, /* geometry */
  176. NULL /* ioctl */
  177. };
  178. /* A slot structure allocated for each configured slot */
  179. static struct mmcsd_slot_s g_mmcsdslot[CONFIG_MMCSD_NSLOTS];
  180. /* Timing *******************************************************************/
  181. /* We will use the TRAN_SPEED from the CSD to determine the maximum SPI
  182. * clocking (TRAN_SPEED defines the maximum transfer rate per bit per data
  183. * line).
  184. *
  185. * The CSD TRAN_SPEED is provided as a 3 bit rate unit (RU) and a 4 bit time
  186. * value (TU). We need the transfer frequency which is: RU*TU bits/sec
  187. *
  188. * g_transpeedru holds RU/10 and g_transpeedtu holds TU*10 so that the
  189. * correct value is returned in the product
  190. */
  191. static const uint32_t g_transpeedru[8] =
  192. {
  193. 10000, /* 0: 100 Kbit/sec / 10 */
  194. 100000, /* 1: 1 Mbit/sec / 10 */
  195. 1000000, /* 2: 10 Mbit/sec / 10 */
  196. 10000000, /* 3: 100 Mbit/sec / 10 */
  197. 0, 0, 0, 0 /* 4-7: Reserved values */
  198. };
  199. static const uint32_t g_transpeedtu[16] =
  200. {
  201. 0, 10, 12, 13, /* 0-3: Reserved, 1.0, 1.1, 1.2, 1.3 */
  202. 15, 20, 25, 30, /* 4-7: 1.5, 2.0, 2.5, 3.0 */
  203. 35, 40, 45, 50, /* 8-11: 3.5, 4.0, 4.5, 5.0 */
  204. 55, 60, 70, 80, /* 12-15: 5.5, 6.0, 7.0, 8.0 */
  205. };
  206. /* The TAAC defines the asynchronous part of the data access time. The
  207. * read access time the sum of the TAAC and the NSAC. These define the
  208. * time from the end bit of the read command to start bit of the data block.
  209. *
  210. * The TAAC consists of a 3-bit time unit (TU) and a 4-bit time value (TV).
  211. * TAAC is in units of time; NSAC is in units of SPI clocks.
  212. * The access time we need is then given by:
  213. *
  214. * taccess = TU*TV + NSAC/spifrequency
  215. *
  216. * g_taactu holds TU in units of nanoseconds and microseconds (you have to
  217. * use the index to distinguish). g_taactv holds TV with 8-bits of
  218. * fraction.
  219. */
  220. #define MAX_USTUNDX 2
  221. static const uint16_t g_taactu[8] =
  222. {
  223. /* Units of nanoseconds */
  224. 1, /* 0: 1 ns */
  225. 10, /* 1: 10 ns */
  226. 100, /* 2: 100 ns */
  227. /* Units of microseconds */
  228. 1, /* 3: 1 us 1,000 ns */
  229. 10, /* 4: 10 us 10,000 ns */
  230. 100, /* 5: 100 us 100,000 ns */
  231. 1000, /* 6: 1 ms 1,000,000 ns */
  232. 10000, /* 7: 10 ms 10,000,000 ns */
  233. };
  234. static const uint16_t g_taactv[] =
  235. {
  236. 0x000, 0x100, 0x133, 0x14d, /* 0-3: Reserved, 1.0, 1.2, 1.3 */
  237. 0x180, 0x200, 0x280, 0x300, /* 4-7: 1.5, 2.0, 2.5, 3.0 */
  238. 0x380, 0x400, 0x480, 0x500, /* 8-11: 3.5, 4.0, 4.5, 5.0 */
  239. 0x580, 0x600, 0x700, 0x800 /* 12-15: 5.5, 6.0, 7.0, 8.0 */
  240. };
  241. /* Commands *****************************************************************/
  242. static const struct mmcsd_cmdinfo_s g_cmd0 =
  243. {
  244. CMD0, MMCSD_CMDRESP_R1, 0x95
  245. };
  246. static const struct mmcsd_cmdinfo_s g_cmd1 =
  247. {
  248. CMD1, MMCSD_CMDRESP_R1, 0xff
  249. };
  250. static const struct mmcsd_cmdinfo_s g_cmd8 =
  251. {
  252. CMD8, MMCSD_CMDRESP_R7, 0x87
  253. };
  254. static const struct mmcsd_cmdinfo_s g_cmd9 =
  255. {
  256. CMD9, MMCSD_CMDRESP_R1, 0xff
  257. };
  258. #if 0 /* Not used */
  259. static const struct mmcsd_cmdinfo_s g_cmd10 =
  260. {
  261. CMD10, MMCSD_CMDRESP_R1, 0xff
  262. };
  263. #endif
  264. static const struct mmcsd_cmdinfo_s g_cmd12 =
  265. {
  266. CMD12, MMCSD_CMDRESP_R1, 0xff
  267. };
  268. static const struct mmcsd_cmdinfo_s g_cmd16 =
  269. {
  270. CMD16, MMCSD_CMDRESP_R1, 0xff
  271. };
  272. static const struct mmcsd_cmdinfo_s g_cmd17 =
  273. {
  274. CMD17, MMCSD_CMDRESP_R1, 0xff
  275. };
  276. static const struct mmcsd_cmdinfo_s g_cmd18 =
  277. {
  278. CMD18, MMCSD_CMDRESP_R1, 0xff
  279. };
  280. #if !defined(CONFIG_MMCSD_READONLY)
  281. static const struct mmcsd_cmdinfo_s g_cmd24 =
  282. {
  283. CMD24, MMCSD_CMDRESP_R1, 0xff
  284. };
  285. static const struct mmcsd_cmdinfo_s g_cmd25 =
  286. {
  287. CMD25, MMCSD_CMDRESP_R1, 0xff
  288. };
  289. #endif
  290. static const struct mmcsd_cmdinfo_s g_cmd55 =
  291. {
  292. CMD55, MMCSD_CMDRESP_R1, 0xff
  293. };
  294. static const struct mmcsd_cmdinfo_s g_cmd58 =
  295. {
  296. CMD58, MMCSD_CMDRESP_R3, 0xff
  297. };
  298. #if !defined(CONFIG_MMCSD_READONLY)
  299. static const struct mmcsd_cmdinfo_s g_acmd23 =
  300. {
  301. ACMD23, MMCSD_CMDRESP_R1, 0xff
  302. };
  303. #endif
  304. static const struct mmcsd_cmdinfo_s g_acmd41 =
  305. {
  306. ACMD41, MMCSD_CMDRESP_R1, 0xff
  307. };
  308. /****************************************************************************
  309. * Private Functions
  310. ****************************************************************************/
  311. /****************************************************************************
  312. * Name: mmcsd_semtake
  313. ****************************************************************************/
  314. static int mmcsd_semtake(FAR struct mmcsd_slot_s *slot)
  315. {
  316. int ret;
  317. /* Get exclusive access to the MMC/SD device (possibly unnecessary if
  318. * SPI_LOCK is also implemented as a semaphore).
  319. */
  320. ret = nxsem_wait_uninterruptible(&slot->sem);
  321. if (ret < 0)
  322. {
  323. return ret;
  324. }
  325. /* Get exclusive access to the SPI bus (if necessary) */
  326. SPI_LOCK(slot->spi, true);
  327. /* Set the frequency, bit width and mode, as some other driver could have
  328. * changed those since the last time that we had the SPI bus.
  329. */
  330. SPI_SETMODE(slot->spi, CONFIG_MMCSD_SPIMODE);
  331. SPI_SETBITS(slot->spi, 8);
  332. SPI_HWFEATURES(slot->spi, 0);
  333. SPI_SETFREQUENCY(slot->spi, slot->spispeed);
  334. return ret;
  335. }
  336. /****************************************************************************
  337. * Name: mmcsd_semgive
  338. ****************************************************************************/
  339. static void mmcsd_semgive(FAR struct mmcsd_slot_s *slot)
  340. {
  341. /* Relinquish the lock on the SPI bus */
  342. /* The card may need up to 8 SCLK cycles to sample the CS status
  343. * and release the MISO line.
  344. */
  345. SPI_SEND(slot->spi, 0xff);
  346. /* Relinquish exclusive access to the SPI bus */
  347. SPI_LOCK(slot->spi, false);
  348. /* Relinquish the lock on the MMC/SD device */
  349. nxsem_post(&slot->sem);
  350. }
  351. /****************************************************************************
  352. * Name: mmcsd_waitready
  353. *
  354. * Description:
  355. * Wait until the card is no longer busy
  356. *
  357. * Assumptions:
  358. * MMC/SD card already selected
  359. *
  360. ****************************************************************************/
  361. static int mmcsd_waitready(FAR struct mmcsd_slot_s *slot)
  362. {
  363. FAR struct spi_dev_s *spi = slot->spi;
  364. uint8_t response;
  365. clock_t start;
  366. clock_t elapsed;
  367. /* Wait until the card is no longer busy (up to 500MS) */
  368. start = START_TIME;
  369. do
  370. {
  371. response = SPI_SEND(spi, 0xff);
  372. if (response == 0xff)
  373. {
  374. return OK;
  375. }
  376. elapsed = ELAPSED_TIME(start);
  377. if (elapsed > MMCSD_DELAY_10MS)
  378. {
  379. /* Give other threads time to run */
  380. nxsig_usleep(10000);
  381. }
  382. }
  383. while (elapsed < MMCSD_DELAY_500MS);
  384. finfo("Card still busy, last response: %02x\n", response);
  385. return -EBUSY;
  386. }
  387. /****************************************************************************
  388. * Name: mmcsd_sendcmd
  389. *
  390. * Description:
  391. * Send a command to MMC
  392. *
  393. * Assumptions:
  394. * MMC/SD card already selected
  395. *
  396. ****************************************************************************/
  397. static uint32_t mmcsd_sendcmd(FAR struct mmcsd_slot_s *slot,
  398. FAR const struct mmcsd_cmdinfo_s *cmd,
  399. uint32_t arg)
  400. {
  401. FAR struct spi_dev_s *spi = slot->spi;
  402. uint32_t result;
  403. uint8_t response = 0xff;
  404. int ret;
  405. int i;
  406. /* Wait until the card is not busy. Some SD cards will not enter the IDLE
  407. * state until CMD0 is sent for the first time, switching the card to SPI
  408. * mode. Having a pull-up resistor on MISO may avoid this problem, but
  409. * this check makes it work also without the pull-up.
  410. */
  411. ret = mmcsd_waitready(slot);
  412. if (ret != OK && cmd != &g_cmd0)
  413. {
  414. return ret;
  415. }
  416. /* Send command code */
  417. SPI_SEND(spi, cmd->cmd);
  418. /* Send command's arguments (should be zero if there are no arguments) */
  419. SPI_SEND(spi, (arg >> 24) & 0xff);
  420. SPI_SEND(spi, (arg >> 16) & 0xff);
  421. SPI_SEND(spi, (arg >> 8) & 0xff);
  422. SPI_SEND(spi, arg & 0xff);
  423. /* Send CRC if needed. The SPI interface is initialized in non-protected
  424. * mode. However, the reset command (CMD0) and CMD8 are received by the
  425. * card while it is still in SD mode and, therefore, must have a valid
  426. * CRC field.
  427. */
  428. SPI_SEND(spi, cmd->chksum);
  429. /* Skip stuff byte on CMD12 */
  430. if (cmd->cmd == CMD12)
  431. {
  432. SPI_SEND(spi, 0xff);
  433. }
  434. /* Get the response to the command. A valid response will have bit7=0.
  435. * Usually, the non-response is 0xff, but I have seen 0xc0 too.
  436. */
  437. for (i = 0; i < 9 && (response & 0x80) != 0; i++)
  438. {
  439. response = SPI_SEND(spi, 0xff);
  440. }
  441. if ((response & 0x80) != 0)
  442. {
  443. ferr("ERROR: Failed: i=%d response=%02x\n", i, response);
  444. return (uint32_t)-1;
  445. }
  446. /* Interpret the response according to the command */
  447. result = response;
  448. switch (cmd->resp)
  449. {
  450. /* The R1B response is two bytes long */
  451. case MMCSD_CMDRESP_R1B:
  452. {
  453. uint32_t busy = 0;
  454. clock_t start;
  455. clock_t elapsed;
  456. start = START_TIME;
  457. do
  458. {
  459. busy = SPI_SEND(spi, 0xff);
  460. elapsed = ELAPSED_TIME(start);
  461. }
  462. while (elapsed < slot->twrite && busy != 0xff);
  463. if (busy != 0xff)
  464. {
  465. ferr("ERROR: Failed: card still busy (%02" PRIx32 ")\n", busy);
  466. return (uint32_t)-1;
  467. }
  468. finfo("CMD%d[%08" PRIx32 "] R1B=%02" PRIx8 "\n",
  469. cmd->cmd & 0x3f, arg, response);
  470. }
  471. break;
  472. /* The R1 response is a single byte */
  473. case MMCSD_CMDRESP_R1:
  474. {
  475. finfo("CMD%d[%08" PRIx32 "] R1=%02" PRIx8 "\n",
  476. cmd->cmd & 0x3f, arg, response);
  477. }
  478. break;
  479. /* The R2 response is two bytes long */
  480. case MMCSD_CMDRESP_R2:
  481. {
  482. result = ((uint32_t)(response & 0xff) << 8);
  483. result |= SPI_SEND(spi, 0xff) & 0xff;
  484. finfo("CMD%d[%08" PRIx32 "] R2=%04" PRIx32 "\n",
  485. cmd->cmd & 0x3f, arg, result);
  486. }
  487. break;
  488. /* The R3 response is 5 bytes long. The first byte is identical to R1. */
  489. case MMCSD_CMDRESP_R3:
  490. {
  491. slot->ocr = ((uint32_t)(SPI_SEND(spi, 0xff) & 0xff) << 24);
  492. slot->ocr |= ((uint32_t)(SPI_SEND(spi, 0xff) & 0xff) << 16);
  493. slot->ocr |= ((uint32_t)(SPI_SEND(spi, 0xff) & 0xff) << 8);
  494. slot->ocr |= SPI_SEND(spi, 0xff) & 0xff;
  495. finfo("CMD%d[%08" PRIx32 "] R1=%02" PRIx8 " OCR=%08" PRIx32 "\n",
  496. cmd->cmd & 0x3f, arg, response, slot->ocr);
  497. }
  498. break;
  499. /* The R7 response is 5 bytes long. The first byte is identical to R1. */
  500. case MMCSD_CMDRESP_R7:
  501. default:
  502. {
  503. slot->r7 = ((uint32_t)(SPI_SEND(spi, 0xff) & 0xff) << 24);
  504. slot->r7 |= ((uint32_t)(SPI_SEND(spi, 0xff) & 0xff) << 16);
  505. slot->r7 |= ((uint32_t)(SPI_SEND(spi, 0xff) & 0xff) << 8);
  506. slot->r7 |= SPI_SEND(spi, 0xff) & 0xff;
  507. finfo("CMD%d[%08" PRIx32 "] R1=%02" PRIx8 " R7=%08" PRIx32 "\n",
  508. cmd->cmd & 0x3f, arg, response, slot->r7);
  509. }
  510. break;
  511. }
  512. return result;
  513. }
  514. /****************************************************************************
  515. * Name: mmcsd_setblklen
  516. *
  517. * Description:
  518. * Set block length
  519. *
  520. * Assumptions:
  521. * MMC/SD card already selected
  522. *
  523. ****************************************************************************/
  524. static void mmcsd_setblklen(FAR struct mmcsd_slot_s *slot, uint32_t length)
  525. {
  526. uint32_t response;
  527. finfo("Set block length to %" PRId32 "\n", length);
  528. response = mmcsd_sendcmd(slot, &g_cmd16, length);
  529. if (response != MMCSD_SPIR1_OK)
  530. {
  531. ferr("ERROR: Failed to set block length: %02" PRIx32 "\n", response);
  532. }
  533. }
  534. /****************************************************************************
  535. * Name: mmcsd_nsac
  536. *
  537. * Description: Convert the value of the NSAC to microseconds
  538. *
  539. ****************************************************************************/
  540. static uint32_t mmcsd_nsac(FAR struct mmcsd_slot_s *slot, uint8_t *csd,
  541. uint32_t frequency)
  542. {
  543. /* NSAC is 8-bits wide and is in units of 100 clock cycles. Therefore,
  544. * the maximum value is 25.5K clock cycles.
  545. */
  546. uint32_t nsac = MMCSD_CSD_NSAC(csd) * ((uint32_t)100 * 1000);
  547. uint32_t fhkz = (frequency + 500) / 1000;
  548. return (nsac + (fhkz >> 1)) / fhkz;
  549. }
  550. /****************************************************************************
  551. * Name: mmcsd_taac
  552. *
  553. * Description: Convert the value of the TAAC to microseconds
  554. *
  555. ****************************************************************************/
  556. static uint32_t mmcsd_taac(FAR struct mmcsd_slot_s *slot, uint8_t *csd)
  557. {
  558. int tundx;
  559. /* The TAAC consists of a 3-bit time unit (TU) and a 4-bit time value (TV).
  560. * TAAC is in units of time; NSAC is in units of SPI clocks.
  561. * The access time we need is then given by:
  562. *
  563. * taccess = TU*TV + NSAC/spifrequency
  564. *
  565. * g_taactu holds TU in units of nanoseconds and microseconds (you have to
  566. * use the index to distinguish. g_taactv holds TV with 8-bits of
  567. * fraction.
  568. */
  569. tundx = MMCSD_CSD_TAAC_TIMEUNIT(csd);
  570. if (tundx <= MAX_USTUNDX)
  571. {
  572. /* The maximum value of the nanosecond TAAC is 800 ns. The rounded
  573. * answer in microseconds will be at most 1.
  574. */
  575. return 1;
  576. }
  577. else
  578. {
  579. /* Return the answer in microseconds */
  580. return (g_taactu[tundx] * g_taactv[MMCSD_CSD_TAAC_TIMEVALUE(csd)] +
  581. 0x80) >> 8;
  582. }
  583. }
  584. /****************************************************************************
  585. * Name: mmcsd_decodecsd
  586. *
  587. * Description:
  588. *
  589. ****************************************************************************/
  590. static void mmcsd_decodecsd(FAR struct mmcsd_slot_s *slot, uint8_t *csd)
  591. {
  592. FAR struct spi_dev_s *spi = slot->spi;
  593. uint32_t maxfrequency;
  594. uint32_t frequency;
  595. uint32_t readbllen;
  596. uint32_t csizemult;
  597. uint32_t csize;
  598. /* Calculate the SPI max clock frequency */
  599. maxfrequency =
  600. g_transpeedtu[MMCSD_CSD_TRANSPEED_TIMEVALUE(csd)] *
  601. g_transpeedru[MMCSD_CSD_TRANSPEED_TRANSFERRATEUNIT(csd)];
  602. /* Clip the max frequency to account for board limitations */
  603. frequency = maxfrequency;
  604. if (frequency > CONFIG_MMCSD_SPICLOCK)
  605. {
  606. frequency = CONFIG_MMCSD_SPICLOCK;
  607. }
  608. /* Set the actual SPI frequency as close as possible to the max frequency */
  609. slot->spispeed = frequency;
  610. frequency = SPI_SETFREQUENCY(spi, frequency);
  611. /* Now determine the delay to access data */
  612. if (slot->type == MMCSD_CARDTYPE_MMC)
  613. {
  614. /* The TAAC consists of a 3-bit time unit (TU) and a 4-bit time value
  615. * (TV). TAAC is in units of time; NSAC is in units of SPI clocks.
  616. * The access time we need is then given by:
  617. *
  618. * taccess = TU*TV + NSAC/spifrequency
  619. *
  620. * Example: TAAC = 1.5 ms, NSAC = 0, r2wfactor = 4, CLK_TCK=100
  621. * taccessus = 1,500uS
  622. * taccess = (1,500 * 100) / 100,000) + 1 = 2
  623. * (ideal, 1.5)
  624. * twrite = (1,500 * 4 * 100) / 100,000) + 1 = 7
  625. * (ideal 6.0)
  626. *
  627. * First get the access time in microseconds
  628. */
  629. uint32_t taccessus = mmcsd_taac(slot, csd) +
  630. mmcsd_nsac(slot, csd, frequency);
  631. /* Then convert to system clock ticks. The maximum read access is 10
  632. * times the tacc value:
  633. *
  634. * taccess = 10 * (taccessus / 1,000,000) * CLK_TCK
  635. */
  636. slot->taccess = (taccessus * CLK_TCK) / 100000 + 1;
  637. /* NOTE that we add one to taccess to assure that we wait at least
  638. * this time. The write access time is larger by the R2WFACTOR:
  639. */
  640. slot->taccess = (taccessus * MMCSD_CSD_R2WFACTOR(csd) * CLK_TCK) /
  641. 100000 + 1;
  642. }
  643. else
  644. {
  645. /* For SD, the average is still given by the TAAC+NSAC, but the
  646. * maximum are the constants 100 and 250MS
  647. */
  648. slot->taccess = SD_READACCESS;
  649. slot->twrite = SD_WRITEACCESS;
  650. }
  651. finfo("SPI Frequency\n");
  652. finfo(" Maximum: %" PRId32 " Hz\n", maxfrequency);
  653. finfo(" Actual: %" PRId32 " Hz\n", frequency);
  654. finfo("Read access time: %" PRId32 " ticks\n", slot->taccess);
  655. finfo("Write access time: %" PRId32 " ticks\n", slot->twrite);
  656. /* Get the physical geometry of the card: sector size and number of
  657. * sectors. The card's total capacity is computed from
  658. *
  659. * capacity = BLOCKNR * BLOCK_LEN
  660. * BLOCKNR = (C_SIZE+1)*MULT
  661. * MULT = 2**(C_SIZE_MULT+2) (C_SIZE_MULT < 8)
  662. * BLOCK_LEN = 2**READ_BL_LEN (READ_BL_LEN < 12)
  663. *
  664. * Or
  665. *
  666. * capacity = ((C_SIZE+1) << (READD_BL_LEN + C_SIZE_MULT + 2))
  667. *
  668. * In units of the sector size (1 << READ_BL_LEN), then simplifies to
  669. *
  670. * nsectors = ((C_SIZE+1) << (C_SIZE_MULT + 2))
  671. */
  672. if (MMCSD_CSD_CSDSTRUCT(csd) != 0)
  673. {
  674. /* SDC structure ver 2.xx */
  675. /* Note: On SD card WRITE_BL_LEN is always the same as READ_BL_LEN */
  676. readbllen = SD20_CSD_READBLLEN(csd);
  677. csizemult = SD20_CSD_CSIZEMULT(csd) + 2;
  678. csize = SD20_CSD_CSIZE(csd) + 1;
  679. }
  680. else
  681. {
  682. /* MMC or SD structure ver 1.xx */
  683. /* Note: On SD card WRITE_BL_LEN is always the same as READ_BL_LEN */
  684. readbllen = MMCSD_CSD_READBLLEN(csd);
  685. csizemult = MMCSD_CSD_CSIZEMULT(csd) + 2;
  686. csize = MMCSD_CSD_CSIZE(csd) + 1;
  687. }
  688. /* SDHC ver2.x cards have fixed block transfer size of 512 bytes. SDC
  689. * ver1.x cards with capacity less than 1Gb, will have sector size
  690. * 512 bytes. SDC ver1.x cards with capacity of 2Gb will report readbllen
  691. * of 1024 but should use 512 bytes for block transfers. SDC ver1.x 4Gb
  692. * cards will report readbllen of 2048 bytes -- are they also 512 bytes?
  693. */
  694. #ifdef CONFIG_MMCSD_SECTOR512
  695. if (readbllen > 9)
  696. {
  697. csizemult += (readbllen - 9);
  698. }
  699. else
  700. {
  701. DEBUGASSERT(readbllen == 9);
  702. }
  703. #else
  704. if (IS_SDV2(slot->type))
  705. {
  706. if (readbllen > 9)
  707. {
  708. fwarn("WARNING: Forcing 512 byte sector size\n");
  709. csizemult += (readbllen - 9);
  710. readbllen = 9;
  711. }
  712. }
  713. slot->sectorsize = 1 << readbllen;
  714. #endif
  715. slot->nsectors = csize << csizemult;
  716. finfo("Sector size: %d\n", SECTORSIZE(slot));
  717. finfo("Number of sectors: %" PRId32 "\n", slot->nsectors);
  718. }
  719. /****************************************************************************
  720. * Name: mmcsd_checkwrprotect
  721. *
  722. * Description:
  723. *
  724. ****************************************************************************/
  725. static void mmcsd_checkwrprotect(FAR struct mmcsd_slot_s *slot, uint8_t *csd)
  726. {
  727. FAR struct spi_dev_s *spi = slot->spi;
  728. /* Check if (1) the slot is reporting that reporting that write protection
  729. * is set, (2) the card reports permanent write protect, or (2) the card
  730. * reports temporary write protect.
  731. */
  732. if ((SPI_STATUS(spi, SPIDEV_MMCSD(0)) & SPI_STATUS_WRPROTECTED) != 0 ||
  733. MMCSD_CSD_PERMWRITEPROTECT(csd) ||
  734. MMCSD_CSD_TMPWRITEPROTECT(csd))
  735. {
  736. slot->state |= MMCSD_SLOTSTATUS_WRPROTECT;
  737. }
  738. else
  739. {
  740. slot->state &= ~MMCSD_SLOTSTATUS_WRPROTECT;
  741. }
  742. }
  743. /****************************************************************************
  744. * Name: mmcsd_getcardinfo
  745. *
  746. * Description:
  747. * Read CSD or CID registers
  748. *
  749. * Assumptions:
  750. * MMC/SD card already selected
  751. *
  752. ****************************************************************************/
  753. static int mmcsd_getcardinfo(FAR struct mmcsd_slot_s *slot, uint8_t *buffer,
  754. const struct mmcsd_cmdinfo_s *cmd)
  755. {
  756. FAR struct spi_dev_s *spi = slot->spi;
  757. uint32_t result;
  758. uint8_t response;
  759. int i;
  760. SPI_SEND(spi, 0xff);
  761. /* Send the CMD9 or CMD10 */
  762. result = mmcsd_sendcmd(slot, cmd, 0);
  763. if (result != MMCSD_SPIR1_OK)
  764. {
  765. ferr("ERROR: CMD9/10 failed: R1=%02" PRIx32 "\n", result);
  766. return -EIO;
  767. }
  768. /* Try up to 8 times to find the start of block or until an error occurs */
  769. for (i = 0; i < 8; i++)
  770. {
  771. response = SPI_SEND(spi, 0xff);
  772. finfo("%d. SPI send returned %02x\n", i, response);
  773. /* If a read operation fails and the card cannot provide the requested
  774. * data, it will send a data error token instead. The 4 least
  775. * significant bits are the same as those in the R2 response.
  776. */
  777. if (response != 0 && (response & MMCSD_SPIDET_UPPER) == 0)
  778. {
  779. ferr("ERROR: %d. Data transfer error: %02x\n", i, response);
  780. return -EIO;
  781. }
  782. else if (response == MMCSD_SPIDT_STARTBLKSNGL)
  783. {
  784. for (i = 0; i < 16; ++i)
  785. {
  786. *buffer++ = SPI_SEND(spi, 0xff);
  787. }
  788. /* CRC receive */
  789. SPI_SEND(spi, 0xff);
  790. SPI_SEND(spi, 0xff);
  791. return OK;
  792. }
  793. }
  794. ferr("ERROR: Did not find start of block\n");
  795. return -EIO;
  796. }
  797. /****************************************************************************
  798. * Name: mmcsd_recvblock
  799. *
  800. * Description: Receive a data block from the card
  801. *
  802. ****************************************************************************/
  803. static int mmcsd_recvblock(FAR struct mmcsd_slot_s *slot, uint8_t *buffer,
  804. int nbytes)
  805. {
  806. FAR struct spi_dev_s *spi = slot->spi;
  807. clock_t start;
  808. clock_t elapsed;
  809. uint8_t token;
  810. /* Wait up to the maximum to receive a valid data token. taccess is the
  811. * time from when the command is sent until the first byte of data is
  812. * received.
  813. */
  814. start = START_TIME;
  815. do
  816. {
  817. token = SPI_SEND(spi, 0xff);
  818. elapsed = ELAPSED_TIME(start);
  819. }
  820. while (token == 0xff && elapsed < slot->taccess);
  821. if (token == MMCSD_SPIDT_STARTBLKSNGL)
  822. {
  823. /* Receive the block */
  824. SPI_RECVBLOCK(spi, buffer, nbytes);
  825. /* Discard the CRC */
  826. SPI_SEND(spi, 0xff);
  827. SPI_SEND(spi, 0xff);
  828. return OK;
  829. }
  830. ferr("ERROR: Did not receive data token (%02x)\n", token);
  831. return ERROR;
  832. }
  833. /****************************************************************************
  834. * Name: mmcsd_xmitblock
  835. *
  836. * Description: Transmit a data block to the card
  837. *
  838. ****************************************************************************/
  839. #if !defined(CONFIG_MMCSD_READONLY)
  840. static int mmcsd_xmitblock(FAR struct mmcsd_slot_s *slot,
  841. FAR const uint8_t *buffer, int nbytes,
  842. uint8_t token)
  843. {
  844. FAR struct spi_dev_s *spi = slot->spi;
  845. uint8_t response;
  846. /* Start the block transfer:
  847. * 1. 0xff (sync)
  848. * 2. 0xfe or 0xfc (start of block token)
  849. * 3. Followed by the block of data and 2 byte CRC
  850. */
  851. SPI_SEND(spi, 0xff); /* sync */
  852. SPI_SEND(spi, token); /* data token */
  853. /* Transmit the block to the MMC/SD card */
  854. SPI_SNDBLOCK(spi, buffer, nbytes);
  855. /* Add the bogus CRC. By default, the SPI interface is initialized in
  856. * non-protected mode. However, we still have to send bogus CRC values
  857. */
  858. SPI_SEND(spi, 0xff);
  859. SPI_SEND(spi, 0xff);
  860. /* Now get the data response */
  861. response = SPI_SEND(spi, 0xff);
  862. if ((response & MMCSD_SPIDR_MASK) != MMCSD_SPIDR_ACCEPTED)
  863. {
  864. ferr("ERROR: Bad data response: %02x\n", response);
  865. return -EIO;
  866. }
  867. return OK;
  868. }
  869. #endif /* !CONFIG_MMCSD_READONLY */
  870. /****************************************************************************
  871. * Block Driver Operations
  872. ****************************************************************************/
  873. /****************************************************************************
  874. * Name: mmcsd_open
  875. *
  876. * Description: Open the block device
  877. *
  878. ****************************************************************************/
  879. static int mmcsd_open(FAR struct inode *inode)
  880. {
  881. FAR struct mmcsd_slot_s *slot;
  882. FAR struct spi_dev_s *spi;
  883. int ret;
  884. finfo("Entry\n");
  885. #ifdef CONFIG_DEBUG_FEATURES
  886. if (!inode || !inode->i_private)
  887. {
  888. ferr("ERROR: Internal confusion\n");
  889. return -EIO;
  890. }
  891. #endif
  892. /* Extract our private data from the inode structure */
  893. slot = (FAR struct mmcsd_slot_s *)inode->i_private;
  894. spi = slot->spi;
  895. #ifdef CONFIG_DEBUG_FEATURES
  896. if (!spi)
  897. {
  898. ferr("ERROR: Internal confusion\n");
  899. return -EIO;
  900. }
  901. #endif
  902. ret = mmcsd_semtake(slot);
  903. if (ret < 0)
  904. {
  905. return ret;
  906. }
  907. /* Verify that an MMC/SD card has been inserted */
  908. ret = -ENODEV;
  909. if ((SPI_STATUS(spi, SPIDEV_MMCSD(0)) & SPI_STATUS_PRESENT) != 0)
  910. {
  911. /* Yes.. a card is present. Has it been initialized? */
  912. if (slot->type == MMCSD_CARDTYPE_UNKNOWN)
  913. {
  914. /* Initialize for the media in the slot */
  915. ret = mmcsd_mediainitialize(slot);
  916. if (ret < 0)
  917. {
  918. finfo("Failed to initialize card\n");
  919. goto errout_with_sem;
  920. }
  921. }
  922. /* Make sure that the card is ready */
  923. SPI_SELECT(spi, SPIDEV_MMCSD(0), true);
  924. ret = mmcsd_waitready(slot);
  925. SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
  926. }
  927. errout_with_sem:
  928. mmcsd_semgive(slot);
  929. return ret;
  930. }
  931. /****************************************************************************
  932. * Name: mmcsd_close
  933. *
  934. * Description: close the block device
  935. *
  936. ****************************************************************************/
  937. static int mmcsd_close(FAR struct inode *inode)
  938. {
  939. finfo("Entry\n");
  940. return OK;
  941. }
  942. /****************************************************************************
  943. * Name: mmcsd_read
  944. *
  945. * Description: Read the specified number of sectors
  946. *
  947. ****************************************************************************/
  948. static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer,
  949. blkcnt_t start_sector, unsigned int nsectors)
  950. {
  951. FAR struct mmcsd_slot_s *slot;
  952. FAR struct spi_dev_s *spi;
  953. size_t nbytes;
  954. off_t offset;
  955. uint8_t response;
  956. int i;
  957. int ret;
  958. finfo("start_sector=%" PRIu32 " nsectors=%u\n", start_sector, nsectors);
  959. #ifdef CONFIG_DEBUG_FEATURES
  960. if (!buffer)
  961. {
  962. ferr("ERROR: Invalid parameters\n");
  963. return -EINVAL;
  964. }
  965. if (!inode || !inode->i_private)
  966. {
  967. ferr("ERROR: Internal confusion\n");
  968. return -EIO;
  969. }
  970. #endif
  971. /* Extract our private data from the inode structure */
  972. slot = (FAR struct mmcsd_slot_s *)inode->i_private;
  973. spi = slot->spi;
  974. #ifdef CONFIG_DEBUG_FEATURES
  975. if (!spi)
  976. {
  977. ferr("ERROR: Internal confusion\n");
  978. return -EIO;
  979. }
  980. #endif
  981. /* Verify that card is available */
  982. if (slot->state & MMCSD_SLOTSTATUS_NOTREADY)
  983. {
  984. ferr("ERROR: Slot not ready\n");
  985. return -ENODEV;
  986. }
  987. /* Do nothing on zero-length transfer */
  988. if (nsectors < 1)
  989. {
  990. return 0;
  991. }
  992. /* Convert sector and nsectors to nbytes and byte offset */
  993. nbytes = nsectors * SECTORSIZE(slot);
  994. UNUSED(nbytes);
  995. if (IS_BLOCK(slot->type))
  996. {
  997. offset = start_sector;
  998. finfo("nbytes=%zu sector offset=%jd\n", nbytes, (intmax_t)offset);
  999. }
  1000. else
  1001. {
  1002. offset = start_sector * SECTORSIZE(slot);
  1003. finfo("nbytes=%zu byte offset=%jd\n", nbytes, (intmax_t)offset);
  1004. }
  1005. /* Select the slave */
  1006. ret = mmcsd_semtake(slot);
  1007. if (ret < 0)
  1008. {
  1009. return (ssize_t)ret;
  1010. }
  1011. SPI_SELECT(spi, SPIDEV_MMCSD(0), true);
  1012. /* Single or multiple block read? */
  1013. if (nsectors == 1)
  1014. {
  1015. /* Send CMD17: Reads a block of the size selected by the SET_BLOCKLEN
  1016. * command and verify that good R1 status is returned
  1017. */
  1018. response = mmcsd_sendcmd(slot, &g_cmd17, offset);
  1019. if (response != MMCSD_SPIR1_OK)
  1020. {
  1021. ferr("ERROR: CMD17 failed: R1=%02x\n", response);
  1022. goto errout_with_eio;
  1023. }
  1024. /* Receive the block */
  1025. if (mmcsd_recvblock(slot, buffer, SECTORSIZE(slot)) != 0)
  1026. {
  1027. ferr("ERROR: Failed: to receive the block\n");
  1028. goto errout_with_eio;
  1029. }
  1030. }
  1031. else
  1032. {
  1033. /* Send CMD18: Reads a block of the size selected by the SET_BLOCKLEN
  1034. * command and verify that good R1 status is returned
  1035. */
  1036. response = mmcsd_sendcmd(slot, &g_cmd18, offset);
  1037. if (response != MMCSD_SPIR1_OK)
  1038. {
  1039. ferr("ERROR: CMD18 failed: R1=%02x\n", response);
  1040. goto errout_with_eio;
  1041. }
  1042. /* Receive each block */
  1043. for (i = 0; i < nsectors; i++)
  1044. {
  1045. if (mmcsd_recvblock(slot, buffer, SECTORSIZE(slot)) != 0)
  1046. {
  1047. ferr("ERROR: Failed: to receive the block\n");
  1048. goto errout_with_eio;
  1049. }
  1050. buffer += SECTORSIZE(slot);
  1051. }
  1052. /* Send CMD12: Stops transmission */
  1053. response = mmcsd_sendcmd(slot, &g_cmd12, 0);
  1054. }
  1055. /* On success, return the number of sectors transfer */
  1056. SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
  1057. SPI_SEND(spi, 0xff);
  1058. mmcsd_semgive(slot);
  1059. finfo("Read %d bytes:\n", nbytes);
  1060. mmcsd_dumpbuffer("Read buffer", buffer, nbytes);
  1061. return nsectors;
  1062. errout_with_eio:
  1063. SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
  1064. mmcsd_semgive(slot);
  1065. return -EIO;
  1066. }
  1067. /****************************************************************************
  1068. * Name: mmcsd_write
  1069. *
  1070. * Description:
  1071. * Write the specified number of sectors
  1072. *
  1073. ****************************************************************************/
  1074. #if !defined(CONFIG_MMCSD_READONLY)
  1075. static ssize_t mmcsd_write(FAR struct inode *inode,
  1076. FAR const unsigned char *buffer,
  1077. blkcnt_t start_sector, unsigned int nsectors)
  1078. {
  1079. FAR struct mmcsd_slot_s *slot;
  1080. FAR struct spi_dev_s *spi;
  1081. size_t nbytes;
  1082. off_t offset;
  1083. uint8_t response;
  1084. int i;
  1085. int ret;
  1086. finfo("start_sector=%" PRIu32 " nsectors=%u\n", start_sector, nsectors);
  1087. #ifdef CONFIG_DEBUG_FEATURES
  1088. if (!buffer)
  1089. {
  1090. ferr("ERROR: Invalid parameters\n");
  1091. return -EINVAL;
  1092. }
  1093. if (!inode || !inode->i_private)
  1094. {
  1095. ferr("ERROR: Internal confusion\n");
  1096. return -EIO;
  1097. }
  1098. #endif
  1099. /* Extract our private data from the inode structure */
  1100. slot = (FAR struct mmcsd_slot_s *)inode->i_private;
  1101. spi = slot->spi;
  1102. #ifdef CONFIG_DEBUG_FEATURES
  1103. if (!spi)
  1104. {
  1105. ferr("ERROR: Internal confusion\n");
  1106. return -EIO;
  1107. }
  1108. #endif
  1109. /* Verify that card is available */
  1110. if (slot->state & MMCSD_SLOTSTATUS_NOTREADY)
  1111. {
  1112. ferr("ERROR: Slot not ready\n");
  1113. return -ENODEV;
  1114. }
  1115. /* Verify that the card is write enabled */
  1116. if (slot->state & MMCSD_SLOTSTATUS_WRPROTECT)
  1117. {
  1118. ferr("ERROR: Not write enabled\n");
  1119. return -EACCES;
  1120. }
  1121. /* Do nothing on zero-length transfer */
  1122. if (nsectors < 1)
  1123. {
  1124. return 0;
  1125. }
  1126. /* Convert sector and nsectors to nbytes and byte offset */
  1127. nbytes = nsectors * SECTORSIZE(slot);
  1128. UNUSED(nbytes);
  1129. if (IS_BLOCK(slot->type))
  1130. {
  1131. offset = start_sector;
  1132. finfo("nbytes=%zu sector offset=%jd\n", nbytes, (intmax_t)offset);
  1133. }
  1134. else
  1135. {
  1136. offset = start_sector * SECTORSIZE(slot);
  1137. finfo("nbytes=%zu byte offset=%jd\n", nbytes, (intmax_t)offset);
  1138. }
  1139. mmcsd_dumpbuffer("Write buffer", buffer, nbytes);
  1140. /* Select the slave */
  1141. ret = mmcsd_semtake(slot);
  1142. if (ret < 0)
  1143. {
  1144. return (ssize_t)ret;
  1145. }
  1146. SPI_SELECT(spi, SPIDEV_MMCSD(0), true);
  1147. /* Single or multiple block transfer? */
  1148. if (nsectors == 1)
  1149. {
  1150. /* Send CMD24(WRITE_BLOCK) and verify that good R1 is returned */
  1151. response = mmcsd_sendcmd(slot, &g_cmd24, offset);
  1152. if (response != MMCSD_SPIR1_OK)
  1153. {
  1154. ferr("ERROR: CMD24 failed: R1=%02x\n", response);
  1155. goto errout_with_sem;
  1156. }
  1157. /* Then transfer the sector */
  1158. if (mmcsd_xmitblock(slot, buffer, SECTORSIZE(slot), 0xfe) != 0)
  1159. {
  1160. ferr("ERROR: Block transfer failed\n");
  1161. goto errout_with_sem;
  1162. }
  1163. }
  1164. else
  1165. {
  1166. /* Set the number of blocks to be pre-erased (SD only) */
  1167. if (IS_SD(slot->type))
  1168. {
  1169. response = mmcsd_sendcmd(slot, &g_cmd55, 0);
  1170. if (response != MMCSD_SPIR1_OK)
  1171. {
  1172. ferr("ERROR: CMD55 failed: R1=%02x\n", response);
  1173. goto errout_with_sem;
  1174. }
  1175. response = mmcsd_sendcmd(slot, &g_acmd23, nsectors);
  1176. if (response != MMCSD_SPIR1_OK)
  1177. {
  1178. ferr("ERROR: ACMD23 failed: R1=%02x\n", response);
  1179. goto errout_with_sem;
  1180. }
  1181. }
  1182. /* Send CMD25: Continuously write blocks of data until the
  1183. * transmission is stopped.
  1184. */
  1185. response = mmcsd_sendcmd(slot, &g_cmd25, offset);
  1186. if (response != MMCSD_SPIR1_OK)
  1187. {
  1188. ferr("ERROR: CMD25 failed: R1=%02x\n", response);
  1189. goto errout_with_sem;
  1190. }
  1191. /* Transmit each block */
  1192. for (i = 0; i < nsectors; i++)
  1193. {
  1194. if (mmcsd_xmitblock(slot, buffer, SECTORSIZE(slot), 0xfc) != 0)
  1195. {
  1196. ferr("ERROR: Failed: to receive the block\n");
  1197. goto errout_with_sem;
  1198. }
  1199. buffer += SECTORSIZE(slot);
  1200. if (mmcsd_waitready(slot) != OK)
  1201. {
  1202. ferr("ERROR: Failed: card is busy\n");
  1203. goto errout_with_sem;
  1204. }
  1205. }
  1206. /* Send the stop transmission token */
  1207. SPI_SEND(spi, MMCSD_SPIDT_STOPTRANS);
  1208. }
  1209. /* Wait until the card is no longer busy */
  1210. mmcsd_waitready(slot);
  1211. SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
  1212. SPI_SEND(spi, 0xff);
  1213. mmcsd_semgive(slot);
  1214. /* The success return value is the number of sectors written */
  1215. return nsectors;
  1216. errout_with_sem:
  1217. SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
  1218. mmcsd_semgive(slot);
  1219. return -EIO;
  1220. }
  1221. #endif
  1222. /****************************************************************************
  1223. * Name: mmcsd_geometry
  1224. *
  1225. * Description:
  1226. * Return device geometry
  1227. *
  1228. ****************************************************************************/
  1229. static int mmcsd_geometry(FAR struct inode *inode, struct geometry *geometry)
  1230. {
  1231. FAR struct mmcsd_slot_s *slot;
  1232. FAR struct spi_dev_s *spi;
  1233. uint8_t csd[16];
  1234. int ret;
  1235. #ifdef CONFIG_DEBUG_FEATURES
  1236. if (!geometry)
  1237. {
  1238. ferr("ERROR: Invalid parameters\n");
  1239. return -EINVAL;
  1240. }
  1241. if (!inode || !inode->i_private)
  1242. {
  1243. ferr("ERROR: Internal confusion\n");
  1244. return -EIO;
  1245. }
  1246. #endif
  1247. /* Extract our private data from the inode structure */
  1248. slot = (FAR struct mmcsd_slot_s *)inode->i_private;
  1249. spi = slot->spi;
  1250. #ifdef CONFIG_DEBUG_FEATURES
  1251. if (!spi)
  1252. {
  1253. ferr("ERROR: Internal confusion\n");
  1254. return -EIO;
  1255. }
  1256. #endif
  1257. /* Re-sample the CSD */
  1258. ret = mmcsd_semtake(slot);
  1259. if (ret < 0)
  1260. {
  1261. return ret;
  1262. }
  1263. SPI_SELECT(spi, SPIDEV_MMCSD(0), true);
  1264. ret = mmcsd_getcsd(slot, csd);
  1265. SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
  1266. if (ret < 0)
  1267. {
  1268. mmcsd_semgive(slot);
  1269. ferr("ERROR: mmcsd_getcsd returned %d\n", ret);
  1270. return ret;
  1271. }
  1272. /* Check for changes related to write protection */
  1273. mmcsd_checkwrprotect(slot, csd);
  1274. /* Then return the card geometry */
  1275. geometry->geo_available =
  1276. ((slot->state & (MMCSD_SLOTSTATUS_NOTREADY |
  1277. MMCSD_SLOTSTATUS_NODISK)) == 0);
  1278. geometry->geo_mediachanged =
  1279. ((slot->state & MMCSD_SLOTSTATUS_MEDIACHGD) != 0);
  1280. #if !defined(CONFIG_MMCSD_READONLY)
  1281. geometry->geo_writeenabled =
  1282. ((slot->state & MMCSD_SLOTSTATUS_WRPROTECT) == 0);
  1283. #else
  1284. geometry->geo_writeenabled = false;
  1285. #endif
  1286. geometry->geo_nsectors = slot->nsectors;
  1287. geometry->geo_sectorsize = SECTORSIZE(slot);
  1288. /* After reporting mediachanged, clear the indication so that it is not
  1289. * reported again.
  1290. */
  1291. slot->state &= ~MMCSD_SLOTSTATUS_MEDIACHGD;
  1292. mmcsd_semgive(slot);
  1293. finfo("geo_available: %d\n", geometry->geo_available);
  1294. finfo("geo_mediachanged: %d\n", geometry->geo_mediachanged);
  1295. finfo("geo_writeenabled: %d\n", geometry->geo_writeenabled);
  1296. finfo("geo_nsectors: %" PRIu32 "\n", geometry->geo_nsectors);
  1297. finfo("geo_sectorsize: %" PRIi16 "\n", geometry->geo_sectorsize);
  1298. return OK;
  1299. }
  1300. /****************************************************************************
  1301. * Name: mmcsd_mediainitialize
  1302. *
  1303. * Description:
  1304. * Detect media and initialize
  1305. *
  1306. * Precondition:
  1307. * Semaphore has been taken.
  1308. ****************************************************************************/
  1309. static int mmcsd_mediainitialize(FAR struct mmcsd_slot_s *slot)
  1310. {
  1311. FAR struct spi_dev_s *spi = slot->spi;
  1312. uint8_t csd[16];
  1313. uint32_t result = MMCSD_SPIR1_IDLESTATE;
  1314. clock_t start;
  1315. clock_t elapsed;
  1316. int i;
  1317. int j;
  1318. /* Assume that the card is not ready (we'll clear this on successful card
  1319. * initialization.
  1320. */
  1321. slot->state |= MMCSD_SLOTSTATUS_NOTREADY;
  1322. /* Check if there is a card present in the slot. This is normally a
  1323. * matter is of GPIO sensing and does not really involve SPI, but by
  1324. * putting this functionality in the SPI interface, we encapsulate the
  1325. * SPI MMC/SD interface
  1326. */
  1327. if ((SPI_STATUS(spi, SPIDEV_MMCSD(0)) & SPI_STATUS_PRESENT) == 0)
  1328. {
  1329. fwarn("WARNING: No card present\n");
  1330. slot->state |= MMCSD_SLOTSTATUS_NODISK;
  1331. return -ENODEV;
  1332. }
  1333. /* Clock Freq. Identification Mode < 400kHz */
  1334. slot->spispeed = CONFIG_MMCSD_IDMODE_CLOCK;
  1335. SPI_SETFREQUENCY(spi, CONFIG_MMCSD_IDMODE_CLOCK);
  1336. /* Set the maximum access time out */
  1337. slot->taccess = SD_READACCESS;
  1338. /* The SD card wakes up in SD mode. It will enter SPI mode if the chip
  1339. * select signal is asserted (negative) during the reception of the reset
  1340. * command (CMD0) and the card is in IDLE state.
  1341. */
  1342. for (i = 0; i < 2; i++)
  1343. {
  1344. /* After power up at least 74 clock cycles are required prior to
  1345. * starting bus communication
  1346. */
  1347. for (j = 10; j; j--)
  1348. {
  1349. SPI_SEND(spi, 0xff);
  1350. }
  1351. /* Send CMD0 (GO_TO_IDLE) with CS asserted to put MMC/SD in
  1352. * IDLE/SPI mode. Return from CMD0 is R1 which should now
  1353. * show IDLE STATE
  1354. */
  1355. finfo("Send CMD0\n");
  1356. SPI_SELECT(spi, SPIDEV_MMCSD(0), true);
  1357. result = mmcsd_sendcmd(slot, &g_cmd0, 0);
  1358. if (result == MMCSD_SPIR1_IDLESTATE)
  1359. {
  1360. /* Break out of the loop with card selected */
  1361. finfo("Card is in IDLE state\n");
  1362. break;
  1363. }
  1364. /* De-select card and try again */
  1365. SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
  1366. }
  1367. /* Verify that we exit the above loop with the card reporting IDLE state */
  1368. if (result != MMCSD_SPIR1_IDLESTATE)
  1369. {
  1370. ferr("ERROR: Send CMD0 failed: R1=%02" PRIx32 "\n", result);
  1371. SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
  1372. mmcsd_semgive(slot);
  1373. return -EIO;
  1374. }
  1375. slot->type = MMCSD_CARDTYPE_UNKNOWN;
  1376. /* Check for SDHC Version 2.x. CMD 8 is reserved on SD version 1.0 and
  1377. * MMC.
  1378. */
  1379. finfo("Send CMD8\n");
  1380. result = mmcsd_sendcmd(slot, &g_cmd8, 0x1aa);
  1381. if (result == MMCSD_SPIR1_IDLESTATE)
  1382. {
  1383. /* Verify the operating voltage and 0xaa was correctly echoed */
  1384. if (((slot->r7 & MMCSD_SPIR7_VOLTAGE_MASK) ==
  1385. MMCSD_SPIR7_VOLTAGE_27) &&
  1386. ((slot->r7 & MMCSD_SPIR7_ECHO_MASK) == 0xaa))
  1387. {
  1388. /* Try CMD55/ACMD41 for up to 1 second or until the card exits
  1389. * the IDLE state
  1390. */
  1391. start = START_TIME;
  1392. elapsed = 0;
  1393. do
  1394. {
  1395. finfo("%ju. Send CMD55/ACMD41\n", (uintmax_t)elapsed);
  1396. result = mmcsd_sendcmd(slot, &g_cmd55, 0);
  1397. if (result == MMCSD_SPIR1_IDLESTATE ||
  1398. result == MMCSD_SPIR1_OK)
  1399. {
  1400. result = mmcsd_sendcmd(slot, &g_acmd41, (uint32_t)1 << 30);
  1401. if (result == MMCSD_SPIR1_OK)
  1402. {
  1403. break;
  1404. }
  1405. }
  1406. elapsed = ELAPSED_TIME(start);
  1407. }
  1408. while (elapsed < MMCSD_DELAY_1SEC);
  1409. /* Check if ACMD41 was sent successfully */
  1410. if (elapsed < MMCSD_DELAY_1SEC)
  1411. {
  1412. finfo("Send CMD58\n");
  1413. SPI_SEND(spi, 0xff);
  1414. result = mmcsd_sendcmd(slot, &g_cmd58, 0);
  1415. if (result == MMCSD_SPIR1_OK)
  1416. {
  1417. finfo("OCR: %08" PRIx32 "\n", slot->ocr);
  1418. if ((slot->ocr & MMCSD_OCR_CCS) != 0)
  1419. {
  1420. finfo("Identified SD ver2 card/with block access\n");
  1421. slot->type = MMCSD_CARDTYPE_SDV2 |
  1422. MMCSD_CARDTYPE_BLOCK;
  1423. }
  1424. else
  1425. {
  1426. finfo("Identified SD ver2 card\n");
  1427. slot->type = MMCSD_CARDTYPE_SDV2;
  1428. }
  1429. }
  1430. }
  1431. }
  1432. }
  1433. /* Check for SDC version 1.x or MMC */
  1434. else
  1435. {
  1436. /* Both the MMC card and the SD card support CMD55 */
  1437. finfo("Send CMD55/ACMD41\n");
  1438. result = mmcsd_sendcmd(slot, &g_cmd55, 0);
  1439. if (result == MMCSD_SPIR1_IDLESTATE || result == MMCSD_SPIR1_OK)
  1440. {
  1441. /* But ACMD41 is supported only on SD */
  1442. result = mmcsd_sendcmd(slot, &g_acmd41, 0);
  1443. if (result == MMCSD_SPIR1_IDLESTATE || result == MMCSD_SPIR1_OK)
  1444. {
  1445. finfo("Identified SD ver1 card\n");
  1446. slot->type = MMCSD_CARDTYPE_SDV1;
  1447. }
  1448. }
  1449. /* Make sure that we are out of the Idle state */
  1450. start = START_TIME;
  1451. elapsed = 0;
  1452. do
  1453. {
  1454. if (IS_SD(slot->type))
  1455. {
  1456. finfo("%ju. Send CMD55/ACMD41\n", (uintmax_t)elapsed);
  1457. result = mmcsd_sendcmd(slot, &g_cmd55, 0);
  1458. if (result == MMCSD_SPIR1_IDLESTATE ||
  1459. result == MMCSD_SPIR1_OK)
  1460. {
  1461. result = mmcsd_sendcmd(slot, &g_acmd41, 0);
  1462. if (result == MMCSD_SPIR1_OK)
  1463. {
  1464. break;
  1465. }
  1466. }
  1467. }
  1468. else
  1469. {
  1470. finfo("%d. Send CMD1\n", i);
  1471. result = mmcsd_sendcmd(slot, &g_cmd1, 0);
  1472. if (result == MMCSD_SPIR1_OK)
  1473. {
  1474. finfo("%d. Identified MMC card\n", i);
  1475. slot->type = MMCSD_CARDTYPE_MMC;
  1476. break;
  1477. }
  1478. }
  1479. elapsed = ELAPSED_TIME(start);
  1480. }
  1481. while (elapsed < MMCSD_DELAY_1SEC);
  1482. if (elapsed >= MMCSD_DELAY_1SEC)
  1483. {
  1484. ferr("ERROR: Failed to exit IDLE state\n");
  1485. SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
  1486. mmcsd_semgive(slot);
  1487. return -EIO;
  1488. }
  1489. }
  1490. if (slot->type == MMCSD_CARDTYPE_UNKNOWN)
  1491. {
  1492. ferr("ERROR: Failed to identify card\n");
  1493. SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
  1494. mmcsd_semgive(slot);
  1495. return -EIO;
  1496. }
  1497. /* Read CSD. CSD must always be valid */
  1498. finfo("Get CSD\n");
  1499. result = mmcsd_getcsd(slot, csd);
  1500. if (result != OK)
  1501. {
  1502. ferr("ERROR: mmcsd_getcsd(CMD9) failed: %" PRId32 "\n", result);
  1503. SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
  1504. mmcsd_semgive(slot);
  1505. return -EIO;
  1506. }
  1507. mmcsd_dmpcsd(csd, slot->type);
  1508. /* CSD data and set block size */
  1509. mmcsd_decodecsd(slot, csd);
  1510. mmcsd_checkwrprotect(slot, csd);
  1511. /* SDHC ver2.x cards have fixed block transfer size of 512 bytes. SDC
  1512. * ver1.x cards with capacity less than 1Gb, will have sector size
  1513. * 512 bytes. SDC ver1.x cards with capacity of 2Gb will report readbllen
  1514. * of 1024 but should use 512 bytes for block transfers. SDC ver1.x 4Gb
  1515. * cards will report readbllen of 2048 bytes -- are they also 512 bytes?
  1516. * I think that none of these high capacity cards support setting the
  1517. * block length??
  1518. */
  1519. #ifdef CONFIG_MMCSD_SECTOR512
  1520. /* Using 512 byte sectors, the maximum ver1.x capacity is 4096 x 512
  1521. * blocks. The saved slot->nsectors is converted to 512 byte blocks, so
  1522. * if slot->nsectors exceeds 4096 x 512, then we must be dealing with a
  1523. * card with read_bl_len of 1024 or 2048.
  1524. */
  1525. if (!IS_SDV2(slot->type) && slot->nsectors <= ((uint32_t)4096 * 12))
  1526. {
  1527. /* Don't set the block len on high capacity cards (ver1.x or ver2.x) */
  1528. mmcsd_setblklen(slot, SECTORSIZE(slot));
  1529. }
  1530. #else
  1531. if (!IS_SDV2(slot->type))
  1532. {
  1533. /* Don't set the block len on ver2.x cards */
  1534. mmcsd_setblklen(slot, SECTORSIZE(slot));
  1535. }
  1536. #endif
  1537. slot->state &= ~MMCSD_SLOTSTATUS_NOTREADY;
  1538. SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
  1539. return OK;
  1540. }
  1541. /****************************************************************************
  1542. * Name: mmcsd_mediachanged
  1543. *
  1544. * Description:
  1545. * Handle initialization/media change events
  1546. *
  1547. ****************************************************************************/
  1548. static void mmcsd_mediachanged(void *arg)
  1549. {
  1550. FAR struct mmcsd_slot_s *slot = (FAR struct mmcsd_slot_s *)arg;
  1551. FAR struct spi_dev_s *spi;
  1552. uint8_t oldstate;
  1553. int ret;
  1554. #ifdef CONFIG_DEBUG_FEATURES
  1555. if (!slot || !slot->spi)
  1556. {
  1557. ferr("ERROR: Internal confusion\n");
  1558. return;
  1559. }
  1560. #endif
  1561. spi = slot->spi;
  1562. /* Save the current slot state and reassess the new state */
  1563. ret = mmcsd_semtake(slot);
  1564. if (ret < 0)
  1565. {
  1566. return;
  1567. }
  1568. oldstate = slot->state;
  1569. /* Check if media was removed or inserted */
  1570. slot->state &= ~(MMCSD_SLOTSTATUS_NODISK | MMCSD_SLOTSTATUS_NOTREADY |
  1571. MMCSD_SLOTSTATUS_MEDIACHGD);
  1572. if ((SPI_STATUS(spi, SPIDEV_MMCSD(0)) & SPI_STATUS_PRESENT) == 0)
  1573. {
  1574. /* Media is not present */
  1575. fwarn("WARNING: No card present\n");
  1576. slot->state |= (MMCSD_SLOTSTATUS_NODISK | MMCSD_SLOTSTATUS_NOTREADY);
  1577. /* Was media removed? */
  1578. if ((oldstate & MMCSD_SLOTSTATUS_NODISK) == 0)
  1579. {
  1580. slot->state |= MMCSD_SLOTSTATUS_MEDIACHGD;
  1581. }
  1582. }
  1583. /* Media is present, was it just inserted? Or, if it was previously not
  1584. * ready, then try re-initializing it
  1585. */
  1586. else if ((oldstate & (MMCSD_SLOTSTATUS_NODISK |
  1587. MMCSD_SLOTSTATUS_NOTREADY)) != 0)
  1588. {
  1589. /* (Re-)initialize for the media in the slot */
  1590. ret = mmcsd_mediainitialize(slot);
  1591. if (ret == 0)
  1592. {
  1593. finfo("mmcsd_mediainitialize returned OK\n");
  1594. slot->state |= MMCSD_SLOTSTATUS_MEDIACHGD;
  1595. }
  1596. }
  1597. mmcsd_semgive(slot);
  1598. }
  1599. /****************************************************************************
  1600. * Public Functions
  1601. ****************************************************************************/
  1602. /****************************************************************************
  1603. * Name: mmcsd_spislotinitialize
  1604. *
  1605. * Description:
  1606. * Initialize one slot for operation using the SPI MMC/SD interface
  1607. *
  1608. * Input Parameters:
  1609. * minor - The MMC/SD minor device number. The MMC/SD device will be
  1610. * registered as /dev/mmcsdN where N is the minor number
  1611. * slotno - The slot number to use. This is only meaningful for
  1612. * architectures that support multiple MMC/SD slots. This value must be
  1613. * in the range {0, ..., CONFIG_MMCSD_NSLOTS}.
  1614. * spi - And instance of an SPI interface obtained by called the
  1615. * appropriate xyz_spibus_initialize() function for the MCU "xyz" with
  1616. * the appropriate port number.
  1617. *
  1618. ****************************************************************************/
  1619. int mmcsd_spislotinitialize(int minor, int slotno, FAR struct spi_dev_s *spi)
  1620. {
  1621. struct mmcsd_slot_s *slot;
  1622. char devname[16];
  1623. int ret;
  1624. #ifdef CONFIG_DEBUG_FEATURES
  1625. if ((unsigned)slotno >= CONFIG_MMCSD_NSLOTS || (unsigned)minor > 255 ||
  1626. spi == NULL)
  1627. {
  1628. ferr("ERROR: Invalid arguments\n");
  1629. return -EINVAL;
  1630. }
  1631. #endif
  1632. /* Select the slot structure */
  1633. slot = &g_mmcsdslot[slotno];
  1634. memset(slot, 0, sizeof(struct mmcsd_slot_s));
  1635. nxsem_init(&slot->sem, 0, 1);
  1636. #ifdef CONFIG_DEBUG_FEATURES
  1637. if (slot->spi)
  1638. {
  1639. ferr("ERROR: Already registered\n");
  1640. return -EBUSY;
  1641. }
  1642. #endif
  1643. /* Bind the SPI port to the slot */
  1644. slot->spi = spi;
  1645. slot->spispeed = CONFIG_MMCSD_IDMODE_CLOCK;
  1646. /* Get exclusive access to the SPI bus and make sure that SPI is properly
  1647. * configured for the MMC/SD card
  1648. */
  1649. ret = mmcsd_semtake(slot);
  1650. if (ret < 0)
  1651. {
  1652. return ret;
  1653. }
  1654. /* Initialize for the media in the slot (if any) */
  1655. ret = mmcsd_mediainitialize(slot);
  1656. mmcsd_semgive(slot);
  1657. if (ret == 0)
  1658. {
  1659. finfo("mmcsd_mediainitialize returned OK\n");
  1660. slot->state |= MMCSD_SLOTSTATUS_MEDIACHGD;
  1661. }
  1662. /* Create a MMC/SD device name */
  1663. snprintf(devname, 16, "/dev/mmcsd%d", minor);
  1664. /* Register the driver, even on a failure condition. A
  1665. * card may be inserted later, for example.
  1666. */
  1667. ret = register_blockdriver(devname, &g_bops, MMCSD_MODE, slot);
  1668. if (ret < 0)
  1669. {
  1670. ferr("ERROR: register_blockdriver failed: %d\n", -ret);
  1671. slot->spi = NULL;
  1672. return ret;
  1673. }
  1674. /* Register a media change callback to handle insertion and
  1675. * removal of cards.
  1676. */
  1677. SPI_REGISTERCALLBACK(spi, mmcsd_mediachanged, (FAR void *)slot);
  1678. return OK;
  1679. }
  1680. #endif /* defined (CONFIG_MMCSD) && defined (CONFIG_MMCSD_SPI) */