omap.c 31 KB

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