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