omap_hsmmc.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401
  1. /*
  2. * drivers/mmc/host/omap_hsmmc.c
  3. *
  4. * Driver for OMAP2430/3430 MMC controller.
  5. *
  6. * Copyright (C) 2007 Texas Instruments.
  7. *
  8. * Authors:
  9. * Syed Mohammed Khasim <x0khasim@ti.com>
  10. * Madhusudhan <madhu.cr@ti.com>
  11. * Mohit Jalori <mjalori@ti.com>
  12. *
  13. * This file is licensed under the terms of the GNU General Public License
  14. * version 2. This program is licensed "as is" without any warranty of any
  15. * kind, whether express or implied.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/sizes.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/timer.h>
  29. #include <linux/clk.h>
  30. #include <linux/of.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/of_device.h>
  34. #include <linux/omap-dmaengine.h>
  35. #include <linux/mmc/host.h>
  36. #include <linux/mmc/core.h>
  37. #include <linux/mmc/mmc.h>
  38. #include <linux/mmc/slot-gpio.h>
  39. #include <linux/io.h>
  40. #include <linux/irq.h>
  41. #include <linux/gpio.h>
  42. #include <linux/regulator/consumer.h>
  43. #include <linux/pinctrl/consumer.h>
  44. #include <linux/pm_runtime.h>
  45. #include <linux/pm_wakeirq.h>
  46. #include <linux/platform_data/hsmmc-omap.h>
  47. /* OMAP HSMMC Host Controller Registers */
  48. #define OMAP_HSMMC_SYSSTATUS 0x0014
  49. #define OMAP_HSMMC_CON 0x002C
  50. #define OMAP_HSMMC_SDMASA 0x0100
  51. #define OMAP_HSMMC_BLK 0x0104
  52. #define OMAP_HSMMC_ARG 0x0108
  53. #define OMAP_HSMMC_CMD 0x010C
  54. #define OMAP_HSMMC_RSP10 0x0110
  55. #define OMAP_HSMMC_RSP32 0x0114
  56. #define OMAP_HSMMC_RSP54 0x0118
  57. #define OMAP_HSMMC_RSP76 0x011C
  58. #define OMAP_HSMMC_DATA 0x0120
  59. #define OMAP_HSMMC_PSTATE 0x0124
  60. #define OMAP_HSMMC_HCTL 0x0128
  61. #define OMAP_HSMMC_SYSCTL 0x012C
  62. #define OMAP_HSMMC_STAT 0x0130
  63. #define OMAP_HSMMC_IE 0x0134
  64. #define OMAP_HSMMC_ISE 0x0138
  65. #define OMAP_HSMMC_AC12 0x013C
  66. #define OMAP_HSMMC_CAPA 0x0140
  67. #define VS18 (1 << 26)
  68. #define VS30 (1 << 25)
  69. #define HSS (1 << 21)
  70. #define SDVS18 (0x5 << 9)
  71. #define SDVS30 (0x6 << 9)
  72. #define SDVS33 (0x7 << 9)
  73. #define SDVS_MASK 0x00000E00
  74. #define SDVSCLR 0xFFFFF1FF
  75. #define SDVSDET 0x00000400
  76. #define AUTOIDLE 0x1
  77. #define SDBP (1 << 8)
  78. #define DTO 0xe
  79. #define ICE 0x1
  80. #define ICS 0x2
  81. #define CEN (1 << 2)
  82. #define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
  83. #define CLKD_MASK 0x0000FFC0
  84. #define CLKD_SHIFT 6
  85. #define DTO_MASK 0x000F0000
  86. #define DTO_SHIFT 16
  87. #define INIT_STREAM (1 << 1)
  88. #define ACEN_ACMD23 (2 << 2)
  89. #define DP_SELECT (1 << 21)
  90. #define DDIR (1 << 4)
  91. #define DMAE 0x1
  92. #define MSBS (1 << 5)
  93. #define BCE (1 << 1)
  94. #define FOUR_BIT (1 << 1)
  95. #define HSPE (1 << 2)
  96. #define IWE (1 << 24)
  97. #define DDR (1 << 19)
  98. #define CLKEXTFREE (1 << 16)
  99. #define CTPL (1 << 11)
  100. #define DW8 (1 << 5)
  101. #define OD 0x1
  102. #define STAT_CLEAR 0xFFFFFFFF
  103. #define INIT_STREAM_CMD 0x00000000
  104. #define DUAL_VOLT_OCR_BIT 7
  105. #define SRC (1 << 25)
  106. #define SRD (1 << 26)
  107. #define SOFTRESET (1 << 1)
  108. /* PSTATE */
  109. #define DLEV_DAT(x) (1 << (20 + (x)))
  110. /* Interrupt masks for IE and ISE register */
  111. #define CC_EN (1 << 0)
  112. #define TC_EN (1 << 1)
  113. #define BWR_EN (1 << 4)
  114. #define BRR_EN (1 << 5)
  115. #define CIRQ_EN (1 << 8)
  116. #define ERR_EN (1 << 15)
  117. #define CTO_EN (1 << 16)
  118. #define CCRC_EN (1 << 17)
  119. #define CEB_EN (1 << 18)
  120. #define CIE_EN (1 << 19)
  121. #define DTO_EN (1 << 20)
  122. #define DCRC_EN (1 << 21)
  123. #define DEB_EN (1 << 22)
  124. #define ACE_EN (1 << 24)
  125. #define CERR_EN (1 << 28)
  126. #define BADA_EN (1 << 29)
  127. #define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
  128. DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
  129. BRR_EN | BWR_EN | TC_EN | CC_EN)
  130. #define CNI (1 << 7)
  131. #define ACIE (1 << 4)
  132. #define ACEB (1 << 3)
  133. #define ACCE (1 << 2)
  134. #define ACTO (1 << 1)
  135. #define ACNE (1 << 0)
  136. #define MMC_AUTOSUSPEND_DELAY 100
  137. #define MMC_TIMEOUT_MS 20 /* 20 mSec */
  138. #define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
  139. #define OMAP_MMC_MIN_CLOCK 400000
  140. #define OMAP_MMC_MAX_CLOCK 52000000
  141. #define DRIVER_NAME "omap_hsmmc"
  142. #define VDD_1V8 1800000 /* 180000 uV */
  143. #define VDD_3V0 3000000 /* 300000 uV */
  144. #define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
  145. /*
  146. * One controller can have multiple slots, like on some omap boards using
  147. * omap.c controller driver. Luckily this is not currently done on any known
  148. * omap_hsmmc.c device.
  149. */
  150. #define mmc_pdata(host) host->pdata
  151. /*
  152. * MMC Host controller read/write API's
  153. */
  154. #define OMAP_HSMMC_READ(base, reg) \
  155. __raw_readl((base) + OMAP_HSMMC_##reg)
  156. #define OMAP_HSMMC_WRITE(base, reg, val) \
  157. __raw_writel((val), (base) + OMAP_HSMMC_##reg)
  158. struct omap_hsmmc_next {
  159. unsigned int dma_len;
  160. s32 cookie;
  161. };
  162. struct omap_hsmmc_host {
  163. struct device *dev;
  164. struct mmc_host *mmc;
  165. struct mmc_request *mrq;
  166. struct mmc_command *cmd;
  167. struct mmc_data *data;
  168. struct clk *fclk;
  169. struct clk *dbclk;
  170. struct regulator *pbias;
  171. void __iomem *base;
  172. int vqmmc_enabled;
  173. resource_size_t mapbase;
  174. spinlock_t irq_lock; /* Prevent races with irq handler */
  175. unsigned int dma_len;
  176. unsigned int dma_sg_idx;
  177. unsigned char bus_mode;
  178. unsigned char power_mode;
  179. int suspended;
  180. u32 con;
  181. u32 hctl;
  182. u32 sysctl;
  183. u32 capa;
  184. int irq;
  185. int wake_irq;
  186. int use_dma, dma_ch;
  187. struct dma_chan *tx_chan;
  188. struct dma_chan *rx_chan;
  189. int response_busy;
  190. int context_loss;
  191. int protect_card;
  192. int reqs_blocked;
  193. int req_in_progress;
  194. unsigned long clk_rate;
  195. unsigned int flags;
  196. #define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
  197. #define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
  198. struct omap_hsmmc_next next_data;
  199. struct omap_hsmmc_platform_data *pdata;
  200. /* return MMC cover switch state, can be NULL if not supported.
  201. *
  202. * possible return values:
  203. * 0 - closed
  204. * 1 - open
  205. */
  206. int (*get_cover_state)(struct device *dev);
  207. int (*card_detect)(struct device *dev);
  208. };
  209. struct omap_mmc_of_data {
  210. u32 reg_offset;
  211. u8 controller_flags;
  212. };
  213. static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
  214. static int omap_hsmmc_card_detect(struct device *dev)
  215. {
  216. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  217. return mmc_gpio_get_cd(host->mmc);
  218. }
  219. static int omap_hsmmc_get_cover_state(struct device *dev)
  220. {
  221. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  222. return mmc_gpio_get_cd(host->mmc);
  223. }
  224. static int omap_hsmmc_enable_supply(struct mmc_host *mmc)
  225. {
  226. int ret;
  227. struct omap_hsmmc_host *host = mmc_priv(mmc);
  228. struct mmc_ios *ios = &mmc->ios;
  229. if (mmc->supply.vmmc) {
  230. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
  231. if (ret)
  232. return ret;
  233. }
  234. /* Enable interface voltage rail, if needed */
  235. if (mmc->supply.vqmmc && !host->vqmmc_enabled) {
  236. ret = regulator_enable(mmc->supply.vqmmc);
  237. if (ret) {
  238. dev_err(mmc_dev(mmc), "vmmc_aux reg enable failed\n");
  239. goto err_vqmmc;
  240. }
  241. host->vqmmc_enabled = 1;
  242. }
  243. return 0;
  244. err_vqmmc:
  245. if (mmc->supply.vmmc)
  246. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  247. return ret;
  248. }
  249. static int omap_hsmmc_disable_supply(struct mmc_host *mmc)
  250. {
  251. int ret;
  252. int status;
  253. struct omap_hsmmc_host *host = mmc_priv(mmc);
  254. if (mmc->supply.vqmmc && host->vqmmc_enabled) {
  255. ret = regulator_disable(mmc->supply.vqmmc);
  256. if (ret) {
  257. dev_err(mmc_dev(mmc), "vmmc_aux reg disable failed\n");
  258. return ret;
  259. }
  260. host->vqmmc_enabled = 0;
  261. }
  262. if (mmc->supply.vmmc) {
  263. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  264. if (ret)
  265. goto err_set_ocr;
  266. }
  267. return 0;
  268. err_set_ocr:
  269. if (mmc->supply.vqmmc) {
  270. status = regulator_enable(mmc->supply.vqmmc);
  271. if (status)
  272. dev_err(mmc_dev(mmc), "vmmc_aux re-enable failed\n");
  273. }
  274. return ret;
  275. }
  276. static int omap_hsmmc_set_pbias(struct omap_hsmmc_host *host, bool power_on,
  277. int vdd)
  278. {
  279. int ret;
  280. if (!host->pbias)
  281. return 0;
  282. if (power_on) {
  283. if (vdd <= VDD_165_195)
  284. ret = regulator_set_voltage(host->pbias, VDD_1V8,
  285. VDD_1V8);
  286. else
  287. ret = regulator_set_voltage(host->pbias, VDD_3V0,
  288. VDD_3V0);
  289. if (ret < 0) {
  290. dev_err(host->dev, "pbias set voltage fail\n");
  291. return ret;
  292. }
  293. if (!regulator_is_enabled(host->pbias)) {
  294. ret = regulator_enable(host->pbias);
  295. if (ret) {
  296. dev_err(host->dev, "pbias reg enable fail\n");
  297. return ret;
  298. }
  299. }
  300. } else {
  301. if (regulator_is_enabled(host->pbias)) {
  302. ret = regulator_disable(host->pbias);
  303. if (ret) {
  304. dev_err(host->dev, "pbias reg disable fail\n");
  305. return ret;
  306. }
  307. }
  308. }
  309. return 0;
  310. }
  311. static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
  312. {
  313. struct omap_hsmmc_host *host =
  314. platform_get_drvdata(to_platform_device(dev));
  315. struct mmc_host *mmc = host->mmc;
  316. int ret = 0;
  317. if (mmc_pdata(host)->set_power)
  318. return mmc_pdata(host)->set_power(dev, power_on, vdd);
  319. /*
  320. * If we don't see a Vcc regulator, assume it's a fixed
  321. * voltage always-on regulator.
  322. */
  323. if (!mmc->supply.vmmc)
  324. return 0;
  325. if (mmc_pdata(host)->before_set_reg)
  326. mmc_pdata(host)->before_set_reg(dev, power_on, vdd);
  327. ret = omap_hsmmc_set_pbias(host, false, 0);
  328. if (ret)
  329. return ret;
  330. /*
  331. * Assume Vcc regulator is used only to power the card ... OMAP
  332. * VDDS is used to power the pins, optionally with a transceiver to
  333. * support cards using voltages other than VDDS (1.8V nominal). When a
  334. * transceiver is used, DAT3..7 are muxed as transceiver control pins.
  335. *
  336. * In some cases this regulator won't support enable/disable;
  337. * e.g. it's a fixed rail for a WLAN chip.
  338. *
  339. * In other cases vcc_aux switches interface power. Example, for
  340. * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
  341. * chips/cards need an interface voltage rail too.
  342. */
  343. if (power_on) {
  344. ret = omap_hsmmc_enable_supply(mmc);
  345. if (ret)
  346. return ret;
  347. ret = omap_hsmmc_set_pbias(host, true, vdd);
  348. if (ret)
  349. goto err_set_voltage;
  350. } else {
  351. ret = omap_hsmmc_disable_supply(mmc);
  352. if (ret)
  353. return ret;
  354. }
  355. if (mmc_pdata(host)->after_set_reg)
  356. mmc_pdata(host)->after_set_reg(dev, power_on, vdd);
  357. return 0;
  358. err_set_voltage:
  359. omap_hsmmc_disable_supply(mmc);
  360. return ret;
  361. }
  362. static int omap_hsmmc_disable_boot_regulator(struct regulator *reg)
  363. {
  364. int ret;
  365. if (!reg)
  366. return 0;
  367. if (regulator_is_enabled(reg)) {
  368. ret = regulator_enable(reg);
  369. if (ret)
  370. return ret;
  371. ret = regulator_disable(reg);
  372. if (ret)
  373. return ret;
  374. }
  375. return 0;
  376. }
  377. static int omap_hsmmc_disable_boot_regulators(struct omap_hsmmc_host *host)
  378. {
  379. struct mmc_host *mmc = host->mmc;
  380. int ret;
  381. /*
  382. * disable regulators enabled during boot and get the usecount
  383. * right so that regulators can be enabled/disabled by checking
  384. * the return value of regulator_is_enabled
  385. */
  386. ret = omap_hsmmc_disable_boot_regulator(mmc->supply.vmmc);
  387. if (ret) {
  388. dev_err(host->dev, "fail to disable boot enabled vmmc reg\n");
  389. return ret;
  390. }
  391. ret = omap_hsmmc_disable_boot_regulator(mmc->supply.vqmmc);
  392. if (ret) {
  393. dev_err(host->dev,
  394. "fail to disable boot enabled vmmc_aux reg\n");
  395. return ret;
  396. }
  397. ret = omap_hsmmc_disable_boot_regulator(host->pbias);
  398. if (ret) {
  399. dev_err(host->dev,
  400. "failed to disable boot enabled pbias reg\n");
  401. return ret;
  402. }
  403. return 0;
  404. }
  405. static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
  406. {
  407. int ocr_value = 0;
  408. int ret;
  409. struct mmc_host *mmc = host->mmc;
  410. if (mmc_pdata(host)->set_power)
  411. return 0;
  412. mmc->supply.vmmc = devm_regulator_get_optional(host->dev, "vmmc");
  413. if (IS_ERR(mmc->supply.vmmc)) {
  414. ret = PTR_ERR(mmc->supply.vmmc);
  415. if (ret != -ENODEV)
  416. return ret;
  417. dev_dbg(host->dev, "unable to get vmmc regulator %ld\n",
  418. PTR_ERR(mmc->supply.vmmc));
  419. mmc->supply.vmmc = NULL;
  420. } else {
  421. ocr_value = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
  422. if (ocr_value > 0)
  423. mmc_pdata(host)->ocr_mask = ocr_value;
  424. }
  425. /* Allow an aux regulator */
  426. mmc->supply.vqmmc = devm_regulator_get_optional(host->dev, "vmmc_aux");
  427. if (IS_ERR(mmc->supply.vqmmc)) {
  428. ret = PTR_ERR(mmc->supply.vqmmc);
  429. if (ret != -ENODEV)
  430. return ret;
  431. dev_dbg(host->dev, "unable to get vmmc_aux regulator %ld\n",
  432. PTR_ERR(mmc->supply.vqmmc));
  433. mmc->supply.vqmmc = NULL;
  434. }
  435. host->pbias = devm_regulator_get_optional(host->dev, "pbias");
  436. if (IS_ERR(host->pbias)) {
  437. ret = PTR_ERR(host->pbias);
  438. if (ret != -ENODEV)
  439. return ret;
  440. dev_dbg(host->dev, "unable to get pbias regulator %ld\n",
  441. PTR_ERR(host->pbias));
  442. host->pbias = NULL;
  443. }
  444. /* For eMMC do not power off when not in sleep state */
  445. if (mmc_pdata(host)->no_regulator_off_init)
  446. return 0;
  447. ret = omap_hsmmc_disable_boot_regulators(host);
  448. if (ret)
  449. return ret;
  450. return 0;
  451. }
  452. static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id);
  453. static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
  454. struct omap_hsmmc_host *host,
  455. struct omap_hsmmc_platform_data *pdata)
  456. {
  457. int ret;
  458. if (gpio_is_valid(pdata->gpio_cod)) {
  459. ret = mmc_gpio_request_cd(mmc, pdata->gpio_cod, 0);
  460. if (ret)
  461. return ret;
  462. host->get_cover_state = omap_hsmmc_get_cover_state;
  463. mmc_gpio_set_cd_isr(mmc, omap_hsmmc_cover_irq);
  464. } else if (gpio_is_valid(pdata->gpio_cd)) {
  465. ret = mmc_gpio_request_cd(mmc, pdata->gpio_cd, 0);
  466. if (ret)
  467. return ret;
  468. host->card_detect = omap_hsmmc_card_detect;
  469. }
  470. if (gpio_is_valid(pdata->gpio_wp)) {
  471. ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
  472. if (ret)
  473. return ret;
  474. }
  475. return 0;
  476. }
  477. /*
  478. * Start clock to the card
  479. */
  480. static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
  481. {
  482. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  483. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  484. }
  485. /*
  486. * Stop clock to the card
  487. */
  488. static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
  489. {
  490. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  491. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  492. if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
  493. dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
  494. }
  495. static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
  496. struct mmc_command *cmd)
  497. {
  498. u32 irq_mask = INT_EN_MASK;
  499. unsigned long flags;
  500. if (host->use_dma)
  501. irq_mask &= ~(BRR_EN | BWR_EN);
  502. /* Disable timeout for erases */
  503. if (cmd->opcode == MMC_ERASE)
  504. irq_mask &= ~DTO_EN;
  505. spin_lock_irqsave(&host->irq_lock, flags);
  506. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  507. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  508. /* latch pending CIRQ, but don't signal MMC core */
  509. if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
  510. irq_mask |= CIRQ_EN;
  511. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  512. spin_unlock_irqrestore(&host->irq_lock, flags);
  513. }
  514. static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
  515. {
  516. u32 irq_mask = 0;
  517. unsigned long flags;
  518. spin_lock_irqsave(&host->irq_lock, flags);
  519. /* no transfer running but need to keep cirq if enabled */
  520. if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
  521. irq_mask |= CIRQ_EN;
  522. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  523. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  524. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  525. spin_unlock_irqrestore(&host->irq_lock, flags);
  526. }
  527. /* Calculate divisor for the given clock frequency */
  528. static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
  529. {
  530. u16 dsor = 0;
  531. if (ios->clock) {
  532. dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
  533. if (dsor > CLKD_MAX)
  534. dsor = CLKD_MAX;
  535. }
  536. return dsor;
  537. }
  538. static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
  539. {
  540. struct mmc_ios *ios = &host->mmc->ios;
  541. unsigned long regval;
  542. unsigned long timeout;
  543. unsigned long clkdiv;
  544. dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
  545. omap_hsmmc_stop_clock(host);
  546. regval = OMAP_HSMMC_READ(host->base, SYSCTL);
  547. regval = regval & ~(CLKD_MASK | DTO_MASK);
  548. clkdiv = calc_divisor(host, ios);
  549. regval = regval | (clkdiv << 6) | (DTO << 16);
  550. OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
  551. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  552. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  553. /* Wait till the ICS bit is set */
  554. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  555. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  556. && time_before(jiffies, timeout))
  557. cpu_relax();
  558. /*
  559. * Enable High-Speed Support
  560. * Pre-Requisites
  561. * - Controller should support High-Speed-Enable Bit
  562. * - Controller should not be using DDR Mode
  563. * - Controller should advertise that it supports High Speed
  564. * in capabilities register
  565. * - MMC/SD clock coming out of controller > 25MHz
  566. */
  567. if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
  568. (ios->timing != MMC_TIMING_MMC_DDR52) &&
  569. (ios->timing != MMC_TIMING_UHS_DDR50) &&
  570. ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
  571. regval = OMAP_HSMMC_READ(host->base, HCTL);
  572. if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
  573. regval |= HSPE;
  574. else
  575. regval &= ~HSPE;
  576. OMAP_HSMMC_WRITE(host->base, HCTL, regval);
  577. }
  578. omap_hsmmc_start_clock(host);
  579. }
  580. static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
  581. {
  582. struct mmc_ios *ios = &host->mmc->ios;
  583. u32 con;
  584. con = OMAP_HSMMC_READ(host->base, CON);
  585. if (ios->timing == MMC_TIMING_MMC_DDR52 ||
  586. ios->timing == MMC_TIMING_UHS_DDR50)
  587. con |= DDR; /* configure in DDR mode */
  588. else
  589. con &= ~DDR;
  590. switch (ios->bus_width) {
  591. case MMC_BUS_WIDTH_8:
  592. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  593. break;
  594. case MMC_BUS_WIDTH_4:
  595. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  596. OMAP_HSMMC_WRITE(host->base, HCTL,
  597. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  598. break;
  599. case MMC_BUS_WIDTH_1:
  600. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  601. OMAP_HSMMC_WRITE(host->base, HCTL,
  602. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  603. break;
  604. }
  605. }
  606. static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
  607. {
  608. struct mmc_ios *ios = &host->mmc->ios;
  609. u32 con;
  610. con = OMAP_HSMMC_READ(host->base, CON);
  611. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  612. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  613. else
  614. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  615. }
  616. #ifdef CONFIG_PM
  617. /*
  618. * Restore the MMC host context, if it was lost as result of a
  619. * power state change.
  620. */
  621. static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
  622. {
  623. struct mmc_ios *ios = &host->mmc->ios;
  624. u32 hctl, capa;
  625. unsigned long timeout;
  626. if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
  627. host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
  628. host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
  629. host->capa == OMAP_HSMMC_READ(host->base, CAPA))
  630. return 0;
  631. host->context_loss++;
  632. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  633. if (host->power_mode != MMC_POWER_OFF &&
  634. (1 << ios->vdd) <= MMC_VDD_23_24)
  635. hctl = SDVS18;
  636. else
  637. hctl = SDVS30;
  638. capa = VS30 | VS18;
  639. } else {
  640. hctl = SDVS18;
  641. capa = VS18;
  642. }
  643. if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
  644. hctl |= IWE;
  645. OMAP_HSMMC_WRITE(host->base, HCTL,
  646. OMAP_HSMMC_READ(host->base, HCTL) | hctl);
  647. OMAP_HSMMC_WRITE(host->base, CAPA,
  648. OMAP_HSMMC_READ(host->base, CAPA) | capa);
  649. OMAP_HSMMC_WRITE(host->base, HCTL,
  650. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  651. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  652. while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
  653. && time_before(jiffies, timeout))
  654. ;
  655. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  656. OMAP_HSMMC_WRITE(host->base, IE, 0);
  657. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  658. /* Do not initialize card-specific things if the power is off */
  659. if (host->power_mode == MMC_POWER_OFF)
  660. goto out;
  661. omap_hsmmc_set_bus_width(host);
  662. omap_hsmmc_set_clock(host);
  663. omap_hsmmc_set_bus_mode(host);
  664. out:
  665. dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
  666. host->context_loss);
  667. return 0;
  668. }
  669. /*
  670. * Save the MMC host context (store the number of power state changes so far).
  671. */
  672. static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
  673. {
  674. host->con = OMAP_HSMMC_READ(host->base, CON);
  675. host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
  676. host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
  677. host->capa = OMAP_HSMMC_READ(host->base, CAPA);
  678. }
  679. #else
  680. static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
  681. {
  682. return 0;
  683. }
  684. static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
  685. {
  686. }
  687. #endif
  688. /*
  689. * Send init stream sequence to card
  690. * before sending IDLE command
  691. */
  692. static void send_init_stream(struct omap_hsmmc_host *host)
  693. {
  694. int reg = 0;
  695. unsigned long timeout;
  696. if (host->protect_card)
  697. return;
  698. disable_irq(host->irq);
  699. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  700. OMAP_HSMMC_WRITE(host->base, CON,
  701. OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
  702. OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
  703. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  704. while ((reg != CC_EN) && time_before(jiffies, timeout))
  705. reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
  706. OMAP_HSMMC_WRITE(host->base, CON,
  707. OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
  708. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  709. OMAP_HSMMC_READ(host->base, STAT);
  710. enable_irq(host->irq);
  711. }
  712. static inline
  713. int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
  714. {
  715. int r = 1;
  716. if (host->get_cover_state)
  717. r = host->get_cover_state(host->dev);
  718. return r;
  719. }
  720. static ssize_t
  721. omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
  722. char *buf)
  723. {
  724. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  725. struct omap_hsmmc_host *host = mmc_priv(mmc);
  726. return sprintf(buf, "%s\n",
  727. omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
  728. }
  729. static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
  730. static ssize_t
  731. omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
  732. char *buf)
  733. {
  734. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  735. struct omap_hsmmc_host *host = mmc_priv(mmc);
  736. return sprintf(buf, "%s\n", mmc_pdata(host)->name);
  737. }
  738. static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
  739. /*
  740. * Configure the response type and send the cmd.
  741. */
  742. static void
  743. omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
  744. struct mmc_data *data)
  745. {
  746. int cmdreg = 0, resptype = 0, cmdtype = 0;
  747. dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
  748. mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
  749. host->cmd = cmd;
  750. omap_hsmmc_enable_irq(host, cmd);
  751. host->response_busy = 0;
  752. if (cmd->flags & MMC_RSP_PRESENT) {
  753. if (cmd->flags & MMC_RSP_136)
  754. resptype = 1;
  755. else if (cmd->flags & MMC_RSP_BUSY) {
  756. resptype = 3;
  757. host->response_busy = 1;
  758. } else
  759. resptype = 2;
  760. }
  761. /*
  762. * Unlike OMAP1 controller, the cmdtype does not seem to be based on
  763. * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
  764. * a val of 0x3, rest 0x0.
  765. */
  766. if (cmd == host->mrq->stop)
  767. cmdtype = 0x3;
  768. cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
  769. if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
  770. host->mrq->sbc) {
  771. cmdreg |= ACEN_ACMD23;
  772. OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
  773. }
  774. if (data) {
  775. cmdreg |= DP_SELECT | MSBS | BCE;
  776. if (data->flags & MMC_DATA_READ)
  777. cmdreg |= DDIR;
  778. else
  779. cmdreg &= ~(DDIR);
  780. }
  781. if (host->use_dma)
  782. cmdreg |= DMAE;
  783. host->req_in_progress = 1;
  784. OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
  785. OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
  786. }
  787. static int
  788. omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
  789. {
  790. if (data->flags & MMC_DATA_WRITE)
  791. return DMA_TO_DEVICE;
  792. else
  793. return DMA_FROM_DEVICE;
  794. }
  795. static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
  796. struct mmc_data *data)
  797. {
  798. return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
  799. }
  800. static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
  801. {
  802. int dma_ch;
  803. unsigned long flags;
  804. spin_lock_irqsave(&host->irq_lock, flags);
  805. host->req_in_progress = 0;
  806. dma_ch = host->dma_ch;
  807. spin_unlock_irqrestore(&host->irq_lock, flags);
  808. omap_hsmmc_disable_irq(host);
  809. /* Do not complete the request if DMA is still in progress */
  810. if (mrq->data && host->use_dma && dma_ch != -1)
  811. return;
  812. host->mrq = NULL;
  813. mmc_request_done(host->mmc, mrq);
  814. pm_runtime_mark_last_busy(host->dev);
  815. pm_runtime_put_autosuspend(host->dev);
  816. }
  817. /*
  818. * Notify the transfer complete to MMC core
  819. */
  820. static void
  821. omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
  822. {
  823. if (!data) {
  824. struct mmc_request *mrq = host->mrq;
  825. /* TC before CC from CMD6 - don't know why, but it happens */
  826. if (host->cmd && host->cmd->opcode == 6 &&
  827. host->response_busy) {
  828. host->response_busy = 0;
  829. return;
  830. }
  831. omap_hsmmc_request_done(host, mrq);
  832. return;
  833. }
  834. host->data = NULL;
  835. if (!data->error)
  836. data->bytes_xfered += data->blocks * (data->blksz);
  837. else
  838. data->bytes_xfered = 0;
  839. if (data->stop && (data->error || !host->mrq->sbc))
  840. omap_hsmmc_start_command(host, data->stop, NULL);
  841. else
  842. omap_hsmmc_request_done(host, data->mrq);
  843. }
  844. /*
  845. * Notify the core about command completion
  846. */
  847. static void
  848. omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
  849. {
  850. if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
  851. !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
  852. host->cmd = NULL;
  853. omap_hsmmc_start_dma_transfer(host);
  854. omap_hsmmc_start_command(host, host->mrq->cmd,
  855. host->mrq->data);
  856. return;
  857. }
  858. host->cmd = NULL;
  859. if (cmd->flags & MMC_RSP_PRESENT) {
  860. if (cmd->flags & MMC_RSP_136) {
  861. /* response type 2 */
  862. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  863. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  864. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  865. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  866. } else {
  867. /* response types 1, 1b, 3, 4, 5, 6 */
  868. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  869. }
  870. }
  871. if ((host->data == NULL && !host->response_busy) || cmd->error)
  872. omap_hsmmc_request_done(host, host->mrq);
  873. }
  874. /*
  875. * DMA clean up for command errors
  876. */
  877. static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
  878. {
  879. int dma_ch;
  880. unsigned long flags;
  881. host->data->error = errno;
  882. spin_lock_irqsave(&host->irq_lock, flags);
  883. dma_ch = host->dma_ch;
  884. host->dma_ch = -1;
  885. spin_unlock_irqrestore(&host->irq_lock, flags);
  886. if (host->use_dma && dma_ch != -1) {
  887. struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
  888. dmaengine_terminate_all(chan);
  889. dma_unmap_sg(chan->device->dev,
  890. host->data->sg, host->data->sg_len,
  891. omap_hsmmc_get_dma_dir(host, host->data));
  892. host->data->host_cookie = 0;
  893. }
  894. host->data = NULL;
  895. }
  896. /*
  897. * Readable error output
  898. */
  899. #ifdef CONFIG_MMC_DEBUG
  900. static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
  901. {
  902. /* --- means reserved bit without definition at documentation */
  903. static const char *omap_hsmmc_status_bits[] = {
  904. "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
  905. "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
  906. "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
  907. "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
  908. };
  909. char res[256];
  910. char *buf = res;
  911. int len, i;
  912. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  913. buf += len;
  914. for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
  915. if (status & (1 << i)) {
  916. len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
  917. buf += len;
  918. }
  919. dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
  920. }
  921. #else
  922. static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
  923. u32 status)
  924. {
  925. }
  926. #endif /* CONFIG_MMC_DEBUG */
  927. /*
  928. * MMC controller internal state machines reset
  929. *
  930. * Used to reset command or data internal state machines, using respectively
  931. * SRC or SRD bit of SYSCTL register
  932. * Can be called from interrupt context
  933. */
  934. static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
  935. unsigned long bit)
  936. {
  937. unsigned long i = 0;
  938. unsigned long limit = MMC_TIMEOUT_US;
  939. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  940. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  941. /*
  942. * OMAP4 ES2 and greater has an updated reset logic.
  943. * Monitor a 0->1 transition first
  944. */
  945. if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
  946. while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
  947. && (i++ < limit))
  948. udelay(1);
  949. }
  950. i = 0;
  951. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  952. (i++ < limit))
  953. udelay(1);
  954. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  955. dev_err(mmc_dev(host->mmc),
  956. "Timeout waiting on controller reset in %s\n",
  957. __func__);
  958. }
  959. static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
  960. int err, int end_cmd)
  961. {
  962. if (end_cmd) {
  963. omap_hsmmc_reset_controller_fsm(host, SRC);
  964. if (host->cmd)
  965. host->cmd->error = err;
  966. }
  967. if (host->data) {
  968. omap_hsmmc_reset_controller_fsm(host, SRD);
  969. omap_hsmmc_dma_cleanup(host, err);
  970. } else if (host->mrq && host->mrq->cmd)
  971. host->mrq->cmd->error = err;
  972. }
  973. static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
  974. {
  975. struct mmc_data *data;
  976. int end_cmd = 0, end_trans = 0;
  977. int error = 0;
  978. data = host->data;
  979. dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  980. if (status & ERR_EN) {
  981. omap_hsmmc_dbg_report_irq(host, status);
  982. if (status & (CTO_EN | CCRC_EN))
  983. end_cmd = 1;
  984. if (host->data || host->response_busy) {
  985. end_trans = !end_cmd;
  986. host->response_busy = 0;
  987. }
  988. if (status & (CTO_EN | DTO_EN))
  989. hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
  990. else if (status & (CCRC_EN | DCRC_EN | DEB_EN | CEB_EN |
  991. BADA_EN))
  992. hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
  993. if (status & ACE_EN) {
  994. u32 ac12;
  995. ac12 = OMAP_HSMMC_READ(host->base, AC12);
  996. if (!(ac12 & ACNE) && host->mrq->sbc) {
  997. end_cmd = 1;
  998. if (ac12 & ACTO)
  999. error = -ETIMEDOUT;
  1000. else if (ac12 & (ACCE | ACEB | ACIE))
  1001. error = -EILSEQ;
  1002. host->mrq->sbc->error = error;
  1003. hsmmc_command_incomplete(host, error, end_cmd);
  1004. }
  1005. dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
  1006. }
  1007. }
  1008. OMAP_HSMMC_WRITE(host->base, STAT, status);
  1009. if (end_cmd || ((status & CC_EN) && host->cmd))
  1010. omap_hsmmc_cmd_done(host, host->cmd);
  1011. if ((end_trans || (status & TC_EN)) && host->mrq)
  1012. omap_hsmmc_xfer_done(host, data);
  1013. }
  1014. /*
  1015. * MMC controller IRQ handler
  1016. */
  1017. static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
  1018. {
  1019. struct omap_hsmmc_host *host = dev_id;
  1020. int status;
  1021. status = OMAP_HSMMC_READ(host->base, STAT);
  1022. while (status & (INT_EN_MASK | CIRQ_EN)) {
  1023. if (host->req_in_progress)
  1024. omap_hsmmc_do_irq(host, status);
  1025. if (status & CIRQ_EN)
  1026. mmc_signal_sdio_irq(host->mmc);
  1027. /* Flush posted write */
  1028. status = OMAP_HSMMC_READ(host->base, STAT);
  1029. }
  1030. return IRQ_HANDLED;
  1031. }
  1032. static void set_sd_bus_power(struct omap_hsmmc_host *host)
  1033. {
  1034. unsigned long i;
  1035. OMAP_HSMMC_WRITE(host->base, HCTL,
  1036. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  1037. for (i = 0; i < loops_per_jiffy; i++) {
  1038. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  1039. break;
  1040. cpu_relax();
  1041. }
  1042. }
  1043. /*
  1044. * Switch MMC interface voltage ... only relevant for MMC1.
  1045. *
  1046. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  1047. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  1048. * Some chips, like eMMC ones, use internal transceivers.
  1049. */
  1050. static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
  1051. {
  1052. u32 reg_val = 0;
  1053. int ret;
  1054. /* Disable the clocks */
  1055. pm_runtime_put_sync(host->dev);
  1056. if (host->dbclk)
  1057. clk_disable_unprepare(host->dbclk);
  1058. /* Turn the power off */
  1059. ret = omap_hsmmc_set_power(host->dev, 0, 0);
  1060. /* Turn the power ON with given VDD 1.8 or 3.0v */
  1061. if (!ret)
  1062. ret = omap_hsmmc_set_power(host->dev, 1, vdd);
  1063. pm_runtime_get_sync(host->dev);
  1064. if (host->dbclk)
  1065. clk_prepare_enable(host->dbclk);
  1066. if (ret != 0)
  1067. goto err;
  1068. OMAP_HSMMC_WRITE(host->base, HCTL,
  1069. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  1070. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  1071. /*
  1072. * If a MMC dual voltage card is detected, the set_ios fn calls
  1073. * this fn with VDD bit set for 1.8V. Upon card removal from the
  1074. * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  1075. *
  1076. * Cope with a bit of slop in the range ... per data sheets:
  1077. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  1078. * but recommended values are 1.71V to 1.89V
  1079. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  1080. * but recommended values are 2.7V to 3.3V
  1081. *
  1082. * Board setup code shouldn't permit anything very out-of-range.
  1083. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  1084. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  1085. */
  1086. if ((1 << vdd) <= MMC_VDD_23_24)
  1087. reg_val |= SDVS18;
  1088. else
  1089. reg_val |= SDVS30;
  1090. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  1091. set_sd_bus_power(host);
  1092. return 0;
  1093. err:
  1094. dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  1095. return ret;
  1096. }
  1097. /* Protect the card while the cover is open */
  1098. static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
  1099. {
  1100. if (!host->get_cover_state)
  1101. return;
  1102. host->reqs_blocked = 0;
  1103. if (host->get_cover_state(host->dev)) {
  1104. if (host->protect_card) {
  1105. dev_info(host->dev, "%s: cover is closed, "
  1106. "card is now accessible\n",
  1107. mmc_hostname(host->mmc));
  1108. host->protect_card = 0;
  1109. }
  1110. } else {
  1111. if (!host->protect_card) {
  1112. dev_info(host->dev, "%s: cover is open, "
  1113. "card is now inaccessible\n",
  1114. mmc_hostname(host->mmc));
  1115. host->protect_card = 1;
  1116. }
  1117. }
  1118. }
  1119. /*
  1120. * irq handler when (cell-phone) cover is mounted/removed
  1121. */
  1122. static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id)
  1123. {
  1124. struct omap_hsmmc_host *host = dev_id;
  1125. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  1126. omap_hsmmc_protect_card(host);
  1127. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  1128. return IRQ_HANDLED;
  1129. }
  1130. static void omap_hsmmc_dma_callback(void *param)
  1131. {
  1132. struct omap_hsmmc_host *host = param;
  1133. struct dma_chan *chan;
  1134. struct mmc_data *data;
  1135. int req_in_progress;
  1136. spin_lock_irq(&host->irq_lock);
  1137. if (host->dma_ch < 0) {
  1138. spin_unlock_irq(&host->irq_lock);
  1139. return;
  1140. }
  1141. data = host->mrq->data;
  1142. chan = omap_hsmmc_get_dma_chan(host, data);
  1143. if (!data->host_cookie)
  1144. dma_unmap_sg(chan->device->dev,
  1145. data->sg, data->sg_len,
  1146. omap_hsmmc_get_dma_dir(host, data));
  1147. req_in_progress = host->req_in_progress;
  1148. host->dma_ch = -1;
  1149. spin_unlock_irq(&host->irq_lock);
  1150. /* If DMA has finished after TC, complete the request */
  1151. if (!req_in_progress) {
  1152. struct mmc_request *mrq = host->mrq;
  1153. host->mrq = NULL;
  1154. mmc_request_done(host->mmc, mrq);
  1155. pm_runtime_mark_last_busy(host->dev);
  1156. pm_runtime_put_autosuspend(host->dev);
  1157. }
  1158. }
  1159. static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
  1160. struct mmc_data *data,
  1161. struct omap_hsmmc_next *next,
  1162. struct dma_chan *chan)
  1163. {
  1164. int dma_len;
  1165. if (!next && data->host_cookie &&
  1166. data->host_cookie != host->next_data.cookie) {
  1167. dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
  1168. " host->next_data.cookie %d\n",
  1169. __func__, data->host_cookie, host->next_data.cookie);
  1170. data->host_cookie = 0;
  1171. }
  1172. /* Check if next job is already prepared */
  1173. if (next || data->host_cookie != host->next_data.cookie) {
  1174. dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
  1175. omap_hsmmc_get_dma_dir(host, data));
  1176. } else {
  1177. dma_len = host->next_data.dma_len;
  1178. host->next_data.dma_len = 0;
  1179. }
  1180. if (dma_len == 0)
  1181. return -EINVAL;
  1182. if (next) {
  1183. next->dma_len = dma_len;
  1184. data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
  1185. } else
  1186. host->dma_len = dma_len;
  1187. return 0;
  1188. }
  1189. /*
  1190. * Routine to configure and start DMA for the MMC card
  1191. */
  1192. static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
  1193. struct mmc_request *req)
  1194. {
  1195. struct dma_slave_config cfg;
  1196. struct dma_async_tx_descriptor *tx;
  1197. int ret = 0, i;
  1198. struct mmc_data *data = req->data;
  1199. struct dma_chan *chan;
  1200. /* Sanity check: all the SG entries must be aligned by block size. */
  1201. for (i = 0; i < data->sg_len; i++) {
  1202. struct scatterlist *sgl;
  1203. sgl = data->sg + i;
  1204. if (sgl->length % data->blksz)
  1205. return -EINVAL;
  1206. }
  1207. if ((data->blksz % 4) != 0)
  1208. /* REVISIT: The MMC buffer increments only when MSB is written.
  1209. * Return error for blksz which is non multiple of four.
  1210. */
  1211. return -EINVAL;
  1212. BUG_ON(host->dma_ch != -1);
  1213. chan = omap_hsmmc_get_dma_chan(host, data);
  1214. cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
  1215. cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
  1216. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1217. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1218. cfg.src_maxburst = data->blksz / 4;
  1219. cfg.dst_maxburst = data->blksz / 4;
  1220. ret = dmaengine_slave_config(chan, &cfg);
  1221. if (ret)
  1222. return ret;
  1223. ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
  1224. if (ret)
  1225. return ret;
  1226. tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
  1227. data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  1228. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1229. if (!tx) {
  1230. dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
  1231. /* FIXME: cleanup */
  1232. return -1;
  1233. }
  1234. tx->callback = omap_hsmmc_dma_callback;
  1235. tx->callback_param = host;
  1236. /* Does not fail */
  1237. dmaengine_submit(tx);
  1238. host->dma_ch = 1;
  1239. return 0;
  1240. }
  1241. static void set_data_timeout(struct omap_hsmmc_host *host,
  1242. unsigned int timeout_ns,
  1243. unsigned int timeout_clks)
  1244. {
  1245. unsigned int timeout, cycle_ns;
  1246. uint32_t reg, clkd, dto = 0;
  1247. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  1248. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  1249. if (clkd == 0)
  1250. clkd = 1;
  1251. cycle_ns = 1000000000 / (host->clk_rate / clkd);
  1252. timeout = timeout_ns / cycle_ns;
  1253. timeout += timeout_clks;
  1254. if (timeout) {
  1255. while ((timeout & 0x80000000) == 0) {
  1256. dto += 1;
  1257. timeout <<= 1;
  1258. }
  1259. dto = 31 - dto;
  1260. timeout <<= 1;
  1261. if (timeout && dto)
  1262. dto += 1;
  1263. if (dto >= 13)
  1264. dto -= 13;
  1265. else
  1266. dto = 0;
  1267. if (dto > 14)
  1268. dto = 14;
  1269. }
  1270. reg &= ~DTO_MASK;
  1271. reg |= dto << DTO_SHIFT;
  1272. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  1273. }
  1274. static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
  1275. {
  1276. struct mmc_request *req = host->mrq;
  1277. struct dma_chan *chan;
  1278. if (!req->data)
  1279. return;
  1280. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  1281. | (req->data->blocks << 16));
  1282. set_data_timeout(host, req->data->timeout_ns,
  1283. req->data->timeout_clks);
  1284. chan = omap_hsmmc_get_dma_chan(host, req->data);
  1285. dma_async_issue_pending(chan);
  1286. }
  1287. /*
  1288. * Configure block length for MMC/SD cards and initiate the transfer.
  1289. */
  1290. static int
  1291. omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
  1292. {
  1293. int ret;
  1294. host->data = req->data;
  1295. if (req->data == NULL) {
  1296. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  1297. /*
  1298. * Set an arbitrary 100ms data timeout for commands with
  1299. * busy signal.
  1300. */
  1301. if (req->cmd->flags & MMC_RSP_BUSY)
  1302. set_data_timeout(host, 100000000U, 0);
  1303. return 0;
  1304. }
  1305. if (host->use_dma) {
  1306. ret = omap_hsmmc_setup_dma_transfer(host, req);
  1307. if (ret != 0) {
  1308. dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
  1309. return ret;
  1310. }
  1311. }
  1312. return 0;
  1313. }
  1314. static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1315. int err)
  1316. {
  1317. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1318. struct mmc_data *data = mrq->data;
  1319. if (host->use_dma && data->host_cookie) {
  1320. struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
  1321. dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
  1322. omap_hsmmc_get_dma_dir(host, data));
  1323. data->host_cookie = 0;
  1324. }
  1325. }
  1326. static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1327. bool is_first_req)
  1328. {
  1329. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1330. if (mrq->data->host_cookie) {
  1331. mrq->data->host_cookie = 0;
  1332. return ;
  1333. }
  1334. if (host->use_dma) {
  1335. struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
  1336. if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
  1337. &host->next_data, c))
  1338. mrq->data->host_cookie = 0;
  1339. }
  1340. }
  1341. /*
  1342. * Request function. for read/write operation
  1343. */
  1344. static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
  1345. {
  1346. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1347. int err;
  1348. BUG_ON(host->req_in_progress);
  1349. BUG_ON(host->dma_ch != -1);
  1350. pm_runtime_get_sync(host->dev);
  1351. if (host->protect_card) {
  1352. if (host->reqs_blocked < 3) {
  1353. /*
  1354. * Ensure the controller is left in a consistent
  1355. * state by resetting the command and data state
  1356. * machines.
  1357. */
  1358. omap_hsmmc_reset_controller_fsm(host, SRD);
  1359. omap_hsmmc_reset_controller_fsm(host, SRC);
  1360. host->reqs_blocked += 1;
  1361. }
  1362. req->cmd->error = -EBADF;
  1363. if (req->data)
  1364. req->data->error = -EBADF;
  1365. req->cmd->retries = 0;
  1366. mmc_request_done(mmc, req);
  1367. pm_runtime_mark_last_busy(host->dev);
  1368. pm_runtime_put_autosuspend(host->dev);
  1369. return;
  1370. } else if (host->reqs_blocked)
  1371. host->reqs_blocked = 0;
  1372. WARN_ON(host->mrq != NULL);
  1373. host->mrq = req;
  1374. host->clk_rate = clk_get_rate(host->fclk);
  1375. err = omap_hsmmc_prepare_data(host, req);
  1376. if (err) {
  1377. req->cmd->error = err;
  1378. if (req->data)
  1379. req->data->error = err;
  1380. host->mrq = NULL;
  1381. mmc_request_done(mmc, req);
  1382. pm_runtime_mark_last_busy(host->dev);
  1383. pm_runtime_put_autosuspend(host->dev);
  1384. return;
  1385. }
  1386. if (req->sbc && !(host->flags & AUTO_CMD23)) {
  1387. omap_hsmmc_start_command(host, req->sbc, NULL);
  1388. return;
  1389. }
  1390. omap_hsmmc_start_dma_transfer(host);
  1391. omap_hsmmc_start_command(host, req->cmd, req->data);
  1392. }
  1393. /* Routine to configure clock values. Exposed API to core */
  1394. static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1395. {
  1396. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1397. int do_send_init_stream = 0;
  1398. pm_runtime_get_sync(host->dev);
  1399. if (ios->power_mode != host->power_mode) {
  1400. switch (ios->power_mode) {
  1401. case MMC_POWER_OFF:
  1402. omap_hsmmc_set_power(host->dev, 0, 0);
  1403. break;
  1404. case MMC_POWER_UP:
  1405. omap_hsmmc_set_power(host->dev, 1, ios->vdd);
  1406. break;
  1407. case MMC_POWER_ON:
  1408. do_send_init_stream = 1;
  1409. break;
  1410. }
  1411. host->power_mode = ios->power_mode;
  1412. }
  1413. /* FIXME: set registers based only on changes to ios */
  1414. omap_hsmmc_set_bus_width(host);
  1415. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1416. /* Only MMC1 can interface at 3V without some flavor
  1417. * of external transceiver; but they all handle 1.8V.
  1418. */
  1419. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  1420. (ios->vdd == DUAL_VOLT_OCR_BIT)) {
  1421. /*
  1422. * The mmc_select_voltage fn of the core does
  1423. * not seem to set the power_mode to
  1424. * MMC_POWER_UP upon recalculating the voltage.
  1425. * vdd 1.8v.
  1426. */
  1427. if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
  1428. dev_dbg(mmc_dev(host->mmc),
  1429. "Switch operation failed\n");
  1430. }
  1431. }
  1432. omap_hsmmc_set_clock(host);
  1433. if (do_send_init_stream)
  1434. send_init_stream(host);
  1435. omap_hsmmc_set_bus_mode(host);
  1436. pm_runtime_put_autosuspend(host->dev);
  1437. }
  1438. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  1439. {
  1440. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1441. if (!host->card_detect)
  1442. return -ENOSYS;
  1443. return host->card_detect(host->dev);
  1444. }
  1445. static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
  1446. {
  1447. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1448. if (mmc_pdata(host)->init_card)
  1449. mmc_pdata(host)->init_card(card);
  1450. }
  1451. static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1452. {
  1453. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1454. u32 irq_mask, con;
  1455. unsigned long flags;
  1456. spin_lock_irqsave(&host->irq_lock, flags);
  1457. con = OMAP_HSMMC_READ(host->base, CON);
  1458. irq_mask = OMAP_HSMMC_READ(host->base, ISE);
  1459. if (enable) {
  1460. host->flags |= HSMMC_SDIO_IRQ_ENABLED;
  1461. irq_mask |= CIRQ_EN;
  1462. con |= CTPL | CLKEXTFREE;
  1463. } else {
  1464. host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
  1465. irq_mask &= ~CIRQ_EN;
  1466. con &= ~(CTPL | CLKEXTFREE);
  1467. }
  1468. OMAP_HSMMC_WRITE(host->base, CON, con);
  1469. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  1470. /*
  1471. * if enable, piggy back detection on current request
  1472. * but always disable immediately
  1473. */
  1474. if (!host->req_in_progress || !enable)
  1475. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  1476. /* flush posted write */
  1477. OMAP_HSMMC_READ(host->base, IE);
  1478. spin_unlock_irqrestore(&host->irq_lock, flags);
  1479. }
  1480. static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
  1481. {
  1482. int ret;
  1483. /*
  1484. * For omaps with wake-up path, wakeirq will be irq from pinctrl and
  1485. * for other omaps, wakeirq will be from GPIO (dat line remuxed to
  1486. * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
  1487. * with functional clock disabled.
  1488. */
  1489. if (!host->dev->of_node || !host->wake_irq)
  1490. return -ENODEV;
  1491. ret = dev_pm_set_dedicated_wake_irq(host->dev, host->wake_irq);
  1492. if (ret) {
  1493. dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
  1494. goto err;
  1495. }
  1496. /*
  1497. * Some omaps don't have wake-up path from deeper idle states
  1498. * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
  1499. */
  1500. if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
  1501. struct pinctrl *p = devm_pinctrl_get(host->dev);
  1502. if (!p) {
  1503. ret = -ENODEV;
  1504. goto err_free_irq;
  1505. }
  1506. if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
  1507. dev_info(host->dev, "missing default pinctrl state\n");
  1508. devm_pinctrl_put(p);
  1509. ret = -EINVAL;
  1510. goto err_free_irq;
  1511. }
  1512. if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
  1513. dev_info(host->dev, "missing idle pinctrl state\n");
  1514. devm_pinctrl_put(p);
  1515. ret = -EINVAL;
  1516. goto err_free_irq;
  1517. }
  1518. devm_pinctrl_put(p);
  1519. }
  1520. OMAP_HSMMC_WRITE(host->base, HCTL,
  1521. OMAP_HSMMC_READ(host->base, HCTL) | IWE);
  1522. return 0;
  1523. err_free_irq:
  1524. dev_pm_clear_wake_irq(host->dev);
  1525. err:
  1526. dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
  1527. host->wake_irq = 0;
  1528. return ret;
  1529. }
  1530. static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
  1531. {
  1532. u32 hctl, capa, value;
  1533. /* Only MMC1 supports 3.0V */
  1534. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1535. hctl = SDVS30;
  1536. capa = VS30 | VS18;
  1537. } else {
  1538. hctl = SDVS18;
  1539. capa = VS18;
  1540. }
  1541. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  1542. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  1543. value = OMAP_HSMMC_READ(host->base, CAPA);
  1544. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  1545. /* Set SD bus power bit */
  1546. set_sd_bus_power(host);
  1547. }
  1548. static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
  1549. unsigned int direction, int blk_size)
  1550. {
  1551. /* This controller can't do multiblock reads due to hw bugs */
  1552. if (direction == MMC_DATA_READ)
  1553. return 1;
  1554. return blk_size;
  1555. }
  1556. static struct mmc_host_ops omap_hsmmc_ops = {
  1557. .post_req = omap_hsmmc_post_req,
  1558. .pre_req = omap_hsmmc_pre_req,
  1559. .request = omap_hsmmc_request,
  1560. .set_ios = omap_hsmmc_set_ios,
  1561. .get_cd = omap_hsmmc_get_cd,
  1562. .get_ro = mmc_gpio_get_ro,
  1563. .init_card = omap_hsmmc_init_card,
  1564. .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
  1565. };
  1566. #ifdef CONFIG_DEBUG_FS
  1567. static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
  1568. {
  1569. struct mmc_host *mmc = s->private;
  1570. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1571. seq_printf(s, "mmc%d:\n", mmc->index);
  1572. seq_printf(s, "sdio irq mode\t%s\n",
  1573. (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
  1574. if (mmc->caps & MMC_CAP_SDIO_IRQ) {
  1575. seq_printf(s, "sdio irq \t%s\n",
  1576. (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
  1577. : "disabled");
  1578. }
  1579. seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
  1580. pm_runtime_get_sync(host->dev);
  1581. seq_puts(s, "\nregs:\n");
  1582. seq_printf(s, "CON:\t\t0x%08x\n",
  1583. OMAP_HSMMC_READ(host->base, CON));
  1584. seq_printf(s, "PSTATE:\t\t0x%08x\n",
  1585. OMAP_HSMMC_READ(host->base, PSTATE));
  1586. seq_printf(s, "HCTL:\t\t0x%08x\n",
  1587. OMAP_HSMMC_READ(host->base, HCTL));
  1588. seq_printf(s, "SYSCTL:\t\t0x%08x\n",
  1589. OMAP_HSMMC_READ(host->base, SYSCTL));
  1590. seq_printf(s, "IE:\t\t0x%08x\n",
  1591. OMAP_HSMMC_READ(host->base, IE));
  1592. seq_printf(s, "ISE:\t\t0x%08x\n",
  1593. OMAP_HSMMC_READ(host->base, ISE));
  1594. seq_printf(s, "CAPA:\t\t0x%08x\n",
  1595. OMAP_HSMMC_READ(host->base, CAPA));
  1596. pm_runtime_mark_last_busy(host->dev);
  1597. pm_runtime_put_autosuspend(host->dev);
  1598. return 0;
  1599. }
  1600. static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
  1601. {
  1602. return single_open(file, omap_hsmmc_regs_show, inode->i_private);
  1603. }
  1604. static const struct file_operations mmc_regs_fops = {
  1605. .open = omap_hsmmc_regs_open,
  1606. .read = seq_read,
  1607. .llseek = seq_lseek,
  1608. .release = single_release,
  1609. };
  1610. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1611. {
  1612. if (mmc->debugfs_root)
  1613. debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
  1614. mmc, &mmc_regs_fops);
  1615. }
  1616. #else
  1617. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1618. {
  1619. }
  1620. #endif
  1621. #ifdef CONFIG_OF
  1622. static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
  1623. /* See 35xx errata 2.1.1.128 in SPRZ278F */
  1624. .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
  1625. };
  1626. static const struct omap_mmc_of_data omap4_mmc_of_data = {
  1627. .reg_offset = 0x100,
  1628. };
  1629. static const struct omap_mmc_of_data am33xx_mmc_of_data = {
  1630. .reg_offset = 0x100,
  1631. .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
  1632. };
  1633. static const struct of_device_id omap_mmc_of_match[] = {
  1634. {
  1635. .compatible = "ti,omap2-hsmmc",
  1636. },
  1637. {
  1638. .compatible = "ti,omap3-pre-es3-hsmmc",
  1639. .data = &omap3_pre_es3_mmc_of_data,
  1640. },
  1641. {
  1642. .compatible = "ti,omap3-hsmmc",
  1643. },
  1644. {
  1645. .compatible = "ti,omap4-hsmmc",
  1646. .data = &omap4_mmc_of_data,
  1647. },
  1648. {
  1649. .compatible = "ti,am33xx-hsmmc",
  1650. .data = &am33xx_mmc_of_data,
  1651. },
  1652. {},
  1653. };
  1654. MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
  1655. static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
  1656. {
  1657. struct omap_hsmmc_platform_data *pdata;
  1658. struct device_node *np = dev->of_node;
  1659. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  1660. if (!pdata)
  1661. return ERR_PTR(-ENOMEM); /* out of memory */
  1662. if (of_find_property(np, "ti,dual-volt", NULL))
  1663. pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
  1664. pdata->gpio_cd = -EINVAL;
  1665. pdata->gpio_cod = -EINVAL;
  1666. pdata->gpio_wp = -EINVAL;
  1667. if (of_find_property(np, "ti,non-removable", NULL)) {
  1668. pdata->nonremovable = true;
  1669. pdata->no_regulator_off_init = true;
  1670. }
  1671. if (of_find_property(np, "ti,needs-special-reset", NULL))
  1672. pdata->features |= HSMMC_HAS_UPDATED_RESET;
  1673. if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
  1674. pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
  1675. return pdata;
  1676. }
  1677. #else
  1678. static inline struct omap_hsmmc_platform_data
  1679. *of_get_hsmmc_pdata(struct device *dev)
  1680. {
  1681. return ERR_PTR(-EINVAL);
  1682. }
  1683. #endif
  1684. static int omap_hsmmc_probe(struct platform_device *pdev)
  1685. {
  1686. struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
  1687. struct mmc_host *mmc;
  1688. struct omap_hsmmc_host *host = NULL;
  1689. struct resource *res;
  1690. int ret, irq;
  1691. const struct of_device_id *match;
  1692. dma_cap_mask_t mask;
  1693. unsigned tx_req, rx_req;
  1694. const struct omap_mmc_of_data *data;
  1695. void __iomem *base;
  1696. match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
  1697. if (match) {
  1698. pdata = of_get_hsmmc_pdata(&pdev->dev);
  1699. if (IS_ERR(pdata))
  1700. return PTR_ERR(pdata);
  1701. if (match->data) {
  1702. data = match->data;
  1703. pdata->reg_offset = data->reg_offset;
  1704. pdata->controller_flags |= data->controller_flags;
  1705. }
  1706. }
  1707. if (pdata == NULL) {
  1708. dev_err(&pdev->dev, "Platform Data is missing\n");
  1709. return -ENXIO;
  1710. }
  1711. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1712. irq = platform_get_irq(pdev, 0);
  1713. if (res == NULL || irq < 0)
  1714. return -ENXIO;
  1715. base = devm_ioremap_resource(&pdev->dev, res);
  1716. if (IS_ERR(base))
  1717. return PTR_ERR(base);
  1718. mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
  1719. if (!mmc) {
  1720. ret = -ENOMEM;
  1721. goto err;
  1722. }
  1723. ret = mmc_of_parse(mmc);
  1724. if (ret)
  1725. goto err1;
  1726. host = mmc_priv(mmc);
  1727. host->mmc = mmc;
  1728. host->pdata = pdata;
  1729. host->dev = &pdev->dev;
  1730. host->use_dma = 1;
  1731. host->dma_ch = -1;
  1732. host->irq = irq;
  1733. host->mapbase = res->start + pdata->reg_offset;
  1734. host->base = base + pdata->reg_offset;
  1735. host->power_mode = MMC_POWER_OFF;
  1736. host->next_data.cookie = 1;
  1737. host->vqmmc_enabled = 0;
  1738. ret = omap_hsmmc_gpio_init(mmc, host, pdata);
  1739. if (ret)
  1740. goto err_gpio;
  1741. platform_set_drvdata(pdev, host);
  1742. if (pdev->dev.of_node)
  1743. host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
  1744. mmc->ops = &omap_hsmmc_ops;
  1745. mmc->f_min = OMAP_MMC_MIN_CLOCK;
  1746. if (pdata->max_freq > 0)
  1747. mmc->f_max = pdata->max_freq;
  1748. else if (mmc->f_max == 0)
  1749. mmc->f_max = OMAP_MMC_MAX_CLOCK;
  1750. spin_lock_init(&host->irq_lock);
  1751. host->fclk = devm_clk_get(&pdev->dev, "fck");
  1752. if (IS_ERR(host->fclk)) {
  1753. ret = PTR_ERR(host->fclk);
  1754. host->fclk = NULL;
  1755. goto err1;
  1756. }
  1757. if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
  1758. dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
  1759. omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
  1760. }
  1761. device_init_wakeup(&pdev->dev, true);
  1762. pm_runtime_enable(host->dev);
  1763. pm_runtime_get_sync(host->dev);
  1764. pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
  1765. pm_runtime_use_autosuspend(host->dev);
  1766. omap_hsmmc_context_save(host);
  1767. host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
  1768. /*
  1769. * MMC can still work without debounce clock.
  1770. */
  1771. if (IS_ERR(host->dbclk)) {
  1772. host->dbclk = NULL;
  1773. } else if (clk_prepare_enable(host->dbclk) != 0) {
  1774. dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
  1775. host->dbclk = NULL;
  1776. }
  1777. /* Since we do only SG emulation, we can have as many segs
  1778. * as we want. */
  1779. mmc->max_segs = 1024;
  1780. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  1781. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  1782. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1783. mmc->max_seg_size = mmc->max_req_size;
  1784. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  1785. MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
  1786. mmc->caps |= mmc_pdata(host)->caps;
  1787. if (mmc->caps & MMC_CAP_8_BIT_DATA)
  1788. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1789. if (mmc_pdata(host)->nonremovable)
  1790. mmc->caps |= MMC_CAP_NONREMOVABLE;
  1791. mmc->pm_caps |= mmc_pdata(host)->pm_caps;
  1792. omap_hsmmc_conf_bus_power(host);
  1793. if (!pdev->dev.of_node) {
  1794. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  1795. if (!res) {
  1796. dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
  1797. ret = -ENXIO;
  1798. goto err_irq;
  1799. }
  1800. tx_req = res->start;
  1801. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  1802. if (!res) {
  1803. dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
  1804. ret = -ENXIO;
  1805. goto err_irq;
  1806. }
  1807. rx_req = res->start;
  1808. }
  1809. dma_cap_zero(mask);
  1810. dma_cap_set(DMA_SLAVE, mask);
  1811. host->rx_chan =
  1812. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  1813. &rx_req, &pdev->dev, "rx");
  1814. if (!host->rx_chan) {
  1815. dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
  1816. ret = -ENXIO;
  1817. goto err_irq;
  1818. }
  1819. host->tx_chan =
  1820. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  1821. &tx_req, &pdev->dev, "tx");
  1822. if (!host->tx_chan) {
  1823. dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
  1824. ret = -ENXIO;
  1825. goto err_irq;
  1826. }
  1827. /* Request IRQ for MMC operations */
  1828. ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
  1829. mmc_hostname(mmc), host);
  1830. if (ret) {
  1831. dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  1832. goto err_irq;
  1833. }
  1834. ret = omap_hsmmc_reg_get(host);
  1835. if (ret)
  1836. goto err_irq;
  1837. mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
  1838. omap_hsmmc_disable_irq(host);
  1839. /*
  1840. * For now, only support SDIO interrupt if we have a separate
  1841. * wake-up interrupt configured from device tree. This is because
  1842. * the wake-up interrupt is needed for idle state and some
  1843. * platforms need special quirks. And we don't want to add new
  1844. * legacy mux platform init code callbacks any longer as we
  1845. * are moving to DT based booting anyways.
  1846. */
  1847. ret = omap_hsmmc_configure_wake_irq(host);
  1848. if (!ret)
  1849. mmc->caps |= MMC_CAP_SDIO_IRQ;
  1850. omap_hsmmc_protect_card(host);
  1851. mmc_add_host(mmc);
  1852. if (mmc_pdata(host)->name != NULL) {
  1853. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  1854. if (ret < 0)
  1855. goto err_slot_name;
  1856. }
  1857. if (host->get_cover_state) {
  1858. ret = device_create_file(&mmc->class_dev,
  1859. &dev_attr_cover_switch);
  1860. if (ret < 0)
  1861. goto err_slot_name;
  1862. }
  1863. omap_hsmmc_debugfs(mmc);
  1864. pm_runtime_mark_last_busy(host->dev);
  1865. pm_runtime_put_autosuspend(host->dev);
  1866. return 0;
  1867. err_slot_name:
  1868. mmc_remove_host(mmc);
  1869. err_irq:
  1870. device_init_wakeup(&pdev->dev, false);
  1871. if (host->tx_chan)
  1872. dma_release_channel(host->tx_chan);
  1873. if (host->rx_chan)
  1874. dma_release_channel(host->rx_chan);
  1875. pm_runtime_put_sync(host->dev);
  1876. pm_runtime_disable(host->dev);
  1877. if (host->dbclk)
  1878. clk_disable_unprepare(host->dbclk);
  1879. err1:
  1880. err_gpio:
  1881. mmc_free_host(mmc);
  1882. err:
  1883. return ret;
  1884. }
  1885. static int omap_hsmmc_remove(struct platform_device *pdev)
  1886. {
  1887. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  1888. pm_runtime_get_sync(host->dev);
  1889. mmc_remove_host(host->mmc);
  1890. if (host->tx_chan)
  1891. dma_release_channel(host->tx_chan);
  1892. if (host->rx_chan)
  1893. dma_release_channel(host->rx_chan);
  1894. pm_runtime_put_sync(host->dev);
  1895. pm_runtime_disable(host->dev);
  1896. device_init_wakeup(&pdev->dev, false);
  1897. if (host->dbclk)
  1898. clk_disable_unprepare(host->dbclk);
  1899. mmc_free_host(host->mmc);
  1900. return 0;
  1901. }
  1902. #ifdef CONFIG_PM_SLEEP
  1903. static int omap_hsmmc_suspend(struct device *dev)
  1904. {
  1905. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1906. if (!host)
  1907. return 0;
  1908. pm_runtime_get_sync(host->dev);
  1909. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
  1910. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1911. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1912. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1913. OMAP_HSMMC_WRITE(host->base, HCTL,
  1914. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1915. }
  1916. if (host->dbclk)
  1917. clk_disable_unprepare(host->dbclk);
  1918. pm_runtime_put_sync(host->dev);
  1919. return 0;
  1920. }
  1921. /* Routine to resume the MMC device */
  1922. static int omap_hsmmc_resume(struct device *dev)
  1923. {
  1924. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1925. if (!host)
  1926. return 0;
  1927. pm_runtime_get_sync(host->dev);
  1928. if (host->dbclk)
  1929. clk_prepare_enable(host->dbclk);
  1930. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
  1931. omap_hsmmc_conf_bus_power(host);
  1932. omap_hsmmc_protect_card(host);
  1933. pm_runtime_mark_last_busy(host->dev);
  1934. pm_runtime_put_autosuspend(host->dev);
  1935. return 0;
  1936. }
  1937. #endif
  1938. static int omap_hsmmc_runtime_suspend(struct device *dev)
  1939. {
  1940. struct omap_hsmmc_host *host;
  1941. unsigned long flags;
  1942. int ret = 0;
  1943. host = platform_get_drvdata(to_platform_device(dev));
  1944. omap_hsmmc_context_save(host);
  1945. dev_dbg(dev, "disabled\n");
  1946. spin_lock_irqsave(&host->irq_lock, flags);
  1947. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  1948. (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
  1949. /* disable sdio irq handling to prevent race */
  1950. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1951. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1952. if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
  1953. /*
  1954. * dat1 line low, pending sdio irq
  1955. * race condition: possible irq handler running on
  1956. * multi-core, abort
  1957. */
  1958. dev_dbg(dev, "pending sdio irq, abort suspend\n");
  1959. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1960. OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
  1961. OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
  1962. pm_runtime_mark_last_busy(dev);
  1963. ret = -EBUSY;
  1964. goto abort;
  1965. }
  1966. pinctrl_pm_select_idle_state(dev);
  1967. } else {
  1968. pinctrl_pm_select_idle_state(dev);
  1969. }
  1970. abort:
  1971. spin_unlock_irqrestore(&host->irq_lock, flags);
  1972. return ret;
  1973. }
  1974. static int omap_hsmmc_runtime_resume(struct device *dev)
  1975. {
  1976. struct omap_hsmmc_host *host;
  1977. unsigned long flags;
  1978. host = platform_get_drvdata(to_platform_device(dev));
  1979. omap_hsmmc_context_restore(host);
  1980. dev_dbg(dev, "enabled\n");
  1981. spin_lock_irqsave(&host->irq_lock, flags);
  1982. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  1983. (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
  1984. pinctrl_pm_select_default_state(host->dev);
  1985. /* irq lost, if pinmux incorrect */
  1986. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1987. OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
  1988. OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
  1989. } else {
  1990. pinctrl_pm_select_default_state(host->dev);
  1991. }
  1992. spin_unlock_irqrestore(&host->irq_lock, flags);
  1993. return 0;
  1994. }
  1995. static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
  1996. SET_SYSTEM_SLEEP_PM_OPS(omap_hsmmc_suspend, omap_hsmmc_resume)
  1997. .runtime_suspend = omap_hsmmc_runtime_suspend,
  1998. .runtime_resume = omap_hsmmc_runtime_resume,
  1999. };
  2000. static struct platform_driver omap_hsmmc_driver = {
  2001. .probe = omap_hsmmc_probe,
  2002. .remove = omap_hsmmc_remove,
  2003. .driver = {
  2004. .name = DRIVER_NAME,
  2005. .pm = &omap_hsmmc_dev_pm_ops,
  2006. .of_match_table = of_match_ptr(omap_mmc_of_match),
  2007. },
  2008. };
  2009. module_platform_driver(omap_hsmmc_driver);
  2010. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  2011. MODULE_LICENSE("GPL");
  2012. MODULE_ALIAS("platform:" DRIVER_NAME);
  2013. MODULE_AUTHOR("Texas Instruments Inc");