omap.c 38 KB

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