omap_hsmmc.c 56 KB

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