wbsd.c 39 KB

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