omap_hsmmc.c 53 KB

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