sdhci.c 91 KB

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