wbsd.c 39 KB

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