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, struct pt_regs *regs)
  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, struct pt_regs *regs)
  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. if (unlikely(count > 0xffff))
  557. BUG();
  558. OMAP_MMC_WRITE(host->base, BUF, buf);
  559. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
  560. frame, count, OMAP_DMA_SYNC_FRAME,
  561. sync_dev, 0);
  562. }
  563. /* A scatterlist segment completed */
  564. static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
  565. {
  566. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  567. struct mmc_data *mmcdat = host->data;
  568. if (unlikely(host->dma_ch < 0)) {
  569. dev_err(mmc_dev(host->mmc),
  570. "DMA callback while DMA not enabled\n");
  571. return;
  572. }
  573. /* FIXME: We really should do something to _handle_ the errors */
  574. if (ch_status & OMAP1_DMA_TOUT_IRQ) {
  575. dev_err(mmc_dev(host->mmc),"DMA timeout\n");
  576. return;
  577. }
  578. if (ch_status & OMAP_DMA_DROP_IRQ) {
  579. dev_err(mmc_dev(host->mmc), "DMA sync error\n");
  580. return;
  581. }
  582. if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
  583. return;
  584. }
  585. mmcdat->bytes_xfered += host->dma_len;
  586. host->sg_idx++;
  587. if (host->sg_idx < host->sg_len) {
  588. mmc_omap_prepare_dma(host, host->data);
  589. omap_start_dma(host->dma_ch);
  590. } else
  591. mmc_omap_dma_done(host, host->data);
  592. }
  593. static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
  594. {
  595. const char *dev_name;
  596. int sync_dev, dma_ch, is_read, r;
  597. is_read = !(data->flags & MMC_DATA_WRITE);
  598. del_timer_sync(&host->dma_timer);
  599. if (host->dma_ch >= 0) {
  600. if (is_read == host->dma_is_read)
  601. return 0;
  602. omap_free_dma(host->dma_ch);
  603. host->dma_ch = -1;
  604. }
  605. if (is_read) {
  606. if (host->id == 1) {
  607. sync_dev = OMAP_DMA_MMC_RX;
  608. dev_name = "MMC1 read";
  609. } else {
  610. sync_dev = OMAP_DMA_MMC2_RX;
  611. dev_name = "MMC2 read";
  612. }
  613. } else {
  614. if (host->id == 1) {
  615. sync_dev = OMAP_DMA_MMC_TX;
  616. dev_name = "MMC1 write";
  617. } else {
  618. sync_dev = OMAP_DMA_MMC2_TX;
  619. dev_name = "MMC2 write";
  620. }
  621. }
  622. r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
  623. host, &dma_ch);
  624. if (r != 0) {
  625. dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
  626. return r;
  627. }
  628. host->dma_ch = dma_ch;
  629. host->dma_is_read = is_read;
  630. return 0;
  631. }
  632. static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  633. {
  634. u16 reg;
  635. reg = OMAP_MMC_READ(host->base, SDIO);
  636. reg &= ~(1 << 5);
  637. OMAP_MMC_WRITE(host->base, SDIO, reg);
  638. /* Set maximum timeout */
  639. OMAP_MMC_WRITE(host->base, CTO, 0xff);
  640. }
  641. static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  642. {
  643. int timeout;
  644. u16 reg;
  645. /* Convert ns to clock cycles by assuming 20MHz frequency
  646. * 1 cycle at 20MHz = 500 ns
  647. */
  648. timeout = req->data->timeout_clks + req->data->timeout_ns / 500;
  649. /* Check if we need to use timeout multiplier register */
  650. reg = OMAP_MMC_READ(host->base, SDIO);
  651. if (timeout > 0xffff) {
  652. reg |= (1 << 5);
  653. timeout /= 1024;
  654. } else
  655. reg &= ~(1 << 5);
  656. OMAP_MMC_WRITE(host->base, SDIO, reg);
  657. OMAP_MMC_WRITE(host->base, DTO, timeout);
  658. }
  659. static void
  660. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  661. {
  662. struct mmc_data *data = req->data;
  663. int i, use_dma, block_size;
  664. unsigned sg_len;
  665. host->data = data;
  666. if (data == NULL) {
  667. OMAP_MMC_WRITE(host->base, BLEN, 0);
  668. OMAP_MMC_WRITE(host->base, NBLK, 0);
  669. OMAP_MMC_WRITE(host->base, BUF, 0);
  670. host->dma_in_use = 0;
  671. set_cmd_timeout(host, req);
  672. return;
  673. }
  674. block_size = data->blksz;
  675. OMAP_MMC_WRITE(host->base, NBLK, data->blocks - 1);
  676. OMAP_MMC_WRITE(host->base, BLEN, block_size - 1);
  677. set_data_timeout(host, req);
  678. /* cope with calling layer confusion; it issues "single
  679. * block" writes using multi-block scatterlists.
  680. */
  681. sg_len = (data->blocks == 1) ? 1 : data->sg_len;
  682. /* Only do DMA for entire blocks */
  683. use_dma = host->use_dma;
  684. if (use_dma) {
  685. for (i = 0; i < sg_len; i++) {
  686. if ((data->sg[i].length % block_size) != 0) {
  687. use_dma = 0;
  688. break;
  689. }
  690. }
  691. }
  692. host->sg_idx = 0;
  693. if (use_dma) {
  694. if (mmc_omap_get_dma_channel(host, data) == 0) {
  695. enum dma_data_direction dma_data_dir;
  696. if (data->flags & MMC_DATA_WRITE)
  697. dma_data_dir = DMA_TO_DEVICE;
  698. else
  699. dma_data_dir = DMA_FROM_DEVICE;
  700. host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  701. sg_len, dma_data_dir);
  702. host->total_bytes_left = 0;
  703. mmc_omap_prepare_dma(host, req->data);
  704. host->brs_received = 0;
  705. host->dma_done = 0;
  706. host->dma_in_use = 1;
  707. } else
  708. use_dma = 0;
  709. }
  710. /* Revert to PIO? */
  711. if (!use_dma) {
  712. OMAP_MMC_WRITE(host->base, BUF, 0x1f1f);
  713. host->total_bytes_left = data->blocks * block_size;
  714. host->sg_len = sg_len;
  715. mmc_omap_sg_to_buf(host);
  716. host->dma_in_use = 0;
  717. }
  718. }
  719. static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
  720. {
  721. struct mmc_omap_host *host = mmc_priv(mmc);
  722. WARN_ON(host->mrq != NULL);
  723. host->mrq = req;
  724. /* only touch fifo AFTER the controller readies it */
  725. mmc_omap_prepare_data(host, req);
  726. mmc_omap_start_command(host, req->cmd);
  727. if (host->dma_in_use)
  728. omap_start_dma(host->dma_ch);
  729. }
  730. static void innovator_fpga_socket_power(int on)
  731. {
  732. #if defined(CONFIG_MACH_OMAP_INNOVATOR) && defined(CONFIG_ARCH_OMAP15XX)
  733. if (on) {
  734. fpga_write(fpga_read(OMAP1510_FPGA_POWER) | (1 << 3),
  735. OMAP1510_FPGA_POWER);
  736. } else {
  737. fpga_write(fpga_read(OMAP1510_FPGA_POWER) & ~(1 << 3),
  738. OMAP1510_FPGA_POWER);
  739. }
  740. #endif
  741. }
  742. /*
  743. * Turn the socket power on/off. Innovator uses FPGA, most boards
  744. * probably use GPIO.
  745. */
  746. static void mmc_omap_power(struct mmc_omap_host *host, int on)
  747. {
  748. if (on) {
  749. if (machine_is_omap_innovator())
  750. innovator_fpga_socket_power(1);
  751. else if (machine_is_omap_h2())
  752. tps65010_set_gpio_out_value(GPIO3, HIGH);
  753. else if (machine_is_omap_h3())
  754. /* GPIO 4 of TPS65010 sends SD_EN signal */
  755. tps65010_set_gpio_out_value(GPIO4, HIGH);
  756. else if (cpu_is_omap24xx()) {
  757. u16 reg = OMAP_MMC_READ(host->base, CON);
  758. OMAP_MMC_WRITE(host->base, CON, reg | (1 << 11));
  759. } else
  760. if (host->power_pin >= 0)
  761. omap_set_gpio_dataout(host->power_pin, 1);
  762. } else {
  763. if (machine_is_omap_innovator())
  764. innovator_fpga_socket_power(0);
  765. else if (machine_is_omap_h2())
  766. tps65010_set_gpio_out_value(GPIO3, LOW);
  767. else if (machine_is_omap_h3())
  768. tps65010_set_gpio_out_value(GPIO4, LOW);
  769. else if (cpu_is_omap24xx()) {
  770. u16 reg = OMAP_MMC_READ(host->base, CON);
  771. OMAP_MMC_WRITE(host->base, CON, reg & ~(1 << 11));
  772. } else
  773. if (host->power_pin >= 0)
  774. omap_set_gpio_dataout(host->power_pin, 0);
  775. }
  776. }
  777. static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  778. {
  779. struct mmc_omap_host *host = mmc_priv(mmc);
  780. int dsor;
  781. int realclock, i;
  782. realclock = ios->clock;
  783. if (ios->clock == 0)
  784. dsor = 0;
  785. else {
  786. int func_clk_rate = clk_get_rate(host->fclk);
  787. dsor = func_clk_rate / realclock;
  788. if (dsor < 1)
  789. dsor = 1;
  790. if (func_clk_rate / dsor > realclock)
  791. dsor++;
  792. if (dsor > 250)
  793. dsor = 250;
  794. dsor++;
  795. if (ios->bus_width == MMC_BUS_WIDTH_4)
  796. dsor |= 1 << 15;
  797. }
  798. switch (ios->power_mode) {
  799. case MMC_POWER_OFF:
  800. mmc_omap_power(host, 0);
  801. break;
  802. case MMC_POWER_UP:
  803. case MMC_POWER_ON:
  804. mmc_omap_power(host, 1);
  805. dsor |= 1<<11;
  806. break;
  807. }
  808. host->bus_mode = ios->bus_mode;
  809. host->hw_bus_mode = host->bus_mode;
  810. clk_enable(host->fclk);
  811. /* On insanely high arm_per frequencies something sometimes
  812. * goes somehow out of sync, and the POW bit is not being set,
  813. * which results in the while loop below getting stuck.
  814. * Writing to the CON register twice seems to do the trick. */
  815. for (i = 0; i < 2; i++)
  816. OMAP_MMC_WRITE(host->base, CON, dsor);
  817. if (ios->power_mode == MMC_POWER_UP) {
  818. /* Send clock cycles, poll completion */
  819. OMAP_MMC_WRITE(host->base, IE, 0);
  820. OMAP_MMC_WRITE(host->base, STAT, 0xffff);
  821. OMAP_MMC_WRITE(host->base, CMD, 1<<7);
  822. while (0 == (OMAP_MMC_READ(host->base, STAT) & 1));
  823. OMAP_MMC_WRITE(host->base, STAT, 1);
  824. }
  825. clk_disable(host->fclk);
  826. }
  827. static int mmc_omap_get_ro(struct mmc_host *mmc)
  828. {
  829. struct mmc_omap_host *host = mmc_priv(mmc);
  830. return host->wp_pin && omap_get_gpio_datain(host->wp_pin);
  831. }
  832. static struct mmc_host_ops mmc_omap_ops = {
  833. .request = mmc_omap_request,
  834. .set_ios = mmc_omap_set_ios,
  835. .get_ro = mmc_omap_get_ro,
  836. };
  837. static int __init mmc_omap_probe(struct platform_device *pdev)
  838. {
  839. struct omap_mmc_conf *minfo = pdev->dev.platform_data;
  840. struct mmc_host *mmc;
  841. struct mmc_omap_host *host = NULL;
  842. struct resource *r;
  843. int ret = 0;
  844. int irq;
  845. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  846. irq = platform_get_irq(pdev, 0);
  847. if (!r || irq < 0)
  848. return -ENXIO;
  849. r = request_mem_region(pdev->resource[0].start,
  850. pdev->resource[0].end - pdev->resource[0].start + 1,
  851. pdev->name);
  852. if (!r)
  853. return -EBUSY;
  854. mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
  855. if (!mmc) {
  856. ret = -ENOMEM;
  857. goto out;
  858. }
  859. host = mmc_priv(mmc);
  860. host->mmc = mmc;
  861. spin_lock_init(&host->dma_lock);
  862. init_timer(&host->dma_timer);
  863. host->dma_timer.function = mmc_omap_dma_timer;
  864. host->dma_timer.data = (unsigned long) host;
  865. host->id = pdev->id;
  866. host->res = r;
  867. host->irq = irq;
  868. if (cpu_is_omap24xx()) {
  869. host->iclk = clk_get(&pdev->dev, "mmc_ick");
  870. if (IS_ERR(host->iclk))
  871. goto out;
  872. clk_enable(host->iclk);
  873. }
  874. if (!cpu_is_omap24xx())
  875. host->fclk = clk_get(&pdev->dev, "mmc_ck");
  876. else
  877. host->fclk = clk_get(&pdev->dev, "mmc_fck");
  878. if (IS_ERR(host->fclk)) {
  879. ret = PTR_ERR(host->fclk);
  880. goto out;
  881. }
  882. /* REVISIT:
  883. * Also, use minfo->cover to decide how to manage
  884. * the card detect sensing.
  885. */
  886. host->power_pin = minfo->power_pin;
  887. host->switch_pin = minfo->switch_pin;
  888. host->wp_pin = minfo->wp_pin;
  889. host->use_dma = 1;
  890. host->dma_ch = -1;
  891. host->irq = pdev->resource[1].start;
  892. host->base = (void __iomem*)IO_ADDRESS(r->start);
  893. if (minfo->wire4)
  894. mmc->caps |= MMC_CAP_4_BIT_DATA;
  895. mmc->ops = &mmc_omap_ops;
  896. mmc->f_min = 400000;
  897. mmc->f_max = 24000000;
  898. mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
  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ä");