omap.c 38 KB

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