omap_hsmmc.c 57 KB

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