omap_hsmmc.c 55 KB

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