wbsd.c 43 KB

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