wbsd.c 42 KB

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