omap.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  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 <mach/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 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. schedule_work(&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. BUG_ON(host->dma_ch < 0);
  345. if (data->error)
  346. omap_stop_dma(host->dma_ch);
  347. /* Release DMA channel lazily */
  348. mod_timer(&host->dma_timer, jiffies + HZ);
  349. if (data->flags & MMC_DATA_WRITE)
  350. dma_data_dir = DMA_TO_DEVICE;
  351. else
  352. dma_data_dir = DMA_FROM_DEVICE;
  353. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
  354. dma_data_dir);
  355. }
  356. static void mmc_omap_send_stop_work(struct work_struct *work)
  357. {
  358. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  359. send_stop_work);
  360. struct mmc_omap_slot *slot = host->current_slot;
  361. struct mmc_data *data = host->stop_data;
  362. unsigned long tick_ns;
  363. tick_ns = (1000000000 + slot->fclk_freq - 1)/slot->fclk_freq;
  364. ndelay(8*tick_ns);
  365. mmc_omap_start_command(host, data->stop);
  366. }
  367. static void
  368. mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
  369. {
  370. if (host->dma_in_use)
  371. mmc_omap_release_dma(host, data, data->error);
  372. host->data = NULL;
  373. host->sg_len = 0;
  374. /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
  375. * dozens of requests until the card finishes writing data.
  376. * It'd be cheaper to just wait till an EOFB interrupt arrives...
  377. */
  378. if (!data->stop) {
  379. struct mmc_host *mmc;
  380. host->mrq = NULL;
  381. mmc = host->mmc;
  382. mmc_omap_release_slot(host->current_slot, 1);
  383. mmc_request_done(mmc, data->mrq);
  384. return;
  385. }
  386. host->stop_data = data;
  387. schedule_work(&host->send_stop_work);
  388. }
  389. static void
  390. mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
  391. {
  392. struct mmc_omap_slot *slot = host->current_slot;
  393. unsigned int restarts, passes, timeout;
  394. u16 stat = 0;
  395. /* Sending abort takes 80 clocks. Have some extra and round up */
  396. timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
  397. restarts = 0;
  398. while (restarts < maxloops) {
  399. OMAP_MMC_WRITE(host, STAT, 0xFFFF);
  400. OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
  401. passes = 0;
  402. while (passes < timeout) {
  403. stat = OMAP_MMC_READ(host, STAT);
  404. if (stat & OMAP_MMC_STAT_END_OF_CMD)
  405. goto out;
  406. udelay(1);
  407. passes++;
  408. }
  409. restarts++;
  410. }
  411. out:
  412. OMAP_MMC_WRITE(host, STAT, stat);
  413. }
  414. static void
  415. mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
  416. {
  417. if (host->dma_in_use)
  418. mmc_omap_release_dma(host, data, 1);
  419. host->data = NULL;
  420. host->sg_len = 0;
  421. mmc_omap_send_abort(host, 10000);
  422. }
  423. static void
  424. mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
  425. {
  426. unsigned long flags;
  427. int done;
  428. if (!host->dma_in_use) {
  429. mmc_omap_xfer_done(host, data);
  430. return;
  431. }
  432. done = 0;
  433. spin_lock_irqsave(&host->dma_lock, flags);
  434. if (host->dma_done)
  435. done = 1;
  436. else
  437. host->brs_received = 1;
  438. spin_unlock_irqrestore(&host->dma_lock, flags);
  439. if (done)
  440. mmc_omap_xfer_done(host, data);
  441. }
  442. static void
  443. mmc_omap_dma_timer(unsigned long data)
  444. {
  445. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  446. BUG_ON(host->dma_ch < 0);
  447. omap_free_dma(host->dma_ch);
  448. host->dma_ch = -1;
  449. }
  450. static void
  451. mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
  452. {
  453. unsigned long flags;
  454. int done;
  455. done = 0;
  456. spin_lock_irqsave(&host->dma_lock, flags);
  457. if (host->brs_received)
  458. done = 1;
  459. else
  460. host->dma_done = 1;
  461. spin_unlock_irqrestore(&host->dma_lock, flags);
  462. if (done)
  463. mmc_omap_xfer_done(host, data);
  464. }
  465. static void
  466. mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
  467. {
  468. host->cmd = NULL;
  469. del_timer(&host->cmd_abort_timer);
  470. if (cmd->flags & MMC_RSP_PRESENT) {
  471. if (cmd->flags & MMC_RSP_136) {
  472. /* response type 2 */
  473. cmd->resp[3] =
  474. OMAP_MMC_READ(host, RSP0) |
  475. (OMAP_MMC_READ(host, RSP1) << 16);
  476. cmd->resp[2] =
  477. OMAP_MMC_READ(host, RSP2) |
  478. (OMAP_MMC_READ(host, RSP3) << 16);
  479. cmd->resp[1] =
  480. OMAP_MMC_READ(host, RSP4) |
  481. (OMAP_MMC_READ(host, RSP5) << 16);
  482. cmd->resp[0] =
  483. OMAP_MMC_READ(host, RSP6) |
  484. (OMAP_MMC_READ(host, RSP7) << 16);
  485. } else {
  486. /* response types 1, 1b, 3, 4, 5, 6 */
  487. cmd->resp[0] =
  488. OMAP_MMC_READ(host, RSP6) |
  489. (OMAP_MMC_READ(host, RSP7) << 16);
  490. }
  491. }
  492. if (host->data == NULL || cmd->error) {
  493. struct mmc_host *mmc;
  494. if (host->data != NULL)
  495. mmc_omap_abort_xfer(host, host->data);
  496. host->mrq = NULL;
  497. mmc = host->mmc;
  498. mmc_omap_release_slot(host->current_slot, 1);
  499. mmc_request_done(mmc, cmd->mrq);
  500. }
  501. }
  502. /*
  503. * Abort stuck command. Can occur when card is removed while it is being
  504. * read.
  505. */
  506. static void mmc_omap_abort_command(struct work_struct *work)
  507. {
  508. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  509. cmd_abort_work);
  510. BUG_ON(!host->cmd);
  511. dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
  512. host->cmd->opcode);
  513. if (host->cmd->error == 0)
  514. host->cmd->error = -ETIMEDOUT;
  515. if (host->data == NULL) {
  516. struct mmc_command *cmd;
  517. struct mmc_host *mmc;
  518. cmd = host->cmd;
  519. host->cmd = NULL;
  520. mmc_omap_send_abort(host, 10000);
  521. host->mrq = NULL;
  522. mmc = host->mmc;
  523. mmc_omap_release_slot(host->current_slot, 1);
  524. mmc_request_done(mmc, cmd->mrq);
  525. } else
  526. mmc_omap_cmd_done(host, host->cmd);
  527. host->abort = 0;
  528. enable_irq(host->irq);
  529. }
  530. static void
  531. mmc_omap_cmd_timer(unsigned long data)
  532. {
  533. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  534. unsigned long flags;
  535. spin_lock_irqsave(&host->slot_lock, flags);
  536. if (host->cmd != NULL && !host->abort) {
  537. OMAP_MMC_WRITE(host, IE, 0);
  538. disable_irq(host->irq);
  539. host->abort = 1;
  540. schedule_work(&host->cmd_abort_work);
  541. }
  542. spin_unlock_irqrestore(&host->slot_lock, flags);
  543. }
  544. /* PIO only */
  545. static void
  546. mmc_omap_sg_to_buf(struct mmc_omap_host *host)
  547. {
  548. struct scatterlist *sg;
  549. sg = host->data->sg + host->sg_idx;
  550. host->buffer_bytes_left = sg->length;
  551. host->buffer = sg_virt(sg);
  552. if (host->buffer_bytes_left > host->total_bytes_left)
  553. host->buffer_bytes_left = host->total_bytes_left;
  554. }
  555. static void
  556. mmc_omap_clk_timer(unsigned long data)
  557. {
  558. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  559. mmc_omap_fclk_enable(host, 0);
  560. }
  561. /* PIO only */
  562. static void
  563. mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
  564. {
  565. int n;
  566. if (host->buffer_bytes_left == 0) {
  567. host->sg_idx++;
  568. BUG_ON(host->sg_idx == host->sg_len);
  569. mmc_omap_sg_to_buf(host);
  570. }
  571. n = 64;
  572. if (n > host->buffer_bytes_left)
  573. n = host->buffer_bytes_left;
  574. host->buffer_bytes_left -= n;
  575. host->total_bytes_left -= n;
  576. host->data->bytes_xfered += n;
  577. if (write) {
  578. __raw_writesw(host->virt_base + OMAP_MMC_REG(host, DATA), host->buffer, n);
  579. } else {
  580. __raw_readsw(host->virt_base + OMAP_MMC_REG(host, DATA), host->buffer, n);
  581. }
  582. }
  583. static inline void mmc_omap_report_irq(u16 status)
  584. {
  585. static const char *mmc_omap_status_bits[] = {
  586. "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
  587. "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
  588. };
  589. int i, c = 0;
  590. for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
  591. if (status & (1 << i)) {
  592. if (c)
  593. printk(" ");
  594. printk("%s", mmc_omap_status_bits[i]);
  595. c++;
  596. }
  597. }
  598. static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
  599. {
  600. struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
  601. u16 status;
  602. int end_command;
  603. int end_transfer;
  604. int transfer_error, cmd_error;
  605. if (host->cmd == NULL && host->data == NULL) {
  606. status = OMAP_MMC_READ(host, STAT);
  607. dev_info(mmc_dev(host->slots[0]->mmc),
  608. "Spurious IRQ 0x%04x\n", status);
  609. if (status != 0) {
  610. OMAP_MMC_WRITE(host, STAT, status);
  611. OMAP_MMC_WRITE(host, IE, 0);
  612. }
  613. return IRQ_HANDLED;
  614. }
  615. end_command = 0;
  616. end_transfer = 0;
  617. transfer_error = 0;
  618. cmd_error = 0;
  619. while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
  620. int cmd;
  621. OMAP_MMC_WRITE(host, STAT, status);
  622. if (host->cmd != NULL)
  623. cmd = host->cmd->opcode;
  624. else
  625. cmd = -1;
  626. #ifdef CONFIG_MMC_DEBUG
  627. dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
  628. status, cmd);
  629. mmc_omap_report_irq(status);
  630. printk("\n");
  631. #endif
  632. if (host->total_bytes_left) {
  633. if ((status & OMAP_MMC_STAT_A_FULL) ||
  634. (status & OMAP_MMC_STAT_END_OF_DATA))
  635. mmc_omap_xfer_data(host, 0);
  636. if (status & OMAP_MMC_STAT_A_EMPTY)
  637. mmc_omap_xfer_data(host, 1);
  638. }
  639. if (status & OMAP_MMC_STAT_END_OF_DATA)
  640. end_transfer = 1;
  641. if (status & OMAP_MMC_STAT_DATA_TOUT) {
  642. dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
  643. cmd);
  644. if (host->data) {
  645. host->data->error = -ETIMEDOUT;
  646. transfer_error = 1;
  647. }
  648. }
  649. if (status & OMAP_MMC_STAT_DATA_CRC) {
  650. if (host->data) {
  651. host->data->error = -EILSEQ;
  652. dev_dbg(mmc_dev(host->mmc),
  653. "data CRC error, bytes left %d\n",
  654. host->total_bytes_left);
  655. transfer_error = 1;
  656. } else {
  657. dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
  658. }
  659. }
  660. if (status & OMAP_MMC_STAT_CMD_TOUT) {
  661. /* Timeouts are routine with some commands */
  662. if (host->cmd) {
  663. struct mmc_omap_slot *slot =
  664. host->current_slot;
  665. if (slot == NULL ||
  666. !mmc_omap_cover_is_open(slot))
  667. dev_err(mmc_dev(host->mmc),
  668. "command timeout (CMD%d)\n",
  669. cmd);
  670. host->cmd->error = -ETIMEDOUT;
  671. end_command = 1;
  672. cmd_error = 1;
  673. }
  674. }
  675. if (status & OMAP_MMC_STAT_CMD_CRC) {
  676. if (host->cmd) {
  677. dev_err(mmc_dev(host->mmc),
  678. "command CRC error (CMD%d, arg 0x%08x)\n",
  679. cmd, host->cmd->arg);
  680. host->cmd->error = -EILSEQ;
  681. end_command = 1;
  682. cmd_error = 1;
  683. } else
  684. dev_err(mmc_dev(host->mmc),
  685. "command CRC error without cmd?\n");
  686. }
  687. if (status & OMAP_MMC_STAT_CARD_ERR) {
  688. dev_dbg(mmc_dev(host->mmc),
  689. "ignoring card status error (CMD%d)\n",
  690. cmd);
  691. end_command = 1;
  692. }
  693. /*
  694. * NOTE: On 1610 the END_OF_CMD may come too early when
  695. * starting a write
  696. */
  697. if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
  698. (!(status & OMAP_MMC_STAT_A_EMPTY))) {
  699. end_command = 1;
  700. }
  701. }
  702. if (cmd_error && host->data) {
  703. del_timer(&host->cmd_abort_timer);
  704. host->abort = 1;
  705. OMAP_MMC_WRITE(host, IE, 0);
  706. disable_irq_nosync(host->irq);
  707. schedule_work(&host->cmd_abort_work);
  708. return IRQ_HANDLED;
  709. }
  710. if (end_command)
  711. mmc_omap_cmd_done(host, host->cmd);
  712. if (host->data != NULL) {
  713. if (transfer_error)
  714. mmc_omap_xfer_done(host, host->data);
  715. else if (end_transfer)
  716. mmc_omap_end_of_data(host, host->data);
  717. }
  718. return IRQ_HANDLED;
  719. }
  720. void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
  721. {
  722. int cover_open;
  723. struct mmc_omap_host *host = dev_get_drvdata(dev);
  724. struct mmc_omap_slot *slot = host->slots[num];
  725. BUG_ON(num >= host->nr_slots);
  726. /* Other subsystems can call in here before we're initialised. */
  727. if (host->nr_slots == 0 || !host->slots[num])
  728. return;
  729. cover_open = mmc_omap_cover_is_open(slot);
  730. if (cover_open != slot->cover_open) {
  731. slot->cover_open = cover_open;
  732. sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
  733. }
  734. tasklet_hi_schedule(&slot->cover_tasklet);
  735. }
  736. static void mmc_omap_cover_timer(unsigned long arg)
  737. {
  738. struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
  739. tasklet_schedule(&slot->cover_tasklet);
  740. }
  741. static void mmc_omap_cover_handler(unsigned long param)
  742. {
  743. struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
  744. int cover_open = mmc_omap_cover_is_open(slot);
  745. mmc_detect_change(slot->mmc, 0);
  746. if (!cover_open)
  747. return;
  748. /*
  749. * If no card is inserted, we postpone polling until
  750. * the cover has been closed.
  751. */
  752. if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
  753. return;
  754. mod_timer(&slot->cover_timer,
  755. jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
  756. }
  757. /* Prepare to transfer the next segment of a scatterlist */
  758. static void
  759. mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
  760. {
  761. int dma_ch = host->dma_ch;
  762. unsigned long data_addr;
  763. u16 buf, frame;
  764. u32 count;
  765. struct scatterlist *sg = &data->sg[host->sg_idx];
  766. int src_port = 0;
  767. int dst_port = 0;
  768. int sync_dev = 0;
  769. data_addr = host->phys_base + OMAP_MMC_REG(host, DATA);
  770. frame = data->blksz;
  771. count = sg_dma_len(sg);
  772. if ((data->blocks == 1) && (count > data->blksz))
  773. count = frame;
  774. host->dma_len = count;
  775. /* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
  776. * Use 16 or 32 word frames when the blocksize is at least that large.
  777. * Blocksize is usually 512 bytes; but not for some SD reads.
  778. */
  779. if (cpu_is_omap15xx() && frame > 32)
  780. frame = 32;
  781. else if (frame > 64)
  782. frame = 64;
  783. count /= frame;
  784. frame >>= 1;
  785. if (!(data->flags & MMC_DATA_WRITE)) {
  786. buf = 0x800f | ((frame - 1) << 8);
  787. if (cpu_class_is_omap1()) {
  788. src_port = OMAP_DMA_PORT_TIPB;
  789. dst_port = OMAP_DMA_PORT_EMIFF;
  790. }
  791. if (cpu_is_omap24xx())
  792. sync_dev = OMAP24XX_DMA_MMC1_RX;
  793. omap_set_dma_src_params(dma_ch, src_port,
  794. OMAP_DMA_AMODE_CONSTANT,
  795. data_addr, 0, 0);
  796. omap_set_dma_dest_params(dma_ch, dst_port,
  797. OMAP_DMA_AMODE_POST_INC,
  798. sg_dma_address(sg), 0, 0);
  799. omap_set_dma_dest_data_pack(dma_ch, 1);
  800. omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
  801. } else {
  802. buf = 0x0f80 | ((frame - 1) << 0);
  803. if (cpu_class_is_omap1()) {
  804. src_port = OMAP_DMA_PORT_EMIFF;
  805. dst_port = OMAP_DMA_PORT_TIPB;
  806. }
  807. if (cpu_is_omap24xx())
  808. sync_dev = OMAP24XX_DMA_MMC1_TX;
  809. omap_set_dma_dest_params(dma_ch, dst_port,
  810. OMAP_DMA_AMODE_CONSTANT,
  811. data_addr, 0, 0);
  812. omap_set_dma_src_params(dma_ch, src_port,
  813. OMAP_DMA_AMODE_POST_INC,
  814. sg_dma_address(sg), 0, 0);
  815. omap_set_dma_src_data_pack(dma_ch, 1);
  816. omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
  817. }
  818. /* Max limit for DMA frame count is 0xffff */
  819. BUG_ON(count > 0xffff);
  820. OMAP_MMC_WRITE(host, BUF, buf);
  821. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
  822. frame, count, OMAP_DMA_SYNC_FRAME,
  823. sync_dev, 0);
  824. }
  825. /* A scatterlist segment completed */
  826. static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
  827. {
  828. struct mmc_omap_host *host = (struct mmc_omap_host *) data;
  829. struct mmc_data *mmcdat = host->data;
  830. if (unlikely(host->dma_ch < 0)) {
  831. dev_err(mmc_dev(host->mmc),
  832. "DMA callback while DMA not enabled\n");
  833. return;
  834. }
  835. /* FIXME: We really should do something to _handle_ the errors */
  836. if (ch_status & OMAP1_DMA_TOUT_IRQ) {
  837. dev_err(mmc_dev(host->mmc),"DMA timeout\n");
  838. return;
  839. }
  840. if (ch_status & OMAP_DMA_DROP_IRQ) {
  841. dev_err(mmc_dev(host->mmc), "DMA sync error\n");
  842. return;
  843. }
  844. if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
  845. return;
  846. }
  847. mmcdat->bytes_xfered += host->dma_len;
  848. host->sg_idx++;
  849. if (host->sg_idx < host->sg_len) {
  850. mmc_omap_prepare_dma(host, host->data);
  851. omap_start_dma(host->dma_ch);
  852. } else
  853. mmc_omap_dma_done(host, host->data);
  854. }
  855. static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
  856. {
  857. const char *dma_dev_name;
  858. int sync_dev, dma_ch, is_read, r;
  859. is_read = !(data->flags & MMC_DATA_WRITE);
  860. del_timer_sync(&host->dma_timer);
  861. if (host->dma_ch >= 0) {
  862. if (is_read == host->dma_is_read)
  863. return 0;
  864. omap_free_dma(host->dma_ch);
  865. host->dma_ch = -1;
  866. }
  867. if (is_read) {
  868. if (host->id == 0) {
  869. sync_dev = OMAP_DMA_MMC_RX;
  870. dma_dev_name = "MMC1 read";
  871. } else {
  872. sync_dev = OMAP_DMA_MMC2_RX;
  873. dma_dev_name = "MMC2 read";
  874. }
  875. } else {
  876. if (host->id == 0) {
  877. sync_dev = OMAP_DMA_MMC_TX;
  878. dma_dev_name = "MMC1 write";
  879. } else {
  880. sync_dev = OMAP_DMA_MMC2_TX;
  881. dma_dev_name = "MMC2 write";
  882. }
  883. }
  884. r = omap_request_dma(sync_dev, dma_dev_name, mmc_omap_dma_cb,
  885. host, &dma_ch);
  886. if (r != 0) {
  887. dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
  888. return r;
  889. }
  890. host->dma_ch = dma_ch;
  891. host->dma_is_read = is_read;
  892. return 0;
  893. }
  894. static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  895. {
  896. u16 reg;
  897. reg = OMAP_MMC_READ(host, SDIO);
  898. reg &= ~(1 << 5);
  899. OMAP_MMC_WRITE(host, SDIO, reg);
  900. /* Set maximum timeout */
  901. OMAP_MMC_WRITE(host, CTO, 0xff);
  902. }
  903. static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  904. {
  905. unsigned int timeout, cycle_ns;
  906. u16 reg;
  907. cycle_ns = 1000000000 / host->current_slot->fclk_freq;
  908. timeout = req->data->timeout_ns / cycle_ns;
  909. timeout += req->data->timeout_clks;
  910. /* Check if we need to use timeout multiplier register */
  911. reg = OMAP_MMC_READ(host, SDIO);
  912. if (timeout > 0xffff) {
  913. reg |= (1 << 5);
  914. timeout /= 1024;
  915. } else
  916. reg &= ~(1 << 5);
  917. OMAP_MMC_WRITE(host, SDIO, reg);
  918. OMAP_MMC_WRITE(host, DTO, timeout);
  919. }
  920. static void
  921. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  922. {
  923. struct mmc_data *data = req->data;
  924. int i, use_dma, block_size;
  925. unsigned sg_len;
  926. host->data = data;
  927. if (data == NULL) {
  928. OMAP_MMC_WRITE(host, BLEN, 0);
  929. OMAP_MMC_WRITE(host, NBLK, 0);
  930. OMAP_MMC_WRITE(host, BUF, 0);
  931. host->dma_in_use = 0;
  932. set_cmd_timeout(host, req);
  933. return;
  934. }
  935. block_size = data->blksz;
  936. OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
  937. OMAP_MMC_WRITE(host, BLEN, block_size - 1);
  938. set_data_timeout(host, req);
  939. /* cope with calling layer confusion; it issues "single
  940. * block" writes using multi-block scatterlists.
  941. */
  942. sg_len = (data->blocks == 1) ? 1 : data->sg_len;
  943. /* Only do DMA for entire blocks */
  944. use_dma = host->use_dma;
  945. if (use_dma) {
  946. for (i = 0; i < sg_len; i++) {
  947. if ((data->sg[i].length % block_size) != 0) {
  948. use_dma = 0;
  949. break;
  950. }
  951. }
  952. }
  953. host->sg_idx = 0;
  954. if (use_dma) {
  955. if (mmc_omap_get_dma_channel(host, data) == 0) {
  956. enum dma_data_direction dma_data_dir;
  957. if (data->flags & MMC_DATA_WRITE)
  958. dma_data_dir = DMA_TO_DEVICE;
  959. else
  960. dma_data_dir = DMA_FROM_DEVICE;
  961. host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  962. sg_len, dma_data_dir);
  963. host->total_bytes_left = 0;
  964. mmc_omap_prepare_dma(host, req->data);
  965. host->brs_received = 0;
  966. host->dma_done = 0;
  967. host->dma_in_use = 1;
  968. } else
  969. use_dma = 0;
  970. }
  971. /* Revert to PIO? */
  972. if (!use_dma) {
  973. OMAP_MMC_WRITE(host, BUF, 0x1f1f);
  974. host->total_bytes_left = data->blocks * block_size;
  975. host->sg_len = sg_len;
  976. mmc_omap_sg_to_buf(host);
  977. host->dma_in_use = 0;
  978. }
  979. }
  980. static void mmc_omap_start_request(struct mmc_omap_host *host,
  981. struct mmc_request *req)
  982. {
  983. BUG_ON(host->mrq != NULL);
  984. host->mrq = req;
  985. /* only touch fifo AFTER the controller readies it */
  986. mmc_omap_prepare_data(host, req);
  987. mmc_omap_start_command(host, req->cmd);
  988. if (host->dma_in_use)
  989. omap_start_dma(host->dma_ch);
  990. }
  991. static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
  992. {
  993. struct mmc_omap_slot *slot = mmc_priv(mmc);
  994. struct mmc_omap_host *host = slot->host;
  995. unsigned long flags;
  996. spin_lock_irqsave(&host->slot_lock, flags);
  997. if (host->mmc != NULL) {
  998. BUG_ON(slot->mrq != NULL);
  999. slot->mrq = req;
  1000. spin_unlock_irqrestore(&host->slot_lock, flags);
  1001. return;
  1002. } else
  1003. host->mmc = mmc;
  1004. spin_unlock_irqrestore(&host->slot_lock, flags);
  1005. mmc_omap_select_slot(slot, 1);
  1006. mmc_omap_start_request(host, req);
  1007. }
  1008. static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
  1009. int vdd)
  1010. {
  1011. struct mmc_omap_host *host;
  1012. host = slot->host;
  1013. if (slot->pdata->set_power != NULL)
  1014. slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
  1015. vdd);
  1016. if (cpu_is_omap24xx()) {
  1017. u16 w;
  1018. if (power_on) {
  1019. w = OMAP_MMC_READ(host, CON);
  1020. OMAP_MMC_WRITE(host, CON, w | (1 << 11));
  1021. } else {
  1022. w = OMAP_MMC_READ(host, CON);
  1023. OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
  1024. }
  1025. }
  1026. }
  1027. static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
  1028. {
  1029. struct mmc_omap_slot *slot = mmc_priv(mmc);
  1030. struct mmc_omap_host *host = slot->host;
  1031. int func_clk_rate = clk_get_rate(host->fclk);
  1032. int dsor;
  1033. if (ios->clock == 0)
  1034. return 0;
  1035. dsor = func_clk_rate / ios->clock;
  1036. if (dsor < 1)
  1037. dsor = 1;
  1038. if (func_clk_rate / dsor > ios->clock)
  1039. dsor++;
  1040. if (dsor > 250)
  1041. dsor = 250;
  1042. slot->fclk_freq = func_clk_rate / dsor;
  1043. if (ios->bus_width == MMC_BUS_WIDTH_4)
  1044. dsor |= 1 << 15;
  1045. return dsor;
  1046. }
  1047. static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1048. {
  1049. struct mmc_omap_slot *slot = mmc_priv(mmc);
  1050. struct mmc_omap_host *host = slot->host;
  1051. int i, dsor;
  1052. int clk_enabled;
  1053. mmc_omap_select_slot(slot, 0);
  1054. dsor = mmc_omap_calc_divisor(mmc, ios);
  1055. if (ios->vdd != slot->vdd)
  1056. slot->vdd = ios->vdd;
  1057. clk_enabled = 0;
  1058. switch (ios->power_mode) {
  1059. case MMC_POWER_OFF:
  1060. mmc_omap_set_power(slot, 0, ios->vdd);
  1061. break;
  1062. case MMC_POWER_UP:
  1063. /* Cannot touch dsor yet, just power up MMC */
  1064. mmc_omap_set_power(slot, 1, ios->vdd);
  1065. goto exit;
  1066. case MMC_POWER_ON:
  1067. mmc_omap_fclk_enable(host, 1);
  1068. clk_enabled = 1;
  1069. dsor |= 1 << 11;
  1070. break;
  1071. }
  1072. if (slot->bus_mode != ios->bus_mode) {
  1073. if (slot->pdata->set_bus_mode != NULL)
  1074. slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
  1075. ios->bus_mode);
  1076. slot->bus_mode = ios->bus_mode;
  1077. }
  1078. /* On insanely high arm_per frequencies something sometimes
  1079. * goes somehow out of sync, and the POW bit is not being set,
  1080. * which results in the while loop below getting stuck.
  1081. * Writing to the CON register twice seems to do the trick. */
  1082. for (i = 0; i < 2; i++)
  1083. OMAP_MMC_WRITE(host, CON, dsor);
  1084. slot->saved_con = dsor;
  1085. if (ios->power_mode == MMC_POWER_ON) {
  1086. /* worst case at 400kHz, 80 cycles makes 200 microsecs */
  1087. int usecs = 250;
  1088. /* Send clock cycles, poll completion */
  1089. OMAP_MMC_WRITE(host, IE, 0);
  1090. OMAP_MMC_WRITE(host, STAT, 0xffff);
  1091. OMAP_MMC_WRITE(host, CMD, 1 << 7);
  1092. while (usecs > 0 && (OMAP_MMC_READ(host, STAT) & 1) == 0) {
  1093. udelay(1);
  1094. usecs--;
  1095. }
  1096. OMAP_MMC_WRITE(host, STAT, 1);
  1097. }
  1098. exit:
  1099. mmc_omap_release_slot(slot, clk_enabled);
  1100. }
  1101. static const struct mmc_host_ops mmc_omap_ops = {
  1102. .request = mmc_omap_request,
  1103. .set_ios = mmc_omap_set_ios,
  1104. };
  1105. static int __init mmc_omap_new_slot(struct mmc_omap_host *host, int id)
  1106. {
  1107. struct mmc_omap_slot *slot = NULL;
  1108. struct mmc_host *mmc;
  1109. int r;
  1110. mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
  1111. if (mmc == NULL)
  1112. return -ENOMEM;
  1113. slot = mmc_priv(mmc);
  1114. slot->host = host;
  1115. slot->mmc = mmc;
  1116. slot->id = id;
  1117. slot->pdata = &host->pdata->slots[id];
  1118. host->slots[id] = slot;
  1119. mmc->caps = 0;
  1120. if (host->pdata->slots[id].wires >= 4)
  1121. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1122. mmc->ops = &mmc_omap_ops;
  1123. mmc->f_min = 400000;
  1124. if (cpu_class_is_omap2())
  1125. mmc->f_max = 48000000;
  1126. else
  1127. mmc->f_max = 24000000;
  1128. if (host->pdata->max_freq)
  1129. mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
  1130. mmc->ocr_avail = slot->pdata->ocr_mask;
  1131. /* Use scatterlist DMA to reduce per-transfer costs.
  1132. * NOTE max_seg_size assumption that small blocks aren't
  1133. * normally used (except e.g. for reading SD registers).
  1134. */
  1135. mmc->max_phys_segs = 32;
  1136. mmc->max_hw_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_scheduled_work();
  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, res->end - res->start + 1,
  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, res->end - res->start + 1);
  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, res->end - res->start + 1);
  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. return platform_driver_probe(&mmc_omap_driver, mmc_omap_probe);
  1359. }
  1360. static void __exit mmc_omap_exit(void)
  1361. {
  1362. platform_driver_unregister(&mmc_omap_driver);
  1363. }
  1364. module_init(mmc_omap_init);
  1365. module_exit(mmc_omap_exit);
  1366. MODULE_DESCRIPTION("OMAP Multimedia Card driver");
  1367. MODULE_LICENSE("GPL");
  1368. MODULE_ALIAS("platform:" DRIVER_NAME);
  1369. MODULE_AUTHOR("Juha Yrjölä");