omap_hsmmc.c 49 KB

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