omap_hsmmc.c 55 KB

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