omap_hsmmc.c 60 KB

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