wbsd.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014
  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. #ifdef CONFIG_MMC_DEBUG
  680. pr_warn("%s: Data command %d is not 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. * transferred.
  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 immediately
  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. pr_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 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_segs = 128;
  1013. /*
  1014. * Maximum request size. Also limited by 64KiB buffer.
  1015. */
  1016. mmc->max_req_size = 65536;
  1017. /*
  1018. * Maximum segment size. Could be one segment with the maximum number
  1019. * of bytes.
  1020. */
  1021. mmc->max_seg_size = mmc->max_req_size;
  1022. /*
  1023. * Maximum block size. We have 12 bits (= 4095) but have to subtract
  1024. * space for CRC. So the maximum is 4095 - 4*2 = 4087.
  1025. */
  1026. mmc->max_blk_size = 4087;
  1027. /*
  1028. * Maximum block count. There is no real limit so the maximum
  1029. * request size will be the only restriction.
  1030. */
  1031. mmc->max_blk_count = mmc->max_req_size;
  1032. dev_set_drvdata(dev, mmc);
  1033. return 0;
  1034. }
  1035. static void wbsd_free_mmc(struct device *dev)
  1036. {
  1037. struct mmc_host *mmc;
  1038. struct wbsd_host *host;
  1039. mmc = dev_get_drvdata(dev);
  1040. if (!mmc)
  1041. return;
  1042. host = mmc_priv(mmc);
  1043. BUG_ON(host == NULL);
  1044. del_timer_sync(&host->ignore_timer);
  1045. mmc_free_host(mmc);
  1046. dev_set_drvdata(dev, NULL);
  1047. }
  1048. /*
  1049. * Scan for known chip id:s
  1050. */
  1051. static int wbsd_scan(struct wbsd_host *host)
  1052. {
  1053. int i, j, k;
  1054. int id;
  1055. /*
  1056. * Iterate through all ports, all codes to
  1057. * find hardware that is in our known list.
  1058. */
  1059. for (i = 0; i < ARRAY_SIZE(config_ports); i++) {
  1060. if (!request_region(config_ports[i], 2, DRIVER_NAME))
  1061. continue;
  1062. for (j = 0; j < ARRAY_SIZE(unlock_codes); j++) {
  1063. id = 0xFFFF;
  1064. host->config = config_ports[i];
  1065. host->unlock_code = unlock_codes[j];
  1066. wbsd_unlock_config(host);
  1067. outb(WBSD_CONF_ID_HI, config_ports[i]);
  1068. id = inb(config_ports[i] + 1) << 8;
  1069. outb(WBSD_CONF_ID_LO, config_ports[i]);
  1070. id |= inb(config_ports[i] + 1);
  1071. wbsd_lock_config(host);
  1072. for (k = 0; k < ARRAY_SIZE(valid_ids); k++) {
  1073. if (id == valid_ids[k]) {
  1074. host->chip_id = id;
  1075. return 0;
  1076. }
  1077. }
  1078. if (id != 0xFFFF) {
  1079. DBG("Unknown hardware (id %x) found at %x\n",
  1080. id, config_ports[i]);
  1081. }
  1082. }
  1083. release_region(config_ports[i], 2);
  1084. }
  1085. host->config = 0;
  1086. host->unlock_code = 0;
  1087. return -ENODEV;
  1088. }
  1089. /*
  1090. * Allocate/free io port ranges
  1091. */
  1092. static int wbsd_request_region(struct wbsd_host *host, int base)
  1093. {
  1094. if (base & 0x7)
  1095. return -EINVAL;
  1096. if (!request_region(base, 8, DRIVER_NAME))
  1097. return -EIO;
  1098. host->base = base;
  1099. return 0;
  1100. }
  1101. static void wbsd_release_regions(struct wbsd_host *host)
  1102. {
  1103. if (host->base)
  1104. release_region(host->base, 8);
  1105. host->base = 0;
  1106. if (host->config)
  1107. release_region(host->config, 2);
  1108. host->config = 0;
  1109. }
  1110. /*
  1111. * Allocate/free DMA port and buffer
  1112. */
  1113. static void wbsd_request_dma(struct wbsd_host *host, int dma)
  1114. {
  1115. if (dma < 0)
  1116. return;
  1117. if (request_dma(dma, DRIVER_NAME))
  1118. goto err;
  1119. /*
  1120. * We need to allocate a special buffer in
  1121. * order for ISA to be able to DMA to it.
  1122. */
  1123. host->dma_buffer = kmalloc(WBSD_DMA_SIZE,
  1124. GFP_NOIO | GFP_DMA | __GFP_REPEAT | __GFP_NOWARN);
  1125. if (!host->dma_buffer)
  1126. goto free;
  1127. /*
  1128. * Translate the address to a physical address.
  1129. */
  1130. host->dma_addr = dma_map_single(mmc_dev(host->mmc), host->dma_buffer,
  1131. WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
  1132. /*
  1133. * ISA DMA must be aligned on a 64k basis.
  1134. */
  1135. if ((host->dma_addr & 0xffff) != 0)
  1136. goto kfree;
  1137. /*
  1138. * ISA cannot access memory above 16 MB.
  1139. */
  1140. else if (host->dma_addr >= 0x1000000)
  1141. goto kfree;
  1142. host->dma = dma;
  1143. return;
  1144. kfree:
  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(host->dma_buffer);
  1153. host->dma_buffer = NULL;
  1154. free:
  1155. free_dma(dma);
  1156. err:
  1157. pr_warn(DRIVER_NAME ": Unable to allocate DMA %d - falling back on FIFO\n",
  1158. dma);
  1159. }
  1160. static void wbsd_release_dma(struct wbsd_host *host)
  1161. {
  1162. if (host->dma_addr) {
  1163. dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
  1164. WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
  1165. }
  1166. kfree(host->dma_buffer);
  1167. if (host->dma >= 0)
  1168. free_dma(host->dma);
  1169. host->dma = -1;
  1170. host->dma_buffer = NULL;
  1171. host->dma_addr = 0;
  1172. }
  1173. /*
  1174. * Allocate/free IRQ.
  1175. */
  1176. static int wbsd_request_irq(struct wbsd_host *host, int irq)
  1177. {
  1178. int ret;
  1179. /*
  1180. * Set up tasklets. Must be done before requesting interrupt.
  1181. */
  1182. tasklet_init(&host->card_tasklet, wbsd_tasklet_card,
  1183. (unsigned long)host);
  1184. tasklet_init(&host->fifo_tasklet, wbsd_tasklet_fifo,
  1185. (unsigned long)host);
  1186. tasklet_init(&host->crc_tasklet, wbsd_tasklet_crc,
  1187. (unsigned long)host);
  1188. tasklet_init(&host->timeout_tasklet, wbsd_tasklet_timeout,
  1189. (unsigned long)host);
  1190. tasklet_init(&host->finish_tasklet, wbsd_tasklet_finish,
  1191. (unsigned long)host);
  1192. /*
  1193. * Allocate interrupt.
  1194. */
  1195. ret = request_irq(irq, wbsd_irq, IRQF_SHARED, DRIVER_NAME, host);
  1196. if (ret)
  1197. return ret;
  1198. host->irq = irq;
  1199. return 0;
  1200. }
  1201. static void wbsd_release_irq(struct wbsd_host *host)
  1202. {
  1203. if (!host->irq)
  1204. return;
  1205. free_irq(host->irq, host);
  1206. host->irq = 0;
  1207. tasklet_kill(&host->card_tasklet);
  1208. tasklet_kill(&host->fifo_tasklet);
  1209. tasklet_kill(&host->crc_tasklet);
  1210. tasklet_kill(&host->timeout_tasklet);
  1211. tasklet_kill(&host->finish_tasklet);
  1212. }
  1213. /*
  1214. * Allocate all resources for the host.
  1215. */
  1216. static int wbsd_request_resources(struct wbsd_host *host,
  1217. int base, int irq, int dma)
  1218. {
  1219. int ret;
  1220. /*
  1221. * Allocate I/O ports.
  1222. */
  1223. ret = wbsd_request_region(host, base);
  1224. if (ret)
  1225. return ret;
  1226. /*
  1227. * Allocate interrupt.
  1228. */
  1229. ret = wbsd_request_irq(host, irq);
  1230. if (ret)
  1231. return ret;
  1232. /*
  1233. * Allocate DMA.
  1234. */
  1235. wbsd_request_dma(host, dma);
  1236. return 0;
  1237. }
  1238. /*
  1239. * Release all resources for the host.
  1240. */
  1241. static void wbsd_release_resources(struct wbsd_host *host)
  1242. {
  1243. wbsd_release_dma(host);
  1244. wbsd_release_irq(host);
  1245. wbsd_release_regions(host);
  1246. }
  1247. /*
  1248. * Configure the resources the chip should use.
  1249. */
  1250. static void wbsd_chip_config(struct wbsd_host *host)
  1251. {
  1252. wbsd_unlock_config(host);
  1253. /*
  1254. * Reset the chip.
  1255. */
  1256. wbsd_write_config(host, WBSD_CONF_SWRST, 1);
  1257. wbsd_write_config(host, WBSD_CONF_SWRST, 0);
  1258. /*
  1259. * Select SD/MMC function.
  1260. */
  1261. wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
  1262. /*
  1263. * Set up card detection.
  1264. */
  1265. wbsd_write_config(host, WBSD_CONF_PINS, WBSD_PINS_DETECT_GP11);
  1266. /*
  1267. * Configure chip
  1268. */
  1269. wbsd_write_config(host, WBSD_CONF_PORT_HI, host->base >> 8);
  1270. wbsd_write_config(host, WBSD_CONF_PORT_LO, host->base & 0xff);
  1271. wbsd_write_config(host, WBSD_CONF_IRQ, host->irq);
  1272. if (host->dma >= 0)
  1273. wbsd_write_config(host, WBSD_CONF_DRQ, host->dma);
  1274. /*
  1275. * Enable and power up chip.
  1276. */
  1277. wbsd_write_config(host, WBSD_CONF_ENABLE, 1);
  1278. wbsd_write_config(host, WBSD_CONF_POWER, 0x20);
  1279. wbsd_lock_config(host);
  1280. }
  1281. /*
  1282. * Check that configured resources are correct.
  1283. */
  1284. static int wbsd_chip_validate(struct wbsd_host *host)
  1285. {
  1286. int base, irq, dma;
  1287. wbsd_unlock_config(host);
  1288. /*
  1289. * Select SD/MMC function.
  1290. */
  1291. wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
  1292. /*
  1293. * Read configuration.
  1294. */
  1295. base = wbsd_read_config(host, WBSD_CONF_PORT_HI) << 8;
  1296. base |= wbsd_read_config(host, WBSD_CONF_PORT_LO);
  1297. irq = wbsd_read_config(host, WBSD_CONF_IRQ);
  1298. dma = wbsd_read_config(host, WBSD_CONF_DRQ);
  1299. wbsd_lock_config(host);
  1300. /*
  1301. * Validate against given configuration.
  1302. */
  1303. if (base != host->base)
  1304. return 0;
  1305. if (irq != host->irq)
  1306. return 0;
  1307. if ((dma != host->dma) && (host->dma != -1))
  1308. return 0;
  1309. return 1;
  1310. }
  1311. /*
  1312. * Powers down the SD function
  1313. */
  1314. static void wbsd_chip_poweroff(struct wbsd_host *host)
  1315. {
  1316. wbsd_unlock_config(host);
  1317. wbsd_write_config(host, WBSD_CONF_DEVICE, DEVICE_SD);
  1318. wbsd_write_config(host, WBSD_CONF_ENABLE, 0);
  1319. wbsd_lock_config(host);
  1320. }
  1321. /*****************************************************************************\
  1322. * *
  1323. * Devices setup and shutdown *
  1324. * *
  1325. \*****************************************************************************/
  1326. static int wbsd_init(struct device *dev, int base, int irq, int dma,
  1327. int pnp)
  1328. {
  1329. struct wbsd_host *host = NULL;
  1330. struct mmc_host *mmc = NULL;
  1331. int ret;
  1332. ret = wbsd_alloc_mmc(dev);
  1333. if (ret)
  1334. return ret;
  1335. mmc = dev_get_drvdata(dev);
  1336. host = mmc_priv(mmc);
  1337. /*
  1338. * Scan for hardware.
  1339. */
  1340. ret = wbsd_scan(host);
  1341. if (ret) {
  1342. if (pnp && (ret == -ENODEV)) {
  1343. pr_warn(DRIVER_NAME ": Unable to confirm device presence - you may experience lock-ups\n");
  1344. } else {
  1345. wbsd_free_mmc(dev);
  1346. return ret;
  1347. }
  1348. }
  1349. /*
  1350. * Request resources.
  1351. */
  1352. ret = wbsd_request_resources(host, base, irq, dma);
  1353. if (ret) {
  1354. wbsd_release_resources(host);
  1355. wbsd_free_mmc(dev);
  1356. return ret;
  1357. }
  1358. /*
  1359. * See if chip needs to be configured.
  1360. */
  1361. if (pnp) {
  1362. if ((host->config != 0) && !wbsd_chip_validate(host)) {
  1363. pr_warn(DRIVER_NAME ": PnP active but chip not configured! You probably have a buggy BIOS. Configuring chip manually.\n");
  1364. wbsd_chip_config(host);
  1365. }
  1366. } else
  1367. wbsd_chip_config(host);
  1368. /*
  1369. * Power Management stuff. No idea how this works.
  1370. * Not tested.
  1371. */
  1372. #ifdef CONFIG_PM
  1373. if (host->config) {
  1374. wbsd_unlock_config(host);
  1375. wbsd_write_config(host, WBSD_CONF_PME, 0xA0);
  1376. wbsd_lock_config(host);
  1377. }
  1378. #endif
  1379. /*
  1380. * Allow device to initialise itself properly.
  1381. */
  1382. mdelay(5);
  1383. /*
  1384. * Reset the chip into a known state.
  1385. */
  1386. wbsd_init_device(host);
  1387. mmc_add_host(mmc);
  1388. pr_info("%s: W83L51xD", mmc_hostname(mmc));
  1389. if (host->chip_id != 0)
  1390. printk(" id %x", (int)host->chip_id);
  1391. printk(" at 0x%x irq %d", (int)host->base, (int)host->irq);
  1392. if (host->dma >= 0)
  1393. printk(" dma %d", (int)host->dma);
  1394. else
  1395. printk(" FIFO");
  1396. if (pnp)
  1397. printk(" PnP");
  1398. printk("\n");
  1399. return 0;
  1400. }
  1401. static void wbsd_shutdown(struct device *dev, int pnp)
  1402. {
  1403. struct mmc_host *mmc = dev_get_drvdata(dev);
  1404. struct wbsd_host *host;
  1405. if (!mmc)
  1406. return;
  1407. host = mmc_priv(mmc);
  1408. mmc_remove_host(mmc);
  1409. /*
  1410. * Power down the SD/MMC function.
  1411. */
  1412. if (!pnp)
  1413. wbsd_chip_poweroff(host);
  1414. wbsd_release_resources(host);
  1415. wbsd_free_mmc(dev);
  1416. }
  1417. /*
  1418. * Non-PnP
  1419. */
  1420. static int wbsd_probe(struct platform_device *dev)
  1421. {
  1422. /* Use the module parameters for resources */
  1423. return wbsd_init(&dev->dev, param_io, param_irq, param_dma, 0);
  1424. }
  1425. static int wbsd_remove(struct platform_device *dev)
  1426. {
  1427. wbsd_shutdown(&dev->dev, 0);
  1428. return 0;
  1429. }
  1430. /*
  1431. * PnP
  1432. */
  1433. #ifdef CONFIG_PNP
  1434. static int
  1435. wbsd_pnp_probe(struct pnp_dev *pnpdev, const struct pnp_device_id *dev_id)
  1436. {
  1437. int io, irq, dma;
  1438. /*
  1439. * Get resources from PnP layer.
  1440. */
  1441. io = pnp_port_start(pnpdev, 0);
  1442. irq = pnp_irq(pnpdev, 0);
  1443. if (pnp_dma_valid(pnpdev, 0))
  1444. dma = pnp_dma(pnpdev, 0);
  1445. else
  1446. dma = -1;
  1447. DBGF("PnP resources: port %3x irq %d dma %d\n", io, irq, dma);
  1448. return wbsd_init(&pnpdev->dev, io, irq, dma, 1);
  1449. }
  1450. static void wbsd_pnp_remove(struct pnp_dev *dev)
  1451. {
  1452. wbsd_shutdown(&dev->dev, 1);
  1453. }
  1454. #endif /* CONFIG_PNP */
  1455. /*
  1456. * Power management
  1457. */
  1458. #ifdef CONFIG_PM
  1459. static int wbsd_platform_suspend(struct platform_device *dev,
  1460. pm_message_t state)
  1461. {
  1462. struct mmc_host *mmc = platform_get_drvdata(dev);
  1463. struct wbsd_host *host;
  1464. if (mmc == NULL)
  1465. return 0;
  1466. DBGF("Suspending...\n");
  1467. host = mmc_priv(mmc);
  1468. wbsd_chip_poweroff(host);
  1469. return 0;
  1470. }
  1471. static int wbsd_platform_resume(struct platform_device *dev)
  1472. {
  1473. struct mmc_host *mmc = platform_get_drvdata(dev);
  1474. struct wbsd_host *host;
  1475. if (mmc == NULL)
  1476. return 0;
  1477. DBGF("Resuming...\n");
  1478. host = mmc_priv(mmc);
  1479. wbsd_chip_config(host);
  1480. /*
  1481. * Allow device to initialise itself properly.
  1482. */
  1483. mdelay(5);
  1484. wbsd_init_device(host);
  1485. return 0;
  1486. }
  1487. #ifdef CONFIG_PNP
  1488. static int wbsd_pnp_suspend(struct pnp_dev *pnp_dev, pm_message_t state)
  1489. {
  1490. struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
  1491. if (mmc == NULL)
  1492. return 0;
  1493. DBGF("Suspending...\n");
  1494. return 0;
  1495. }
  1496. static int wbsd_pnp_resume(struct pnp_dev *pnp_dev)
  1497. {
  1498. struct mmc_host *mmc = dev_get_drvdata(&pnp_dev->dev);
  1499. struct wbsd_host *host;
  1500. if (mmc == NULL)
  1501. return 0;
  1502. DBGF("Resuming...\n");
  1503. host = mmc_priv(mmc);
  1504. /*
  1505. * See if chip needs to be configured.
  1506. */
  1507. if (host->config != 0) {
  1508. if (!wbsd_chip_validate(host)) {
  1509. pr_warn(DRIVER_NAME ": PnP active but chip not configured! You probably have a buggy BIOS. Configuring chip manually.\n");
  1510. wbsd_chip_config(host);
  1511. }
  1512. }
  1513. /*
  1514. * Allow device to initialise itself properly.
  1515. */
  1516. mdelay(5);
  1517. wbsd_init_device(host);
  1518. return 0;
  1519. }
  1520. #endif /* CONFIG_PNP */
  1521. #else /* CONFIG_PM */
  1522. #define wbsd_platform_suspend NULL
  1523. #define wbsd_platform_resume NULL
  1524. #define wbsd_pnp_suspend NULL
  1525. #define wbsd_pnp_resume NULL
  1526. #endif /* CONFIG_PM */
  1527. static struct platform_device *wbsd_device;
  1528. static struct platform_driver wbsd_driver = {
  1529. .probe = wbsd_probe,
  1530. .remove = wbsd_remove,
  1531. .suspend = wbsd_platform_suspend,
  1532. .resume = wbsd_platform_resume,
  1533. .driver = {
  1534. .name = DRIVER_NAME,
  1535. },
  1536. };
  1537. #ifdef CONFIG_PNP
  1538. static struct pnp_driver wbsd_pnp_driver = {
  1539. .name = DRIVER_NAME,
  1540. .id_table = pnp_dev_table,
  1541. .probe = wbsd_pnp_probe,
  1542. .remove = wbsd_pnp_remove,
  1543. .suspend = wbsd_pnp_suspend,
  1544. .resume = wbsd_pnp_resume,
  1545. };
  1546. #endif /* CONFIG_PNP */
  1547. /*
  1548. * Module loading/unloading
  1549. */
  1550. static int __init wbsd_drv_init(void)
  1551. {
  1552. int result;
  1553. pr_info(DRIVER_NAME
  1554. ": Winbond W83L51xD SD/MMC card interface driver\n");
  1555. pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  1556. #ifdef CONFIG_PNP
  1557. if (!param_nopnp) {
  1558. result = pnp_register_driver(&wbsd_pnp_driver);
  1559. if (result < 0)
  1560. return result;
  1561. }
  1562. #endif /* CONFIG_PNP */
  1563. if (param_nopnp) {
  1564. result = platform_driver_register(&wbsd_driver);
  1565. if (result < 0)
  1566. return result;
  1567. wbsd_device = platform_device_alloc(DRIVER_NAME, -1);
  1568. if (!wbsd_device) {
  1569. platform_driver_unregister(&wbsd_driver);
  1570. return -ENOMEM;
  1571. }
  1572. result = platform_device_add(wbsd_device);
  1573. if (result) {
  1574. platform_device_put(wbsd_device);
  1575. platform_driver_unregister(&wbsd_driver);
  1576. return result;
  1577. }
  1578. }
  1579. return 0;
  1580. }
  1581. static void __exit wbsd_drv_exit(void)
  1582. {
  1583. #ifdef CONFIG_PNP
  1584. if (!param_nopnp)
  1585. pnp_unregister_driver(&wbsd_pnp_driver);
  1586. #endif /* CONFIG_PNP */
  1587. if (param_nopnp) {
  1588. platform_device_unregister(wbsd_device);
  1589. platform_driver_unregister(&wbsd_driver);
  1590. }
  1591. DBG("unloaded\n");
  1592. }
  1593. module_init(wbsd_drv_init);
  1594. module_exit(wbsd_drv_exit);
  1595. #ifdef CONFIG_PNP
  1596. module_param_named(nopnp, param_nopnp, uint, 0444);
  1597. #endif
  1598. module_param_named(io, param_io, uint, 0444);
  1599. module_param_named(irq, param_irq, uint, 0444);
  1600. module_param_named(dma, param_dma, int, 0444);
  1601. MODULE_LICENSE("GPL");
  1602. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1603. MODULE_DESCRIPTION("Winbond W83L51xD SD/MMC card interface driver");
  1604. #ifdef CONFIG_PNP
  1605. MODULE_PARM_DESC(nopnp, "Scan for device instead of relying on PNP. (default 0)");
  1606. #endif
  1607. MODULE_PARM_DESC(io, "I/O base to allocate. Must be 8 byte aligned. (default 0x248)");
  1608. MODULE_PARM_DESC(irq, "IRQ to allocate. (default 6)");
  1609. MODULE_PARM_DESC(dma, "DMA channel to allocate. -1 for no DMA. (default 2)");