wbsd.c 42 KB

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