sdhci.c 79 KB

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