omap.c 39 KB

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