omap.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  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/dmaengine.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/delay.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/timer.h>
  24. #include <linux/omap-dma.h>
  25. #include <linux/mmc/host.h>
  26. #include <linux/mmc/card.h>
  27. #include <linux/clk.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/i2c/tps65010.h>
  30. #include <linux/slab.h>
  31. #include <asm/io.h>
  32. #include <asm/irq.h>
  33. #include <plat/mmc.h>
  34. #include <asm/gpio.h>
  35. #include <plat/dma.h>
  36. #include <plat/mux.h>
  37. #include <plat/fpga.h>
  38. #define OMAP_MMC_REG_CMD 0x00
  39. #define OMAP_MMC_REG_ARGL 0x01
  40. #define OMAP_MMC_REG_ARGH 0x02
  41. #define OMAP_MMC_REG_CON 0x03
  42. #define OMAP_MMC_REG_STAT 0x04
  43. #define OMAP_MMC_REG_IE 0x05
  44. #define OMAP_MMC_REG_CTO 0x06
  45. #define OMAP_MMC_REG_DTO 0x07
  46. #define OMAP_MMC_REG_DATA 0x08
  47. #define OMAP_MMC_REG_BLEN 0x09
  48. #define OMAP_MMC_REG_NBLK 0x0a
  49. #define OMAP_MMC_REG_BUF 0x0b
  50. #define OMAP_MMC_REG_SDIO 0x0d
  51. #define OMAP_MMC_REG_REV 0x0f
  52. #define OMAP_MMC_REG_RSP0 0x10
  53. #define OMAP_MMC_REG_RSP1 0x11
  54. #define OMAP_MMC_REG_RSP2 0x12
  55. #define OMAP_MMC_REG_RSP3 0x13
  56. #define OMAP_MMC_REG_RSP4 0x14
  57. #define OMAP_MMC_REG_RSP5 0x15
  58. #define OMAP_MMC_REG_RSP6 0x16
  59. #define OMAP_MMC_REG_RSP7 0x17
  60. #define OMAP_MMC_REG_IOSR 0x18
  61. #define OMAP_MMC_REG_SYSC 0x19
  62. #define OMAP_MMC_REG_SYSS 0x1a
  63. #define OMAP_MMC_STAT_CARD_ERR (1 << 14)
  64. #define OMAP_MMC_STAT_CARD_IRQ (1 << 13)
  65. #define OMAP_MMC_STAT_OCR_BUSY (1 << 12)
  66. #define OMAP_MMC_STAT_A_EMPTY (1 << 11)
  67. #define OMAP_MMC_STAT_A_FULL (1 << 10)
  68. #define OMAP_MMC_STAT_CMD_CRC (1 << 8)
  69. #define OMAP_MMC_STAT_CMD_TOUT (1 << 7)
  70. #define OMAP_MMC_STAT_DATA_CRC (1 << 6)
  71. #define OMAP_MMC_STAT_DATA_TOUT (1 << 5)
  72. #define OMAP_MMC_STAT_END_BUSY (1 << 4)
  73. #define OMAP_MMC_STAT_END_OF_DATA (1 << 3)
  74. #define OMAP_MMC_STAT_CARD_BUSY (1 << 2)
  75. #define OMAP_MMC_STAT_END_OF_CMD (1 << 0)
  76. #define OMAP_MMC_REG(host, reg) (OMAP_MMC_REG_##reg << (host)->reg_shift)
  77. #define OMAP_MMC_READ(host, reg) __raw_readw((host)->virt_base + OMAP_MMC_REG(host, reg))
  78. #define OMAP_MMC_WRITE(host, reg, val) __raw_writew((val), (host)->virt_base + OMAP_MMC_REG(host, reg))
  79. /*
  80. * Command types
  81. */
  82. #define OMAP_MMC_CMDTYPE_BC 0
  83. #define OMAP_MMC_CMDTYPE_BCR 1
  84. #define OMAP_MMC_CMDTYPE_AC 2
  85. #define OMAP_MMC_CMDTYPE_ADTC 3
  86. #define DRIVER_NAME "mmci-omap"
  87. /* Specifies how often in millisecs to poll for card status changes
  88. * when the cover switch is open */
  89. #define OMAP_MMC_COVER_POLL_DELAY 500
  90. struct mmc_omap_host;
  91. struct mmc_omap_slot {
  92. int id;
  93. unsigned int vdd;
  94. u16 saved_con;
  95. u16 bus_mode;
  96. unsigned int fclk_freq;
  97. unsigned powered:1;
  98. struct tasklet_struct cover_tasklet;
  99. struct timer_list cover_timer;
  100. unsigned cover_open;
  101. struct mmc_request *mrq;
  102. struct mmc_omap_host *host;
  103. struct mmc_host *mmc;
  104. struct omap_mmc_slot_data *pdata;
  105. };
  106. struct mmc_omap_host {
  107. int initialized;
  108. int suspended;
  109. struct mmc_request * mrq;
  110. struct mmc_command * cmd;
  111. struct mmc_data * data;
  112. struct mmc_host * mmc;
  113. struct device * dev;
  114. unsigned char id; /* 16xx chips have 2 MMC blocks */
  115. struct clk * iclk;
  116. struct clk * fclk;
  117. struct dma_chan *dma_rx;
  118. u32 dma_rx_burst;
  119. struct dma_chan *dma_tx;
  120. u32 dma_tx_burst;
  121. struct resource *mem_res;
  122. void __iomem *virt_base;
  123. unsigned int phys_base;
  124. int irq;
  125. unsigned char bus_mode;
  126. unsigned char hw_bus_mode;
  127. unsigned int reg_shift;
  128. struct work_struct cmd_abort_work;
  129. unsigned abort:1;
  130. struct timer_list cmd_abort_timer;
  131. struct work_struct slot_release_work;
  132. struct mmc_omap_slot *next_slot;
  133. struct work_struct send_stop_work;
  134. struct mmc_data *stop_data;
  135. unsigned int sg_len;
  136. int sg_idx;
  137. u16 * buffer;
  138. u32 buffer_bytes_left;
  139. u32 total_bytes_left;
  140. unsigned use_dma:1;
  141. unsigned brs_received:1, dma_done:1;
  142. unsigned dma_in_use:1;
  143. spinlock_t dma_lock;
  144. struct mmc_omap_slot *slots[OMAP_MMC_MAX_SLOTS];
  145. struct mmc_omap_slot *current_slot;
  146. spinlock_t slot_lock;
  147. wait_queue_head_t slot_wq;
  148. int nr_slots;
  149. struct timer_list clk_timer;
  150. spinlock_t clk_lock; /* for changing enabled state */
  151. unsigned int fclk_enabled:1;
  152. struct workqueue_struct *mmc_omap_wq;
  153. struct omap_mmc_platform_data *pdata;
  154. };
  155. static void mmc_omap_fclk_offdelay(struct mmc_omap_slot *slot)
  156. {
  157. unsigned long tick_ns;
  158. if (slot != NULL && slot->host->fclk_enabled && slot->fclk_freq > 0) {
  159. tick_ns = (1000000000 + slot->fclk_freq - 1) / slot->fclk_freq;
  160. ndelay(8 * tick_ns);
  161. }
  162. }
  163. static void mmc_omap_fclk_enable(struct mmc_omap_host *host, unsigned int enable)
  164. {
  165. unsigned long flags;
  166. spin_lock_irqsave(&host->clk_lock, flags);
  167. if (host->fclk_enabled != enable) {
  168. host->fclk_enabled = enable;
  169. if (enable)
  170. clk_enable(host->fclk);
  171. else
  172. clk_disable(host->fclk);
  173. }
  174. spin_unlock_irqrestore(&host->clk_lock, flags);
  175. }
  176. static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
  177. {
  178. struct mmc_omap_host *host = slot->host;
  179. unsigned long flags;
  180. if (claimed)
  181. goto no_claim;
  182. spin_lock_irqsave(&host->slot_lock, flags);
  183. while (host->mmc != NULL) {
  184. spin_unlock_irqrestore(&host->slot_lock, flags);
  185. wait_event(host->slot_wq, host->mmc == NULL);
  186. spin_lock_irqsave(&host->slot_lock, flags);
  187. }
  188. host->mmc = slot->mmc;
  189. spin_unlock_irqrestore(&host->slot_lock, flags);
  190. no_claim:
  191. del_timer(&host->clk_timer);
  192. if (host->current_slot != slot || !claimed)
  193. mmc_omap_fclk_offdelay(host->current_slot);
  194. if (host->current_slot != slot) {
  195. OMAP_MMC_WRITE(host, CON, slot->saved_con & 0xFC00);
  196. if (host->pdata->switch_slot != NULL)
  197. host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
  198. host->current_slot = slot;
  199. }
  200. if (claimed) {
  201. mmc_omap_fclk_enable(host, 1);
  202. /* Doing the dummy read here seems to work around some bug
  203. * at least in OMAP24xx silicon where the command would not
  204. * start after writing the CMD register. Sigh. */
  205. OMAP_MMC_READ(host, CON);
  206. OMAP_MMC_WRITE(host, CON, slot->saved_con);
  207. } else
  208. mmc_omap_fclk_enable(host, 0);
  209. }
  210. static void mmc_omap_start_request(struct mmc_omap_host *host,
  211. struct mmc_request *req);
  212. static void mmc_omap_slot_release_work(struct work_struct *work)
  213. {
  214. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  215. slot_release_work);
  216. struct mmc_omap_slot *next_slot = host->next_slot;
  217. struct mmc_request *rq;
  218. host->next_slot = NULL;
  219. mmc_omap_select_slot(next_slot, 1);
  220. rq = next_slot->mrq;
  221. next_slot->mrq = NULL;
  222. mmc_omap_start_request(host, rq);
  223. }
  224. static void mmc_omap_release_slot(struct mmc_omap_slot *slot, int clk_enabled)
  225. {
  226. struct mmc_omap_host *host = slot->host;
  227. unsigned long flags;
  228. int i;
  229. BUG_ON(slot == NULL || host->mmc == NULL);
  230. if (clk_enabled)
  231. /* Keeps clock running for at least 8 cycles on valid freq */
  232. mod_timer(&host->clk_timer, jiffies + HZ/10);
  233. else {
  234. del_timer(&host->clk_timer);
  235. mmc_omap_fclk_offdelay(slot);
  236. mmc_omap_fclk_enable(host, 0);
  237. }
  238. spin_lock_irqsave(&host->slot_lock, flags);
  239. /* Check for any pending requests */
  240. for (i = 0; i < host->nr_slots; i++) {
  241. struct mmc_omap_slot *new_slot;
  242. if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
  243. continue;
  244. BUG_ON(host->next_slot != NULL);
  245. new_slot = host->slots[i];
  246. /* The current slot should not have a request in queue */
  247. BUG_ON(new_slot == host->current_slot);
  248. host->next_slot = new_slot;
  249. host->mmc = new_slot->mmc;
  250. spin_unlock_irqrestore(&host->slot_lock, flags);
  251. queue_work(host->mmc_omap_wq, &host->slot_release_work);
  252. return;
  253. }
  254. host->mmc = NULL;
  255. wake_up(&host->slot_wq);
  256. spin_unlock_irqrestore(&host->slot_lock, flags);
  257. }
  258. static inline
  259. int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
  260. {
  261. if (slot->pdata->get_cover_state)
  262. return slot->pdata->get_cover_state(mmc_dev(slot->mmc),
  263. slot->id);
  264. return 0;
  265. }
  266. static ssize_t
  267. mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
  268. char *buf)
  269. {
  270. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  271. struct mmc_omap_slot *slot = mmc_priv(mmc);
  272. return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
  273. "closed");
  274. }
  275. static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
  276. static ssize_t
  277. mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
  278. char *buf)
  279. {
  280. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  281. struct mmc_omap_slot *slot = mmc_priv(mmc);
  282. return sprintf(buf, "%s\n", slot->pdata->name);
  283. }
  284. static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
  285. static void
  286. mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
  287. {
  288. u32 cmdreg;
  289. u32 resptype;
  290. u32 cmdtype;
  291. host->cmd = cmd;
  292. resptype = 0;
  293. cmdtype = 0;
  294. /* Our hardware needs to know exact type */
  295. switch (mmc_resp_type(cmd)) {
  296. case MMC_RSP_NONE:
  297. break;
  298. case MMC_RSP_R1:
  299. case MMC_RSP_R1B:
  300. /* resp 1, 1b, 6, 7 */
  301. resptype = 1;
  302. break;
  303. case MMC_RSP_R2:
  304. resptype = 2;
  305. break;
  306. case MMC_RSP_R3:
  307. resptype = 3;
  308. break;
  309. default:
  310. dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
  311. break;
  312. }
  313. if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
  314. cmdtype = OMAP_MMC_CMDTYPE_ADTC;
  315. } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
  316. cmdtype = OMAP_MMC_CMDTYPE_BC;
  317. } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
  318. cmdtype = OMAP_MMC_CMDTYPE_BCR;
  319. } else {
  320. cmdtype = OMAP_MMC_CMDTYPE_AC;
  321. }
  322. cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
  323. if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
  324. cmdreg |= 1 << 6;
  325. if (cmd->flags & MMC_RSP_BUSY)
  326. cmdreg |= 1 << 11;
  327. if (host->data && !(host->data->flags & MMC_DATA_WRITE))
  328. cmdreg |= 1 << 15;
  329. mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
  330. OMAP_MMC_WRITE(host, CTO, 200);
  331. OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
  332. OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
  333. OMAP_MMC_WRITE(host, IE,
  334. OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
  335. OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
  336. OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
  337. OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
  338. OMAP_MMC_STAT_END_OF_DATA);
  339. OMAP_MMC_WRITE(host, CMD, cmdreg);
  340. }
  341. static void
  342. mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
  343. int abort)
  344. {
  345. enum dma_data_direction dma_data_dir;
  346. struct device *dev = mmc_dev(host->mmc);
  347. struct dma_chan *c;
  348. if (data->flags & MMC_DATA_WRITE) {
  349. dma_data_dir = DMA_TO_DEVICE;
  350. c = host->dma_tx;
  351. } else {
  352. dma_data_dir = DMA_FROM_DEVICE;
  353. c = host->dma_rx;
  354. }
  355. if (c) {
  356. if (data->error) {
  357. dmaengine_terminate_all(c);
  358. /* Claim nothing transferred on error... */
  359. data->bytes_xfered = 0;
  360. }
  361. dev = c->device->dev;
  362. }
  363. dma_unmap_sg(dev, data->sg, host->sg_len, dma_data_dir);
  364. }
  365. static void mmc_omap_send_stop_work(struct work_struct *work)
  366. {
  367. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  368. send_stop_work);
  369. struct mmc_omap_slot *slot = host->current_slot;
  370. struct mmc_data *data = host->stop_data;
  371. unsigned long tick_ns;
  372. tick_ns = (1000000000 + slot->fclk_freq - 1)/slot->fclk_freq;
  373. ndelay(8*tick_ns);
  374. mmc_omap_start_command(host, data->stop);
  375. }
  376. static void
  377. mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
  378. {
  379. if (host->dma_in_use)
  380. mmc_omap_release_dma(host, data, data->error);
  381. host->data = NULL;
  382. host->sg_len = 0;
  383. /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
  384. * dozens of requests until the card finishes writing data.
  385. * It'd be cheaper to just wait till an EOFB interrupt arrives...
  386. */
  387. if (!data->stop) {
  388. struct mmc_host *mmc;
  389. host->mrq = NULL;
  390. mmc = host->mmc;
  391. mmc_omap_release_slot(host->current_slot, 1);
  392. mmc_request_done(mmc, data->mrq);
  393. return;
  394. }
  395. host->stop_data = data;
  396. queue_work(host->mmc_omap_wq, &host->send_stop_work);
  397. }
  398. static void
  399. mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
  400. {
  401. struct mmc_omap_slot *slot = host->current_slot;
  402. unsigned int restarts, passes, timeout;
  403. u16 stat = 0;
  404. /* Sending abort takes 80 clocks. Have some extra and round up */
  405. timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
  406. restarts = 0;
  407. while (restarts < maxloops) {
  408. OMAP_MMC_WRITE(host, STAT, 0xFFFF);
  409. OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
  410. passes = 0;
  411. while (passes < timeout) {
  412. stat = OMAP_MMC_READ(host, STAT);
  413. if (stat & OMAP_MMC_STAT_END_OF_CMD)
  414. goto out;
  415. udelay(1);
  416. passes++;
  417. }
  418. restarts++;
  419. }
  420. out:
  421. OMAP_MMC_WRITE(host, STAT, stat);
  422. }
  423. static void
  424. mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
  425. {
  426. if (host->dma_in_use)
  427. mmc_omap_release_dma(host, data, 1);
  428. host->data = NULL;
  429. host->sg_len = 0;
  430. mmc_omap_send_abort(host, 10000);
  431. }
  432. static void
  433. mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
  434. {
  435. unsigned long flags;
  436. int done;
  437. if (!host->dma_in_use) {
  438. mmc_omap_xfer_done(host, data);
  439. return;
  440. }
  441. done = 0;
  442. spin_lock_irqsave(&host->dma_lock, flags);
  443. if (host->dma_done)
  444. done = 1;
  445. else
  446. host->brs_received = 1;
  447. spin_unlock_irqrestore(&host->dma_lock, flags);
  448. if (done)
  449. mmc_omap_xfer_done(host, data);
  450. }
  451. static void
  452. mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
  453. {
  454. unsigned long flags;
  455. int done;
  456. done = 0;
  457. spin_lock_irqsave(&host->dma_lock, flags);
  458. if (host->brs_received)
  459. done = 1;
  460. else
  461. host->dma_done = 1;
  462. spin_unlock_irqrestore(&host->dma_lock, flags);
  463. if (done)
  464. mmc_omap_xfer_done(host, data);
  465. }
  466. static void
  467. mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
  468. {
  469. host->cmd = NULL;
  470. del_timer(&host->cmd_abort_timer);
  471. if (cmd->flags & MMC_RSP_PRESENT) {
  472. if (cmd->flags & MMC_RSP_136) {
  473. /* response type 2 */
  474. cmd->resp[3] =
  475. OMAP_MMC_READ(host, RSP0) |
  476. (OMAP_MMC_READ(host, RSP1) << 16);
  477. cmd->resp[2] =
  478. OMAP_MMC_READ(host, RSP2) |
  479. (OMAP_MMC_READ(host, RSP3) << 16);
  480. cmd->resp[1] =
  481. OMAP_MMC_READ(host, RSP4) |
  482. (OMAP_MMC_READ(host, RSP5) << 16);
  483. cmd->resp[0] =
  484. OMAP_MMC_READ(host, RSP6) |
  485. (OMAP_MMC_READ(host, RSP7) << 16);
  486. } else {
  487. /* response types 1, 1b, 3, 4, 5, 6 */
  488. cmd->resp[0] =
  489. OMAP_MMC_READ(host, RSP6) |
  490. (OMAP_MMC_READ(host, RSP7) << 16);
  491. }
  492. }
  493. if (host->data == NULL || cmd->error) {
  494. struct mmc_host *mmc;
  495. if (host->data != NULL)
  496. mmc_omap_abort_xfer(host, host->data);
  497. host->mrq = NULL;
  498. mmc = host->mmc;
  499. mmc_omap_release_slot(host->current_slot, 1);
  500. mmc_request_done(mmc, cmd->mrq);
  501. }
  502. }
  503. /*
  504. * Abort stuck command. Can occur when card is removed while it is being
  505. * read.
  506. */
  507. static void mmc_omap_abort_command(struct work_struct *work)
  508. {
  509. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  510. cmd_abort_work);
  511. BUG_ON(!host->cmd);
  512. dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
  513. host->cmd->opcode);
  514. if (host->cmd->error == 0)
  515. host->cmd->error = -ETIMEDOUT;
  516. if (host->data == NULL) {
  517. struct mmc_command *cmd;
  518. struct mmc_host *mmc;
  519. cmd = host->cmd;
  520. host->cmd = NULL;
  521. mmc_omap_send_abort(host, 10000);
  522. host->mrq = NULL;
  523. mmc = host->mmc;
  524. mmc_omap_release_slot(host->current_slot, 1);
  525. mmc_request_done(mmc, cmd->mrq);
  526. } else
  527. mmc_omap_cmd_done(host, host->cmd);
  528. host->abort = 0;
  529. enable_irq(host->irq);
  530. }
  531. static void
  532. mmc_omap_cmd_timer(unsigned long data)
  533. {
  534. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  535. unsigned long flags;
  536. spin_lock_irqsave(&host->slot_lock, flags);
  537. if (host->cmd != NULL && !host->abort) {
  538. OMAP_MMC_WRITE(host, IE, 0);
  539. disable_irq(host->irq);
  540. host->abort = 1;
  541. queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
  542. }
  543. spin_unlock_irqrestore(&host->slot_lock, flags);
  544. }
  545. /* PIO only */
  546. static void
  547. mmc_omap_sg_to_buf(struct mmc_omap_host *host)
  548. {
  549. struct scatterlist *sg;
  550. sg = host->data->sg + host->sg_idx;
  551. host->buffer_bytes_left = sg->length;
  552. host->buffer = sg_virt(sg);
  553. if (host->buffer_bytes_left > host->total_bytes_left)
  554. host->buffer_bytes_left = host->total_bytes_left;
  555. }
  556. static void
  557. mmc_omap_clk_timer(unsigned long data)
  558. {
  559. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  560. mmc_omap_fclk_enable(host, 0);
  561. }
  562. /* PIO only */
  563. static void
  564. mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
  565. {
  566. int n, nwords;
  567. if (host->buffer_bytes_left == 0) {
  568. host->sg_idx++;
  569. BUG_ON(host->sg_idx == host->sg_len);
  570. mmc_omap_sg_to_buf(host);
  571. }
  572. n = 64;
  573. if (n > host->buffer_bytes_left)
  574. n = host->buffer_bytes_left;
  575. nwords = n / 2;
  576. nwords += n & 1; /* handle odd number of bytes to transfer */
  577. host->buffer_bytes_left -= n;
  578. host->total_bytes_left -= n;
  579. host->data->bytes_xfered += n;
  580. if (write) {
  581. __raw_writesw(host->virt_base + OMAP_MMC_REG(host, DATA),
  582. host->buffer, nwords);
  583. } else {
  584. __raw_readsw(host->virt_base + OMAP_MMC_REG(host, DATA),
  585. host->buffer, nwords);
  586. }
  587. host->buffer += nwords;
  588. }
  589. static inline void mmc_omap_report_irq(u16 status)
  590. {
  591. static const char *mmc_omap_status_bits[] = {
  592. "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
  593. "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
  594. };
  595. int i, c = 0;
  596. for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
  597. if (status & (1 << i)) {
  598. if (c)
  599. printk(" ");
  600. printk("%s", mmc_omap_status_bits[i]);
  601. c++;
  602. }
  603. }
  604. static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
  605. {
  606. struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
  607. u16 status;
  608. int end_command;
  609. int end_transfer;
  610. int transfer_error, cmd_error;
  611. if (host->cmd == NULL && host->data == NULL) {
  612. status = OMAP_MMC_READ(host, STAT);
  613. dev_info(mmc_dev(host->slots[0]->mmc),
  614. "Spurious IRQ 0x%04x\n", status);
  615. if (status != 0) {
  616. OMAP_MMC_WRITE(host, STAT, status);
  617. OMAP_MMC_WRITE(host, IE, 0);
  618. }
  619. return IRQ_HANDLED;
  620. }
  621. end_command = 0;
  622. end_transfer = 0;
  623. transfer_error = 0;
  624. cmd_error = 0;
  625. while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
  626. int cmd;
  627. OMAP_MMC_WRITE(host, STAT, status);
  628. if (host->cmd != NULL)
  629. cmd = host->cmd->opcode;
  630. else
  631. cmd = -1;
  632. #ifdef CONFIG_MMC_DEBUG
  633. dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
  634. status, cmd);
  635. mmc_omap_report_irq(status);
  636. printk("\n");
  637. #endif
  638. if (host->total_bytes_left) {
  639. if ((status & OMAP_MMC_STAT_A_FULL) ||
  640. (status & OMAP_MMC_STAT_END_OF_DATA))
  641. mmc_omap_xfer_data(host, 0);
  642. if (status & OMAP_MMC_STAT_A_EMPTY)
  643. mmc_omap_xfer_data(host, 1);
  644. }
  645. if (status & OMAP_MMC_STAT_END_OF_DATA)
  646. end_transfer = 1;
  647. if (status & OMAP_MMC_STAT_DATA_TOUT) {
  648. dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
  649. cmd);
  650. if (host->data) {
  651. host->data->error = -ETIMEDOUT;
  652. transfer_error = 1;
  653. }
  654. }
  655. if (status & OMAP_MMC_STAT_DATA_CRC) {
  656. if (host->data) {
  657. host->data->error = -EILSEQ;
  658. dev_dbg(mmc_dev(host->mmc),
  659. "data CRC error, bytes left %d\n",
  660. host->total_bytes_left);
  661. transfer_error = 1;
  662. } else {
  663. dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
  664. }
  665. }
  666. if (status & OMAP_MMC_STAT_CMD_TOUT) {
  667. /* Timeouts are routine with some commands */
  668. if (host->cmd) {
  669. struct mmc_omap_slot *slot =
  670. host->current_slot;
  671. if (slot == NULL ||
  672. !mmc_omap_cover_is_open(slot))
  673. dev_err(mmc_dev(host->mmc),
  674. "command timeout (CMD%d)\n",
  675. cmd);
  676. host->cmd->error = -ETIMEDOUT;
  677. end_command = 1;
  678. cmd_error = 1;
  679. }
  680. }
  681. if (status & OMAP_MMC_STAT_CMD_CRC) {
  682. if (host->cmd) {
  683. dev_err(mmc_dev(host->mmc),
  684. "command CRC error (CMD%d, arg 0x%08x)\n",
  685. cmd, host->cmd->arg);
  686. host->cmd->error = -EILSEQ;
  687. end_command = 1;
  688. cmd_error = 1;
  689. } else
  690. dev_err(mmc_dev(host->mmc),
  691. "command CRC error without cmd?\n");
  692. }
  693. if (status & OMAP_MMC_STAT_CARD_ERR) {
  694. dev_dbg(mmc_dev(host->mmc),
  695. "ignoring card status error (CMD%d)\n",
  696. cmd);
  697. end_command = 1;
  698. }
  699. /*
  700. * NOTE: On 1610 the END_OF_CMD may come too early when
  701. * starting a write
  702. */
  703. if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
  704. (!(status & OMAP_MMC_STAT_A_EMPTY))) {
  705. end_command = 1;
  706. }
  707. }
  708. if (cmd_error && host->data) {
  709. del_timer(&host->cmd_abort_timer);
  710. host->abort = 1;
  711. OMAP_MMC_WRITE(host, IE, 0);
  712. disable_irq_nosync(host->irq);
  713. queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
  714. return IRQ_HANDLED;
  715. }
  716. if (end_command && host->cmd)
  717. mmc_omap_cmd_done(host, host->cmd);
  718. if (host->data != NULL) {
  719. if (transfer_error)
  720. mmc_omap_xfer_done(host, host->data);
  721. else if (end_transfer)
  722. mmc_omap_end_of_data(host, host->data);
  723. }
  724. return IRQ_HANDLED;
  725. }
  726. void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
  727. {
  728. int cover_open;
  729. struct mmc_omap_host *host = dev_get_drvdata(dev);
  730. struct mmc_omap_slot *slot = host->slots[num];
  731. BUG_ON(num >= host->nr_slots);
  732. /* Other subsystems can call in here before we're initialised. */
  733. if (host->nr_slots == 0 || !host->slots[num])
  734. return;
  735. cover_open = mmc_omap_cover_is_open(slot);
  736. if (cover_open != slot->cover_open) {
  737. slot->cover_open = cover_open;
  738. sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
  739. }
  740. tasklet_hi_schedule(&slot->cover_tasklet);
  741. }
  742. static void mmc_omap_cover_timer(unsigned long arg)
  743. {
  744. struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
  745. tasklet_schedule(&slot->cover_tasklet);
  746. }
  747. static void mmc_omap_cover_handler(unsigned long param)
  748. {
  749. struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
  750. int cover_open = mmc_omap_cover_is_open(slot);
  751. mmc_detect_change(slot->mmc, 0);
  752. if (!cover_open)
  753. return;
  754. /*
  755. * If no card is inserted, we postpone polling until
  756. * the cover has been closed.
  757. */
  758. if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
  759. return;
  760. mod_timer(&slot->cover_timer,
  761. jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
  762. }
  763. static void mmc_omap_dma_callback(void *priv)
  764. {
  765. struct mmc_omap_host *host = priv;
  766. struct mmc_data *data = host->data;
  767. /* If we got to the end of DMA, assume everything went well */
  768. data->bytes_xfered += data->blocks * data->blksz;
  769. mmc_omap_dma_done(host, data);
  770. }
  771. static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  772. {
  773. u16 reg;
  774. reg = OMAP_MMC_READ(host, SDIO);
  775. reg &= ~(1 << 5);
  776. OMAP_MMC_WRITE(host, SDIO, reg);
  777. /* Set maximum timeout */
  778. OMAP_MMC_WRITE(host, CTO, 0xff);
  779. }
  780. static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  781. {
  782. unsigned int timeout, cycle_ns;
  783. u16 reg;
  784. cycle_ns = 1000000000 / host->current_slot->fclk_freq;
  785. timeout = req->data->timeout_ns / cycle_ns;
  786. timeout += req->data->timeout_clks;
  787. /* Check if we need to use timeout multiplier register */
  788. reg = OMAP_MMC_READ(host, SDIO);
  789. if (timeout > 0xffff) {
  790. reg |= (1 << 5);
  791. timeout /= 1024;
  792. } else
  793. reg &= ~(1 << 5);
  794. OMAP_MMC_WRITE(host, SDIO, reg);
  795. OMAP_MMC_WRITE(host, DTO, timeout);
  796. }
  797. static void
  798. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  799. {
  800. struct mmc_data *data = req->data;
  801. int i, use_dma, block_size;
  802. unsigned sg_len;
  803. host->data = data;
  804. if (data == NULL) {
  805. OMAP_MMC_WRITE(host, BLEN, 0);
  806. OMAP_MMC_WRITE(host, NBLK, 0);
  807. OMAP_MMC_WRITE(host, BUF, 0);
  808. host->dma_in_use = 0;
  809. set_cmd_timeout(host, req);
  810. return;
  811. }
  812. block_size = data->blksz;
  813. OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
  814. OMAP_MMC_WRITE(host, BLEN, block_size - 1);
  815. set_data_timeout(host, req);
  816. /* cope with calling layer confusion; it issues "single
  817. * block" writes using multi-block scatterlists.
  818. */
  819. sg_len = (data->blocks == 1) ? 1 : data->sg_len;
  820. /* Only do DMA for entire blocks */
  821. use_dma = host->use_dma;
  822. if (use_dma) {
  823. for (i = 0; i < sg_len; i++) {
  824. if ((data->sg[i].length % block_size) != 0) {
  825. use_dma = 0;
  826. break;
  827. }
  828. }
  829. }
  830. host->sg_idx = 0;
  831. if (use_dma) {
  832. enum dma_data_direction dma_data_dir;
  833. struct dma_async_tx_descriptor *tx;
  834. struct dma_chan *c;
  835. u32 burst, *bp;
  836. u16 buf;
  837. /*
  838. * FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx
  839. * and 24xx. Use 16 or 32 word frames when the
  840. * blocksize is at least that large. Blocksize is
  841. * usually 512 bytes; but not for some SD reads.
  842. */
  843. burst = cpu_is_omap15xx() ? 32 : 64;
  844. if (burst > data->blksz)
  845. burst = data->blksz;
  846. burst >>= 1;
  847. if (data->flags & MMC_DATA_WRITE) {
  848. c = host->dma_tx;
  849. bp = &host->dma_tx_burst;
  850. buf = 0x0f80 | (burst - 1) << 0;
  851. dma_data_dir = DMA_TO_DEVICE;
  852. } else {
  853. c = host->dma_rx;
  854. bp = &host->dma_rx_burst;
  855. buf = 0x800f | (burst - 1) << 8;
  856. dma_data_dir = DMA_FROM_DEVICE;
  857. }
  858. if (!c)
  859. goto use_pio;
  860. /* Only reconfigure if we have a different burst size */
  861. if (*bp != burst) {
  862. struct dma_slave_config cfg;
  863. cfg.src_addr = host->phys_base + OMAP_MMC_REG(host, DATA);
  864. cfg.dst_addr = host->phys_base + OMAP_MMC_REG(host, DATA);
  865. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  866. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  867. cfg.src_maxburst = burst;
  868. cfg.dst_maxburst = burst;
  869. if (dmaengine_slave_config(c, &cfg))
  870. goto use_pio;
  871. *bp = burst;
  872. }
  873. host->sg_len = dma_map_sg(c->device->dev, data->sg, sg_len,
  874. dma_data_dir);
  875. if (host->sg_len == 0)
  876. goto use_pio;
  877. tx = dmaengine_prep_slave_sg(c, data->sg, host->sg_len,
  878. data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  879. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  880. if (!tx)
  881. goto use_pio;
  882. OMAP_MMC_WRITE(host, BUF, buf);
  883. tx->callback = mmc_omap_dma_callback;
  884. tx->callback_param = host;
  885. dmaengine_submit(tx);
  886. host->brs_received = 0;
  887. host->dma_done = 0;
  888. host->dma_in_use = 1;
  889. return;
  890. }
  891. use_pio:
  892. /* Revert to PIO? */
  893. OMAP_MMC_WRITE(host, BUF, 0x1f1f);
  894. host->total_bytes_left = data->blocks * block_size;
  895. host->sg_len = sg_len;
  896. mmc_omap_sg_to_buf(host);
  897. host->dma_in_use = 0;
  898. }
  899. static void mmc_omap_start_request(struct mmc_omap_host *host,
  900. struct mmc_request *req)
  901. {
  902. BUG_ON(host->mrq != NULL);
  903. host->mrq = req;
  904. /* only touch fifo AFTER the controller readies it */
  905. mmc_omap_prepare_data(host, req);
  906. mmc_omap_start_command(host, req->cmd);
  907. if (host->dma_in_use) {
  908. struct dma_chan *c = host->data->flags & MMC_DATA_WRITE ?
  909. host->dma_tx : host->dma_rx;
  910. dma_async_issue_pending(c);
  911. }
  912. }
  913. static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
  914. {
  915. struct mmc_omap_slot *slot = mmc_priv(mmc);
  916. struct mmc_omap_host *host = slot->host;
  917. unsigned long flags;
  918. spin_lock_irqsave(&host->slot_lock, flags);
  919. if (host->mmc != NULL) {
  920. BUG_ON(slot->mrq != NULL);
  921. slot->mrq = req;
  922. spin_unlock_irqrestore(&host->slot_lock, flags);
  923. return;
  924. } else
  925. host->mmc = mmc;
  926. spin_unlock_irqrestore(&host->slot_lock, flags);
  927. mmc_omap_select_slot(slot, 1);
  928. mmc_omap_start_request(host, req);
  929. }
  930. static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
  931. int vdd)
  932. {
  933. struct mmc_omap_host *host;
  934. host = slot->host;
  935. if (slot->pdata->set_power != NULL)
  936. slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
  937. vdd);
  938. if (cpu_is_omap24xx()) {
  939. u16 w;
  940. if (power_on) {
  941. w = OMAP_MMC_READ(host, CON);
  942. OMAP_MMC_WRITE(host, CON, w | (1 << 11));
  943. } else {
  944. w = OMAP_MMC_READ(host, CON);
  945. OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
  946. }
  947. }
  948. }
  949. static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
  950. {
  951. struct mmc_omap_slot *slot = mmc_priv(mmc);
  952. struct mmc_omap_host *host = slot->host;
  953. int func_clk_rate = clk_get_rate(host->fclk);
  954. int dsor;
  955. if (ios->clock == 0)
  956. return 0;
  957. dsor = func_clk_rate / ios->clock;
  958. if (dsor < 1)
  959. dsor = 1;
  960. if (func_clk_rate / dsor > ios->clock)
  961. dsor++;
  962. if (dsor > 250)
  963. dsor = 250;
  964. slot->fclk_freq = func_clk_rate / dsor;
  965. if (ios->bus_width == MMC_BUS_WIDTH_4)
  966. dsor |= 1 << 15;
  967. return dsor;
  968. }
  969. static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  970. {
  971. struct mmc_omap_slot *slot = mmc_priv(mmc);
  972. struct mmc_omap_host *host = slot->host;
  973. int i, dsor;
  974. int clk_enabled;
  975. mmc_omap_select_slot(slot, 0);
  976. dsor = mmc_omap_calc_divisor(mmc, ios);
  977. if (ios->vdd != slot->vdd)
  978. slot->vdd = ios->vdd;
  979. clk_enabled = 0;
  980. switch (ios->power_mode) {
  981. case MMC_POWER_OFF:
  982. mmc_omap_set_power(slot, 0, ios->vdd);
  983. break;
  984. case MMC_POWER_UP:
  985. /* Cannot touch dsor yet, just power up MMC */
  986. mmc_omap_set_power(slot, 1, ios->vdd);
  987. goto exit;
  988. case MMC_POWER_ON:
  989. mmc_omap_fclk_enable(host, 1);
  990. clk_enabled = 1;
  991. dsor |= 1 << 11;
  992. break;
  993. }
  994. if (slot->bus_mode != ios->bus_mode) {
  995. if (slot->pdata->set_bus_mode != NULL)
  996. slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
  997. ios->bus_mode);
  998. slot->bus_mode = ios->bus_mode;
  999. }
  1000. /* On insanely high arm_per frequencies something sometimes
  1001. * goes somehow out of sync, and the POW bit is not being set,
  1002. * which results in the while loop below getting stuck.
  1003. * Writing to the CON register twice seems to do the trick. */
  1004. for (i = 0; i < 2; i++)
  1005. OMAP_MMC_WRITE(host, CON, dsor);
  1006. slot->saved_con = dsor;
  1007. if (ios->power_mode == MMC_POWER_ON) {
  1008. /* worst case at 400kHz, 80 cycles makes 200 microsecs */
  1009. int usecs = 250;
  1010. /* Send clock cycles, poll completion */
  1011. OMAP_MMC_WRITE(host, IE, 0);
  1012. OMAP_MMC_WRITE(host, STAT, 0xffff);
  1013. OMAP_MMC_WRITE(host, CMD, 1 << 7);
  1014. while (usecs > 0 && (OMAP_MMC_READ(host, STAT) & 1) == 0) {
  1015. udelay(1);
  1016. usecs--;
  1017. }
  1018. OMAP_MMC_WRITE(host, STAT, 1);
  1019. }
  1020. exit:
  1021. mmc_omap_release_slot(slot, clk_enabled);
  1022. }
  1023. static const struct mmc_host_ops mmc_omap_ops = {
  1024. .request = mmc_omap_request,
  1025. .set_ios = mmc_omap_set_ios,
  1026. };
  1027. static int __devinit mmc_omap_new_slot(struct mmc_omap_host *host, int id)
  1028. {
  1029. struct mmc_omap_slot *slot = NULL;
  1030. struct mmc_host *mmc;
  1031. int r;
  1032. mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
  1033. if (mmc == NULL)
  1034. return -ENOMEM;
  1035. slot = mmc_priv(mmc);
  1036. slot->host = host;
  1037. slot->mmc = mmc;
  1038. slot->id = id;
  1039. slot->pdata = &host->pdata->slots[id];
  1040. host->slots[id] = slot;
  1041. mmc->caps = 0;
  1042. if (host->pdata->slots[id].wires >= 4)
  1043. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1044. mmc->ops = &mmc_omap_ops;
  1045. mmc->f_min = 400000;
  1046. if (cpu_class_is_omap2())
  1047. mmc->f_max = 48000000;
  1048. else
  1049. mmc->f_max = 24000000;
  1050. if (host->pdata->max_freq)
  1051. mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
  1052. mmc->ocr_avail = slot->pdata->ocr_mask;
  1053. /* Use scatterlist DMA to reduce per-transfer costs.
  1054. * NOTE max_seg_size assumption that small blocks aren't
  1055. * normally used (except e.g. for reading SD registers).
  1056. */
  1057. mmc->max_segs = 32;
  1058. mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
  1059. mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
  1060. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1061. mmc->max_seg_size = mmc->max_req_size;
  1062. r = mmc_add_host(mmc);
  1063. if (r < 0)
  1064. goto err_remove_host;
  1065. if (slot->pdata->name != NULL) {
  1066. r = device_create_file(&mmc->class_dev,
  1067. &dev_attr_slot_name);
  1068. if (r < 0)
  1069. goto err_remove_host;
  1070. }
  1071. if (slot->pdata->get_cover_state != NULL) {
  1072. r = device_create_file(&mmc->class_dev,
  1073. &dev_attr_cover_switch);
  1074. if (r < 0)
  1075. goto err_remove_slot_name;
  1076. setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
  1077. (unsigned long)slot);
  1078. tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
  1079. (unsigned long)slot);
  1080. tasklet_schedule(&slot->cover_tasklet);
  1081. }
  1082. return 0;
  1083. err_remove_slot_name:
  1084. if (slot->pdata->name != NULL)
  1085. device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
  1086. err_remove_host:
  1087. mmc_remove_host(mmc);
  1088. mmc_free_host(mmc);
  1089. return r;
  1090. }
  1091. static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
  1092. {
  1093. struct mmc_host *mmc = slot->mmc;
  1094. if (slot->pdata->name != NULL)
  1095. device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
  1096. if (slot->pdata->get_cover_state != NULL)
  1097. device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
  1098. tasklet_kill(&slot->cover_tasklet);
  1099. del_timer_sync(&slot->cover_timer);
  1100. flush_workqueue(slot->host->mmc_omap_wq);
  1101. mmc_remove_host(mmc);
  1102. mmc_free_host(mmc);
  1103. }
  1104. static int __devinit mmc_omap_probe(struct platform_device *pdev)
  1105. {
  1106. struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
  1107. struct mmc_omap_host *host = NULL;
  1108. struct resource *res;
  1109. dma_cap_mask_t mask;
  1110. unsigned sig;
  1111. int i, ret = 0;
  1112. int irq;
  1113. if (pdata == NULL) {
  1114. dev_err(&pdev->dev, "platform data missing\n");
  1115. return -ENXIO;
  1116. }
  1117. if (pdata->nr_slots == 0) {
  1118. dev_err(&pdev->dev, "no slots\n");
  1119. return -ENXIO;
  1120. }
  1121. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1122. irq = platform_get_irq(pdev, 0);
  1123. if (res == NULL || irq < 0)
  1124. return -ENXIO;
  1125. res = request_mem_region(res->start, resource_size(res),
  1126. pdev->name);
  1127. if (res == NULL)
  1128. return -EBUSY;
  1129. host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
  1130. if (host == NULL) {
  1131. ret = -ENOMEM;
  1132. goto err_free_mem_region;
  1133. }
  1134. INIT_WORK(&host->slot_release_work, mmc_omap_slot_release_work);
  1135. INIT_WORK(&host->send_stop_work, mmc_omap_send_stop_work);
  1136. INIT_WORK(&host->cmd_abort_work, mmc_omap_abort_command);
  1137. setup_timer(&host->cmd_abort_timer, mmc_omap_cmd_timer,
  1138. (unsigned long) host);
  1139. spin_lock_init(&host->clk_lock);
  1140. setup_timer(&host->clk_timer, mmc_omap_clk_timer, (unsigned long) host);
  1141. spin_lock_init(&host->dma_lock);
  1142. spin_lock_init(&host->slot_lock);
  1143. init_waitqueue_head(&host->slot_wq);
  1144. host->pdata = pdata;
  1145. host->dev = &pdev->dev;
  1146. platform_set_drvdata(pdev, host);
  1147. host->id = pdev->id;
  1148. host->mem_res = res;
  1149. host->irq = irq;
  1150. host->use_dma = 1;
  1151. host->irq = irq;
  1152. host->phys_base = host->mem_res->start;
  1153. host->virt_base = ioremap(res->start, resource_size(res));
  1154. if (!host->virt_base)
  1155. goto err_ioremap;
  1156. host->iclk = clk_get(&pdev->dev, "ick");
  1157. if (IS_ERR(host->iclk)) {
  1158. ret = PTR_ERR(host->iclk);
  1159. goto err_free_mmc_host;
  1160. }
  1161. clk_enable(host->iclk);
  1162. host->fclk = clk_get(&pdev->dev, "fck");
  1163. if (IS_ERR(host->fclk)) {
  1164. ret = PTR_ERR(host->fclk);
  1165. goto err_free_iclk;
  1166. }
  1167. dma_cap_zero(mask);
  1168. dma_cap_set(DMA_SLAVE, mask);
  1169. host->dma_tx_burst = -1;
  1170. host->dma_rx_burst = -1;
  1171. if (cpu_is_omap24xx())
  1172. sig = host->id == 0 ? OMAP24XX_DMA_MMC1_TX : OMAP24XX_DMA_MMC2_TX;
  1173. else
  1174. sig = host->id == 0 ? OMAP_DMA_MMC_TX : OMAP_DMA_MMC2_TX;
  1175. host->dma_tx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  1176. #if 0
  1177. if (!host->dma_tx) {
  1178. dev_err(host->dev, "unable to obtain TX DMA engine channel %u\n",
  1179. sig);
  1180. goto err_dma;
  1181. }
  1182. #else
  1183. if (!host->dma_tx)
  1184. dev_warn(host->dev, "unable to obtain TX DMA engine channel %u\n",
  1185. sig);
  1186. #endif
  1187. if (cpu_is_omap24xx())
  1188. sig = host->id == 0 ? OMAP24XX_DMA_MMC1_RX : OMAP24XX_DMA_MMC2_RX;
  1189. else
  1190. sig = host->id == 0 ? OMAP_DMA_MMC_RX : OMAP_DMA_MMC2_RX;
  1191. host->dma_rx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  1192. #if 0
  1193. if (!host->dma_rx) {
  1194. dev_err(host->dev, "unable to obtain RX DMA engine channel %u\n",
  1195. sig);
  1196. goto err_dma;
  1197. }
  1198. #else
  1199. if (!host->dma_rx)
  1200. dev_warn(host->dev, "unable to obtain RX DMA engine channel %u\n",
  1201. sig);
  1202. #endif
  1203. ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
  1204. if (ret)
  1205. goto err_free_dma;
  1206. if (pdata->init != NULL) {
  1207. ret = pdata->init(&pdev->dev);
  1208. if (ret < 0)
  1209. goto err_free_irq;
  1210. }
  1211. host->nr_slots = pdata->nr_slots;
  1212. host->reg_shift = (cpu_is_omap7xx() ? 1 : 2);
  1213. host->mmc_omap_wq = alloc_workqueue("mmc_omap", 0, 0);
  1214. if (!host->mmc_omap_wq)
  1215. goto err_plat_cleanup;
  1216. for (i = 0; i < pdata->nr_slots; i++) {
  1217. ret = mmc_omap_new_slot(host, i);
  1218. if (ret < 0) {
  1219. while (--i >= 0)
  1220. mmc_omap_remove_slot(host->slots[i]);
  1221. goto err_destroy_wq;
  1222. }
  1223. }
  1224. return 0;
  1225. err_destroy_wq:
  1226. destroy_workqueue(host->mmc_omap_wq);
  1227. err_plat_cleanup:
  1228. if (pdata->cleanup)
  1229. pdata->cleanup(&pdev->dev);
  1230. err_free_irq:
  1231. free_irq(host->irq, host);
  1232. err_free_dma:
  1233. if (host->dma_tx)
  1234. dma_release_channel(host->dma_tx);
  1235. if (host->dma_rx)
  1236. dma_release_channel(host->dma_rx);
  1237. clk_put(host->fclk);
  1238. err_free_iclk:
  1239. clk_disable(host->iclk);
  1240. clk_put(host->iclk);
  1241. err_free_mmc_host:
  1242. iounmap(host->virt_base);
  1243. err_ioremap:
  1244. kfree(host);
  1245. err_free_mem_region:
  1246. release_mem_region(res->start, resource_size(res));
  1247. return ret;
  1248. }
  1249. static int __devexit mmc_omap_remove(struct platform_device *pdev)
  1250. {
  1251. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1252. int i;
  1253. platform_set_drvdata(pdev, NULL);
  1254. BUG_ON(host == NULL);
  1255. for (i = 0; i < host->nr_slots; i++)
  1256. mmc_omap_remove_slot(host->slots[i]);
  1257. if (host->pdata->cleanup)
  1258. host->pdata->cleanup(&pdev->dev);
  1259. mmc_omap_fclk_enable(host, 0);
  1260. free_irq(host->irq, host);
  1261. clk_put(host->fclk);
  1262. clk_disable(host->iclk);
  1263. clk_put(host->iclk);
  1264. if (host->dma_tx)
  1265. dma_release_channel(host->dma_tx);
  1266. if (host->dma_rx)
  1267. dma_release_channel(host->dma_rx);
  1268. iounmap(host->virt_base);
  1269. release_mem_region(pdev->resource[0].start,
  1270. pdev->resource[0].end - pdev->resource[0].start + 1);
  1271. destroy_workqueue(host->mmc_omap_wq);
  1272. kfree(host);
  1273. return 0;
  1274. }
  1275. #ifdef CONFIG_PM
  1276. static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
  1277. {
  1278. int i, ret = 0;
  1279. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1280. if (host == NULL || host->suspended)
  1281. return 0;
  1282. for (i = 0; i < host->nr_slots; i++) {
  1283. struct mmc_omap_slot *slot;
  1284. slot = host->slots[i];
  1285. ret = mmc_suspend_host(slot->mmc);
  1286. if (ret < 0) {
  1287. while (--i >= 0) {
  1288. slot = host->slots[i];
  1289. mmc_resume_host(slot->mmc);
  1290. }
  1291. return ret;
  1292. }
  1293. }
  1294. host->suspended = 1;
  1295. return 0;
  1296. }
  1297. static int mmc_omap_resume(struct platform_device *pdev)
  1298. {
  1299. int i, ret = 0;
  1300. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1301. if (host == NULL || !host->suspended)
  1302. return 0;
  1303. for (i = 0; i < host->nr_slots; i++) {
  1304. struct mmc_omap_slot *slot;
  1305. slot = host->slots[i];
  1306. ret = mmc_resume_host(slot->mmc);
  1307. if (ret < 0)
  1308. return ret;
  1309. host->suspended = 0;
  1310. }
  1311. return 0;
  1312. }
  1313. #else
  1314. #define mmc_omap_suspend NULL
  1315. #define mmc_omap_resume NULL
  1316. #endif
  1317. static struct platform_driver mmc_omap_driver = {
  1318. .probe = mmc_omap_probe,
  1319. .remove = __devexit_p(mmc_omap_remove),
  1320. .suspend = mmc_omap_suspend,
  1321. .resume = mmc_omap_resume,
  1322. .driver = {
  1323. .name = DRIVER_NAME,
  1324. .owner = THIS_MODULE,
  1325. },
  1326. };
  1327. module_platform_driver(mmc_omap_driver);
  1328. MODULE_DESCRIPTION("OMAP Multimedia Card driver");
  1329. MODULE_LICENSE("GPL");
  1330. MODULE_ALIAS("platform:" DRIVER_NAME);
  1331. MODULE_AUTHOR("Juha Yrjölä");