wbsd.c 42 KB

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