sdhci.c 40 KB

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