omap_hsmmc.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  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/io.h>
  30. #include <linux/semaphore.h>
  31. #include <mach/dma.h>
  32. #include <mach/hardware.h>
  33. #include <mach/board.h>
  34. #include <mach/mmc.h>
  35. #include <mach/cpu.h>
  36. /* OMAP HSMMC Host Controller Registers */
  37. #define OMAP_HSMMC_SYSCONFIG 0x0010
  38. #define OMAP_HSMMC_SYSSTATUS 0x0014
  39. #define OMAP_HSMMC_CON 0x002C
  40. #define OMAP_HSMMC_BLK 0x0104
  41. #define OMAP_HSMMC_ARG 0x0108
  42. #define OMAP_HSMMC_CMD 0x010C
  43. #define OMAP_HSMMC_RSP10 0x0110
  44. #define OMAP_HSMMC_RSP32 0x0114
  45. #define OMAP_HSMMC_RSP54 0x0118
  46. #define OMAP_HSMMC_RSP76 0x011C
  47. #define OMAP_HSMMC_DATA 0x0120
  48. #define OMAP_HSMMC_HCTL 0x0128
  49. #define OMAP_HSMMC_SYSCTL 0x012C
  50. #define OMAP_HSMMC_STAT 0x0130
  51. #define OMAP_HSMMC_IE 0x0134
  52. #define OMAP_HSMMC_ISE 0x0138
  53. #define OMAP_HSMMC_CAPA 0x0140
  54. #define VS18 (1 << 26)
  55. #define VS30 (1 << 25)
  56. #define SDVS18 (0x5 << 9)
  57. #define SDVS30 (0x6 << 9)
  58. #define SDVS33 (0x7 << 9)
  59. #define SDVS_MASK 0x00000E00
  60. #define SDVSCLR 0xFFFFF1FF
  61. #define SDVSDET 0x00000400
  62. #define AUTOIDLE 0x1
  63. #define SDBP (1 << 8)
  64. #define DTO 0xe
  65. #define ICE 0x1
  66. #define ICS 0x2
  67. #define CEN (1 << 2)
  68. #define CLKD_MASK 0x0000FFC0
  69. #define CLKD_SHIFT 6
  70. #define DTO_MASK 0x000F0000
  71. #define DTO_SHIFT 16
  72. #define INT_EN_MASK 0x307F0033
  73. #define BWR_ENABLE (1 << 4)
  74. #define BRR_ENABLE (1 << 5)
  75. #define INIT_STREAM (1 << 1)
  76. #define DP_SELECT (1 << 21)
  77. #define DDIR (1 << 4)
  78. #define DMA_EN 0x1
  79. #define MSBS (1 << 5)
  80. #define BCE (1 << 1)
  81. #define FOUR_BIT (1 << 1)
  82. #define DW8 (1 << 5)
  83. #define CC 0x1
  84. #define TC 0x02
  85. #define OD 0x1
  86. #define ERR (1 << 15)
  87. #define CMD_TIMEOUT (1 << 16)
  88. #define DATA_TIMEOUT (1 << 20)
  89. #define CMD_CRC (1 << 17)
  90. #define DATA_CRC (1 << 21)
  91. #define CARD_ERR (1 << 28)
  92. #define STAT_CLEAR 0xFFFFFFFF
  93. #define INIT_STREAM_CMD 0x00000000
  94. #define DUAL_VOLT_OCR_BIT 7
  95. #define SRC (1 << 25)
  96. #define SRD (1 << 26)
  97. #define SOFTRESET (1 << 1)
  98. #define RESETDONE (1 << 0)
  99. /*
  100. * FIXME: Most likely all the data using these _DEVID defines should come
  101. * from the platform_data, or implemented in controller and slot specific
  102. * functions.
  103. */
  104. #define OMAP_MMC1_DEVID 0
  105. #define OMAP_MMC2_DEVID 1
  106. #define OMAP_MMC3_DEVID 2
  107. #define MMC_TIMEOUT_MS 20
  108. #define OMAP_MMC_MASTER_CLOCK 96000000
  109. #define DRIVER_NAME "mmci-omap-hs"
  110. /* Timeouts for entering power saving states on inactivity, msec */
  111. #define OMAP_MMC_DISABLED_TIMEOUT 100
  112. #define OMAP_MMC_OFF_TIMEOUT 1000
  113. /*
  114. * One controller can have multiple slots, like on some omap boards using
  115. * omap.c controller driver. Luckily this is not currently done on any known
  116. * omap_hsmmc.c device.
  117. */
  118. #define mmc_slot(host) (host->pdata->slots[host->slot_id])
  119. /*
  120. * MMC Host controller read/write API's
  121. */
  122. #define OMAP_HSMMC_READ(base, reg) \
  123. __raw_readl((base) + OMAP_HSMMC_##reg)
  124. #define OMAP_HSMMC_WRITE(base, reg, val) \
  125. __raw_writel((val), (base) + OMAP_HSMMC_##reg)
  126. struct mmc_omap_host {
  127. struct device *dev;
  128. struct mmc_host *mmc;
  129. struct mmc_request *mrq;
  130. struct mmc_command *cmd;
  131. struct mmc_data *data;
  132. struct clk *fclk;
  133. struct clk *iclk;
  134. struct clk *dbclk;
  135. struct semaphore sem;
  136. struct work_struct mmc_carddetect_work;
  137. void __iomem *base;
  138. resource_size_t mapbase;
  139. unsigned int id;
  140. unsigned int dma_len;
  141. unsigned int dma_sg_idx;
  142. unsigned char bus_mode;
  143. unsigned char power_mode;
  144. u32 *buffer;
  145. u32 bytesleft;
  146. int suspended;
  147. int irq;
  148. int use_dma, dma_ch;
  149. int dma_line_tx, dma_line_rx;
  150. int slot_id;
  151. int dbclk_enabled;
  152. int response_busy;
  153. int context_loss;
  154. int dpm_state;
  155. int vdd;
  156. struct omap_mmc_platform_data *pdata;
  157. };
  158. /*
  159. * Stop clock to the card
  160. */
  161. static void omap_mmc_stop_clock(struct mmc_omap_host *host)
  162. {
  163. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  164. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  165. if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
  166. dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
  167. }
  168. #ifdef CONFIG_PM
  169. /*
  170. * Restore the MMC host context, if it was lost as result of a
  171. * power state change.
  172. */
  173. static int omap_mmc_restore_ctx(struct mmc_omap_host *host)
  174. {
  175. struct mmc_ios *ios = &host->mmc->ios;
  176. struct omap_mmc_platform_data *pdata = host->pdata;
  177. int context_loss = 0;
  178. u32 hctl, capa, con;
  179. u16 dsor = 0;
  180. unsigned long timeout;
  181. if (pdata->get_context_loss_count) {
  182. context_loss = pdata->get_context_loss_count(host->dev);
  183. if (context_loss < 0)
  184. return 1;
  185. }
  186. dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
  187. context_loss == host->context_loss ? "not " : "");
  188. if (host->context_loss == context_loss)
  189. return 1;
  190. /* Wait for hardware reset */
  191. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  192. while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
  193. && time_before(jiffies, timeout))
  194. ;
  195. /* Do software reset */
  196. OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
  197. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  198. while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
  199. && time_before(jiffies, timeout))
  200. ;
  201. OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
  202. OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
  203. if (host->id == OMAP_MMC1_DEVID) {
  204. if (host->power_mode != MMC_POWER_OFF &&
  205. (1 << ios->vdd) <= MMC_VDD_23_24)
  206. hctl = SDVS18;
  207. else
  208. hctl = SDVS30;
  209. capa = VS30 | VS18;
  210. } else {
  211. hctl = SDVS18;
  212. capa = VS18;
  213. }
  214. OMAP_HSMMC_WRITE(host->base, HCTL,
  215. OMAP_HSMMC_READ(host->base, HCTL) | hctl);
  216. OMAP_HSMMC_WRITE(host->base, CAPA,
  217. OMAP_HSMMC_READ(host->base, CAPA) | capa);
  218. OMAP_HSMMC_WRITE(host->base, HCTL,
  219. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  220. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  221. while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
  222. && time_before(jiffies, timeout))
  223. ;
  224. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  225. OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
  226. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  227. /* Do not initialize card-specific things if the power is off */
  228. if (host->power_mode == MMC_POWER_OFF)
  229. goto out;
  230. con = OMAP_HSMMC_READ(host->base, CON);
  231. switch (ios->bus_width) {
  232. case MMC_BUS_WIDTH_8:
  233. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  234. break;
  235. case MMC_BUS_WIDTH_4:
  236. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  237. OMAP_HSMMC_WRITE(host->base, HCTL,
  238. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  239. break;
  240. case MMC_BUS_WIDTH_1:
  241. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  242. OMAP_HSMMC_WRITE(host->base, HCTL,
  243. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  244. break;
  245. }
  246. if (ios->clock) {
  247. dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
  248. if (dsor < 1)
  249. dsor = 1;
  250. if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
  251. dsor++;
  252. if (dsor > 250)
  253. dsor = 250;
  254. }
  255. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  256. OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
  257. OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
  258. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  259. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  260. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  261. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  262. && time_before(jiffies, timeout))
  263. ;
  264. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  265. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  266. con = OMAP_HSMMC_READ(host->base, CON);
  267. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  268. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  269. else
  270. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  271. out:
  272. host->context_loss = context_loss;
  273. dev_dbg(mmc_dev(host->mmc), "context is restored\n");
  274. return 0;
  275. }
  276. /*
  277. * Save the MMC host context (store the number of power state changes so far).
  278. */
  279. static void omap_mmc_save_ctx(struct mmc_omap_host *host)
  280. {
  281. struct omap_mmc_platform_data *pdata = host->pdata;
  282. int context_loss;
  283. if (pdata->get_context_loss_count) {
  284. context_loss = pdata->get_context_loss_count(host->dev);
  285. if (context_loss < 0)
  286. return;
  287. host->context_loss = context_loss;
  288. }
  289. }
  290. #else
  291. static int omap_mmc_restore_ctx(struct mmc_omap_host *host)
  292. {
  293. return 0;
  294. }
  295. static void omap_mmc_save_ctx(struct mmc_omap_host *host)
  296. {
  297. }
  298. #endif
  299. /*
  300. * Send init stream sequence to card
  301. * before sending IDLE command
  302. */
  303. static void send_init_stream(struct mmc_omap_host *host)
  304. {
  305. int reg = 0;
  306. unsigned long timeout;
  307. disable_irq(host->irq);
  308. OMAP_HSMMC_WRITE(host->base, CON,
  309. OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
  310. OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
  311. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  312. while ((reg != CC) && time_before(jiffies, timeout))
  313. reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
  314. OMAP_HSMMC_WRITE(host->base, CON,
  315. OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
  316. enable_irq(host->irq);
  317. }
  318. static inline
  319. int mmc_omap_cover_is_closed(struct mmc_omap_host *host)
  320. {
  321. int r = 1;
  322. if (host->pdata->slots[host->slot_id].get_cover_state)
  323. r = host->pdata->slots[host->slot_id].get_cover_state(host->dev,
  324. host->slot_id);
  325. return r;
  326. }
  327. static ssize_t
  328. mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
  329. char *buf)
  330. {
  331. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  332. struct mmc_omap_host *host = mmc_priv(mmc);
  333. return sprintf(buf, "%s\n", mmc_omap_cover_is_closed(host) ? "closed" :
  334. "open");
  335. }
  336. static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
  337. static ssize_t
  338. mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
  339. char *buf)
  340. {
  341. struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
  342. struct mmc_omap_host *host = mmc_priv(mmc);
  343. struct omap_mmc_slot_data slot = host->pdata->slots[host->slot_id];
  344. return sprintf(buf, "%s\n", slot.name);
  345. }
  346. static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
  347. /*
  348. * Configure the response type and send the cmd.
  349. */
  350. static void
  351. mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd,
  352. struct mmc_data *data)
  353. {
  354. int cmdreg = 0, resptype = 0, cmdtype = 0;
  355. dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
  356. mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
  357. host->cmd = cmd;
  358. /*
  359. * Clear status bits and enable interrupts
  360. */
  361. OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
  362. OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
  363. if (host->use_dma)
  364. OMAP_HSMMC_WRITE(host->base, IE,
  365. INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE));
  366. else
  367. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  368. host->response_busy = 0;
  369. if (cmd->flags & MMC_RSP_PRESENT) {
  370. if (cmd->flags & MMC_RSP_136)
  371. resptype = 1;
  372. else if (cmd->flags & MMC_RSP_BUSY) {
  373. resptype = 3;
  374. host->response_busy = 1;
  375. } else
  376. resptype = 2;
  377. }
  378. /*
  379. * Unlike OMAP1 controller, the cmdtype does not seem to be based on
  380. * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
  381. * a val of 0x3, rest 0x0.
  382. */
  383. if (cmd == host->mrq->stop)
  384. cmdtype = 0x3;
  385. cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
  386. if (data) {
  387. cmdreg |= DP_SELECT | MSBS | BCE;
  388. if (data->flags & MMC_DATA_READ)
  389. cmdreg |= DDIR;
  390. else
  391. cmdreg &= ~(DDIR);
  392. }
  393. if (host->use_dma)
  394. cmdreg |= DMA_EN;
  395. OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
  396. OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
  397. }
  398. static int
  399. mmc_omap_get_dma_dir(struct mmc_omap_host *host, struct mmc_data *data)
  400. {
  401. if (data->flags & MMC_DATA_WRITE)
  402. return DMA_TO_DEVICE;
  403. else
  404. return DMA_FROM_DEVICE;
  405. }
  406. /*
  407. * Notify the transfer complete to MMC core
  408. */
  409. static void
  410. mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
  411. {
  412. if (!data) {
  413. struct mmc_request *mrq = host->mrq;
  414. host->mrq = NULL;
  415. mmc_request_done(host->mmc, mrq);
  416. return;
  417. }
  418. host->data = NULL;
  419. if (host->use_dma && host->dma_ch != -1)
  420. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
  421. mmc_omap_get_dma_dir(host, data));
  422. if (!data->error)
  423. data->bytes_xfered += data->blocks * (data->blksz);
  424. else
  425. data->bytes_xfered = 0;
  426. if (!data->stop) {
  427. host->mrq = NULL;
  428. mmc_request_done(host->mmc, data->mrq);
  429. return;
  430. }
  431. mmc_omap_start_command(host, data->stop, NULL);
  432. }
  433. /*
  434. * Notify the core about command completion
  435. */
  436. static void
  437. mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
  438. {
  439. host->cmd = NULL;
  440. if (cmd->flags & MMC_RSP_PRESENT) {
  441. if (cmd->flags & MMC_RSP_136) {
  442. /* response type 2 */
  443. cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
  444. cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
  445. cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
  446. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
  447. } else {
  448. /* response types 1, 1b, 3, 4, 5, 6 */
  449. cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
  450. }
  451. }
  452. if ((host->data == NULL && !host->response_busy) || cmd->error) {
  453. host->mrq = NULL;
  454. mmc_request_done(host->mmc, cmd->mrq);
  455. }
  456. }
  457. /*
  458. * DMA clean up for command errors
  459. */
  460. static void mmc_dma_cleanup(struct mmc_omap_host *host, int errno)
  461. {
  462. host->data->error = errno;
  463. if (host->use_dma && host->dma_ch != -1) {
  464. dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
  465. mmc_omap_get_dma_dir(host, host->data));
  466. omap_free_dma(host->dma_ch);
  467. host->dma_ch = -1;
  468. up(&host->sem);
  469. }
  470. host->data = NULL;
  471. }
  472. /*
  473. * Readable error output
  474. */
  475. #ifdef CONFIG_MMC_DEBUG
  476. static void mmc_omap_report_irq(struct mmc_omap_host *host, u32 status)
  477. {
  478. /* --- means reserved bit without definition at documentation */
  479. static const char *mmc_omap_status_bits[] = {
  480. "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
  481. "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
  482. "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
  483. "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
  484. };
  485. char res[256];
  486. char *buf = res;
  487. int len, i;
  488. len = sprintf(buf, "MMC IRQ 0x%x :", status);
  489. buf += len;
  490. for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
  491. if (status & (1 << i)) {
  492. len = sprintf(buf, " %s", mmc_omap_status_bits[i]);
  493. buf += len;
  494. }
  495. dev_dbg(mmc_dev(host->mmc), "%s\n", res);
  496. }
  497. #endif /* CONFIG_MMC_DEBUG */
  498. /*
  499. * MMC controller internal state machines reset
  500. *
  501. * Used to reset command or data internal state machines, using respectively
  502. * SRC or SRD bit of SYSCTL register
  503. * Can be called from interrupt context
  504. */
  505. static inline void mmc_omap_reset_controller_fsm(struct mmc_omap_host *host,
  506. unsigned long bit)
  507. {
  508. unsigned long i = 0;
  509. unsigned long limit = (loops_per_jiffy *
  510. msecs_to_jiffies(MMC_TIMEOUT_MS));
  511. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  512. OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
  513. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
  514. (i++ < limit))
  515. cpu_relax();
  516. if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
  517. dev_err(mmc_dev(host->mmc),
  518. "Timeout waiting on controller reset in %s\n",
  519. __func__);
  520. }
  521. /*
  522. * MMC controller IRQ handler
  523. */
  524. static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
  525. {
  526. struct mmc_omap_host *host = dev_id;
  527. struct mmc_data *data;
  528. int end_cmd = 0, end_trans = 0, status;
  529. if (host->mrq == NULL) {
  530. OMAP_HSMMC_WRITE(host->base, STAT,
  531. OMAP_HSMMC_READ(host->base, STAT));
  532. /* Flush posted write */
  533. OMAP_HSMMC_READ(host->base, STAT);
  534. return IRQ_HANDLED;
  535. }
  536. data = host->data;
  537. status = OMAP_HSMMC_READ(host->base, STAT);
  538. dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
  539. if (status & ERR) {
  540. #ifdef CONFIG_MMC_DEBUG
  541. mmc_omap_report_irq(host, status);
  542. #endif
  543. if ((status & CMD_TIMEOUT) ||
  544. (status & CMD_CRC)) {
  545. if (host->cmd) {
  546. if (status & CMD_TIMEOUT) {
  547. mmc_omap_reset_controller_fsm(host, SRC);
  548. host->cmd->error = -ETIMEDOUT;
  549. } else {
  550. host->cmd->error = -EILSEQ;
  551. }
  552. end_cmd = 1;
  553. }
  554. if (host->data || host->response_busy) {
  555. if (host->data)
  556. mmc_dma_cleanup(host, -ETIMEDOUT);
  557. host->response_busy = 0;
  558. mmc_omap_reset_controller_fsm(host, SRD);
  559. }
  560. }
  561. if ((status & DATA_TIMEOUT) ||
  562. (status & DATA_CRC)) {
  563. if (host->data || host->response_busy) {
  564. int err = (status & DATA_TIMEOUT) ?
  565. -ETIMEDOUT : -EILSEQ;
  566. if (host->data)
  567. mmc_dma_cleanup(host, err);
  568. else
  569. host->mrq->cmd->error = err;
  570. host->response_busy = 0;
  571. mmc_omap_reset_controller_fsm(host, SRD);
  572. end_trans = 1;
  573. }
  574. }
  575. if (status & CARD_ERR) {
  576. dev_dbg(mmc_dev(host->mmc),
  577. "Ignoring card err CMD%d\n", host->cmd->opcode);
  578. if (host->cmd)
  579. end_cmd = 1;
  580. if (host->data)
  581. end_trans = 1;
  582. }
  583. }
  584. OMAP_HSMMC_WRITE(host->base, STAT, status);
  585. /* Flush posted write */
  586. OMAP_HSMMC_READ(host->base, STAT);
  587. if (end_cmd || ((status & CC) && host->cmd))
  588. mmc_omap_cmd_done(host, host->cmd);
  589. if (end_trans || (status & TC))
  590. mmc_omap_xfer_done(host, data);
  591. return IRQ_HANDLED;
  592. }
  593. static void set_sd_bus_power(struct mmc_omap_host *host)
  594. {
  595. unsigned long i;
  596. OMAP_HSMMC_WRITE(host->base, HCTL,
  597. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  598. for (i = 0; i < loops_per_jiffy; i++) {
  599. if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
  600. break;
  601. cpu_relax();
  602. }
  603. }
  604. /*
  605. * Switch MMC interface voltage ... only relevant for MMC1.
  606. *
  607. * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
  608. * The MMC2 transceiver controls are used instead of DAT4..DAT7.
  609. * Some chips, like eMMC ones, use internal transceivers.
  610. */
  611. static int omap_mmc_switch_opcond(struct mmc_omap_host *host, int vdd)
  612. {
  613. u32 reg_val = 0;
  614. int ret;
  615. /* Disable the clocks */
  616. clk_disable(host->fclk);
  617. clk_disable(host->iclk);
  618. clk_disable(host->dbclk);
  619. /* Turn the power off */
  620. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
  621. if (ret != 0)
  622. goto err;
  623. /* Turn the power ON with given VDD 1.8 or 3.0v */
  624. ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
  625. if (ret != 0)
  626. goto err;
  627. clk_enable(host->fclk);
  628. clk_enable(host->iclk);
  629. clk_enable(host->dbclk);
  630. OMAP_HSMMC_WRITE(host->base, HCTL,
  631. OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
  632. reg_val = OMAP_HSMMC_READ(host->base, HCTL);
  633. /*
  634. * If a MMC dual voltage card is detected, the set_ios fn calls
  635. * this fn with VDD bit set for 1.8V. Upon card removal from the
  636. * slot, omap_mmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
  637. *
  638. * Cope with a bit of slop in the range ... per data sheets:
  639. * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
  640. * but recommended values are 1.71V to 1.89V
  641. * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
  642. * but recommended values are 2.7V to 3.3V
  643. *
  644. * Board setup code shouldn't permit anything very out-of-range.
  645. * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
  646. * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
  647. */
  648. if ((1 << vdd) <= MMC_VDD_23_24)
  649. reg_val |= SDVS18;
  650. else
  651. reg_val |= SDVS30;
  652. OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
  653. set_sd_bus_power(host);
  654. return 0;
  655. err:
  656. dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
  657. return ret;
  658. }
  659. /*
  660. * Work Item to notify the core about card insertion/removal
  661. */
  662. static void mmc_omap_detect(struct work_struct *work)
  663. {
  664. struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
  665. mmc_carddetect_work);
  666. struct omap_mmc_slot_data *slot = &mmc_slot(host);
  667. int carddetect;
  668. if (host->suspended)
  669. return;
  670. sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
  671. if (mmc_slot(host).card_detect)
  672. carddetect = slot->card_detect(slot->card_detect_irq);
  673. else
  674. carddetect = -ENOSYS;
  675. if (carddetect) {
  676. mmc_detect_change(host->mmc, (HZ * 200) / 1000);
  677. } else {
  678. mmc_host_enable(host->mmc);
  679. mmc_omap_reset_controller_fsm(host, SRD);
  680. mmc_host_lazy_disable(host->mmc);
  681. mmc_detect_change(host->mmc, (HZ * 50) / 1000);
  682. }
  683. }
  684. /*
  685. * ISR for handling card insertion and removal
  686. */
  687. static irqreturn_t omap_mmc_cd_handler(int irq, void *dev_id)
  688. {
  689. struct mmc_omap_host *host = (struct mmc_omap_host *)dev_id;
  690. if (host->suspended)
  691. return IRQ_HANDLED;
  692. schedule_work(&host->mmc_carddetect_work);
  693. return IRQ_HANDLED;
  694. }
  695. static int mmc_omap_get_dma_sync_dev(struct mmc_omap_host *host,
  696. struct mmc_data *data)
  697. {
  698. int sync_dev;
  699. if (data->flags & MMC_DATA_WRITE)
  700. sync_dev = host->dma_line_tx;
  701. else
  702. sync_dev = host->dma_line_rx;
  703. return sync_dev;
  704. }
  705. static void mmc_omap_config_dma_params(struct mmc_omap_host *host,
  706. struct mmc_data *data,
  707. struct scatterlist *sgl)
  708. {
  709. int blksz, nblk, dma_ch;
  710. dma_ch = host->dma_ch;
  711. if (data->flags & MMC_DATA_WRITE) {
  712. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  713. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  714. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  715. sg_dma_address(sgl), 0, 0);
  716. } else {
  717. omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  718. (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
  719. omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  720. sg_dma_address(sgl), 0, 0);
  721. }
  722. blksz = host->data->blksz;
  723. nblk = sg_dma_len(sgl) / blksz;
  724. omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
  725. blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
  726. mmc_omap_get_dma_sync_dev(host, data),
  727. !(data->flags & MMC_DATA_WRITE));
  728. omap_start_dma(dma_ch);
  729. }
  730. /*
  731. * DMA call back function
  732. */
  733. static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
  734. {
  735. struct mmc_omap_host *host = data;
  736. if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
  737. dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
  738. if (host->dma_ch < 0)
  739. return;
  740. host->dma_sg_idx++;
  741. if (host->dma_sg_idx < host->dma_len) {
  742. /* Fire up the next transfer. */
  743. mmc_omap_config_dma_params(host, host->data,
  744. host->data->sg + host->dma_sg_idx);
  745. return;
  746. }
  747. omap_free_dma(host->dma_ch);
  748. host->dma_ch = -1;
  749. /*
  750. * DMA Callback: run in interrupt context.
  751. * mutex_unlock will throw a kernel warning if used.
  752. */
  753. up(&host->sem);
  754. }
  755. /*
  756. * Routine to configure and start DMA for the MMC card
  757. */
  758. static int
  759. mmc_omap_start_dma_transfer(struct mmc_omap_host *host, struct mmc_request *req)
  760. {
  761. int dma_ch = 0, ret = 0, err = 1, i;
  762. struct mmc_data *data = req->data;
  763. /* Sanity check: all the SG entries must be aligned by block size. */
  764. for (i = 0; i < data->sg_len; i++) {
  765. struct scatterlist *sgl;
  766. sgl = data->sg + i;
  767. if (sgl->length % data->blksz)
  768. return -EINVAL;
  769. }
  770. if ((data->blksz % 4) != 0)
  771. /* REVISIT: The MMC buffer increments only when MSB is written.
  772. * Return error for blksz which is non multiple of four.
  773. */
  774. return -EINVAL;
  775. /*
  776. * If for some reason the DMA transfer is still active,
  777. * we wait for timeout period and free the dma
  778. */
  779. if (host->dma_ch != -1) {
  780. set_current_state(TASK_UNINTERRUPTIBLE);
  781. schedule_timeout(100);
  782. if (down_trylock(&host->sem)) {
  783. omap_free_dma(host->dma_ch);
  784. host->dma_ch = -1;
  785. up(&host->sem);
  786. return err;
  787. }
  788. } else {
  789. if (down_trylock(&host->sem))
  790. return err;
  791. }
  792. ret = omap_request_dma(mmc_omap_get_dma_sync_dev(host, data), "MMC/SD",
  793. mmc_omap_dma_cb,host, &dma_ch);
  794. if (ret != 0) {
  795. dev_err(mmc_dev(host->mmc),
  796. "%s: omap_request_dma() failed with %d\n",
  797. mmc_hostname(host->mmc), ret);
  798. return ret;
  799. }
  800. host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  801. data->sg_len, mmc_omap_get_dma_dir(host, data));
  802. host->dma_ch = dma_ch;
  803. host->dma_sg_idx = 0;
  804. mmc_omap_config_dma_params(host, data, data->sg);
  805. return 0;
  806. }
  807. static void set_data_timeout(struct mmc_omap_host *host,
  808. struct mmc_request *req)
  809. {
  810. unsigned int timeout, cycle_ns;
  811. uint32_t reg, clkd, dto = 0;
  812. reg = OMAP_HSMMC_READ(host->base, SYSCTL);
  813. clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
  814. if (clkd == 0)
  815. clkd = 1;
  816. cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
  817. timeout = req->data->timeout_ns / cycle_ns;
  818. timeout += req->data->timeout_clks;
  819. if (timeout) {
  820. while ((timeout & 0x80000000) == 0) {
  821. dto += 1;
  822. timeout <<= 1;
  823. }
  824. dto = 31 - dto;
  825. timeout <<= 1;
  826. if (timeout && dto)
  827. dto += 1;
  828. if (dto >= 13)
  829. dto -= 13;
  830. else
  831. dto = 0;
  832. if (dto > 14)
  833. dto = 14;
  834. }
  835. reg &= ~DTO_MASK;
  836. reg |= dto << DTO_SHIFT;
  837. OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
  838. }
  839. /*
  840. * Configure block length for MMC/SD cards and initiate the transfer.
  841. */
  842. static int
  843. mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
  844. {
  845. int ret;
  846. host->data = req->data;
  847. if (req->data == NULL) {
  848. OMAP_HSMMC_WRITE(host->base, BLK, 0);
  849. return 0;
  850. }
  851. OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
  852. | (req->data->blocks << 16));
  853. set_data_timeout(host, req);
  854. if (host->use_dma) {
  855. ret = mmc_omap_start_dma_transfer(host, req);
  856. if (ret != 0) {
  857. dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
  858. return ret;
  859. }
  860. }
  861. return 0;
  862. }
  863. /*
  864. * Request function. for read/write operation
  865. */
  866. static void omap_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
  867. {
  868. struct mmc_omap_host *host = mmc_priv(mmc);
  869. int err;
  870. WARN_ON(host->mrq != NULL);
  871. host->mrq = req;
  872. err = mmc_omap_prepare_data(host, req);
  873. if (err) {
  874. req->cmd->error = err;
  875. if (req->data)
  876. req->data->error = err;
  877. host->mrq = NULL;
  878. mmc_request_done(mmc, req);
  879. return;
  880. }
  881. mmc_omap_start_command(host, req->cmd, req->data);
  882. }
  883. /* Routine to configure clock values. Exposed API to core */
  884. static void omap_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  885. {
  886. struct mmc_omap_host *host = mmc_priv(mmc);
  887. u16 dsor = 0;
  888. unsigned long regval;
  889. unsigned long timeout;
  890. u32 con;
  891. int do_send_init_stream = 0;
  892. mmc_host_enable(host->mmc);
  893. if (ios->power_mode != host->power_mode) {
  894. switch (ios->power_mode) {
  895. case MMC_POWER_OFF:
  896. mmc_slot(host).set_power(host->dev, host->slot_id,
  897. 0, 0);
  898. host->vdd = 0;
  899. break;
  900. case MMC_POWER_UP:
  901. mmc_slot(host).set_power(host->dev, host->slot_id,
  902. 1, ios->vdd);
  903. host->vdd = ios->vdd;
  904. break;
  905. case MMC_POWER_ON:
  906. do_send_init_stream = 1;
  907. break;
  908. }
  909. host->power_mode = ios->power_mode;
  910. }
  911. /* FIXME: set registers based only on changes to ios */
  912. con = OMAP_HSMMC_READ(host->base, CON);
  913. switch (mmc->ios.bus_width) {
  914. case MMC_BUS_WIDTH_8:
  915. OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
  916. break;
  917. case MMC_BUS_WIDTH_4:
  918. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  919. OMAP_HSMMC_WRITE(host->base, HCTL,
  920. OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
  921. break;
  922. case MMC_BUS_WIDTH_1:
  923. OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
  924. OMAP_HSMMC_WRITE(host->base, HCTL,
  925. OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
  926. break;
  927. }
  928. if (host->id == OMAP_MMC1_DEVID) {
  929. /* Only MMC1 can interface at 3V without some flavor
  930. * of external transceiver; but they all handle 1.8V.
  931. */
  932. if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
  933. (ios->vdd == DUAL_VOLT_OCR_BIT)) {
  934. /*
  935. * The mmc_select_voltage fn of the core does
  936. * not seem to set the power_mode to
  937. * MMC_POWER_UP upon recalculating the voltage.
  938. * vdd 1.8v.
  939. */
  940. if (omap_mmc_switch_opcond(host, ios->vdd) != 0)
  941. dev_dbg(mmc_dev(host->mmc),
  942. "Switch operation failed\n");
  943. }
  944. }
  945. if (ios->clock) {
  946. dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
  947. if (dsor < 1)
  948. dsor = 1;
  949. if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
  950. dsor++;
  951. if (dsor > 250)
  952. dsor = 250;
  953. }
  954. omap_mmc_stop_clock(host);
  955. regval = OMAP_HSMMC_READ(host->base, SYSCTL);
  956. regval = regval & ~(CLKD_MASK);
  957. regval = regval | (dsor << 6) | (DTO << 16);
  958. OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
  959. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  960. OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
  961. /* Wait till the ICS bit is set */
  962. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  963. while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
  964. && time_before(jiffies, timeout))
  965. msleep(1);
  966. OMAP_HSMMC_WRITE(host->base, SYSCTL,
  967. OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
  968. if (do_send_init_stream)
  969. send_init_stream(host);
  970. con = OMAP_HSMMC_READ(host->base, CON);
  971. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  972. OMAP_HSMMC_WRITE(host->base, CON, con | OD);
  973. else
  974. OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
  975. if (host->power_mode == MMC_POWER_OFF)
  976. mmc_host_disable(host->mmc);
  977. else
  978. mmc_host_lazy_disable(host->mmc);
  979. }
  980. static int omap_hsmmc_get_cd(struct mmc_host *mmc)
  981. {
  982. struct mmc_omap_host *host = mmc_priv(mmc);
  983. struct omap_mmc_platform_data *pdata = host->pdata;
  984. if (!pdata->slots[0].card_detect)
  985. return -ENOSYS;
  986. return pdata->slots[0].card_detect(pdata->slots[0].card_detect_irq);
  987. }
  988. static int omap_hsmmc_get_ro(struct mmc_host *mmc)
  989. {
  990. struct mmc_omap_host *host = mmc_priv(mmc);
  991. struct omap_mmc_platform_data *pdata = host->pdata;
  992. if (!pdata->slots[0].get_ro)
  993. return -ENOSYS;
  994. return pdata->slots[0].get_ro(host->dev, 0);
  995. }
  996. static void omap_hsmmc_init(struct mmc_omap_host *host)
  997. {
  998. u32 hctl, capa, value;
  999. /* Only MMC1 supports 3.0V */
  1000. if (host->id == OMAP_MMC1_DEVID) {
  1001. hctl = SDVS30;
  1002. capa = VS30 | VS18;
  1003. } else {
  1004. hctl = SDVS18;
  1005. capa = VS18;
  1006. }
  1007. value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
  1008. OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
  1009. value = OMAP_HSMMC_READ(host->base, CAPA);
  1010. OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
  1011. /* Set the controller to AUTO IDLE mode */
  1012. value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
  1013. OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
  1014. /* Set SD bus power bit */
  1015. set_sd_bus_power(host);
  1016. }
  1017. /*
  1018. * Dynamic power saving handling, FSM:
  1019. * ENABLED -> DISABLED -> OFF / REGSLEEP
  1020. * ^___________| |
  1021. * |______________________|
  1022. *
  1023. * ENABLED: mmc host is fully functional
  1024. * DISABLED: fclk is off
  1025. * OFF: fclk is off,voltage regulator is off
  1026. * REGSLEEP: fclk is off,voltage regulator is asleep
  1027. *
  1028. * Transition handlers return the timeout for the next state transition
  1029. * or negative error.
  1030. */
  1031. enum {ENABLED = 0, DISABLED, REGSLEEP, OFF};
  1032. /* Handler for [ENABLED -> DISABLED] transition */
  1033. static int omap_mmc_enabled_to_disabled(struct mmc_omap_host *host)
  1034. {
  1035. omap_mmc_save_ctx(host);
  1036. clk_disable(host->fclk);
  1037. host->dpm_state = DISABLED;
  1038. dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
  1039. if (host->power_mode == MMC_POWER_OFF)
  1040. return 0;
  1041. return msecs_to_jiffies(OMAP_MMC_OFF_TIMEOUT);
  1042. }
  1043. /* Handler for [DISABLED -> OFF] transition */
  1044. static int omap_mmc_disabled_to_off(struct mmc_omap_host *host)
  1045. {
  1046. int new_state;
  1047. dev_dbg(mmc_dev(host->mmc), "DISABLED -> OFF\n");
  1048. if (!mmc_try_claim_host(host->mmc))
  1049. return 0;
  1050. clk_enable(host->fclk);
  1051. omap_mmc_restore_ctx(host);
  1052. if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
  1053. mmc_slot(host).card_detect ||
  1054. (mmc_slot(host).get_cover_state &&
  1055. mmc_slot(host).get_cover_state(host->dev, host->slot_id))) {
  1056. mmc_power_save_host(host->mmc);
  1057. new_state = OFF;
  1058. } else {
  1059. if (mmc_slot(host).set_sleep)
  1060. mmc_slot(host).set_sleep(host->dev, host->slot_id,
  1061. 1, 0, 0);
  1062. new_state = REGSLEEP;
  1063. }
  1064. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1065. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1066. OMAP_HSMMC_WRITE(host->base, HCTL,
  1067. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1068. clk_disable(host->fclk);
  1069. clk_disable(host->iclk);
  1070. clk_disable(host->dbclk);
  1071. host->dpm_state = new_state;
  1072. mmc_release_host(host->mmc);
  1073. return 0;
  1074. }
  1075. /* Handler for [DISABLED -> ENABLED] transition */
  1076. static int omap_mmc_disabled_to_enabled(struct mmc_omap_host *host)
  1077. {
  1078. int err;
  1079. err = clk_enable(host->fclk);
  1080. if (err < 0)
  1081. return err;
  1082. omap_mmc_restore_ctx(host);
  1083. host->dpm_state = ENABLED;
  1084. dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
  1085. return 0;
  1086. }
  1087. /* Handler for [OFF -> ENABLED] transition */
  1088. static int omap_mmc_off_to_enabled(struct mmc_omap_host *host)
  1089. {
  1090. clk_enable(host->fclk);
  1091. clk_enable(host->iclk);
  1092. if (clk_enable(host->dbclk))
  1093. dev_dbg(mmc_dev(host->mmc),
  1094. "Enabling debounce clk failed\n");
  1095. omap_mmc_restore_ctx(host);
  1096. omap_hsmmc_init(host);
  1097. mmc_power_restore_host(host->mmc);
  1098. host->dpm_state = ENABLED;
  1099. dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
  1100. return 0;
  1101. }
  1102. /* Handler for [REGSLEEP -> ENABLED] transition */
  1103. static int omap_mmc_regsleep_to_enabled(struct mmc_omap_host *host)
  1104. {
  1105. unsigned long timeout;
  1106. dev_dbg(mmc_dev(host->mmc), "REGSLEEP -> ENABLED\n");
  1107. clk_enable(host->fclk);
  1108. clk_enable(host->iclk);
  1109. if (clk_enable(host->dbclk))
  1110. dev_dbg(mmc_dev(host->mmc),
  1111. "Enabling debounce clk failed\n");
  1112. omap_mmc_restore_ctx(host);
  1113. /*
  1114. * We turned off interrupts and bus power. Interrupts
  1115. * are turned on by 'mmc_omap_start_command()' so we
  1116. * just need to turn on the bus power here.
  1117. */
  1118. OMAP_HSMMC_WRITE(host->base, HCTL,
  1119. OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
  1120. timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
  1121. while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP &&
  1122. time_before(jiffies, timeout))
  1123. ;
  1124. if (mmc_slot(host).set_sleep)
  1125. mmc_slot(host).set_sleep(host->dev, host->slot_id,
  1126. 0, host->vdd, 0);
  1127. host->dpm_state = ENABLED;
  1128. return 0;
  1129. }
  1130. /*
  1131. * Bring MMC host to ENABLED from any other PM state.
  1132. */
  1133. static int omap_mmc_enable(struct mmc_host *mmc)
  1134. {
  1135. struct mmc_omap_host *host = mmc_priv(mmc);
  1136. switch (host->dpm_state) {
  1137. case DISABLED:
  1138. return omap_mmc_disabled_to_enabled(host);
  1139. case REGSLEEP:
  1140. return omap_mmc_regsleep_to_enabled(host);
  1141. case OFF:
  1142. return omap_mmc_off_to_enabled(host);
  1143. default:
  1144. dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
  1145. return -EINVAL;
  1146. }
  1147. }
  1148. /*
  1149. * Bring MMC host in PM state (one level deeper).
  1150. */
  1151. static int omap_mmc_disable(struct mmc_host *mmc, int lazy)
  1152. {
  1153. struct mmc_omap_host *host = mmc_priv(mmc);
  1154. switch (host->dpm_state) {
  1155. case ENABLED: {
  1156. int delay;
  1157. delay = omap_mmc_enabled_to_disabled(host);
  1158. if (lazy || delay < 0)
  1159. return delay;
  1160. return 0;
  1161. }
  1162. case DISABLED:
  1163. return omap_mmc_disabled_to_off(host);
  1164. default:
  1165. dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
  1166. return -EINVAL;
  1167. }
  1168. }
  1169. static int omap_mmc_enable_fclk(struct mmc_host *mmc)
  1170. {
  1171. struct mmc_omap_host *host = mmc_priv(mmc);
  1172. int err;
  1173. err = clk_enable(host->fclk);
  1174. if (err)
  1175. return err;
  1176. dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
  1177. omap_mmc_restore_ctx(host);
  1178. return 0;
  1179. }
  1180. static int omap_mmc_disable_fclk(struct mmc_host *mmc, int lazy)
  1181. {
  1182. struct mmc_omap_host *host = mmc_priv(mmc);
  1183. omap_mmc_save_ctx(host);
  1184. clk_disable(host->fclk);
  1185. dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
  1186. return 0;
  1187. }
  1188. static const struct mmc_host_ops mmc_omap_ops = {
  1189. .enable = omap_mmc_enable_fclk,
  1190. .disable = omap_mmc_disable_fclk,
  1191. .request = omap_mmc_request,
  1192. .set_ios = omap_mmc_set_ios,
  1193. .get_cd = omap_hsmmc_get_cd,
  1194. .get_ro = omap_hsmmc_get_ro,
  1195. /* NYET -- enable_sdio_irq */
  1196. };
  1197. static const struct mmc_host_ops mmc_omap_ps_ops = {
  1198. .enable = omap_mmc_enable,
  1199. .disable = omap_mmc_disable,
  1200. .request = omap_mmc_request,
  1201. .set_ios = omap_mmc_set_ios,
  1202. .get_cd = omap_hsmmc_get_cd,
  1203. .get_ro = omap_hsmmc_get_ro,
  1204. /* NYET -- enable_sdio_irq */
  1205. };
  1206. #ifdef CONFIG_DEBUG_FS
  1207. static int mmc_regs_show(struct seq_file *s, void *data)
  1208. {
  1209. struct mmc_host *mmc = s->private;
  1210. struct mmc_omap_host *host = mmc_priv(mmc);
  1211. struct omap_mmc_platform_data *pdata = host->pdata;
  1212. int context_loss = 0;
  1213. if (pdata->get_context_loss_count)
  1214. context_loss = pdata->get_context_loss_count(host->dev);
  1215. seq_printf(s, "mmc%d:\n"
  1216. " enabled:\t%d\n"
  1217. " dpm_state:\t%d\n"
  1218. " nesting_cnt:\t%d\n"
  1219. " ctx_loss:\t%d:%d\n"
  1220. "\nregs:\n",
  1221. mmc->index, mmc->enabled ? 1 : 0,
  1222. host->dpm_state, mmc->nesting_cnt,
  1223. host->context_loss, context_loss);
  1224. if (host->suspended || host->dpm_state == OFF ||
  1225. host->dpm_state == REGSLEEP) {
  1226. seq_printf(s, "host suspended, can't read registers\n");
  1227. return 0;
  1228. }
  1229. if (clk_enable(host->fclk) != 0) {
  1230. seq_printf(s, "can't read the regs\n");
  1231. return 0;
  1232. }
  1233. seq_printf(s, "SYSCONFIG:\t0x%08x\n",
  1234. OMAP_HSMMC_READ(host->base, SYSCONFIG));
  1235. seq_printf(s, "CON:\t\t0x%08x\n",
  1236. OMAP_HSMMC_READ(host->base, CON));
  1237. seq_printf(s, "HCTL:\t\t0x%08x\n",
  1238. OMAP_HSMMC_READ(host->base, HCTL));
  1239. seq_printf(s, "SYSCTL:\t\t0x%08x\n",
  1240. OMAP_HSMMC_READ(host->base, SYSCTL));
  1241. seq_printf(s, "IE:\t\t0x%08x\n",
  1242. OMAP_HSMMC_READ(host->base, IE));
  1243. seq_printf(s, "ISE:\t\t0x%08x\n",
  1244. OMAP_HSMMC_READ(host->base, ISE));
  1245. seq_printf(s, "CAPA:\t\t0x%08x\n",
  1246. OMAP_HSMMC_READ(host->base, CAPA));
  1247. clk_disable(host->fclk);
  1248. return 0;
  1249. }
  1250. static int mmc_regs_open(struct inode *inode, struct file *file)
  1251. {
  1252. return single_open(file, mmc_regs_show, inode->i_private);
  1253. }
  1254. static const struct file_operations mmc_regs_fops = {
  1255. .open = mmc_regs_open,
  1256. .read = seq_read,
  1257. .llseek = seq_lseek,
  1258. .release = single_release,
  1259. };
  1260. static void omap_mmc_debugfs(struct mmc_host *mmc)
  1261. {
  1262. if (mmc->debugfs_root)
  1263. debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
  1264. mmc, &mmc_regs_fops);
  1265. }
  1266. #else
  1267. static void omap_mmc_debugfs(struct mmc_host *mmc)
  1268. {
  1269. }
  1270. #endif
  1271. static int __init omap_mmc_probe(struct platform_device *pdev)
  1272. {
  1273. struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
  1274. struct mmc_host *mmc;
  1275. struct mmc_omap_host *host = NULL;
  1276. struct resource *res;
  1277. int ret = 0, irq;
  1278. if (pdata == NULL) {
  1279. dev_err(&pdev->dev, "Platform Data is missing\n");
  1280. return -ENXIO;
  1281. }
  1282. if (pdata->nr_slots == 0) {
  1283. dev_err(&pdev->dev, "No Slots\n");
  1284. return -ENXIO;
  1285. }
  1286. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1287. irq = platform_get_irq(pdev, 0);
  1288. if (res == NULL || irq < 0)
  1289. return -ENXIO;
  1290. res = request_mem_region(res->start, res->end - res->start + 1,
  1291. pdev->name);
  1292. if (res == NULL)
  1293. return -EBUSY;
  1294. mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
  1295. if (!mmc) {
  1296. ret = -ENOMEM;
  1297. goto err;
  1298. }
  1299. host = mmc_priv(mmc);
  1300. host->mmc = mmc;
  1301. host->pdata = pdata;
  1302. host->dev = &pdev->dev;
  1303. host->use_dma = 1;
  1304. host->dev->dma_mask = &pdata->dma_mask;
  1305. host->dma_ch = -1;
  1306. host->irq = irq;
  1307. host->id = pdev->id;
  1308. host->slot_id = 0;
  1309. host->mapbase = res->start;
  1310. host->base = ioremap(host->mapbase, SZ_4K);
  1311. host->power_mode = -1;
  1312. platform_set_drvdata(pdev, host);
  1313. INIT_WORK(&host->mmc_carddetect_work, mmc_omap_detect);
  1314. if (pdata->slots[host->slot_id].power_saving)
  1315. mmc->ops = &mmc_omap_ps_ops;
  1316. else
  1317. mmc->ops = &mmc_omap_ops;
  1318. mmc->f_min = 400000;
  1319. mmc->f_max = 52000000;
  1320. sema_init(&host->sem, 1);
  1321. host->iclk = clk_get(&pdev->dev, "ick");
  1322. if (IS_ERR(host->iclk)) {
  1323. ret = PTR_ERR(host->iclk);
  1324. host->iclk = NULL;
  1325. goto err1;
  1326. }
  1327. host->fclk = clk_get(&pdev->dev, "fck");
  1328. if (IS_ERR(host->fclk)) {
  1329. ret = PTR_ERR(host->fclk);
  1330. host->fclk = NULL;
  1331. clk_put(host->iclk);
  1332. goto err1;
  1333. }
  1334. omap_mmc_save_ctx(host);
  1335. mmc->caps |= MMC_CAP_DISABLE;
  1336. mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
  1337. /* we start off in DISABLED state */
  1338. host->dpm_state = DISABLED;
  1339. if (mmc_host_enable(host->mmc) != 0) {
  1340. clk_put(host->iclk);
  1341. clk_put(host->fclk);
  1342. goto err1;
  1343. }
  1344. if (clk_enable(host->iclk) != 0) {
  1345. mmc_host_disable(host->mmc);
  1346. clk_put(host->iclk);
  1347. clk_put(host->fclk);
  1348. goto err1;
  1349. }
  1350. host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
  1351. /*
  1352. * MMC can still work without debounce clock.
  1353. */
  1354. if (IS_ERR(host->dbclk))
  1355. dev_warn(mmc_dev(host->mmc), "Failed to get debounce clock\n");
  1356. else
  1357. if (clk_enable(host->dbclk) != 0)
  1358. dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
  1359. " clk failed\n");
  1360. else
  1361. host->dbclk_enabled = 1;
  1362. /* Since we do only SG emulation, we can have as many segs
  1363. * as we want. */
  1364. mmc->max_phys_segs = 1024;
  1365. mmc->max_hw_segs = 1024;
  1366. mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
  1367. mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
  1368. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1369. mmc->max_seg_size = mmc->max_req_size;
  1370. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
  1371. if (pdata->slots[host->slot_id].wires >= 8)
  1372. mmc->caps |= MMC_CAP_8_BIT_DATA;
  1373. else if (pdata->slots[host->slot_id].wires >= 4)
  1374. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1375. if (pdata->slots[host->slot_id].nonremovable)
  1376. mmc->caps |= MMC_CAP_NONREMOVABLE;
  1377. omap_hsmmc_init(host);
  1378. /* Select DMA lines */
  1379. switch (host->id) {
  1380. case OMAP_MMC1_DEVID:
  1381. host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
  1382. host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
  1383. break;
  1384. case OMAP_MMC2_DEVID:
  1385. host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
  1386. host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
  1387. break;
  1388. case OMAP_MMC3_DEVID:
  1389. host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
  1390. host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
  1391. break;
  1392. default:
  1393. dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
  1394. goto err_irq;
  1395. }
  1396. /* Request IRQ for MMC operations */
  1397. ret = request_irq(host->irq, mmc_omap_irq, IRQF_DISABLED,
  1398. mmc_hostname(mmc), host);
  1399. if (ret) {
  1400. dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
  1401. goto err_irq;
  1402. }
  1403. /* initialize power supplies, gpios, etc */
  1404. if (pdata->init != NULL) {
  1405. if (pdata->init(&pdev->dev) != 0) {
  1406. dev_dbg(mmc_dev(host->mmc), "late init error\n");
  1407. goto err_irq_cd_init;
  1408. }
  1409. }
  1410. mmc->ocr_avail = mmc_slot(host).ocr_mask;
  1411. /* Request IRQ for card detect */
  1412. if ((mmc_slot(host).card_detect_irq)) {
  1413. ret = request_irq(mmc_slot(host).card_detect_irq,
  1414. omap_mmc_cd_handler,
  1415. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
  1416. | IRQF_DISABLED,
  1417. mmc_hostname(mmc), host);
  1418. if (ret) {
  1419. dev_dbg(mmc_dev(host->mmc),
  1420. "Unable to grab MMC CD IRQ\n");
  1421. goto err_irq_cd;
  1422. }
  1423. }
  1424. OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
  1425. OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
  1426. mmc_host_lazy_disable(host->mmc);
  1427. mmc_add_host(mmc);
  1428. if (host->pdata->slots[host->slot_id].name != NULL) {
  1429. ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
  1430. if (ret < 0)
  1431. goto err_slot_name;
  1432. }
  1433. if (mmc_slot(host).card_detect_irq &&
  1434. host->pdata->slots[host->slot_id].get_cover_state) {
  1435. ret = device_create_file(&mmc->class_dev,
  1436. &dev_attr_cover_switch);
  1437. if (ret < 0)
  1438. goto err_cover_switch;
  1439. }
  1440. omap_mmc_debugfs(mmc);
  1441. return 0;
  1442. err_cover_switch:
  1443. device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
  1444. err_slot_name:
  1445. mmc_remove_host(mmc);
  1446. err_irq_cd:
  1447. free_irq(mmc_slot(host).card_detect_irq, host);
  1448. err_irq_cd_init:
  1449. free_irq(host->irq, host);
  1450. err_irq:
  1451. mmc_host_disable(host->mmc);
  1452. clk_disable(host->iclk);
  1453. clk_put(host->fclk);
  1454. clk_put(host->iclk);
  1455. if (host->dbclk_enabled) {
  1456. clk_disable(host->dbclk);
  1457. clk_put(host->dbclk);
  1458. }
  1459. err1:
  1460. iounmap(host->base);
  1461. err:
  1462. dev_dbg(mmc_dev(host->mmc), "Probe Failed\n");
  1463. release_mem_region(res->start, res->end - res->start + 1);
  1464. if (host)
  1465. mmc_free_host(mmc);
  1466. return ret;
  1467. }
  1468. static int omap_mmc_remove(struct platform_device *pdev)
  1469. {
  1470. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1471. struct resource *res;
  1472. if (host) {
  1473. mmc_host_enable(host->mmc);
  1474. mmc_remove_host(host->mmc);
  1475. if (host->pdata->cleanup)
  1476. host->pdata->cleanup(&pdev->dev);
  1477. free_irq(host->irq, host);
  1478. if (mmc_slot(host).card_detect_irq)
  1479. free_irq(mmc_slot(host).card_detect_irq, host);
  1480. flush_scheduled_work();
  1481. mmc_host_disable(host->mmc);
  1482. clk_disable(host->iclk);
  1483. clk_put(host->fclk);
  1484. clk_put(host->iclk);
  1485. if (host->dbclk_enabled) {
  1486. clk_disable(host->dbclk);
  1487. clk_put(host->dbclk);
  1488. }
  1489. mmc_free_host(host->mmc);
  1490. iounmap(host->base);
  1491. }
  1492. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1493. if (res)
  1494. release_mem_region(res->start, res->end - res->start + 1);
  1495. platform_set_drvdata(pdev, NULL);
  1496. return 0;
  1497. }
  1498. #ifdef CONFIG_PM
  1499. static int omap_mmc_suspend(struct platform_device *pdev, pm_message_t state)
  1500. {
  1501. int ret = 0;
  1502. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1503. if (host && host->suspended)
  1504. return 0;
  1505. if (host) {
  1506. host->suspended = 1;
  1507. if (host->pdata->suspend) {
  1508. ret = host->pdata->suspend(&pdev->dev,
  1509. host->slot_id);
  1510. if (ret) {
  1511. dev_dbg(mmc_dev(host->mmc),
  1512. "Unable to handle MMC board"
  1513. " level suspend\n");
  1514. host->suspended = 0;
  1515. return ret;
  1516. }
  1517. }
  1518. cancel_work_sync(&host->mmc_carddetect_work);
  1519. mmc_host_enable(host->mmc);
  1520. ret = mmc_suspend_host(host->mmc, state);
  1521. if (ret == 0) {
  1522. OMAP_HSMMC_WRITE(host->base, ISE, 0);
  1523. OMAP_HSMMC_WRITE(host->base, IE, 0);
  1524. OMAP_HSMMC_WRITE(host->base, HCTL,
  1525. OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
  1526. mmc_host_disable(host->mmc);
  1527. clk_disable(host->iclk);
  1528. clk_disable(host->dbclk);
  1529. } else {
  1530. host->suspended = 0;
  1531. if (host->pdata->resume) {
  1532. ret = host->pdata->resume(&pdev->dev,
  1533. host->slot_id);
  1534. if (ret)
  1535. dev_dbg(mmc_dev(host->mmc),
  1536. "Unmask interrupt failed\n");
  1537. }
  1538. mmc_host_disable(host->mmc);
  1539. }
  1540. }
  1541. return ret;
  1542. }
  1543. /* Routine to resume the MMC device */
  1544. static int omap_mmc_resume(struct platform_device *pdev)
  1545. {
  1546. int ret = 0;
  1547. struct mmc_omap_host *host = platform_get_drvdata(pdev);
  1548. if (host && !host->suspended)
  1549. return 0;
  1550. if (host) {
  1551. ret = clk_enable(host->iclk);
  1552. if (ret)
  1553. goto clk_en_err;
  1554. if (clk_enable(host->dbclk) != 0)
  1555. dev_dbg(mmc_dev(host->mmc),
  1556. "Enabling debounce clk failed\n");
  1557. if (mmc_host_enable(host->mmc) != 0) {
  1558. clk_disable(host->iclk);
  1559. goto clk_en_err;
  1560. }
  1561. omap_hsmmc_init(host);
  1562. if (host->pdata->resume) {
  1563. ret = host->pdata->resume(&pdev->dev, host->slot_id);
  1564. if (ret)
  1565. dev_dbg(mmc_dev(host->mmc),
  1566. "Unmask interrupt failed\n");
  1567. }
  1568. /* Notify the core to resume the host */
  1569. ret = mmc_resume_host(host->mmc);
  1570. if (ret == 0)
  1571. host->suspended = 0;
  1572. mmc_host_lazy_disable(host->mmc);
  1573. }
  1574. return ret;
  1575. clk_en_err:
  1576. dev_dbg(mmc_dev(host->mmc),
  1577. "Failed to enable MMC clocks during resume\n");
  1578. return ret;
  1579. }
  1580. #else
  1581. #define omap_mmc_suspend NULL
  1582. #define omap_mmc_resume NULL
  1583. #endif
  1584. static struct platform_driver omap_mmc_driver = {
  1585. .remove = omap_mmc_remove,
  1586. .suspend = omap_mmc_suspend,
  1587. .resume = omap_mmc_resume,
  1588. .driver = {
  1589. .name = DRIVER_NAME,
  1590. .owner = THIS_MODULE,
  1591. },
  1592. };
  1593. static int __init omap_mmc_init(void)
  1594. {
  1595. /* Register the MMC driver */
  1596. return platform_driver_probe(&omap_mmc_driver, omap_mmc_probe);
  1597. }
  1598. static void __exit omap_mmc_cleanup(void)
  1599. {
  1600. /* Unregister MMC driver */
  1601. platform_driver_unregister(&omap_mmc_driver);
  1602. }
  1603. module_init(omap_mmc_init);
  1604. module_exit(omap_mmc_cleanup);
  1605. MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
  1606. MODULE_LICENSE("GPL");
  1607. MODULE_ALIAS("platform:" DRIVER_NAME);
  1608. MODULE_AUTHOR("Texas Instruments Inc");