sdhci.c 73 KB

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