omap_hsmmc.c 57 KB

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