sdhci.c 44 KB

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