omap.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. /*
  2. * linux/drivers/media/mmc/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/protocol.h>
  25. #include <linux/mmc/card.h>
  26. #include <linux/clk.h>
  27. #include <asm/io.h>
  28. #include <asm/irq.h>
  29. #include <asm/scatterlist.h>
  30. #include <asm/mach-types.h>
  31. #include <asm/arch/board.h>
  32. #include <asm/arch/gpio.h>
  33. #include <asm/arch/dma.h>
  34. #include <asm/arch/mux.h>
  35. #include <asm/arch/fpga.h>
  36. #include <asm/arch/tps65010.h>
  37. #include "omap.h"
  38. #define DRIVER_NAME "mmci-omap"
  39. #define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE))
  40. /* Specifies how often in millisecs to poll for card status changes
  41. * when the cover switch is open */
  42. #define OMAP_MMC_SWITCH_POLL_DELAY 500
  43. static int mmc_omap_enable_poll = 1;
  44. struct mmc_omap_host {
  45. int initialized;
  46. int suspended;
  47. struct mmc_request * mrq;
  48. struct mmc_command * cmd;
  49. struct mmc_data * data;
  50. struct mmc_host * mmc;
  51. struct device * dev;
  52. unsigned char id; /* 16xx chips have 2 MMC blocks */
  53. struct clk * iclk;
  54. struct clk * fclk;
  55. struct resource *res;
  56. void __iomem *base;
  57. int irq;
  58. unsigned char bus_mode;
  59. unsigned char hw_bus_mode;
  60. unsigned int sg_len;
  61. int sg_idx;
  62. u16 * buffer;
  63. u32 buffer_bytes_left;
  64. u32 total_bytes_left;
  65. unsigned use_dma:1;
  66. unsigned brs_received:1, dma_done:1;
  67. unsigned dma_is_read:1;
  68. unsigned dma_in_use:1;
  69. int dma_ch;
  70. spinlock_t dma_lock;
  71. struct timer_list dma_timer;
  72. unsigned dma_len;
  73. short power_pin;
  74. short wp_pin;
  75. int switch_pin;
  76. struct work_struct switch_work;
  77. struct timer_list switch_timer;
  78. int switch_last_state;
  79. };
  80. static inline int
  81. mmc_omap_cover_is_open(struct mmc_omap_host *host)
  82. {
  83. if (host->switch_pin < 0)
  84. return 0;
  85. return omap_get_gpio_datain(host->switch_pin);
  86. }
  87. static ssize_t
  88. mmc_omap_show_cover_switch(struct device *dev,
  89. struct device_attribute *attr, char *buf)
  90. {
  91. struct mmc_omap_host *host = dev_get_drvdata(dev);
  92. return sprintf(buf, "%s\n", mmc_omap_cover_is_open(host) ? "open" :
  93. "closed");
  94. }
  95. static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
  96. static ssize_t
  97. mmc_omap_show_enable_poll(struct device *dev,
  98. struct device_attribute *attr, char *buf)
  99. {
  100. return snprintf(buf, PAGE_SIZE, "%d\n", mmc_omap_enable_poll);
  101. }
  102. static ssize_t
  103. mmc_omap_store_enable_poll(struct device *dev,
  104. struct device_attribute *attr, const char *buf,
  105. size_t size)
  106. {
  107. int enable_poll;
  108. if (sscanf(buf, "%10d", &enable_poll) != 1)
  109. return -EINVAL;
  110. if (enable_poll != mmc_omap_enable_poll) {
  111. struct mmc_omap_host *host = dev_get_drvdata(dev);
  112. mmc_omap_enable_poll = enable_poll;
  113. if (enable_poll && host->switch_pin >= 0)
  114. schedule_work(&host->switch_work);
  115. }
  116. return size;
  117. }
  118. static DEVICE_ATTR(enable_poll, 0664,
  119. mmc_omap_show_enable_poll, mmc_omap_store_enable_poll);
  120. static void
  121. mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
  122. {
  123. u32 cmdreg;
  124. u32 resptype;
  125. u32 cmdtype;
  126. host->cmd = cmd;
  127. resptype = 0;
  128. cmdtype = 0;
  129. /* Our hardware needs to know exact type */
  130. switch (RSP_TYPE(mmc_resp_type(cmd))) {
  131. case RSP_TYPE(MMC_RSP_R1):
  132. /* resp 1, resp 1b */
  133. resptype = 1;
  134. break;
  135. case RSP_TYPE(MMC_RSP_R2):
  136. resptype = 2;
  137. break;
  138. case RSP_TYPE(MMC_RSP_R3):
  139. resptype = 3;
  140. break;
  141. default:
  142. break;
  143. }
  144. if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
  145. cmdtype = OMAP_MMC_CMDTYPE_ADTC;
  146. } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
  147. cmdtype = OMAP_MMC_CMDTYPE_BC;
  148. } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
  149. cmdtype = OMAP_MMC_CMDTYPE_BCR;
  150. } else {
  151. cmdtype = OMAP_MMC_CMDTYPE_AC;
  152. }
  153. cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
  154. if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
  155. cmdreg |= 1 << 6;
  156. if (cmd->flags & MMC_RSP_BUSY)
  157. cmdreg |= 1 << 11;
  158. if (host->data && !(host->data->flags & MMC_DATA_WRITE))
  159. cmdreg |= 1 << 15;
  160. clk_enable(host->fclk);
  161. OMAP_MMC_WRITE(host->base, CTO, 200);
  162. OMAP_MMC_WRITE(host->base, ARGL, cmd->arg & 0xffff);
  163. OMAP_MMC_WRITE(host->base, ARGH, cmd->arg >> 16);
  164. OMAP_MMC_WRITE(host->base, IE,
  165. OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
  166. OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
  167. OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
  168. OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
  169. OMAP_MMC_STAT_END_OF_DATA);
  170. OMAP_MMC_WRITE(host->base, CMD, cmdreg);
  171. }
  172. static void
  173. mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
  174. {
  175. if (host->dma_in_use) {
  176. enum dma_data_direction dma_data_dir;
  177. BUG_ON(host->dma_ch < 0);
  178. if (data->error != MMC_ERR_NONE)
  179. omap_stop_dma(host->dma_ch);
  180. /* Release DMA channel lazily */
  181. mod_timer(&host->dma_timer, jiffies + HZ);
  182. if (data->flags & MMC_DATA_WRITE)
  183. dma_data_dir = DMA_TO_DEVICE;
  184. else
  185. dma_data_dir = DMA_FROM_DEVICE;
  186. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
  187. dma_data_dir);
  188. }
  189. host->data = NULL;
  190. host->sg_len = 0;
  191. clk_disable(host->fclk);
  192. /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
  193. * dozens of requests until the card finishes writing data.
  194. * It'd be cheaper to just wait till an EOFB interrupt arrives...
  195. */
  196. if (!data->stop) {
  197. host->mrq = NULL;
  198. mmc_request_done(host->mmc, data->mrq);
  199. return;
  200. }
  201. mmc_omap_start_command(host, data->stop);
  202. }
  203. static void
  204. mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
  205. {
  206. unsigned long flags;
  207. int done;
  208. if (!host->dma_in_use) {
  209. mmc_omap_xfer_done(host, data);
  210. return;
  211. }
  212. done = 0;
  213. spin_lock_irqsave(&host->dma_lock, flags);
  214. if (host->dma_done)
  215. done = 1;
  216. else
  217. host->brs_received = 1;
  218. spin_unlock_irqrestore(&host->dma_lock, flags);
  219. if (done)
  220. mmc_omap_xfer_done(host, data);
  221. }
  222. static void
  223. mmc_omap_dma_timer(unsigned long data)
  224. {
  225. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  226. BUG_ON(host->dma_ch < 0);
  227. omap_free_dma(host->dma_ch);
  228. host->dma_ch = -1;
  229. }
  230. static void
  231. mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
  232. {
  233. unsigned long flags;
  234. int done;
  235. done = 0;
  236. spin_lock_irqsave(&host->dma_lock, flags);
  237. if (host->brs_received)
  238. done = 1;
  239. else
  240. host->dma_done = 1;
  241. spin_unlock_irqrestore(&host->dma_lock, flags);
  242. if (done)
  243. mmc_omap_xfer_done(host, data);
  244. }
  245. static void
  246. mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
  247. {
  248. host->cmd = NULL;
  249. if (cmd->flags & MMC_RSP_PRESENT) {
  250. if (cmd->flags & MMC_RSP_136) {
  251. /* response type 2 */
  252. cmd->resp[3] =
  253. OMAP_MMC_READ(host->base, RSP0) |
  254. (OMAP_MMC_READ(host->base, RSP1) << 16);
  255. cmd->resp[2] =
  256. OMAP_MMC_READ(host->base, RSP2) |
  257. (OMAP_MMC_READ(host->base, RSP3) << 16);
  258. cmd->resp[1] =
  259. OMAP_MMC_READ(host->base, RSP4) |
  260. (OMAP_MMC_READ(host->base, RSP5) << 16);
  261. cmd->resp[0] =
  262. OMAP_MMC_READ(host->base, RSP6) |
  263. (OMAP_MMC_READ(host->base, RSP7) << 16);
  264. } else {
  265. /* response types 1, 1b, 3, 4, 5, 6 */
  266. cmd->resp[0] =
  267. OMAP_MMC_READ(host->base, RSP6) |
  268. (OMAP_MMC_READ(host->base, RSP7) << 16);
  269. }
  270. }
  271. if (host->data == NULL || cmd->error != MMC_ERR_NONE) {
  272. host->mrq = NULL;
  273. clk_disable(host->fclk);
  274. mmc_request_done(host->mmc, cmd->mrq);
  275. }
  276. }
  277. /* PIO only */
  278. static void
  279. mmc_omap_sg_to_buf(struct mmc_omap_host *host)
  280. {
  281. struct scatterlist *sg;
  282. sg = host->data->sg + host->sg_idx;
  283. host->buffer_bytes_left = sg->length;
  284. host->buffer = page_address(sg->page) + sg->offset;
  285. if (host->buffer_bytes_left > host->total_bytes_left)
  286. host->buffer_bytes_left = host->total_bytes_left;
  287. }
  288. /* PIO only */
  289. static void
  290. mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
  291. {
  292. int n;
  293. if (host->buffer_bytes_left == 0) {
  294. host->sg_idx++;
  295. BUG_ON(host->sg_idx == host->sg_len);
  296. mmc_omap_sg_to_buf(host);
  297. }
  298. n = 64;
  299. if (n > host->buffer_bytes_left)
  300. n = host->buffer_bytes_left;
  301. host->buffer_bytes_left -= n;
  302. host->total_bytes_left -= n;
  303. host->data->bytes_xfered += n;
  304. if (write) {
  305. __raw_writesw(host->base + OMAP_MMC_REG_DATA, host->buffer, n);
  306. } else {
  307. __raw_readsw(host->base + OMAP_MMC_REG_DATA, host->buffer, n);
  308. }
  309. }
  310. static inline void mmc_omap_report_irq(u16 status)
  311. {
  312. static const char *mmc_omap_status_bits[] = {
  313. "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
  314. "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
  315. };
  316. int i, c = 0;
  317. for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
  318. if (status & (1 << i)) {
  319. if (c)
  320. printk(" ");
  321. printk("%s", mmc_omap_status_bits[i]);
  322. c++;
  323. }
  324. }
  325. static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
  326. {
  327. struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
  328. u16 status;
  329. int end_command;
  330. int end_transfer;
  331. int transfer_error;
  332. if (host->cmd == NULL && host->data == NULL) {
  333. status = OMAP_MMC_READ(host->base, STAT);
  334. dev_info(mmc_dev(host->mmc),"spurious irq 0x%04x\n", status);
  335. if (status != 0) {
  336. OMAP_MMC_WRITE(host->base, STAT, status);
  337. OMAP_MMC_WRITE(host->base, IE, 0);
  338. }
  339. return IRQ_HANDLED;
  340. }
  341. end_command = 0;
  342. end_transfer = 0;
  343. transfer_error = 0;
  344. while ((status = OMAP_MMC_READ(host->base, STAT)) != 0) {
  345. OMAP_MMC_WRITE(host->base, STAT, status);
  346. #ifdef CONFIG_MMC_DEBUG
  347. dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
  348. status, host->cmd != NULL ? host->cmd->opcode : -1);
  349. mmc_omap_report_irq(status);
  350. printk("\n");
  351. #endif
  352. if (host->total_bytes_left) {
  353. if ((status & OMAP_MMC_STAT_A_FULL) ||
  354. (status & OMAP_MMC_STAT_END_OF_DATA))
  355. mmc_omap_xfer_data(host, 0);
  356. if (status & OMAP_MMC_STAT_A_EMPTY)
  357. mmc_omap_xfer_data(host, 1);
  358. }
  359. if (status & OMAP_MMC_STAT_END_OF_DATA) {
  360. end_transfer = 1;
  361. }
  362. if (status & OMAP_MMC_STAT_DATA_TOUT) {
  363. dev_dbg(mmc_dev(host->mmc), "data timeout\n");
  364. if (host->data) {
  365. host->data->error |= MMC_ERR_TIMEOUT;
  366. transfer_error = 1;
  367. }
  368. }
  369. if (status & OMAP_MMC_STAT_DATA_CRC) {
  370. if (host->data) {
  371. host->data->error |= MMC_ERR_BADCRC;
  372. dev_dbg(mmc_dev(host->mmc),
  373. "data CRC error, bytes left %d\n",
  374. host->total_bytes_left);
  375. transfer_error = 1;
  376. } else {
  377. dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
  378. }
  379. }
  380. if (status & OMAP_MMC_STAT_CMD_TOUT) {
  381. /* Timeouts are routine with some commands */
  382. if (host->cmd) {
  383. if (host->cmd->opcode != MMC_ALL_SEND_CID &&
  384. host->cmd->opcode !=
  385. MMC_SEND_OP_COND &&
  386. host->cmd->opcode !=
  387. MMC_APP_CMD &&
  388. !mmc_omap_cover_is_open(host))
  389. dev_err(mmc_dev(host->mmc),
  390. "command timeout, CMD %d\n",
  391. host->cmd->opcode);
  392. host->cmd->error = MMC_ERR_TIMEOUT;
  393. end_command = 1;
  394. }
  395. }
  396. if (status & OMAP_MMC_STAT_CMD_CRC) {
  397. if (host->cmd) {
  398. dev_err(mmc_dev(host->mmc),
  399. "command CRC error (CMD%d, arg 0x%08x)\n",
  400. host->cmd->opcode, host->cmd->arg);
  401. host->cmd->error = MMC_ERR_BADCRC;
  402. end_command = 1;
  403. } else
  404. dev_err(mmc_dev(host->mmc),
  405. "command CRC error without cmd?\n");
  406. }
  407. if (status & OMAP_MMC_STAT_CARD_ERR) {
  408. if (host->cmd && host->cmd->opcode == MMC_STOP_TRANSMISSION) {
  409. u32 response = OMAP_MMC_READ(host->base, RSP6)
  410. | (OMAP_MMC_READ(host->base, RSP7) << 16);
  411. /* STOP sometimes sets must-ignore bits */
  412. if (!(response & (R1_CC_ERROR
  413. | R1_ILLEGAL_COMMAND
  414. | R1_COM_CRC_ERROR))) {
  415. end_command = 1;
  416. continue;
  417. }
  418. }
  419. dev_dbg(mmc_dev(host->mmc), "card status error (CMD%d)\n",
  420. host->cmd->opcode);
  421. if (host->cmd) {
  422. host->cmd->error = MMC_ERR_FAILED;
  423. end_command = 1;
  424. }
  425. if (host->data) {
  426. host->data->error = MMC_ERR_FAILED;
  427. transfer_error = 1;
  428. }
  429. }
  430. /*
  431. * NOTE: On 1610 the END_OF_CMD may come too early when
  432. * starting a write
  433. */
  434. if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
  435. (!(status & OMAP_MMC_STAT_A_EMPTY))) {
  436. end_command = 1;
  437. }
  438. }
  439. if (end_command) {
  440. mmc_omap_cmd_done(host, host->cmd);
  441. }
  442. if (transfer_error)
  443. mmc_omap_xfer_done(host, host->data);
  444. else if (end_transfer)
  445. mmc_omap_end_of_data(host, host->data);
  446. return IRQ_HANDLED;
  447. }
  448. static irqreturn_t mmc_omap_switch_irq(int irq, void *dev_id)
  449. {
  450. struct mmc_omap_host *host = (struct mmc_omap_host *) dev_id;
  451. schedule_work(&host->switch_work);
  452. return IRQ_HANDLED;
  453. }
  454. static void mmc_omap_switch_timer(unsigned long arg)
  455. {
  456. struct mmc_omap_host *host = (struct mmc_omap_host *) arg;
  457. schedule_work(&host->switch_work);
  458. }
  459. /* FIXME: Handle card insertion and removal properly. Maybe use a mask
  460. * for MMC state? */
  461. static void mmc_omap_switch_callback(unsigned long data, u8 mmc_mask)
  462. {
  463. }
  464. static void mmc_omap_switch_handler(void *data)
  465. {
  466. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  467. struct mmc_card *card;
  468. static int complained = 0;
  469. int cards = 0, cover_open;
  470. if (host->switch_pin == -1)
  471. return;
  472. cover_open = mmc_omap_cover_is_open(host);
  473. if (cover_open != host->switch_last_state) {
  474. kobject_uevent(&host->dev->kobj, KOBJ_CHANGE);
  475. host->switch_last_state = cover_open;
  476. }
  477. mmc_detect_change(host->mmc, 0);
  478. list_for_each_entry(card, &host->mmc->cards, node) {
  479. if (mmc_card_present(card))
  480. cards++;
  481. }
  482. if (mmc_omap_cover_is_open(host)) {
  483. if (!complained) {
  484. dev_info(mmc_dev(host->mmc), "cover is open");
  485. complained = 1;
  486. }
  487. if (mmc_omap_enable_poll)
  488. mod_timer(&host->switch_timer, jiffies +
  489. msecs_to_jiffies(OMAP_MMC_SWITCH_POLL_DELAY));
  490. } else {
  491. complained = 0;
  492. }
  493. }
  494. /* Prepare to transfer the next segment of a scatterlist */
  495. static void
  496. mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
  497. {
  498. int dma_ch = host->dma_ch;
  499. unsigned long data_addr;
  500. u16 buf, frame;
  501. u32 count;
  502. struct scatterlist *sg = &data->sg[host->sg_idx];
  503. int src_port = 0;
  504. int dst_port = 0;
  505. int sync_dev = 0;
  506. data_addr = io_v2p((u32) host->base) + OMAP_MMC_REG_DATA;
  507. frame = data->blksz;
  508. count = sg_dma_len(sg);
  509. if ((data->blocks == 1) && (count > data->blksz))
  510. count = frame;
  511. host->dma_len = count;
  512. /* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
  513. * Use 16 or 32 word frames when the blocksize is at least that large.
  514. * Blocksize is usually 512 bytes; but not for some SD reads.
  515. */
  516. if (cpu_is_omap15xx() && frame > 32)
  517. frame = 32;
  518. else if (frame > 64)
  519. frame = 64;
  520. count /= frame;
  521. frame >>= 1;
  522. if (!(data->flags & MMC_DATA_WRITE)) {
  523. buf = 0x800f | ((frame - 1) << 8);
  524. if (cpu_class_is_omap1()) {
  525. src_port = OMAP_DMA_PORT_TIPB;
  526. dst_port = OMAP_DMA_PORT_EMIFF;
  527. }
  528. if (cpu_is_omap24xx())
  529. sync_dev = OMAP24XX_DMA_MMC1_RX;
  530. omap_set_dma_src_params(dma_ch, src_port,
  531. OMAP_DMA_AMODE_CONSTANT,
  532. data_addr, 0, 0);
  533. omap_set_dma_dest_params(dma_ch, dst_port,
  534. OMAP_DMA_AMODE_POST_INC,
  535. sg_dma_address(sg), 0, 0);
  536. omap_set_dma_dest_data_pack(dma_ch, 1);
  537. omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
  538. } else {
  539. buf = 0x0f80 | ((frame - 1) << 0);
  540. if (cpu_class_is_omap1()) {
  541. src_port = OMAP_DMA_PORT_EMIFF;
  542. dst_port = OMAP_DMA_PORT_TIPB;
  543. }
  544. if (cpu_is_omap24xx())
  545. sync_dev = OMAP24XX_DMA_MMC1_TX;
  546. omap_set_dma_dest_params(dma_ch, dst_port,
  547. OMAP_DMA_AMODE_CONSTANT,
  548. data_addr, 0, 0);
  549. omap_set_dma_src_params(dma_ch, src_port,
  550. OMAP_DMA_AMODE_POST_INC,
  551. sg_dma_address(sg), 0, 0);
  552. omap_set_dma_src_data_pack(dma_ch, 1);
  553. omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
  554. }
  555. /* Max limit for DMA frame count is 0xffff */
  556. BUG_ON(count > 0xffff);
  557. OMAP_MMC_WRITE(host->base, BUF, buf);
  558. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
  559. frame, count, OMAP_DMA_SYNC_FRAME,
  560. sync_dev, 0);
  561. }
  562. /* A scatterlist segment completed */
  563. static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
  564. {
  565. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  566. struct mmc_data *mmcdat = host->data;
  567. if (unlikely(host->dma_ch < 0)) {
  568. dev_err(mmc_dev(host->mmc),
  569. "DMA callback while DMA not enabled\n");
  570. return;
  571. }
  572. /* FIXME: We really should do something to _handle_ the errors */
  573. if (ch_status & OMAP1_DMA_TOUT_IRQ) {
  574. dev_err(mmc_dev(host->mmc),"DMA timeout\n");
  575. return;
  576. }
  577. if (ch_status & OMAP_DMA_DROP_IRQ) {
  578. dev_err(mmc_dev(host->mmc), "DMA sync error\n");
  579. return;
  580. }
  581. if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
  582. return;
  583. }
  584. mmcdat->bytes_xfered += host->dma_len;
  585. host->sg_idx++;
  586. if (host->sg_idx < host->sg_len) {
  587. mmc_omap_prepare_dma(host, host->data);
  588. omap_start_dma(host->dma_ch);
  589. } else
  590. mmc_omap_dma_done(host, host->data);
  591. }
  592. static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
  593. {
  594. const char *dev_name;
  595. int sync_dev, dma_ch, is_read, r;
  596. is_read = !(data->flags & MMC_DATA_WRITE);
  597. del_timer_sync(&host->dma_timer);
  598. if (host->dma_ch >= 0) {
  599. if (is_read == host->dma_is_read)
  600. return 0;
  601. omap_free_dma(host->dma_ch);
  602. host->dma_ch = -1;
  603. }
  604. if (is_read) {
  605. if (host->id == 1) {
  606. sync_dev = OMAP_DMA_MMC_RX;
  607. dev_name = "MMC1 read";
  608. } else {
  609. sync_dev = OMAP_DMA_MMC2_RX;
  610. dev_name = "MMC2 read";
  611. }
  612. } else {
  613. if (host->id == 1) {
  614. sync_dev = OMAP_DMA_MMC_TX;
  615. dev_name = "MMC1 write";
  616. } else {
  617. sync_dev = OMAP_DMA_MMC2_TX;
  618. dev_name = "MMC2 write";
  619. }
  620. }
  621. r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
  622. host, &dma_ch);
  623. if (r != 0) {
  624. dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
  625. return r;
  626. }
  627. host->dma_ch = dma_ch;
  628. host->dma_is_read = is_read;
  629. return 0;
  630. }
  631. static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  632. {
  633. u16 reg;
  634. reg = OMAP_MMC_READ(host->base, SDIO);
  635. reg &= ~(1 << 5);
  636. OMAP_MMC_WRITE(host->base, SDIO, reg);
  637. /* Set maximum timeout */
  638. OMAP_MMC_WRITE(host->base, CTO, 0xff);
  639. }
  640. static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  641. {
  642. int timeout;
  643. u16 reg;
  644. /* Convert ns to clock cycles by assuming 20MHz frequency
  645. * 1 cycle at 20MHz = 500 ns
  646. */
  647. timeout = req->data->timeout_clks + req->data->timeout_ns / 500;
  648. /* Check if we need to use timeout multiplier register */
  649. reg = OMAP_MMC_READ(host->base, SDIO);
  650. if (timeout > 0xffff) {
  651. reg |= (1 << 5);
  652. timeout /= 1024;
  653. } else
  654. reg &= ~(1 << 5);
  655. OMAP_MMC_WRITE(host->base, SDIO, reg);
  656. OMAP_MMC_WRITE(host->base, DTO, timeout);
  657. }
  658. static void
  659. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  660. {
  661. struct mmc_data *data = req->data;
  662. int i, use_dma, block_size;
  663. unsigned sg_len;
  664. host->data = data;
  665. if (data == NULL) {
  666. OMAP_MMC_WRITE(host->base, BLEN, 0);
  667. OMAP_MMC_WRITE(host->base, NBLK, 0);
  668. OMAP_MMC_WRITE(host->base, BUF, 0);
  669. host->dma_in_use = 0;
  670. set_cmd_timeout(host, req);
  671. return;
  672. }
  673. block_size = data->blksz;
  674. OMAP_MMC_WRITE(host->base, NBLK, data->blocks - 1);
  675. OMAP_MMC_WRITE(host->base, BLEN, block_size - 1);
  676. set_data_timeout(host, req);
  677. /* cope with calling layer confusion; it issues "single
  678. * block" writes using multi-block scatterlists.
  679. */
  680. sg_len = (data->blocks == 1) ? 1 : data->sg_len;
  681. /* Only do DMA for entire blocks */
  682. use_dma = host->use_dma;
  683. if (use_dma) {
  684. for (i = 0; i < sg_len; i++) {
  685. if ((data->sg[i].length % block_size) != 0) {
  686. use_dma = 0;
  687. break;
  688. }
  689. }
  690. }
  691. host->sg_idx = 0;
  692. if (use_dma) {
  693. if (mmc_omap_get_dma_channel(host, data) == 0) {
  694. enum dma_data_direction dma_data_dir;
  695. if (data->flags & MMC_DATA_WRITE)
  696. dma_data_dir = DMA_TO_DEVICE;
  697. else
  698. dma_data_dir = DMA_FROM_DEVICE;
  699. host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  700. sg_len, dma_data_dir);
  701. host->total_bytes_left = 0;
  702. mmc_omap_prepare_dma(host, req->data);
  703. host->brs_received = 0;
  704. host->dma_done = 0;
  705. host->dma_in_use = 1;
  706. } else
  707. use_dma = 0;
  708. }
  709. /* Revert to PIO? */
  710. if (!use_dma) {
  711. OMAP_MMC_WRITE(host->base, BUF, 0x1f1f);
  712. host->total_bytes_left = data->blocks * block_size;
  713. host->sg_len = sg_len;
  714. mmc_omap_sg_to_buf(host);
  715. host->dma_in_use = 0;
  716. }
  717. }
  718. static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
  719. {
  720. struct mmc_omap_host *host = mmc_priv(mmc);
  721. WARN_ON(host->mrq != NULL);
  722. host->mrq = req;
  723. /* only touch fifo AFTER the controller readies it */
  724. mmc_omap_prepare_data(host, req);
  725. mmc_omap_start_command(host, req->cmd);
  726. if (host->dma_in_use)
  727. omap_start_dma(host->dma_ch);
  728. }
  729. static void innovator_fpga_socket_power(int on)
  730. {
  731. #if defined(CONFIG_MACH_OMAP_INNOVATOR) && defined(CONFIG_ARCH_OMAP15XX)
  732. if (on) {
  733. fpga_write(fpga_read(OMAP1510_FPGA_POWER) | (1 << 3),
  734. OMAP1510_FPGA_POWER);
  735. } else {
  736. fpga_write(fpga_read(OMAP1510_FPGA_POWER) & ~(1 << 3),
  737. OMAP1510_FPGA_POWER);
  738. }
  739. #endif
  740. }
  741. /*
  742. * Turn the socket power on/off. Innovator uses FPGA, most boards
  743. * probably use GPIO.
  744. */
  745. static void mmc_omap_power(struct mmc_omap_host *host, int on)
  746. {
  747. if (on) {
  748. if (machine_is_omap_innovator())
  749. innovator_fpga_socket_power(1);
  750. else if (machine_is_omap_h2())
  751. tps65010_set_gpio_out_value(GPIO3, HIGH);
  752. else if (machine_is_omap_h3())
  753. /* GPIO 4 of TPS65010 sends SD_EN signal */
  754. tps65010_set_gpio_out_value(GPIO4, HIGH);
  755. else if (cpu_is_omap24xx()) {
  756. u16 reg = OMAP_MMC_READ(host->base, CON);
  757. OMAP_MMC_WRITE(host->base, CON, reg | (1 << 11));
  758. } else
  759. if (host->power_pin >= 0)
  760. omap_set_gpio_dataout(host->power_pin, 1);
  761. } else {
  762. if (machine_is_omap_innovator())
  763. innovator_fpga_socket_power(0);
  764. else if (machine_is_omap_h2())
  765. tps65010_set_gpio_out_value(GPIO3, LOW);
  766. else if (machine_is_omap_h3())
  767. tps65010_set_gpio_out_value(GPIO4, LOW);
  768. else if (cpu_is_omap24xx()) {
  769. u16 reg = OMAP_MMC_READ(host->base, CON);
  770. OMAP_MMC_WRITE(host->base, CON, reg & ~(1 << 11));
  771. } else
  772. if (host->power_pin >= 0)
  773. omap_set_gpio_dataout(host->power_pin, 0);
  774. }
  775. }
  776. static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  777. {
  778. struct mmc_omap_host *host = mmc_priv(mmc);
  779. int dsor;
  780. int realclock, i;
  781. realclock = ios->clock;
  782. if (ios->clock == 0)
  783. dsor = 0;
  784. else {
  785. int func_clk_rate = clk_get_rate(host->fclk);
  786. dsor = func_clk_rate / realclock;
  787. if (dsor < 1)
  788. dsor = 1;
  789. if (func_clk_rate / dsor > realclock)
  790. dsor++;
  791. if (dsor > 250)
  792. dsor = 250;
  793. dsor++;
  794. if (ios->bus_width == MMC_BUS_WIDTH_4)
  795. dsor |= 1 << 15;
  796. }
  797. switch (ios->power_mode) {
  798. case MMC_POWER_OFF:
  799. mmc_omap_power(host, 0);
  800. break;
  801. case MMC_POWER_UP:
  802. case MMC_POWER_ON:
  803. mmc_omap_power(host, 1);
  804. dsor |= 1<<11;
  805. break;
  806. }
  807. host->bus_mode = ios->bus_mode;
  808. host->hw_bus_mode = host->bus_mode;
  809. clk_enable(host->fclk);
  810. /* On insanely high arm_per frequencies something sometimes
  811. * goes somehow out of sync, and the POW bit is not being set,
  812. * which results in the while loop below getting stuck.
  813. * Writing to the CON register twice seems to do the trick. */
  814. for (i = 0; i < 2; i++)
  815. OMAP_MMC_WRITE(host->base, CON, dsor);
  816. if (ios->power_mode == MMC_POWER_UP) {
  817. /* Send clock cycles, poll completion */
  818. OMAP_MMC_WRITE(host->base, IE, 0);
  819. OMAP_MMC_WRITE(host->base, STAT, 0xffff);
  820. OMAP_MMC_WRITE(host->base, CMD, 1<<7);
  821. while (0 == (OMAP_MMC_READ(host->base, STAT) & 1));
  822. OMAP_MMC_WRITE(host->base, STAT, 1);
  823. }
  824. clk_disable(host->fclk);
  825. }
  826. static int mmc_omap_get_ro(struct mmc_host *mmc)
  827. {
  828. struct mmc_omap_host *host = mmc_priv(mmc);
  829. return host->wp_pin && omap_get_gpio_datain(host->wp_pin);
  830. }
  831. static struct mmc_host_ops mmc_omap_ops = {
  832. .request = mmc_omap_request,
  833. .set_ios = mmc_omap_set_ios,
  834. .get_ro = mmc_omap_get_ro,
  835. };
  836. static int __init mmc_omap_probe(struct platform_device *pdev)
  837. {
  838. struct omap_mmc_conf *minfo = pdev->dev.platform_data;
  839. struct mmc_host *mmc;
  840. struct mmc_omap_host *host = NULL;
  841. struct resource *r;
  842. int ret = 0;
  843. int irq;
  844. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  845. irq = platform_get_irq(pdev, 0);
  846. if (!r || irq < 0)
  847. return -ENXIO;
  848. r = request_mem_region(pdev->resource[0].start,
  849. pdev->resource[0].end - pdev->resource[0].start + 1,
  850. pdev->name);
  851. if (!r)
  852. return -EBUSY;
  853. mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
  854. if (!mmc) {
  855. ret = -ENOMEM;
  856. goto out;
  857. }
  858. host = mmc_priv(mmc);
  859. host->mmc = mmc;
  860. spin_lock_init(&host->dma_lock);
  861. init_timer(&host->dma_timer);
  862. host->dma_timer.function = mmc_omap_dma_timer;
  863. host->dma_timer.data = (unsigned long) host;
  864. host->id = pdev->id;
  865. host->res = r;
  866. host->irq = irq;
  867. if (cpu_is_omap24xx()) {
  868. host->iclk = clk_get(&pdev->dev, "mmc_ick");
  869. if (IS_ERR(host->iclk))
  870. goto out;
  871. clk_enable(host->iclk);
  872. }
  873. if (!cpu_is_omap24xx())
  874. host->fclk = clk_get(&pdev->dev, "mmc_ck");
  875. else
  876. host->fclk = clk_get(&pdev->dev, "mmc_fck");
  877. if (IS_ERR(host->fclk)) {
  878. ret = PTR_ERR(host->fclk);
  879. goto out;
  880. }
  881. /* REVISIT:
  882. * Also, use minfo->cover to decide how to manage
  883. * the card detect sensing.
  884. */
  885. host->power_pin = minfo->power_pin;
  886. host->switch_pin = minfo->switch_pin;
  887. host->wp_pin = minfo->wp_pin;
  888. host->use_dma = 1;
  889. host->dma_ch = -1;
  890. host->irq = pdev->resource[1].start;
  891. host->base = (void __iomem*)IO_ADDRESS(r->start);
  892. mmc->ops = &mmc_omap_ops;
  893. mmc->f_min = 400000;
  894. mmc->f_max = 24000000;
  895. mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
  896. mmc->caps = MMC_CAP_BYTEBLOCK;
  897. if (minfo->wire4)
  898. mmc->caps |= MMC_CAP_4_BIT_DATA;
  899. /* Use scatterlist DMA to reduce per-transfer costs.
  900. * NOTE max_seg_size assumption that small blocks aren't
  901. * normally used (except e.g. for reading SD registers).
  902. */
  903. mmc->max_phys_segs = 32;
  904. mmc->max_hw_segs = 32;
  905. mmc->max_sectors = 256; /* NBLK max 11-bits, OMAP also limited by DMA */
  906. mmc->max_seg_size = mmc->max_sectors * 512;
  907. if (host->power_pin >= 0) {
  908. if ((ret = omap_request_gpio(host->power_pin)) != 0) {
  909. dev_err(mmc_dev(host->mmc),
  910. "Unable to get GPIO pin for MMC power\n");
  911. goto out;
  912. }
  913. omap_set_gpio_direction(host->power_pin, 0);
  914. }
  915. ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
  916. if (ret)
  917. goto out;
  918. host->dev = &pdev->dev;
  919. platform_set_drvdata(pdev, host);
  920. mmc_add_host(mmc);
  921. if (host->switch_pin >= 0) {
  922. INIT_WORK(&host->switch_work, mmc_omap_switch_handler, host);
  923. init_timer(&host->switch_timer);
  924. host->switch_timer.function = mmc_omap_switch_timer;
  925. host->switch_timer.data = (unsigned long) host;
  926. if (omap_request_gpio(host->switch_pin) != 0) {
  927. dev_warn(mmc_dev(host->mmc), "Unable to get GPIO pin for MMC cover switch\n");
  928. host->switch_pin = -1;
  929. goto no_switch;
  930. }
  931. omap_set_gpio_direction(host->switch_pin, 1);
  932. ret = request_irq(OMAP_GPIO_IRQ(host->switch_pin),
  933. mmc_omap_switch_irq, IRQF_TRIGGER_RISING, DRIVER_NAME, host);
  934. if (ret) {
  935. dev_warn(mmc_dev(host->mmc), "Unable to get IRQ for MMC cover switch\n");
  936. omap_free_gpio(host->switch_pin);
  937. host->switch_pin = -1;
  938. goto no_switch;
  939. }
  940. ret = device_create_file(&pdev->dev, &dev_attr_cover_switch);
  941. if (ret == 0) {
  942. ret = device_create_file(&pdev->dev, &dev_attr_enable_poll);
  943. if (ret != 0)
  944. device_remove_file(&pdev->dev, &dev_attr_cover_switch);
  945. }
  946. if (ret) {
  947. dev_warn(mmc_dev(host->mmc), "Unable to create sysfs attributes\n");
  948. free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
  949. omap_free_gpio(host->switch_pin);
  950. host->switch_pin = -1;
  951. goto no_switch;
  952. }
  953. if (mmc_omap_enable_poll && mmc_omap_cover_is_open(host))
  954. schedule_work(&host->switch_work);
  955. }
  956. no_switch:
  957. return 0;
  958. out:
  959. /* FIXME: Free other resources too. */
  960. if (host) {
  961. if (host->iclk && !IS_ERR(host->iclk))
  962. clk_put(host->iclk);
  963. if (host->fclk && !IS_ERR(host->fclk))
  964. clk_put(host->fclk);
  965. mmc_free_host(host->mmc);
  966. }
  967. return ret;
  968. }
  969. static int mmc_omap_remove(struct platform_device *pdev)
  970. {
  971. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  972. platform_set_drvdata(pdev, NULL);
  973. if (host) {
  974. mmc_remove_host(host->mmc);
  975. free_irq(host->irq, host);
  976. if (host->power_pin >= 0)
  977. omap_free_gpio(host->power_pin);
  978. if (host->switch_pin >= 0) {
  979. device_remove_file(&pdev->dev, &dev_attr_enable_poll);
  980. device_remove_file(&pdev->dev, &dev_attr_cover_switch);
  981. free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
  982. omap_free_gpio(host->switch_pin);
  983. host->switch_pin = -1;
  984. del_timer_sync(&host->switch_timer);
  985. flush_scheduled_work();
  986. }
  987. if (host->iclk && !IS_ERR(host->iclk))
  988. clk_put(host->iclk);
  989. if (host->fclk && !IS_ERR(host->fclk))
  990. clk_put(host->fclk);
  991. mmc_free_host(host->mmc);
  992. }
  993. release_mem_region(pdev->resource[0].start,
  994. pdev->resource[0].end - pdev->resource[0].start + 1);
  995. return 0;
  996. }
  997. #ifdef CONFIG_PM
  998. static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
  999. {
  1000. int ret = 0;
  1001. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1002. if (host && host->suspended)
  1003. return 0;
  1004. if (host) {
  1005. ret = mmc_suspend_host(host->mmc, mesg);
  1006. if (ret == 0)
  1007. host->suspended = 1;
  1008. }
  1009. return ret;
  1010. }
  1011. static int mmc_omap_resume(struct platform_device *pdev)
  1012. {
  1013. int ret = 0;
  1014. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1015. if (host && !host->suspended)
  1016. return 0;
  1017. if (host) {
  1018. ret = mmc_resume_host(host->mmc);
  1019. if (ret == 0)
  1020. host->suspended = 0;
  1021. }
  1022. return ret;
  1023. }
  1024. #else
  1025. #define mmc_omap_suspend NULL
  1026. #define mmc_omap_resume NULL
  1027. #endif
  1028. static struct platform_driver mmc_omap_driver = {
  1029. .probe = mmc_omap_probe,
  1030. .remove = mmc_omap_remove,
  1031. .suspend = mmc_omap_suspend,
  1032. .resume = mmc_omap_resume,
  1033. .driver = {
  1034. .name = DRIVER_NAME,
  1035. },
  1036. };
  1037. static int __init mmc_omap_init(void)
  1038. {
  1039. return platform_driver_register(&mmc_omap_driver);
  1040. }
  1041. static void __exit mmc_omap_exit(void)
  1042. {
  1043. platform_driver_unregister(&mmc_omap_driver);
  1044. }
  1045. module_init(mmc_omap_init);
  1046. module_exit(mmc_omap_exit);
  1047. MODULE_DESCRIPTION("OMAP Multimedia Card driver");
  1048. MODULE_LICENSE("GPL");
  1049. MODULE_ALIAS(DRIVER_NAME);
  1050. MODULE_AUTHOR("Juha Yrjölä");