sdhci.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247
  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. bool 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. /* If polling, assume that the card is always present. */
  1059. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  1060. present = true;
  1061. else
  1062. present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  1063. SDHCI_CARD_PRESENT;
  1064. /* If we're using a cd-gpio, testing the presence bit might fail. */
  1065. if (!present) {
  1066. int ret = mmc_gpio_get_cd(host->mmc);
  1067. if (ret > 0)
  1068. present = true;
  1069. }
  1070. if (!present || host->flags & SDHCI_DEVICE_DEAD) {
  1071. host->mrq->cmd->error = -ENOMEDIUM;
  1072. tasklet_schedule(&host->finish_tasklet);
  1073. } else {
  1074. u32 present_state;
  1075. present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  1076. /*
  1077. * Check if the re-tuning timer has already expired and there
  1078. * is no on-going data transfer. If so, we need to execute
  1079. * tuning procedure before sending command.
  1080. */
  1081. if ((host->flags & SDHCI_NEEDS_RETUNING) &&
  1082. !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
  1083. if (mmc->card) {
  1084. /* eMMC uses cmd21 but sd and sdio use cmd19 */
  1085. tuning_opcode =
  1086. mmc->card->type == MMC_TYPE_MMC ?
  1087. MMC_SEND_TUNING_BLOCK_HS200 :
  1088. MMC_SEND_TUNING_BLOCK;
  1089. spin_unlock_irqrestore(&host->lock, flags);
  1090. sdhci_execute_tuning(mmc, tuning_opcode);
  1091. spin_lock_irqsave(&host->lock, flags);
  1092. /* Restore original mmc_request structure */
  1093. host->mrq = mrq;
  1094. }
  1095. }
  1096. if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
  1097. sdhci_send_command(host, mrq->sbc);
  1098. else
  1099. sdhci_send_command(host, mrq->cmd);
  1100. }
  1101. mmiowb();
  1102. spin_unlock_irqrestore(&host->lock, flags);
  1103. }
  1104. static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
  1105. {
  1106. unsigned long flags;
  1107. int vdd_bit = -1;
  1108. u8 ctrl;
  1109. spin_lock_irqsave(&host->lock, flags);
  1110. if (host->flags & SDHCI_DEVICE_DEAD) {
  1111. spin_unlock_irqrestore(&host->lock, flags);
  1112. if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
  1113. mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
  1114. return;
  1115. }
  1116. /*
  1117. * Reset the chip on each power off.
  1118. * Should clear out any weird states.
  1119. */
  1120. if (ios->power_mode == MMC_POWER_OFF) {
  1121. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  1122. sdhci_reinit(host);
  1123. }
  1124. sdhci_set_clock(host, ios->clock);
  1125. if (ios->power_mode == MMC_POWER_OFF)
  1126. vdd_bit = sdhci_set_power(host, -1);
  1127. else
  1128. vdd_bit = sdhci_set_power(host, ios->vdd);
  1129. if (host->vmmc && vdd_bit != -1) {
  1130. spin_unlock_irqrestore(&host->lock, flags);
  1131. mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
  1132. spin_lock_irqsave(&host->lock, flags);
  1133. }
  1134. if (host->ops->platform_send_init_74_clocks)
  1135. host->ops->platform_send_init_74_clocks(host, ios->power_mode);
  1136. /*
  1137. * If your platform has 8-bit width support but is not a v3 controller,
  1138. * or if it requires special setup code, you should implement that in
  1139. * platform_8bit_width().
  1140. */
  1141. if (host->ops->platform_8bit_width)
  1142. host->ops->platform_8bit_width(host, ios->bus_width);
  1143. else {
  1144. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  1145. if (ios->bus_width == MMC_BUS_WIDTH_8) {
  1146. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1147. if (host->version >= SDHCI_SPEC_300)
  1148. ctrl |= SDHCI_CTRL_8BITBUS;
  1149. } else {
  1150. if (host->version >= SDHCI_SPEC_300)
  1151. ctrl &= ~SDHCI_CTRL_8BITBUS;
  1152. if (ios->bus_width == MMC_BUS_WIDTH_4)
  1153. ctrl |= SDHCI_CTRL_4BITBUS;
  1154. else
  1155. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1156. }
  1157. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1158. }
  1159. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  1160. if ((ios->timing == MMC_TIMING_SD_HS ||
  1161. ios->timing == MMC_TIMING_MMC_HS)
  1162. && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
  1163. ctrl |= SDHCI_CTRL_HISPD;
  1164. else
  1165. ctrl &= ~SDHCI_CTRL_HISPD;
  1166. if (host->version >= SDHCI_SPEC_300) {
  1167. u16 clk, ctrl_2;
  1168. unsigned int clock;
  1169. /* In case of UHS-I modes, set High Speed Enable */
  1170. if ((ios->timing == MMC_TIMING_MMC_HS200) ||
  1171. (ios->timing == MMC_TIMING_UHS_SDR50) ||
  1172. (ios->timing == MMC_TIMING_UHS_SDR104) ||
  1173. (ios->timing == MMC_TIMING_UHS_DDR50) ||
  1174. (ios->timing == MMC_TIMING_UHS_SDR25))
  1175. ctrl |= SDHCI_CTRL_HISPD;
  1176. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1177. if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
  1178. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1179. /*
  1180. * We only need to set Driver Strength if the
  1181. * preset value enable is not set.
  1182. */
  1183. ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
  1184. if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
  1185. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
  1186. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
  1187. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
  1188. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1189. } else {
  1190. /*
  1191. * According to SDHC Spec v3.00, if the Preset Value
  1192. * Enable in the Host Control 2 register is set, we
  1193. * need to reset SD Clock Enable before changing High
  1194. * Speed Enable to avoid generating clock gliches.
  1195. */
  1196. /* Reset SD Clock Enable */
  1197. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1198. clk &= ~SDHCI_CLOCK_CARD_EN;
  1199. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1200. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1201. /* Re-enable SD Clock */
  1202. clock = host->clock;
  1203. host->clock = 0;
  1204. sdhci_set_clock(host, clock);
  1205. }
  1206. /* Reset SD Clock Enable */
  1207. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1208. clk &= ~SDHCI_CLOCK_CARD_EN;
  1209. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1210. if (host->ops->set_uhs_signaling)
  1211. host->ops->set_uhs_signaling(host, ios->timing);
  1212. else {
  1213. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1214. /* Select Bus Speed Mode for host */
  1215. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  1216. if (ios->timing == MMC_TIMING_MMC_HS200)
  1217. ctrl_2 |= SDHCI_CTRL_HS_SDR200;
  1218. else if (ios->timing == MMC_TIMING_UHS_SDR12)
  1219. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  1220. else if (ios->timing == MMC_TIMING_UHS_SDR25)
  1221. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  1222. else if (ios->timing == MMC_TIMING_UHS_SDR50)
  1223. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  1224. else if (ios->timing == MMC_TIMING_UHS_SDR104)
  1225. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  1226. else if (ios->timing == MMC_TIMING_UHS_DDR50)
  1227. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  1228. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1229. }
  1230. /* Re-enable SD Clock */
  1231. clock = host->clock;
  1232. host->clock = 0;
  1233. sdhci_set_clock(host, clock);
  1234. } else
  1235. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1236. /*
  1237. * Some (ENE) controllers go apeshit on some ios operation,
  1238. * signalling timeout and CRC errors even on CMD0. Resetting
  1239. * it on each ios seems to solve the problem.
  1240. */
  1241. if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
  1242. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  1243. mmiowb();
  1244. spin_unlock_irqrestore(&host->lock, flags);
  1245. }
  1246. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1247. {
  1248. struct sdhci_host *host = mmc_priv(mmc);
  1249. sdhci_runtime_pm_get(host);
  1250. sdhci_do_set_ios(host, ios);
  1251. sdhci_runtime_pm_put(host);
  1252. }
  1253. static int sdhci_check_ro(struct sdhci_host *host)
  1254. {
  1255. unsigned long flags;
  1256. int is_readonly;
  1257. spin_lock_irqsave(&host->lock, flags);
  1258. if (host->flags & SDHCI_DEVICE_DEAD)
  1259. is_readonly = 0;
  1260. else if (host->ops->get_ro)
  1261. is_readonly = host->ops->get_ro(host);
  1262. else
  1263. is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
  1264. & SDHCI_WRITE_PROTECT);
  1265. spin_unlock_irqrestore(&host->lock, flags);
  1266. /* This quirk needs to be replaced by a callback-function later */
  1267. return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
  1268. !is_readonly : is_readonly;
  1269. }
  1270. #define SAMPLE_COUNT 5
  1271. static int sdhci_do_get_ro(struct sdhci_host *host)
  1272. {
  1273. int i, ro_count;
  1274. if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
  1275. return sdhci_check_ro(host);
  1276. ro_count = 0;
  1277. for (i = 0; i < SAMPLE_COUNT; i++) {
  1278. if (sdhci_check_ro(host)) {
  1279. if (++ro_count > SAMPLE_COUNT / 2)
  1280. return 1;
  1281. }
  1282. msleep(30);
  1283. }
  1284. return 0;
  1285. }
  1286. static void sdhci_hw_reset(struct mmc_host *mmc)
  1287. {
  1288. struct sdhci_host *host = mmc_priv(mmc);
  1289. if (host->ops && host->ops->hw_reset)
  1290. host->ops->hw_reset(host);
  1291. }
  1292. static int sdhci_get_ro(struct mmc_host *mmc)
  1293. {
  1294. struct sdhci_host *host = mmc_priv(mmc);
  1295. int ret;
  1296. sdhci_runtime_pm_get(host);
  1297. ret = sdhci_do_get_ro(host);
  1298. sdhci_runtime_pm_put(host);
  1299. return ret;
  1300. }
  1301. static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
  1302. {
  1303. if (host->flags & SDHCI_DEVICE_DEAD)
  1304. goto out;
  1305. if (enable)
  1306. host->flags |= SDHCI_SDIO_IRQ_ENABLED;
  1307. else
  1308. host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
  1309. /* SDIO IRQ will be enabled as appropriate in runtime resume */
  1310. if (host->runtime_suspended)
  1311. goto out;
  1312. if (enable)
  1313. sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
  1314. else
  1315. sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
  1316. out:
  1317. mmiowb();
  1318. }
  1319. static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1320. {
  1321. struct sdhci_host *host = mmc_priv(mmc);
  1322. unsigned long flags;
  1323. spin_lock_irqsave(&host->lock, flags);
  1324. sdhci_enable_sdio_irq_nolock(host, enable);
  1325. spin_unlock_irqrestore(&host->lock, flags);
  1326. }
  1327. static int sdhci_do_3_3v_signal_voltage_switch(struct sdhci_host *host,
  1328. u16 ctrl)
  1329. {
  1330. int ret;
  1331. /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
  1332. ctrl &= ~SDHCI_CTRL_VDD_180;
  1333. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1334. if (host->vqmmc) {
  1335. ret = regulator_set_voltage(host->vqmmc, 2700000, 3600000);
  1336. if (ret) {
  1337. pr_warning("%s: Switching to 3.3V signalling voltage "
  1338. " failed\n", mmc_hostname(host->mmc));
  1339. return -EIO;
  1340. }
  1341. }
  1342. /* Wait for 5ms */
  1343. usleep_range(5000, 5500);
  1344. /* 3.3V regulator output should be stable within 5 ms */
  1345. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1346. if (!(ctrl & SDHCI_CTRL_VDD_180))
  1347. return 0;
  1348. pr_warning("%s: 3.3V regulator output did not became stable\n",
  1349. mmc_hostname(host->mmc));
  1350. return -EIO;
  1351. }
  1352. static int sdhci_do_1_8v_signal_voltage_switch(struct sdhci_host *host,
  1353. u16 ctrl)
  1354. {
  1355. u8 pwr;
  1356. u16 clk;
  1357. u32 present_state;
  1358. int ret;
  1359. /* Stop SDCLK */
  1360. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1361. clk &= ~SDHCI_CLOCK_CARD_EN;
  1362. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1363. /* Check whether DAT[3:0] is 0000 */
  1364. present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  1365. if (!((present_state & SDHCI_DATA_LVL_MASK) >>
  1366. SDHCI_DATA_LVL_SHIFT)) {
  1367. /*
  1368. * Enable 1.8V Signal Enable in the Host Control2
  1369. * register
  1370. */
  1371. if (host->vqmmc)
  1372. ret = regulator_set_voltage(host->vqmmc,
  1373. 1700000, 1950000);
  1374. else
  1375. ret = 0;
  1376. if (!ret) {
  1377. ctrl |= SDHCI_CTRL_VDD_180;
  1378. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1379. /* Wait for 5ms */
  1380. usleep_range(5000, 5500);
  1381. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1382. if (ctrl & SDHCI_CTRL_VDD_180) {
  1383. /* Provide SDCLK again and wait for 1ms */
  1384. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1385. clk |= SDHCI_CLOCK_CARD_EN;
  1386. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1387. usleep_range(1000, 1500);
  1388. /*
  1389. * If DAT[3:0] level is 1111b, then the card
  1390. * was successfully switched to 1.8V signaling.
  1391. */
  1392. present_state = sdhci_readl(host,
  1393. SDHCI_PRESENT_STATE);
  1394. if ((present_state & SDHCI_DATA_LVL_MASK) ==
  1395. SDHCI_DATA_LVL_MASK)
  1396. return 0;
  1397. }
  1398. }
  1399. }
  1400. /*
  1401. * If we are here, that means the switch to 1.8V signaling
  1402. * failed. We power cycle the card, and retry initialization
  1403. * sequence by setting S18R to 0.
  1404. */
  1405. pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
  1406. pwr &= ~SDHCI_POWER_ON;
  1407. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1408. if (host->vmmc)
  1409. regulator_disable(host->vmmc);
  1410. /* Wait for 1ms as per the spec */
  1411. usleep_range(1000, 1500);
  1412. pwr |= SDHCI_POWER_ON;
  1413. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1414. if (host->vmmc)
  1415. regulator_enable(host->vmmc);
  1416. pr_warning("%s: Switching to 1.8V signalling voltage failed, "
  1417. "retrying with S18R set to 0\n", mmc_hostname(host->mmc));
  1418. return -EAGAIN;
  1419. }
  1420. static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
  1421. struct mmc_ios *ios)
  1422. {
  1423. u16 ctrl;
  1424. /*
  1425. * Signal Voltage Switching is only applicable for Host Controllers
  1426. * v3.00 and above.
  1427. */
  1428. if (host->version < SDHCI_SPEC_300)
  1429. return 0;
  1430. /*
  1431. * We first check whether the request is to set signalling voltage
  1432. * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
  1433. */
  1434. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1435. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
  1436. return sdhci_do_3_3v_signal_voltage_switch(host, ctrl);
  1437. else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
  1438. (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180))
  1439. return sdhci_do_1_8v_signal_voltage_switch(host, ctrl);
  1440. else
  1441. /* No signal voltage switch required */
  1442. return 0;
  1443. }
  1444. static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
  1445. struct mmc_ios *ios)
  1446. {
  1447. struct sdhci_host *host = mmc_priv(mmc);
  1448. int err;
  1449. if (host->version < SDHCI_SPEC_300)
  1450. return 0;
  1451. sdhci_runtime_pm_get(host);
  1452. err = sdhci_do_start_signal_voltage_switch(host, ios);
  1453. sdhci_runtime_pm_put(host);
  1454. return err;
  1455. }
  1456. static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1457. {
  1458. struct sdhci_host *host;
  1459. u16 ctrl;
  1460. u32 ier;
  1461. int tuning_loop_counter = MAX_TUNING_LOOP;
  1462. unsigned long timeout;
  1463. int err = 0;
  1464. bool requires_tuning_nonuhs = false;
  1465. host = mmc_priv(mmc);
  1466. sdhci_runtime_pm_get(host);
  1467. disable_irq(host->irq);
  1468. spin_lock(&host->lock);
  1469. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1470. /*
  1471. * The Host Controller needs tuning only in case of SDR104 mode
  1472. * and for SDR50 mode when Use Tuning for SDR50 is set in the
  1473. * Capabilities register.
  1474. * If the Host Controller supports the HS200 mode then the
  1475. * tuning function has to be executed.
  1476. */
  1477. if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
  1478. (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
  1479. host->flags & SDHCI_HS200_NEEDS_TUNING))
  1480. requires_tuning_nonuhs = true;
  1481. if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
  1482. requires_tuning_nonuhs)
  1483. ctrl |= SDHCI_CTRL_EXEC_TUNING;
  1484. else {
  1485. spin_unlock(&host->lock);
  1486. enable_irq(host->irq);
  1487. sdhci_runtime_pm_put(host);
  1488. return 0;
  1489. }
  1490. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1491. /*
  1492. * As per the Host Controller spec v3.00, tuning command
  1493. * generates Buffer Read Ready interrupt, so enable that.
  1494. *
  1495. * Note: The spec clearly says that when tuning sequence
  1496. * is being performed, the controller does not generate
  1497. * interrupts other than Buffer Read Ready interrupt. But
  1498. * to make sure we don't hit a controller bug, we _only_
  1499. * enable Buffer Read Ready interrupt here.
  1500. */
  1501. ier = sdhci_readl(host, SDHCI_INT_ENABLE);
  1502. sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
  1503. /*
  1504. * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
  1505. * of loops reaches 40 times or a timeout of 150ms occurs.
  1506. */
  1507. timeout = 150;
  1508. do {
  1509. struct mmc_command cmd = {0};
  1510. struct mmc_request mrq = {NULL};
  1511. if (!tuning_loop_counter && !timeout)
  1512. break;
  1513. cmd.opcode = opcode;
  1514. cmd.arg = 0;
  1515. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  1516. cmd.retries = 0;
  1517. cmd.data = NULL;
  1518. cmd.error = 0;
  1519. mrq.cmd = &cmd;
  1520. host->mrq = &mrq;
  1521. /*
  1522. * In response to CMD19, the card sends 64 bytes of tuning
  1523. * block to the Host Controller. So we set the block size
  1524. * to 64 here.
  1525. */
  1526. if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
  1527. if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  1528. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
  1529. SDHCI_BLOCK_SIZE);
  1530. else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
  1531. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
  1532. SDHCI_BLOCK_SIZE);
  1533. } else {
  1534. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
  1535. SDHCI_BLOCK_SIZE);
  1536. }
  1537. /*
  1538. * The tuning block is sent by the card to the host controller.
  1539. * So we set the TRNS_READ bit in the Transfer Mode register.
  1540. * This also takes care of setting DMA Enable and Multi Block
  1541. * Select in the same register to 0.
  1542. */
  1543. sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
  1544. sdhci_send_command(host, &cmd);
  1545. host->cmd = NULL;
  1546. host->mrq = NULL;
  1547. spin_unlock(&host->lock);
  1548. enable_irq(host->irq);
  1549. /* Wait for Buffer Read Ready interrupt */
  1550. wait_event_interruptible_timeout(host->buf_ready_int,
  1551. (host->tuning_done == 1),
  1552. msecs_to_jiffies(50));
  1553. disable_irq(host->irq);
  1554. spin_lock(&host->lock);
  1555. if (!host->tuning_done) {
  1556. pr_info(DRIVER_NAME ": Timeout waiting for "
  1557. "Buffer Read Ready interrupt during tuning "
  1558. "procedure, falling back to fixed sampling "
  1559. "clock\n");
  1560. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1561. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  1562. ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
  1563. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1564. err = -EIO;
  1565. goto out;
  1566. }
  1567. host->tuning_done = 0;
  1568. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1569. tuning_loop_counter--;
  1570. timeout--;
  1571. mdelay(1);
  1572. } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
  1573. /*
  1574. * The Host Driver has exhausted the maximum number of loops allowed,
  1575. * so use fixed sampling frequency.
  1576. */
  1577. if (!tuning_loop_counter || !timeout) {
  1578. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  1579. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1580. } else {
  1581. if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
  1582. pr_info(DRIVER_NAME ": Tuning procedure"
  1583. " failed, falling back to fixed sampling"
  1584. " clock\n");
  1585. err = -EIO;
  1586. }
  1587. }
  1588. out:
  1589. /*
  1590. * If this is the very first time we are here, we start the retuning
  1591. * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
  1592. * flag won't be set, we check this condition before actually starting
  1593. * the timer.
  1594. */
  1595. if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
  1596. (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
  1597. host->flags |= SDHCI_USING_RETUNING_TIMER;
  1598. mod_timer(&host->tuning_timer, jiffies +
  1599. host->tuning_count * HZ);
  1600. /* Tuning mode 1 limits the maximum data length to 4MB */
  1601. mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
  1602. } else {
  1603. host->flags &= ~SDHCI_NEEDS_RETUNING;
  1604. /* Reload the new initial value for timer */
  1605. if (host->tuning_mode == SDHCI_TUNING_MODE_1)
  1606. mod_timer(&host->tuning_timer, jiffies +
  1607. host->tuning_count * HZ);
  1608. }
  1609. /*
  1610. * In case tuning fails, host controllers which support re-tuning can
  1611. * try tuning again at a later time, when the re-tuning timer expires.
  1612. * So for these controllers, we return 0. Since there might be other
  1613. * controllers who do not have this capability, we return error for
  1614. * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
  1615. * a retuning timer to do the retuning for the card.
  1616. */
  1617. if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
  1618. err = 0;
  1619. sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
  1620. spin_unlock(&host->lock);
  1621. enable_irq(host->irq);
  1622. sdhci_runtime_pm_put(host);
  1623. return err;
  1624. }
  1625. static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
  1626. {
  1627. u16 ctrl;
  1628. unsigned long flags;
  1629. /* Host Controller v3.00 defines preset value registers */
  1630. if (host->version < SDHCI_SPEC_300)
  1631. return;
  1632. spin_lock_irqsave(&host->lock, flags);
  1633. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1634. /*
  1635. * We only enable or disable Preset Value if they are not already
  1636. * enabled or disabled respectively. Otherwise, we bail out.
  1637. */
  1638. if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
  1639. ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
  1640. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1641. host->flags |= SDHCI_PV_ENABLED;
  1642. } else 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. }
  1647. spin_unlock_irqrestore(&host->lock, flags);
  1648. }
  1649. static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
  1650. {
  1651. struct sdhci_host *host = mmc_priv(mmc);
  1652. sdhci_runtime_pm_get(host);
  1653. sdhci_do_enable_preset_value(host, enable);
  1654. sdhci_runtime_pm_put(host);
  1655. }
  1656. static void sdhci_card_event(struct mmc_host *mmc)
  1657. {
  1658. struct sdhci_host *host = mmc_priv(mmc);
  1659. unsigned long flags;
  1660. spin_lock_irqsave(&host->lock, flags);
  1661. /* Check host->mrq first in case we are runtime suspended */
  1662. if (host->mrq &&
  1663. !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
  1664. pr_err("%s: Card removed during transfer!\n",
  1665. mmc_hostname(host->mmc));
  1666. pr_err("%s: Resetting controller.\n",
  1667. mmc_hostname(host->mmc));
  1668. sdhci_reset(host, SDHCI_RESET_CMD);
  1669. sdhci_reset(host, SDHCI_RESET_DATA);
  1670. host->mrq->cmd->error = -ENOMEDIUM;
  1671. tasklet_schedule(&host->finish_tasklet);
  1672. }
  1673. spin_unlock_irqrestore(&host->lock, flags);
  1674. }
  1675. static const struct mmc_host_ops sdhci_ops = {
  1676. .request = sdhci_request,
  1677. .set_ios = sdhci_set_ios,
  1678. .get_ro = sdhci_get_ro,
  1679. .hw_reset = sdhci_hw_reset,
  1680. .enable_sdio_irq = sdhci_enable_sdio_irq,
  1681. .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
  1682. .execute_tuning = sdhci_execute_tuning,
  1683. .enable_preset_value = sdhci_enable_preset_value,
  1684. .card_event = sdhci_card_event,
  1685. };
  1686. /*****************************************************************************\
  1687. * *
  1688. * Tasklets *
  1689. * *
  1690. \*****************************************************************************/
  1691. static void sdhci_tasklet_card(unsigned long param)
  1692. {
  1693. struct sdhci_host *host = (struct sdhci_host*)param;
  1694. sdhci_card_event(host->mmc);
  1695. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  1696. }
  1697. static void sdhci_tasklet_finish(unsigned long param)
  1698. {
  1699. struct sdhci_host *host;
  1700. unsigned long flags;
  1701. struct mmc_request *mrq;
  1702. host = (struct sdhci_host*)param;
  1703. spin_lock_irqsave(&host->lock, flags);
  1704. /*
  1705. * If this tasklet gets rescheduled while running, it will
  1706. * be run again afterwards but without any active request.
  1707. */
  1708. if (!host->mrq) {
  1709. spin_unlock_irqrestore(&host->lock, flags);
  1710. return;
  1711. }
  1712. del_timer(&host->timer);
  1713. mrq = host->mrq;
  1714. /*
  1715. * The controller needs a reset of internal state machines
  1716. * upon error conditions.
  1717. */
  1718. if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  1719. ((mrq->cmd && mrq->cmd->error) ||
  1720. (mrq->data && (mrq->data->error ||
  1721. (mrq->data->stop && mrq->data->stop->error))) ||
  1722. (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
  1723. /* Some controllers need this kick or reset won't work here */
  1724. if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
  1725. unsigned int clock;
  1726. /* This is to force an update */
  1727. clock = host->clock;
  1728. host->clock = 0;
  1729. sdhci_set_clock(host, clock);
  1730. }
  1731. /* Spec says we should do both at the same time, but Ricoh
  1732. controllers do not like that. */
  1733. sdhci_reset(host, SDHCI_RESET_CMD);
  1734. sdhci_reset(host, SDHCI_RESET_DATA);
  1735. }
  1736. host->mrq = NULL;
  1737. host->cmd = NULL;
  1738. host->data = NULL;
  1739. #ifndef SDHCI_USE_LEDS_CLASS
  1740. sdhci_deactivate_led(host);
  1741. #endif
  1742. mmiowb();
  1743. spin_unlock_irqrestore(&host->lock, flags);
  1744. mmc_request_done(host->mmc, mrq);
  1745. sdhci_runtime_pm_put(host);
  1746. }
  1747. static void sdhci_timeout_timer(unsigned long data)
  1748. {
  1749. struct sdhci_host *host;
  1750. unsigned long flags;
  1751. host = (struct sdhci_host*)data;
  1752. spin_lock_irqsave(&host->lock, flags);
  1753. if (host->mrq) {
  1754. pr_err("%s: Timeout waiting for hardware "
  1755. "interrupt.\n", mmc_hostname(host->mmc));
  1756. sdhci_dumpregs(host);
  1757. if (host->data) {
  1758. host->data->error = -ETIMEDOUT;
  1759. sdhci_finish_data(host);
  1760. } else {
  1761. if (host->cmd)
  1762. host->cmd->error = -ETIMEDOUT;
  1763. else
  1764. host->mrq->cmd->error = -ETIMEDOUT;
  1765. tasklet_schedule(&host->finish_tasklet);
  1766. }
  1767. }
  1768. mmiowb();
  1769. spin_unlock_irqrestore(&host->lock, flags);
  1770. }
  1771. static void sdhci_tuning_timer(unsigned long data)
  1772. {
  1773. struct sdhci_host *host;
  1774. unsigned long flags;
  1775. host = (struct sdhci_host *)data;
  1776. spin_lock_irqsave(&host->lock, flags);
  1777. host->flags |= SDHCI_NEEDS_RETUNING;
  1778. spin_unlock_irqrestore(&host->lock, flags);
  1779. }
  1780. /*****************************************************************************\
  1781. * *
  1782. * Interrupt handling *
  1783. * *
  1784. \*****************************************************************************/
  1785. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
  1786. {
  1787. BUG_ON(intmask == 0);
  1788. if (!host->cmd) {
  1789. pr_err("%s: Got command interrupt 0x%08x even "
  1790. "though no command operation was in progress.\n",
  1791. mmc_hostname(host->mmc), (unsigned)intmask);
  1792. sdhci_dumpregs(host);
  1793. return;
  1794. }
  1795. if (intmask & SDHCI_INT_TIMEOUT)
  1796. host->cmd->error = -ETIMEDOUT;
  1797. else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  1798. SDHCI_INT_INDEX))
  1799. host->cmd->error = -EILSEQ;
  1800. if (host->cmd->error) {
  1801. tasklet_schedule(&host->finish_tasklet);
  1802. return;
  1803. }
  1804. /*
  1805. * The host can send and interrupt when the busy state has
  1806. * ended, allowing us to wait without wasting CPU cycles.
  1807. * Unfortunately this is overloaded on the "data complete"
  1808. * interrupt, so we need to take some care when handling
  1809. * it.
  1810. *
  1811. * Note: The 1.0 specification is a bit ambiguous about this
  1812. * feature so there might be some problems with older
  1813. * controllers.
  1814. */
  1815. if (host->cmd->flags & MMC_RSP_BUSY) {
  1816. if (host->cmd->data)
  1817. DBG("Cannot wait for busy signal when also "
  1818. "doing a data transfer");
  1819. else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
  1820. return;
  1821. /* The controller does not support the end-of-busy IRQ,
  1822. * fall through and take the SDHCI_INT_RESPONSE */
  1823. }
  1824. if (intmask & SDHCI_INT_RESPONSE)
  1825. sdhci_finish_command(host);
  1826. }
  1827. #ifdef CONFIG_MMC_DEBUG
  1828. static void sdhci_show_adma_error(struct sdhci_host *host)
  1829. {
  1830. const char *name = mmc_hostname(host->mmc);
  1831. u8 *desc = host->adma_desc;
  1832. __le32 *dma;
  1833. __le16 *len;
  1834. u8 attr;
  1835. sdhci_dumpregs(host);
  1836. while (true) {
  1837. dma = (__le32 *)(desc + 4);
  1838. len = (__le16 *)(desc + 2);
  1839. attr = *desc;
  1840. DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
  1841. name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
  1842. desc += 8;
  1843. if (attr & 2)
  1844. break;
  1845. }
  1846. }
  1847. #else
  1848. static void sdhci_show_adma_error(struct sdhci_host *host) { }
  1849. #endif
  1850. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  1851. {
  1852. u32 command;
  1853. BUG_ON(intmask == 0);
  1854. /* CMD19 generates _only_ Buffer Read Ready interrupt */
  1855. if (intmask & SDHCI_INT_DATA_AVAIL) {
  1856. command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
  1857. if (command == MMC_SEND_TUNING_BLOCK ||
  1858. command == MMC_SEND_TUNING_BLOCK_HS200) {
  1859. host->tuning_done = 1;
  1860. wake_up(&host->buf_ready_int);
  1861. return;
  1862. }
  1863. }
  1864. if (!host->data) {
  1865. /*
  1866. * The "data complete" interrupt is also used to
  1867. * indicate that a busy state has ended. See comment
  1868. * above in sdhci_cmd_irq().
  1869. */
  1870. if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  1871. if (intmask & SDHCI_INT_DATA_END) {
  1872. sdhci_finish_command(host);
  1873. return;
  1874. }
  1875. }
  1876. pr_err("%s: Got data interrupt 0x%08x even "
  1877. "though no data operation was in progress.\n",
  1878. mmc_hostname(host->mmc), (unsigned)intmask);
  1879. sdhci_dumpregs(host);
  1880. return;
  1881. }
  1882. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  1883. host->data->error = -ETIMEDOUT;
  1884. else if (intmask & SDHCI_INT_DATA_END_BIT)
  1885. host->data->error = -EILSEQ;
  1886. else if ((intmask & SDHCI_INT_DATA_CRC) &&
  1887. SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
  1888. != MMC_BUS_TEST_R)
  1889. host->data->error = -EILSEQ;
  1890. else if (intmask & SDHCI_INT_ADMA_ERROR) {
  1891. pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
  1892. sdhci_show_adma_error(host);
  1893. host->data->error = -EIO;
  1894. if (host->ops->adma_workaround)
  1895. host->ops->adma_workaround(host, intmask);
  1896. }
  1897. if (host->data->error)
  1898. sdhci_finish_data(host);
  1899. else {
  1900. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  1901. sdhci_transfer_pio(host);
  1902. /*
  1903. * We currently don't do anything fancy with DMA
  1904. * boundaries, but as we can't disable the feature
  1905. * we need to at least restart the transfer.
  1906. *
  1907. * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
  1908. * should return a valid address to continue from, but as
  1909. * some controllers are faulty, don't trust them.
  1910. */
  1911. if (intmask & SDHCI_INT_DMA_END) {
  1912. u32 dmastart, dmanow;
  1913. dmastart = sg_dma_address(host->data->sg);
  1914. dmanow = dmastart + host->data->bytes_xfered;
  1915. /*
  1916. * Force update to the next DMA block boundary.
  1917. */
  1918. dmanow = (dmanow &
  1919. ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
  1920. SDHCI_DEFAULT_BOUNDARY_SIZE;
  1921. host->data->bytes_xfered = dmanow - dmastart;
  1922. DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
  1923. " next 0x%08x\n",
  1924. mmc_hostname(host->mmc), dmastart,
  1925. host->data->bytes_xfered, dmanow);
  1926. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  1927. }
  1928. if (intmask & SDHCI_INT_DATA_END) {
  1929. if (host->cmd) {
  1930. /*
  1931. * Data managed to finish before the
  1932. * command completed. Make sure we do
  1933. * things in the proper order.
  1934. */
  1935. host->data_early = 1;
  1936. } else {
  1937. sdhci_finish_data(host);
  1938. }
  1939. }
  1940. }
  1941. }
  1942. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  1943. {
  1944. irqreturn_t result;
  1945. struct sdhci_host *host = dev_id;
  1946. u32 intmask, unexpected = 0;
  1947. int cardint = 0, max_loops = 16;
  1948. spin_lock(&host->lock);
  1949. if (host->runtime_suspended) {
  1950. spin_unlock(&host->lock);
  1951. pr_warning("%s: got irq while runtime suspended\n",
  1952. mmc_hostname(host->mmc));
  1953. return IRQ_HANDLED;
  1954. }
  1955. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  1956. if (!intmask || intmask == 0xffffffff) {
  1957. result = IRQ_NONE;
  1958. goto out;
  1959. }
  1960. again:
  1961. DBG("*** %s got interrupt: 0x%08x\n",
  1962. mmc_hostname(host->mmc), intmask);
  1963. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  1964. u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  1965. SDHCI_CARD_PRESENT;
  1966. /*
  1967. * There is a observation on i.mx esdhc. INSERT bit will be
  1968. * immediately set again when it gets cleared, if a card is
  1969. * inserted. We have to mask the irq to prevent interrupt
  1970. * storm which will freeze the system. And the REMOVE gets
  1971. * the same situation.
  1972. *
  1973. * More testing are needed here to ensure it works for other
  1974. * platforms though.
  1975. */
  1976. sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
  1977. SDHCI_INT_CARD_REMOVE);
  1978. sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
  1979. SDHCI_INT_CARD_INSERT);
  1980. sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
  1981. SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
  1982. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
  1983. tasklet_schedule(&host->card_tasklet);
  1984. }
  1985. if (intmask & SDHCI_INT_CMD_MASK) {
  1986. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
  1987. SDHCI_INT_STATUS);
  1988. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  1989. }
  1990. if (intmask & SDHCI_INT_DATA_MASK) {
  1991. sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
  1992. SDHCI_INT_STATUS);
  1993. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  1994. }
  1995. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  1996. intmask &= ~SDHCI_INT_ERROR;
  1997. if (intmask & SDHCI_INT_BUS_POWER) {
  1998. pr_err("%s: Card is consuming too much power!\n",
  1999. mmc_hostname(host->mmc));
  2000. sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
  2001. }
  2002. intmask &= ~SDHCI_INT_BUS_POWER;
  2003. if (intmask & SDHCI_INT_CARD_INT)
  2004. cardint = 1;
  2005. intmask &= ~SDHCI_INT_CARD_INT;
  2006. if (intmask) {
  2007. unexpected |= intmask;
  2008. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  2009. }
  2010. result = IRQ_HANDLED;
  2011. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  2012. if (intmask && --max_loops)
  2013. goto again;
  2014. out:
  2015. spin_unlock(&host->lock);
  2016. if (unexpected) {
  2017. pr_err("%s: Unexpected interrupt 0x%08x.\n",
  2018. mmc_hostname(host->mmc), unexpected);
  2019. sdhci_dumpregs(host);
  2020. }
  2021. /*
  2022. * We have to delay this as it calls back into the driver.
  2023. */
  2024. if (cardint)
  2025. mmc_signal_sdio_irq(host->mmc);
  2026. return result;
  2027. }
  2028. /*****************************************************************************\
  2029. * *
  2030. * Suspend/resume *
  2031. * *
  2032. \*****************************************************************************/
  2033. #ifdef CONFIG_PM
  2034. int sdhci_suspend_host(struct sdhci_host *host)
  2035. {
  2036. int ret;
  2037. if (host->ops->platform_suspend)
  2038. host->ops->platform_suspend(host);
  2039. sdhci_disable_card_detection(host);
  2040. /* Disable tuning since we are suspending */
  2041. if (host->flags & SDHCI_USING_RETUNING_TIMER) {
  2042. del_timer_sync(&host->tuning_timer);
  2043. host->flags &= ~SDHCI_NEEDS_RETUNING;
  2044. }
  2045. ret = mmc_suspend_host(host->mmc);
  2046. if (ret) {
  2047. if (host->flags & SDHCI_USING_RETUNING_TIMER) {
  2048. host->flags |= SDHCI_NEEDS_RETUNING;
  2049. mod_timer(&host->tuning_timer, jiffies +
  2050. host->tuning_count * HZ);
  2051. }
  2052. sdhci_enable_card_detection(host);
  2053. return ret;
  2054. }
  2055. free_irq(host->irq, host);
  2056. return ret;
  2057. }
  2058. EXPORT_SYMBOL_GPL(sdhci_suspend_host);
  2059. int sdhci_resume_host(struct sdhci_host *host)
  2060. {
  2061. int ret;
  2062. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2063. if (host->ops->enable_dma)
  2064. host->ops->enable_dma(host);
  2065. }
  2066. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  2067. mmc_hostname(host->mmc), host);
  2068. if (ret)
  2069. return ret;
  2070. if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
  2071. (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
  2072. /* Card keeps power but host controller does not */
  2073. sdhci_init(host, 0);
  2074. host->pwr = 0;
  2075. host->clock = 0;
  2076. sdhci_do_set_ios(host, &host->mmc->ios);
  2077. } else {
  2078. sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
  2079. mmiowb();
  2080. }
  2081. ret = mmc_resume_host(host->mmc);
  2082. sdhci_enable_card_detection(host);
  2083. if (host->ops->platform_resume)
  2084. host->ops->platform_resume(host);
  2085. /* Set the re-tuning expiration flag */
  2086. if (host->flags & SDHCI_USING_RETUNING_TIMER)
  2087. host->flags |= SDHCI_NEEDS_RETUNING;
  2088. return ret;
  2089. }
  2090. EXPORT_SYMBOL_GPL(sdhci_resume_host);
  2091. void sdhci_enable_irq_wakeups(struct sdhci_host *host)
  2092. {
  2093. u8 val;
  2094. val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
  2095. val |= SDHCI_WAKE_ON_INT;
  2096. sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
  2097. }
  2098. EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
  2099. #endif /* CONFIG_PM */
  2100. #ifdef CONFIG_PM_RUNTIME
  2101. static int sdhci_runtime_pm_get(struct sdhci_host *host)
  2102. {
  2103. return pm_runtime_get_sync(host->mmc->parent);
  2104. }
  2105. static int sdhci_runtime_pm_put(struct sdhci_host *host)
  2106. {
  2107. pm_runtime_mark_last_busy(host->mmc->parent);
  2108. return pm_runtime_put_autosuspend(host->mmc->parent);
  2109. }
  2110. int sdhci_runtime_suspend_host(struct sdhci_host *host)
  2111. {
  2112. unsigned long flags;
  2113. int ret = 0;
  2114. /* Disable tuning since we are suspending */
  2115. if (host->flags & SDHCI_USING_RETUNING_TIMER) {
  2116. del_timer_sync(&host->tuning_timer);
  2117. host->flags &= ~SDHCI_NEEDS_RETUNING;
  2118. }
  2119. spin_lock_irqsave(&host->lock, flags);
  2120. sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
  2121. spin_unlock_irqrestore(&host->lock, flags);
  2122. synchronize_irq(host->irq);
  2123. spin_lock_irqsave(&host->lock, flags);
  2124. host->runtime_suspended = true;
  2125. spin_unlock_irqrestore(&host->lock, flags);
  2126. return ret;
  2127. }
  2128. EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
  2129. int sdhci_runtime_resume_host(struct sdhci_host *host)
  2130. {
  2131. unsigned long flags;
  2132. int ret = 0, host_flags = host->flags;
  2133. if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2134. if (host->ops->enable_dma)
  2135. host->ops->enable_dma(host);
  2136. }
  2137. sdhci_init(host, 0);
  2138. /* Force clock and power re-program */
  2139. host->pwr = 0;
  2140. host->clock = 0;
  2141. sdhci_do_set_ios(host, &host->mmc->ios);
  2142. sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
  2143. if (host_flags & SDHCI_PV_ENABLED)
  2144. sdhci_do_enable_preset_value(host, true);
  2145. /* Set the re-tuning expiration flag */
  2146. if (host->flags & SDHCI_USING_RETUNING_TIMER)
  2147. host->flags |= SDHCI_NEEDS_RETUNING;
  2148. spin_lock_irqsave(&host->lock, flags);
  2149. host->runtime_suspended = false;
  2150. /* Enable SDIO IRQ */
  2151. if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
  2152. sdhci_enable_sdio_irq_nolock(host, true);
  2153. /* Enable Card Detection */
  2154. sdhci_enable_card_detection(host);
  2155. spin_unlock_irqrestore(&host->lock, flags);
  2156. return ret;
  2157. }
  2158. EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
  2159. #endif
  2160. /*****************************************************************************\
  2161. * *
  2162. * Device allocation/registration *
  2163. * *
  2164. \*****************************************************************************/
  2165. struct sdhci_host *sdhci_alloc_host(struct device *dev,
  2166. size_t priv_size)
  2167. {
  2168. struct mmc_host *mmc;
  2169. struct sdhci_host *host;
  2170. WARN_ON(dev == NULL);
  2171. mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
  2172. if (!mmc)
  2173. return ERR_PTR(-ENOMEM);
  2174. host = mmc_priv(mmc);
  2175. host->mmc = mmc;
  2176. return host;
  2177. }
  2178. EXPORT_SYMBOL_GPL(sdhci_alloc_host);
  2179. int sdhci_add_host(struct sdhci_host *host)
  2180. {
  2181. struct mmc_host *mmc;
  2182. u32 caps[2] = {0, 0};
  2183. u32 max_current_caps;
  2184. unsigned int ocr_avail;
  2185. int ret;
  2186. WARN_ON(host == NULL);
  2187. if (host == NULL)
  2188. return -EINVAL;
  2189. mmc = host->mmc;
  2190. if (debug_quirks)
  2191. host->quirks = debug_quirks;
  2192. if (debug_quirks2)
  2193. host->quirks2 = debug_quirks2;
  2194. sdhci_reset(host, SDHCI_RESET_ALL);
  2195. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  2196. host->version = (host->version & SDHCI_SPEC_VER_MASK)
  2197. >> SDHCI_SPEC_VER_SHIFT;
  2198. if (host->version > SDHCI_SPEC_300) {
  2199. pr_err("%s: Unknown controller version (%d). "
  2200. "You may experience problems.\n", mmc_hostname(mmc),
  2201. host->version);
  2202. }
  2203. caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
  2204. sdhci_readl(host, SDHCI_CAPABILITIES);
  2205. if (host->version >= SDHCI_SPEC_300)
  2206. caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
  2207. host->caps1 :
  2208. sdhci_readl(host, SDHCI_CAPABILITIES_1);
  2209. if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
  2210. host->flags |= SDHCI_USE_SDMA;
  2211. else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
  2212. DBG("Controller doesn't have SDMA capability\n");
  2213. else
  2214. host->flags |= SDHCI_USE_SDMA;
  2215. if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
  2216. (host->flags & SDHCI_USE_SDMA)) {
  2217. DBG("Disabling DMA as it is marked broken\n");
  2218. host->flags &= ~SDHCI_USE_SDMA;
  2219. }
  2220. if ((host->version >= SDHCI_SPEC_200) &&
  2221. (caps[0] & SDHCI_CAN_DO_ADMA2))
  2222. host->flags |= SDHCI_USE_ADMA;
  2223. if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
  2224. (host->flags & SDHCI_USE_ADMA)) {
  2225. DBG("Disabling ADMA as it is marked broken\n");
  2226. host->flags &= ~SDHCI_USE_ADMA;
  2227. }
  2228. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2229. if (host->ops->enable_dma) {
  2230. if (host->ops->enable_dma(host)) {
  2231. pr_warning("%s: No suitable DMA "
  2232. "available. Falling back to PIO.\n",
  2233. mmc_hostname(mmc));
  2234. host->flags &=
  2235. ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  2236. }
  2237. }
  2238. }
  2239. if (host->flags & SDHCI_USE_ADMA) {
  2240. /*
  2241. * We need to allocate descriptors for all sg entries
  2242. * (128) and potentially one alignment transfer for
  2243. * each of those entries.
  2244. */
  2245. host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
  2246. host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
  2247. if (!host->adma_desc || !host->align_buffer) {
  2248. kfree(host->adma_desc);
  2249. kfree(host->align_buffer);
  2250. pr_warning("%s: Unable to allocate ADMA "
  2251. "buffers. Falling back to standard DMA.\n",
  2252. mmc_hostname(mmc));
  2253. host->flags &= ~SDHCI_USE_ADMA;
  2254. }
  2255. }
  2256. /*
  2257. * If we use DMA, then it's up to the caller to set the DMA
  2258. * mask, but PIO does not need the hw shim so we set a new
  2259. * mask here in that case.
  2260. */
  2261. if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
  2262. host->dma_mask = DMA_BIT_MASK(64);
  2263. mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
  2264. }
  2265. if (host->version >= SDHCI_SPEC_300)
  2266. host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
  2267. >> SDHCI_CLOCK_BASE_SHIFT;
  2268. else
  2269. host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
  2270. >> SDHCI_CLOCK_BASE_SHIFT;
  2271. host->max_clk *= 1000000;
  2272. if (host->max_clk == 0 || host->quirks &
  2273. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
  2274. if (!host->ops->get_max_clock) {
  2275. pr_err("%s: Hardware doesn't specify base clock "
  2276. "frequency.\n", mmc_hostname(mmc));
  2277. return -ENODEV;
  2278. }
  2279. host->max_clk = host->ops->get_max_clock(host);
  2280. }
  2281. /*
  2282. * In case of Host Controller v3.00, find out whether clock
  2283. * multiplier is supported.
  2284. */
  2285. host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
  2286. SDHCI_CLOCK_MUL_SHIFT;
  2287. /*
  2288. * In case the value in Clock Multiplier is 0, then programmable
  2289. * clock mode is not supported, otherwise the actual clock
  2290. * multiplier is one more than the value of Clock Multiplier
  2291. * in the Capabilities Register.
  2292. */
  2293. if (host->clk_mul)
  2294. host->clk_mul += 1;
  2295. /*
  2296. * Set host parameters.
  2297. */
  2298. mmc->ops = &sdhci_ops;
  2299. mmc->f_max = host->max_clk;
  2300. if (host->ops->get_min_clock)
  2301. mmc->f_min = host->ops->get_min_clock(host);
  2302. else if (host->version >= SDHCI_SPEC_300) {
  2303. if (host->clk_mul) {
  2304. mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
  2305. mmc->f_max = host->max_clk * host->clk_mul;
  2306. } else
  2307. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  2308. } else
  2309. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
  2310. host->timeout_clk =
  2311. (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
  2312. if (host->timeout_clk == 0) {
  2313. if (host->ops->get_timeout_clock) {
  2314. host->timeout_clk = host->ops->get_timeout_clock(host);
  2315. } else if (!(host->quirks &
  2316. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
  2317. pr_err("%s: Hardware doesn't specify timeout clock "
  2318. "frequency.\n", mmc_hostname(mmc));
  2319. return -ENODEV;
  2320. }
  2321. }
  2322. if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
  2323. host->timeout_clk *= 1000;
  2324. if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
  2325. host->timeout_clk = mmc->f_max / 1000;
  2326. mmc->max_discard_to = (1 << 27) / host->timeout_clk;
  2327. mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
  2328. if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
  2329. host->flags |= SDHCI_AUTO_CMD12;
  2330. /* Auto-CMD23 stuff only works in ADMA or PIO. */
  2331. if ((host->version >= SDHCI_SPEC_300) &&
  2332. ((host->flags & SDHCI_USE_ADMA) ||
  2333. !(host->flags & SDHCI_USE_SDMA))) {
  2334. host->flags |= SDHCI_AUTO_CMD23;
  2335. DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
  2336. } else {
  2337. DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
  2338. }
  2339. /*
  2340. * A controller may support 8-bit width, but the board itself
  2341. * might not have the pins brought out. Boards that support
  2342. * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
  2343. * their platform code before calling sdhci_add_host(), and we
  2344. * won't assume 8-bit width for hosts without that CAP.
  2345. */
  2346. if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
  2347. mmc->caps |= MMC_CAP_4_BIT_DATA;
  2348. if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
  2349. mmc->caps &= ~MMC_CAP_CMD23;
  2350. if (caps[0] & SDHCI_CAN_DO_HISPD)
  2351. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  2352. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
  2353. !(host->mmc->caps & MMC_CAP_NONREMOVABLE))
  2354. mmc->caps |= MMC_CAP_NEEDS_POLL;
  2355. /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
  2356. host->vqmmc = regulator_get(mmc_dev(mmc), "vqmmc");
  2357. if (IS_ERR_OR_NULL(host->vqmmc)) {
  2358. if (PTR_ERR(host->vqmmc) < 0) {
  2359. pr_info("%s: no vqmmc regulator found\n",
  2360. mmc_hostname(mmc));
  2361. host->vqmmc = NULL;
  2362. }
  2363. } else {
  2364. regulator_enable(host->vqmmc);
  2365. if (!regulator_is_supported_voltage(host->vqmmc, 1700000,
  2366. 1950000))
  2367. caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
  2368. SDHCI_SUPPORT_SDR50 |
  2369. SDHCI_SUPPORT_DDR50);
  2370. }
  2371. if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
  2372. caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
  2373. SDHCI_SUPPORT_DDR50);
  2374. /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
  2375. if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
  2376. SDHCI_SUPPORT_DDR50))
  2377. mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
  2378. /* SDR104 supports also implies SDR50 support */
  2379. if (caps[1] & SDHCI_SUPPORT_SDR104)
  2380. mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
  2381. else if (caps[1] & SDHCI_SUPPORT_SDR50)
  2382. mmc->caps |= MMC_CAP_UHS_SDR50;
  2383. if (caps[1] & SDHCI_SUPPORT_DDR50)
  2384. mmc->caps |= MMC_CAP_UHS_DDR50;
  2385. /* Does the host need tuning for SDR50? */
  2386. if (caps[1] & SDHCI_USE_SDR50_TUNING)
  2387. host->flags |= SDHCI_SDR50_NEEDS_TUNING;
  2388. /* Does the host need tuning for HS200? */
  2389. if (mmc->caps2 & MMC_CAP2_HS200)
  2390. host->flags |= SDHCI_HS200_NEEDS_TUNING;
  2391. /* Driver Type(s) (A, C, D) supported by the host */
  2392. if (caps[1] & SDHCI_DRIVER_TYPE_A)
  2393. mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
  2394. if (caps[1] & SDHCI_DRIVER_TYPE_C)
  2395. mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
  2396. if (caps[1] & SDHCI_DRIVER_TYPE_D)
  2397. mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
  2398. /* Initial value for re-tuning timer count */
  2399. host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
  2400. SDHCI_RETUNING_TIMER_COUNT_SHIFT;
  2401. /*
  2402. * In case Re-tuning Timer is not disabled, the actual value of
  2403. * re-tuning timer will be 2 ^ (n - 1).
  2404. */
  2405. if (host->tuning_count)
  2406. host->tuning_count = 1 << (host->tuning_count - 1);
  2407. /* Re-tuning mode supported by the Host Controller */
  2408. host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
  2409. SDHCI_RETUNING_MODE_SHIFT;
  2410. ocr_avail = 0;
  2411. host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
  2412. if (IS_ERR_OR_NULL(host->vmmc)) {
  2413. if (PTR_ERR(host->vmmc) < 0) {
  2414. pr_info("%s: no vmmc regulator found\n",
  2415. mmc_hostname(mmc));
  2416. host->vmmc = NULL;
  2417. }
  2418. }
  2419. #ifdef CONFIG_REGULATOR
  2420. if (host->vmmc) {
  2421. ret = regulator_is_supported_voltage(host->vmmc, 2700000,
  2422. 3600000);
  2423. if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_330)))
  2424. caps[0] &= ~SDHCI_CAN_VDD_330;
  2425. if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_300)))
  2426. caps[0] &= ~SDHCI_CAN_VDD_300;
  2427. ret = regulator_is_supported_voltage(host->vmmc, 1700000,
  2428. 1950000);
  2429. if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_180)))
  2430. caps[0] &= ~SDHCI_CAN_VDD_180;
  2431. }
  2432. #endif /* CONFIG_REGULATOR */
  2433. /*
  2434. * According to SD Host Controller spec v3.00, if the Host System
  2435. * can afford more than 150mA, Host Driver should set XPC to 1. Also
  2436. * the value is meaningful only if Voltage Support in the Capabilities
  2437. * register is set. The actual current value is 4 times the register
  2438. * value.
  2439. */
  2440. max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
  2441. if (!max_current_caps && host->vmmc) {
  2442. u32 curr = regulator_get_current_limit(host->vmmc);
  2443. if (curr > 0) {
  2444. /* convert to SDHCI_MAX_CURRENT format */
  2445. curr = curr/1000; /* convert to mA */
  2446. curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
  2447. curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
  2448. max_current_caps =
  2449. (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
  2450. (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
  2451. (curr << SDHCI_MAX_CURRENT_180_SHIFT);
  2452. }
  2453. }
  2454. if (caps[0] & SDHCI_CAN_VDD_330) {
  2455. ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
  2456. mmc->max_current_330 = ((max_current_caps &
  2457. SDHCI_MAX_CURRENT_330_MASK) >>
  2458. SDHCI_MAX_CURRENT_330_SHIFT) *
  2459. SDHCI_MAX_CURRENT_MULTIPLIER;
  2460. }
  2461. if (caps[0] & SDHCI_CAN_VDD_300) {
  2462. ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
  2463. mmc->max_current_300 = ((max_current_caps &
  2464. SDHCI_MAX_CURRENT_300_MASK) >>
  2465. SDHCI_MAX_CURRENT_300_SHIFT) *
  2466. SDHCI_MAX_CURRENT_MULTIPLIER;
  2467. }
  2468. if (caps[0] & SDHCI_CAN_VDD_180) {
  2469. ocr_avail |= MMC_VDD_165_195;
  2470. mmc->max_current_180 = ((max_current_caps &
  2471. SDHCI_MAX_CURRENT_180_MASK) >>
  2472. SDHCI_MAX_CURRENT_180_SHIFT) *
  2473. SDHCI_MAX_CURRENT_MULTIPLIER;
  2474. }
  2475. mmc->ocr_avail = ocr_avail;
  2476. mmc->ocr_avail_sdio = ocr_avail;
  2477. if (host->ocr_avail_sdio)
  2478. mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
  2479. mmc->ocr_avail_sd = ocr_avail;
  2480. if (host->ocr_avail_sd)
  2481. mmc->ocr_avail_sd &= host->ocr_avail_sd;
  2482. else /* normal SD controllers don't support 1.8V */
  2483. mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
  2484. mmc->ocr_avail_mmc = ocr_avail;
  2485. if (host->ocr_avail_mmc)
  2486. mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
  2487. if (mmc->ocr_avail == 0) {
  2488. pr_err("%s: Hardware doesn't report any "
  2489. "support voltages.\n", mmc_hostname(mmc));
  2490. return -ENODEV;
  2491. }
  2492. spin_lock_init(&host->lock);
  2493. /*
  2494. * Maximum number of segments. Depends on if the hardware
  2495. * can do scatter/gather or not.
  2496. */
  2497. if (host->flags & SDHCI_USE_ADMA)
  2498. mmc->max_segs = 128;
  2499. else if (host->flags & SDHCI_USE_SDMA)
  2500. mmc->max_segs = 1;
  2501. else /* PIO */
  2502. mmc->max_segs = 128;
  2503. /*
  2504. * Maximum number of sectors in one transfer. Limited by DMA boundary
  2505. * size (512KiB).
  2506. */
  2507. mmc->max_req_size = 524288;
  2508. /*
  2509. * Maximum segment size. Could be one segment with the maximum number
  2510. * of bytes. When doing hardware scatter/gather, each entry cannot
  2511. * be larger than 64 KiB though.
  2512. */
  2513. if (host->flags & SDHCI_USE_ADMA) {
  2514. if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
  2515. mmc->max_seg_size = 65535;
  2516. else
  2517. mmc->max_seg_size = 65536;
  2518. } else {
  2519. mmc->max_seg_size = mmc->max_req_size;
  2520. }
  2521. /*
  2522. * Maximum block size. This varies from controller to controller and
  2523. * is specified in the capabilities register.
  2524. */
  2525. if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
  2526. mmc->max_blk_size = 2;
  2527. } else {
  2528. mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
  2529. SDHCI_MAX_BLOCK_SHIFT;
  2530. if (mmc->max_blk_size >= 3) {
  2531. pr_warning("%s: Invalid maximum block size, "
  2532. "assuming 512 bytes\n", mmc_hostname(mmc));
  2533. mmc->max_blk_size = 0;
  2534. }
  2535. }
  2536. mmc->max_blk_size = 512 << mmc->max_blk_size;
  2537. /*
  2538. * Maximum block count.
  2539. */
  2540. mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
  2541. /*
  2542. * Init tasklets.
  2543. */
  2544. tasklet_init(&host->card_tasklet,
  2545. sdhci_tasklet_card, (unsigned long)host);
  2546. tasklet_init(&host->finish_tasklet,
  2547. sdhci_tasklet_finish, (unsigned long)host);
  2548. setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
  2549. if (host->version >= SDHCI_SPEC_300) {
  2550. init_waitqueue_head(&host->buf_ready_int);
  2551. /* Initialize re-tuning timer */
  2552. init_timer(&host->tuning_timer);
  2553. host->tuning_timer.data = (unsigned long)host;
  2554. host->tuning_timer.function = sdhci_tuning_timer;
  2555. }
  2556. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  2557. mmc_hostname(mmc), host);
  2558. if (ret) {
  2559. pr_err("%s: Failed to request IRQ %d: %d\n",
  2560. mmc_hostname(mmc), host->irq, ret);
  2561. goto untasklet;
  2562. }
  2563. sdhci_init(host, 0);
  2564. #ifdef CONFIG_MMC_DEBUG
  2565. sdhci_dumpregs(host);
  2566. #endif
  2567. #ifdef SDHCI_USE_LEDS_CLASS
  2568. snprintf(host->led_name, sizeof(host->led_name),
  2569. "%s::", mmc_hostname(mmc));
  2570. host->led.name = host->led_name;
  2571. host->led.brightness = LED_OFF;
  2572. host->led.default_trigger = mmc_hostname(mmc);
  2573. host->led.brightness_set = sdhci_led_control;
  2574. ret = led_classdev_register(mmc_dev(mmc), &host->led);
  2575. if (ret) {
  2576. pr_err("%s: Failed to register LED device: %d\n",
  2577. mmc_hostname(mmc), ret);
  2578. goto reset;
  2579. }
  2580. #endif
  2581. mmiowb();
  2582. mmc_add_host(mmc);
  2583. pr_info("%s: SDHCI controller on %s [%s] using %s\n",
  2584. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  2585. (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
  2586. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  2587. sdhci_enable_card_detection(host);
  2588. return 0;
  2589. #ifdef SDHCI_USE_LEDS_CLASS
  2590. reset:
  2591. sdhci_reset(host, SDHCI_RESET_ALL);
  2592. free_irq(host->irq, host);
  2593. #endif
  2594. untasklet:
  2595. tasklet_kill(&host->card_tasklet);
  2596. tasklet_kill(&host->finish_tasklet);
  2597. return ret;
  2598. }
  2599. EXPORT_SYMBOL_GPL(sdhci_add_host);
  2600. void sdhci_remove_host(struct sdhci_host *host, int dead)
  2601. {
  2602. unsigned long flags;
  2603. if (dead) {
  2604. spin_lock_irqsave(&host->lock, flags);
  2605. host->flags |= SDHCI_DEVICE_DEAD;
  2606. if (host->mrq) {
  2607. pr_err("%s: Controller removed during "
  2608. " transfer!\n", mmc_hostname(host->mmc));
  2609. host->mrq->cmd->error = -ENOMEDIUM;
  2610. tasklet_schedule(&host->finish_tasklet);
  2611. }
  2612. spin_unlock_irqrestore(&host->lock, flags);
  2613. }
  2614. sdhci_disable_card_detection(host);
  2615. mmc_remove_host(host->mmc);
  2616. #ifdef SDHCI_USE_LEDS_CLASS
  2617. led_classdev_unregister(&host->led);
  2618. #endif
  2619. if (!dead)
  2620. sdhci_reset(host, SDHCI_RESET_ALL);
  2621. free_irq(host->irq, host);
  2622. del_timer_sync(&host->timer);
  2623. tasklet_kill(&host->card_tasklet);
  2624. tasklet_kill(&host->finish_tasklet);
  2625. if (host->vmmc) {
  2626. regulator_disable(host->vmmc);
  2627. regulator_put(host->vmmc);
  2628. }
  2629. if (host->vqmmc) {
  2630. regulator_disable(host->vqmmc);
  2631. regulator_put(host->vqmmc);
  2632. }
  2633. kfree(host->adma_desc);
  2634. kfree(host->align_buffer);
  2635. host->adma_desc = NULL;
  2636. host->align_buffer = NULL;
  2637. }
  2638. EXPORT_SYMBOL_GPL(sdhci_remove_host);
  2639. void sdhci_free_host(struct sdhci_host *host)
  2640. {
  2641. mmc_free_host(host->mmc);
  2642. }
  2643. EXPORT_SYMBOL_GPL(sdhci_free_host);
  2644. /*****************************************************************************\
  2645. * *
  2646. * Driver init/exit *
  2647. * *
  2648. \*****************************************************************************/
  2649. static int __init sdhci_drv_init(void)
  2650. {
  2651. pr_info(DRIVER_NAME
  2652. ": Secure Digital Host Controller Interface driver\n");
  2653. pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  2654. return 0;
  2655. }
  2656. static void __exit sdhci_drv_exit(void)
  2657. {
  2658. }
  2659. module_init(sdhci_drv_init);
  2660. module_exit(sdhci_drv_exit);
  2661. module_param(debug_quirks, uint, 0444);
  2662. module_param(debug_quirks2, uint, 0444);
  2663. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  2664. MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
  2665. MODULE_LICENSE("GPL");
  2666. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
  2667. MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");