omap_hsmmc.c 54 KB

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