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