omap.c 30 KB

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