sdhci.c 43 KB

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