sdhci.c 44 KB

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