wbsd.c 43 KB

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