sdhci.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. /*
  2. * linux/drivers/mmc/sdhci.c - Secure Digital Host Controller Interface driver
  3. *
  4. * Copyright (C) 2005-2006 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. #include <linux/delay.h>
  12. #include <linux/highmem.h>
  13. #include <linux/pci.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/mmc/host.h>
  16. #include <linux/mmc/protocol.h>
  17. #include <asm/scatterlist.h>
  18. #include "sdhci.h"
  19. #define DRIVER_NAME "sdhci"
  20. #define DRIVER_VERSION "0.12"
  21. #define BUGMAIL "<sdhci-devel@list.drzeus.cx>"
  22. #define DBG(f, x...) \
  23. pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
  24. static unsigned int debug_nodma = 0;
  25. static unsigned int debug_forcedma = 0;
  26. static unsigned int debug_quirks = 0;
  27. #define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0)
  28. #define SDHCI_QUIRK_FORCE_DMA (1<<1)
  29. /* Controller doesn't like some resets when there is no card inserted. */
  30. #define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<2)
  31. #define SDHCI_QUIRK_SINGLE_POWER_WRITE (1<<3)
  32. static const struct pci_device_id pci_ids[] __devinitdata = {
  33. {
  34. .vendor = PCI_VENDOR_ID_RICOH,
  35. .device = PCI_DEVICE_ID_RICOH_R5C822,
  36. .subvendor = PCI_VENDOR_ID_IBM,
  37. .subdevice = PCI_ANY_ID,
  38. .driver_data = SDHCI_QUIRK_CLOCK_BEFORE_RESET |
  39. SDHCI_QUIRK_FORCE_DMA,
  40. },
  41. {
  42. .vendor = PCI_VENDOR_ID_RICOH,
  43. .device = PCI_DEVICE_ID_RICOH_R5C822,
  44. .subvendor = PCI_ANY_ID,
  45. .subdevice = PCI_ANY_ID,
  46. .driver_data = SDHCI_QUIRK_FORCE_DMA |
  47. SDHCI_QUIRK_NO_CARD_NO_RESET,
  48. },
  49. {
  50. .vendor = PCI_VENDOR_ID_TI,
  51. .device = PCI_DEVICE_ID_TI_XX21_XX11_SD,
  52. .subvendor = PCI_ANY_ID,
  53. .subdevice = PCI_ANY_ID,
  54. .driver_data = SDHCI_QUIRK_FORCE_DMA,
  55. },
  56. {
  57. .vendor = PCI_VENDOR_ID_ENE,
  58. .device = PCI_DEVICE_ID_ENE_CB712_SD,
  59. .subvendor = PCI_ANY_ID,
  60. .subdevice = PCI_ANY_ID,
  61. .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE,
  62. },
  63. { /* Generic SD host controller */
  64. PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
  65. },
  66. { /* end: all zeroes */ },
  67. };
  68. MODULE_DEVICE_TABLE(pci, pci_ids);
  69. static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
  70. static void sdhci_finish_data(struct sdhci_host *);
  71. static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
  72. static void sdhci_finish_command(struct sdhci_host *);
  73. static void sdhci_dumpregs(struct sdhci_host *host)
  74. {
  75. printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
  76. printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
  77. readl(host->ioaddr + SDHCI_DMA_ADDRESS),
  78. readw(host->ioaddr + SDHCI_HOST_VERSION));
  79. printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  80. readw(host->ioaddr + SDHCI_BLOCK_SIZE),
  81. readw(host->ioaddr + SDHCI_BLOCK_COUNT));
  82. printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
  83. readl(host->ioaddr + SDHCI_ARGUMENT),
  84. readw(host->ioaddr + SDHCI_TRANSFER_MODE));
  85. printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
  86. readl(host->ioaddr + SDHCI_PRESENT_STATE),
  87. readb(host->ioaddr + SDHCI_HOST_CONTROL));
  88. printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
  89. readb(host->ioaddr + SDHCI_POWER_CONTROL),
  90. readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
  91. printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
  92. readb(host->ioaddr + SDHCI_WALK_UP_CONTROL),
  93. readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
  94. printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
  95. readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
  96. readl(host->ioaddr + SDHCI_INT_STATUS));
  97. printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
  98. readl(host->ioaddr + SDHCI_INT_ENABLE),
  99. readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
  100. printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
  101. readw(host->ioaddr + SDHCI_ACMD12_ERR),
  102. readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
  103. printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
  104. readl(host->ioaddr + SDHCI_CAPABILITIES),
  105. readl(host->ioaddr + SDHCI_MAX_CURRENT));
  106. printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
  107. }
  108. /*****************************************************************************\
  109. * *
  110. * Low level functions *
  111. * *
  112. \*****************************************************************************/
  113. static void sdhci_reset(struct sdhci_host *host, u8 mask)
  114. {
  115. unsigned long timeout;
  116. if (host->chip->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
  117. if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
  118. SDHCI_CARD_PRESENT))
  119. return;
  120. }
  121. writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
  122. if (mask & SDHCI_RESET_ALL)
  123. host->clock = 0;
  124. /* Wait max 100 ms */
  125. timeout = 100;
  126. /* hw clears the bit when it's done */
  127. while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) {
  128. if (timeout == 0) {
  129. printk(KERN_ERR "%s: Reset 0x%x never completed. "
  130. "Please report this to " BUGMAIL ".\n",
  131. mmc_hostname(host->mmc), (int)mask);
  132. sdhci_dumpregs(host);
  133. return;
  134. }
  135. timeout--;
  136. mdelay(1);
  137. }
  138. }
  139. static void sdhci_init(struct sdhci_host *host)
  140. {
  141. u32 intmask;
  142. sdhci_reset(host, SDHCI_RESET_ALL);
  143. intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
  144. SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
  145. SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
  146. SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
  147. SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
  148. SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE;
  149. writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
  150. writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  151. }
  152. static void sdhci_activate_led(struct sdhci_host *host)
  153. {
  154. u8 ctrl;
  155. ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
  156. ctrl |= SDHCI_CTRL_LED;
  157. writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
  158. }
  159. static void sdhci_deactivate_led(struct sdhci_host *host)
  160. {
  161. u8 ctrl;
  162. ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
  163. ctrl &= ~SDHCI_CTRL_LED;
  164. writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
  165. }
  166. /*****************************************************************************\
  167. * *
  168. * Core functions *
  169. * *
  170. \*****************************************************************************/
  171. static inline char* sdhci_kmap_sg(struct sdhci_host* host)
  172. {
  173. host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ);
  174. return host->mapped_sg + host->cur_sg->offset;
  175. }
  176. static inline void sdhci_kunmap_sg(struct sdhci_host* host)
  177. {
  178. kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
  179. }
  180. static inline int sdhci_next_sg(struct sdhci_host* host)
  181. {
  182. /*
  183. * Skip to next SG entry.
  184. */
  185. host->cur_sg++;
  186. host->num_sg--;
  187. /*
  188. * Any entries left?
  189. */
  190. if (host->num_sg > 0) {
  191. host->offset = 0;
  192. host->remain = host->cur_sg->length;
  193. }
  194. return host->num_sg;
  195. }
  196. static void sdhci_read_block_pio(struct sdhci_host *host)
  197. {
  198. int blksize, chunk_remain;
  199. u32 data;
  200. char *buffer;
  201. int size;
  202. DBG("PIO reading\n");
  203. blksize = host->data->blksz;
  204. chunk_remain = 0;
  205. data = 0;
  206. buffer = sdhci_kmap_sg(host) + host->offset;
  207. while (blksize) {
  208. if (chunk_remain == 0) {
  209. data = readl(host->ioaddr + SDHCI_BUFFER);
  210. chunk_remain = min(blksize, 4);
  211. }
  212. size = min(host->size, host->remain);
  213. size = min(size, chunk_remain);
  214. chunk_remain -= size;
  215. blksize -= size;
  216. host->offset += size;
  217. host->remain -= size;
  218. host->size -= size;
  219. while (size) {
  220. *buffer = data & 0xFF;
  221. buffer++;
  222. data >>= 8;
  223. size--;
  224. }
  225. if (host->remain == 0) {
  226. sdhci_kunmap_sg(host);
  227. if (sdhci_next_sg(host) == 0) {
  228. BUG_ON(blksize != 0);
  229. return;
  230. }
  231. buffer = sdhci_kmap_sg(host);
  232. }
  233. }
  234. sdhci_kunmap_sg(host);
  235. }
  236. static void sdhci_write_block_pio(struct sdhci_host *host)
  237. {
  238. int blksize, chunk_remain;
  239. u32 data;
  240. char *buffer;
  241. int bytes, size;
  242. DBG("PIO writing\n");
  243. blksize = host->data->blksz;
  244. chunk_remain = 4;
  245. data = 0;
  246. bytes = 0;
  247. buffer = sdhci_kmap_sg(host) + host->offset;
  248. while (blksize) {
  249. size = min(host->size, host->remain);
  250. size = min(size, chunk_remain);
  251. chunk_remain -= size;
  252. blksize -= size;
  253. host->offset += size;
  254. host->remain -= size;
  255. host->size -= size;
  256. while (size) {
  257. data >>= 8;
  258. data |= (u32)*buffer << 24;
  259. buffer++;
  260. size--;
  261. }
  262. if (chunk_remain == 0) {
  263. writel(data, host->ioaddr + SDHCI_BUFFER);
  264. chunk_remain = min(blksize, 4);
  265. }
  266. if (host->remain == 0) {
  267. sdhci_kunmap_sg(host);
  268. if (sdhci_next_sg(host) == 0) {
  269. BUG_ON(blksize != 0);
  270. return;
  271. }
  272. buffer = sdhci_kmap_sg(host);
  273. }
  274. }
  275. sdhci_kunmap_sg(host);
  276. }
  277. static void sdhci_transfer_pio(struct sdhci_host *host)
  278. {
  279. u32 mask;
  280. BUG_ON(!host->data);
  281. if (host->size == 0)
  282. return;
  283. if (host->data->flags & MMC_DATA_READ)
  284. mask = SDHCI_DATA_AVAILABLE;
  285. else
  286. mask = SDHCI_SPACE_AVAILABLE;
  287. while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
  288. if (host->data->flags & MMC_DATA_READ)
  289. sdhci_read_block_pio(host);
  290. else
  291. sdhci_write_block_pio(host);
  292. if (host->size == 0)
  293. break;
  294. BUG_ON(host->num_sg == 0);
  295. }
  296. DBG("PIO transfer complete.\n");
  297. }
  298. static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
  299. {
  300. u8 count;
  301. unsigned target_timeout, current_timeout;
  302. WARN_ON(host->data);
  303. if (data == NULL)
  304. return;
  305. DBG("blksz %04x blks %04x flags %08x\n",
  306. data->blksz, data->blocks, data->flags);
  307. DBG("tsac %d ms nsac %d clk\n",
  308. data->timeout_ns / 1000000, data->timeout_clks);
  309. /* Sanity checks */
  310. BUG_ON(data->blksz * data->blocks > 524288);
  311. BUG_ON(data->blksz > host->mmc->max_blk_size);
  312. BUG_ON(data->blocks > 65535);
  313. /* timeout in us */
  314. target_timeout = data->timeout_ns / 1000 +
  315. data->timeout_clks / host->clock;
  316. /*
  317. * Figure out needed cycles.
  318. * We do this in steps in order to fit inside a 32 bit int.
  319. * The first step is the minimum timeout, which will have a
  320. * minimum resolution of 6 bits:
  321. * (1) 2^13*1000 > 2^22,
  322. * (2) host->timeout_clk < 2^16
  323. * =>
  324. * (1) / (2) > 2^6
  325. */
  326. count = 0;
  327. current_timeout = (1 << 13) * 1000 / host->timeout_clk;
  328. while (current_timeout < target_timeout) {
  329. count++;
  330. current_timeout <<= 1;
  331. if (count >= 0xF)
  332. break;
  333. }
  334. if (count >= 0xF) {
  335. printk(KERN_WARNING "%s: Too large timeout requested!\n",
  336. mmc_hostname(host->mmc));
  337. count = 0xE;
  338. }
  339. writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
  340. if (host->flags & SDHCI_USE_DMA) {
  341. int count;
  342. count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
  343. (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
  344. BUG_ON(count != 1);
  345. writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
  346. } else {
  347. host->size = data->blksz * data->blocks;
  348. host->cur_sg = data->sg;
  349. host->num_sg = data->sg_len;
  350. host->offset = 0;
  351. host->remain = host->cur_sg->length;
  352. }
  353. /* We do not handle DMA boundaries, so set it to max (512 KiB) */
  354. writew(SDHCI_MAKE_BLKSZ(7, data->blksz),
  355. host->ioaddr + SDHCI_BLOCK_SIZE);
  356. writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
  357. }
  358. static void sdhci_set_transfer_mode(struct sdhci_host *host,
  359. struct mmc_data *data)
  360. {
  361. u16 mode;
  362. WARN_ON(host->data);
  363. if (data == NULL)
  364. return;
  365. mode = SDHCI_TRNS_BLK_CNT_EN;
  366. if (data->blocks > 1)
  367. mode |= SDHCI_TRNS_MULTI;
  368. if (data->flags & MMC_DATA_READ)
  369. mode |= SDHCI_TRNS_READ;
  370. if (host->flags & SDHCI_USE_DMA)
  371. mode |= SDHCI_TRNS_DMA;
  372. writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
  373. }
  374. static void sdhci_finish_data(struct sdhci_host *host)
  375. {
  376. struct mmc_data *data;
  377. u16 blocks;
  378. BUG_ON(!host->data);
  379. data = host->data;
  380. host->data = NULL;
  381. if (host->flags & SDHCI_USE_DMA) {
  382. pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
  383. (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
  384. }
  385. /*
  386. * Controller doesn't count down when in single block mode.
  387. */
  388. if ((data->blocks == 1) && (data->error == MMC_ERR_NONE))
  389. blocks = 0;
  390. else
  391. blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
  392. data->bytes_xfered = data->blksz * (data->blocks - blocks);
  393. if ((data->error == MMC_ERR_NONE) && blocks) {
  394. printk(KERN_ERR "%s: Controller signalled completion even "
  395. "though there were blocks left. Please report this "
  396. "to " BUGMAIL ".\n", mmc_hostname(host->mmc));
  397. data->error = MMC_ERR_FAILED;
  398. } else if (host->size != 0) {
  399. printk(KERN_ERR "%s: %d bytes were left untransferred. "
  400. "Please report this to " BUGMAIL ".\n",
  401. mmc_hostname(host->mmc), host->size);
  402. data->error = MMC_ERR_FAILED;
  403. }
  404. DBG("Ending data transfer (%d bytes)\n", data->bytes_xfered);
  405. if (data->stop) {
  406. /*
  407. * The controller needs a reset of internal state machines
  408. * upon error conditions.
  409. */
  410. if (data->error != MMC_ERR_NONE) {
  411. sdhci_reset(host, SDHCI_RESET_CMD);
  412. sdhci_reset(host, SDHCI_RESET_DATA);
  413. }
  414. sdhci_send_command(host, data->stop);
  415. } else
  416. tasklet_schedule(&host->finish_tasklet);
  417. }
  418. static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
  419. {
  420. int flags;
  421. u32 mask;
  422. unsigned long timeout;
  423. WARN_ON(host->cmd);
  424. DBG("Sending cmd (%x)\n", cmd->opcode);
  425. /* Wait max 10 ms */
  426. timeout = 10;
  427. mask = SDHCI_CMD_INHIBIT;
  428. if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  429. mask |= SDHCI_DATA_INHIBIT;
  430. /* We shouldn't wait for data inihibit for stop commands, even
  431. though they might use busy signaling */
  432. if (host->mrq->data && (cmd == host->mrq->data->stop))
  433. mask &= ~SDHCI_DATA_INHIBIT;
  434. while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
  435. if (timeout == 0) {
  436. printk(KERN_ERR "%s: Controller never released "
  437. "inhibit bit(s). Please report this to "
  438. BUGMAIL ".\n", mmc_hostname(host->mmc));
  439. sdhci_dumpregs(host);
  440. cmd->error = MMC_ERR_FAILED;
  441. tasklet_schedule(&host->finish_tasklet);
  442. return;
  443. }
  444. timeout--;
  445. mdelay(1);
  446. }
  447. mod_timer(&host->timer, jiffies + 10 * HZ);
  448. host->cmd = cmd;
  449. sdhci_prepare_data(host, cmd->data);
  450. writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
  451. sdhci_set_transfer_mode(host, cmd->data);
  452. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  453. printk(KERN_ERR "%s: Unsupported response type! "
  454. "Please report this to " BUGMAIL ".\n",
  455. mmc_hostname(host->mmc));
  456. cmd->error = MMC_ERR_INVALID;
  457. tasklet_schedule(&host->finish_tasklet);
  458. return;
  459. }
  460. if (!(cmd->flags & MMC_RSP_PRESENT))
  461. flags = SDHCI_CMD_RESP_NONE;
  462. else if (cmd->flags & MMC_RSP_136)
  463. flags = SDHCI_CMD_RESP_LONG;
  464. else if (cmd->flags & MMC_RSP_BUSY)
  465. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  466. else
  467. flags = SDHCI_CMD_RESP_SHORT;
  468. if (cmd->flags & MMC_RSP_CRC)
  469. flags |= SDHCI_CMD_CRC;
  470. if (cmd->flags & MMC_RSP_OPCODE)
  471. flags |= SDHCI_CMD_INDEX;
  472. if (cmd->data)
  473. flags |= SDHCI_CMD_DATA;
  474. writew(SDHCI_MAKE_CMD(cmd->opcode, flags),
  475. host->ioaddr + SDHCI_COMMAND);
  476. }
  477. static void sdhci_finish_command(struct sdhci_host *host)
  478. {
  479. int i;
  480. BUG_ON(host->cmd == NULL);
  481. if (host->cmd->flags & MMC_RSP_PRESENT) {
  482. if (host->cmd->flags & MMC_RSP_136) {
  483. /* CRC is stripped so we need to do some shifting. */
  484. for (i = 0;i < 4;i++) {
  485. host->cmd->resp[i] = readl(host->ioaddr +
  486. SDHCI_RESPONSE + (3-i)*4) << 8;
  487. if (i != 3)
  488. host->cmd->resp[i] |=
  489. readb(host->ioaddr +
  490. SDHCI_RESPONSE + (3-i)*4-1);
  491. }
  492. } else {
  493. host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
  494. }
  495. }
  496. host->cmd->error = MMC_ERR_NONE;
  497. DBG("Ending cmd (%x)\n", host->cmd->opcode);
  498. if (host->cmd->data)
  499. host->data = host->cmd->data;
  500. else
  501. tasklet_schedule(&host->finish_tasklet);
  502. host->cmd = NULL;
  503. }
  504. static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
  505. {
  506. int div;
  507. u8 ctrl;
  508. u16 clk;
  509. unsigned long timeout;
  510. if (clock == host->clock)
  511. return;
  512. writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
  513. ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
  514. if (clock > 25000000)
  515. ctrl |= SDHCI_CTRL_HISPD;
  516. else
  517. ctrl &= ~SDHCI_CTRL_HISPD;
  518. writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
  519. if (clock == 0)
  520. goto out;
  521. for (div = 1;div < 256;div *= 2) {
  522. if ((host->max_clk / div) <= clock)
  523. break;
  524. }
  525. div >>= 1;
  526. clk = div << SDHCI_DIVIDER_SHIFT;
  527. clk |= SDHCI_CLOCK_INT_EN;
  528. writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
  529. /* Wait max 10 ms */
  530. timeout = 10;
  531. while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
  532. & SDHCI_CLOCK_INT_STABLE)) {
  533. if (timeout == 0) {
  534. printk(KERN_ERR "%s: Internal clock never stabilised. "
  535. "Please report this to " BUGMAIL ".\n",
  536. mmc_hostname(host->mmc));
  537. sdhci_dumpregs(host);
  538. return;
  539. }
  540. timeout--;
  541. mdelay(1);
  542. }
  543. clk |= SDHCI_CLOCK_CARD_EN;
  544. writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
  545. out:
  546. host->clock = clock;
  547. }
  548. static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
  549. {
  550. u8 pwr;
  551. if (host->power == power)
  552. return;
  553. if (power == (unsigned short)-1) {
  554. writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
  555. goto out;
  556. }
  557. /*
  558. * Spec says that we should clear the power reg before setting
  559. * a new value. Some controllers don't seem to like this though.
  560. */
  561. if (!(host->chip->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
  562. writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
  563. pwr = SDHCI_POWER_ON;
  564. switch (power) {
  565. case MMC_VDD_170:
  566. case MMC_VDD_180:
  567. case MMC_VDD_190:
  568. pwr |= SDHCI_POWER_180;
  569. break;
  570. case MMC_VDD_290:
  571. case MMC_VDD_300:
  572. case MMC_VDD_310:
  573. pwr |= SDHCI_POWER_300;
  574. break;
  575. case MMC_VDD_320:
  576. case MMC_VDD_330:
  577. case MMC_VDD_340:
  578. pwr |= SDHCI_POWER_330;
  579. break;
  580. default:
  581. BUG();
  582. }
  583. writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
  584. out:
  585. host->power = power;
  586. }
  587. /*****************************************************************************\
  588. * *
  589. * MMC callbacks *
  590. * *
  591. \*****************************************************************************/
  592. static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  593. {
  594. struct sdhci_host *host;
  595. unsigned long flags;
  596. host = mmc_priv(mmc);
  597. spin_lock_irqsave(&host->lock, flags);
  598. WARN_ON(host->mrq != NULL);
  599. sdhci_activate_led(host);
  600. host->mrq = mrq;
  601. if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
  602. host->mrq->cmd->error = MMC_ERR_TIMEOUT;
  603. tasklet_schedule(&host->finish_tasklet);
  604. } else
  605. sdhci_send_command(host, mrq->cmd);
  606. mmiowb();
  607. spin_unlock_irqrestore(&host->lock, flags);
  608. }
  609. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  610. {
  611. struct sdhci_host *host;
  612. unsigned long flags;
  613. u8 ctrl;
  614. host = mmc_priv(mmc);
  615. spin_lock_irqsave(&host->lock, flags);
  616. /*
  617. * Reset the chip on each power off.
  618. * Should clear out any weird states.
  619. */
  620. if (ios->power_mode == MMC_POWER_OFF) {
  621. writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  622. sdhci_init(host);
  623. }
  624. sdhci_set_clock(host, ios->clock);
  625. if (ios->power_mode == MMC_POWER_OFF)
  626. sdhci_set_power(host, -1);
  627. else
  628. sdhci_set_power(host, ios->vdd);
  629. ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
  630. if (ios->bus_width == MMC_BUS_WIDTH_4)
  631. ctrl |= SDHCI_CTRL_4BITBUS;
  632. else
  633. ctrl &= ~SDHCI_CTRL_4BITBUS;
  634. writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
  635. mmiowb();
  636. spin_unlock_irqrestore(&host->lock, flags);
  637. }
  638. static int sdhci_get_ro(struct mmc_host *mmc)
  639. {
  640. struct sdhci_host *host;
  641. unsigned long flags;
  642. int present;
  643. host = mmc_priv(mmc);
  644. spin_lock_irqsave(&host->lock, flags);
  645. present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
  646. spin_unlock_irqrestore(&host->lock, flags);
  647. return !(present & SDHCI_WRITE_PROTECT);
  648. }
  649. static const struct mmc_host_ops sdhci_ops = {
  650. .request = sdhci_request,
  651. .set_ios = sdhci_set_ios,
  652. .get_ro = sdhci_get_ro,
  653. };
  654. /*****************************************************************************\
  655. * *
  656. * Tasklets *
  657. * *
  658. \*****************************************************************************/
  659. static void sdhci_tasklet_card(unsigned long param)
  660. {
  661. struct sdhci_host *host;
  662. unsigned long flags;
  663. host = (struct sdhci_host*)param;
  664. spin_lock_irqsave(&host->lock, flags);
  665. if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
  666. if (host->mrq) {
  667. printk(KERN_ERR "%s: Card removed during transfer!\n",
  668. mmc_hostname(host->mmc));
  669. printk(KERN_ERR "%s: Resetting controller.\n",
  670. mmc_hostname(host->mmc));
  671. sdhci_reset(host, SDHCI_RESET_CMD);
  672. sdhci_reset(host, SDHCI_RESET_DATA);
  673. host->mrq->cmd->error = MMC_ERR_FAILED;
  674. tasklet_schedule(&host->finish_tasklet);
  675. }
  676. }
  677. spin_unlock_irqrestore(&host->lock, flags);
  678. mmc_detect_change(host->mmc, msecs_to_jiffies(500));
  679. }
  680. static void sdhci_tasklet_finish(unsigned long param)
  681. {
  682. struct sdhci_host *host;
  683. unsigned long flags;
  684. struct mmc_request *mrq;
  685. host = (struct sdhci_host*)param;
  686. spin_lock_irqsave(&host->lock, flags);
  687. del_timer(&host->timer);
  688. mrq = host->mrq;
  689. DBG("Ending request, cmd (%x)\n", mrq->cmd->opcode);
  690. /*
  691. * The controller needs a reset of internal state machines
  692. * upon error conditions.
  693. */
  694. if ((mrq->cmd->error != MMC_ERR_NONE) ||
  695. (mrq->data && ((mrq->data->error != MMC_ERR_NONE) ||
  696. (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) {
  697. /* Some controllers need this kick or reset won't work here */
  698. if (host->chip->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
  699. unsigned int clock;
  700. /* This is to force an update */
  701. clock = host->clock;
  702. host->clock = 0;
  703. sdhci_set_clock(host, clock);
  704. }
  705. /* Spec says we should do both at the same time, but Ricoh
  706. controllers do not like that. */
  707. sdhci_reset(host, SDHCI_RESET_CMD);
  708. sdhci_reset(host, SDHCI_RESET_DATA);
  709. }
  710. host->mrq = NULL;
  711. host->cmd = NULL;
  712. host->data = NULL;
  713. sdhci_deactivate_led(host);
  714. mmiowb();
  715. spin_unlock_irqrestore(&host->lock, flags);
  716. mmc_request_done(host->mmc, mrq);
  717. }
  718. static void sdhci_timeout_timer(unsigned long data)
  719. {
  720. struct sdhci_host *host;
  721. unsigned long flags;
  722. host = (struct sdhci_host*)data;
  723. spin_lock_irqsave(&host->lock, flags);
  724. if (host->mrq) {
  725. printk(KERN_ERR "%s: Timeout waiting for hardware interrupt. "
  726. "Please report this to " BUGMAIL ".\n",
  727. mmc_hostname(host->mmc));
  728. sdhci_dumpregs(host);
  729. if (host->data) {
  730. host->data->error = MMC_ERR_TIMEOUT;
  731. sdhci_finish_data(host);
  732. } else {
  733. if (host->cmd)
  734. host->cmd->error = MMC_ERR_TIMEOUT;
  735. else
  736. host->mrq->cmd->error = MMC_ERR_TIMEOUT;
  737. tasklet_schedule(&host->finish_tasklet);
  738. }
  739. }
  740. mmiowb();
  741. spin_unlock_irqrestore(&host->lock, flags);
  742. }
  743. /*****************************************************************************\
  744. * *
  745. * Interrupt handling *
  746. * *
  747. \*****************************************************************************/
  748. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
  749. {
  750. BUG_ON(intmask == 0);
  751. if (!host->cmd) {
  752. printk(KERN_ERR "%s: Got command interrupt even though no "
  753. "command operation was in progress.\n",
  754. mmc_hostname(host->mmc));
  755. printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
  756. mmc_hostname(host->mmc));
  757. sdhci_dumpregs(host);
  758. return;
  759. }
  760. if (intmask & SDHCI_INT_RESPONSE)
  761. sdhci_finish_command(host);
  762. else {
  763. if (intmask & SDHCI_INT_TIMEOUT)
  764. host->cmd->error = MMC_ERR_TIMEOUT;
  765. else if (intmask & SDHCI_INT_CRC)
  766. host->cmd->error = MMC_ERR_BADCRC;
  767. else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
  768. host->cmd->error = MMC_ERR_FAILED;
  769. else
  770. host->cmd->error = MMC_ERR_INVALID;
  771. tasklet_schedule(&host->finish_tasklet);
  772. }
  773. }
  774. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  775. {
  776. BUG_ON(intmask == 0);
  777. if (!host->data) {
  778. /*
  779. * A data end interrupt is sent together with the response
  780. * for the stop command.
  781. */
  782. if (intmask & SDHCI_INT_DATA_END)
  783. return;
  784. printk(KERN_ERR "%s: Got data interrupt even though no "
  785. "data operation was in progress.\n",
  786. mmc_hostname(host->mmc));
  787. printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
  788. mmc_hostname(host->mmc));
  789. sdhci_dumpregs(host);
  790. return;
  791. }
  792. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  793. host->data->error = MMC_ERR_TIMEOUT;
  794. else if (intmask & SDHCI_INT_DATA_CRC)
  795. host->data->error = MMC_ERR_BADCRC;
  796. else if (intmask & SDHCI_INT_DATA_END_BIT)
  797. host->data->error = MMC_ERR_FAILED;
  798. if (host->data->error != MMC_ERR_NONE)
  799. sdhci_finish_data(host);
  800. else {
  801. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  802. sdhci_transfer_pio(host);
  803. if (intmask & SDHCI_INT_DATA_END)
  804. sdhci_finish_data(host);
  805. }
  806. }
  807. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  808. {
  809. irqreturn_t result;
  810. struct sdhci_host* host = dev_id;
  811. u32 intmask;
  812. spin_lock(&host->lock);
  813. intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
  814. if (!intmask) {
  815. result = IRQ_NONE;
  816. goto out;
  817. }
  818. DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
  819. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  820. writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
  821. host->ioaddr + SDHCI_INT_STATUS);
  822. tasklet_schedule(&host->card_tasklet);
  823. }
  824. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
  825. if (intmask & SDHCI_INT_CMD_MASK) {
  826. writel(intmask & SDHCI_INT_CMD_MASK,
  827. host->ioaddr + SDHCI_INT_STATUS);
  828. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  829. }
  830. if (intmask & SDHCI_INT_DATA_MASK) {
  831. writel(intmask & SDHCI_INT_DATA_MASK,
  832. host->ioaddr + SDHCI_INT_STATUS);
  833. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  834. }
  835. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  836. if (intmask & SDHCI_INT_BUS_POWER) {
  837. printk(KERN_ERR "%s: Card is consuming too much power!\n",
  838. mmc_hostname(host->mmc));
  839. writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
  840. }
  841. intmask &= SDHCI_INT_BUS_POWER;
  842. if (intmask) {
  843. printk(KERN_ERR "%s: Unexpected interrupt 0x%08x. Please "
  844. "report this to " BUGMAIL ".\n",
  845. mmc_hostname(host->mmc), intmask);
  846. sdhci_dumpregs(host);
  847. writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
  848. }
  849. result = IRQ_HANDLED;
  850. mmiowb();
  851. out:
  852. spin_unlock(&host->lock);
  853. return result;
  854. }
  855. /*****************************************************************************\
  856. * *
  857. * Suspend/resume *
  858. * *
  859. \*****************************************************************************/
  860. #ifdef CONFIG_PM
  861. static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
  862. {
  863. struct sdhci_chip *chip;
  864. int i, ret;
  865. chip = pci_get_drvdata(pdev);
  866. if (!chip)
  867. return 0;
  868. DBG("Suspending...\n");
  869. for (i = 0;i < chip->num_slots;i++) {
  870. if (!chip->hosts[i])
  871. continue;
  872. ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
  873. if (ret) {
  874. for (i--;i >= 0;i--)
  875. mmc_resume_host(chip->hosts[i]->mmc);
  876. return ret;
  877. }
  878. }
  879. pci_save_state(pdev);
  880. pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
  881. pci_disable_device(pdev);
  882. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  883. return 0;
  884. }
  885. static int sdhci_resume (struct pci_dev *pdev)
  886. {
  887. struct sdhci_chip *chip;
  888. int i, ret;
  889. chip = pci_get_drvdata(pdev);
  890. if (!chip)
  891. return 0;
  892. DBG("Resuming...\n");
  893. pci_set_power_state(pdev, PCI_D0);
  894. pci_restore_state(pdev);
  895. pci_enable_device(pdev);
  896. for (i = 0;i < chip->num_slots;i++) {
  897. if (!chip->hosts[i])
  898. continue;
  899. if (chip->hosts[i]->flags & SDHCI_USE_DMA)
  900. pci_set_master(pdev);
  901. sdhci_init(chip->hosts[i]);
  902. mmiowb();
  903. ret = mmc_resume_host(chip->hosts[i]->mmc);
  904. if (ret)
  905. return ret;
  906. }
  907. return 0;
  908. }
  909. #else /* CONFIG_PM */
  910. #define sdhci_suspend NULL
  911. #define sdhci_resume NULL
  912. #endif /* CONFIG_PM */
  913. /*****************************************************************************\
  914. * *
  915. * Device probing/removal *
  916. * *
  917. \*****************************************************************************/
  918. static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
  919. {
  920. int ret;
  921. unsigned int version;
  922. struct sdhci_chip *chip;
  923. struct mmc_host *mmc;
  924. struct sdhci_host *host;
  925. u8 first_bar;
  926. unsigned int caps;
  927. chip = pci_get_drvdata(pdev);
  928. BUG_ON(!chip);
  929. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
  930. if (ret)
  931. return ret;
  932. first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
  933. if (first_bar > 5) {
  934. printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
  935. return -ENODEV;
  936. }
  937. if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
  938. printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
  939. return -ENODEV;
  940. }
  941. if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
  942. printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. "
  943. "You may experience problems.\n");
  944. }
  945. if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  946. printk(KERN_ERR DRIVER_NAME ": Vendor specific interface. Aborting.\n");
  947. return -ENODEV;
  948. }
  949. if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
  950. printk(KERN_ERR DRIVER_NAME ": Unknown interface. Aborting.\n");
  951. return -ENODEV;
  952. }
  953. mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
  954. if (!mmc)
  955. return -ENOMEM;
  956. host = mmc_priv(mmc);
  957. host->mmc = mmc;
  958. host->chip = chip;
  959. chip->hosts[slot] = host;
  960. host->bar = first_bar + slot;
  961. host->addr = pci_resource_start(pdev, host->bar);
  962. host->irq = pdev->irq;
  963. DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
  964. snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
  965. ret = pci_request_region(pdev, host->bar, host->slot_descr);
  966. if (ret)
  967. goto free;
  968. host->ioaddr = ioremap_nocache(host->addr,
  969. pci_resource_len(pdev, host->bar));
  970. if (!host->ioaddr) {
  971. ret = -ENOMEM;
  972. goto release;
  973. }
  974. sdhci_reset(host, SDHCI_RESET_ALL);
  975. version = readw(host->ioaddr + SDHCI_HOST_VERSION);
  976. version = (version & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
  977. if (version != 0) {
  978. printk(KERN_ERR "%s: Unknown controller version (%d). "
  979. "You may experience problems.\n", host->slot_descr,
  980. version);
  981. }
  982. caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
  983. if (debug_nodma)
  984. DBG("DMA forced off\n");
  985. else if (debug_forcedma) {
  986. DBG("DMA forced on\n");
  987. host->flags |= SDHCI_USE_DMA;
  988. } else if (chip->quirks & SDHCI_QUIRK_FORCE_DMA)
  989. host->flags |= SDHCI_USE_DMA;
  990. else if ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA)
  991. DBG("Controller doesn't have DMA interface\n");
  992. else if (!(caps & SDHCI_CAN_DO_DMA))
  993. DBG("Controller doesn't have DMA capability\n");
  994. else
  995. host->flags |= SDHCI_USE_DMA;
  996. if (host->flags & SDHCI_USE_DMA) {
  997. if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
  998. printk(KERN_WARNING "%s: No suitable DMA available. "
  999. "Falling back to PIO.\n", host->slot_descr);
  1000. host->flags &= ~SDHCI_USE_DMA;
  1001. }
  1002. }
  1003. if (host->flags & SDHCI_USE_DMA)
  1004. pci_set_master(pdev);
  1005. else /* XXX: Hack to get MMC layer to avoid highmem */
  1006. pdev->dma_mask = 0;
  1007. host->max_clk =
  1008. (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
  1009. if (host->max_clk == 0) {
  1010. printk(KERN_ERR "%s: Hardware doesn't specify base clock "
  1011. "frequency.\n", host->slot_descr);
  1012. ret = -ENODEV;
  1013. goto unmap;
  1014. }
  1015. host->max_clk *= 1000000;
  1016. host->timeout_clk =
  1017. (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
  1018. if (host->timeout_clk == 0) {
  1019. printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
  1020. "frequency.\n", host->slot_descr);
  1021. ret = -ENODEV;
  1022. goto unmap;
  1023. }
  1024. if (caps & SDHCI_TIMEOUT_CLK_UNIT)
  1025. host->timeout_clk *= 1000;
  1026. /*
  1027. * Set host parameters.
  1028. */
  1029. mmc->ops = &sdhci_ops;
  1030. mmc->f_min = host->max_clk / 256;
  1031. mmc->f_max = host->max_clk;
  1032. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
  1033. mmc->ocr_avail = 0;
  1034. if (caps & SDHCI_CAN_VDD_330)
  1035. mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
  1036. else if (caps & SDHCI_CAN_VDD_300)
  1037. mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
  1038. else if (caps & SDHCI_CAN_VDD_180)
  1039. mmc->ocr_avail |= MMC_VDD_17_18|MMC_VDD_18_19;
  1040. if ((host->max_clk > 25000000) && !(caps & SDHCI_CAN_DO_HISPD)) {
  1041. printk(KERN_ERR "%s: Controller reports > 25 MHz base clock,"
  1042. " but no high speed support.\n",
  1043. host->slot_descr);
  1044. mmc->f_max = 25000000;
  1045. }
  1046. if (mmc->ocr_avail == 0) {
  1047. printk(KERN_ERR "%s: Hardware doesn't report any "
  1048. "support voltages.\n", host->slot_descr);
  1049. ret = -ENODEV;
  1050. goto unmap;
  1051. }
  1052. spin_lock_init(&host->lock);
  1053. /*
  1054. * Maximum number of segments. Hardware cannot do scatter lists.
  1055. */
  1056. if (host->flags & SDHCI_USE_DMA)
  1057. mmc->max_hw_segs = 1;
  1058. else
  1059. mmc->max_hw_segs = 16;
  1060. mmc->max_phys_segs = 16;
  1061. /*
  1062. * Maximum number of sectors in one transfer. Limited by DMA boundary
  1063. * size (512KiB), which means (512 KiB/512=) 1024 entries.
  1064. */
  1065. mmc->max_sectors = 1024;
  1066. /*
  1067. * Maximum segment size. Could be one segment with the maximum number
  1068. * of sectors.
  1069. */
  1070. mmc->max_seg_size = mmc->max_sectors * 512;
  1071. /*
  1072. * Maximum block size. This varies from controller to controller and
  1073. * is specified in the capabilities register.
  1074. */
  1075. mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
  1076. if (mmc->max_blk_size >= 3) {
  1077. printk(KERN_ERR "%s: Invalid maximum block size.\n",
  1078. host->slot_descr);
  1079. ret = -ENODEV;
  1080. goto unmap;
  1081. }
  1082. mmc->max_blk_size = 512 << mmc->max_blk_size;
  1083. /*
  1084. * Init tasklets.
  1085. */
  1086. tasklet_init(&host->card_tasklet,
  1087. sdhci_tasklet_card, (unsigned long)host);
  1088. tasklet_init(&host->finish_tasklet,
  1089. sdhci_tasklet_finish, (unsigned long)host);
  1090. setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
  1091. ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  1092. host->slot_descr, host);
  1093. if (ret)
  1094. goto untasklet;
  1095. sdhci_init(host);
  1096. #ifdef CONFIG_MMC_DEBUG
  1097. sdhci_dumpregs(host);
  1098. #endif
  1099. mmiowb();
  1100. mmc_add_host(mmc);
  1101. printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
  1102. host->addr, host->irq,
  1103. (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
  1104. return 0;
  1105. untasklet:
  1106. tasklet_kill(&host->card_tasklet);
  1107. tasklet_kill(&host->finish_tasklet);
  1108. unmap:
  1109. iounmap(host->ioaddr);
  1110. release:
  1111. pci_release_region(pdev, host->bar);
  1112. free:
  1113. mmc_free_host(mmc);
  1114. return ret;
  1115. }
  1116. static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
  1117. {
  1118. struct sdhci_chip *chip;
  1119. struct mmc_host *mmc;
  1120. struct sdhci_host *host;
  1121. chip = pci_get_drvdata(pdev);
  1122. host = chip->hosts[slot];
  1123. mmc = host->mmc;
  1124. chip->hosts[slot] = NULL;
  1125. mmc_remove_host(mmc);
  1126. sdhci_reset(host, SDHCI_RESET_ALL);
  1127. free_irq(host->irq, host);
  1128. del_timer_sync(&host->timer);
  1129. tasklet_kill(&host->card_tasklet);
  1130. tasklet_kill(&host->finish_tasklet);
  1131. iounmap(host->ioaddr);
  1132. pci_release_region(pdev, host->bar);
  1133. mmc_free_host(mmc);
  1134. }
  1135. static int __devinit sdhci_probe(struct pci_dev *pdev,
  1136. const struct pci_device_id *ent)
  1137. {
  1138. int ret, i;
  1139. u8 slots, rev;
  1140. struct sdhci_chip *chip;
  1141. BUG_ON(pdev == NULL);
  1142. BUG_ON(ent == NULL);
  1143. pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
  1144. printk(KERN_INFO DRIVER_NAME
  1145. ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
  1146. pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
  1147. (int)rev);
  1148. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
  1149. if (ret)
  1150. return ret;
  1151. slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
  1152. DBG("found %d slot(s)\n", slots);
  1153. if (slots == 0)
  1154. return -ENODEV;
  1155. ret = pci_enable_device(pdev);
  1156. if (ret)
  1157. return ret;
  1158. chip = kzalloc(sizeof(struct sdhci_chip) +
  1159. sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
  1160. if (!chip) {
  1161. ret = -ENOMEM;
  1162. goto err;
  1163. }
  1164. chip->pdev = pdev;
  1165. chip->quirks = ent->driver_data;
  1166. if (debug_quirks)
  1167. chip->quirks = debug_quirks;
  1168. chip->num_slots = slots;
  1169. pci_set_drvdata(pdev, chip);
  1170. for (i = 0;i < slots;i++) {
  1171. ret = sdhci_probe_slot(pdev, i);
  1172. if (ret) {
  1173. for (i--;i >= 0;i--)
  1174. sdhci_remove_slot(pdev, i);
  1175. goto free;
  1176. }
  1177. }
  1178. return 0;
  1179. free:
  1180. pci_set_drvdata(pdev, NULL);
  1181. kfree(chip);
  1182. err:
  1183. pci_disable_device(pdev);
  1184. return ret;
  1185. }
  1186. static void __devexit sdhci_remove(struct pci_dev *pdev)
  1187. {
  1188. int i;
  1189. struct sdhci_chip *chip;
  1190. chip = pci_get_drvdata(pdev);
  1191. if (chip) {
  1192. for (i = 0;i < chip->num_slots;i++)
  1193. sdhci_remove_slot(pdev, i);
  1194. pci_set_drvdata(pdev, NULL);
  1195. kfree(chip);
  1196. }
  1197. pci_disable_device(pdev);
  1198. }
  1199. static struct pci_driver sdhci_driver = {
  1200. .name = DRIVER_NAME,
  1201. .id_table = pci_ids,
  1202. .probe = sdhci_probe,
  1203. .remove = __devexit_p(sdhci_remove),
  1204. .suspend = sdhci_suspend,
  1205. .resume = sdhci_resume,
  1206. };
  1207. /*****************************************************************************\
  1208. * *
  1209. * Driver init/exit *
  1210. * *
  1211. \*****************************************************************************/
  1212. static int __init sdhci_drv_init(void)
  1213. {
  1214. printk(KERN_INFO DRIVER_NAME
  1215. ": Secure Digital Host Controller Interface driver, "
  1216. DRIVER_VERSION "\n");
  1217. printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  1218. return pci_register_driver(&sdhci_driver);
  1219. }
  1220. static void __exit sdhci_drv_exit(void)
  1221. {
  1222. DBG("Exiting\n");
  1223. pci_unregister_driver(&sdhci_driver);
  1224. }
  1225. module_init(sdhci_drv_init);
  1226. module_exit(sdhci_drv_exit);
  1227. module_param(debug_nodma, uint, 0444);
  1228. module_param(debug_forcedma, uint, 0444);
  1229. module_param(debug_quirks, uint, 0444);
  1230. MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
  1231. MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
  1232. MODULE_VERSION(DRIVER_VERSION);
  1233. MODULE_LICENSE("GPL");
  1234. MODULE_PARM_DESC(debug_nodma, "Forcefully disable DMA transfers. (default 0)");
  1235. MODULE_PARM_DESC(debug_forcedma, "Forcefully enable DMA transfers. (default 0)");
  1236. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");