omap_hsmmc.c 56 KB

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