omap.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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. #include <plat/dma.h>
  32. #define OMAP_MMC_REG_CMD 0x00
  33. #define OMAP_MMC_REG_ARGL 0x01
  34. #define OMAP_MMC_REG_ARGH 0x02
  35. #define OMAP_MMC_REG_CON 0x03
  36. #define OMAP_MMC_REG_STAT 0x04
  37. #define OMAP_MMC_REG_IE 0x05
  38. #define OMAP_MMC_REG_CTO 0x06
  39. #define OMAP_MMC_REG_DTO 0x07
  40. #define OMAP_MMC_REG_DATA 0x08
  41. #define OMAP_MMC_REG_BLEN 0x09
  42. #define OMAP_MMC_REG_NBLK 0x0a
  43. #define OMAP_MMC_REG_BUF 0x0b
  44. #define OMAP_MMC_REG_SDIO 0x0d
  45. #define OMAP_MMC_REG_REV 0x0f
  46. #define OMAP_MMC_REG_RSP0 0x10
  47. #define OMAP_MMC_REG_RSP1 0x11
  48. #define OMAP_MMC_REG_RSP2 0x12
  49. #define OMAP_MMC_REG_RSP3 0x13
  50. #define OMAP_MMC_REG_RSP4 0x14
  51. #define OMAP_MMC_REG_RSP5 0x15
  52. #define OMAP_MMC_REG_RSP6 0x16
  53. #define OMAP_MMC_REG_RSP7 0x17
  54. #define OMAP_MMC_REG_IOSR 0x18
  55. #define OMAP_MMC_REG_SYSC 0x19
  56. #define OMAP_MMC_REG_SYSS 0x1a
  57. #define OMAP_MMC_STAT_CARD_ERR (1 << 14)
  58. #define OMAP_MMC_STAT_CARD_IRQ (1 << 13)
  59. #define OMAP_MMC_STAT_OCR_BUSY (1 << 12)
  60. #define OMAP_MMC_STAT_A_EMPTY (1 << 11)
  61. #define OMAP_MMC_STAT_A_FULL (1 << 10)
  62. #define OMAP_MMC_STAT_CMD_CRC (1 << 8)
  63. #define OMAP_MMC_STAT_CMD_TOUT (1 << 7)
  64. #define OMAP_MMC_STAT_DATA_CRC (1 << 6)
  65. #define OMAP_MMC_STAT_DATA_TOUT (1 << 5)
  66. #define OMAP_MMC_STAT_END_BUSY (1 << 4)
  67. #define OMAP_MMC_STAT_END_OF_DATA (1 << 3)
  68. #define OMAP_MMC_STAT_CARD_BUSY (1 << 2)
  69. #define OMAP_MMC_STAT_END_OF_CMD (1 << 0)
  70. #define mmc_omap7xx() (host->features & MMC_OMAP7XX)
  71. #define mmc_omap15xx() (host->features & MMC_OMAP15XX)
  72. #define mmc_omap16xx() (host->features & MMC_OMAP16XX)
  73. #define MMC_OMAP1_MASK (MMC_OMAP7XX | MMC_OMAP15XX | MMC_OMAP16XX)
  74. #define mmc_omap1() (host->features & MMC_OMAP1_MASK)
  75. #define mmc_omap2() (!mmc_omap1())
  76. #define OMAP_MMC_REG(host, reg) (OMAP_MMC_REG_##reg << (host)->reg_shift)
  77. #define OMAP_MMC_READ(host, reg) __raw_readw((host)->virt_base + OMAP_MMC_REG(host, reg))
  78. #define OMAP_MMC_WRITE(host, reg, val) __raw_writew((val), (host)->virt_base + OMAP_MMC_REG(host, reg))
  79. /*
  80. * Command types
  81. */
  82. #define OMAP_MMC_CMDTYPE_BC 0
  83. #define OMAP_MMC_CMDTYPE_BCR 1
  84. #define OMAP_MMC_CMDTYPE_AC 2
  85. #define OMAP_MMC_CMDTYPE_ADTC 3
  86. #define DRIVER_NAME "mmci-omap"
  87. /* Specifies how often in millisecs to poll for card status changes
  88. * when the cover switch is open */
  89. #define OMAP_MMC_COVER_POLL_DELAY 500
  90. struct mmc_omap_host;
  91. struct mmc_omap_slot {
  92. int id;
  93. unsigned int vdd;
  94. u16 saved_con;
  95. u16 bus_mode;
  96. unsigned int fclk_freq;
  97. struct tasklet_struct cover_tasklet;
  98. struct timer_list cover_timer;
  99. unsigned cover_open;
  100. struct mmc_request *mrq;
  101. struct mmc_omap_host *host;
  102. struct mmc_host *mmc;
  103. struct omap_mmc_slot_data *pdata;
  104. };
  105. struct mmc_omap_host {
  106. int initialized;
  107. int suspended;
  108. struct mmc_request * mrq;
  109. struct mmc_command * cmd;
  110. struct mmc_data * data;
  111. struct mmc_host * mmc;
  112. struct device * dev;
  113. unsigned char id; /* 16xx chips have 2 MMC blocks */
  114. struct clk * iclk;
  115. struct clk * fclk;
  116. struct dma_chan *dma_rx;
  117. u32 dma_rx_burst;
  118. struct dma_chan *dma_tx;
  119. u32 dma_tx_burst;
  120. struct resource *mem_res;
  121. void __iomem *virt_base;
  122. unsigned int phys_base;
  123. int irq;
  124. unsigned char bus_mode;
  125. unsigned int reg_shift;
  126. struct work_struct cmd_abort_work;
  127. unsigned abort:1;
  128. struct timer_list cmd_abort_timer;
  129. struct work_struct slot_release_work;
  130. struct mmc_omap_slot *next_slot;
  131. struct work_struct send_stop_work;
  132. struct mmc_data *stop_data;
  133. unsigned int sg_len;
  134. int sg_idx;
  135. u16 * buffer;
  136. u32 buffer_bytes_left;
  137. u32 total_bytes_left;
  138. unsigned features;
  139. unsigned use_dma:1;
  140. unsigned brs_received:1, dma_done:1;
  141. unsigned dma_in_use:1;
  142. spinlock_t dma_lock;
  143. struct mmc_omap_slot *slots[OMAP_MMC_MAX_SLOTS];
  144. struct mmc_omap_slot *current_slot;
  145. spinlock_t slot_lock;
  146. wait_queue_head_t slot_wq;
  147. int nr_slots;
  148. struct timer_list clk_timer;
  149. spinlock_t clk_lock; /* for changing enabled state */
  150. unsigned int fclk_enabled:1;
  151. struct workqueue_struct *mmc_omap_wq;
  152. struct omap_mmc_platform_data *pdata;
  153. };
  154. static void mmc_omap_fclk_offdelay(struct mmc_omap_slot *slot)
  155. {
  156. unsigned long tick_ns;
  157. if (slot != NULL && slot->host->fclk_enabled && slot->fclk_freq > 0) {
  158. tick_ns = (1000000000 + slot->fclk_freq - 1) / slot->fclk_freq;
  159. ndelay(8 * tick_ns);
  160. }
  161. }
  162. static void mmc_omap_fclk_enable(struct mmc_omap_host *host, unsigned int enable)
  163. {
  164. unsigned long flags;
  165. spin_lock_irqsave(&host->clk_lock, flags);
  166. if (host->fclk_enabled != enable) {
  167. host->fclk_enabled = enable;
  168. if (enable)
  169. clk_enable(host->fclk);
  170. else
  171. clk_disable(host->fclk);
  172. }
  173. spin_unlock_irqrestore(&host->clk_lock, flags);
  174. }
  175. static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
  176. {
  177. struct mmc_omap_host *host = slot->host;
  178. unsigned long flags;
  179. if (claimed)
  180. goto no_claim;
  181. spin_lock_irqsave(&host->slot_lock, flags);
  182. while (host->mmc != NULL) {
  183. spin_unlock_irqrestore(&host->slot_lock, flags);
  184. wait_event(host->slot_wq, host->mmc == NULL);
  185. spin_lock_irqsave(&host->slot_lock, flags);
  186. }
  187. host->mmc = slot->mmc;
  188. spin_unlock_irqrestore(&host->slot_lock, flags);
  189. no_claim:
  190. del_timer(&host->clk_timer);
  191. if (host->current_slot != slot || !claimed)
  192. mmc_omap_fclk_offdelay(host->current_slot);
  193. if (host->current_slot != slot) {
  194. OMAP_MMC_WRITE(host, CON, slot->saved_con & 0xFC00);
  195. if (host->pdata->switch_slot != NULL)
  196. host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
  197. host->current_slot = slot;
  198. }
  199. if (claimed) {
  200. mmc_omap_fclk_enable(host, 1);
  201. /* Doing the dummy read here seems to work around some bug
  202. * at least in OMAP24xx silicon where the command would not
  203. * start after writing the CMD register. Sigh. */
  204. OMAP_MMC_READ(host, CON);
  205. OMAP_MMC_WRITE(host, CON, slot->saved_con);
  206. } else
  207. mmc_omap_fclk_enable(host, 0);
  208. }
  209. static void mmc_omap_start_request(struct mmc_omap_host *host,
  210. struct mmc_request *req);
  211. static void mmc_omap_slot_release_work(struct work_struct *work)
  212. {
  213. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  214. slot_release_work);
  215. struct mmc_omap_slot *next_slot = host->next_slot;
  216. struct mmc_request *rq;
  217. host->next_slot = NULL;
  218. mmc_omap_select_slot(next_slot, 1);
  219. rq = next_slot->mrq;
  220. next_slot->mrq = NULL;
  221. mmc_omap_start_request(host, rq);
  222. }
  223. static void mmc_omap_release_slot(struct mmc_omap_slot *slot, int clk_enabled)
  224. {
  225. struct mmc_omap_host *host = slot->host;
  226. unsigned long flags;
  227. int i;
  228. BUG_ON(slot == NULL || host->mmc == NULL);
  229. if (clk_enabled)
  230. /* Keeps clock running for at least 8 cycles on valid freq */
  231. mod_timer(&host->clk_timer, jiffies + HZ/10);
  232. else {
  233. del_timer(&host->clk_timer);
  234. mmc_omap_fclk_offdelay(slot);
  235. mmc_omap_fclk_enable(host, 0);
  236. }
  237. spin_lock_irqsave(&host->slot_lock, flags);
  238. /* Check for any pending requests */
  239. for (i = 0; i < host->nr_slots; i++) {
  240. struct mmc_omap_slot *new_slot;
  241. if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
  242. continue;
  243. BUG_ON(host->next_slot != NULL);
  244. new_slot = host->slots[i];
  245. /* The current slot should not have a request in queue */
  246. BUG_ON(new_slot == host->current_slot);
  247. host->next_slot = new_slot;
  248. host->mmc = new_slot->mmc;
  249. spin_unlock_irqrestore(&host->slot_lock, flags);
  250. queue_work(host->mmc_omap_wq, &host->slot_release_work);
  251. return;
  252. }
  253. host->mmc = NULL;
  254. wake_up(&host->slot_wq);
  255. spin_unlock_irqrestore(&host->slot_lock, flags);
  256. }
  257. static inline
  258. int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
  259. {
  260. if (slot->pdata->get_cover_state)
  261. return slot->pdata->get_cover_state(mmc_dev(slot->mmc),
  262. slot->id);
  263. return 0;
  264. }
  265. static ssize_t
  266. mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
  267. char *buf)
  268. {
  269. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  270. struct mmc_omap_slot *slot = mmc_priv(mmc);
  271. return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
  272. "closed");
  273. }
  274. static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
  275. static ssize_t
  276. mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
  277. char *buf)
  278. {
  279. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  280. struct mmc_omap_slot *slot = mmc_priv(mmc);
  281. return sprintf(buf, "%s\n", slot->pdata->name);
  282. }
  283. static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
  284. static void
  285. mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
  286. {
  287. u32 cmdreg;
  288. u32 resptype;
  289. u32 cmdtype;
  290. host->cmd = cmd;
  291. resptype = 0;
  292. cmdtype = 0;
  293. /* Our hardware needs to know exact type */
  294. switch (mmc_resp_type(cmd)) {
  295. case MMC_RSP_NONE:
  296. break;
  297. case MMC_RSP_R1:
  298. case MMC_RSP_R1B:
  299. /* resp 1, 1b, 6, 7 */
  300. resptype = 1;
  301. break;
  302. case MMC_RSP_R2:
  303. resptype = 2;
  304. break;
  305. case MMC_RSP_R3:
  306. resptype = 3;
  307. break;
  308. default:
  309. dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
  310. break;
  311. }
  312. if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
  313. cmdtype = OMAP_MMC_CMDTYPE_ADTC;
  314. } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
  315. cmdtype = OMAP_MMC_CMDTYPE_BC;
  316. } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
  317. cmdtype = OMAP_MMC_CMDTYPE_BCR;
  318. } else {
  319. cmdtype = OMAP_MMC_CMDTYPE_AC;
  320. }
  321. cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
  322. if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
  323. cmdreg |= 1 << 6;
  324. if (cmd->flags & MMC_RSP_BUSY)
  325. cmdreg |= 1 << 11;
  326. if (host->data && !(host->data->flags & MMC_DATA_WRITE))
  327. cmdreg |= 1 << 15;
  328. mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
  329. OMAP_MMC_WRITE(host, CTO, 200);
  330. OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
  331. OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
  332. OMAP_MMC_WRITE(host, IE,
  333. OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
  334. OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
  335. OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
  336. OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
  337. OMAP_MMC_STAT_END_OF_DATA);
  338. OMAP_MMC_WRITE(host, CMD, cmdreg);
  339. }
  340. static void
  341. mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
  342. int abort)
  343. {
  344. enum dma_data_direction dma_data_dir;
  345. struct device *dev = mmc_dev(host->mmc);
  346. struct dma_chan *c;
  347. if (data->flags & MMC_DATA_WRITE) {
  348. dma_data_dir = DMA_TO_DEVICE;
  349. c = host->dma_tx;
  350. } else {
  351. dma_data_dir = DMA_FROM_DEVICE;
  352. c = host->dma_rx;
  353. }
  354. if (c) {
  355. if (data->error) {
  356. dmaengine_terminate_all(c);
  357. /* Claim nothing transferred on error... */
  358. data->bytes_xfered = 0;
  359. }
  360. dev = c->device->dev;
  361. }
  362. dma_unmap_sg(dev, data->sg, host->sg_len, dma_data_dir);
  363. }
  364. static void mmc_omap_send_stop_work(struct work_struct *work)
  365. {
  366. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  367. send_stop_work);
  368. struct mmc_omap_slot *slot = host->current_slot;
  369. struct mmc_data *data = host->stop_data;
  370. unsigned long tick_ns;
  371. tick_ns = (1000000000 + slot->fclk_freq - 1)/slot->fclk_freq;
  372. ndelay(8*tick_ns);
  373. mmc_omap_start_command(host, data->stop);
  374. }
  375. static void
  376. mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
  377. {
  378. if (host->dma_in_use)
  379. mmc_omap_release_dma(host, data, data->error);
  380. host->data = NULL;
  381. host->sg_len = 0;
  382. /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
  383. * dozens of requests until the card finishes writing data.
  384. * It'd be cheaper to just wait till an EOFB interrupt arrives...
  385. */
  386. if (!data->stop) {
  387. struct mmc_host *mmc;
  388. host->mrq = NULL;
  389. mmc = host->mmc;
  390. mmc_omap_release_slot(host->current_slot, 1);
  391. mmc_request_done(mmc, data->mrq);
  392. return;
  393. }
  394. host->stop_data = data;
  395. queue_work(host->mmc_omap_wq, &host->send_stop_work);
  396. }
  397. static void
  398. mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
  399. {
  400. struct mmc_omap_slot *slot = host->current_slot;
  401. unsigned int restarts, passes, timeout;
  402. u16 stat = 0;
  403. /* Sending abort takes 80 clocks. Have some extra and round up */
  404. timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
  405. restarts = 0;
  406. while (restarts < maxloops) {
  407. OMAP_MMC_WRITE(host, STAT, 0xFFFF);
  408. OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
  409. passes = 0;
  410. while (passes < timeout) {
  411. stat = OMAP_MMC_READ(host, STAT);
  412. if (stat & OMAP_MMC_STAT_END_OF_CMD)
  413. goto out;
  414. udelay(1);
  415. passes++;
  416. }
  417. restarts++;
  418. }
  419. out:
  420. OMAP_MMC_WRITE(host, STAT, stat);
  421. }
  422. static void
  423. mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
  424. {
  425. if (host->dma_in_use)
  426. mmc_omap_release_dma(host, data, 1);
  427. host->data = NULL;
  428. host->sg_len = 0;
  429. mmc_omap_send_abort(host, 10000);
  430. }
  431. static void
  432. mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
  433. {
  434. unsigned long flags;
  435. int done;
  436. if (!host->dma_in_use) {
  437. mmc_omap_xfer_done(host, data);
  438. return;
  439. }
  440. done = 0;
  441. spin_lock_irqsave(&host->dma_lock, flags);
  442. if (host->dma_done)
  443. done = 1;
  444. else
  445. host->brs_received = 1;
  446. spin_unlock_irqrestore(&host->dma_lock, flags);
  447. if (done)
  448. mmc_omap_xfer_done(host, data);
  449. }
  450. static void
  451. mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
  452. {
  453. unsigned long flags;
  454. int done;
  455. done = 0;
  456. spin_lock_irqsave(&host->dma_lock, flags);
  457. if (host->brs_received)
  458. done = 1;
  459. else
  460. host->dma_done = 1;
  461. spin_unlock_irqrestore(&host->dma_lock, flags);
  462. if (done)
  463. mmc_omap_xfer_done(host, data);
  464. }
  465. static void
  466. mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
  467. {
  468. host->cmd = NULL;
  469. del_timer(&host->cmd_abort_timer);
  470. if (cmd->flags & MMC_RSP_PRESENT) {
  471. if (cmd->flags & MMC_RSP_136) {
  472. /* response type 2 */
  473. cmd->resp[3] =
  474. OMAP_MMC_READ(host, RSP0) |
  475. (OMAP_MMC_READ(host, RSP1) << 16);
  476. cmd->resp[2] =
  477. OMAP_MMC_READ(host, RSP2) |
  478. (OMAP_MMC_READ(host, RSP3) << 16);
  479. cmd->resp[1] =
  480. OMAP_MMC_READ(host, RSP4) |
  481. (OMAP_MMC_READ(host, RSP5) << 16);
  482. cmd->resp[0] =
  483. OMAP_MMC_READ(host, RSP6) |
  484. (OMAP_MMC_READ(host, RSP7) << 16);
  485. } else {
  486. /* response types 1, 1b, 3, 4, 5, 6 */
  487. cmd->resp[0] =
  488. OMAP_MMC_READ(host, RSP6) |
  489. (OMAP_MMC_READ(host, RSP7) << 16);
  490. }
  491. }
  492. if (host->data == NULL || cmd->error) {
  493. struct mmc_host *mmc;
  494. if (host->data != NULL)
  495. mmc_omap_abort_xfer(host, host->data);
  496. host->mrq = NULL;
  497. mmc = host->mmc;
  498. mmc_omap_release_slot(host->current_slot, 1);
  499. mmc_request_done(mmc, cmd->mrq);
  500. }
  501. }
  502. /*
  503. * Abort stuck command. Can occur when card is removed while it is being
  504. * read.
  505. */
  506. static void mmc_omap_abort_command(struct work_struct *work)
  507. {
  508. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  509. cmd_abort_work);
  510. BUG_ON(!host->cmd);
  511. dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
  512. host->cmd->opcode);
  513. if (host->cmd->error == 0)
  514. host->cmd->error = -ETIMEDOUT;
  515. if (host->data == NULL) {
  516. struct mmc_command *cmd;
  517. struct mmc_host *mmc;
  518. cmd = host->cmd;
  519. host->cmd = NULL;
  520. mmc_omap_send_abort(host, 10000);
  521. host->mrq = NULL;
  522. mmc = host->mmc;
  523. mmc_omap_release_slot(host->current_slot, 1);
  524. mmc_request_done(mmc, cmd->mrq);
  525. } else
  526. mmc_omap_cmd_done(host, host->cmd);
  527. host->abort = 0;
  528. enable_irq(host->irq);
  529. }
  530. static void
  531. mmc_omap_cmd_timer(unsigned long data)
  532. {
  533. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  534. unsigned long flags;
  535. spin_lock_irqsave(&host->slot_lock, flags);
  536. if (host->cmd != NULL && !host->abort) {
  537. OMAP_MMC_WRITE(host, IE, 0);
  538. disable_irq(host->irq);
  539. host->abort = 1;
  540. queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
  541. }
  542. spin_unlock_irqrestore(&host->slot_lock, flags);
  543. }
  544. /* PIO only */
  545. static void
  546. mmc_omap_sg_to_buf(struct mmc_omap_host *host)
  547. {
  548. struct scatterlist *sg;
  549. sg = host->data->sg + host->sg_idx;
  550. host->buffer_bytes_left = sg->length;
  551. host->buffer = sg_virt(sg);
  552. if (host->buffer_bytes_left > host->total_bytes_left)
  553. host->buffer_bytes_left = host->total_bytes_left;
  554. }
  555. static void
  556. mmc_omap_clk_timer(unsigned long data)
  557. {
  558. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  559. mmc_omap_fclk_enable(host, 0);
  560. }
  561. /* PIO only */
  562. static void
  563. mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
  564. {
  565. int n, nwords;
  566. if (host->buffer_bytes_left == 0) {
  567. host->sg_idx++;
  568. BUG_ON(host->sg_idx == host->sg_len);
  569. mmc_omap_sg_to_buf(host);
  570. }
  571. n = 64;
  572. if (n > host->buffer_bytes_left)
  573. n = host->buffer_bytes_left;
  574. nwords = n / 2;
  575. nwords += n & 1; /* handle odd number of bytes to transfer */
  576. host->buffer_bytes_left -= n;
  577. host->total_bytes_left -= n;
  578. host->data->bytes_xfered += n;
  579. if (write) {
  580. __raw_writesw(host->virt_base + OMAP_MMC_REG(host, DATA),
  581. host->buffer, nwords);
  582. } else {
  583. __raw_readsw(host->virt_base + OMAP_MMC_REG(host, DATA),
  584. host->buffer, nwords);
  585. }
  586. host->buffer += nwords;
  587. }
  588. #ifdef CONFIG_MMC_DEBUG
  589. static void mmc_omap_report_irq(struct mmc_omap_host *host, u16 status)
  590. {
  591. static const char *mmc_omap_status_bits[] = {
  592. "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
  593. "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
  594. };
  595. int i;
  596. char res[64], *buf = res;
  597. buf += sprintf(buf, "MMC IRQ 0x%x:", status);
  598. for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
  599. if (status & (1 << i))
  600. buf += sprintf(buf, " %s", mmc_omap_status_bits[i]);
  601. dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
  602. }
  603. #else
  604. static void mmc_omap_report_irq(struct mmc_omap_host *host, u16 status)
  605. {
  606. }
  607. #endif
  608. static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
  609. {
  610. struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
  611. u16 status;
  612. int end_command;
  613. int end_transfer;
  614. int transfer_error, cmd_error;
  615. if (host->cmd == NULL && host->data == NULL) {
  616. status = OMAP_MMC_READ(host, STAT);
  617. dev_info(mmc_dev(host->slots[0]->mmc),
  618. "Spurious IRQ 0x%04x\n", status);
  619. if (status != 0) {
  620. OMAP_MMC_WRITE(host, STAT, status);
  621. OMAP_MMC_WRITE(host, IE, 0);
  622. }
  623. return IRQ_HANDLED;
  624. }
  625. end_command = 0;
  626. end_transfer = 0;
  627. transfer_error = 0;
  628. cmd_error = 0;
  629. while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
  630. int cmd;
  631. OMAP_MMC_WRITE(host, STAT, status);
  632. if (host->cmd != NULL)
  633. cmd = host->cmd->opcode;
  634. else
  635. cmd = -1;
  636. dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
  637. status, cmd);
  638. mmc_omap_report_irq(host, status);
  639. if (host->total_bytes_left) {
  640. if ((status & OMAP_MMC_STAT_A_FULL) ||
  641. (status & OMAP_MMC_STAT_END_OF_DATA))
  642. mmc_omap_xfer_data(host, 0);
  643. if (status & OMAP_MMC_STAT_A_EMPTY)
  644. mmc_omap_xfer_data(host, 1);
  645. }
  646. if (status & OMAP_MMC_STAT_END_OF_DATA)
  647. end_transfer = 1;
  648. if (status & OMAP_MMC_STAT_DATA_TOUT) {
  649. dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
  650. cmd);
  651. if (host->data) {
  652. host->data->error = -ETIMEDOUT;
  653. transfer_error = 1;
  654. }
  655. }
  656. if (status & OMAP_MMC_STAT_DATA_CRC) {
  657. if (host->data) {
  658. host->data->error = -EILSEQ;
  659. dev_dbg(mmc_dev(host->mmc),
  660. "data CRC error, bytes left %d\n",
  661. host->total_bytes_left);
  662. transfer_error = 1;
  663. } else {
  664. dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
  665. }
  666. }
  667. if (status & OMAP_MMC_STAT_CMD_TOUT) {
  668. /* Timeouts are routine with some commands */
  669. if (host->cmd) {
  670. struct mmc_omap_slot *slot =
  671. host->current_slot;
  672. if (slot == NULL ||
  673. !mmc_omap_cover_is_open(slot))
  674. dev_err(mmc_dev(host->mmc),
  675. "command timeout (CMD%d)\n",
  676. cmd);
  677. host->cmd->error = -ETIMEDOUT;
  678. end_command = 1;
  679. cmd_error = 1;
  680. }
  681. }
  682. if (status & OMAP_MMC_STAT_CMD_CRC) {
  683. if (host->cmd) {
  684. dev_err(mmc_dev(host->mmc),
  685. "command CRC error (CMD%d, arg 0x%08x)\n",
  686. cmd, host->cmd->arg);
  687. host->cmd->error = -EILSEQ;
  688. end_command = 1;
  689. cmd_error = 1;
  690. } else
  691. dev_err(mmc_dev(host->mmc),
  692. "command CRC error without cmd?\n");
  693. }
  694. if (status & OMAP_MMC_STAT_CARD_ERR) {
  695. dev_dbg(mmc_dev(host->mmc),
  696. "ignoring card status error (CMD%d)\n",
  697. cmd);
  698. end_command = 1;
  699. }
  700. /*
  701. * NOTE: On 1610 the END_OF_CMD may come too early when
  702. * starting a write
  703. */
  704. if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
  705. (!(status & OMAP_MMC_STAT_A_EMPTY))) {
  706. end_command = 1;
  707. }
  708. }
  709. if (cmd_error && host->data) {
  710. del_timer(&host->cmd_abort_timer);
  711. host->abort = 1;
  712. OMAP_MMC_WRITE(host, IE, 0);
  713. disable_irq_nosync(host->irq);
  714. queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
  715. return IRQ_HANDLED;
  716. }
  717. if (end_command && host->cmd)
  718. mmc_omap_cmd_done(host, host->cmd);
  719. if (host->data != NULL) {
  720. if (transfer_error)
  721. mmc_omap_xfer_done(host, host->data);
  722. else if (end_transfer)
  723. mmc_omap_end_of_data(host, host->data);
  724. }
  725. return IRQ_HANDLED;
  726. }
  727. void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
  728. {
  729. int cover_open;
  730. struct mmc_omap_host *host = dev_get_drvdata(dev);
  731. struct mmc_omap_slot *slot = host->slots[num];
  732. BUG_ON(num >= host->nr_slots);
  733. /* Other subsystems can call in here before we're initialised. */
  734. if (host->nr_slots == 0 || !host->slots[num])
  735. return;
  736. cover_open = mmc_omap_cover_is_open(slot);
  737. if (cover_open != slot->cover_open) {
  738. slot->cover_open = cover_open;
  739. sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
  740. }
  741. tasklet_hi_schedule(&slot->cover_tasklet);
  742. }
  743. static void mmc_omap_cover_timer(unsigned long arg)
  744. {
  745. struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
  746. tasklet_schedule(&slot->cover_tasklet);
  747. }
  748. static void mmc_omap_cover_handler(unsigned long param)
  749. {
  750. struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
  751. int cover_open = mmc_omap_cover_is_open(slot);
  752. mmc_detect_change(slot->mmc, 0);
  753. if (!cover_open)
  754. return;
  755. /*
  756. * If no card is inserted, we postpone polling until
  757. * the cover has been closed.
  758. */
  759. if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
  760. return;
  761. mod_timer(&slot->cover_timer,
  762. jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
  763. }
  764. static void mmc_omap_dma_callback(void *priv)
  765. {
  766. struct mmc_omap_host *host = priv;
  767. struct mmc_data *data = host->data;
  768. /* If we got to the end of DMA, assume everything went well */
  769. data->bytes_xfered += data->blocks * data->blksz;
  770. mmc_omap_dma_done(host, data);
  771. }
  772. static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  773. {
  774. u16 reg;
  775. reg = OMAP_MMC_READ(host, SDIO);
  776. reg &= ~(1 << 5);
  777. OMAP_MMC_WRITE(host, SDIO, reg);
  778. /* Set maximum timeout */
  779. OMAP_MMC_WRITE(host, CTO, 0xff);
  780. }
  781. static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  782. {
  783. unsigned int timeout, cycle_ns;
  784. u16 reg;
  785. cycle_ns = 1000000000 / host->current_slot->fclk_freq;
  786. timeout = req->data->timeout_ns / cycle_ns;
  787. timeout += req->data->timeout_clks;
  788. /* Check if we need to use timeout multiplier register */
  789. reg = OMAP_MMC_READ(host, SDIO);
  790. if (timeout > 0xffff) {
  791. reg |= (1 << 5);
  792. timeout /= 1024;
  793. } else
  794. reg &= ~(1 << 5);
  795. OMAP_MMC_WRITE(host, SDIO, reg);
  796. OMAP_MMC_WRITE(host, DTO, timeout);
  797. }
  798. static void
  799. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  800. {
  801. struct mmc_data *data = req->data;
  802. int i, use_dma, 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 = mmc_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 (mmc_omap2()) {
  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 (mmc_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->features = host->pdata->slots[0].features;
  1147. host->dev = &pdev->dev;
  1148. platform_set_drvdata(pdev, host);
  1149. host->id = pdev->id;
  1150. host->mem_res = res;
  1151. host->irq = irq;
  1152. host->use_dma = 1;
  1153. host->irq = irq;
  1154. host->phys_base = host->mem_res->start;
  1155. host->virt_base = ioremap(res->start, resource_size(res));
  1156. if (!host->virt_base)
  1157. goto err_ioremap;
  1158. host->iclk = clk_get(&pdev->dev, "ick");
  1159. if (IS_ERR(host->iclk)) {
  1160. ret = PTR_ERR(host->iclk);
  1161. goto err_free_mmc_host;
  1162. }
  1163. clk_enable(host->iclk);
  1164. host->fclk = clk_get(&pdev->dev, "fck");
  1165. if (IS_ERR(host->fclk)) {
  1166. ret = PTR_ERR(host->fclk);
  1167. goto err_free_iclk;
  1168. }
  1169. dma_cap_zero(mask);
  1170. dma_cap_set(DMA_SLAVE, mask);
  1171. host->dma_tx_burst = -1;
  1172. host->dma_rx_burst = -1;
  1173. if (mmc_omap2())
  1174. sig = host->id == 0 ? OMAP24XX_DMA_MMC1_TX : OMAP24XX_DMA_MMC2_TX;
  1175. else
  1176. sig = host->id == 0 ? OMAP_DMA_MMC_TX : OMAP_DMA_MMC2_TX;
  1177. host->dma_tx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  1178. #if 0
  1179. if (!host->dma_tx) {
  1180. dev_err(host->dev, "unable to obtain TX DMA engine channel %u\n",
  1181. sig);
  1182. goto err_dma;
  1183. }
  1184. #else
  1185. if (!host->dma_tx)
  1186. dev_warn(host->dev, "unable to obtain TX DMA engine channel %u\n",
  1187. sig);
  1188. #endif
  1189. if (mmc_omap2())
  1190. sig = host->id == 0 ? OMAP24XX_DMA_MMC1_RX : OMAP24XX_DMA_MMC2_RX;
  1191. else
  1192. sig = host->id == 0 ? OMAP_DMA_MMC_RX : OMAP_DMA_MMC2_RX;
  1193. host->dma_rx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  1194. #if 0
  1195. if (!host->dma_rx) {
  1196. dev_err(host->dev, "unable to obtain RX DMA engine channel %u\n",
  1197. sig);
  1198. goto err_dma;
  1199. }
  1200. #else
  1201. if (!host->dma_rx)
  1202. dev_warn(host->dev, "unable to obtain RX DMA engine channel %u\n",
  1203. sig);
  1204. #endif
  1205. ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
  1206. if (ret)
  1207. goto err_free_dma;
  1208. if (pdata->init != NULL) {
  1209. ret = pdata->init(&pdev->dev);
  1210. if (ret < 0)
  1211. goto err_free_irq;
  1212. }
  1213. host->nr_slots = pdata->nr_slots;
  1214. host->reg_shift = (mmc_omap7xx() ? 1 : 2);
  1215. host->mmc_omap_wq = alloc_workqueue("mmc_omap", 0, 0);
  1216. if (!host->mmc_omap_wq)
  1217. goto err_plat_cleanup;
  1218. for (i = 0; i < pdata->nr_slots; i++) {
  1219. ret = mmc_omap_new_slot(host, i);
  1220. if (ret < 0) {
  1221. while (--i >= 0)
  1222. mmc_omap_remove_slot(host->slots[i]);
  1223. goto err_destroy_wq;
  1224. }
  1225. }
  1226. return 0;
  1227. err_destroy_wq:
  1228. destroy_workqueue(host->mmc_omap_wq);
  1229. err_plat_cleanup:
  1230. if (pdata->cleanup)
  1231. pdata->cleanup(&pdev->dev);
  1232. err_free_irq:
  1233. free_irq(host->irq, host);
  1234. err_free_dma:
  1235. if (host->dma_tx)
  1236. dma_release_channel(host->dma_tx);
  1237. if (host->dma_rx)
  1238. dma_release_channel(host->dma_rx);
  1239. clk_put(host->fclk);
  1240. err_free_iclk:
  1241. clk_disable(host->iclk);
  1242. clk_put(host->iclk);
  1243. err_free_mmc_host:
  1244. iounmap(host->virt_base);
  1245. err_ioremap:
  1246. kfree(host);
  1247. err_free_mem_region:
  1248. release_mem_region(res->start, resource_size(res));
  1249. return ret;
  1250. }
  1251. static int __devexit mmc_omap_remove(struct platform_device *pdev)
  1252. {
  1253. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1254. int i;
  1255. platform_set_drvdata(pdev, NULL);
  1256. BUG_ON(host == NULL);
  1257. for (i = 0; i < host->nr_slots; i++)
  1258. mmc_omap_remove_slot(host->slots[i]);
  1259. if (host->pdata->cleanup)
  1260. host->pdata->cleanup(&pdev->dev);
  1261. mmc_omap_fclk_enable(host, 0);
  1262. free_irq(host->irq, host);
  1263. clk_put(host->fclk);
  1264. clk_disable(host->iclk);
  1265. clk_put(host->iclk);
  1266. if (host->dma_tx)
  1267. dma_release_channel(host->dma_tx);
  1268. if (host->dma_rx)
  1269. dma_release_channel(host->dma_rx);
  1270. iounmap(host->virt_base);
  1271. release_mem_region(pdev->resource[0].start,
  1272. pdev->resource[0].end - pdev->resource[0].start + 1);
  1273. destroy_workqueue(host->mmc_omap_wq);
  1274. kfree(host);
  1275. return 0;
  1276. }
  1277. #ifdef CONFIG_PM
  1278. static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
  1279. {
  1280. int i, ret = 0;
  1281. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1282. if (host == NULL || host->suspended)
  1283. return 0;
  1284. for (i = 0; i < host->nr_slots; i++) {
  1285. struct mmc_omap_slot *slot;
  1286. slot = host->slots[i];
  1287. ret = mmc_suspend_host(slot->mmc);
  1288. if (ret < 0) {
  1289. while (--i >= 0) {
  1290. slot = host->slots[i];
  1291. mmc_resume_host(slot->mmc);
  1292. }
  1293. return ret;
  1294. }
  1295. }
  1296. host->suspended = 1;
  1297. return 0;
  1298. }
  1299. static int mmc_omap_resume(struct platform_device *pdev)
  1300. {
  1301. int i, ret = 0;
  1302. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1303. if (host == NULL || !host->suspended)
  1304. return 0;
  1305. for (i = 0; i < host->nr_slots; i++) {
  1306. struct mmc_omap_slot *slot;
  1307. slot = host->slots[i];
  1308. ret = mmc_resume_host(slot->mmc);
  1309. if (ret < 0)
  1310. return ret;
  1311. host->suspended = 0;
  1312. }
  1313. return 0;
  1314. }
  1315. #else
  1316. #define mmc_omap_suspend NULL
  1317. #define mmc_omap_resume NULL
  1318. #endif
  1319. static struct platform_driver mmc_omap_driver = {
  1320. .probe = mmc_omap_probe,
  1321. .remove = __devexit_p(mmc_omap_remove),
  1322. .suspend = mmc_omap_suspend,
  1323. .resume = mmc_omap_resume,
  1324. .driver = {
  1325. .name = DRIVER_NAME,
  1326. .owner = THIS_MODULE,
  1327. },
  1328. };
  1329. module_platform_driver(mmc_omap_driver);
  1330. MODULE_DESCRIPTION("OMAP Multimedia Card driver");
  1331. MODULE_LICENSE("GPL");
  1332. MODULE_ALIAS("platform:" DRIVER_NAME);
  1333. MODULE_AUTHOR("Juha Yrjölä");