sdio.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292
  1. /*
  2. * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the
  12. * distribution.
  13. * 3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "stdint.h"
  30. #include "sys/param.h"
  31. #include "sys/endian.h"
  32. #include "hal_sdhost.h"
  33. #include "sdmmc.h"
  34. #include "sdio.h"
  35. #include "_sd_define.h"
  36. #include "_core.h"
  37. #include "_sdio.h"
  38. #ifdef CONFIG_USE_SDIO
  39. //inline int mmc_io_rw_direct(struct mmc_card *card, int32_t write, uint32_t fn, uint32_t addr, uint8_t in, uint8_t *out);
  40. #define SDIO_MIN(a, b) MIN(a, b)
  41. /*
  42. * Calculate the maximum byte mode transfer size
  43. */
  44. static inline unsigned int sdio_max_byte_size(struct sdio_func *func)
  45. {
  46. unsigned mval = func->card->host->max_blk_size;
  47. //if (mmc_blksz_for_byte_mode(func->card))
  48. // mval = min(mval, func->cur_blksize);
  49. //else
  50. mval = SDIO_MIN(mval, func->max_blksize);
  51. //if (mmc_card_broken_byte_mode_512(func->card))
  52. return SDIO_MIN(mval, 511u);
  53. return SDIO_MIN(mval, 512u); /* maximum size for byte mode */
  54. }
  55. #ifdef CONFIG_SDIO_USE_FUNS
  56. static int32_t cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
  57. const unsigned char *buf, uint32_t size)
  58. {
  59. uint32_t i, nr_strings;
  60. char **buffer, *string;
  61. /* Find all null-terminated (including zero length) strings in
  62. the TPLLV1_INFO field. Trailing garbage is ignored. */
  63. buf += 2;
  64. size -= 2;
  65. nr_strings = 0;
  66. for (i = 0; i < size; i++) {
  67. if (buf[i] == 0xff)
  68. break;
  69. if (buf[i] == 0)
  70. nr_strings++;
  71. }
  72. if (nr_strings == 0)
  73. return 0;
  74. size = i;
  75. buffer = HAL_Malloc(sizeof(char *) * nr_strings + size);
  76. SDC_Memset(buffer, 0, sizeof(char *) * nr_strings + size);
  77. if (!buffer)
  78. return -1;
  79. string = (char*)(buffer + nr_strings);
  80. for (i = 0; i < nr_strings; i++) {
  81. buffer[i] = string;
  82. strcpy(string, (char *)buf);
  83. string += strlen(string) + 1;
  84. buf += strlen((char *)buf) + 1;
  85. }
  86. if (func) {
  87. func->num_info = nr_strings;
  88. func->info = (const char**)buffer;
  89. } else {
  90. card->num_info = nr_strings;
  91. card->info = (const int8_t **)buffer;
  92. }
  93. return 0;
  94. }
  95. static int32_t cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
  96. const unsigned char *buf, unsigned size)
  97. {
  98. uint32_t vendor, device;
  99. /* TPLMID_MANF */
  100. vendor = buf[0] | (buf[1] << 8);
  101. /* TPLMID_CARD */
  102. device = buf[2] | (buf[3] << 8);
  103. if (func) {
  104. func->vendor = vendor;
  105. func->device = device;
  106. } else {
  107. card->cis.vendor = vendor;
  108. card->cis.device = device;
  109. }
  110. return 0;
  111. }
  112. static const unsigned char speed_val[16] =
  113. { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
  114. static const uint32_t speed_unit[8] =
  115. { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
  116. typedef int32_t (tpl_parse_t)(struct mmc_card *, struct sdio_func *, const unsigned char *, unsigned);
  117. struct cis_tpl {
  118. unsigned char code;
  119. unsigned char min_size;
  120. tpl_parse_t *parse;
  121. };
  122. static int32_t cis_tpl_parse(struct mmc_card *card, struct sdio_func *func,
  123. const char *tpl_descr,
  124. const struct cis_tpl *tpl, int32_t tpl_count,
  125. uint8_t code, const uint8_t *buf, uint32_t size)
  126. {
  127. int32_t i, ret = 0;
  128. /* look for a matching code in the table */
  129. for (i = 0; i < tpl_count; i++, tpl++) {
  130. if (tpl->code == code)
  131. break;
  132. }
  133. if (i < tpl_count) {
  134. if (size >= tpl->min_size) {
  135. if (tpl->parse)
  136. ret = tpl->parse(card, func, buf, size);
  137. else
  138. ret = 0; /* known tuple, not parsed */
  139. } else {
  140. /* invalid tuple */
  141. ret = -1;
  142. }
  143. if (ret) {
  144. SD_LOGW("%s: bad %s tuple 0x%02x (%u bytes)\n",
  145. __func__, tpl_descr, (unsigned int)code, (unsigned int)size);
  146. }
  147. } else {
  148. /* warn about unknown tuples */
  149. SD_LOGW("%s: queuing unknown CIS tuple 0x%02x (%u bytes)\n",
  150. __func__, (unsigned int)code, (unsigned int)size);
  151. }
  152. return ret;
  153. }
  154. static int32_t cistpl_funce_common(struct mmc_card *card, struct sdio_func *func,
  155. const unsigned char *buf, unsigned size)
  156. {
  157. /* Only valid for the common CIS (function 0) */
  158. if (func)
  159. return -1;
  160. /* TPLFE_FN0_BLK_SIZE */
  161. card->cis.blksize = buf[1] | (buf[2] << 8);
  162. /* TPLFE_MAX_TRAN_SPEED */
  163. card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
  164. speed_unit[buf[3] & 7];
  165. return 0;
  166. }
  167. static int32_t cistpl_funce_func(struct mmc_card *card, struct sdio_func *func,
  168. const unsigned char *buf, uint32_t size)
  169. {
  170. uint32_t vsn;
  171. uint32_t min_size;
  172. /* Only valid for the individual function's CIS (1-7) */
  173. if (!func)
  174. return -1;
  175. /*
  176. * This tuple has a different length depending on the SDIO spec
  177. * version.
  178. */
  179. vsn = func->card->cccr.sdio_vsn;
  180. min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
  181. if (size < min_size)
  182. return -1;
  183. /* TPLFE_MAX_BLK_SIZE */
  184. func->max_blksize = buf[12] | (buf[13] << 8);
  185. /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
  186. #ifdef CONFIG_MMC_ERASE
  187. if (vsn > SDIO_SDIO_REV_1_00)
  188. func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
  189. else
  190. func->enable_timeout = jiffies_to_msecs(HZ);
  191. #endif
  192. return 0;
  193. }
  194. /*
  195. * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples.
  196. *
  197. * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending
  198. * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO
  199. * TPLFID_FUNCTION is always hardcoded to 0x0C.
  200. */
  201. static const struct cis_tpl cis_tpl_funce_list[] = {
  202. { (unsigned char)0x00, (unsigned char)4, (tpl_parse_t *)cistpl_funce_common },
  203. { (unsigned char)0x01, (unsigned char)0, (tpl_parse_t *)cistpl_funce_func },
  204. { (unsigned char)0x04, (unsigned char)(1+1+6), NULL/* CISTPL_FUNCE_LAN_NODE_ID */ },
  205. };
  206. static int32_t cistpl_funce(struct mmc_card *card, struct sdio_func *func,
  207. const unsigned char *buf, unsigned size)
  208. {
  209. if (size < 1)
  210. return -1;
  211. return cis_tpl_parse(card, func, "CISTPL_FUNCE",
  212. cis_tpl_funce_list,
  213. HAL_ARRAY_SIZE(cis_tpl_funce_list),
  214. buf[0], buf, size);
  215. }
  216. /* Known TPL_CODEs table for CIS tuples */
  217. static const struct cis_tpl cis_tpl_list[] = {
  218. { 0x15, 3, (tpl_parse_t *)cistpl_vers_1 },
  219. { 0x20, 4, (tpl_parse_t *)cistpl_manfid },
  220. { 0x21, 2, NULL/* cistpl_funcid */ },
  221. { 0x22, 0, (tpl_parse_t *)cistpl_funce },
  222. };
  223. static int32_t sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
  224. {
  225. int32_t ret;
  226. struct sdio_func_tuple *this, **prev;
  227. uint32_t i, ptr = 0;
  228. /*
  229. * Note that this works for the common CIS (function number 0) as
  230. * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
  231. * have the same offset.
  232. */
  233. for (i = 0; i < 3; i++) {
  234. uint8_t x, fn;
  235. if (func)
  236. fn = func->num;
  237. else
  238. fn = 0;
  239. ret = mmc_io_rw_direct(card, 0, FN0, SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
  240. if (ret)
  241. return ret;
  242. ptr |= x << (i * 8);
  243. }
  244. if (func)
  245. prev = &func->tuples;
  246. else
  247. prev = &card->tuples;
  248. if (*prev) {
  249. SD_LOGE("%s,%d no prev found\n", __func__, __LINE__);
  250. return -1;
  251. }
  252. do {
  253. uint8_t tpl_code, tpl_link;
  254. ret = mmc_io_rw_direct(card, 0, FN0, ptr++, 0, &tpl_code);
  255. if (ret)
  256. break;
  257. /* 0xff means we're done */
  258. if (tpl_code == 0xff)
  259. break;
  260. /* null entries have no link field or data */
  261. if (tpl_code == 0x00)
  262. continue;
  263. ret = mmc_io_rw_direct(card, 0, FN0, ptr++, 0, &tpl_link);
  264. if (ret)
  265. break;
  266. /* a size of 0xff also means we're done */
  267. if (tpl_link == 0xff)
  268. break;
  269. this = HAL_Malloc(sizeof(*this) + tpl_link);
  270. if (!this)
  271. return -1;
  272. for (i = 0; i < tpl_link; i++) {
  273. ret = mmc_io_rw_direct(card, 0, FN0, ptr + i, 0, &this->data[i]);
  274. if (ret)
  275. break;
  276. }
  277. if (ret) {
  278. HAL_Free(this);
  279. break;
  280. }
  281. /* Try to parse the CIS tuple */
  282. ret = cis_tpl_parse(card, func, "CIS",
  283. cis_tpl_list, HAL_ARRAY_SIZE(cis_tpl_list),
  284. tpl_code, this->data, tpl_link);
  285. if (ret == -1) {
  286. /*
  287. * The tuple is unknown or known but not parsed.
  288. * Queue the tuple for the function driver.
  289. */
  290. this->next = NULL;
  291. this->code = tpl_code;
  292. this->size = tpl_link;
  293. *prev = this;
  294. prev = &this->next;
  295. /* keep on analyzing tuples */
  296. ret = 0;
  297. } else {
  298. /*
  299. * We don't need the tuple anymore if it was
  300. * successfully parsed by the SDIO core or if it is
  301. * not going to be queued for a driver.
  302. */
  303. HAL_Free(this);
  304. }
  305. ptr += tpl_link;
  306. } while (!ret);
  307. /*
  308. * Link in all unknown tuples found in the common CIS so that
  309. * drivers don't have to go digging in two places.
  310. */
  311. if (func)
  312. *prev = card->tuples;
  313. return ret;
  314. }
  315. int32_t sdio_read_common_cis(struct mmc_card *card)
  316. {
  317. return sdio_read_cis(card, NULL);
  318. }
  319. void sdio_free_common_cis(struct mmc_card *card)
  320. {
  321. struct sdio_func_tuple *tuple, *victim;
  322. tuple = card->tuples;
  323. while (tuple) {
  324. victim = tuple;
  325. tuple = tuple->next;
  326. HAL_Free(victim);
  327. }
  328. card->tuples = NULL;
  329. }
  330. void sdio_free_func_cis(struct sdio_func *func)
  331. {
  332. struct sdio_func_tuple *tuple, *victim;
  333. tuple = func->tuples;
  334. while (tuple && tuple != func->card->tuples) {
  335. victim = tuple;
  336. tuple = tuple->next;
  337. HAL_Free(victim);
  338. }
  339. func->tuples = NULL;
  340. }
  341. int sdio_read_func_cis(struct sdio_func *func)
  342. {
  343. int ret;
  344. ret = sdio_read_cis(func->card, func);
  345. if (ret)
  346. return ret;
  347. /*
  348. * Since we've linked to tuples in the card structure,
  349. * we must make sure we have a reference to it.
  350. */
  351. // get_device(&func->card->dev);
  352. /*
  353. * Vendor/device id is optional for function CIS, so
  354. * copy it from the card structure as needed.
  355. */
  356. if (func->vendor == 0) {
  357. func->vendor = func->card->cis.vendor;
  358. func->device = func->card->cis.device;
  359. }
  360. return 0;
  361. }
  362. static int32_t sdio_read_fbr(struct sdio_func *func)
  363. {
  364. int32_t ret;
  365. unsigned char data;
  366. //if (mmc_card_nonstd_func_interface(func->card)) {
  367. // func->class = SDIO_CLASS_NONE;
  368. // return 0;
  369. //}
  370. ret = mmc_io_rw_direct(func->card, 0, FN0,
  371. SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
  372. if (ret)
  373. goto out;
  374. data &= 0x0f;
  375. if (data == 0x0f) {
  376. ret = mmc_io_rw_direct(func->card, 0, FN0,
  377. SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
  378. if (ret)
  379. goto out;
  380. }
  381. func->class = data;
  382. out:
  383. return ret;
  384. }
  385. #endif
  386. static int32_t mmc_send_io_op_cond(struct mmc_card *card, uint32_t ocr, uint32_t *rocr)
  387. {
  388. struct mmc_command cmd = {0};
  389. uint32_t i, err = 0;
  390. cmd.opcode = SD_IO_SEND_OP_COND;
  391. cmd.arg = ocr;
  392. cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR;
  393. for (i = 1000; i; i--) {
  394. err = mmc_wait_for_cmd(card->host, &cmd);
  395. if (err)
  396. break;
  397. /* if we're just probing, do a single pass */
  398. if (ocr == 0)
  399. break;
  400. /* otherwise wait until reset completes */
  401. if (cmd.resp[0] & MMC_CARD_BUSY)
  402. break;
  403. err = -1;
  404. mmc_mdelay(1);
  405. }
  406. if (rocr)
  407. *rocr = cmd.resp[0];
  408. return err;
  409. }
  410. int32_t
  411. mmc_io_rw_direct_host(struct mmc_host *host, int32_t write, uint32_t fn,
  412. uint32_t addr, uint8_t in, uint8_t *out)
  413. {
  414. struct mmc_command cmd = {0};
  415. int32_t err;
  416. uint32_t temp;
  417. if (!host) {
  418. SD_LOGE_RAW(ROM_ERR_MASK, "%s,%d no host exist!\n", __func__, __LINE__);
  419. return -1;
  420. }
  421. if (fn > 7) {
  422. SDC_LOGE("%s,%d wrong fn:%ld!\n", __func__, __LINE__, HAL_PR_SZ_L(fn));
  423. return -1;
  424. }
  425. SDC_LOGD("%s,fun %ld, raw %d ,addr %lx,wrte data %x\n", write?"Write":"Read", HAL_PR_SZ_L(fn), (write && out)?1:0, HAL_PR_SZ_L(addr), in);
  426. /* sanity check */
  427. if (addr & ~0x1FFFF)
  428. return -1;
  429. cmd.opcode = SD_IO_RW_DIRECT;
  430. cmd.arg = write ? 0x80000000 : 0x00000000;
  431. cmd.arg |= fn << 28;
  432. cmd.arg |= (write && out) ? 0x08000000 : 0x00000000;
  433. cmd.arg |= addr << 9;
  434. cmd.arg |= in;
  435. cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
  436. err = mmc_wait_for_cmd(host, &cmd);
  437. if (err)
  438. return err;
  439. temp = cmd.resp[0];
  440. if (temp & 0xcf00) {
  441. SDC_LOGE("CMD52 %s Operation Error:%s%s%s%s%s !\n",
  442. write ? "wirte" : "read",
  443. temp&0x8000 ? " CMDCRCErr" : "",
  444. temp&0x4000 ? " ILLEGALCmd" : "",
  445. temp&0x800 ? " GenERR" : "",
  446. temp&0x200 ? " FucNumErr" : "",
  447. temp&0x100 ? " ArgOutRange" : "");
  448. }
  449. if (cmd.resp[0] & R5_ERROR)
  450. return -1;
  451. if (cmd.resp[0] & R5_FUNCTION_NUMBER)
  452. return -1;
  453. if (cmd.resp[0] & R5_OUT_OF_RANGE)
  454. return -1;
  455. if (out) {
  456. *out = cmd.resp[0] & 0xFF;
  457. }
  458. return 0;
  459. }
  460. int mmc_io_rw_direct(struct mmc_card *card, int32_t write, uint32_t fn, uint32_t addr, uint8_t in, uint8_t *out)
  461. {
  462. if (!card) {
  463. SD_LOGE_RAW(ROM_ERR_MASK, "%s,%d no card exist!\n", __func__, __LINE__);
  464. return -1;
  465. }
  466. return mmc_io_rw_direct_host(card->host, write, fn, addr, in, out);
  467. }
  468. /**
  469. * SDIO Spec: CMD53 is 48 bit long. read or write multiple I/O registers with a
  470. * single command
  471. * ----------------------------------------------------------------------------
  472. * S|D|CMDIND|R|FUN|BLOCK MODE|OP CODE|REGISTER ADDRESS|BYTE/BLOCK COUNT|CRC7|E
  473. * ----------------------------------------------------------------------------
  474. * @ret : RET_OK, RET_FAIL
  475. */
  476. int32_t
  477. mmc_io_rw_extended(struct mmc_card *card, uint8_t write, uint8_t fn, uint32_t addr, uint32_t incr_addr,
  478. const void *buf, uint32_t blocks, uint32_t blksz)
  479. {
  480. struct mmc_request mrq;
  481. struct mmc_command cmd = {0};
  482. struct mmc_data data = {0};
  483. struct scatterlist sg = {0};
  484. int32_t ret;
  485. if (fn > 7) {
  486. SD_LOGE("%s,%d err fn:%d\n", __func__, __LINE__, fn);
  487. return -1;
  488. }
  489. SDC_WARN_ON(blksz == 0);
  490. /* sanity check */
  491. if (addr & ~0x1FFFF)
  492. return -1;
  493. cmd.opcode = SD_IO_RW_EXTENDED; /* cmd53 --index */
  494. cmd.arg = write ? 0x80000000 : 0x00000000; /* write/read --flag */
  495. cmd.arg |= fn << 28; /* function number */
  496. cmd.arg |= incr_addr ? 0x04000000 : 0x00000000; /* incrementing address */
  497. cmd.arg |= addr << 9;
  498. if (blocks == 0 )
  499. cmd.arg |= (blksz == 512) ? 0 : blksz; /* byte mode */
  500. else
  501. cmd.arg |= 0x08000000| blocks; /* block mode */
  502. cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
  503. data.blksz = blksz;
  504. data.blocks = blocks ? blocks : 1;
  505. data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
  506. data.sg = &sg;
  507. data.sg_len = 1;
  508. sg.buffer = (void *)buf;
  509. sg.len = data.blksz * data.blocks;
  510. mrq.cmd = &cmd;
  511. mrq.data = &data;
  512. SD_LOGD("starting %s CMD%ld add %lx,arg 0x%08lx flags %lx\n", write?"Write":"Read", HAL_PR_SZ_L(cmd.opcode), HAL_PR_SZ_L(addr), HAL_PR_SZ_L(cmd.arg), HAL_PR_SZ_L(cmd.flags));
  513. SD_LOGD("blksz %ld blocks %ld flags %lx\n", HAL_PR_SZ_L(data.blksz), HAL_PR_SZ_L(data.blocks), HAL_PR_SZ_L(data.flags));
  514. ret = mmc_wait_for_req(card->host, &mrq);
  515. if (ret) {
  516. SD_LOGE("SDC write operation failed\n");
  517. }
  518. if (cmd.resp[0] & R5_ERROR)
  519. return -1;
  520. if (cmd.resp[0] & R5_FUNCTION_NUMBER)
  521. return -1;
  522. if (cmd.resp[0] & R5_OUT_OF_RANGE)
  523. return -1;
  524. // printf("%s,%d,%x\n", __FUNCTION__,__LINE__, sg.buffer);
  525. return ret;
  526. }
  527. /**
  528. * read/write IO memory in Block/Stream mode with fixed/increaming address
  529. * This will split an arbitrarily sized data transfer into several IO_RW_EXTENDED commands.
  530. * @func_num: function number
  531. * @write: IOMEM_WR:write, IOMEM_RD:read
  532. * @addr: address of register read from or write to
  533. * @incr_addr: 1: in, 0: not, used for fifio
  534. * @buf: pointer to data buffer
  535. * @size: block counter(block mode)/byte counter(byte mode)
  536. * @ret: RET_OK, RET_FAIL
  537. */
  538. static int32_t
  539. sdio_io_rw_ext_helper(struct mmc_card *card, uint8_t func_num, int32_t write, uint32_t addr,
  540. uint32_t incr_addr, uint8_t *buf, uint32_t size)
  541. {
  542. uint32_t remainder = size;
  543. int32_t ret = -1;
  544. uint32_t blocks;
  545. int32_t fn_bsize;
  546. uint8_t *buf_tmp = buf;
  547. /* Do the bulk of the transfer using block mode (if supported). */
  548. //if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
  549. fn_bsize = card->fn_bsize[func_num];
  550. /* Do the bulk of the transfer using block mode (if supported). */
  551. while (remainder >= fn_bsize) {
  552. blocks = remainder / fn_bsize;
  553. size = blocks * fn_bsize;
  554. ret = mmc_io_rw_extended(card, write, func_num, addr, incr_addr, buf, blocks, fn_bsize);
  555. if (ret) {
  556. SD_LOGE("%s,%d %s IO%x [%lx] SZ:%ld Err:%ld !!\n", __func__, __LINE__,
  557. write?"W":"R", func_num, HAL_PR_SZ_L(addr), HAL_PR_SZ_L(size), HAL_PR_SZ_L(ret));
  558. return ret;
  559. }
  560. remainder -= size;
  561. buf += size;
  562. if (incr_addr)
  563. addr += size;
  564. }
  565. /* Write the remainder using byte mode. */
  566. while (remainder > 0) {
  567. size = SDIO_MIN(remainder, 512);
  568. ret = mmc_io_rw_extended(card, write, func_num, addr, incr_addr, buf, 0, size);
  569. if (ret) {
  570. SD_LOGE("%s,%d %s IO%x [%lx] SZ:%ld Err:%ld !!\n", __func__, __LINE__,
  571. write?"W":"R", func_num, HAL_PR_SZ_L(addr), HAL_PR_SZ_L(size), HAL_PR_SZ_L(ret));
  572. return ret;
  573. }
  574. remainder -= size;
  575. buf += size;
  576. if (incr_addr)
  577. addr += size;
  578. }
  579. // printf("%s,%d,%x %x,%x\n", __FUNCTION__,__LINE__, buf_tmp,(uint32_t)buf_tmp, (uint32_t)buf);
  580. return ret;
  581. }
  582. /**
  583. * sdio_readb - read a single byte from a SDIO function
  584. * @func_num: SDIO function to access
  585. * @addr: address to read
  586. * @err_ret: optional status value from transfer
  587. *
  588. * Reads a single byte from the address space of a given SDIO
  589. * function. If there is a problem reading the address, 0xff
  590. * is returned and @err_ret will contain the error code.
  591. */
  592. uint8_t sdio_readb(struct mmc_card *card, uint32_t func_num, uint32_t addr, int32_t *err_ret)
  593. {
  594. int32_t ret;
  595. uint8_t val;
  596. if (err_ret)
  597. *err_ret = 0;
  598. ret = mmc_io_rw_direct_host(card->host, 0, func_num, addr, 0, &val);
  599. if (ret) {
  600. if (err_ret)
  601. *err_ret = ret;
  602. return 0xFF;
  603. }
  604. return val;
  605. }
  606. /**
  607. * sdio_writeb - write a single byte to a SDIO function
  608. * @func_num: SDIO function to access
  609. * @b: byte to write
  610. * @addr: address to write to
  611. * @err_ret: optional status value from transfer
  612. *
  613. * Writes a single byte to the address space of a given SDIO
  614. * function. @err_ret will contain the status of the actual
  615. * transfer.
  616. */
  617. void sdio_writeb(struct mmc_card *card, uint32_t func_num, const uint8_t b, uint32_t addr, int32_t *err_ret)
  618. {
  619. int32_t ret;
  620. ret = mmc_io_rw_direct_host(card->host, 1, func_num, addr, b, NULL);
  621. if (err_ret)
  622. *err_ret = ret;
  623. }
  624. /**
  625. * sdio_readw - read a 16 bit integer from a SDIO function
  626. * @func: SDIO function to access
  627. * @addr: address to read
  628. * @err_ret: optional status value from transfer
  629. *
  630. * Reads a 16 bit integer from the address space of a given SDIO
  631. * function. If there is a problem reading the address, 0xffff
  632. * is returned and @err_ret will contain the error code.
  633. */
  634. uint16_t sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
  635. {
  636. int ret;
  637. if (err_ret)
  638. *err_ret = 0;
  639. ret = sdio_memcpy_fromio(func->card, func->num,func->tmpbuf, addr, 2);
  640. if (ret) {
  641. if (err_ret)
  642. *err_ret = ret;
  643. return 0xFFFF;
  644. }
  645. //return le16_to_cpup((__le16 *)func->tmpbuf);
  646. return le16toh( *((uint16_t *)func->tmpbuf));
  647. }
  648. //EXPORT_SYMBOL_GPL(sdio_readw);
  649. /**
  650. * sdio_writew - write a 16 bit integer to a SDIO function
  651. * @func: SDIO function to access
  652. * @b: integer to write
  653. * @addr: address to write to
  654. * @err_ret: optional status value from transfer
  655. *
  656. * Writes a 16 bit integer to the address space of a given SDIO
  657. * function. @err_ret will contain the status of the actual
  658. * transfer.
  659. */
  660. void sdio_writew(struct sdio_func *func, uint16_t b, unsigned int addr, int *err_ret)
  661. {
  662. int ret;
  663. //*(__le16 *)func->tmpbuf = cpu_to_le16(b);
  664. *(int16_t *)func->tmpbuf = cpu_to_le16(b);
  665. ret = sdio_memcpy_toio(func->card, func->num, addr, func->tmpbuf, 2);
  666. if (err_ret)
  667. *err_ret = ret;
  668. }
  669. //EXPORT_SYMBOL_GPL(sdio_writew);
  670. /**
  671. * sdio_readl - read a 32 bit integer from a SDIO function
  672. * @func: SDIO function to access
  673. * @addr: address to read
  674. * @err_ret: optional status value from transfer
  675. *
  676. * Reads a 32 bit integer from the address space of a given SDIO
  677. * function. If there is a problem reading the address,
  678. * 0xffffffff is returned and @err_ret will contain the error
  679. * code.
  680. */
  681. uint32_t sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
  682. {
  683. int ret;
  684. if (err_ret)
  685. *err_ret = 0;
  686. ret = sdio_memcpy_fromio(func->card, func->num,func->tmpbuf, addr, 4);
  687. if (ret) {
  688. if (err_ret)
  689. *err_ret = ret;
  690. return 0xFFFFFFFF;
  691. }
  692. //return le32_to_cpup((__le32 *)func->tmpbuf);
  693. return le32toh(*((uint32_t *)func->tmpbuf));
  694. }
  695. //EXPORT_SYMBOL_GPL(sdio_readl);
  696. /**
  697. * sdio_writel - write a 32 bit integer to a SDIO function
  698. * @func: SDIO function to access
  699. * @b: integer to write
  700. * @addr: address to write to
  701. * @err_ret: optional status value from transfer
  702. *
  703. * Writes a 32 bit integer to the address space of a given SDIO
  704. * function. @err_ret will contain the status of the actual
  705. * transfer.
  706. */
  707. void sdio_writel(struct sdio_func *func, uint32_t b, unsigned int addr, int *err_ret)
  708. {
  709. int ret;
  710. //*(__le32 *)func->tmpbuf = cpu_to_le32(b);
  711. *(int32_t *)func->tmpbuf = cpu_to_le32(b);
  712. ret = sdio_memcpy_toio(func->card, func->num, addr, func->tmpbuf, 4);
  713. if (err_ret)
  714. *err_ret = ret;
  715. }
  716. //EXPORT_SYMBOL_GPL(sdio_writel);
  717. /**
  718. * sdio_enable_func - enables a SDIO function for usage
  719. * @func: SDIO function to enable
  720. *
  721. * Powers up and activates a SDIO function so that register
  722. * access is possible.
  723. */
  724. int32_t sdio_enable_func(struct mmc_card *card, uint32_t func_num)
  725. {
  726. uint8_t dat;
  727. mmc_io_rw_direct_host(card->host, 0, FN0, SDIO_CCCR_IOEx, 0, &dat);
  728. dat |= (1 << func_num); /* keep the value of other FN's IO enb state */
  729. mmc_io_rw_direct_host(card->host, 1, FN0, SDIO_CCCR_IOEx, dat, &dat);
  730. if (!(dat & (1 << func_num))) {
  731. SD_LOGE("%s,%d IO%ld Enable failed !!\n", __func__, __LINE__, HAL_PR_SZ_L(func_num));
  732. return -1;
  733. }
  734. SD_LOGD("IO%ld is enabled !!\n", HAL_PR_SZ_L(func_num));
  735. mmc_io_rw_direct_host(card->host, 0, FN0, SDIO_CCCR_IORx, 0, &dat);
  736. if (!(dat & (1 << func_num))) {
  737. SD_LOGD("%s,%d IO%ld is not ready !!\n", __func__, __LINE__, HAL_PR_SZ_L(func_num));
  738. return -1;
  739. }
  740. SD_LOGD("IO%ld is ready !!\n", HAL_PR_SZ_L(func_num));
  741. return 0;
  742. }
  743. /**
  744. * Disable IOx's Function.
  745. * @io_num: 1~7
  746. */
  747. int32_t sdio_disable_func(struct mmc_card *card, uint32_t func_num)
  748. {
  749. uint8_t dat;
  750. mmc_io_rw_direct_host(card->host, 0, FN0, SDIO_CCCR_IOEx, 0, &dat);
  751. dat &= ~(1 << func_num); /* keep the value of other FN's IO enb state */
  752. mmc_io_rw_direct_host(card->host, 1, FN0, SDIO_CCCR_IOEx, dat, &dat);
  753. if (dat & (1 << func_num)) {
  754. SD_LOGE("%s,%d IO%ld Disable failed !!\n", __func__, __LINE__, HAL_PR_SZ_L(func_num));
  755. return -1;
  756. }
  757. SD_LOGD("IO%ld is Disabled !!\n", HAL_PR_SZ_L(func_num));
  758. return 0;
  759. }
  760. int32_t sdio_set_block_size(struct mmc_card *card, uint32_t fn_num, uint32_t blksz)
  761. {
  762. int32_t ret;
  763. #if 0
  764. if ((blksz == 0) || card->fn_bsize[fn_num] == blksz) {
  765. SD_LOGW("block size has beed set to %d !!\n", blksz);
  766. return -1;
  767. }
  768. #endif
  769. if ((blksz == 0)) {
  770. SD_LOGW("block size has beed set to %ld !!\n", HAL_PR_SZ_L(blksz));
  771. return -1;
  772. }
  773. /* sdio block size set in FN0 CCIA */
  774. sdio_writeb(card, FN0, blksz & 0xff, SDIO_FBR_BASE(fn_num) + SDIO_FBR_BLKSIZE, &ret);
  775. if (ret)
  776. return ret;
  777. sdio_writeb(card, FN0, (blksz >> 8) & 0xff, SDIO_FBR_BASE(fn_num) + SDIO_FBR_BLKSIZE + 1, &ret);
  778. if (ret)
  779. return ret;
  780. card->fn_bsize[fn_num] = blksz;
  781. SD_LOGD("sdio FN%ld set block size to %ld\n", HAL_PR_SZ_L(fn_num), HAL_PR_SZ_L(blksz));
  782. return 0;
  783. }
  784. /*
  785. * Test if the card supports high-speed mode and, if so, switch to it.
  786. */
  787. static int32_t mmc_sdio_switch_hs(struct mmc_card *card, uint32_t enable)
  788. {
  789. int32_t ret;
  790. uint8_t speed;
  791. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  792. return 0;
  793. if (!card->cccr.high_speed)
  794. return 0;
  795. ret = mmc_io_rw_direct(card, 0, FN0, SDIO_CCCR_SPEED, 0, &speed);
  796. if (ret)
  797. return ret;
  798. if (!(speed & SDIO_SPEED_SHS)) {
  799. SD_LOGE("%s not support high speed mode!\n", __func__);
  800. return -1;
  801. }
  802. if (enable) {
  803. speed |= SDIO_SPEED_EHS;
  804. } else {
  805. speed &= ~SDIO_SPEED_EHS;
  806. }
  807. ret = mmc_io_rw_direct(card, 1, FN0, SDIO_CCCR_SPEED, speed, NULL);
  808. if(ret)
  809. return ret;
  810. return 1;
  811. }
  812. uint32_t mmc_sdio_get_max_clock(struct mmc_card *card)
  813. {
  814. unsigned max_dtr;
  815. if (mmc_card_highspeed(card)) {
  816. /*
  817. * The SDIO specification doesn't mention how
  818. * the CIS transfer speed register relates to
  819. * high-speed, but it seems that 50 MHz is
  820. * mandatory.
  821. */
  822. max_dtr = 50000000;
  823. } else {
  824. max_dtr = card->cis.max_dtr;
  825. }
  826. #ifdef CONFIG_USE_SDIO_COMBO
  827. if (card->type == MMC_TYPE_SD_COMBO)
  828. max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
  829. #endif
  830. return max_dtr;
  831. }
  832. #ifdef CONFIG_SDIO_USE_FUNS
  833. /*
  834. * Allocate and initialise a new SDIO function structure.
  835. */
  836. struct sdio_func *sdio_alloc_func(struct mmc_card *card)
  837. {
  838. struct sdio_func *func;
  839. func = HAL_Malloc(sizeof(struct sdio_func));
  840. if (!func)
  841. return NULL;
  842. SDC_Memset(func, 0, sizeof(struct sdio_func));
  843. func->card = card;
  844. return func;
  845. }
  846. /*
  847. * Register a new SDIO function with the driver model.
  848. */
  849. int32_t sdio_add_func(struct sdio_func *func, uint32_t id)
  850. {
  851. // fprintf(func->name, "%s:%d", id, func->num);
  852. sdio_func_set_present(func);
  853. return 0;
  854. }
  855. /*
  856. * Unregister a SDIO function with the driver model, and
  857. * (eventually) free it.
  858. * This function can be called through error paths where sdio_add_func() was
  859. * never executed (because a failure occurred at an earlier point).
  860. */
  861. void sdio_remove_func(struct sdio_func *func)
  862. {
  863. if (!sdio_func_present(func))
  864. return;
  865. }
  866. static int32_t sdio_init_func(struct mmc_card *card, uint32_t fn)
  867. {
  868. int32_t ret;
  869. struct sdio_func *func;
  870. if (fn > SDIO_MAX_FUNCS) {
  871. SD_LOGE("%s,%d wrong fn:%ld!\n", __func__, __LINE__, HAL_PR_SZ_L(fn));
  872. return -1;
  873. }
  874. func = sdio_alloc_func(card);
  875. if (!func)
  876. return -1;
  877. func->num = fn;
  878. if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
  879. ret = sdio_read_fbr(func);
  880. if (ret)
  881. goto fail;
  882. ret = sdio_read_func_cis(func);
  883. if (ret)
  884. goto fail;
  885. } else {
  886. func->vendor = func->card->cis.vendor;
  887. func->device = func->card->cis.device;
  888. func->max_blksize = func->card->cis.blksize;
  889. }
  890. card->sdio_func[fn - 1] = func;
  891. printf("func address %p\n",func);
  892. return 0;
  893. fail:
  894. /*
  895. * It is okay to remove the function here even though we hold
  896. * the host lock as we haven't registered the device yet.
  897. */
  898. sdio_remove_func(func);
  899. return ret;
  900. }
  901. /*
  902. * Host is being removed. Free up the current card.
  903. */
  904. int mmc_sdio_remove(struct mmc_host *host)
  905. {
  906. int i;
  907. if (!host) {
  908. SD_LOGE_RAW(ROM_ERR_MASK, "%s,%d no host exist!\n", __func__, __LINE__);
  909. return -1;
  910. }
  911. if (!host->card) {
  912. SDC_LOGE("%s,%d no card exist!\n", __func__, __LINE__);
  913. return -1;
  914. }
  915. for (i = 0;i < host->card->sdio_funcs;i++) {
  916. if (host->card->sdio_func[i]) {
  917. sdio_remove_func(host->card->sdio_func[i]);
  918. host->card->sdio_func[i] = NULL;
  919. }
  920. }
  921. //mmc_remove_card(host->card); /* do nothing */
  922. host->card = NULL;
  923. return 0;
  924. }
  925. #endif
  926. static int32_t sdio_read_cccr(struct mmc_card *card, uint32_t ocr)
  927. {
  928. int32_t ret;
  929. int32_t cccr_vsn;
  930. int32_t uhs = ocr & R4_18V_PRESENT;
  931. uint8_t data;
  932. uint8_t speed;
  933. SDC_Memset(&card->cccr, 0, sizeof(struct sdio_cccr));
  934. ret = mmc_io_rw_direct(card, 0, FN0, SDIO_CCCR_CCCR, 0, &data);
  935. if (ret)
  936. goto out;
  937. cccr_vsn = data & 0x0f;
  938. if (cccr_vsn > SDIO_CCCR_REV_3_00) {
  939. SD_LOGW("%s: unrecognised CCCR structure version %ld\n",
  940. __func__, HAL_PR_SZ_L(cccr_vsn));
  941. return -1;
  942. }
  943. card->cccr.sdio_vsn = (data & 0xf0) >> 4;
  944. ret = mmc_io_rw_direct(card, 0, FN0, SDIO_CCCR_CAPS, 0, &data);
  945. if (ret)
  946. goto out;
  947. if (data & SDIO_CCCR_CAP_SMB)
  948. card->cccr.multi_block = 1;
  949. if (data & SDIO_CCCR_CAP_LSC)
  950. card->cccr.low_speed = 1;
  951. if (data & SDIO_CCCR_CAP_4BLS)
  952. card->cccr.wide_bus = 1;
  953. if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
  954. ret = mmc_io_rw_direct(card, 0, FN0, SDIO_CCCR_POWER, 0, &data);
  955. if (ret)
  956. goto out;
  957. if (data & SDIO_POWER_SMPC)
  958. card->cccr.high_power = 1;
  959. }
  960. if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
  961. ret = mmc_io_rw_direct(card, 0, FN0, SDIO_CCCR_SPEED, 0, &speed);
  962. if (ret)
  963. goto out;
  964. card->scr.sda_spec3 = 0;
  965. card->sw_caps.sd3_bus_mode = 0;
  966. card->sw_caps.sd3_drv_type = 0;
  967. if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
  968. card->scr.sda_spec3 = 1;
  969. ret = mmc_io_rw_direct(card, 0, FN0, SDIO_CCCR_UHS, 0, &data);
  970. if (ret)
  971. goto out;
  972. if (card->host->caps &
  973. (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  974. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
  975. MMC_CAP_UHS_DDR50)) {
  976. if (data & SDIO_UHS_DDR50)
  977. card->sw_caps.sd3_bus_mode |= SD_MODE_UHS_DDR50;
  978. if (data & SDIO_UHS_SDR50)
  979. card->sw_caps.sd3_bus_mode |= SD_MODE_UHS_SDR50;
  980. if (data & SDIO_UHS_SDR104)
  981. card->sw_caps.sd3_bus_mode |= SD_MODE_UHS_SDR104;
  982. }
  983. ret = mmc_io_rw_direct(card, 0, FN0, SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
  984. if (ret)
  985. goto out;
  986. if (data & SDIO_DRIVE_SDTA)
  987. card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
  988. if (data & SDIO_DRIVE_SDTC)
  989. card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
  990. if (data & SDIO_DRIVE_SDTD)
  991. card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
  992. }
  993. /* if no uhs mode ensure we check for high speed */
  994. if (!card->sw_caps.sd3_bus_mode) {
  995. if (speed & SDIO_SPEED_SHS) {
  996. card->cccr.high_speed = 1;
  997. card->sw_caps.hs_max_dtr = 50000000;
  998. } else {
  999. card->cccr.high_speed = 0;
  1000. card->sw_caps.hs_max_dtr = 25000000;
  1001. }
  1002. }
  1003. }
  1004. out:
  1005. SD_LOGD("%s high_speed:%d sda_spec3:%d sd3_drv_type:%lx sd3_bus_mode:%lx\n",
  1006. __func__, card->cccr.high_speed, card->scr.sda_spec3,
  1007. HAL_PR_SZ_L(card->sw_caps.sd3_drv_type), HAL_PR_SZ_L(card->sw_caps.sd3_bus_mode));
  1008. return ret;
  1009. }
  1010. static int32_t sdio_enable_wide(struct mmc_card *card)
  1011. {
  1012. int32_t ret;
  1013. uint8_t ctrl;
  1014. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  1015. return 0;
  1016. if (card->cccr.low_speed && !card->cccr.wide_bus)
  1017. return 0;
  1018. ret = mmc_io_rw_direct(card, 0, FN0, SDIO_CCCR_IF, 0, &ctrl);
  1019. if (ret)
  1020. return ret;
  1021. ctrl |= SDIO_BUS_WIDTH_4BIT;
  1022. ret = mmc_io_rw_direct(card, 1, FN0, SDIO_CCCR_IF, ctrl, NULL);
  1023. if (!ret && card->type == MMC_TYPE_SDIO)
  1024. card->bus_width = MMC_BUS_WIDTH_4;
  1025. return ret;
  1026. }
  1027. /*
  1028. * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
  1029. * of the card. This may be required on certain setups of boards,
  1030. * controllers and embedded sdio device which do not need the card's
  1031. * pull-up. As a result, card detection is disabled and power is saved.
  1032. */
  1033. static int32_t sdio_disable_cd(struct mmc_card *card)
  1034. {
  1035. int32_t ret;
  1036. uint8_t ctrl;
  1037. if (!mmc_card_disable_cd(card))
  1038. return 0;
  1039. ret = mmc_io_rw_direct(card, 0, FN0, SDIO_CCCR_IF, 0, &ctrl);
  1040. if (ret)
  1041. return ret;
  1042. ctrl |= SDIO_BUS_CD_DISABLE;
  1043. return mmc_io_rw_direct(card, 1, FN0, SDIO_CCCR_IF, ctrl, NULL);
  1044. }
  1045. /*
  1046. * Devices that remain active during a system suspend are
  1047. * put back into 1-bit mode. called by mmc_sdio_suspend(host).
  1048. */
  1049. #ifdef CONFIG_SD_PM
  1050. static int32_t sdio_disable_wide(struct mmc_card *card)
  1051. {
  1052. int32_t ret;
  1053. uint8_t ctrl;
  1054. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  1055. return 0;
  1056. if (card->cccr.low_speed && !card->cccr.wide_bus)
  1057. return 0;
  1058. ret = mmc_io_rw_direct(card, 0, FN0, SDIO_CCCR_IF, 0, &ctrl);
  1059. if (ret)
  1060. return ret;
  1061. if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
  1062. return 0;
  1063. ctrl &= ~SDIO_BUS_WIDTH_4BIT;
  1064. ctrl |= SDIO_BUS_ASYNC_INT;
  1065. ret = mmc_io_rw_direct(card, 1, FN0, SDIO_CCCR_IF, ctrl, NULL);
  1066. if (ret)
  1067. return ret;
  1068. HAL_SDC_Set_BusWidth(card->host, MMC_BUS_WIDTH_1);
  1069. return 0;
  1070. }
  1071. #endif
  1072. static int32_t sdio_enable_4bit_bus(struct mmc_card *card)
  1073. {
  1074. int32_t err = 0;
  1075. if (card->type == MMC_TYPE_SDIO)
  1076. return sdio_enable_wide(card);
  1077. #ifdef CONFIG_USE_SDIO_COMBO
  1078. if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
  1079. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  1080. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  1081. if (err)
  1082. return err;
  1083. } else
  1084. return 0;
  1085. err = sdio_enable_wide(card);
  1086. if (err <= 0)
  1087. mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
  1088. #endif
  1089. return err;
  1090. }
  1091. /*
  1092. * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
  1093. */
  1094. static int32_t sdio_enable_hs(struct mmc_card *card)
  1095. {
  1096. int32_t ret;
  1097. ret = mmc_sdio_switch_hs(card, 1);
  1098. if (ret <= 0 || card->type == MMC_TYPE_SDIO)
  1099. return ret;
  1100. #ifdef CONFIG_USE_SDIO_COMBO
  1101. ret = mmc_sd_switch_hs(card);
  1102. if (ret <= 0)
  1103. mmc_sdio_switch_hs(card, 0);
  1104. #endif
  1105. return ret;
  1106. }
  1107. /*----------------------------------------------------------------------------------------------*\
  1108. Function : This function gets the CIS adress present on the SDIO card
  1109. Argumennt : pointer to controller structure
  1110. Return : 0, -1
  1111. \*----------------------------------------------------------------------------------------------*/
  1112. /*
  1113. static uint32_t sdio_get_cisptr(struct mmc_card *card)
  1114. {
  1115. uint32_t fn;
  1116. uint8_t dat;
  1117. for (fn=0; fn<=card->io_num; fn++) {
  1118. sdio_ioreg_read(card, FN0, FN_CIS_POINTER_0_REG(fn), &dat);
  1119. card->cisptr[fn] = dat;
  1120. sdio_ioreg_read(card, FN0, FN_CIS_POINTER_1_REG(fn), &dat);
  1121. card->cisptr[fn] |= (uint32_t)dat << 8;
  1122. sdio_ioreg_read(card, FN0, FN_CIS_POINTER_2_REG(fn), &dat);
  1123. card->cisptr[fn] |= (uint32_t)dat << 16;
  1124. SD_LOGD("Card CIS Addr = %x, fn = %d\n", card->cisptr[fn], fn);
  1125. }
  1126. return 0;
  1127. }
  1128. */
  1129. #if 0
  1130. /*--------------------------------------------------------------*\
  1131. @function : uint32_t sdio_read_cisinfo(uint8_t* buf)
  1132. @brief : this function read CIS infomation of card
  1133. @arguments: buf - pointer to data buffer
  1134. @return : 0, -1
  1135. \*--------------------------------------------------------------*/
  1136. int32_t sdio_read_cisinfo(struct mmc_card *card, uint8_t* buf)
  1137. {
  1138. uint32_t i;
  1139. int32_t ret=0;
  1140. /* Read the Tuple Data */
  1141. for (i = 0; i < SIZE_OF_TUPLE; i++) {
  1142. ret = sdio_ioreg_read(card, FN0, card->cisptr[FN0]+i, buf+i);
  1143. if (ret==-1)
  1144. return ret;
  1145. }
  1146. return ret;
  1147. }
  1148. /*--------------------------------------------------------------*\
  1149. @function : uint32_t sdio_read_manfid(uint32_t func_num)
  1150. @brief : this function read Manufacturer identification of card
  1151. @arguments: func_num - pointer to data buffer
  1152. @return : 0, -1
  1153. \*--------------------------------------------------------------*/
  1154. int32_t sdio_read_manfid(struct mmc_card *card, uint32_t func_num)
  1155. {
  1156. uint32_t offset = 0;
  1157. uint32_t manfid, card_id;
  1158. int32_t ret=0;
  1159. uint8_t tuple, link, datah, datal;
  1160. do {
  1161. ret = sdio_ioreg_read(card, func_num, card->cisptr[func_num]+offset, &tuple);
  1162. if (ret == -1)
  1163. return ret;
  1164. if (tuple == CISTPL_MANFID) {
  1165. offset += 2;
  1166. ret = sdio_ioreg_read(card, func_num, card->cisptr[func_num]+offset, &datal);
  1167. if (ret == -1)
  1168. return ret;
  1169. offset++;
  1170. ret = sdio_ioreg_read(card, func_num, card->cisptr[func_num]+offset, &datah);
  1171. if (ret == -1)
  1172. return ret;
  1173. manfid = (uint32_t)datal | (uint32_t)datah << 8;
  1174. offset++;
  1175. ret = sdio_ioreg_read(card, func_num, card->cisptr[func_num]+offset, &datal);
  1176. if (ret == -1)
  1177. return ret;
  1178. offset++;
  1179. ret = sdio_ioreg_read(card, func_num, card->cisptr[func_num]+offset, &datah);
  1180. if (ret == -1)
  1181. return ret;
  1182. card_id = (uint32_t)datal | (uint32_t)datah << 8;
  1183. card->manfid = (card_id<<16)|manfid;
  1184. SD_LOGD("Card id = %08x manfid = %08x\n", card_id, manfid);
  1185. return manfid;
  1186. }
  1187. ret = sdio_ioreg_read(card, func_num, card->cisptr[func_num]+offset+1, &link);
  1188. if (ret == -1)
  1189. return ret;
  1190. offset += link + 2;
  1191. } while(tuple != CISTPL_END);
  1192. return 0;
  1193. }
  1194. int32_t sdio_get_vendor_id(struct mmc_card *card)
  1195. {
  1196. return (sdio_read_manfid(card, FN0));
  1197. }
  1198. #endif
  1199. int
  1200. sdio_memcpy_fromio(struct mmc_card *card, unsigned int func_num, void *dst, unsigned int addr, int count)
  1201. {
  1202. return sdio_io_rw_ext_helper(card, func_num, 0, addr, 1, dst, count);
  1203. }
  1204. int
  1205. sdio_memcpy_toio(struct mmc_card *card, unsigned int func_num, unsigned int addr, const void *src, int count)
  1206. {
  1207. return sdio_io_rw_ext_helper(card, func_num, 1, addr, 1, (uint8_t *)src, count);
  1208. }
  1209. #ifdef CONFIG_SDIO_IRQ_SUPPORT
  1210. #if 0
  1211. /*-----------------------------------*\
  1212. Read IOx's Interrupt Pending
  1213. Argument: io_num - 1~7
  1214. Return: 1-pending, 0-not pending
  1215. \*-----------------------------------*/
  1216. int32_t sdio_intx_pend_rd(struct mmc_card *card, uint32_t io_num)
  1217. {
  1218. uint8_t dat;
  1219. //func_num uses default value 0
  1220. mmc_io_rw_direct_host(card->host, 0, FN0, INT_PENDING_REG, 0, &dat);
  1221. if (dat & INT(io_num)) {
  1222. SD_LOGE("IO%x Interrupt is pending !!\n", io_num);
  1223. return 0;
  1224. }
  1225. SD_LOGD("IO%x Interrupt is not pending !!\n", io_num);
  1226. return -1;
  1227. }
  1228. /* IOx's Abort *\
  1229. \* io_num: 1~7 */
  1230. int32_t sdio_iox_abort(struct mmc_card *card, uint32_t io_num)
  1231. {
  1232. mmc_io_rw_direct_host(card->host, 0, FN0, IO_ABORT_REG, AS(io_num), NULL);
  1233. return 0;
  1234. }
  1235. /**
  1236. * sdio_claim_irq - claim the IRQ for a SDIO function
  1237. * @func: SDIO function
  1238. * @handler: IRQ handler callback
  1239. *
  1240. * Claim and activate the IRQ for the given SDIO function. The provided
  1241. * handler will be called when that IRQ is asserted. The host is always
  1242. * claimed already when the handler is called so the handler must not
  1243. * call sdio_claim_host() nor sdio_release_host().
  1244. */
  1245. int sdio_claim_irq(struct mmc_card *card, unsigned int func_num, sdio_irq_handler_t *handler)
  1246. {
  1247. int32_t ret;
  1248. uint8_t reg;
  1249. if (!card) {
  1250. SD_LOGE_RAW(ROM_ERR_MASK, "%s,%d\n", __func__, __LINE__);
  1251. return -1;
  1252. }
  1253. SD_LOGN("SDIO: Enabling IRQ for %d...\n", func_num);
  1254. if (card->irq_handler) {
  1255. SD_LOGE("SDIO: IRQ for %d already in use.\n", func_num);
  1256. return -EBUSY;
  1257. }
  1258. reg = sdio_readb(card, FN0, SDIO_CCCR_IENx, &ret);
  1259. if (ret)
  1260. return ret;
  1261. reg |= 1 << func_num;
  1262. reg |= 1; /* Master interrupt enable */
  1263. sdio_writeb(card, FN0, reg, SDIO_CCCR_IENx, &ret);
  1264. if (ret)
  1265. return ret;
  1266. card->irq_handler = handler;
  1267. return ret;
  1268. }
  1269. /**
  1270. * sdio_release_irq - release the IRQ for a SDIO function
  1271. * @func: SDIO function
  1272. *
  1273. * Disable and release the IRQ for the given SDIO function.
  1274. */
  1275. int sdio_release_irq(struct mmc_card *card, unsigned int func_num)
  1276. {
  1277. int ret;
  1278. uint8_t reg;
  1279. if (!card) {
  1280. SD_LOGE_RAW(ROM_ERR_MASK, "%s,%d\n", __func__, __LINE__);
  1281. return -1;
  1282. }
  1283. SD_LOGN("SDIO: Disabling IRQ for %d...\n", func_num);
  1284. if (card->irq_handler)
  1285. card->irq_handler = NULL;
  1286. reg = sdio_readb(card, FN0, SDIO_CCCR_IENx, &ret);
  1287. if (ret)
  1288. return ret;
  1289. reg &= ~(1 << func_num);
  1290. /* Disable master interrupt with the last function interrupt */
  1291. if (!(reg & 0xFE))
  1292. reg = 0;
  1293. sdio_writeb(card, FN0, reg, SDIO_CCCR_IENx, &ret);
  1294. if (ret)
  1295. return ret;
  1296. return 0;
  1297. }
  1298. #endif
  1299. #endif
  1300. /*
  1301. * Fetch CID from card.
  1302. */
  1303. #ifdef CONFIG_USE_SDIO_COMBO
  1304. int32_t mmc_sd_get_cid(struct mmc_host *host, uint32_t ocr, uint32_t *cid, uint32_t *rocr)
  1305. {
  1306. int32_t err;
  1307. /*
  1308. * Since we're changing the OCR value, we seem to
  1309. * need to tell some cards to go back to the idle
  1310. * state. We wait 1ms to give cards time to
  1311. * respond.
  1312. */
  1313. mmc_go_idle(host);
  1314. /*
  1315. * If SD_SEND_IF_COND indicates an SD 2.0
  1316. * compliant card and we should set bit 30
  1317. * of the ocr to indicate that we can handle
  1318. * block-addressed SDHC cards.
  1319. */
  1320. err = mmc_send_if_cond(host, ocr);
  1321. if (!err)
  1322. ocr |= SD_OCR_CCS;
  1323. /*
  1324. * If the host supports one of UHS-I modes, request the card
  1325. * to switch to 1.8V signaling level.
  1326. */
  1327. if (host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  1328. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))
  1329. ocr |= SD_OCR_S18R;
  1330. /* If the host can supply more than 150mA, XPC should be set to 1. */
  1331. if (host->caps & (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 |
  1332. MMC_CAP_SET_XPC_180))
  1333. ocr |= SD_OCR_XPC;
  1334. try_again:
  1335. err = mmc_send_app_op_cond(host, ocr, rocr);
  1336. if (err)
  1337. return err;
  1338. /*
  1339. * In case CCS and S18A in the response is set, start Signal Voltage
  1340. * Switch procedure. SPI mode doesn't support CMD11.
  1341. */
  1342. if (rocr && ((*rocr & 0x41000000) == 0x41000000)) {
  1343. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180, true);
  1344. if (err) {
  1345. ocr &= ~SD_OCR_S18R;
  1346. goto try_again;
  1347. }
  1348. }
  1349. err = mmc_all_send_cid(host, cid);
  1350. return err;
  1351. }
  1352. #endif
  1353. /*--------------------------------------------------------------*\
  1354. @function : uint32_t sdio_enumerate(uint32_t smc_no)
  1355. @brief : this function enumerates and initializes SDIO card
  1356. @arguments: smc_no - number of the smc channel selected
  1357. @return : 0, -1
  1358. \*--------------------------------------------------------------*/
  1359. int32_t mmc_sdio_init_card(struct mmc_card *card)
  1360. {
  1361. int32_t err;
  1362. //struct mmc_host *host = card->host;
  1363. /* The initialization should be done at 3.3 V I/O voltage. */
  1364. //mmc_set_signal_voltage();
  1365. err = mmc_send_io_op_cond(card, card->ocr.ocr & 0xFF8000, &card->ocr.ocr);
  1366. if (err)
  1367. return err;
  1368. #ifdef CONFIG_USE_SDIO_COMBO
  1369. if ((card->ocr.ocr & R4_MEMORY_PRESENT) &&
  1370. mmc_sd_get_cid(host, host->ocr & card->ocr.ocr, card->raw_cid, NULL) == 0) {
  1371. card->type = MMC_TYPE_SD_COMBO;
  1372. SD_LOGW("SDIO Combo Card(With I/O & Memory) !!\n");
  1373. /* if with memory in it, allocate memory space for storing memory portion information */
  1374. //card->mem_info_p = (SDMMCInfo *)HAL_Malloc(sizeof(SDMMCInfo));
  1375. /* relative operations for memory portion */
  1376. } else
  1377. #endif
  1378. {
  1379. card->type = MMC_TYPE_SDIO;
  1380. SD_LOGD("Standard SDIO Card with IO portion only !!\n");
  1381. }
  1382. /*
  1383. * If the host and card support UHS-I mode request the card
  1384. * to switch to 1.8V signaling level. No 1.8v signalling if
  1385. * UHS mode is not enabled to maintain compatibilty and some
  1386. * systems that claim 1.8v signalling in fact do not support
  1387. * it.
  1388. */
  1389. #ifdef SD_SUPPORT_VERSION3
  1390. if ((card->ocr.ocr & R4_18V_PRESENT) &&
  1391. (card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  1392. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
  1393. MMC_CAP_UHS_DDR50))) {
  1394. err = mmc_set_signal_voltage(card->host, MMC_SIGNAL_VOLTAGE_180, true);
  1395. if (err) {
  1396. card->ocr.ocr &= ~R4_18V_PRESENT;
  1397. card->host->ocr &= ~R4_18V_PRESENT;
  1398. }
  1399. err = 0;
  1400. } else {
  1401. card->ocr.ocr &= ~R4_18V_PRESENT;
  1402. card->host->ocr &= ~R4_18V_PRESENT;
  1403. }
  1404. #endif
  1405. /* CMD3, Ask the card to publish a new Relative Card Address (RCA) */
  1406. err = mmc_send_relative_addr(card->host, &card->rca);
  1407. if (err) {
  1408. SD_LOGW("SD public RCA failed\n");
  1409. return err;
  1410. }
  1411. SD_LOGD("Card RCA %lx\n", HAL_PR_SZ_L(card->rca));
  1412. HAL_SDC_Update_Clk(card->host, 25000000);
  1413. /*
  1414. * Read CSD, before selecting the card
  1415. */
  1416. #ifdef CONFIG_USE_SDIO_COMBO
  1417. if (card->type == MMC_TYPE_SD_COMBO) {
  1418. err = mmc_sd_get_csd(host, card);
  1419. if (err)
  1420. return err;
  1421. mmc_decode_cid(card);
  1422. }
  1423. #endif
  1424. /* CMD7, Select card, as all following commands rely on that. */
  1425. err = mmc_select_card(card, 1);
  1426. if (err) {
  1427. SD_LOGE("Card Select Error!!\n");
  1428. return -1;
  1429. }
  1430. #ifdef CONFIG_USE_MMC_QUIRK
  1431. if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
  1432. /*
  1433. * This is non-standard SDIO device, meaning it doesn't
  1434. * have any CIA (Common I/O area) registers present.
  1435. * It's host's responsibility to fill cccr and cis
  1436. * structures in init_card().
  1437. */
  1438. mmc_set_clock(host, card->cis.max_dtr);
  1439. if (card->cccr.high_speed) {
  1440. mmc_card_set_highspeed(card);
  1441. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  1442. }
  1443. goto finish;
  1444. }
  1445. #endif
  1446. /*
  1447. * Read the common registers.
  1448. */
  1449. err = sdio_read_cccr(card, card->ocr.ocr);
  1450. if (err)
  1451. goto remove;
  1452. /*
  1453. * Read the common CIS tuples.
  1454. */
  1455. #ifdef CONFIG_SDIO_USE_FUNS
  1456. err = sdio_read_common_cis(card);
  1457. if (err)
  1458. goto remove;
  1459. #endif
  1460. #ifdef CONFIG_USE_MMC_QUIRK
  1461. mmc_fixup_device(card, NULL);
  1462. #endif
  1463. /*
  1464. * If needed, disconnect card detection pull-up resistor.
  1465. */
  1466. err = sdio_disable_cd(card);
  1467. if (err)
  1468. goto remove;
  1469. /* Initialization sequence for UHS-I cards */
  1470. /* Only if card supports 1.8v and UHS signaling */
  1471. #ifdef SD_SUPPORT_VERSION3
  1472. if ((card->ocr.ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
  1473. err = mmc_sdio_init_uhs_card(card);
  1474. if (err)
  1475. goto remove;
  1476. /* Card is an ultra-high-speed card */
  1477. mmc_card_set_uhs(card);
  1478. } else
  1479. #endif
  1480. {
  1481. /*
  1482. * Switch to high-speed (if supported).
  1483. */
  1484. err = sdio_enable_hs(card);
  1485. if (err > 0) {
  1486. mmc_card_set_highspeed(card);
  1487. SD_LOGN("sdio highspeed \n");
  1488. } else if (err)
  1489. goto remove;
  1490. /*
  1491. * Change to the card's maximum speed.
  1492. */
  1493. HAL_SDC_Update_Clk(card->host, mmc_sdio_get_max_clock(card));
  1494. /*
  1495. * Switch to wider bus (if supported).
  1496. */
  1497. err = sdio_enable_4bit_bus(card);
  1498. if (!err) {
  1499. /* sd Host Controller Register SDXC_REG_WIDTH */
  1500. HAL_SDC_Set_BusWidth(card->host, MMC_BUS_WIDTH_4);
  1501. SD_LOGN("%s bus width type:%d\n", __func__, MMC_BUS_WIDTH_4);
  1502. } else if (err)
  1503. goto remove;
  1504. }
  1505. #ifdef CONFIG_USE_MMC_QUIRK
  1506. finish:
  1507. #endif
  1508. card->host->card = card;
  1509. return 0;
  1510. remove:
  1511. card->host->card = NULL;
  1512. #ifdef CONFIG_SDIO_USE_FUNS
  1513. sdio_free_func_cis((struct sdio_func *)card->sdio_func);
  1514. sdio_free_common_cis(card);
  1515. #endif
  1516. return err;
  1517. }
  1518. uint32_t sdio_reset(struct mmc_host *host)
  1519. {
  1520. int32_t ret;
  1521. uint8_t abort;
  1522. /* SDIO Simplified Specification V2.0, 4.4 Reset for SDIO */
  1523. ret = mmc_io_rw_direct_host(host, 0, 0, SDIO_CCCR_ABORT, 0, &abort);
  1524. if (ret)
  1525. abort = 0x08;
  1526. else
  1527. abort |= 0x08;
  1528. ret = mmc_io_rw_direct_host(host, 1, 0, SDIO_CCCR_ABORT, abort, NULL);
  1529. return ret;
  1530. }
  1531. /**
  1532. * sdio_set_host_pm_flags - set wanted host power management capabilities
  1533. * @func: SDIO function attached to host
  1534. *
  1535. * Set a capability bitmask corresponding to wanted host controller
  1536. * power management features for the upcoming suspend state.
  1537. * This must be called, if needed, each time the suspend method of
  1538. * the function driver is called, and must contain only bits that
  1539. * were returned by sdio_get_host_pm_caps().
  1540. * The host doesn't need to be claimed, nor the function active,
  1541. * for this information to be set.
  1542. */
  1543. static int32_t sdio_set_host_pm_flags(sdio_t *card, uint32_t flags)
  1544. {
  1545. #ifdef CONFIG_SD_PM
  1546. struct mmc_host *host = card->host;
  1547. if (flags & ~host->pm_caps)
  1548. return -1;
  1549. /* function suspend methods are serialized, hence no lock needed */
  1550. host->pm_flags |= flags;
  1551. #endif
  1552. return 0;
  1553. }
  1554. int32_t sdio_pm(sdio_t *card, int32_t suspend)
  1555. {
  1556. int32_t ret = 0;
  1557. if (suspend) {
  1558. /* Notify SDIO that ALLWINNERTECH will remain powered during suspend */
  1559. ret = sdio_set_host_pm_flags(card, MMC_PM_KEEP_POWER);
  1560. if (ret)
  1561. SD_LOGE("Error setting SDIO pm flags: %ld\n", HAL_PR_SZ_L(ret));
  1562. }
  1563. return ret;
  1564. }
  1565. #ifdef CONFIG_SD_PM
  1566. /*
  1567. * SDIO suspend. We need to suspend all functions separately.
  1568. * Therefore all registered functions must have drivers with suspend
  1569. * and resume methods. Failing that we simply remove the whole card.
  1570. */
  1571. static int mmc_sdio_suspend(struct mmc_host *host)
  1572. {
  1573. int32_t i, err = 0;
  1574. struct mmc_card *card;
  1575. card = host->card;
  1576. card->suspend = 1;
  1577. #ifdef CONFIG_SDIO_USE_FUNS
  1578. for (i = 0; i < host->card->sdio_funcs; i++) {
  1579. struct sdio_func *func = host->card->sdio_func[i];
  1580. if (func && sdio_func_present(func) && func->dev.driver) {
  1581. err = sdio_bus_pm_suspend(&func->dev);
  1582. sdio_disable_func(host->card, FN1);
  1583. if (err)
  1584. break;
  1585. }
  1586. }
  1587. while (err && --i >= 0) {
  1588. struct sdio_func *func = host->card->sdio_func[i];
  1589. if (func && sdio_func_present(func) && func->dev.driver) {
  1590. sdio_bus_pm_resume(&func->dev);
  1591. }
  1592. }
  1593. #else
  1594. (void)i;
  1595. #endif
  1596. if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
  1597. mmc_claim_host(host);
  1598. sdio_disable_wide(host->card);
  1599. mmc_release_host(host);
  1600. }
  1601. return err;
  1602. }
  1603. int mmc_sdio_resume(struct mmc_host *host)
  1604. {
  1605. int32_t i, err = 0;
  1606. struct mmc_card *card;
  1607. if (!host) {
  1608. SDC_LOGE_RAW(ROM_ERR_MASK, "%s,%d no host exist!\n", __func__, __LINE__);
  1609. return -1;
  1610. }
  1611. if (!host->card) {
  1612. SDC_LOGE("%s,%d no card exist!\n", __func__, __LINE__);
  1613. return -1;
  1614. }
  1615. card = host->card;
  1616. /* Basic card reinitialization. */
  1617. mmc_claim_host(host);
  1618. /* No need to reinitialize powered-resumed nonremovable cards */
  1619. if (!mmc_card_keep_power(host)) {
  1620. err = mmc_sdio_init_card(host->card);
  1621. } else if (mmc_card_wake_sdio_irq(host)) {
  1622. /* We may have switched to 1-bit mode during suspend */
  1623. err = sdio_enable_4bit_bus(host->card);
  1624. if (!err) {
  1625. HAL_SDC_Set_BusWidth(host->card->host, MMC_BUS_WIDTH_4);
  1626. SDC_LOGN("%s bus width type:%d\n", __func__, MMC_BUS_WIDTH_4);
  1627. err = 0;
  1628. }
  1629. }
  1630. #ifdef CONFIG_SDIO_IRQ_SUPPORT
  1631. if (!err && host->sdio_irqs)
  1632. wake_up_process(host->sdio_irq_thread);
  1633. #endif
  1634. mmc_release_host(host);
  1635. #ifdef CONFIG_SDIO_USE_FUNS
  1636. /*
  1637. * If the card looked to be the same as before suspending, then
  1638. * we proceed to resume all card functions. If one of them returns
  1639. * an error then we simply return that error to the core and the
  1640. * card will be redetected as new. It is the responsibility of
  1641. * the function driver to perform further tests with the extra
  1642. * knowledge it has of the card to confirm the card is indeed the
  1643. * same as before suspending (same MAC address for network cards,
  1644. * etc.) and return an error otherwise.
  1645. */
  1646. for (i = 0; !err && i < host->card->sdio_funcs; i++) {
  1647. struct sdio_func *func = host->card->sdio_func[i];
  1648. if (func && sdio_func_present(func) && func->dev.driver) {
  1649. err = sdio_bus_pm_resume(&func->dev);
  1650. sdio_enable_func(host->card, FN1);
  1651. }
  1652. }
  1653. #else
  1654. (void)i;
  1655. #endif
  1656. card->suspend = 0;
  1657. return err;
  1658. }
  1659. static const struct mmc_bus_ops sdio_bus_ops = {
  1660. .suspend = mmc_sdio_suspend,
  1661. .resume = mmc_sdio_resume,
  1662. };
  1663. #endif
  1664. /*
  1665. * Starting point for SDIO card init.
  1666. */
  1667. int mmc_attach_sdio(struct mmc_card *card, struct mmc_host *host)
  1668. {
  1669. int err = -1;
  1670. int32_t funcs;
  1671. int i;
  1672. if (!host) {
  1673. SD_LOGE("%s,%d no host exist!\n", __func__, __LINE__);
  1674. return -1;
  1675. }
  1676. /* CMD5 arg=0; get card support Voltage */
  1677. err = mmc_send_io_op_cond(card, 0, &card->ocr.ocr);
  1678. if (err)
  1679. return err;
  1680. SD_LOGD("card ocr:%lx\n", HAL_PR_SZ_L(card->ocr.ocr));
  1681. /*
  1682. * Sanity check the voltages that the card claims to
  1683. * support.
  1684. */
  1685. if (card->ocr.ocr & 0x7F) {
  1686. SD_LOGW("card claims to support voltages below the defined range."
  1687. "These will be ignored.\n");
  1688. card->ocr.ocr &= ~0x7F;
  1689. }
  1690. /* Detect and init the card. */
  1691. err = mmc_sdio_init_card(card);
  1692. if (err)
  1693. return err;
  1694. funcs = (card->ocr.ocr >> 28) & 0x7;
  1695. SD_LOGD("Number of I/O Functions: %02lx\n", HAL_PR_SZ_L(funcs));
  1696. /*
  1697. * Initialize (but don't add) all present functions.
  1698. */
  1699. #ifdef CONFIG_SDIO_USE_FUNS
  1700. card->sdio_funcs = 0;
  1701. // for (int i = 0; i < card->io_num; i++, card->sdio_funcs++) {
  1702. // for (int i = 0; i < funcs; i++, card->sdio_funcs++) {
  1703. for (i = 0; i < funcs; i++, card->sdio_funcs++) {
  1704. err = sdio_init_func(host->card, i + 1);
  1705. if (err)
  1706. goto remove;
  1707. }
  1708. #endif
  1709. /*
  1710. * First add the card to the driver model...
  1711. */
  1712. mmc_release_host(host);
  1713. if(host->caps&MMC_CAP_SDIO_IRQ){
  1714. int ret = OS_SemaphoreCreateBinary(&host->sdio_irq_signal);
  1715. SDC_BUG_ON(ret!=OS_OK);
  1716. }
  1717. mmc_add_card(host->card);
  1718. /*
  1719. * ...then the SDIO functions.
  1720. */
  1721. #ifdef CONFIG_SDIO_USE_FUNS
  1722. // for (int i = 0; i < funcs; i++) {
  1723. for (i = 0; i < funcs; i++) {
  1724. err = sdio_add_func(host->card->sdio_func[i], i);
  1725. if (err)
  1726. goto remove_added;
  1727. }
  1728. #else
  1729. (void)funcs;
  1730. #endif
  1731. card->fn_bsize[1] = 512;
  1732. mmc_claim_host(host);
  1733. #ifdef CONFIG_SD_PM
  1734. if (!card->suspend) {
  1735. mmc_attach_bus(host, &sdio_bus_ops);
  1736. }
  1737. #endif
  1738. return 0;
  1739. #ifdef CONFIG_SDIO_USE_FUNS
  1740. remove_added:
  1741. /* Remove without lock if the device has been added. */
  1742. mmc_sdio_remove(host);
  1743. #endif
  1744. card->host->card = NULL;
  1745. mmc_claim_host(host);
  1746. #ifdef CONFIG_SDIO_USE_FUNS
  1747. remove:
  1748. /* And with lock if it hasn't been added. */
  1749. mmc_release_host(host);
  1750. if (host->card) {
  1751. mmc_sdio_remove(host);
  1752. card->host->card = NULL;
  1753. }
  1754. mmc_claim_host(host);
  1755. #else
  1756. card->host->card = NULL;
  1757. #endif
  1758. SD_LOGE("%s: error %d whilst initialising SDIO card\n", __func__, err);
  1759. return err;
  1760. }
  1761. void mmc_deattach_sdio(struct mmc_card *card, struct mmc_host *host)
  1762. {
  1763. if (!card) {
  1764. SD_LOGE_RAW(ROM_ERR_MASK, "%s,%d\n", __func__, __LINE__);
  1765. return ;
  1766. }
  1767. #ifdef CONFIG_SD_PM
  1768. if (!card->suspend) {
  1769. mmc_detach_bus(card->host);
  1770. }
  1771. #endif
  1772. if(host->caps&MMC_CAP_SDIO_IRQ){
  1773. int ret = OS_SemaphoreDelete(&host->sdio_irq_signal);
  1774. SDC_BUG_ON(ret!=OS_OK);
  1775. }
  1776. }
  1777. /**
  1778. * sdio_align_size - pads a transfer size to a more optimal value
  1779. * @card: SDIO card
  1780. * @sz: original transfer size
  1781. *
  1782. * Pads the original data size with a number of extra bytes in
  1783. * order to avoid controller bugs and/or performance hits
  1784. * (e.g. some controllers revert to PIO for certain sizes).
  1785. *
  1786. * If possible, it will also adjust the size so that it can be
  1787. * handled in just a single request.
  1788. *
  1789. * Returns the improved size, which might be unmodified.
  1790. */
  1791. //unsigned int sdio_align_size(struct mmc_card *card, unsigned int sz)
  1792. unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
  1793. {
  1794. #ifdef CONFIG_SDIO_USE_FUNS
  1795. unsigned int orig_sz;
  1796. unsigned int blk_sz, byte_sz;
  1797. unsigned chunk_sz;
  1798. orig_sz = sz;
  1799. #endif
  1800. /* Do a first check with the controller, in case it
  1801. * wants to increase the size up to a point where it
  1802. * might need more than one block.
  1803. */
  1804. //sz = mmc_align_data_size(card, sz);
  1805. sz = mmc_align_data_size(func->card, sz);
  1806. #ifdef CONFIG_SDIO_USE_FUNS
  1807. /*
  1808. * If we can still do this with just a byte transfer, then
  1809. * we're done.
  1810. */
  1811. if (sz <= sdio_max_byte_size(func))
  1812. return sz;
  1813. if (func->card->cccr.multi_block) {
  1814. /*
  1815. * Check if the transfer is already block aligned
  1816. */
  1817. if ((sz % func->cur_blksize) == 0) {
  1818. return sz;
  1819. }
  1820. /*
  1821. * Realign it so that it can be done with one request,
  1822. * and recheck if the controller still likes it.
  1823. */
  1824. blk_sz = ((sz + func->cur_blksize - 1) /
  1825. func->cur_blksize) * func->cur_blksize;
  1826. blk_sz = mmc_align_data_size(func->card, blk_sz);
  1827. /*
  1828. * This value is only good if it is still just
  1829. * one request.
  1830. */
  1831. if ((blk_sz % func->cur_blksize) == 0)
  1832. return blk_sz;
  1833. /*
  1834. * We failed to do one request, but at least try to
  1835. * pad the remainder properly.
  1836. */
  1837. byte_sz = mmc_align_data_size(func->card,
  1838. sz % func->cur_blksize);
  1839. if (byte_sz <= sdio_max_byte_size(func)) {
  1840. blk_sz = sz / func->cur_blksize;
  1841. return blk_sz * func->cur_blksize + byte_sz;
  1842. }
  1843. } else {
  1844. /*
  1845. * We need multiple requests, so first check that the
  1846. * controller can handle the chunk size;
  1847. */
  1848. chunk_sz = mmc_align_data_size(func->card,
  1849. sdio_max_byte_size(func));
  1850. if (chunk_sz == sdio_max_byte_size(func)) {
  1851. /*
  1852. * Fix up the size of the remainder (if any)
  1853. */
  1854. byte_sz = orig_sz % chunk_sz;
  1855. if (byte_sz) {
  1856. byte_sz = mmc_align_data_size(func->card,
  1857. byte_sz);
  1858. }
  1859. return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
  1860. }
  1861. }
  1862. /* The controller is simply incapable of transferring the size
  1863. * we want in decent manner, so just return the original size.
  1864. */
  1865. return orig_sz;
  1866. #endif
  1867. return sz;
  1868. }
  1869. #ifdef SDIO_EXCLUSIVE_HOST
  1870. /**
  1871. * sdio_claim_host - exclusively claim a bus for a certain SDIO function
  1872. * @card: SDIO that will be accessed
  1873. *
  1874. * Claim a bus for a set of operations. The SDIO function given
  1875. * is used to figure out which bus is relevant.
  1876. */
  1877. void sdio_claim_host(struct mmc_card *card)
  1878. {
  1879. HAL_SDC_Claim_Host(card->host);
  1880. }
  1881. /**
  1882. * sdio_release_host - release a bus for a certain SDIO function
  1883. * @card: SDIO that was accessed
  1884. *
  1885. * Release a bus, allowing others to claim the bus for their
  1886. * operations.
  1887. */
  1888. void sdio_release_host(struct mmc_card *card)
  1889. {
  1890. HAL_SDC_Release_Host(card->host);
  1891. }
  1892. #endif
  1893. #endif /* CONFIG_USE_SDIO */