sdhci.c 44 KB

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