wbsd.c 40 KB

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