omap_hsmmc.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  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. /*
  171. * vcc == configured supply
  172. * vcc_aux == optional
  173. * - MMC1, supply for DAT4..DAT7
  174. * - MMC2/MMC2, external level shifter voltage supply, for
  175. * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
  176. */
  177. struct regulator *vcc;
  178. struct regulator *vcc_aux;
  179. struct regulator *pbias;
  180. bool pbias_enabled;
  181. void __iomem *base;
  182. resource_size_t mapbase;
  183. spinlock_t irq_lock; /* Prevent races with irq handler */
  184. unsigned int dma_len;
  185. unsigned int dma_sg_idx;
  186. unsigned char bus_mode;
  187. unsigned char power_mode;
  188. int suspended;
  189. u32 con;
  190. u32 hctl;
  191. u32 sysctl;
  192. u32 capa;
  193. int irq;
  194. int wake_irq;
  195. int use_dma, dma_ch;
  196. struct dma_chan *tx_chan;
  197. struct dma_chan *rx_chan;
  198. int response_busy;
  199. int context_loss;
  200. int protect_card;
  201. int reqs_blocked;
  202. int use_reg;
  203. int req_in_progress;
  204. unsigned long clk_rate;
  205. unsigned int flags;
  206. #define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
  207. #define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
  208. struct omap_hsmmc_next next_data;
  209. struct omap_hsmmc_platform_data *pdata;
  210. /* return MMC cover switch state, can be NULL if not supported.
  211. *
  212. * possible return values:
  213. * 0 - closed
  214. * 1 - open
  215. */
  216. int (*get_cover_state)(struct device *dev);
  217. int (*card_detect)(struct device *dev);
  218. };
  219. struct omap_mmc_of_data {
  220. u32 reg_offset;
  221. u8 controller_flags;
  222. };
  223. static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
  224. static int omap_hsmmc_card_detect(struct device *dev)
  225. {
  226. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  227. return mmc_gpio_get_cd(host->mmc);
  228. }
  229. static int omap_hsmmc_get_cover_state(struct device *dev)
  230. {
  231. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  232. return mmc_gpio_get_cd(host->mmc);
  233. }
  234. #ifdef CONFIG_REGULATOR
  235. static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
  236. {
  237. struct omap_hsmmc_host *host =
  238. platform_get_drvdata(to_platform_device(dev));
  239. int ret = 0;
  240. /*
  241. * If we don't see a Vcc regulator, assume it's a fixed
  242. * voltage always-on regulator.
  243. */
  244. if (!host->vcc)
  245. return 0;
  246. if (mmc_pdata(host)->before_set_reg)
  247. mmc_pdata(host)->before_set_reg(dev, power_on, vdd);
  248. if (host->pbias) {
  249. if (host->pbias_enabled == 1) {
  250. ret = regulator_disable(host->pbias);
  251. if (!ret)
  252. host->pbias_enabled = 0;
  253. }
  254. regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
  255. }
  256. /*
  257. * Assume Vcc regulator is used only to power the card ... OMAP
  258. * VDDS is used to power the pins, optionally with a transceiver to
  259. * support cards using voltages other than VDDS (1.8V nominal). When a
  260. * transceiver is used, DAT3..7 are muxed as transceiver control pins.
  261. *
  262. * In some cases this regulator won't support enable/disable;
  263. * e.g. it's a fixed rail for a WLAN chip.
  264. *
  265. * In other cases vcc_aux switches interface power. Example, for
  266. * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
  267. * chips/cards need an interface voltage rail too.
  268. */
  269. if (power_on) {
  270. if (host->vcc)
  271. ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
  272. /* Enable interface voltage rail, if needed */
  273. if (ret == 0 && host->vcc_aux) {
  274. ret = regulator_enable(host->vcc_aux);
  275. if (ret < 0 && host->vcc)
  276. ret = mmc_regulator_set_ocr(host->mmc,
  277. host->vcc, 0);
  278. }
  279. } else {
  280. /* Shut down the rail */
  281. if (host->vcc_aux)
  282. ret = regulator_disable(host->vcc_aux);
  283. if (host->vcc) {
  284. /* Then proceed to shut down the local regulator */
  285. ret = mmc_regulator_set_ocr(host->mmc,
  286. host->vcc, 0);
  287. }
  288. }
  289. if (host->pbias) {
  290. if (vdd <= VDD_165_195)
  291. ret = regulator_set_voltage(host->pbias, VDD_1V8,
  292. VDD_1V8);
  293. else
  294. ret = regulator_set_voltage(host->pbias, VDD_3V0,
  295. VDD_3V0);
  296. if (ret < 0)
  297. goto error_set_power;
  298. if (host->pbias_enabled == 0) {
  299. ret = regulator_enable(host->pbias);
  300. if (!ret)
  301. host->pbias_enabled = 1;
  302. }
  303. }
  304. if (mmc_pdata(host)->after_set_reg)
  305. mmc_pdata(host)->after_set_reg(dev, power_on, vdd);
  306. error_set_power:
  307. return ret;
  308. }
  309. static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
  310. {
  311. struct regulator *reg;
  312. int ocr_value = 0;
  313. reg = devm_regulator_get(host->dev, "vmmc");
  314. if (IS_ERR(reg)) {
  315. dev_err(host->dev, "unable to get vmmc regulator %ld\n",
  316. PTR_ERR(reg));
  317. return PTR_ERR(reg);
  318. } else {
  319. host->vcc = reg;
  320. ocr_value = mmc_regulator_get_ocrmask(reg);
  321. if (!mmc_pdata(host)->ocr_mask) {
  322. mmc_pdata(host)->ocr_mask = ocr_value;
  323. } else {
  324. if (!(mmc_pdata(host)->ocr_mask & ocr_value)) {
  325. dev_err(host->dev, "ocrmask %x is not supported\n",
  326. mmc_pdata(host)->ocr_mask);
  327. mmc_pdata(host)->ocr_mask = 0;
  328. return -EINVAL;
  329. }
  330. }
  331. }
  332. mmc_pdata(host)->set_power = omap_hsmmc_set_power;
  333. /* Allow an aux regulator */
  334. reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
  335. host->vcc_aux = IS_ERR(reg) ? NULL : reg;
  336. reg = devm_regulator_get_optional(host->dev, "pbias");
  337. host->pbias = IS_ERR(reg) ? NULL : reg;
  338. /* For eMMC do not power off when not in sleep state */
  339. if (mmc_pdata(host)->no_regulator_off_init)
  340. return 0;
  341. /*
  342. * To disable boot_on regulator, enable regulator
  343. * to increase usecount and then disable it.
  344. */
  345. if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
  346. (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
  347. int vdd = ffs(mmc_pdata(host)->ocr_mask) - 1;
  348. mmc_pdata(host)->set_power(host->dev, 1, vdd);
  349. mmc_pdata(host)->set_power(host->dev, 0, 0);
  350. }
  351. return 0;
  352. }
  353. static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
  354. {
  355. mmc_pdata(host)->set_power = NULL;
  356. }
  357. static inline int omap_hsmmc_have_reg(void)
  358. {
  359. return 1;
  360. }
  361. #else
  362. static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
  363. {
  364. return -EINVAL;
  365. }
  366. static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
  367. {
  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 (status & (CTO_EN | DTO_EN))
  907. hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
  908. else if (status & (CCRC_EN | DCRC_EN))
  909. hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
  910. if (status & ACE_EN) {
  911. u32 ac12;
  912. ac12 = OMAP_HSMMC_READ(host->base, AC12);
  913. if (!(ac12 & ACNE) && host->mrq->sbc) {
  914. end_cmd = 1;
  915. if (ac12 & ACTO)
  916. error = -ETIMEDOUT;
  917. else if (ac12 & (ACCE | ACEB | ACIE))
  918. error = -EILSEQ;
  919. host->mrq->sbc->error = error;
  920. hsmmc_command_incomplete(host, error, end_cmd);
  921. }
  922. dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
  923. }
  924. if (host->data || host->response_busy) {
  925. end_trans = !end_cmd;
  926. host->response_busy = 0;
  927. }
  928. }
  929. OMAP_HSMMC_WRITE(host->base, STAT, status);
  930. if (end_cmd || ((status & CC_EN) && host->cmd))
  931. omap_hsmmc_cmd_done(host, host->cmd);
  932. if ((end_trans || (status & TC_EN)) && host->mrq)
  933. omap_hsmmc_xfer_done(host, data);
  934. }
  935. /*
  936. * MMC controller IRQ handler
  937. */
  938. static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
  939. {
  940. struct omap_hsmmc_host *host = dev_id;
  941. int status;
  942. status = OMAP_HSMMC_READ(host->base, STAT);
  943. while (status & (INT_EN_MASK | CIRQ_EN)) {
  944. if (host->req_in_progress)
  945. omap_hsmmc_do_irq(host, status);
  946. if (status & CIRQ_EN)
  947. mmc_signal_sdio_irq(host->mmc);
  948. /* Flush posted write */
  949. status = OMAP_HSMMC_READ(host->base, STAT);
  950. }
  951. return IRQ_HANDLED;
  952. }
  953. static void set_sd_bus_power(struct omap_hsmmc_host *host)
  954. {
  955. unsigned long i;
  956. OMAP_HSMMC_WRITE(host->base, HCTL,
  957. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  958. for (i = 0; i < loops_per_jiffy; i++) {
  959. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  960. break;
  961. cpu_relax();
  962. }
  963. }
  964. /*
  965. * Switch MMC interface voltage ... only relevant for MMC1.
  966. *
  967. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  968. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  969. * Some chips, like eMMC ones, use internal transceivers.
  970. */
  971. static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
  972. {
  973. u32 reg_val = 0;
  974. int ret;
  975. /* Disable the clocks */
  976. pm_runtime_put_sync(host->dev);
  977. if (host->dbclk)
  978. clk_disable_unprepare(host->dbclk);
  979. /* Turn the power off */
  980. ret = mmc_pdata(host)->set_power(host->dev, 0, 0);
  981. /* Turn the power ON with given VDD 1.8 or 3.0v */
  982. if (!ret)
  983. ret = mmc_pdata(host)->set_power(host->dev, 1, vdd);
  984. pm_runtime_get_sync(host->dev);
  985. if (host->dbclk)
  986. clk_prepare_enable(host->dbclk);
  987. if (ret != 0)
  988. goto err;
  989. OMAP_HSMMC_WRITE(host->base, HCTL,
  990. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  991. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  992. /*
  993. * If a MMC dual voltage card is detected, the set_ios fn calls
  994. * this fn with VDD bit set for 1.8V. Upon card removal from the
  995. * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  996. *
  997. * Cope with a bit of slop in the range ... per data sheets:
  998. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  999. * but recommended values are 1.71V to 1.89V
  1000. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  1001. * but recommended values are 2.7V to 3.3V
  1002. *
  1003. * Board setup code shouldn't permit anything very out-of-range.
  1004. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  1005. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  1006. */
  1007. if ((1 << vdd) <= MMC_VDD_23_24)
  1008. reg_val |= SDVS18;
  1009. else
  1010. reg_val |= SDVS30;
  1011. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  1012. set_sd_bus_power(host);
  1013. return 0;
  1014. err:
  1015. dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  1016. return ret;
  1017. }
  1018. /* Protect the card while the cover is open */
  1019. static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
  1020. {
  1021. if (!host->get_cover_state)
  1022. return;
  1023. host->reqs_blocked = 0;
  1024. if (host->get_cover_state(host->dev)) {
  1025. if (host->protect_card) {
  1026. dev_info(host->dev, "%s: cover is closed, "
  1027. "card is now accessible\n",
  1028. mmc_hostname(host->mmc));
  1029. host->protect_card = 0;
  1030. }
  1031. } else {
  1032. if (!host->protect_card) {
  1033. dev_info(host->dev, "%s: cover is open, "
  1034. "card is now inaccessible\n",
  1035. mmc_hostname(host->mmc));
  1036. host->protect_card = 1;
  1037. }
  1038. }
  1039. }
  1040. /*
  1041. * irq handler when (cell-phone) cover is mounted/removed
  1042. */
  1043. static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id)
  1044. {
  1045. struct omap_hsmmc_host *host = dev_id;
  1046. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  1047. omap_hsmmc_protect_card(host);
  1048. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  1049. return IRQ_HANDLED;
  1050. }
  1051. static void omap_hsmmc_dma_callback(void *param)
  1052. {
  1053. struct omap_hsmmc_host *host = param;
  1054. struct dma_chan *chan;
  1055. struct mmc_data *data;
  1056. int req_in_progress;
  1057. spin_lock_irq(&host->irq_lock);
  1058. if (host->dma_ch < 0) {
  1059. spin_unlock_irq(&host->irq_lock);
  1060. return;
  1061. }
  1062. data = host->mrq->data;
  1063. chan = omap_hsmmc_get_dma_chan(host, data);
  1064. if (!data->host_cookie)
  1065. dma_unmap_sg(chan->device->dev,
  1066. data->sg, data->sg_len,
  1067. omap_hsmmc_get_dma_dir(host, data));
  1068. req_in_progress = host->req_in_progress;
  1069. host->dma_ch = -1;
  1070. spin_unlock_irq(&host->irq_lock);
  1071. /* If DMA has finished after TC, complete the request */
  1072. if (!req_in_progress) {
  1073. struct mmc_request *mrq = host->mrq;
  1074. host->mrq = NULL;
  1075. mmc_request_done(host->mmc, mrq);
  1076. pm_runtime_mark_last_busy(host->dev);
  1077. pm_runtime_put_autosuspend(host->dev);
  1078. }
  1079. }
  1080. static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
  1081. struct mmc_data *data,
  1082. struct omap_hsmmc_next *next,
  1083. struct dma_chan *chan)
  1084. {
  1085. int dma_len;
  1086. if (!next && data->host_cookie &&
  1087. data->host_cookie != host->next_data.cookie) {
  1088. dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
  1089. " host->next_data.cookie %d\n",
  1090. __func__, data->host_cookie, host->next_data.cookie);
  1091. data->host_cookie = 0;
  1092. }
  1093. /* Check if next job is already prepared */
  1094. if (next || data->host_cookie != host->next_data.cookie) {
  1095. dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
  1096. omap_hsmmc_get_dma_dir(host, data));
  1097. } else {
  1098. dma_len = host->next_data.dma_len;
  1099. host->next_data.dma_len = 0;
  1100. }
  1101. if (dma_len == 0)
  1102. return -EINVAL;
  1103. if (next) {
  1104. next->dma_len = dma_len;
  1105. data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
  1106. } else
  1107. host->dma_len = dma_len;
  1108. return 0;
  1109. }
  1110. /*
  1111. * Routine to configure and start DMA for the MMC card
  1112. */
  1113. static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
  1114. struct mmc_request *req)
  1115. {
  1116. struct dma_slave_config cfg;
  1117. struct dma_async_tx_descriptor *tx;
  1118. int ret = 0, i;
  1119. struct mmc_data *data = req->data;
  1120. struct dma_chan *chan;
  1121. /* Sanity check: all the SG entries must be aligned by block size. */
  1122. for (i = 0; i < data->sg_len; i++) {
  1123. struct scatterlist *sgl;
  1124. sgl = data->sg + i;
  1125. if (sgl->length % data->blksz)
  1126. return -EINVAL;
  1127. }
  1128. if ((data->blksz % 4) != 0)
  1129. /* REVISIT: The MMC buffer increments only when MSB is written.
  1130. * Return error for blksz which is non multiple of four.
  1131. */
  1132. return -EINVAL;
  1133. BUG_ON(host->dma_ch != -1);
  1134. chan = omap_hsmmc_get_dma_chan(host, data);
  1135. cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
  1136. cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
  1137. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1138. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1139. cfg.src_maxburst = data->blksz / 4;
  1140. cfg.dst_maxburst = data->blksz / 4;
  1141. ret = dmaengine_slave_config(chan, &cfg);
  1142. if (ret)
  1143. return ret;
  1144. ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
  1145. if (ret)
  1146. return ret;
  1147. tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
  1148. data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  1149. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1150. if (!tx) {
  1151. dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
  1152. /* FIXME: cleanup */
  1153. return -1;
  1154. }
  1155. tx->callback = omap_hsmmc_dma_callback;
  1156. tx->callback_param = host;
  1157. /* Does not fail */
  1158. dmaengine_submit(tx);
  1159. host->dma_ch = 1;
  1160. return 0;
  1161. }
  1162. static void set_data_timeout(struct omap_hsmmc_host *host,
  1163. unsigned int timeout_ns,
  1164. unsigned int timeout_clks)
  1165. {
  1166. unsigned int timeout, cycle_ns;
  1167. uint32_t reg, clkd, dto = 0;
  1168. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  1169. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  1170. if (clkd == 0)
  1171. clkd = 1;
  1172. cycle_ns = 1000000000 / (host->clk_rate / clkd);
  1173. timeout = timeout_ns / cycle_ns;
  1174. timeout += timeout_clks;
  1175. if (timeout) {
  1176. while ((timeout & 0x80000000) == 0) {
  1177. dto += 1;
  1178. timeout <<= 1;
  1179. }
  1180. dto = 31 - dto;
  1181. timeout <<= 1;
  1182. if (timeout && dto)
  1183. dto += 1;
  1184. if (dto >= 13)
  1185. dto -= 13;
  1186. else
  1187. dto = 0;
  1188. if (dto > 14)
  1189. dto = 14;
  1190. }
  1191. reg &= ~DTO_MASK;
  1192. reg |= dto << DTO_SHIFT;
  1193. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  1194. }
  1195. static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
  1196. {
  1197. struct mmc_request *req = host->mrq;
  1198. struct dma_chan *chan;
  1199. if (!req->data)
  1200. return;
  1201. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  1202. | (req->data->blocks << 16));
  1203. set_data_timeout(host, req->data->timeout_ns,
  1204. req->data->timeout_clks);
  1205. chan = omap_hsmmc_get_dma_chan(host, req->data);
  1206. dma_async_issue_pending(chan);
  1207. }
  1208. /*
  1209. * Configure block length for MMC/SD cards and initiate the transfer.
  1210. */
  1211. static int
  1212. omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
  1213. {
  1214. int ret;
  1215. host->data = req->data;
  1216. if (req->data == NULL) {
  1217. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  1218. /*
  1219. * Set an arbitrary 100ms data timeout for commands with
  1220. * busy signal.
  1221. */
  1222. if (req->cmd->flags & MMC_RSP_BUSY)
  1223. set_data_timeout(host, 100000000U, 0);
  1224. return 0;
  1225. }
  1226. if (host->use_dma) {
  1227. ret = omap_hsmmc_setup_dma_transfer(host, req);
  1228. if (ret != 0) {
  1229. dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
  1230. return ret;
  1231. }
  1232. }
  1233. return 0;
  1234. }
  1235. static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1236. int err)
  1237. {
  1238. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1239. struct mmc_data *data = mrq->data;
  1240. if (host->use_dma && data->host_cookie) {
  1241. struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
  1242. dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
  1243. omap_hsmmc_get_dma_dir(host, data));
  1244. data->host_cookie = 0;
  1245. }
  1246. }
  1247. static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1248. bool is_first_req)
  1249. {
  1250. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1251. if (mrq->data->host_cookie) {
  1252. mrq->data->host_cookie = 0;
  1253. return ;
  1254. }
  1255. if (host->use_dma) {
  1256. struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
  1257. if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
  1258. &host->next_data, c))
  1259. mrq->data->host_cookie = 0;
  1260. }
  1261. }
  1262. /*
  1263. * Request function. for read/write operation
  1264. */
  1265. static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
  1266. {
  1267. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1268. int err;
  1269. BUG_ON(host->req_in_progress);
  1270. BUG_ON(host->dma_ch != -1);
  1271. pm_runtime_get_sync(host->dev);
  1272. if (host->protect_card) {
  1273. if (host->reqs_blocked < 3) {
  1274. /*
  1275. * Ensure the controller is left in a consistent
  1276. * state by resetting the command and data state
  1277. * machines.
  1278. */
  1279. omap_hsmmc_reset_controller_fsm(host, SRD);
  1280. omap_hsmmc_reset_controller_fsm(host, SRC);
  1281. host->reqs_blocked += 1;
  1282. }
  1283. req->cmd->error = -EBADF;
  1284. if (req->data)
  1285. req->data->error = -EBADF;
  1286. req->cmd->retries = 0;
  1287. mmc_request_done(mmc, req);
  1288. pm_runtime_mark_last_busy(host->dev);
  1289. pm_runtime_put_autosuspend(host->dev);
  1290. return;
  1291. } else if (host->reqs_blocked)
  1292. host->reqs_blocked = 0;
  1293. WARN_ON(host->mrq != NULL);
  1294. host->mrq = req;
  1295. host->clk_rate = clk_get_rate(host->fclk);
  1296. err = omap_hsmmc_prepare_data(host, req);
  1297. if (err) {
  1298. req->cmd->error = err;
  1299. if (req->data)
  1300. req->data->error = err;
  1301. host->mrq = NULL;
  1302. mmc_request_done(mmc, req);
  1303. pm_runtime_mark_last_busy(host->dev);
  1304. pm_runtime_put_autosuspend(host->dev);
  1305. return;
  1306. }
  1307. if (req->sbc && !(host->flags & AUTO_CMD23)) {
  1308. omap_hsmmc_start_command(host, req->sbc, NULL);
  1309. return;
  1310. }
  1311. omap_hsmmc_start_dma_transfer(host);
  1312. omap_hsmmc_start_command(host, req->cmd, req->data);
  1313. }
  1314. /* Routine to configure clock values. Exposed API to core */
  1315. static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1316. {
  1317. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1318. int do_send_init_stream = 0;
  1319. pm_runtime_get_sync(host->dev);
  1320. if (ios->power_mode != host->power_mode) {
  1321. switch (ios->power_mode) {
  1322. case MMC_POWER_OFF:
  1323. mmc_pdata(host)->set_power(host->dev, 0, 0);
  1324. break;
  1325. case MMC_POWER_UP:
  1326. mmc_pdata(host)->set_power(host->dev, 1, ios->vdd);
  1327. break;
  1328. case MMC_POWER_ON:
  1329. do_send_init_stream = 1;
  1330. break;
  1331. }
  1332. host->power_mode = ios->power_mode;
  1333. }
  1334. /* FIXME: set registers based only on changes to ios */
  1335. omap_hsmmc_set_bus_width(host);
  1336. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1337. /* Only MMC1 can interface at 3V without some flavor
  1338. * of external transceiver; but they all handle 1.8V.
  1339. */
  1340. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  1341. (ios->vdd == DUAL_VOLT_OCR_BIT)) {
  1342. /*
  1343. * The mmc_select_voltage fn of the core does
  1344. * not seem to set the power_mode to
  1345. * MMC_POWER_UP upon recalculating the voltage.
  1346. * vdd 1.8v.
  1347. */
  1348. if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
  1349. dev_dbg(mmc_dev(host->mmc),
  1350. "Switch operation failed\n");
  1351. }
  1352. }
  1353. omap_hsmmc_set_clock(host);
  1354. if (do_send_init_stream)
  1355. send_init_stream(host);
  1356. omap_hsmmc_set_bus_mode(host);
  1357. pm_runtime_put_autosuspend(host->dev);
  1358. }
  1359. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  1360. {
  1361. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1362. if (!host->card_detect)
  1363. return -ENOSYS;
  1364. return host->card_detect(host->dev);
  1365. }
  1366. static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
  1367. {
  1368. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1369. if (mmc_pdata(host)->init_card)
  1370. mmc_pdata(host)->init_card(card);
  1371. }
  1372. static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1373. {
  1374. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1375. u32 irq_mask, con;
  1376. unsigned long flags;
  1377. spin_lock_irqsave(&host->irq_lock, flags);
  1378. con = OMAP_HSMMC_READ(host->base, CON);
  1379. irq_mask = OMAP_HSMMC_READ(host->base, ISE);
  1380. if (enable) {
  1381. host->flags |= HSMMC_SDIO_IRQ_ENABLED;
  1382. irq_mask |= CIRQ_EN;
  1383. con |= CTPL | CLKEXTFREE;
  1384. } else {
  1385. host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
  1386. irq_mask &= ~CIRQ_EN;
  1387. con &= ~(CTPL | CLKEXTFREE);
  1388. }
  1389. OMAP_HSMMC_WRITE(host->base, CON, con);
  1390. OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
  1391. /*
  1392. * if enable, piggy back detection on current request
  1393. * but always disable immediately
  1394. */
  1395. if (!host->req_in_progress || !enable)
  1396. OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
  1397. /* flush posted write */
  1398. OMAP_HSMMC_READ(host->base, IE);
  1399. spin_unlock_irqrestore(&host->irq_lock, flags);
  1400. }
  1401. static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
  1402. {
  1403. int ret;
  1404. /*
  1405. * For omaps with wake-up path, wakeirq will be irq from pinctrl and
  1406. * for other omaps, wakeirq will be from GPIO (dat line remuxed to
  1407. * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
  1408. * with functional clock disabled.
  1409. */
  1410. if (!host->dev->of_node || !host->wake_irq)
  1411. return -ENODEV;
  1412. ret = dev_pm_set_dedicated_wake_irq(host->dev, host->wake_irq);
  1413. if (ret) {
  1414. dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
  1415. goto err;
  1416. }
  1417. /*
  1418. * Some omaps don't have wake-up path from deeper idle states
  1419. * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
  1420. */
  1421. if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
  1422. struct pinctrl *p = devm_pinctrl_get(host->dev);
  1423. if (!p) {
  1424. ret = -ENODEV;
  1425. goto err_free_irq;
  1426. }
  1427. if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
  1428. dev_info(host->dev, "missing default pinctrl state\n");
  1429. devm_pinctrl_put(p);
  1430. ret = -EINVAL;
  1431. goto err_free_irq;
  1432. }
  1433. if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
  1434. dev_info(host->dev, "missing idle pinctrl state\n");
  1435. devm_pinctrl_put(p);
  1436. ret = -EINVAL;
  1437. goto err_free_irq;
  1438. }
  1439. devm_pinctrl_put(p);
  1440. }
  1441. OMAP_HSMMC_WRITE(host->base, HCTL,
  1442. OMAP_HSMMC_READ(host->base, HCTL) | IWE);
  1443. return 0;
  1444. err_free_irq:
  1445. dev_pm_clear_wake_irq(host->dev);
  1446. err:
  1447. dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
  1448. host->wake_irq = 0;
  1449. return ret;
  1450. }
  1451. static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
  1452. {
  1453. u32 hctl, capa, value;
  1454. /* Only MMC1 supports 3.0V */
  1455. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1456. hctl = SDVS30;
  1457. capa = VS30 | VS18;
  1458. } else {
  1459. hctl = SDVS18;
  1460. capa = VS18;
  1461. }
  1462. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  1463. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  1464. value = OMAP_HSMMC_READ(host->base, CAPA);
  1465. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  1466. /* Set SD bus power bit */
  1467. set_sd_bus_power(host);
  1468. }
  1469. static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
  1470. unsigned int direction, int blk_size)
  1471. {
  1472. /* This controller can't do multiblock reads due to hw bugs */
  1473. if (direction == MMC_DATA_READ)
  1474. return 1;
  1475. return blk_size;
  1476. }
  1477. static struct mmc_host_ops omap_hsmmc_ops = {
  1478. .post_req = omap_hsmmc_post_req,
  1479. .pre_req = omap_hsmmc_pre_req,
  1480. .request = omap_hsmmc_request,
  1481. .set_ios = omap_hsmmc_set_ios,
  1482. .get_cd = omap_hsmmc_get_cd,
  1483. .get_ro = mmc_gpio_get_ro,
  1484. .init_card = omap_hsmmc_init_card,
  1485. .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
  1486. };
  1487. #ifdef CONFIG_DEBUG_FS
  1488. static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
  1489. {
  1490. struct mmc_host *mmc = s->private;
  1491. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1492. seq_printf(s, "mmc%d:\n", mmc->index);
  1493. seq_printf(s, "sdio irq mode\t%s\n",
  1494. (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
  1495. if (mmc->caps & MMC_CAP_SDIO_IRQ) {
  1496. seq_printf(s, "sdio irq \t%s\n",
  1497. (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
  1498. : "disabled");
  1499. }
  1500. seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
  1501. pm_runtime_get_sync(host->dev);
  1502. seq_puts(s, "\nregs:\n");
  1503. seq_printf(s, "CON:\t\t0x%08x\n",
  1504. OMAP_HSMMC_READ(host->base, CON));
  1505. seq_printf(s, "PSTATE:\t\t0x%08x\n",
  1506. OMAP_HSMMC_READ(host->base, PSTATE));
  1507. seq_printf(s, "HCTL:\t\t0x%08x\n",
  1508. OMAP_HSMMC_READ(host->base, HCTL));
  1509. seq_printf(s, "SYSCTL:\t\t0x%08x\n",
  1510. OMAP_HSMMC_READ(host->base, SYSCTL));
  1511. seq_printf(s, "IE:\t\t0x%08x\n",
  1512. OMAP_HSMMC_READ(host->base, IE));
  1513. seq_printf(s, "ISE:\t\t0x%08x\n",
  1514. OMAP_HSMMC_READ(host->base, ISE));
  1515. seq_printf(s, "CAPA:\t\t0x%08x\n",
  1516. OMAP_HSMMC_READ(host->base, CAPA));
  1517. pm_runtime_mark_last_busy(host->dev);
  1518. pm_runtime_put_autosuspend(host->dev);
  1519. return 0;
  1520. }
  1521. static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
  1522. {
  1523. return single_open(file, omap_hsmmc_regs_show, inode->i_private);
  1524. }
  1525. static const struct file_operations mmc_regs_fops = {
  1526. .open = omap_hsmmc_regs_open,
  1527. .read = seq_read,
  1528. .llseek = seq_lseek,
  1529. .release = single_release,
  1530. };
  1531. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1532. {
  1533. if (mmc->debugfs_root)
  1534. debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
  1535. mmc, &mmc_regs_fops);
  1536. }
  1537. #else
  1538. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1539. {
  1540. }
  1541. #endif
  1542. #ifdef CONFIG_OF
  1543. static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
  1544. /* See 35xx errata 2.1.1.128 in SPRZ278F */
  1545. .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
  1546. };
  1547. static const struct omap_mmc_of_data omap4_mmc_of_data = {
  1548. .reg_offset = 0x100,
  1549. };
  1550. static const struct omap_mmc_of_data am33xx_mmc_of_data = {
  1551. .reg_offset = 0x100,
  1552. .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
  1553. };
  1554. static const struct of_device_id omap_mmc_of_match[] = {
  1555. {
  1556. .compatible = "ti,omap2-hsmmc",
  1557. },
  1558. {
  1559. .compatible = "ti,omap3-pre-es3-hsmmc",
  1560. .data = &omap3_pre_es3_mmc_of_data,
  1561. },
  1562. {
  1563. .compatible = "ti,omap3-hsmmc",
  1564. },
  1565. {
  1566. .compatible = "ti,omap4-hsmmc",
  1567. .data = &omap4_mmc_of_data,
  1568. },
  1569. {
  1570. .compatible = "ti,am33xx-hsmmc",
  1571. .data = &am33xx_mmc_of_data,
  1572. },
  1573. {},
  1574. };
  1575. MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
  1576. static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
  1577. {
  1578. struct omap_hsmmc_platform_data *pdata;
  1579. struct device_node *np = dev->of_node;
  1580. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  1581. if (!pdata)
  1582. return ERR_PTR(-ENOMEM); /* out of memory */
  1583. if (of_find_property(np, "ti,dual-volt", NULL))
  1584. pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
  1585. pdata->gpio_cd = -EINVAL;
  1586. pdata->gpio_cod = -EINVAL;
  1587. pdata->gpio_wp = -EINVAL;
  1588. if (of_find_property(np, "ti,non-removable", NULL)) {
  1589. pdata->nonremovable = true;
  1590. pdata->no_regulator_off_init = true;
  1591. }
  1592. if (of_find_property(np, "ti,needs-special-reset", NULL))
  1593. pdata->features |= HSMMC_HAS_UPDATED_RESET;
  1594. if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
  1595. pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
  1596. return pdata;
  1597. }
  1598. #else
  1599. static inline struct omap_hsmmc_platform_data
  1600. *of_get_hsmmc_pdata(struct device *dev)
  1601. {
  1602. return ERR_PTR(-EINVAL);
  1603. }
  1604. #endif
  1605. static int omap_hsmmc_probe(struct platform_device *pdev)
  1606. {
  1607. struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
  1608. struct mmc_host *mmc;
  1609. struct omap_hsmmc_host *host = NULL;
  1610. struct resource *res;
  1611. int ret, irq;
  1612. const struct of_device_id *match;
  1613. dma_cap_mask_t mask;
  1614. unsigned tx_req, rx_req;
  1615. const struct omap_mmc_of_data *data;
  1616. void __iomem *base;
  1617. match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
  1618. if (match) {
  1619. pdata = of_get_hsmmc_pdata(&pdev->dev);
  1620. if (IS_ERR(pdata))
  1621. return PTR_ERR(pdata);
  1622. if (match->data) {
  1623. data = match->data;
  1624. pdata->reg_offset = data->reg_offset;
  1625. pdata->controller_flags |= data->controller_flags;
  1626. }
  1627. }
  1628. if (pdata == NULL) {
  1629. dev_err(&pdev->dev, "Platform Data is missing\n");
  1630. return -ENXIO;
  1631. }
  1632. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1633. irq = platform_get_irq(pdev, 0);
  1634. if (res == NULL || irq < 0)
  1635. return -ENXIO;
  1636. base = devm_ioremap_resource(&pdev->dev, res);
  1637. if (IS_ERR(base))
  1638. return PTR_ERR(base);
  1639. mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
  1640. if (!mmc) {
  1641. ret = -ENOMEM;
  1642. goto err;
  1643. }
  1644. ret = mmc_of_parse(mmc);
  1645. if (ret)
  1646. goto err1;
  1647. host = mmc_priv(mmc);
  1648. host->mmc = mmc;
  1649. host->pdata = pdata;
  1650. host->dev = &pdev->dev;
  1651. host->use_dma = 1;
  1652. host->dma_ch = -1;
  1653. host->irq = irq;
  1654. host->mapbase = res->start + pdata->reg_offset;
  1655. host->base = base + pdata->reg_offset;
  1656. host->power_mode = MMC_POWER_OFF;
  1657. host->next_data.cookie = 1;
  1658. host->pbias_enabled = 0;
  1659. ret = omap_hsmmc_gpio_init(mmc, host, pdata);
  1660. if (ret)
  1661. goto err_gpio;
  1662. platform_set_drvdata(pdev, host);
  1663. if (pdev->dev.of_node)
  1664. host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
  1665. mmc->ops = &omap_hsmmc_ops;
  1666. mmc->f_min = OMAP_MMC_MIN_CLOCK;
  1667. if (pdata->max_freq > 0)
  1668. mmc->f_max = pdata->max_freq;
  1669. else if (mmc->f_max == 0)
  1670. mmc->f_max = OMAP_MMC_MAX_CLOCK;
  1671. spin_lock_init(&host->irq_lock);
  1672. host->fclk = devm_clk_get(&pdev->dev, "fck");
  1673. if (IS_ERR(host->fclk)) {
  1674. ret = PTR_ERR(host->fclk);
  1675. host->fclk = NULL;
  1676. goto err1;
  1677. }
  1678. if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
  1679. dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
  1680. omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
  1681. }
  1682. device_init_wakeup(&pdev->dev, true);
  1683. pm_runtime_enable(host->dev);
  1684. pm_runtime_get_sync(host->dev);
  1685. pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
  1686. pm_runtime_use_autosuspend(host->dev);
  1687. omap_hsmmc_context_save(host);
  1688. host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
  1689. /*
  1690. * MMC can still work without debounce clock.
  1691. */
  1692. if (IS_ERR(host->dbclk)) {
  1693. host->dbclk = NULL;
  1694. } else if (clk_prepare_enable(host->dbclk) != 0) {
  1695. dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
  1696. host->dbclk = NULL;
  1697. }
  1698. /* Since we do only SG emulation, we can have as many segs
  1699. * as we want. */
  1700. mmc->max_segs = 1024;
  1701. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  1702. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  1703. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1704. mmc->max_seg_size = mmc->max_req_size;
  1705. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  1706. MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
  1707. mmc->caps |= mmc_pdata(host)->caps;
  1708. if (mmc->caps & MMC_CAP_8_BIT_DATA)
  1709. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1710. if (mmc_pdata(host)->nonremovable)
  1711. mmc->caps |= MMC_CAP_NONREMOVABLE;
  1712. mmc->pm_caps |= mmc_pdata(host)->pm_caps;
  1713. omap_hsmmc_conf_bus_power(host);
  1714. if (!pdev->dev.of_node) {
  1715. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  1716. if (!res) {
  1717. dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
  1718. ret = -ENXIO;
  1719. goto err_irq;
  1720. }
  1721. tx_req = res->start;
  1722. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  1723. if (!res) {
  1724. dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
  1725. ret = -ENXIO;
  1726. goto err_irq;
  1727. }
  1728. rx_req = res->start;
  1729. }
  1730. dma_cap_zero(mask);
  1731. dma_cap_set(DMA_SLAVE, mask);
  1732. host->rx_chan =
  1733. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  1734. &rx_req, &pdev->dev, "rx");
  1735. if (!host->rx_chan) {
  1736. dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
  1737. ret = -ENXIO;
  1738. goto err_irq;
  1739. }
  1740. host->tx_chan =
  1741. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  1742. &tx_req, &pdev->dev, "tx");
  1743. if (!host->tx_chan) {
  1744. dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
  1745. ret = -ENXIO;
  1746. goto err_irq;
  1747. }
  1748. /* Request IRQ for MMC operations */
  1749. ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
  1750. mmc_hostname(mmc), host);
  1751. if (ret) {
  1752. dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  1753. goto err_irq;
  1754. }
  1755. if (omap_hsmmc_have_reg() && !mmc_pdata(host)->set_power) {
  1756. ret = omap_hsmmc_reg_get(host);
  1757. if (ret)
  1758. goto err_irq;
  1759. host->use_reg = 1;
  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. if (host->use_reg)
  1794. omap_hsmmc_reg_put(host);
  1795. err_irq:
  1796. device_init_wakeup(&pdev->dev, false);
  1797. if (host->tx_chan)
  1798. dma_release_channel(host->tx_chan);
  1799. if (host->rx_chan)
  1800. dma_release_channel(host->rx_chan);
  1801. pm_runtime_put_sync(host->dev);
  1802. pm_runtime_disable(host->dev);
  1803. if (host->dbclk)
  1804. clk_disable_unprepare(host->dbclk);
  1805. err1:
  1806. err_gpio:
  1807. mmc_free_host(mmc);
  1808. err:
  1809. return ret;
  1810. }
  1811. static int omap_hsmmc_remove(struct platform_device *pdev)
  1812. {
  1813. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  1814. pm_runtime_get_sync(host->dev);
  1815. mmc_remove_host(host->mmc);
  1816. if (host->use_reg)
  1817. omap_hsmmc_reg_put(host);
  1818. if (host->tx_chan)
  1819. dma_release_channel(host->tx_chan);
  1820. if (host->rx_chan)
  1821. dma_release_channel(host->rx_chan);
  1822. pm_runtime_put_sync(host->dev);
  1823. pm_runtime_disable(host->dev);
  1824. device_init_wakeup(&pdev->dev, false);
  1825. if (host->dbclk)
  1826. clk_disable_unprepare(host->dbclk);
  1827. mmc_free_host(host->mmc);
  1828. return 0;
  1829. }
  1830. #ifdef CONFIG_PM_SLEEP
  1831. static int omap_hsmmc_suspend(struct device *dev)
  1832. {
  1833. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1834. if (!host)
  1835. return 0;
  1836. pm_runtime_get_sync(host->dev);
  1837. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
  1838. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1839. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1840. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1841. OMAP_HSMMC_WRITE(host->base, HCTL,
  1842. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1843. }
  1844. if (host->dbclk)
  1845. clk_disable_unprepare(host->dbclk);
  1846. pm_runtime_put_sync(host->dev);
  1847. return 0;
  1848. }
  1849. /* Routine to resume the MMC device */
  1850. static int omap_hsmmc_resume(struct device *dev)
  1851. {
  1852. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1853. if (!host)
  1854. return 0;
  1855. pm_runtime_get_sync(host->dev);
  1856. if (host->dbclk)
  1857. clk_prepare_enable(host->dbclk);
  1858. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
  1859. omap_hsmmc_conf_bus_power(host);
  1860. omap_hsmmc_protect_card(host);
  1861. pm_runtime_mark_last_busy(host->dev);
  1862. pm_runtime_put_autosuspend(host->dev);
  1863. return 0;
  1864. }
  1865. #endif
  1866. static int omap_hsmmc_runtime_suspend(struct device *dev)
  1867. {
  1868. struct omap_hsmmc_host *host;
  1869. unsigned long flags;
  1870. int ret = 0;
  1871. host = platform_get_drvdata(to_platform_device(dev));
  1872. omap_hsmmc_context_save(host);
  1873. dev_dbg(dev, "disabled\n");
  1874. spin_lock_irqsave(&host->irq_lock, flags);
  1875. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  1876. (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
  1877. /* disable sdio irq handling to prevent race */
  1878. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1879. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1880. if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
  1881. /*
  1882. * dat1 line low, pending sdio irq
  1883. * race condition: possible irq handler running on
  1884. * multi-core, abort
  1885. */
  1886. dev_dbg(dev, "pending sdio irq, abort suspend\n");
  1887. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1888. OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
  1889. OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
  1890. pm_runtime_mark_last_busy(dev);
  1891. ret = -EBUSY;
  1892. goto abort;
  1893. }
  1894. pinctrl_pm_select_idle_state(dev);
  1895. } else {
  1896. pinctrl_pm_select_idle_state(dev);
  1897. }
  1898. abort:
  1899. spin_unlock_irqrestore(&host->irq_lock, flags);
  1900. return ret;
  1901. }
  1902. static int omap_hsmmc_runtime_resume(struct device *dev)
  1903. {
  1904. struct omap_hsmmc_host *host;
  1905. unsigned long flags;
  1906. host = platform_get_drvdata(to_platform_device(dev));
  1907. omap_hsmmc_context_restore(host);
  1908. dev_dbg(dev, "enabled\n");
  1909. spin_lock_irqsave(&host->irq_lock, flags);
  1910. if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
  1911. (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
  1912. pinctrl_pm_select_default_state(host->dev);
  1913. /* irq lost, if pinmux incorrect */
  1914. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  1915. OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
  1916. OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
  1917. } else {
  1918. pinctrl_pm_select_default_state(host->dev);
  1919. }
  1920. spin_unlock_irqrestore(&host->irq_lock, flags);
  1921. return 0;
  1922. }
  1923. static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
  1924. SET_SYSTEM_SLEEP_PM_OPS(omap_hsmmc_suspend, omap_hsmmc_resume)
  1925. .runtime_suspend = omap_hsmmc_runtime_suspend,
  1926. .runtime_resume = omap_hsmmc_runtime_resume,
  1927. };
  1928. static struct platform_driver omap_hsmmc_driver = {
  1929. .probe = omap_hsmmc_probe,
  1930. .remove = omap_hsmmc_remove,
  1931. .driver = {
  1932. .name = DRIVER_NAME,
  1933. .pm = &omap_hsmmc_dev_pm_ops,
  1934. .of_match_table = of_match_ptr(omap_mmc_of_match),
  1935. },
  1936. };
  1937. module_platform_driver(omap_hsmmc_driver);
  1938. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  1939. MODULE_LICENSE("GPL");
  1940. MODULE_ALIAS("platform:" DRIVER_NAME);
  1941. MODULE_AUTHOR("Texas Instruments Inc");