intel8x0m.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. /*
  2. * ALSA modem driver for Intel ICH (i8x0) chipsets
  3. *
  4. * Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
  5. *
  6. * This is modified (by Sasha Khapyorsky <sashak@smlink.com>) version
  7. * of ALSA ICH sound driver intel8x0.c .
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <sound/driver.h>
  26. #include <asm/io.h>
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/init.h>
  30. #include <linux/pci.h>
  31. #include <linux/slab.h>
  32. #include <linux/moduleparam.h>
  33. #include <sound/core.h>
  34. #include <sound/pcm.h>
  35. #include <sound/ac97_codec.h>
  36. #include <sound/info.h>
  37. #include <sound/initval.h>
  38. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  39. MODULE_DESCRIPTION("Intel 82801AA,82901AB,i810,i820,i830,i840,i845,MX440; SiS 7013; NVidia MCP/2/2S/3 modems");
  40. MODULE_LICENSE("GPL");
  41. MODULE_SUPPORTED_DEVICE("{{Intel,82801AA-ICH},"
  42. "{Intel,82901AB-ICH0},"
  43. "{Intel,82801BA-ICH2},"
  44. "{Intel,82801CA-ICH3},"
  45. "{Intel,82801DB-ICH4},"
  46. "{Intel,ICH5},"
  47. "{Intel,ICH6},"
  48. "{Intel,ICH7},"
  49. "{Intel,MX440},"
  50. "{SiS,7013},"
  51. "{NVidia,NForce Modem},"
  52. "{NVidia,NForce2 Modem},"
  53. "{NVidia,NForce2s Modem},"
  54. "{NVidia,NForce3 Modem},"
  55. "{AMD,AMD768}}");
  56. static int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* Exclude the first card */
  57. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  58. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  59. static int ac97_clock[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
  60. module_param_array(index, int, NULL, 0444);
  61. MODULE_PARM_DESC(index, "Index value for Intel i8x0 modemcard.");
  62. module_param_array(id, charp, NULL, 0444);
  63. MODULE_PARM_DESC(id, "ID string for Intel i8x0 modemcard.");
  64. module_param_array(enable, bool, NULL, 0444);
  65. MODULE_PARM_DESC(enable, "Enable Intel i8x0 modemcard.");
  66. module_param_array(ac97_clock, int, NULL, 0444);
  67. MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (0 = auto-detect).");
  68. /*
  69. * Direct registers
  70. */
  71. #ifndef PCI_DEVICE_ID_INTEL_82801_6
  72. #define PCI_DEVICE_ID_INTEL_82801_6 0x2416
  73. #endif
  74. #ifndef PCI_DEVICE_ID_INTEL_82901_6
  75. #define PCI_DEVICE_ID_INTEL_82901_6 0x2426
  76. #endif
  77. #ifndef PCI_DEVICE_ID_INTEL_82801BA_6
  78. #define PCI_DEVICE_ID_INTEL_82801BA_6 0x2446
  79. #endif
  80. #ifndef PCI_DEVICE_ID_INTEL_440MX_6
  81. #define PCI_DEVICE_ID_INTEL_440MX_6 0x7196
  82. #endif
  83. #ifndef PCI_DEVICE_ID_INTEL_ICH3_6
  84. #define PCI_DEVICE_ID_INTEL_ICH3_6 0x2486
  85. #endif
  86. #ifndef PCI_DEVICE_ID_INTEL_ICH4_6
  87. #define PCI_DEVICE_ID_INTEL_ICH4_6 0x24c6
  88. #endif
  89. #ifndef PCI_DEVICE_ID_INTEL_ICH5_6
  90. #define PCI_DEVICE_ID_INTEL_ICH5_6 0x24d6
  91. #endif
  92. #ifndef PCI_DEVICE_ID_INTEL_ICH6_6
  93. #define PCI_DEVICE_ID_INTEL_ICH6_6 0x266d
  94. #endif
  95. #ifndef PCI_DEVICE_ID_INTEL_ICH7_6
  96. #define PCI_DEVICE_ID_INTEL_ICH7_6 0x27dd
  97. #endif
  98. #ifndef PCI_DEVICE_ID_SI_7013
  99. #define PCI_DEVICE_ID_SI_7013 0x7013
  100. #endif
  101. #ifndef PCI_DEVICE_ID_NVIDIA_MCP_MODEM
  102. #define PCI_DEVICE_ID_NVIDIA_MCP_MODEM 0x01c1
  103. #endif
  104. #ifndef PCI_DEVICE_ID_NVIDIA_MCP2_MODEM
  105. #define PCI_DEVICE_ID_NVIDIA_MCP2_MODEM 0x0069
  106. #endif
  107. #ifndef PCI_DEVICE_ID_NVIDIA_MCP2S_MODEM
  108. #define PCI_DEVICE_ID_NVIDIA_MCP2S_MODEM 0x0089
  109. #endif
  110. #ifndef PCI_DEVICE_ID_NVIDIA_MCP3_MODEM
  111. #define PCI_DEVICE_ID_NVIDIA_MCP3_MODEM 0x00d9
  112. #endif
  113. enum { DEVICE_INTEL, DEVICE_SIS, DEVICE_ALI, DEVICE_NFORCE };
  114. #define ICHREG(x) ICH_REG_##x
  115. #define DEFINE_REGSET(name,base) \
  116. enum { \
  117. ICH_REG_##name##_BDBAR = base + 0x0, /* dword - buffer descriptor list base address */ \
  118. ICH_REG_##name##_CIV = base + 0x04, /* byte - current index value */ \
  119. ICH_REG_##name##_LVI = base + 0x05, /* byte - last valid index */ \
  120. ICH_REG_##name##_SR = base + 0x06, /* byte - status register */ \
  121. ICH_REG_##name##_PICB = base + 0x08, /* word - position in current buffer */ \
  122. ICH_REG_##name##_PIV = base + 0x0a, /* byte - prefetched index value */ \
  123. ICH_REG_##name##_CR = base + 0x0b, /* byte - control register */ \
  124. };
  125. /* busmaster blocks */
  126. DEFINE_REGSET(OFF, 0); /* offset */
  127. /* values for each busmaster block */
  128. /* LVI */
  129. #define ICH_REG_LVI_MASK 0x1f
  130. /* SR */
  131. #define ICH_FIFOE 0x10 /* FIFO error */
  132. #define ICH_BCIS 0x08 /* buffer completion interrupt status */
  133. #define ICH_LVBCI 0x04 /* last valid buffer completion interrupt */
  134. #define ICH_CELV 0x02 /* current equals last valid */
  135. #define ICH_DCH 0x01 /* DMA controller halted */
  136. /* PIV */
  137. #define ICH_REG_PIV_MASK 0x1f /* mask */
  138. /* CR */
  139. #define ICH_IOCE 0x10 /* interrupt on completion enable */
  140. #define ICH_FEIE 0x08 /* fifo error interrupt enable */
  141. #define ICH_LVBIE 0x04 /* last valid buffer interrupt enable */
  142. #define ICH_RESETREGS 0x02 /* reset busmaster registers */
  143. #define ICH_STARTBM 0x01 /* start busmaster operation */
  144. /* global block */
  145. #define ICH_REG_GLOB_CNT 0x3c /* dword - global control */
  146. #define ICH_TRIE 0x00000040 /* tertiary resume interrupt enable */
  147. #define ICH_SRIE 0x00000020 /* secondary resume interrupt enable */
  148. #define ICH_PRIE 0x00000010 /* primary resume interrupt enable */
  149. #define ICH_ACLINK 0x00000008 /* AClink shut off */
  150. #define ICH_AC97WARM 0x00000004 /* AC'97 warm reset */
  151. #define ICH_AC97COLD 0x00000002 /* AC'97 cold reset */
  152. #define ICH_GIE 0x00000001 /* GPI interrupt enable */
  153. #define ICH_REG_GLOB_STA 0x40 /* dword - global status */
  154. #define ICH_TRI 0x20000000 /* ICH4: tertiary (AC_SDIN2) resume interrupt */
  155. #define ICH_TCR 0x10000000 /* ICH4: tertiary (AC_SDIN2) codec ready */
  156. #define ICH_BCS 0x08000000 /* ICH4: bit clock stopped */
  157. #define ICH_SPINT 0x04000000 /* ICH4: S/PDIF interrupt */
  158. #define ICH_P2INT 0x02000000 /* ICH4: PCM2-In interrupt */
  159. #define ICH_M2INT 0x01000000 /* ICH4: Mic2-In interrupt */
  160. #define ICH_SAMPLE_CAP 0x00c00000 /* ICH4: sample capability bits (RO) */
  161. #define ICH_MULTICHAN_CAP 0x00300000 /* ICH4: multi-channel capability bits (RO) */
  162. #define ICH_MD3 0x00020000 /* modem power down semaphore */
  163. #define ICH_AD3 0x00010000 /* audio power down semaphore */
  164. #define ICH_RCS 0x00008000 /* read completion status */
  165. #define ICH_BIT3 0x00004000 /* bit 3 slot 12 */
  166. #define ICH_BIT2 0x00002000 /* bit 2 slot 12 */
  167. #define ICH_BIT1 0x00001000 /* bit 1 slot 12 */
  168. #define ICH_SRI 0x00000800 /* secondary (AC_SDIN1) resume interrupt */
  169. #define ICH_PRI 0x00000400 /* primary (AC_SDIN0) resume interrupt */
  170. #define ICH_SCR 0x00000200 /* secondary (AC_SDIN1) codec ready */
  171. #define ICH_PCR 0x00000100 /* primary (AC_SDIN0) codec ready */
  172. #define ICH_MCINT 0x00000080 /* MIC capture interrupt */
  173. #define ICH_POINT 0x00000040 /* playback interrupt */
  174. #define ICH_PIINT 0x00000020 /* capture interrupt */
  175. #define ICH_NVSPINT 0x00000010 /* nforce spdif interrupt */
  176. #define ICH_MOINT 0x00000004 /* modem playback interrupt */
  177. #define ICH_MIINT 0x00000002 /* modem capture interrupt */
  178. #define ICH_GSCI 0x00000001 /* GPI status change interrupt */
  179. #define ICH_REG_ACC_SEMA 0x44 /* byte - codec write semaphore */
  180. #define ICH_CAS 0x01 /* codec access semaphore */
  181. #define ICH_MAX_FRAGS 32 /* max hw frags */
  182. /*
  183. *
  184. */
  185. enum { ICHD_MDMIN, ICHD_MDMOUT, ICHD_MDMLAST = ICHD_MDMOUT };
  186. enum { ALID_MDMIN, ALID_MDMOUT, ALID_MDMLAST = ALID_MDMOUT };
  187. #define get_ichdev(substream) (ichdev_t *)(substream->runtime->private_data)
  188. typedef struct {
  189. unsigned int ichd; /* ich device number */
  190. unsigned long reg_offset; /* offset to bmaddr */
  191. u32 *bdbar; /* CPU address (32bit) */
  192. unsigned int bdbar_addr; /* PCI bus address (32bit) */
  193. snd_pcm_substream_t *substream;
  194. unsigned int physbuf; /* physical address (32bit) */
  195. unsigned int size;
  196. unsigned int fragsize;
  197. unsigned int fragsize1;
  198. unsigned int position;
  199. int frags;
  200. int lvi;
  201. int lvi_frag;
  202. int civ;
  203. int ack;
  204. int ack_reload;
  205. unsigned int ack_bit;
  206. unsigned int roff_sr;
  207. unsigned int roff_picb;
  208. unsigned int int_sta_mask; /* interrupt status mask */
  209. unsigned int ali_slot; /* ALI DMA slot */
  210. ac97_t *ac97;
  211. } ichdev_t;
  212. typedef struct _snd_intel8x0m intel8x0_t;
  213. struct _snd_intel8x0m {
  214. unsigned int device_type;
  215. int irq;
  216. unsigned int mmio;
  217. unsigned long addr;
  218. void __iomem *remap_addr;
  219. unsigned int bm_mmio;
  220. unsigned long bmaddr;
  221. void __iomem *remap_bmaddr;
  222. struct pci_dev *pci;
  223. snd_card_t *card;
  224. int pcm_devs;
  225. snd_pcm_t *pcm[2];
  226. ichdev_t ichd[2];
  227. unsigned int in_ac97_init: 1;
  228. ac97_bus_t *ac97_bus;
  229. ac97_t *ac97;
  230. spinlock_t reg_lock;
  231. struct snd_dma_buffer bdbars;
  232. u32 bdbars_count;
  233. u32 int_sta_reg; /* interrupt status register */
  234. u32 int_sta_mask; /* interrupt status mask */
  235. unsigned int pcm_pos_shift;
  236. };
  237. static struct pci_device_id snd_intel8x0m_ids[] = {
  238. { 0x8086, 0x2416, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801AA */
  239. { 0x8086, 0x2426, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82901AB */
  240. { 0x8086, 0x2446, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801BA */
  241. { 0x8086, 0x2486, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH3 */
  242. { 0x8086, 0x24c6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH4 */
  243. { 0x8086, 0x24d6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH5 */
  244. { 0x8086, 0x266d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH6 */
  245. { 0x8086, 0x27dd, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH7 */
  246. { 0x8086, 0x7196, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 440MX */
  247. { 0x1022, 0x7446, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD768 */
  248. { 0x1039, 0x7013, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_SIS }, /* SI7013 */
  249. { 0x10de, 0x01c1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE */
  250. { 0x10de, 0x0069, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE2 */
  251. { 0x10de, 0x0089, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE2s */
  252. { 0x10de, 0x00d9, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE3 */
  253. #if 0
  254. { 0x1022, 0x746d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD8111 */
  255. { 0x10b9, 0x5455, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALI }, /* Ali5455 */
  256. #endif
  257. { 0, }
  258. };
  259. MODULE_DEVICE_TABLE(pci, snd_intel8x0m_ids);
  260. /*
  261. * Lowlevel I/O - busmaster
  262. */
  263. static u8 igetbyte(intel8x0_t *chip, u32 offset)
  264. {
  265. if (chip->bm_mmio)
  266. return readb(chip->remap_bmaddr + offset);
  267. else
  268. return inb(chip->bmaddr + offset);
  269. }
  270. static u16 igetword(intel8x0_t *chip, u32 offset)
  271. {
  272. if (chip->bm_mmio)
  273. return readw(chip->remap_bmaddr + offset);
  274. else
  275. return inw(chip->bmaddr + offset);
  276. }
  277. static u32 igetdword(intel8x0_t *chip, u32 offset)
  278. {
  279. if (chip->bm_mmio)
  280. return readl(chip->remap_bmaddr + offset);
  281. else
  282. return inl(chip->bmaddr + offset);
  283. }
  284. static void iputbyte(intel8x0_t *chip, u32 offset, u8 val)
  285. {
  286. if (chip->bm_mmio)
  287. writeb(val, chip->remap_bmaddr + offset);
  288. else
  289. outb(val, chip->bmaddr + offset);
  290. }
  291. static void iputword(intel8x0_t *chip, u32 offset, u16 val)
  292. {
  293. if (chip->bm_mmio)
  294. writew(val, chip->remap_bmaddr + offset);
  295. else
  296. outw(val, chip->bmaddr + offset);
  297. }
  298. static void iputdword(intel8x0_t *chip, u32 offset, u32 val)
  299. {
  300. if (chip->bm_mmio)
  301. writel(val, chip->remap_bmaddr + offset);
  302. else
  303. outl(val, chip->bmaddr + offset);
  304. }
  305. /*
  306. * Lowlevel I/O - AC'97 registers
  307. */
  308. static u16 iagetword(intel8x0_t *chip, u32 offset)
  309. {
  310. if (chip->mmio)
  311. return readw(chip->remap_addr + offset);
  312. else
  313. return inw(chip->addr + offset);
  314. }
  315. static void iaputword(intel8x0_t *chip, u32 offset, u16 val)
  316. {
  317. if (chip->mmio)
  318. writew(val, chip->remap_addr + offset);
  319. else
  320. outw(val, chip->addr + offset);
  321. }
  322. /*
  323. * Basic I/O
  324. */
  325. /*
  326. * access to AC97 codec via normal i/o (for ICH and SIS7013)
  327. */
  328. /* return the GLOB_STA bit for the corresponding codec */
  329. static unsigned int get_ich_codec_bit(intel8x0_t *chip, unsigned int codec)
  330. {
  331. static unsigned int codec_bit[3] = {
  332. ICH_PCR, ICH_SCR, ICH_TCR
  333. };
  334. snd_assert(codec < 3, return ICH_PCR);
  335. return codec_bit[codec];
  336. }
  337. static int snd_intel8x0m_codec_semaphore(intel8x0_t *chip, unsigned int codec)
  338. {
  339. int time;
  340. if (codec > 1)
  341. return -EIO;
  342. codec = get_ich_codec_bit(chip, codec);
  343. /* codec ready ? */
  344. if ((igetdword(chip, ICHREG(GLOB_STA)) & codec) == 0)
  345. return -EIO;
  346. /* Anyone holding a semaphore for 1 msec should be shot... */
  347. time = 100;
  348. do {
  349. if (!(igetbyte(chip, ICHREG(ACC_SEMA)) & ICH_CAS))
  350. return 0;
  351. udelay(10);
  352. } while (time--);
  353. /* access to some forbidden (non existant) ac97 registers will not
  354. * reset the semaphore. So even if you don't get the semaphore, still
  355. * continue the access. We don't need the semaphore anyway. */
  356. snd_printk("codec_semaphore: semaphore is not ready [0x%x][0x%x]\n",
  357. igetbyte(chip, ICHREG(ACC_SEMA)), igetdword(chip, ICHREG(GLOB_STA)));
  358. iagetword(chip, 0); /* clear semaphore flag */
  359. /* I don't care about the semaphore */
  360. return -EBUSY;
  361. }
  362. static void snd_intel8x0_codec_write(ac97_t *ac97,
  363. unsigned short reg,
  364. unsigned short val)
  365. {
  366. intel8x0_t *chip = ac97->private_data;
  367. if (snd_intel8x0m_codec_semaphore(chip, ac97->num) < 0) {
  368. if (! chip->in_ac97_init)
  369. snd_printk("codec_write %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
  370. }
  371. iaputword(chip, reg + ac97->num * 0x80, val);
  372. }
  373. static unsigned short snd_intel8x0_codec_read(ac97_t *ac97,
  374. unsigned short reg)
  375. {
  376. intel8x0_t *chip = ac97->private_data;
  377. unsigned short res;
  378. unsigned int tmp;
  379. if (snd_intel8x0m_codec_semaphore(chip, ac97->num) < 0) {
  380. if (! chip->in_ac97_init)
  381. snd_printk("codec_read %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
  382. res = 0xffff;
  383. } else {
  384. res = iagetword(chip, reg + ac97->num * 0x80);
  385. if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
  386. /* reset RCS and preserve other R/WC bits */
  387. iputdword(chip, ICHREG(GLOB_STA), tmp & ~(ICH_SRI|ICH_PRI|ICH_TRI|ICH_GSCI));
  388. if (! chip->in_ac97_init)
  389. snd_printk("codec_read %d: read timeout for register 0x%x\n", ac97->num, reg);
  390. res = 0xffff;
  391. }
  392. }
  393. if (reg == AC97_GPIO_STATUS)
  394. iagetword(chip, 0); /* clear semaphore */
  395. return res;
  396. }
  397. /*
  398. * DMA I/O
  399. */
  400. static void snd_intel8x0_setup_periods(intel8x0_t *chip, ichdev_t *ichdev)
  401. {
  402. int idx;
  403. u32 *bdbar = ichdev->bdbar;
  404. unsigned long port = ichdev->reg_offset;
  405. iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
  406. if (ichdev->size == ichdev->fragsize) {
  407. ichdev->ack_reload = ichdev->ack = 2;
  408. ichdev->fragsize1 = ichdev->fragsize >> 1;
  409. for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 4) {
  410. bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf);
  411. bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
  412. ichdev->fragsize1 >> chip->pcm_pos_shift);
  413. bdbar[idx + 2] = cpu_to_le32(ichdev->physbuf + (ichdev->size >> 1));
  414. bdbar[idx + 3] = cpu_to_le32(0x80000000 | /* interrupt on completion */
  415. ichdev->fragsize1 >> chip->pcm_pos_shift);
  416. }
  417. ichdev->frags = 2;
  418. } else {
  419. ichdev->ack_reload = ichdev->ack = 1;
  420. ichdev->fragsize1 = ichdev->fragsize;
  421. for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 2) {
  422. bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf + (((idx >> 1) * ichdev->fragsize) % ichdev->size));
  423. bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
  424. ichdev->fragsize >> chip->pcm_pos_shift);
  425. // printk("bdbar[%i] = 0x%x [0x%x]\n", idx + 0, bdbar[idx + 0], bdbar[idx + 1]);
  426. }
  427. ichdev->frags = ichdev->size / ichdev->fragsize;
  428. }
  429. iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi = ICH_REG_LVI_MASK);
  430. ichdev->civ = 0;
  431. iputbyte(chip, port + ICH_REG_OFF_CIV, 0);
  432. ichdev->lvi_frag = ICH_REG_LVI_MASK % ichdev->frags;
  433. ichdev->position = 0;
  434. #if 0
  435. printk("lvi_frag = %i, frags = %i, period_size = 0x%x, period_size1 = 0x%x\n",
  436. ichdev->lvi_frag, ichdev->frags, ichdev->fragsize, ichdev->fragsize1);
  437. #endif
  438. /* clear interrupts */
  439. iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
  440. }
  441. /*
  442. * Interrupt handler
  443. */
  444. static inline void snd_intel8x0_update(intel8x0_t *chip, ichdev_t *ichdev)
  445. {
  446. unsigned long port = ichdev->reg_offset;
  447. int civ, i, step;
  448. int ack = 0;
  449. civ = igetbyte(chip, port + ICH_REG_OFF_CIV);
  450. if (civ == ichdev->civ) {
  451. // snd_printd("civ same %d\n", civ);
  452. step = 1;
  453. ichdev->civ++;
  454. ichdev->civ &= ICH_REG_LVI_MASK;
  455. } else {
  456. step = civ - ichdev->civ;
  457. if (step < 0)
  458. step += ICH_REG_LVI_MASK + 1;
  459. // if (step != 1)
  460. // snd_printd("step = %d, %d -> %d\n", step, ichdev->civ, civ);
  461. ichdev->civ = civ;
  462. }
  463. ichdev->position += step * ichdev->fragsize1;
  464. ichdev->position %= ichdev->size;
  465. ichdev->lvi += step;
  466. ichdev->lvi &= ICH_REG_LVI_MASK;
  467. iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
  468. for (i = 0; i < step; i++) {
  469. ichdev->lvi_frag++;
  470. ichdev->lvi_frag %= ichdev->frags;
  471. ichdev->bdbar[ichdev->lvi * 2] = cpu_to_le32(ichdev->physbuf + ichdev->lvi_frag * ichdev->fragsize1);
  472. // printk("new: bdbar[%i] = 0x%x [0x%x], prefetch = %i, all = 0x%x, 0x%x\n", ichdev->lvi * 2, ichdev->bdbar[ichdev->lvi * 2], ichdev->bdbar[ichdev->lvi * 2 + 1], inb(ICH_REG_OFF_PIV + port), inl(port + 4), inb(port + ICH_REG_OFF_CR));
  473. if (--ichdev->ack == 0) {
  474. ichdev->ack = ichdev->ack_reload;
  475. ack = 1;
  476. }
  477. }
  478. if (ack && ichdev->substream) {
  479. spin_unlock(&chip->reg_lock);
  480. snd_pcm_period_elapsed(ichdev->substream);
  481. spin_lock(&chip->reg_lock);
  482. }
  483. iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
  484. }
  485. static irqreturn_t snd_intel8x0_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  486. {
  487. intel8x0_t *chip = dev_id;
  488. ichdev_t *ichdev;
  489. unsigned int status;
  490. unsigned int i;
  491. spin_lock(&chip->reg_lock);
  492. status = igetdword(chip, chip->int_sta_reg);
  493. if (status == 0xffffffff) { /* we are not yet resumed */
  494. spin_unlock(&chip->reg_lock);
  495. return IRQ_NONE;
  496. }
  497. if ((status & chip->int_sta_mask) == 0) {
  498. if (status)
  499. iputdword(chip, chip->int_sta_reg, status);
  500. spin_unlock(&chip->reg_lock);
  501. return IRQ_NONE;
  502. }
  503. for (i = 0; i < chip->bdbars_count; i++) {
  504. ichdev = &chip->ichd[i];
  505. if (status & ichdev->int_sta_mask)
  506. snd_intel8x0_update(chip, ichdev);
  507. }
  508. /* ack them */
  509. iputdword(chip, chip->int_sta_reg, status & chip->int_sta_mask);
  510. spin_unlock(&chip->reg_lock);
  511. return IRQ_HANDLED;
  512. }
  513. /*
  514. * PCM part
  515. */
  516. static int snd_intel8x0_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
  517. {
  518. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  519. ichdev_t *ichdev = get_ichdev(substream);
  520. unsigned char val = 0;
  521. unsigned long port = ichdev->reg_offset;
  522. switch (cmd) {
  523. case SNDRV_PCM_TRIGGER_START:
  524. case SNDRV_PCM_TRIGGER_RESUME:
  525. val = ICH_IOCE | ICH_STARTBM;
  526. break;
  527. case SNDRV_PCM_TRIGGER_STOP:
  528. case SNDRV_PCM_TRIGGER_SUSPEND:
  529. val = 0;
  530. break;
  531. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  532. val = ICH_IOCE;
  533. break;
  534. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  535. val = ICH_IOCE | ICH_STARTBM;
  536. break;
  537. default:
  538. return -EINVAL;
  539. }
  540. iputbyte(chip, port + ICH_REG_OFF_CR, val);
  541. if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  542. /* wait until DMA stopped */
  543. while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH)) ;
  544. /* reset whole DMA things */
  545. iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
  546. }
  547. return 0;
  548. }
  549. static int snd_intel8x0_hw_params(snd_pcm_substream_t * substream,
  550. snd_pcm_hw_params_t * hw_params)
  551. {
  552. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  553. }
  554. static int snd_intel8x0_hw_free(snd_pcm_substream_t * substream)
  555. {
  556. return snd_pcm_lib_free_pages(substream);
  557. }
  558. static snd_pcm_uframes_t snd_intel8x0_pcm_pointer(snd_pcm_substream_t * substream)
  559. {
  560. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  561. ichdev_t *ichdev = get_ichdev(substream);
  562. size_t ptr1, ptr;
  563. ptr1 = igetword(chip, ichdev->reg_offset + ichdev->roff_picb) << chip->pcm_pos_shift;
  564. if (ptr1 != 0)
  565. ptr = ichdev->fragsize1 - ptr1;
  566. else
  567. ptr = 0;
  568. ptr += ichdev->position;
  569. if (ptr >= ichdev->size)
  570. return 0;
  571. return bytes_to_frames(substream->runtime, ptr);
  572. }
  573. static int snd_intel8x0m_pcm_prepare(snd_pcm_substream_t * substream)
  574. {
  575. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  576. snd_pcm_runtime_t *runtime = substream->runtime;
  577. ichdev_t *ichdev = get_ichdev(substream);
  578. ichdev->physbuf = runtime->dma_addr;
  579. ichdev->size = snd_pcm_lib_buffer_bytes(substream);
  580. ichdev->fragsize = snd_pcm_lib_period_bytes(substream);
  581. snd_ac97_write(ichdev->ac97, AC97_LINE1_RATE, runtime->rate);
  582. snd_ac97_write(ichdev->ac97, AC97_LINE1_LEVEL, 0);
  583. snd_intel8x0_setup_periods(chip, ichdev);
  584. return 0;
  585. }
  586. static snd_pcm_hardware_t snd_intel8x0m_stream =
  587. {
  588. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  589. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  590. SNDRV_PCM_INFO_MMAP_VALID |
  591. SNDRV_PCM_INFO_PAUSE |
  592. SNDRV_PCM_INFO_RESUME),
  593. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  594. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
  595. .rate_min = 8000,
  596. .rate_max = 16000,
  597. .channels_min = 1,
  598. .channels_max = 1,
  599. .buffer_bytes_max = 64 * 1024,
  600. .period_bytes_min = 32,
  601. .period_bytes_max = 64 * 1024,
  602. .periods_min = 1,
  603. .periods_max = 1024,
  604. .fifo_size = 0,
  605. };
  606. static int snd_intel8x0m_pcm_open(snd_pcm_substream_t * substream, ichdev_t *ichdev)
  607. {
  608. static unsigned int rates[] = { 8000, 9600, 12000, 16000 };
  609. static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
  610. .count = ARRAY_SIZE(rates),
  611. .list = rates,
  612. .mask = 0,
  613. };
  614. snd_pcm_runtime_t *runtime = substream->runtime;
  615. int err;
  616. ichdev->substream = substream;
  617. runtime->hw = snd_intel8x0m_stream;
  618. err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  619. if ( err < 0 )
  620. return err;
  621. runtime->private_data = ichdev;
  622. return 0;
  623. }
  624. static int snd_intel8x0m_playback_open(snd_pcm_substream_t * substream)
  625. {
  626. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  627. return snd_intel8x0m_pcm_open(substream, &chip->ichd[ICHD_MDMOUT]);
  628. }
  629. static int snd_intel8x0m_playback_close(snd_pcm_substream_t * substream)
  630. {
  631. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  632. chip->ichd[ICHD_MDMOUT].substream = NULL;
  633. return 0;
  634. }
  635. static int snd_intel8x0m_capture_open(snd_pcm_substream_t * substream)
  636. {
  637. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  638. return snd_intel8x0m_pcm_open(substream, &chip->ichd[ICHD_MDMIN]);
  639. }
  640. static int snd_intel8x0m_capture_close(snd_pcm_substream_t * substream)
  641. {
  642. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  643. chip->ichd[ICHD_MDMIN].substream = NULL;
  644. return 0;
  645. }
  646. static snd_pcm_ops_t snd_intel8x0m_playback_ops = {
  647. .open = snd_intel8x0m_playback_open,
  648. .close = snd_intel8x0m_playback_close,
  649. .ioctl = snd_pcm_lib_ioctl,
  650. .hw_params = snd_intel8x0_hw_params,
  651. .hw_free = snd_intel8x0_hw_free,
  652. .prepare = snd_intel8x0m_pcm_prepare,
  653. .trigger = snd_intel8x0_pcm_trigger,
  654. .pointer = snd_intel8x0_pcm_pointer,
  655. };
  656. static snd_pcm_ops_t snd_intel8x0m_capture_ops = {
  657. .open = snd_intel8x0m_capture_open,
  658. .close = snd_intel8x0m_capture_close,
  659. .ioctl = snd_pcm_lib_ioctl,
  660. .hw_params = snd_intel8x0_hw_params,
  661. .hw_free = snd_intel8x0_hw_free,
  662. .prepare = snd_intel8x0m_pcm_prepare,
  663. .trigger = snd_intel8x0_pcm_trigger,
  664. .pointer = snd_intel8x0_pcm_pointer,
  665. };
  666. struct ich_pcm_table {
  667. char *suffix;
  668. snd_pcm_ops_t *playback_ops;
  669. snd_pcm_ops_t *capture_ops;
  670. size_t prealloc_size;
  671. size_t prealloc_max_size;
  672. int ac97_idx;
  673. };
  674. static int __devinit snd_intel8x0_pcm1(intel8x0_t *chip, int device, struct ich_pcm_table *rec)
  675. {
  676. snd_pcm_t *pcm;
  677. int err;
  678. char name[32];
  679. if (rec->suffix)
  680. sprintf(name, "Intel ICH - %s", rec->suffix);
  681. else
  682. strcpy(name, "Intel ICH");
  683. err = snd_pcm_new(chip->card, name, device,
  684. rec->playback_ops ? 1 : 0,
  685. rec->capture_ops ? 1 : 0, &pcm);
  686. if (err < 0)
  687. return err;
  688. if (rec->playback_ops)
  689. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, rec->playback_ops);
  690. if (rec->capture_ops)
  691. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, rec->capture_ops);
  692. pcm->private_data = chip;
  693. pcm->info_flags = 0;
  694. if (rec->suffix)
  695. sprintf(pcm->name, "%s - %s", chip->card->shortname, rec->suffix);
  696. else
  697. strcpy(pcm->name, chip->card->shortname);
  698. chip->pcm[device] = pcm;
  699. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  700. snd_dma_pci_data(chip->pci),
  701. rec->prealloc_size,
  702. rec->prealloc_max_size);
  703. return 0;
  704. }
  705. static struct ich_pcm_table intel_pcms[] __devinitdata = {
  706. {
  707. .suffix = "Modem",
  708. .playback_ops = &snd_intel8x0m_playback_ops,
  709. .capture_ops = &snd_intel8x0m_capture_ops,
  710. .prealloc_size = 32 * 1024,
  711. .prealloc_max_size = 64 * 1024,
  712. },
  713. };
  714. static int __devinit snd_intel8x0_pcm(intel8x0_t *chip)
  715. {
  716. int i, tblsize, device, err;
  717. struct ich_pcm_table *tbl, *rec;
  718. #if 1
  719. tbl = intel_pcms;
  720. tblsize = 1;
  721. #else
  722. switch (chip->device_type) {
  723. case DEVICE_NFORCE:
  724. tbl = nforce_pcms;
  725. tblsize = ARRAY_SIZE(nforce_pcms);
  726. break;
  727. case DEVICE_ALI:
  728. tbl = ali_pcms;
  729. tblsize = ARRAY_SIZE(ali_pcms);
  730. break;
  731. default:
  732. tbl = intel_pcms;
  733. tblsize = 2;
  734. break;
  735. }
  736. #endif
  737. device = 0;
  738. for (i = 0; i < tblsize; i++) {
  739. rec = tbl + i;
  740. if (i > 0 && rec->ac97_idx) {
  741. /* activate PCM only when associated AC'97 codec */
  742. if (! chip->ichd[rec->ac97_idx].ac97)
  743. continue;
  744. }
  745. err = snd_intel8x0_pcm1(chip, device, rec);
  746. if (err < 0)
  747. return err;
  748. device++;
  749. }
  750. chip->pcm_devs = device;
  751. return 0;
  752. }
  753. /*
  754. * Mixer part
  755. */
  756. static void snd_intel8x0_mixer_free_ac97_bus(ac97_bus_t *bus)
  757. {
  758. intel8x0_t *chip = bus->private_data;
  759. chip->ac97_bus = NULL;
  760. }
  761. static void snd_intel8x0_mixer_free_ac97(ac97_t *ac97)
  762. {
  763. intel8x0_t *chip = ac97->private_data;
  764. chip->ac97 = NULL;
  765. }
  766. static int __devinit snd_intel8x0_mixer(intel8x0_t *chip, int ac97_clock)
  767. {
  768. ac97_bus_t *pbus;
  769. ac97_template_t ac97;
  770. ac97_t *x97;
  771. int err;
  772. unsigned int glob_sta = 0;
  773. static ac97_bus_ops_t ops = {
  774. .write = snd_intel8x0_codec_write,
  775. .read = snd_intel8x0_codec_read,
  776. };
  777. chip->in_ac97_init = 1;
  778. memset(&ac97, 0, sizeof(ac97));
  779. ac97.private_data = chip;
  780. ac97.private_free = snd_intel8x0_mixer_free_ac97;
  781. ac97.scaps = AC97_SCAP_SKIP_AUDIO;
  782. glob_sta = igetdword(chip, ICHREG(GLOB_STA));
  783. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  784. goto __err;
  785. pbus->private_free = snd_intel8x0_mixer_free_ac97_bus;
  786. pbus->shared_type = AC97_SHARED_TYPE_ICH; /* shared with audio driver */
  787. if (ac97_clock >= 8000 && ac97_clock <= 48000)
  788. pbus->clock = ac97_clock;
  789. chip->ac97_bus = pbus;
  790. ac97.pci = chip->pci;
  791. ac97.num = glob_sta & ICH_SCR ? 1 : 0;
  792. if ((err = snd_ac97_mixer(pbus, &ac97, &x97)) < 0) {
  793. snd_printk(KERN_ERR "Unable to initialize codec #%d\n", ac97.num);
  794. if (ac97.num == 0)
  795. goto __err;
  796. return err;
  797. }
  798. chip->ac97 = x97;
  799. if(ac97_is_modem(x97) && !chip->ichd[ICHD_MDMIN].ac97) {
  800. chip->ichd[ICHD_MDMIN].ac97 = x97;
  801. chip->ichd[ICHD_MDMOUT].ac97 = x97;
  802. }
  803. chip->in_ac97_init = 0;
  804. return 0;
  805. __err:
  806. /* clear the cold-reset bit for the next chance */
  807. if (chip->device_type != DEVICE_ALI)
  808. iputdword(chip, ICHREG(GLOB_CNT), igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_AC97COLD);
  809. return err;
  810. }
  811. /*
  812. *
  813. */
  814. #define do_delay(chip) do {\
  815. set_current_state(TASK_UNINTERRUPTIBLE);\
  816. schedule_timeout(1);\
  817. } while (0)
  818. static int snd_intel8x0m_ich_chip_init(intel8x0_t *chip, int probing)
  819. {
  820. unsigned long end_time;
  821. unsigned int cnt, status, nstatus;
  822. /* put logic to right state */
  823. /* first clear status bits */
  824. status = ICH_RCS | ICH_MIINT | ICH_MOINT;
  825. cnt = igetdword(chip, ICHREG(GLOB_STA));
  826. iputdword(chip, ICHREG(GLOB_STA), cnt & status);
  827. /* ACLink on, 2 channels */
  828. cnt = igetdword(chip, ICHREG(GLOB_CNT));
  829. cnt &= ~(ICH_ACLINK);
  830. /* finish cold or do warm reset */
  831. cnt |= (cnt & ICH_AC97COLD) == 0 ? ICH_AC97COLD : ICH_AC97WARM;
  832. iputdword(chip, ICHREG(GLOB_CNT), cnt);
  833. end_time = (jiffies + (HZ / 4)) + 1;
  834. do {
  835. if ((igetdword(chip, ICHREG(GLOB_CNT)) & ICH_AC97WARM) == 0)
  836. goto __ok;
  837. do_delay(chip);
  838. } while (time_after_eq(end_time, jiffies));
  839. snd_printk("AC'97 warm reset still in progress? [0x%x]\n", igetdword(chip, ICHREG(GLOB_CNT)));
  840. return -EIO;
  841. __ok:
  842. if (probing) {
  843. /* wait for any codec ready status.
  844. * Once it becomes ready it should remain ready
  845. * as long as we do not disable the ac97 link.
  846. */
  847. end_time = jiffies + HZ;
  848. do {
  849. status = igetdword(chip, ICHREG(GLOB_STA)) & (ICH_PCR | ICH_SCR | ICH_TCR);
  850. if (status)
  851. break;
  852. do_delay(chip);
  853. } while (time_after_eq(end_time, jiffies));
  854. if (! status) {
  855. /* no codec is found */
  856. snd_printk(KERN_ERR "codec_ready: codec is not ready [0x%x]\n", igetdword(chip, ICHREG(GLOB_STA)));
  857. return -EIO;
  858. }
  859. /* up to two codecs (modem cannot be tertiary with ICH4) */
  860. nstatus = ICH_PCR | ICH_SCR;
  861. /* wait for other codecs ready status. */
  862. end_time = jiffies + HZ / 4;
  863. while (status != nstatus && time_after_eq(end_time, jiffies)) {
  864. do_delay(chip);
  865. status |= igetdword(chip, ICHREG(GLOB_STA)) & nstatus;
  866. }
  867. } else {
  868. /* resume phase */
  869. status = 0;
  870. if (chip->ac97)
  871. status |= get_ich_codec_bit(chip, chip->ac97->num);
  872. /* wait until all the probed codecs are ready */
  873. end_time = jiffies + HZ;
  874. do {
  875. nstatus = igetdword(chip, ICHREG(GLOB_STA)) & (ICH_PCR | ICH_SCR | ICH_TCR);
  876. if (status == nstatus)
  877. break;
  878. do_delay(chip);
  879. } while (time_after_eq(end_time, jiffies));
  880. }
  881. if (chip->device_type == DEVICE_SIS) {
  882. /* unmute the output on SIS7012 */
  883. iputword(chip, 0x4c, igetword(chip, 0x4c) | 1);
  884. }
  885. return 0;
  886. }
  887. static int snd_intel8x0_chip_init(intel8x0_t *chip, int probing)
  888. {
  889. unsigned int i;
  890. int err;
  891. if ((err = snd_intel8x0m_ich_chip_init(chip, probing)) < 0)
  892. return err;
  893. iagetword(chip, 0); /* clear semaphore flag */
  894. /* disable interrupts */
  895. for (i = 0; i < chip->bdbars_count; i++)
  896. iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
  897. /* reset channels */
  898. for (i = 0; i < chip->bdbars_count; i++)
  899. iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
  900. /* initialize Buffer Descriptor Lists */
  901. for (i = 0; i < chip->bdbars_count; i++)
  902. iputdword(chip, ICH_REG_OFF_BDBAR + chip->ichd[i].reg_offset, chip->ichd[i].bdbar_addr);
  903. return 0;
  904. }
  905. static int snd_intel8x0_free(intel8x0_t *chip)
  906. {
  907. unsigned int i;
  908. if (chip->irq < 0)
  909. goto __hw_end;
  910. /* disable interrupts */
  911. for (i = 0; i < chip->bdbars_count; i++)
  912. iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
  913. /* reset channels */
  914. for (i = 0; i < chip->bdbars_count; i++)
  915. iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
  916. /* --- */
  917. synchronize_irq(chip->irq);
  918. __hw_end:
  919. if (chip->bdbars.area)
  920. snd_dma_free_pages(&chip->bdbars);
  921. if (chip->remap_addr)
  922. iounmap(chip->remap_addr);
  923. if (chip->remap_bmaddr)
  924. iounmap(chip->remap_bmaddr);
  925. if (chip->irq >= 0)
  926. free_irq(chip->irq, (void *)chip);
  927. pci_release_regions(chip->pci);
  928. pci_disable_device(chip->pci);
  929. kfree(chip);
  930. return 0;
  931. }
  932. #ifdef CONFIG_PM
  933. /*
  934. * power management
  935. */
  936. static int intel8x0m_suspend(snd_card_t *card, pm_message_t state)
  937. {
  938. intel8x0_t *chip = card->pm_private_data;
  939. int i;
  940. for (i = 0; i < chip->pcm_devs; i++)
  941. snd_pcm_suspend_all(chip->pcm[i]);
  942. if (chip->ac97)
  943. snd_ac97_suspend(chip->ac97);
  944. pci_disable_device(chip->pci);
  945. return 0;
  946. }
  947. static int intel8x0m_resume(snd_card_t *card)
  948. {
  949. intel8x0_t *chip = card->pm_private_data;
  950. pci_enable_device(chip->pci);
  951. pci_set_master(chip->pci);
  952. snd_intel8x0_chip_init(chip, 0);
  953. if (chip->ac97)
  954. snd_ac97_resume(chip->ac97);
  955. return 0;
  956. }
  957. #endif /* CONFIG_PM */
  958. static void snd_intel8x0m_proc_read(snd_info_entry_t * entry,
  959. snd_info_buffer_t * buffer)
  960. {
  961. intel8x0_t *chip = entry->private_data;
  962. unsigned int tmp;
  963. snd_iprintf(buffer, "Intel8x0m\n\n");
  964. if (chip->device_type == DEVICE_ALI)
  965. return;
  966. tmp = igetdword(chip, ICHREG(GLOB_STA));
  967. snd_iprintf(buffer, "Global control : 0x%08x\n", igetdword(chip, ICHREG(GLOB_CNT)));
  968. snd_iprintf(buffer, "Global status : 0x%08x\n", tmp);
  969. snd_iprintf(buffer, "AC'97 codecs ready :%s%s%s%s\n",
  970. tmp & ICH_PCR ? " primary" : "",
  971. tmp & ICH_SCR ? " secondary" : "",
  972. tmp & ICH_TCR ? " tertiary" : "",
  973. (tmp & (ICH_PCR | ICH_SCR | ICH_TCR)) == 0 ? " none" : "");
  974. }
  975. static void __devinit snd_intel8x0m_proc_init(intel8x0_t * chip)
  976. {
  977. snd_info_entry_t *entry;
  978. if (! snd_card_proc_new(chip->card, "intel8x0m", &entry))
  979. snd_info_set_text_ops(entry, chip, 1024, snd_intel8x0m_proc_read);
  980. }
  981. static int snd_intel8x0_dev_free(snd_device_t *device)
  982. {
  983. intel8x0_t *chip = device->device_data;
  984. return snd_intel8x0_free(chip);
  985. }
  986. struct ich_reg_info {
  987. unsigned int int_sta_mask;
  988. unsigned int offset;
  989. };
  990. static int __devinit snd_intel8x0m_create(snd_card_t * card,
  991. struct pci_dev *pci,
  992. unsigned long device_type,
  993. intel8x0_t ** r_intel8x0)
  994. {
  995. intel8x0_t *chip;
  996. int err;
  997. unsigned int i;
  998. unsigned int int_sta_masks;
  999. ichdev_t *ichdev;
  1000. static snd_device_ops_t ops = {
  1001. .dev_free = snd_intel8x0_dev_free,
  1002. };
  1003. static struct ich_reg_info intel_regs[2] = {
  1004. { ICH_MIINT, 0 },
  1005. { ICH_MOINT, 0x10 },
  1006. };
  1007. struct ich_reg_info *tbl;
  1008. *r_intel8x0 = NULL;
  1009. if ((err = pci_enable_device(pci)) < 0)
  1010. return err;
  1011. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  1012. if (chip == NULL) {
  1013. pci_disable_device(pci);
  1014. return -ENOMEM;
  1015. }
  1016. spin_lock_init(&chip->reg_lock);
  1017. chip->device_type = device_type;
  1018. chip->card = card;
  1019. chip->pci = pci;
  1020. chip->irq = -1;
  1021. if ((err = pci_request_regions(pci, card->shortname)) < 0) {
  1022. kfree(chip);
  1023. pci_disable_device(pci);
  1024. return err;
  1025. }
  1026. if (device_type == DEVICE_ALI) {
  1027. /* ALI5455 has no ac97 region */
  1028. chip->bmaddr = pci_resource_start(pci, 0);
  1029. goto port_inited;
  1030. }
  1031. if (pci_resource_flags(pci, 2) & IORESOURCE_MEM) { /* ICH4 and Nforce */
  1032. chip->mmio = 1;
  1033. chip->addr = pci_resource_start(pci, 2);
  1034. chip->remap_addr = ioremap_nocache(chip->addr,
  1035. pci_resource_len(pci, 2));
  1036. if (chip->remap_addr == NULL) {
  1037. snd_printk("AC'97 space ioremap problem\n");
  1038. snd_intel8x0_free(chip);
  1039. return -EIO;
  1040. }
  1041. } else {
  1042. chip->addr = pci_resource_start(pci, 0);
  1043. }
  1044. if (pci_resource_flags(pci, 3) & IORESOURCE_MEM) { /* ICH4 */
  1045. chip->bm_mmio = 1;
  1046. chip->bmaddr = pci_resource_start(pci, 3);
  1047. chip->remap_bmaddr = ioremap_nocache(chip->bmaddr,
  1048. pci_resource_len(pci, 3));
  1049. if (chip->remap_bmaddr == NULL) {
  1050. snd_printk("Controller space ioremap problem\n");
  1051. snd_intel8x0_free(chip);
  1052. return -EIO;
  1053. }
  1054. } else {
  1055. chip->bmaddr = pci_resource_start(pci, 1);
  1056. }
  1057. port_inited:
  1058. if (request_irq(pci->irq, snd_intel8x0_interrupt, SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip)) {
  1059. snd_printk("unable to grab IRQ %d\n", pci->irq);
  1060. snd_intel8x0_free(chip);
  1061. return -EBUSY;
  1062. }
  1063. chip->irq = pci->irq;
  1064. pci_set_master(pci);
  1065. synchronize_irq(chip->irq);
  1066. /* initialize offsets */
  1067. chip->bdbars_count = 2;
  1068. tbl = intel_regs;
  1069. for (i = 0; i < chip->bdbars_count; i++) {
  1070. ichdev = &chip->ichd[i];
  1071. ichdev->ichd = i;
  1072. ichdev->reg_offset = tbl[i].offset;
  1073. ichdev->int_sta_mask = tbl[i].int_sta_mask;
  1074. if (device_type == DEVICE_SIS) {
  1075. /* SiS 7013 swaps the registers */
  1076. ichdev->roff_sr = ICH_REG_OFF_PICB;
  1077. ichdev->roff_picb = ICH_REG_OFF_SR;
  1078. } else {
  1079. ichdev->roff_sr = ICH_REG_OFF_SR;
  1080. ichdev->roff_picb = ICH_REG_OFF_PICB;
  1081. }
  1082. if (device_type == DEVICE_ALI)
  1083. ichdev->ali_slot = (ichdev->reg_offset - 0x40) / 0x10;
  1084. }
  1085. /* SIS7013 handles the pcm data in bytes, others are in words */
  1086. chip->pcm_pos_shift = (device_type == DEVICE_SIS) ? 0 : 1;
  1087. /* allocate buffer descriptor lists */
  1088. /* the start of each lists must be aligned to 8 bytes */
  1089. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1090. chip->bdbars_count * sizeof(u32) * ICH_MAX_FRAGS * 2,
  1091. &chip->bdbars) < 0) {
  1092. snd_intel8x0_free(chip);
  1093. return -ENOMEM;
  1094. }
  1095. /* tables must be aligned to 8 bytes here, but the kernel pages
  1096. are much bigger, so we don't care (on i386) */
  1097. int_sta_masks = 0;
  1098. for (i = 0; i < chip->bdbars_count; i++) {
  1099. ichdev = &chip->ichd[i];
  1100. ichdev->bdbar = ((u32 *)chip->bdbars.area) + (i * ICH_MAX_FRAGS * 2);
  1101. ichdev->bdbar_addr = chip->bdbars.addr + (i * sizeof(u32) * ICH_MAX_FRAGS * 2);
  1102. int_sta_masks |= ichdev->int_sta_mask;
  1103. }
  1104. chip->int_sta_reg = ICH_REG_GLOB_STA;
  1105. chip->int_sta_mask = int_sta_masks;
  1106. if ((err = snd_intel8x0_chip_init(chip, 1)) < 0) {
  1107. snd_intel8x0_free(chip);
  1108. return err;
  1109. }
  1110. snd_card_set_pm_callback(card, intel8x0m_suspend, intel8x0m_resume, chip);
  1111. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1112. snd_intel8x0_free(chip);
  1113. return err;
  1114. }
  1115. snd_card_set_dev(card, &pci->dev);
  1116. *r_intel8x0 = chip;
  1117. return 0;
  1118. }
  1119. static struct shortname_table {
  1120. unsigned int id;
  1121. const char *s;
  1122. } shortnames[] __devinitdata = {
  1123. { PCI_DEVICE_ID_INTEL_82801_6, "Intel 82801AA-ICH" },
  1124. { PCI_DEVICE_ID_INTEL_82901_6, "Intel 82901AB-ICH0" },
  1125. { PCI_DEVICE_ID_INTEL_82801BA_6, "Intel 82801BA-ICH2" },
  1126. { PCI_DEVICE_ID_INTEL_440MX_6, "Intel 440MX" },
  1127. { PCI_DEVICE_ID_INTEL_ICH3_6, "Intel 82801CA-ICH3" },
  1128. { PCI_DEVICE_ID_INTEL_ICH4_6, "Intel 82801DB-ICH4" },
  1129. { PCI_DEVICE_ID_INTEL_ICH5_6, "Intel ICH5" },
  1130. { PCI_DEVICE_ID_INTEL_ICH6_6, "Intel ICH6" },
  1131. { PCI_DEVICE_ID_INTEL_ICH7_6, "Intel ICH7" },
  1132. { 0x7446, "AMD AMD768" },
  1133. { PCI_DEVICE_ID_SI_7013, "SiS SI7013" },
  1134. { PCI_DEVICE_ID_NVIDIA_MCP_MODEM, "NVidia nForce" },
  1135. { PCI_DEVICE_ID_NVIDIA_MCP2_MODEM, "NVidia nForce2" },
  1136. { PCI_DEVICE_ID_NVIDIA_MCP2S_MODEM, "NVidia nForce2s" },
  1137. { PCI_DEVICE_ID_NVIDIA_MCP3_MODEM, "NVidia nForce3" },
  1138. #if 0
  1139. { 0x5455, "ALi M5455" },
  1140. { 0x746d, "AMD AMD8111" },
  1141. #endif
  1142. { 0 },
  1143. };
  1144. static int __devinit snd_intel8x0m_probe(struct pci_dev *pci,
  1145. const struct pci_device_id *pci_id)
  1146. {
  1147. static int dev;
  1148. snd_card_t *card;
  1149. intel8x0_t *chip;
  1150. int err;
  1151. struct shortname_table *name;
  1152. if (dev >= SNDRV_CARDS)
  1153. return -ENODEV;
  1154. if (!enable[dev]) {
  1155. dev++;
  1156. return -ENOENT;
  1157. }
  1158. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1159. if (card == NULL)
  1160. return -ENOMEM;
  1161. strcpy(card->driver, "ICH-MODEM");
  1162. strcpy(card->shortname, "Intel ICH");
  1163. for (name = shortnames; name->id; name++) {
  1164. if (pci->device == name->id) {
  1165. strcpy(card->shortname, name->s);
  1166. break;
  1167. }
  1168. }
  1169. strcat(card->shortname," Modem");
  1170. if ((err = snd_intel8x0m_create(card, pci, pci_id->driver_data, &chip)) < 0) {
  1171. snd_card_free(card);
  1172. return err;
  1173. }
  1174. if ((err = snd_intel8x0_mixer(chip, ac97_clock[dev])) < 0) {
  1175. snd_card_free(card);
  1176. return err;
  1177. }
  1178. if ((err = snd_intel8x0_pcm(chip)) < 0) {
  1179. snd_card_free(card);
  1180. return err;
  1181. }
  1182. snd_intel8x0m_proc_init(chip);
  1183. sprintf(card->longname, "%s at 0x%lx, irq %i",
  1184. card->shortname, chip->addr, chip->irq);
  1185. if ((err = snd_card_register(card)) < 0) {
  1186. snd_card_free(card);
  1187. return err;
  1188. }
  1189. pci_set_drvdata(pci, card);
  1190. dev++;
  1191. return 0;
  1192. }
  1193. static void __devexit snd_intel8x0m_remove(struct pci_dev *pci)
  1194. {
  1195. snd_card_free(pci_get_drvdata(pci));
  1196. pci_set_drvdata(pci, NULL);
  1197. }
  1198. static struct pci_driver driver = {
  1199. .name = "Intel ICH Modem",
  1200. .id_table = snd_intel8x0m_ids,
  1201. .probe = snd_intel8x0m_probe,
  1202. .remove = __devexit_p(snd_intel8x0m_remove),
  1203. SND_PCI_PM_CALLBACKS
  1204. };
  1205. static int __init alsa_card_intel8x0m_init(void)
  1206. {
  1207. return pci_register_driver(&driver);
  1208. }
  1209. static void __exit alsa_card_intel8x0m_exit(void)
  1210. {
  1211. pci_unregister_driver(&driver);
  1212. }
  1213. module_init(alsa_card_intel8x0m_init)
  1214. module_exit(alsa_card_intel8x0m_exit)