wbsd.c 39 KB

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