omap_hsmmc.c 54 KB

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