sdhci.c 91 KB

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