sdhci.c 84 KB

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