wbsd.c 40 KB

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