wbsd.c 39 KB

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