omap_hsmmc.c 56 KB

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