sdhci.c 39 KB

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