omap_hsmmc.c 53 KB

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