sdhci.c 52 KB

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