omap.c 39 KB

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