omap.c 39 KB

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