omap.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  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/of.h>
  25. #include <linux/mmc/host.h>
  26. #include <linux/mmc/card.h>
  27. #include <linux/mmc/mmc.h>
  28. #include <linux/clk.h>
  29. #include <linux/scatterlist.h>
  30. #include <linux/slab.h>
  31. #include <linux/platform_data/mmc-omap.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 mmc_omap7xx() (host->features & MMC_OMAP7XX)
  71. #define mmc_omap15xx() (host->features & MMC_OMAP15XX)
  72. #define mmc_omap16xx() (host->features & MMC_OMAP16XX)
  73. #define MMC_OMAP1_MASK (MMC_OMAP7XX | MMC_OMAP15XX | MMC_OMAP16XX)
  74. #define mmc_omap1() (host->features & MMC_OMAP1_MASK)
  75. #define mmc_omap2() (!mmc_omap1())
  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. struct tasklet_struct cover_tasklet;
  98. struct timer_list cover_timer;
  99. unsigned cover_open;
  100. struct mmc_request *mrq;
  101. struct mmc_omap_host *host;
  102. struct mmc_host *mmc;
  103. struct omap_mmc_slot_data *pdata;
  104. };
  105. struct mmc_omap_host {
  106. int initialized;
  107. struct mmc_request * mrq;
  108. struct mmc_command * cmd;
  109. struct mmc_data * data;
  110. struct mmc_host * mmc;
  111. struct device * dev;
  112. unsigned char id; /* 16xx chips have 2 MMC blocks */
  113. struct clk * iclk;
  114. struct clk * fclk;
  115. struct dma_chan *dma_rx;
  116. u32 dma_rx_burst;
  117. struct dma_chan *dma_tx;
  118. u32 dma_tx_burst;
  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 features;
  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 = DIV_ROUND_UP(NSEC_PER_SEC, 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. u16 irq_mask;
  288. host->cmd = cmd;
  289. resptype = 0;
  290. cmdtype = 0;
  291. /* Our hardware needs to know exact type */
  292. switch (mmc_resp_type(cmd)) {
  293. case MMC_RSP_NONE:
  294. break;
  295. case MMC_RSP_R1:
  296. case MMC_RSP_R1B:
  297. /* resp 1, 1b, 6, 7 */
  298. resptype = 1;
  299. break;
  300. case MMC_RSP_R2:
  301. resptype = 2;
  302. break;
  303. case MMC_RSP_R3:
  304. resptype = 3;
  305. break;
  306. default:
  307. dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
  308. break;
  309. }
  310. if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
  311. cmdtype = OMAP_MMC_CMDTYPE_ADTC;
  312. } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
  313. cmdtype = OMAP_MMC_CMDTYPE_BC;
  314. } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
  315. cmdtype = OMAP_MMC_CMDTYPE_BCR;
  316. } else {
  317. cmdtype = OMAP_MMC_CMDTYPE_AC;
  318. }
  319. cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
  320. if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
  321. cmdreg |= 1 << 6;
  322. if (cmd->flags & MMC_RSP_BUSY)
  323. cmdreg |= 1 << 11;
  324. if (host->data && !(host->data->flags & MMC_DATA_WRITE))
  325. cmdreg |= 1 << 15;
  326. mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
  327. OMAP_MMC_WRITE(host, CTO, 200);
  328. OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
  329. OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
  330. irq_mask = 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. if (cmd->opcode == MMC_ERASE)
  336. irq_mask &= ~OMAP_MMC_STAT_DATA_TOUT;
  337. OMAP_MMC_WRITE(host, IE, irq_mask);
  338. OMAP_MMC_WRITE(host, CMD, cmdreg);
  339. }
  340. static void
  341. mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
  342. int abort)
  343. {
  344. enum dma_data_direction dma_data_dir;
  345. struct device *dev = mmc_dev(host->mmc);
  346. struct dma_chan *c;
  347. if (data->flags & MMC_DATA_WRITE) {
  348. dma_data_dir = DMA_TO_DEVICE;
  349. c = host->dma_tx;
  350. } else {
  351. dma_data_dir = DMA_FROM_DEVICE;
  352. c = host->dma_rx;
  353. }
  354. if (c) {
  355. if (data->error) {
  356. dmaengine_terminate_all(c);
  357. /* Claim nothing transferred on error... */
  358. data->bytes_xfered = 0;
  359. }
  360. dev = c->device->dev;
  361. }
  362. dma_unmap_sg(dev, data->sg, host->sg_len, dma_data_dir);
  363. }
  364. static void mmc_omap_send_stop_work(struct work_struct *work)
  365. {
  366. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  367. send_stop_work);
  368. struct mmc_omap_slot *slot = host->current_slot;
  369. struct mmc_data *data = host->stop_data;
  370. unsigned long tick_ns;
  371. tick_ns = DIV_ROUND_UP(NSEC_PER_SEC, slot->fclk_freq);
  372. ndelay(8*tick_ns);
  373. mmc_omap_start_command(host, data->stop);
  374. }
  375. static void
  376. mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
  377. {
  378. if (host->dma_in_use)
  379. mmc_omap_release_dma(host, data, data->error);
  380. host->data = NULL;
  381. host->sg_len = 0;
  382. /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
  383. * dozens of requests until the card finishes writing data.
  384. * It'd be cheaper to just wait till an EOFB interrupt arrives...
  385. */
  386. if (!data->stop) {
  387. struct mmc_host *mmc;
  388. host->mrq = NULL;
  389. mmc = host->mmc;
  390. mmc_omap_release_slot(host->current_slot, 1);
  391. mmc_request_done(mmc, data->mrq);
  392. return;
  393. }
  394. host->stop_data = data;
  395. queue_work(host->mmc_omap_wq, &host->send_stop_work);
  396. }
  397. static void
  398. mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
  399. {
  400. struct mmc_omap_slot *slot = host->current_slot;
  401. unsigned int restarts, passes, timeout;
  402. u16 stat = 0;
  403. /* Sending abort takes 80 clocks. Have some extra and round up */
  404. timeout = DIV_ROUND_UP(120 * USEC_PER_SEC, slot->fclk_freq);
  405. restarts = 0;
  406. while (restarts < maxloops) {
  407. OMAP_MMC_WRITE(host, STAT, 0xFFFF);
  408. OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
  409. passes = 0;
  410. while (passes < timeout) {
  411. stat = OMAP_MMC_READ(host, STAT);
  412. if (stat & OMAP_MMC_STAT_END_OF_CMD)
  413. goto out;
  414. udelay(1);
  415. passes++;
  416. }
  417. restarts++;
  418. }
  419. out:
  420. OMAP_MMC_WRITE(host, STAT, stat);
  421. }
  422. static void
  423. mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
  424. {
  425. if (host->dma_in_use)
  426. mmc_omap_release_dma(host, data, 1);
  427. host->data = NULL;
  428. host->sg_len = 0;
  429. mmc_omap_send_abort(host, 10000);
  430. }
  431. static void
  432. mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
  433. {
  434. unsigned long flags;
  435. int done;
  436. if (!host->dma_in_use) {
  437. mmc_omap_xfer_done(host, data);
  438. return;
  439. }
  440. done = 0;
  441. spin_lock_irqsave(&host->dma_lock, flags);
  442. if (host->dma_done)
  443. done = 1;
  444. else
  445. host->brs_received = 1;
  446. spin_unlock_irqrestore(&host->dma_lock, flags);
  447. if (done)
  448. mmc_omap_xfer_done(host, data);
  449. }
  450. static void
  451. mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
  452. {
  453. unsigned long flags;
  454. int done;
  455. done = 0;
  456. spin_lock_irqsave(&host->dma_lock, flags);
  457. if (host->brs_received)
  458. done = 1;
  459. else
  460. host->dma_done = 1;
  461. spin_unlock_irqrestore(&host->dma_lock, flags);
  462. if (done)
  463. mmc_omap_xfer_done(host, data);
  464. }
  465. static void
  466. mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
  467. {
  468. host->cmd = NULL;
  469. del_timer(&host->cmd_abort_timer);
  470. if (cmd->flags & MMC_RSP_PRESENT) {
  471. if (cmd->flags & MMC_RSP_136) {
  472. /* response type 2 */
  473. cmd->resp[3] =
  474. OMAP_MMC_READ(host, RSP0) |
  475. (OMAP_MMC_READ(host, RSP1) << 16);
  476. cmd->resp[2] =
  477. OMAP_MMC_READ(host, RSP2) |
  478. (OMAP_MMC_READ(host, RSP3) << 16);
  479. cmd->resp[1] =
  480. OMAP_MMC_READ(host, RSP4) |
  481. (OMAP_MMC_READ(host, RSP5) << 16);
  482. cmd->resp[0] =
  483. OMAP_MMC_READ(host, RSP6) |
  484. (OMAP_MMC_READ(host, RSP7) << 16);
  485. } else {
  486. /* response types 1, 1b, 3, 4, 5, 6 */
  487. cmd->resp[0] =
  488. OMAP_MMC_READ(host, RSP6) |
  489. (OMAP_MMC_READ(host, RSP7) << 16);
  490. }
  491. }
  492. if (host->data == NULL || cmd->error) {
  493. struct mmc_host *mmc;
  494. if (host->data != NULL)
  495. mmc_omap_abort_xfer(host, host->data);
  496. host->mrq = NULL;
  497. mmc = host->mmc;
  498. mmc_omap_release_slot(host->current_slot, 1);
  499. mmc_request_done(mmc, cmd->mrq);
  500. }
  501. }
  502. /*
  503. * Abort stuck command. Can occur when card is removed while it is being
  504. * read.
  505. */
  506. static void mmc_omap_abort_command(struct work_struct *work)
  507. {
  508. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  509. cmd_abort_work);
  510. BUG_ON(!host->cmd);
  511. dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
  512. host->cmd->opcode);
  513. if (host->cmd->error == 0)
  514. host->cmd->error = -ETIMEDOUT;
  515. if (host->data == NULL) {
  516. struct mmc_command *cmd;
  517. struct mmc_host *mmc;
  518. cmd = host->cmd;
  519. host->cmd = NULL;
  520. mmc_omap_send_abort(host, 10000);
  521. host->mrq = NULL;
  522. mmc = host->mmc;
  523. mmc_omap_release_slot(host->current_slot, 1);
  524. mmc_request_done(mmc, cmd->mrq);
  525. } else
  526. mmc_omap_cmd_done(host, host->cmd);
  527. host->abort = 0;
  528. enable_irq(host->irq);
  529. }
  530. static void
  531. mmc_omap_cmd_timer(unsigned long data)
  532. {
  533. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  534. unsigned long flags;
  535. spin_lock_irqsave(&host->slot_lock, flags);
  536. if (host->cmd != NULL && !host->abort) {
  537. OMAP_MMC_WRITE(host, IE, 0);
  538. disable_irq(host->irq);
  539. host->abort = 1;
  540. queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
  541. }
  542. spin_unlock_irqrestore(&host->slot_lock, flags);
  543. }
  544. /* PIO only */
  545. static void
  546. mmc_omap_sg_to_buf(struct mmc_omap_host *host)
  547. {
  548. struct scatterlist *sg;
  549. sg = host->data->sg + host->sg_idx;
  550. host->buffer_bytes_left = sg->length;
  551. host->buffer = sg_virt(sg);
  552. if (host->buffer_bytes_left > host->total_bytes_left)
  553. host->buffer_bytes_left = host->total_bytes_left;
  554. }
  555. static void
  556. mmc_omap_clk_timer(unsigned long data)
  557. {
  558. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  559. mmc_omap_fclk_enable(host, 0);
  560. }
  561. /* PIO only */
  562. static void
  563. mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
  564. {
  565. int n, nwords;
  566. if (host->buffer_bytes_left == 0) {
  567. host->sg_idx++;
  568. BUG_ON(host->sg_idx == host->sg_len);
  569. mmc_omap_sg_to_buf(host);
  570. }
  571. n = 64;
  572. if (n > host->buffer_bytes_left)
  573. n = host->buffer_bytes_left;
  574. /* Round up to handle odd number of bytes to transfer */
  575. nwords = DIV_ROUND_UP(n, 2);
  576. host->buffer_bytes_left -= n;
  577. host->total_bytes_left -= n;
  578. host->data->bytes_xfered += n;
  579. if (write) {
  580. __raw_writesw(host->virt_base + OMAP_MMC_REG(host, DATA),
  581. host->buffer, nwords);
  582. } else {
  583. __raw_readsw(host->virt_base + OMAP_MMC_REG(host, DATA),
  584. host->buffer, nwords);
  585. }
  586. host->buffer += nwords;
  587. }
  588. #ifdef CONFIG_MMC_DEBUG
  589. static void mmc_omap_report_irq(struct mmc_omap_host *host, 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;
  596. char res[64], *buf = res;
  597. buf += sprintf(buf, "MMC IRQ 0x%x:", status);
  598. for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
  599. if (status & (1 << i))
  600. buf += sprintf(buf, " %s", mmc_omap_status_bits[i]);
  601. dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
  602. }
  603. #else
  604. static void mmc_omap_report_irq(struct mmc_omap_host *host, u16 status)
  605. {
  606. }
  607. #endif
  608. static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
  609. {
  610. struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
  611. u16 status;
  612. int end_command;
  613. int end_transfer;
  614. int transfer_error, cmd_error;
  615. if (host->cmd == NULL && host->data == NULL) {
  616. status = OMAP_MMC_READ(host, STAT);
  617. dev_info(mmc_dev(host->slots[0]->mmc),
  618. "Spurious IRQ 0x%04x\n", status);
  619. if (status != 0) {
  620. OMAP_MMC_WRITE(host, STAT, status);
  621. OMAP_MMC_WRITE(host, IE, 0);
  622. }
  623. return IRQ_HANDLED;
  624. }
  625. end_command = 0;
  626. end_transfer = 0;
  627. transfer_error = 0;
  628. cmd_error = 0;
  629. while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
  630. int cmd;
  631. OMAP_MMC_WRITE(host, STAT, status);
  632. if (host->cmd != NULL)
  633. cmd = host->cmd->opcode;
  634. else
  635. cmd = -1;
  636. dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
  637. status, cmd);
  638. mmc_omap_report_irq(host, status);
  639. if (host->total_bytes_left) {
  640. if ((status & OMAP_MMC_STAT_A_FULL) ||
  641. (status & OMAP_MMC_STAT_END_OF_DATA))
  642. mmc_omap_xfer_data(host, 0);
  643. if (status & OMAP_MMC_STAT_A_EMPTY)
  644. mmc_omap_xfer_data(host, 1);
  645. }
  646. if (status & OMAP_MMC_STAT_END_OF_DATA)
  647. end_transfer = 1;
  648. if (status & OMAP_MMC_STAT_DATA_TOUT) {
  649. dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
  650. cmd);
  651. if (host->data) {
  652. host->data->error = -ETIMEDOUT;
  653. transfer_error = 1;
  654. }
  655. }
  656. if (status & OMAP_MMC_STAT_DATA_CRC) {
  657. if (host->data) {
  658. host->data->error = -EILSEQ;
  659. dev_dbg(mmc_dev(host->mmc),
  660. "data CRC error, bytes left %d\n",
  661. host->total_bytes_left);
  662. transfer_error = 1;
  663. } else {
  664. dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
  665. }
  666. }
  667. if (status & OMAP_MMC_STAT_CMD_TOUT) {
  668. /* Timeouts are routine with some commands */
  669. if (host->cmd) {
  670. struct mmc_omap_slot *slot =
  671. host->current_slot;
  672. if (slot == NULL ||
  673. !mmc_omap_cover_is_open(slot))
  674. dev_err(mmc_dev(host->mmc),
  675. "command timeout (CMD%d)\n",
  676. cmd);
  677. host->cmd->error = -ETIMEDOUT;
  678. end_command = 1;
  679. cmd_error = 1;
  680. }
  681. }
  682. if (status & OMAP_MMC_STAT_CMD_CRC) {
  683. if (host->cmd) {
  684. dev_err(mmc_dev(host->mmc),
  685. "command CRC error (CMD%d, arg 0x%08x)\n",
  686. cmd, host->cmd->arg);
  687. host->cmd->error = -EILSEQ;
  688. end_command = 1;
  689. cmd_error = 1;
  690. } else
  691. dev_err(mmc_dev(host->mmc),
  692. "command CRC error without cmd?\n");
  693. }
  694. if (status & OMAP_MMC_STAT_CARD_ERR) {
  695. dev_dbg(mmc_dev(host->mmc),
  696. "ignoring card status error (CMD%d)\n",
  697. cmd);
  698. end_command = 1;
  699. }
  700. /*
  701. * NOTE: On 1610 the END_OF_CMD may come too early when
  702. * starting a write
  703. */
  704. if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
  705. (!(status & OMAP_MMC_STAT_A_EMPTY))) {
  706. end_command = 1;
  707. }
  708. }
  709. if (cmd_error && host->data) {
  710. del_timer(&host->cmd_abort_timer);
  711. host->abort = 1;
  712. OMAP_MMC_WRITE(host, IE, 0);
  713. disable_irq_nosync(host->irq);
  714. queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
  715. return IRQ_HANDLED;
  716. }
  717. if (end_command && host->cmd)
  718. mmc_omap_cmd_done(host, host->cmd);
  719. if (host->data != NULL) {
  720. if (transfer_error)
  721. mmc_omap_xfer_done(host, host->data);
  722. else if (end_transfer)
  723. mmc_omap_end_of_data(host, host->data);
  724. }
  725. return IRQ_HANDLED;
  726. }
  727. void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
  728. {
  729. int cover_open;
  730. struct mmc_omap_host *host = dev_get_drvdata(dev);
  731. struct mmc_omap_slot *slot = host->slots[num];
  732. BUG_ON(num >= host->nr_slots);
  733. /* Other subsystems can call in here before we're initialised. */
  734. if (host->nr_slots == 0 || !host->slots[num])
  735. return;
  736. cover_open = mmc_omap_cover_is_open(slot);
  737. if (cover_open != slot->cover_open) {
  738. slot->cover_open = cover_open;
  739. sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
  740. }
  741. tasklet_hi_schedule(&slot->cover_tasklet);
  742. }
  743. static void mmc_omap_cover_timer(unsigned long arg)
  744. {
  745. struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
  746. tasklet_schedule(&slot->cover_tasklet);
  747. }
  748. static void mmc_omap_cover_handler(unsigned long param)
  749. {
  750. struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
  751. int cover_open = mmc_omap_cover_is_open(slot);
  752. mmc_detect_change(slot->mmc, 0);
  753. if (!cover_open)
  754. return;
  755. /*
  756. * If no card is inserted, we postpone polling until
  757. * the cover has been closed.
  758. */
  759. if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
  760. return;
  761. mod_timer(&slot->cover_timer,
  762. jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
  763. }
  764. static void mmc_omap_dma_callback(void *priv)
  765. {
  766. struct mmc_omap_host *host = priv;
  767. struct mmc_data *data = host->data;
  768. /* If we got to the end of DMA, assume everything went well */
  769. data->bytes_xfered += data->blocks * data->blksz;
  770. mmc_omap_dma_done(host, data);
  771. }
  772. static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  773. {
  774. u16 reg;
  775. reg = OMAP_MMC_READ(host, SDIO);
  776. reg &= ~(1 << 5);
  777. OMAP_MMC_WRITE(host, SDIO, reg);
  778. /* Set maximum timeout */
  779. OMAP_MMC_WRITE(host, CTO, 0xff);
  780. }
  781. static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  782. {
  783. unsigned int timeout, cycle_ns;
  784. u16 reg;
  785. cycle_ns = 1000000000 / host->current_slot->fclk_freq;
  786. timeout = req->data->timeout_ns / cycle_ns;
  787. timeout += req->data->timeout_clks;
  788. /* Check if we need to use timeout multiplier register */
  789. reg = OMAP_MMC_READ(host, SDIO);
  790. if (timeout > 0xffff) {
  791. reg |= (1 << 5);
  792. timeout /= 1024;
  793. } else
  794. reg &= ~(1 << 5);
  795. OMAP_MMC_WRITE(host, SDIO, reg);
  796. OMAP_MMC_WRITE(host, DTO, timeout);
  797. }
  798. static void
  799. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  800. {
  801. struct mmc_data *data = req->data;
  802. int i, use_dma = 1, block_size;
  803. struct scatterlist *sg;
  804. unsigned sg_len;
  805. host->data = data;
  806. if (data == NULL) {
  807. OMAP_MMC_WRITE(host, BLEN, 0);
  808. OMAP_MMC_WRITE(host, NBLK, 0);
  809. OMAP_MMC_WRITE(host, BUF, 0);
  810. host->dma_in_use = 0;
  811. set_cmd_timeout(host, req);
  812. return;
  813. }
  814. block_size = data->blksz;
  815. OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
  816. OMAP_MMC_WRITE(host, BLEN, block_size - 1);
  817. set_data_timeout(host, req);
  818. /* cope with calling layer confusion; it issues "single
  819. * block" writes using multi-block scatterlists.
  820. */
  821. sg_len = (data->blocks == 1) ? 1 : data->sg_len;
  822. /* Only do DMA for entire blocks */
  823. for_each_sg(data->sg, sg, sg_len, i) {
  824. if ((sg->length % block_size) != 0) {
  825. use_dma = 0;
  826. break;
  827. }
  828. }
  829. host->sg_idx = 0;
  830. if (use_dma) {
  831. enum dma_data_direction dma_data_dir;
  832. struct dma_async_tx_descriptor *tx;
  833. struct dma_chan *c;
  834. u32 burst, *bp;
  835. u16 buf;
  836. /*
  837. * FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx
  838. * and 24xx. Use 16 or 32 word frames when the
  839. * blocksize is at least that large. Blocksize is
  840. * usually 512 bytes; but not for some SD reads.
  841. */
  842. burst = mmc_omap15xx() ? 32 : 64;
  843. if (burst > data->blksz)
  844. burst = data->blksz;
  845. burst >>= 1;
  846. if (data->flags & MMC_DATA_WRITE) {
  847. c = host->dma_tx;
  848. bp = &host->dma_tx_burst;
  849. buf = 0x0f80 | (burst - 1) << 0;
  850. dma_data_dir = DMA_TO_DEVICE;
  851. } else {
  852. c = host->dma_rx;
  853. bp = &host->dma_rx_burst;
  854. buf = 0x800f | (burst - 1) << 8;
  855. dma_data_dir = DMA_FROM_DEVICE;
  856. }
  857. if (!c)
  858. goto use_pio;
  859. /* Only reconfigure if we have a different burst size */
  860. if (*bp != burst) {
  861. struct dma_slave_config cfg = {
  862. .src_addr = host->phys_base +
  863. OMAP_MMC_REG(host, DATA),
  864. .dst_addr = host->phys_base +
  865. OMAP_MMC_REG(host, DATA),
  866. .src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES,
  867. .dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES,
  868. .src_maxburst = burst,
  869. .dst_maxburst = burst,
  870. };
  871. if (dmaengine_slave_config(c, &cfg))
  872. goto use_pio;
  873. *bp = burst;
  874. }
  875. host->sg_len = dma_map_sg(c->device->dev, data->sg, sg_len,
  876. dma_data_dir);
  877. if (host->sg_len == 0)
  878. goto use_pio;
  879. tx = dmaengine_prep_slave_sg(c, data->sg, host->sg_len,
  880. data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  881. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  882. if (!tx)
  883. goto use_pio;
  884. OMAP_MMC_WRITE(host, BUF, buf);
  885. tx->callback = mmc_omap_dma_callback;
  886. tx->callback_param = host;
  887. dmaengine_submit(tx);
  888. host->brs_received = 0;
  889. host->dma_done = 0;
  890. host->dma_in_use = 1;
  891. return;
  892. }
  893. use_pio:
  894. /* Revert to PIO? */
  895. OMAP_MMC_WRITE(host, BUF, 0x1f1f);
  896. host->total_bytes_left = data->blocks * block_size;
  897. host->sg_len = sg_len;
  898. mmc_omap_sg_to_buf(host);
  899. host->dma_in_use = 0;
  900. }
  901. static void mmc_omap_start_request(struct mmc_omap_host *host,
  902. struct mmc_request *req)
  903. {
  904. BUG_ON(host->mrq != NULL);
  905. host->mrq = req;
  906. /* only touch fifo AFTER the controller readies it */
  907. mmc_omap_prepare_data(host, req);
  908. mmc_omap_start_command(host, req->cmd);
  909. if (host->dma_in_use) {
  910. struct dma_chan *c = host->data->flags & MMC_DATA_WRITE ?
  911. host->dma_tx : host->dma_rx;
  912. dma_async_issue_pending(c);
  913. }
  914. }
  915. static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
  916. {
  917. struct mmc_omap_slot *slot = mmc_priv(mmc);
  918. struct mmc_omap_host *host = slot->host;
  919. unsigned long flags;
  920. spin_lock_irqsave(&host->slot_lock, flags);
  921. if (host->mmc != NULL) {
  922. BUG_ON(slot->mrq != NULL);
  923. slot->mrq = req;
  924. spin_unlock_irqrestore(&host->slot_lock, flags);
  925. return;
  926. } else
  927. host->mmc = mmc;
  928. spin_unlock_irqrestore(&host->slot_lock, flags);
  929. mmc_omap_select_slot(slot, 1);
  930. mmc_omap_start_request(host, req);
  931. }
  932. static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
  933. int vdd)
  934. {
  935. struct mmc_omap_host *host;
  936. host = slot->host;
  937. if (slot->pdata->set_power != NULL)
  938. slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
  939. vdd);
  940. if (mmc_omap2()) {
  941. u16 w;
  942. if (power_on) {
  943. w = OMAP_MMC_READ(host, CON);
  944. OMAP_MMC_WRITE(host, CON, w | (1 << 11));
  945. } else {
  946. w = OMAP_MMC_READ(host, CON);
  947. OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
  948. }
  949. }
  950. }
  951. static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
  952. {
  953. struct mmc_omap_slot *slot = mmc_priv(mmc);
  954. struct mmc_omap_host *host = slot->host;
  955. int func_clk_rate = clk_get_rate(host->fclk);
  956. int dsor;
  957. if (ios->clock == 0)
  958. return 0;
  959. dsor = func_clk_rate / ios->clock;
  960. if (dsor < 1)
  961. dsor = 1;
  962. if (func_clk_rate / dsor > ios->clock)
  963. dsor++;
  964. if (dsor > 250)
  965. dsor = 250;
  966. slot->fclk_freq = func_clk_rate / dsor;
  967. if (ios->bus_width == MMC_BUS_WIDTH_4)
  968. dsor |= 1 << 15;
  969. return dsor;
  970. }
  971. static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  972. {
  973. struct mmc_omap_slot *slot = mmc_priv(mmc);
  974. struct mmc_omap_host *host = slot->host;
  975. int i, dsor;
  976. int clk_enabled;
  977. mmc_omap_select_slot(slot, 0);
  978. dsor = mmc_omap_calc_divisor(mmc, ios);
  979. if (ios->vdd != slot->vdd)
  980. slot->vdd = ios->vdd;
  981. clk_enabled = 0;
  982. switch (ios->power_mode) {
  983. case MMC_POWER_OFF:
  984. mmc_omap_set_power(slot, 0, ios->vdd);
  985. break;
  986. case MMC_POWER_UP:
  987. /* Cannot touch dsor yet, just power up MMC */
  988. mmc_omap_set_power(slot, 1, ios->vdd);
  989. goto exit;
  990. case MMC_POWER_ON:
  991. mmc_omap_fclk_enable(host, 1);
  992. clk_enabled = 1;
  993. dsor |= 1 << 11;
  994. break;
  995. }
  996. if (slot->bus_mode != ios->bus_mode) {
  997. if (slot->pdata->set_bus_mode != NULL)
  998. slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
  999. ios->bus_mode);
  1000. slot->bus_mode = ios->bus_mode;
  1001. }
  1002. /* On insanely high arm_per frequencies something sometimes
  1003. * goes somehow out of sync, and the POW bit is not being set,
  1004. * which results in the while loop below getting stuck.
  1005. * Writing to the CON register twice seems to do the trick. */
  1006. for (i = 0; i < 2; i++)
  1007. OMAP_MMC_WRITE(host, CON, dsor);
  1008. slot->saved_con = dsor;
  1009. if (ios->power_mode == MMC_POWER_ON) {
  1010. /* worst case at 400kHz, 80 cycles makes 200 microsecs */
  1011. int usecs = 250;
  1012. /* Send clock cycles, poll completion */
  1013. OMAP_MMC_WRITE(host, IE, 0);
  1014. OMAP_MMC_WRITE(host, STAT, 0xffff);
  1015. OMAP_MMC_WRITE(host, CMD, 1 << 7);
  1016. while (usecs > 0 && (OMAP_MMC_READ(host, STAT) & 1) == 0) {
  1017. udelay(1);
  1018. usecs--;
  1019. }
  1020. OMAP_MMC_WRITE(host, STAT, 1);
  1021. }
  1022. exit:
  1023. mmc_omap_release_slot(slot, clk_enabled);
  1024. }
  1025. static const struct mmc_host_ops mmc_omap_ops = {
  1026. .request = mmc_omap_request,
  1027. .set_ios = mmc_omap_set_ios,
  1028. };
  1029. static int mmc_omap_new_slot(struct mmc_omap_host *host, int id)
  1030. {
  1031. struct mmc_omap_slot *slot = NULL;
  1032. struct mmc_host *mmc;
  1033. int r;
  1034. mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
  1035. if (mmc == NULL)
  1036. return -ENOMEM;
  1037. slot = mmc_priv(mmc);
  1038. slot->host = host;
  1039. slot->mmc = mmc;
  1040. slot->id = id;
  1041. slot->pdata = &host->pdata->slots[id];
  1042. host->slots[id] = slot;
  1043. mmc->caps = 0;
  1044. if (host->pdata->slots[id].wires >= 4)
  1045. mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_ERASE;
  1046. mmc->ops = &mmc_omap_ops;
  1047. mmc->f_min = 400000;
  1048. if (mmc_omap2())
  1049. mmc->f_max = 48000000;
  1050. else
  1051. mmc->f_max = 24000000;
  1052. if (host->pdata->max_freq)
  1053. mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
  1054. mmc->ocr_avail = slot->pdata->ocr_mask;
  1055. /* Use scatterlist DMA to reduce per-transfer costs.
  1056. * NOTE max_seg_size assumption that small blocks aren't
  1057. * normally used (except e.g. for reading SD registers).
  1058. */
  1059. mmc->max_segs = 32;
  1060. mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
  1061. mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
  1062. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1063. mmc->max_seg_size = mmc->max_req_size;
  1064. if (slot->pdata->get_cover_state != NULL) {
  1065. setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
  1066. (unsigned long)slot);
  1067. tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
  1068. (unsigned long)slot);
  1069. }
  1070. r = mmc_add_host(mmc);
  1071. if (r < 0)
  1072. goto err_remove_host;
  1073. if (slot->pdata->name != NULL) {
  1074. r = device_create_file(&mmc->class_dev,
  1075. &dev_attr_slot_name);
  1076. if (r < 0)
  1077. goto err_remove_host;
  1078. }
  1079. if (slot->pdata->get_cover_state != NULL) {
  1080. r = device_create_file(&mmc->class_dev,
  1081. &dev_attr_cover_switch);
  1082. if (r < 0)
  1083. goto err_remove_slot_name;
  1084. tasklet_schedule(&slot->cover_tasklet);
  1085. }
  1086. return 0;
  1087. err_remove_slot_name:
  1088. if (slot->pdata->name != NULL)
  1089. device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
  1090. err_remove_host:
  1091. mmc_remove_host(mmc);
  1092. mmc_free_host(mmc);
  1093. return r;
  1094. }
  1095. static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
  1096. {
  1097. struct mmc_host *mmc = slot->mmc;
  1098. if (slot->pdata->name != NULL)
  1099. device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
  1100. if (slot->pdata->get_cover_state != NULL)
  1101. device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
  1102. tasklet_kill(&slot->cover_tasklet);
  1103. del_timer_sync(&slot->cover_timer);
  1104. flush_workqueue(slot->host->mmc_omap_wq);
  1105. mmc_remove_host(mmc);
  1106. mmc_free_host(mmc);
  1107. }
  1108. static int mmc_omap_probe(struct platform_device *pdev)
  1109. {
  1110. struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
  1111. struct mmc_omap_host *host = NULL;
  1112. struct resource *res;
  1113. int i, ret = 0;
  1114. int irq;
  1115. if (pdata == NULL) {
  1116. dev_err(&pdev->dev, "platform data missing\n");
  1117. return -ENXIO;
  1118. }
  1119. if (pdata->nr_slots == 0) {
  1120. dev_err(&pdev->dev, "no slots\n");
  1121. return -EPROBE_DEFER;
  1122. }
  1123. host = devm_kzalloc(&pdev->dev, sizeof(struct mmc_omap_host),
  1124. GFP_KERNEL);
  1125. if (host == NULL)
  1126. return -ENOMEM;
  1127. irq = platform_get_irq(pdev, 0);
  1128. if (irq < 0)
  1129. return -ENXIO;
  1130. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1131. host->virt_base = devm_ioremap_resource(&pdev->dev, res);
  1132. if (IS_ERR(host->virt_base))
  1133. return PTR_ERR(host->virt_base);
  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->features = host->pdata->slots[0].features;
  1146. host->dev = &pdev->dev;
  1147. platform_set_drvdata(pdev, host);
  1148. host->id = pdev->id;
  1149. host->irq = irq;
  1150. host->phys_base = res->start;
  1151. host->iclk = clk_get(&pdev->dev, "ick");
  1152. if (IS_ERR(host->iclk))
  1153. return PTR_ERR(host->iclk);
  1154. clk_enable(host->iclk);
  1155. host->fclk = clk_get(&pdev->dev, "fck");
  1156. if (IS_ERR(host->fclk)) {
  1157. ret = PTR_ERR(host->fclk);
  1158. goto err_free_iclk;
  1159. }
  1160. host->dma_tx_burst = -1;
  1161. host->dma_rx_burst = -1;
  1162. host->dma_tx = dma_request_chan(&pdev->dev, "tx");
  1163. if (IS_ERR(host->dma_tx)) {
  1164. ret = PTR_ERR(host->dma_tx);
  1165. if (ret == -EPROBE_DEFER) {
  1166. clk_put(host->fclk);
  1167. goto err_free_iclk;
  1168. }
  1169. host->dma_tx = NULL;
  1170. dev_warn(host->dev, "TX DMA channel request failed\n");
  1171. }
  1172. host->dma_rx = dma_request_chan(&pdev->dev, "rx");
  1173. if (IS_ERR(host->dma_rx)) {
  1174. ret = PTR_ERR(host->dma_rx);
  1175. if (ret == -EPROBE_DEFER) {
  1176. if (host->dma_tx)
  1177. dma_release_channel(host->dma_tx);
  1178. clk_put(host->fclk);
  1179. goto err_free_iclk;
  1180. }
  1181. host->dma_rx = NULL;
  1182. dev_warn(host->dev, "RX DMA channel request failed\n");
  1183. }
  1184. ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
  1185. if (ret)
  1186. goto err_free_dma;
  1187. if (pdata->init != NULL) {
  1188. ret = pdata->init(&pdev->dev);
  1189. if (ret < 0)
  1190. goto err_free_irq;
  1191. }
  1192. host->nr_slots = pdata->nr_slots;
  1193. host->reg_shift = (mmc_omap7xx() ? 1 : 2);
  1194. host->mmc_omap_wq = alloc_workqueue("mmc_omap", 0, 0);
  1195. if (!host->mmc_omap_wq) {
  1196. ret = -ENOMEM;
  1197. goto err_plat_cleanup;
  1198. }
  1199. for (i = 0; i < pdata->nr_slots; i++) {
  1200. ret = mmc_omap_new_slot(host, i);
  1201. if (ret < 0) {
  1202. while (--i >= 0)
  1203. mmc_omap_remove_slot(host->slots[i]);
  1204. goto err_destroy_wq;
  1205. }
  1206. }
  1207. return 0;
  1208. err_destroy_wq:
  1209. destroy_workqueue(host->mmc_omap_wq);
  1210. err_plat_cleanup:
  1211. if (pdata->cleanup)
  1212. pdata->cleanup(&pdev->dev);
  1213. err_free_irq:
  1214. free_irq(host->irq, host);
  1215. err_free_dma:
  1216. if (host->dma_tx)
  1217. dma_release_channel(host->dma_tx);
  1218. if (host->dma_rx)
  1219. dma_release_channel(host->dma_rx);
  1220. clk_put(host->fclk);
  1221. err_free_iclk:
  1222. clk_disable(host->iclk);
  1223. clk_put(host->iclk);
  1224. return ret;
  1225. }
  1226. static int mmc_omap_remove(struct platform_device *pdev)
  1227. {
  1228. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1229. int i;
  1230. BUG_ON(host == NULL);
  1231. for (i = 0; i < host->nr_slots; i++)
  1232. mmc_omap_remove_slot(host->slots[i]);
  1233. if (host->pdata->cleanup)
  1234. host->pdata->cleanup(&pdev->dev);
  1235. mmc_omap_fclk_enable(host, 0);
  1236. free_irq(host->irq, host);
  1237. clk_put(host->fclk);
  1238. clk_disable(host->iclk);
  1239. clk_put(host->iclk);
  1240. if (host->dma_tx)
  1241. dma_release_channel(host->dma_tx);
  1242. if (host->dma_rx)
  1243. dma_release_channel(host->dma_rx);
  1244. destroy_workqueue(host->mmc_omap_wq);
  1245. return 0;
  1246. }
  1247. #if IS_BUILTIN(CONFIG_OF)
  1248. static const struct of_device_id mmc_omap_match[] = {
  1249. { .compatible = "ti,omap2420-mmc", },
  1250. { },
  1251. };
  1252. MODULE_DEVICE_TABLE(of, mmc_omap_match);
  1253. #endif
  1254. static struct platform_driver mmc_omap_driver = {
  1255. .probe = mmc_omap_probe,
  1256. .remove = mmc_omap_remove,
  1257. .driver = {
  1258. .name = DRIVER_NAME,
  1259. .of_match_table = of_match_ptr(mmc_omap_match),
  1260. },
  1261. };
  1262. module_platform_driver(mmc_omap_driver);
  1263. MODULE_DESCRIPTION("OMAP Multimedia Card driver");
  1264. MODULE_LICENSE("GPL");
  1265. MODULE_ALIAS("platform:" DRIVER_NAME);
  1266. MODULE_AUTHOR("Juha Yrjölä");