omap_hsmmc.c 56 KB

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