omap.c 37 KB

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