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