omap.c 38 KB

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