omap.c 35 KB

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