omap.c 38 KB

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