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