omap.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  1. /*
  2. * linux/drivers/mmc/host/omap.c
  3. *
  4. * Copyright (C) 2004 Nokia Corporation
  5. * Written by Tuukka Tikkanen and Juha Yrjölä<juha.yrjola@nokia.com>
  6. * Misc hacks here and there by Tony Lindgren <tony@atomide.com>
  7. * Other hacks (DMA, SD, etc) by David Brownell
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/ioport.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/delay.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/timer.h>
  23. #include <linux/mmc/host.h>
  24. #include <linux/mmc/card.h>
  25. #include <linux/clk.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/i2c/tps65010.h>
  28. #include <asm/io.h>
  29. #include <asm/irq.h>
  30. #include <asm/mach-types.h>
  31. #include <asm/arch/board.h>
  32. #include <asm/arch/mmc.h>
  33. #include <asm/arch/gpio.h>
  34. #include <asm/arch/dma.h>
  35. #include <asm/arch/mux.h>
  36. #include <asm/arch/fpga.h>
  37. #define OMAP_MMC_REG_CMD 0x00
  38. #define OMAP_MMC_REG_ARGL 0x04
  39. #define OMAP_MMC_REG_ARGH 0x08
  40. #define OMAP_MMC_REG_CON 0x0c
  41. #define OMAP_MMC_REG_STAT 0x10
  42. #define OMAP_MMC_REG_IE 0x14
  43. #define OMAP_MMC_REG_CTO 0x18
  44. #define OMAP_MMC_REG_DTO 0x1c
  45. #define OMAP_MMC_REG_DATA 0x20
  46. #define OMAP_MMC_REG_BLEN 0x24
  47. #define OMAP_MMC_REG_NBLK 0x28
  48. #define OMAP_MMC_REG_BUF 0x2c
  49. #define OMAP_MMC_REG_SDIO 0x34
  50. #define OMAP_MMC_REG_REV 0x3c
  51. #define OMAP_MMC_REG_RSP0 0x40
  52. #define OMAP_MMC_REG_RSP1 0x44
  53. #define OMAP_MMC_REG_RSP2 0x48
  54. #define OMAP_MMC_REG_RSP3 0x4c
  55. #define OMAP_MMC_REG_RSP4 0x50
  56. #define OMAP_MMC_REG_RSP5 0x54
  57. #define OMAP_MMC_REG_RSP6 0x58
  58. #define OMAP_MMC_REG_RSP7 0x5c
  59. #define OMAP_MMC_REG_IOSR 0x60
  60. #define OMAP_MMC_REG_SYSC 0x64
  61. #define OMAP_MMC_REG_SYSS 0x68
  62. #define OMAP_MMC_STAT_CARD_ERR (1 << 14)
  63. #define OMAP_MMC_STAT_CARD_IRQ (1 << 13)
  64. #define OMAP_MMC_STAT_OCR_BUSY (1 << 12)
  65. #define OMAP_MMC_STAT_A_EMPTY (1 << 11)
  66. #define OMAP_MMC_STAT_A_FULL (1 << 10)
  67. #define OMAP_MMC_STAT_CMD_CRC (1 << 8)
  68. #define OMAP_MMC_STAT_CMD_TOUT (1 << 7)
  69. #define OMAP_MMC_STAT_DATA_CRC (1 << 6)
  70. #define OMAP_MMC_STAT_DATA_TOUT (1 << 5)
  71. #define OMAP_MMC_STAT_END_BUSY (1 << 4)
  72. #define OMAP_MMC_STAT_END_OF_DATA (1 << 3)
  73. #define OMAP_MMC_STAT_CARD_BUSY (1 << 2)
  74. #define OMAP_MMC_STAT_END_OF_CMD (1 << 0)
  75. #define OMAP_MMC_READ(host, reg) __raw_readw((host)->virt_base + OMAP_MMC_REG_##reg)
  76. #define OMAP_MMC_WRITE(host, reg, val) __raw_writew((val), (host)->virt_base + OMAP_MMC_REG_##reg)
  77. /*
  78. * Command types
  79. */
  80. #define OMAP_MMC_CMDTYPE_BC 0
  81. #define OMAP_MMC_CMDTYPE_BCR 1
  82. #define OMAP_MMC_CMDTYPE_AC 2
  83. #define OMAP_MMC_CMDTYPE_ADTC 3
  84. #define DRIVER_NAME "mmci-omap"
  85. /* Specifies how often in millisecs to poll for card status changes
  86. * when the cover switch is open */
  87. #define OMAP_MMC_COVER_POLL_DELAY 500
  88. struct mmc_omap_host;
  89. struct mmc_omap_slot {
  90. int id;
  91. unsigned int vdd;
  92. u16 saved_con;
  93. u16 bus_mode;
  94. unsigned int fclk_freq;
  95. unsigned powered:1;
  96. struct tasklet_struct cover_tasklet;
  97. struct timer_list cover_timer;
  98. unsigned cover_open;
  99. struct mmc_request *mrq;
  100. struct mmc_omap_host *host;
  101. struct mmc_host *mmc;
  102. struct omap_mmc_slot_data *pdata;
  103. };
  104. struct mmc_omap_host {
  105. int initialized;
  106. int suspended;
  107. struct mmc_request * mrq;
  108. struct mmc_command * cmd;
  109. struct mmc_data * data;
  110. struct mmc_host * mmc;
  111. struct device * dev;
  112. unsigned char id; /* 16xx chips have 2 MMC blocks */
  113. struct clk * iclk;
  114. struct clk * fclk;
  115. struct resource *mem_res;
  116. void __iomem *virt_base;
  117. unsigned int phys_base;
  118. int irq;
  119. unsigned char bus_mode;
  120. unsigned char hw_bus_mode;
  121. struct work_struct cmd_abort_work;
  122. unsigned abort:1;
  123. struct timer_list cmd_abort_timer;
  124. unsigned int sg_len;
  125. int sg_idx;
  126. u16 * buffer;
  127. u32 buffer_bytes_left;
  128. u32 total_bytes_left;
  129. unsigned use_dma:1;
  130. unsigned brs_received:1, dma_done:1;
  131. unsigned dma_is_read:1;
  132. unsigned dma_in_use:1;
  133. int dma_ch;
  134. spinlock_t dma_lock;
  135. struct timer_list dma_timer;
  136. unsigned dma_len;
  137. short power_pin;
  138. struct mmc_omap_slot *slots[OMAP_MMC_MAX_SLOTS];
  139. struct mmc_omap_slot *current_slot;
  140. spinlock_t slot_lock;
  141. wait_queue_head_t slot_wq;
  142. int nr_slots;
  143. struct omap_mmc_platform_data *pdata;
  144. };
  145. static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
  146. {
  147. struct mmc_omap_host *host = slot->host;
  148. unsigned long flags;
  149. if (claimed)
  150. goto no_claim;
  151. spin_lock_irqsave(&host->slot_lock, flags);
  152. while (host->mmc != NULL) {
  153. spin_unlock_irqrestore(&host->slot_lock, flags);
  154. wait_event(host->slot_wq, host->mmc == NULL);
  155. spin_lock_irqsave(&host->slot_lock, flags);
  156. }
  157. host->mmc = slot->mmc;
  158. spin_unlock_irqrestore(&host->slot_lock, flags);
  159. no_claim:
  160. clk_enable(host->fclk);
  161. if (host->current_slot != slot) {
  162. if (host->pdata->switch_slot != NULL)
  163. host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
  164. host->current_slot = slot;
  165. }
  166. /* Doing the dummy read here seems to work around some bug
  167. * at least in OMAP24xx silicon where the command would not
  168. * start after writing the CMD register. Sigh. */
  169. OMAP_MMC_READ(host, CON);
  170. OMAP_MMC_WRITE(host, CON, slot->saved_con);
  171. }
  172. static void mmc_omap_start_request(struct mmc_omap_host *host,
  173. struct mmc_request *req);
  174. static void mmc_omap_release_slot(struct mmc_omap_slot *slot)
  175. {
  176. struct mmc_omap_host *host = slot->host;
  177. unsigned long flags;
  178. int i;
  179. BUG_ON(slot == NULL || host->mmc == NULL);
  180. clk_disable(host->fclk);
  181. spin_lock_irqsave(&host->slot_lock, flags);
  182. /* Check for any pending requests */
  183. for (i = 0; i < host->nr_slots; i++) {
  184. struct mmc_omap_slot *new_slot;
  185. struct mmc_request *rq;
  186. if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
  187. continue;
  188. new_slot = host->slots[i];
  189. /* The current slot should not have a request in queue */
  190. BUG_ON(new_slot == host->current_slot);
  191. host->mmc = new_slot->mmc;
  192. spin_unlock_irqrestore(&host->slot_lock, flags);
  193. mmc_omap_select_slot(new_slot, 1);
  194. rq = new_slot->mrq;
  195. new_slot->mrq = NULL;
  196. mmc_omap_start_request(host, rq);
  197. return;
  198. }
  199. host->mmc = NULL;
  200. wake_up(&host->slot_wq);
  201. spin_unlock_irqrestore(&host->slot_lock, flags);
  202. }
  203. static inline
  204. int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
  205. {
  206. if (slot->pdata->get_cover_state)
  207. return slot->pdata->get_cover_state(mmc_dev(slot->mmc),
  208. slot->id);
  209. return 0;
  210. }
  211. static ssize_t
  212. mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
  213. char *buf)
  214. {
  215. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  216. struct mmc_omap_slot *slot = mmc_priv(mmc);
  217. return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
  218. "closed");
  219. }
  220. static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
  221. static ssize_t
  222. mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
  223. char *buf)
  224. {
  225. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  226. struct mmc_omap_slot *slot = mmc_priv(mmc);
  227. return sprintf(buf, "%s\n", slot->pdata->name);
  228. }
  229. static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
  230. static void
  231. mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
  232. {
  233. u32 cmdreg;
  234. u32 resptype;
  235. u32 cmdtype;
  236. host->cmd = cmd;
  237. resptype = 0;
  238. cmdtype = 0;
  239. /* Our hardware needs to know exact type */
  240. switch (mmc_resp_type(cmd)) {
  241. case MMC_RSP_NONE:
  242. break;
  243. case MMC_RSP_R1:
  244. case MMC_RSP_R1B:
  245. /* resp 1, 1b, 6, 7 */
  246. resptype = 1;
  247. break;
  248. case MMC_RSP_R2:
  249. resptype = 2;
  250. break;
  251. case MMC_RSP_R3:
  252. resptype = 3;
  253. break;
  254. default:
  255. dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
  256. break;
  257. }
  258. if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
  259. cmdtype = OMAP_MMC_CMDTYPE_ADTC;
  260. } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
  261. cmdtype = OMAP_MMC_CMDTYPE_BC;
  262. } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
  263. cmdtype = OMAP_MMC_CMDTYPE_BCR;
  264. } else {
  265. cmdtype = OMAP_MMC_CMDTYPE_AC;
  266. }
  267. cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
  268. if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
  269. cmdreg |= 1 << 6;
  270. if (cmd->flags & MMC_RSP_BUSY)
  271. cmdreg |= 1 << 11;
  272. if (host->data && !(host->data->flags & MMC_DATA_WRITE))
  273. cmdreg |= 1 << 15;
  274. mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
  275. OMAP_MMC_WRITE(host, CTO, 200);
  276. OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
  277. OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
  278. OMAP_MMC_WRITE(host, IE,
  279. OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
  280. OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
  281. OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
  282. OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
  283. OMAP_MMC_STAT_END_OF_DATA);
  284. OMAP_MMC_WRITE(host, CMD, cmdreg);
  285. }
  286. static void
  287. mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
  288. int abort)
  289. {
  290. enum dma_data_direction dma_data_dir;
  291. BUG_ON(host->dma_ch < 0);
  292. if (data->error)
  293. omap_stop_dma(host->dma_ch);
  294. /* Release DMA channel lazily */
  295. mod_timer(&host->dma_timer, jiffies + HZ);
  296. if (data->flags & MMC_DATA_WRITE)
  297. dma_data_dir = DMA_TO_DEVICE;
  298. else
  299. dma_data_dir = DMA_FROM_DEVICE;
  300. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
  301. dma_data_dir);
  302. }
  303. static void
  304. mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
  305. {
  306. if (host->dma_in_use)
  307. mmc_omap_release_dma(host, data, data->error);
  308. host->data = NULL;
  309. host->sg_len = 0;
  310. /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
  311. * dozens of requests until the card finishes writing data.
  312. * It'd be cheaper to just wait till an EOFB interrupt arrives...
  313. */
  314. if (!data->stop) {
  315. struct mmc_host *mmc;
  316. host->mrq = NULL;
  317. mmc = host->mmc;
  318. mmc_omap_release_slot(host->current_slot);
  319. mmc_request_done(mmc, data->mrq);
  320. return;
  321. }
  322. mmc_omap_start_command(host, data->stop);
  323. }
  324. static void
  325. mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
  326. {
  327. struct mmc_omap_slot *slot = host->current_slot;
  328. unsigned int restarts, passes, timeout;
  329. u16 stat = 0;
  330. /* Sending abort takes 80 clocks. Have some extra and round up */
  331. timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
  332. restarts = 0;
  333. while (restarts < maxloops) {
  334. OMAP_MMC_WRITE(host, STAT, 0xFFFF);
  335. OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
  336. passes = 0;
  337. while (passes < timeout) {
  338. stat = OMAP_MMC_READ(host, STAT);
  339. if (stat & OMAP_MMC_STAT_END_OF_CMD)
  340. goto out;
  341. udelay(1);
  342. passes++;
  343. }
  344. restarts++;
  345. }
  346. out:
  347. OMAP_MMC_WRITE(host, STAT, stat);
  348. }
  349. static void
  350. mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
  351. {
  352. if (host->dma_in_use)
  353. mmc_omap_release_dma(host, data, 1);
  354. host->data = NULL;
  355. host->sg_len = 0;
  356. mmc_omap_send_abort(host, 10000);
  357. }
  358. static void
  359. mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
  360. {
  361. unsigned long flags;
  362. int done;
  363. if (!host->dma_in_use) {
  364. mmc_omap_xfer_done(host, data);
  365. return;
  366. }
  367. done = 0;
  368. spin_lock_irqsave(&host->dma_lock, flags);
  369. if (host->dma_done)
  370. done = 1;
  371. else
  372. host->brs_received = 1;
  373. spin_unlock_irqrestore(&host->dma_lock, flags);
  374. if (done)
  375. mmc_omap_xfer_done(host, data);
  376. }
  377. static void
  378. mmc_omap_dma_timer(unsigned long data)
  379. {
  380. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  381. BUG_ON(host->dma_ch < 0);
  382. omap_free_dma(host->dma_ch);
  383. host->dma_ch = -1;
  384. }
  385. static void
  386. mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
  387. {
  388. unsigned long flags;
  389. int done;
  390. done = 0;
  391. spin_lock_irqsave(&host->dma_lock, flags);
  392. if (host->brs_received)
  393. done = 1;
  394. else
  395. host->dma_done = 1;
  396. spin_unlock_irqrestore(&host->dma_lock, flags);
  397. if (done)
  398. mmc_omap_xfer_done(host, data);
  399. }
  400. static void
  401. mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
  402. {
  403. host->cmd = NULL;
  404. del_timer(&host->cmd_abort_timer);
  405. if (cmd->flags & MMC_RSP_PRESENT) {
  406. if (cmd->flags & MMC_RSP_136) {
  407. /* response type 2 */
  408. cmd->resp[3] =
  409. OMAP_MMC_READ(host, RSP0) |
  410. (OMAP_MMC_READ(host, RSP1) << 16);
  411. cmd->resp[2] =
  412. OMAP_MMC_READ(host, RSP2) |
  413. (OMAP_MMC_READ(host, RSP3) << 16);
  414. cmd->resp[1] =
  415. OMAP_MMC_READ(host, RSP4) |
  416. (OMAP_MMC_READ(host, RSP5) << 16);
  417. cmd->resp[0] =
  418. OMAP_MMC_READ(host, RSP6) |
  419. (OMAP_MMC_READ(host, RSP7) << 16);
  420. } else {
  421. /* response types 1, 1b, 3, 4, 5, 6 */
  422. cmd->resp[0] =
  423. OMAP_MMC_READ(host, RSP6) |
  424. (OMAP_MMC_READ(host, RSP7) << 16);
  425. }
  426. }
  427. if (host->data == NULL || cmd->error) {
  428. struct mmc_host *mmc;
  429. if (host->data != NULL)
  430. mmc_omap_abort_xfer(host, host->data);
  431. host->mrq = NULL;
  432. mmc = host->mmc;
  433. mmc_omap_release_slot(host->current_slot);
  434. mmc_request_done(mmc, cmd->mrq);
  435. }
  436. }
  437. /*
  438. * Abort stuck command. Can occur when card is removed while it is being
  439. * read.
  440. */
  441. static void mmc_omap_abort_command(struct work_struct *work)
  442. {
  443. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  444. cmd_abort_work);
  445. BUG_ON(!host->cmd);
  446. dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
  447. host->cmd->opcode);
  448. if (host->cmd->error == 0)
  449. host->cmd->error = -ETIMEDOUT;
  450. if (host->data == NULL) {
  451. struct mmc_command *cmd;
  452. struct mmc_host *mmc;
  453. cmd = host->cmd;
  454. host->cmd = NULL;
  455. mmc_omap_send_abort(host, 10000);
  456. host->mrq = NULL;
  457. mmc = host->mmc;
  458. mmc_omap_release_slot(host->current_slot);
  459. mmc_request_done(mmc, cmd->mrq);
  460. } else
  461. mmc_omap_cmd_done(host, host->cmd);
  462. host->abort = 0;
  463. enable_irq(host->irq);
  464. }
  465. static void
  466. mmc_omap_cmd_timer(unsigned long data)
  467. {
  468. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  469. unsigned long flags;
  470. spin_lock_irqsave(&host->slot_lock, flags);
  471. if (host->cmd != NULL && !host->abort) {
  472. OMAP_MMC_WRITE(host, IE, 0);
  473. disable_irq(host->irq);
  474. host->abort = 1;
  475. schedule_work(&host->cmd_abort_work);
  476. }
  477. spin_unlock_irqrestore(&host->slot_lock, flags);
  478. }
  479. /* PIO only */
  480. static void
  481. mmc_omap_sg_to_buf(struct mmc_omap_host *host)
  482. {
  483. struct scatterlist *sg;
  484. sg = host->data->sg + host->sg_idx;
  485. host->buffer_bytes_left = sg->length;
  486. host->buffer = sg_virt(sg);
  487. if (host->buffer_bytes_left > host->total_bytes_left)
  488. host->buffer_bytes_left = host->total_bytes_left;
  489. }
  490. /* PIO only */
  491. static void
  492. mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
  493. {
  494. int n;
  495. if (host->buffer_bytes_left == 0) {
  496. host->sg_idx++;
  497. BUG_ON(host->sg_idx == host->sg_len);
  498. mmc_omap_sg_to_buf(host);
  499. }
  500. n = 64;
  501. if (n > host->buffer_bytes_left)
  502. n = host->buffer_bytes_left;
  503. host->buffer_bytes_left -= n;
  504. host->total_bytes_left -= n;
  505. host->data->bytes_xfered += n;
  506. if (write) {
  507. __raw_writesw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
  508. } else {
  509. __raw_readsw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
  510. }
  511. }
  512. static inline void mmc_omap_report_irq(u16 status)
  513. {
  514. static const char *mmc_omap_status_bits[] = {
  515. "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
  516. "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
  517. };
  518. int i, c = 0;
  519. for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
  520. if (status & (1 << i)) {
  521. if (c)
  522. printk(" ");
  523. printk("%s", mmc_omap_status_bits[i]);
  524. c++;
  525. }
  526. }
  527. static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
  528. {
  529. struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
  530. u16 status;
  531. int end_command;
  532. int end_transfer;
  533. int transfer_error, cmd_error;
  534. if (host->cmd == NULL && host->data == NULL) {
  535. status = OMAP_MMC_READ(host, STAT);
  536. dev_info(mmc_dev(host->slots[0]->mmc),
  537. "Spurious IRQ 0x%04x\n", status);
  538. if (status != 0) {
  539. OMAP_MMC_WRITE(host, STAT, status);
  540. OMAP_MMC_WRITE(host, IE, 0);
  541. }
  542. return IRQ_HANDLED;
  543. }
  544. end_command = 0;
  545. end_transfer = 0;
  546. transfer_error = 0;
  547. cmd_error = 0;
  548. while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
  549. int cmd;
  550. OMAP_MMC_WRITE(host, STAT, status);
  551. if (host->cmd != NULL)
  552. cmd = host->cmd->opcode;
  553. else
  554. cmd = -1;
  555. #ifdef CONFIG_MMC_DEBUG
  556. dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
  557. status, cmd);
  558. mmc_omap_report_irq(status);
  559. printk("\n");
  560. #endif
  561. if (host->total_bytes_left) {
  562. if ((status & OMAP_MMC_STAT_A_FULL) ||
  563. (status & OMAP_MMC_STAT_END_OF_DATA))
  564. mmc_omap_xfer_data(host, 0);
  565. if (status & OMAP_MMC_STAT_A_EMPTY)
  566. mmc_omap_xfer_data(host, 1);
  567. }
  568. if (status & OMAP_MMC_STAT_END_OF_DATA)
  569. end_transfer = 1;
  570. if (status & OMAP_MMC_STAT_DATA_TOUT) {
  571. dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
  572. cmd);
  573. if (host->data) {
  574. host->data->error = -ETIMEDOUT;
  575. transfer_error = 1;
  576. }
  577. }
  578. if (status & OMAP_MMC_STAT_DATA_CRC) {
  579. if (host->data) {
  580. host->data->error = -EILSEQ;
  581. dev_dbg(mmc_dev(host->mmc),
  582. "data CRC error, bytes left %d\n",
  583. host->total_bytes_left);
  584. transfer_error = 1;
  585. } else {
  586. dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
  587. }
  588. }
  589. if (status & OMAP_MMC_STAT_CMD_TOUT) {
  590. /* Timeouts are routine with some commands */
  591. if (host->cmd) {
  592. struct mmc_omap_slot *slot =
  593. host->current_slot;
  594. if (slot == NULL ||
  595. !mmc_omap_cover_is_open(slot))
  596. dev_err(mmc_dev(host->mmc),
  597. "command timeout (CMD%d)\n",
  598. cmd);
  599. host->cmd->error = -ETIMEDOUT;
  600. end_command = 1;
  601. cmd_error = 1;
  602. }
  603. }
  604. if (status & OMAP_MMC_STAT_CMD_CRC) {
  605. if (host->cmd) {
  606. dev_err(mmc_dev(host->mmc),
  607. "command CRC error (CMD%d, arg 0x%08x)\n",
  608. cmd, host->cmd->arg);
  609. host->cmd->error = -EILSEQ;
  610. end_command = 1;
  611. cmd_error = 1;
  612. } else
  613. dev_err(mmc_dev(host->mmc),
  614. "command CRC error without cmd?\n");
  615. }
  616. if (status & OMAP_MMC_STAT_CARD_ERR) {
  617. dev_dbg(mmc_dev(host->mmc),
  618. "ignoring card status error (CMD%d)\n",
  619. cmd);
  620. end_command = 1;
  621. }
  622. /*
  623. * NOTE: On 1610 the END_OF_CMD may come too early when
  624. * starting a write
  625. */
  626. if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
  627. (!(status & OMAP_MMC_STAT_A_EMPTY))) {
  628. end_command = 1;
  629. }
  630. }
  631. if (cmd_error && host->data) {
  632. del_timer(&host->cmd_abort_timer);
  633. host->abort = 1;
  634. OMAP_MMC_WRITE(host, IE, 0);
  635. disable_irq(host->irq);
  636. schedule_work(&host->cmd_abort_work);
  637. return IRQ_HANDLED;
  638. }
  639. if (end_command)
  640. mmc_omap_cmd_done(host, host->cmd);
  641. if (host->data != NULL) {
  642. if (transfer_error)
  643. mmc_omap_xfer_done(host, host->data);
  644. else if (end_transfer)
  645. mmc_omap_end_of_data(host, host->data);
  646. }
  647. return IRQ_HANDLED;
  648. }
  649. void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
  650. {
  651. int cover_open;
  652. struct mmc_omap_host *host = dev_get_drvdata(dev);
  653. struct mmc_omap_slot *slot = host->slots[num];
  654. BUG_ON(num >= host->nr_slots);
  655. /* Other subsystems can call in here before we're initialised. */
  656. if (host->nr_slots == 0 || !host->slots[num])
  657. return;
  658. cover_open = mmc_omap_cover_is_open(slot);
  659. if (cover_open != slot->cover_open) {
  660. slot->cover_open = cover_open;
  661. sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
  662. }
  663. tasklet_hi_schedule(&slot->cover_tasklet);
  664. }
  665. static void mmc_omap_cover_timer(unsigned long arg)
  666. {
  667. struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
  668. tasklet_schedule(&slot->cover_tasklet);
  669. }
  670. static void mmc_omap_cover_handler(unsigned long param)
  671. {
  672. struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
  673. int cover_open = mmc_omap_cover_is_open(slot);
  674. mmc_detect_change(slot->mmc, 0);
  675. if (!cover_open)
  676. return;
  677. /*
  678. * If no card is inserted, we postpone polling until
  679. * the cover has been closed.
  680. */
  681. if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
  682. return;
  683. mod_timer(&slot->cover_timer,
  684. jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
  685. }
  686. /* Prepare to transfer the next segment of a scatterlist */
  687. static void
  688. mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
  689. {
  690. int dma_ch = host->dma_ch;
  691. unsigned long data_addr;
  692. u16 buf, frame;
  693. u32 count;
  694. struct scatterlist *sg = &data->sg[host->sg_idx];
  695. int src_port = 0;
  696. int dst_port = 0;
  697. int sync_dev = 0;
  698. data_addr = host->phys_base + OMAP_MMC_REG_DATA;
  699. frame = data->blksz;
  700. count = sg_dma_len(sg);
  701. if ((data->blocks == 1) && (count > data->blksz))
  702. count = frame;
  703. host->dma_len = count;
  704. /* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
  705. * Use 16 or 32 word frames when the blocksize is at least that large.
  706. * Blocksize is usually 512 bytes; but not for some SD reads.
  707. */
  708. if (cpu_is_omap15xx() && frame > 32)
  709. frame = 32;
  710. else if (frame > 64)
  711. frame = 64;
  712. count /= frame;
  713. frame >>= 1;
  714. if (!(data->flags & MMC_DATA_WRITE)) {
  715. buf = 0x800f | ((frame - 1) << 8);
  716. if (cpu_class_is_omap1()) {
  717. src_port = OMAP_DMA_PORT_TIPB;
  718. dst_port = OMAP_DMA_PORT_EMIFF;
  719. }
  720. if (cpu_is_omap24xx())
  721. sync_dev = OMAP24XX_DMA_MMC1_RX;
  722. omap_set_dma_src_params(dma_ch, src_port,
  723. OMAP_DMA_AMODE_CONSTANT,
  724. data_addr, 0, 0);
  725. omap_set_dma_dest_params(dma_ch, dst_port,
  726. OMAP_DMA_AMODE_POST_INC,
  727. sg_dma_address(sg), 0, 0);
  728. omap_set_dma_dest_data_pack(dma_ch, 1);
  729. omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
  730. } else {
  731. buf = 0x0f80 | ((frame - 1) << 0);
  732. if (cpu_class_is_omap1()) {
  733. src_port = OMAP_DMA_PORT_EMIFF;
  734. dst_port = OMAP_DMA_PORT_TIPB;
  735. }
  736. if (cpu_is_omap24xx())
  737. sync_dev = OMAP24XX_DMA_MMC1_TX;
  738. omap_set_dma_dest_params(dma_ch, dst_port,
  739. OMAP_DMA_AMODE_CONSTANT,
  740. data_addr, 0, 0);
  741. omap_set_dma_src_params(dma_ch, src_port,
  742. OMAP_DMA_AMODE_POST_INC,
  743. sg_dma_address(sg), 0, 0);
  744. omap_set_dma_src_data_pack(dma_ch, 1);
  745. omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
  746. }
  747. /* Max limit for DMA frame count is 0xffff */
  748. BUG_ON(count > 0xffff);
  749. OMAP_MMC_WRITE(host, BUF, buf);
  750. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
  751. frame, count, OMAP_DMA_SYNC_FRAME,
  752. sync_dev, 0);
  753. }
  754. /* A scatterlist segment completed */
  755. static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
  756. {
  757. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  758. struct mmc_data *mmcdat = host->data;
  759. if (unlikely(host->dma_ch < 0)) {
  760. dev_err(mmc_dev(host->mmc),
  761. "DMA callback while DMA not enabled\n");
  762. return;
  763. }
  764. /* FIXME: We really should do something to _handle_ the errors */
  765. if (ch_status & OMAP1_DMA_TOUT_IRQ) {
  766. dev_err(mmc_dev(host->mmc),"DMA timeout\n");
  767. return;
  768. }
  769. if (ch_status & OMAP_DMA_DROP_IRQ) {
  770. dev_err(mmc_dev(host->mmc), "DMA sync error\n");
  771. return;
  772. }
  773. if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
  774. return;
  775. }
  776. mmcdat->bytes_xfered += host->dma_len;
  777. host->sg_idx++;
  778. if (host->sg_idx < host->sg_len) {
  779. mmc_omap_prepare_dma(host, host->data);
  780. omap_start_dma(host->dma_ch);
  781. } else
  782. mmc_omap_dma_done(host, host->data);
  783. }
  784. static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
  785. {
  786. const char *dev_name;
  787. int sync_dev, dma_ch, is_read, r;
  788. is_read = !(data->flags & MMC_DATA_WRITE);
  789. del_timer_sync(&host->dma_timer);
  790. if (host->dma_ch >= 0) {
  791. if (is_read == host->dma_is_read)
  792. return 0;
  793. omap_free_dma(host->dma_ch);
  794. host->dma_ch = -1;
  795. }
  796. if (is_read) {
  797. if (host->id == 1) {
  798. sync_dev = OMAP_DMA_MMC_RX;
  799. dev_name = "MMC1 read";
  800. } else {
  801. sync_dev = OMAP_DMA_MMC2_RX;
  802. dev_name = "MMC2 read";
  803. }
  804. } else {
  805. if (host->id == 1) {
  806. sync_dev = OMAP_DMA_MMC_TX;
  807. dev_name = "MMC1 write";
  808. } else {
  809. sync_dev = OMAP_DMA_MMC2_TX;
  810. dev_name = "MMC2 write";
  811. }
  812. }
  813. r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
  814. host, &dma_ch);
  815. if (r != 0) {
  816. dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
  817. return r;
  818. }
  819. host->dma_ch = dma_ch;
  820. host->dma_is_read = is_read;
  821. return 0;
  822. }
  823. static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  824. {
  825. u16 reg;
  826. reg = OMAP_MMC_READ(host, SDIO);
  827. reg &= ~(1 << 5);
  828. OMAP_MMC_WRITE(host, SDIO, reg);
  829. /* Set maximum timeout */
  830. OMAP_MMC_WRITE(host, CTO, 0xff);
  831. }
  832. static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  833. {
  834. unsigned int timeout, cycle_ns;
  835. u16 reg;
  836. cycle_ns = 1000000000 / host->current_slot->fclk_freq;
  837. timeout = req->data->timeout_ns / cycle_ns;
  838. timeout += req->data->timeout_clks;
  839. /* Check if we need to use timeout multiplier register */
  840. reg = OMAP_MMC_READ(host, SDIO);
  841. if (timeout > 0xffff) {
  842. reg |= (1 << 5);
  843. timeout /= 1024;
  844. } else
  845. reg &= ~(1 << 5);
  846. OMAP_MMC_WRITE(host, SDIO, reg);
  847. OMAP_MMC_WRITE(host, DTO, timeout);
  848. }
  849. static void
  850. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  851. {
  852. struct mmc_data *data = req->data;
  853. int i, use_dma, block_size;
  854. unsigned sg_len;
  855. host->data = data;
  856. if (data == NULL) {
  857. OMAP_MMC_WRITE(host, BLEN, 0);
  858. OMAP_MMC_WRITE(host, NBLK, 0);
  859. OMAP_MMC_WRITE(host, BUF, 0);
  860. host->dma_in_use = 0;
  861. set_cmd_timeout(host, req);
  862. return;
  863. }
  864. block_size = data->blksz;
  865. OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
  866. OMAP_MMC_WRITE(host, BLEN, block_size - 1);
  867. set_data_timeout(host, req);
  868. /* cope with calling layer confusion; it issues "single
  869. * block" writes using multi-block scatterlists.
  870. */
  871. sg_len = (data->blocks == 1) ? 1 : data->sg_len;
  872. /* Only do DMA for entire blocks */
  873. use_dma = host->use_dma;
  874. if (use_dma) {
  875. for (i = 0; i < sg_len; i++) {
  876. if ((data->sg[i].length % block_size) != 0) {
  877. use_dma = 0;
  878. break;
  879. }
  880. }
  881. }
  882. host->sg_idx = 0;
  883. if (use_dma) {
  884. if (mmc_omap_get_dma_channel(host, data) == 0) {
  885. enum dma_data_direction dma_data_dir;
  886. if (data->flags & MMC_DATA_WRITE)
  887. dma_data_dir = DMA_TO_DEVICE;
  888. else
  889. dma_data_dir = DMA_FROM_DEVICE;
  890. host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  891. sg_len, dma_data_dir);
  892. host->total_bytes_left = 0;
  893. mmc_omap_prepare_dma(host, req->data);
  894. host->brs_received = 0;
  895. host->dma_done = 0;
  896. host->dma_in_use = 1;
  897. } else
  898. use_dma = 0;
  899. }
  900. /* Revert to PIO? */
  901. if (!use_dma) {
  902. OMAP_MMC_WRITE(host, BUF, 0x1f1f);
  903. host->total_bytes_left = data->blocks * block_size;
  904. host->sg_len = sg_len;
  905. mmc_omap_sg_to_buf(host);
  906. host->dma_in_use = 0;
  907. }
  908. }
  909. static void mmc_omap_start_request(struct mmc_omap_host *host,
  910. struct mmc_request *req)
  911. {
  912. BUG_ON(host->mrq != NULL);
  913. host->mrq = req;
  914. /* only touch fifo AFTER the controller readies it */
  915. mmc_omap_prepare_data(host, req);
  916. mmc_omap_start_command(host, req->cmd);
  917. if (host->dma_in_use)
  918. omap_start_dma(host->dma_ch);
  919. BUG_ON(irqs_disabled());
  920. }
  921. static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
  922. {
  923. struct mmc_omap_slot *slot = mmc_priv(mmc);
  924. struct mmc_omap_host *host = slot->host;
  925. unsigned long flags;
  926. spin_lock_irqsave(&host->slot_lock, flags);
  927. if (host->mmc != NULL) {
  928. BUG_ON(slot->mrq != NULL);
  929. slot->mrq = req;
  930. spin_unlock_irqrestore(&host->slot_lock, flags);
  931. return;
  932. } else
  933. host->mmc = mmc;
  934. spin_unlock_irqrestore(&host->slot_lock, flags);
  935. mmc_omap_select_slot(slot, 1);
  936. mmc_omap_start_request(host, req);
  937. }
  938. static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
  939. int vdd)
  940. {
  941. struct mmc_omap_host *host;
  942. host = slot->host;
  943. if (slot->pdata->set_power != NULL)
  944. slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
  945. vdd);
  946. if (cpu_is_omap24xx()) {
  947. u16 w;
  948. if (power_on) {
  949. w = OMAP_MMC_READ(host, CON);
  950. OMAP_MMC_WRITE(host, CON, w | (1 << 11));
  951. } else {
  952. w = OMAP_MMC_READ(host, CON);
  953. OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
  954. }
  955. }
  956. }
  957. static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
  958. {
  959. struct mmc_omap_slot *slot = mmc_priv(mmc);
  960. struct mmc_omap_host *host = slot->host;
  961. int func_clk_rate = clk_get_rate(host->fclk);
  962. int dsor;
  963. if (ios->clock == 0)
  964. return 0;
  965. dsor = func_clk_rate / ios->clock;
  966. if (dsor < 1)
  967. dsor = 1;
  968. if (func_clk_rate / dsor > ios->clock)
  969. dsor++;
  970. if (dsor > 250)
  971. dsor = 250;
  972. slot->fclk_freq = func_clk_rate / dsor;
  973. if (ios->bus_width == MMC_BUS_WIDTH_4)
  974. dsor |= 1 << 15;
  975. return dsor;
  976. }
  977. static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  978. {
  979. struct mmc_omap_slot *slot = mmc_priv(mmc);
  980. struct mmc_omap_host *host = slot->host;
  981. int i, dsor;
  982. dsor = mmc_omap_calc_divisor(mmc, ios);
  983. mmc_omap_select_slot(slot, 0);
  984. if (ios->vdd != slot->vdd)
  985. slot->vdd = ios->vdd;
  986. switch (ios->power_mode) {
  987. case MMC_POWER_OFF:
  988. mmc_omap_set_power(slot, 0, ios->vdd);
  989. break;
  990. case MMC_POWER_UP:
  991. /* Cannot touch dsor yet, just power up MMC */
  992. mmc_omap_set_power(slot, 1, ios->vdd);
  993. goto exit;
  994. case MMC_POWER_ON:
  995. dsor |= 1 << 11;
  996. break;
  997. }
  998. if (slot->bus_mode != ios->bus_mode) {
  999. if (slot->pdata->set_bus_mode != NULL)
  1000. slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
  1001. ios->bus_mode);
  1002. slot->bus_mode = ios->bus_mode;
  1003. }
  1004. /* On insanely high arm_per frequencies something sometimes
  1005. * goes somehow out of sync, and the POW bit is not being set,
  1006. * which results in the while loop below getting stuck.
  1007. * Writing to the CON register twice seems to do the trick. */
  1008. for (i = 0; i < 2; i++)
  1009. OMAP_MMC_WRITE(host, CON, dsor);
  1010. slot->saved_con = dsor;
  1011. if (ios->power_mode == MMC_POWER_ON) {
  1012. /* Send clock cycles, poll completion */
  1013. OMAP_MMC_WRITE(host, IE, 0);
  1014. OMAP_MMC_WRITE(host, STAT, 0xffff);
  1015. OMAP_MMC_WRITE(host, CMD, 1 << 7);
  1016. while ((OMAP_MMC_READ(host, STAT) & 1) == 0);
  1017. OMAP_MMC_WRITE(host, STAT, 1);
  1018. }
  1019. exit:
  1020. mmc_omap_release_slot(slot);
  1021. }
  1022. static const struct mmc_host_ops mmc_omap_ops = {
  1023. .request = mmc_omap_request,
  1024. .set_ios = mmc_omap_set_ios,
  1025. };
  1026. static int __init mmc_omap_new_slot(struct mmc_omap_host *host, int id)
  1027. {
  1028. struct mmc_omap_slot *slot = NULL;
  1029. struct mmc_host *mmc;
  1030. int r;
  1031. mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
  1032. if (mmc == NULL)
  1033. return -ENOMEM;
  1034. slot = mmc_priv(mmc);
  1035. slot->host = host;
  1036. slot->mmc = mmc;
  1037. slot->id = id;
  1038. slot->pdata = &host->pdata->slots[id];
  1039. host->slots[id] = slot;
  1040. mmc->caps = MMC_CAP_MULTIWRITE;
  1041. if (host->pdata->conf.wire4)
  1042. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1043. mmc->ops = &mmc_omap_ops;
  1044. mmc->f_min = 400000;
  1045. if (cpu_class_is_omap2())
  1046. mmc->f_max = 48000000;
  1047. else
  1048. mmc->f_max = 24000000;
  1049. if (host->pdata->max_freq)
  1050. mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
  1051. mmc->ocr_avail = slot->pdata->ocr_mask;
  1052. /* Use scatterlist DMA to reduce per-transfer costs.
  1053. * NOTE max_seg_size assumption that small blocks aren't
  1054. * normally used (except e.g. for reading SD registers).
  1055. */
  1056. mmc->max_phys_segs = 32;
  1057. mmc->max_hw_segs = 32;
  1058. mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
  1059. mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
  1060. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1061. mmc->max_seg_size = mmc->max_req_size;
  1062. r = mmc_add_host(mmc);
  1063. if (r < 0)
  1064. goto err_remove_host;
  1065. if (slot->pdata->name != NULL) {
  1066. r = device_create_file(&mmc->class_dev,
  1067. &dev_attr_slot_name);
  1068. if (r < 0)
  1069. goto err_remove_host;
  1070. }
  1071. if (slot->pdata->get_cover_state != NULL) {
  1072. r = device_create_file(&mmc->class_dev,
  1073. &dev_attr_cover_switch);
  1074. if (r < 0)
  1075. goto err_remove_slot_name;
  1076. setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
  1077. (unsigned long)slot);
  1078. tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
  1079. (unsigned long)slot);
  1080. tasklet_schedule(&slot->cover_tasklet);
  1081. }
  1082. return 0;
  1083. err_remove_slot_name:
  1084. if (slot->pdata->name != NULL)
  1085. device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
  1086. err_remove_host:
  1087. mmc_remove_host(mmc);
  1088. mmc_free_host(mmc);
  1089. return r;
  1090. }
  1091. static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
  1092. {
  1093. struct mmc_host *mmc = slot->mmc;
  1094. if (slot->pdata->name != NULL)
  1095. device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
  1096. if (slot->pdata->get_cover_state != NULL)
  1097. device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
  1098. tasklet_kill(&slot->cover_tasklet);
  1099. del_timer_sync(&slot->cover_timer);
  1100. flush_scheduled_work();
  1101. mmc_remove_host(mmc);
  1102. mmc_free_host(mmc);
  1103. }
  1104. static int __init mmc_omap_probe(struct platform_device *pdev)
  1105. {
  1106. struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
  1107. struct mmc_omap_host *host = NULL;
  1108. struct resource *res;
  1109. int i, ret = 0;
  1110. int irq;
  1111. if (pdata == NULL) {
  1112. dev_err(&pdev->dev, "platform data missing\n");
  1113. return -ENXIO;
  1114. }
  1115. if (pdata->nr_slots == 0) {
  1116. dev_err(&pdev->dev, "no slots\n");
  1117. return -ENXIO;
  1118. }
  1119. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1120. irq = platform_get_irq(pdev, 0);
  1121. if (res == NULL || irq < 0)
  1122. return -ENXIO;
  1123. res = request_mem_region(res->start, res->end - res->start + 1,
  1124. pdev->name);
  1125. if (res == NULL)
  1126. return -EBUSY;
  1127. host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
  1128. if (host == NULL) {
  1129. ret = -ENOMEM;
  1130. goto err_free_mem_region;
  1131. }
  1132. INIT_WORK(&host->cmd_abort_work, mmc_omap_abort_command);
  1133. setup_timer(&host->cmd_abort_timer, mmc_omap_cmd_timer,
  1134. (unsigned long) host);
  1135. spin_lock_init(&host->dma_lock);
  1136. setup_timer(&host->dma_timer, mmc_omap_dma_timer, (unsigned long) host);
  1137. spin_lock_init(&host->slot_lock);
  1138. init_waitqueue_head(&host->slot_wq);
  1139. host->pdata = pdata;
  1140. host->dev = &pdev->dev;
  1141. platform_set_drvdata(pdev, host);
  1142. host->id = pdev->id;
  1143. host->mem_res = res;
  1144. host->irq = irq;
  1145. host->use_dma = 1;
  1146. host->dma_ch = -1;
  1147. host->irq = irq;
  1148. host->phys_base = host->mem_res->start;
  1149. host->virt_base = (void __iomem *) IO_ADDRESS(host->phys_base);
  1150. if (cpu_is_omap24xx()) {
  1151. host->iclk = clk_get(&pdev->dev, "mmc_ick");
  1152. if (IS_ERR(host->iclk))
  1153. goto err_free_mmc_host;
  1154. clk_enable(host->iclk);
  1155. }
  1156. if (!cpu_is_omap24xx())
  1157. host->fclk = clk_get(&pdev->dev, "mmc_ck");
  1158. else
  1159. host->fclk = clk_get(&pdev->dev, "mmc_fck");
  1160. if (IS_ERR(host->fclk)) {
  1161. ret = PTR_ERR(host->fclk);
  1162. goto err_free_iclk;
  1163. }
  1164. ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
  1165. if (ret)
  1166. goto err_free_fclk;
  1167. if (pdata->init != NULL) {
  1168. ret = pdata->init(&pdev->dev);
  1169. if (ret < 0)
  1170. goto err_free_irq;
  1171. }
  1172. host->nr_slots = pdata->nr_slots;
  1173. for (i = 0; i < pdata->nr_slots; i++) {
  1174. ret = mmc_omap_new_slot(host, i);
  1175. if (ret < 0) {
  1176. while (--i >= 0)
  1177. mmc_omap_remove_slot(host->slots[i]);
  1178. goto err_plat_cleanup;
  1179. }
  1180. }
  1181. return 0;
  1182. err_plat_cleanup:
  1183. if (pdata->cleanup)
  1184. pdata->cleanup(&pdev->dev);
  1185. err_free_irq:
  1186. free_irq(host->irq, host);
  1187. err_free_fclk:
  1188. clk_put(host->fclk);
  1189. err_free_iclk:
  1190. if (host->iclk != NULL) {
  1191. clk_disable(host->iclk);
  1192. clk_put(host->iclk);
  1193. }
  1194. err_free_mmc_host:
  1195. kfree(host);
  1196. err_free_mem_region:
  1197. release_mem_region(res->start, res->end - res->start + 1);
  1198. return ret;
  1199. }
  1200. static int mmc_omap_remove(struct platform_device *pdev)
  1201. {
  1202. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1203. int i;
  1204. platform_set_drvdata(pdev, NULL);
  1205. BUG_ON(host == NULL);
  1206. for (i = 0; i < host->nr_slots; i++)
  1207. mmc_omap_remove_slot(host->slots[i]);
  1208. if (host->pdata->cleanup)
  1209. host->pdata->cleanup(&pdev->dev);
  1210. if (host->iclk && !IS_ERR(host->iclk))
  1211. clk_put(host->iclk);
  1212. if (host->fclk && !IS_ERR(host->fclk))
  1213. clk_put(host->fclk);
  1214. release_mem_region(pdev->resource[0].start,
  1215. pdev->resource[0].end - pdev->resource[0].start + 1);
  1216. kfree(host);
  1217. return 0;
  1218. }
  1219. #ifdef CONFIG_PM
  1220. static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
  1221. {
  1222. int i, ret = 0;
  1223. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1224. if (host == NULL || host->suspended)
  1225. return 0;
  1226. for (i = 0; i < host->nr_slots; i++) {
  1227. struct mmc_omap_slot *slot;
  1228. slot = host->slots[i];
  1229. ret = mmc_suspend_host(slot->mmc, mesg);
  1230. if (ret < 0) {
  1231. while (--i >= 0) {
  1232. slot = host->slots[i];
  1233. mmc_resume_host(slot->mmc);
  1234. }
  1235. return ret;
  1236. }
  1237. }
  1238. host->suspended = 1;
  1239. return 0;
  1240. }
  1241. static int mmc_omap_resume(struct platform_device *pdev)
  1242. {
  1243. int i, ret = 0;
  1244. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1245. if (host == NULL || !host->suspended)
  1246. return 0;
  1247. for (i = 0; i < host->nr_slots; i++) {
  1248. struct mmc_omap_slot *slot;
  1249. slot = host->slots[i];
  1250. ret = mmc_resume_host(slot->mmc);
  1251. if (ret < 0)
  1252. return ret;
  1253. host->suspended = 0;
  1254. }
  1255. return 0;
  1256. }
  1257. #else
  1258. #define mmc_omap_suspend NULL
  1259. #define mmc_omap_resume NULL
  1260. #endif
  1261. static struct platform_driver mmc_omap_driver = {
  1262. .probe = mmc_omap_probe,
  1263. .remove = mmc_omap_remove,
  1264. .suspend = mmc_omap_suspend,
  1265. .resume = mmc_omap_resume,
  1266. .driver = {
  1267. .name = DRIVER_NAME,
  1268. .owner = THIS_MODULE,
  1269. },
  1270. };
  1271. static int __init mmc_omap_init(void)
  1272. {
  1273. return platform_driver_register(&mmc_omap_driver);
  1274. }
  1275. static void __exit mmc_omap_exit(void)
  1276. {
  1277. platform_driver_unregister(&mmc_omap_driver);
  1278. }
  1279. module_init(mmc_omap_init);
  1280. module_exit(mmc_omap_exit);
  1281. MODULE_DESCRIPTION("OMAP Multimedia Card driver");
  1282. MODULE_LICENSE("GPL");
  1283. MODULE_ALIAS("platform:" DRIVER_NAME);
  1284. MODULE_AUTHOR("Juha Yrjölä");