sdhci.c 48 KB

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