omap_hsmmc.c 58 KB

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