omap_hsmmc.c 54 KB

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