omap_hsmmc.c 56 KB

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