sdhci.c 87 KB

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