via-sdmmc.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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. void __iomem *addrbase;
  665. unsigned long flags;
  666. u8 gatt;
  667. addrbase = host->pcictrl_mmiobase;
  668. spin_lock_irqsave(&host->lock, flags);
  669. via_save_pcictrlreg(host);
  670. via_save_sdcreg(host);
  671. spin_unlock_irqrestore(&host->lock, flags);
  672. gatt = VIA_CRDR_PCICLKGATT_PAD_PWRON;
  673. if (host->power == MMC_VDD_165_195)
  674. gatt &= VIA_CRDR_PCICLKGATT_3V3;
  675. else
  676. gatt |= VIA_CRDR_PCICLKGATT_3V3;
  677. writeb(gatt, host->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  678. via_pwron_sleep(host);
  679. gatt |= VIA_CRDR_PCICLKGATT_SFTRST;
  680. writeb(gatt, host->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  681. msleep(3);
  682. spin_lock_irqsave(&host->lock, flags);
  683. via_restore_pcictrlreg(host);
  684. via_restore_sdcreg(host);
  685. mmiowb();
  686. spin_unlock_irqrestore(&host->lock, flags);
  687. }
  688. static void via_sdc_cmd_isr(struct via_crdr_mmc_host *host, u16 intmask)
  689. {
  690. BUG_ON(intmask == 0);
  691. if (!host->cmd) {
  692. pr_err("%s: Got command interrupt 0x%x even "
  693. "though no command operation was in progress.\n",
  694. mmc_hostname(host->mmc), intmask);
  695. return;
  696. }
  697. if (intmask & VIA_CRDR_SDSTS_CRTO)
  698. host->cmd->error = -ETIMEDOUT;
  699. else if (intmask & VIA_CRDR_SDSTS_SC)
  700. host->cmd->error = -EILSEQ;
  701. if (host->cmd->error)
  702. tasklet_schedule(&host->finish_tasklet);
  703. else if (intmask & VIA_CRDR_SDSTS_CRD)
  704. via_sdc_finish_command(host);
  705. }
  706. static void via_sdc_data_isr(struct via_crdr_mmc_host *host, u16 intmask)
  707. {
  708. BUG_ON(intmask == 0);
  709. if (intmask & VIA_CRDR_SDSTS_DT)
  710. host->data->error = -ETIMEDOUT;
  711. else if (intmask & (VIA_CRDR_SDSTS_RC | VIA_CRDR_SDSTS_WC))
  712. host->data->error = -EILSEQ;
  713. via_sdc_finish_data(host);
  714. }
  715. static irqreturn_t via_sdc_isr(int irq, void *dev_id)
  716. {
  717. struct via_crdr_mmc_host *sdhost = dev_id;
  718. void __iomem *addrbase;
  719. u8 pci_status;
  720. u16 sd_status;
  721. irqreturn_t result;
  722. if (!sdhost)
  723. return IRQ_NONE;
  724. spin_lock(&sdhost->lock);
  725. addrbase = sdhost->pcictrl_mmiobase;
  726. pci_status = readb(addrbase + VIA_CRDR_PCIINTSTATUS);
  727. if (!(pci_status & VIA_CRDR_PCIINTSTATUS_SDC)) {
  728. result = IRQ_NONE;
  729. goto out;
  730. }
  731. addrbase = sdhost->sdhc_mmiobase;
  732. sd_status = readw(addrbase + VIA_CRDR_SDSTATUS);
  733. sd_status &= VIA_CRDR_SDSTS_INT_MASK;
  734. sd_status &= ~VIA_CRDR_SDSTS_IGN_MASK;
  735. if (!sd_status) {
  736. result = IRQ_NONE;
  737. goto out;
  738. }
  739. if (sd_status & VIA_CRDR_SDSTS_CIR) {
  740. writew(sd_status & VIA_CRDR_SDSTS_CIR,
  741. addrbase + VIA_CRDR_SDSTATUS);
  742. schedule_work(&sdhost->carddet_work);
  743. }
  744. sd_status &= ~VIA_CRDR_SDSTS_CIR;
  745. if (sd_status & VIA_CRDR_SDSTS_CMD_MASK) {
  746. writew(sd_status & VIA_CRDR_SDSTS_CMD_MASK,
  747. addrbase + VIA_CRDR_SDSTATUS);
  748. via_sdc_cmd_isr(sdhost, sd_status & VIA_CRDR_SDSTS_CMD_MASK);
  749. }
  750. if (sd_status & VIA_CRDR_SDSTS_DATA_MASK) {
  751. writew(sd_status & VIA_CRDR_SDSTS_DATA_MASK,
  752. addrbase + VIA_CRDR_SDSTATUS);
  753. via_sdc_data_isr(sdhost, sd_status & VIA_CRDR_SDSTS_DATA_MASK);
  754. }
  755. sd_status &= ~(VIA_CRDR_SDSTS_CMD_MASK | VIA_CRDR_SDSTS_DATA_MASK);
  756. if (sd_status) {
  757. pr_err("%s: Unexpected interrupt 0x%x\n",
  758. mmc_hostname(sdhost->mmc), sd_status);
  759. writew(sd_status, addrbase + VIA_CRDR_SDSTATUS);
  760. }
  761. result = IRQ_HANDLED;
  762. mmiowb();
  763. out:
  764. spin_unlock(&sdhost->lock);
  765. return result;
  766. }
  767. static void via_sdc_timeout(unsigned long ulongdata)
  768. {
  769. struct via_crdr_mmc_host *sdhost;
  770. unsigned long flags;
  771. sdhost = (struct via_crdr_mmc_host *)ulongdata;
  772. spin_lock_irqsave(&sdhost->lock, flags);
  773. if (sdhost->mrq) {
  774. pr_err("%s: Timeout waiting for hardware interrupt."
  775. "cmd:0x%x\n", mmc_hostname(sdhost->mmc),
  776. sdhost->mrq->cmd->opcode);
  777. if (sdhost->data) {
  778. writel(VIA_CRDR_DMACTRL_SFTRST,
  779. sdhost->ddma_mmiobase + VIA_CRDR_DMACTRL);
  780. sdhost->data->error = -ETIMEDOUT;
  781. via_sdc_finish_data(sdhost);
  782. } else {
  783. if (sdhost->cmd)
  784. sdhost->cmd->error = -ETIMEDOUT;
  785. else
  786. sdhost->mrq->cmd->error = -ETIMEDOUT;
  787. tasklet_schedule(&sdhost->finish_tasklet);
  788. }
  789. }
  790. mmiowb();
  791. spin_unlock_irqrestore(&sdhost->lock, flags);
  792. }
  793. static void via_sdc_tasklet_finish(unsigned long param)
  794. {
  795. struct via_crdr_mmc_host *host;
  796. unsigned long flags;
  797. struct mmc_request *mrq;
  798. host = (struct via_crdr_mmc_host *)param;
  799. spin_lock_irqsave(&host->lock, flags);
  800. del_timer(&host->timer);
  801. mrq = host->mrq;
  802. host->mrq = NULL;
  803. host->cmd = NULL;
  804. host->data = NULL;
  805. spin_unlock_irqrestore(&host->lock, flags);
  806. mmc_request_done(host->mmc, mrq);
  807. }
  808. static void via_sdc_card_detect(struct work_struct *work)
  809. {
  810. struct via_crdr_mmc_host *host;
  811. void __iomem *addrbase;
  812. unsigned long flags;
  813. u16 status;
  814. host = container_of(work, struct via_crdr_mmc_host, carddet_work);
  815. addrbase = host->ddma_mmiobase;
  816. writel(VIA_CRDR_DMACTRL_SFTRST, addrbase + VIA_CRDR_DMACTRL);
  817. spin_lock_irqsave(&host->lock, flags);
  818. addrbase = host->pcictrl_mmiobase;
  819. writeb(VIA_CRDR_PCIDMACLK_SDC, addrbase + VIA_CRDR_PCIDMACLK);
  820. addrbase = host->sdhc_mmiobase;
  821. status = readw(addrbase + VIA_CRDR_SDSTATUS);
  822. if (!(status & VIA_CRDR_SDSTS_SLOTG)) {
  823. if (host->mrq) {
  824. pr_err("%s: Card removed during transfer!\n",
  825. mmc_hostname(host->mmc));
  826. host->mrq->cmd->error = -ENOMEDIUM;
  827. tasklet_schedule(&host->finish_tasklet);
  828. }
  829. mmiowb();
  830. spin_unlock_irqrestore(&host->lock, flags);
  831. via_reset_pcictrl(host);
  832. spin_lock_irqsave(&host->lock, flags);
  833. }
  834. mmiowb();
  835. spin_unlock_irqrestore(&host->lock, flags);
  836. via_print_pcictrl(host);
  837. via_print_sdchc(host);
  838. mmc_detect_change(host->mmc, msecs_to_jiffies(500));
  839. }
  840. static void via_init_mmc_host(struct via_crdr_mmc_host *host)
  841. {
  842. struct mmc_host *mmc = host->mmc;
  843. void __iomem *addrbase;
  844. u32 lenreg;
  845. u32 status;
  846. init_timer(&host->timer);
  847. host->timer.data = (unsigned long)host;
  848. host->timer.function = via_sdc_timeout;
  849. spin_lock_init(&host->lock);
  850. mmc->f_min = VIA_CRDR_MIN_CLOCK;
  851. mmc->f_max = VIA_CRDR_MAX_CLOCK;
  852. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  853. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SD_HIGHSPEED;
  854. mmc->ops = &via_sdc_ops;
  855. /*Hardware cannot do scatter lists*/
  856. mmc->max_hw_segs = 1;
  857. mmc->max_phys_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 __devinit 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 rev, gatt;
  887. int ret;
  888. pci_read_config_byte(pcidev, PCI_CLASS_REVISION, &rev);
  889. pr_info(DRV_NAME
  890. ": VIA SDMMC controller found at %s [%04x:%04x] (rev %x)\n",
  891. pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device,
  892. (int)rev);
  893. ret = pci_enable_device(pcidev);
  894. if (ret)
  895. return ret;
  896. ret = pci_request_regions(pcidev, DRV_NAME);
  897. if (ret)
  898. goto disable;
  899. pci_write_config_byte(pcidev, VIA_CRDR_PCI_WORK_MODE, 0);
  900. pci_write_config_byte(pcidev, VIA_CRDR_PCI_DBG_MODE, 0);
  901. mmc = mmc_alloc_host(sizeof(struct via_crdr_mmc_host), &pcidev->dev);
  902. if (!mmc) {
  903. ret = -ENOMEM;
  904. goto release;
  905. }
  906. sdhost = mmc_priv(mmc);
  907. sdhost->mmc = mmc;
  908. dev_set_drvdata(&pcidev->dev, sdhost);
  909. len = pci_resource_len(pcidev, 0);
  910. base = pci_resource_start(pcidev, 0);
  911. sdhost->mmiobase = ioremap_nocache(base, len);
  912. if (!sdhost->mmiobase) {
  913. ret = -ENOMEM;
  914. goto free_mmc_host;
  915. }
  916. sdhost->sdhc_mmiobase =
  917. sdhost->mmiobase + VIA_CRDR_SDC_OFF;
  918. sdhost->ddma_mmiobase =
  919. sdhost->mmiobase + VIA_CRDR_DDMA_OFF;
  920. sdhost->pcictrl_mmiobase =
  921. sdhost->mmiobase + VIA_CRDR_PCICTRL_OFF;
  922. sdhost->power = MMC_VDD_165_195;
  923. gatt = VIA_CRDR_PCICLKGATT_3V3 | VIA_CRDR_PCICLKGATT_PAD_PWRON;
  924. writeb(gatt, sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  925. via_pwron_sleep(sdhost);
  926. gatt |= VIA_CRDR_PCICLKGATT_SFTRST;
  927. writeb(gatt, sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  928. msleep(3);
  929. via_init_mmc_host(sdhost);
  930. ret =
  931. request_irq(pcidev->irq, via_sdc_isr, IRQF_SHARED, DRV_NAME,
  932. sdhost);
  933. if (ret)
  934. goto unmap;
  935. writeb(VIA_CRDR_PCIINTCTRL_SDCIRQEN,
  936. sdhost->pcictrl_mmiobase + VIA_CRDR_PCIINTCTRL);
  937. writeb(VIA_CRDR_PCITMOCTRL_1024MS,
  938. sdhost->pcictrl_mmiobase + VIA_CRDR_PCITMOCTRL);
  939. /* device-specific quirks */
  940. if (pcidev->subsystem_vendor == PCI_VENDOR_ID_LENOVO &&
  941. pcidev->subsystem_device == 0x3891)
  942. sdhost->quirks = VIA_CRDR_QUIRK_300MS_PWRDELAY;
  943. mmc_add_host(mmc);
  944. return 0;
  945. unmap:
  946. iounmap(sdhost->mmiobase);
  947. free_mmc_host:
  948. dev_set_drvdata(&pcidev->dev, NULL);
  949. mmc_free_host(mmc);
  950. release:
  951. pci_release_regions(pcidev);
  952. disable:
  953. pci_disable_device(pcidev);
  954. return ret;
  955. }
  956. static void __devexit via_sd_remove(struct pci_dev *pcidev)
  957. {
  958. struct via_crdr_mmc_host *sdhost = pci_get_drvdata(pcidev);
  959. unsigned long flags;
  960. u8 gatt;
  961. spin_lock_irqsave(&sdhost->lock, flags);
  962. /* Ensure we don't accept more commands from mmc layer */
  963. sdhost->reject = 1;
  964. /* Disable generating further interrupts */
  965. writeb(0x0, sdhost->pcictrl_mmiobase + VIA_CRDR_PCIINTCTRL);
  966. mmiowb();
  967. if (sdhost->mrq) {
  968. printk(KERN_ERR "%s: Controller removed during "
  969. "transfer\n", mmc_hostname(sdhost->mmc));
  970. /* make sure all DMA is stopped */
  971. writel(VIA_CRDR_DMACTRL_SFTRST,
  972. sdhost->ddma_mmiobase + VIA_CRDR_DMACTRL);
  973. mmiowb();
  974. sdhost->mrq->cmd->error = -ENOMEDIUM;
  975. if (sdhost->mrq->stop)
  976. sdhost->mrq->stop->error = -ENOMEDIUM;
  977. tasklet_schedule(&sdhost->finish_tasklet);
  978. }
  979. spin_unlock_irqrestore(&sdhost->lock, flags);
  980. mmc_remove_host(sdhost->mmc);
  981. free_irq(pcidev->irq, sdhost);
  982. del_timer_sync(&sdhost->timer);
  983. tasklet_kill(&sdhost->finish_tasklet);
  984. /* switch off power */
  985. gatt = readb(sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  986. gatt &= ~VIA_CRDR_PCICLKGATT_PAD_PWRON;
  987. writeb(gatt, sdhost->pcictrl_mmiobase + VIA_CRDR_PCICLKGATT);
  988. iounmap(sdhost->mmiobase);
  989. dev_set_drvdata(&pcidev->dev, NULL);
  990. mmc_free_host(sdhost->mmc);
  991. pci_release_regions(pcidev);
  992. pci_disable_device(pcidev);
  993. pr_info(DRV_NAME
  994. ": VIA SDMMC controller at %s [%04x:%04x] has been removed\n",
  995. pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device);
  996. }
  997. #ifdef CONFIG_PM
  998. static void via_init_sdc_pm(struct via_crdr_mmc_host *host)
  999. {
  1000. struct sdhcreg *pm_sdhcreg;
  1001. void __iomem *addrbase;
  1002. u32 lenreg;
  1003. u16 status;
  1004. pm_sdhcreg = &(host->pm_sdhc_reg);
  1005. addrbase = host->sdhc_mmiobase;
  1006. writel(0x0, addrbase + VIA_CRDR_SDINTMASK);
  1007. lenreg = VIA_CRDR_SDBLKLEN_GPIDET | VIA_CRDR_SDBLKLEN_INTEN;
  1008. writel(lenreg, addrbase + VIA_CRDR_SDBLKLEN);
  1009. status = readw(addrbase + VIA_CRDR_SDSTATUS);
  1010. status &= VIA_CRDR_SDSTS_W1C_MASK;
  1011. writew(status, addrbase + VIA_CRDR_SDSTATUS);
  1012. status = readw(addrbase + VIA_CRDR_SDSTATUS2);
  1013. status |= VIA_CRDR_SDSTS_CFE;
  1014. writew(status, addrbase + VIA_CRDR_SDSTATUS2);
  1015. writel(pm_sdhcreg->sdcontrol_reg, addrbase + VIA_CRDR_SDCTRL);
  1016. writel(pm_sdhcreg->sdcmdarg_reg, addrbase + VIA_CRDR_SDCARG);
  1017. writel(pm_sdhcreg->sdintmask_reg, addrbase + VIA_CRDR_SDINTMASK);
  1018. writel(pm_sdhcreg->sdrsptmo_reg, addrbase + VIA_CRDR_SDRSPTMO);
  1019. writel(pm_sdhcreg->sdclksel_reg, addrbase + VIA_CRDR_SDCLKSEL);
  1020. writel(pm_sdhcreg->sdextctrl_reg, addrbase + VIA_CRDR_SDEXTCTRL);
  1021. via_print_pcictrl(host);
  1022. via_print_sdchc(host);
  1023. }
  1024. static int via_sd_suspend(struct pci_dev *pcidev, pm_message_t state)
  1025. {
  1026. struct via_crdr_mmc_host *host;
  1027. int ret = 0;
  1028. host = pci_get_drvdata(pcidev);
  1029. via_save_pcictrlreg(host);
  1030. via_save_sdcreg(host);
  1031. ret = mmc_suspend_host(host->mmc, state);
  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 ret;
  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. ret = mmc_resume_host(sdhost->mmc);
  1063. return ret;
  1064. }
  1065. #else /* CONFIG_PM */
  1066. #define via_sd_suspend NULL
  1067. #define via_sd_resume NULL
  1068. #endif /* CONFIG_PM */
  1069. static struct pci_driver via_sd_driver = {
  1070. .name = DRV_NAME,
  1071. .id_table = via_ids,
  1072. .probe = via_sd_probe,
  1073. .remove = __devexit_p(via_sd_remove),
  1074. .suspend = via_sd_suspend,
  1075. .resume = via_sd_resume,
  1076. };
  1077. static int __init via_sd_drv_init(void)
  1078. {
  1079. pr_info(DRV_NAME ": VIA SD/MMC Card Reader driver "
  1080. "(C) 2008 VIA Technologies, Inc.\n");
  1081. return pci_register_driver(&via_sd_driver);
  1082. }
  1083. static void __exit via_sd_drv_exit(void)
  1084. {
  1085. pci_unregister_driver(&via_sd_driver);
  1086. }
  1087. module_init(via_sd_drv_init);
  1088. module_exit(via_sd_drv_exit);
  1089. MODULE_LICENSE("GPL");
  1090. MODULE_AUTHOR("VIA Technologies Inc.");
  1091. MODULE_DESCRIPTION("VIA SD/MMC Card Interface driver");