sdhci.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568
  1. /*
  2. * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
  3. *
  4. * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * Thanks to the following companies for their support:
  12. *
  13. * - JMicron (hardware and technical support)
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/highmem.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/slab.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/leds.h>
  25. #include <linux/mmc/mmc.h>
  26. #include <linux/mmc/host.h>
  27. #include <linux/mmc/card.h>
  28. #include <linux/mmc/sdio.h>
  29. #include <linux/mmc/slot-gpio.h>
  30. #include "sdhci.h"
  31. #define DRIVER_NAME "sdhci"
  32. #define DBG(f, x...) \
  33. pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
  34. #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
  35. defined(CONFIG_MMC_SDHCI_MODULE))
  36. #define SDHCI_USE_LEDS_CLASS
  37. #endif
  38. #define MAX_TUNING_LOOP 40
  39. static unsigned int debug_quirks = 0;
  40. static unsigned int debug_quirks2;
  41. static void sdhci_finish_data(struct sdhci_host *);
  42. static void sdhci_finish_command(struct sdhci_host *);
  43. static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
  44. static void sdhci_tuning_timer(unsigned long data);
  45. static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
  46. static int sdhci_pre_dma_transfer(struct sdhci_host *host,
  47. struct mmc_data *data,
  48. struct sdhci_host_next *next);
  49. static int sdhci_do_get_cd(struct sdhci_host *host);
  50. #ifdef CONFIG_PM
  51. static int sdhci_runtime_pm_get(struct sdhci_host *host);
  52. static int sdhci_runtime_pm_put(struct sdhci_host *host);
  53. static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
  54. static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
  55. #else
  56. static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
  57. {
  58. return 0;
  59. }
  60. static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
  61. {
  62. return 0;
  63. }
  64. static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
  65. {
  66. }
  67. static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
  68. {
  69. }
  70. #endif
  71. static void sdhci_dumpregs(struct sdhci_host *host)
  72. {
  73. pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
  74. mmc_hostname(host->mmc));
  75. pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
  76. sdhci_readl(host, SDHCI_DMA_ADDRESS),
  77. sdhci_readw(host, SDHCI_HOST_VERSION));
  78. pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  79. sdhci_readw(host, SDHCI_BLOCK_SIZE),
  80. sdhci_readw(host, SDHCI_BLOCK_COUNT));
  81. pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
  82. sdhci_readl(host, SDHCI_ARGUMENT),
  83. sdhci_readw(host, SDHCI_TRANSFER_MODE));
  84. pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
  85. sdhci_readl(host, SDHCI_PRESENT_STATE),
  86. sdhci_readb(host, SDHCI_HOST_CONTROL));
  87. pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
  88. sdhci_readb(host, SDHCI_POWER_CONTROL),
  89. sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
  90. pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
  91. sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
  92. sdhci_readw(host, SDHCI_CLOCK_CONTROL));
  93. pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
  94. sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
  95. sdhci_readl(host, SDHCI_INT_STATUS));
  96. pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
  97. sdhci_readl(host, SDHCI_INT_ENABLE),
  98. sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
  99. pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
  100. sdhci_readw(host, SDHCI_ACMD12_ERR),
  101. sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
  102. pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
  103. sdhci_readl(host, SDHCI_CAPABILITIES),
  104. sdhci_readl(host, SDHCI_CAPABILITIES_1));
  105. pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
  106. sdhci_readw(host, SDHCI_COMMAND),
  107. sdhci_readl(host, SDHCI_MAX_CURRENT));
  108. pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
  109. sdhci_readw(host, SDHCI_HOST_CONTROL2));
  110. if (host->flags & SDHCI_USE_ADMA) {
  111. if (host->flags & SDHCI_USE_64_BIT_DMA)
  112. pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
  113. readl(host->ioaddr + SDHCI_ADMA_ERROR),
  114. readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
  115. readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
  116. else
  117. pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
  118. readl(host->ioaddr + SDHCI_ADMA_ERROR),
  119. readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
  120. }
  121. pr_debug(DRIVER_NAME ": ===========================================\n");
  122. }
  123. /*****************************************************************************\
  124. * *
  125. * Low level functions *
  126. * *
  127. \*****************************************************************************/
  128. static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
  129. {
  130. u32 present;
  131. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
  132. (host->mmc->caps & MMC_CAP_NONREMOVABLE))
  133. return;
  134. if (enable) {
  135. present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  136. SDHCI_CARD_PRESENT;
  137. host->ier |= present ? SDHCI_INT_CARD_REMOVE :
  138. SDHCI_INT_CARD_INSERT;
  139. } else {
  140. host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
  141. }
  142. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  143. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  144. }
  145. static void sdhci_enable_card_detection(struct sdhci_host *host)
  146. {
  147. sdhci_set_card_detection(host, true);
  148. }
  149. static void sdhci_disable_card_detection(struct sdhci_host *host)
  150. {
  151. sdhci_set_card_detection(host, false);
  152. }
  153. void sdhci_reset(struct sdhci_host *host, u8 mask)
  154. {
  155. unsigned long timeout;
  156. sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  157. if (mask & SDHCI_RESET_ALL) {
  158. host->clock = 0;
  159. /* Reset-all turns off SD Bus Power */
  160. if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  161. sdhci_runtime_pm_bus_off(host);
  162. }
  163. /* Wait max 100 ms */
  164. timeout = 100;
  165. /* hw clears the bit when it's done */
  166. while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
  167. if (timeout == 0) {
  168. pr_err("%s: Reset 0x%x never completed.\n",
  169. mmc_hostname(host->mmc), (int)mask);
  170. sdhci_dumpregs(host);
  171. return;
  172. }
  173. timeout--;
  174. mdelay(1);
  175. }
  176. }
  177. EXPORT_SYMBOL_GPL(sdhci_reset);
  178. static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
  179. {
  180. if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
  181. if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
  182. SDHCI_CARD_PRESENT))
  183. return;
  184. }
  185. host->ops->reset(host, mask);
  186. if (mask & SDHCI_RESET_ALL) {
  187. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  188. if (host->ops->enable_dma)
  189. host->ops->enable_dma(host);
  190. }
  191. /* Resetting the controller clears many */
  192. host->preset_enabled = false;
  193. }
  194. }
  195. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
  196. static void sdhci_init(struct sdhci_host *host, int soft)
  197. {
  198. if (soft)
  199. sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
  200. else
  201. sdhci_do_reset(host, SDHCI_RESET_ALL);
  202. host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
  203. SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
  204. SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
  205. SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
  206. SDHCI_INT_RESPONSE;
  207. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  208. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  209. if (soft) {
  210. /* force clock reconfiguration */
  211. host->clock = 0;
  212. sdhci_set_ios(host->mmc, &host->mmc->ios);
  213. }
  214. }
  215. static void sdhci_reinit(struct sdhci_host *host)
  216. {
  217. sdhci_init(host, 0);
  218. /*
  219. * Retuning stuffs are affected by different cards inserted and only
  220. * applicable to UHS-I cards. So reset these fields to their initial
  221. * value when card is removed.
  222. */
  223. if (host->flags & SDHCI_USING_RETUNING_TIMER) {
  224. host->flags &= ~SDHCI_USING_RETUNING_TIMER;
  225. del_timer_sync(&host->tuning_timer);
  226. host->flags &= ~SDHCI_NEEDS_RETUNING;
  227. }
  228. sdhci_enable_card_detection(host);
  229. }
  230. static void sdhci_activate_led(struct sdhci_host *host)
  231. {
  232. u8 ctrl;
  233. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  234. ctrl |= SDHCI_CTRL_LED;
  235. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  236. }
  237. static void sdhci_deactivate_led(struct sdhci_host *host)
  238. {
  239. u8 ctrl;
  240. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  241. ctrl &= ~SDHCI_CTRL_LED;
  242. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  243. }
  244. #ifdef SDHCI_USE_LEDS_CLASS
  245. static void sdhci_led_control(struct led_classdev *led,
  246. enum led_brightness brightness)
  247. {
  248. struct sdhci_host *host = container_of(led, struct sdhci_host, led);
  249. unsigned long flags;
  250. spin_lock_irqsave(&host->lock, flags);
  251. if (host->runtime_suspended)
  252. goto out;
  253. if (brightness == LED_OFF)
  254. sdhci_deactivate_led(host);
  255. else
  256. sdhci_activate_led(host);
  257. out:
  258. spin_unlock_irqrestore(&host->lock, flags);
  259. }
  260. #endif
  261. /*****************************************************************************\
  262. * *
  263. * Core functions *
  264. * *
  265. \*****************************************************************************/
  266. static void sdhci_read_block_pio(struct sdhci_host *host)
  267. {
  268. unsigned long flags;
  269. size_t blksize, len, chunk;
  270. u32 uninitialized_var(scratch);
  271. u8 *buf;
  272. DBG("PIO reading\n");
  273. blksize = host->data->blksz;
  274. chunk = 0;
  275. local_irq_save(flags);
  276. while (blksize) {
  277. if (!sg_miter_next(&host->sg_miter))
  278. BUG();
  279. len = min(host->sg_miter.length, blksize);
  280. blksize -= len;
  281. host->sg_miter.consumed = len;
  282. buf = host->sg_miter.addr;
  283. while (len) {
  284. if (chunk == 0) {
  285. scratch = sdhci_readl(host, SDHCI_BUFFER);
  286. chunk = 4;
  287. }
  288. *buf = scratch & 0xFF;
  289. buf++;
  290. scratch >>= 8;
  291. chunk--;
  292. len--;
  293. }
  294. }
  295. sg_miter_stop(&host->sg_miter);
  296. local_irq_restore(flags);
  297. }
  298. static void sdhci_write_block_pio(struct sdhci_host *host)
  299. {
  300. unsigned long flags;
  301. size_t blksize, len, chunk;
  302. u32 scratch;
  303. u8 *buf;
  304. DBG("PIO writing\n");
  305. blksize = host->data->blksz;
  306. chunk = 0;
  307. scratch = 0;
  308. local_irq_save(flags);
  309. while (blksize) {
  310. if (!sg_miter_next(&host->sg_miter))
  311. BUG();
  312. len = min(host->sg_miter.length, blksize);
  313. blksize -= len;
  314. host->sg_miter.consumed = len;
  315. buf = host->sg_miter.addr;
  316. while (len) {
  317. scratch |= (u32)*buf << (chunk * 8);
  318. buf++;
  319. chunk++;
  320. len--;
  321. if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
  322. sdhci_writel(host, scratch, SDHCI_BUFFER);
  323. chunk = 0;
  324. scratch = 0;
  325. }
  326. }
  327. }
  328. sg_miter_stop(&host->sg_miter);
  329. local_irq_restore(flags);
  330. }
  331. static void sdhci_transfer_pio(struct sdhci_host *host)
  332. {
  333. u32 mask;
  334. BUG_ON(!host->data);
  335. if (host->blocks == 0)
  336. return;
  337. if (host->data->flags & MMC_DATA_READ)
  338. mask = SDHCI_DATA_AVAILABLE;
  339. else
  340. mask = SDHCI_SPACE_AVAILABLE;
  341. /*
  342. * Some controllers (JMicron JMB38x) mess up the buffer bits
  343. * for transfers < 4 bytes. As long as it is just one block,
  344. * we can ignore the bits.
  345. */
  346. if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
  347. (host->data->blocks == 1))
  348. mask = ~0;
  349. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  350. if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
  351. udelay(100);
  352. if (host->data->flags & MMC_DATA_READ)
  353. sdhci_read_block_pio(host);
  354. else
  355. sdhci_write_block_pio(host);
  356. host->blocks--;
  357. if (host->blocks == 0)
  358. break;
  359. }
  360. DBG("PIO transfer complete.\n");
  361. }
  362. static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
  363. {
  364. local_irq_save(*flags);
  365. return kmap_atomic(sg_page(sg)) + sg->offset;
  366. }
  367. static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
  368. {
  369. kunmap_atomic(buffer);
  370. local_irq_restore(*flags);
  371. }
  372. static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
  373. dma_addr_t addr, int len, unsigned cmd)
  374. {
  375. struct sdhci_adma2_64_desc *dma_desc = desc;
  376. /* 32-bit and 64-bit descriptors have these members in same position */
  377. dma_desc->cmd = cpu_to_le16(cmd);
  378. dma_desc->len = cpu_to_le16(len);
  379. dma_desc->addr_lo = cpu_to_le32((u32)addr);
  380. if (host->flags & SDHCI_USE_64_BIT_DMA)
  381. dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
  382. }
  383. static void sdhci_adma_mark_end(void *desc)
  384. {
  385. struct sdhci_adma2_64_desc *dma_desc = desc;
  386. /* 32-bit and 64-bit descriptors have 'cmd' in same position */
  387. dma_desc->cmd |= cpu_to_le16(ADMA2_END);
  388. }
  389. static int sdhci_adma_table_pre(struct sdhci_host *host,
  390. struct mmc_data *data)
  391. {
  392. int direction;
  393. void *desc;
  394. void *align;
  395. dma_addr_t addr;
  396. dma_addr_t align_addr;
  397. int len, offset;
  398. struct scatterlist *sg;
  399. int i;
  400. char *buffer;
  401. unsigned long flags;
  402. /*
  403. * The spec does not specify endianness of descriptor table.
  404. * We currently guess that it is LE.
  405. */
  406. if (data->flags & MMC_DATA_READ)
  407. direction = DMA_FROM_DEVICE;
  408. else
  409. direction = DMA_TO_DEVICE;
  410. host->align_addr = dma_map_single(mmc_dev(host->mmc),
  411. host->align_buffer, host->align_buffer_sz, direction);
  412. if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
  413. goto fail;
  414. BUG_ON(host->align_addr & host->align_mask);
  415. host->sg_count = sdhci_pre_dma_transfer(host, data, NULL);
  416. if (host->sg_count < 0)
  417. goto unmap_align;
  418. desc = host->adma_table;
  419. align = host->align_buffer;
  420. align_addr = host->align_addr;
  421. for_each_sg(data->sg, sg, host->sg_count, i) {
  422. addr = sg_dma_address(sg);
  423. len = sg_dma_len(sg);
  424. /*
  425. * The SDHCI specification states that ADMA
  426. * addresses must be 32-bit aligned. If they
  427. * aren't, then we use a bounce buffer for
  428. * the (up to three) bytes that screw up the
  429. * alignment.
  430. */
  431. offset = (host->align_sz - (addr & host->align_mask)) &
  432. host->align_mask;
  433. if (offset) {
  434. if (data->flags & MMC_DATA_WRITE) {
  435. buffer = sdhci_kmap_atomic(sg, &flags);
  436. memcpy(align, buffer, offset);
  437. sdhci_kunmap_atomic(buffer, &flags);
  438. }
  439. /* tran, valid */
  440. sdhci_adma_write_desc(host, desc, align_addr, offset,
  441. ADMA2_TRAN_VALID);
  442. BUG_ON(offset > 65536);
  443. align += host->align_sz;
  444. align_addr += host->align_sz;
  445. desc += host->desc_sz;
  446. addr += offset;
  447. len -= offset;
  448. }
  449. BUG_ON(len > 65536);
  450. /* tran, valid */
  451. sdhci_adma_write_desc(host, desc, addr, len, ADMA2_TRAN_VALID);
  452. desc += host->desc_sz;
  453. /*
  454. * If this triggers then we have a calculation bug
  455. * somewhere. :/
  456. */
  457. WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
  458. }
  459. if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
  460. /*
  461. * Mark the last descriptor as the terminating descriptor
  462. */
  463. if (desc != host->adma_table) {
  464. desc -= host->desc_sz;
  465. sdhci_adma_mark_end(desc);
  466. }
  467. } else {
  468. /*
  469. * Add a terminating entry.
  470. */
  471. /* nop, end, valid */
  472. sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
  473. }
  474. /*
  475. * Resync align buffer as we might have changed it.
  476. */
  477. if (data->flags & MMC_DATA_WRITE) {
  478. dma_sync_single_for_device(mmc_dev(host->mmc),
  479. host->align_addr, host->align_buffer_sz, direction);
  480. }
  481. return 0;
  482. unmap_align:
  483. dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
  484. host->align_buffer_sz, direction);
  485. fail:
  486. return -EINVAL;
  487. }
  488. static void sdhci_adma_table_post(struct sdhci_host *host,
  489. struct mmc_data *data)
  490. {
  491. int direction;
  492. struct scatterlist *sg;
  493. int i, size;
  494. void *align;
  495. char *buffer;
  496. unsigned long flags;
  497. bool has_unaligned;
  498. if (data->flags & MMC_DATA_READ)
  499. direction = DMA_FROM_DEVICE;
  500. else
  501. direction = DMA_TO_DEVICE;
  502. dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
  503. host->align_buffer_sz, direction);
  504. /* Do a quick scan of the SG list for any unaligned mappings */
  505. has_unaligned = false;
  506. for_each_sg(data->sg, sg, host->sg_count, i)
  507. if (sg_dma_address(sg) & host->align_mask) {
  508. has_unaligned = true;
  509. break;
  510. }
  511. if (has_unaligned && data->flags & MMC_DATA_READ) {
  512. dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
  513. data->sg_len, direction);
  514. align = host->align_buffer;
  515. for_each_sg(data->sg, sg, host->sg_count, i) {
  516. if (sg_dma_address(sg) & host->align_mask) {
  517. size = host->align_sz -
  518. (sg_dma_address(sg) & host->align_mask);
  519. buffer = sdhci_kmap_atomic(sg, &flags);
  520. memcpy(buffer, align, size);
  521. sdhci_kunmap_atomic(buffer, &flags);
  522. align += host->align_sz;
  523. }
  524. }
  525. }
  526. if (!data->host_cookie)
  527. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  528. data->sg_len, direction);
  529. }
  530. static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  531. {
  532. u8 count;
  533. struct mmc_data *data = cmd->data;
  534. unsigned target_timeout, current_timeout;
  535. /*
  536. * If the host controller provides us with an incorrect timeout
  537. * value, just skip the check and use 0xE. The hardware may take
  538. * longer to time out, but that's much better than having a too-short
  539. * timeout value.
  540. */
  541. if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
  542. return 0xE;
  543. /* Unspecified timeout, assume max */
  544. if (!data && !cmd->busy_timeout)
  545. return 0xE;
  546. /* timeout in us */
  547. if (!data)
  548. target_timeout = cmd->busy_timeout * 1000;
  549. else {
  550. target_timeout = data->timeout_ns / 1000;
  551. if (host->clock)
  552. target_timeout += data->timeout_clks / host->clock;
  553. }
  554. /*
  555. * Figure out needed cycles.
  556. * We do this in steps in order to fit inside a 32 bit int.
  557. * The first step is the minimum timeout, which will have a
  558. * minimum resolution of 6 bits:
  559. * (1) 2^13*1000 > 2^22,
  560. * (2) host->timeout_clk < 2^16
  561. * =>
  562. * (1) / (2) > 2^6
  563. */
  564. count = 0;
  565. current_timeout = (1 << 13) * 1000 / host->timeout_clk;
  566. while (current_timeout < target_timeout) {
  567. count++;
  568. current_timeout <<= 1;
  569. if (count >= 0xF)
  570. break;
  571. }
  572. if (count >= 0xF) {
  573. DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
  574. mmc_hostname(host->mmc), count, cmd->opcode);
  575. count = 0xE;
  576. }
  577. return count;
  578. }
  579. static void sdhci_set_transfer_irqs(struct sdhci_host *host)
  580. {
  581. u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  582. u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  583. if (host->flags & SDHCI_REQ_USE_DMA)
  584. host->ier = (host->ier & ~pio_irqs) | dma_irqs;
  585. else
  586. host->ier = (host->ier & ~dma_irqs) | pio_irqs;
  587. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  588. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  589. }
  590. static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  591. {
  592. u8 count;
  593. if (host->ops->set_timeout) {
  594. host->ops->set_timeout(host, cmd);
  595. } else {
  596. count = sdhci_calc_timeout(host, cmd);
  597. sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
  598. }
  599. }
  600. static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
  601. {
  602. u8 ctrl;
  603. struct mmc_data *data = cmd->data;
  604. int ret;
  605. WARN_ON(host->data);
  606. if (data || (cmd->flags & MMC_RSP_BUSY))
  607. sdhci_set_timeout(host, cmd);
  608. if (!data)
  609. return;
  610. /* Sanity checks */
  611. BUG_ON(data->blksz * data->blocks > 524288);
  612. BUG_ON(data->blksz > host->mmc->max_blk_size);
  613. BUG_ON(data->blocks > 65535);
  614. host->data = data;
  615. host->data_early = 0;
  616. host->data->bytes_xfered = 0;
  617. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
  618. host->flags |= SDHCI_REQ_USE_DMA;
  619. /*
  620. * FIXME: This doesn't account for merging when mapping the
  621. * scatterlist.
  622. */
  623. if (host->flags & SDHCI_REQ_USE_DMA) {
  624. int broken, i;
  625. struct scatterlist *sg;
  626. broken = 0;
  627. if (host->flags & SDHCI_USE_ADMA) {
  628. if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
  629. broken = 1;
  630. } else {
  631. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
  632. broken = 1;
  633. }
  634. if (unlikely(broken)) {
  635. for_each_sg(data->sg, sg, data->sg_len, i) {
  636. if (sg->length & 0x3) {
  637. DBG("Reverting to PIO because of "
  638. "transfer size (%d)\n",
  639. sg->length);
  640. host->flags &= ~SDHCI_REQ_USE_DMA;
  641. break;
  642. }
  643. }
  644. }
  645. }
  646. /*
  647. * The assumption here being that alignment is the same after
  648. * translation to device address space.
  649. */
  650. if (host->flags & SDHCI_REQ_USE_DMA) {
  651. int broken, i;
  652. struct scatterlist *sg;
  653. broken = 0;
  654. if (host->flags & SDHCI_USE_ADMA) {
  655. /*
  656. * As we use 3 byte chunks to work around
  657. * alignment problems, we need to check this
  658. * quirk.
  659. */
  660. if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
  661. broken = 1;
  662. } else {
  663. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
  664. broken = 1;
  665. }
  666. if (unlikely(broken)) {
  667. for_each_sg(data->sg, sg, data->sg_len, i) {
  668. if (sg->offset & 0x3) {
  669. DBG("Reverting to PIO because of "
  670. "bad alignment\n");
  671. host->flags &= ~SDHCI_REQ_USE_DMA;
  672. break;
  673. }
  674. }
  675. }
  676. }
  677. if (host->flags & SDHCI_REQ_USE_DMA) {
  678. if (host->flags & SDHCI_USE_ADMA) {
  679. ret = sdhci_adma_table_pre(host, data);
  680. if (ret) {
  681. /*
  682. * This only happens when someone fed
  683. * us an invalid request.
  684. */
  685. WARN_ON(1);
  686. host->flags &= ~SDHCI_REQ_USE_DMA;
  687. } else {
  688. sdhci_writel(host, host->adma_addr,
  689. SDHCI_ADMA_ADDRESS);
  690. if (host->flags & SDHCI_USE_64_BIT_DMA)
  691. sdhci_writel(host,
  692. (u64)host->adma_addr >> 32,
  693. SDHCI_ADMA_ADDRESS_HI);
  694. }
  695. } else {
  696. int sg_cnt;
  697. sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
  698. if (sg_cnt == 0) {
  699. /*
  700. * This only happens when someone fed
  701. * us an invalid request.
  702. */
  703. WARN_ON(1);
  704. host->flags &= ~SDHCI_REQ_USE_DMA;
  705. } else {
  706. WARN_ON(sg_cnt != 1);
  707. sdhci_writel(host, sg_dma_address(data->sg),
  708. SDHCI_DMA_ADDRESS);
  709. }
  710. }
  711. }
  712. /*
  713. * Always adjust the DMA selection as some controllers
  714. * (e.g. JMicron) can't do PIO properly when the selection
  715. * is ADMA.
  716. */
  717. if (host->version >= SDHCI_SPEC_200) {
  718. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  719. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  720. if ((host->flags & SDHCI_REQ_USE_DMA) &&
  721. (host->flags & SDHCI_USE_ADMA)) {
  722. if (host->flags & SDHCI_USE_64_BIT_DMA)
  723. ctrl |= SDHCI_CTRL_ADMA64;
  724. else
  725. ctrl |= SDHCI_CTRL_ADMA32;
  726. } else {
  727. ctrl |= SDHCI_CTRL_SDMA;
  728. }
  729. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  730. }
  731. if (!(host->flags & SDHCI_REQ_USE_DMA)) {
  732. int flags;
  733. flags = SG_MITER_ATOMIC;
  734. if (host->data->flags & MMC_DATA_READ)
  735. flags |= SG_MITER_TO_SG;
  736. else
  737. flags |= SG_MITER_FROM_SG;
  738. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  739. host->blocks = data->blocks;
  740. }
  741. sdhci_set_transfer_irqs(host);
  742. /* Set the DMA boundary value and block size */
  743. sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
  744. data->blksz), SDHCI_BLOCK_SIZE);
  745. sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  746. }
  747. static void sdhci_set_transfer_mode(struct sdhci_host *host,
  748. struct mmc_command *cmd)
  749. {
  750. u16 mode = 0;
  751. struct mmc_data *data = cmd->data;
  752. if (data == NULL) {
  753. if (host->quirks2 &
  754. SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
  755. sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
  756. } else {
  757. /* clear Auto CMD settings for no data CMDs */
  758. mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
  759. sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
  760. SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
  761. }
  762. return;
  763. }
  764. WARN_ON(!host->data);
  765. if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
  766. mode = SDHCI_TRNS_BLK_CNT_EN;
  767. if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
  768. mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
  769. /*
  770. * If we are sending CMD23, CMD12 never gets sent
  771. * on successful completion (so no Auto-CMD12).
  772. */
  773. if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
  774. (cmd->opcode != SD_IO_RW_EXTENDED))
  775. mode |= SDHCI_TRNS_AUTO_CMD12;
  776. else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
  777. mode |= SDHCI_TRNS_AUTO_CMD23;
  778. sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
  779. }
  780. }
  781. if (data->flags & MMC_DATA_READ)
  782. mode |= SDHCI_TRNS_READ;
  783. if (host->flags & SDHCI_REQ_USE_DMA)
  784. mode |= SDHCI_TRNS_DMA;
  785. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  786. }
  787. static void sdhci_finish_data(struct sdhci_host *host)
  788. {
  789. struct mmc_data *data;
  790. BUG_ON(!host->data);
  791. data = host->data;
  792. host->data = NULL;
  793. if (host->flags & SDHCI_REQ_USE_DMA) {
  794. if (host->flags & SDHCI_USE_ADMA)
  795. sdhci_adma_table_post(host, data);
  796. else {
  797. if (!data->host_cookie)
  798. dma_unmap_sg(mmc_dev(host->mmc),
  799. data->sg, data->sg_len,
  800. (data->flags & MMC_DATA_READ) ?
  801. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  802. }
  803. }
  804. /*
  805. * The specification states that the block count register must
  806. * be updated, but it does not specify at what point in the
  807. * data flow. That makes the register entirely useless to read
  808. * back so we have to assume that nothing made it to the card
  809. * in the event of an error.
  810. */
  811. if (data->error)
  812. data->bytes_xfered = 0;
  813. else
  814. data->bytes_xfered = data->blksz * data->blocks;
  815. /*
  816. * Need to send CMD12 if -
  817. * a) open-ended multiblock transfer (no CMD23)
  818. * b) error in multiblock transfer
  819. */
  820. if (data->stop &&
  821. (data->error ||
  822. !host->mrq->sbc)) {
  823. /*
  824. * The controller needs a reset of internal state machines
  825. * upon error conditions.
  826. */
  827. if (data->error) {
  828. sdhci_do_reset(host, SDHCI_RESET_CMD);
  829. sdhci_do_reset(host, SDHCI_RESET_DATA);
  830. }
  831. sdhci_send_command(host, data->stop);
  832. } else
  833. tasklet_schedule(&host->finish_tasklet);
  834. }
  835. void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
  836. {
  837. int flags;
  838. u32 mask;
  839. unsigned long timeout;
  840. WARN_ON(host->cmd);
  841. /* Wait max 10 ms */
  842. timeout = 10;
  843. mask = SDHCI_CMD_INHIBIT;
  844. if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  845. mask |= SDHCI_DATA_INHIBIT;
  846. /* We shouldn't wait for data inihibit for stop commands, even
  847. though they might use busy signaling */
  848. if (host->mrq->data && (cmd == host->mrq->data->stop))
  849. mask &= ~SDHCI_DATA_INHIBIT;
  850. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  851. if (timeout == 0) {
  852. pr_err("%s: Controller never released "
  853. "inhibit bit(s).\n", mmc_hostname(host->mmc));
  854. sdhci_dumpregs(host);
  855. cmd->error = -EIO;
  856. tasklet_schedule(&host->finish_tasklet);
  857. return;
  858. }
  859. timeout--;
  860. mdelay(1);
  861. }
  862. timeout = jiffies;
  863. if (!cmd->data && cmd->busy_timeout > 9000)
  864. timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
  865. else
  866. timeout += 10 * HZ;
  867. mod_timer(&host->timer, timeout);
  868. host->cmd = cmd;
  869. host->busy_handle = 0;
  870. sdhci_prepare_data(host, cmd);
  871. sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
  872. sdhci_set_transfer_mode(host, cmd);
  873. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  874. pr_err("%s: Unsupported response type!\n",
  875. mmc_hostname(host->mmc));
  876. cmd->error = -EINVAL;
  877. tasklet_schedule(&host->finish_tasklet);
  878. return;
  879. }
  880. if (!(cmd->flags & MMC_RSP_PRESENT))
  881. flags = SDHCI_CMD_RESP_NONE;
  882. else if (cmd->flags & MMC_RSP_136)
  883. flags = SDHCI_CMD_RESP_LONG;
  884. else if (cmd->flags & MMC_RSP_BUSY)
  885. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  886. else
  887. flags = SDHCI_CMD_RESP_SHORT;
  888. if (cmd->flags & MMC_RSP_CRC)
  889. flags |= SDHCI_CMD_CRC;
  890. if (cmd->flags & MMC_RSP_OPCODE)
  891. flags |= SDHCI_CMD_INDEX;
  892. /* CMD19 is special in that the Data Present Select should be set */
  893. if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
  894. cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
  895. flags |= SDHCI_CMD_DATA;
  896. sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
  897. }
  898. EXPORT_SYMBOL_GPL(sdhci_send_command);
  899. static void sdhci_finish_command(struct sdhci_host *host)
  900. {
  901. int i;
  902. BUG_ON(host->cmd == NULL);
  903. if (host->cmd->flags & MMC_RSP_PRESENT) {
  904. if (host->cmd->flags & MMC_RSP_136) {
  905. /* CRC is stripped so we need to do some shifting. */
  906. for (i = 0;i < 4;i++) {
  907. host->cmd->resp[i] = sdhci_readl(host,
  908. SDHCI_RESPONSE + (3-i)*4) << 8;
  909. if (i != 3)
  910. host->cmd->resp[i] |=
  911. sdhci_readb(host,
  912. SDHCI_RESPONSE + (3-i)*4-1);
  913. }
  914. } else {
  915. host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
  916. }
  917. }
  918. host->cmd->error = 0;
  919. /* Finished CMD23, now send actual command. */
  920. if (host->cmd == host->mrq->sbc) {
  921. host->cmd = NULL;
  922. sdhci_send_command(host, host->mrq->cmd);
  923. } else {
  924. /* Processed actual command. */
  925. if (host->data && host->data_early)
  926. sdhci_finish_data(host);
  927. if (!host->cmd->data)
  928. tasklet_schedule(&host->finish_tasklet);
  929. host->cmd = NULL;
  930. }
  931. }
  932. static u16 sdhci_get_preset_value(struct sdhci_host *host)
  933. {
  934. u16 preset = 0;
  935. switch (host->timing) {
  936. case MMC_TIMING_UHS_SDR12:
  937. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
  938. break;
  939. case MMC_TIMING_UHS_SDR25:
  940. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
  941. break;
  942. case MMC_TIMING_UHS_SDR50:
  943. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
  944. break;
  945. case MMC_TIMING_UHS_SDR104:
  946. case MMC_TIMING_MMC_HS200:
  947. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
  948. break;
  949. case MMC_TIMING_UHS_DDR50:
  950. preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
  951. break;
  952. case MMC_TIMING_MMC_HS400:
  953. preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
  954. break;
  955. default:
  956. pr_warn("%s: Invalid UHS-I mode selected\n",
  957. mmc_hostname(host->mmc));
  958. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
  959. break;
  960. }
  961. return preset;
  962. }
  963. void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
  964. {
  965. int div = 0; /* Initialized for compiler warning */
  966. int real_div = div, clk_mul = 1;
  967. u16 clk = 0;
  968. unsigned long timeout;
  969. host->mmc->actual_clock = 0;
  970. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  971. if (clock == 0)
  972. return;
  973. if (host->version >= SDHCI_SPEC_300) {
  974. if (host->preset_enabled) {
  975. u16 pre_val;
  976. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  977. pre_val = sdhci_get_preset_value(host);
  978. div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
  979. >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
  980. if (host->clk_mul &&
  981. (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
  982. clk = SDHCI_PROG_CLOCK_MODE;
  983. real_div = div + 1;
  984. clk_mul = host->clk_mul;
  985. } else {
  986. real_div = max_t(int, 1, div << 1);
  987. }
  988. goto clock_set;
  989. }
  990. /*
  991. * Check if the Host Controller supports Programmable Clock
  992. * Mode.
  993. */
  994. if (host->clk_mul) {
  995. for (div = 1; div <= 1024; div++) {
  996. if ((host->max_clk * host->clk_mul / div)
  997. <= clock)
  998. break;
  999. }
  1000. /*
  1001. * Set Programmable Clock Mode in the Clock
  1002. * Control register.
  1003. */
  1004. clk = SDHCI_PROG_CLOCK_MODE;
  1005. real_div = div;
  1006. clk_mul = host->clk_mul;
  1007. div--;
  1008. } else {
  1009. /* Version 3.00 divisors must be a multiple of 2. */
  1010. if (host->max_clk <= clock)
  1011. div = 1;
  1012. else {
  1013. for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
  1014. div += 2) {
  1015. if ((host->max_clk / div) <= clock)
  1016. break;
  1017. }
  1018. }
  1019. real_div = div;
  1020. div >>= 1;
  1021. }
  1022. } else {
  1023. /* Version 2.00 divisors must be a power of 2. */
  1024. for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
  1025. if ((host->max_clk / div) <= clock)
  1026. break;
  1027. }
  1028. real_div = div;
  1029. div >>= 1;
  1030. }
  1031. clock_set:
  1032. if (real_div)
  1033. host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
  1034. clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  1035. clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  1036. << SDHCI_DIVIDER_HI_SHIFT;
  1037. clk |= SDHCI_CLOCK_INT_EN;
  1038. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1039. /* Wait max 20 ms */
  1040. timeout = 20;
  1041. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  1042. & SDHCI_CLOCK_INT_STABLE)) {
  1043. if (timeout == 0) {
  1044. pr_err("%s: Internal clock never "
  1045. "stabilised.\n", mmc_hostname(host->mmc));
  1046. sdhci_dumpregs(host);
  1047. return;
  1048. }
  1049. timeout--;
  1050. mdelay(1);
  1051. }
  1052. clk |= SDHCI_CLOCK_CARD_EN;
  1053. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1054. }
  1055. EXPORT_SYMBOL_GPL(sdhci_set_clock);
  1056. static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
  1057. unsigned short vdd)
  1058. {
  1059. struct mmc_host *mmc = host->mmc;
  1060. u8 pwr = 0;
  1061. if (!IS_ERR(mmc->supply.vmmc)) {
  1062. spin_unlock_irq(&host->lock);
  1063. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  1064. spin_lock_irq(&host->lock);
  1065. if (mode != MMC_POWER_OFF)
  1066. sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
  1067. else
  1068. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  1069. return;
  1070. }
  1071. if (mode != MMC_POWER_OFF) {
  1072. switch (1 << vdd) {
  1073. case MMC_VDD_165_195:
  1074. pwr = SDHCI_POWER_180;
  1075. break;
  1076. case MMC_VDD_29_30:
  1077. case MMC_VDD_30_31:
  1078. pwr = SDHCI_POWER_300;
  1079. break;
  1080. case MMC_VDD_32_33:
  1081. case MMC_VDD_33_34:
  1082. pwr = SDHCI_POWER_330;
  1083. break;
  1084. default:
  1085. BUG();
  1086. }
  1087. }
  1088. if (host->pwr == pwr)
  1089. return;
  1090. host->pwr = pwr;
  1091. if (pwr == 0) {
  1092. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  1093. if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  1094. sdhci_runtime_pm_bus_off(host);
  1095. vdd = 0;
  1096. } else {
  1097. /*
  1098. * Spec says that we should clear the power reg before setting
  1099. * a new value. Some controllers don't seem to like this though.
  1100. */
  1101. if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
  1102. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  1103. /*
  1104. * At least the Marvell CaFe chip gets confused if we set the
  1105. * voltage and set turn on power at the same time, so set the
  1106. * voltage first.
  1107. */
  1108. if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
  1109. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1110. pwr |= SDHCI_POWER_ON;
  1111. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1112. if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  1113. sdhci_runtime_pm_bus_on(host);
  1114. /*
  1115. * Some controllers need an extra 10ms delay of 10ms before
  1116. * they can apply clock after applying power
  1117. */
  1118. if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
  1119. mdelay(10);
  1120. }
  1121. }
  1122. /*****************************************************************************\
  1123. * *
  1124. * MMC callbacks *
  1125. * *
  1126. \*****************************************************************************/
  1127. static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1128. {
  1129. struct sdhci_host *host;
  1130. int present;
  1131. unsigned long flags;
  1132. u32 tuning_opcode;
  1133. host = mmc_priv(mmc);
  1134. sdhci_runtime_pm_get(host);
  1135. /* Firstly check card presence */
  1136. present = sdhci_do_get_cd(host);
  1137. spin_lock_irqsave(&host->lock, flags);
  1138. WARN_ON(host->mrq != NULL);
  1139. #ifndef SDHCI_USE_LEDS_CLASS
  1140. sdhci_activate_led(host);
  1141. #endif
  1142. /*
  1143. * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
  1144. * requests if Auto-CMD12 is enabled.
  1145. */
  1146. if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
  1147. if (mrq->stop) {
  1148. mrq->data->stop = NULL;
  1149. mrq->stop = NULL;
  1150. }
  1151. }
  1152. host->mrq = mrq;
  1153. if (!present || host->flags & SDHCI_DEVICE_DEAD) {
  1154. host->mrq->cmd->error = -ENOMEDIUM;
  1155. tasklet_schedule(&host->finish_tasklet);
  1156. } else {
  1157. u32 present_state;
  1158. present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  1159. /*
  1160. * Check if the re-tuning timer has already expired and there
  1161. * is no on-going data transfer and DAT0 is not busy. If so,
  1162. * we need to execute tuning procedure before sending command.
  1163. */
  1164. if ((host->flags & SDHCI_NEEDS_RETUNING) &&
  1165. !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ)) &&
  1166. (present_state & SDHCI_DATA_0_LVL_MASK)) {
  1167. if (mmc->card) {
  1168. /* eMMC uses cmd21 but sd and sdio use cmd19 */
  1169. tuning_opcode =
  1170. mmc->card->type == MMC_TYPE_MMC ?
  1171. MMC_SEND_TUNING_BLOCK_HS200 :
  1172. MMC_SEND_TUNING_BLOCK;
  1173. /* Here we need to set the host->mrq to NULL,
  1174. * in case the pending finish_tasklet
  1175. * finishes it incorrectly.
  1176. */
  1177. host->mrq = NULL;
  1178. spin_unlock_irqrestore(&host->lock, flags);
  1179. sdhci_execute_tuning(mmc, tuning_opcode);
  1180. spin_lock_irqsave(&host->lock, flags);
  1181. /* Restore original mmc_request structure */
  1182. host->mrq = mrq;
  1183. }
  1184. }
  1185. if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
  1186. sdhci_send_command(host, mrq->sbc);
  1187. else
  1188. sdhci_send_command(host, mrq->cmd);
  1189. }
  1190. mmiowb();
  1191. spin_unlock_irqrestore(&host->lock, flags);
  1192. }
  1193. void sdhci_set_bus_width(struct sdhci_host *host, int width)
  1194. {
  1195. u8 ctrl;
  1196. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  1197. if (width == MMC_BUS_WIDTH_8) {
  1198. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1199. if (host->version >= SDHCI_SPEC_300)
  1200. ctrl |= SDHCI_CTRL_8BITBUS;
  1201. } else {
  1202. if (host->version >= SDHCI_SPEC_300)
  1203. ctrl &= ~SDHCI_CTRL_8BITBUS;
  1204. if (width == MMC_BUS_WIDTH_4)
  1205. ctrl |= SDHCI_CTRL_4BITBUS;
  1206. else
  1207. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1208. }
  1209. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1210. }
  1211. EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
  1212. void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
  1213. {
  1214. u16 ctrl_2;
  1215. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1216. /* Select Bus Speed Mode for host */
  1217. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  1218. if ((timing == MMC_TIMING_MMC_HS200) ||
  1219. (timing == MMC_TIMING_UHS_SDR104))
  1220. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  1221. else if (timing == MMC_TIMING_UHS_SDR12)
  1222. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  1223. else if (timing == MMC_TIMING_UHS_SDR25)
  1224. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  1225. else if (timing == MMC_TIMING_UHS_SDR50)
  1226. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  1227. else if ((timing == MMC_TIMING_UHS_DDR50) ||
  1228. (timing == MMC_TIMING_MMC_DDR52))
  1229. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  1230. else if (timing == MMC_TIMING_MMC_HS400)
  1231. ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
  1232. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1233. }
  1234. EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
  1235. static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
  1236. {
  1237. unsigned long flags;
  1238. u8 ctrl;
  1239. struct mmc_host *mmc = host->mmc;
  1240. spin_lock_irqsave(&host->lock, flags);
  1241. if (host->flags & SDHCI_DEVICE_DEAD) {
  1242. spin_unlock_irqrestore(&host->lock, flags);
  1243. if (!IS_ERR(mmc->supply.vmmc) &&
  1244. ios->power_mode == MMC_POWER_OFF)
  1245. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  1246. return;
  1247. }
  1248. /*
  1249. * Reset the chip on each power off.
  1250. * Should clear out any weird states.
  1251. */
  1252. if (ios->power_mode == MMC_POWER_OFF) {
  1253. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  1254. sdhci_reinit(host);
  1255. }
  1256. if (host->version >= SDHCI_SPEC_300 &&
  1257. (ios->power_mode == MMC_POWER_UP) &&
  1258. !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
  1259. sdhci_enable_preset_value(host, false);
  1260. if (!ios->clock || ios->clock != host->clock) {
  1261. host->ops->set_clock(host, ios->clock);
  1262. host->clock = ios->clock;
  1263. if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
  1264. host->clock) {
  1265. host->timeout_clk = host->mmc->actual_clock ?
  1266. host->mmc->actual_clock / 1000 :
  1267. host->clock / 1000;
  1268. host->mmc->max_busy_timeout =
  1269. host->ops->get_max_timeout_count ?
  1270. host->ops->get_max_timeout_count(host) :
  1271. 1 << 27;
  1272. host->mmc->max_busy_timeout /= host->timeout_clk;
  1273. }
  1274. }
  1275. sdhci_set_power(host, ios->power_mode, ios->vdd);
  1276. if (host->ops->platform_send_init_74_clocks)
  1277. host->ops->platform_send_init_74_clocks(host, ios->power_mode);
  1278. host->ops->set_bus_width(host, ios->bus_width);
  1279. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  1280. if ((ios->timing == MMC_TIMING_SD_HS ||
  1281. ios->timing == MMC_TIMING_MMC_HS)
  1282. && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
  1283. ctrl |= SDHCI_CTRL_HISPD;
  1284. else
  1285. ctrl &= ~SDHCI_CTRL_HISPD;
  1286. if (host->version >= SDHCI_SPEC_300) {
  1287. u16 clk, ctrl_2;
  1288. /* In case of UHS-I modes, set High Speed Enable */
  1289. if ((ios->timing == MMC_TIMING_MMC_HS400) ||
  1290. (ios->timing == MMC_TIMING_MMC_HS200) ||
  1291. (ios->timing == MMC_TIMING_MMC_DDR52) ||
  1292. (ios->timing == MMC_TIMING_UHS_SDR50) ||
  1293. (ios->timing == MMC_TIMING_UHS_SDR104) ||
  1294. (ios->timing == MMC_TIMING_UHS_DDR50) ||
  1295. (ios->timing == MMC_TIMING_UHS_SDR25))
  1296. ctrl |= SDHCI_CTRL_HISPD;
  1297. if (!host->preset_enabled) {
  1298. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1299. /*
  1300. * We only need to set Driver Strength if the
  1301. * preset value enable is not set.
  1302. */
  1303. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1304. ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
  1305. if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
  1306. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
  1307. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
  1308. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
  1309. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1310. } else {
  1311. /*
  1312. * According to SDHC Spec v3.00, if the Preset Value
  1313. * Enable in the Host Control 2 register is set, we
  1314. * need to reset SD Clock Enable before changing High
  1315. * Speed Enable to avoid generating clock gliches.
  1316. */
  1317. /* Reset SD Clock Enable */
  1318. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1319. clk &= ~SDHCI_CLOCK_CARD_EN;
  1320. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1321. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1322. /* Re-enable SD Clock */
  1323. host->ops->set_clock(host, host->clock);
  1324. }
  1325. /* Reset SD Clock Enable */
  1326. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1327. clk &= ~SDHCI_CLOCK_CARD_EN;
  1328. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1329. host->ops->set_uhs_signaling(host, ios->timing);
  1330. host->timing = ios->timing;
  1331. if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
  1332. ((ios->timing == MMC_TIMING_UHS_SDR12) ||
  1333. (ios->timing == MMC_TIMING_UHS_SDR25) ||
  1334. (ios->timing == MMC_TIMING_UHS_SDR50) ||
  1335. (ios->timing == MMC_TIMING_UHS_SDR104) ||
  1336. (ios->timing == MMC_TIMING_UHS_DDR50))) {
  1337. u16 preset;
  1338. sdhci_enable_preset_value(host, true);
  1339. preset = sdhci_get_preset_value(host);
  1340. ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
  1341. >> SDHCI_PRESET_DRV_SHIFT;
  1342. }
  1343. /* Re-enable SD Clock */
  1344. host->ops->set_clock(host, host->clock);
  1345. } else
  1346. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1347. /*
  1348. * Some (ENE) controllers go apeshit on some ios operation,
  1349. * signalling timeout and CRC errors even on CMD0. Resetting
  1350. * it on each ios seems to solve the problem.
  1351. */
  1352. if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
  1353. sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  1354. mmiowb();
  1355. spin_unlock_irqrestore(&host->lock, flags);
  1356. }
  1357. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1358. {
  1359. struct sdhci_host *host = mmc_priv(mmc);
  1360. sdhci_runtime_pm_get(host);
  1361. sdhci_do_set_ios(host, ios);
  1362. sdhci_runtime_pm_put(host);
  1363. }
  1364. static int sdhci_do_get_cd(struct sdhci_host *host)
  1365. {
  1366. int gpio_cd = mmc_gpio_get_cd(host->mmc);
  1367. if (host->flags & SDHCI_DEVICE_DEAD)
  1368. return 0;
  1369. /* If polling/nonremovable, assume that the card is always present. */
  1370. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
  1371. (host->mmc->caps & MMC_CAP_NONREMOVABLE))
  1372. return 1;
  1373. /* Try slot gpio detect */
  1374. if (!IS_ERR_VALUE(gpio_cd))
  1375. return !!gpio_cd;
  1376. /* Host native card detect */
  1377. return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
  1378. }
  1379. static int sdhci_get_cd(struct mmc_host *mmc)
  1380. {
  1381. struct sdhci_host *host = mmc_priv(mmc);
  1382. int ret;
  1383. sdhci_runtime_pm_get(host);
  1384. ret = sdhci_do_get_cd(host);
  1385. sdhci_runtime_pm_put(host);
  1386. return ret;
  1387. }
  1388. static int sdhci_check_ro(struct sdhci_host *host)
  1389. {
  1390. unsigned long flags;
  1391. int is_readonly;
  1392. spin_lock_irqsave(&host->lock, flags);
  1393. if (host->flags & SDHCI_DEVICE_DEAD)
  1394. is_readonly = 0;
  1395. else if (host->ops->get_ro)
  1396. is_readonly = host->ops->get_ro(host);
  1397. else
  1398. is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
  1399. & SDHCI_WRITE_PROTECT);
  1400. spin_unlock_irqrestore(&host->lock, flags);
  1401. /* This quirk needs to be replaced by a callback-function later */
  1402. return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
  1403. !is_readonly : is_readonly;
  1404. }
  1405. #define SAMPLE_COUNT 5
  1406. static int sdhci_do_get_ro(struct sdhci_host *host)
  1407. {
  1408. int i, ro_count;
  1409. if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
  1410. return sdhci_check_ro(host);
  1411. ro_count = 0;
  1412. for (i = 0; i < SAMPLE_COUNT; i++) {
  1413. if (sdhci_check_ro(host)) {
  1414. if (++ro_count > SAMPLE_COUNT / 2)
  1415. return 1;
  1416. }
  1417. msleep(30);
  1418. }
  1419. return 0;
  1420. }
  1421. static void sdhci_hw_reset(struct mmc_host *mmc)
  1422. {
  1423. struct sdhci_host *host = mmc_priv(mmc);
  1424. if (host->ops && host->ops->hw_reset)
  1425. host->ops->hw_reset(host);
  1426. }
  1427. static int sdhci_get_ro(struct mmc_host *mmc)
  1428. {
  1429. struct sdhci_host *host = mmc_priv(mmc);
  1430. int ret;
  1431. sdhci_runtime_pm_get(host);
  1432. ret = sdhci_do_get_ro(host);
  1433. sdhci_runtime_pm_put(host);
  1434. return ret;
  1435. }
  1436. static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
  1437. {
  1438. if (!(host->flags & SDHCI_DEVICE_DEAD)) {
  1439. if (enable)
  1440. host->ier |= SDHCI_INT_CARD_INT;
  1441. else
  1442. host->ier &= ~SDHCI_INT_CARD_INT;
  1443. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  1444. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  1445. mmiowb();
  1446. }
  1447. }
  1448. static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1449. {
  1450. struct sdhci_host *host = mmc_priv(mmc);
  1451. unsigned long flags;
  1452. sdhci_runtime_pm_get(host);
  1453. spin_lock_irqsave(&host->lock, flags);
  1454. if (enable)
  1455. host->flags |= SDHCI_SDIO_IRQ_ENABLED;
  1456. else
  1457. host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
  1458. sdhci_enable_sdio_irq_nolock(host, enable);
  1459. spin_unlock_irqrestore(&host->lock, flags);
  1460. sdhci_runtime_pm_put(host);
  1461. }
  1462. static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
  1463. struct mmc_ios *ios)
  1464. {
  1465. struct mmc_host *mmc = host->mmc;
  1466. u16 ctrl;
  1467. int ret;
  1468. /*
  1469. * Signal Voltage Switching is only applicable for Host Controllers
  1470. * v3.00 and above.
  1471. */
  1472. if (host->version < SDHCI_SPEC_300)
  1473. return 0;
  1474. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1475. switch (ios->signal_voltage) {
  1476. case MMC_SIGNAL_VOLTAGE_330:
  1477. /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
  1478. ctrl &= ~SDHCI_CTRL_VDD_180;
  1479. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1480. if (!IS_ERR(mmc->supply.vqmmc)) {
  1481. ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
  1482. 3600000);
  1483. if (ret) {
  1484. pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
  1485. mmc_hostname(mmc));
  1486. return -EIO;
  1487. }
  1488. }
  1489. /* Wait for 5ms */
  1490. usleep_range(5000, 5500);
  1491. /* 3.3V regulator output should be stable within 5 ms */
  1492. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1493. if (!(ctrl & SDHCI_CTRL_VDD_180))
  1494. return 0;
  1495. pr_warn("%s: 3.3V regulator output did not became stable\n",
  1496. mmc_hostname(mmc));
  1497. return -EAGAIN;
  1498. case MMC_SIGNAL_VOLTAGE_180:
  1499. if (!IS_ERR(mmc->supply.vqmmc)) {
  1500. ret = regulator_set_voltage(mmc->supply.vqmmc,
  1501. 1700000, 1950000);
  1502. if (ret) {
  1503. pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
  1504. mmc_hostname(mmc));
  1505. return -EIO;
  1506. }
  1507. }
  1508. /*
  1509. * Enable 1.8V Signal Enable in the Host Control2
  1510. * register
  1511. */
  1512. ctrl |= SDHCI_CTRL_VDD_180;
  1513. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1514. /* Some controller need to do more when switching */
  1515. if (host->ops->voltage_switch)
  1516. host->ops->voltage_switch(host);
  1517. /* 1.8V regulator output should be stable within 5 ms */
  1518. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1519. if (ctrl & SDHCI_CTRL_VDD_180)
  1520. return 0;
  1521. pr_warn("%s: 1.8V regulator output did not became stable\n",
  1522. mmc_hostname(mmc));
  1523. return -EAGAIN;
  1524. case MMC_SIGNAL_VOLTAGE_120:
  1525. if (!IS_ERR(mmc->supply.vqmmc)) {
  1526. ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
  1527. 1300000);
  1528. if (ret) {
  1529. pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
  1530. mmc_hostname(mmc));
  1531. return -EIO;
  1532. }
  1533. }
  1534. return 0;
  1535. default:
  1536. /* No signal voltage switch required */
  1537. return 0;
  1538. }
  1539. }
  1540. static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
  1541. struct mmc_ios *ios)
  1542. {
  1543. struct sdhci_host *host = mmc_priv(mmc);
  1544. int err;
  1545. if (host->version < SDHCI_SPEC_300)
  1546. return 0;
  1547. sdhci_runtime_pm_get(host);
  1548. err = sdhci_do_start_signal_voltage_switch(host, ios);
  1549. sdhci_runtime_pm_put(host);
  1550. return err;
  1551. }
  1552. static int sdhci_card_busy(struct mmc_host *mmc)
  1553. {
  1554. struct sdhci_host *host = mmc_priv(mmc);
  1555. u32 present_state;
  1556. sdhci_runtime_pm_get(host);
  1557. /* Check whether DAT[3:0] is 0000 */
  1558. present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  1559. sdhci_runtime_pm_put(host);
  1560. return !(present_state & SDHCI_DATA_LVL_MASK);
  1561. }
  1562. static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
  1563. {
  1564. struct sdhci_host *host = mmc_priv(mmc);
  1565. unsigned long flags;
  1566. spin_lock_irqsave(&host->lock, flags);
  1567. host->flags |= SDHCI_HS400_TUNING;
  1568. spin_unlock_irqrestore(&host->lock, flags);
  1569. return 0;
  1570. }
  1571. static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1572. {
  1573. struct sdhci_host *host = mmc_priv(mmc);
  1574. u16 ctrl;
  1575. int tuning_loop_counter = MAX_TUNING_LOOP;
  1576. int err = 0;
  1577. unsigned long flags;
  1578. unsigned int tuning_count = 0;
  1579. bool hs400_tuning;
  1580. sdhci_runtime_pm_get(host);
  1581. spin_lock_irqsave(&host->lock, flags);
  1582. hs400_tuning = host->flags & SDHCI_HS400_TUNING;
  1583. host->flags &= ~SDHCI_HS400_TUNING;
  1584. if (host->tuning_mode == SDHCI_TUNING_MODE_1)
  1585. tuning_count = host->tuning_count;
  1586. /*
  1587. * The Host Controller needs tuning only in case of SDR104 mode
  1588. * and for SDR50 mode when Use Tuning for SDR50 is set in the
  1589. * Capabilities register.
  1590. * If the Host Controller supports the HS200 mode then the
  1591. * tuning function has to be executed.
  1592. */
  1593. switch (host->timing) {
  1594. /* HS400 tuning is done in HS200 mode */
  1595. case MMC_TIMING_MMC_HS400:
  1596. err = -EINVAL;
  1597. goto out_unlock;
  1598. case MMC_TIMING_MMC_HS200:
  1599. /*
  1600. * Periodic re-tuning for HS400 is not expected to be needed, so
  1601. * disable it here.
  1602. */
  1603. if (hs400_tuning)
  1604. tuning_count = 0;
  1605. break;
  1606. case MMC_TIMING_UHS_SDR104:
  1607. break;
  1608. case MMC_TIMING_UHS_SDR50:
  1609. if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
  1610. host->flags & SDHCI_SDR104_NEEDS_TUNING)
  1611. break;
  1612. /* FALLTHROUGH */
  1613. default:
  1614. goto out_unlock;
  1615. }
  1616. if (host->ops->platform_execute_tuning) {
  1617. spin_unlock_irqrestore(&host->lock, flags);
  1618. err = host->ops->platform_execute_tuning(host, opcode);
  1619. sdhci_runtime_pm_put(host);
  1620. return err;
  1621. }
  1622. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1623. ctrl |= SDHCI_CTRL_EXEC_TUNING;
  1624. if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
  1625. ctrl |= SDHCI_CTRL_TUNED_CLK;
  1626. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1627. /*
  1628. * As per the Host Controller spec v3.00, tuning command
  1629. * generates Buffer Read Ready interrupt, so enable that.
  1630. *
  1631. * Note: The spec clearly says that when tuning sequence
  1632. * is being performed, the controller does not generate
  1633. * interrupts other than Buffer Read Ready interrupt. But
  1634. * to make sure we don't hit a controller bug, we _only_
  1635. * enable Buffer Read Ready interrupt here.
  1636. */
  1637. sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
  1638. sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
  1639. /*
  1640. * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
  1641. * of loops reaches 40 times or a timeout of 150ms occurs.
  1642. */
  1643. do {
  1644. struct mmc_command cmd = {0};
  1645. struct mmc_request mrq = {NULL};
  1646. cmd.opcode = opcode;
  1647. cmd.arg = 0;
  1648. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  1649. cmd.retries = 0;
  1650. cmd.data = NULL;
  1651. cmd.error = 0;
  1652. if (tuning_loop_counter-- == 0)
  1653. break;
  1654. mrq.cmd = &cmd;
  1655. host->mrq = &mrq;
  1656. /*
  1657. * In response to CMD19, the card sends 64 bytes of tuning
  1658. * block to the Host Controller. So we set the block size
  1659. * to 64 here.
  1660. */
  1661. if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
  1662. if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  1663. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
  1664. SDHCI_BLOCK_SIZE);
  1665. else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
  1666. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
  1667. SDHCI_BLOCK_SIZE);
  1668. } else {
  1669. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
  1670. SDHCI_BLOCK_SIZE);
  1671. }
  1672. /*
  1673. * The tuning block is sent by the card to the host controller.
  1674. * So we set the TRNS_READ bit in the Transfer Mode register.
  1675. * This also takes care of setting DMA Enable and Multi Block
  1676. * Select in the same register to 0.
  1677. */
  1678. sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
  1679. sdhci_send_command(host, &cmd);
  1680. host->cmd = NULL;
  1681. host->mrq = NULL;
  1682. spin_unlock_irqrestore(&host->lock, flags);
  1683. /* Wait for Buffer Read Ready interrupt */
  1684. wait_event_interruptible_timeout(host->buf_ready_int,
  1685. (host->tuning_done == 1),
  1686. msecs_to_jiffies(50));
  1687. spin_lock_irqsave(&host->lock, flags);
  1688. if (!host->tuning_done) {
  1689. pr_info(DRIVER_NAME ": Timeout waiting for "
  1690. "Buffer Read Ready interrupt during tuning "
  1691. "procedure, falling back to fixed sampling "
  1692. "clock\n");
  1693. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1694. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  1695. ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
  1696. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1697. err = -EIO;
  1698. goto out;
  1699. }
  1700. host->tuning_done = 0;
  1701. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1702. /* eMMC spec does not require a delay between tuning cycles */
  1703. if (opcode == MMC_SEND_TUNING_BLOCK)
  1704. mdelay(1);
  1705. } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
  1706. /*
  1707. * The Host Driver has exhausted the maximum number of loops allowed,
  1708. * so use fixed sampling frequency.
  1709. */
  1710. if (tuning_loop_counter < 0) {
  1711. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  1712. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1713. }
  1714. if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
  1715. pr_info(DRIVER_NAME ": Tuning procedure"
  1716. " failed, falling back to fixed sampling"
  1717. " clock\n");
  1718. err = -EIO;
  1719. }
  1720. out:
  1721. host->flags &= ~SDHCI_NEEDS_RETUNING;
  1722. if (tuning_count) {
  1723. host->flags |= SDHCI_USING_RETUNING_TIMER;
  1724. mod_timer(&host->tuning_timer, jiffies + tuning_count * HZ);
  1725. }
  1726. /*
  1727. * In case tuning fails, host controllers which support re-tuning can
  1728. * try tuning again at a later time, when the re-tuning timer expires.
  1729. * So for these controllers, we return 0. Since there might be other
  1730. * controllers who do not have this capability, we return error for
  1731. * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
  1732. * a retuning timer to do the retuning for the card.
  1733. */
  1734. if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
  1735. err = 0;
  1736. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  1737. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  1738. out_unlock:
  1739. spin_unlock_irqrestore(&host->lock, flags);
  1740. sdhci_runtime_pm_put(host);
  1741. return err;
  1742. }
  1743. static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
  1744. {
  1745. /* Host Controller v3.00 defines preset value registers */
  1746. if (host->version < SDHCI_SPEC_300)
  1747. return;
  1748. /*
  1749. * We only enable or disable Preset Value if they are not already
  1750. * enabled or disabled respectively. Otherwise, we bail out.
  1751. */
  1752. if (host->preset_enabled != enable) {
  1753. u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1754. if (enable)
  1755. ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
  1756. else
  1757. ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  1758. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1759. if (enable)
  1760. host->flags |= SDHCI_PV_ENABLED;
  1761. else
  1762. host->flags &= ~SDHCI_PV_ENABLED;
  1763. host->preset_enabled = enable;
  1764. }
  1765. }
  1766. static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1767. int err)
  1768. {
  1769. struct sdhci_host *host = mmc_priv(mmc);
  1770. struct mmc_data *data = mrq->data;
  1771. if (host->flags & SDHCI_REQ_USE_DMA) {
  1772. if (data->host_cookie)
  1773. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  1774. data->flags & MMC_DATA_WRITE ?
  1775. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1776. mrq->data->host_cookie = 0;
  1777. }
  1778. }
  1779. static int sdhci_pre_dma_transfer(struct sdhci_host *host,
  1780. struct mmc_data *data,
  1781. struct sdhci_host_next *next)
  1782. {
  1783. int sg_count;
  1784. if (!next && data->host_cookie &&
  1785. data->host_cookie != host->next_data.cookie) {
  1786. pr_debug(DRIVER_NAME "[%s] invalid cookie: %d, next-cookie %d\n",
  1787. __func__, data->host_cookie, host->next_data.cookie);
  1788. data->host_cookie = 0;
  1789. }
  1790. /* Check if next job is already prepared */
  1791. if (next ||
  1792. (!next && data->host_cookie != host->next_data.cookie)) {
  1793. sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg,
  1794. data->sg_len,
  1795. data->flags & MMC_DATA_WRITE ?
  1796. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1797. } else {
  1798. sg_count = host->next_data.sg_count;
  1799. host->next_data.sg_count = 0;
  1800. }
  1801. if (sg_count == 0)
  1802. return -EINVAL;
  1803. if (next) {
  1804. next->sg_count = sg_count;
  1805. data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
  1806. } else
  1807. host->sg_count = sg_count;
  1808. return sg_count;
  1809. }
  1810. static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1811. bool is_first_req)
  1812. {
  1813. struct sdhci_host *host = mmc_priv(mmc);
  1814. if (mrq->data->host_cookie) {
  1815. mrq->data->host_cookie = 0;
  1816. return;
  1817. }
  1818. if (host->flags & SDHCI_REQ_USE_DMA)
  1819. if (sdhci_pre_dma_transfer(host,
  1820. mrq->data,
  1821. &host->next_data) < 0)
  1822. mrq->data->host_cookie = 0;
  1823. }
  1824. static void sdhci_card_event(struct mmc_host *mmc)
  1825. {
  1826. struct sdhci_host *host = mmc_priv(mmc);
  1827. unsigned long flags;
  1828. int present;
  1829. /* First check if client has provided their own card event */
  1830. if (host->ops->card_event)
  1831. host->ops->card_event(host);
  1832. present = sdhci_do_get_cd(host);
  1833. spin_lock_irqsave(&host->lock, flags);
  1834. /* Check host->mrq first in case we are runtime suspended */
  1835. if (host->mrq && !present) {
  1836. pr_err("%s: Card removed during transfer!\n",
  1837. mmc_hostname(host->mmc));
  1838. pr_err("%s: Resetting controller.\n",
  1839. mmc_hostname(host->mmc));
  1840. sdhci_do_reset(host, SDHCI_RESET_CMD);
  1841. sdhci_do_reset(host, SDHCI_RESET_DATA);
  1842. host->mrq->cmd->error = -ENOMEDIUM;
  1843. tasklet_schedule(&host->finish_tasklet);
  1844. }
  1845. spin_unlock_irqrestore(&host->lock, flags);
  1846. }
  1847. static const struct mmc_host_ops sdhci_ops = {
  1848. .request = sdhci_request,
  1849. .post_req = sdhci_post_req,
  1850. .pre_req = sdhci_pre_req,
  1851. .set_ios = sdhci_set_ios,
  1852. .get_cd = sdhci_get_cd,
  1853. .get_ro = sdhci_get_ro,
  1854. .hw_reset = sdhci_hw_reset,
  1855. .enable_sdio_irq = sdhci_enable_sdio_irq,
  1856. .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
  1857. .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
  1858. .execute_tuning = sdhci_execute_tuning,
  1859. .card_event = sdhci_card_event,
  1860. .card_busy = sdhci_card_busy,
  1861. };
  1862. /*****************************************************************************\
  1863. * *
  1864. * Tasklets *
  1865. * *
  1866. \*****************************************************************************/
  1867. static void sdhci_tasklet_finish(unsigned long param)
  1868. {
  1869. struct sdhci_host *host;
  1870. unsigned long flags;
  1871. struct mmc_request *mrq;
  1872. host = (struct sdhci_host*)param;
  1873. spin_lock_irqsave(&host->lock, flags);
  1874. /*
  1875. * If this tasklet gets rescheduled while running, it will
  1876. * be run again afterwards but without any active request.
  1877. */
  1878. if (!host->mrq) {
  1879. spin_unlock_irqrestore(&host->lock, flags);
  1880. return;
  1881. }
  1882. del_timer(&host->timer);
  1883. mrq = host->mrq;
  1884. /*
  1885. * The controller needs a reset of internal state machines
  1886. * upon error conditions.
  1887. */
  1888. if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  1889. ((mrq->cmd && mrq->cmd->error) ||
  1890. (mrq->sbc && mrq->sbc->error) ||
  1891. (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
  1892. (mrq->data->stop && mrq->data->stop->error))) ||
  1893. (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
  1894. /* Some controllers need this kick or reset won't work here */
  1895. if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
  1896. /* This is to force an update */
  1897. host->ops->set_clock(host, host->clock);
  1898. /* Spec says we should do both at the same time, but Ricoh
  1899. controllers do not like that. */
  1900. sdhci_do_reset(host, SDHCI_RESET_CMD);
  1901. sdhci_do_reset(host, SDHCI_RESET_DATA);
  1902. }
  1903. host->mrq = NULL;
  1904. host->cmd = NULL;
  1905. host->data = NULL;
  1906. #ifndef SDHCI_USE_LEDS_CLASS
  1907. sdhci_deactivate_led(host);
  1908. #endif
  1909. mmiowb();
  1910. spin_unlock_irqrestore(&host->lock, flags);
  1911. mmc_request_done(host->mmc, mrq);
  1912. sdhci_runtime_pm_put(host);
  1913. }
  1914. static void sdhci_timeout_timer(unsigned long data)
  1915. {
  1916. struct sdhci_host *host;
  1917. unsigned long flags;
  1918. host = (struct sdhci_host*)data;
  1919. spin_lock_irqsave(&host->lock, flags);
  1920. if (host->mrq) {
  1921. pr_err("%s: Timeout waiting for hardware "
  1922. "interrupt.\n", mmc_hostname(host->mmc));
  1923. sdhci_dumpregs(host);
  1924. if (host->data) {
  1925. host->data->error = -ETIMEDOUT;
  1926. sdhci_finish_data(host);
  1927. } else {
  1928. if (host->cmd)
  1929. host->cmd->error = -ETIMEDOUT;
  1930. else
  1931. host->mrq->cmd->error = -ETIMEDOUT;
  1932. tasklet_schedule(&host->finish_tasklet);
  1933. }
  1934. }
  1935. mmiowb();
  1936. spin_unlock_irqrestore(&host->lock, flags);
  1937. }
  1938. static void sdhci_tuning_timer(unsigned long data)
  1939. {
  1940. struct sdhci_host *host;
  1941. unsigned long flags;
  1942. host = (struct sdhci_host *)data;
  1943. spin_lock_irqsave(&host->lock, flags);
  1944. host->flags |= SDHCI_NEEDS_RETUNING;
  1945. spin_unlock_irqrestore(&host->lock, flags);
  1946. }
  1947. /*****************************************************************************\
  1948. * *
  1949. * Interrupt handling *
  1950. * *
  1951. \*****************************************************************************/
  1952. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
  1953. {
  1954. BUG_ON(intmask == 0);
  1955. if (!host->cmd) {
  1956. pr_err("%s: Got command interrupt 0x%08x even "
  1957. "though no command operation was in progress.\n",
  1958. mmc_hostname(host->mmc), (unsigned)intmask);
  1959. sdhci_dumpregs(host);
  1960. return;
  1961. }
  1962. if (intmask & SDHCI_INT_TIMEOUT)
  1963. host->cmd->error = -ETIMEDOUT;
  1964. else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  1965. SDHCI_INT_INDEX))
  1966. host->cmd->error = -EILSEQ;
  1967. if (host->cmd->error) {
  1968. tasklet_schedule(&host->finish_tasklet);
  1969. return;
  1970. }
  1971. /*
  1972. * The host can send and interrupt when the busy state has
  1973. * ended, allowing us to wait without wasting CPU cycles.
  1974. * Unfortunately this is overloaded on the "data complete"
  1975. * interrupt, so we need to take some care when handling
  1976. * it.
  1977. *
  1978. * Note: The 1.0 specification is a bit ambiguous about this
  1979. * feature so there might be some problems with older
  1980. * controllers.
  1981. */
  1982. if (host->cmd->flags & MMC_RSP_BUSY) {
  1983. if (host->cmd->data)
  1984. DBG("Cannot wait for busy signal when also "
  1985. "doing a data transfer");
  1986. else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
  1987. && !host->busy_handle) {
  1988. /* Mark that command complete before busy is ended */
  1989. host->busy_handle = 1;
  1990. return;
  1991. }
  1992. /* The controller does not support the end-of-busy IRQ,
  1993. * fall through and take the SDHCI_INT_RESPONSE */
  1994. } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
  1995. host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
  1996. *mask &= ~SDHCI_INT_DATA_END;
  1997. }
  1998. if (intmask & SDHCI_INT_RESPONSE)
  1999. sdhci_finish_command(host);
  2000. }
  2001. #ifdef CONFIG_MMC_DEBUG
  2002. static void sdhci_adma_show_error(struct sdhci_host *host)
  2003. {
  2004. const char *name = mmc_hostname(host->mmc);
  2005. void *desc = host->adma_table;
  2006. sdhci_dumpregs(host);
  2007. while (true) {
  2008. struct sdhci_adma2_64_desc *dma_desc = desc;
  2009. if (host->flags & SDHCI_USE_64_BIT_DMA)
  2010. DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
  2011. name, desc, le32_to_cpu(dma_desc->addr_hi),
  2012. le32_to_cpu(dma_desc->addr_lo),
  2013. le16_to_cpu(dma_desc->len),
  2014. le16_to_cpu(dma_desc->cmd));
  2015. else
  2016. DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
  2017. name, desc, le32_to_cpu(dma_desc->addr_lo),
  2018. le16_to_cpu(dma_desc->len),
  2019. le16_to_cpu(dma_desc->cmd));
  2020. desc += host->desc_sz;
  2021. if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
  2022. break;
  2023. }
  2024. }
  2025. #else
  2026. static void sdhci_adma_show_error(struct sdhci_host *host) { }
  2027. #endif
  2028. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  2029. {
  2030. u32 command;
  2031. BUG_ON(intmask == 0);
  2032. /* CMD19 generates _only_ Buffer Read Ready interrupt */
  2033. if (intmask & SDHCI_INT_DATA_AVAIL) {
  2034. command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
  2035. if (command == MMC_SEND_TUNING_BLOCK ||
  2036. command == MMC_SEND_TUNING_BLOCK_HS200) {
  2037. host->tuning_done = 1;
  2038. wake_up(&host->buf_ready_int);
  2039. return;
  2040. }
  2041. }
  2042. if (!host->data) {
  2043. /*
  2044. * The "data complete" interrupt is also used to
  2045. * indicate that a busy state has ended. See comment
  2046. * above in sdhci_cmd_irq().
  2047. */
  2048. if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  2049. if (intmask & SDHCI_INT_DATA_TIMEOUT) {
  2050. host->cmd->error = -ETIMEDOUT;
  2051. tasklet_schedule(&host->finish_tasklet);
  2052. return;
  2053. }
  2054. if (intmask & SDHCI_INT_DATA_END) {
  2055. /*
  2056. * Some cards handle busy-end interrupt
  2057. * before the command completed, so make
  2058. * sure we do things in the proper order.
  2059. */
  2060. if (host->busy_handle)
  2061. sdhci_finish_command(host);
  2062. else
  2063. host->busy_handle = 1;
  2064. return;
  2065. }
  2066. }
  2067. pr_err("%s: Got data interrupt 0x%08x even "
  2068. "though no data operation was in progress.\n",
  2069. mmc_hostname(host->mmc), (unsigned)intmask);
  2070. sdhci_dumpregs(host);
  2071. return;
  2072. }
  2073. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  2074. host->data->error = -ETIMEDOUT;
  2075. else if (intmask & SDHCI_INT_DATA_END_BIT)
  2076. host->data->error = -EILSEQ;
  2077. else if ((intmask & SDHCI_INT_DATA_CRC) &&
  2078. SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
  2079. != MMC_BUS_TEST_R)
  2080. host->data->error = -EILSEQ;
  2081. else if (intmask & SDHCI_INT_ADMA_ERROR) {
  2082. pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
  2083. sdhci_adma_show_error(host);
  2084. host->data->error = -EIO;
  2085. if (host->ops->adma_workaround)
  2086. host->ops->adma_workaround(host, intmask);
  2087. }
  2088. if (host->data->error)
  2089. sdhci_finish_data(host);
  2090. else {
  2091. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  2092. sdhci_transfer_pio(host);
  2093. /*
  2094. * We currently don't do anything fancy with DMA
  2095. * boundaries, but as we can't disable the feature
  2096. * we need to at least restart the transfer.
  2097. *
  2098. * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
  2099. * should return a valid address to continue from, but as
  2100. * some controllers are faulty, don't trust them.
  2101. */
  2102. if (intmask & SDHCI_INT_DMA_END) {
  2103. u32 dmastart, dmanow;
  2104. dmastart = sg_dma_address(host->data->sg);
  2105. dmanow = dmastart + host->data->bytes_xfered;
  2106. /*
  2107. * Force update to the next DMA block boundary.
  2108. */
  2109. dmanow = (dmanow &
  2110. ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
  2111. SDHCI_DEFAULT_BOUNDARY_SIZE;
  2112. host->data->bytes_xfered = dmanow - dmastart;
  2113. DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
  2114. " next 0x%08x\n",
  2115. mmc_hostname(host->mmc), dmastart,
  2116. host->data->bytes_xfered, dmanow);
  2117. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  2118. }
  2119. if (intmask & SDHCI_INT_DATA_END) {
  2120. if (host->cmd) {
  2121. /*
  2122. * Data managed to finish before the
  2123. * command completed. Make sure we do
  2124. * things in the proper order.
  2125. */
  2126. host->data_early = 1;
  2127. } else {
  2128. sdhci_finish_data(host);
  2129. }
  2130. }
  2131. }
  2132. }
  2133. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  2134. {
  2135. irqreturn_t result = IRQ_NONE;
  2136. struct sdhci_host *host = dev_id;
  2137. u32 intmask, mask, unexpected = 0;
  2138. int max_loops = 16;
  2139. spin_lock(&host->lock);
  2140. if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
  2141. spin_unlock(&host->lock);
  2142. return IRQ_NONE;
  2143. }
  2144. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  2145. if (!intmask || intmask == 0xffffffff) {
  2146. result = IRQ_NONE;
  2147. goto out;
  2148. }
  2149. do {
  2150. /* Clear selected interrupts. */
  2151. mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  2152. SDHCI_INT_BUS_POWER);
  2153. sdhci_writel(host, mask, SDHCI_INT_STATUS);
  2154. DBG("*** %s got interrupt: 0x%08x\n",
  2155. mmc_hostname(host->mmc), intmask);
  2156. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  2157. u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  2158. SDHCI_CARD_PRESENT;
  2159. /*
  2160. * There is a observation on i.mx esdhc. INSERT
  2161. * bit will be immediately set again when it gets
  2162. * cleared, if a card is inserted. We have to mask
  2163. * the irq to prevent interrupt storm which will
  2164. * freeze the system. And the REMOVE gets the
  2165. * same situation.
  2166. *
  2167. * More testing are needed here to ensure it works
  2168. * for other platforms though.
  2169. */
  2170. host->ier &= ~(SDHCI_INT_CARD_INSERT |
  2171. SDHCI_INT_CARD_REMOVE);
  2172. host->ier |= present ? SDHCI_INT_CARD_REMOVE :
  2173. SDHCI_INT_CARD_INSERT;
  2174. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  2175. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  2176. sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
  2177. SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
  2178. host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
  2179. SDHCI_INT_CARD_REMOVE);
  2180. result = IRQ_WAKE_THREAD;
  2181. }
  2182. if (intmask & SDHCI_INT_CMD_MASK)
  2183. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
  2184. &intmask);
  2185. if (intmask & SDHCI_INT_DATA_MASK)
  2186. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  2187. if (intmask & SDHCI_INT_BUS_POWER)
  2188. pr_err("%s: Card is consuming too much power!\n",
  2189. mmc_hostname(host->mmc));
  2190. if (intmask & SDHCI_INT_CARD_INT) {
  2191. sdhci_enable_sdio_irq_nolock(host, false);
  2192. host->thread_isr |= SDHCI_INT_CARD_INT;
  2193. result = IRQ_WAKE_THREAD;
  2194. }
  2195. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
  2196. SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  2197. SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
  2198. SDHCI_INT_CARD_INT);
  2199. if (intmask) {
  2200. unexpected |= intmask;
  2201. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  2202. }
  2203. if (result == IRQ_NONE)
  2204. result = IRQ_HANDLED;
  2205. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  2206. } while (intmask && --max_loops);
  2207. out:
  2208. spin_unlock(&host->lock);
  2209. if (unexpected) {
  2210. pr_err("%s: Unexpected interrupt 0x%08x.\n",
  2211. mmc_hostname(host->mmc), unexpected);
  2212. sdhci_dumpregs(host);
  2213. }
  2214. return result;
  2215. }
  2216. static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
  2217. {
  2218. struct sdhci_host *host = dev_id;
  2219. unsigned long flags;
  2220. u32 isr;
  2221. spin_lock_irqsave(&host->lock, flags);
  2222. isr = host->thread_isr;
  2223. host->thread_isr = 0;
  2224. spin_unlock_irqrestore(&host->lock, flags);
  2225. if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  2226. sdhci_card_event(host->mmc);
  2227. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  2228. }
  2229. if (isr & SDHCI_INT_CARD_INT) {
  2230. sdio_run_irqs(host->mmc);
  2231. spin_lock_irqsave(&host->lock, flags);
  2232. if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
  2233. sdhci_enable_sdio_irq_nolock(host, true);
  2234. spin_unlock_irqrestore(&host->lock, flags);
  2235. }
  2236. return isr ? IRQ_HANDLED : IRQ_NONE;
  2237. }
  2238. /*****************************************************************************\
  2239. * *
  2240. * Suspend/resume *
  2241. * *
  2242. \*****************************************************************************/
  2243. #ifdef CONFIG_PM
  2244. void sdhci_enable_irq_wakeups(struct sdhci_host *host)
  2245. {
  2246. u8 val;
  2247. u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
  2248. | SDHCI_WAKE_ON_INT;
  2249. val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
  2250. val |= mask ;
  2251. /* Avoid fake wake up */
  2252. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  2253. val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
  2254. sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
  2255. }
  2256. EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
  2257. static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
  2258. {
  2259. u8 val;
  2260. u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
  2261. | SDHCI_WAKE_ON_INT;
  2262. val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
  2263. val &= ~mask;
  2264. sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
  2265. }
  2266. int sdhci_suspend_host(struct sdhci_host *host)
  2267. {
  2268. sdhci_disable_card_detection(host);
  2269. /* Disable tuning since we are suspending */
  2270. if (host->flags & SDHCI_USING_RETUNING_TIMER) {
  2271. del_timer_sync(&host->tuning_timer);
  2272. host->flags &= ~SDHCI_NEEDS_RETUNING;
  2273. }
  2274. if (!device_may_wakeup(mmc_dev(host->mmc))) {
  2275. host->ier = 0;
  2276. sdhci_writel(host, 0, SDHCI_INT_ENABLE);
  2277. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  2278. free_irq(host->irq, host);
  2279. } else {
  2280. sdhci_enable_irq_wakeups(host);
  2281. enable_irq_wake(host->irq);
  2282. }
  2283. return 0;
  2284. }
  2285. EXPORT_SYMBOL_GPL(sdhci_suspend_host);
  2286. int sdhci_resume_host(struct sdhci_host *host)
  2287. {
  2288. int ret = 0;
  2289. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2290. if (host->ops->enable_dma)
  2291. host->ops->enable_dma(host);
  2292. }
  2293. if (!device_may_wakeup(mmc_dev(host->mmc))) {
  2294. ret = request_threaded_irq(host->irq, sdhci_irq,
  2295. sdhci_thread_irq, IRQF_SHARED,
  2296. mmc_hostname(host->mmc), host);
  2297. if (ret)
  2298. return ret;
  2299. } else {
  2300. sdhci_disable_irq_wakeups(host);
  2301. disable_irq_wake(host->irq);
  2302. }
  2303. if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
  2304. (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
  2305. /* Card keeps power but host controller does not */
  2306. sdhci_init(host, 0);
  2307. host->pwr = 0;
  2308. host->clock = 0;
  2309. sdhci_do_set_ios(host, &host->mmc->ios);
  2310. } else {
  2311. sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
  2312. mmiowb();
  2313. }
  2314. sdhci_enable_card_detection(host);
  2315. /* Set the re-tuning expiration flag */
  2316. if (host->flags & SDHCI_USING_RETUNING_TIMER)
  2317. host->flags |= SDHCI_NEEDS_RETUNING;
  2318. return ret;
  2319. }
  2320. EXPORT_SYMBOL_GPL(sdhci_resume_host);
  2321. static int sdhci_runtime_pm_get(struct sdhci_host *host)
  2322. {
  2323. return pm_runtime_get_sync(host->mmc->parent);
  2324. }
  2325. static int sdhci_runtime_pm_put(struct sdhci_host *host)
  2326. {
  2327. pm_runtime_mark_last_busy(host->mmc->parent);
  2328. return pm_runtime_put_autosuspend(host->mmc->parent);
  2329. }
  2330. static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
  2331. {
  2332. if (host->runtime_suspended || host->bus_on)
  2333. return;
  2334. host->bus_on = true;
  2335. pm_runtime_get_noresume(host->mmc->parent);
  2336. }
  2337. static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
  2338. {
  2339. if (host->runtime_suspended || !host->bus_on)
  2340. return;
  2341. host->bus_on = false;
  2342. pm_runtime_put_noidle(host->mmc->parent);
  2343. }
  2344. int sdhci_runtime_suspend_host(struct sdhci_host *host)
  2345. {
  2346. unsigned long flags;
  2347. /* Disable tuning since we are suspending */
  2348. if (host->flags & SDHCI_USING_RETUNING_TIMER) {
  2349. del_timer_sync(&host->tuning_timer);
  2350. host->flags &= ~SDHCI_NEEDS_RETUNING;
  2351. }
  2352. spin_lock_irqsave(&host->lock, flags);
  2353. host->ier &= SDHCI_INT_CARD_INT;
  2354. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  2355. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  2356. spin_unlock_irqrestore(&host->lock, flags);
  2357. synchronize_hardirq(host->irq);
  2358. spin_lock_irqsave(&host->lock, flags);
  2359. host->runtime_suspended = true;
  2360. spin_unlock_irqrestore(&host->lock, flags);
  2361. return 0;
  2362. }
  2363. EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
  2364. int sdhci_runtime_resume_host(struct sdhci_host *host)
  2365. {
  2366. unsigned long flags;
  2367. int host_flags = host->flags;
  2368. if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2369. if (host->ops->enable_dma)
  2370. host->ops->enable_dma(host);
  2371. }
  2372. sdhci_init(host, 0);
  2373. /* Force clock and power re-program */
  2374. host->pwr = 0;
  2375. host->clock = 0;
  2376. sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
  2377. sdhci_do_set_ios(host, &host->mmc->ios);
  2378. if ((host_flags & SDHCI_PV_ENABLED) &&
  2379. !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
  2380. spin_lock_irqsave(&host->lock, flags);
  2381. sdhci_enable_preset_value(host, true);
  2382. spin_unlock_irqrestore(&host->lock, flags);
  2383. }
  2384. /* Set the re-tuning expiration flag */
  2385. if (host->flags & SDHCI_USING_RETUNING_TIMER)
  2386. host->flags |= SDHCI_NEEDS_RETUNING;
  2387. spin_lock_irqsave(&host->lock, flags);
  2388. host->runtime_suspended = false;
  2389. /* Enable SDIO IRQ */
  2390. if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
  2391. sdhci_enable_sdio_irq_nolock(host, true);
  2392. /* Enable Card Detection */
  2393. sdhci_enable_card_detection(host);
  2394. spin_unlock_irqrestore(&host->lock, flags);
  2395. return 0;
  2396. }
  2397. EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
  2398. #endif /* CONFIG_PM */
  2399. /*****************************************************************************\
  2400. * *
  2401. * Device allocation/registration *
  2402. * *
  2403. \*****************************************************************************/
  2404. struct sdhci_host *sdhci_alloc_host(struct device *dev,
  2405. size_t priv_size)
  2406. {
  2407. struct mmc_host *mmc;
  2408. struct sdhci_host *host;
  2409. WARN_ON(dev == NULL);
  2410. mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
  2411. if (!mmc)
  2412. return ERR_PTR(-ENOMEM);
  2413. host = mmc_priv(mmc);
  2414. host->mmc = mmc;
  2415. return host;
  2416. }
  2417. EXPORT_SYMBOL_GPL(sdhci_alloc_host);
  2418. int sdhci_add_host(struct sdhci_host *host)
  2419. {
  2420. struct mmc_host *mmc;
  2421. u32 caps[2] = {0, 0};
  2422. u32 max_current_caps;
  2423. unsigned int ocr_avail;
  2424. unsigned int override_timeout_clk;
  2425. int ret;
  2426. WARN_ON(host == NULL);
  2427. if (host == NULL)
  2428. return -EINVAL;
  2429. mmc = host->mmc;
  2430. if (debug_quirks)
  2431. host->quirks = debug_quirks;
  2432. if (debug_quirks2)
  2433. host->quirks2 = debug_quirks2;
  2434. override_timeout_clk = host->timeout_clk;
  2435. sdhci_do_reset(host, SDHCI_RESET_ALL);
  2436. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  2437. host->version = (host->version & SDHCI_SPEC_VER_MASK)
  2438. >> SDHCI_SPEC_VER_SHIFT;
  2439. if (host->version > SDHCI_SPEC_300) {
  2440. pr_err("%s: Unknown controller version (%d). "
  2441. "You may experience problems.\n", mmc_hostname(mmc),
  2442. host->version);
  2443. }
  2444. caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
  2445. sdhci_readl(host, SDHCI_CAPABILITIES);
  2446. if (host->version >= SDHCI_SPEC_300)
  2447. caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
  2448. host->caps1 :
  2449. sdhci_readl(host, SDHCI_CAPABILITIES_1);
  2450. if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
  2451. host->flags |= SDHCI_USE_SDMA;
  2452. else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
  2453. DBG("Controller doesn't have SDMA capability\n");
  2454. else
  2455. host->flags |= SDHCI_USE_SDMA;
  2456. if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
  2457. (host->flags & SDHCI_USE_SDMA)) {
  2458. DBG("Disabling DMA as it is marked broken\n");
  2459. host->flags &= ~SDHCI_USE_SDMA;
  2460. }
  2461. if ((host->version >= SDHCI_SPEC_200) &&
  2462. (caps[0] & SDHCI_CAN_DO_ADMA2))
  2463. host->flags |= SDHCI_USE_ADMA;
  2464. if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
  2465. (host->flags & SDHCI_USE_ADMA)) {
  2466. DBG("Disabling ADMA as it is marked broken\n");
  2467. host->flags &= ~SDHCI_USE_ADMA;
  2468. }
  2469. /*
  2470. * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
  2471. * and *must* do 64-bit DMA. A driver has the opportunity to change
  2472. * that during the first call to ->enable_dma(). Similarly
  2473. * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
  2474. * implement.
  2475. */
  2476. if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT)
  2477. host->flags |= SDHCI_USE_64_BIT_DMA;
  2478. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2479. if (host->ops->enable_dma) {
  2480. if (host->ops->enable_dma(host)) {
  2481. pr_warn("%s: No suitable DMA available - falling back to PIO\n",
  2482. mmc_hostname(mmc));
  2483. host->flags &=
  2484. ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  2485. }
  2486. }
  2487. }
  2488. /* SDMA does not support 64-bit DMA */
  2489. if (host->flags & SDHCI_USE_64_BIT_DMA)
  2490. host->flags &= ~SDHCI_USE_SDMA;
  2491. if (host->flags & SDHCI_USE_ADMA) {
  2492. /*
  2493. * The DMA descriptor table size is calculated as the maximum
  2494. * number of segments times 2, to allow for an alignment
  2495. * descriptor for each segment, plus 1 for a nop end descriptor,
  2496. * all multipled by the descriptor size.
  2497. */
  2498. if (host->flags & SDHCI_USE_64_BIT_DMA) {
  2499. host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
  2500. SDHCI_ADMA2_64_DESC_SZ;
  2501. host->align_buffer_sz = SDHCI_MAX_SEGS *
  2502. SDHCI_ADMA2_64_ALIGN;
  2503. host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
  2504. host->align_sz = SDHCI_ADMA2_64_ALIGN;
  2505. host->align_mask = SDHCI_ADMA2_64_ALIGN - 1;
  2506. } else {
  2507. host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
  2508. SDHCI_ADMA2_32_DESC_SZ;
  2509. host->align_buffer_sz = SDHCI_MAX_SEGS *
  2510. SDHCI_ADMA2_32_ALIGN;
  2511. host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
  2512. host->align_sz = SDHCI_ADMA2_32_ALIGN;
  2513. host->align_mask = SDHCI_ADMA2_32_ALIGN - 1;
  2514. }
  2515. host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
  2516. host->adma_table_sz,
  2517. &host->adma_addr,
  2518. GFP_KERNEL);
  2519. host->align_buffer = kmalloc(host->align_buffer_sz, GFP_KERNEL);
  2520. if (!host->adma_table || !host->align_buffer) {
  2521. dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
  2522. host->adma_table, host->adma_addr);
  2523. kfree(host->align_buffer);
  2524. pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
  2525. mmc_hostname(mmc));
  2526. host->flags &= ~SDHCI_USE_ADMA;
  2527. host->adma_table = NULL;
  2528. host->align_buffer = NULL;
  2529. } else if (host->adma_addr & host->align_mask) {
  2530. pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
  2531. mmc_hostname(mmc));
  2532. host->flags &= ~SDHCI_USE_ADMA;
  2533. dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
  2534. host->adma_table, host->adma_addr);
  2535. kfree(host->align_buffer);
  2536. host->adma_table = NULL;
  2537. host->align_buffer = NULL;
  2538. }
  2539. }
  2540. /*
  2541. * If we use DMA, then it's up to the caller to set the DMA
  2542. * mask, but PIO does not need the hw shim so we set a new
  2543. * mask here in that case.
  2544. */
  2545. if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
  2546. host->dma_mask = DMA_BIT_MASK(64);
  2547. mmc_dev(mmc)->dma_mask = &host->dma_mask;
  2548. }
  2549. if (host->version >= SDHCI_SPEC_300)
  2550. host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
  2551. >> SDHCI_CLOCK_BASE_SHIFT;
  2552. else
  2553. host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
  2554. >> SDHCI_CLOCK_BASE_SHIFT;
  2555. host->max_clk *= 1000000;
  2556. if (host->max_clk == 0 || host->quirks &
  2557. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
  2558. if (!host->ops->get_max_clock) {
  2559. pr_err("%s: Hardware doesn't specify base clock "
  2560. "frequency.\n", mmc_hostname(mmc));
  2561. return -ENODEV;
  2562. }
  2563. host->max_clk = host->ops->get_max_clock(host);
  2564. }
  2565. host->next_data.cookie = 1;
  2566. /*
  2567. * In case of Host Controller v3.00, find out whether clock
  2568. * multiplier is supported.
  2569. */
  2570. host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
  2571. SDHCI_CLOCK_MUL_SHIFT;
  2572. /*
  2573. * In case the value in Clock Multiplier is 0, then programmable
  2574. * clock mode is not supported, otherwise the actual clock
  2575. * multiplier is one more than the value of Clock Multiplier
  2576. * in the Capabilities Register.
  2577. */
  2578. if (host->clk_mul)
  2579. host->clk_mul += 1;
  2580. /*
  2581. * Set host parameters.
  2582. */
  2583. mmc->ops = &sdhci_ops;
  2584. mmc->f_max = host->max_clk;
  2585. if (host->ops->get_min_clock)
  2586. mmc->f_min = host->ops->get_min_clock(host);
  2587. else if (host->version >= SDHCI_SPEC_300) {
  2588. if (host->clk_mul) {
  2589. mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
  2590. mmc->f_max = host->max_clk * host->clk_mul;
  2591. } else
  2592. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  2593. } else
  2594. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
  2595. if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
  2596. host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
  2597. SDHCI_TIMEOUT_CLK_SHIFT;
  2598. if (host->timeout_clk == 0) {
  2599. if (host->ops->get_timeout_clock) {
  2600. host->timeout_clk =
  2601. host->ops->get_timeout_clock(host);
  2602. } else {
  2603. pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
  2604. mmc_hostname(mmc));
  2605. return -ENODEV;
  2606. }
  2607. }
  2608. if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
  2609. host->timeout_clk *= 1000;
  2610. mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
  2611. host->ops->get_max_timeout_count(host) : 1 << 27;
  2612. mmc->max_busy_timeout /= host->timeout_clk;
  2613. }
  2614. if (override_timeout_clk)
  2615. host->timeout_clk = override_timeout_clk;
  2616. mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
  2617. mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
  2618. if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
  2619. host->flags |= SDHCI_AUTO_CMD12;
  2620. /* Auto-CMD23 stuff only works in ADMA or PIO. */
  2621. if ((host->version >= SDHCI_SPEC_300) &&
  2622. ((host->flags & SDHCI_USE_ADMA) ||
  2623. !(host->flags & SDHCI_USE_SDMA)) &&
  2624. !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
  2625. host->flags |= SDHCI_AUTO_CMD23;
  2626. DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
  2627. } else {
  2628. DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
  2629. }
  2630. /*
  2631. * A controller may support 8-bit width, but the board itself
  2632. * might not have the pins brought out. Boards that support
  2633. * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
  2634. * their platform code before calling sdhci_add_host(), and we
  2635. * won't assume 8-bit width for hosts without that CAP.
  2636. */
  2637. if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
  2638. mmc->caps |= MMC_CAP_4_BIT_DATA;
  2639. if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
  2640. mmc->caps &= ~MMC_CAP_CMD23;
  2641. if (caps[0] & SDHCI_CAN_DO_HISPD)
  2642. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  2643. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
  2644. !(mmc->caps & MMC_CAP_NONREMOVABLE))
  2645. mmc->caps |= MMC_CAP_NEEDS_POLL;
  2646. /* If there are external regulators, get them */
  2647. if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
  2648. return -EPROBE_DEFER;
  2649. /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
  2650. if (!IS_ERR(mmc->supply.vqmmc)) {
  2651. ret = regulator_enable(mmc->supply.vqmmc);
  2652. if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
  2653. 1950000))
  2654. caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
  2655. SDHCI_SUPPORT_SDR50 |
  2656. SDHCI_SUPPORT_DDR50);
  2657. if (ret) {
  2658. pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
  2659. mmc_hostname(mmc), ret);
  2660. mmc->supply.vqmmc = ERR_PTR(-EINVAL);
  2661. }
  2662. }
  2663. if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
  2664. caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
  2665. SDHCI_SUPPORT_DDR50);
  2666. /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
  2667. if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
  2668. SDHCI_SUPPORT_DDR50))
  2669. mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
  2670. /* SDR104 supports also implies SDR50 support */
  2671. if (caps[1] & SDHCI_SUPPORT_SDR104) {
  2672. mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
  2673. /* SD3.0: SDR104 is supported so (for eMMC) the caps2
  2674. * field can be promoted to support HS200.
  2675. */
  2676. if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
  2677. mmc->caps2 |= MMC_CAP2_HS200;
  2678. } else if (caps[1] & SDHCI_SUPPORT_SDR50)
  2679. mmc->caps |= MMC_CAP_UHS_SDR50;
  2680. if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
  2681. (caps[1] & SDHCI_SUPPORT_HS400))
  2682. mmc->caps2 |= MMC_CAP2_HS400;
  2683. if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
  2684. (IS_ERR(mmc->supply.vqmmc) ||
  2685. !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
  2686. 1300000)))
  2687. mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
  2688. if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
  2689. !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
  2690. mmc->caps |= MMC_CAP_UHS_DDR50;
  2691. /* Does the host need tuning for SDR50? */
  2692. if (caps[1] & SDHCI_USE_SDR50_TUNING)
  2693. host->flags |= SDHCI_SDR50_NEEDS_TUNING;
  2694. /* Does the host need tuning for SDR104 / HS200? */
  2695. if (mmc->caps2 & MMC_CAP2_HS200)
  2696. host->flags |= SDHCI_SDR104_NEEDS_TUNING;
  2697. /* Driver Type(s) (A, C, D) supported by the host */
  2698. if (caps[1] & SDHCI_DRIVER_TYPE_A)
  2699. mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
  2700. if (caps[1] & SDHCI_DRIVER_TYPE_C)
  2701. mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
  2702. if (caps[1] & SDHCI_DRIVER_TYPE_D)
  2703. mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
  2704. /* Initial value for re-tuning timer count */
  2705. host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
  2706. SDHCI_RETUNING_TIMER_COUNT_SHIFT;
  2707. /*
  2708. * In case Re-tuning Timer is not disabled, the actual value of
  2709. * re-tuning timer will be 2 ^ (n - 1).
  2710. */
  2711. if (host->tuning_count)
  2712. host->tuning_count = 1 << (host->tuning_count - 1);
  2713. /* Re-tuning mode supported by the Host Controller */
  2714. host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
  2715. SDHCI_RETUNING_MODE_SHIFT;
  2716. ocr_avail = 0;
  2717. /*
  2718. * According to SD Host Controller spec v3.00, if the Host System
  2719. * can afford more than 150mA, Host Driver should set XPC to 1. Also
  2720. * the value is meaningful only if Voltage Support in the Capabilities
  2721. * register is set. The actual current value is 4 times the register
  2722. * value.
  2723. */
  2724. max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
  2725. if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
  2726. int curr = regulator_get_current_limit(mmc->supply.vmmc);
  2727. if (curr > 0) {
  2728. /* convert to SDHCI_MAX_CURRENT format */
  2729. curr = curr/1000; /* convert to mA */
  2730. curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
  2731. curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
  2732. max_current_caps =
  2733. (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
  2734. (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
  2735. (curr << SDHCI_MAX_CURRENT_180_SHIFT);
  2736. }
  2737. }
  2738. if (caps[0] & SDHCI_CAN_VDD_330) {
  2739. ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
  2740. mmc->max_current_330 = ((max_current_caps &
  2741. SDHCI_MAX_CURRENT_330_MASK) >>
  2742. SDHCI_MAX_CURRENT_330_SHIFT) *
  2743. SDHCI_MAX_CURRENT_MULTIPLIER;
  2744. }
  2745. if (caps[0] & SDHCI_CAN_VDD_300) {
  2746. ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
  2747. mmc->max_current_300 = ((max_current_caps &
  2748. SDHCI_MAX_CURRENT_300_MASK) >>
  2749. SDHCI_MAX_CURRENT_300_SHIFT) *
  2750. SDHCI_MAX_CURRENT_MULTIPLIER;
  2751. }
  2752. if (caps[0] & SDHCI_CAN_VDD_180) {
  2753. ocr_avail |= MMC_VDD_165_195;
  2754. mmc->max_current_180 = ((max_current_caps &
  2755. SDHCI_MAX_CURRENT_180_MASK) >>
  2756. SDHCI_MAX_CURRENT_180_SHIFT) *
  2757. SDHCI_MAX_CURRENT_MULTIPLIER;
  2758. }
  2759. /* If OCR set by external regulators, use it instead */
  2760. if (mmc->ocr_avail)
  2761. ocr_avail = mmc->ocr_avail;
  2762. if (host->ocr_mask)
  2763. ocr_avail &= host->ocr_mask;
  2764. mmc->ocr_avail = ocr_avail;
  2765. mmc->ocr_avail_sdio = ocr_avail;
  2766. if (host->ocr_avail_sdio)
  2767. mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
  2768. mmc->ocr_avail_sd = ocr_avail;
  2769. if (host->ocr_avail_sd)
  2770. mmc->ocr_avail_sd &= host->ocr_avail_sd;
  2771. else /* normal SD controllers don't support 1.8V */
  2772. mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
  2773. mmc->ocr_avail_mmc = ocr_avail;
  2774. if (host->ocr_avail_mmc)
  2775. mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
  2776. if (mmc->ocr_avail == 0) {
  2777. pr_err("%s: Hardware doesn't report any "
  2778. "support voltages.\n", mmc_hostname(mmc));
  2779. return -ENODEV;
  2780. }
  2781. spin_lock_init(&host->lock);
  2782. /*
  2783. * Maximum number of segments. Depends on if the hardware
  2784. * can do scatter/gather or not.
  2785. */
  2786. if (host->flags & SDHCI_USE_ADMA)
  2787. mmc->max_segs = SDHCI_MAX_SEGS;
  2788. else if (host->flags & SDHCI_USE_SDMA)
  2789. mmc->max_segs = 1;
  2790. else /* PIO */
  2791. mmc->max_segs = SDHCI_MAX_SEGS;
  2792. /*
  2793. * Maximum number of sectors in one transfer. Limited by SDMA boundary
  2794. * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
  2795. * is less anyway.
  2796. */
  2797. mmc->max_req_size = 524288;
  2798. /*
  2799. * Maximum segment size. Could be one segment with the maximum number
  2800. * of bytes. When doing hardware scatter/gather, each entry cannot
  2801. * be larger than 64 KiB though.
  2802. */
  2803. if (host->flags & SDHCI_USE_ADMA) {
  2804. if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
  2805. mmc->max_seg_size = 65535;
  2806. else
  2807. mmc->max_seg_size = 65536;
  2808. } else {
  2809. mmc->max_seg_size = mmc->max_req_size;
  2810. }
  2811. /*
  2812. * Maximum block size. This varies from controller to controller and
  2813. * is specified in the capabilities register.
  2814. */
  2815. if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
  2816. mmc->max_blk_size = 2;
  2817. } else {
  2818. mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
  2819. SDHCI_MAX_BLOCK_SHIFT;
  2820. if (mmc->max_blk_size >= 3) {
  2821. pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
  2822. mmc_hostname(mmc));
  2823. mmc->max_blk_size = 0;
  2824. }
  2825. }
  2826. mmc->max_blk_size = 512 << mmc->max_blk_size;
  2827. /*
  2828. * Maximum block count.
  2829. */
  2830. mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
  2831. /*
  2832. * Init tasklets.
  2833. */
  2834. tasklet_init(&host->finish_tasklet,
  2835. sdhci_tasklet_finish, (unsigned long)host);
  2836. setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
  2837. init_waitqueue_head(&host->buf_ready_int);
  2838. if (host->version >= SDHCI_SPEC_300) {
  2839. /* Initialize re-tuning timer */
  2840. init_timer(&host->tuning_timer);
  2841. host->tuning_timer.data = (unsigned long)host;
  2842. host->tuning_timer.function = sdhci_tuning_timer;
  2843. }
  2844. sdhci_init(host, 0);
  2845. ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
  2846. IRQF_SHARED, mmc_hostname(mmc), host);
  2847. if (ret) {
  2848. pr_err("%s: Failed to request IRQ %d: %d\n",
  2849. mmc_hostname(mmc), host->irq, ret);
  2850. goto untasklet;
  2851. }
  2852. #ifdef CONFIG_MMC_DEBUG
  2853. sdhci_dumpregs(host);
  2854. #endif
  2855. #ifdef SDHCI_USE_LEDS_CLASS
  2856. snprintf(host->led_name, sizeof(host->led_name),
  2857. "%s::", mmc_hostname(mmc));
  2858. host->led.name = host->led_name;
  2859. host->led.brightness = LED_OFF;
  2860. host->led.default_trigger = mmc_hostname(mmc);
  2861. host->led.brightness_set = sdhci_led_control;
  2862. ret = led_classdev_register(mmc_dev(mmc), &host->led);
  2863. if (ret) {
  2864. pr_err("%s: Failed to register LED device: %d\n",
  2865. mmc_hostname(mmc), ret);
  2866. goto reset;
  2867. }
  2868. #endif
  2869. mmiowb();
  2870. mmc_add_host(mmc);
  2871. pr_info("%s: SDHCI controller on %s [%s] using %s\n",
  2872. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  2873. (host->flags & SDHCI_USE_ADMA) ?
  2874. (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
  2875. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  2876. sdhci_enable_card_detection(host);
  2877. return 0;
  2878. #ifdef SDHCI_USE_LEDS_CLASS
  2879. reset:
  2880. sdhci_do_reset(host, SDHCI_RESET_ALL);
  2881. sdhci_writel(host, 0, SDHCI_INT_ENABLE);
  2882. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  2883. free_irq(host->irq, host);
  2884. #endif
  2885. untasklet:
  2886. tasklet_kill(&host->finish_tasklet);
  2887. return ret;
  2888. }
  2889. EXPORT_SYMBOL_GPL(sdhci_add_host);
  2890. void sdhci_remove_host(struct sdhci_host *host, int dead)
  2891. {
  2892. struct mmc_host *mmc = host->mmc;
  2893. unsigned long flags;
  2894. if (dead) {
  2895. spin_lock_irqsave(&host->lock, flags);
  2896. host->flags |= SDHCI_DEVICE_DEAD;
  2897. if (host->mrq) {
  2898. pr_err("%s: Controller removed during "
  2899. " transfer!\n", mmc_hostname(mmc));
  2900. host->mrq->cmd->error = -ENOMEDIUM;
  2901. tasklet_schedule(&host->finish_tasklet);
  2902. }
  2903. spin_unlock_irqrestore(&host->lock, flags);
  2904. }
  2905. sdhci_disable_card_detection(host);
  2906. mmc_remove_host(mmc);
  2907. #ifdef SDHCI_USE_LEDS_CLASS
  2908. led_classdev_unregister(&host->led);
  2909. #endif
  2910. if (!dead)
  2911. sdhci_do_reset(host, SDHCI_RESET_ALL);
  2912. sdhci_writel(host, 0, SDHCI_INT_ENABLE);
  2913. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  2914. free_irq(host->irq, host);
  2915. del_timer_sync(&host->timer);
  2916. tasklet_kill(&host->finish_tasklet);
  2917. if (!IS_ERR(mmc->supply.vqmmc))
  2918. regulator_disable(mmc->supply.vqmmc);
  2919. if (host->adma_table)
  2920. dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
  2921. host->adma_table, host->adma_addr);
  2922. kfree(host->align_buffer);
  2923. host->adma_table = NULL;
  2924. host->align_buffer = NULL;
  2925. }
  2926. EXPORT_SYMBOL_GPL(sdhci_remove_host);
  2927. void sdhci_free_host(struct sdhci_host *host)
  2928. {
  2929. mmc_free_host(host->mmc);
  2930. }
  2931. EXPORT_SYMBOL_GPL(sdhci_free_host);
  2932. /*****************************************************************************\
  2933. * *
  2934. * Driver init/exit *
  2935. * *
  2936. \*****************************************************************************/
  2937. static int __init sdhci_drv_init(void)
  2938. {
  2939. pr_info(DRIVER_NAME
  2940. ": Secure Digital Host Controller Interface driver\n");
  2941. pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  2942. return 0;
  2943. }
  2944. static void __exit sdhci_drv_exit(void)
  2945. {
  2946. }
  2947. module_init(sdhci_drv_init);
  2948. module_exit(sdhci_drv_exit);
  2949. module_param(debug_quirks, uint, 0444);
  2950. module_param(debug_quirks2, uint, 0444);
  2951. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  2952. MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
  2953. MODULE_LICENSE("GPL");
  2954. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
  2955. MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");