via-sdmmc.c 35 KB

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