wbsd.c 40 KB

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