omap.c 37 KB

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