omap.c 39 KB

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