sdhci.c 50 KB

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