via-sdmmc.c 35 KB

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