sdhci.c 43 KB

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