via-sdmmc.c 35 KB

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