omap_hsmmc.c 54 KB

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