sdhci.c 88 KB

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