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