omap_hsmmc.c 53 KB

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