omap_hsmmc.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  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. int 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 = 1;
  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 = 0;
  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 == 0) {
  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 = 1;
  265. }
  266. } else {
  267. if (host->pbias_enabled == 1) {
  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 = 0;
  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 int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
  606. {
  607. return 0;
  608. }
  609. static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
  610. {
  611. }
  612. #endif
  613. /*
  614. * Send init stream sequence to card
  615. * before sending IDLE command
  616. */
  617. static void send_init_stream(struct omap_hsmmc_host *host)
  618. {
  619. int reg = 0;
  620. unsigned long timeout;
  621. disable_irq(host->irq);
  622. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  623. OMAP_HSMMC_WRITE(host->base, CON,
  624. OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
  625. OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
  626. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  627. while ((reg != CC_EN) && time_before(jiffies, timeout))
  628. reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
  629. OMAP_HSMMC_WRITE(host->base, CON,
  630. OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
  631. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  632. OMAP_HSMMC_READ(host->base, STAT);
  633. enable_irq(host->irq);
  634. }
  635. static ssize_t
  636. omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
  637. char *buf)
  638. {
  639. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  640. struct omap_hsmmc_host *host = mmc_priv(mmc);
  641. return sprintf(buf, "%s\n", mmc_pdata(host)->name);
  642. }
  643. static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
  644. /*
  645. * Configure the response type and send the cmd.
  646. */
  647. static void
  648. omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
  649. struct mmc_data *data)
  650. {
  651. int cmdreg = 0, resptype = 0, cmdtype = 0;
  652. dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
  653. mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
  654. host->cmd = cmd;
  655. omap_hsmmc_enable_irq(host, cmd);
  656. host->response_busy = 0;
  657. if (cmd->flags & MMC_RSP_PRESENT) {
  658. if (cmd->flags & MMC_RSP_136)
  659. resptype = 1;
  660. else if (cmd->flags & MMC_RSP_BUSY) {
  661. resptype = 3;
  662. host->response_busy = 1;
  663. } else
  664. resptype = 2;
  665. }
  666. /*
  667. * Unlike OMAP1 controller, the cmdtype does not seem to be based on
  668. * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
  669. * a val of 0x3, rest 0x0.
  670. */
  671. if (cmd == host->mrq->stop)
  672. cmdtype = 0x3;
  673. cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
  674. if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
  675. host->mrq->sbc) {
  676. cmdreg |= ACEN_ACMD23;
  677. OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
  678. }
  679. if (data) {
  680. cmdreg |= DP_SELECT | MSBS | BCE;
  681. if (data->flags & MMC_DATA_READ)
  682. cmdreg |= DDIR;
  683. else
  684. cmdreg &= ~(DDIR);
  685. }
  686. if (host->use_dma)
  687. cmdreg |= DMAE;
  688. host->req_in_progress = 1;
  689. OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
  690. OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
  691. }
  692. static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
  693. struct mmc_data *data)
  694. {
  695. return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
  696. }
  697. static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
  698. {
  699. int dma_ch;
  700. unsigned long flags;
  701. spin_lock_irqsave(&host->irq_lock, flags);
  702. host->req_in_progress = 0;
  703. dma_ch = host->dma_ch;
  704. spin_unlock_irqrestore(&host->irq_lock, flags);
  705. omap_hsmmc_disable_irq(host);
  706. /* Do not complete the request if DMA is still in progress */
  707. if (mrq->data && host->use_dma && dma_ch != -1)
  708. return;
  709. host->mrq = NULL;
  710. mmc_request_done(host->mmc, mrq);
  711. }
  712. /*
  713. * Notify the transfer complete to MMC core
  714. */
  715. static void
  716. omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
  717. {
  718. if (!data) {
  719. struct mmc_request *mrq = host->mrq;
  720. /* TC before CC from CMD6 - don't know why, but it happens */
  721. if (host->cmd && host->cmd->opcode == 6 &&
  722. host->response_busy) {
  723. host->response_busy = 0;
  724. return;
  725. }
  726. omap_hsmmc_request_done(host, mrq);
  727. return;
  728. }
  729. host->data = NULL;
  730. if (!data->error)
  731. data->bytes_xfered += data->blocks * (data->blksz);
  732. else
  733. data->bytes_xfered = 0;
  734. if (data->stop && (data->error || !host->mrq->sbc))
  735. omap_hsmmc_start_command(host, data->stop, NULL);
  736. else
  737. omap_hsmmc_request_done(host, data->mrq);
  738. }
  739. /*
  740. * Notify the core about command completion
  741. */
  742. static void
  743. omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
  744. {
  745. if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
  746. !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
  747. host->cmd = NULL;
  748. omap_hsmmc_start_dma_transfer(host);
  749. omap_hsmmc_start_command(host, host->mrq->cmd,
  750. host->mrq->data);
  751. return;
  752. }
  753. host->cmd = NULL;
  754. if (cmd->flags & MMC_RSP_PRESENT) {
  755. if (cmd->flags & MMC_RSP_136) {
  756. /* response type 2 */
  757. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  758. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  759. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  760. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  761. } else {
  762. /* response types 1, 1b, 3, 4, 5, 6 */
  763. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  764. }
  765. }
  766. if ((host->data == NULL && !host->response_busy) || cmd->error)
  767. omap_hsmmc_request_done(host, host->mrq);
  768. }
  769. /*
  770. * DMA clean up for command errors
  771. */
  772. static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
  773. {
  774. int dma_ch;
  775. unsigned long flags;
  776. host->data->error = errno;
  777. spin_lock_irqsave(&host->irq_lock, flags);
  778. dma_ch = host->dma_ch;
  779. host->dma_ch = -1;
  780. spin_unlock_irqrestore(&host->irq_lock, flags);
  781. if (host->use_dma && dma_ch != -1) {
  782. struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
  783. dmaengine_terminate_all(chan);
  784. dma_unmap_sg(chan->device->dev,
  785. host->data->sg, host->data->sg_len,
  786. mmc_get_dma_dir(host->data));
  787. host->data->host_cookie = 0;
  788. }
  789. host->data = NULL;
  790. }
  791. /*
  792. * Readable error output
  793. */
  794. #ifdef CONFIG_MMC_DEBUG
  795. static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
  796. {
  797. /* --- means reserved bit without definition at documentation */
  798. static const char *omap_hsmmc_status_bits[] = {
  799. "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
  800. "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
  801. "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
  802. "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
  803. };
  804. char res[256];
  805. char *buf = res;
  806. int len, i;
  807. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  808. buf += len;
  809. for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
  810. if (status & (1 << i)) {
  811. len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
  812. buf += len;
  813. }
  814. dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
  815. }
  816. #else
  817. static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
  818. u32 status)
  819. {
  820. }
  821. #endif /* CONFIG_MMC_DEBUG */
  822. /*
  823. * MMC controller internal state machines reset
  824. *
  825. * Used to reset command or data internal state machines, using respectively
  826. * SRC or SRD bit of SYSCTL register
  827. * Can be called from interrupt context
  828. */
  829. static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
  830. unsigned long bit)
  831. {
  832. unsigned long i = 0;
  833. unsigned long limit = MMC_TIMEOUT_US;
  834. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  835. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  836. /*
  837. * OMAP4 ES2 and greater has an updated reset logic.
  838. * Monitor a 0->1 transition first
  839. */
  840. if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
  841. while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
  842. && (i++ < limit))
  843. udelay(1);
  844. }
  845. i = 0;
  846. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  847. (i++ < limit))
  848. udelay(1);
  849. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  850. dev_err(mmc_dev(host->mmc),
  851. "Timeout waiting on controller reset in %s\n",
  852. __func__);
  853. }
  854. static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
  855. int err, int end_cmd)
  856. {
  857. if (end_cmd) {
  858. omap_hsmmc_reset_controller_fsm(host, SRC);
  859. if (host->cmd)
  860. host->cmd->error = err;
  861. }
  862. if (host->data) {
  863. omap_hsmmc_reset_controller_fsm(host, SRD);
  864. omap_hsmmc_dma_cleanup(host, err);
  865. } else if (host->mrq && host->mrq->cmd)
  866. host->mrq->cmd->error = err;
  867. }
  868. static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
  869. {
  870. struct mmc_data *data;
  871. int end_cmd = 0, end_trans = 0;
  872. int error = 0;
  873. data = host->data;
  874. dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  875. if (status & ERR_EN) {
  876. omap_hsmmc_dbg_report_irq(host, status);
  877. if (status & (CTO_EN | CCRC_EN | CEB_EN))
  878. end_cmd = 1;
  879. if (host->data || host->response_busy) {
  880. end_trans = !end_cmd;
  881. host->response_busy = 0;
  882. }
  883. if (status & (CTO_EN | DTO_EN))
  884. hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
  885. else if (status & (CCRC_EN | DCRC_EN | DEB_EN | CEB_EN |
  886. BADA_EN))
  887. hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
  888. if (status & ACE_EN) {
  889. u32 ac12;
  890. ac12 = OMAP_HSMMC_READ(host->base, AC12);
  891. if (!(ac12 & ACNE) && host->mrq->sbc) {
  892. end_cmd = 1;
  893. if (ac12 & ACTO)
  894. error = -ETIMEDOUT;
  895. else if (ac12 & (ACCE | ACEB | ACIE))
  896. error = -EILSEQ;
  897. host->mrq->sbc->error = error;
  898. hsmmc_command_incomplete(host, error, end_cmd);
  899. }
  900. dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
  901. }
  902. }
  903. OMAP_HSMMC_WRITE(host->base, STAT, status);
  904. if (end_cmd || ((status & CC_EN) && host->cmd))
  905. omap_hsmmc_cmd_done(host, host->cmd);
  906. if ((end_trans || (status & TC_EN)) && host->mrq)
  907. omap_hsmmc_xfer_done(host, data);
  908. }
  909. /*
  910. * MMC controller IRQ handler
  911. */
  912. static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
  913. {
  914. struct omap_hsmmc_host *host = dev_id;
  915. int status;
  916. status = OMAP_HSMMC_READ(host->base, STAT);
  917. while (status & (INT_EN_MASK | CIRQ_EN)) {
  918. if (host->req_in_progress)
  919. omap_hsmmc_do_irq(host, status);
  920. if (status & CIRQ_EN)
  921. mmc_signal_sdio_irq(host->mmc);
  922. /* Flush posted write */
  923. status = OMAP_HSMMC_READ(host->base, STAT);
  924. }
  925. return IRQ_HANDLED;
  926. }
  927. static void set_sd_bus_power(struct omap_hsmmc_host *host)
  928. {
  929. unsigned long i;
  930. OMAP_HSMMC_WRITE(host->base, HCTL,
  931. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  932. for (i = 0; i < loops_per_jiffy; i++) {
  933. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  934. break;
  935. cpu_relax();
  936. }
  937. }
  938. /*
  939. * Switch MMC interface voltage ... only relevant for MMC1.
  940. *
  941. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  942. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  943. * Some chips, like eMMC ones, use internal transceivers.
  944. */
  945. static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
  946. {
  947. u32 reg_val = 0;
  948. int ret;
  949. /* Disable the clocks */
  950. if (host->dbclk)
  951. clk_disable_unprepare(host->dbclk);
  952. /* Turn the power off */
  953. ret = omap_hsmmc_set_power(host, 0);
  954. /* Turn the power ON with given VDD 1.8 or 3.0v */
  955. if (!ret)
  956. ret = omap_hsmmc_set_power(host, 1);
  957. if (host->dbclk)
  958. clk_prepare_enable(host->dbclk);
  959. if (ret != 0)
  960. goto err;
  961. OMAP_HSMMC_WRITE(host->base, HCTL,
  962. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  963. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  964. /*
  965. * If a MMC dual voltage card is detected, the set_ios fn calls
  966. * this fn with VDD bit set for 1.8V. Upon card removal from the
  967. * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  968. *
  969. * Cope with a bit of slop in the range ... per data sheets:
  970. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  971. * but recommended values are 1.71V to 1.89V
  972. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  973. * but recommended values are 2.7V to 3.3V
  974. *
  975. * Board setup code shouldn't permit anything very out-of-range.
  976. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  977. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  978. */
  979. if ((1 << vdd) <= MMC_VDD_23_24)
  980. reg_val |= SDVS18;
  981. else
  982. reg_val |= SDVS30;
  983. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  984. set_sd_bus_power(host);
  985. return 0;
  986. err:
  987. dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  988. return ret;
  989. }
  990. static void omap_hsmmc_dma_callback(void *param)
  991. {
  992. struct omap_hsmmc_host *host = param;
  993. struct dma_chan *chan;
  994. struct mmc_data *data;
  995. int req_in_progress;
  996. spin_lock_irq(&host->irq_lock);
  997. if (host->dma_ch < 0) {
  998. spin_unlock_irq(&host->irq_lock);
  999. return;
  1000. }
  1001. data = host->mrq->data;
  1002. chan = omap_hsmmc_get_dma_chan(host, data);
  1003. if (!data->host_cookie)
  1004. dma_unmap_sg(chan->device->dev,
  1005. data->sg, data->sg_len,
  1006. mmc_get_dma_dir(data));
  1007. req_in_progress = host->req_in_progress;
  1008. host->dma_ch = -1;
  1009. spin_unlock_irq(&host->irq_lock);
  1010. /* If DMA has finished after TC, complete the request */
  1011. if (!req_in_progress) {
  1012. struct mmc_request *mrq = host->mrq;
  1013. host->mrq = NULL;
  1014. mmc_request_done(host->mmc, mrq);
  1015. }
  1016. }
  1017. static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
  1018. struct mmc_data *data,
  1019. struct omap_hsmmc_next *next,
  1020. struct dma_chan *chan)
  1021. {
  1022. int dma_len;
  1023. if (!next && data->host_cookie &&
  1024. data->host_cookie != host->next_data.cookie) {
  1025. dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
  1026. " host->next_data.cookie %d\n",
  1027. __func__, data->host_cookie, host->next_data.cookie);
  1028. data->host_cookie = 0;
  1029. }
  1030. /* Check if next job is already prepared */
  1031. if (next || data->host_cookie != host->next_data.cookie) {
  1032. dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
  1033. mmc_get_dma_dir(data));
  1034. } else {
  1035. dma_len = host->next_data.dma_len;
  1036. host->next_data.dma_len = 0;
  1037. }
  1038. if (dma_len == 0)
  1039. return -EINVAL;
  1040. if (next) {
  1041. next->dma_len = dma_len;
  1042. data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
  1043. } else
  1044. host->dma_len = dma_len;
  1045. return 0;
  1046. }
  1047. /*
  1048. * Routine to configure and start DMA for the MMC card
  1049. */
  1050. static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
  1051. struct mmc_request *req)
  1052. {
  1053. struct dma_async_tx_descriptor *tx;
  1054. int ret = 0, i;
  1055. struct mmc_data *data = req->data;
  1056. struct dma_chan *chan;
  1057. struct dma_slave_config cfg = {
  1058. .src_addr = host->mapbase + OMAP_HSMMC_DATA,
  1059. .dst_addr = host->mapbase + OMAP_HSMMC_DATA,
  1060. .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  1061. .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  1062. .src_maxburst = data->blksz / 4,
  1063. .dst_maxburst = data->blksz / 4,
  1064. };
  1065. /* Sanity check: all the SG entries must be aligned by block size. */
  1066. for (i = 0; i < data->sg_len; i++) {
  1067. struct scatterlist *sgl;
  1068. sgl = data->sg + i;
  1069. if (sgl->length % data->blksz)
  1070. return -EINVAL;
  1071. }
  1072. if ((data->blksz % 4) != 0)
  1073. /* REVISIT: The MMC buffer increments only when MSB is written.
  1074. * Return error for blksz which is non multiple of four.
  1075. */
  1076. return -EINVAL;
  1077. BUG_ON(host->dma_ch != -1);
  1078. chan = omap_hsmmc_get_dma_chan(host, data);
  1079. ret = dmaengine_slave_config(chan, &cfg);
  1080. if (ret)
  1081. return ret;
  1082. ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
  1083. if (ret)
  1084. return ret;
  1085. tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
  1086. data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  1087. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1088. if (!tx) {
  1089. dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
  1090. /* FIXME: cleanup */
  1091. return -1;
  1092. }
  1093. tx->callback = omap_hsmmc_dma_callback;
  1094. tx->callback_param = host;
  1095. /* Does not fail */
  1096. dmaengine_submit(tx);
  1097. host->dma_ch = 1;
  1098. return 0;
  1099. }
  1100. static void set_data_timeout(struct omap_hsmmc_host *host,
  1101. unsigned long long timeout_ns,
  1102. unsigned int timeout_clks)
  1103. {
  1104. unsigned long long timeout = timeout_ns;
  1105. unsigned int cycle_ns;
  1106. uint32_t reg, clkd, dto = 0;
  1107. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  1108. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  1109. if (clkd == 0)
  1110. clkd = 1;
  1111. cycle_ns = 1000000000 / (host->clk_rate / clkd);
  1112. do_div(timeout, cycle_ns);
  1113. timeout += timeout_clks;
  1114. if (timeout) {
  1115. while ((timeout & 0x80000000) == 0) {
  1116. dto += 1;
  1117. timeout <<= 1;
  1118. }
  1119. dto = 31 - dto;
  1120. timeout <<= 1;
  1121. if (timeout && dto)
  1122. dto += 1;
  1123. if (dto >= 13)
  1124. dto -= 13;
  1125. else
  1126. dto = 0;
  1127. if (dto > 14)
  1128. dto = 14;
  1129. }
  1130. reg &= ~DTO_MASK;
  1131. reg |= dto << DTO_SHIFT;
  1132. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  1133. }
  1134. static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
  1135. {
  1136. struct mmc_request *req = host->mrq;
  1137. struct dma_chan *chan;
  1138. if (!req->data)
  1139. return;
  1140. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  1141. | (req->data->blocks << 16));
  1142. set_data_timeout(host, req->data->timeout_ns,
  1143. req->data->timeout_clks);
  1144. chan = omap_hsmmc_get_dma_chan(host, req->data);
  1145. dma_async_issue_pending(chan);
  1146. }
  1147. /*
  1148. * Configure block length for MMC/SD cards and initiate the transfer.
  1149. */
  1150. static int
  1151. omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
  1152. {
  1153. int ret;
  1154. unsigned long long timeout;
  1155. host->data = req->data;
  1156. if (req->data == NULL) {
  1157. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  1158. if (req->cmd->flags & MMC_RSP_BUSY) {
  1159. timeout = req->cmd->busy_timeout * NSEC_PER_MSEC;
  1160. /*
  1161. * Set an arbitrary 100ms data timeout for commands with
  1162. * busy signal and no indication of busy_timeout.
  1163. */
  1164. if (!timeout)
  1165. timeout = 100000000U;
  1166. set_data_timeout(host, timeout, 0);
  1167. }
  1168. return 0;
  1169. }
  1170. if (host->use_dma) {
  1171. ret = omap_hsmmc_setup_dma_transfer(host, req);
  1172. if (ret != 0) {
  1173. dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
  1174. return ret;
  1175. }
  1176. }
  1177. return 0;
  1178. }
  1179. static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1180. int err)
  1181. {
  1182. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1183. struct mmc_data *data = mrq->data;
  1184. if (host->use_dma && data->host_cookie) {
  1185. struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
  1186. dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
  1187. mmc_get_dma_dir(data));
  1188. data->host_cookie = 0;
  1189. }
  1190. }
  1191. static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
  1192. {
  1193. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1194. if (mrq->data->host_cookie) {
  1195. mrq->data->host_cookie = 0;
  1196. return ;
  1197. }
  1198. if (host->use_dma) {
  1199. struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
  1200. if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
  1201. &host->next_data, c))
  1202. mrq->data->host_cookie = 0;
  1203. }
  1204. }
  1205. /*
  1206. * Request function. for read/write operation
  1207. */
  1208. static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
  1209. {
  1210. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1211. int err;
  1212. BUG_ON(host->req_in_progress);
  1213. BUG_ON(host->dma_ch != -1);
  1214. if (host->reqs_blocked)
  1215. host->reqs_blocked = 0;
  1216. WARN_ON(host->mrq != NULL);
  1217. host->mrq = req;
  1218. host->clk_rate = clk_get_rate(host->fclk);
  1219. err = omap_hsmmc_prepare_data(host, req);
  1220. if (err) {
  1221. req->cmd->error = err;
  1222. if (req->data)
  1223. req->data->error = err;
  1224. host->mrq = NULL;
  1225. mmc_request_done(mmc, req);
  1226. return;
  1227. }
  1228. if (req->sbc && !(host->flags & AUTO_CMD23)) {
  1229. omap_hsmmc_start_command(host, req->sbc, NULL);
  1230. return;
  1231. }
  1232. omap_hsmmc_start_dma_transfer(host);
  1233. omap_hsmmc_start_command(host, req->cmd, req->data);
  1234. }
  1235. /* Routine to configure clock values. Exposed API to core */
  1236. static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1237. {
  1238. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1239. int do_send_init_stream = 0;
  1240. if (ios->power_mode != host->power_mode) {
  1241. switch (ios->power_mode) {
  1242. case MMC_POWER_OFF:
  1243. omap_hsmmc_set_power(host, 0);
  1244. break;
  1245. case MMC_POWER_UP:
  1246. omap_hsmmc_set_power(host, 1);
  1247. break;
  1248. case MMC_POWER_ON:
  1249. do_send_init_stream = 1;
  1250. break;
  1251. }
  1252. host->power_mode = ios->power_mode;
  1253. }
  1254. /* FIXME: set registers based only on changes to ios */
  1255. omap_hsmmc_set_bus_width(host);
  1256. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1257. /* Only MMC1 can interface at 3V without some flavor
  1258. * of external transceiver; but they all handle 1.8V.
  1259. */
  1260. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  1261. (ios->vdd == DUAL_VOLT_OCR_BIT)) {
  1262. /*
  1263. * The mmc_select_voltage fn of the core does
  1264. * not seem to set the power_mode to
  1265. * MMC_POWER_UP upon recalculating the voltage.
  1266. * vdd 1.8v.
  1267. */
  1268. if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
  1269. dev_dbg(mmc_dev(host->mmc),
  1270. "Switch operation failed\n");
  1271. }
  1272. }
  1273. omap_hsmmc_set_clock(host);
  1274. if (do_send_init_stream)
  1275. send_init_stream(host);
  1276. omap_hsmmc_set_bus_mode(host);
  1277. }
  1278. static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
  1279. {
  1280. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1281. if (mmc_pdata(host)->init_card)
  1282. mmc_pdata(host)->init_card(card);
  1283. }
  1284. static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1285. {
  1286. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1287. u32 irq_mask, con;
  1288. unsigned long flags;
  1289. spin_lock_irqsave(&host->irq_lock, flags);
  1290. con = OMAP_HSMMC_READ(host->base, CON);
  1291. irq_mask = OMAP_HSMMC_READ(host->base, ISE);
  1292. if (enable) {
  1293. host->flags |= HSMMC_SDIO_IRQ_ENABLED;
  1294. irq_mask |= CIRQ_EN;
  1295. con |= CTPL | CLKEXTFREE;
  1296. } else {
  1297. host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
  1298. irq_mask &= ~CIRQ_EN;
  1299. con &= ~(CTPL | CLKEXTFREE);
  1300. }
  1301. OMAP_HSMMC_WRITE(host->base, CON, con);
  1302. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  1303. /*
  1304. * if enable, piggy back detection on current request
  1305. * but always disable immediately
  1306. */
  1307. if (!host->req_in_progress || !enable)
  1308. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  1309. /* flush posted write */
  1310. OMAP_HSMMC_READ(host->base, IE);
  1311. spin_unlock_irqrestore(&host->irq_lock, flags);
  1312. }
  1313. static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
  1314. {
  1315. int ret;
  1316. /*
  1317. * For omaps with wake-up path, wakeirq will be irq from pinctrl and
  1318. * for other omaps, wakeirq will be from GPIO (dat line remuxed to
  1319. * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
  1320. * with functional clock disabled.
  1321. */
  1322. if (!host->dev->of_node || !host->wake_irq)
  1323. return -ENODEV;
  1324. ret = dev_pm_set_dedicated_wake_irq(host->dev, host->wake_irq);
  1325. if (ret) {
  1326. dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
  1327. goto err;
  1328. }
  1329. /*
  1330. * Some omaps don't have wake-up path from deeper idle states
  1331. * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
  1332. */
  1333. if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
  1334. struct pinctrl *p = devm_pinctrl_get(host->dev);
  1335. if (IS_ERR(p)) {
  1336. ret = PTR_ERR(p);
  1337. goto err_free_irq;
  1338. }
  1339. if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
  1340. dev_info(host->dev, "missing default pinctrl state\n");
  1341. devm_pinctrl_put(p);
  1342. ret = -EINVAL;
  1343. goto err_free_irq;
  1344. }
  1345. if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
  1346. dev_info(host->dev, "missing idle pinctrl state\n");
  1347. devm_pinctrl_put(p);
  1348. ret = -EINVAL;
  1349. goto err_free_irq;
  1350. }
  1351. devm_pinctrl_put(p);
  1352. }
  1353. OMAP_HSMMC_WRITE(host->base, HCTL,
  1354. OMAP_HSMMC_READ(host->base, HCTL) | IWE);
  1355. return 0;
  1356. err_free_irq:
  1357. dev_pm_clear_wake_irq(host->dev);
  1358. err:
  1359. dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
  1360. host->wake_irq = 0;
  1361. return ret;
  1362. }
  1363. static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
  1364. {
  1365. u32 hctl, capa, value;
  1366. /* Only MMC1 supports 3.0V */
  1367. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1368. hctl = SDVS30;
  1369. capa = VS30 | VS18;
  1370. } else {
  1371. hctl = SDVS18;
  1372. capa = VS18;
  1373. }
  1374. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  1375. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  1376. value = OMAP_HSMMC_READ(host->base, CAPA);
  1377. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  1378. /* Set SD bus power bit */
  1379. set_sd_bus_power(host);
  1380. }
  1381. static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
  1382. unsigned int direction, int blk_size)
  1383. {
  1384. /* This controller can't do multiblock reads due to hw bugs */
  1385. if (direction == MMC_DATA_READ)
  1386. return 1;
  1387. return blk_size;
  1388. }
  1389. static struct mmc_host_ops omap_hsmmc_ops = {
  1390. .post_req = omap_hsmmc_post_req,
  1391. .pre_req = omap_hsmmc_pre_req,
  1392. .request = omap_hsmmc_request,
  1393. .set_ios = omap_hsmmc_set_ios,
  1394. .get_cd = mmc_gpio_get_cd,
  1395. .get_ro = mmc_gpio_get_ro,
  1396. .init_card = omap_hsmmc_init_card,
  1397. .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
  1398. };
  1399. #ifdef CONFIG_DEBUG_FS
  1400. static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
  1401. {
  1402. struct mmc_host *mmc = s->private;
  1403. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1404. seq_printf(s, "mmc%d:\n", mmc->index);
  1405. seq_printf(s, "sdio irq mode\t%s\n",
  1406. (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
  1407. if (mmc->caps & MMC_CAP_SDIO_IRQ) {
  1408. seq_printf(s, "sdio irq \t%s\n",
  1409. (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
  1410. : "disabled");
  1411. }
  1412. seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
  1413. pm_runtime_get_sync(host->dev);
  1414. seq_puts(s, "\nregs:\n");
  1415. seq_printf(s, "CON:\t\t0x%08x\n",
  1416. OMAP_HSMMC_READ(host->base, CON));
  1417. seq_printf(s, "PSTATE:\t\t0x%08x\n",
  1418. OMAP_HSMMC_READ(host->base, PSTATE));
  1419. seq_printf(s, "HCTL:\t\t0x%08x\n",
  1420. OMAP_HSMMC_READ(host->base, HCTL));
  1421. seq_printf(s, "SYSCTL:\t\t0x%08x\n",
  1422. OMAP_HSMMC_READ(host->base, SYSCTL));
  1423. seq_printf(s, "IE:\t\t0x%08x\n",
  1424. OMAP_HSMMC_READ(host->base, IE));
  1425. seq_printf(s, "ISE:\t\t0x%08x\n",
  1426. OMAP_HSMMC_READ(host->base, ISE));
  1427. seq_printf(s, "CAPA:\t\t0x%08x\n",
  1428. OMAP_HSMMC_READ(host->base, CAPA));
  1429. pm_runtime_mark_last_busy(host->dev);
  1430. pm_runtime_put_autosuspend(host->dev);
  1431. return 0;
  1432. }
  1433. static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
  1434. {
  1435. return single_open(file, omap_hsmmc_regs_show, inode->i_private);
  1436. }
  1437. static const struct file_operations mmc_regs_fops = {
  1438. .open = omap_hsmmc_regs_open,
  1439. .read = seq_read,
  1440. .llseek = seq_lseek,
  1441. .release = single_release,
  1442. };
  1443. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1444. {
  1445. if (mmc->debugfs_root)
  1446. debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
  1447. mmc, &mmc_regs_fops);
  1448. }
  1449. #else
  1450. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1451. {
  1452. }
  1453. #endif
  1454. #ifdef CONFIG_OF
  1455. static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
  1456. /* See 35xx errata 2.1.1.128 in SPRZ278F */
  1457. .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
  1458. };
  1459. static const struct omap_mmc_of_data omap4_mmc_of_data = {
  1460. .reg_offset = 0x100,
  1461. };
  1462. static const struct omap_mmc_of_data am33xx_mmc_of_data = {
  1463. .reg_offset = 0x100,
  1464. .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
  1465. };
  1466. static const struct of_device_id omap_mmc_of_match[] = {
  1467. {
  1468. .compatible = "ti,omap2-hsmmc",
  1469. },
  1470. {
  1471. .compatible = "ti,omap3-pre-es3-hsmmc",
  1472. .data = &omap3_pre_es3_mmc_of_data,
  1473. },
  1474. {
  1475. .compatible = "ti,omap3-hsmmc",
  1476. },
  1477. {
  1478. .compatible = "ti,omap4-hsmmc",
  1479. .data = &omap4_mmc_of_data,
  1480. },
  1481. {
  1482. .compatible = "ti,am33xx-hsmmc",
  1483. .data = &am33xx_mmc_of_data,
  1484. },
  1485. {},
  1486. };
  1487. MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
  1488. static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
  1489. {
  1490. struct omap_hsmmc_platform_data *pdata, *legacy;
  1491. struct device_node *np = dev->of_node;
  1492. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  1493. if (!pdata)
  1494. return ERR_PTR(-ENOMEM); /* out of memory */
  1495. legacy = dev_get_platdata(dev);
  1496. if (legacy && legacy->name)
  1497. pdata->name = legacy->name;
  1498. if (of_find_property(np, "ti,dual-volt", NULL))
  1499. pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
  1500. if (of_find_property(np, "ti,non-removable", NULL)) {
  1501. pdata->nonremovable = true;
  1502. pdata->no_regulator_off_init = true;
  1503. }
  1504. if (of_find_property(np, "ti,needs-special-reset", NULL))
  1505. pdata->features |= HSMMC_HAS_UPDATED_RESET;
  1506. if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
  1507. pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
  1508. return pdata;
  1509. }
  1510. #else
  1511. static inline struct omap_hsmmc_platform_data
  1512. *of_get_hsmmc_pdata(struct device *dev)
  1513. {
  1514. return ERR_PTR(-EINVAL);
  1515. }
  1516. #endif
  1517. static int omap_hsmmc_probe(struct platform_device *pdev)
  1518. {
  1519. struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
  1520. struct mmc_host *mmc;
  1521. struct omap_hsmmc_host *host = NULL;
  1522. struct resource *res;
  1523. int ret, irq;
  1524. const struct of_device_id *match;
  1525. const struct omap_mmc_of_data *data;
  1526. void __iomem *base;
  1527. match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
  1528. if (match) {
  1529. pdata = of_get_hsmmc_pdata(&pdev->dev);
  1530. if (IS_ERR(pdata))
  1531. return PTR_ERR(pdata);
  1532. if (match->data) {
  1533. data = match->data;
  1534. pdata->reg_offset = data->reg_offset;
  1535. pdata->controller_flags |= data->controller_flags;
  1536. }
  1537. }
  1538. if (pdata == NULL) {
  1539. dev_err(&pdev->dev, "Platform Data is missing\n");
  1540. return -ENXIO;
  1541. }
  1542. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1543. irq = platform_get_irq(pdev, 0);
  1544. if (res == NULL || irq < 0)
  1545. return -ENXIO;
  1546. base = devm_ioremap_resource(&pdev->dev, res);
  1547. if (IS_ERR(base))
  1548. return PTR_ERR(base);
  1549. mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
  1550. if (!mmc) {
  1551. ret = -ENOMEM;
  1552. goto err;
  1553. }
  1554. ret = mmc_of_parse(mmc);
  1555. if (ret)
  1556. goto err1;
  1557. host = mmc_priv(mmc);
  1558. host->mmc = mmc;
  1559. host->pdata = pdata;
  1560. host->dev = &pdev->dev;
  1561. host->use_dma = 1;
  1562. host->dma_ch = -1;
  1563. host->irq = irq;
  1564. host->mapbase = res->start + pdata->reg_offset;
  1565. host->base = base + pdata->reg_offset;
  1566. host->power_mode = MMC_POWER_OFF;
  1567. host->next_data.cookie = 1;
  1568. host->pbias_enabled = 0;
  1569. host->vqmmc_enabled = 0;
  1570. platform_set_drvdata(pdev, host);
  1571. if (pdev->dev.of_node)
  1572. host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
  1573. mmc->ops = &omap_hsmmc_ops;
  1574. mmc->f_min = OMAP_MMC_MIN_CLOCK;
  1575. if (pdata->max_freq > 0)
  1576. mmc->f_max = pdata->max_freq;
  1577. else if (mmc->f_max == 0)
  1578. mmc->f_max = OMAP_MMC_MAX_CLOCK;
  1579. spin_lock_init(&host->irq_lock);
  1580. host->fclk = devm_clk_get(&pdev->dev, "fck");
  1581. if (IS_ERR(host->fclk)) {
  1582. ret = PTR_ERR(host->fclk);
  1583. host->fclk = NULL;
  1584. goto err1;
  1585. }
  1586. if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
  1587. dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
  1588. omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
  1589. }
  1590. device_init_wakeup(&pdev->dev, true);
  1591. pm_runtime_enable(host->dev);
  1592. pm_runtime_get_sync(host->dev);
  1593. pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
  1594. pm_runtime_use_autosuspend(host->dev);
  1595. omap_hsmmc_context_save(host);
  1596. host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
  1597. /*
  1598. * MMC can still work without debounce clock.
  1599. */
  1600. if (IS_ERR(host->dbclk)) {
  1601. host->dbclk = NULL;
  1602. } else if (clk_prepare_enable(host->dbclk) != 0) {
  1603. dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
  1604. host->dbclk = NULL;
  1605. }
  1606. /* Set this to a value that allows allocating an entire descriptor
  1607. * list within a page (zero order allocation). */
  1608. mmc->max_segs = 64;
  1609. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  1610. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  1611. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1612. mmc->max_seg_size = mmc->max_req_size;
  1613. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  1614. MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE | MMC_CAP_CMD23;
  1615. mmc->caps |= mmc_pdata(host)->caps;
  1616. if (mmc->caps & MMC_CAP_8_BIT_DATA)
  1617. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1618. if (mmc_pdata(host)->nonremovable)
  1619. mmc->caps |= MMC_CAP_NONREMOVABLE;
  1620. mmc->pm_caps |= mmc_pdata(host)->pm_caps;
  1621. omap_hsmmc_conf_bus_power(host);
  1622. host->rx_chan = dma_request_chan(&pdev->dev, "rx");
  1623. if (IS_ERR(host->rx_chan)) {
  1624. dev_err(mmc_dev(host->mmc), "RX DMA channel request failed\n");
  1625. ret = PTR_ERR(host->rx_chan);
  1626. goto err_irq;
  1627. }
  1628. host->tx_chan = dma_request_chan(&pdev->dev, "tx");
  1629. if (IS_ERR(host->tx_chan)) {
  1630. dev_err(mmc_dev(host->mmc), "TX DMA channel request failed\n");
  1631. ret = PTR_ERR(host->tx_chan);
  1632. goto err_irq;
  1633. }
  1634. /* Request IRQ for MMC operations */
  1635. ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
  1636. mmc_hostname(mmc), host);
  1637. if (ret) {
  1638. dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  1639. goto err_irq;
  1640. }
  1641. ret = omap_hsmmc_reg_get(host);
  1642. if (ret)
  1643. goto err_irq;
  1644. if (!mmc->ocr_avail)
  1645. mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
  1646. omap_hsmmc_disable_irq(host);
  1647. /*
  1648. * For now, only support SDIO interrupt if we have a separate
  1649. * wake-up interrupt configured from device tree. This is because
  1650. * the wake-up interrupt is needed for idle state and some
  1651. * platforms need special quirks. And we don't want to add new
  1652. * legacy mux platform init code callbacks any longer as we
  1653. * are moving to DT based booting anyways.
  1654. */
  1655. ret = omap_hsmmc_configure_wake_irq(host);
  1656. if (!ret)
  1657. mmc->caps |= MMC_CAP_SDIO_IRQ;
  1658. mmc_add_host(mmc);
  1659. if (mmc_pdata(host)->name != NULL) {
  1660. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  1661. if (ret < 0)
  1662. goto err_slot_name;
  1663. }
  1664. omap_hsmmc_debugfs(mmc);
  1665. pm_runtime_mark_last_busy(host->dev);
  1666. pm_runtime_put_autosuspend(host->dev);
  1667. return 0;
  1668. err_slot_name:
  1669. mmc_remove_host(mmc);
  1670. err_irq:
  1671. device_init_wakeup(&pdev->dev, false);
  1672. if (!IS_ERR_OR_NULL(host->tx_chan))
  1673. dma_release_channel(host->tx_chan);
  1674. if (!IS_ERR_OR_NULL(host->rx_chan))
  1675. dma_release_channel(host->rx_chan);
  1676. pm_runtime_dont_use_autosuspend(host->dev);
  1677. pm_runtime_put_sync(host->dev);
  1678. pm_runtime_disable(host->dev);
  1679. if (host->dbclk)
  1680. clk_disable_unprepare(host->dbclk);
  1681. err1:
  1682. mmc_free_host(mmc);
  1683. err:
  1684. return ret;
  1685. }
  1686. static int omap_hsmmc_remove(struct platform_device *pdev)
  1687. {
  1688. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  1689. pm_runtime_get_sync(host->dev);
  1690. mmc_remove_host(host->mmc);
  1691. dma_release_channel(host->tx_chan);
  1692. dma_release_channel(host->rx_chan);
  1693. dev_pm_clear_wake_irq(host->dev);
  1694. pm_runtime_dont_use_autosuspend(host->dev);
  1695. pm_runtime_put_sync(host->dev);
  1696. pm_runtime_disable(host->dev);
  1697. device_init_wakeup(&pdev->dev, false);
  1698. if (host->dbclk)
  1699. clk_disable_unprepare(host->dbclk);
  1700. mmc_free_host(host->mmc);
  1701. return 0;
  1702. }
  1703. #ifdef CONFIG_PM_SLEEP
  1704. static int omap_hsmmc_suspend(struct device *dev)
  1705. {
  1706. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1707. if (!host)
  1708. return 0;
  1709. pm_runtime_get_sync(host->dev);
  1710. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
  1711. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1712. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1713. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1714. OMAP_HSMMC_WRITE(host->base, HCTL,
  1715. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1716. }
  1717. if (host->dbclk)
  1718. clk_disable_unprepare(host->dbclk);
  1719. pm_runtime_put_sync(host->dev);
  1720. return 0;
  1721. }
  1722. /* Routine to resume the MMC device */
  1723. static int omap_hsmmc_resume(struct device *dev)
  1724. {
  1725. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1726. if (!host)
  1727. return 0;
  1728. pm_runtime_get_sync(host->dev);
  1729. if (host->dbclk)
  1730. clk_prepare_enable(host->dbclk);
  1731. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
  1732. omap_hsmmc_conf_bus_power(host);
  1733. pm_runtime_mark_last_busy(host->dev);
  1734. pm_runtime_put_autosuspend(host->dev);
  1735. return 0;
  1736. }
  1737. #endif
  1738. static int omap_hsmmc_runtime_suspend(struct device *dev)
  1739. {
  1740. struct omap_hsmmc_host *host;
  1741. unsigned long flags;
  1742. int ret = 0;
  1743. host = platform_get_drvdata(to_platform_device(dev));
  1744. omap_hsmmc_context_save(host);
  1745. dev_dbg(dev, "disabled\n");
  1746. spin_lock_irqsave(&host->irq_lock, flags);
  1747. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  1748. (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
  1749. /* disable sdio irq handling to prevent race */
  1750. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1751. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1752. if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
  1753. /*
  1754. * dat1 line low, pending sdio irq
  1755. * race condition: possible irq handler running on
  1756. * multi-core, abort
  1757. */
  1758. dev_dbg(dev, "pending sdio irq, abort suspend\n");
  1759. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1760. OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
  1761. OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
  1762. pm_runtime_mark_last_busy(dev);
  1763. ret = -EBUSY;
  1764. goto abort;
  1765. }
  1766. pinctrl_pm_select_idle_state(dev);
  1767. } else {
  1768. pinctrl_pm_select_idle_state(dev);
  1769. }
  1770. abort:
  1771. spin_unlock_irqrestore(&host->irq_lock, flags);
  1772. return ret;
  1773. }
  1774. static int omap_hsmmc_runtime_resume(struct device *dev)
  1775. {
  1776. struct omap_hsmmc_host *host;
  1777. unsigned long flags;
  1778. host = platform_get_drvdata(to_platform_device(dev));
  1779. omap_hsmmc_context_restore(host);
  1780. dev_dbg(dev, "enabled\n");
  1781. spin_lock_irqsave(&host->irq_lock, flags);
  1782. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  1783. (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
  1784. pinctrl_pm_select_default_state(host->dev);
  1785. /* irq lost, if pinmux incorrect */
  1786. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1787. OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
  1788. OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
  1789. } else {
  1790. pinctrl_pm_select_default_state(host->dev);
  1791. }
  1792. spin_unlock_irqrestore(&host->irq_lock, flags);
  1793. return 0;
  1794. }
  1795. static const struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
  1796. SET_SYSTEM_SLEEP_PM_OPS(omap_hsmmc_suspend, omap_hsmmc_resume)
  1797. .runtime_suspend = omap_hsmmc_runtime_suspend,
  1798. .runtime_resume = omap_hsmmc_runtime_resume,
  1799. };
  1800. static struct platform_driver omap_hsmmc_driver = {
  1801. .probe = omap_hsmmc_probe,
  1802. .remove = omap_hsmmc_remove,
  1803. .driver = {
  1804. .name = DRIVER_NAME,
  1805. .pm = &omap_hsmmc_dev_pm_ops,
  1806. .of_match_table = of_match_ptr(omap_mmc_of_match),
  1807. },
  1808. };
  1809. module_platform_driver(omap_hsmmc_driver);
  1810. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  1811. MODULE_LICENSE("GPL");
  1812. MODULE_ALIAS("platform:" DRIVER_NAME);
  1813. MODULE_AUTHOR("Texas Instruments Inc");