omap_hsmmc.c 54 KB

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