omap.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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. 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. mmc->ops = &mmc_omap_ops;
  894. mmc->f_min = 400000;
  895. mmc->f_max = 24000000;
  896. mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
  897. mmc->caps = MMC_CAP_BYTEBLOCK;
  898. if (minfo->wire4)
  899. mmc->caps |= MMC_CAP_4_BIT_DATA;
  900. /* Use scatterlist DMA to reduce per-transfer costs.
  901. * NOTE max_seg_size assumption that small blocks aren't
  902. * normally used (except e.g. for reading SD registers).
  903. */
  904. mmc->max_phys_segs = 32;
  905. mmc->max_hw_segs = 32;
  906. mmc->max_sectors = 256; /* NBLK max 11-bits, OMAP also limited by DMA */
  907. mmc->max_seg_size = mmc->max_sectors * 512;
  908. if (host->power_pin >= 0) {
  909. if ((ret = omap_request_gpio(host->power_pin)) != 0) {
  910. dev_err(mmc_dev(host->mmc),
  911. "Unable to get GPIO pin for MMC power\n");
  912. goto out;
  913. }
  914. omap_set_gpio_direction(host->power_pin, 0);
  915. }
  916. ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
  917. if (ret)
  918. goto out;
  919. host->dev = &pdev->dev;
  920. platform_set_drvdata(pdev, host);
  921. mmc_add_host(mmc);
  922. if (host->switch_pin >= 0) {
  923. INIT_WORK(&host->switch_work, mmc_omap_switch_handler, host);
  924. init_timer(&host->switch_timer);
  925. host->switch_timer.function = mmc_omap_switch_timer;
  926. host->switch_timer.data = (unsigned long) host;
  927. if (omap_request_gpio(host->switch_pin) != 0) {
  928. dev_warn(mmc_dev(host->mmc), "Unable to get GPIO pin for MMC cover switch\n");
  929. host->switch_pin = -1;
  930. goto no_switch;
  931. }
  932. omap_set_gpio_direction(host->switch_pin, 1);
  933. ret = request_irq(OMAP_GPIO_IRQ(host->switch_pin),
  934. mmc_omap_switch_irq, IRQF_TRIGGER_RISING, DRIVER_NAME, host);
  935. if (ret) {
  936. dev_warn(mmc_dev(host->mmc), "Unable to get IRQ for MMC cover switch\n");
  937. omap_free_gpio(host->switch_pin);
  938. host->switch_pin = -1;
  939. goto no_switch;
  940. }
  941. ret = device_create_file(&pdev->dev, &dev_attr_cover_switch);
  942. if (ret == 0) {
  943. ret = device_create_file(&pdev->dev, &dev_attr_enable_poll);
  944. if (ret != 0)
  945. device_remove_file(&pdev->dev, &dev_attr_cover_switch);
  946. }
  947. if (ret) {
  948. dev_warn(mmc_dev(host->mmc), "Unable to create sysfs attributes\n");
  949. free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
  950. omap_free_gpio(host->switch_pin);
  951. host->switch_pin = -1;
  952. goto no_switch;
  953. }
  954. if (mmc_omap_enable_poll && mmc_omap_cover_is_open(host))
  955. schedule_work(&host->switch_work);
  956. }
  957. no_switch:
  958. return 0;
  959. out:
  960. /* FIXME: Free other resources too. */
  961. if (host) {
  962. if (host->iclk && !IS_ERR(host->iclk))
  963. clk_put(host->iclk);
  964. if (host->fclk && !IS_ERR(host->fclk))
  965. clk_put(host->fclk);
  966. mmc_free_host(host->mmc);
  967. }
  968. return ret;
  969. }
  970. static int mmc_omap_remove(struct platform_device *pdev)
  971. {
  972. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  973. platform_set_drvdata(pdev, NULL);
  974. if (host) {
  975. mmc_remove_host(host->mmc);
  976. free_irq(host->irq, host);
  977. if (host->power_pin >= 0)
  978. omap_free_gpio(host->power_pin);
  979. if (host->switch_pin >= 0) {
  980. device_remove_file(&pdev->dev, &dev_attr_enable_poll);
  981. device_remove_file(&pdev->dev, &dev_attr_cover_switch);
  982. free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
  983. omap_free_gpio(host->switch_pin);
  984. host->switch_pin = -1;
  985. del_timer_sync(&host->switch_timer);
  986. flush_scheduled_work();
  987. }
  988. if (host->iclk && !IS_ERR(host->iclk))
  989. clk_put(host->iclk);
  990. if (host->fclk && !IS_ERR(host->fclk))
  991. clk_put(host->fclk);
  992. mmc_free_host(host->mmc);
  993. }
  994. release_mem_region(pdev->resource[0].start,
  995. pdev->resource[0].end - pdev->resource[0].start + 1);
  996. return 0;
  997. }
  998. #ifdef CONFIG_PM
  999. static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
  1000. {
  1001. int ret = 0;
  1002. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1003. if (host && host->suspended)
  1004. return 0;
  1005. if (host) {
  1006. ret = mmc_suspend_host(host->mmc, mesg);
  1007. if (ret == 0)
  1008. host->suspended = 1;
  1009. }
  1010. return ret;
  1011. }
  1012. static int mmc_omap_resume(struct platform_device *pdev)
  1013. {
  1014. int ret = 0;
  1015. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1016. if (host && !host->suspended)
  1017. return 0;
  1018. if (host) {
  1019. ret = mmc_resume_host(host->mmc);
  1020. if (ret == 0)
  1021. host->suspended = 0;
  1022. }
  1023. return ret;
  1024. }
  1025. #else
  1026. #define mmc_omap_suspend NULL
  1027. #define mmc_omap_resume NULL
  1028. #endif
  1029. static struct platform_driver mmc_omap_driver = {
  1030. .probe = mmc_omap_probe,
  1031. .remove = mmc_omap_remove,
  1032. .suspend = mmc_omap_suspend,
  1033. .resume = mmc_omap_resume,
  1034. .driver = {
  1035. .name = DRIVER_NAME,
  1036. },
  1037. };
  1038. static int __init mmc_omap_init(void)
  1039. {
  1040. return platform_driver_register(&mmc_omap_driver);
  1041. }
  1042. static void __exit mmc_omap_exit(void)
  1043. {
  1044. platform_driver_unregister(&mmc_omap_driver);
  1045. }
  1046. module_init(mmc_omap_init);
  1047. module_exit(mmc_omap_exit);
  1048. MODULE_DESCRIPTION("OMAP Multimedia Card driver");
  1049. MODULE_LICENSE("GPL");
  1050. MODULE_ALIAS(DRIVER_NAME);
  1051. MODULE_AUTHOR("Juha Yrjölä");