omap_hsmmc.c 52 KB

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