sdhci.c 40 KB

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