omap.c 33 KB

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