sdhci.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  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/dma-mapping.h>
  19. #include <linux/slab.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/leds.h>
  23. #include <linux/mmc/host.h>
  24. #include "sdhci.h"
  25. #define DRIVER_NAME "sdhci"
  26. #define DBG(f, x...) \
  27. pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
  28. #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
  29. defined(CONFIG_MMC_SDHCI_MODULE))
  30. #define SDHCI_USE_LEDS_CLASS
  31. #endif
  32. static unsigned int debug_quirks = 0;
  33. static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
  34. static void sdhci_finish_data(struct sdhci_host *);
  35. static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
  36. static void sdhci_finish_command(struct sdhci_host *);
  37. static void sdhci_dumpregs(struct sdhci_host *host)
  38. {
  39. printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
  40. printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
  41. sdhci_readl(host, SDHCI_DMA_ADDRESS),
  42. sdhci_readw(host, SDHCI_HOST_VERSION));
  43. printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  44. sdhci_readw(host, SDHCI_BLOCK_SIZE),
  45. sdhci_readw(host, SDHCI_BLOCK_COUNT));
  46. printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
  47. sdhci_readl(host, SDHCI_ARGUMENT),
  48. sdhci_readw(host, SDHCI_TRANSFER_MODE));
  49. printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
  50. sdhci_readl(host, SDHCI_PRESENT_STATE),
  51. sdhci_readb(host, SDHCI_HOST_CONTROL));
  52. printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
  53. sdhci_readb(host, SDHCI_POWER_CONTROL),
  54. sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
  55. printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
  56. sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
  57. sdhci_readw(host, SDHCI_CLOCK_CONTROL));
  58. printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
  59. sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
  60. sdhci_readl(host, SDHCI_INT_STATUS));
  61. printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
  62. sdhci_readl(host, SDHCI_INT_ENABLE),
  63. sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
  64. printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
  65. sdhci_readw(host, SDHCI_ACMD12_ERR),
  66. sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
  67. printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
  68. sdhci_readl(host, SDHCI_CAPABILITIES),
  69. sdhci_readl(host, SDHCI_MAX_CURRENT));
  70. if (host->flags & SDHCI_USE_ADMA)
  71. printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
  72. readl(host->ioaddr + SDHCI_ADMA_ERROR),
  73. readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
  74. printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
  75. }
  76. /*****************************************************************************\
  77. * *
  78. * Low level functions *
  79. * *
  80. \*****************************************************************************/
  81. static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
  82. {
  83. u32 ier;
  84. ier = sdhci_readl(host, SDHCI_INT_ENABLE);
  85. ier &= ~clear;
  86. ier |= set;
  87. sdhci_writel(host, ier, SDHCI_INT_ENABLE);
  88. sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  89. }
  90. static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
  91. {
  92. sdhci_clear_set_irqs(host, 0, irqs);
  93. }
  94. static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
  95. {
  96. sdhci_clear_set_irqs(host, irqs, 0);
  97. }
  98. static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
  99. {
  100. u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
  101. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  102. return;
  103. if (enable)
  104. sdhci_unmask_irqs(host, irqs);
  105. else
  106. sdhci_mask_irqs(host, irqs);
  107. }
  108. static void sdhci_enable_card_detection(struct sdhci_host *host)
  109. {
  110. sdhci_set_card_detection(host, true);
  111. }
  112. static void sdhci_disable_card_detection(struct sdhci_host *host)
  113. {
  114. sdhci_set_card_detection(host, false);
  115. }
  116. static void sdhci_reset(struct sdhci_host *host, u8 mask)
  117. {
  118. unsigned long timeout;
  119. u32 uninitialized_var(ier);
  120. if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
  121. if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
  122. SDHCI_CARD_PRESENT))
  123. return;
  124. }
  125. if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
  126. ier = sdhci_readl(host, SDHCI_INT_ENABLE);
  127. sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  128. if (mask & SDHCI_RESET_ALL)
  129. host->clock = 0;
  130. /* Wait max 100 ms */
  131. timeout = 100;
  132. /* hw clears the bit when it's done */
  133. while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
  134. if (timeout == 0) {
  135. printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
  136. mmc_hostname(host->mmc), (int)mask);
  137. sdhci_dumpregs(host);
  138. return;
  139. }
  140. timeout--;
  141. mdelay(1);
  142. }
  143. if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
  144. sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
  145. }
  146. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
  147. static void sdhci_init(struct sdhci_host *host, int soft)
  148. {
  149. if (soft)
  150. sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
  151. else
  152. sdhci_reset(host, SDHCI_RESET_ALL);
  153. sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
  154. SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
  155. SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
  156. SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
  157. SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
  158. if (soft) {
  159. /* force clock reconfiguration */
  160. host->clock = 0;
  161. sdhci_set_ios(host->mmc, &host->mmc->ios);
  162. }
  163. }
  164. static void sdhci_reinit(struct sdhci_host *host)
  165. {
  166. sdhci_init(host, 0);
  167. sdhci_enable_card_detection(host);
  168. }
  169. static void sdhci_activate_led(struct sdhci_host *host)
  170. {
  171. u8 ctrl;
  172. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  173. ctrl |= SDHCI_CTRL_LED;
  174. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  175. }
  176. static void sdhci_deactivate_led(struct sdhci_host *host)
  177. {
  178. u8 ctrl;
  179. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  180. ctrl &= ~SDHCI_CTRL_LED;
  181. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  182. }
  183. #ifdef SDHCI_USE_LEDS_CLASS
  184. static void sdhci_led_control(struct led_classdev *led,
  185. enum led_brightness brightness)
  186. {
  187. struct sdhci_host *host = container_of(led, struct sdhci_host, led);
  188. unsigned long flags;
  189. spin_lock_irqsave(&host->lock, flags);
  190. if (brightness == LED_OFF)
  191. sdhci_deactivate_led(host);
  192. else
  193. sdhci_activate_led(host);
  194. spin_unlock_irqrestore(&host->lock, flags);
  195. }
  196. #endif
  197. /*****************************************************************************\
  198. * *
  199. * Core functions *
  200. * *
  201. \*****************************************************************************/
  202. static void sdhci_read_block_pio(struct sdhci_host *host)
  203. {
  204. unsigned long flags;
  205. size_t blksize, len, chunk;
  206. u32 uninitialized_var(scratch);
  207. u8 *buf;
  208. DBG("PIO reading\n");
  209. blksize = host->data->blksz;
  210. chunk = 0;
  211. local_irq_save(flags);
  212. while (blksize) {
  213. if (!sg_miter_next(&host->sg_miter))
  214. BUG();
  215. len = min(host->sg_miter.length, blksize);
  216. blksize -= len;
  217. host->sg_miter.consumed = len;
  218. buf = host->sg_miter.addr;
  219. while (len) {
  220. if (chunk == 0) {
  221. scratch = sdhci_readl(host, SDHCI_BUFFER);
  222. chunk = 4;
  223. }
  224. *buf = scratch & 0xFF;
  225. buf++;
  226. scratch >>= 8;
  227. chunk--;
  228. len--;
  229. }
  230. }
  231. sg_miter_stop(&host->sg_miter);
  232. local_irq_restore(flags);
  233. }
  234. static void sdhci_write_block_pio(struct sdhci_host *host)
  235. {
  236. unsigned long flags;
  237. size_t blksize, len, chunk;
  238. u32 scratch;
  239. u8 *buf;
  240. DBG("PIO writing\n");
  241. blksize = host->data->blksz;
  242. chunk = 0;
  243. scratch = 0;
  244. local_irq_save(flags);
  245. while (blksize) {
  246. if (!sg_miter_next(&host->sg_miter))
  247. BUG();
  248. len = min(host->sg_miter.length, blksize);
  249. blksize -= len;
  250. host->sg_miter.consumed = len;
  251. buf = host->sg_miter.addr;
  252. while (len) {
  253. scratch |= (u32)*buf << (chunk * 8);
  254. buf++;
  255. chunk++;
  256. len--;
  257. if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
  258. sdhci_writel(host, scratch, SDHCI_BUFFER);
  259. chunk = 0;
  260. scratch = 0;
  261. }
  262. }
  263. }
  264. sg_miter_stop(&host->sg_miter);
  265. local_irq_restore(flags);
  266. }
  267. static void sdhci_transfer_pio(struct sdhci_host *host)
  268. {
  269. u32 mask;
  270. BUG_ON(!host->data);
  271. if (host->blocks == 0)
  272. return;
  273. if (host->data->flags & MMC_DATA_READ)
  274. mask = SDHCI_DATA_AVAILABLE;
  275. else
  276. mask = SDHCI_SPACE_AVAILABLE;
  277. /*
  278. * Some controllers (JMicron JMB38x) mess up the buffer bits
  279. * for transfers < 4 bytes. As long as it is just one block,
  280. * we can ignore the bits.
  281. */
  282. if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
  283. (host->data->blocks == 1))
  284. mask = ~0;
  285. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  286. if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
  287. udelay(100);
  288. if (host->data->flags & MMC_DATA_READ)
  289. sdhci_read_block_pio(host);
  290. else
  291. sdhci_write_block_pio(host);
  292. host->blocks--;
  293. if (host->blocks == 0)
  294. break;
  295. }
  296. DBG("PIO transfer complete.\n");
  297. }
  298. static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
  299. {
  300. local_irq_save(*flags);
  301. return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
  302. }
  303. static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
  304. {
  305. kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
  306. local_irq_restore(*flags);
  307. }
  308. static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
  309. {
  310. __le32 *dataddr = (__le32 __force *)(desc + 4);
  311. __le16 *cmdlen = (__le16 __force *)desc;
  312. /* SDHCI specification says ADMA descriptors should be 4 byte
  313. * aligned, so using 16 or 32bit operations should be safe. */
  314. cmdlen[0] = cpu_to_le16(cmd);
  315. cmdlen[1] = cpu_to_le16(len);
  316. dataddr[0] = cpu_to_le32(addr);
  317. }
  318. static int sdhci_adma_table_pre(struct sdhci_host *host,
  319. struct mmc_data *data)
  320. {
  321. int direction;
  322. u8 *desc;
  323. u8 *align;
  324. dma_addr_t addr;
  325. dma_addr_t align_addr;
  326. int len, offset;
  327. struct scatterlist *sg;
  328. int i;
  329. char *buffer;
  330. unsigned long flags;
  331. /*
  332. * The spec does not specify endianness of descriptor table.
  333. * We currently guess that it is LE.
  334. */
  335. if (data->flags & MMC_DATA_READ)
  336. direction = DMA_FROM_DEVICE;
  337. else
  338. direction = DMA_TO_DEVICE;
  339. /*
  340. * The ADMA descriptor table is mapped further down as we
  341. * need to fill it with data first.
  342. */
  343. host->align_addr = dma_map_single(mmc_dev(host->mmc),
  344. host->align_buffer, 128 * 4, direction);
  345. if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
  346. goto fail;
  347. BUG_ON(host->align_addr & 0x3);
  348. host->sg_count = dma_map_sg(mmc_dev(host->mmc),
  349. data->sg, data->sg_len, direction);
  350. if (host->sg_count == 0)
  351. goto unmap_align;
  352. desc = host->adma_desc;
  353. align = host->align_buffer;
  354. align_addr = host->align_addr;
  355. for_each_sg(data->sg, sg, host->sg_count, i) {
  356. addr = sg_dma_address(sg);
  357. len = sg_dma_len(sg);
  358. /*
  359. * The SDHCI specification states that ADMA
  360. * addresses must be 32-bit aligned. If they
  361. * aren't, then we use a bounce buffer for
  362. * the (up to three) bytes that screw up the
  363. * alignment.
  364. */
  365. offset = (4 - (addr & 0x3)) & 0x3;
  366. if (offset) {
  367. if (data->flags & MMC_DATA_WRITE) {
  368. buffer = sdhci_kmap_atomic(sg, &flags);
  369. WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
  370. memcpy(align, buffer, offset);
  371. sdhci_kunmap_atomic(buffer, &flags);
  372. }
  373. /* tran, valid */
  374. sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
  375. BUG_ON(offset > 65536);
  376. align += 4;
  377. align_addr += 4;
  378. desc += 8;
  379. addr += offset;
  380. len -= offset;
  381. }
  382. BUG_ON(len > 65536);
  383. /* tran, valid */
  384. sdhci_set_adma_desc(desc, addr, len, 0x21);
  385. desc += 8;
  386. /*
  387. * If this triggers then we have a calculation bug
  388. * somewhere. :/
  389. */
  390. WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
  391. }
  392. if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
  393. /*
  394. * Mark the last descriptor as the terminating descriptor
  395. */
  396. if (desc != host->adma_desc) {
  397. desc -= 8;
  398. desc[0] |= 0x2; /* end */
  399. }
  400. } else {
  401. /*
  402. * Add a terminating entry.
  403. */
  404. /* nop, end, valid */
  405. sdhci_set_adma_desc(desc, 0, 0, 0x3);
  406. }
  407. /*
  408. * Resync align buffer as we might have changed it.
  409. */
  410. if (data->flags & MMC_DATA_WRITE) {
  411. dma_sync_single_for_device(mmc_dev(host->mmc),
  412. host->align_addr, 128 * 4, direction);
  413. }
  414. host->adma_addr = dma_map_single(mmc_dev(host->mmc),
  415. host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
  416. if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
  417. goto unmap_entries;
  418. BUG_ON(host->adma_addr & 0x3);
  419. return 0;
  420. unmap_entries:
  421. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  422. data->sg_len, direction);
  423. unmap_align:
  424. dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
  425. 128 * 4, direction);
  426. fail:
  427. return -EINVAL;
  428. }
  429. static void sdhci_adma_table_post(struct sdhci_host *host,
  430. struct mmc_data *data)
  431. {
  432. int direction;
  433. struct scatterlist *sg;
  434. int i, size;
  435. u8 *align;
  436. char *buffer;
  437. unsigned long flags;
  438. if (data->flags & MMC_DATA_READ)
  439. direction = DMA_FROM_DEVICE;
  440. else
  441. direction = DMA_TO_DEVICE;
  442. dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
  443. (128 * 2 + 1) * 4, DMA_TO_DEVICE);
  444. dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
  445. 128 * 4, direction);
  446. if (data->flags & MMC_DATA_READ) {
  447. dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
  448. data->sg_len, direction);
  449. align = host->align_buffer;
  450. for_each_sg(data->sg, sg, host->sg_count, i) {
  451. if (sg_dma_address(sg) & 0x3) {
  452. size = 4 - (sg_dma_address(sg) & 0x3);
  453. buffer = sdhci_kmap_atomic(sg, &flags);
  454. WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
  455. memcpy(buffer, align, size);
  456. sdhci_kunmap_atomic(buffer, &flags);
  457. align += 4;
  458. }
  459. }
  460. }
  461. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  462. data->sg_len, direction);
  463. }
  464. static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
  465. {
  466. u8 count;
  467. unsigned target_timeout, current_timeout;
  468. /*
  469. * If the host controller provides us with an incorrect timeout
  470. * value, just skip the check and use 0xE. The hardware may take
  471. * longer to time out, but that's much better than having a too-short
  472. * timeout value.
  473. */
  474. if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
  475. return 0xE;
  476. /* timeout in us */
  477. target_timeout = data->timeout_ns / 1000 +
  478. data->timeout_clks / host->clock;
  479. if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
  480. host->timeout_clk = host->clock / 1000;
  481. /*
  482. * Figure out needed cycles.
  483. * We do this in steps in order to fit inside a 32 bit int.
  484. * The first step is the minimum timeout, which will have a
  485. * minimum resolution of 6 bits:
  486. * (1) 2^13*1000 > 2^22,
  487. * (2) host->timeout_clk < 2^16
  488. * =>
  489. * (1) / (2) > 2^6
  490. */
  491. count = 0;
  492. current_timeout = (1 << 13) * 1000 / host->timeout_clk;
  493. while (current_timeout < target_timeout) {
  494. count++;
  495. current_timeout <<= 1;
  496. if (count >= 0xF)
  497. break;
  498. }
  499. if (count >= 0xF) {
  500. printk(KERN_WARNING "%s: Too large timeout requested!\n",
  501. mmc_hostname(host->mmc));
  502. count = 0xE;
  503. }
  504. return count;
  505. }
  506. static void sdhci_set_transfer_irqs(struct sdhci_host *host)
  507. {
  508. u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  509. u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  510. if (host->flags & SDHCI_REQ_USE_DMA)
  511. sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
  512. else
  513. sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
  514. }
  515. static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
  516. {
  517. u8 count;
  518. u8 ctrl;
  519. int ret;
  520. WARN_ON(host->data);
  521. if (data == NULL)
  522. return;
  523. /* Sanity checks */
  524. BUG_ON(data->blksz * data->blocks > 524288);
  525. BUG_ON(data->blksz > host->mmc->max_blk_size);
  526. BUG_ON(data->blocks > 65535);
  527. host->data = data;
  528. host->data_early = 0;
  529. count = sdhci_calc_timeout(host, data);
  530. sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
  531. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
  532. host->flags |= SDHCI_REQ_USE_DMA;
  533. /*
  534. * FIXME: This doesn't account for merging when mapping the
  535. * scatterlist.
  536. */
  537. if (host->flags & SDHCI_REQ_USE_DMA) {
  538. int broken, i;
  539. struct scatterlist *sg;
  540. broken = 0;
  541. if (host->flags & SDHCI_USE_ADMA) {
  542. if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
  543. broken = 1;
  544. } else {
  545. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
  546. broken = 1;
  547. }
  548. if (unlikely(broken)) {
  549. for_each_sg(data->sg, sg, data->sg_len, i) {
  550. if (sg->length & 0x3) {
  551. DBG("Reverting to PIO because of "
  552. "transfer size (%d)\n",
  553. sg->length);
  554. host->flags &= ~SDHCI_REQ_USE_DMA;
  555. break;
  556. }
  557. }
  558. }
  559. }
  560. /*
  561. * The assumption here being that alignment is the same after
  562. * translation to device address space.
  563. */
  564. if (host->flags & SDHCI_REQ_USE_DMA) {
  565. int broken, i;
  566. struct scatterlist *sg;
  567. broken = 0;
  568. if (host->flags & SDHCI_USE_ADMA) {
  569. /*
  570. * As we use 3 byte chunks to work around
  571. * alignment problems, we need to check this
  572. * quirk.
  573. */
  574. if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
  575. broken = 1;
  576. } else {
  577. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
  578. broken = 1;
  579. }
  580. if (unlikely(broken)) {
  581. for_each_sg(data->sg, sg, data->sg_len, i) {
  582. if (sg->offset & 0x3) {
  583. DBG("Reverting to PIO because of "
  584. "bad alignment\n");
  585. host->flags &= ~SDHCI_REQ_USE_DMA;
  586. break;
  587. }
  588. }
  589. }
  590. }
  591. if (host->flags & SDHCI_REQ_USE_DMA) {
  592. if (host->flags & SDHCI_USE_ADMA) {
  593. ret = sdhci_adma_table_pre(host, data);
  594. if (ret) {
  595. /*
  596. * This only happens when someone fed
  597. * us an invalid request.
  598. */
  599. WARN_ON(1);
  600. host->flags &= ~SDHCI_REQ_USE_DMA;
  601. } else {
  602. sdhci_writel(host, host->adma_addr,
  603. SDHCI_ADMA_ADDRESS);
  604. }
  605. } else {
  606. int sg_cnt;
  607. sg_cnt = dma_map_sg(mmc_dev(host->mmc),
  608. data->sg, data->sg_len,
  609. (data->flags & MMC_DATA_READ) ?
  610. DMA_FROM_DEVICE :
  611. DMA_TO_DEVICE);
  612. if (sg_cnt == 0) {
  613. /*
  614. * This only happens when someone fed
  615. * us an invalid request.
  616. */
  617. WARN_ON(1);
  618. host->flags &= ~SDHCI_REQ_USE_DMA;
  619. } else {
  620. WARN_ON(sg_cnt != 1);
  621. sdhci_writel(host, sg_dma_address(data->sg),
  622. SDHCI_DMA_ADDRESS);
  623. }
  624. }
  625. }
  626. /*
  627. * Always adjust the DMA selection as some controllers
  628. * (e.g. JMicron) can't do PIO properly when the selection
  629. * is ADMA.
  630. */
  631. if (host->version >= SDHCI_SPEC_200) {
  632. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  633. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  634. if ((host->flags & SDHCI_REQ_USE_DMA) &&
  635. (host->flags & SDHCI_USE_ADMA))
  636. ctrl |= SDHCI_CTRL_ADMA32;
  637. else
  638. ctrl |= SDHCI_CTRL_SDMA;
  639. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  640. }
  641. if (!(host->flags & SDHCI_REQ_USE_DMA)) {
  642. int flags;
  643. flags = SG_MITER_ATOMIC;
  644. if (host->data->flags & MMC_DATA_READ)
  645. flags |= SG_MITER_TO_SG;
  646. else
  647. flags |= SG_MITER_FROM_SG;
  648. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  649. host->blocks = data->blocks;
  650. }
  651. sdhci_set_transfer_irqs(host);
  652. /* We do not handle DMA boundaries, so set it to max (512 KiB) */
  653. sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
  654. sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  655. }
  656. static void sdhci_set_transfer_mode(struct sdhci_host *host,
  657. struct mmc_data *data)
  658. {
  659. u16 mode;
  660. if (data == NULL)
  661. return;
  662. WARN_ON(!host->data);
  663. mode = SDHCI_TRNS_BLK_CNT_EN;
  664. if (data->blocks > 1) {
  665. if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
  666. mode |= SDHCI_TRNS_MULTI | SDHCI_TRNS_ACMD12;
  667. else
  668. mode |= SDHCI_TRNS_MULTI;
  669. }
  670. if (data->flags & MMC_DATA_READ)
  671. mode |= SDHCI_TRNS_READ;
  672. if (host->flags & SDHCI_REQ_USE_DMA)
  673. mode |= SDHCI_TRNS_DMA;
  674. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  675. }
  676. static void sdhci_finish_data(struct sdhci_host *host)
  677. {
  678. struct mmc_data *data;
  679. BUG_ON(!host->data);
  680. data = host->data;
  681. host->data = NULL;
  682. if (host->flags & SDHCI_REQ_USE_DMA) {
  683. if (host->flags & SDHCI_USE_ADMA)
  684. sdhci_adma_table_post(host, data);
  685. else {
  686. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  687. data->sg_len, (data->flags & MMC_DATA_READ) ?
  688. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  689. }
  690. }
  691. /*
  692. * The specification states that the block count register must
  693. * be updated, but it does not specify at what point in the
  694. * data flow. That makes the register entirely useless to read
  695. * back so we have to assume that nothing made it to the card
  696. * in the event of an error.
  697. */
  698. if (data->error)
  699. data->bytes_xfered = 0;
  700. else
  701. data->bytes_xfered = data->blksz * data->blocks;
  702. if (data->stop) {
  703. /*
  704. * The controller needs a reset of internal state machines
  705. * upon error conditions.
  706. */
  707. if (data->error) {
  708. sdhci_reset(host, SDHCI_RESET_CMD);
  709. sdhci_reset(host, SDHCI_RESET_DATA);
  710. }
  711. sdhci_send_command(host, data->stop);
  712. } else
  713. tasklet_schedule(&host->finish_tasklet);
  714. }
  715. static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
  716. {
  717. int flags;
  718. u32 mask;
  719. unsigned long timeout;
  720. WARN_ON(host->cmd);
  721. /* Wait max 10 ms */
  722. timeout = 10;
  723. mask = SDHCI_CMD_INHIBIT;
  724. if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  725. mask |= SDHCI_DATA_INHIBIT;
  726. /* We shouldn't wait for data inihibit for stop commands, even
  727. though they might use busy signaling */
  728. if (host->mrq->data && (cmd == host->mrq->data->stop))
  729. mask &= ~SDHCI_DATA_INHIBIT;
  730. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  731. if (timeout == 0) {
  732. printk(KERN_ERR "%s: Controller never released "
  733. "inhibit bit(s).\n", mmc_hostname(host->mmc));
  734. sdhci_dumpregs(host);
  735. cmd->error = -EIO;
  736. tasklet_schedule(&host->finish_tasklet);
  737. return;
  738. }
  739. timeout--;
  740. mdelay(1);
  741. }
  742. mod_timer(&host->timer, jiffies + 10 * HZ);
  743. host->cmd = cmd;
  744. sdhci_prepare_data(host, cmd->data);
  745. sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
  746. sdhci_set_transfer_mode(host, cmd->data);
  747. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  748. printk(KERN_ERR "%s: Unsupported response type!\n",
  749. mmc_hostname(host->mmc));
  750. cmd->error = -EINVAL;
  751. tasklet_schedule(&host->finish_tasklet);
  752. return;
  753. }
  754. if (!(cmd->flags & MMC_RSP_PRESENT))
  755. flags = SDHCI_CMD_RESP_NONE;
  756. else if (cmd->flags & MMC_RSP_136)
  757. flags = SDHCI_CMD_RESP_LONG;
  758. else if (cmd->flags & MMC_RSP_BUSY)
  759. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  760. else
  761. flags = SDHCI_CMD_RESP_SHORT;
  762. if (cmd->flags & MMC_RSP_CRC)
  763. flags |= SDHCI_CMD_CRC;
  764. if (cmd->flags & MMC_RSP_OPCODE)
  765. flags |= SDHCI_CMD_INDEX;
  766. if (cmd->data)
  767. flags |= SDHCI_CMD_DATA;
  768. sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
  769. }
  770. static void sdhci_finish_command(struct sdhci_host *host)
  771. {
  772. int i;
  773. BUG_ON(host->cmd == NULL);
  774. if (host->cmd->flags & MMC_RSP_PRESENT) {
  775. if (host->cmd->flags & MMC_RSP_136) {
  776. /* CRC is stripped so we need to do some shifting. */
  777. for (i = 0;i < 4;i++) {
  778. host->cmd->resp[i] = sdhci_readl(host,
  779. SDHCI_RESPONSE + (3-i)*4) << 8;
  780. if (i != 3)
  781. host->cmd->resp[i] |=
  782. sdhci_readb(host,
  783. SDHCI_RESPONSE + (3-i)*4-1);
  784. }
  785. } else {
  786. host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
  787. }
  788. }
  789. host->cmd->error = 0;
  790. if (host->data && host->data_early)
  791. sdhci_finish_data(host);
  792. if (!host->cmd->data)
  793. tasklet_schedule(&host->finish_tasklet);
  794. host->cmd = NULL;
  795. }
  796. static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
  797. {
  798. int div;
  799. u16 clk;
  800. unsigned long timeout;
  801. if (clock == host->clock)
  802. return;
  803. if (host->ops->set_clock) {
  804. host->ops->set_clock(host, clock);
  805. if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
  806. return;
  807. }
  808. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  809. if (clock == 0)
  810. goto out;
  811. if (host->version >= SDHCI_SPEC_300) {
  812. /* Version 3.00 divisors must be a multiple of 2. */
  813. if (host->max_clk <= clock)
  814. div = 1;
  815. else {
  816. for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
  817. if ((host->max_clk / div) <= clock)
  818. break;
  819. }
  820. }
  821. } else {
  822. /* Version 2.00 divisors must be a power of 2. */
  823. for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
  824. if ((host->max_clk / div) <= clock)
  825. break;
  826. }
  827. }
  828. div >>= 1;
  829. clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  830. clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  831. << SDHCI_DIVIDER_HI_SHIFT;
  832. clk |= SDHCI_CLOCK_INT_EN;
  833. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  834. /* Wait max 20 ms */
  835. timeout = 20;
  836. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  837. & SDHCI_CLOCK_INT_STABLE)) {
  838. if (timeout == 0) {
  839. printk(KERN_ERR "%s: Internal clock never "
  840. "stabilised.\n", mmc_hostname(host->mmc));
  841. sdhci_dumpregs(host);
  842. return;
  843. }
  844. timeout--;
  845. mdelay(1);
  846. }
  847. clk |= SDHCI_CLOCK_CARD_EN;
  848. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  849. out:
  850. host->clock = clock;
  851. }
  852. static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
  853. {
  854. u8 pwr;
  855. if (power == (unsigned short)-1)
  856. pwr = 0;
  857. else {
  858. switch (1 << power) {
  859. case MMC_VDD_165_195:
  860. pwr = SDHCI_POWER_180;
  861. break;
  862. case MMC_VDD_29_30:
  863. case MMC_VDD_30_31:
  864. pwr = SDHCI_POWER_300;
  865. break;
  866. case MMC_VDD_32_33:
  867. case MMC_VDD_33_34:
  868. pwr = SDHCI_POWER_330;
  869. break;
  870. default:
  871. BUG();
  872. }
  873. }
  874. if (host->pwr == pwr)
  875. return;
  876. host->pwr = pwr;
  877. if (pwr == 0) {
  878. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  879. return;
  880. }
  881. /*
  882. * Spec says that we should clear the power reg before setting
  883. * a new value. Some controllers don't seem to like this though.
  884. */
  885. if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
  886. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  887. /*
  888. * At least the Marvell CaFe chip gets confused if we set the voltage
  889. * and set turn on power at the same time, so set the voltage first.
  890. */
  891. if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
  892. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  893. pwr |= SDHCI_POWER_ON;
  894. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  895. /*
  896. * Some controllers need an extra 10ms delay of 10ms before they
  897. * can apply clock after applying power
  898. */
  899. if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
  900. mdelay(10);
  901. }
  902. /*****************************************************************************\
  903. * *
  904. * MMC callbacks *
  905. * *
  906. \*****************************************************************************/
  907. static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  908. {
  909. struct sdhci_host *host;
  910. bool present;
  911. unsigned long flags;
  912. host = mmc_priv(mmc);
  913. spin_lock_irqsave(&host->lock, flags);
  914. WARN_ON(host->mrq != NULL);
  915. #ifndef SDHCI_USE_LEDS_CLASS
  916. sdhci_activate_led(host);
  917. #endif
  918. if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) {
  919. if (mrq->stop) {
  920. mrq->data->stop = NULL;
  921. mrq->stop = NULL;
  922. }
  923. }
  924. host->mrq = mrq;
  925. /* If polling, assume that the card is always present. */
  926. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  927. present = true;
  928. else
  929. present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  930. SDHCI_CARD_PRESENT;
  931. if (!present || host->flags & SDHCI_DEVICE_DEAD) {
  932. host->mrq->cmd->error = -ENOMEDIUM;
  933. tasklet_schedule(&host->finish_tasklet);
  934. } else
  935. sdhci_send_command(host, mrq->cmd);
  936. mmiowb();
  937. spin_unlock_irqrestore(&host->lock, flags);
  938. }
  939. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  940. {
  941. struct sdhci_host *host;
  942. unsigned long flags;
  943. u8 ctrl;
  944. host = mmc_priv(mmc);
  945. spin_lock_irqsave(&host->lock, flags);
  946. if (host->flags & SDHCI_DEVICE_DEAD)
  947. goto out;
  948. /*
  949. * Reset the chip on each power off.
  950. * Should clear out any weird states.
  951. */
  952. if (ios->power_mode == MMC_POWER_OFF) {
  953. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  954. sdhci_reinit(host);
  955. }
  956. sdhci_set_clock(host, ios->clock);
  957. if (ios->power_mode == MMC_POWER_OFF)
  958. sdhci_set_power(host, -1);
  959. else
  960. sdhci_set_power(host, ios->vdd);
  961. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  962. if (ios->bus_width == MMC_BUS_WIDTH_8)
  963. ctrl |= SDHCI_CTRL_8BITBUS;
  964. else
  965. ctrl &= ~SDHCI_CTRL_8BITBUS;
  966. if (ios->bus_width == MMC_BUS_WIDTH_4)
  967. ctrl |= SDHCI_CTRL_4BITBUS;
  968. else
  969. ctrl &= ~SDHCI_CTRL_4BITBUS;
  970. if (ios->timing == MMC_TIMING_SD_HS &&
  971. !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
  972. ctrl |= SDHCI_CTRL_HISPD;
  973. else
  974. ctrl &= ~SDHCI_CTRL_HISPD;
  975. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  976. /*
  977. * Some (ENE) controllers go apeshit on some ios operation,
  978. * signalling timeout and CRC errors even on CMD0. Resetting
  979. * it on each ios seems to solve the problem.
  980. */
  981. if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
  982. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  983. out:
  984. mmiowb();
  985. spin_unlock_irqrestore(&host->lock, flags);
  986. }
  987. static int sdhci_get_ro(struct mmc_host *mmc)
  988. {
  989. struct sdhci_host *host;
  990. unsigned long flags;
  991. int present;
  992. host = mmc_priv(mmc);
  993. spin_lock_irqsave(&host->lock, flags);
  994. if (host->flags & SDHCI_DEVICE_DEAD)
  995. present = 0;
  996. else
  997. present = sdhci_readl(host, SDHCI_PRESENT_STATE);
  998. spin_unlock_irqrestore(&host->lock, flags);
  999. if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
  1000. return !!(present & SDHCI_WRITE_PROTECT);
  1001. return !(present & SDHCI_WRITE_PROTECT);
  1002. }
  1003. static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1004. {
  1005. struct sdhci_host *host;
  1006. unsigned long flags;
  1007. host = mmc_priv(mmc);
  1008. spin_lock_irqsave(&host->lock, flags);
  1009. if (host->flags & SDHCI_DEVICE_DEAD)
  1010. goto out;
  1011. if (enable)
  1012. sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
  1013. else
  1014. sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
  1015. out:
  1016. mmiowb();
  1017. spin_unlock_irqrestore(&host->lock, flags);
  1018. }
  1019. static const struct mmc_host_ops sdhci_ops = {
  1020. .request = sdhci_request,
  1021. .set_ios = sdhci_set_ios,
  1022. .get_ro = sdhci_get_ro,
  1023. .enable_sdio_irq = sdhci_enable_sdio_irq,
  1024. };
  1025. /*****************************************************************************\
  1026. * *
  1027. * Tasklets *
  1028. * *
  1029. \*****************************************************************************/
  1030. static void sdhci_tasklet_card(unsigned long param)
  1031. {
  1032. struct sdhci_host *host;
  1033. unsigned long flags;
  1034. host = (struct sdhci_host*)param;
  1035. spin_lock_irqsave(&host->lock, flags);
  1036. if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
  1037. if (host->mrq) {
  1038. printk(KERN_ERR "%s: Card removed during transfer!\n",
  1039. mmc_hostname(host->mmc));
  1040. printk(KERN_ERR "%s: Resetting controller.\n",
  1041. mmc_hostname(host->mmc));
  1042. sdhci_reset(host, SDHCI_RESET_CMD);
  1043. sdhci_reset(host, SDHCI_RESET_DATA);
  1044. host->mrq->cmd->error = -ENOMEDIUM;
  1045. tasklet_schedule(&host->finish_tasklet);
  1046. }
  1047. }
  1048. spin_unlock_irqrestore(&host->lock, flags);
  1049. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  1050. }
  1051. static void sdhci_tasklet_finish(unsigned long param)
  1052. {
  1053. struct sdhci_host *host;
  1054. unsigned long flags;
  1055. struct mmc_request *mrq;
  1056. host = (struct sdhci_host*)param;
  1057. spin_lock_irqsave(&host->lock, flags);
  1058. del_timer(&host->timer);
  1059. mrq = host->mrq;
  1060. /*
  1061. * The controller needs a reset of internal state machines
  1062. * upon error conditions.
  1063. */
  1064. if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  1065. (mrq->cmd->error ||
  1066. (mrq->data && (mrq->data->error ||
  1067. (mrq->data->stop && mrq->data->stop->error))) ||
  1068. (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
  1069. /* Some controllers need this kick or reset won't work here */
  1070. if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
  1071. unsigned int clock;
  1072. /* This is to force an update */
  1073. clock = host->clock;
  1074. host->clock = 0;
  1075. sdhci_set_clock(host, clock);
  1076. }
  1077. /* Spec says we should do both at the same time, but Ricoh
  1078. controllers do not like that. */
  1079. sdhci_reset(host, SDHCI_RESET_CMD);
  1080. sdhci_reset(host, SDHCI_RESET_DATA);
  1081. }
  1082. host->mrq = NULL;
  1083. host->cmd = NULL;
  1084. host->data = NULL;
  1085. #ifndef SDHCI_USE_LEDS_CLASS
  1086. sdhci_deactivate_led(host);
  1087. #endif
  1088. mmiowb();
  1089. spin_unlock_irqrestore(&host->lock, flags);
  1090. mmc_request_done(host->mmc, mrq);
  1091. }
  1092. static void sdhci_timeout_timer(unsigned long data)
  1093. {
  1094. struct sdhci_host *host;
  1095. unsigned long flags;
  1096. host = (struct sdhci_host*)data;
  1097. spin_lock_irqsave(&host->lock, flags);
  1098. if (host->mrq) {
  1099. printk(KERN_ERR "%s: Timeout waiting for hardware "
  1100. "interrupt.\n", mmc_hostname(host->mmc));
  1101. sdhci_dumpregs(host);
  1102. if (host->data) {
  1103. host->data->error = -ETIMEDOUT;
  1104. sdhci_finish_data(host);
  1105. } else {
  1106. if (host->cmd)
  1107. host->cmd->error = -ETIMEDOUT;
  1108. else
  1109. host->mrq->cmd->error = -ETIMEDOUT;
  1110. tasklet_schedule(&host->finish_tasklet);
  1111. }
  1112. }
  1113. mmiowb();
  1114. spin_unlock_irqrestore(&host->lock, flags);
  1115. }
  1116. /*****************************************************************************\
  1117. * *
  1118. * Interrupt handling *
  1119. * *
  1120. \*****************************************************************************/
  1121. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
  1122. {
  1123. BUG_ON(intmask == 0);
  1124. if (!host->cmd) {
  1125. printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
  1126. "though no command operation was in progress.\n",
  1127. mmc_hostname(host->mmc), (unsigned)intmask);
  1128. sdhci_dumpregs(host);
  1129. return;
  1130. }
  1131. if (intmask & SDHCI_INT_TIMEOUT)
  1132. host->cmd->error = -ETIMEDOUT;
  1133. else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  1134. SDHCI_INT_INDEX))
  1135. host->cmd->error = -EILSEQ;
  1136. if (host->cmd->error) {
  1137. tasklet_schedule(&host->finish_tasklet);
  1138. return;
  1139. }
  1140. /*
  1141. * The host can send and interrupt when the busy state has
  1142. * ended, allowing us to wait without wasting CPU cycles.
  1143. * Unfortunately this is overloaded on the "data complete"
  1144. * interrupt, so we need to take some care when handling
  1145. * it.
  1146. *
  1147. * Note: The 1.0 specification is a bit ambiguous about this
  1148. * feature so there might be some problems with older
  1149. * controllers.
  1150. */
  1151. if (host->cmd->flags & MMC_RSP_BUSY) {
  1152. if (host->cmd->data)
  1153. DBG("Cannot wait for busy signal when also "
  1154. "doing a data transfer");
  1155. else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
  1156. return;
  1157. /* The controller does not support the end-of-busy IRQ,
  1158. * fall through and take the SDHCI_INT_RESPONSE */
  1159. }
  1160. if (intmask & SDHCI_INT_RESPONSE)
  1161. sdhci_finish_command(host);
  1162. }
  1163. #ifdef CONFIG_MMC_DEBUG
  1164. static void sdhci_show_adma_error(struct sdhci_host *host)
  1165. {
  1166. const char *name = mmc_hostname(host->mmc);
  1167. u8 *desc = host->adma_desc;
  1168. __le32 *dma;
  1169. __le16 *len;
  1170. u8 attr;
  1171. sdhci_dumpregs(host);
  1172. while (true) {
  1173. dma = (__le32 *)(desc + 4);
  1174. len = (__le16 *)(desc + 2);
  1175. attr = *desc;
  1176. DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
  1177. name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
  1178. desc += 8;
  1179. if (attr & 2)
  1180. break;
  1181. }
  1182. }
  1183. #else
  1184. static void sdhci_show_adma_error(struct sdhci_host *host) { }
  1185. #endif
  1186. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  1187. {
  1188. BUG_ON(intmask == 0);
  1189. if (!host->data) {
  1190. /*
  1191. * The "data complete" interrupt is also used to
  1192. * indicate that a busy state has ended. See comment
  1193. * above in sdhci_cmd_irq().
  1194. */
  1195. if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  1196. if (intmask & SDHCI_INT_DATA_END) {
  1197. sdhci_finish_command(host);
  1198. return;
  1199. }
  1200. }
  1201. printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
  1202. "though no data operation was in progress.\n",
  1203. mmc_hostname(host->mmc), (unsigned)intmask);
  1204. sdhci_dumpregs(host);
  1205. return;
  1206. }
  1207. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  1208. host->data->error = -ETIMEDOUT;
  1209. else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
  1210. host->data->error = -EILSEQ;
  1211. else if (intmask & SDHCI_INT_ADMA_ERROR) {
  1212. printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
  1213. sdhci_show_adma_error(host);
  1214. host->data->error = -EIO;
  1215. }
  1216. if (host->data->error)
  1217. sdhci_finish_data(host);
  1218. else {
  1219. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  1220. sdhci_transfer_pio(host);
  1221. /*
  1222. * We currently don't do anything fancy with DMA
  1223. * boundaries, but as we can't disable the feature
  1224. * we need to at least restart the transfer.
  1225. */
  1226. if (intmask & SDHCI_INT_DMA_END)
  1227. sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
  1228. SDHCI_DMA_ADDRESS);
  1229. if (intmask & SDHCI_INT_DATA_END) {
  1230. if (host->cmd) {
  1231. /*
  1232. * Data managed to finish before the
  1233. * command completed. Make sure we do
  1234. * things in the proper order.
  1235. */
  1236. host->data_early = 1;
  1237. } else {
  1238. sdhci_finish_data(host);
  1239. }
  1240. }
  1241. }
  1242. }
  1243. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  1244. {
  1245. irqreturn_t result;
  1246. struct sdhci_host* host = dev_id;
  1247. u32 intmask;
  1248. int cardint = 0;
  1249. spin_lock(&host->lock);
  1250. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  1251. if (!intmask || intmask == 0xffffffff) {
  1252. result = IRQ_NONE;
  1253. goto out;
  1254. }
  1255. DBG("*** %s got interrupt: 0x%08x\n",
  1256. mmc_hostname(host->mmc), intmask);
  1257. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  1258. sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
  1259. SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
  1260. tasklet_schedule(&host->card_tasklet);
  1261. }
  1262. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
  1263. if (intmask & SDHCI_INT_CMD_MASK) {
  1264. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
  1265. SDHCI_INT_STATUS);
  1266. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  1267. }
  1268. if (intmask & SDHCI_INT_DATA_MASK) {
  1269. sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
  1270. SDHCI_INT_STATUS);
  1271. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  1272. }
  1273. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  1274. intmask &= ~SDHCI_INT_ERROR;
  1275. if (intmask & SDHCI_INT_BUS_POWER) {
  1276. printk(KERN_ERR "%s: Card is consuming too much power!\n",
  1277. mmc_hostname(host->mmc));
  1278. sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
  1279. }
  1280. intmask &= ~SDHCI_INT_BUS_POWER;
  1281. if (intmask & SDHCI_INT_CARD_INT)
  1282. cardint = 1;
  1283. intmask &= ~SDHCI_INT_CARD_INT;
  1284. if (intmask) {
  1285. printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
  1286. mmc_hostname(host->mmc), intmask);
  1287. sdhci_dumpregs(host);
  1288. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  1289. }
  1290. result = IRQ_HANDLED;
  1291. mmiowb();
  1292. out:
  1293. spin_unlock(&host->lock);
  1294. /*
  1295. * We have to delay this as it calls back into the driver.
  1296. */
  1297. if (cardint)
  1298. mmc_signal_sdio_irq(host->mmc);
  1299. return result;
  1300. }
  1301. /*****************************************************************************\
  1302. * *
  1303. * Suspend/resume *
  1304. * *
  1305. \*****************************************************************************/
  1306. #ifdef CONFIG_PM
  1307. int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
  1308. {
  1309. int ret;
  1310. sdhci_disable_card_detection(host);
  1311. ret = mmc_suspend_host(host->mmc);
  1312. if (ret)
  1313. return ret;
  1314. free_irq(host->irq, host);
  1315. if (host->vmmc)
  1316. ret = regulator_disable(host->vmmc);
  1317. return ret;
  1318. }
  1319. EXPORT_SYMBOL_GPL(sdhci_suspend_host);
  1320. int sdhci_resume_host(struct sdhci_host *host)
  1321. {
  1322. int ret;
  1323. if (host->vmmc) {
  1324. int ret = regulator_enable(host->vmmc);
  1325. if (ret)
  1326. return ret;
  1327. }
  1328. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  1329. if (host->ops->enable_dma)
  1330. host->ops->enable_dma(host);
  1331. }
  1332. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  1333. mmc_hostname(host->mmc), host);
  1334. if (ret)
  1335. return ret;
  1336. sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
  1337. mmiowb();
  1338. ret = mmc_resume_host(host->mmc);
  1339. sdhci_enable_card_detection(host);
  1340. return ret;
  1341. }
  1342. EXPORT_SYMBOL_GPL(sdhci_resume_host);
  1343. #endif /* CONFIG_PM */
  1344. /*****************************************************************************\
  1345. * *
  1346. * Device allocation/registration *
  1347. * *
  1348. \*****************************************************************************/
  1349. struct sdhci_host *sdhci_alloc_host(struct device *dev,
  1350. size_t priv_size)
  1351. {
  1352. struct mmc_host *mmc;
  1353. struct sdhci_host *host;
  1354. WARN_ON(dev == NULL);
  1355. mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
  1356. if (!mmc)
  1357. return ERR_PTR(-ENOMEM);
  1358. host = mmc_priv(mmc);
  1359. host->mmc = mmc;
  1360. return host;
  1361. }
  1362. EXPORT_SYMBOL_GPL(sdhci_alloc_host);
  1363. int sdhci_add_host(struct sdhci_host *host)
  1364. {
  1365. struct mmc_host *mmc;
  1366. unsigned int caps;
  1367. int ret;
  1368. WARN_ON(host == NULL);
  1369. if (host == NULL)
  1370. return -EINVAL;
  1371. mmc = host->mmc;
  1372. if (debug_quirks)
  1373. host->quirks = debug_quirks;
  1374. sdhci_reset(host, SDHCI_RESET_ALL);
  1375. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  1376. host->version = (host->version & SDHCI_SPEC_VER_MASK)
  1377. >> SDHCI_SPEC_VER_SHIFT;
  1378. if (host->version > SDHCI_SPEC_300) {
  1379. printk(KERN_ERR "%s: Unknown controller version (%d). "
  1380. "You may experience problems.\n", mmc_hostname(mmc),
  1381. host->version);
  1382. }
  1383. caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
  1384. sdhci_readl(host, SDHCI_CAPABILITIES);
  1385. if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
  1386. host->flags |= SDHCI_USE_SDMA;
  1387. else if (!(caps & SDHCI_CAN_DO_SDMA))
  1388. DBG("Controller doesn't have SDMA capability\n");
  1389. else
  1390. host->flags |= SDHCI_USE_SDMA;
  1391. if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
  1392. (host->flags & SDHCI_USE_SDMA)) {
  1393. DBG("Disabling DMA as it is marked broken\n");
  1394. host->flags &= ~SDHCI_USE_SDMA;
  1395. }
  1396. if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
  1397. host->flags |= SDHCI_USE_ADMA;
  1398. if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
  1399. (host->flags & SDHCI_USE_ADMA)) {
  1400. DBG("Disabling ADMA as it is marked broken\n");
  1401. host->flags &= ~SDHCI_USE_ADMA;
  1402. }
  1403. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  1404. if (host->ops->enable_dma) {
  1405. if (host->ops->enable_dma(host)) {
  1406. printk(KERN_WARNING "%s: No suitable DMA "
  1407. "available. Falling back to PIO.\n",
  1408. mmc_hostname(mmc));
  1409. host->flags &=
  1410. ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  1411. }
  1412. }
  1413. }
  1414. if (host->flags & SDHCI_USE_ADMA) {
  1415. /*
  1416. * We need to allocate descriptors for all sg entries
  1417. * (128) and potentially one alignment transfer for
  1418. * each of those entries.
  1419. */
  1420. host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
  1421. host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
  1422. if (!host->adma_desc || !host->align_buffer) {
  1423. kfree(host->adma_desc);
  1424. kfree(host->align_buffer);
  1425. printk(KERN_WARNING "%s: Unable to allocate ADMA "
  1426. "buffers. Falling back to standard DMA.\n",
  1427. mmc_hostname(mmc));
  1428. host->flags &= ~SDHCI_USE_ADMA;
  1429. }
  1430. }
  1431. /*
  1432. * If we use DMA, then it's up to the caller to set the DMA
  1433. * mask, but PIO does not need the hw shim so we set a new
  1434. * mask here in that case.
  1435. */
  1436. if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
  1437. host->dma_mask = DMA_BIT_MASK(64);
  1438. mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
  1439. }
  1440. if (host->version >= SDHCI_SPEC_300)
  1441. host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK)
  1442. >> SDHCI_CLOCK_BASE_SHIFT;
  1443. else
  1444. host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK)
  1445. >> SDHCI_CLOCK_BASE_SHIFT;
  1446. host->max_clk *= 1000000;
  1447. if (host->max_clk == 0 || host->quirks &
  1448. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
  1449. if (!host->ops->get_max_clock) {
  1450. printk(KERN_ERR
  1451. "%s: Hardware doesn't specify base clock "
  1452. "frequency.\n", mmc_hostname(mmc));
  1453. return -ENODEV;
  1454. }
  1455. host->max_clk = host->ops->get_max_clock(host);
  1456. }
  1457. host->timeout_clk =
  1458. (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
  1459. if (host->timeout_clk == 0) {
  1460. if (host->ops->get_timeout_clock) {
  1461. host->timeout_clk = host->ops->get_timeout_clock(host);
  1462. } else if (!(host->quirks &
  1463. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
  1464. printk(KERN_ERR
  1465. "%s: Hardware doesn't specify timeout clock "
  1466. "frequency.\n", mmc_hostname(mmc));
  1467. return -ENODEV;
  1468. }
  1469. }
  1470. if (caps & SDHCI_TIMEOUT_CLK_UNIT)
  1471. host->timeout_clk *= 1000;
  1472. /*
  1473. * Set host parameters.
  1474. */
  1475. mmc->ops = &sdhci_ops;
  1476. if (host->ops->get_min_clock)
  1477. mmc->f_min = host->ops->get_min_clock(host);
  1478. else if (host->version >= SDHCI_SPEC_300)
  1479. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  1480. else
  1481. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
  1482. mmc->f_max = host->max_clk;
  1483. mmc->caps |= MMC_CAP_SDIO_IRQ;
  1484. if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
  1485. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1486. if (caps & SDHCI_CAN_DO_HISPD)
  1487. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  1488. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  1489. mmc->caps |= MMC_CAP_NEEDS_POLL;
  1490. mmc->ocr_avail = 0;
  1491. if (caps & SDHCI_CAN_VDD_330)
  1492. mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
  1493. if (caps & SDHCI_CAN_VDD_300)
  1494. mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
  1495. if (caps & SDHCI_CAN_VDD_180)
  1496. mmc->ocr_avail |= MMC_VDD_165_195;
  1497. if (mmc->ocr_avail == 0) {
  1498. printk(KERN_ERR "%s: Hardware doesn't report any "
  1499. "support voltages.\n", mmc_hostname(mmc));
  1500. return -ENODEV;
  1501. }
  1502. spin_lock_init(&host->lock);
  1503. /*
  1504. * Maximum number of segments. Depends on if the hardware
  1505. * can do scatter/gather or not.
  1506. */
  1507. if (host->flags & SDHCI_USE_ADMA)
  1508. mmc->max_segs = 128;
  1509. else if (host->flags & SDHCI_USE_SDMA)
  1510. mmc->max_segs = 1;
  1511. else /* PIO */
  1512. mmc->max_segs = 128;
  1513. /*
  1514. * Maximum number of sectors in one transfer. Limited by DMA boundary
  1515. * size (512KiB).
  1516. */
  1517. mmc->max_req_size = 524288;
  1518. /*
  1519. * Maximum segment size. Could be one segment with the maximum number
  1520. * of bytes. When doing hardware scatter/gather, each entry cannot
  1521. * be larger than 64 KiB though.
  1522. */
  1523. if (host->flags & SDHCI_USE_ADMA)
  1524. mmc->max_seg_size = 65536;
  1525. else
  1526. mmc->max_seg_size = mmc->max_req_size;
  1527. /*
  1528. * Maximum block size. This varies from controller to controller and
  1529. * is specified in the capabilities register.
  1530. */
  1531. if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
  1532. mmc->max_blk_size = 2;
  1533. } else {
  1534. mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
  1535. SDHCI_MAX_BLOCK_SHIFT;
  1536. if (mmc->max_blk_size >= 3) {
  1537. printk(KERN_WARNING "%s: Invalid maximum block size, "
  1538. "assuming 512 bytes\n", mmc_hostname(mmc));
  1539. mmc->max_blk_size = 0;
  1540. }
  1541. }
  1542. mmc->max_blk_size = 512 << mmc->max_blk_size;
  1543. /*
  1544. * Maximum block count.
  1545. */
  1546. mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
  1547. /*
  1548. * Init tasklets.
  1549. */
  1550. tasklet_init(&host->card_tasklet,
  1551. sdhci_tasklet_card, (unsigned long)host);
  1552. tasklet_init(&host->finish_tasklet,
  1553. sdhci_tasklet_finish, (unsigned long)host);
  1554. setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
  1555. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  1556. mmc_hostname(mmc), host);
  1557. if (ret)
  1558. goto untasklet;
  1559. host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
  1560. if (IS_ERR(host->vmmc)) {
  1561. printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
  1562. host->vmmc = NULL;
  1563. } else {
  1564. regulator_enable(host->vmmc);
  1565. }
  1566. sdhci_init(host, 0);
  1567. #ifdef CONFIG_MMC_DEBUG
  1568. sdhci_dumpregs(host);
  1569. #endif
  1570. #ifdef SDHCI_USE_LEDS_CLASS
  1571. snprintf(host->led_name, sizeof(host->led_name),
  1572. "%s::", mmc_hostname(mmc));
  1573. host->led.name = host->led_name;
  1574. host->led.brightness = LED_OFF;
  1575. host->led.default_trigger = mmc_hostname(mmc);
  1576. host->led.brightness_set = sdhci_led_control;
  1577. ret = led_classdev_register(mmc_dev(mmc), &host->led);
  1578. if (ret)
  1579. goto reset;
  1580. #endif
  1581. mmiowb();
  1582. mmc_add_host(mmc);
  1583. printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
  1584. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  1585. (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
  1586. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  1587. sdhci_enable_card_detection(host);
  1588. return 0;
  1589. #ifdef SDHCI_USE_LEDS_CLASS
  1590. reset:
  1591. sdhci_reset(host, SDHCI_RESET_ALL);
  1592. free_irq(host->irq, host);
  1593. #endif
  1594. untasklet:
  1595. tasklet_kill(&host->card_tasklet);
  1596. tasklet_kill(&host->finish_tasklet);
  1597. return ret;
  1598. }
  1599. EXPORT_SYMBOL_GPL(sdhci_add_host);
  1600. void sdhci_remove_host(struct sdhci_host *host, int dead)
  1601. {
  1602. unsigned long flags;
  1603. if (dead) {
  1604. spin_lock_irqsave(&host->lock, flags);
  1605. host->flags |= SDHCI_DEVICE_DEAD;
  1606. if (host->mrq) {
  1607. printk(KERN_ERR "%s: Controller removed during "
  1608. " transfer!\n", mmc_hostname(host->mmc));
  1609. host->mrq->cmd->error = -ENOMEDIUM;
  1610. tasklet_schedule(&host->finish_tasklet);
  1611. }
  1612. spin_unlock_irqrestore(&host->lock, flags);
  1613. }
  1614. sdhci_disable_card_detection(host);
  1615. mmc_remove_host(host->mmc);
  1616. #ifdef SDHCI_USE_LEDS_CLASS
  1617. led_classdev_unregister(&host->led);
  1618. #endif
  1619. if (!dead)
  1620. sdhci_reset(host, SDHCI_RESET_ALL);
  1621. free_irq(host->irq, host);
  1622. del_timer_sync(&host->timer);
  1623. tasklet_kill(&host->card_tasklet);
  1624. tasklet_kill(&host->finish_tasklet);
  1625. if (host->vmmc) {
  1626. regulator_disable(host->vmmc);
  1627. regulator_put(host->vmmc);
  1628. }
  1629. kfree(host->adma_desc);
  1630. kfree(host->align_buffer);
  1631. host->adma_desc = NULL;
  1632. host->align_buffer = NULL;
  1633. }
  1634. EXPORT_SYMBOL_GPL(sdhci_remove_host);
  1635. void sdhci_free_host(struct sdhci_host *host)
  1636. {
  1637. mmc_free_host(host->mmc);
  1638. }
  1639. EXPORT_SYMBOL_GPL(sdhci_free_host);
  1640. /*****************************************************************************\
  1641. * *
  1642. * Driver init/exit *
  1643. * *
  1644. \*****************************************************************************/
  1645. static int __init sdhci_drv_init(void)
  1646. {
  1647. printk(KERN_INFO DRIVER_NAME
  1648. ": Secure Digital Host Controller Interface driver\n");
  1649. printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  1650. return 0;
  1651. }
  1652. static void __exit sdhci_drv_exit(void)
  1653. {
  1654. }
  1655. module_init(sdhci_drv_init);
  1656. module_exit(sdhci_drv_exit);
  1657. module_param(debug_quirks, uint, 0444);
  1658. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1659. MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
  1660. MODULE_LICENSE("GPL");
  1661. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");