omap_hsmmc.c 56 KB

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