omap_hsmmc.c 47 KB

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