sdhci.c 77 KB

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