omap.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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. timer_delete(&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. timer_delete(&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, 0444, 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, 0444, 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. timer_delete(&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 = timer_container_of(host, t,
  546. cmd_abort_timer);
  547. unsigned long flags;
  548. spin_lock_irqsave(&host->slot_lock, flags);
  549. if (host->cmd != NULL && !host->abort) {
  550. OMAP_MMC_WRITE(host, IE, 0);
  551. disable_irq(host->irq);
  552. host->abort = 1;
  553. queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
  554. }
  555. spin_unlock_irqrestore(&host->slot_lock, flags);
  556. }
  557. static void
  558. mmc_omap_clk_timer(struct timer_list *t)
  559. {
  560. struct mmc_omap_host *host = timer_container_of(host, t, clk_timer);
  561. mmc_omap_fclk_enable(host, 0);
  562. }
  563. /* PIO only */
  564. static void
  565. mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
  566. {
  567. struct sg_mapping_iter *sgm = &host->sg_miter;
  568. int n, nwords;
  569. u16 *buffer;
  570. if (!sg_miter_next(sgm)) {
  571. /* This should not happen */
  572. dev_err(mmc_dev(host->mmc), "ran out of scatterlist prematurely\n");
  573. return;
  574. }
  575. buffer = sgm->addr;
  576. n = 64;
  577. if (n > sgm->length)
  578. n = sgm->length;
  579. if (n > host->total_bytes_left)
  580. n = host->total_bytes_left;
  581. /* Round up to handle odd number of bytes to transfer */
  582. nwords = DIV_ROUND_UP(n, 2);
  583. sgm->consumed = n;
  584. host->total_bytes_left -= n;
  585. host->data->bytes_xfered += n;
  586. if (write) {
  587. __raw_writesw(host->virt_base + OMAP_MMC_REG(host, DATA),
  588. buffer, nwords);
  589. } else {
  590. __raw_readsw(host->virt_base + OMAP_MMC_REG(host, DATA),
  591. buffer, nwords);
  592. }
  593. }
  594. #ifdef CONFIG_MMC_DEBUG
  595. static void mmc_omap_report_irq(struct mmc_omap_host *host, u16 status)
  596. {
  597. static const char *mmc_omap_status_bits[] = {
  598. "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
  599. "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
  600. };
  601. int i;
  602. char res[64], *buf = res;
  603. buf += sprintf(buf, "MMC IRQ 0x%x:", status);
  604. for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
  605. if (status & (1 << i))
  606. buf += sprintf(buf, " %s", mmc_omap_status_bits[i]);
  607. dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
  608. }
  609. #else
  610. static void mmc_omap_report_irq(struct mmc_omap_host *host, u16 status)
  611. {
  612. }
  613. #endif
  614. static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
  615. {
  616. struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
  617. u16 status;
  618. int end_command;
  619. int end_transfer;
  620. int transfer_error, cmd_error;
  621. if (host->cmd == NULL && host->data == NULL) {
  622. status = OMAP_MMC_READ(host, STAT);
  623. dev_info(mmc_dev(host->slots[0]->mmc),
  624. "Spurious IRQ 0x%04x\n", status);
  625. if (status != 0) {
  626. OMAP_MMC_WRITE(host, STAT, status);
  627. OMAP_MMC_WRITE(host, IE, 0);
  628. }
  629. return IRQ_HANDLED;
  630. }
  631. end_command = 0;
  632. end_transfer = 0;
  633. transfer_error = 0;
  634. cmd_error = 0;
  635. while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
  636. int cmd;
  637. OMAP_MMC_WRITE(host, STAT, status);
  638. if (host->cmd != NULL)
  639. cmd = host->cmd->opcode;
  640. else
  641. cmd = -1;
  642. dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
  643. status, cmd);
  644. mmc_omap_report_irq(host, status);
  645. if (host->total_bytes_left) {
  646. if ((status & OMAP_MMC_STAT_A_FULL) ||
  647. (status & OMAP_MMC_STAT_END_OF_DATA))
  648. mmc_omap_xfer_data(host, 0);
  649. if (status & OMAP_MMC_STAT_A_EMPTY)
  650. mmc_omap_xfer_data(host, 1);
  651. }
  652. if (status & OMAP_MMC_STAT_END_OF_DATA)
  653. end_transfer = 1;
  654. if (status & OMAP_MMC_STAT_DATA_TOUT) {
  655. dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
  656. cmd);
  657. if (host->data) {
  658. host->data->error = -ETIMEDOUT;
  659. transfer_error = 1;
  660. }
  661. }
  662. if (status & OMAP_MMC_STAT_DATA_CRC) {
  663. if (host->data) {
  664. host->data->error = -EILSEQ;
  665. dev_dbg(mmc_dev(host->mmc),
  666. "data CRC error, bytes left %d\n",
  667. host->total_bytes_left);
  668. transfer_error = 1;
  669. } else {
  670. dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
  671. }
  672. }
  673. if (status & OMAP_MMC_STAT_CMD_TOUT) {
  674. /* Timeouts are routine with some commands */
  675. if (host->cmd) {
  676. struct mmc_omap_slot *slot =
  677. host->current_slot;
  678. if (slot == NULL ||
  679. !mmc_omap_cover_is_open(slot))
  680. dev_err(mmc_dev(host->mmc),
  681. "command timeout (CMD%d)\n",
  682. cmd);
  683. host->cmd->error = -ETIMEDOUT;
  684. end_command = 1;
  685. cmd_error = 1;
  686. }
  687. }
  688. if (status & OMAP_MMC_STAT_CMD_CRC) {
  689. if (host->cmd) {
  690. dev_err(mmc_dev(host->mmc),
  691. "command CRC error (CMD%d, arg 0x%08x)\n",
  692. cmd, host->cmd->arg);
  693. host->cmd->error = -EILSEQ;
  694. end_command = 1;
  695. cmd_error = 1;
  696. } else
  697. dev_err(mmc_dev(host->mmc),
  698. "command CRC error without cmd?\n");
  699. }
  700. if (status & OMAP_MMC_STAT_CARD_ERR) {
  701. dev_dbg(mmc_dev(host->mmc),
  702. "ignoring card status error (CMD%d)\n",
  703. cmd);
  704. end_command = 1;
  705. }
  706. /*
  707. * NOTE: On 1610 the END_OF_CMD may come too early when
  708. * starting a write
  709. */
  710. if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
  711. (!(status & OMAP_MMC_STAT_A_EMPTY))) {
  712. end_command = 1;
  713. }
  714. }
  715. if (cmd_error && host->data) {
  716. timer_delete(&host->cmd_abort_timer);
  717. host->abort = 1;
  718. OMAP_MMC_WRITE(host, IE, 0);
  719. disable_irq_nosync(host->irq);
  720. queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
  721. return IRQ_HANDLED;
  722. }
  723. if (end_command && host->cmd)
  724. mmc_omap_cmd_done(host, host->cmd);
  725. if (host->data != NULL) {
  726. if (transfer_error)
  727. mmc_omap_xfer_done(host, host->data);
  728. else if (end_transfer)
  729. mmc_omap_end_of_data(host, host->data);
  730. }
  731. return IRQ_HANDLED;
  732. }
  733. void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
  734. {
  735. int cover_open;
  736. struct mmc_omap_host *host = dev_get_drvdata(dev);
  737. struct mmc_omap_slot *slot = host->slots[num];
  738. BUG_ON(num >= host->nr_slots);
  739. /* Other subsystems can call in here before we're initialised. */
  740. if (host->nr_slots == 0 || !host->slots[num])
  741. return;
  742. cover_open = mmc_omap_cover_is_open(slot);
  743. if (cover_open != slot->cover_open) {
  744. slot->cover_open = cover_open;
  745. sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
  746. }
  747. queue_work(system_bh_highpri_wq, &slot->cover_bh_work);
  748. }
  749. static void mmc_omap_cover_timer(struct timer_list *t)
  750. {
  751. struct mmc_omap_slot *slot = timer_container_of(slot, t, cover_timer);
  752. queue_work(system_bh_wq, &slot->cover_bh_work);
  753. }
  754. static void mmc_omap_cover_bh_handler(struct work_struct *t)
  755. {
  756. struct mmc_omap_slot *slot = from_work(slot, t, cover_bh_work);
  757. int cover_open = mmc_omap_cover_is_open(slot);
  758. mmc_detect_change(slot->mmc, 0);
  759. if (!cover_open)
  760. return;
  761. /*
  762. * If no card is inserted, we postpone polling until
  763. * the cover has been closed.
  764. */
  765. if (slot->mmc->card == NULL)
  766. return;
  767. mod_timer(&slot->cover_timer,
  768. jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
  769. }
  770. static void mmc_omap_dma_callback(void *priv)
  771. {
  772. struct mmc_omap_host *host = priv;
  773. struct mmc_data *data = host->data;
  774. /* If we got to the end of DMA, assume everything went well */
  775. data->bytes_xfered += data->blocks * data->blksz;
  776. mmc_omap_dma_done(host, data);
  777. }
  778. static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  779. {
  780. u16 reg;
  781. reg = OMAP_MMC_READ(host, SDIO);
  782. reg &= ~(1 << 5);
  783. OMAP_MMC_WRITE(host, SDIO, reg);
  784. /* Set maximum timeout */
  785. OMAP_MMC_WRITE(host, CTO, 0xfd);
  786. }
  787. static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
  788. {
  789. unsigned int timeout, cycle_ns;
  790. u16 reg;
  791. cycle_ns = 1000000000 / host->current_slot->fclk_freq;
  792. timeout = req->data->timeout_ns / cycle_ns;
  793. timeout += req->data->timeout_clks;
  794. /* Check if we need to use timeout multiplier register */
  795. reg = OMAP_MMC_READ(host, SDIO);
  796. if (timeout > 0xffff) {
  797. reg |= (1 << 5);
  798. timeout /= 1024;
  799. } else
  800. reg &= ~(1 << 5);
  801. OMAP_MMC_WRITE(host, SDIO, reg);
  802. OMAP_MMC_WRITE(host, DTO, timeout);
  803. }
  804. static void
  805. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  806. {
  807. unsigned int miter_flags = SG_MITER_ATOMIC; /* Used from IRQ */
  808. struct mmc_data *data = req->data;
  809. int i, use_dma = 1, block_size;
  810. struct scatterlist *sg;
  811. unsigned sg_len;
  812. host->data = data;
  813. if (data == NULL) {
  814. OMAP_MMC_WRITE(host, BLEN, 0);
  815. OMAP_MMC_WRITE(host, NBLK, 0);
  816. OMAP_MMC_WRITE(host, BUF, 0);
  817. host->dma_in_use = 0;
  818. set_cmd_timeout(host, req);
  819. return;
  820. }
  821. block_size = data->blksz;
  822. OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
  823. OMAP_MMC_WRITE(host, BLEN, block_size - 1);
  824. set_data_timeout(host, req);
  825. /* cope with calling layer confusion; it issues "single
  826. * block" writes using multi-block scatterlists.
  827. */
  828. sg_len = (data->blocks == 1) ? 1 : data->sg_len;
  829. /* Only do DMA for entire blocks */
  830. for_each_sg(data->sg, sg, sg_len, i) {
  831. if ((sg->length % block_size) != 0) {
  832. use_dma = 0;
  833. break;
  834. }
  835. }
  836. if (use_dma) {
  837. enum dma_data_direction dma_data_dir;
  838. struct dma_async_tx_descriptor *tx;
  839. struct dma_chan *c;
  840. u32 burst, *bp;
  841. u16 buf;
  842. /*
  843. * FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx
  844. * and 24xx. Use 16 or 32 word frames when the
  845. * blocksize is at least that large. Blocksize is
  846. * usually 512 bytes; but not for some SD reads.
  847. */
  848. burst = mmc_omap15xx() ? 32 : 64;
  849. if (burst > data->blksz)
  850. burst = data->blksz;
  851. burst >>= 1;
  852. if (data->flags & MMC_DATA_WRITE) {
  853. c = host->dma_tx;
  854. bp = &host->dma_tx_burst;
  855. buf = 0x0f80 | (burst - 1) << 0;
  856. dma_data_dir = DMA_TO_DEVICE;
  857. } else {
  858. c = host->dma_rx;
  859. bp = &host->dma_rx_burst;
  860. buf = 0x800f | (burst - 1) << 8;
  861. dma_data_dir = DMA_FROM_DEVICE;
  862. }
  863. if (!c)
  864. goto use_pio;
  865. /* Only reconfigure if we have a different burst size */
  866. if (*bp != burst) {
  867. struct dma_slave_config cfg = {
  868. .src_addr = host->phys_base +
  869. OMAP_MMC_REG(host, DATA),
  870. .dst_addr = host->phys_base +
  871. OMAP_MMC_REG(host, DATA),
  872. .src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES,
  873. .dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES,
  874. .src_maxburst = burst,
  875. .dst_maxburst = burst,
  876. };
  877. if (dmaengine_slave_config(c, &cfg))
  878. goto use_pio;
  879. *bp = burst;
  880. }
  881. host->sg_len = dma_map_sg(c->device->dev, data->sg, sg_len,
  882. dma_data_dir);
  883. if (host->sg_len == 0)
  884. goto use_pio;
  885. tx = dmaengine_prep_slave_sg(c, data->sg, host->sg_len,
  886. data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  887. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  888. if (!tx)
  889. goto use_pio;
  890. OMAP_MMC_WRITE(host, BUF, buf);
  891. tx->callback = mmc_omap_dma_callback;
  892. tx->callback_param = host;
  893. dmaengine_submit(tx);
  894. host->brs_received = 0;
  895. host->dma_done = 0;
  896. host->dma_in_use = 1;
  897. return;
  898. }
  899. use_pio:
  900. /* Revert to PIO? */
  901. OMAP_MMC_WRITE(host, BUF, 0x1f1f);
  902. host->total_bytes_left = data->blocks * block_size;
  903. host->sg_len = sg_len;
  904. if (data->flags & MMC_DATA_READ)
  905. miter_flags |= SG_MITER_TO_SG;
  906. else
  907. miter_flags |= SG_MITER_FROM_SG;
  908. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, miter_flags);
  909. host->dma_in_use = 0;
  910. }
  911. static void mmc_omap_start_request(struct mmc_omap_host *host,
  912. struct mmc_request *req)
  913. {
  914. BUG_ON(host->mrq != NULL);
  915. host->mrq = req;
  916. /* only touch fifo AFTER the controller readies it */
  917. mmc_omap_prepare_data(host, req);
  918. mmc_omap_start_command(host, req->cmd);
  919. if (host->dma_in_use) {
  920. struct dma_chan *c = host->data->flags & MMC_DATA_WRITE ?
  921. host->dma_tx : host->dma_rx;
  922. dma_async_issue_pending(c);
  923. }
  924. }
  925. static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
  926. {
  927. struct mmc_omap_slot *slot = mmc_priv(mmc);
  928. struct mmc_omap_host *host = slot->host;
  929. unsigned long flags;
  930. spin_lock_irqsave(&host->slot_lock, flags);
  931. if (host->mmc != NULL) {
  932. BUG_ON(slot->mrq != NULL);
  933. slot->mrq = req;
  934. spin_unlock_irqrestore(&host->slot_lock, flags);
  935. return;
  936. } else
  937. host->mmc = mmc;
  938. spin_unlock_irqrestore(&host->slot_lock, flags);
  939. mmc_omap_select_slot(slot, 1);
  940. mmc_omap_start_request(host, req);
  941. }
  942. static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
  943. int vdd)
  944. {
  945. struct mmc_omap_host *host;
  946. host = slot->host;
  947. if (power_on) {
  948. if (slot->vsd) {
  949. gpiod_set_value(slot->vsd, power_on);
  950. msleep(1);
  951. }
  952. if (slot->vio) {
  953. gpiod_set_value(slot->vio, power_on);
  954. msleep(1);
  955. }
  956. } else {
  957. if (slot->vio) {
  958. gpiod_set_value(slot->vio, power_on);
  959. msleep(50);
  960. }
  961. if (slot->vsd) {
  962. gpiod_set_value(slot->vsd, power_on);
  963. msleep(50);
  964. }
  965. }
  966. if (slot->pdata->set_power != NULL)
  967. slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
  968. vdd);
  969. if (mmc_omap2()) {
  970. u16 w;
  971. if (power_on) {
  972. w = OMAP_MMC_READ(host, CON);
  973. OMAP_MMC_WRITE(host, CON, w | (1 << 11));
  974. } else {
  975. w = OMAP_MMC_READ(host, CON);
  976. OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
  977. }
  978. }
  979. }
  980. static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
  981. {
  982. struct mmc_omap_slot *slot = mmc_priv(mmc);
  983. struct mmc_omap_host *host = slot->host;
  984. int func_clk_rate = clk_get_rate(host->fclk);
  985. int dsor;
  986. if (ios->clock == 0)
  987. return 0;
  988. dsor = func_clk_rate / ios->clock;
  989. if (dsor < 1)
  990. dsor = 1;
  991. if (func_clk_rate / dsor > ios->clock)
  992. dsor++;
  993. if (dsor > 250)
  994. dsor = 250;
  995. slot->fclk_freq = func_clk_rate / dsor;
  996. if (ios->bus_width == MMC_BUS_WIDTH_4)
  997. dsor |= 1 << 15;
  998. return dsor;
  999. }
  1000. static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1001. {
  1002. struct mmc_omap_slot *slot = mmc_priv(mmc);
  1003. struct mmc_omap_host *host = slot->host;
  1004. int i, dsor;
  1005. int clk_enabled, init_stream;
  1006. mmc_omap_select_slot(slot, 0);
  1007. dsor = mmc_omap_calc_divisor(mmc, ios);
  1008. if (ios->vdd != slot->vdd)
  1009. slot->vdd = ios->vdd;
  1010. clk_enabled = 0;
  1011. init_stream = 0;
  1012. switch (ios->power_mode) {
  1013. case MMC_POWER_OFF:
  1014. mmc_omap_set_power(slot, 0, ios->vdd);
  1015. break;
  1016. case MMC_POWER_UP:
  1017. /* Cannot touch dsor yet, just power up MMC */
  1018. mmc_omap_set_power(slot, 1, ios->vdd);
  1019. slot->power_mode = ios->power_mode;
  1020. goto exit;
  1021. case MMC_POWER_ON:
  1022. mmc_omap_fclk_enable(host, 1);
  1023. clk_enabled = 1;
  1024. dsor |= 1 << 11;
  1025. if (slot->power_mode != MMC_POWER_ON)
  1026. init_stream = 1;
  1027. break;
  1028. }
  1029. slot->power_mode = ios->power_mode;
  1030. if (slot->bus_mode != ios->bus_mode) {
  1031. if (slot->pdata->set_bus_mode != NULL)
  1032. slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
  1033. ios->bus_mode);
  1034. slot->bus_mode = ios->bus_mode;
  1035. }
  1036. /* On insanely high arm_per frequencies something sometimes
  1037. * goes somehow out of sync, and the POW bit is not being set,
  1038. * which results in the while loop below getting stuck.
  1039. * Writing to the CON register twice seems to do the trick. */
  1040. for (i = 0; i < 2; i++)
  1041. OMAP_MMC_WRITE(host, CON, dsor);
  1042. slot->saved_con = dsor;
  1043. if (init_stream) {
  1044. /* worst case at 400kHz, 80 cycles makes 200 microsecs */
  1045. int usecs = 250;
  1046. /* Send clock cycles, poll completion */
  1047. OMAP_MMC_WRITE(host, IE, 0);
  1048. OMAP_MMC_WRITE(host, STAT, 0xffff);
  1049. OMAP_MMC_WRITE(host, CMD, 1 << 7);
  1050. while (usecs > 0 && (OMAP_MMC_READ(host, STAT) & 1) == 0) {
  1051. udelay(1);
  1052. usecs--;
  1053. }
  1054. OMAP_MMC_WRITE(host, STAT, 1);
  1055. }
  1056. exit:
  1057. mmc_omap_release_slot(slot, clk_enabled);
  1058. }
  1059. static const struct mmc_host_ops mmc_omap_ops = {
  1060. .request = mmc_omap_request,
  1061. .set_ios = mmc_omap_set_ios,
  1062. };
  1063. static int mmc_omap_new_slot(struct mmc_omap_host *host, int id)
  1064. {
  1065. struct mmc_omap_slot *slot = NULL;
  1066. struct mmc_host *mmc;
  1067. int r;
  1068. mmc = devm_mmc_alloc_host(host->dev, sizeof(*slot));
  1069. if (mmc == NULL)
  1070. return -ENOMEM;
  1071. slot = mmc_priv(mmc);
  1072. slot->host = host;
  1073. slot->mmc = mmc;
  1074. slot->id = id;
  1075. slot->power_mode = MMC_POWER_UNDEFINED;
  1076. slot->pdata = &host->pdata->slots[id];
  1077. /* Check for some optional GPIO controls */
  1078. slot->vsd = devm_gpiod_get_index_optional(host->dev, "vsd",
  1079. id, GPIOD_OUT_LOW);
  1080. if (IS_ERR(slot->vsd))
  1081. return dev_err_probe(host->dev, PTR_ERR(slot->vsd),
  1082. "error looking up VSD GPIO\n");
  1083. slot->vio = devm_gpiod_get_index_optional(host->dev, "vio",
  1084. id, GPIOD_OUT_LOW);
  1085. if (IS_ERR(slot->vio))
  1086. return dev_err_probe(host->dev, PTR_ERR(slot->vio),
  1087. "error looking up VIO GPIO\n");
  1088. slot->cover = devm_gpiod_get_index_optional(host->dev, "cover",
  1089. id, GPIOD_IN);
  1090. if (IS_ERR(slot->cover))
  1091. return dev_err_probe(host->dev, PTR_ERR(slot->cover),
  1092. "error looking up cover switch GPIO\n");
  1093. host->slots[id] = slot;
  1094. mmc->caps = 0;
  1095. if (host->pdata->slots[id].wires >= 4)
  1096. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1097. mmc->ops = &mmc_omap_ops;
  1098. mmc->f_min = 400000;
  1099. if (mmc_omap2())
  1100. mmc->f_max = 48000000;
  1101. else
  1102. mmc->f_max = 24000000;
  1103. if (host->pdata->max_freq)
  1104. mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
  1105. mmc->ocr_avail = slot->pdata->ocr_mask;
  1106. /* Use scatterlist DMA to reduce per-transfer costs.
  1107. * NOTE max_seg_size assumption that small blocks aren't
  1108. * normally used (except e.g. for reading SD registers).
  1109. */
  1110. mmc->max_segs = 32;
  1111. mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
  1112. mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
  1113. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1114. mmc->max_seg_size = mmc->max_req_size;
  1115. if (slot->pdata->get_cover_state != NULL) {
  1116. timer_setup(&slot->cover_timer, mmc_omap_cover_timer, 0);
  1117. INIT_WORK(&slot->cover_bh_work, mmc_omap_cover_bh_handler);
  1118. }
  1119. r = mmc_add_host(mmc);
  1120. if (r < 0)
  1121. goto err_remove_host;
  1122. if (slot->pdata->name != NULL) {
  1123. r = device_create_file(&mmc->class_dev,
  1124. &dev_attr_slot_name);
  1125. if (r < 0)
  1126. goto err_remove_host;
  1127. }
  1128. if (slot->pdata->get_cover_state != NULL) {
  1129. r = device_create_file(&mmc->class_dev,
  1130. &dev_attr_cover_switch);
  1131. if (r < 0)
  1132. goto err_remove_slot_name;
  1133. queue_work(system_bh_wq, &slot->cover_bh_work);
  1134. }
  1135. return 0;
  1136. err_remove_slot_name:
  1137. if (slot->pdata->name != NULL)
  1138. device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
  1139. err_remove_host:
  1140. mmc_remove_host(mmc);
  1141. return r;
  1142. }
  1143. static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
  1144. {
  1145. struct mmc_host *mmc = slot->mmc;
  1146. if (slot->pdata->name != NULL)
  1147. device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
  1148. if (slot->pdata->get_cover_state != NULL)
  1149. device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
  1150. cancel_work_sync(&slot->cover_bh_work);
  1151. timer_delete_sync(&slot->cover_timer);
  1152. flush_workqueue(slot->host->mmc_omap_wq);
  1153. mmc_remove_host(mmc);
  1154. }
  1155. static int mmc_omap_probe(struct platform_device *pdev)
  1156. {
  1157. struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
  1158. struct mmc_omap_host *host = NULL;
  1159. struct resource *res;
  1160. int i, ret = 0;
  1161. int irq;
  1162. if (pdata == NULL) {
  1163. dev_err(&pdev->dev, "platform data missing\n");
  1164. return -ENXIO;
  1165. }
  1166. if (pdata->nr_slots == 0) {
  1167. dev_err(&pdev->dev, "no slots\n");
  1168. return -EPROBE_DEFER;
  1169. }
  1170. host = devm_kzalloc(&pdev->dev, sizeof(struct mmc_omap_host),
  1171. GFP_KERNEL);
  1172. if (host == NULL)
  1173. return -ENOMEM;
  1174. irq = platform_get_irq(pdev, 0);
  1175. if (irq < 0)
  1176. return irq;
  1177. host->virt_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1178. if (IS_ERR(host->virt_base))
  1179. return PTR_ERR(host->virt_base);
  1180. INIT_WORK(&host->slot_release_work, mmc_omap_slot_release_work);
  1181. INIT_WORK(&host->send_stop_work, mmc_omap_send_stop_work);
  1182. INIT_WORK(&host->cmd_abort_work, mmc_omap_abort_command);
  1183. timer_setup(&host->cmd_abort_timer, mmc_omap_cmd_timer, 0);
  1184. spin_lock_init(&host->clk_lock);
  1185. timer_setup(&host->clk_timer, mmc_omap_clk_timer, 0);
  1186. spin_lock_init(&host->dma_lock);
  1187. spin_lock_init(&host->slot_lock);
  1188. init_waitqueue_head(&host->slot_wq);
  1189. host->pdata = pdata;
  1190. host->features = host->pdata->slots[0].features;
  1191. host->dev = &pdev->dev;
  1192. platform_set_drvdata(pdev, host);
  1193. host->slot_switch = devm_gpiod_get_optional(host->dev, "switch",
  1194. GPIOD_OUT_LOW);
  1195. if (IS_ERR(host->slot_switch))
  1196. return dev_err_probe(host->dev, PTR_ERR(host->slot_switch),
  1197. "error looking up slot switch GPIO\n");
  1198. host->id = pdev->id;
  1199. host->irq = irq;
  1200. host->phys_base = res->start;
  1201. host->iclk = clk_get(&pdev->dev, "ick");
  1202. if (IS_ERR(host->iclk))
  1203. return PTR_ERR(host->iclk);
  1204. clk_prepare_enable(host->iclk);
  1205. host->fclk = clk_get(&pdev->dev, "fck");
  1206. if (IS_ERR(host->fclk)) {
  1207. ret = PTR_ERR(host->fclk);
  1208. goto err_free_iclk;
  1209. }
  1210. ret = clk_prepare(host->fclk);
  1211. if (ret)
  1212. goto err_put_fclk;
  1213. host->dma_tx_burst = -1;
  1214. host->dma_rx_burst = -1;
  1215. host->dma_tx = dma_request_chan(&pdev->dev, "tx");
  1216. if (IS_ERR(host->dma_tx)) {
  1217. ret = PTR_ERR(host->dma_tx);
  1218. if (ret == -EPROBE_DEFER)
  1219. goto err_free_fclk;
  1220. host->dma_tx = NULL;
  1221. dev_warn(host->dev, "TX DMA channel request failed\n");
  1222. }
  1223. host->dma_rx = dma_request_chan(&pdev->dev, "rx");
  1224. if (IS_ERR(host->dma_rx)) {
  1225. ret = PTR_ERR(host->dma_rx);
  1226. if (ret == -EPROBE_DEFER) {
  1227. if (host->dma_tx)
  1228. dma_release_channel(host->dma_tx);
  1229. goto err_free_fclk;
  1230. }
  1231. host->dma_rx = NULL;
  1232. dev_warn(host->dev, "RX DMA channel request failed\n");
  1233. }
  1234. ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
  1235. if (ret)
  1236. goto err_free_dma;
  1237. if (pdata->init != NULL) {
  1238. ret = pdata->init(&pdev->dev);
  1239. if (ret < 0)
  1240. goto err_free_irq;
  1241. }
  1242. host->nr_slots = pdata->nr_slots;
  1243. host->reg_shift = (mmc_omap7xx() ? 1 : 2);
  1244. host->mmc_omap_wq = alloc_workqueue("mmc_omap", WQ_PERCPU, 0);
  1245. if (!host->mmc_omap_wq) {
  1246. ret = -ENOMEM;
  1247. goto err_plat_cleanup;
  1248. }
  1249. for (i = 0; i < pdata->nr_slots; i++) {
  1250. ret = mmc_omap_new_slot(host, i);
  1251. if (ret < 0) {
  1252. while (--i >= 0)
  1253. mmc_omap_remove_slot(host->slots[i]);
  1254. goto err_destroy_wq;
  1255. }
  1256. }
  1257. return 0;
  1258. err_destroy_wq:
  1259. destroy_workqueue(host->mmc_omap_wq);
  1260. err_plat_cleanup:
  1261. if (pdata->cleanup)
  1262. pdata->cleanup(&pdev->dev);
  1263. err_free_irq:
  1264. free_irq(host->irq, host);
  1265. err_free_dma:
  1266. if (host->dma_tx)
  1267. dma_release_channel(host->dma_tx);
  1268. if (host->dma_rx)
  1269. dma_release_channel(host->dma_rx);
  1270. err_free_fclk:
  1271. clk_unprepare(host->fclk);
  1272. err_put_fclk:
  1273. clk_put(host->fclk);
  1274. err_free_iclk:
  1275. clk_disable_unprepare(host->iclk);
  1276. clk_put(host->iclk);
  1277. return ret;
  1278. }
  1279. static void mmc_omap_remove(struct platform_device *pdev)
  1280. {
  1281. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1282. int i;
  1283. BUG_ON(host == NULL);
  1284. for (i = 0; i < host->nr_slots; i++)
  1285. mmc_omap_remove_slot(host->slots[i]);
  1286. if (host->pdata->cleanup)
  1287. host->pdata->cleanup(&pdev->dev);
  1288. mmc_omap_fclk_enable(host, 0);
  1289. free_irq(host->irq, host);
  1290. clk_unprepare(host->fclk);
  1291. clk_put(host->fclk);
  1292. clk_disable_unprepare(host->iclk);
  1293. clk_put(host->iclk);
  1294. if (host->dma_tx)
  1295. dma_release_channel(host->dma_tx);
  1296. if (host->dma_rx)
  1297. dma_release_channel(host->dma_rx);
  1298. destroy_workqueue(host->mmc_omap_wq);
  1299. }
  1300. #if IS_BUILTIN(CONFIG_OF)
  1301. static const struct of_device_id mmc_omap_match[] = {
  1302. { .compatible = "ti,omap2420-mmc", },
  1303. { },
  1304. };
  1305. MODULE_DEVICE_TABLE(of, mmc_omap_match);
  1306. #endif
  1307. static struct platform_driver mmc_omap_driver = {
  1308. .probe = mmc_omap_probe,
  1309. .remove = mmc_omap_remove,
  1310. .driver = {
  1311. .name = DRIVER_NAME,
  1312. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1313. .of_match_table = of_match_ptr(mmc_omap_match),
  1314. },
  1315. };
  1316. module_platform_driver(mmc_omap_driver);
  1317. MODULE_DESCRIPTION("OMAP Multimedia Card driver");
  1318. MODULE_LICENSE("GPL");
  1319. MODULE_ALIAS("platform:" DRIVER_NAME);
  1320. MODULE_AUTHOR("Juha Yrjölä");