wbsd.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  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. pr_warn("%s: Data command %d is not supported by this controller\n",
  680. mmc_hostname(host->mmc), cmd->opcode);
  681. cmd->error = -EINVAL;
  682. goto done;
  683. }
  684. }
  685. /*
  686. * Does the request include data?
  687. */
  688. if (cmd->data) {
  689. wbsd_prepare_data(host, cmd->data);
  690. if (cmd->data->error)
  691. goto done;
  692. }
  693. wbsd_send_command(host, cmd);
  694. /*
  695. * If this is a data transfer the request
  696. * will be finished after the data has
  697. * transferred.
  698. */
  699. if (cmd->data && !cmd->error) {
  700. /*
  701. * Dirty fix for hardware bug.
  702. */
  703. if (host->dma == -1)
  704. tasklet_schedule(&host->fifo_tasklet);
  705. spin_unlock_bh(&host->lock);
  706. return;
  707. }
  708. done:
  709. wbsd_request_end(host, mrq);
  710. spin_unlock_bh(&host->lock);
  711. }
  712. static void wbsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  713. {
  714. struct wbsd_host *host = mmc_priv(mmc);
  715. u8 clk, setup, pwr;
  716. spin_lock_bh(&host->lock);
  717. /*
  718. * Reset the chip on each power off.
  719. * Should clear out any weird states.
  720. */
  721. if (ios->power_mode == MMC_POWER_OFF)
  722. wbsd_init_device(host);
  723. if (ios->clock >= 24000000)
  724. clk = WBSD_CLK_24M;
  725. else if (ios->clock >= 16000000)
  726. clk = WBSD_CLK_16M;
  727. else if (ios->clock >= 12000000)
  728. clk = WBSD_CLK_12M;
  729. else
  730. clk = WBSD_CLK_375K;
  731. /*
  732. * Only write to the clock register when
  733. * there is an actual change.
  734. */
  735. if (clk != host->clk) {
  736. wbsd_write_index(host, WBSD_IDX_CLK, clk);
  737. host->clk = clk;
  738. }
  739. /*
  740. * Power up card.
  741. */
  742. if (ios->power_mode != MMC_POWER_OFF) {
  743. pwr = inb(host->base + WBSD_CSR);
  744. pwr &= ~WBSD_POWER_N;
  745. outb(pwr, host->base + WBSD_CSR);
  746. }
  747. /*
  748. * MMC cards need to have pin 1 high during init.
  749. * It wreaks havoc with the card detection though so
  750. * that needs to be disabled.
  751. */
  752. setup = wbsd_read_index(host, WBSD_IDX_SETUP);
  753. if (ios->chip_select == MMC_CS_HIGH) {
  754. BUG_ON(ios->bus_width != MMC_BUS_WIDTH_1);
  755. setup |= WBSD_DAT3_H;
  756. host->flags |= WBSD_FIGNORE_DETECT;
  757. } else {
  758. if (setup & WBSD_DAT3_H) {
  759. setup &= ~WBSD_DAT3_H;
  760. /*
  761. * We cannot resume card detection immediately
  762. * because of capacitance and delays in the chip.
  763. */
  764. mod_timer(&host->ignore_timer, jiffies + HZ / 100);
  765. }
  766. }
  767. wbsd_write_index(host, WBSD_IDX_SETUP, setup);
  768. /*
  769. * Store bus width for later. Will be used when
  770. * setting up the data transfer.
  771. */
  772. host->bus_width = ios->bus_width;
  773. spin_unlock_bh(&host->lock);
  774. }
  775. static int wbsd_get_ro(struct mmc_host *mmc)
  776. {
  777. struct wbsd_host *host = mmc_priv(mmc);
  778. u8 csr;
  779. spin_lock_bh(&host->lock);
  780. csr = inb(host->base + WBSD_CSR);
  781. csr |= WBSD_MSLED;
  782. outb(csr, host->base + WBSD_CSR);
  783. mdelay(1);
  784. csr = inb(host->base + WBSD_CSR);
  785. csr &= ~WBSD_MSLED;
  786. outb(csr, host->base + WBSD_CSR);
  787. spin_unlock_bh(&host->lock);
  788. return !!(csr & WBSD_WRPT);
  789. }
  790. static const struct mmc_host_ops wbsd_ops = {
  791. .request = wbsd_request,
  792. .set_ios = wbsd_set_ios,
  793. .get_ro = wbsd_get_ro,
  794. };
  795. /*****************************************************************************\
  796. * *
  797. * Interrupt handling *
  798. * *
  799. \*****************************************************************************/
  800. /*
  801. * Helper function to reset detection ignore
  802. */
  803. static void wbsd_reset_ignore(struct timer_list *t)
  804. {
  805. struct wbsd_host *host = from_timer(host, t, ignore_timer);
  806. BUG_ON(host == NULL);
  807. DBG("Resetting card detection ignore\n");
  808. spin_lock_bh(&host->lock);
  809. host->flags &= ~WBSD_FIGNORE_DETECT;
  810. /*
  811. * Card status might have changed during the
  812. * blackout.
  813. */
  814. tasklet_schedule(&host->card_tasklet);
  815. spin_unlock_bh(&host->lock);
  816. }
  817. /*
  818. * Tasklets
  819. */
  820. static inline struct mmc_data *wbsd_get_data(struct wbsd_host *host)
  821. {
  822. WARN_ON(!host->mrq);
  823. if (!host->mrq)
  824. return NULL;
  825. WARN_ON(!host->mrq->cmd);
  826. if (!host->mrq->cmd)
  827. return NULL;
  828. WARN_ON(!host->mrq->cmd->data);
  829. if (!host->mrq->cmd->data)
  830. return NULL;
  831. return host->mrq->cmd->data;
  832. }
  833. static void wbsd_tasklet_card(unsigned long param)
  834. {
  835. struct wbsd_host *host = (struct wbsd_host *)param;
  836. u8 csr;
  837. int delay = -1;
  838. spin_lock(&host->lock);
  839. if (host->flags & WBSD_FIGNORE_DETECT) {
  840. spin_unlock(&host->lock);
  841. return;
  842. }
  843. csr = inb(host->base + WBSD_CSR);
  844. WARN_ON(csr == 0xff);
  845. if (csr & WBSD_CARDPRESENT) {
  846. if (!(host->flags & WBSD_FCARD_PRESENT)) {
  847. DBG("Card inserted\n");
  848. host->flags |= WBSD_FCARD_PRESENT;
  849. delay = 500;
  850. }
  851. } else if (host->flags & WBSD_FCARD_PRESENT) {
  852. DBG("Card removed\n");
  853. host->flags &= ~WBSD_FCARD_PRESENT;
  854. if (host->mrq) {
  855. pr_err("%s: Card removed during transfer!\n",
  856. mmc_hostname(host->mmc));
  857. wbsd_reset(host);
  858. host->mrq->cmd->error = -ENOMEDIUM;
  859. tasklet_schedule(&host->finish_tasklet);
  860. }
  861. delay = 0;
  862. }
  863. /*
  864. * Unlock first since we might get a call back.
  865. */
  866. spin_unlock(&host->lock);
  867. if (delay != -1)
  868. mmc_detect_change(host->mmc, msecs_to_jiffies(delay));
  869. }
  870. static void wbsd_tasklet_fifo(unsigned long param)
  871. {
  872. struct wbsd_host *host = (struct wbsd_host *)param;
  873. struct mmc_data *data;
  874. spin_lock(&host->lock);
  875. if (!host->mrq)
  876. goto end;
  877. data = wbsd_get_data(host);
  878. if (!data)
  879. goto end;
  880. if (data->flags & MMC_DATA_WRITE)
  881. wbsd_fill_fifo(host);
  882. else
  883. wbsd_empty_fifo(host);
  884. /*
  885. * Done?
  886. */
  887. if (host->num_sg == 0) {
  888. wbsd_write_index(host, WBSD_IDX_FIFOEN, 0);
  889. tasklet_schedule(&host->finish_tasklet);
  890. }
  891. end:
  892. spin_unlock(&host->lock);
  893. }
  894. static void wbsd_tasklet_crc(unsigned long param)
  895. {
  896. struct wbsd_host *host = (struct wbsd_host *)param;
  897. struct mmc_data *data;
  898. spin_lock(&host->lock);
  899. if (!host->mrq)
  900. goto end;
  901. data = wbsd_get_data(host);
  902. if (!data)
  903. goto end;
  904. DBGF("CRC error\n");
  905. data->error = -EILSEQ;
  906. tasklet_schedule(&host->finish_tasklet);
  907. end:
  908. spin_unlock(&host->lock);
  909. }
  910. static void wbsd_tasklet_timeout(unsigned long param)
  911. {
  912. struct wbsd_host *host = (struct wbsd_host *)param;
  913. struct mmc_data *data;
  914. spin_lock(&host->lock);
  915. if (!host->mrq)
  916. goto end;
  917. data = wbsd_get_data(host);
  918. if (!data)
  919. goto end;
  920. DBGF("Timeout\n");
  921. data->error = -ETIMEDOUT;
  922. tasklet_schedule(&host->finish_tasklet);
  923. end:
  924. spin_unlock(&host->lock);
  925. }
  926. static void wbsd_tasklet_finish(unsigned long param)
  927. {
  928. struct wbsd_host *host = (struct wbsd_host *)param;
  929. struct mmc_data *data;
  930. spin_lock(&host->lock);
  931. WARN_ON(!host->mrq);
  932. if (!host->mrq)
  933. goto end;
  934. data = wbsd_get_data(host);
  935. if (!data)
  936. goto end;
  937. wbsd_finish_data(host, data);
  938. end:
  939. spin_unlock(&host->lock);
  940. }
  941. /*
  942. * Interrupt handling
  943. */
  944. static irqreturn_t wbsd_irq(int irq, void *dev_id)
  945. {
  946. struct wbsd_host *host = dev_id;
  947. int isr;
  948. isr = inb(host->base + WBSD_ISR);
  949. /*
  950. * Was it actually our hardware that caused the interrupt?
  951. */
  952. if (isr == 0xff || isr == 0x00)
  953. return IRQ_NONE;
  954. host->isr |= isr;
  955. /*
  956. * Schedule tasklets as needed.
  957. */
  958. if (isr & WBSD_INT_CARD)
  959. tasklet_schedule(&host->card_tasklet);
  960. if (isr & WBSD_INT_FIFO_THRE)
  961. tasklet_schedule(&host->fifo_tasklet);
  962. if (isr & WBSD_INT_CRC)
  963. tasklet_hi_schedule(&host->crc_tasklet);
  964. if (isr & WBSD_INT_TIMEOUT)
  965. tasklet_hi_schedule(&host->timeout_tasklet);
  966. if (isr & WBSD_INT_TC)
  967. tasklet_schedule(&host->finish_tasklet);
  968. return IRQ_HANDLED;
  969. }
  970. /*****************************************************************************\
  971. * *
  972. * Device initialisation and shutdown *
  973. * *
  974. \*****************************************************************************/
  975. /*
  976. * Allocate/free MMC structure.
  977. */
  978. static int wbsd_alloc_mmc(struct device *dev)
  979. {
  980. struct mmc_host *mmc;
  981. struct wbsd_host *host;
  982. /*
  983. * Allocate MMC structure.
  984. */
  985. mmc = mmc_alloc_host(sizeof(struct wbsd_host), dev);
  986. if (!mmc)
  987. return -ENOMEM;
  988. host = mmc_priv(mmc);
  989. host->mmc = mmc;
  990. host->dma = -1;
  991. /*
  992. * Set host parameters.
  993. */
  994. mmc->ops = &wbsd_ops;
  995. mmc->f_min = 375000;
  996. mmc->f_max = 24000000;
  997. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  998. mmc->caps = MMC_CAP_4_BIT_DATA;
  999. spin_lock_init(&host->lock);
  1000. /*
  1001. * Set up timers
  1002. */
  1003. timer_setup(&host->ignore_timer, wbsd_reset_ignore, 0);
  1004. /*
  1005. * Maximum number of segments. Worst case is one sector per segment
  1006. * so this will be 64kB/512.
  1007. */
  1008. mmc->max_segs = 128;
  1009. /*
  1010. * Maximum request size. Also limited by 64KiB buffer.
  1011. */
  1012. mmc->max_req_size = 65536;
  1013. /*
  1014. * Maximum segment size. Could be one segment with the maximum number
  1015. * of bytes.
  1016. */
  1017. mmc->max_seg_size = mmc->max_req_size;
  1018. /*
  1019. * Maximum block size. We have 12 bits (= 4095) but have to subtract
  1020. * space for CRC. So the maximum is 4095 - 4*2 = 4087.
  1021. */
  1022. mmc->max_blk_size = 4087;
  1023. /*
  1024. * Maximum block count. There is no real limit so the maximum
  1025. * request size will be the only restriction.
  1026. */
  1027. mmc->max_blk_count = mmc->max_req_size;
  1028. dev_set_drvdata(dev, mmc);
  1029. return 0;
  1030. }
  1031. static void wbsd_free_mmc(struct device *dev)
  1032. {
  1033. struct mmc_host *mmc;
  1034. struct wbsd_host *host;
  1035. mmc = dev_get_drvdata(dev);
  1036. if (!mmc)
  1037. return;
  1038. host = mmc_priv(mmc);
  1039. BUG_ON(host == NULL);
  1040. del_timer_sync(&host->ignore_timer);
  1041. mmc_free_host(mmc);
  1042. dev_set_drvdata(dev, NULL);
  1043. }
  1044. /*
  1045. * Scan for known chip id:s
  1046. */
  1047. static int wbsd_scan(struct wbsd_host *host)
  1048. {
  1049. int i, j, k;
  1050. int id;
  1051. /*
  1052. * Iterate through all ports, all codes to
  1053. * find hardware that is in our known list.
  1054. */
  1055. for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
  1056. if (!request_region(config_ports[i], 2, DRIVER_NAME))
  1057. continue;
  1058. for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
  1059. id = 0xFFFF;
  1060. host->config = config_ports[i];
  1061. host->unlock_code = unlock_codes[j];
  1062. wbsd_unlock_config(host);
  1063. outb(WBSD_CONF_ID_HI, config_ports[i]);
  1064. id = inb(config_ports[i] + 1) << 8;
  1065. outb(WBSD_CONF_ID_LO, config_ports[i]);
  1066. id |= inb(config_ports[i] + 1);
  1067. wbsd_lock_config(host);
  1068. for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
  1069. if (id == valid_ids[k]) {
  1070. host->chip_id = id;
  1071. return 0;
  1072. }
  1073. }
  1074. if (id != 0xFFFF) {
  1075. DBG("Unknown hardware (id %x) found at %x\n",
  1076. id, config_ports[i]);
  1077. }
  1078. }
  1079. release_region(config_ports[i], 2);
  1080. }
  1081. host->config = 0;
  1082. host->unlock_code = 0;
  1083. return -ENODEV;
  1084. }
  1085. /*
  1086. * Allocate/free io port ranges
  1087. */
  1088. static int wbsd_request_region(struct wbsd_host *host, int base)
  1089. {
  1090. if (base & 0x7)
  1091. return -EINVAL;
  1092. if (!request_region(base, 8, DRIVER_NAME))
  1093. return -EIO;
  1094. host->base = base;
  1095. return 0;
  1096. }
  1097. static void wbsd_release_regions(struct wbsd_host *host)
  1098. {
  1099. if (host->base)
  1100. release_region(host->base, 8);
  1101. host->base = 0;
  1102. if (host->config)
  1103. release_region(host->config, 2);
  1104. host->config = 0;
  1105. }
  1106. /*
  1107. * Allocate/free DMA port and buffer
  1108. */
  1109. static void wbsd_request_dma(struct wbsd_host *host, int dma)
  1110. {
  1111. if (dma < 0)
  1112. return;
  1113. if (request_dma(dma, DRIVER_NAME))
  1114. goto err;
  1115. /*
  1116. * We need to allocate a special buffer in
  1117. * order for ISA to be able to DMA to it.
  1118. */
  1119. host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
  1120. GFP_NOIO | GFP_DMA | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
  1121. if (!host->dma_buffer)
  1122. goto free;
  1123. /*
  1124. * Translate the address to a physical address.
  1125. */
  1126. host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
  1127. WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
  1128. if (dma_mapping_error(mmc_dev(host->mmc), host->dma_addr))
  1129. goto kfree;
  1130. /*
  1131. * ISA DMA must be aligned on a 64k basis.
  1132. */
  1133. if ((host->dma_addr & 0xffff) != 0)
  1134. goto unmap;
  1135. /*
  1136. * ISA cannot access memory above 16 MB.
  1137. */
  1138. else if (host->dma_addr >= 0x1000000)
  1139. goto unmap;
  1140. host->dma = dma;
  1141. return;
  1142. unmap:
  1143. /*
  1144. * If we've gotten here then there is some kind of alignment bug
  1145. */
  1146. BUG_ON(1);
  1147. dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
  1148. WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
  1149. host->dma_addr = 0;
  1150. kfree:
  1151. kfree(host->dma_buffer);
  1152. host->dma_buffer = NULL;
  1153. free:
  1154. free_dma(dma);
  1155. err:
  1156. pr_warn(DRIVER_NAME ": Unable to allocate DMA %d - falling back on FIFO\n",
  1157. dma);
  1158. }
  1159. static void wbsd_release_dma(struct wbsd_host *host)
  1160. {
  1161. /*
  1162. * host->dma_addr is valid here iff host->dma_buffer is not NULL.
  1163. */
  1164. if (host->dma_buffer) {
  1165. dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
  1166. WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
  1167. kfree(host->dma_buffer);
  1168. }
  1169. if (host->dma >= 0)
  1170. free_dma(host->dma);
  1171. host->dma = -1;
  1172. host->dma_buffer = NULL;
  1173. host->dma_addr = 0;
  1174. }
  1175. /*
  1176. * Allocate/free IRQ.
  1177. */
  1178. static int wbsd_request_irq(struct wbsd_host *host, int irq)
  1179. {
  1180. int ret;
  1181. /*
  1182. * Set up tasklets. Must be done before requesting interrupt.
  1183. */
  1184. tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
  1185. (unsigned long)host);
  1186. tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
  1187. (unsigned long)host);
  1188. tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
  1189. (unsigned long)host);
  1190. tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
  1191. (unsigned long)host);
  1192. tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
  1193. (unsigned long)host);
  1194. /*
  1195. * Allocate interrupt.
  1196. */
  1197. ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
  1198. if (ret)
  1199. return ret;
  1200. host->irq = irq;
  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 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 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. pr_warn(DRIVER_NAME ": Unable to confirm device presence - you may experience lock-ups\n");
  1346. } else {
  1347. wbsd_free_mmc(dev);
  1348. return ret;
  1349. }
  1350. }
  1351. /*
  1352. * Request resources.
  1353. */
  1354. ret = wbsd_request_resources(host, base, irq, dma);
  1355. if (ret) {
  1356. wbsd_release_resources(host);
  1357. wbsd_free_mmc(dev);
  1358. return ret;
  1359. }
  1360. /*
  1361. * See if chip needs to be configured.
  1362. */
  1363. if (pnp) {
  1364. if ((host->config != 0) && !wbsd_chip_validate(host)) {
  1365. pr_warn(DRIVER_NAME ": PnP active but chip not configured! You probably have a buggy BIOS. Configuring chip manually.\n");
  1366. wbsd_chip_config(host);
  1367. }
  1368. } else
  1369. wbsd_chip_config(host);
  1370. /*
  1371. * Power Management stuff. No idea how this works.
  1372. * Not tested.
  1373. */
  1374. #ifdef CONFIG_PM
  1375. if (host->config) {
  1376. wbsd_unlock_config(host);
  1377. wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
  1378. wbsd_lock_config(host);
  1379. }
  1380. #endif
  1381. /*
  1382. * Allow device to initialise itself properly.
  1383. */
  1384. mdelay(5);
  1385. /*
  1386. * Reset the chip into a known state.
  1387. */
  1388. wbsd_init_device(host);
  1389. mmc_add_host(mmc);
  1390. pr_info("%s: W83L51xD", mmc_hostname(mmc));
  1391. if (host->chip_id != 0)
  1392. printk(" id %x", (int)host->chip_id);
  1393. printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
  1394. if (host->dma >= 0)
  1395. printk(" dma %d", (int)host->dma);
  1396. else
  1397. printk(" FIFO");
  1398. if (pnp)
  1399. printk(" PnP");
  1400. printk("\n");
  1401. return 0;
  1402. }
  1403. static void wbsd_shutdown(struct device *dev, int pnp)
  1404. {
  1405. struct mmc_host *mmc = dev_get_drvdata(dev);
  1406. struct wbsd_host *host;
  1407. if (!mmc)
  1408. return;
  1409. host = mmc_priv(mmc);
  1410. mmc_remove_host(mmc);
  1411. /*
  1412. * Power down the SD/MMC function.
  1413. */
  1414. if (!pnp)
  1415. wbsd_chip_poweroff(host);
  1416. wbsd_release_resources(host);
  1417. wbsd_free_mmc(dev);
  1418. }
  1419. /*
  1420. * Non-PnP
  1421. */
  1422. static int wbsd_probe(struct platform_device *dev)
  1423. {
  1424. /* Use the module parameters for resources */
  1425. return wbsd_init(&dev->dev, param_io, param_irq, param_dma, 0);
  1426. }
  1427. static int wbsd_remove(struct platform_device *dev)
  1428. {
  1429. wbsd_shutdown(&dev->dev, 0);
  1430. return 0;
  1431. }
  1432. /*
  1433. * PnP
  1434. */
  1435. #ifdef CONFIG_PNP
  1436. static int
  1437. wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
  1438. {
  1439. int io, irq, dma;
  1440. /*
  1441. * Get resources from PnP layer.
  1442. */
  1443. io = pnp_port_start(pnpdev, 0);
  1444. irq = pnp_irq(pnpdev, 0);
  1445. if (pnp_dma_valid(pnpdev, 0))
  1446. dma = pnp_dma(pnpdev, 0);
  1447. else
  1448. dma = -1;
  1449. DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
  1450. return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
  1451. }
  1452. static void wbsd_pnp_remove(struct pnp_dev *dev)
  1453. {
  1454. wbsd_shutdown(&dev->dev, 1);
  1455. }
  1456. #endif /* CONFIG_PNP */
  1457. /*
  1458. * Power management
  1459. */
  1460. #ifdef CONFIG_PM
  1461. static int wbsd_platform_suspend(struct platform_device *dev,
  1462. pm_message_t state)
  1463. {
  1464. struct mmc_host *mmc = platform_get_drvdata(dev);
  1465. struct wbsd_host *host;
  1466. if (mmc == NULL)
  1467. return 0;
  1468. DBGF("Suspending...\n");
  1469. host = mmc_priv(mmc);
  1470. wbsd_chip_poweroff(host);
  1471. return 0;
  1472. }
  1473. static int wbsd_platform_resume(struct platform_device *dev)
  1474. {
  1475. struct mmc_host *mmc = platform_get_drvdata(dev);
  1476. struct wbsd_host *host;
  1477. if (mmc == NULL)
  1478. return 0;
  1479. DBGF("Resuming...\n");
  1480. host = mmc_priv(mmc);
  1481. wbsd_chip_config(host);
  1482. /*
  1483. * Allow device to initialise itself properly.
  1484. */
  1485. mdelay(5);
  1486. wbsd_init_device(host);
  1487. return 0;
  1488. }
  1489. #ifdef CONFIG_PNP
  1490. static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
  1491. {
  1492. struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
  1493. if (mmc == NULL)
  1494. return 0;
  1495. DBGF("Suspending...\n");
  1496. return 0;
  1497. }
  1498. static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
  1499. {
  1500. struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
  1501. struct wbsd_host *host;
  1502. if (mmc == NULL)
  1503. return 0;
  1504. DBGF("Resuming...\n");
  1505. host = mmc_priv(mmc);
  1506. /*
  1507. * See if chip needs to be configured.
  1508. */
  1509. if (host->config != 0) {
  1510. if (!wbsd_chip_validate(host)) {
  1511. pr_warn(DRIVER_NAME ": PnP active but chip not configured! You probably have a buggy BIOS. Configuring chip manually.\n");
  1512. wbsd_chip_config(host);
  1513. }
  1514. }
  1515. /*
  1516. * Allow device to initialise itself properly.
  1517. */
  1518. mdelay(5);
  1519. wbsd_init_device(host);
  1520. return 0;
  1521. }
  1522. #endif /* CONFIG_PNP */
  1523. #else /* CONFIG_PM */
  1524. #define wbsd_platform_suspend NULL
  1525. #define wbsd_platform_resume NULL
  1526. #define wbsd_pnp_suspend NULL
  1527. #define wbsd_pnp_resume NULL
  1528. #endif /* CONFIG_PM */
  1529. static struct platform_device *wbsd_device;
  1530. static struct platform_driver wbsd_driver = {
  1531. .probe = wbsd_probe,
  1532. .remove = wbsd_remove,
  1533. .suspend = wbsd_platform_suspend,
  1534. .resume = wbsd_platform_resume,
  1535. .driver = {
  1536. .name = DRIVER_NAME,
  1537. },
  1538. };
  1539. #ifdef CONFIG_PNP
  1540. static struct pnp_driver wbsd_pnp_driver = {
  1541. .name = DRIVER_NAME,
  1542. .id_table = pnp_dev_table,
  1543. .probe = wbsd_pnp_probe,
  1544. .remove = wbsd_pnp_remove,
  1545. .suspend = wbsd_pnp_suspend,
  1546. .resume = wbsd_pnp_resume,
  1547. };
  1548. #endif /* CONFIG_PNP */
  1549. /*
  1550. * Module loading/unloading
  1551. */
  1552. static int __init wbsd_drv_init(void)
  1553. {
  1554. int result;
  1555. pr_info(DRIVER_NAME
  1556. ": Winbond W83L51xD SD/MMC card interface driver\n");
  1557. pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  1558. #ifdef CONFIG_PNP
  1559. if (!param_nopnp) {
  1560. result = pnp_register_driver(&wbsd_pnp_driver);
  1561. if (result < 0)
  1562. return result;
  1563. }
  1564. #endif /* CONFIG_PNP */
  1565. if (param_nopnp) {
  1566. result = platform_driver_register(&wbsd_driver);
  1567. if (result < 0)
  1568. return result;
  1569. wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
  1570. if (!wbsd_device) {
  1571. platform_driver_unregister(&wbsd_driver);
  1572. return -ENOMEM;
  1573. }
  1574. result = platform_device_add(wbsd_device);
  1575. if (result) {
  1576. platform_device_put(wbsd_device);
  1577. platform_driver_unregister(&wbsd_driver);
  1578. return result;
  1579. }
  1580. }
  1581. return 0;
  1582. }
  1583. static void __exit wbsd_drv_exit(void)
  1584. {
  1585. #ifdef CONFIG_PNP
  1586. if (!param_nopnp)
  1587. pnp_unregister_driver(&wbsd_pnp_driver);
  1588. #endif /* CONFIG_PNP */
  1589. if (param_nopnp) {
  1590. platform_device_unregister(wbsd_device);
  1591. platform_driver_unregister(&wbsd_driver);
  1592. }
  1593. DBG("unloaded\n");
  1594. }
  1595. module_init(wbsd_drv_init);
  1596. module_exit(wbsd_drv_exit);
  1597. #ifdef CONFIG_PNP
  1598. module_param_hw_named(nopnp, param_nopnp, uint, other, 0444);
  1599. #endif
  1600. module_param_hw_named(io, param_io, uint, ioport, 0444);
  1601. module_param_hw_named(irq, param_irq, uint, irq, 0444);
  1602. module_param_hw_named(dma, param_dma, int, dma, 0444);
  1603. MODULE_LICENSE("GPL");
  1604. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1605. MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
  1606. #ifdef CONFIG_PNP
  1607. MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
  1608. #endif
  1609. MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
  1610. MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
  1611. MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");