wbsd.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  1. /*
  2. * linux/drivers/mmc/wbsd.c - Winbond W83L51xD SD/MMC driver
  3. *
  4. * Copyright (C) 2004-2005 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 version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. *
  11. * Warning!
  12. *
  13. * Changes to the FIFO system should be done with extreme care since
  14. * the hardware is full of bugs related to the FIFO. Known issues are:
  15. *
  16. * - FIFO size field in FSR is always zero.
  17. *
  18. * - FIFO interrupts tend not to work as they should. Interrupts are
  19. * triggered only for full/empty events, not for threshold values.
  20. *
  21. * - On APIC systems the FIFO empty interrupt is sometimes lost.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/init.h>
  26. #include <linux/ioport.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/delay.h>
  31. #include <linux/pnp.h>
  32. #include <linux/highmem.h>
  33. #include <linux/mmc/host.h>
  34. #include <linux/mmc/protocol.h>
  35. #include <asm/io.h>
  36. #include <asm/dma.h>
  37. #include <asm/scatterlist.h>
  38. #include "wbsd.h"
  39. #define DRIVER_NAME "wbsd"
  40. #define DRIVER_VERSION "1.5"
  41. #define DBG(x...) \
  42. pr_debug(DRIVER_NAME ": " x)
  43. #define DBGF(f, x...) \
  44. pr_debug(DRIVER_NAME " [%s()]: " f, __func__ , ##x)
  45. /*
  46. * Device resources
  47. */
  48. #ifdef CONFIG_PNP
  49. static const struct pnp_device_id pnp_dev_table[] = {
  50. { "WEC0517", 0 },
  51. { "WEC0518", 0 },
  52. { "", 0 },
  53. };
  54. MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
  55. #endif /* CONFIG_PNP */
  56. static const int config_ports[] = { 0x2E, 0x4E };
  57. static const int unlock_codes[] = { 0x83, 0x87 };
  58. static const int valid_ids[] = {
  59. 0x7112,
  60. };
  61. #ifdef CONFIG_PNP
  62. static unsigned int nopnp = 0;
  63. #else
  64. static const unsigned int nopnp = 1;
  65. #endif
  66. static unsigned int io = 0x248;
  67. static unsigned int irq = 6;
  68. static int dma = 2;
  69. /*
  70. * Basic functions
  71. */
  72. static inline void wbsd_unlock_config(struct wbsd_host *host)
  73. {
  74. BUG_ON(host->config == 0);
  75. outb(host->unlock_code, host->config);
  76. outb(host->unlock_code, host->config);
  77. }
  78. static inline void wbsd_lock_config(struct wbsd_host *host)
  79. {
  80. BUG_ON(host->config == 0);
  81. outb(LOCK_CODE, host->config);
  82. }
  83. static inline void wbsd_write_config(struct wbsd_host *host, u8 reg, u8 value)
  84. {
  85. BUG_ON(host->config == 0);
  86. outb(reg, host->config);
  87. outb(value, host->config + 1);
  88. }
  89. static inline u8 wbsd_read_config(struct wbsd_host *host, u8 reg)
  90. {
  91. BUG_ON(host->config == 0);
  92. outb(reg, host->config);
  93. return inb(host->config + 1);
  94. }
  95. static inline void wbsd_write_index(struct wbsd_host *host, u8 index, u8 value)
  96. {
  97. outb(index, host->base + WBSD_IDXR);
  98. outb(value, host->base + WBSD_DATAR);
  99. }
  100. static inline u8 wbsd_read_index(struct wbsd_host *host, u8 index)
  101. {
  102. outb(index, host->base + WBSD_IDXR);
  103. return inb(host->base + WBSD_DATAR);
  104. }
  105. /*
  106. * Common routines
  107. */
  108. static void wbsd_init_device(struct wbsd_host *host)
  109. {
  110. u8 setup, ier;
  111. /*
  112. * Reset chip (SD/MMC part) and fifo.
  113. */
  114. setup = wbsd_read_index(host, WBSD_IDX_SETUP);
  115. setup |= WBSD_FIFO_RESET | WBSD_SOFT_RESET;
  116. wbsd_write_index(host, WBSD_IDX_SETUP, setup);
  117. /*
  118. * Set DAT3 to input
  119. */
  120. setup &= ~WBSD_DAT3_H;
  121. wbsd_write_index(host, WBSD_IDX_SETUP, setup);
  122. host->flags &= ~WBSD_FIGNORE_DETECT;
  123. /*
  124. * Read back default clock.
  125. */
  126. host->clk = wbsd_read_index(host, WBSD_IDX_CLK);
  127. /*
  128. * Power down port.
  129. */
  130. outb(WBSD_POWER_N, host->base + WBSD_CSR);
  131. /*
  132. * Set maximum timeout.
  133. */
  134. wbsd_write_index(host, WBSD_IDX_TAAC, 0x7F);
  135. /*
  136. * Test for card presence
  137. */
  138. if (inb(host->base + WBSD_CSR) & WBSD_CARDPRESENT)
  139. host->flags |= WBSD_FCARD_PRESENT;
  140. else
  141. host->flags &= ~WBSD_FCARD_PRESENT;
  142. /*
  143. * Enable interesting interrupts.
  144. */
  145. ier = 0;
  146. ier |= WBSD_EINT_CARD;
  147. ier |= WBSD_EINT_FIFO_THRE;
  148. ier |= WBSD_EINT_CCRC;
  149. ier |= WBSD_EINT_TIMEOUT;
  150. ier |= WBSD_EINT_CRC;
  151. ier |= WBSD_EINT_TC;
  152. outb(ier, host->base + WBSD_EIR);
  153. /*
  154. * Clear interrupts.
  155. */
  156. inb(host->base + WBSD_ISR);
  157. }
  158. static void wbsd_reset(struct wbsd_host *host)
  159. {
  160. u8 setup;
  161. printk(KERN_ERR "%s: Resetting chip\n", mmc_hostname(host->mmc));
  162. /*
  163. * Soft reset of chip (SD/MMC part).
  164. */
  165. setup = wbsd_read_index(host, WBSD_IDX_SETUP);
  166. setup |= WBSD_SOFT_RESET;
  167. wbsd_write_index(host, WBSD_IDX_SETUP, setup);
  168. }
  169. static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
  170. {
  171. unsigned long dmaflags;
  172. DBGF("Ending request, cmd (%x)\n", mrq->cmd->opcode);
  173. if (host->dma >= 0) {
  174. /*
  175. * Release ISA DMA controller.
  176. */
  177. dmaflags = claim_dma_lock();
  178. disable_dma(host->dma);
  179. clear_dma_ff(host->dma);
  180. release_dma_lock(dmaflags);
  181. /*
  182. * Disable DMA on host.
  183. */
  184. wbsd_write_index(host, WBSD_IDX_DMA, 0);
  185. }
  186. host->mrq = NULL;
  187. /*
  188. * MMC layer might call back into the driver so first unlock.
  189. */
  190. spin_unlock(&host->lock);
  191. mmc_request_done(host->mmc, mrq);
  192. spin_lock(&host->lock);
  193. }
  194. /*
  195. * Scatter/gather functions
  196. */
  197. static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
  198. {
  199. /*
  200. * Get info. about SG list from data structure.
  201. */
  202. host->cur_sg = data->sg;
  203. host->num_sg = data->sg_len;
  204. host->offset = 0;
  205. host->remain = host->cur_sg->length;
  206. }
  207. static inline int wbsd_next_sg(struct wbsd_host *host)
  208. {
  209. /*
  210. * Skip to next SG entry.
  211. */
  212. host->cur_sg++;
  213. host->num_sg--;
  214. /*
  215. * Any entries left?
  216. */
  217. if (host->num_sg > 0) {
  218. host->offset = 0;
  219. host->remain = host->cur_sg->length;
  220. }
  221. return host->num_sg;
  222. }
  223. static inline char *wbsd_kmap_sg(struct wbsd_host *host)
  224. {
  225. host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ) +
  226. host->cur_sg->offset;
  227. return host->mapped_sg;
  228. }
  229. static inline void wbsd_kunmap_sg(struct wbsd_host *host)
  230. {
  231. kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
  232. }
  233. static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
  234. {
  235. unsigned int len, i, size;
  236. struct scatterlist *sg;
  237. char *dmabuf = host->dma_buffer;
  238. char *sgbuf;
  239. size = host->size;
  240. sg = data->sg;
  241. len = data->sg_len;
  242. /*
  243. * Just loop through all entries. Size might not
  244. * be the entire list though so make sure that
  245. * we do not transfer too much.
  246. */
  247. for (i = 0; i < len; i++) {
  248. sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
  249. if (size < sg[i].length)
  250. memcpy(dmabuf, sgbuf, size);
  251. else
  252. memcpy(dmabuf, sgbuf, sg[i].length);
  253. kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
  254. dmabuf += sg[i].length;
  255. if (size < sg[i].length)
  256. size = 0;
  257. else
  258. size -= sg[i].length;
  259. if (size == 0)
  260. break;
  261. }
  262. /*
  263. * Check that we didn't get a request to transfer
  264. * more data than can fit into the SG list.
  265. */
  266. BUG_ON(size != 0);
  267. host->size -= size;
  268. }
  269. static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
  270. {
  271. unsigned int len, i, size;
  272. struct scatterlist *sg;
  273. char *dmabuf = host->dma_buffer;
  274. char *sgbuf;
  275. size = host->size;
  276. sg = data->sg;
  277. len = data->sg_len;
  278. /*
  279. * Just loop through all entries. Size might not
  280. * be the entire list though so make sure that
  281. * we do not transfer too much.
  282. */
  283. for (i = 0; i < len; i++) {
  284. sgbuf = kmap_atomic(sg[i].page, KM_BIO_SRC_IRQ) + sg[i].offset;
  285. if (size < sg[i].length)
  286. memcpy(sgbuf, dmabuf, size);
  287. else
  288. memcpy(sgbuf, dmabuf, sg[i].length);
  289. kunmap_atomic(sgbuf, KM_BIO_SRC_IRQ);
  290. dmabuf += sg[i].length;
  291. if (size < sg[i].length)
  292. size = 0;
  293. else
  294. size -= sg[i].length;
  295. if (size == 0)
  296. break;
  297. }
  298. /*
  299. * Check that we didn't get a request to transfer
  300. * more data than can fit into the SG list.
  301. */
  302. BUG_ON(size != 0);
  303. host->size -= size;
  304. }
  305. /*
  306. * Command handling
  307. */
  308. static inline void wbsd_get_short_reply(struct wbsd_host *host,
  309. struct mmc_command *cmd)
  310. {
  311. /*
  312. * Correct response type?
  313. */
  314. if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
  315. cmd->error = MMC_ERR_INVALID;
  316. return;
  317. }
  318. cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
  319. cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
  320. cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
  321. cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
  322. cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
  323. }
  324. static inline void wbsd_get_long_reply(struct wbsd_host *host,
  325. struct mmc_command *cmd)
  326. {
  327. int i;
  328. /*
  329. * Correct response type?
  330. */
  331. if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
  332. cmd->error = MMC_ERR_INVALID;
  333. return;
  334. }
  335. for (i = 0; i < 4; i++) {
  336. cmd->resp[i] =
  337. wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
  338. cmd->resp[i] |=
  339. wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
  340. cmd->resp[i] |=
  341. wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
  342. cmd->resp[i] |=
  343. wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
  344. }
  345. }
  346. static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
  347. {
  348. int i;
  349. u8 status, isr;
  350. DBGF("Sending cmd (%x)\n", cmd->opcode);
  351. /*
  352. * Clear accumulated ISR. The interrupt routine
  353. * will fill this one with events that occur during
  354. * transfer.
  355. */
  356. host->isr = 0;
  357. /*
  358. * Send the command (CRC calculated by host).
  359. */
  360. outb(cmd->opcode, host->base + WBSD_CMDR);
  361. for (i = 3; i >= 0; i--)
  362. outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
  363. cmd->error = MMC_ERR_NONE;
  364. /*
  365. * Wait for the request to complete.
  366. */
  367. do {
  368. status = wbsd_read_index(host, WBSD_IDX_STATUS);
  369. } while (status & WBSD_CARDTRAFFIC);
  370. /*
  371. * Do we expect a reply?
  372. */
  373. if (cmd->flags & MMC_RSP_PRESENT) {
  374. /*
  375. * Read back status.
  376. */
  377. isr = host->isr;
  378. /* Card removed? */
  379. if (isr & WBSD_INT_CARD)
  380. cmd->error = MMC_ERR_TIMEOUT;
  381. /* Timeout? */
  382. else if (isr & WBSD_INT_TIMEOUT)
  383. cmd->error = MMC_ERR_TIMEOUT;
  384. /* CRC? */
  385. else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
  386. cmd->error = MMC_ERR_BADCRC;
  387. /* All ok */
  388. else {
  389. if (cmd->flags & MMC_RSP_136)
  390. wbsd_get_long_reply(host, cmd);
  391. else
  392. wbsd_get_short_reply(host, cmd);
  393. }
  394. }
  395. DBGF("Sent cmd (%x), res %d\n", cmd->opcode, cmd->error);
  396. }
  397. /*
  398. * Data functions
  399. */
  400. static void wbsd_empty_fifo(struct wbsd_host *host)
  401. {
  402. struct mmc_data *data = host->mrq->cmd->data;
  403. char *buffer;
  404. int i, fsr, fifo;
  405. /*
  406. * Handle excessive data.
  407. */
  408. if (data->bytes_xfered == host->size)
  409. return;
  410. buffer = wbsd_kmap_sg(host) + host->offset;
  411. /*
  412. * Drain the fifo. This has a tendency to loop longer
  413. * than the FIFO length (usually one block).
  414. */
  415. while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
  416. /*
  417. * The size field in the FSR is broken so we have to
  418. * do some guessing.
  419. */
  420. if (fsr & WBSD_FIFO_FULL)
  421. fifo = 16;
  422. else if (fsr & WBSD_FIFO_FUTHRE)
  423. fifo = 8;
  424. else
  425. fifo = 1;
  426. for (i = 0; i < fifo; i++) {
  427. *buffer = inb(host->base + WBSD_DFR);
  428. buffer++;
  429. host->offset++;
  430. host->remain--;
  431. data->bytes_xfered++;
  432. /*
  433. * Transfer done?
  434. */
  435. if (data->bytes_xfered == host->size) {
  436. wbsd_kunmap_sg(host);
  437. return;
  438. }
  439. /*
  440. * End of scatter list entry?
  441. */
  442. if (host->remain == 0) {
  443. wbsd_kunmap_sg(host);
  444. /*
  445. * Get next entry. Check if last.
  446. */
  447. if (!wbsd_next_sg(host)) {
  448. /*
  449. * We should never reach this point.
  450. * It means that we're trying to
  451. * transfer more blocks than can fit
  452. * into the scatter list.
  453. */
  454. BUG_ON(1);
  455. host->size = data->bytes_xfered;
  456. return;
  457. }
  458. buffer = wbsd_kmap_sg(host);
  459. }
  460. }
  461. }
  462. wbsd_kunmap_sg(host);
  463. /*
  464. * This is a very dirty hack to solve a
  465. * hardware problem. The chip doesn't trigger
  466. * FIFO threshold interrupts properly.
  467. */
  468. if ((host->size - data->bytes_xfered) < 16)
  469. tasklet_schedule(&host->fifo_tasklet);
  470. }
  471. static void wbsd_fill_fifo(struct wbsd_host *host)
  472. {
  473. struct mmc_data *data = host->mrq->cmd->data;
  474. char *buffer;
  475. int i, fsr, fifo;
  476. /*
  477. * Check that we aren't being called after the
  478. * entire buffer has been transfered.
  479. */
  480. if (data->bytes_xfered == host->size)
  481. return;
  482. buffer = wbsd_kmap_sg(host) + host->offset;
  483. /*
  484. * Fill the fifo. This has a tendency to loop longer
  485. * than the FIFO length (usually one block).
  486. */
  487. while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_FULL)) {
  488. /*
  489. * The size field in the FSR is broken so we have to
  490. * do some guessing.
  491. */
  492. if (fsr & WBSD_FIFO_EMPTY)
  493. fifo = 0;
  494. else if (fsr & WBSD_FIFO_EMTHRE)
  495. fifo = 8;
  496. else
  497. fifo = 15;
  498. for (i = 16; i > fifo; i--) {
  499. outb(*buffer, host->base + WBSD_DFR);
  500. buffer++;
  501. host->offset++;
  502. host->remain--;
  503. data->bytes_xfered++;
  504. /*
  505. * Transfer done?
  506. */
  507. if (data->bytes_xfered == host->size) {
  508. wbsd_kunmap_sg(host);
  509. return;
  510. }
  511. /*
  512. * End of scatter list entry?
  513. */
  514. if (host->remain == 0) {
  515. wbsd_kunmap_sg(host);
  516. /*
  517. * Get next entry. Check if last.
  518. */
  519. if (!wbsd_next_sg(host)) {
  520. /*
  521. * We should never reach this point.
  522. * It means that we're trying to
  523. * transfer more blocks than can fit
  524. * into the scatter list.
  525. */
  526. BUG_ON(1);
  527. host->size = data->bytes_xfered;
  528. return;
  529. }
  530. buffer = wbsd_kmap_sg(host);
  531. }
  532. }
  533. }
  534. wbsd_kunmap_sg(host);
  535. /*
  536. * The controller stops sending interrupts for
  537. * 'FIFO empty' under certain conditions. So we
  538. * need to be a bit more pro-active.
  539. */
  540. tasklet_schedule(&host->fifo_tasklet);
  541. }
  542. static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
  543. {
  544. u16 blksize;
  545. u8 setup;
  546. unsigned long dmaflags;
  547. DBGF("blksz %04x blks %04x flags %08x\n",
  548. data->blksz, data->blocks, data->flags);
  549. DBGF("tsac %d ms nsac %d clk\n",
  550. data->timeout_ns / 1000000, data->timeout_clks);
  551. /*
  552. * Calculate size.
  553. */
  554. host->size = data->blocks * data->blksz;
  555. /*
  556. * Check timeout values for overflow.
  557. * (Yes, some cards cause this value to overflow).
  558. */
  559. if (data->timeout_ns > 127000000)
  560. wbsd_write_index(host, WBSD_IDX_TAAC, 127);
  561. else {
  562. wbsd_write_index(host, WBSD_IDX_TAAC,
  563. data->timeout_ns / 1000000);
  564. }
  565. if (data->timeout_clks > 255)
  566. wbsd_write_index(host, WBSD_IDX_NSAC, 255);
  567. else
  568. wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
  569. /*
  570. * Inform the chip of how large blocks will be
  571. * sent. It needs this to determine when to
  572. * calculate CRC.
  573. *
  574. * Space for CRC must be included in the size.
  575. * Two bytes are needed for each data line.
  576. */
  577. if (host->bus_width == MMC_BUS_WIDTH_1) {
  578. blksize = data->blksz + 2;
  579. wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
  580. wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
  581. } else if (host->bus_width == MMC_BUS_WIDTH_4) {
  582. blksize = data->blksz + 2 * 4;
  583. wbsd_write_index(host, WBSD_IDX_PBSMSB,
  584. ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
  585. wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
  586. } else {
  587. data->error = MMC_ERR_INVALID;
  588. return;
  589. }
  590. /*
  591. * Clear the FIFO. This is needed even for DMA
  592. * transfers since the chip still uses the FIFO
  593. * internally.
  594. */
  595. setup = wbsd_read_index(host, WBSD_IDX_SETUP);
  596. setup |= WBSD_FIFO_RESET;
  597. wbsd_write_index(host, WBSD_IDX_SETUP, setup);
  598. /*
  599. * DMA transfer?
  600. */
  601. if (host->dma >= 0) {
  602. /*
  603. * The buffer for DMA is only 64 kB.
  604. */
  605. BUG_ON(host->size > 0x10000);
  606. if (host->size > 0x10000) {
  607. data->error = MMC_ERR_INVALID;
  608. return;
  609. }
  610. /*
  611. * Transfer data from the SG list to
  612. * the DMA buffer.
  613. */
  614. if (data->flags & MMC_DATA_WRITE)
  615. wbsd_sg_to_dma(host, data);
  616. /*
  617. * Initialise the ISA DMA controller.
  618. */
  619. dmaflags = claim_dma_lock();
  620. disable_dma(host->dma);
  621. clear_dma_ff(host->dma);
  622. if (data->flags & MMC_DATA_READ)
  623. set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
  624. else
  625. set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
  626. set_dma_addr(host->dma, host->dma_addr);
  627. set_dma_count(host->dma, host->size);
  628. enable_dma(host->dma);
  629. release_dma_lock(dmaflags);
  630. /*
  631. * Enable DMA on the host.
  632. */
  633. wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
  634. } else {
  635. /*
  636. * This flag is used to keep printk
  637. * output to a minimum.
  638. */
  639. host->firsterr = 1;
  640. /*
  641. * Initialise the SG list.
  642. */
  643. wbsd_init_sg(host, data);
  644. /*
  645. * Turn off DMA.
  646. */
  647. wbsd_write_index(host, WBSD_IDX_DMA, 0);
  648. /*
  649. * Set up FIFO threshold levels (and fill
  650. * buffer if doing a write).
  651. */
  652. if (data->flags & MMC_DATA_READ) {
  653. wbsd_write_index(host, WBSD_IDX_FIFOEN,
  654. WBSD_FIFOEN_FULL | 8);
  655. } else {
  656. wbsd_write_index(host, WBSD_IDX_FIFOEN,
  657. WBSD_FIFOEN_EMPTY | 8);
  658. wbsd_fill_fifo(host);
  659. }
  660. }
  661. data->error = MMC_ERR_NONE;
  662. }
  663. static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
  664. {
  665. unsigned long dmaflags;
  666. int count;
  667. u8 status;
  668. WARN_ON(host->mrq == NULL);
  669. /*
  670. * Send a stop command if needed.
  671. */
  672. if (data->stop)
  673. wbsd_send_command(host, data->stop);
  674. /*
  675. * Wait for the controller to leave data
  676. * transfer state.
  677. */
  678. do {
  679. status = wbsd_read_index(host, WBSD_IDX_STATUS);
  680. } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
  681. /*
  682. * DMA transfer?
  683. */
  684. if (host->dma >= 0) {
  685. /*
  686. * Disable DMA on the host.
  687. */
  688. wbsd_write_index(host, WBSD_IDX_DMA, 0);
  689. /*
  690. * Turn of ISA DMA controller.
  691. */
  692. dmaflags = claim_dma_lock();
  693. disable_dma(host->dma);
  694. clear_dma_ff(host->dma);
  695. count = get_dma_residue(host->dma);
  696. release_dma_lock(dmaflags);
  697. /*
  698. * Any leftover data?
  699. */
  700. if (count) {
  701. printk(KERN_ERR "%s: Incomplete DMA transfer. "
  702. "%d bytes left.\n",
  703. mmc_hostname(host->mmc), count);
  704. data->error = MMC_ERR_FAILED;
  705. } else {
  706. /*
  707. * Transfer data from DMA buffer to
  708. * SG list.
  709. */
  710. if (data->flags & MMC_DATA_READ)
  711. wbsd_dma_to_sg(host, data);
  712. data->bytes_xfered = host->size;
  713. }
  714. }
  715. DBGF("Ending data transfer (%d bytes)\n", data->bytes_xfered);
  716. wbsd_request_end(host, host->mrq);
  717. }
  718. /*****************************************************************************\
  719. * *
  720. * MMC layer callbacks *
  721. * *
  722. \*****************************************************************************/
  723. static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
  724. {
  725. struct wbsd_host *host = mmc_priv(mmc);
  726. struct mmc_command *cmd;
  727. /*
  728. * Disable tasklets to avoid a deadlock.
  729. */
  730. spin_lock_bh(&host->lock);
  731. BUG_ON(host->mrq != NULL);
  732. cmd = mrq->cmd;
  733. host->mrq = mrq;
  734. /*
  735. * If there is no card in the slot then
  736. * timeout immediatly.
  737. */
  738. if (!(host->flags & WBSD_FCARD_PRESENT)) {
  739. cmd->error = MMC_ERR_TIMEOUT;
  740. goto done;
  741. }
  742. /*
  743. * Does the request include data?
  744. */
  745. if (cmd->data) {
  746. wbsd_prepare_data(host, cmd->data);
  747. if (cmd->data->error != MMC_ERR_NONE)
  748. goto done;
  749. }
  750. wbsd_send_command(host, cmd);
  751. /*
  752. * If this is a data transfer the request
  753. * will be finished after the data has
  754. * transfered.
  755. */
  756. if (cmd->data && (cmd->error == MMC_ERR_NONE)) {
  757. /*
  758. * Dirty fix for hardware bug.
  759. */
  760. if (host->dma == -1)
  761. tasklet_schedule(&host->fifo_tasklet);
  762. spin_unlock_bh(&host->lock);
  763. return;
  764. }
  765. done:
  766. wbsd_request_end(host, mrq);
  767. spin_unlock_bh(&host->lock);
  768. }
  769. static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  770. {
  771. struct wbsd_host *host = mmc_priv(mmc);
  772. u8 clk, setup, pwr;
  773. spin_lock_bh(&host->lock);
  774. /*
  775. * Reset the chip on each power off.
  776. * Should clear out any weird states.
  777. */
  778. if (ios->power_mode == MMC_POWER_OFF)
  779. wbsd_init_device(host);
  780. if (ios->clock >= 24000000)
  781. clk = WBSD_CLK_24M;
  782. else if (ios->clock >= 16000000)
  783. clk = WBSD_CLK_16M;
  784. else if (ios->clock >= 12000000)
  785. clk = WBSD_CLK_12M;
  786. else
  787. clk = WBSD_CLK_375K;
  788. /*
  789. * Only write to the clock register when
  790. * there is an actual change.
  791. */
  792. if (clk != host->clk) {
  793. wbsd_write_index(host, WBSD_IDX_CLK, clk);
  794. host->clk = clk;
  795. }
  796. /*
  797. * Power up card.
  798. */
  799. if (ios->power_mode != MMC_POWER_OFF) {
  800. pwr = inb(host->base + WBSD_CSR);
  801. pwr &= ~WBSD_POWER_N;
  802. outb(pwr, host->base + WBSD_CSR);
  803. }
  804. /*
  805. * MMC cards need to have pin 1 high during init.
  806. * It wreaks havoc with the card detection though so
  807. * that needs to be disabled.
  808. */
  809. setup = wbsd_read_index(host, WBSD_IDX_SETUP);
  810. if (ios->chip_select == MMC_CS_HIGH) {
  811. BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
  812. setup |= WBSD_DAT3_H;
  813. host->flags |= WBSD_FIGNORE_DETECT;
  814. } else {
  815. if (setup & WBSD_DAT3_H) {
  816. setup &= ~WBSD_DAT3_H;
  817. /*
  818. * We cannot resume card detection immediatly
  819. * because of capacitance and delays in the chip.
  820. */
  821. mod_timer(&host->ignore_timer, jiffies + HZ / 100);
  822. }
  823. }
  824. wbsd_write_index(host, WBSD_IDX_SETUP, setup);
  825. /*
  826. * Store bus width for later. Will be used when
  827. * setting up the data transfer.
  828. */
  829. host->bus_width = ios->bus_width;
  830. spin_unlock_bh(&host->lock);
  831. }
  832. static int wbsd_get_ro(struct mmc_host *mmc)
  833. {
  834. struct wbsd_host *host = mmc_priv(mmc);
  835. u8 csr;
  836. spin_lock_bh(&host->lock);
  837. csr = inb(host->base + WBSD_CSR);
  838. csr |= WBSD_MSLED;
  839. outb(csr, host->base + WBSD_CSR);
  840. mdelay(1);
  841. csr = inb(host->base + WBSD_CSR);
  842. csr &= ~WBSD_MSLED;
  843. outb(csr, host->base + WBSD_CSR);
  844. spin_unlock_bh(&host->lock);
  845. return csr & WBSD_WRPT;
  846. }
  847. static struct mmc_host_ops wbsd_ops = {
  848. .request = wbsd_request,
  849. .set_ios = wbsd_set_ios,
  850. .get_ro = wbsd_get_ro,
  851. };
  852. /*****************************************************************************\
  853. * *
  854. * Interrupt handling *
  855. * *
  856. \*****************************************************************************/
  857. /*
  858. * Helper function to reset detection ignore
  859. */
  860. static void wbsd_reset_ignore(unsigned long data)
  861. {
  862. struct wbsd_host *host = (struct wbsd_host *)data;
  863. BUG_ON(host == NULL);
  864. DBG("Resetting card detection ignore\n");
  865. spin_lock_bh(&host->lock);
  866. host->flags &= ~WBSD_FIGNORE_DETECT;
  867. /*
  868. * Card status might have changed during the
  869. * blackout.
  870. */
  871. tasklet_schedule(&host->card_tasklet);
  872. spin_unlock_bh(&host->lock);
  873. }
  874. /*
  875. * Tasklets
  876. */
  877. static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
  878. {
  879. WARN_ON(!host->mrq);
  880. if (!host->mrq)
  881. return NULL;
  882. WARN_ON(!host->mrq->cmd);
  883. if (!host->mrq->cmd)
  884. return NULL;
  885. WARN_ON(!host->mrq->cmd->data);
  886. if (!host->mrq->cmd->data)
  887. return NULL;
  888. return host->mrq->cmd->data;
  889. }
  890. static void wbsd_tasklet_card(unsigned long param)
  891. {
  892. struct wbsd_host *host = (struct wbsd_host *)param;
  893. u8 csr;
  894. int delay = -1;
  895. spin_lock(&host->lock);
  896. if (host->flags & WBSD_FIGNORE_DETECT) {
  897. spin_unlock(&host->lock);
  898. return;
  899. }
  900. csr = inb(host->base + WBSD_CSR);
  901. WARN_ON(csr == 0xff);
  902. if (csr & WBSD_CARDPRESENT) {
  903. if (!(host->flags & WBSD_FCARD_PRESENT)) {
  904. DBG("Card inserted\n");
  905. host->flags |= WBSD_FCARD_PRESENT;
  906. delay = 500;
  907. }
  908. } else if (host->flags & WBSD_FCARD_PRESENT) {
  909. DBG("Card removed\n");
  910. host->flags &= ~WBSD_FCARD_PRESENT;
  911. if (host->mrq) {
  912. printk(KERN_ERR "%s: Card removed during transfer!\n",
  913. mmc_hostname(host->mmc));
  914. wbsd_reset(host);
  915. host->mrq->cmd->error = MMC_ERR_FAILED;
  916. tasklet_schedule(&host->finish_tasklet);
  917. }
  918. delay = 0;
  919. }
  920. /*
  921. * Unlock first since we might get a call back.
  922. */
  923. spin_unlock(&host->lock);
  924. if (delay != -1)
  925. mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
  926. }
  927. static void wbsd_tasklet_fifo(unsigned long param)
  928. {
  929. struct wbsd_host *host = (struct wbsd_host *)param;
  930. struct mmc_data *data;
  931. spin_lock(&host->lock);
  932. if (!host->mrq)
  933. goto end;
  934. data = wbsd_get_data(host);
  935. if (!data)
  936. goto end;
  937. if (data->flags & MMC_DATA_WRITE)
  938. wbsd_fill_fifo(host);
  939. else
  940. wbsd_empty_fifo(host);
  941. /*
  942. * Done?
  943. */
  944. if (host->size == data->bytes_xfered) {
  945. wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
  946. tasklet_schedule(&host->finish_tasklet);
  947. }
  948. end:
  949. spin_unlock(&host->lock);
  950. }
  951. static void wbsd_tasklet_crc(unsigned long param)
  952. {
  953. struct wbsd_host *host = (struct wbsd_host *)param;
  954. struct mmc_data *data;
  955. spin_lock(&host->lock);
  956. if (!host->mrq)
  957. goto end;
  958. data = wbsd_get_data(host);
  959. if (!data)
  960. goto end;
  961. DBGF("CRC error\n");
  962. data->error = MMC_ERR_BADCRC;
  963. tasklet_schedule(&host->finish_tasklet);
  964. end:
  965. spin_unlock(&host->lock);
  966. }
  967. static void wbsd_tasklet_timeout(unsigned long param)
  968. {
  969. struct wbsd_host *host = (struct wbsd_host *)param;
  970. struct mmc_data *data;
  971. spin_lock(&host->lock);
  972. if (!host->mrq)
  973. goto end;
  974. data = wbsd_get_data(host);
  975. if (!data)
  976. goto end;
  977. DBGF("Timeout\n");
  978. data->error = MMC_ERR_TIMEOUT;
  979. tasklet_schedule(&host->finish_tasklet);
  980. end:
  981. spin_unlock(&host->lock);
  982. }
  983. static void wbsd_tasklet_finish(unsigned long param)
  984. {
  985. struct wbsd_host *host = (struct wbsd_host *)param;
  986. struct mmc_data *data;
  987. spin_lock(&host->lock);
  988. WARN_ON(!host->mrq);
  989. if (!host->mrq)
  990. goto end;
  991. data = wbsd_get_data(host);
  992. if (!data)
  993. goto end;
  994. wbsd_finish_data(host, data);
  995. end:
  996. spin_unlock(&host->lock);
  997. }
  998. static void wbsd_tasklet_block(unsigned long param)
  999. {
  1000. struct wbsd_host *host = (struct wbsd_host *)param;
  1001. struct mmc_data *data;
  1002. spin_lock(&host->lock);
  1003. if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) !=
  1004. WBSD_CRC_OK) {
  1005. data = wbsd_get_data(host);
  1006. if (!data)
  1007. goto end;
  1008. DBGF("CRC error\n");
  1009. data->error = MMC_ERR_BADCRC;
  1010. tasklet_schedule(&host->finish_tasklet);
  1011. }
  1012. end:
  1013. spin_unlock(&host->lock);
  1014. }
  1015. /*
  1016. * Interrupt handling
  1017. */
  1018. static irqreturn_t wbsd_irq(int irq, void *dev_id, struct pt_regs *regs)
  1019. {
  1020. struct wbsd_host *host = dev_id;
  1021. int isr;
  1022. isr = inb(host->base + WBSD_ISR);
  1023. /*
  1024. * Was it actually our hardware that caused the interrupt?
  1025. */
  1026. if (isr == 0xff || isr == 0x00)
  1027. return IRQ_NONE;
  1028. host->isr |= isr;
  1029. /*
  1030. * Schedule tasklets as needed.
  1031. */
  1032. if (isr & WBSD_INT_CARD)
  1033. tasklet_schedule(&host->card_tasklet);
  1034. if (isr & WBSD_INT_FIFO_THRE)
  1035. tasklet_schedule(&host->fifo_tasklet);
  1036. if (isr & WBSD_INT_CRC)
  1037. tasklet_hi_schedule(&host->crc_tasklet);
  1038. if (isr & WBSD_INT_TIMEOUT)
  1039. tasklet_hi_schedule(&host->timeout_tasklet);
  1040. if (isr & WBSD_INT_BUSYEND)
  1041. tasklet_hi_schedule(&host->block_tasklet);
  1042. if (isr & WBSD_INT_TC)
  1043. tasklet_schedule(&host->finish_tasklet);
  1044. return IRQ_HANDLED;
  1045. }
  1046. /*****************************************************************************\
  1047. * *
  1048. * Device initialisation and shutdown *
  1049. * *
  1050. \*****************************************************************************/
  1051. /*
  1052. * Allocate/free MMC structure.
  1053. */
  1054. static int __devinit wbsd_alloc_mmc(struct device *dev)
  1055. {
  1056. struct mmc_host *mmc;
  1057. struct wbsd_host *host;
  1058. /*
  1059. * Allocate MMC structure.
  1060. */
  1061. mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
  1062. if (!mmc)
  1063. return -ENOMEM;
  1064. host = mmc_priv(mmc);
  1065. host->mmc = mmc;
  1066. host->dma = -1;
  1067. /*
  1068. * Set host parameters.
  1069. */
  1070. mmc->ops = &wbsd_ops;
  1071. mmc->f_min = 375000;
  1072. mmc->f_max = 24000000;
  1073. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1074. mmc->caps = MMC_CAP_4_BIT_DATA;
  1075. spin_lock_init(&host->lock);
  1076. /*
  1077. * Set up timers
  1078. */
  1079. init_timer(&host->ignore_timer);
  1080. host->ignore_timer.data = (unsigned long)host;
  1081. host->ignore_timer.function = wbsd_reset_ignore;
  1082. /*
  1083. * Maximum number of segments. Worst case is one sector per segment
  1084. * so this will be 64kB/512.
  1085. */
  1086. mmc->max_hw_segs = 128;
  1087. mmc->max_phys_segs = 128;
  1088. /*
  1089. * Maximum number of sectors in one transfer. Also limited by 64kB
  1090. * buffer.
  1091. */
  1092. mmc->max_sectors = 128;
  1093. /*
  1094. * Maximum segment size. Could be one segment with the maximum number
  1095. * of segments.
  1096. */
  1097. mmc->max_seg_size = mmc->max_sectors * 512;
  1098. dev_set_drvdata(dev, mmc);
  1099. return 0;
  1100. }
  1101. static void __devexit wbsd_free_mmc(struct device *dev)
  1102. {
  1103. struct mmc_host *mmc;
  1104. struct wbsd_host *host;
  1105. mmc = dev_get_drvdata(dev);
  1106. if (!mmc)
  1107. return;
  1108. host = mmc_priv(mmc);
  1109. BUG_ON(host == NULL);
  1110. del_timer_sync(&host->ignore_timer);
  1111. mmc_free_host(mmc);
  1112. dev_set_drvdata(dev, NULL);
  1113. }
  1114. /*
  1115. * Scan for known chip id:s
  1116. */
  1117. static int __devinit wbsd_scan(struct wbsd_host *host)
  1118. {
  1119. int i, j, k;
  1120. int id;
  1121. /*
  1122. * Iterate through all ports, all codes to
  1123. * find hardware that is in our known list.
  1124. */
  1125. for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
  1126. if (!request_region(config_ports[i], 2, DRIVER_NAME))
  1127. continue;
  1128. for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
  1129. id = 0xFFFF;
  1130. host->config = config_ports[i];
  1131. host->unlock_code = unlock_codes[j];
  1132. wbsd_unlock_config(host);
  1133. outb(WBSD_CONF_ID_HI, config_ports[i]);
  1134. id = inb(config_ports[i] + 1) << 8;
  1135. outb(WBSD_CONF_ID_LO, config_ports[i]);
  1136. id |= inb(config_ports[i] + 1);
  1137. wbsd_lock_config(host);
  1138. for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
  1139. if (id == valid_ids[k]) {
  1140. host->chip_id = id;
  1141. return 0;
  1142. }
  1143. }
  1144. if (id != 0xFFFF) {
  1145. DBG("Unknown hardware (id %x) found at %x\n",
  1146. id, config_ports[i]);
  1147. }
  1148. }
  1149. release_region(config_ports[i], 2);
  1150. }
  1151. host->config = 0;
  1152. host->unlock_code = 0;
  1153. return -ENODEV;
  1154. }
  1155. /*
  1156. * Allocate/free io port ranges
  1157. */
  1158. static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
  1159. {
  1160. if (io & 0x7)
  1161. return -EINVAL;
  1162. if (!request_region(base, 8, DRIVER_NAME))
  1163. return -EIO;
  1164. host->base = io;
  1165. return 0;
  1166. }
  1167. static void __devexit wbsd_release_regions(struct wbsd_host *host)
  1168. {
  1169. if (host->base)
  1170. release_region(host->base, 8);
  1171. host->base = 0;
  1172. if (host->config)
  1173. release_region(host->config, 2);
  1174. host->config = 0;
  1175. }
  1176. /*
  1177. * Allocate/free DMA port and buffer
  1178. */
  1179. static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
  1180. {
  1181. if (dma < 0)
  1182. return;
  1183. if (request_dma(dma, DRIVER_NAME))
  1184. goto err;
  1185. /*
  1186. * We need to allocate a special buffer in
  1187. * order for ISA to be able to DMA to it.
  1188. */
  1189. host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
  1190. GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
  1191. if (!host->dma_buffer)
  1192. goto free;
  1193. /*
  1194. * Translate the address to a physical address.
  1195. */
  1196. host->dma_addr = dma_map_single(host->mmc->dev, host->dma_buffer,
  1197. WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
  1198. /*
  1199. * ISA DMA must be aligned on a 64k basis.
  1200. */
  1201. if ((host->dma_addr & 0xffff) != 0)
  1202. goto kfree;
  1203. /*
  1204. * ISA cannot access memory above 16 MB.
  1205. */
  1206. else if (host->dma_addr >= 0x1000000)
  1207. goto kfree;
  1208. host->dma = dma;
  1209. return;
  1210. kfree:
  1211. /*
  1212. * If we've gotten here then there is some kind of alignment bug
  1213. */
  1214. BUG_ON(1);
  1215. dma_unmap_single(host->mmc->dev, host->dma_addr,
  1216. WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
  1217. host->dma_addr = (dma_addr_t)NULL;
  1218. kfree(host->dma_buffer);
  1219. host->dma_buffer = NULL;
  1220. free:
  1221. free_dma(dma);
  1222. err:
  1223. printk(KERN_WARNING DRIVER_NAME ": Unable to allocate DMA %d. "
  1224. "Falling back on FIFO.\n", dma);
  1225. }
  1226. static void __devexit wbsd_release_dma(struct wbsd_host *host)
  1227. {
  1228. if (host->dma_addr) {
  1229. dma_unmap_single(host->mmc->dev, host->dma_addr,
  1230. WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
  1231. }
  1232. kfree(host->dma_buffer);
  1233. if (host->dma >= 0)
  1234. free_dma(host->dma);
  1235. host->dma = -1;
  1236. host->dma_buffer = NULL;
  1237. host->dma_addr = (dma_addr_t)NULL;
  1238. }
  1239. /*
  1240. * Allocate/free IRQ.
  1241. */
  1242. static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
  1243. {
  1244. int ret;
  1245. /*
  1246. * Allocate interrupt.
  1247. */
  1248. ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
  1249. if (ret)
  1250. return ret;
  1251. host->irq = irq;
  1252. /*
  1253. * Set up tasklets.
  1254. */
  1255. tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
  1256. (unsigned long)host);
  1257. tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
  1258. (unsigned long)host);
  1259. tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
  1260. (unsigned long)host);
  1261. tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
  1262. (unsigned long)host);
  1263. tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
  1264. (unsigned long)host);
  1265. tasklet_init(&host->block_tasklet, wbsd_tasklet_block,
  1266. (unsigned long)host);
  1267. return 0;
  1268. }
  1269. static void __devexit wbsd_release_irq(struct wbsd_host *host)
  1270. {
  1271. if (!host->irq)
  1272. return;
  1273. free_irq(host->irq, host);
  1274. host->irq = 0;
  1275. tasklet_kill(&host->card_tasklet);
  1276. tasklet_kill(&host->fifo_tasklet);
  1277. tasklet_kill(&host->crc_tasklet);
  1278. tasklet_kill(&host->timeout_tasklet);
  1279. tasklet_kill(&host->finish_tasklet);
  1280. tasklet_kill(&host->block_tasklet);
  1281. }
  1282. /*
  1283. * Allocate all resources for the host.
  1284. */
  1285. static int __devinit wbsd_request_resources(struct wbsd_host *host,
  1286. int base, int irq, int dma)
  1287. {
  1288. int ret;
  1289. /*
  1290. * Allocate I/O ports.
  1291. */
  1292. ret = wbsd_request_region(host, base);
  1293. if (ret)
  1294. return ret;
  1295. /*
  1296. * Allocate interrupt.
  1297. */
  1298. ret = wbsd_request_irq(host, irq);
  1299. if (ret)
  1300. return ret;
  1301. /*
  1302. * Allocate DMA.
  1303. */
  1304. wbsd_request_dma(host, dma);
  1305. return 0;
  1306. }
  1307. /*
  1308. * Release all resources for the host.
  1309. */
  1310. static void __devexit wbsd_release_resources(struct wbsd_host *host)
  1311. {
  1312. wbsd_release_dma(host);
  1313. wbsd_release_irq(host);
  1314. wbsd_release_regions(host);
  1315. }
  1316. /*
  1317. * Configure the resources the chip should use.
  1318. */
  1319. static void wbsd_chip_config(struct wbsd_host *host)
  1320. {
  1321. wbsd_unlock_config(host);
  1322. /*
  1323. * Reset the chip.
  1324. */
  1325. wbsd_write_config(host, WBSD_CONF_SWRST, 1);
  1326. wbsd_write_config(host, WBSD_CONF_SWRST, 0);
  1327. /*
  1328. * Select SD/MMC function.
  1329. */
  1330. wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
  1331. /*
  1332. * Set up card detection.
  1333. */
  1334. wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
  1335. /*
  1336. * Configure chip
  1337. */
  1338. wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
  1339. wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
  1340. wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
  1341. if (host->dma >= 0)
  1342. wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
  1343. /*
  1344. * Enable and power up chip.
  1345. */
  1346. wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
  1347. wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
  1348. wbsd_lock_config(host);
  1349. }
  1350. /*
  1351. * Check that configured resources are correct.
  1352. */
  1353. static int wbsd_chip_validate(struct wbsd_host *host)
  1354. {
  1355. int base, irq, dma;
  1356. wbsd_unlock_config(host);
  1357. /*
  1358. * Select SD/MMC function.
  1359. */
  1360. wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
  1361. /*
  1362. * Read configuration.
  1363. */
  1364. base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
  1365. base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
  1366. irq = wbsd_read_config(host, WBSD_CONF_IRQ);
  1367. dma = wbsd_read_config(host, WBSD_CONF_DRQ);
  1368. wbsd_lock_config(host);
  1369. /*
  1370. * Validate against given configuration.
  1371. */
  1372. if (base != host->base)
  1373. return 0;
  1374. if (irq != host->irq)
  1375. return 0;
  1376. if ((dma != host->dma) && (host->dma != -1))
  1377. return 0;
  1378. return 1;
  1379. }
  1380. /*
  1381. * Powers down the SD function
  1382. */
  1383. static void wbsd_chip_poweroff(struct wbsd_host *host)
  1384. {
  1385. wbsd_unlock_config(host);
  1386. wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
  1387. wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
  1388. wbsd_lock_config(host);
  1389. }
  1390. /*****************************************************************************\
  1391. * *
  1392. * Devices setup and shutdown *
  1393. * *
  1394. \*****************************************************************************/
  1395. static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
  1396. int pnp)
  1397. {
  1398. struct wbsd_host *host = NULL;
  1399. struct mmc_host *mmc = NULL;
  1400. int ret;
  1401. ret = wbsd_alloc_mmc(dev);
  1402. if (ret)
  1403. return ret;
  1404. mmc = dev_get_drvdata(dev);
  1405. host = mmc_priv(mmc);
  1406. /*
  1407. * Scan for hardware.
  1408. */
  1409. ret = wbsd_scan(host);
  1410. if (ret) {
  1411. if (pnp && (ret == -ENODEV)) {
  1412. printk(KERN_WARNING DRIVER_NAME
  1413. ": Unable to confirm device presence. You may "
  1414. "experience lock-ups.\n");
  1415. } else {
  1416. wbsd_free_mmc(dev);
  1417. return ret;
  1418. }
  1419. }
  1420. /*
  1421. * Request resources.
  1422. */
  1423. ret = wbsd_request_resources(host, io, irq, dma);
  1424. if (ret) {
  1425. wbsd_release_resources(host);
  1426. wbsd_free_mmc(dev);
  1427. return ret;
  1428. }
  1429. /*
  1430. * See if chip needs to be configured.
  1431. */
  1432. if (pnp) {
  1433. if ((host->config != 0) && !wbsd_chip_validate(host)) {
  1434. printk(KERN_WARNING DRIVER_NAME
  1435. ": PnP active but chip not configured! "
  1436. "You probably have a buggy BIOS. "
  1437. "Configuring chip manually.\n");
  1438. wbsd_chip_config(host);
  1439. }
  1440. } else
  1441. wbsd_chip_config(host);
  1442. /*
  1443. * Power Management stuff. No idea how this works.
  1444. * Not tested.
  1445. */
  1446. #ifdef CONFIG_PM
  1447. if (host->config) {
  1448. wbsd_unlock_config(host);
  1449. wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
  1450. wbsd_lock_config(host);
  1451. }
  1452. #endif
  1453. /*
  1454. * Allow device to initialise itself properly.
  1455. */
  1456. mdelay(5);
  1457. /*
  1458. * Reset the chip into a known state.
  1459. */
  1460. wbsd_init_device(host);
  1461. mmc_add_host(mmc);
  1462. printk(KERN_INFO "%s: W83L51xD", mmc_hostname(mmc));
  1463. if (host->chip_id != 0)
  1464. printk(" id %x", (int)host->chip_id);
  1465. printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
  1466. if (host->dma >= 0)
  1467. printk(" dma %d", (int)host->dma);
  1468. else
  1469. printk(" FIFO");
  1470. if (pnp)
  1471. printk(" PnP");
  1472. printk("\n");
  1473. return 0;
  1474. }
  1475. static void __devexit wbsd_shutdown(struct device *dev, int pnp)
  1476. {
  1477. struct mmc_host *mmc = dev_get_drvdata(dev);
  1478. struct wbsd_host *host;
  1479. if (!mmc)
  1480. return;
  1481. host = mmc_priv(mmc);
  1482. mmc_remove_host(mmc);
  1483. /*
  1484. * Power down the SD/MMC function.
  1485. */
  1486. if (!pnp)
  1487. wbsd_chip_poweroff(host);
  1488. wbsd_release_resources(host);
  1489. wbsd_free_mmc(dev);
  1490. }
  1491. /*
  1492. * Non-PnP
  1493. */
  1494. static int __devinit wbsd_probe(struct platform_device *dev)
  1495. {
  1496. return wbsd_init(&dev->dev, io, irq, dma, 0);
  1497. }
  1498. static int __devexit wbsd_remove(struct platform_device *dev)
  1499. {
  1500. wbsd_shutdown(&dev->dev, 0);
  1501. return 0;
  1502. }
  1503. /*
  1504. * PnP
  1505. */
  1506. #ifdef CONFIG_PNP
  1507. static int __devinit
  1508. wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
  1509. {
  1510. int io, irq, dma;
  1511. /*
  1512. * Get resources from PnP layer.
  1513. */
  1514. io = pnp_port_start(pnpdev, 0);
  1515. irq = pnp_irq(pnpdev, 0);
  1516. if (pnp_dma_valid(pnpdev, 0))
  1517. dma = pnp_dma(pnpdev, 0);
  1518. else
  1519. dma = -1;
  1520. DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
  1521. return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
  1522. }
  1523. static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
  1524. {
  1525. wbsd_shutdown(&dev->dev, 1);
  1526. }
  1527. #endif /* CONFIG_PNP */
  1528. /*
  1529. * Power management
  1530. */
  1531. #ifdef CONFIG_PM
  1532. static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
  1533. {
  1534. BUG_ON(host == NULL);
  1535. return mmc_suspend_host(host->mmc, state);
  1536. }
  1537. static int wbsd_resume(struct wbsd_host *host)
  1538. {
  1539. BUG_ON(host == NULL);
  1540. wbsd_init_device(host);
  1541. return mmc_resume_host(host->mmc);
  1542. }
  1543. static int wbsd_platform_suspend(struct platform_device *dev,
  1544. pm_message_t state)
  1545. {
  1546. struct mmc_host *mmc = platform_get_drvdata(dev);
  1547. struct wbsd_host *host;
  1548. int ret;
  1549. if (mmc == NULL)
  1550. return 0;
  1551. DBGF("Suspending...\n");
  1552. host = mmc_priv(mmc);
  1553. ret = wbsd_suspend(host, state);
  1554. if (ret)
  1555. return ret;
  1556. wbsd_chip_poweroff(host);
  1557. return 0;
  1558. }
  1559. static int wbsd_platform_resume(struct platform_device *dev)
  1560. {
  1561. struct mmc_host *mmc = platform_get_drvdata(dev);
  1562. struct wbsd_host *host;
  1563. if (mmc == NULL)
  1564. return 0;
  1565. DBGF("Resuming...\n");
  1566. host = mmc_priv(mmc);
  1567. wbsd_chip_config(host);
  1568. /*
  1569. * Allow device to initialise itself properly.
  1570. */
  1571. mdelay(5);
  1572. return wbsd_resume(host);
  1573. }
  1574. #ifdef CONFIG_PNP
  1575. static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
  1576. {
  1577. struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
  1578. struct wbsd_host *host;
  1579. if (mmc == NULL)
  1580. return 0;
  1581. DBGF("Suspending...\n");
  1582. host = mmc_priv(mmc);
  1583. return wbsd_suspend(host, state);
  1584. }
  1585. static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
  1586. {
  1587. struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
  1588. struct wbsd_host *host;
  1589. if (mmc == NULL)
  1590. return 0;
  1591. DBGF("Resuming...\n");
  1592. host = mmc_priv(mmc);
  1593. /*
  1594. * See if chip needs to be configured.
  1595. */
  1596. if (host->config != 0) {
  1597. if (!wbsd_chip_validate(host)) {
  1598. printk(KERN_WARNING DRIVER_NAME
  1599. ": PnP active but chip not configured! "
  1600. "You probably have a buggy BIOS. "
  1601. "Configuring chip manually.\n");
  1602. wbsd_chip_config(host);
  1603. }
  1604. }
  1605. /*
  1606. * Allow device to initialise itself properly.
  1607. */
  1608. mdelay(5);
  1609. return wbsd_resume(host);
  1610. }
  1611. #endif /* CONFIG_PNP */
  1612. #else /* CONFIG_PM */
  1613. #define wbsd_platform_suspend NULL
  1614. #define wbsd_platform_resume NULL
  1615. #define wbsd_pnp_suspend NULL
  1616. #define wbsd_pnp_resume NULL
  1617. #endif /* CONFIG_PM */
  1618. static struct platform_device *wbsd_device;
  1619. static struct platform_driver wbsd_driver = {
  1620. .probe = wbsd_probe,
  1621. .remove = __devexit_p(wbsd_remove),
  1622. .suspend = wbsd_platform_suspend,
  1623. .resume = wbsd_platform_resume,
  1624. .driver = {
  1625. .name = DRIVER_NAME,
  1626. },
  1627. };
  1628. #ifdef CONFIG_PNP
  1629. static struct pnp_driver wbsd_pnp_driver = {
  1630. .name = DRIVER_NAME,
  1631. .id_table = pnp_dev_table,
  1632. .probe = wbsd_pnp_probe,
  1633. .remove = __devexit_p(wbsd_pnp_remove),
  1634. .suspend = wbsd_pnp_suspend,
  1635. .resume = wbsd_pnp_resume,
  1636. };
  1637. #endif /* CONFIG_PNP */
  1638. /*
  1639. * Module loading/unloading
  1640. */
  1641. static int __init wbsd_drv_init(void)
  1642. {
  1643. int result;
  1644. printk(KERN_INFO DRIVER_NAME
  1645. ": Winbond W83L51xD SD/MMC card interface driver, "
  1646. DRIVER_VERSION "\n");
  1647. printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  1648. #ifdef CONFIG_PNP
  1649. if (!nopnp) {
  1650. result = pnp_register_driver(&wbsd_pnp_driver);
  1651. if (result < 0)
  1652. return result;
  1653. }
  1654. #endif /* CONFIG_PNP */
  1655. if (nopnp) {
  1656. result = platform_driver_register(&wbsd_driver);
  1657. if (result < 0)
  1658. return result;
  1659. wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
  1660. if (!wbsd_device) {
  1661. platform_driver_unregister(&wbsd_driver);
  1662. return -ENOMEM;
  1663. }
  1664. result = platform_device_add(wbsd_device);
  1665. if (result) {
  1666. platform_device_put(wbsd_device);
  1667. platform_driver_unregister(&wbsd_driver);
  1668. return result;
  1669. }
  1670. }
  1671. return 0;
  1672. }
  1673. static void __exit wbsd_drv_exit(void)
  1674. {
  1675. #ifdef CONFIG_PNP
  1676. if (!nopnp)
  1677. pnp_unregister_driver(&wbsd_pnp_driver);
  1678. #endif /* CONFIG_PNP */
  1679. if (nopnp) {
  1680. platform_device_unregister(wbsd_device);
  1681. platform_driver_unregister(&wbsd_driver);
  1682. }
  1683. DBG("unloaded\n");
  1684. }
  1685. module_init(wbsd_drv_init);
  1686. module_exit(wbsd_drv_exit);
  1687. #ifdef CONFIG_PNP
  1688. module_param(nopnp, uint, 0444);
  1689. #endif
  1690. module_param(io, uint, 0444);
  1691. module_param(irq, uint, 0444);
  1692. module_param(dma, int, 0444);
  1693. MODULE_LICENSE("GPL");
  1694. MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
  1695. MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
  1696. MODULE_VERSION(DRIVER_VERSION);
  1697. #ifdef CONFIG_PNP
  1698. MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
  1699. #endif
  1700. MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
  1701. MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
  1702. MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");