omap.c 34 KB

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