sdhci.c 51 KB

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