omap_hsmmc.c 58 KB

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