wbsd.c 39 KB

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