omap_hsmmc.c 53 KB

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