omap_hsmmc.c 53 KB

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