sdhci.c 38 KB

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