omap_hsmmc.c 40 KB

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