wbsd.c 40 KB

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