wbsd.c 41 KB

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