sdhci.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  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. for (div = 1;div < 256;div *= 2) {
  812. if ((host->max_clk / div) <= clock)
  813. break;
  814. }
  815. div >>= 1;
  816. clk = div << SDHCI_DIVIDER_SHIFT;
  817. clk |= SDHCI_CLOCK_INT_EN;
  818. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  819. /* Wait max 20 ms */
  820. timeout = 20;
  821. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  822. & SDHCI_CLOCK_INT_STABLE)) {
  823. if (timeout == 0) {
  824. printk(KERN_ERR "%s: Internal clock never "
  825. "stabilised.\n", mmc_hostname(host->mmc));
  826. sdhci_dumpregs(host);
  827. return;
  828. }
  829. timeout--;
  830. mdelay(1);
  831. }
  832. clk |= SDHCI_CLOCK_CARD_EN;
  833. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  834. out:
  835. host->clock = clock;
  836. }
  837. static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
  838. {
  839. u8 pwr;
  840. if (power == (unsigned short)-1)
  841. pwr = 0;
  842. else {
  843. switch (1 << power) {
  844. case MMC_VDD_165_195:
  845. pwr = SDHCI_POWER_180;
  846. break;
  847. case MMC_VDD_29_30:
  848. case MMC_VDD_30_31:
  849. pwr = SDHCI_POWER_300;
  850. break;
  851. case MMC_VDD_32_33:
  852. case MMC_VDD_33_34:
  853. pwr = SDHCI_POWER_330;
  854. break;
  855. default:
  856. BUG();
  857. }
  858. }
  859. if (host->pwr == pwr)
  860. return;
  861. host->pwr = pwr;
  862. if (pwr == 0) {
  863. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  864. return;
  865. }
  866. /*
  867. * Spec says that we should clear the power reg before setting
  868. * a new value. Some controllers don't seem to like this though.
  869. */
  870. if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
  871. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  872. /*
  873. * At least the Marvell CaFe chip gets confused if we set the voltage
  874. * and set turn on power at the same time, so set the voltage first.
  875. */
  876. if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
  877. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  878. pwr |= SDHCI_POWER_ON;
  879. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  880. /*
  881. * Some controllers need an extra 10ms delay of 10ms before they
  882. * can apply clock after applying power
  883. */
  884. if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
  885. mdelay(10);
  886. }
  887. /*****************************************************************************\
  888. * *
  889. * MMC callbacks *
  890. * *
  891. \*****************************************************************************/
  892. static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  893. {
  894. struct sdhci_host *host;
  895. bool present;
  896. unsigned long flags;
  897. host = mmc_priv(mmc);
  898. spin_lock_irqsave(&host->lock, flags);
  899. WARN_ON(host->mrq != NULL);
  900. #ifndef SDHCI_USE_LEDS_CLASS
  901. sdhci_activate_led(host);
  902. #endif
  903. if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) {
  904. if (mrq->stop) {
  905. mrq->data->stop = NULL;
  906. mrq->stop = NULL;
  907. }
  908. }
  909. host->mrq = mrq;
  910. /* If polling, assume that the card is always present. */
  911. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  912. present = true;
  913. else
  914. present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  915. SDHCI_CARD_PRESENT;
  916. if (!present || host->flags & SDHCI_DEVICE_DEAD) {
  917. host->mrq->cmd->error = -ENOMEDIUM;
  918. tasklet_schedule(&host->finish_tasklet);
  919. } else
  920. sdhci_send_command(host, mrq->cmd);
  921. mmiowb();
  922. spin_unlock_irqrestore(&host->lock, flags);
  923. }
  924. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  925. {
  926. struct sdhci_host *host;
  927. unsigned long flags;
  928. u8 ctrl;
  929. host = mmc_priv(mmc);
  930. spin_lock_irqsave(&host->lock, flags);
  931. if (host->flags & SDHCI_DEVICE_DEAD)
  932. goto out;
  933. /*
  934. * Reset the chip on each power off.
  935. * Should clear out any weird states.
  936. */
  937. if (ios->power_mode == MMC_POWER_OFF) {
  938. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  939. sdhci_reinit(host);
  940. }
  941. sdhci_set_clock(host, ios->clock);
  942. if (ios->power_mode == MMC_POWER_OFF)
  943. sdhci_set_power(host, -1);
  944. else
  945. sdhci_set_power(host, ios->vdd);
  946. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  947. if (ios->bus_width == MMC_BUS_WIDTH_8)
  948. ctrl |= SDHCI_CTRL_8BITBUS;
  949. else
  950. ctrl &= ~SDHCI_CTRL_8BITBUS;
  951. if (ios->bus_width == MMC_BUS_WIDTH_4)
  952. ctrl |= SDHCI_CTRL_4BITBUS;
  953. else
  954. ctrl &= ~SDHCI_CTRL_4BITBUS;
  955. if (ios->timing == MMC_TIMING_SD_HS &&
  956. !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
  957. ctrl |= SDHCI_CTRL_HISPD;
  958. else
  959. ctrl &= ~SDHCI_CTRL_HISPD;
  960. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  961. /*
  962. * Some (ENE) controllers go apeshit on some ios operation,
  963. * signalling timeout and CRC errors even on CMD0. Resetting
  964. * it on each ios seems to solve the problem.
  965. */
  966. if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
  967. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  968. out:
  969. mmiowb();
  970. spin_unlock_irqrestore(&host->lock, flags);
  971. }
  972. static int sdhci_get_ro(struct mmc_host *mmc)
  973. {
  974. struct sdhci_host *host;
  975. unsigned long flags;
  976. int present;
  977. host = mmc_priv(mmc);
  978. spin_lock_irqsave(&host->lock, flags);
  979. if (host->flags & SDHCI_DEVICE_DEAD)
  980. present = 0;
  981. else
  982. present = sdhci_readl(host, SDHCI_PRESENT_STATE);
  983. spin_unlock_irqrestore(&host->lock, flags);
  984. if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
  985. return !!(present & SDHCI_WRITE_PROTECT);
  986. return !(present & SDHCI_WRITE_PROTECT);
  987. }
  988. static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  989. {
  990. struct sdhci_host *host;
  991. unsigned long flags;
  992. host = mmc_priv(mmc);
  993. spin_lock_irqsave(&host->lock, flags);
  994. if (host->flags & SDHCI_DEVICE_DEAD)
  995. goto out;
  996. if (enable)
  997. sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
  998. else
  999. sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
  1000. out:
  1001. mmiowb();
  1002. spin_unlock_irqrestore(&host->lock, flags);
  1003. }
  1004. static const struct mmc_host_ops sdhci_ops = {
  1005. .request = sdhci_request,
  1006. .set_ios = sdhci_set_ios,
  1007. .get_ro = sdhci_get_ro,
  1008. .enable_sdio_irq = sdhci_enable_sdio_irq,
  1009. };
  1010. /*****************************************************************************\
  1011. * *
  1012. * Tasklets *
  1013. * *
  1014. \*****************************************************************************/
  1015. static void sdhci_tasklet_card(unsigned long param)
  1016. {
  1017. struct sdhci_host *host;
  1018. unsigned long flags;
  1019. host = (struct sdhci_host*)param;
  1020. spin_lock_irqsave(&host->lock, flags);
  1021. if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
  1022. if (host->mrq) {
  1023. printk(KERN_ERR "%s: Card removed during transfer!\n",
  1024. mmc_hostname(host->mmc));
  1025. printk(KERN_ERR "%s: Resetting controller.\n",
  1026. mmc_hostname(host->mmc));
  1027. sdhci_reset(host, SDHCI_RESET_CMD);
  1028. sdhci_reset(host, SDHCI_RESET_DATA);
  1029. host->mrq->cmd->error = -ENOMEDIUM;
  1030. tasklet_schedule(&host->finish_tasklet);
  1031. }
  1032. }
  1033. spin_unlock_irqrestore(&host->lock, flags);
  1034. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  1035. }
  1036. static void sdhci_tasklet_finish(unsigned long param)
  1037. {
  1038. struct sdhci_host *host;
  1039. unsigned long flags;
  1040. struct mmc_request *mrq;
  1041. host = (struct sdhci_host*)param;
  1042. spin_lock_irqsave(&host->lock, flags);
  1043. del_timer(&host->timer);
  1044. mrq = host->mrq;
  1045. /*
  1046. * The controller needs a reset of internal state machines
  1047. * upon error conditions.
  1048. */
  1049. if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  1050. (mrq->cmd->error ||
  1051. (mrq->data && (mrq->data->error ||
  1052. (mrq->data->stop && mrq->data->stop->error))) ||
  1053. (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
  1054. /* Some controllers need this kick or reset won't work here */
  1055. if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
  1056. unsigned int clock;
  1057. /* This is to force an update */
  1058. clock = host->clock;
  1059. host->clock = 0;
  1060. sdhci_set_clock(host, clock);
  1061. }
  1062. /* Spec says we should do both at the same time, but Ricoh
  1063. controllers do not like that. */
  1064. sdhci_reset(host, SDHCI_RESET_CMD);
  1065. sdhci_reset(host, SDHCI_RESET_DATA);
  1066. }
  1067. host->mrq = NULL;
  1068. host->cmd = NULL;
  1069. host->data = NULL;
  1070. #ifndef SDHCI_USE_LEDS_CLASS
  1071. sdhci_deactivate_led(host);
  1072. #endif
  1073. mmiowb();
  1074. spin_unlock_irqrestore(&host->lock, flags);
  1075. mmc_request_done(host->mmc, mrq);
  1076. }
  1077. static void sdhci_timeout_timer(unsigned long data)
  1078. {
  1079. struct sdhci_host *host;
  1080. unsigned long flags;
  1081. host = (struct sdhci_host*)data;
  1082. spin_lock_irqsave(&host->lock, flags);
  1083. if (host->mrq) {
  1084. printk(KERN_ERR "%s: Timeout waiting for hardware "
  1085. "interrupt.\n", mmc_hostname(host->mmc));
  1086. sdhci_dumpregs(host);
  1087. if (host->data) {
  1088. host->data->error = -ETIMEDOUT;
  1089. sdhci_finish_data(host);
  1090. } else {
  1091. if (host->cmd)
  1092. host->cmd->error = -ETIMEDOUT;
  1093. else
  1094. host->mrq->cmd->error = -ETIMEDOUT;
  1095. tasklet_schedule(&host->finish_tasklet);
  1096. }
  1097. }
  1098. mmiowb();
  1099. spin_unlock_irqrestore(&host->lock, flags);
  1100. }
  1101. /*****************************************************************************\
  1102. * *
  1103. * Interrupt handling *
  1104. * *
  1105. \*****************************************************************************/
  1106. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
  1107. {
  1108. BUG_ON(intmask == 0);
  1109. if (!host->cmd) {
  1110. printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
  1111. "though no command operation was in progress.\n",
  1112. mmc_hostname(host->mmc), (unsigned)intmask);
  1113. sdhci_dumpregs(host);
  1114. return;
  1115. }
  1116. if (intmask & SDHCI_INT_TIMEOUT)
  1117. host->cmd->error = -ETIMEDOUT;
  1118. else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  1119. SDHCI_INT_INDEX))
  1120. host->cmd->error = -EILSEQ;
  1121. if (host->cmd->error) {
  1122. tasklet_schedule(&host->finish_tasklet);
  1123. return;
  1124. }
  1125. /*
  1126. * The host can send and interrupt when the busy state has
  1127. * ended, allowing us to wait without wasting CPU cycles.
  1128. * Unfortunately this is overloaded on the "data complete"
  1129. * interrupt, so we need to take some care when handling
  1130. * it.
  1131. *
  1132. * Note: The 1.0 specification is a bit ambiguous about this
  1133. * feature so there might be some problems with older
  1134. * controllers.
  1135. */
  1136. if (host->cmd->flags & MMC_RSP_BUSY) {
  1137. if (host->cmd->data)
  1138. DBG("Cannot wait for busy signal when also "
  1139. "doing a data transfer");
  1140. else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
  1141. return;
  1142. /* The controller does not support the end-of-busy IRQ,
  1143. * fall through and take the SDHCI_INT_RESPONSE */
  1144. }
  1145. if (intmask & SDHCI_INT_RESPONSE)
  1146. sdhci_finish_command(host);
  1147. }
  1148. #ifdef DEBUG
  1149. static void sdhci_show_adma_error(struct sdhci_host *host)
  1150. {
  1151. const char *name = mmc_hostname(host->mmc);
  1152. u8 *desc = host->adma_desc;
  1153. __le32 *dma;
  1154. __le16 *len;
  1155. u8 attr;
  1156. sdhci_dumpregs(host);
  1157. while (true) {
  1158. dma = (__le32 *)(desc + 4);
  1159. len = (__le16 *)(desc + 2);
  1160. attr = *desc;
  1161. DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
  1162. name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
  1163. desc += 8;
  1164. if (attr & 2)
  1165. break;
  1166. }
  1167. }
  1168. #else
  1169. static void sdhci_show_adma_error(struct sdhci_host *host) { }
  1170. #endif
  1171. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  1172. {
  1173. BUG_ON(intmask == 0);
  1174. if (!host->data) {
  1175. /*
  1176. * The "data complete" interrupt is also used to
  1177. * indicate that a busy state has ended. See comment
  1178. * above in sdhci_cmd_irq().
  1179. */
  1180. if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  1181. if (intmask & SDHCI_INT_DATA_END) {
  1182. sdhci_finish_command(host);
  1183. return;
  1184. }
  1185. }
  1186. printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
  1187. "though no data operation was in progress.\n",
  1188. mmc_hostname(host->mmc), (unsigned)intmask);
  1189. sdhci_dumpregs(host);
  1190. return;
  1191. }
  1192. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  1193. host->data->error = -ETIMEDOUT;
  1194. else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
  1195. host->data->error = -EILSEQ;
  1196. else if (intmask & SDHCI_INT_ADMA_ERROR) {
  1197. printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
  1198. sdhci_show_adma_error(host);
  1199. host->data->error = -EIO;
  1200. }
  1201. if (host->data->error)
  1202. sdhci_finish_data(host);
  1203. else {
  1204. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  1205. sdhci_transfer_pio(host);
  1206. /*
  1207. * We currently don't do anything fancy with DMA
  1208. * boundaries, but as we can't disable the feature
  1209. * we need to at least restart the transfer.
  1210. */
  1211. if (intmask & SDHCI_INT_DMA_END)
  1212. sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
  1213. SDHCI_DMA_ADDRESS);
  1214. if (intmask & SDHCI_INT_DATA_END) {
  1215. if (host->cmd) {
  1216. /*
  1217. * Data managed to finish before the
  1218. * command completed. Make sure we do
  1219. * things in the proper order.
  1220. */
  1221. host->data_early = 1;
  1222. } else {
  1223. sdhci_finish_data(host);
  1224. }
  1225. }
  1226. }
  1227. }
  1228. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  1229. {
  1230. irqreturn_t result;
  1231. struct sdhci_host* host = dev_id;
  1232. u32 intmask;
  1233. int cardint = 0;
  1234. spin_lock(&host->lock);
  1235. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  1236. if (!intmask || intmask == 0xffffffff) {
  1237. result = IRQ_NONE;
  1238. goto out;
  1239. }
  1240. DBG("*** %s got interrupt: 0x%08x\n",
  1241. mmc_hostname(host->mmc), intmask);
  1242. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  1243. sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
  1244. SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
  1245. tasklet_schedule(&host->card_tasklet);
  1246. }
  1247. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
  1248. if (intmask & SDHCI_INT_CMD_MASK) {
  1249. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
  1250. SDHCI_INT_STATUS);
  1251. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  1252. }
  1253. if (intmask & SDHCI_INT_DATA_MASK) {
  1254. sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
  1255. SDHCI_INT_STATUS);
  1256. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  1257. }
  1258. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  1259. intmask &= ~SDHCI_INT_ERROR;
  1260. if (intmask & SDHCI_INT_BUS_POWER) {
  1261. printk(KERN_ERR "%s: Card is consuming too much power!\n",
  1262. mmc_hostname(host->mmc));
  1263. sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
  1264. }
  1265. intmask &= ~SDHCI_INT_BUS_POWER;
  1266. if (intmask & SDHCI_INT_CARD_INT)
  1267. cardint = 1;
  1268. intmask &= ~SDHCI_INT_CARD_INT;
  1269. if (intmask) {
  1270. printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
  1271. mmc_hostname(host->mmc), intmask);
  1272. sdhci_dumpregs(host);
  1273. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  1274. }
  1275. result = IRQ_HANDLED;
  1276. mmiowb();
  1277. out:
  1278. spin_unlock(&host->lock);
  1279. /*
  1280. * We have to delay this as it calls back into the driver.
  1281. */
  1282. if (cardint)
  1283. mmc_signal_sdio_irq(host->mmc);
  1284. return result;
  1285. }
  1286. /*****************************************************************************\
  1287. * *
  1288. * Suspend/resume *
  1289. * *
  1290. \*****************************************************************************/
  1291. #ifdef CONFIG_PM
  1292. int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
  1293. {
  1294. int ret;
  1295. sdhci_disable_card_detection(host);
  1296. ret = mmc_suspend_host(host->mmc);
  1297. if (ret)
  1298. return ret;
  1299. free_irq(host->irq, host);
  1300. if (host->vmmc)
  1301. ret = regulator_disable(host->vmmc);
  1302. return ret;
  1303. }
  1304. EXPORT_SYMBOL_GPL(sdhci_suspend_host);
  1305. int sdhci_resume_host(struct sdhci_host *host)
  1306. {
  1307. int ret;
  1308. if (host->vmmc) {
  1309. int ret = regulator_enable(host->vmmc);
  1310. if (ret)
  1311. return ret;
  1312. }
  1313. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  1314. if (host->ops->enable_dma)
  1315. host->ops->enable_dma(host);
  1316. }
  1317. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  1318. mmc_hostname(host->mmc), host);
  1319. if (ret)
  1320. return ret;
  1321. sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
  1322. mmiowb();
  1323. ret = mmc_resume_host(host->mmc);
  1324. sdhci_enable_card_detection(host);
  1325. return ret;
  1326. }
  1327. EXPORT_SYMBOL_GPL(sdhci_resume_host);
  1328. #endif /* CONFIG_PM */
  1329. /*****************************************************************************\
  1330. * *
  1331. * Device allocation/registration *
  1332. * *
  1333. \*****************************************************************************/
  1334. struct sdhci_host *sdhci_alloc_host(struct device *dev,
  1335. size_t priv_size)
  1336. {
  1337. struct mmc_host *mmc;
  1338. struct sdhci_host *host;
  1339. WARN_ON(dev == NULL);
  1340. mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
  1341. if (!mmc)
  1342. return ERR_PTR(-ENOMEM);
  1343. host = mmc_priv(mmc);
  1344. host->mmc = mmc;
  1345. return host;
  1346. }
  1347. EXPORT_SYMBOL_GPL(sdhci_alloc_host);
  1348. int sdhci_add_host(struct sdhci_host *host)
  1349. {
  1350. struct mmc_host *mmc;
  1351. unsigned int caps;
  1352. int ret;
  1353. WARN_ON(host == NULL);
  1354. if (host == NULL)
  1355. return -EINVAL;
  1356. mmc = host->mmc;
  1357. if (debug_quirks)
  1358. host->quirks = debug_quirks;
  1359. sdhci_reset(host, SDHCI_RESET_ALL);
  1360. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  1361. host->version = (host->version & SDHCI_SPEC_VER_MASK)
  1362. >> SDHCI_SPEC_VER_SHIFT;
  1363. if (host->version > SDHCI_SPEC_200) {
  1364. printk(KERN_ERR "%s: Unknown controller version (%d). "
  1365. "You may experience problems.\n", mmc_hostname(mmc),
  1366. host->version);
  1367. }
  1368. caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
  1369. sdhci_readl(host, SDHCI_CAPABILITIES);
  1370. if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
  1371. host->flags |= SDHCI_USE_SDMA;
  1372. else if (!(caps & SDHCI_CAN_DO_SDMA))
  1373. DBG("Controller doesn't have SDMA capability\n");
  1374. else
  1375. host->flags |= SDHCI_USE_SDMA;
  1376. if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
  1377. (host->flags & SDHCI_USE_SDMA)) {
  1378. DBG("Disabling DMA as it is marked broken\n");
  1379. host->flags &= ~SDHCI_USE_SDMA;
  1380. }
  1381. if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
  1382. host->flags |= SDHCI_USE_ADMA;
  1383. if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
  1384. (host->flags & SDHCI_USE_ADMA)) {
  1385. DBG("Disabling ADMA as it is marked broken\n");
  1386. host->flags &= ~SDHCI_USE_ADMA;
  1387. }
  1388. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  1389. if (host->ops->enable_dma) {
  1390. if (host->ops->enable_dma(host)) {
  1391. printk(KERN_WARNING "%s: No suitable DMA "
  1392. "available. Falling back to PIO.\n",
  1393. mmc_hostname(mmc));
  1394. host->flags &=
  1395. ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  1396. }
  1397. }
  1398. }
  1399. if (host->flags & SDHCI_USE_ADMA) {
  1400. /*
  1401. * We need to allocate descriptors for all sg entries
  1402. * (128) and potentially one alignment transfer for
  1403. * each of those entries.
  1404. */
  1405. host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
  1406. host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
  1407. if (!host->adma_desc || !host->align_buffer) {
  1408. kfree(host->adma_desc);
  1409. kfree(host->align_buffer);
  1410. printk(KERN_WARNING "%s: Unable to allocate ADMA "
  1411. "buffers. Falling back to standard DMA.\n",
  1412. mmc_hostname(mmc));
  1413. host->flags &= ~SDHCI_USE_ADMA;
  1414. }
  1415. }
  1416. /*
  1417. * If we use DMA, then it's up to the caller to set the DMA
  1418. * mask, but PIO does not need the hw shim so we set a new
  1419. * mask here in that case.
  1420. */
  1421. if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
  1422. host->dma_mask = DMA_BIT_MASK(64);
  1423. mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
  1424. }
  1425. host->max_clk =
  1426. (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
  1427. host->max_clk *= 1000000;
  1428. if (host->max_clk == 0 || host->quirks &
  1429. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
  1430. if (!host->ops->get_max_clock) {
  1431. printk(KERN_ERR
  1432. "%s: Hardware doesn't specify base clock "
  1433. "frequency.\n", mmc_hostname(mmc));
  1434. return -ENODEV;
  1435. }
  1436. host->max_clk = host->ops->get_max_clock(host);
  1437. }
  1438. host->timeout_clk =
  1439. (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
  1440. if (host->timeout_clk == 0) {
  1441. if (host->ops->get_timeout_clock) {
  1442. host->timeout_clk = host->ops->get_timeout_clock(host);
  1443. } else if (!(host->quirks &
  1444. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
  1445. printk(KERN_ERR
  1446. "%s: Hardware doesn't specify timeout clock "
  1447. "frequency.\n", mmc_hostname(mmc));
  1448. return -ENODEV;
  1449. }
  1450. }
  1451. if (caps & SDHCI_TIMEOUT_CLK_UNIT)
  1452. host->timeout_clk *= 1000;
  1453. /*
  1454. * Set host parameters.
  1455. */
  1456. mmc->ops = &sdhci_ops;
  1457. if (host->ops->get_min_clock)
  1458. mmc->f_min = host->ops->get_min_clock(host);
  1459. else
  1460. mmc->f_min = host->max_clk / 256;
  1461. mmc->f_max = host->max_clk;
  1462. mmc->caps |= MMC_CAP_SDIO_IRQ;
  1463. if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
  1464. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1465. if (caps & SDHCI_CAN_DO_HISPD)
  1466. mmc->caps |= MMC_CAP_SD_HIGHSPEED;
  1467. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  1468. mmc->caps |= MMC_CAP_NEEDS_POLL;
  1469. mmc->ocr_avail = 0;
  1470. if (caps & SDHCI_CAN_VDD_330)
  1471. mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
  1472. if (caps & SDHCI_CAN_VDD_300)
  1473. mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
  1474. if (caps & SDHCI_CAN_VDD_180)
  1475. mmc->ocr_avail |= MMC_VDD_165_195;
  1476. if (mmc->ocr_avail == 0) {
  1477. printk(KERN_ERR "%s: Hardware doesn't report any "
  1478. "support voltages.\n", mmc_hostname(mmc));
  1479. return -ENODEV;
  1480. }
  1481. spin_lock_init(&host->lock);
  1482. /*
  1483. * Maximum number of segments. Depends on if the hardware
  1484. * can do scatter/gather or not.
  1485. */
  1486. if (host->flags & SDHCI_USE_ADMA)
  1487. mmc->max_hw_segs = 128;
  1488. else if (host->flags & SDHCI_USE_SDMA)
  1489. mmc->max_hw_segs = 1;
  1490. else /* PIO */
  1491. mmc->max_hw_segs = 128;
  1492. mmc->max_phys_segs = 128;
  1493. /*
  1494. * Maximum number of sectors in one transfer. Limited by DMA boundary
  1495. * size (512KiB).
  1496. */
  1497. mmc->max_req_size = 524288;
  1498. /*
  1499. * Maximum segment size. Could be one segment with the maximum number
  1500. * of bytes. When doing hardware scatter/gather, each entry cannot
  1501. * be larger than 64 KiB though.
  1502. */
  1503. if (host->flags & SDHCI_USE_ADMA)
  1504. mmc->max_seg_size = 65536;
  1505. else
  1506. mmc->max_seg_size = mmc->max_req_size;
  1507. /*
  1508. * Maximum block size. This varies from controller to controller and
  1509. * is specified in the capabilities register.
  1510. */
  1511. if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
  1512. mmc->max_blk_size = 2;
  1513. } else {
  1514. mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
  1515. SDHCI_MAX_BLOCK_SHIFT;
  1516. if (mmc->max_blk_size >= 3) {
  1517. printk(KERN_WARNING "%s: Invalid maximum block size, "
  1518. "assuming 512 bytes\n", mmc_hostname(mmc));
  1519. mmc->max_blk_size = 0;
  1520. }
  1521. }
  1522. mmc->max_blk_size = 512 << mmc->max_blk_size;
  1523. /*
  1524. * Maximum block count.
  1525. */
  1526. mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
  1527. /*
  1528. * Init tasklets.
  1529. */
  1530. tasklet_init(&host->card_tasklet,
  1531. sdhci_tasklet_card, (unsigned long)host);
  1532. tasklet_init(&host->finish_tasklet,
  1533. sdhci_tasklet_finish, (unsigned long)host);
  1534. setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
  1535. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  1536. mmc_hostname(mmc), host);
  1537. if (ret)
  1538. goto untasklet;
  1539. host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
  1540. if (IS_ERR(host->vmmc)) {
  1541. printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
  1542. host->vmmc = NULL;
  1543. } else {
  1544. regulator_enable(host->vmmc);
  1545. }
  1546. sdhci_init(host, 0);
  1547. #ifdef CONFIG_MMC_DEBUG
  1548. sdhci_dumpregs(host);
  1549. #endif
  1550. #ifdef SDHCI_USE_LEDS_CLASS
  1551. snprintf(host->led_name, sizeof(host->led_name),
  1552. "%s::", mmc_hostname(mmc));
  1553. host->led.name = host->led_name;
  1554. host->led.brightness = LED_OFF;
  1555. host->led.default_trigger = mmc_hostname(mmc);
  1556. host->led.brightness_set = sdhci_led_control;
  1557. ret = led_classdev_register(mmc_dev(mmc), &host->led);
  1558. if (ret)
  1559. goto reset;
  1560. #endif
  1561. mmiowb();
  1562. mmc_add_host(mmc);
  1563. printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
  1564. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  1565. (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
  1566. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  1567. sdhci_enable_card_detection(host);
  1568. return 0;
  1569. #ifdef SDHCI_USE_LEDS_CLASS
  1570. reset:
  1571. sdhci_reset(host, SDHCI_RESET_ALL);
  1572. free_irq(host->irq, host);
  1573. #endif
  1574. untasklet:
  1575. tasklet_kill(&host->card_tasklet);
  1576. tasklet_kill(&host->finish_tasklet);
  1577. return ret;
  1578. }
  1579. EXPORT_SYMBOL_GPL(sdhci_add_host);
  1580. void sdhci_remove_host(struct sdhci_host *host, int dead)
  1581. {
  1582. unsigned long flags;
  1583. if (dead) {
  1584. spin_lock_irqsave(&host->lock, flags);
  1585. host->flags |= SDHCI_DEVICE_DEAD;
  1586. if (host->mrq) {
  1587. printk(KERN_ERR "%s: Controller removed during "
  1588. " transfer!\n", mmc_hostname(host->mmc));
  1589. host->mrq->cmd->error = -ENOMEDIUM;
  1590. tasklet_schedule(&host->finish_tasklet);
  1591. }
  1592. spin_unlock_irqrestore(&host->lock, flags);
  1593. }
  1594. sdhci_disable_card_detection(host);
  1595. mmc_remove_host(host->mmc);
  1596. #ifdef SDHCI_USE_LEDS_CLASS
  1597. led_classdev_unregister(&host->led);
  1598. #endif
  1599. if (!dead)
  1600. sdhci_reset(host, SDHCI_RESET_ALL);
  1601. free_irq(host->irq, host);
  1602. del_timer_sync(&host->timer);
  1603. tasklet_kill(&host->card_tasklet);
  1604. tasklet_kill(&host->finish_tasklet);
  1605. if (host->vmmc) {
  1606. regulator_disable(host->vmmc);
  1607. regulator_put(host->vmmc);
  1608. }
  1609. kfree(host->adma_desc);
  1610. kfree(host->align_buffer);
  1611. host->adma_desc = NULL;
  1612. host->align_buffer = NULL;
  1613. }
  1614. EXPORT_SYMBOL_GPL(sdhci_remove_host);
  1615. void sdhci_free_host(struct sdhci_host *host)
  1616. {
  1617. mmc_free_host(host->mmc);
  1618. }
  1619. EXPORT_SYMBOL_GPL(sdhci_free_host);
  1620. /*****************************************************************************\
  1621. * *
  1622. * Driver init/exit *
  1623. * *
  1624. \*****************************************************************************/
  1625. static int __init sdhci_drv_init(void)
  1626. {
  1627. printk(KERN_INFO DRIVER_NAME
  1628. ": Secure Digital Host Controller Interface driver\n");
  1629. printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  1630. return 0;
  1631. }
  1632. static void __exit sdhci_drv_exit(void)
  1633. {
  1634. }
  1635. module_init(sdhci_drv_init);
  1636. module_exit(sdhci_drv_exit);
  1637. module_param(debug_quirks, uint, 0444);
  1638. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1639. MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
  1640. MODULE_LICENSE("GPL");
  1641. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");