omap.c 39 KB

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