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