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 <linux/scatterlist.h>
  36. #include <linux/slab.h>
  37. #include <asm/io.h>
  38. #include <asm/dma.h>
  39. #include "wbsd.h"
  40. #define DRIVER_NAME "wbsd"
  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 param_nopnp = 0;
  63. #else
  64. static const unsigned int param_nopnp = 1;
  65. #endif
  66. static unsigned int param_io = 0x248;
  67. static unsigned int param_irq = 6;
  68. static int param_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_CRC;
  149. ier |= WBSD_EINT_TIMEOUT;
  150. ier |= WBSD_EINT_TC;
  151. outb(ier, host->base + WBSD_EIR);
  152. /*
  153. * Clear interrupts.
  154. */
  155. inb(host->base + WBSD_ISR);
  156. }
  157. static void wbsd_reset(struct wbsd_host *host)
  158. {
  159. u8 setup;
  160. pr_err("%s: Resetting chip\n", mmc_hostname(host->mmc));
  161. /*
  162. * Soft reset of chip (SD/MMC part).
  163. */
  164. setup = wbsd_read_index(host, WBSD_IDX_SETUP);
  165. setup |= WBSD_SOFT_RESET;
  166. wbsd_write_index(host, WBSD_IDX_SETUP, setup);
  167. }
  168. static void wbsd_request_end(struct wbsd_host *host, struct mmc_request *mrq)
  169. {
  170. unsigned long dmaflags;
  171. if (host->dma >= 0) {
  172. /*
  173. * Release ISA DMA controller.
  174. */
  175. dmaflags = claim_dma_lock();
  176. disable_dma(host->dma);
  177. clear_dma_ff(host->dma);
  178. release_dma_lock(dmaflags);
  179. /*
  180. * Disable DMA on host.
  181. */
  182. wbsd_write_index(host, WBSD_IDX_DMA, 0);
  183. }
  184. host->mrq = NULL;
  185. /*
  186. * MMC layer might call back into the driver so first unlock.
  187. */
  188. spin_unlock(&host->lock);
  189. mmc_request_done(host->mmc, mrq);
  190. spin_lock(&host->lock);
  191. }
  192. /*
  193. * Scatter/gather functions
  194. */
  195. static inline void wbsd_init_sg(struct wbsd_host *host, struct mmc_data *data)
  196. {
  197. /*
  198. * Get info. about SG list from data structure.
  199. */
  200. host->cur_sg = data->sg;
  201. host->num_sg = data->sg_len;
  202. host->offset = 0;
  203. host->remain = host->cur_sg->length;
  204. }
  205. static inline int wbsd_next_sg(struct wbsd_host *host)
  206. {
  207. /*
  208. * Skip to next SG entry.
  209. */
  210. host->cur_sg++;
  211. host->num_sg--;
  212. /*
  213. * Any entries left?
  214. */
  215. if (host->num_sg > 0) {
  216. host->offset = 0;
  217. host->remain = host->cur_sg->length;
  218. }
  219. return host->num_sg;
  220. }
  221. static inline char *wbsd_sg_to_buffer(struct wbsd_host *host)
  222. {
  223. return sg_virt(host->cur_sg);
  224. }
  225. static inline void wbsd_sg_to_dma(struct wbsd_host *host, struct mmc_data *data)
  226. {
  227. unsigned int len, i;
  228. struct scatterlist *sg;
  229. char *dmabuf = host->dma_buffer;
  230. char *sgbuf;
  231. sg = data->sg;
  232. len = data->sg_len;
  233. for (i = 0; i < len; i++) {
  234. sgbuf = sg_virt(&sg[i]);
  235. memcpy(dmabuf, sgbuf, sg[i].length);
  236. dmabuf += sg[i].length;
  237. }
  238. }
  239. static inline void wbsd_dma_to_sg(struct wbsd_host *host, struct mmc_data *data)
  240. {
  241. unsigned int len, i;
  242. struct scatterlist *sg;
  243. char *dmabuf = host->dma_buffer;
  244. char *sgbuf;
  245. sg = data->sg;
  246. len = data->sg_len;
  247. for (i = 0; i < len; i++) {
  248. sgbuf = sg_virt(&sg[i]);
  249. memcpy(sgbuf, dmabuf, sg[i].length);
  250. dmabuf += sg[i].length;
  251. }
  252. }
  253. /*
  254. * Command handling
  255. */
  256. static inline void wbsd_get_short_reply(struct wbsd_host *host,
  257. struct mmc_command *cmd)
  258. {
  259. /*
  260. * Correct response type?
  261. */
  262. if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
  263. cmd->error = -EILSEQ;
  264. return;
  265. }
  266. cmd->resp[0] = wbsd_read_index(host, WBSD_IDX_RESP12) << 24;
  267. cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP13) << 16;
  268. cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP14) << 8;
  269. cmd->resp[0] |= wbsd_read_index(host, WBSD_IDX_RESP15) << 0;
  270. cmd->resp[1] = wbsd_read_index(host, WBSD_IDX_RESP16) << 24;
  271. }
  272. static inline void wbsd_get_long_reply(struct wbsd_host *host,
  273. struct mmc_command *cmd)
  274. {
  275. int i;
  276. /*
  277. * Correct response type?
  278. */
  279. if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
  280. cmd->error = -EILSEQ;
  281. return;
  282. }
  283. for (i = 0; i < 4; i++) {
  284. cmd->resp[i] =
  285. wbsd_read_index(host, WBSD_IDX_RESP1 + i * 4) << 24;
  286. cmd->resp[i] |=
  287. wbsd_read_index(host, WBSD_IDX_RESP2 + i * 4) << 16;
  288. cmd->resp[i] |=
  289. wbsd_read_index(host, WBSD_IDX_RESP3 + i * 4) << 8;
  290. cmd->resp[i] |=
  291. wbsd_read_index(host, WBSD_IDX_RESP4 + i * 4) << 0;
  292. }
  293. }
  294. static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
  295. {
  296. int i;
  297. u8 status, isr;
  298. /*
  299. * Clear accumulated ISR. The interrupt routine
  300. * will fill this one with events that occur during
  301. * transfer.
  302. */
  303. host->isr = 0;
  304. /*
  305. * Send the command (CRC calculated by host).
  306. */
  307. outb(cmd->opcode, host->base + WBSD_CMDR);
  308. for (i = 3; i >= 0; i--)
  309. outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
  310. cmd->error = 0;
  311. /*
  312. * Wait for the request to complete.
  313. */
  314. do {
  315. status = wbsd_read_index(host, WBSD_IDX_STATUS);
  316. } while (status & WBSD_CARDTRAFFIC);
  317. /*
  318. * Do we expect a reply?
  319. */
  320. if (cmd->flags & MMC_RSP_PRESENT) {
  321. /*
  322. * Read back status.
  323. */
  324. isr = host->isr;
  325. /* Card removed? */
  326. if (isr & WBSD_INT_CARD)
  327. cmd->error = -ENOMEDIUM;
  328. /* Timeout? */
  329. else if (isr & WBSD_INT_TIMEOUT)
  330. cmd->error = -ETIMEDOUT;
  331. /* CRC? */
  332. else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
  333. cmd->error = -EILSEQ;
  334. /* All ok */
  335. else {
  336. if (cmd->flags & MMC_RSP_136)
  337. wbsd_get_long_reply(host, cmd);
  338. else
  339. wbsd_get_short_reply(host, cmd);
  340. }
  341. }
  342. }
  343. /*
  344. * Data functions
  345. */
  346. static void wbsd_empty_fifo(struct wbsd_host *host)
  347. {
  348. struct mmc_data *data = host->mrq->cmd->data;
  349. char *buffer;
  350. int i, fsr, fifo;
  351. /*
  352. * Handle excessive data.
  353. */
  354. if (host->num_sg == 0)
  355. return;
  356. buffer = wbsd_sg_to_buffer(host) + host->offset;
  357. /*
  358. * Drain the fifo. This has a tendency to loop longer
  359. * than the FIFO length (usually one block).
  360. */
  361. while (!((fsr = inb(host->base + WBSD_FSR)) & WBSD_FIFO_EMPTY)) {
  362. /*
  363. * The size field in the FSR is broken so we have to
  364. * do some guessing.
  365. */
  366. if (fsr & WBSD_FIFO_FULL)
  367. fifo = 16;
  368. else if (fsr & WBSD_FIFO_FUTHRE)
  369. fifo = 8;
  370. else
  371. fifo = 1;
  372. for (i = 0; i < fifo; i++) {
  373. *buffer = inb(host->base + WBSD_DFR);
  374. buffer++;
  375. host->offset++;
  376. host->remain--;
  377. data->bytes_xfered++;
  378. /*
  379. * End of scatter list entry?
  380. */
  381. if (host->remain == 0) {
  382. /*
  383. * Get next entry. Check if last.
  384. */
  385. if (!wbsd_next_sg(host))
  386. return;
  387. buffer = wbsd_sg_to_buffer(host);
  388. }
  389. }
  390. }
  391. /*
  392. * This is a very dirty hack to solve a
  393. * hardware problem. The chip doesn't trigger
  394. * FIFO threshold interrupts properly.
  395. */
  396. if ((data->blocks * data->blksz - data->bytes_xfered) < 16)
  397. tasklet_schedule(&host->fifo_tasklet);
  398. }
  399. static void wbsd_fill_fifo(struct wbsd_host *host)
  400. {
  401. struct mmc_data *data = host->mrq->cmd->data;
  402. char *buffer;
  403. int i, fsr, fifo;
  404. /*
  405. * Check that we aren't being called after the
  406. * entire buffer has been transferred.
  407. */
  408. if (host->num_sg == 0)
  409. return;
  410. buffer = wbsd_sg_to_buffer(host) + host->offset;
  411. /*
  412. * Fill 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_FULL)) {
  416. /*
  417. * The size field in the FSR is broken so we have to
  418. * do some guessing.
  419. */
  420. if (fsr & WBSD_FIFO_EMPTY)
  421. fifo = 0;
  422. else if (fsr & WBSD_FIFO_EMTHRE)
  423. fifo = 8;
  424. else
  425. fifo = 15;
  426. for (i = 16; i > fifo; i--) {
  427. outb(*buffer, host->base + WBSD_DFR);
  428. buffer++;
  429. host->offset++;
  430. host->remain--;
  431. data->bytes_xfered++;
  432. /*
  433. * End of scatter list entry?
  434. */
  435. if (host->remain == 0) {
  436. /*
  437. * Get next entry. Check if last.
  438. */
  439. if (!wbsd_next_sg(host))
  440. return;
  441. buffer = wbsd_sg_to_buffer(host);
  442. }
  443. }
  444. }
  445. /*
  446. * The controller stops sending interrupts for
  447. * 'FIFO empty' under certain conditions. So we
  448. * need to be a bit more pro-active.
  449. */
  450. tasklet_schedule(&host->fifo_tasklet);
  451. }
  452. static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
  453. {
  454. u16 blksize;
  455. u8 setup;
  456. unsigned long dmaflags;
  457. unsigned int size;
  458. /*
  459. * Calculate size.
  460. */
  461. size = data->blocks * data->blksz;
  462. /*
  463. * Check timeout values for overflow.
  464. * (Yes, some cards cause this value to overflow).
  465. */
  466. if (data->timeout_ns > 127000000)
  467. wbsd_write_index(host, WBSD_IDX_TAAC, 127);
  468. else {
  469. wbsd_write_index(host, WBSD_IDX_TAAC,
  470. data->timeout_ns / 1000000);
  471. }
  472. if (data->timeout_clks > 255)
  473. wbsd_write_index(host, WBSD_IDX_NSAC, 255);
  474. else
  475. wbsd_write_index(host, WBSD_IDX_NSAC, data->timeout_clks);
  476. /*
  477. * Inform the chip of how large blocks will be
  478. * sent. It needs this to determine when to
  479. * calculate CRC.
  480. *
  481. * Space for CRC must be included in the size.
  482. * Two bytes are needed for each data line.
  483. */
  484. if (host->bus_width == MMC_BUS_WIDTH_1) {
  485. blksize = data->blksz + 2;
  486. wbsd_write_index(host, WBSD_IDX_PBSMSB, (blksize >> 4) & 0xF0);
  487. wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
  488. } else if (host->bus_width == MMC_BUS_WIDTH_4) {
  489. blksize = data->blksz + 2 * 4;
  490. wbsd_write_index(host, WBSD_IDX_PBSMSB,
  491. ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
  492. wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
  493. } else {
  494. data->error = -EINVAL;
  495. return;
  496. }
  497. /*
  498. * Clear the FIFO. This is needed even for DMA
  499. * transfers since the chip still uses the FIFO
  500. * internally.
  501. */
  502. setup = wbsd_read_index(host, WBSD_IDX_SETUP);
  503. setup |= WBSD_FIFO_RESET;
  504. wbsd_write_index(host, WBSD_IDX_SETUP, setup);
  505. /*
  506. * DMA transfer?
  507. */
  508. if (host->dma >= 0) {
  509. /*
  510. * The buffer for DMA is only 64 kB.
  511. */
  512. BUG_ON(size > 0x10000);
  513. if (size > 0x10000) {
  514. data->error = -EINVAL;
  515. return;
  516. }
  517. /*
  518. * Transfer data from the SG list to
  519. * the DMA buffer.
  520. */
  521. if (data->flags & MMC_DATA_WRITE)
  522. wbsd_sg_to_dma(host, data);
  523. /*
  524. * Initialise the ISA DMA controller.
  525. */
  526. dmaflags = claim_dma_lock();
  527. disable_dma(host->dma);
  528. clear_dma_ff(host->dma);
  529. if (data->flags & MMC_DATA_READ)
  530. set_dma_mode(host->dma, DMA_MODE_READ & ~0x40);
  531. else
  532. set_dma_mode(host->dma, DMA_MODE_WRITE & ~0x40);
  533. set_dma_addr(host->dma, host->dma_addr);
  534. set_dma_count(host->dma, size);
  535. enable_dma(host->dma);
  536. release_dma_lock(dmaflags);
  537. /*
  538. * Enable DMA on the host.
  539. */
  540. wbsd_write_index(host, WBSD_IDX_DMA, WBSD_DMA_ENABLE);
  541. } else {
  542. /*
  543. * This flag is used to keep printk
  544. * output to a minimum.
  545. */
  546. host->firsterr = 1;
  547. /*
  548. * Initialise the SG list.
  549. */
  550. wbsd_init_sg(host, data);
  551. /*
  552. * Turn off DMA.
  553. */
  554. wbsd_write_index(host, WBSD_IDX_DMA, 0);
  555. /*
  556. * Set up FIFO threshold levels (and fill
  557. * buffer if doing a write).
  558. */
  559. if (data->flags & MMC_DATA_READ) {
  560. wbsd_write_index(host, WBSD_IDX_FIFOEN,
  561. WBSD_FIFOEN_FULL | 8);
  562. } else {
  563. wbsd_write_index(host, WBSD_IDX_FIFOEN,
  564. WBSD_FIFOEN_EMPTY | 8);
  565. wbsd_fill_fifo(host);
  566. }
  567. }
  568. data->error = 0;
  569. }
  570. static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
  571. {
  572. unsigned long dmaflags;
  573. int count;
  574. u8 status;
  575. WARN_ON(host->mrq == NULL);
  576. /*
  577. * Send a stop command if needed.
  578. */
  579. if (data->stop)
  580. wbsd_send_command(host, data->stop);
  581. /*
  582. * Wait for the controller to leave data
  583. * transfer state.
  584. */
  585. do {
  586. status = wbsd_read_index(host, WBSD_IDX_STATUS);
  587. } while (status & (WBSD_BLOCK_READ | WBSD_BLOCK_WRITE));
  588. /*
  589. * DMA transfer?
  590. */
  591. if (host->dma >= 0) {
  592. /*
  593. * Disable DMA on the host.
  594. */
  595. wbsd_write_index(host, WBSD_IDX_DMA, 0);
  596. /*
  597. * Turn of ISA DMA controller.
  598. */
  599. dmaflags = claim_dma_lock();
  600. disable_dma(host->dma);
  601. clear_dma_ff(host->dma);
  602. count = get_dma_residue(host->dma);
  603. release_dma_lock(dmaflags);
  604. data->bytes_xfered = host->mrq->data->blocks *
  605. host->mrq->data->blksz - count;
  606. data->bytes_xfered -= data->bytes_xfered % data->blksz;
  607. /*
  608. * Any leftover data?
  609. */
  610. if (count) {
  611. pr_err("%s: Incomplete DMA transfer. "
  612. "%d bytes left.\n",
  613. mmc_hostname(host->mmc), count);
  614. if (!data->error)
  615. data->error = -EIO;
  616. } else {
  617. /*
  618. * Transfer data from DMA buffer to
  619. * SG list.
  620. */
  621. if (data->flags & MMC_DATA_READ)
  622. wbsd_dma_to_sg(host, data);
  623. }
  624. if (data->error) {
  625. if (data->bytes_xfered)
  626. data->bytes_xfered -= data->blksz;
  627. }
  628. }
  629. wbsd_request_end(host, host->mrq);
  630. }
  631. /*****************************************************************************\
  632. * *
  633. * MMC layer callbacks *
  634. * *
  635. \*****************************************************************************/
  636. static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
  637. {
  638. struct wbsd_host *host = mmc_priv(mmc);
  639. struct mmc_command *cmd;
  640. /*
  641. * Disable tasklets to avoid a deadlock.
  642. */
  643. spin_lock_bh(&host->lock);
  644. BUG_ON(host->mrq != NULL);
  645. cmd = mrq->cmd;
  646. host->mrq = mrq;
  647. /*
  648. * Check that there is actually a card in the slot.
  649. */
  650. if (!(host->flags & WBSD_FCARD_PRESENT)) {
  651. cmd->error = -ENOMEDIUM;
  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. pr_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 = -EINVAL;
  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)
  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. * transferred.
  701. */
  702. if (cmd->data && !cmd->error) {
  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 immediately
  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. pr_err("%s: Card removed during transfer!\n",
  859. mmc_hostname(host->mmc));
  860. wbsd_reset(host);
  861. host->mrq->cmd->error = -ENOMEDIUM;
  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 = -EILSEQ;
  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 = -ETIMEDOUT;
  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;
  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_segs = 128;
  1014. /*
  1015. * Maximum request size. Also limited by 64KiB buffer.
  1016. */
  1017. mmc->max_req_size = 65536;
  1018. /*
  1019. * Maximum segment size. Could be one segment with the maximum number
  1020. * of bytes.
  1021. */
  1022. mmc->max_seg_size = mmc->max_req_size;
  1023. /*
  1024. * Maximum block size. We have 12 bits (= 4095) but have to subtract
  1025. * space for CRC. So the maximum is 4095 - 4*2 = 4087.
  1026. */
  1027. mmc->max_blk_size = 4087;
  1028. /*
  1029. * Maximum block count. There is no real limit so the maximum
  1030. * request size will be the only restriction.
  1031. */
  1032. mmc->max_blk_count = mmc->max_req_size;
  1033. dev_set_drvdata(dev, mmc);
  1034. return 0;
  1035. }
  1036. static void wbsd_free_mmc(struct device *dev)
  1037. {
  1038. struct mmc_host *mmc;
  1039. struct wbsd_host *host;
  1040. mmc = dev_get_drvdata(dev);
  1041. if (!mmc)
  1042. return;
  1043. host = mmc_priv(mmc);
  1044. BUG_ON(host == NULL);
  1045. del_timer_sync(&host->ignore_timer);
  1046. mmc_free_host(mmc);
  1047. dev_set_drvdata(dev, NULL);
  1048. }
  1049. /*
  1050. * Scan for known chip id:s
  1051. */
  1052. static int __devinit wbsd_scan(struct wbsd_host *host)
  1053. {
  1054. int i, j, k;
  1055. int id;
  1056. /*
  1057. * Iterate through all ports, all codes to
  1058. * find hardware that is in our known list.
  1059. */
  1060. for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
  1061. if (!request_region(config_ports[i], 2, DRIVER_NAME))
  1062. continue;
  1063. for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
  1064. id = 0xFFFF;
  1065. host->config = config_ports[i];
  1066. host->unlock_code = unlock_codes[j];
  1067. wbsd_unlock_config(host);
  1068. outb(WBSD_CONF_ID_HI, config_ports[i]);
  1069. id = inb(config_ports[i] + 1) << 8;
  1070. outb(WBSD_CONF_ID_LO, config_ports[i]);
  1071. id |= inb(config_ports[i] + 1);
  1072. wbsd_lock_config(host);
  1073. for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
  1074. if (id == valid_ids[k]) {
  1075. host->chip_id = id;
  1076. return 0;
  1077. }
  1078. }
  1079. if (id != 0xFFFF) {
  1080. DBG("Unknown hardware (id %x) found at %x\n",
  1081. id, config_ports[i]);
  1082. }
  1083. }
  1084. release_region(config_ports[i], 2);
  1085. }
  1086. host->config = 0;
  1087. host->unlock_code = 0;
  1088. return -ENODEV;
  1089. }
  1090. /*
  1091. * Allocate/free io port ranges
  1092. */
  1093. static int __devinit wbsd_request_region(struct wbsd_host *host, int base)
  1094. {
  1095. if (base & 0x7)
  1096. return -EINVAL;
  1097. if (!request_region(base, 8, DRIVER_NAME))
  1098. return -EIO;
  1099. host->base = base;
  1100. return 0;
  1101. }
  1102. static void wbsd_release_regions(struct wbsd_host *host)
  1103. {
  1104. if (host->base)
  1105. release_region(host->base, 8);
  1106. host->base = 0;
  1107. if (host->config)
  1108. release_region(host->config, 2);
  1109. host->config = 0;
  1110. }
  1111. /*
  1112. * Allocate/free DMA port and buffer
  1113. */
  1114. static void __devinit wbsd_request_dma(struct wbsd_host *host, int dma)
  1115. {
  1116. if (dma < 0)
  1117. return;
  1118. if (request_dma(dma, DRIVER_NAME))
  1119. goto err;
  1120. /*
  1121. * We need to allocate a special buffer in
  1122. * order for ISA to be able to DMA to it.
  1123. */
  1124. host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
  1125. GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
  1126. if (!host->dma_buffer)
  1127. goto free;
  1128. /*
  1129. * Translate the address to a physical address.
  1130. */
  1131. host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
  1132. WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
  1133. /*
  1134. * ISA DMA must be aligned on a 64k basis.
  1135. */
  1136. if ((host->dma_addr & 0xffff) != 0)
  1137. goto kfree;
  1138. /*
  1139. * ISA cannot access memory above 16 MB.
  1140. */
  1141. else if (host->dma_addr >= 0x1000000)
  1142. goto kfree;
  1143. host->dma = dma;
  1144. return;
  1145. kfree:
  1146. /*
  1147. * If we've gotten here then there is some kind of alignment bug
  1148. */
  1149. BUG_ON(1);
  1150. dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
  1151. WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
  1152. host->dma_addr = 0;
  1153. kfree(host->dma_buffer);
  1154. host->dma_buffer = NULL;
  1155. free:
  1156. free_dma(dma);
  1157. err:
  1158. pr_warning(DRIVER_NAME ": Unable to allocate DMA %d. "
  1159. "Falling back on FIFO.\n", dma);
  1160. }
  1161. static void wbsd_release_dma(struct wbsd_host *host)
  1162. {
  1163. if (host->dma_addr) {
  1164. dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
  1165. WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
  1166. }
  1167. kfree(host->dma_buffer);
  1168. if (host->dma >= 0)
  1169. free_dma(host->dma);
  1170. host->dma = -1;
  1171. host->dma_buffer = NULL;
  1172. host->dma_addr = 0;
  1173. }
  1174. /*
  1175. * Allocate/free IRQ.
  1176. */
  1177. static int __devinit wbsd_request_irq(struct wbsd_host *host, int irq)
  1178. {
  1179. int ret;
  1180. /*
  1181. * Set up tasklets. Must be done before requesting interrupt.
  1182. */
  1183. tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
  1184. (unsigned long)host);
  1185. tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
  1186. (unsigned long)host);
  1187. tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
  1188. (unsigned long)host);
  1189. tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
  1190. (unsigned long)host);
  1191. tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
  1192. (unsigned long)host);
  1193. /*
  1194. * Allocate interrupt.
  1195. */
  1196. ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
  1197. if (ret)
  1198. return ret;
  1199. host->irq = irq;
  1200. return 0;
  1201. }
  1202. static void wbsd_release_irq(struct wbsd_host *host)
  1203. {
  1204. if (!host->irq)
  1205. return;
  1206. free_irq(host->irq, host);
  1207. host->irq = 0;
  1208. tasklet_kill(&host->card_tasklet);
  1209. tasklet_kill(&host->fifo_tasklet);
  1210. tasklet_kill(&host->crc_tasklet);
  1211. tasklet_kill(&host->timeout_tasklet);
  1212. tasklet_kill(&host->finish_tasklet);
  1213. }
  1214. /*
  1215. * Allocate all resources for the host.
  1216. */
  1217. static int __devinit wbsd_request_resources(struct wbsd_host *host,
  1218. int base, int irq, int dma)
  1219. {
  1220. int ret;
  1221. /*
  1222. * Allocate I/O ports.
  1223. */
  1224. ret = wbsd_request_region(host, base);
  1225. if (ret)
  1226. return ret;
  1227. /*
  1228. * Allocate interrupt.
  1229. */
  1230. ret = wbsd_request_irq(host, irq);
  1231. if (ret)
  1232. return ret;
  1233. /*
  1234. * Allocate DMA.
  1235. */
  1236. wbsd_request_dma(host, dma);
  1237. return 0;
  1238. }
  1239. /*
  1240. * Release all resources for the host.
  1241. */
  1242. static void wbsd_release_resources(struct wbsd_host *host)
  1243. {
  1244. wbsd_release_dma(host);
  1245. wbsd_release_irq(host);
  1246. wbsd_release_regions(host);
  1247. }
  1248. /*
  1249. * Configure the resources the chip should use.
  1250. */
  1251. static void wbsd_chip_config(struct wbsd_host *host)
  1252. {
  1253. wbsd_unlock_config(host);
  1254. /*
  1255. * Reset the chip.
  1256. */
  1257. wbsd_write_config(host, WBSD_CONF_SWRST, 1);
  1258. wbsd_write_config(host, WBSD_CONF_SWRST, 0);
  1259. /*
  1260. * Select SD/MMC function.
  1261. */
  1262. wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
  1263. /*
  1264. * Set up card detection.
  1265. */
  1266. wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
  1267. /*
  1268. * Configure chip
  1269. */
  1270. wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
  1271. wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
  1272. wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
  1273. if (host->dma >= 0)
  1274. wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
  1275. /*
  1276. * Enable and power up chip.
  1277. */
  1278. wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
  1279. wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
  1280. wbsd_lock_config(host);
  1281. }
  1282. /*
  1283. * Check that configured resources are correct.
  1284. */
  1285. static int wbsd_chip_validate(struct wbsd_host *host)
  1286. {
  1287. int base, irq, dma;
  1288. wbsd_unlock_config(host);
  1289. /*
  1290. * Select SD/MMC function.
  1291. */
  1292. wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
  1293. /*
  1294. * Read configuration.
  1295. */
  1296. base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
  1297. base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
  1298. irq = wbsd_read_config(host, WBSD_CONF_IRQ);
  1299. dma = wbsd_read_config(host, WBSD_CONF_DRQ);
  1300. wbsd_lock_config(host);
  1301. /*
  1302. * Validate against given configuration.
  1303. */
  1304. if (base != host->base)
  1305. return 0;
  1306. if (irq != host->irq)
  1307. return 0;
  1308. if ((dma != host->dma) && (host->dma != -1))
  1309. return 0;
  1310. return 1;
  1311. }
  1312. /*
  1313. * Powers down the SD function
  1314. */
  1315. static void wbsd_chip_poweroff(struct wbsd_host *host)
  1316. {
  1317. wbsd_unlock_config(host);
  1318. wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
  1319. wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
  1320. wbsd_lock_config(host);
  1321. }
  1322. /*****************************************************************************\
  1323. * *
  1324. * Devices setup and shutdown *
  1325. * *
  1326. \*****************************************************************************/
  1327. static int __devinit wbsd_init(struct device *dev, int base, int irq, int dma,
  1328. int pnp)
  1329. {
  1330. struct wbsd_host *host = NULL;
  1331. struct mmc_host *mmc = NULL;
  1332. int ret;
  1333. ret = wbsd_alloc_mmc(dev);
  1334. if (ret)
  1335. return ret;
  1336. mmc = dev_get_drvdata(dev);
  1337. host = mmc_priv(mmc);
  1338. /*
  1339. * Scan for hardware.
  1340. */
  1341. ret = wbsd_scan(host);
  1342. if (ret) {
  1343. if (pnp && (ret == -ENODEV)) {
  1344. pr_warning(DRIVER_NAME
  1345. ": Unable to confirm device presence. You may "
  1346. "experience lock-ups.\n");
  1347. } else {
  1348. wbsd_free_mmc(dev);
  1349. return ret;
  1350. }
  1351. }
  1352. /*
  1353. * Request resources.
  1354. */
  1355. ret = wbsd_request_resources(host, base, irq, dma);
  1356. if (ret) {
  1357. wbsd_release_resources(host);
  1358. wbsd_free_mmc(dev);
  1359. return ret;
  1360. }
  1361. /*
  1362. * See if chip needs to be configured.
  1363. */
  1364. if (pnp) {
  1365. if ((host->config != 0) && !wbsd_chip_validate(host)) {
  1366. pr_warning(DRIVER_NAME
  1367. ": PnP active but chip not configured! "
  1368. "You probably have a buggy BIOS. "
  1369. "Configuring chip manually.\n");
  1370. wbsd_chip_config(host);
  1371. }
  1372. } else
  1373. wbsd_chip_config(host);
  1374. /*
  1375. * Power Management stuff. No idea how this works.
  1376. * Not tested.
  1377. */
  1378. #ifdef CONFIG_PM
  1379. if (host->config) {
  1380. wbsd_unlock_config(host);
  1381. wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
  1382. wbsd_lock_config(host);
  1383. }
  1384. #endif
  1385. /*
  1386. * Allow device to initialise itself properly.
  1387. */
  1388. mdelay(5);
  1389. /*
  1390. * Reset the chip into a known state.
  1391. */
  1392. wbsd_init_device(host);
  1393. mmc_add_host(mmc);
  1394. pr_info("%s: W83L51xD", mmc_hostname(mmc));
  1395. if (host->chip_id != 0)
  1396. printk(" id %x", (int)host->chip_id);
  1397. printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
  1398. if (host->dma >= 0)
  1399. printk(" dma %d", (int)host->dma);
  1400. else
  1401. printk(" FIFO");
  1402. if (pnp)
  1403. printk(" PnP");
  1404. printk("\n");
  1405. return 0;
  1406. }
  1407. static void __devexit wbsd_shutdown(struct device *dev, int pnp)
  1408. {
  1409. struct mmc_host *mmc = dev_get_drvdata(dev);
  1410. struct wbsd_host *host;
  1411. if (!mmc)
  1412. return;
  1413. host = mmc_priv(mmc);
  1414. mmc_remove_host(mmc);
  1415. /*
  1416. * Power down the SD/MMC function.
  1417. */
  1418. if (!pnp)
  1419. wbsd_chip_poweroff(host);
  1420. wbsd_release_resources(host);
  1421. wbsd_free_mmc(dev);
  1422. }
  1423. /*
  1424. * Non-PnP
  1425. */
  1426. static int __devinit wbsd_probe(struct platform_device *dev)
  1427. {
  1428. /* Use the module parameters for resources */
  1429. return wbsd_init(&dev->dev, param_io, param_irq, param_dma, 0);
  1430. }
  1431. static int __devexit wbsd_remove(struct platform_device *dev)
  1432. {
  1433. wbsd_shutdown(&dev->dev, 0);
  1434. return 0;
  1435. }
  1436. /*
  1437. * PnP
  1438. */
  1439. #ifdef CONFIG_PNP
  1440. static int __devinit
  1441. wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
  1442. {
  1443. int io, irq, dma;
  1444. /*
  1445. * Get resources from PnP layer.
  1446. */
  1447. io = pnp_port_start(pnpdev, 0);
  1448. irq = pnp_irq(pnpdev, 0);
  1449. if (pnp_dma_valid(pnpdev, 0))
  1450. dma = pnp_dma(pnpdev, 0);
  1451. else
  1452. dma = -1;
  1453. DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
  1454. return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
  1455. }
  1456. static void __devexit wbsd_pnp_remove(struct pnp_dev *dev)
  1457. {
  1458. wbsd_shutdown(&dev->dev, 1);
  1459. }
  1460. #endif /* CONFIG_PNP */
  1461. /*
  1462. * Power management
  1463. */
  1464. #ifdef CONFIG_PM
  1465. static int wbsd_suspend(struct wbsd_host *host, pm_message_t state)
  1466. {
  1467. BUG_ON(host == NULL);
  1468. return mmc_suspend_host(host->mmc);
  1469. }
  1470. static int wbsd_resume(struct wbsd_host *host)
  1471. {
  1472. BUG_ON(host == NULL);
  1473. wbsd_init_device(host);
  1474. return mmc_resume_host(host->mmc);
  1475. }
  1476. static int wbsd_platform_suspend(struct platform_device *dev,
  1477. pm_message_t state)
  1478. {
  1479. struct mmc_host *mmc = platform_get_drvdata(dev);
  1480. struct wbsd_host *host;
  1481. int ret;
  1482. if (mmc == NULL)
  1483. return 0;
  1484. DBGF("Suspending...\n");
  1485. host = mmc_priv(mmc);
  1486. ret = wbsd_suspend(host, state);
  1487. if (ret)
  1488. return ret;
  1489. wbsd_chip_poweroff(host);
  1490. return 0;
  1491. }
  1492. static int wbsd_platform_resume(struct platform_device *dev)
  1493. {
  1494. struct mmc_host *mmc = platform_get_drvdata(dev);
  1495. struct wbsd_host *host;
  1496. if (mmc == NULL)
  1497. return 0;
  1498. DBGF("Resuming...\n");
  1499. host = mmc_priv(mmc);
  1500. wbsd_chip_config(host);
  1501. /*
  1502. * Allow device to initialise itself properly.
  1503. */
  1504. mdelay(5);
  1505. return wbsd_resume(host);
  1506. }
  1507. #ifdef CONFIG_PNP
  1508. static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
  1509. {
  1510. struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
  1511. struct wbsd_host *host;
  1512. if (mmc == NULL)
  1513. return 0;
  1514. DBGF("Suspending...\n");
  1515. host = mmc_priv(mmc);
  1516. return wbsd_suspend(host, state);
  1517. }
  1518. static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
  1519. {
  1520. struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
  1521. struct wbsd_host *host;
  1522. if (mmc == NULL)
  1523. return 0;
  1524. DBGF("Resuming...\n");
  1525. host = mmc_priv(mmc);
  1526. /*
  1527. * See if chip needs to be configured.
  1528. */
  1529. if (host->config != 0) {
  1530. if (!wbsd_chip_validate(host)) {
  1531. pr_warning(DRIVER_NAME
  1532. ": PnP active but chip not configured! "
  1533. "You probably have a buggy BIOS. "
  1534. "Configuring chip manually.\n");
  1535. wbsd_chip_config(host);
  1536. }
  1537. }
  1538. /*
  1539. * Allow device to initialise itself properly.
  1540. */
  1541. mdelay(5);
  1542. return wbsd_resume(host);
  1543. }
  1544. #endif /* CONFIG_PNP */
  1545. #else /* CONFIG_PM */
  1546. #define wbsd_platform_suspend NULL
  1547. #define wbsd_platform_resume NULL
  1548. #define wbsd_pnp_suspend NULL
  1549. #define wbsd_pnp_resume NULL
  1550. #endif /* CONFIG_PM */
  1551. static struct platform_device *wbsd_device;
  1552. static struct platform_driver wbsd_driver = {
  1553. .probe = wbsd_probe,
  1554. .remove = __devexit_p(wbsd_remove),
  1555. .suspend = wbsd_platform_suspend,
  1556. .resume = wbsd_platform_resume,
  1557. .driver = {
  1558. .name = DRIVER_NAME,
  1559. .owner = THIS_MODULE,
  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. pr_info(DRIVER_NAME
  1579. ": Winbond W83L51xD SD/MMC card interface driver\n");
  1580. pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  1581. #ifdef CONFIG_PNP
  1582. if (!param_nopnp) {
  1583. result = pnp_register_driver(&wbsd_pnp_driver);
  1584. if (result < 0)
  1585. return result;
  1586. }
  1587. #endif /* CONFIG_PNP */
  1588. if (param_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 (!param_nopnp)
  1610. pnp_unregister_driver(&wbsd_pnp_driver);
  1611. #endif /* CONFIG_PNP */
  1612. if (param_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_named(nopnp, param_nopnp, uint, 0444);
  1622. #endif
  1623. module_param_named(io, param_io, uint, 0444);
  1624. module_param_named(irq, param_irq, uint, 0444);
  1625. module_param_named(dma, param_dma, int, 0444);
  1626. MODULE_LICENSE("GPL");
  1627. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  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)");