omap_hsmmc.c 49 KB

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