sdhci.c 51 KB

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