omap.c 37 KB

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