omap.c 38 KB

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