omap_hsmmc.c 54 KB

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