sdhci.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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. readl(host->ioaddr + SDHCI_DMA_ADDRESS),
  40. readw(host->ioaddr + SDHCI_HOST_VERSION));
  41. printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  42. readw(host->ioaddr + SDHCI_BLOCK_SIZE),
  43. readw(host->ioaddr + SDHCI_BLOCK_COUNT));
  44. printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
  45. readl(host->ioaddr + SDHCI_ARGUMENT),
  46. readw(host->ioaddr + SDHCI_TRANSFER_MODE));
  47. printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
  48. readl(host->ioaddr + SDHCI_PRESENT_STATE),
  49. readb(host->ioaddr + SDHCI_HOST_CONTROL));
  50. printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
  51. readb(host->ioaddr + SDHCI_POWER_CONTROL),
  52. readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
  53. printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
  54. readb(host->ioaddr + SDHCI_WAKE_UP_CONTROL),
  55. readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
  56. printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
  57. readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
  58. readl(host->ioaddr + SDHCI_INT_STATUS));
  59. printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
  60. readl(host->ioaddr + SDHCI_INT_ENABLE),
  61. readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
  62. printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
  63. readw(host->ioaddr + SDHCI_ACMD12_ERR),
  64. readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
  65. printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
  66. readl(host->ioaddr + SDHCI_CAPABILITIES),
  67. readl(host->ioaddr + 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 (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
  80. SDHCI_CARD_PRESENT))
  81. return;
  82. }
  83. writeb(mask, host->ioaddr + 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 (readb(host->ioaddr + 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. writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
  112. writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  113. }
  114. static void sdhci_activate_led(struct sdhci_host *host)
  115. {
  116. u8 ctrl;
  117. ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
  118. ctrl |= SDHCI_CTRL_LED;
  119. writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
  120. }
  121. static void sdhci_deactivate_led(struct sdhci_host *host)
  122. {
  123. u8 ctrl;
  124. ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
  125. ctrl &= ~SDHCI_CTRL_LED;
  126. writeb(ctrl, host->ioaddr + 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 = readl(host->ioaddr + 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. writel(scratch, host->ioaddr + 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 (readl(host->ioaddr + 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. writeb(count, host->ioaddr + 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. writel(host->adma_addr,
  533. host->ioaddr + 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. writel(sg_dma_address(data->sg),
  552. host->ioaddr + 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 = readb(host->ioaddr + 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. writeb(ctrl, host->ioaddr + 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. writew(SDHCI_MAKE_BLKSZ(7, data->blksz),
  578. host->ioaddr + SDHCI_BLOCK_SIZE);
  579. writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
  580. }
  581. static void sdhci_set_transfer_mode(struct sdhci_host *host,
  582. struct mmc_data *data)
  583. {
  584. u16 mode;
  585. if (data == NULL)
  586. return;
  587. WARN_ON(!host->data);
  588. mode = SDHCI_TRNS_BLK_CNT_EN;
  589. if (data->blocks > 1)
  590. mode |= SDHCI_TRNS_MULTI;
  591. if (data->flags & MMC_DATA_READ)
  592. mode |= SDHCI_TRNS_READ;
  593. if (host->flags & SDHCI_REQ_USE_DMA)
  594. mode |= SDHCI_TRNS_DMA;
  595. writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
  596. }
  597. static void sdhci_finish_data(struct sdhci_host *host)
  598. {
  599. struct mmc_data *data;
  600. BUG_ON(!host->data);
  601. data = host->data;
  602. host->data = NULL;
  603. if (host->flags & SDHCI_REQ_USE_DMA) {
  604. if (host->flags & SDHCI_USE_ADMA)
  605. sdhci_adma_table_post(host, data);
  606. else {
  607. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  608. data->sg_len, (data->flags & MMC_DATA_READ) ?
  609. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  610. }
  611. }
  612. /*
  613. * The specification states that the block count register must
  614. * be updated, but it does not specify at what point in the
  615. * data flow. That makes the register entirely useless to read
  616. * back so we have to assume that nothing made it to the card
  617. * in the event of an error.
  618. */
  619. if (data->error)
  620. data->bytes_xfered = 0;
  621. else
  622. data->bytes_xfered = data->blksz * data->blocks;
  623. if (data->stop) {
  624. /*
  625. * The controller needs a reset of internal state machines
  626. * upon error conditions.
  627. */
  628. if (data->error) {
  629. sdhci_reset(host, SDHCI_RESET_CMD);
  630. sdhci_reset(host, SDHCI_RESET_DATA);
  631. }
  632. sdhci_send_command(host, data->stop);
  633. } else
  634. tasklet_schedule(&host->finish_tasklet);
  635. }
  636. static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
  637. {
  638. int flags;
  639. u32 mask;
  640. unsigned long timeout;
  641. WARN_ON(host->cmd);
  642. /* Wait max 10 ms */
  643. timeout = 10;
  644. mask = SDHCI_CMD_INHIBIT;
  645. if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  646. mask |= SDHCI_DATA_INHIBIT;
  647. /* We shouldn't wait for data inihibit for stop commands, even
  648. though they might use busy signaling */
  649. if (host->mrq->data && (cmd == host->mrq->data->stop))
  650. mask &= ~SDHCI_DATA_INHIBIT;
  651. while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
  652. if (timeout == 0) {
  653. printk(KERN_ERR "%s: Controller never released "
  654. "inhibit bit(s).\n", mmc_hostname(host->mmc));
  655. sdhci_dumpregs(host);
  656. cmd->error = -EIO;
  657. tasklet_schedule(&host->finish_tasklet);
  658. return;
  659. }
  660. timeout--;
  661. mdelay(1);
  662. }
  663. mod_timer(&host->timer, jiffies + 10 * HZ);
  664. host->cmd = cmd;
  665. sdhci_prepare_data(host, cmd->data);
  666. writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
  667. sdhci_set_transfer_mode(host, cmd->data);
  668. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  669. printk(KERN_ERR "%s: Unsupported response type!\n",
  670. mmc_hostname(host->mmc));
  671. cmd->error = -EINVAL;
  672. tasklet_schedule(&host->finish_tasklet);
  673. return;
  674. }
  675. if (!(cmd->flags & MMC_RSP_PRESENT))
  676. flags = SDHCI_CMD_RESP_NONE;
  677. else if (cmd->flags & MMC_RSP_136)
  678. flags = SDHCI_CMD_RESP_LONG;
  679. else if (cmd->flags & MMC_RSP_BUSY)
  680. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  681. else
  682. flags = SDHCI_CMD_RESP_SHORT;
  683. if (cmd->flags & MMC_RSP_CRC)
  684. flags |= SDHCI_CMD_CRC;
  685. if (cmd->flags & MMC_RSP_OPCODE)
  686. flags |= SDHCI_CMD_INDEX;
  687. if (cmd->data)
  688. flags |= SDHCI_CMD_DATA;
  689. writew(SDHCI_MAKE_CMD(cmd->opcode, flags),
  690. host->ioaddr + SDHCI_COMMAND);
  691. }
  692. static void sdhci_finish_command(struct sdhci_host *host)
  693. {
  694. int i;
  695. BUG_ON(host->cmd == NULL);
  696. if (host->cmd->flags & MMC_RSP_PRESENT) {
  697. if (host->cmd->flags & MMC_RSP_136) {
  698. /* CRC is stripped so we need to do some shifting. */
  699. for (i = 0;i < 4;i++) {
  700. host->cmd->resp[i] = readl(host->ioaddr +
  701. SDHCI_RESPONSE + (3-i)*4) << 8;
  702. if (i != 3)
  703. host->cmd->resp[i] |=
  704. readb(host->ioaddr +
  705. SDHCI_RESPONSE + (3-i)*4-1);
  706. }
  707. } else {
  708. host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
  709. }
  710. }
  711. host->cmd->error = 0;
  712. if (host->data && host->data_early)
  713. sdhci_finish_data(host);
  714. if (!host->cmd->data)
  715. tasklet_schedule(&host->finish_tasklet);
  716. host->cmd = NULL;
  717. }
  718. static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
  719. {
  720. int div;
  721. u16 clk;
  722. unsigned long timeout;
  723. if (clock == host->clock)
  724. return;
  725. writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
  726. if (clock == 0)
  727. goto out;
  728. for (div = 1;div < 256;div *= 2) {
  729. if ((host->max_clk / div) <= clock)
  730. break;
  731. }
  732. div >>= 1;
  733. clk = div << SDHCI_DIVIDER_SHIFT;
  734. clk |= SDHCI_CLOCK_INT_EN;
  735. writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
  736. /* Wait max 10 ms */
  737. timeout = 10;
  738. while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
  739. & SDHCI_CLOCK_INT_STABLE)) {
  740. if (timeout == 0) {
  741. printk(KERN_ERR "%s: Internal clock never "
  742. "stabilised.\n", mmc_hostname(host->mmc));
  743. sdhci_dumpregs(host);
  744. return;
  745. }
  746. timeout--;
  747. mdelay(1);
  748. }
  749. clk |= SDHCI_CLOCK_CARD_EN;
  750. writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
  751. out:
  752. host->clock = clock;
  753. }
  754. static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
  755. {
  756. u8 pwr;
  757. if (host->power == power)
  758. return;
  759. if (power == (unsigned short)-1) {
  760. writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
  761. goto out;
  762. }
  763. /*
  764. * Spec says that we should clear the power reg before setting
  765. * a new value. Some controllers don't seem to like this though.
  766. */
  767. if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
  768. writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
  769. pwr = SDHCI_POWER_ON;
  770. switch (1 << power) {
  771. case MMC_VDD_165_195:
  772. pwr |= SDHCI_POWER_180;
  773. break;
  774. case MMC_VDD_29_30:
  775. case MMC_VDD_30_31:
  776. pwr |= SDHCI_POWER_300;
  777. break;
  778. case MMC_VDD_32_33:
  779. case MMC_VDD_33_34:
  780. pwr |= SDHCI_POWER_330;
  781. break;
  782. default:
  783. BUG();
  784. }
  785. /*
  786. * At least the Marvell CaFe chip gets confused if we set the voltage
  787. * and set turn on power at the same time, so set the voltage first.
  788. */
  789. if ((host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER))
  790. writeb(pwr & ~SDHCI_POWER_ON,
  791. host->ioaddr + SDHCI_POWER_CONTROL);
  792. writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
  793. out:
  794. host->power = power;
  795. }
  796. /*****************************************************************************\
  797. * *
  798. * MMC callbacks *
  799. * *
  800. \*****************************************************************************/
  801. static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  802. {
  803. struct sdhci_host *host;
  804. unsigned long flags;
  805. host = mmc_priv(mmc);
  806. spin_lock_irqsave(&host->lock, flags);
  807. WARN_ON(host->mrq != NULL);
  808. #ifndef SDHCI_USE_LEDS_CLASS
  809. sdhci_activate_led(host);
  810. #endif
  811. host->mrq = mrq;
  812. if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)
  813. || (host->flags & SDHCI_DEVICE_DEAD)) {
  814. host->mrq->cmd->error = -ENOMEDIUM;
  815. tasklet_schedule(&host->finish_tasklet);
  816. } else
  817. sdhci_send_command(host, mrq->cmd);
  818. mmiowb();
  819. spin_unlock_irqrestore(&host->lock, flags);
  820. }
  821. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  822. {
  823. struct sdhci_host *host;
  824. unsigned long flags;
  825. u8 ctrl;
  826. host = mmc_priv(mmc);
  827. spin_lock_irqsave(&host->lock, flags);
  828. if (host->flags & SDHCI_DEVICE_DEAD)
  829. goto out;
  830. /*
  831. * Reset the chip on each power off.
  832. * Should clear out any weird states.
  833. */
  834. if (ios->power_mode == MMC_POWER_OFF) {
  835. writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  836. sdhci_init(host);
  837. }
  838. sdhci_set_clock(host, ios->clock);
  839. if (ios->power_mode == MMC_POWER_OFF)
  840. sdhci_set_power(host, -1);
  841. else
  842. sdhci_set_power(host, ios->vdd);
  843. ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
  844. if (ios->bus_width == MMC_BUS_WIDTH_4)
  845. ctrl |= SDHCI_CTRL_4BITBUS;
  846. else
  847. ctrl &= ~SDHCI_CTRL_4BITBUS;
  848. if (ios->timing == MMC_TIMING_SD_HS)
  849. ctrl |= SDHCI_CTRL_HISPD;
  850. else
  851. ctrl &= ~SDHCI_CTRL_HISPD;
  852. writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
  853. /*
  854. * Some (ENE) controllers go apeshit on some ios operation,
  855. * signalling timeout and CRC errors even on CMD0. Resetting
  856. * it on each ios seems to solve the problem.
  857. */
  858. if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
  859. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  860. out:
  861. mmiowb();
  862. spin_unlock_irqrestore(&host->lock, flags);
  863. }
  864. static int sdhci_get_ro(struct mmc_host *mmc)
  865. {
  866. struct sdhci_host *host;
  867. unsigned long flags;
  868. int present;
  869. host = mmc_priv(mmc);
  870. spin_lock_irqsave(&host->lock, flags);
  871. if (host->flags & SDHCI_DEVICE_DEAD)
  872. present = 0;
  873. else
  874. present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
  875. spin_unlock_irqrestore(&host->lock, flags);
  876. return !(present & SDHCI_WRITE_PROTECT);
  877. }
  878. static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  879. {
  880. struct sdhci_host *host;
  881. unsigned long flags;
  882. u32 ier;
  883. host = mmc_priv(mmc);
  884. spin_lock_irqsave(&host->lock, flags);
  885. if (host->flags & SDHCI_DEVICE_DEAD)
  886. goto out;
  887. ier = readl(host->ioaddr + SDHCI_INT_ENABLE);
  888. ier &= ~SDHCI_INT_CARD_INT;
  889. if (enable)
  890. ier |= SDHCI_INT_CARD_INT;
  891. writel(ier, host->ioaddr + SDHCI_INT_ENABLE);
  892. writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  893. out:
  894. mmiowb();
  895. spin_unlock_irqrestore(&host->lock, flags);
  896. }
  897. static const struct mmc_host_ops sdhci_ops = {
  898. .request = sdhci_request,
  899. .set_ios = sdhci_set_ios,
  900. .get_ro = sdhci_get_ro,
  901. .enable_sdio_irq = sdhci_enable_sdio_irq,
  902. };
  903. /*****************************************************************************\
  904. * *
  905. * Tasklets *
  906. * *
  907. \*****************************************************************************/
  908. static void sdhci_tasklet_card(unsigned long param)
  909. {
  910. struct sdhci_host *host;
  911. unsigned long flags;
  912. host = (struct sdhci_host*)param;
  913. spin_lock_irqsave(&host->lock, flags);
  914. if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
  915. if (host->mrq) {
  916. printk(KERN_ERR "%s: Card removed during transfer!\n",
  917. mmc_hostname(host->mmc));
  918. printk(KERN_ERR "%s: Resetting controller.\n",
  919. mmc_hostname(host->mmc));
  920. sdhci_reset(host, SDHCI_RESET_CMD);
  921. sdhci_reset(host, SDHCI_RESET_DATA);
  922. host->mrq->cmd->error = -ENOMEDIUM;
  923. tasklet_schedule(&host->finish_tasklet);
  924. }
  925. }
  926. spin_unlock_irqrestore(&host->lock, flags);
  927. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  928. }
  929. static void sdhci_tasklet_finish(unsigned long param)
  930. {
  931. struct sdhci_host *host;
  932. unsigned long flags;
  933. struct mmc_request *mrq;
  934. host = (struct sdhci_host*)param;
  935. spin_lock_irqsave(&host->lock, flags);
  936. del_timer(&host->timer);
  937. mrq = host->mrq;
  938. /*
  939. * The controller needs a reset of internal state machines
  940. * upon error conditions.
  941. */
  942. if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  943. (mrq->cmd->error ||
  944. (mrq->data && (mrq->data->error ||
  945. (mrq->data->stop && mrq->data->stop->error))) ||
  946. (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
  947. /* Some controllers need this kick or reset won't work here */
  948. if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
  949. unsigned int clock;
  950. /* This is to force an update */
  951. clock = host->clock;
  952. host->clock = 0;
  953. sdhci_set_clock(host, clock);
  954. }
  955. /* Spec says we should do both at the same time, but Ricoh
  956. controllers do not like that. */
  957. sdhci_reset(host, SDHCI_RESET_CMD);
  958. sdhci_reset(host, SDHCI_RESET_DATA);
  959. }
  960. host->mrq = NULL;
  961. host->cmd = NULL;
  962. host->data = NULL;
  963. #ifndef SDHCI_USE_LEDS_CLASS
  964. sdhci_deactivate_led(host);
  965. #endif
  966. mmiowb();
  967. spin_unlock_irqrestore(&host->lock, flags);
  968. mmc_request_done(host->mmc, mrq);
  969. }
  970. static void sdhci_timeout_timer(unsigned long data)
  971. {
  972. struct sdhci_host *host;
  973. unsigned long flags;
  974. host = (struct sdhci_host*)data;
  975. spin_lock_irqsave(&host->lock, flags);
  976. if (host->mrq) {
  977. printk(KERN_ERR "%s: Timeout waiting for hardware "
  978. "interrupt.\n", mmc_hostname(host->mmc));
  979. sdhci_dumpregs(host);
  980. if (host->data) {
  981. host->data->error = -ETIMEDOUT;
  982. sdhci_finish_data(host);
  983. } else {
  984. if (host->cmd)
  985. host->cmd->error = -ETIMEDOUT;
  986. else
  987. host->mrq->cmd->error = -ETIMEDOUT;
  988. tasklet_schedule(&host->finish_tasklet);
  989. }
  990. }
  991. mmiowb();
  992. spin_unlock_irqrestore(&host->lock, flags);
  993. }
  994. /*****************************************************************************\
  995. * *
  996. * Interrupt handling *
  997. * *
  998. \*****************************************************************************/
  999. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
  1000. {
  1001. BUG_ON(intmask == 0);
  1002. if (!host->cmd) {
  1003. printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
  1004. "though no command operation was in progress.\n",
  1005. mmc_hostname(host->mmc), (unsigned)intmask);
  1006. sdhci_dumpregs(host);
  1007. return;
  1008. }
  1009. if (intmask & SDHCI_INT_TIMEOUT)
  1010. host->cmd->error = -ETIMEDOUT;
  1011. else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  1012. SDHCI_INT_INDEX))
  1013. host->cmd->error = -EILSEQ;
  1014. if (host->cmd->error) {
  1015. tasklet_schedule(&host->finish_tasklet);
  1016. return;
  1017. }
  1018. /*
  1019. * The host can send and interrupt when the busy state has
  1020. * ended, allowing us to wait without wasting CPU cycles.
  1021. * Unfortunately this is overloaded on the "data complete"
  1022. * interrupt, so we need to take some care when handling
  1023. * it.
  1024. *
  1025. * Note: The 1.0 specification is a bit ambiguous about this
  1026. * feature so there might be some problems with older
  1027. * controllers.
  1028. */
  1029. if (host->cmd->flags & MMC_RSP_BUSY) {
  1030. if (host->cmd->data)
  1031. DBG("Cannot wait for busy signal when also "
  1032. "doing a data transfer");
  1033. else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
  1034. return;
  1035. /* The controller does not support the end-of-busy IRQ,
  1036. * fall through and take the SDHCI_INT_RESPONSE */
  1037. }
  1038. if (intmask & SDHCI_INT_RESPONSE)
  1039. sdhci_finish_command(host);
  1040. }
  1041. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  1042. {
  1043. BUG_ON(intmask == 0);
  1044. if (!host->data) {
  1045. /*
  1046. * The "data complete" interrupt is also used to
  1047. * indicate that a busy state has ended. See comment
  1048. * above in sdhci_cmd_irq().
  1049. */
  1050. if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  1051. if (intmask & SDHCI_INT_DATA_END) {
  1052. sdhci_finish_command(host);
  1053. return;
  1054. }
  1055. }
  1056. printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
  1057. "though no data operation was in progress.\n",
  1058. mmc_hostname(host->mmc), (unsigned)intmask);
  1059. sdhci_dumpregs(host);
  1060. return;
  1061. }
  1062. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  1063. host->data->error = -ETIMEDOUT;
  1064. else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
  1065. host->data->error = -EILSEQ;
  1066. else if (intmask & SDHCI_INT_ADMA_ERROR)
  1067. host->data->error = -EIO;
  1068. if (host->data->error)
  1069. sdhci_finish_data(host);
  1070. else {
  1071. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  1072. sdhci_transfer_pio(host);
  1073. /*
  1074. * We currently don't do anything fancy with DMA
  1075. * boundaries, but as we can't disable the feature
  1076. * we need to at least restart the transfer.
  1077. */
  1078. if (intmask & SDHCI_INT_DMA_END)
  1079. writel(readl(host->ioaddr + SDHCI_DMA_ADDRESS),
  1080. host->ioaddr + SDHCI_DMA_ADDRESS);
  1081. if (intmask & SDHCI_INT_DATA_END) {
  1082. if (host->cmd) {
  1083. /*
  1084. * Data managed to finish before the
  1085. * command completed. Make sure we do
  1086. * things in the proper order.
  1087. */
  1088. host->data_early = 1;
  1089. } else {
  1090. sdhci_finish_data(host);
  1091. }
  1092. }
  1093. }
  1094. }
  1095. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  1096. {
  1097. irqreturn_t result;
  1098. struct sdhci_host* host = dev_id;
  1099. u32 intmask;
  1100. int cardint = 0;
  1101. spin_lock(&host->lock);
  1102. intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
  1103. if (!intmask || intmask == 0xffffffff) {
  1104. result = IRQ_NONE;
  1105. goto out;
  1106. }
  1107. DBG("*** %s got interrupt: 0x%08x\n",
  1108. mmc_hostname(host->mmc), intmask);
  1109. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  1110. writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
  1111. host->ioaddr + SDHCI_INT_STATUS);
  1112. tasklet_schedule(&host->card_tasklet);
  1113. }
  1114. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
  1115. if (intmask & SDHCI_INT_CMD_MASK) {
  1116. writel(intmask & SDHCI_INT_CMD_MASK,
  1117. host->ioaddr + SDHCI_INT_STATUS);
  1118. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  1119. }
  1120. if (intmask & SDHCI_INT_DATA_MASK) {
  1121. writel(intmask & SDHCI_INT_DATA_MASK,
  1122. host->ioaddr + SDHCI_INT_STATUS);
  1123. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  1124. }
  1125. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  1126. intmask &= ~SDHCI_INT_ERROR;
  1127. if (intmask & SDHCI_INT_BUS_POWER) {
  1128. printk(KERN_ERR "%s: Card is consuming too much power!\n",
  1129. mmc_hostname(host->mmc));
  1130. writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
  1131. }
  1132. intmask &= ~SDHCI_INT_BUS_POWER;
  1133. if (intmask & SDHCI_INT_CARD_INT)
  1134. cardint = 1;
  1135. intmask &= ~SDHCI_INT_CARD_INT;
  1136. if (intmask) {
  1137. printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
  1138. mmc_hostname(host->mmc), intmask);
  1139. sdhci_dumpregs(host);
  1140. writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
  1141. }
  1142. result = IRQ_HANDLED;
  1143. mmiowb();
  1144. out:
  1145. spin_unlock(&host->lock);
  1146. /*
  1147. * We have to delay this as it calls back into the driver.
  1148. */
  1149. if (cardint)
  1150. mmc_signal_sdio_irq(host->mmc);
  1151. return result;
  1152. }
  1153. /*****************************************************************************\
  1154. * *
  1155. * Suspend/resume *
  1156. * *
  1157. \*****************************************************************************/
  1158. #ifdef CONFIG_PM
  1159. int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
  1160. {
  1161. int ret;
  1162. ret = mmc_suspend_host(host->mmc, state);
  1163. if (ret)
  1164. return ret;
  1165. free_irq(host->irq, host);
  1166. return 0;
  1167. }
  1168. EXPORT_SYMBOL_GPL(sdhci_suspend_host);
  1169. int sdhci_resume_host(struct sdhci_host *host)
  1170. {
  1171. int ret;
  1172. if (host->flags & SDHCI_USE_DMA) {
  1173. if (host->ops->enable_dma)
  1174. host->ops->enable_dma(host);
  1175. }
  1176. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  1177. mmc_hostname(host->mmc), host);
  1178. if (ret)
  1179. return ret;
  1180. sdhci_init(host);
  1181. mmiowb();
  1182. ret = mmc_resume_host(host->mmc);
  1183. if (ret)
  1184. return ret;
  1185. return 0;
  1186. }
  1187. EXPORT_SYMBOL_GPL(sdhci_resume_host);
  1188. #endif /* CONFIG_PM */
  1189. /*****************************************************************************\
  1190. * *
  1191. * Device allocation/registration *
  1192. * *
  1193. \*****************************************************************************/
  1194. struct sdhci_host *sdhci_alloc_host(struct device *dev,
  1195. size_t priv_size)
  1196. {
  1197. struct mmc_host *mmc;
  1198. struct sdhci_host *host;
  1199. WARN_ON(dev == NULL);
  1200. mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
  1201. if (!mmc)
  1202. return ERR_PTR(-ENOMEM);
  1203. host = mmc_priv(mmc);
  1204. host->mmc = mmc;
  1205. return host;
  1206. }
  1207. EXPORT_SYMBOL_GPL(sdhci_alloc_host);
  1208. int sdhci_add_host(struct sdhci_host *host)
  1209. {
  1210. struct mmc_host *mmc;
  1211. unsigned int caps;
  1212. int ret;
  1213. WARN_ON(host == NULL);
  1214. if (host == NULL)
  1215. return -EINVAL;
  1216. mmc = host->mmc;
  1217. if (debug_quirks)
  1218. host->quirks = debug_quirks;
  1219. sdhci_reset(host, SDHCI_RESET_ALL);
  1220. host->version = readw(host->ioaddr + SDHCI_HOST_VERSION);
  1221. host->version = (host->version & SDHCI_SPEC_VER_MASK)
  1222. >> SDHCI_SPEC_VER_SHIFT;
  1223. if (host->version > SDHCI_SPEC_200) {
  1224. printk(KERN_ERR "%s: Unknown controller version (%d). "
  1225. "You may experience problems.\n", mmc_hostname(mmc),
  1226. host->version);
  1227. }
  1228. caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
  1229. if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
  1230. host->flags |= SDHCI_USE_DMA;
  1231. else if (!(caps & SDHCI_CAN_DO_DMA))
  1232. DBG("Controller doesn't have DMA capability\n");
  1233. else
  1234. host->flags |= SDHCI_USE_DMA;
  1235. if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
  1236. (host->flags & SDHCI_USE_DMA)) {
  1237. DBG("Disabling DMA as it is marked broken\n");
  1238. host->flags &= ~SDHCI_USE_DMA;
  1239. }
  1240. if (host->flags & SDHCI_USE_DMA) {
  1241. if ((host->version >= SDHCI_SPEC_200) &&
  1242. (caps & SDHCI_CAN_DO_ADMA2))
  1243. host->flags |= SDHCI_USE_ADMA;
  1244. }
  1245. if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
  1246. (host->flags & SDHCI_USE_ADMA)) {
  1247. DBG("Disabling ADMA as it is marked broken\n");
  1248. host->flags &= ~SDHCI_USE_ADMA;
  1249. }
  1250. if (host->flags & SDHCI_USE_DMA) {
  1251. if (host->ops->enable_dma) {
  1252. if (host->ops->enable_dma(host)) {
  1253. printk(KERN_WARNING "%s: No suitable DMA "
  1254. "available. Falling back to PIO.\n",
  1255. mmc_hostname(mmc));
  1256. host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
  1257. }
  1258. }
  1259. }
  1260. if (host->flags & SDHCI_USE_ADMA) {
  1261. /*
  1262. * We need to allocate descriptors for all sg entries
  1263. * (128) and potentially one alignment transfer for
  1264. * each of those entries.
  1265. */
  1266. host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
  1267. host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
  1268. if (!host->adma_desc || !host->align_buffer) {
  1269. kfree(host->adma_desc);
  1270. kfree(host->align_buffer);
  1271. printk(KERN_WARNING "%s: Unable to allocate ADMA "
  1272. "buffers. Falling back to standard DMA.\n",
  1273. mmc_hostname(mmc));
  1274. host->flags &= ~SDHCI_USE_ADMA;
  1275. }
  1276. }
  1277. /*
  1278. * If we use DMA, then it's up to the caller to set the DMA
  1279. * mask, but PIO does not need the hw shim so we set a new
  1280. * mask here in that case.
  1281. */
  1282. if (!(host->flags & SDHCI_USE_DMA)) {
  1283. host->dma_mask = DMA_BIT_MASK(64);
  1284. mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
  1285. }
  1286. host->max_clk =
  1287. (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
  1288. if (host->max_clk == 0) {
  1289. printk(KERN_ERR "%s: Hardware doesn't specify base clock "
  1290. "frequency.\n", mmc_hostname(mmc));
  1291. return -ENODEV;
  1292. }
  1293. host->max_clk *= 1000000;
  1294. host->timeout_clk =
  1295. (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
  1296. if (host->timeout_clk == 0) {
  1297. printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
  1298. "frequency.\n", mmc_hostname(mmc));
  1299. return -ENODEV;
  1300. }
  1301. if (caps & SDHCI_TIMEOUT_CLK_UNIT)
  1302. host->timeout_clk *= 1000;
  1303. /*
  1304. * Set host parameters.
  1305. */
  1306. mmc->ops = &sdhci_ops;
  1307. mmc->f_min = host->max_clk / 256;
  1308. mmc->f_max = host->max_clk;
  1309. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  1310. if (caps & SDHCI_CAN_DO_HISPD)
  1311. mmc->caps |= MMC_CAP_SD_HIGHSPEED;
  1312. mmc->ocr_avail = 0;
  1313. if (caps & SDHCI_CAN_VDD_330)
  1314. mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
  1315. if (caps & SDHCI_CAN_VDD_300)
  1316. mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
  1317. if (caps & SDHCI_CAN_VDD_180)
  1318. mmc->ocr_avail |= MMC_VDD_165_195;
  1319. if (mmc->ocr_avail == 0) {
  1320. printk(KERN_ERR "%s: Hardware doesn't report any "
  1321. "support voltages.\n", mmc_hostname(mmc));
  1322. return -ENODEV;
  1323. }
  1324. spin_lock_init(&host->lock);
  1325. /*
  1326. * Maximum number of segments. Depends on if the hardware
  1327. * can do scatter/gather or not.
  1328. */
  1329. if (host->flags & SDHCI_USE_ADMA)
  1330. mmc->max_hw_segs = 128;
  1331. else if (host->flags & SDHCI_USE_DMA)
  1332. mmc->max_hw_segs = 1;
  1333. else /* PIO */
  1334. mmc->max_hw_segs = 128;
  1335. mmc->max_phys_segs = 128;
  1336. /*
  1337. * Maximum number of sectors in one transfer. Limited by DMA boundary
  1338. * size (512KiB).
  1339. */
  1340. mmc->max_req_size = 524288;
  1341. /*
  1342. * Maximum segment size. Could be one segment with the maximum number
  1343. * of bytes. When doing hardware scatter/gather, each entry cannot
  1344. * be larger than 64 KiB though.
  1345. */
  1346. if (host->flags & SDHCI_USE_ADMA)
  1347. mmc->max_seg_size = 65536;
  1348. else
  1349. mmc->max_seg_size = mmc->max_req_size;
  1350. /*
  1351. * Maximum block size. This varies from controller to controller and
  1352. * is specified in the capabilities register.
  1353. */
  1354. mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
  1355. if (mmc->max_blk_size >= 3) {
  1356. printk(KERN_WARNING "%s: Invalid maximum block size, "
  1357. "assuming 512 bytes\n", mmc_hostname(mmc));
  1358. mmc->max_blk_size = 512;
  1359. } else
  1360. mmc->max_blk_size = 512 << mmc->max_blk_size;
  1361. /*
  1362. * Maximum block count.
  1363. */
  1364. mmc->max_blk_count = 65535;
  1365. /*
  1366. * Init tasklets.
  1367. */
  1368. tasklet_init(&host->card_tasklet,
  1369. sdhci_tasklet_card, (unsigned long)host);
  1370. tasklet_init(&host->finish_tasklet,
  1371. sdhci_tasklet_finish, (unsigned long)host);
  1372. setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
  1373. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  1374. mmc_hostname(mmc), host);
  1375. if (ret)
  1376. goto untasklet;
  1377. sdhci_init(host);
  1378. #ifdef CONFIG_MMC_DEBUG
  1379. sdhci_dumpregs(host);
  1380. #endif
  1381. #ifdef SDHCI_USE_LEDS_CLASS
  1382. snprintf(host->led_name, sizeof(host->led_name),
  1383. "%s::", mmc_hostname(mmc));
  1384. host->led.name = host->led_name;
  1385. host->led.brightness = LED_OFF;
  1386. host->led.default_trigger = mmc_hostname(mmc);
  1387. host->led.brightness_set = sdhci_led_control;
  1388. ret = led_classdev_register(mmc_dev(mmc), &host->led);
  1389. if (ret)
  1390. goto reset;
  1391. #endif
  1392. mmiowb();
  1393. mmc_add_host(mmc);
  1394. printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n",
  1395. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  1396. (host->flags & SDHCI_USE_ADMA)?"A":"",
  1397. (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
  1398. return 0;
  1399. #ifdef SDHCI_USE_LEDS_CLASS
  1400. reset:
  1401. sdhci_reset(host, SDHCI_RESET_ALL);
  1402. free_irq(host->irq, host);
  1403. #endif
  1404. untasklet:
  1405. tasklet_kill(&host->card_tasklet);
  1406. tasklet_kill(&host->finish_tasklet);
  1407. return ret;
  1408. }
  1409. EXPORT_SYMBOL_GPL(sdhci_add_host);
  1410. void sdhci_remove_host(struct sdhci_host *host, int dead)
  1411. {
  1412. unsigned long flags;
  1413. if (dead) {
  1414. spin_lock_irqsave(&host->lock, flags);
  1415. host->flags |= SDHCI_DEVICE_DEAD;
  1416. if (host->mrq) {
  1417. printk(KERN_ERR "%s: Controller removed during "
  1418. " transfer!\n", mmc_hostname(host->mmc));
  1419. host->mrq->cmd->error = -ENOMEDIUM;
  1420. tasklet_schedule(&host->finish_tasklet);
  1421. }
  1422. spin_unlock_irqrestore(&host->lock, flags);
  1423. }
  1424. mmc_remove_host(host->mmc);
  1425. #ifdef SDHCI_USE_LEDS_CLASS
  1426. led_classdev_unregister(&host->led);
  1427. #endif
  1428. if (!dead)
  1429. sdhci_reset(host, SDHCI_RESET_ALL);
  1430. free_irq(host->irq, host);
  1431. del_timer_sync(&host->timer);
  1432. tasklet_kill(&host->card_tasklet);
  1433. tasklet_kill(&host->finish_tasklet);
  1434. kfree(host->adma_desc);
  1435. kfree(host->align_buffer);
  1436. host->adma_desc = NULL;
  1437. host->align_buffer = NULL;
  1438. }
  1439. EXPORT_SYMBOL_GPL(sdhci_remove_host);
  1440. void sdhci_free_host(struct sdhci_host *host)
  1441. {
  1442. mmc_free_host(host->mmc);
  1443. }
  1444. EXPORT_SYMBOL_GPL(sdhci_free_host);
  1445. /*****************************************************************************\
  1446. * *
  1447. * Driver init/exit *
  1448. * *
  1449. \*****************************************************************************/
  1450. static int __init sdhci_drv_init(void)
  1451. {
  1452. printk(KERN_INFO DRIVER_NAME
  1453. ": Secure Digital Host Controller Interface driver\n");
  1454. printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  1455. return 0;
  1456. }
  1457. static void __exit sdhci_drv_exit(void)
  1458. {
  1459. }
  1460. module_init(sdhci_drv_init);
  1461. module_exit(sdhci_drv_exit);
  1462. module_param(debug_quirks, uint, 0444);
  1463. MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
  1464. MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
  1465. MODULE_LICENSE("GPL");
  1466. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");