wbsd.c 42 KB

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