sdhci.c 45 KB

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