intel8x0m.c 37 KB

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