omap_hsmmc.c 54 KB

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