omap_hsmmc.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261
  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-dmaengine.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_MMC_DDR52) &&
  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_MMC_DDR52)
  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. if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
  785. !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
  786. host->cmd = NULL;
  787. omap_hsmmc_start_dma_transfer(host);
  788. omap_hsmmc_start_command(host, host->mrq->cmd,
  789. host->mrq->data);
  790. return;
  791. }
  792. host->cmd = NULL;
  793. if (cmd->flags & MMC_RSP_PRESENT) {
  794. if (cmd->flags & MMC_RSP_136) {
  795. /* response type 2 */
  796. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  797. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  798. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  799. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  800. } else {
  801. /* response types 1, 1b, 3, 4, 5, 6 */
  802. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  803. }
  804. }
  805. if ((host->data == NULL && !host->response_busy) || cmd->error)
  806. omap_hsmmc_request_done(host, host->mrq);
  807. }
  808. /*
  809. * DMA clean up for command errors
  810. */
  811. static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
  812. {
  813. int dma_ch;
  814. unsigned long flags;
  815. host->data->error = errno;
  816. spin_lock_irqsave(&host->irq_lock, flags);
  817. dma_ch = host->dma_ch;
  818. host->dma_ch = -1;
  819. spin_unlock_irqrestore(&host->irq_lock, flags);
  820. if (host->use_dma && dma_ch != -1) {
  821. struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
  822. dmaengine_terminate_all(chan);
  823. dma_unmap_sg(chan->device->dev,
  824. host->data->sg, host->data->sg_len,
  825. omap_hsmmc_get_dma_dir(host, host->data));
  826. host->data->host_cookie = 0;
  827. }
  828. host->data = NULL;
  829. }
  830. /*
  831. * Readable error output
  832. */
  833. #ifdef CONFIG_MMC_DEBUG
  834. static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
  835. {
  836. /* --- means reserved bit without definition at documentation */
  837. static const char *omap_hsmmc_status_bits[] = {
  838. "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
  839. "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
  840. "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
  841. "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
  842. };
  843. char res[256];
  844. char *buf = res;
  845. int len, i;
  846. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  847. buf += len;
  848. for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
  849. if (status & (1 << i)) {
  850. len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
  851. buf += len;
  852. }
  853. dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
  854. }
  855. #else
  856. static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
  857. u32 status)
  858. {
  859. }
  860. #endif /* CONFIG_MMC_DEBUG */
  861. /*
  862. * MMC controller internal state machines reset
  863. *
  864. * Used to reset command or data internal state machines, using respectively
  865. * SRC or SRD bit of SYSCTL register
  866. * Can be called from interrupt context
  867. */
  868. static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
  869. unsigned long bit)
  870. {
  871. unsigned long i = 0;
  872. unsigned long limit = MMC_TIMEOUT_US;
  873. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  874. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  875. /*
  876. * OMAP4 ES2 and greater has an updated reset logic.
  877. * Monitor a 0->1 transition first
  878. */
  879. if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
  880. while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
  881. && (i++ < limit))
  882. udelay(1);
  883. }
  884. i = 0;
  885. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  886. (i++ < limit))
  887. udelay(1);
  888. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  889. dev_err(mmc_dev(host->mmc),
  890. "Timeout waiting on controller reset in %s\n",
  891. __func__);
  892. }
  893. static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
  894. int err, int end_cmd)
  895. {
  896. if (end_cmd) {
  897. omap_hsmmc_reset_controller_fsm(host, SRC);
  898. if (host->cmd)
  899. host->cmd->error = err;
  900. }
  901. if (host->data) {
  902. omap_hsmmc_reset_controller_fsm(host, SRD);
  903. omap_hsmmc_dma_cleanup(host, err);
  904. } else if (host->mrq && host->mrq->cmd)
  905. host->mrq->cmd->error = err;
  906. }
  907. static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
  908. {
  909. struct mmc_data *data;
  910. int end_cmd = 0, end_trans = 0;
  911. int error = 0;
  912. data = host->data;
  913. dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  914. if (status & ERR_EN) {
  915. omap_hsmmc_dbg_report_irq(host, status);
  916. if (status & (CTO_EN | CCRC_EN))
  917. end_cmd = 1;
  918. if (status & (CTO_EN | DTO_EN))
  919. hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
  920. else if (status & (CCRC_EN | DCRC_EN))
  921. hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
  922. if (status & ACE_EN) {
  923. u32 ac12;
  924. ac12 = OMAP_HSMMC_READ(host->base, AC12);
  925. if (!(ac12 & ACNE) && host->mrq->sbc) {
  926. end_cmd = 1;
  927. if (ac12 & ACTO)
  928. error = -ETIMEDOUT;
  929. else if (ac12 & (ACCE | ACEB | ACIE))
  930. error = -EILSEQ;
  931. host->mrq->sbc->error = error;
  932. hsmmc_command_incomplete(host, error, end_cmd);
  933. }
  934. dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
  935. }
  936. if (host->data || host->response_busy) {
  937. end_trans = !end_cmd;
  938. host->response_busy = 0;
  939. }
  940. }
  941. OMAP_HSMMC_WRITE(host->base, STAT, status);
  942. if (end_cmd || ((status & CC_EN) && host->cmd))
  943. omap_hsmmc_cmd_done(host, host->cmd);
  944. if ((end_trans || (status & TC_EN)) && host->mrq)
  945. omap_hsmmc_xfer_done(host, data);
  946. }
  947. /*
  948. * MMC controller IRQ handler
  949. */
  950. static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
  951. {
  952. struct omap_hsmmc_host *host = dev_id;
  953. int status;
  954. status = OMAP_HSMMC_READ(host->base, STAT);
  955. while (status & INT_EN_MASK && host->req_in_progress) {
  956. omap_hsmmc_do_irq(host, status);
  957. /* Flush posted write */
  958. status = OMAP_HSMMC_READ(host->base, STAT);
  959. }
  960. return IRQ_HANDLED;
  961. }
  962. static void set_sd_bus_power(struct omap_hsmmc_host *host)
  963. {
  964. unsigned long i;
  965. OMAP_HSMMC_WRITE(host->base, HCTL,
  966. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  967. for (i = 0; i < loops_per_jiffy; i++) {
  968. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  969. break;
  970. cpu_relax();
  971. }
  972. }
  973. /*
  974. * Switch MMC interface voltage ... only relevant for MMC1.
  975. *
  976. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  977. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  978. * Some chips, like eMMC ones, use internal transceivers.
  979. */
  980. static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
  981. {
  982. u32 reg_val = 0;
  983. int ret;
  984. /* Disable the clocks */
  985. pm_runtime_put_sync(host->dev);
  986. if (host->dbclk)
  987. clk_disable_unprepare(host->dbclk);
  988. /* Turn the power off */
  989. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  990. /* Turn the power ON with given VDD 1.8 or 3.0v */
  991. if (!ret)
  992. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
  993. vdd);
  994. pm_runtime_get_sync(host->dev);
  995. if (host->dbclk)
  996. clk_prepare_enable(host->dbclk);
  997. if (ret != 0)
  998. goto err;
  999. OMAP_HSMMC_WRITE(host->base, HCTL,
  1000. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  1001. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  1002. /*
  1003. * If a MMC dual voltage card is detected, the set_ios fn calls
  1004. * this fn with VDD bit set for 1.8V. Upon card removal from the
  1005. * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  1006. *
  1007. * Cope with a bit of slop in the range ... per data sheets:
  1008. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  1009. * but recommended values are 1.71V to 1.89V
  1010. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  1011. * but recommended values are 2.7V to 3.3V
  1012. *
  1013. * Board setup code shouldn't permit anything very out-of-range.
  1014. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  1015. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  1016. */
  1017. if ((1 << vdd) <= MMC_VDD_23_24)
  1018. reg_val |= SDVS18;
  1019. else
  1020. reg_val |= SDVS30;
  1021. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  1022. set_sd_bus_power(host);
  1023. return 0;
  1024. err:
  1025. dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  1026. return ret;
  1027. }
  1028. /* Protect the card while the cover is open */
  1029. static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
  1030. {
  1031. if (!mmc_slot(host).get_cover_state)
  1032. return;
  1033. host->reqs_blocked = 0;
  1034. if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
  1035. if (host->protect_card) {
  1036. dev_info(host->dev, "%s: cover is closed, "
  1037. "card is now accessible\n",
  1038. mmc_hostname(host->mmc));
  1039. host->protect_card = 0;
  1040. }
  1041. } else {
  1042. if (!host->protect_card) {
  1043. dev_info(host->dev, "%s: cover is open, "
  1044. "card is now inaccessible\n",
  1045. mmc_hostname(host->mmc));
  1046. host->protect_card = 1;
  1047. }
  1048. }
  1049. }
  1050. /*
  1051. * irq handler to notify the core about card insertion/removal
  1052. */
  1053. static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
  1054. {
  1055. struct omap_hsmmc_host *host = dev_id;
  1056. struct omap_mmc_slot_data *slot = &mmc_slot(host);
  1057. int carddetect;
  1058. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  1059. if (slot->card_detect)
  1060. carddetect = slot->card_detect(host->dev, host->slot_id);
  1061. else {
  1062. omap_hsmmc_protect_card(host);
  1063. carddetect = -ENOSYS;
  1064. }
  1065. if (carddetect)
  1066. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  1067. else
  1068. mmc_detect_change(host->mmc, (HZ * 50) / 1000);
  1069. return IRQ_HANDLED;
  1070. }
  1071. static void omap_hsmmc_dma_callback(void *param)
  1072. {
  1073. struct omap_hsmmc_host *host = param;
  1074. struct dma_chan *chan;
  1075. struct mmc_data *data;
  1076. int req_in_progress;
  1077. spin_lock_irq(&host->irq_lock);
  1078. if (host->dma_ch < 0) {
  1079. spin_unlock_irq(&host->irq_lock);
  1080. return;
  1081. }
  1082. data = host->mrq->data;
  1083. chan = omap_hsmmc_get_dma_chan(host, data);
  1084. if (!data->host_cookie)
  1085. dma_unmap_sg(chan->device->dev,
  1086. data->sg, data->sg_len,
  1087. omap_hsmmc_get_dma_dir(host, data));
  1088. req_in_progress = host->req_in_progress;
  1089. host->dma_ch = -1;
  1090. spin_unlock_irq(&host->irq_lock);
  1091. /* If DMA has finished after TC, complete the request */
  1092. if (!req_in_progress) {
  1093. struct mmc_request *mrq = host->mrq;
  1094. host->mrq = NULL;
  1095. mmc_request_done(host->mmc, mrq);
  1096. }
  1097. }
  1098. static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
  1099. struct mmc_data *data,
  1100. struct omap_hsmmc_next *next,
  1101. struct dma_chan *chan)
  1102. {
  1103. int dma_len;
  1104. if (!next && data->host_cookie &&
  1105. data->host_cookie != host->next_data.cookie) {
  1106. dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
  1107. " host->next_data.cookie %d\n",
  1108. __func__, data->host_cookie, host->next_data.cookie);
  1109. data->host_cookie = 0;
  1110. }
  1111. /* Check if next job is already prepared */
  1112. if (next || data->host_cookie != host->next_data.cookie) {
  1113. dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
  1114. omap_hsmmc_get_dma_dir(host, data));
  1115. } else {
  1116. dma_len = host->next_data.dma_len;
  1117. host->next_data.dma_len = 0;
  1118. }
  1119. if (dma_len == 0)
  1120. return -EINVAL;
  1121. if (next) {
  1122. next->dma_len = dma_len;
  1123. data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
  1124. } else
  1125. host->dma_len = dma_len;
  1126. return 0;
  1127. }
  1128. /*
  1129. * Routine to configure and start DMA for the MMC card
  1130. */
  1131. static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
  1132. struct mmc_request *req)
  1133. {
  1134. struct dma_slave_config cfg;
  1135. struct dma_async_tx_descriptor *tx;
  1136. int ret = 0, i;
  1137. struct mmc_data *data = req->data;
  1138. struct dma_chan *chan;
  1139. /* Sanity check: all the SG entries must be aligned by block size. */
  1140. for (i = 0; i < data->sg_len; i++) {
  1141. struct scatterlist *sgl;
  1142. sgl = data->sg + i;
  1143. if (sgl->length % data->blksz)
  1144. return -EINVAL;
  1145. }
  1146. if ((data->blksz % 4) != 0)
  1147. /* REVISIT: The MMC buffer increments only when MSB is written.
  1148. * Return error for blksz which is non multiple of four.
  1149. */
  1150. return -EINVAL;
  1151. BUG_ON(host->dma_ch != -1);
  1152. chan = omap_hsmmc_get_dma_chan(host, data);
  1153. cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
  1154. cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
  1155. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1156. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1157. cfg.src_maxburst = data->blksz / 4;
  1158. cfg.dst_maxburst = data->blksz / 4;
  1159. ret = dmaengine_slave_config(chan, &cfg);
  1160. if (ret)
  1161. return ret;
  1162. ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
  1163. if (ret)
  1164. return ret;
  1165. tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
  1166. data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  1167. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1168. if (!tx) {
  1169. dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
  1170. /* FIXME: cleanup */
  1171. return -1;
  1172. }
  1173. tx->callback = omap_hsmmc_dma_callback;
  1174. tx->callback_param = host;
  1175. /* Does not fail */
  1176. dmaengine_submit(tx);
  1177. host->dma_ch = 1;
  1178. return 0;
  1179. }
  1180. static void set_data_timeout(struct omap_hsmmc_host *host,
  1181. unsigned int timeout_ns,
  1182. unsigned int timeout_clks)
  1183. {
  1184. unsigned int timeout, cycle_ns;
  1185. uint32_t reg, clkd, dto = 0;
  1186. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  1187. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  1188. if (clkd == 0)
  1189. clkd = 1;
  1190. cycle_ns = 1000000000 / (host->clk_rate / clkd);
  1191. timeout = timeout_ns / cycle_ns;
  1192. timeout += timeout_clks;
  1193. if (timeout) {
  1194. while ((timeout & 0x80000000) == 0) {
  1195. dto += 1;
  1196. timeout <<= 1;
  1197. }
  1198. dto = 31 - dto;
  1199. timeout <<= 1;
  1200. if (timeout && dto)
  1201. dto += 1;
  1202. if (dto >= 13)
  1203. dto -= 13;
  1204. else
  1205. dto = 0;
  1206. if (dto > 14)
  1207. dto = 14;
  1208. }
  1209. reg &= ~DTO_MASK;
  1210. reg |= dto << DTO_SHIFT;
  1211. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  1212. }
  1213. static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
  1214. {
  1215. struct mmc_request *req = host->mrq;
  1216. struct dma_chan *chan;
  1217. if (!req->data)
  1218. return;
  1219. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  1220. | (req->data->blocks << 16));
  1221. set_data_timeout(host, req->data->timeout_ns,
  1222. req->data->timeout_clks);
  1223. chan = omap_hsmmc_get_dma_chan(host, req->data);
  1224. dma_async_issue_pending(chan);
  1225. }
  1226. /*
  1227. * Configure block length for MMC/SD cards and initiate the transfer.
  1228. */
  1229. static int
  1230. omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
  1231. {
  1232. int ret;
  1233. host->data = req->data;
  1234. if (req->data == NULL) {
  1235. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  1236. /*
  1237. * Set an arbitrary 100ms data timeout for commands with
  1238. * busy signal.
  1239. */
  1240. if (req->cmd->flags & MMC_RSP_BUSY)
  1241. set_data_timeout(host, 100000000U, 0);
  1242. return 0;
  1243. }
  1244. if (host->use_dma) {
  1245. ret = omap_hsmmc_setup_dma_transfer(host, req);
  1246. if (ret != 0) {
  1247. dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
  1248. return ret;
  1249. }
  1250. }
  1251. return 0;
  1252. }
  1253. static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1254. int err)
  1255. {
  1256. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1257. struct mmc_data *data = mrq->data;
  1258. if (host->use_dma && data->host_cookie) {
  1259. struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
  1260. dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
  1261. omap_hsmmc_get_dma_dir(host, data));
  1262. data->host_cookie = 0;
  1263. }
  1264. }
  1265. static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1266. bool is_first_req)
  1267. {
  1268. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1269. if (mrq->data->host_cookie) {
  1270. mrq->data->host_cookie = 0;
  1271. return ;
  1272. }
  1273. if (host->use_dma) {
  1274. struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
  1275. if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
  1276. &host->next_data, c))
  1277. mrq->data->host_cookie = 0;
  1278. }
  1279. }
  1280. /*
  1281. * Request function. for read/write operation
  1282. */
  1283. static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
  1284. {
  1285. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1286. int err;
  1287. BUG_ON(host->req_in_progress);
  1288. BUG_ON(host->dma_ch != -1);
  1289. if (host->protect_card) {
  1290. if (host->reqs_blocked < 3) {
  1291. /*
  1292. * Ensure the controller is left in a consistent
  1293. * state by resetting the command and data state
  1294. * machines.
  1295. */
  1296. omap_hsmmc_reset_controller_fsm(host, SRD);
  1297. omap_hsmmc_reset_controller_fsm(host, SRC);
  1298. host->reqs_blocked += 1;
  1299. }
  1300. req->cmd->error = -EBADF;
  1301. if (req->data)
  1302. req->data->error = -EBADF;
  1303. req->cmd->retries = 0;
  1304. mmc_request_done(mmc, req);
  1305. return;
  1306. } else if (host->reqs_blocked)
  1307. host->reqs_blocked = 0;
  1308. WARN_ON(host->mrq != NULL);
  1309. host->mrq = req;
  1310. host->clk_rate = clk_get_rate(host->fclk);
  1311. err = omap_hsmmc_prepare_data(host, req);
  1312. if (err) {
  1313. req->cmd->error = err;
  1314. if (req->data)
  1315. req->data->error = err;
  1316. host->mrq = NULL;
  1317. mmc_request_done(mmc, req);
  1318. return;
  1319. }
  1320. if (req->sbc && !(host->flags & AUTO_CMD23)) {
  1321. omap_hsmmc_start_command(host, req->sbc, NULL);
  1322. return;
  1323. }
  1324. omap_hsmmc_start_dma_transfer(host);
  1325. omap_hsmmc_start_command(host, req->cmd, req->data);
  1326. }
  1327. /* Routine to configure clock values. Exposed API to core */
  1328. static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1329. {
  1330. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1331. int do_send_init_stream = 0;
  1332. pm_runtime_get_sync(host->dev);
  1333. if (ios->power_mode != host->power_mode) {
  1334. switch (ios->power_mode) {
  1335. case MMC_POWER_OFF:
  1336. mmc_slot(host).set_power(host->dev, host->slot_id,
  1337. 0, 0);
  1338. break;
  1339. case MMC_POWER_UP:
  1340. mmc_slot(host).set_power(host->dev, host->slot_id,
  1341. 1, ios->vdd);
  1342. break;
  1343. case MMC_POWER_ON:
  1344. do_send_init_stream = 1;
  1345. break;
  1346. }
  1347. host->power_mode = ios->power_mode;
  1348. }
  1349. /* FIXME: set registers based only on changes to ios */
  1350. omap_hsmmc_set_bus_width(host);
  1351. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1352. /* Only MMC1 can interface at 3V without some flavor
  1353. * of external transceiver; but they all handle 1.8V.
  1354. */
  1355. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  1356. (ios->vdd == DUAL_VOLT_OCR_BIT)) {
  1357. /*
  1358. * The mmc_select_voltage fn of the core does
  1359. * not seem to set the power_mode to
  1360. * MMC_POWER_UP upon recalculating the voltage.
  1361. * vdd 1.8v.
  1362. */
  1363. if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
  1364. dev_dbg(mmc_dev(host->mmc),
  1365. "Switch operation failed\n");
  1366. }
  1367. }
  1368. omap_hsmmc_set_clock(host);
  1369. if (do_send_init_stream)
  1370. send_init_stream(host);
  1371. omap_hsmmc_set_bus_mode(host);
  1372. pm_runtime_put_autosuspend(host->dev);
  1373. }
  1374. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  1375. {
  1376. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1377. if (!mmc_slot(host).card_detect)
  1378. return -ENOSYS;
  1379. return mmc_slot(host).card_detect(host->dev, host->slot_id);
  1380. }
  1381. static int omap_hsmmc_get_ro(struct mmc_host *mmc)
  1382. {
  1383. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1384. if (!mmc_slot(host).get_ro)
  1385. return -ENOSYS;
  1386. return mmc_slot(host).get_ro(host->dev, 0);
  1387. }
  1388. static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
  1389. {
  1390. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1391. if (mmc_slot(host).init_card)
  1392. mmc_slot(host).init_card(card);
  1393. }
  1394. static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
  1395. {
  1396. u32 hctl, capa, value;
  1397. /* Only MMC1 supports 3.0V */
  1398. if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
  1399. hctl = SDVS30;
  1400. capa = VS30 | VS18;
  1401. } else {
  1402. hctl = SDVS18;
  1403. capa = VS18;
  1404. }
  1405. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  1406. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  1407. value = OMAP_HSMMC_READ(host->base, CAPA);
  1408. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  1409. /* Set SD bus power bit */
  1410. set_sd_bus_power(host);
  1411. }
  1412. static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
  1413. {
  1414. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1415. pm_runtime_get_sync(host->dev);
  1416. return 0;
  1417. }
  1418. static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
  1419. {
  1420. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1421. pm_runtime_mark_last_busy(host->dev);
  1422. pm_runtime_put_autosuspend(host->dev);
  1423. return 0;
  1424. }
  1425. static const struct mmc_host_ops omap_hsmmc_ops = {
  1426. .enable = omap_hsmmc_enable_fclk,
  1427. .disable = omap_hsmmc_disable_fclk,
  1428. .post_req = omap_hsmmc_post_req,
  1429. .pre_req = omap_hsmmc_pre_req,
  1430. .request = omap_hsmmc_request,
  1431. .set_ios = omap_hsmmc_set_ios,
  1432. .get_cd = omap_hsmmc_get_cd,
  1433. .get_ro = omap_hsmmc_get_ro,
  1434. .init_card = omap_hsmmc_init_card,
  1435. /* NYET -- enable_sdio_irq */
  1436. };
  1437. #ifdef CONFIG_DEBUG_FS
  1438. static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
  1439. {
  1440. struct mmc_host *mmc = s->private;
  1441. struct omap_hsmmc_host *host = mmc_priv(mmc);
  1442. seq_printf(s, "mmc%d:\n ctx_loss:\t%d\n\nregs:\n",
  1443. mmc->index, host->context_loss);
  1444. pm_runtime_get_sync(host->dev);
  1445. seq_printf(s, "CON:\t\t0x%08x\n",
  1446. OMAP_HSMMC_READ(host->base, CON));
  1447. seq_printf(s, "HCTL:\t\t0x%08x\n",
  1448. OMAP_HSMMC_READ(host->base, HCTL));
  1449. seq_printf(s, "SYSCTL:\t\t0x%08x\n",
  1450. OMAP_HSMMC_READ(host->base, SYSCTL));
  1451. seq_printf(s, "IE:\t\t0x%08x\n",
  1452. OMAP_HSMMC_READ(host->base, IE));
  1453. seq_printf(s, "ISE:\t\t0x%08x\n",
  1454. OMAP_HSMMC_READ(host->base, ISE));
  1455. seq_printf(s, "CAPA:\t\t0x%08x\n",
  1456. OMAP_HSMMC_READ(host->base, CAPA));
  1457. pm_runtime_mark_last_busy(host->dev);
  1458. pm_runtime_put_autosuspend(host->dev);
  1459. return 0;
  1460. }
  1461. static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
  1462. {
  1463. return single_open(file, omap_hsmmc_regs_show, inode->i_private);
  1464. }
  1465. static const struct file_operations mmc_regs_fops = {
  1466. .open = omap_hsmmc_regs_open,
  1467. .read = seq_read,
  1468. .llseek = seq_lseek,
  1469. .release = single_release,
  1470. };
  1471. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1472. {
  1473. if (mmc->debugfs_root)
  1474. debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
  1475. mmc, &mmc_regs_fops);
  1476. }
  1477. #else
  1478. static void omap_hsmmc_debugfs(struct mmc_host *mmc)
  1479. {
  1480. }
  1481. #endif
  1482. #ifdef CONFIG_OF
  1483. static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
  1484. /* See 35xx errata 2.1.1.128 in SPRZ278F */
  1485. .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
  1486. };
  1487. static const struct omap_mmc_of_data omap4_mmc_of_data = {
  1488. .reg_offset = 0x100,
  1489. };
  1490. static const struct of_device_id omap_mmc_of_match[] = {
  1491. {
  1492. .compatible = "ti,omap2-hsmmc",
  1493. },
  1494. {
  1495. .compatible = "ti,omap3-pre-es3-hsmmc",
  1496. .data = &omap3_pre_es3_mmc_of_data,
  1497. },
  1498. {
  1499. .compatible = "ti,omap3-hsmmc",
  1500. },
  1501. {
  1502. .compatible = "ti,omap4-hsmmc",
  1503. .data = &omap4_mmc_of_data,
  1504. },
  1505. {},
  1506. };
  1507. MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
  1508. static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
  1509. {
  1510. struct omap_mmc_platform_data *pdata;
  1511. struct device_node *np = dev->of_node;
  1512. u32 bus_width, max_freq;
  1513. int cd_gpio, wp_gpio;
  1514. cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
  1515. wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
  1516. if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
  1517. return ERR_PTR(-EPROBE_DEFER);
  1518. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  1519. if (!pdata)
  1520. return ERR_PTR(-ENOMEM); /* out of memory */
  1521. if (of_find_property(np, "ti,dual-volt", NULL))
  1522. pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
  1523. /* This driver only supports 1 slot */
  1524. pdata->nr_slots = 1;
  1525. pdata->slots[0].switch_pin = cd_gpio;
  1526. pdata->slots[0].gpio_wp = wp_gpio;
  1527. if (of_find_property(np, "ti,non-removable", NULL)) {
  1528. pdata->slots[0].nonremovable = true;
  1529. pdata->slots[0].no_regulator_off_init = true;
  1530. }
  1531. of_property_read_u32(np, "bus-width", &bus_width);
  1532. if (bus_width == 4)
  1533. pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
  1534. else if (bus_width == 8)
  1535. pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
  1536. if (of_find_property(np, "ti,needs-special-reset", NULL))
  1537. pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
  1538. if (!of_property_read_u32(np, "max-frequency", &max_freq))
  1539. pdata->max_freq = max_freq;
  1540. if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
  1541. pdata->slots[0].features |= HSMMC_HAS_HSPE_SUPPORT;
  1542. if (of_find_property(np, "keep-power-in-suspend", NULL))
  1543. pdata->slots[0].pm_caps |= MMC_PM_KEEP_POWER;
  1544. if (of_find_property(np, "enable-sdio-wakeup", NULL))
  1545. pdata->slots[0].pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
  1546. return pdata;
  1547. }
  1548. #else
  1549. static inline struct omap_mmc_platform_data
  1550. *of_get_hsmmc_pdata(struct device *dev)
  1551. {
  1552. return ERR_PTR(-EINVAL);
  1553. }
  1554. #endif
  1555. static int omap_hsmmc_probe(struct platform_device *pdev)
  1556. {
  1557. struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
  1558. struct mmc_host *mmc;
  1559. struct omap_hsmmc_host *host = NULL;
  1560. struct resource *res;
  1561. int ret, irq;
  1562. const struct of_device_id *match;
  1563. dma_cap_mask_t mask;
  1564. unsigned tx_req, rx_req;
  1565. struct pinctrl *pinctrl;
  1566. const struct omap_mmc_of_data *data;
  1567. void __iomem *base;
  1568. match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
  1569. if (match) {
  1570. pdata = of_get_hsmmc_pdata(&pdev->dev);
  1571. if (IS_ERR(pdata))
  1572. return PTR_ERR(pdata);
  1573. if (match->data) {
  1574. data = match->data;
  1575. pdata->reg_offset = data->reg_offset;
  1576. pdata->controller_flags |= data->controller_flags;
  1577. }
  1578. }
  1579. if (pdata == NULL) {
  1580. dev_err(&pdev->dev, "Platform Data is missing\n");
  1581. return -ENXIO;
  1582. }
  1583. if (pdata->nr_slots == 0) {
  1584. dev_err(&pdev->dev, "No Slots\n");
  1585. return -ENXIO;
  1586. }
  1587. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1588. irq = platform_get_irq(pdev, 0);
  1589. if (res == NULL || irq < 0)
  1590. return -ENXIO;
  1591. base = devm_ioremap_resource(&pdev->dev, res);
  1592. if (IS_ERR(base))
  1593. return PTR_ERR(base);
  1594. ret = omap_hsmmc_gpio_init(pdata);
  1595. if (ret)
  1596. goto err;
  1597. mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
  1598. if (!mmc) {
  1599. ret = -ENOMEM;
  1600. goto err_alloc;
  1601. }
  1602. host = mmc_priv(mmc);
  1603. host->mmc = mmc;
  1604. host->pdata = pdata;
  1605. host->dev = &pdev->dev;
  1606. host->use_dma = 1;
  1607. host->dma_ch = -1;
  1608. host->irq = irq;
  1609. host->slot_id = 0;
  1610. host->mapbase = res->start + pdata->reg_offset;
  1611. host->base = base + pdata->reg_offset;
  1612. host->power_mode = MMC_POWER_OFF;
  1613. host->next_data.cookie = 1;
  1614. host->pbias_enabled = 0;
  1615. platform_set_drvdata(pdev, host);
  1616. mmc->ops = &omap_hsmmc_ops;
  1617. mmc->f_min = OMAP_MMC_MIN_CLOCK;
  1618. if (pdata->max_freq > 0)
  1619. mmc->f_max = pdata->max_freq;
  1620. else
  1621. mmc->f_max = OMAP_MMC_MAX_CLOCK;
  1622. spin_lock_init(&host->irq_lock);
  1623. host->fclk = devm_clk_get(&pdev->dev, "fck");
  1624. if (IS_ERR(host->fclk)) {
  1625. ret = PTR_ERR(host->fclk);
  1626. host->fclk = NULL;
  1627. goto err1;
  1628. }
  1629. if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
  1630. dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
  1631. mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
  1632. }
  1633. pm_runtime_enable(host->dev);
  1634. pm_runtime_get_sync(host->dev);
  1635. pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
  1636. pm_runtime_use_autosuspend(host->dev);
  1637. omap_hsmmc_context_save(host);
  1638. host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
  1639. /*
  1640. * MMC can still work without debounce clock.
  1641. */
  1642. if (IS_ERR(host->dbclk)) {
  1643. host->dbclk = NULL;
  1644. } else if (clk_prepare_enable(host->dbclk) != 0) {
  1645. dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
  1646. host->dbclk = NULL;
  1647. }
  1648. /* Since we do only SG emulation, we can have as many segs
  1649. * as we want. */
  1650. mmc->max_segs = 1024;
  1651. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  1652. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  1653. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1654. mmc->max_seg_size = mmc->max_req_size;
  1655. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  1656. MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
  1657. mmc->caps |= mmc_slot(host).caps;
  1658. if (mmc->caps & MMC_CAP_8_BIT_DATA)
  1659. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1660. if (mmc_slot(host).nonremovable)
  1661. mmc->caps |= MMC_CAP_NONREMOVABLE;
  1662. mmc->pm_caps = mmc_slot(host).pm_caps;
  1663. omap_hsmmc_conf_bus_power(host);
  1664. if (!pdev->dev.of_node) {
  1665. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  1666. if (!res) {
  1667. dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
  1668. ret = -ENXIO;
  1669. goto err_irq;
  1670. }
  1671. tx_req = res->start;
  1672. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  1673. if (!res) {
  1674. dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
  1675. ret = -ENXIO;
  1676. goto err_irq;
  1677. }
  1678. rx_req = res->start;
  1679. }
  1680. dma_cap_zero(mask);
  1681. dma_cap_set(DMA_SLAVE, mask);
  1682. host->rx_chan =
  1683. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  1684. &rx_req, &pdev->dev, "rx");
  1685. if (!host->rx_chan) {
  1686. dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
  1687. ret = -ENXIO;
  1688. goto err_irq;
  1689. }
  1690. host->tx_chan =
  1691. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  1692. &tx_req, &pdev->dev, "tx");
  1693. if (!host->tx_chan) {
  1694. dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
  1695. ret = -ENXIO;
  1696. goto err_irq;
  1697. }
  1698. /* Request IRQ for MMC operations */
  1699. ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
  1700. mmc_hostname(mmc), host);
  1701. if (ret) {
  1702. dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  1703. goto err_irq;
  1704. }
  1705. if (pdata->init != NULL) {
  1706. if (pdata->init(&pdev->dev) != 0) {
  1707. dev_err(mmc_dev(host->mmc),
  1708. "Unable to configure MMC IRQs\n");
  1709. goto err_irq;
  1710. }
  1711. }
  1712. if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
  1713. ret = omap_hsmmc_reg_get(host);
  1714. if (ret)
  1715. goto err_reg;
  1716. host->use_reg = 1;
  1717. }
  1718. mmc->ocr_avail = mmc_slot(host).ocr_mask;
  1719. /* Request IRQ for card detect */
  1720. if ((mmc_slot(host).card_detect_irq)) {
  1721. ret = devm_request_threaded_irq(&pdev->dev,
  1722. mmc_slot(host).card_detect_irq,
  1723. NULL, omap_hsmmc_detect,
  1724. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1725. mmc_hostname(mmc), host);
  1726. if (ret) {
  1727. dev_err(mmc_dev(host->mmc),
  1728. "Unable to grab MMC CD IRQ\n");
  1729. goto err_irq_cd;
  1730. }
  1731. pdata->suspend = omap_hsmmc_suspend_cdirq;
  1732. pdata->resume = omap_hsmmc_resume_cdirq;
  1733. }
  1734. omap_hsmmc_disable_irq(host);
  1735. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  1736. if (IS_ERR(pinctrl))
  1737. dev_warn(&pdev->dev,
  1738. "pins are not configured from the driver\n");
  1739. omap_hsmmc_protect_card(host);
  1740. mmc_add_host(mmc);
  1741. if (mmc_slot(host).name != NULL) {
  1742. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  1743. if (ret < 0)
  1744. goto err_slot_name;
  1745. }
  1746. if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
  1747. ret = device_create_file(&mmc->class_dev,
  1748. &dev_attr_cover_switch);
  1749. if (ret < 0)
  1750. goto err_slot_name;
  1751. }
  1752. omap_hsmmc_debugfs(mmc);
  1753. pm_runtime_mark_last_busy(host->dev);
  1754. pm_runtime_put_autosuspend(host->dev);
  1755. return 0;
  1756. err_slot_name:
  1757. mmc_remove_host(mmc);
  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:
  1765. if (host->tx_chan)
  1766. dma_release_channel(host->tx_chan);
  1767. if (host->rx_chan)
  1768. dma_release_channel(host->rx_chan);
  1769. pm_runtime_put_sync(host->dev);
  1770. pm_runtime_disable(host->dev);
  1771. if (host->dbclk)
  1772. clk_disable_unprepare(host->dbclk);
  1773. err1:
  1774. mmc_free_host(mmc);
  1775. err_alloc:
  1776. omap_hsmmc_gpio_free(pdata);
  1777. err:
  1778. return ret;
  1779. }
  1780. static int omap_hsmmc_remove(struct platform_device *pdev)
  1781. {
  1782. struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
  1783. pm_runtime_get_sync(host->dev);
  1784. mmc_remove_host(host->mmc);
  1785. if (host->use_reg)
  1786. omap_hsmmc_reg_put(host);
  1787. if (host->pdata->cleanup)
  1788. host->pdata->cleanup(&pdev->dev);
  1789. if (host->tx_chan)
  1790. dma_release_channel(host->tx_chan);
  1791. if (host->rx_chan)
  1792. dma_release_channel(host->rx_chan);
  1793. pm_runtime_put_sync(host->dev);
  1794. pm_runtime_disable(host->dev);
  1795. if (host->dbclk)
  1796. clk_disable_unprepare(host->dbclk);
  1797. omap_hsmmc_gpio_free(host->pdata);
  1798. mmc_free_host(host->mmc);
  1799. return 0;
  1800. }
  1801. #ifdef CONFIG_PM
  1802. static int omap_hsmmc_prepare(struct device *dev)
  1803. {
  1804. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1805. if (host->pdata->suspend)
  1806. return host->pdata->suspend(dev, host->slot_id);
  1807. return 0;
  1808. }
  1809. static void omap_hsmmc_complete(struct device *dev)
  1810. {
  1811. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1812. if (host->pdata->resume)
  1813. host->pdata->resume(dev, host->slot_id);
  1814. }
  1815. static int omap_hsmmc_suspend(struct device *dev)
  1816. {
  1817. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1818. if (!host)
  1819. return 0;
  1820. pm_runtime_get_sync(host->dev);
  1821. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
  1822. omap_hsmmc_disable_irq(host);
  1823. OMAP_HSMMC_WRITE(host->base, HCTL,
  1824. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1825. }
  1826. if (host->dbclk)
  1827. clk_disable_unprepare(host->dbclk);
  1828. pm_runtime_put_sync(host->dev);
  1829. return 0;
  1830. }
  1831. /* Routine to resume the MMC device */
  1832. static int omap_hsmmc_resume(struct device *dev)
  1833. {
  1834. struct omap_hsmmc_host *host = dev_get_drvdata(dev);
  1835. if (!host)
  1836. return 0;
  1837. pm_runtime_get_sync(host->dev);
  1838. if (host->dbclk)
  1839. clk_prepare_enable(host->dbclk);
  1840. if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
  1841. omap_hsmmc_conf_bus_power(host);
  1842. omap_hsmmc_protect_card(host);
  1843. pm_runtime_mark_last_busy(host->dev);
  1844. pm_runtime_put_autosuspend(host->dev);
  1845. return 0;
  1846. }
  1847. #else
  1848. #define omap_hsmmc_prepare NULL
  1849. #define omap_hsmmc_complete NULL
  1850. #define omap_hsmmc_suspend NULL
  1851. #define omap_hsmmc_resume NULL
  1852. #endif
  1853. static int omap_hsmmc_runtime_suspend(struct device *dev)
  1854. {
  1855. struct omap_hsmmc_host *host;
  1856. host = platform_get_drvdata(to_platform_device(dev));
  1857. omap_hsmmc_context_save(host);
  1858. dev_dbg(dev, "disabled\n");
  1859. return 0;
  1860. }
  1861. static int omap_hsmmc_runtime_resume(struct device *dev)
  1862. {
  1863. struct omap_hsmmc_host *host;
  1864. host = platform_get_drvdata(to_platform_device(dev));
  1865. omap_hsmmc_context_restore(host);
  1866. dev_dbg(dev, "enabled\n");
  1867. return 0;
  1868. }
  1869. static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
  1870. .suspend = omap_hsmmc_suspend,
  1871. .resume = omap_hsmmc_resume,
  1872. .prepare = omap_hsmmc_prepare,
  1873. .complete = omap_hsmmc_complete,
  1874. .runtime_suspend = omap_hsmmc_runtime_suspend,
  1875. .runtime_resume = omap_hsmmc_runtime_resume,
  1876. };
  1877. static struct platform_driver omap_hsmmc_driver = {
  1878. .probe = omap_hsmmc_probe,
  1879. .remove = omap_hsmmc_remove,
  1880. .driver = {
  1881. .name = DRIVER_NAME,
  1882. .owner = THIS_MODULE,
  1883. .pm = &omap_hsmmc_dev_pm_ops,
  1884. .of_match_table = of_match_ptr(omap_mmc_of_match),
  1885. },
  1886. };
  1887. module_platform_driver(omap_hsmmc_driver);
  1888. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  1889. MODULE_LICENSE("GPL");
  1890. MODULE_ALIAS("platform:" DRIVER_NAME);
  1891. MODULE_AUTHOR("Texas Instruments Inc");