sd.c 36 KB

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