sd.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. /*
  2. * Copyright (c) 2017 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #define LOG_LEVEL CONFIG_MMC_LOG_LEVEL
  7. #include <logging/log.h>
  8. LOG_MODULE_REGISTER(mmc_sd);
  9. #include <kernel.h>
  10. #include <init.h>
  11. #include <device.h>
  12. #include <disk/disk_access.h>
  13. #include <drivers/flash.h>
  14. #include <drivers/gpio.h>
  15. #include <sys/byteorder.h>
  16. #include <drivers/mmc/mmc.h>
  17. #include <drivers/mmc/sd.h>
  18. #include <board.h>
  19. #include "mmc_ops.h"
  20. #if IS_ENABLED(CONFIG_SD)
  21. #define CONFIG_SD_CARD_POWER_RESET_MS 80 /* wait in milliseconds SD card to power off */
  22. #define CONFIG_SD_CARD_HOTPLUG_DEBOUNCE_MS 100 /* SD card hot plug debounce */
  23. #define CONFIG_MMC_SDCARD_RETRY_TIMES 2 /* mmc initialization retry times */
  24. #define CONFIG_MMC_SDCARD_ERR_RETRY_NUM 2 /* mmc read/write retry if error happened */
  25. #define CONFIG_MMC_SDCARD_LOW_POWER 0 /* If 1 to enable SD card low power */
  26. #if (CONFIG_MMC_SDCARD_LOW_POWER == 1)
  27. #define CONFIG_MMC_SDCARD_LOW_POWER_SLEEP 1 /* If 1 to check the mmc device can enter sleep */
  28. #endif
  29. #define CONFIG_MMC_SDCARD_SHOW_PERF 0 /* If 1 to enable sd card performance statistics */
  30. #define MMC_CMD_RETRIES (2)
  31. #define SD_CARD_RW_MAX_SECTOR_CNT_PROTOCOL (65536)
  32. #define SD_CARD_INVALID_OFFSET (-1)
  33. #define SD_CARD_RW_MAX_SECTOR_CNT (512)
  34. #define SD_CARD_SECTOR_SIZE (512)
  35. #define SD_CARD_INIT_CLOCK_FREQ (100000)
  36. #define SD_CARD_SDR8_CLOCK_FREQ (16000000)
  37. #define SD_CARD_SDR16_CLOCK_FREQ (40000000)
  38. #define SD_CARD_WAITBUSY_TIMEOUT_MS (1000)
  39. /* card type */
  40. enum {
  41. CARD_TYPE_SD = 0,
  42. CARD_TYPE_MMC = 1,
  43. };
  44. struct mmc_csd {
  45. uint8_t ccs;
  46. uint8_t sd_spec;
  47. uint8_t suport_cmd23;
  48. uint32_t sector_count;
  49. uint32_t sector_size;
  50. };
  51. /* eMMC specific csd and ext_csd all in this structure */
  52. struct mmc_ext_csd {
  53. /* emmc ext_csd */
  54. u8_t rev;
  55. /* emmc csd */
  56. u8_t mmca_vsn;
  57. u32_t erase_size; /* erase size in sectors */
  58. };
  59. struct sd_card_data {
  60. const struct device *mmc_dev;
  61. struct k_sem lock;
  62. #ifdef CONFIG_PM_DEVICE
  63. struct k_work_q sd_workq;
  64. struct k_work work;
  65. volatile bool b_sd_suspend;
  66. volatile bool b_sd_resume;
  67. const struct device *sd_dev;
  68. #endif
  69. u8_t card_type;
  70. bool card_initialized;
  71. bool force_plug_out;
  72. bool is_low_power;
  73. uint32_t rca;
  74. struct mmc_csd mmc_csd;
  75. struct mmc_ext_csd ext_csd; /* mmc v4 extended card specific */
  76. struct mmc_cmd cmd; /* use the data segment for reducing stack consumption */
  77. const struct device *detect_gpio_dev;
  78. const struct device *power_gpio_dev;
  79. #if (CONFIG_MMC_SDCARD_SHOW_PERF == 1)
  80. u32_t max_rd_use_time; /* record the max read use time */
  81. u32_t max_wr_use_time; /* record the max write use time */
  82. u32_t rec_perf_timestamp; /* record the timestamp for showing performance */
  83. u32_t rec_perf_rd_size; /* record the read total size for showing performance */
  84. u32_t rec_perf_wr_size; /* record the read total size for showing performance */
  85. #endif
  86. u32_t next_rw_offs; /* record the next read/write block offset for high performance mode */
  87. u8_t on_high_performance : 1; /* indicates that sdcard works on high performance mode */
  88. u8_t prev_is_write : 1; /* record the previous command is read or write for high performance mode */
  89. };
  90. struct sd_acts_config {
  91. const char *mmc_dev_name;
  92. uint8_t detect_gpio;
  93. uint8_t power_gpio;
  94. uint8_t use_detect_gpio : 1; /* If 1 to use a GPIO pin to detect SD card host plug */
  95. uint8_t detect_gpio_level : 1; /* The GPIO level(high/low voltage) when SD card has been detected */
  96. uint8_t use_power_gpio : 1; /* If 1 to use a GPIO pin to power on/off SD card */
  97. uint8_t power_gpio_level : 1; /* The GPIO level(high/low voltage) to power on/off SD/eMMC */
  98. };
  99. #ifdef CONFIG_PM_DEVICE
  100. static K_KERNEL_STACK_DEFINE(sd_workq_stack, 2048);
  101. #endif
  102. #define UNSTUFF_BITS(resp,start,size) \
  103. ({ \
  104. const int __size = size; \
  105. const uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  106. const int __off = 3 - ((start) / 32); \
  107. const int __shft = (start) & 31; \
  108. uint32_t __res; \
  109. \
  110. __res = resp[__off] >> __shft; \
  111. if (__size + __shft > 32) \
  112. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  113. __res & __mask; \
  114. })
  115. static void sd_sem_take(struct k_sem *sem, k_timeout_t timeout)
  116. {
  117. if ((!k_is_in_isr()) && (sem != NULL)) {
  118. k_sem_take(sem, K_FOREVER);
  119. }
  120. }
  121. static void sd_sem_give(struct k_sem *sem)
  122. {
  123. if ((!k_is_in_isr()) && (sem != NULL)) {
  124. k_sem_give(sem);
  125. }
  126. }
  127. static int sd_check_rw_env(void)
  128. {
  129. extern int check_panic_exe(void);
  130. /* sd read/write is forbidden in irq environment */
  131. if (k_is_in_isr() && !check_panic_exe()) {
  132. printk("sd is not allowed to rw in system irq\n");
  133. k_panic();
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. static int mmc_decode_csd(struct sd_card_data *sd, u32_t *resp)
  139. {
  140. struct mmc_csd *csd = &sd->mmc_csd;
  141. u32_t c_size, c_size_mult, capacity;
  142. int csd_struct;
  143. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  144. switch (csd_struct) {
  145. case 0:
  146. c_size_mult = UNSTUFF_BITS(resp, 47, 3);
  147. c_size = UNSTUFF_BITS(resp, 62, 12);
  148. csd->sector_size = 1 << UNSTUFF_BITS(resp, 80, 4);
  149. csd->sector_count = (1 + c_size) << (c_size_mult + 2);
  150. capacity = csd->sector_size * csd->sector_count / 1024 / 1024;
  151. break;
  152. case 1:
  153. /* c_size: 512KB block count */
  154. c_size = UNSTUFF_BITS(resp, 48, 22);
  155. csd->sector_size = 512;
  156. csd->sector_count = (c_size + 1) * 1024;
  157. capacity = (1 + c_size) / 2;
  158. break;
  159. default:
  160. LOG_ERR("unknown csd version %d", csd_struct);
  161. return -EINVAL;
  162. }
  163. LOG_INF("CSD: capacity %u MB", capacity);
  164. return 0;
  165. }
  166. static int emmc_decode_csd(struct sd_card_data *sd, u32_t *resp)
  167. {
  168. struct mmc_csd *csd = &sd->mmc_csd;
  169. u32_t c_size, c_size_mult, capacity;
  170. u8_t write_blkbits;
  171. int csd_struct;
  172. /*
  173. * We only understand CSD structure v1.1 and v1.2.
  174. * v1.2 has extra information in bits 15, 11 and 10.
  175. * We also support eMMC v4.4 & v4.41.
  176. */
  177. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  178. if (csd_struct == 0) {
  179. LOG_ERR("unrecognised CSD structure version %d\n", csd_struct);
  180. return -EINVAL;
  181. }
  182. sd->ext_csd.mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
  183. write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  184. if (write_blkbits >= 9) {
  185. u8_t a = UNSTUFF_BITS(resp, 42, 5);
  186. u8_t b = UNSTUFF_BITS(resp, 37, 5);
  187. sd->ext_csd.erase_size = (a + 1) * (b + 1);
  188. sd->ext_csd.erase_size <<= write_blkbits - 9;
  189. }
  190. c_size_mult = UNSTUFF_BITS(resp, 47, 3);
  191. c_size = UNSTUFF_BITS(resp, 62, 12);
  192. csd->sector_size = 1 << UNSTUFF_BITS(resp, 80, 4);
  193. csd->sector_count = (1 + c_size) << (c_size_mult + 2);
  194. capacity = csd->sector_size * csd->sector_count / 1024 / 1024;
  195. LOG_INF("CSD: capacity %u MB", capacity);
  196. return 0;
  197. }
  198. /*
  199. * Decode extended CSD.
  200. */
  201. static int emmc_read_ext_csd(struct sd_card_data *sd, u8_t *ext_csd)
  202. {
  203. u32_t sectors = 0;
  204. u32_t data_sector_size = 512;
  205. sd->ext_csd.rev = ext_csd[EXT_CSD_REV];
  206. if (sd->ext_csd.rev > 8) {
  207. LOG_ERR("unrecognised EXT_CSD revision %d\n", sd->ext_csd.rev);
  208. return -EINVAL;
  209. }
  210. sd->ext_csd.rev = ext_csd[EXT_CSD_REV];
  211. if (sd->ext_csd.rev >= 3) {
  212. if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 1)
  213. sd->ext_csd.erase_size = ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
  214. }
  215. if (sd->ext_csd.rev >= 2) {
  216. sectors =
  217. ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
  218. ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
  219. ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
  220. ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  221. } else {
  222. sectors = 0;
  223. }
  224. /* eMMC v4.5 or later */
  225. if (sd->ext_csd.rev >= 6) {
  226. if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
  227. data_sector_size = 4096;
  228. else
  229. data_sector_size = 512;
  230. } else {
  231. data_sector_size = 512;
  232. }
  233. LOG_INF("EXT_CSD: rev %d, sector_cnt %u, sector_size %u, erase_size %u",
  234. sd->ext_csd.rev, sectors, data_sector_size, sd->ext_csd.erase_size);
  235. /* FIXME: copy to mmc_csd */
  236. if (sectors > 0) {
  237. sd->mmc_csd.sector_count = sectors;
  238. sd->mmc_csd.sector_size = data_sector_size;
  239. }
  240. LOG_INF("EXT_CSD: capacity %u MB",
  241. sd->mmc_csd.sector_count * (sd->mmc_csd.sector_size / 512) / 2048);
  242. return 0;
  243. }
  244. static void mmc_decode_scr(struct sd_card_data *sd, u32_t *scr)
  245. {
  246. struct mmc_csd *csd = &sd->mmc_csd;
  247. u32_t resp[4];
  248. resp[3] = scr[1];
  249. resp[2] = scr[0];
  250. csd->sd_spec = UNSTUFF_BITS(resp, 56, 4);
  251. if (csd->sd_spec == SCR_SPEC_VER_2) {
  252. /* Check if Physical Layer Spec v3.0 is supported */
  253. if (UNSTUFF_BITS(resp, 47, 1)) {
  254. csd->sd_spec = 3;
  255. }
  256. }
  257. /* check Set Block Count command */
  258. if(csd->sd_spec == 3)
  259. csd->suport_cmd23 = !!UNSTUFF_BITS(resp, 33, 1);
  260. LOG_INF("SCR: sd_spec %d, suport_cmd23 %d, bus_width 0x%x",
  261. csd->sd_spec, csd->suport_cmd23,
  262. UNSTUFF_BITS(resp, 48, 4));
  263. }
  264. static int mmc_send_if_cond(const struct device *mmc_dev, struct mmc_cmd *cmd, u32_t ocr)
  265. {
  266. static const u8_t test_pattern = 0xAA;
  267. u8_t result_pattern;
  268. int ret;
  269. memset(cmd, 0, sizeof(struct mmc_cmd));
  270. cmd->opcode = SD_SEND_IF_COND;
  271. cmd->arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
  272. cmd->flags = MMC_RSP_R7 | MMC_CMD_BCR;
  273. ret = mmc_send_cmd(mmc_dev, cmd);
  274. result_pattern = cmd->resp[0] & 0xFF;
  275. if (result_pattern != test_pattern)
  276. return -EIO;
  277. return 0;
  278. }
  279. static int mmc_send_app_op_cond(const struct device *mmc_dev, struct mmc_cmd *cmd, u32_t ocr, u32_t *rocr)
  280. {
  281. int i, err = 0;
  282. memset(cmd, 0, sizeof(struct mmc_cmd));
  283. cmd->opcode = SD_APP_OP_COND;
  284. cmd->arg = ocr;
  285. cmd->flags = MMC_RSP_R3 | MMC_CMD_BCR;
  286. for (i = 50; i; i--) {
  287. err = mmc_send_app_cmd(mmc_dev, 0, cmd, MMC_CMD_RETRIES);
  288. if (err)
  289. break;
  290. /* if we're just probing, do a single pass */
  291. if (ocr == 0)
  292. break;
  293. /* otherwise wait until reset completes */
  294. if (cmd->resp[0] & MMC_CARD_BUSY)
  295. break;
  296. err = -ETIMEDOUT;
  297. k_sleep(K_MSEC(20));
  298. }
  299. if (rocr)
  300. *rocr = cmd->resp[0];
  301. return err;
  302. }
  303. static int emmc_send_app_op_cond(const struct device *mmc_dev, struct mmc_cmd *cmd, u32_t ocr, u32_t *rocr)
  304. {
  305. int i, err = 0;
  306. memset(cmd, 0, sizeof(struct mmc_cmd));
  307. cmd->opcode = MMC_SEND_OP_COND;
  308. cmd->arg = ocr;
  309. cmd->flags = MMC_RSP_R3 | MMC_CMD_BCR;
  310. /* There are some eMMC need more time to power up */
  311. for (i = 100; i; i--) {
  312. err = mmc_send_cmd(mmc_dev, cmd);
  313. if (err)
  314. break;
  315. /* if we're just probing, do a single pass */
  316. if (ocr == 0)
  317. break;
  318. /* otherwise wait until reset completes */
  319. if (cmd->resp[0] & MMC_CARD_BUSY)
  320. break;
  321. err = -ETIMEDOUT;
  322. k_sleep(K_MSEC(20));
  323. }
  324. LOG_DBG("Use %d circulations to power up", i);
  325. if (rocr)
  326. *rocr = cmd->resp[0];
  327. return err;
  328. }
  329. static int mmc_sd_switch(const struct device *mmc_dev, int mode, int group,
  330. u8_t value, u8_t *resp)
  331. {
  332. int err;
  333. struct mmc_cmd cmd = {0};
  334. mode = !!mode;
  335. value &= 0xF;
  336. cmd.opcode = SD_SWITCH;
  337. cmd.arg = mode << 31 | 0x00FFFFFF;
  338. cmd.arg &= ~(0xF << (group * 4));
  339. cmd.arg |= value << (group * 4);
  340. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC | MMC_DATA_READ;
  341. cmd.blk_size = 64;
  342. cmd.blk_num = 1;
  343. cmd.buf = resp;
  344. err = mmc_send_cmd(mmc_dev, &cmd);
  345. if (err)
  346. return err;
  347. return 0;
  348. }
  349. static int emmc_switch(const struct device *mmc_dev, u8_t set, u8_t index, u8_t value)
  350. {
  351. struct mmc_cmd cmd = {0};
  352. cmd.opcode = MMC_SWITCH;
  353. cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
  354. cmd.arg = (0x03 << 24) |
  355. (index << 16) |
  356. (value << 8) | set;
  357. return mmc_send_cmd(mmc_dev, &cmd);
  358. }
  359. /*
  360. * Test if the card supports high-speed mode and, if so, switch to it.
  361. */
  362. int mmc_sd_switch_hs(const struct device *mmc_dev)
  363. {
  364. u32_t cap;
  365. int err;
  366. u8_t status[64];
  367. cap = mmc_get_capability(mmc_dev);
  368. if (!(cap & MMC_CAP_SD_HIGHSPEED))
  369. return 0;
  370. err = mmc_sd_switch(mmc_dev, 1, 0, 1, status);
  371. if (err)
  372. goto out;
  373. if ((status[16] & 0xF) != 1) {
  374. LOG_WRN("Failed to switch card to high-speed mode");
  375. err = 0;
  376. } else {
  377. err = 1;
  378. }
  379. out:
  380. return err;
  381. }
  382. int emmc_switch_hs(const struct device *mmc_dev)
  383. {
  384. int err;
  385. err = emmc_switch(mmc_dev, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
  386. if (err)
  387. return 0;
  388. return 1;
  389. }
  390. static int mmc_app_set_bus_width(const struct device *mmc_dev, struct mmc_cmd *cmd, int rca, int width)
  391. {
  392. int err;
  393. memset(cmd, 0, sizeof(struct mmc_cmd));
  394. cmd->opcode = SD_APP_SET_BUS_WIDTH;
  395. cmd->flags = MMC_RSP_R1 | MMC_CMD_AC;
  396. switch (width) {
  397. case MMC_BUS_WIDTH_1:
  398. cmd->arg = SD_BUS_WIDTH_1;
  399. break;
  400. case MMC_BUS_WIDTH_4:
  401. cmd->arg = SD_BUS_WIDTH_4;
  402. break;
  403. default:
  404. return -EINVAL;
  405. }
  406. err = mmc_send_app_cmd(mmc_dev, rca, cmd, MMC_CMD_RETRIES);
  407. if (err)
  408. return err;
  409. return 0;
  410. }
  411. static int mmc_app_send_scr(const struct device *mmc_dev, struct mmc_cmd *cmd, int rca, u32_t *scr)
  412. {
  413. int err;
  414. u32_t tmp_scr[2];
  415. memset(cmd, 0, sizeof(struct mmc_cmd));
  416. cmd->opcode = SD_APP_SEND_SCR;
  417. cmd->arg = 0;
  418. cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC | MMC_DATA_READ;
  419. cmd->blk_size = 8;
  420. cmd->blk_num = 1;
  421. cmd->buf = (u8_t *)tmp_scr;
  422. err = mmc_send_app_cmd(mmc_dev, rca, cmd, MMC_CMD_RETRIES);
  423. if (err)
  424. return err;
  425. scr[0] = sys_be32_to_cpu(tmp_scr[0]);
  426. scr[1] = sys_be32_to_cpu(tmp_scr[1]);
  427. return 0;
  428. }
  429. #if (CONFIG_MMC_SDCARD_LOW_POWER == 1)
  430. #if (CONFIG_MMC_SDCARD_LOW_POWER_SLEEP == 1)
  431. static int mmc_can_sleep(struct sd_card_data *sd)
  432. {
  433. return (sd->card_type == CARD_TYPE_MMC && sd->ext_csd.rev >= 3);
  434. }
  435. static int mmc_sleep_awake(struct sd_card_data *sd, int is_sleep)
  436. {
  437. int err;
  438. struct mmc_cmd *cmd = &sd->cmd;
  439. memset(cmd, 0, sizeof(struct mmc_cmd));
  440. cmd->opcode = MMC_SLEEP_AWAKE;
  441. cmd->flags = MMC_RSP_R1B | MMC_CMD_AC;
  442. cmd->arg = sd->rca << 16;
  443. if (is_sleep) {
  444. cmd.arg |= (1 << 15);
  445. }
  446. err = mmc_send_cmd(sd->mmc_dev, cmd);
  447. if (err) {
  448. LOG_ERR("sleep/wakeup failed, err %d", err);
  449. return err;
  450. }
  451. return 0;
  452. }
  453. #endif /* CONFIG_MMC_SDCARD_LOW_POWER_SLEEP */
  454. static int mmc_enter_low_power(struct sd_card_data *sd)
  455. {
  456. int err;
  457. sd->is_low_power = true;
  458. err = mmc_select_card(sd->mmc_dev, &sd->cmd, 0);
  459. if (err)
  460. return err;
  461. #if (CONFIG_MMC_SDCARD_LOW_POWER_SLEEP == 1)
  462. if (!mmc_can_sleep(sd))
  463. return 0;
  464. err = mmc_sleep_awake(sd, 1);
  465. #endif
  466. return err;
  467. }
  468. static int mmc_exit_low_power(struct sd_card_data *sd)
  469. {
  470. int err;
  471. if (!sd->is_low_power)
  472. return 0;
  473. sd->is_low_power = false;
  474. #if (CONFIG_MMC_SDCARD_LOW_POWER_SLEEP == 1)
  475. if (mmc_can_sleep(sd)) {
  476. err = mmc_sleep_awake(sd, 0);
  477. if (err)
  478. return err;
  479. }
  480. #endif
  481. err = mmc_select_card(sd->mmc_dev, &sd->cmd, sd->rca);
  482. return err;
  483. }
  484. #endif /* CONFIG_MMC_SDCARD_LOW_POWER */
  485. static int get_card_status(struct sd_card_data *sd, u32_t *status)
  486. {
  487. int err;
  488. err = mmc_send_status(sd->mmc_dev, &sd->cmd, sd->rca, status);
  489. return err;
  490. }
  491. int mmc_sigle_blk_rw(const struct device *dev, int is_write, unsigned int addr,
  492. void *dst, int blk_size)
  493. {
  494. struct sd_card_data *sd = dev->data;
  495. const struct device *mmc_dev = sd->mmc_dev;
  496. struct mmc_cmd *cmd = &sd->cmd;
  497. int err;
  498. memset(cmd, 0, sizeof(struct mmc_cmd));
  499. /* When transmission changed from multi-block to single block
  500. * need to send stop command on high performance mode.
  501. */
  502. if (sd->on_high_performance && (sd->next_rw_offs != SD_CARD_INVALID_OFFSET)) {
  503. LOG_DBG("%d next_rw_offs %d", __LINE__, sd->next_rw_offs);
  504. sd->next_rw_offs = SD_CARD_INVALID_OFFSET;
  505. err = mmc_stop_block_transmission(mmc_dev, cmd);
  506. if (err) {
  507. LOG_ERR("mmc stop block transmission failed, ret %d", err);
  508. return err;
  509. }
  510. }
  511. if (is_write) {
  512. cmd->opcode = MMC_WRITE_BLOCK;
  513. cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC | MMC_DATA_WRITE;
  514. } else {
  515. cmd->opcode = MMC_READ_SINGLE_BLOCK;
  516. cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC | MMC_DATA_READ;
  517. }
  518. cmd->arg = addr;
  519. cmd->blk_size = blk_size;
  520. cmd->blk_num = 1;
  521. cmd->buf = dst;
  522. err = mmc_send_cmd(mmc_dev, cmd);
  523. if (err) {
  524. LOG_ERR("sigle_blk r/w failed, ret %d", err);
  525. return err;
  526. }
  527. return 0;
  528. }
  529. int mmc_multi_blk_rw(const struct device *dev, int is_write, unsigned int addr,
  530. void *dst, int blk_size, int blk_num)
  531. {
  532. struct sd_card_data *sd = dev->data;
  533. const struct device *mmc_dev = sd->mmc_dev;
  534. struct mmc_cmd *cmd = &sd->cmd;
  535. int err;
  536. struct mmc_csd *csd = &sd->mmc_csd;
  537. /* if support cmd23, just need to send cmd23 and then cmd25/cmd18 */
  538. if (csd->suport_cmd23 && !sd->on_high_performance) {
  539. err = mmc_set_blockcount(mmc_dev, cmd, blk_num, 0);
  540. if (err) {
  541. LOG_ERR("mmc_set_blockcount r/w failed, ret %d", err);
  542. return err;
  543. }
  544. }
  545. memset(cmd, 0, sizeof(struct mmc_cmd));
  546. if (is_write) {
  547. cmd->opcode = MMC_WRITE_MULTIPLE_BLOCK;
  548. cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC | MMC_DATA_WRITE;
  549. } else {
  550. cmd->opcode = MMC_READ_MULTIPLE_BLOCK;
  551. cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC | MMC_DATA_READ;
  552. }
  553. /* record the next read/wirte offset and at next transmission if hit the read/write offset will not send stop command */
  554. if (sd->on_high_performance) {
  555. LOG_DBG("%d next_rw_offs 0x%x, prev_is_write %d, is_write %d, addr %d",
  556. __LINE__, sd->next_rw_offs, sd->prev_is_write, is_write, addr);
  557. if (sd->next_rw_offs == SD_CARD_INVALID_OFFSET) {
  558. sd->next_rw_offs = (addr + blk_num);
  559. } else {
  560. if ((sd->prev_is_write) && (is_write) && (addr == sd->next_rw_offs)) {
  561. cmd->flags = (MMC_RSP_R1 | MMC_CMD_ADTC | MMC_DATA_WRITE_DIRECT);
  562. sd->next_rw_offs += blk_num;
  563. LOG_DBG("%d, next_rw_offs 0x%x, blk_num %d", __LINE__, sd->next_rw_offs, blk_num);
  564. } else if ((!sd->prev_is_write) && (!is_write) && (addr == sd->next_rw_offs)){
  565. cmd->flags = (MMC_RSP_R1 | MMC_CMD_ADTC | MMC_DATA_READ_DIRECT);
  566. sd->next_rw_offs += blk_num;
  567. LOG_DBG("%d, next_rw_offs 0x%x, blk_num %d", __LINE__, sd->next_rw_offs, blk_num);
  568. } else {
  569. LOG_DBG("%d STOP", __LINE__);
  570. struct mmc_cmd stop_cmd = {0};
  571. err = mmc_stop_block_transmission(mmc_dev, &stop_cmd);
  572. if (err) {
  573. LOG_ERR("mmc stop block transmission failed, ret %d", err);
  574. return err;
  575. }
  576. sd->next_rw_offs = addr + blk_num;
  577. }
  578. }
  579. if (is_write)
  580. sd->prev_is_write = true;
  581. else
  582. sd->prev_is_write = false;
  583. }
  584. cmd->arg = addr;
  585. cmd->blk_size = blk_size;
  586. cmd->blk_num = blk_num;
  587. cmd->buf = dst;
  588. err = mmc_send_cmd(mmc_dev, cmd);
  589. if (err) {
  590. LOG_ERR("multi_blk r/w failed, ret %d", err);
  591. return err;
  592. }
  593. /* if not support cmd23, need to send the cmd12 to stop the transmission */
  594. if (!csd->suport_cmd23 && !sd->on_high_performance) {
  595. err = mmc_stop_block_transmission(mmc_dev, cmd);
  596. if (err) {
  597. LOG_ERR("mmc stop block transmission failed, ret %d", err);
  598. return err;
  599. }
  600. }
  601. return 0;
  602. }
  603. static int mmc_card_busy_detect(const struct device *dev, u32_t timeout_ms)
  604. {
  605. struct sd_card_data *sd = dev->data;
  606. u32_t status = 0;
  607. u32_t start_time, curr_time;
  608. start_time = k_cycle_get_32();
  609. get_card_status(sd, &status);
  610. while (!(status & R1_READY_FOR_DATA) ||
  611. R1_CURRENT_STATE(status) == R1_STATE_PRG) {
  612. LOG_DBG("card busy, status 0x%x", status);
  613. get_card_status(sd, &status);
  614. curr_time = k_cycle_get_32();
  615. if (k_cyc_to_us_floor32(curr_time - start_time)
  616. >= (timeout_ms * 1000)) {
  617. LOG_ERR("wait card busy timeout, status 0x%x", status);
  618. return -ETIMEDOUT;
  619. }
  620. }
  621. return 0;
  622. }
  623. int sd_card_rw(const struct device *dev, int rw, u32_t sector_offset,
  624. void *data, size_t sector_cnt)
  625. {
  626. struct sd_card_data *sd = dev->data;
  627. struct mmc_csd *csd = &sd->mmc_csd;
  628. u32_t addr, chunk_sector_cnt, chunk_size;
  629. int err = 0;
  630. u32_t i;
  631. /* forbidden sd rw in isr environment */
  632. if (sd_check_rw_env())
  633. return -1;
  634. #ifdef CONFIG_PM_DEVICE
  635. if(sd->b_sd_suspend){
  636. k_work_submit_to_queue(&sd->sd_workq, &sd->work);
  637. for(i = 0; i < 300; i++){ // overtime 3S
  638. k_msleep(10);
  639. if(!sd->b_sd_suspend)
  640. break;
  641. }
  642. if(i == 300){
  643. printk("sd card suspend, wait over err\n");
  644. return ENOENT;
  645. }
  646. }
  647. #endif
  648. #if (CONFIG_MMC_SDCARD_SHOW_PERF == 1)
  649. u32_t start_time, end_time, delta, cur_xfer_size;
  650. start_time = k_cycle_get_32();
  651. cur_xfer_size = sector_cnt * SD_CARD_SECTOR_SIZE;
  652. #endif
  653. LOG_DBG("rw%d, sector_offset 0x%x, data %p, sector_cnt %zu\n",
  654. rw, sector_offset, data, sector_cnt);
  655. if (!sd->card_initialized) {
  656. return -ENOENT;
  657. }
  658. if ((sector_offset + sector_cnt) > csd->sector_count) {
  659. return -EINVAL;
  660. }
  661. sd_sem_take(&sd->lock, K_FOREVER);
  662. #if (CONFIG_MMC_SDCARD_LOW_POWER == 1)
  663. mmc_exit_low_power(sd);
  664. #endif
  665. addr = csd->ccs ? sector_offset :
  666. sector_offset * SD_CARD_SECTOR_SIZE;
  667. /* The number of blocks at multi block transmision is 65536 in sdmmc protocol */
  668. if (sd->on_high_performance)
  669. chunk_sector_cnt = SD_CARD_RW_MAX_SECTOR_CNT_PROTOCOL;
  670. else
  671. chunk_sector_cnt = SD_CARD_RW_MAX_SECTOR_CNT;
  672. while (sector_cnt > 0) {
  673. if (sector_cnt < chunk_sector_cnt) {
  674. chunk_sector_cnt = sector_cnt;
  675. }
  676. chunk_size = chunk_sector_cnt * SD_CARD_SECTOR_SIZE;
  677. if (chunk_sector_cnt > 1) {
  678. for (i = 0; i < (CONFIG_MMC_SDCARD_ERR_RETRY_NUM + 1); i++) {
  679. err = mmc_multi_blk_rw(dev, rw, addr, data,
  680. SD_CARD_SECTOR_SIZE, chunk_sector_cnt);
  681. if (!err)
  682. break;
  683. }
  684. } else {
  685. for (i = 0; i < (CONFIG_MMC_SDCARD_ERR_RETRY_NUM + 1); i++) {
  686. err = mmc_sigle_blk_rw(dev, rw, addr, data,
  687. SD_CARD_SECTOR_SIZE);
  688. if (!err)
  689. break;
  690. }
  691. }
  692. /* we need to wait the card program status when write mode */
  693. if (rw)
  694. err |= mmc_card_busy_detect(dev, SD_CARD_WAITBUSY_TIMEOUT_MS);
  695. if (err) {
  696. LOG_ERR("Failed: rw:%d, addr:0x%x, scnt:0x%x!",
  697. rw, addr, chunk_sector_cnt);
  698. /* card error, need reinitialize */
  699. sd->force_plug_out = true;
  700. err = -EIO;
  701. break;
  702. }
  703. if (csd->ccs)
  704. addr += chunk_sector_cnt;
  705. else
  706. addr += chunk_size;
  707. data = (void *)((uint32_t)data + chunk_size);
  708. sector_cnt -= chunk_sector_cnt;
  709. }
  710. #if (CONFIG_MMC_SDCARD_LOW_POWER == 1)
  711. mmc_enter_low_power(sd);
  712. #endif
  713. sd_sem_give(&sd->lock);
  714. #if (CONFIG_MMC_SDCARD_SHOW_PERF == 1)
  715. end_time = k_cycle_get_32();
  716. delta = k_cyc_to_us_floor32(end_time - start_time);
  717. if (rw) {
  718. sd->rec_perf_wr_size += cur_xfer_size;
  719. if (delta > sd->max_wr_use_time)
  720. sd->max_wr_use_time = delta;
  721. } else {
  722. sd->rec_perf_rd_size += cur_xfer_size;
  723. if (delta > sd->max_rd_use_time)
  724. sd->max_rd_use_time = delta;
  725. }
  726. if (k_cyc_to_us_floor32(k_cycle_get_32()
  727. - sd->rec_perf_timestamp) > 1000000UL) {
  728. LOG_INF("bandwidth read %dB/s write %dB/s",
  729. sd->rec_perf_rd_size, sd->rec_perf_wr_size);
  730. LOG_INF("current rw:%d speed %dB/s",
  731. rw, cur_xfer_size * 1000000UL / delta);
  732. LOG_INF("max read time %dus, max write time %dus",
  733. sd->max_rd_use_time, sd->max_wr_use_time);
  734. sd->rec_perf_timestamp = k_cycle_get_32();
  735. sd->rec_perf_rd_size = 0;
  736. sd->rec_perf_wr_size = 0;
  737. }
  738. #endif
  739. return err;
  740. }
  741. static int sd_scan_host(const struct device *dev)
  742. {
  743. struct sd_card_data *sd = dev->data;
  744. const struct device *mmc_dev = sd->mmc_dev;
  745. int err;
  746. u32_t ocr, rocr;
  747. u32_t cid_csd[4];
  748. u32_t scr[16] = {0};
  749. u32_t caps;
  750. ocr = 0x40ff8000;
  751. sd_sem_take(&sd->lock, K_FOREVER);
  752. /* init controller default clock and width */
  753. mmc_set_clock(mmc_dev, SD_CARD_INIT_CLOCK_FREQ);
  754. mmc_set_bus_width(mmc_dev, MMC_BUS_WIDTH_1);
  755. mmc_go_idle(mmc_dev, &sd->cmd);
  756. mmc_send_if_cond(mmc_dev, &sd->cmd, ocr);
  757. /* try CARD_TYPE_SD first */
  758. sd->card_type = CARD_TYPE_SD;
  759. err = mmc_send_app_op_cond(mmc_dev, &sd->cmd, ocr, &rocr);
  760. if (err) {
  761. /* try eMMC card */
  762. err = emmc_send_app_op_cond(mmc_dev, &sd->cmd, ocr, &rocr);
  763. if (err)
  764. goto out;
  765. sd->card_type = CARD_TYPE_MMC;
  766. }
  767. sd->mmc_csd.ccs = (rocr & SD_OCR_CCS)? 1 : 0;
  768. err = mmc_all_send_cid(mmc_dev, &sd->cmd, cid_csd);
  769. if (err)
  770. goto out;
  771. if (sd->card_type == CARD_TYPE_MMC) {
  772. sd->rca = 1;
  773. sd->mmc_csd.suport_cmd23 = 1;
  774. err = emmc_send_relative_addr(mmc_dev, &sd->cmd, &sd->rca);
  775. if (err)
  776. goto out;
  777. err = mmc_send_csd(mmc_dev, &sd->cmd, sd->rca, cid_csd);
  778. if (err)
  779. goto out;
  780. err = emmc_decode_csd(sd, cid_csd);
  781. if (err)
  782. goto out;
  783. err = mmc_select_card(mmc_dev, &sd->cmd, sd->rca);
  784. if (err)
  785. goto out;
  786. } else {
  787. err = mmc_send_relative_addr(mmc_dev, &sd->cmd, &sd->rca);
  788. if (err)
  789. goto out;
  790. err = mmc_send_csd(mmc_dev, &sd->cmd, sd->rca, cid_csd);
  791. if (err)
  792. goto out;
  793. err = mmc_decode_csd(sd, cid_csd);
  794. if (err)
  795. goto out;
  796. err = mmc_select_card(mmc_dev, &sd->cmd, sd->rca);
  797. if (err)
  798. goto out;
  799. err = mmc_app_send_scr(mmc_dev, &sd->cmd, sd->rca, scr);
  800. if (err)
  801. goto out;
  802. mmc_decode_scr(sd, scr);
  803. }
  804. /* set bus speed */
  805. if (CARD_TYPE_SD == sd->card_type)
  806. err = mmc_sd_switch_hs(mmc_dev);
  807. else
  808. err = emmc_switch_hs(mmc_dev);
  809. if (err > 0) {
  810. mmc_set_clock(mmc_dev, SD_CARD_SDR16_CLOCK_FREQ);
  811. } else {
  812. mmc_set_clock(mmc_dev, SD_CARD_SDR8_CLOCK_FREQ);
  813. }
  814. /* set bus width */
  815. caps = mmc_get_capability(mmc_dev);
  816. if (caps & MMC_CAP_8_BIT_DATA) {
  817. if (CARD_TYPE_SD == sd->card_type) {
  818. err = mmc_app_set_bus_width(mmc_dev, &sd->cmd, sd->rca, MMC_BUS_WIDTH_4);
  819. if (err)
  820. goto out;
  821. mmc_set_bus_width(mmc_dev, MMC_BUS_WIDTH_4);
  822. } else {
  823. err = emmc_switch(mmc_dev, EXT_CSD_CMD_SET_NORMAL,
  824. EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_8);
  825. if (err)
  826. goto out;
  827. mmc_set_bus_width(mmc_dev, MMC_BUS_WIDTH_8);
  828. }
  829. } else if (caps & MMC_CAP_4_BIT_DATA) {
  830. if (CARD_TYPE_SD == sd->card_type) {
  831. err = mmc_app_set_bus_width(mmc_dev, &sd->cmd, sd->rca, MMC_BUS_WIDTH_4);
  832. if (err)
  833. goto out;
  834. } else {
  835. err = emmc_switch(mmc_dev, EXT_CSD_CMD_SET_NORMAL,
  836. EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4);
  837. if (err)
  838. goto out;
  839. }
  840. mmc_set_bus_width(mmc_dev, MMC_BUS_WIDTH_4);
  841. }
  842. /* FIXME: workaround to put ext_csd reading here, since fail just after reading csd at present */
  843. if (sd->card_type == CARD_TYPE_MMC && sd->ext_csd.mmca_vsn >= CSD_SPEC_VER_4) {
  844. u8_t ext_csd[512];
  845. err = mmc_send_ext_csd(mmc_dev, &sd->cmd, ext_csd);
  846. if (err) {
  847. goto out;
  848. }
  849. err = emmc_read_ext_csd(sd, ext_csd);
  850. if (err) {
  851. goto out;
  852. }
  853. }
  854. if (sd->mmc_csd.sector_size != SD_CARD_SECTOR_SIZE) {
  855. sd->mmc_csd.sector_count = sd->mmc_csd.sector_count *
  856. (sd->mmc_csd.sector_size / SD_CARD_SECTOR_SIZE);
  857. sd->mmc_csd.sector_size = SD_CARD_SECTOR_SIZE;
  858. }
  859. err = 0;
  860. sd->card_initialized = true;
  861. sd->next_rw_offs = SD_CARD_INVALID_OFFSET;
  862. sd->on_high_performance = false;
  863. #if (CONFIG_MMC_SDCARD_SHOW_PERF == 1)
  864. sd->max_rd_use_time = 0;
  865. sd->max_wr_use_time = 0;
  866. sd->rec_perf_rd_size = 0;
  867. sd->rec_perf_wr_size = 0;
  868. sd->rec_perf_timestamp = 0;
  869. #endif
  870. LOG_INF("sdcard is plugged");
  871. out:
  872. sd_sem_give(&sd->lock);
  873. return err;
  874. }
  875. #if (CONFIG_SD_USE_GPIO_DET == 1)
  876. static int sd_card_check_card_by_gpio(const struct device *dev, struct sd_card_data *sd)
  877. {
  878. int value;
  879. const struct sd_acts_config *cfg = dev->config;
  880. /* Depends on card detect task for debounce */
  881. value = gpio_pin_get(sd->detect_gpio_dev, (cfg->detect_gpio % 32));
  882. if (value < 0)
  883. return -EIO;
  884. if (value != cfg->detect_gpio_level) {
  885. /* card is not detected */
  886. return false;
  887. }
  888. /* card is detected */
  889. return true;
  890. }
  891. /*
  892. * return
  893. * 0 disk ok
  894. * 1 STA_NOINIT
  895. * 2 STA_NODISK
  896. * other: unknow error
  897. */
  898. static int sd_card_detect(const struct device *dev)
  899. {
  900. struct sd_card_data *sd = dev->data;
  901. if (sd->force_plug_out) {
  902. LOG_INF("sdcard plug out forcely due to rw error");
  903. sd->force_plug_out = false;
  904. sd->card_initialized = false;
  905. return STA_NODISK;
  906. }
  907. if (sd_card_check_card_by_gpio(dev, sd) == true) {
  908. if (sd->card_initialized)
  909. return STA_DISK_OK;
  910. else
  911. return STA_NOINIT;
  912. } else {
  913. mmc_release_device(sd->mmc_dev);
  914. sd->card_initialized = false;
  915. }
  916. return STA_NODISK;
  917. }
  918. #else
  919. static int sd_card_is_unplugged(struct sd_card_data *sd)
  920. {
  921. const int retry_times = 3;
  922. int err, i;
  923. uint32_t status;
  924. /* assume emmc is always exist */
  925. if (sd->card_type == CARD_TYPE_MMC) {
  926. return false;
  927. }
  928. sd_sem_take(&sd->lock, K_FOREVER);
  929. for (i = 0; i < retry_times; i++) {
  930. err = get_card_status(sd, &status);
  931. status = R1_CURRENT_STATE(status);
  932. if (!err && (status == R1_STATE_TRAN ||
  933. (status == R1_STATE_STBY) ||
  934. (status == R1_STATE_RCV) ||
  935. (status == R1_STATE_DATA))) {
  936. break;
  937. }
  938. }
  939. sd_sem_give(&sd->lock);
  940. if (i == retry_times) {
  941. return true;
  942. }
  943. return false;
  944. }
  945. static int sd_card_detect(const struct device *dev)
  946. {
  947. struct sd_card_data *sd = dev->data;
  948. int err, ret = STA_NODISK;
  949. if (sd->force_plug_out) {
  950. LOG_INF("sdcard plug out forcely due to rw error");
  951. sd->force_plug_out = false;
  952. sd->card_initialized = false;
  953. return STA_NODISK;
  954. }
  955. /* check card status */
  956. if (!sd->card_initialized) {
  957. /* detect card by send init commands */
  958. err = sd_card_storage_init(dev);
  959. if (!err) {
  960. ret = STA_DISK_OK;
  961. }
  962. } else {
  963. if (sd_card_is_unplugged(sd)) {
  964. LOG_INF("sdcard is unplugged");
  965. sd->card_initialized = false;
  966. } else {
  967. ret = STA_DISK_OK;
  968. }
  969. }
  970. return ret;
  971. }
  972. #endif /* CONFIG_SD_USE_GPIO_DET == 1 */
  973. #if 0
  974. int sd_card_scan_delay_chain(struct device *dev)
  975. {
  976. struct sd_card_data *sd = dev->data;
  977. int err;
  978. uint32_t status;
  979. uint8_t rd;
  980. printk("%s: scan sd card delay chain\n", __func__);
  981. for (rd = 0; rd < 0x0f; rd++) {
  982. printk("%s: scan read delay chain: %d\n", __func__, rd);
  983. sd_card_set_delay_chain(dev, rd, 0xff);
  984. err = get_card_status(sd, &status);
  985. status = R1_CURRENT_STATE(status);
  986. if (err || (status != R1_STATE_TRAN &&
  987. status != R1_STATE_STBY &&
  988. status != R1_STATE_RCV)) {
  989. continue;
  990. }
  991. err = sd_card_storage_read(dev, 0, tmp_card_buf, 1);
  992. if (err) {
  993. continue;
  994. }
  995. printk("%s: scan read delay chain: %d pass\n", __func__, rd);
  996. }
  997. return 0;
  998. }
  999. #endif
  1000. #if (CONFIG_SD_USE_GPIO_POWER == 1)
  1001. static int board_mmc0_pullup_disable(const struct device *dev,
  1002. const struct acts_pin_config *p_pin_config, uint8_t pin_num)
  1003. {
  1004. const struct device *sd_gpio_dev = NULL;
  1005. uint8_t i;
  1006. if (!p_pin_config || !pin_num) {
  1007. LOG_ERR("invalid pin config (%p, %d)", p_pin_config, pin_num);
  1008. return -EINVAL;
  1009. }
  1010. for (i = 0; i < pin_num; i++) {
  1011. sd_gpio_dev = device_get_binding(CONFIG_GPIO_PIN2NAME(p_pin_config[i].pin_num));
  1012. if (!sd_gpio_dev) {
  1013. LOG_ERR("Failed to bind SD power GPIO(%d:%s)",
  1014. p_pin_config[i].pin_num,
  1015. CONFIG_GPIO_PIN2NAME(p_pin_config[i].pin_num));
  1016. return -ENODEV;
  1017. }
  1018. gpio_pin_configure(sd_gpio_dev, (p_pin_config[i].pin_num % 32), GPIO_OUTPUT);
  1019. LOG_DBG("set sdmmc pin(%d) to gpio", p_pin_config[i].pin_num);
  1020. /* sd pins output low level to avoid leakage */
  1021. gpio_pin_set(sd_gpio_dev, (p_pin_config[i].pin_num % 32), 0);
  1022. }
  1023. return 0;
  1024. }
  1025. static void board_mmc0_pullup_enable(const struct device *dev,
  1026. const struct acts_pin_config *p_pin_config, uint8_t pin_num)
  1027. {
  1028. /* restore origin pullup pinmux config */
  1029. acts_pinmux_setup_pins(p_pin_config, pin_num);
  1030. }
  1031. static int mmc_power_gpio_reset(const struct device *dev, uint32_t wait_ms)
  1032. {
  1033. const struct sd_acts_config *cfg = dev->config;
  1034. struct board_pinmux_info pinmux_info;
  1035. #ifndef CONFIG_SD_USE_IOVCC1_POWER
  1036. struct sd_card_data *sd = dev->data;
  1037. int ret;
  1038. uint8_t power_gpio = cfg->power_gpio % 32;
  1039. #endif
  1040. board_get_mmc0_pinmux_info(&pinmux_info);
  1041. #ifdef CONFIG_SD_USE_IOVCC1_POWER
  1042. /* high-z iovcc1 to power off mmc card */
  1043. if (cfg->use_power_gpio)
  1044. sys_write32(0x1000, GPION_CTL(cfg->power_gpio));
  1045. #else
  1046. sd->power_gpio_dev = device_get_binding(CONFIG_GPIO_PIN2NAME(cfg->power_gpio));
  1047. if (!sd->power_gpio_dev) {
  1048. LOG_ERR("Failed to bind SD power GPIO(%d:%s)",
  1049. cfg->power_gpio, CONFIG_GPIO_PIN2NAME(cfg->power_gpio));
  1050. return -ENODEV;
  1051. }
  1052. ret = gpio_pin_configure(sd->power_gpio_dev, power_gpio, GPIO_OUTPUT);
  1053. if (ret) {
  1054. LOG_ERR("Failed to config output GPIO:%d", power_gpio);
  1055. return ret;
  1056. }
  1057. /* power off mmc card */
  1058. gpio_pin_set(sd->power_gpio_dev, power_gpio, !cfg->power_gpio_level);
  1059. #endif
  1060. /* disable mmc0 pull-up to avoid leakage */
  1061. board_mmc0_pullup_disable(dev, pinmux_info.pins_config, pinmux_info.pins_num);
  1062. k_sleep(K_MSEC(wait_ms));
  1063. #ifdef CONFIG_SD_USE_IOVCC1_POWER
  1064. /* iovcc1 output to power on mmc card */
  1065. if (cfg->use_power_gpio)
  1066. sys_write32(0x1f, GPION_CTL(cfg->power_gpio));
  1067. #else
  1068. /* power on mmc card */
  1069. gpio_pin_set(sd->power_gpio_dev, power_gpio, cfg->power_gpio_level);
  1070. #endif
  1071. k_sleep(K_MSEC(10));
  1072. /* restore mmc0 pull-up */
  1073. board_mmc0_pullup_enable(dev, pinmux_info.pins_config, pinmux_info.pins_num);
  1074. return 0;
  1075. }
  1076. #endif
  1077. static int mmc_power_reset(const struct device *dev, uint32_t wait_ms)
  1078. {
  1079. #if (CONFIG_SD_USE_GPIO_POWER == 1)
  1080. const struct sd_acts_config *cfg = dev->config;
  1081. struct sd_card_data *sd = dev->data;
  1082. mmc_power_gpio_reset(dev, wait_ms);
  1083. if (cfg->use_detect_gpio
  1084. && (cfg->detect_gpio == cfg->use_power_gpio)) {
  1085. gpio_pin_configure(sd->detect_gpio_dev,
  1086. (cfg->detect_gpio % 32), GPIO_INPUT);
  1087. }
  1088. #endif
  1089. return 0;
  1090. }
  1091. static int sd_card_power_reset(const struct device *dev, uint32_t wait_ms)
  1092. {
  1093. return mmc_power_reset(dev, wait_ms);
  1094. }
  1095. int sd_card_storage_init(const struct device *dev)
  1096. {
  1097. struct sd_card_data *sd = dev->data;
  1098. int ret;
  1099. u8_t i, cnt;
  1100. if (!sd->mmc_dev)
  1101. return -ENODEV;
  1102. /* In case of without detect pin will depend on the hotplug thread to initialize */
  1103. cnt = CONFIG_MMC_SDCARD_RETRY_TIMES;
  1104. sd_sem_take(&sd->lock, K_FOREVER);
  1105. if (sd->card_initialized) {
  1106. LOG_DBG("SD card has already initialized!");
  1107. sd_sem_give(&sd->lock);
  1108. return 0;
  1109. }
  1110. sd_sem_give(&sd->lock);
  1111. for (i = 0; i < cnt; i++) {
  1112. sd_card_power_reset(dev, CONFIG_SD_CARD_POWER_RESET_MS * cnt);
  1113. ret = sd_scan_host(dev);
  1114. if (!ret)
  1115. break;
  1116. else
  1117. LOG_DBG("SD card storage init failed");
  1118. #if (CONFIG_SD_USE_GPIO_DET == 1)
  1119. if (sd_card_check_card_by_gpio(dev, sd) == false) {
  1120. /* sd cards host plug debounce */
  1121. k_sleep(K_MSEC(CONFIG_SD_CARD_HOTPLUG_DEBOUNCE_MS));
  1122. if (sd_card_check_card_by_gpio(dev, sd) == false) {
  1123. LOG_ERR("SD card is not detected");
  1124. break;
  1125. }
  1126. }
  1127. #endif
  1128. }
  1129. if (!ret) {
  1130. LOG_INF("SD card storage initialized!\n");
  1131. } else {
  1132. ret = -ENODEV;
  1133. }
  1134. return ret;
  1135. }
  1136. static int sd_card_storage_read(const struct device *dev, uint64_t offset, void *data,
  1137. uint64_t len)
  1138. {
  1139. offset = offset >> 9;
  1140. len = len >> 9;
  1141. return sd_card_rw((struct device *)dev, 0, offset, data, len);
  1142. }
  1143. static int sd_card_storage_write(const struct device *dev, uint64_t offset,
  1144. const void *data, uint64_t len)
  1145. {
  1146. offset = offset >> 9;
  1147. len = len >> 9;
  1148. return sd_card_rw((struct device *)dev, 1, offset, (void *)data, len);
  1149. }
  1150. static int sd_card_storage_erase(const struct device *dev, uint64_t offset, uint64_t len)
  1151. {
  1152. printk("%s offset %llx , len %lld \n",__FUNCTION__, offset, len);
  1153. return 0;
  1154. }
  1155. static int sd_card_enter_high_speed(const struct device *dev)
  1156. {
  1157. struct sd_card_data *sd = dev->data;
  1158. const struct sd_acts_config *cfg = dev->config;
  1159. if (!cfg->use_detect_gpio) {
  1160. LOG_ERR("high speed only support with detect pin");
  1161. return -ENOTSUP;
  1162. }
  1163. sd_sem_take(&sd->lock, K_FOREVER);
  1164. if (sd->on_high_performance) {
  1165. LOG_DBG("already enter high speed mode");
  1166. goto out;
  1167. }
  1168. sd->on_high_performance = true;
  1169. out:
  1170. sd_sem_give(&sd->lock);
  1171. LOG_INF("enter high speed mode");
  1172. return 0;
  1173. }
  1174. static int sd_card_exit_high_speed(const struct device *dev)
  1175. {
  1176. int ret;
  1177. u32_t status;
  1178. struct sd_card_data *sd = dev->data;
  1179. const struct sd_acts_config *cfg = dev->config;
  1180. if (!cfg->use_detect_gpio)
  1181. return 0;
  1182. sd_sem_take(&sd->lock, K_FOREVER);
  1183. if (!sd->on_high_performance) {
  1184. LOG_DBG("already exit high speed mode");
  1185. ret = 0;
  1186. goto out;
  1187. }
  1188. ret = mmc_send_status(sd->mmc_dev, &sd->cmd, sd->rca, &status);
  1189. if (!ret) {
  1190. status = R1_CURRENT_STATE(status);
  1191. if ((status == R1_STATE_DATA) || (status == R1_STATE_RCV)) {
  1192. LOG_INF("status:0x%x send stop command", status);
  1193. mmc_stop_block_transmission(sd->mmc_dev, &sd->cmd);
  1194. }
  1195. }
  1196. sd->on_high_performance = false;
  1197. sd->next_rw_offs = SD_CARD_INVALID_OFFSET;
  1198. LOG_INF("sd->next_rw_offs %d", sd->next_rw_offs);
  1199. out:
  1200. sd_sem_give(&sd->lock);
  1201. LOG_INF("exit high speed mode");
  1202. return ret;
  1203. }
  1204. int sd_card_storage_ioctl(const struct device *dev, uint8_t cmd, void *buff)
  1205. {
  1206. struct sd_card_data *sd = dev->data;
  1207. int ret;
  1208. if (!sd->card_initialized && (cmd != DISK_IOCTL_HW_DETECT)) {
  1209. return -ENOENT;
  1210. }
  1211. switch (cmd) {
  1212. case DISK_IOCTL_CTRL_SYNC:
  1213. break;
  1214. case DISK_IOCTL_GET_SECTOR_COUNT:
  1215. *(uint32_t *)buff = sd->mmc_csd.sector_count;
  1216. break;
  1217. case DISK_IOCTL_GET_SECTOR_SIZE:
  1218. *(uint32_t *)buff = sd->mmc_csd.sector_size;
  1219. break;
  1220. case DISK_IOCTL_GET_ERASE_BLOCK_SZ:
  1221. //*(uint32_t *)buff = (sd->mmc_csd.ccs) ? 1 : SD_CARD_SECTOR_SIZE;
  1222. *(uint32_t *)buff = SD_CARD_SECTOR_SIZE;
  1223. break;
  1224. #if 0
  1225. case DISK_IOCTL_GET_DISK_SIZE:
  1226. /* > 4GByte??, add 2GByte replase */
  1227. *(uint32_t *)buff = (sd->mmc_csd.ccs)? 0x80000000 :
  1228. (sd->mmc_csd.sector_size * sd->mmc_csd.sector_count);
  1229. #endif
  1230. break;
  1231. case DISK_IOCTL_HW_DETECT:
  1232. ret = sd_card_detect(dev);
  1233. if (STA_NOINIT == ret || STA_DISK_OK == ret) {
  1234. *(uint8_t *)buff = STA_DISK_OK;
  1235. } else {
  1236. *(uint8_t *)buff = STA_NODISK;
  1237. }
  1238. break;
  1239. case DISK_IOCTL_ENTER_HIGH_SPEED:
  1240. ret = sd_card_enter_high_speed(dev);
  1241. if (ret)
  1242. return ret;
  1243. break;
  1244. case DISK_IOCTL_EXIT_HIGH_SPEED:
  1245. ret = sd_card_exit_high_speed(dev);
  1246. if (ret)
  1247. return ret;
  1248. break;
  1249. default:
  1250. return -EINVAL;
  1251. }
  1252. return 0;
  1253. }
  1254. static const struct flash_driver_api sd_card_storage_api = {
  1255. .read = sd_card_storage_read,
  1256. .write = sd_card_storage_write,
  1257. .erase = sd_card_storage_erase,
  1258. };
  1259. static void sd_card_resume_work(struct k_work *work);
  1260. static int sd_card_init(const struct device *dev)
  1261. {
  1262. const struct sd_acts_config *cfg = dev->config;
  1263. struct sd_card_data *sd = dev->data;
  1264. int ret = 0;
  1265. LOG_INF("sd_card_init");
  1266. sd->mmc_dev = (struct device *)device_get_binding(cfg->mmc_dev_name);
  1267. if (!sd->mmc_dev) {
  1268. LOG_ERR("Cannot find mmc device %s!\n", cfg->mmc_dev_name);
  1269. return -ENODEV;
  1270. }
  1271. if (cfg->use_detect_gpio) {
  1272. sd->detect_gpio_dev = device_get_binding(CONFIG_GPIO_PIN2NAME(cfg->detect_gpio));
  1273. if (!sd->detect_gpio_dev) {
  1274. LOG_ERR("Failed to bind SD detect GPIO(%d:%s)",
  1275. cfg->detect_gpio, CONFIG_GPIO_PIN2NAME(cfg->detect_gpio));
  1276. return -ENODEV;
  1277. }
  1278. /* switch gpio function to input for detecting card plugin */
  1279. ret = gpio_pin_configure(sd->detect_gpio_dev,
  1280. (cfg->detect_gpio % 32), GPIO_INPUT);
  1281. if (ret)
  1282. return ret;
  1283. }
  1284. k_sem_init(&sd->lock, 1, 1);
  1285. sd->is_low_power = false;
  1286. sd->card_initialized = false;
  1287. sd->force_plug_out = false;
  1288. #ifdef CONFIG_PM_DEVICE
  1289. struct k_work_queue_config wk_cfg = {
  1290. .name = "sd_workq",
  1291. .no_yield = false,
  1292. };
  1293. k_work_queue_start(&sd->sd_workq,
  1294. sd_workq_stack,
  1295. K_KERNEL_STACK_SIZEOF(sd_workq_stack), 1, &wk_cfg);
  1296. sd->sd_dev = dev;
  1297. k_work_init(&sd->work, sd_card_resume_work);
  1298. sd->b_sd_suspend = false;
  1299. sd->b_sd_resume = false;
  1300. #endif
  1301. /* assume that the type of mmc medium is SD card */
  1302. sd->card_type = CARD_TYPE_SD;
  1303. sd_card_storage_init(dev);
  1304. return ret;
  1305. }
  1306. #ifdef CONFIG_PM_DEVICE
  1307. static void sd_card_resume_work(struct k_work *work)
  1308. {
  1309. struct sd_card_data *sd = CONTAINER_OF(work,
  1310. struct sd_card_data, work);
  1311. unsigned int cyc;
  1312. if(sd->b_sd_suspend){
  1313. sd->b_sd_resume = true;
  1314. cyc = k_cycle_get_32();
  1315. sd_card_storage_init(sd->sd_dev);
  1316. cyc = k_cycle_get_32()-cyc;
  1317. printk("sd card resume use =%d ms\n", k_cyc_to_ms_ceil32(cyc));
  1318. sd->b_sd_resume = false;
  1319. sd->b_sd_suspend = false;
  1320. }
  1321. }
  1322. int sd_card_pm_control(const struct device *dev, enum pm_device_action action)
  1323. {
  1324. const struct sd_acts_config *cfg = dev->config;
  1325. struct sd_card_data *sd = dev->data;
  1326. struct board_pinmux_info pinmux_info;
  1327. switch (action) {
  1328. case PM_DEVICE_ACTION_RESUME:
  1329. break;
  1330. case PM_DEVICE_ACTION_SUSPEND:
  1331. if(sd->b_sd_resume)/*if sd reusme, can not suspend*/
  1332. return -1;
  1333. if(!k_sem_count_get(&sd->lock)){
  1334. printk("sd now used, nor suspend\n");
  1335. return -1;
  1336. }
  1337. board_get_mmc0_pinmux_info(&pinmux_info);
  1338. /* power off mmc card */
  1339. #ifdef CONFIG_SD_USE_IOVCC1_POWER
  1340. /* high-z iovcc1 */
  1341. if (cfg->use_power_gpio)
  1342. sys_write32(0x1000, GPION_CTL(cfg->power_gpio));
  1343. #else
  1344. if(sd->power_gpio_dev)
  1345. gpio_pin_set(sd->power_gpio_dev, cfg->power_gpio % 32, !cfg->power_gpio_level);
  1346. #endif
  1347. #if (CONFIG_SD_USE_GPIO_POWER == 1)
  1348. /* disable mmc0 pull-up to avoid leakage */
  1349. board_mmc0_pullup_disable(dev, pinmux_info.pins_config, pinmux_info.pins_num);
  1350. #endif
  1351. sd->b_sd_suspend = true;
  1352. sd->is_low_power = false;
  1353. sd->card_initialized = false;
  1354. sd->force_plug_out = false;
  1355. break;
  1356. case PM_DEVICE_ACTION_EARLY_SUSPEND:
  1357. break;
  1358. case PM_DEVICE_ACTION_LATE_RESUME:
  1359. k_work_submit_to_queue(&sd->sd_workq, &sd->work);
  1360. break;
  1361. default:
  1362. return 0;
  1363. }
  1364. return 0;
  1365. }
  1366. #else
  1367. #define sd_card_pm_control NULL
  1368. #endif
  1369. #define gpio_det_use(n) (\
  1370. .detect_gpio = CONFIG_SD_GPIO_DET_NUM,\
  1371. .detect_gpio_level = CONFIG_SD_GPIO_DET_LEVEL,\
  1372. .use_detect_gpio = 1, \
  1373. )
  1374. #define gpio_det_not(n) (\
  1375. .use_detect_gpio = 0, \
  1376. )
  1377. #define gpio_power_use(n) (\
  1378. .power_gpio = CONFIG_SD_GPIO_POWER_NUM,\
  1379. .power_gpio_level = CONFIG_SD_GPIO_POWER_LEVEL,\
  1380. .use_power_gpio = 1, \
  1381. )
  1382. #define gpio_power_not(n) (\
  1383. .use_power_gpio = 0, \
  1384. )
  1385. static const struct sd_acts_config sd_acts_config_0 = {
  1386. .mmc_dev_name = CONFIG_SD_MMC_DEV,
  1387. COND_CODE_1(CONFIG_SD_USE_GPIO_DET, gpio_det_use(0), gpio_det_not(0))
  1388. COND_CODE_1(CONFIG_SD_USE_GPIO_POWER, gpio_power_use(0), gpio_power_not(0))
  1389. };
  1390. struct sd_card_data sdcard_acts_data_0;
  1391. DEVICE_DEFINE(sd_storage_0, CONFIG_SD_NAME, sd_card_init,
  1392. sd_card_pm_control,
  1393. &sdcard_acts_data_0, &sd_acts_config_0,
  1394. POST_KERNEL, 35,
  1395. &sd_card_storage_api);
  1396. #else //#if IS_ENABLED(CONFIG_SD)
  1397. int sd_card_storage_init(const struct device *dev)
  1398. {
  1399. return -ENODEV;
  1400. }
  1401. int sd_card_storage_ioctl(const struct device *dev, uint8_t cmd, void *buff)
  1402. {
  1403. return -ENODEV;
  1404. }
  1405. #endif //END #if IS_ENABLED(CONFIG_SD)