wbsd.c 39 KB

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