via-sdmmc.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /*
  2. * drivers/mmc/host/via-sdmmc.c - VIA SD/MMC Card Reader driver
  3. * Copyright (c) 2008, VIA Technologies Inc. All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. */
  10. #include <linux/pci.h>
  11. #include <linux/module.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/highmem.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/mmc/host.h>
  17. #define DRV_NAME "via_sdmmc"
  18. #define PCI_DEVICE_ID_VIA_9530 0x9530
  19. #define VIA_CRDR_SDC_OFF 0x200
  20. #define VIA_CRDR_DDMA_OFF 0x400
  21. #define VIA_CRDR_PCICTRL_OFF 0x600
  22. #define VIA_CRDR_MIN_CLOCK 375000
  23. #define VIA_CRDR_MAX_CLOCK 48000000
  24. /*
  25. * PCI registers
  26. */
  27. #define VIA_CRDR_PCI_WORK_MODE 0x40
  28. #define VIA_CRDR_PCI_DBG_MODE 0x41
  29. /*
  30. * SDC MMIO Registers
  31. */
  32. #define VIA_CRDR_SDCTRL 0x0
  33. #define VIA_CRDR_SDCTRL_START 0x01
  34. #define VIA_CRDR_SDCTRL_WRITE 0x04
  35. #define VIA_CRDR_SDCTRL_SINGLE_WR 0x10
  36. #define VIA_CRDR_SDCTRL_SINGLE_RD 0x20
  37. #define VIA_CRDR_SDCTRL_MULTI_WR 0x30
  38. #define VIA_CRDR_SDCTRL_MULTI_RD 0x40
  39. #define VIA_CRDR_SDCTRL_STOP 0x70
  40. #define VIA_CRDR_SDCTRL_RSP_NONE 0x0
  41. #define VIA_CRDR_SDCTRL_RSP_R1 0x10000
  42. #define VIA_CRDR_SDCTRL_RSP_R2 0x20000
  43. #define VIA_CRDR_SDCTRL_RSP_R3 0x30000
  44. #define VIA_CRDR_SDCTRL_RSP_R1B 0x90000
  45. #define VIA_CRDR_SDCARG 0x4
  46. #define VIA_CRDR_SDBUSMODE 0x8
  47. #define VIA_CRDR_SDMODE_4BIT 0x02
  48. #define VIA_CRDR_SDMODE_CLK_ON 0x40
  49. #define VIA_CRDR_SDBLKLEN 0xc
  50. /*
  51. * Bit 0 -Bit 10 : Block length. So, the maximum block length should be 2048.
  52. * Bit 11 - Bit 13 : Reserved.
  53. * GPIDET : Select GPI pin to detect card, GPI means CR_CD# in top design.
  54. * INTEN : Enable SD host interrupt.
  55. * Bit 16 - Bit 31 : Block count. So, the maximun block count should be 65536.
  56. */
  57. #define VIA_CRDR_SDBLKLEN_GPIDET 0x2000
  58. #define VIA_CRDR_SDBLKLEN_INTEN 0x8000
  59. #define VIA_CRDR_MAX_BLOCK_COUNT 65536
  60. #define VIA_CRDR_MAX_BLOCK_LENGTH 2048
  61. #define VIA_CRDR_SDRESP0 0x10
  62. #define VIA_CRDR_SDRESP1 0x14
  63. #define VIA_CRDR_SDRESP2 0x18
  64. #define VIA_CRDR_SDRESP3 0x1c
  65. #define VIA_CRDR_SDCURBLKCNT 0x20
  66. #define VIA_CRDR_SDINTMASK 0x24
  67. /*
  68. * MBDIE : Multiple Blocks transfer Done Interrupt Enable
  69. * BDDIE : Block Data transfer Done Interrupt Enable
  70. * CIRIE : Card Insertion or Removal Interrupt Enable
  71. * CRDIE : Command-Response transfer Done Interrupt Enable
  72. * CRTOIE : Command-Response response TimeOut Interrupt Enable
  73. * ASCRDIE : Auto Stop Command-Response transfer Done Interrupt Enable
  74. * DTIE : Data access Timeout Interrupt Enable
  75. * SCIE : reSponse CRC error Interrupt Enable
  76. * RCIE : Read data CRC error Interrupt Enable
  77. * WCIE : Write data CRC error Interrupt Enable
  78. */
  79. #define VIA_CRDR_SDINTMASK_MBDIE 0x10
  80. #define VIA_CRDR_SDINTMASK_BDDIE 0x20
  81. #define VIA_CRDR_SDINTMASK_CIRIE 0x80
  82. #define VIA_CRDR_SDINTMASK_CRDIE 0x200
  83. #define VIA_CRDR_SDINTMASK_CRTOIE 0x400
  84. #define VIA_CRDR_SDINTMASK_ASCRDIE 0x800
  85. #define VIA_CRDR_SDINTMASK_DTIE 0x1000
  86. #define VIA_CRDR_SDINTMASK_SCIE 0x2000
  87. #define VIA_CRDR_SDINTMASK_RCIE 0x4000
  88. #define VIA_CRDR_SDINTMASK_WCIE 0x8000
  89. #define VIA_CRDR_SDACTIVE_INTMASK \
  90. (VIA_CRDR_SDINTMASK_MBDIE | VIA_CRDR_SDINTMASK_CIRIE \
  91. | VIA_CRDR_SDINTMASK_CRDIE | VIA_CRDR_SDINTMASK_CRTOIE \
  92. | VIA_CRDR_SDINTMASK_DTIE | VIA_CRDR_SDINTMASK_SCIE \
  93. | VIA_CRDR_SDINTMASK_RCIE | VIA_CRDR_SDINTMASK_WCIE)
  94. #define VIA_CRDR_SDSTATUS 0x28
  95. /*
  96. * CECC : Reserved
  97. * WP : SD card Write Protect status
  98. * SLOTD : Reserved
  99. * SLOTG : SD SLOT status(Gpi pin status)
  100. * MBD : Multiple Blocks transfer Done interrupt status
  101. * BDD : Block Data transfer Done interrupt status
  102. * CD : Reserved
  103. * CIR : Card Insertion or Removal interrupt detected on GPI pin
  104. * IO : Reserved
  105. * CRD : Command-Response transfer Done interrupt status
  106. * CRTO : Command-Response response TimeOut interrupt status
  107. * ASCRDIE : Auto Stop Command-Response transfer Done interrupt status
  108. * DT : Data access Timeout interrupt status
  109. * SC : reSponse CRC error interrupt status
  110. * RC : Read data CRC error interrupt status
  111. * WC : Write data CRC error interrupt status
  112. */
  113. #define VIA_CRDR_SDSTS_CECC 0x01
  114. #define VIA_CRDR_SDSTS_WP 0x02
  115. #define VIA_CRDR_SDSTS_SLOTD 0x04
  116. #define VIA_CRDR_SDSTS_SLOTG 0x08
  117. #define VIA_CRDR_SDSTS_MBD 0x10
  118. #define VIA_CRDR_SDSTS_BDD 0x20
  119. #define VIA_CRDR_SDSTS_CD 0x40
  120. #define VIA_CRDR_SDSTS_CIR 0x80
  121. #define VIA_CRDR_SDSTS_IO 0x100
  122. #define VIA_CRDR_SDSTS_CRD 0x200
  123. #define VIA_CRDR_SDSTS_CRTO 0x400
  124. #define VIA_CRDR_SDSTS_ASCRDIE 0x800
  125. #define VIA_CRDR_SDSTS_DT 0x1000
  126. #define VIA_CRDR_SDSTS_SC 0x2000
  127. #define VIA_CRDR_SDSTS_RC 0x4000
  128. #define VIA_CRDR_SDSTS_WC 0x8000
  129. #define VIA_CRDR_SDSTS_IGN_MASK\
  130. (VIA_CRDR_SDSTS_BDD | VIA_CRDR_SDSTS_ASCRDIE | VIA_CRDR_SDSTS_IO)
  131. #define VIA_CRDR_SDSTS_INT_MASK \
  132. (VIA_CRDR_SDSTS_MBD | VIA_CRDR_SDSTS_BDD | VIA_CRDR_SDSTS_CD \
  133. | VIA_CRDR_SDSTS_CIR | VIA_CRDR_SDSTS_IO | VIA_CRDR_SDSTS_CRD \
  134. | VIA_CRDR_SDSTS_CRTO | VIA_CRDR_SDSTS_ASCRDIE | VIA_CRDR_SDSTS_DT \
  135. | VIA_CRDR_SDSTS_SC | VIA_CRDR_SDSTS_RC | VIA_CRDR_SDSTS_WC)
  136. #define VIA_CRDR_SDSTS_W1C_MASK \
  137. (VIA_CRDR_SDSTS_CECC | VIA_CRDR_SDSTS_MBD | VIA_CRDR_SDSTS_BDD \
  138. | VIA_CRDR_SDSTS_CD | VIA_CRDR_SDSTS_CIR | VIA_CRDR_SDSTS_CRD \
  139. | VIA_CRDR_SDSTS_CRTO | VIA_CRDR_SDSTS_ASCRDIE | VIA_CRDR_SDSTS_DT \
  140. | VIA_CRDR_SDSTS_SC | VIA_CRDR_SDSTS_RC | VIA_CRDR_SDSTS_WC)
  141. #define VIA_CRDR_SDSTS_CMD_MASK \
  142. (VIA_CRDR_SDSTS_CRD | VIA_CRDR_SDSTS_CRTO | VIA_CRDR_SDSTS_SC)
  143. #define VIA_CRDR_SDSTS_DATA_MASK\
  144. (VIA_CRDR_SDSTS_MBD | VIA_CRDR_SDSTS_DT \
  145. | VIA_CRDR_SDSTS_RC | VIA_CRDR_SDSTS_WC)
  146. #define VIA_CRDR_SDSTATUS2 0x2a
  147. /*
  148. * CFE : Enable SD host automatic Clock FReezing
  149. */
  150. #define VIA_CRDR_SDSTS_CFE 0x80
  151. #define VIA_CRDR_SDRSPTMO 0x2C
  152. #define VIA_CRDR_SDCLKSEL 0x30
  153. #define VIA_CRDR_SDEXTCTRL 0x34
  154. #define VIS_CRDR_SDEXTCTRL_AUTOSTOP_SD 0x01
  155. #define VIS_CRDR_SDEXTCTRL_SHIFT_9 0x02
  156. #define VIS_CRDR_SDEXTCTRL_MMC_8BIT 0x04
  157. #define VIS_CRDR_SDEXTCTRL_RELD_BLK 0x08
  158. #define VIS_CRDR_SDEXTCTRL_BAD_CMDA 0x10
  159. #define VIS_CRDR_SDEXTCTRL_BAD_DATA 0x20
  160. #define VIS_CRDR_SDEXTCTRL_AUTOSTOP_SPI 0x40
  161. #define VIA_CRDR_SDEXTCTRL_HISPD 0x80
  162. /* 0x38-0xFF reserved */
  163. /*
  164. * Data DMA Control Registers
  165. */
  166. #define VIA_CRDR_DMABASEADD 0x0
  167. #define VIA_CRDR_DMACOUNTER 0x4
  168. #define VIA_CRDR_DMACTRL 0x8
  169. /*
  170. * DIR :Transaction Direction
  171. * 0 : From card to memory
  172. * 1 : From memory to card
  173. */
  174. #define VIA_CRDR_DMACTRL_DIR 0x100
  175. #define VIA_CRDR_DMACTRL_ENIRQ 0x10000
  176. #define VIA_CRDR_DMACTRL_SFTRST 0x1000000
  177. #define VIA_CRDR_DMASTS 0xc
  178. #define VIA_CRDR_DMASTART 0x10
  179. /*0x14-0xFF reserved*/
  180. /*
  181. * PCI Control Registers
  182. */
  183. /*0x0 - 0x1 reserved*/
  184. #define VIA_CRDR_PCICLKGATT 0x2
  185. /*
  186. * SFTRST :
  187. * 0 : Soft reset all the controller and it will be de-asserted automatically
  188. * 1 : Soft reset is de-asserted
  189. */
  190. #define VIA_CRDR_PCICLKGATT_SFTRST 0x01
  191. /*
  192. * 3V3 : Pad power select
  193. * 0 : 1.8V
  194. * 1 : 3.3V
  195. * NOTE : No mater what the actual value should be, this bit always
  196. * read as 0. This is a hardware bug.
  197. */
  198. #define VIA_CRDR_PCICLKGATT_3V3 0x10
  199. /*
  200. * PAD_PWRON : Pad Power on/off select
  201. * 0 : Power off
  202. * 1 : Power on
  203. * NOTE : No mater what the actual value should be, this bit always
  204. * read as 0. This is a hardware bug.
  205. */
  206. #define VIA_CRDR_PCICLKGATT_PAD_PWRON 0x20
  207. #define VIA_CRDR_PCISDCCLK 0x5
  208. #define VIA_CRDR_PCIDMACLK 0x7
  209. #define VIA_CRDR_PCIDMACLK_SDC 0x2
  210. #define VIA_CRDR_PCIINTCTRL 0x8
  211. #define VIA_CRDR_PCIINTCTRL_SDCIRQEN 0x04
  212. #define VIA_CRDR_PCIINTSTATUS 0x9
  213. #define VIA_CRDR_PCIINTSTATUS_SDC 0x04
  214. #define VIA_CRDR_PCITMOCTRL 0xa
  215. #define VIA_CRDR_PCITMOCTRL_NO 0x0
  216. #define VIA_CRDR_PCITMOCTRL_32US 0x1
  217. #define VIA_CRDR_PCITMOCTRL_256US 0x2
  218. #define VIA_CRDR_PCITMOCTRL_1024US 0x3
  219. #define VIA_CRDR_PCITMOCTRL_256MS 0x4
  220. #define VIA_CRDR_PCITMOCTRL_512MS 0x5
  221. #define VIA_CRDR_PCITMOCTRL_1024MS 0x6
  222. /*0xB-0xFF reserved*/
  223. enum PCI_HOST_CLK_CONTROL {
  224. PCI_CLK_375K = 0x03,
  225. PCI_CLK_8M = 0x04,
  226. PCI_CLK_12M = 0x00,
  227. PCI_CLK_16M = 0x05,
  228. PCI_CLK_24M = 0x01,
  229. PCI_CLK_33M = 0x06,
  230. PCI_CLK_48M = 0x02
  231. };
  232. struct sdhcreg {
  233. u32 sdcontrol_reg;
  234. u32 sdcmdarg_reg;
  235. u32 sdbusmode_reg;
  236. u32 sdblklen_reg;
  237. u32 sdresp_reg[4];
  238. u32 sdcurblkcnt_reg;
  239. u32 sdintmask_reg;
  240. u32 sdstatus_reg;
  241. u32 sdrsptmo_reg;
  242. u32 sdclksel_reg;
  243. u32 sdextctrl_reg;
  244. };
  245. struct pcictrlreg {
  246. u8 reserve[2];
  247. u8 pciclkgat_reg;
  248. u8 pcinfcclk_reg;
  249. u8 pcimscclk_reg;
  250. u8 pcisdclk_reg;
  251. u8 pcicaclk_reg;
  252. u8 pcidmaclk_reg;
  253. u8 pciintctrl_reg;
  254. u8 pciintstatus_reg;
  255. u8 pcitmoctrl_reg;
  256. u8 Resv;
  257. };
  258. struct via_crdr_mmc_host {
  259. struct mmc_host *mmc;
  260. struct mmc_request *mrq;
  261. struct mmc_command *cmd;
  262. struct mmc_data *data;
  263. void __iomem *mmiobase;
  264. void __iomem *sdhc_mmiobase;
  265. void __iomem *ddma_mmiobase;
  266. void __iomem *pcictrl_mmiobase;
  267. struct pcictrlreg pm_pcictrl_reg;
  268. struct sdhcreg pm_sdhc_reg;
  269. struct work_struct carddet_work;
  270. struct tasklet_struct finish_tasklet;
  271. struct timer_list timer;
  272. spinlock_t lock;
  273. u8 power;
  274. int reject;
  275. unsigned int quirks;
  276. };
  277. /* some devices need a very long delay for power to stabilize */
  278. #define VIA_CRDR_QUIRK_300MS_PWRDELAY 0x0001
  279. #define VIA_CMD_TIMEOUT_MS 1000
  280. static const struct pci_device_id via_ids[] = {
  281. {PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_9530,
  282. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0,},
  283. {0,}
  284. };
  285. MODULE_DEVICE_TABLE(pci, via_ids);
  286. static void via_print_sdchc(struct via_crdr_mmc_host *host)
  287. {
  288. void __iomem *addrbase = host->sdhc_mmiobase;
  289. pr_debug("SDC MMIO Registers:\n");
  290. pr_debug("SDCONTROL=%08x, SDCMDARG=%08x, SDBUSMODE=%08x\n",
  291. readl(addrbase + VIA_CRDR_SDCTRL),
  292. readl(addrbase + VIA_CRDR_SDCARG),
  293. readl(addrbase + VIA_CRDR_SDBUSMODE));
  294. pr_debug("SDBLKLEN=%08x, SDCURBLKCNT=%08x, SDINTMASK=%08x\n",
  295. readl(addrbase + VIA_CRDR_SDBLKLEN),
  296. readl(addrbase + VIA_CRDR_SDCURBLKCNT),
  297. readl(addrbase + VIA_CRDR_SDINTMASK));
  298. pr_debug("SDSTATUS=%08x, SDCLKSEL=%08x, SDEXTCTRL=%08x\n",
  299. readl(addrbase + VIA_CRDR_SDSTATUS),
  300. readl(addrbase + VIA_CRDR_SDCLKSEL),
  301. readl(addrbase + VIA_CRDR_SDEXTCTRL));
  302. }
  303. static void via_print_pcictrl(struct via_crdr_mmc_host *host)
  304. {
  305. void __iomem *addrbase = host->pcictrl_mmiobase;
  306. pr_debug("PCI Control Registers:\n");
  307. pr_debug("PCICLKGATT=%02x, PCISDCCLK=%02x, PCIDMACLK=%02x\n",
  308. readb(addrbase + VIA_CRDR_PCICLKGATT),
  309. readb(addrbase + VIA_CRDR_PCISDCCLK),
  310. readb(addrbase + VIA_CRDR_PCIDMACLK));
  311. pr_debug("PCIINTCTRL=%02x, PCIINTSTATUS=%02x\n",
  312. readb(addrbase + VIA_CRDR_PCIINTCTRL),
  313. readb(addrbase + VIA_CRDR_PCIINTSTATUS));
  314. }
  315. static void via_save_pcictrlreg(struct via_crdr_mmc_host *host)
  316. {
  317. struct pcictrlreg *pm_pcictrl_reg;
  318. void __iomem *addrbase;
  319. pm_pcictrl_reg = &(host->pm_pcictrl_reg);
  320. addrbase = host->pcictrl_mmiobase;
  321. pm_pcictrl_reg->pciclkgat_reg = readb(addrbase + VIA_CRDR_PCICLKGATT);
  322. pm_pcictrl_reg->pciclkgat_reg |=
  323. VIA_CRDR_PCICLKGATT_3V3 | VIA_CRDR_PCICLKGATT_PAD_PWRON;
  324. pm_pcictrl_reg->pcisdclk_reg = readb(addrbase + VIA_CRDR_PCISDCCLK);
  325. pm_pcictrl_reg->pcidmaclk_reg = readb(addrbase + VIA_CRDR_PCIDMACLK);
  326. pm_pcictrl_reg->pciintctrl_reg = readb(addrbase + VIA_CRDR_PCIINTCTRL);
  327. pm_pcictrl_reg->pciintstatus_reg =
  328. readb(addrbase + VIA_CRDR_PCIINTSTATUS);
  329. pm_pcictrl_reg->pcitmoctrl_reg = readb(addrbase + VIA_CRDR_PCITMOCTRL);
  330. }
  331. static void via_restore_pcictrlreg(struct via_crdr_mmc_host *host)
  332. {
  333. struct pcictrlreg *pm_pcictrl_reg;
  334. void __iomem *addrbase;
  335. pm_pcictrl_reg = &(host->pm_pcictrl_reg);
  336. addrbase = host->pcictrl_mmiobase;
  337. writeb(pm_pcictrl_reg->pciclkgat_reg, addrbase + VIA_CRDR_PCICLKGATT);
  338. writeb(pm_pcictrl_reg->pcisdclk_reg, addrbase + VIA_CRDR_PCISDCCLK);
  339. writeb(pm_pcictrl_reg->pcidmaclk_reg, addrbase + VIA_CRDR_PCIDMACLK);
  340. writeb(pm_pcictrl_reg->pciintctrl_reg, addrbase + VIA_CRDR_PCIINTCTRL);
  341. writeb(pm_pcictrl_reg->pciintstatus_reg,
  342. addrbase + VIA_CRDR_PCIINTSTATUS);
  343. writeb(pm_pcictrl_reg->pcitmoctrl_reg, addrbase + VIA_CRDR_PCITMOCTRL);
  344. }
  345. static void via_save_sdcreg(struct via_crdr_mmc_host *host)
  346. {
  347. struct sdhcreg *pm_sdhc_reg;
  348. void __iomem *addrbase;
  349. pm_sdhc_reg = &(host->pm_sdhc_reg);
  350. addrbase = host->sdhc_mmiobase;
  351. pm_sdhc_reg->sdcontrol_reg = readl(addrbase + VIA_CRDR_SDCTRL);
  352. pm_sdhc_reg->sdcmdarg_reg = readl(addrbase + VIA_CRDR_SDCARG);
  353. pm_sdhc_reg->sdbusmode_reg = readl(addrbase + VIA_CRDR_SDBUSMODE);
  354. pm_sdhc_reg->sdblklen_reg = readl(addrbase + VIA_CRDR_SDBLKLEN);
  355. pm_sdhc_reg->sdcurblkcnt_reg = readl(addrbase + VIA_CRDR_SDCURBLKCNT);
  356. pm_sdhc_reg->sdintmask_reg = readl(addrbase + VIA_CRDR_SDINTMASK);
  357. pm_sdhc_reg->sdstatus_reg = readl(addrbase + VIA_CRDR_SDSTATUS);
  358. pm_sdhc_reg->sdrsptmo_reg = readl(addrbase + VIA_CRDR_SDRSPTMO);
  359. pm_sdhc_reg->sdclksel_reg = readl(addrbase + VIA_CRDR_SDCLKSEL);
  360. pm_sdhc_reg->sdextctrl_reg = readl(addrbase + VIA_CRDR_SDEXTCTRL);
  361. }
  362. static void via_restore_sdcreg(struct via_crdr_mmc_host *host)
  363. {
  364. struct sdhcreg *pm_sdhc_reg;
  365. void __iomem *addrbase;
  366. pm_sdhc_reg = &(host->pm_sdhc_reg);
  367. addrbase = host->sdhc_mmiobase;
  368. writel(pm_sdhc_reg->sdcontrol_reg, addrbase + VIA_CRDR_SDCTRL);
  369. writel(pm_sdhc_reg->sdcmdarg_reg, addrbase + VIA_CRDR_SDCARG);
  370. writel(pm_sdhc_reg->sdbusmode_reg, addrbase + VIA_CRDR_SDBUSMODE);
  371. writel(pm_sdhc_reg->sdblklen_reg, addrbase + VIA_CRDR_SDBLKLEN);
  372. writel(pm_sdhc_reg->sdcurblkcnt_reg, addrbase + VIA_CRDR_SDCURBLKCNT);
  373. writel(pm_sdhc_reg->sdintmask_reg, addrbase + VIA_CRDR_SDINTMASK);
  374. writel(pm_sdhc_reg->sdstatus_reg, addrbase + VIA_CRDR_SDSTATUS);
  375. writel(pm_sdhc_reg->sdrsptmo_reg, addrbase + VIA_CRDR_SDRSPTMO);
  376. writel(pm_sdhc_reg->sdclksel_reg, addrbase + VIA_CRDR_SDCLKSEL);
  377. writel(pm_sdhc_reg->sdextctrl_reg, addrbase + VIA_CRDR_SDEXTCTRL);
  378. }
  379. static void via_pwron_sleep(struct via_crdr_mmc_host *sdhost)
  380. {
  381. if (sdhost->quirks & VIA_CRDR_QUIRK_300MS_PWRDELAY)
  382. msleep(300);
  383. else
  384. msleep(3);
  385. }
  386. static void via_set_ddma(struct via_crdr_mmc_host *host,
  387. dma_addr_t dmaaddr, u32 count, int dir, int enirq)
  388. {
  389. void __iomem *addrbase;
  390. u32 ctrl_data = 0;
  391. if (enirq)
  392. ctrl_data |= VIA_CRDR_DMACTRL_ENIRQ;
  393. if (dir)
  394. ctrl_data |= VIA_CRDR_DMACTRL_DIR;
  395. addrbase = host->ddma_mmiobase;
  396. writel(dmaaddr, addrbase + VIA_CRDR_DMABASEADD);
  397. writel(count, addrbase + VIA_CRDR_DMACOUNTER);
  398. writel(ctrl_data, addrbase + VIA_CRDR_DMACTRL);
  399. writel(0x01, addrbase + VIA_CRDR_DMASTART);
  400. /* It seems that our DMA can not work normally with 375kHz clock */
  401. /* FIXME: don't brute-force 8MHz but use PIO at 375kHz !! */
  402. addrbase = host->pcictrl_mmiobase;
  403. if (readb(addrbase + VIA_CRDR_PCISDCCLK) == PCI_CLK_375K) {
  404. dev_info(host->mmc->parent, "forcing card speed to 8MHz\n");
  405. writeb(PCI_CLK_8M, addrbase + VIA_CRDR_PCISDCCLK);
  406. }
  407. }
  408. static void via_sdc_preparedata(struct via_crdr_mmc_host *host,
  409. struct mmc_data *data)
  410. {
  411. void __iomem *addrbase;
  412. u32 blk_reg;
  413. int count;
  414. WARN_ON(host->data);
  415. /* Sanity checks */
  416. BUG_ON(data->blksz > host->mmc->max_blk_size);
  417. BUG_ON(data->blocks > host->mmc->max_blk_count);
  418. host->data = data;
  419. count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  420. ((data->flags & MMC_DATA_READ) ?
  421. PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE));
  422. BUG_ON(count != 1);
  423. via_set_ddma(host, sg_dma_address(data->sg), sg_dma_len(data->sg),
  424. (data->flags & MMC_DATA_WRITE) ? 1 : 0, 1);
  425. addrbase = host->sdhc_mmiobase;
  426. blk_reg = data->blksz - 1;
  427. blk_reg |= VIA_CRDR_SDBLKLEN_GPIDET | VIA_CRDR_SDBLKLEN_INTEN;
  428. blk_reg |= (data->blocks) << 16;
  429. writel(blk_reg, addrbase + VIA_CRDR_SDBLKLEN);
  430. }
  431. static void via_sdc_get_response(struct via_crdr_mmc_host *host,
  432. struct mmc_command *cmd)
  433. {
  434. void __iomem *addrbase = host->sdhc_mmiobase;
  435. u32 dwdata0 = readl(addrbase + VIA_CRDR_SDRESP0);
  436. u32 dwdata1 = readl(addrbase + VIA_CRDR_SDRESP1);
  437. u32 dwdata2 = readl(addrbase + VIA_CRDR_SDRESP2);
  438. u32 dwdata3 = readl(addrbase + VIA_CRDR_SDRESP3);
  439. if (cmd->flags & MMC_RSP_136) {
  440. cmd->resp[0] = ((u8) (dwdata1)) |
  441. (((u8) (dwdata0 >> 24)) << 8) |
  442. (((u8) (dwdata0 >> 16)) << 16) |
  443. (((u8) (dwdata0 >> 8)) << 24);
  444. cmd->resp[1] = ((u8) (dwdata2)) |
  445. (((u8) (dwdata1 >> 24)) << 8) |
  446. (((u8) (dwdata1 >> 16)) << 16) |
  447. (((u8) (dwdata1 >> 8)) << 24);
  448. cmd->resp[2] = ((u8) (dwdata3)) |
  449. (((u8) (dwdata2 >> 24)) << 8) |
  450. (((u8) (dwdata2 >> 16)) << 16) |
  451. (((u8) (dwdata2 >> 8)) << 24);
  452. cmd->resp[3] = 0xff |
  453. ((((u8) (dwdata3 >> 24))) << 8) |
  454. (((u8) (dwdata3 >> 16)) << 16) |
  455. (((u8) (dwdata3 >> 8)) << 24);
  456. } else {
  457. dwdata0 >>= 8;
  458. cmd->resp[0] = ((dwdata0 & 0xff) << 24) |
  459. (((dwdata0 >> 8) & 0xff) << 16) |
  460. (((dwdata0 >> 16) & 0xff) << 8) | (dwdata1 & 0xff);
  461. dwdata1 >>= 8;
  462. cmd->resp[1] = ((dwdata1 & 0xff) << 24) |
  463. (((dwdata1 >> 8) & 0xff) << 16) |
  464. (((dwdata1 >> 16) & 0xff) << 8);
  465. }
  466. }
  467. static void via_sdc_send_command(struct via_crdr_mmc_host *host,
  468. struct mmc_command *cmd)
  469. {
  470. void __iomem *addrbase;
  471. struct mmc_data *data;
  472. unsigned int timeout_ms;
  473. u32 cmdctrl = 0;
  474. WARN_ON(host->cmd);
  475. data = cmd->data;
  476. host->cmd = cmd;
  477. timeout_ms = cmd->busy_timeout ? cmd->busy_timeout : VIA_CMD_TIMEOUT_MS;
  478. mod_timer(&host->timer, jiffies + msecs_to_jiffies(timeout_ms));
  479. /*Command index*/
  480. cmdctrl = cmd->opcode << 8;
  481. /*Response type*/
  482. switch (mmc_resp_type(cmd)) {
  483. case MMC_RSP_NONE:
  484. cmdctrl |= VIA_CRDR_SDCTRL_RSP_NONE;
  485. break;
  486. case MMC_RSP_R1:
  487. cmdctrl |= VIA_CRDR_SDCTRL_RSP_R1;
  488. break;
  489. case MMC_RSP_R1B:
  490. cmdctrl |= VIA_CRDR_SDCTRL_RSP_R1B;
  491. break;
  492. case MMC_RSP_R2:
  493. cmdctrl |= VIA_CRDR_SDCTRL_RSP_R2;
  494. break;
  495. case MMC_RSP_R3:
  496. cmdctrl |= VIA_CRDR_SDCTRL_RSP_R3;
  497. break;
  498. default:
  499. pr_err("%s: cmd->flag is not valid\n", mmc_hostname(host->mmc));
  500. break;
  501. }
  502. if (!(cmd->data))
  503. goto nodata;
  504. via_sdc_preparedata(host, data);
  505. /*Command control*/
  506. if (data->blocks > 1) {
  507. if (data->flags & MMC_DATA_WRITE) {
  508. cmdctrl |= VIA_CRDR_SDCTRL_WRITE;
  509. cmdctrl |= VIA_CRDR_SDCTRL_MULTI_WR;
  510. } else {
  511. cmdctrl |= VIA_CRDR_SDCTRL_MULTI_RD;
  512. }
  513. } else {
  514. if (data->flags & MMC_DATA_WRITE) {
  515. cmdctrl |= VIA_CRDR_SDCTRL_WRITE;
  516. cmdctrl |= VIA_CRDR_SDCTRL_SINGLE_WR;
  517. } else {
  518. cmdctrl |= VIA_CRDR_SDCTRL_SINGLE_RD;
  519. }
  520. }
  521. nodata:
  522. if (cmd == host->mrq->stop)
  523. cmdctrl |= VIA_CRDR_SDCTRL_STOP;
  524. cmdctrl |= VIA_CRDR_SDCTRL_START;
  525. addrbase = host->sdhc_mmiobase;
  526. writel(cmd->arg, addrbase + VIA_CRDR_SDCARG);
  527. writel(cmdctrl, addrbase + VIA_CRDR_SDCTRL);
  528. }
  529. static void via_sdc_finish_data(struct via_crdr_mmc_host *host)
  530. {
  531. struct mmc_data *data;
  532. BUG_ON(!host->data);
  533. data = host->data;
  534. host->data = NULL;
  535. if (data->error)
  536. data->bytes_xfered = 0;
  537. else
  538. data->bytes_xfered = data->blocks * data->blksz;
  539. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  540. ((data->flags & MMC_DATA_READ) ?
  541. PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE));
  542. if (data->stop)
  543. via_sdc_send_command(host, data->stop);
  544. else
  545. tasklet_schedule(&host->finish_tasklet);
  546. }
  547. static void via_sdc_finish_command(struct via_crdr_mmc_host *host)
  548. {
  549. via_sdc_get_response(host, host->cmd);
  550. host->cmd->error = 0;
  551. if (!host->cmd->data)
  552. tasklet_schedule(&host->finish_tasklet);
  553. host->cmd = NULL;
  554. }
  555. static void via_sdc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  556. {
  557. void __iomem *addrbase;
  558. struct via_crdr_mmc_host *host;
  559. unsigned long flags;
  560. u16 status;
  561. host = mmc_priv(mmc);
  562. spin_lock_irqsave(&host->lock, flags);
  563. addrbase = host->pcictrl_mmiobase;
  564. writeb(VIA_CRDR_PCIDMACLK_SDC, addrbase + VIA_CRDR_PCIDMACLK);
  565. status = readw(host->sdhc_mmiobase + VIA_CRDR_SDSTATUS);
  566. status &= VIA_CRDR_SDSTS_W1C_MASK;
  567. writew(status, host->sdhc_mmiobase + VIA_CRDR_SDSTATUS);
  568. WARN_ON(host->mrq != NULL);
  569. host->mrq = mrq;
  570. status = readw(host->sdhc_mmiobase + VIA_CRDR_SDSTATUS);
  571. if (!(status & VIA_CRDR_SDSTS_SLOTG) || host->reject) {
  572. host->mrq->cmd->error = -ENOMEDIUM;
  573. tasklet_schedule(&host->finish_tasklet);
  574. } else {
  575. via_sdc_send_command(host, mrq->cmd);
  576. }
  577. mmiowb();
  578. spin_unlock_irqrestore(&host->lock, flags);
  579. }
  580. static void via_sdc_set_power(struct via_crdr_mmc_host *host,
  581. unsigned short power, unsigned int on)
  582. {
  583. unsigned long flags;
  584. u8 gatt;
  585. spin_lock_irqsave(&host->lock, flags);
  586. host->power = (1 << power);
  587. gatt = readb(host->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  588. if (host->power == MMC_VDD_165_195)
  589. gatt &= ~VIA_CRDR_PCICLKGATT_3V3;
  590. else
  591. gatt |= VIA_CRDR_PCICLKGATT_3V3;
  592. if (on)
  593. gatt |= VIA_CRDR_PCICLKGATT_PAD_PWRON;
  594. else
  595. gatt &= ~VIA_CRDR_PCICLKGATT_PAD_PWRON;
  596. writeb(gatt, host->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  597. mmiowb();
  598. spin_unlock_irqrestore(&host->lock, flags);
  599. via_pwron_sleep(host);
  600. }
  601. static void via_sdc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  602. {
  603. struct via_crdr_mmc_host *host;
  604. unsigned long flags;
  605. void __iomem *addrbase;
  606. u32 org_data, sdextctrl;
  607. u8 clock;
  608. host = mmc_priv(mmc);
  609. spin_lock_irqsave(&host->lock, flags);
  610. addrbase = host->sdhc_mmiobase;
  611. org_data = readl(addrbase + VIA_CRDR_SDBUSMODE);
  612. sdextctrl = readl(addrbase + VIA_CRDR_SDEXTCTRL);
  613. if (ios->bus_width == MMC_BUS_WIDTH_1)
  614. org_data &= ~VIA_CRDR_SDMODE_4BIT;
  615. else
  616. org_data |= VIA_CRDR_SDMODE_4BIT;
  617. if (ios->power_mode == MMC_POWER_OFF)
  618. org_data &= ~VIA_CRDR_SDMODE_CLK_ON;
  619. else
  620. org_data |= VIA_CRDR_SDMODE_CLK_ON;
  621. if (ios->timing == MMC_TIMING_SD_HS)
  622. sdextctrl |= VIA_CRDR_SDEXTCTRL_HISPD;
  623. else
  624. sdextctrl &= ~VIA_CRDR_SDEXTCTRL_HISPD;
  625. writel(org_data, addrbase + VIA_CRDR_SDBUSMODE);
  626. writel(sdextctrl, addrbase + VIA_CRDR_SDEXTCTRL);
  627. if (ios->clock >= 48000000)
  628. clock = PCI_CLK_48M;
  629. else if (ios->clock >= 33000000)
  630. clock = PCI_CLK_33M;
  631. else if (ios->clock >= 24000000)
  632. clock = PCI_CLK_24M;
  633. else if (ios->clock >= 16000000)
  634. clock = PCI_CLK_16M;
  635. else if (ios->clock >= 12000000)
  636. clock = PCI_CLK_12M;
  637. else if (ios->clock >= 8000000)
  638. clock = PCI_CLK_8M;
  639. else
  640. clock = PCI_CLK_375K;
  641. addrbase = host->pcictrl_mmiobase;
  642. if (readb(addrbase + VIA_CRDR_PCISDCCLK) != clock)
  643. writeb(clock, addrbase + VIA_CRDR_PCISDCCLK);
  644. mmiowb();
  645. spin_unlock_irqrestore(&host->lock, flags);
  646. if (ios->power_mode != MMC_POWER_OFF)
  647. via_sdc_set_power(host, ios->vdd, 1);
  648. else
  649. via_sdc_set_power(host, ios->vdd, 0);
  650. }
  651. static int via_sdc_get_ro(struct mmc_host *mmc)
  652. {
  653. struct via_crdr_mmc_host *host;
  654. unsigned long flags;
  655. u16 status;
  656. host = mmc_priv(mmc);
  657. spin_lock_irqsave(&host->lock, flags);
  658. status = readw(host->sdhc_mmiobase + VIA_CRDR_SDSTATUS);
  659. spin_unlock_irqrestore(&host->lock, flags);
  660. return !(status & VIA_CRDR_SDSTS_WP);
  661. }
  662. static const struct mmc_host_ops via_sdc_ops = {
  663. .request = via_sdc_request,
  664. .set_ios = via_sdc_set_ios,
  665. .get_ro = via_sdc_get_ro,
  666. };
  667. static void via_reset_pcictrl(struct via_crdr_mmc_host *host)
  668. {
  669. unsigned long flags;
  670. u8 gatt;
  671. spin_lock_irqsave(&host->lock, flags);
  672. via_save_pcictrlreg(host);
  673. via_save_sdcreg(host);
  674. spin_unlock_irqrestore(&host->lock, flags);
  675. gatt = VIA_CRDR_PCICLKGATT_PAD_PWRON;
  676. if (host->power == MMC_VDD_165_195)
  677. gatt &= VIA_CRDR_PCICLKGATT_3V3;
  678. else
  679. gatt |= VIA_CRDR_PCICLKGATT_3V3;
  680. writeb(gatt, host->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  681. via_pwron_sleep(host);
  682. gatt |= VIA_CRDR_PCICLKGATT_SFTRST;
  683. writeb(gatt, host->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  684. msleep(3);
  685. spin_lock_irqsave(&host->lock, flags);
  686. via_restore_pcictrlreg(host);
  687. via_restore_sdcreg(host);
  688. mmiowb();
  689. spin_unlock_irqrestore(&host->lock, flags);
  690. }
  691. static void via_sdc_cmd_isr(struct via_crdr_mmc_host *host, u16 intmask)
  692. {
  693. BUG_ON(intmask == 0);
  694. if (!host->cmd) {
  695. pr_err("%s: Got command interrupt 0x%x even "
  696. "though no command operation was in progress.\n",
  697. mmc_hostname(host->mmc), intmask);
  698. return;
  699. }
  700. if (intmask & VIA_CRDR_SDSTS_CRTO)
  701. host->cmd->error = -ETIMEDOUT;
  702. else if (intmask & VIA_CRDR_SDSTS_SC)
  703. host->cmd->error = -EILSEQ;
  704. if (host->cmd->error)
  705. tasklet_schedule(&host->finish_tasklet);
  706. else if (intmask & VIA_CRDR_SDSTS_CRD)
  707. via_sdc_finish_command(host);
  708. }
  709. static void via_sdc_data_isr(struct via_crdr_mmc_host *host, u16 intmask)
  710. {
  711. BUG_ON(intmask == 0);
  712. if (intmask & VIA_CRDR_SDSTS_DT)
  713. host->data->error = -ETIMEDOUT;
  714. else if (intmask & (VIA_CRDR_SDSTS_RC | VIA_CRDR_SDSTS_WC))
  715. host->data->error = -EILSEQ;
  716. via_sdc_finish_data(host);
  717. }
  718. static irqreturn_t via_sdc_isr(int irq, void *dev_id)
  719. {
  720. struct via_crdr_mmc_host *sdhost = dev_id;
  721. void __iomem *addrbase;
  722. u8 pci_status;
  723. u16 sd_status;
  724. irqreturn_t result;
  725. if (!sdhost)
  726. return IRQ_NONE;
  727. spin_lock(&sdhost->lock);
  728. addrbase = sdhost->pcictrl_mmiobase;
  729. pci_status = readb(addrbase + VIA_CRDR_PCIINTSTATUS);
  730. if (!(pci_status & VIA_CRDR_PCIINTSTATUS_SDC)) {
  731. result = IRQ_NONE;
  732. goto out;
  733. }
  734. addrbase = sdhost->sdhc_mmiobase;
  735. sd_status = readw(addrbase + VIA_CRDR_SDSTATUS);
  736. sd_status &= VIA_CRDR_SDSTS_INT_MASK;
  737. sd_status &= ~VIA_CRDR_SDSTS_IGN_MASK;
  738. if (!sd_status) {
  739. result = IRQ_NONE;
  740. goto out;
  741. }
  742. if (sd_status & VIA_CRDR_SDSTS_CIR) {
  743. writew(sd_status & VIA_CRDR_SDSTS_CIR,
  744. addrbase + VIA_CRDR_SDSTATUS);
  745. schedule_work(&sdhost->carddet_work);
  746. }
  747. sd_status &= ~VIA_CRDR_SDSTS_CIR;
  748. if (sd_status & VIA_CRDR_SDSTS_CMD_MASK) {
  749. writew(sd_status & VIA_CRDR_SDSTS_CMD_MASK,
  750. addrbase + VIA_CRDR_SDSTATUS);
  751. via_sdc_cmd_isr(sdhost, sd_status & VIA_CRDR_SDSTS_CMD_MASK);
  752. }
  753. if (sd_status & VIA_CRDR_SDSTS_DATA_MASK) {
  754. writew(sd_status & VIA_CRDR_SDSTS_DATA_MASK,
  755. addrbase + VIA_CRDR_SDSTATUS);
  756. via_sdc_data_isr(sdhost, sd_status & VIA_CRDR_SDSTS_DATA_MASK);
  757. }
  758. sd_status &= ~(VIA_CRDR_SDSTS_CMD_MASK | VIA_CRDR_SDSTS_DATA_MASK);
  759. if (sd_status) {
  760. pr_err("%s: Unexpected interrupt 0x%x\n",
  761. mmc_hostname(sdhost->mmc), sd_status);
  762. writew(sd_status, addrbase + VIA_CRDR_SDSTATUS);
  763. }
  764. result = IRQ_HANDLED;
  765. mmiowb();
  766. out:
  767. spin_unlock(&sdhost->lock);
  768. return result;
  769. }
  770. static void via_sdc_timeout(struct timer_list *t)
  771. {
  772. struct via_crdr_mmc_host *sdhost;
  773. unsigned long flags;
  774. sdhost = from_timer(sdhost, t, timer);
  775. spin_lock_irqsave(&sdhost->lock, flags);
  776. if (sdhost->mrq) {
  777. pr_err("%s: Timeout waiting for hardware interrupt."
  778. "cmd:0x%x\n", mmc_hostname(sdhost->mmc),
  779. sdhost->mrq->cmd->opcode);
  780. if (sdhost->data) {
  781. writel(VIA_CRDR_DMACTRL_SFTRST,
  782. sdhost->ddma_mmiobase + VIA_CRDR_DMACTRL);
  783. sdhost->data->error = -ETIMEDOUT;
  784. via_sdc_finish_data(sdhost);
  785. } else {
  786. if (sdhost->cmd)
  787. sdhost->cmd->error = -ETIMEDOUT;
  788. else
  789. sdhost->mrq->cmd->error = -ETIMEDOUT;
  790. tasklet_schedule(&sdhost->finish_tasklet);
  791. }
  792. }
  793. mmiowb();
  794. spin_unlock_irqrestore(&sdhost->lock, flags);
  795. }
  796. static void via_sdc_tasklet_finish(unsigned long param)
  797. {
  798. struct via_crdr_mmc_host *host;
  799. unsigned long flags;
  800. struct mmc_request *mrq;
  801. host = (struct via_crdr_mmc_host *)param;
  802. spin_lock_irqsave(&host->lock, flags);
  803. del_timer(&host->timer);
  804. mrq = host->mrq;
  805. host->mrq = NULL;
  806. host->cmd = NULL;
  807. host->data = NULL;
  808. spin_unlock_irqrestore(&host->lock, flags);
  809. mmc_request_done(host->mmc, mrq);
  810. }
  811. static void via_sdc_card_detect(struct work_struct *work)
  812. {
  813. struct via_crdr_mmc_host *host;
  814. void __iomem *addrbase;
  815. unsigned long flags;
  816. u16 status;
  817. host = container_of(work, struct via_crdr_mmc_host, carddet_work);
  818. addrbase = host->ddma_mmiobase;
  819. writel(VIA_CRDR_DMACTRL_SFTRST, addrbase + VIA_CRDR_DMACTRL);
  820. spin_lock_irqsave(&host->lock, flags);
  821. addrbase = host->pcictrl_mmiobase;
  822. writeb(VIA_CRDR_PCIDMACLK_SDC, addrbase + VIA_CRDR_PCIDMACLK);
  823. addrbase = host->sdhc_mmiobase;
  824. status = readw(addrbase + VIA_CRDR_SDSTATUS);
  825. if (!(status & VIA_CRDR_SDSTS_SLOTG)) {
  826. if (host->mrq) {
  827. pr_err("%s: Card removed during transfer!\n",
  828. mmc_hostname(host->mmc));
  829. host->mrq->cmd->error = -ENOMEDIUM;
  830. tasklet_schedule(&host->finish_tasklet);
  831. }
  832. mmiowb();
  833. spin_unlock_irqrestore(&host->lock, flags);
  834. via_reset_pcictrl(host);
  835. spin_lock_irqsave(&host->lock, flags);
  836. }
  837. mmiowb();
  838. spin_unlock_irqrestore(&host->lock, flags);
  839. via_print_pcictrl(host);
  840. via_print_sdchc(host);
  841. mmc_detect_change(host->mmc, msecs_to_jiffies(500));
  842. }
  843. static void via_init_mmc_host(struct via_crdr_mmc_host *host)
  844. {
  845. struct mmc_host *mmc = host->mmc;
  846. void __iomem *addrbase;
  847. u32 lenreg;
  848. u32 status;
  849. timer_setup(&host->timer, via_sdc_timeout, 0);
  850. spin_lock_init(&host->lock);
  851. mmc->f_min = VIA_CRDR_MIN_CLOCK;
  852. mmc->f_max = VIA_CRDR_MAX_CLOCK;
  853. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  854. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SD_HIGHSPEED;
  855. mmc->ops = &via_sdc_ops;
  856. /*Hardware cannot do scatter lists*/
  857. mmc->max_segs = 1;
  858. mmc->max_blk_size = VIA_CRDR_MAX_BLOCK_LENGTH;
  859. mmc->max_blk_count = VIA_CRDR_MAX_BLOCK_COUNT;
  860. mmc->max_seg_size = mmc->max_blk_size * mmc->max_blk_count;
  861. mmc->max_req_size = mmc->max_seg_size;
  862. INIT_WORK(&host->carddet_work, via_sdc_card_detect);
  863. tasklet_init(&host->finish_tasklet, via_sdc_tasklet_finish,
  864. (unsigned long)host);
  865. addrbase = host->sdhc_mmiobase;
  866. writel(0x0, addrbase + VIA_CRDR_SDINTMASK);
  867. msleep(1);
  868. lenreg = VIA_CRDR_SDBLKLEN_GPIDET | VIA_CRDR_SDBLKLEN_INTEN;
  869. writel(lenreg, addrbase + VIA_CRDR_SDBLKLEN);
  870. status = readw(addrbase + VIA_CRDR_SDSTATUS);
  871. status &= VIA_CRDR_SDSTS_W1C_MASK;
  872. writew(status, addrbase + VIA_CRDR_SDSTATUS);
  873. status = readw(addrbase + VIA_CRDR_SDSTATUS2);
  874. status |= VIA_CRDR_SDSTS_CFE;
  875. writew(status, addrbase + VIA_CRDR_SDSTATUS2);
  876. writeb(0x0, addrbase + VIA_CRDR_SDEXTCTRL);
  877. writel(VIA_CRDR_SDACTIVE_INTMASK, addrbase + VIA_CRDR_SDINTMASK);
  878. msleep(1);
  879. }
  880. static int via_sd_probe(struct pci_dev *pcidev,
  881. const struct pci_device_id *id)
  882. {
  883. struct mmc_host *mmc;
  884. struct via_crdr_mmc_host *sdhost;
  885. u32 base, len;
  886. u8 gatt;
  887. int ret;
  888. pr_info(DRV_NAME
  889. ": VIA SDMMC controller found at %s [%04x:%04x] (rev %x)\n",
  890. pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device,
  891. (int)pcidev->revision);
  892. ret = pci_enable_device(pcidev);
  893. if (ret)
  894. return ret;
  895. ret = pci_request_regions(pcidev, DRV_NAME);
  896. if (ret)
  897. goto disable;
  898. pci_write_config_byte(pcidev, VIA_CRDR_PCI_WORK_MODE, 0);
  899. pci_write_config_byte(pcidev, VIA_CRDR_PCI_DBG_MODE, 0);
  900. mmc = mmc_alloc_host(sizeof(struct via_crdr_mmc_host), &pcidev->dev);
  901. if (!mmc) {
  902. ret = -ENOMEM;
  903. goto release;
  904. }
  905. sdhost = mmc_priv(mmc);
  906. sdhost->mmc = mmc;
  907. dev_set_drvdata(&pcidev->dev, sdhost);
  908. len = pci_resource_len(pcidev, 0);
  909. base = pci_resource_start(pcidev, 0);
  910. sdhost->mmiobase = ioremap_nocache(base, len);
  911. if (!sdhost->mmiobase) {
  912. ret = -ENOMEM;
  913. goto free_mmc_host;
  914. }
  915. sdhost->sdhc_mmiobase =
  916. sdhost->mmiobase + VIA_CRDR_SDC_OFF;
  917. sdhost->ddma_mmiobase =
  918. sdhost->mmiobase + VIA_CRDR_DDMA_OFF;
  919. sdhost->pcictrl_mmiobase =
  920. sdhost->mmiobase + VIA_CRDR_PCICTRL_OFF;
  921. sdhost->power = MMC_VDD_165_195;
  922. gatt = VIA_CRDR_PCICLKGATT_3V3 | VIA_CRDR_PCICLKGATT_PAD_PWRON;
  923. writeb(gatt, sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  924. via_pwron_sleep(sdhost);
  925. gatt |= VIA_CRDR_PCICLKGATT_SFTRST;
  926. writeb(gatt, sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  927. msleep(3);
  928. via_init_mmc_host(sdhost);
  929. ret =
  930. request_irq(pcidev->irq, via_sdc_isr, IRQF_SHARED, DRV_NAME,
  931. sdhost);
  932. if (ret)
  933. goto unmap;
  934. writeb(VIA_CRDR_PCIINTCTRL_SDCIRQEN,
  935. sdhost->pcictrl_mmiobase + VIA_CRDR_PCIINTCTRL);
  936. writeb(VIA_CRDR_PCITMOCTRL_1024MS,
  937. sdhost->pcictrl_mmiobase + VIA_CRDR_PCITMOCTRL);
  938. /* device-specific quirks */
  939. if (pcidev->subsystem_vendor == PCI_VENDOR_ID_LENOVO &&
  940. pcidev->subsystem_device == 0x3891)
  941. sdhost->quirks = VIA_CRDR_QUIRK_300MS_PWRDELAY;
  942. mmc_add_host(mmc);
  943. return 0;
  944. unmap:
  945. iounmap(sdhost->mmiobase);
  946. free_mmc_host:
  947. dev_set_drvdata(&pcidev->dev, NULL);
  948. mmc_free_host(mmc);
  949. release:
  950. pci_release_regions(pcidev);
  951. disable:
  952. pci_disable_device(pcidev);
  953. return ret;
  954. }
  955. static void via_sd_remove(struct pci_dev *pcidev)
  956. {
  957. struct via_crdr_mmc_host *sdhost = pci_get_drvdata(pcidev);
  958. unsigned long flags;
  959. u8 gatt;
  960. spin_lock_irqsave(&sdhost->lock, flags);
  961. /* Ensure we don't accept more commands from mmc layer */
  962. sdhost->reject = 1;
  963. /* Disable generating further interrupts */
  964. writeb(0x0, sdhost->pcictrl_mmiobase + VIA_CRDR_PCIINTCTRL);
  965. mmiowb();
  966. if (sdhost->mrq) {
  967. pr_err("%s: Controller removed during "
  968. "transfer\n", mmc_hostname(sdhost->mmc));
  969. /* make sure all DMA is stopped */
  970. writel(VIA_CRDR_DMACTRL_SFTRST,
  971. sdhost->ddma_mmiobase + VIA_CRDR_DMACTRL);
  972. mmiowb();
  973. sdhost->mrq->cmd->error = -ENOMEDIUM;
  974. if (sdhost->mrq->stop)
  975. sdhost->mrq->stop->error = -ENOMEDIUM;
  976. tasklet_schedule(&sdhost->finish_tasklet);
  977. }
  978. spin_unlock_irqrestore(&sdhost->lock, flags);
  979. mmc_remove_host(sdhost->mmc);
  980. free_irq(pcidev->irq, sdhost);
  981. del_timer_sync(&sdhost->timer);
  982. tasklet_kill(&sdhost->finish_tasklet);
  983. /* switch off power */
  984. gatt = readb(sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  985. gatt &= ~VIA_CRDR_PCICLKGATT_PAD_PWRON;
  986. writeb(gatt, sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  987. iounmap(sdhost->mmiobase);
  988. dev_set_drvdata(&pcidev->dev, NULL);
  989. mmc_free_host(sdhost->mmc);
  990. pci_release_regions(pcidev);
  991. pci_disable_device(pcidev);
  992. pr_info(DRV_NAME
  993. ": VIA SDMMC controller at %s [%04x:%04x] has been removed\n",
  994. pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device);
  995. }
  996. #ifdef CONFIG_PM
  997. static void via_init_sdc_pm(struct via_crdr_mmc_host *host)
  998. {
  999. struct sdhcreg *pm_sdhcreg;
  1000. void __iomem *addrbase;
  1001. u32 lenreg;
  1002. u16 status;
  1003. pm_sdhcreg = &(host->pm_sdhc_reg);
  1004. addrbase = host->sdhc_mmiobase;
  1005. writel(0x0, addrbase + VIA_CRDR_SDINTMASK);
  1006. lenreg = VIA_CRDR_SDBLKLEN_GPIDET | VIA_CRDR_SDBLKLEN_INTEN;
  1007. writel(lenreg, addrbase + VIA_CRDR_SDBLKLEN);
  1008. status = readw(addrbase + VIA_CRDR_SDSTATUS);
  1009. status &= VIA_CRDR_SDSTS_W1C_MASK;
  1010. writew(status, addrbase + VIA_CRDR_SDSTATUS);
  1011. status = readw(addrbase + VIA_CRDR_SDSTATUS2);
  1012. status |= VIA_CRDR_SDSTS_CFE;
  1013. writew(status, addrbase + VIA_CRDR_SDSTATUS2);
  1014. writel(pm_sdhcreg->sdcontrol_reg, addrbase + VIA_CRDR_SDCTRL);
  1015. writel(pm_sdhcreg->sdcmdarg_reg, addrbase + VIA_CRDR_SDCARG);
  1016. writel(pm_sdhcreg->sdintmask_reg, addrbase + VIA_CRDR_SDINTMASK);
  1017. writel(pm_sdhcreg->sdrsptmo_reg, addrbase + VIA_CRDR_SDRSPTMO);
  1018. writel(pm_sdhcreg->sdclksel_reg, addrbase + VIA_CRDR_SDCLKSEL);
  1019. writel(pm_sdhcreg->sdextctrl_reg, addrbase + VIA_CRDR_SDEXTCTRL);
  1020. via_print_pcictrl(host);
  1021. via_print_sdchc(host);
  1022. }
  1023. static int via_sd_suspend(struct pci_dev *pcidev, pm_message_t state)
  1024. {
  1025. struct via_crdr_mmc_host *host;
  1026. unsigned long flags;
  1027. host = pci_get_drvdata(pcidev);
  1028. spin_lock_irqsave(&host->lock, flags);
  1029. via_save_pcictrlreg(host);
  1030. via_save_sdcreg(host);
  1031. spin_unlock_irqrestore(&host->lock, flags);
  1032. pci_save_state(pcidev);
  1033. pci_enable_wake(pcidev, pci_choose_state(pcidev, state), 0);
  1034. pci_disable_device(pcidev);
  1035. pci_set_power_state(pcidev, pci_choose_state(pcidev, state));
  1036. return 0;
  1037. }
  1038. static int via_sd_resume(struct pci_dev *pcidev)
  1039. {
  1040. struct via_crdr_mmc_host *sdhost;
  1041. int ret = 0;
  1042. u8 gatt;
  1043. sdhost = pci_get_drvdata(pcidev);
  1044. gatt = VIA_CRDR_PCICLKGATT_PAD_PWRON;
  1045. if (sdhost->power == MMC_VDD_165_195)
  1046. gatt &= ~VIA_CRDR_PCICLKGATT_3V3;
  1047. else
  1048. gatt |= VIA_CRDR_PCICLKGATT_3V3;
  1049. writeb(gatt, sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  1050. via_pwron_sleep(sdhost);
  1051. gatt |= VIA_CRDR_PCICLKGATT_SFTRST;
  1052. writeb(gatt, sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  1053. msleep(3);
  1054. msleep(100);
  1055. pci_set_power_state(pcidev, PCI_D0);
  1056. pci_restore_state(pcidev);
  1057. ret = pci_enable_device(pcidev);
  1058. if (ret)
  1059. return ret;
  1060. via_restore_pcictrlreg(sdhost);
  1061. via_init_sdc_pm(sdhost);
  1062. return ret;
  1063. }
  1064. #else /* CONFIG_PM */
  1065. #define via_sd_suspend NULL
  1066. #define via_sd_resume NULL
  1067. #endif /* CONFIG_PM */
  1068. static struct pci_driver via_sd_driver = {
  1069. .name = DRV_NAME,
  1070. .id_table = via_ids,
  1071. .probe = via_sd_probe,
  1072. .remove = via_sd_remove,
  1073. .suspend = via_sd_suspend,
  1074. .resume = via_sd_resume,
  1075. };
  1076. module_pci_driver(via_sd_driver);
  1077. MODULE_LICENSE("GPL");
  1078. MODULE_AUTHOR("VIA Technologies Inc.");
  1079. MODULE_DESCRIPTION("VIA SD/MMC Card Interface driver");