intel8x0m.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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 <asm/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/moduleparam.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 int 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 struct pci_device_id snd_intel8x0m_ids[] = {
  191. { 0x8086, 0x2416, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801AA */
  192. { 0x8086, 0x2426, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82901AB */
  193. { 0x8086, 0x2446, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801BA */
  194. { 0x8086, 0x2486, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH3 */
  195. { 0x8086, 0x24c6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH4 */
  196. { 0x8086, 0x24d6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH5 */
  197. { 0x8086, 0x266d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH6 */
  198. { 0x8086, 0x27dd, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH7 */
  199. { 0x8086, 0x7196, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 440MX */
  200. { 0x1022, 0x7446, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD768 */
  201. { 0x1039, 0x7013, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_SIS }, /* SI7013 */
  202. { 0x10de, 0x01c1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE */
  203. { 0x10de, 0x0069, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE2 */
  204. { 0x10de, 0x0089, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE2s */
  205. { 0x10de, 0x00d9, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE3 */
  206. #if 0
  207. { 0x1022, 0x746d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD8111 */
  208. { 0x10b9, 0x5455, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 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. snd_assert(codec < 3, return ICH_PCR);
  264. return codec_bit[codec];
  265. }
  266. static int snd_intel8x0m_codec_semaphore(struct intel8x0m *chip, unsigned int codec)
  267. {
  268. int time;
  269. if (codec > 1)
  270. return -EIO;
  271. codec = get_ich_codec_bit(chip, codec);
  272. /* codec ready ? */
  273. if ((igetdword(chip, ICHREG(GLOB_STA)) & codec) == 0)
  274. return -EIO;
  275. /* Anyone holding a semaphore for 1 msec should be shot... */
  276. time = 100;
  277. do {
  278. if (!(igetbyte(chip, ICHREG(ACC_SEMA)) & ICH_CAS))
  279. return 0;
  280. udelay(10);
  281. } while (time--);
  282. /* access to some forbidden (non existant) ac97 registers will not
  283. * reset the semaphore. So even if you don't get the semaphore, still
  284. * continue the access. We don't need the semaphore anyway. */
  285. snd_printk(KERN_ERR "codec_semaphore: semaphore is not ready [0x%x][0x%x]\n",
  286. igetbyte(chip, ICHREG(ACC_SEMA)), igetdword(chip, ICHREG(GLOB_STA)));
  287. iagetword(chip, 0); /* clear semaphore flag */
  288. /* I don't care about the semaphore */
  289. return -EBUSY;
  290. }
  291. static void snd_intel8x0_codec_write(struct snd_ac97 *ac97,
  292. unsigned short reg,
  293. unsigned short val)
  294. {
  295. struct intel8x0m *chip = ac97->private_data;
  296. if (snd_intel8x0m_codec_semaphore(chip, ac97->num) < 0) {
  297. if (! chip->in_ac97_init)
  298. snd_printk(KERN_ERR "codec_write %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
  299. }
  300. iaputword(chip, reg + ac97->num * 0x80, val);
  301. }
  302. static unsigned short snd_intel8x0_codec_read(struct snd_ac97 *ac97,
  303. unsigned short reg)
  304. {
  305. struct intel8x0m *chip = ac97->private_data;
  306. unsigned short res;
  307. unsigned int tmp;
  308. if (snd_intel8x0m_codec_semaphore(chip, ac97->num) < 0) {
  309. if (! chip->in_ac97_init)
  310. snd_printk(KERN_ERR "codec_read %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
  311. res = 0xffff;
  312. } else {
  313. res = iagetword(chip, reg + ac97->num * 0x80);
  314. if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
  315. /* reset RCS and preserve other R/WC bits */
  316. iputdword(chip, ICHREG(GLOB_STA),
  317. tmp & ~(ICH_SRI|ICH_PRI|ICH_TRI|ICH_GSCI));
  318. if (! chip->in_ac97_init)
  319. snd_printk(KERN_ERR "codec_read %d: read timeout for register 0x%x\n", ac97->num, reg);
  320. res = 0xffff;
  321. }
  322. }
  323. if (reg == AC97_GPIO_STATUS)
  324. iagetword(chip, 0); /* clear semaphore */
  325. return res;
  326. }
  327. /*
  328. * DMA I/O
  329. */
  330. static void snd_intel8x0_setup_periods(struct intel8x0m *chip, struct ichdev *ichdev)
  331. {
  332. int idx;
  333. u32 *bdbar = ichdev->bdbar;
  334. unsigned long port = ichdev->reg_offset;
  335. iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
  336. if (ichdev->size == ichdev->fragsize) {
  337. ichdev->ack_reload = ichdev->ack = 2;
  338. ichdev->fragsize1 = ichdev->fragsize >> 1;
  339. for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 4) {
  340. bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf);
  341. bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
  342. ichdev->fragsize1 >> chip->pcm_pos_shift);
  343. bdbar[idx + 2] = cpu_to_le32(ichdev->physbuf + (ichdev->size >> 1));
  344. bdbar[idx + 3] = cpu_to_le32(0x80000000 | /* interrupt on completion */
  345. ichdev->fragsize1 >> chip->pcm_pos_shift);
  346. }
  347. ichdev->frags = 2;
  348. } else {
  349. ichdev->ack_reload = ichdev->ack = 1;
  350. ichdev->fragsize1 = ichdev->fragsize;
  351. for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 2) {
  352. bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf + (((idx >> 1) * ichdev->fragsize) % ichdev->size));
  353. bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
  354. ichdev->fragsize >> chip->pcm_pos_shift);
  355. // printk("bdbar[%i] = 0x%x [0x%x]\n", idx + 0, bdbar[idx + 0], bdbar[idx + 1]);
  356. }
  357. ichdev->frags = ichdev->size / ichdev->fragsize;
  358. }
  359. iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi = ICH_REG_LVI_MASK);
  360. ichdev->civ = 0;
  361. iputbyte(chip, port + ICH_REG_OFF_CIV, 0);
  362. ichdev->lvi_frag = ICH_REG_LVI_MASK % ichdev->frags;
  363. ichdev->position = 0;
  364. #if 0
  365. printk("lvi_frag = %i, frags = %i, period_size = 0x%x, period_size1 = 0x%x\n",
  366. ichdev->lvi_frag, ichdev->frags, ichdev->fragsize, ichdev->fragsize1);
  367. #endif
  368. /* clear interrupts */
  369. iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
  370. }
  371. /*
  372. * Interrupt handler
  373. */
  374. static inline void snd_intel8x0_update(struct intel8x0m *chip, struct ichdev *ichdev)
  375. {
  376. unsigned long port = ichdev->reg_offset;
  377. int civ, i, step;
  378. int ack = 0;
  379. civ = igetbyte(chip, port + ICH_REG_OFF_CIV);
  380. if (civ == ichdev->civ) {
  381. // snd_printd("civ same %d\n", civ);
  382. step = 1;
  383. ichdev->civ++;
  384. ichdev->civ &= ICH_REG_LVI_MASK;
  385. } else {
  386. step = civ - ichdev->civ;
  387. if (step < 0)
  388. step += ICH_REG_LVI_MASK + 1;
  389. // if (step != 1)
  390. // snd_printd("step = %d, %d -> %d\n", step, ichdev->civ, civ);
  391. ichdev->civ = civ;
  392. }
  393. ichdev->position += step * ichdev->fragsize1;
  394. ichdev->position %= ichdev->size;
  395. ichdev->lvi += step;
  396. ichdev->lvi &= ICH_REG_LVI_MASK;
  397. iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
  398. for (i = 0; i < step; i++) {
  399. ichdev->lvi_frag++;
  400. ichdev->lvi_frag %= ichdev->frags;
  401. ichdev->bdbar[ichdev->lvi * 2] = cpu_to_le32(ichdev->physbuf +
  402. ichdev->lvi_frag *
  403. ichdev->fragsize1);
  404. #if 0
  405. printk("new: bdbar[%i] = 0x%x [0x%x], prefetch = %i, all = 0x%x, 0x%x\n",
  406. ichdev->lvi * 2, ichdev->bdbar[ichdev->lvi * 2],
  407. ichdev->bdbar[ichdev->lvi * 2 + 1], inb(ICH_REG_OFF_PIV + port),
  408. inl(port + 4), inb(port + ICH_REG_OFF_CR));
  409. #endif
  410. if (--ichdev->ack == 0) {
  411. ichdev->ack = ichdev->ack_reload;
  412. ack = 1;
  413. }
  414. }
  415. if (ack && ichdev->substream) {
  416. spin_unlock(&chip->reg_lock);
  417. snd_pcm_period_elapsed(ichdev->substream);
  418. spin_lock(&chip->reg_lock);
  419. }
  420. iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
  421. }
  422. static irqreturn_t snd_intel8x0_interrupt(int irq, void *dev_id)
  423. {
  424. struct intel8x0m *chip = dev_id;
  425. struct ichdev *ichdev;
  426. unsigned int status;
  427. unsigned int i;
  428. spin_lock(&chip->reg_lock);
  429. status = igetdword(chip, chip->int_sta_reg);
  430. if (status == 0xffffffff) { /* we are not yet resumed */
  431. spin_unlock(&chip->reg_lock);
  432. return IRQ_NONE;
  433. }
  434. if ((status & chip->int_sta_mask) == 0) {
  435. if (status)
  436. iputdword(chip, chip->int_sta_reg, status);
  437. spin_unlock(&chip->reg_lock);
  438. return IRQ_NONE;
  439. }
  440. for (i = 0; i < chip->bdbars_count; i++) {
  441. ichdev = &chip->ichd[i];
  442. if (status & ichdev->int_sta_mask)
  443. snd_intel8x0_update(chip, ichdev);
  444. }
  445. /* ack them */
  446. iputdword(chip, chip->int_sta_reg, status & chip->int_sta_mask);
  447. spin_unlock(&chip->reg_lock);
  448. return IRQ_HANDLED;
  449. }
  450. /*
  451. * PCM part
  452. */
  453. static int snd_intel8x0_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  454. {
  455. struct intel8x0m *chip = snd_pcm_substream_chip(substream);
  456. struct ichdev *ichdev = get_ichdev(substream);
  457. unsigned char val = 0;
  458. unsigned long port = ichdev->reg_offset;
  459. switch (cmd) {
  460. case SNDRV_PCM_TRIGGER_START:
  461. case SNDRV_PCM_TRIGGER_RESUME:
  462. val = ICH_IOCE | ICH_STARTBM;
  463. break;
  464. case SNDRV_PCM_TRIGGER_STOP:
  465. case SNDRV_PCM_TRIGGER_SUSPEND:
  466. val = 0;
  467. break;
  468. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  469. val = ICH_IOCE;
  470. break;
  471. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  472. val = ICH_IOCE | ICH_STARTBM;
  473. break;
  474. default:
  475. return -EINVAL;
  476. }
  477. iputbyte(chip, port + ICH_REG_OFF_CR, val);
  478. if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  479. /* wait until DMA stopped */
  480. while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH)) ;
  481. /* reset whole DMA things */
  482. iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
  483. }
  484. return 0;
  485. }
  486. static int snd_intel8x0_hw_params(struct snd_pcm_substream *substream,
  487. struct snd_pcm_hw_params *hw_params)
  488. {
  489. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  490. }
  491. static int snd_intel8x0_hw_free(struct snd_pcm_substream *substream)
  492. {
  493. return snd_pcm_lib_free_pages(substream);
  494. }
  495. static snd_pcm_uframes_t snd_intel8x0_pcm_pointer(struct snd_pcm_substream *substream)
  496. {
  497. struct intel8x0m *chip = snd_pcm_substream_chip(substream);
  498. struct ichdev *ichdev = get_ichdev(substream);
  499. size_t ptr1, ptr;
  500. ptr1 = igetword(chip, ichdev->reg_offset + ichdev->roff_picb) << chip->pcm_pos_shift;
  501. if (ptr1 != 0)
  502. ptr = ichdev->fragsize1 - ptr1;
  503. else
  504. ptr = 0;
  505. ptr += ichdev->position;
  506. if (ptr >= ichdev->size)
  507. return 0;
  508. return bytes_to_frames(substream->runtime, ptr);
  509. }
  510. static int snd_intel8x0m_pcm_prepare(struct snd_pcm_substream *substream)
  511. {
  512. struct intel8x0m *chip = snd_pcm_substream_chip(substream);
  513. struct snd_pcm_runtime *runtime = substream->runtime;
  514. struct ichdev *ichdev = get_ichdev(substream);
  515. ichdev->physbuf = runtime->dma_addr;
  516. ichdev->size = snd_pcm_lib_buffer_bytes(substream);
  517. ichdev->fragsize = snd_pcm_lib_period_bytes(substream);
  518. snd_ac97_write(ichdev->ac97, AC97_LINE1_RATE, runtime->rate);
  519. snd_ac97_write(ichdev->ac97, AC97_LINE1_LEVEL, 0);
  520. snd_intel8x0_setup_periods(chip, ichdev);
  521. return 0;
  522. }
  523. static struct snd_pcm_hardware snd_intel8x0m_stream =
  524. {
  525. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  526. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  527. SNDRV_PCM_INFO_MMAP_VALID |
  528. SNDRV_PCM_INFO_PAUSE |
  529. SNDRV_PCM_INFO_RESUME),
  530. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  531. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
  532. .rate_min = 8000,
  533. .rate_max = 16000,
  534. .channels_min = 1,
  535. .channels_max = 1,
  536. .buffer_bytes_max = 64 * 1024,
  537. .period_bytes_min = 32,
  538. .period_bytes_max = 64 * 1024,
  539. .periods_min = 1,
  540. .periods_max = 1024,
  541. .fifo_size = 0,
  542. };
  543. static int snd_intel8x0m_pcm_open(struct snd_pcm_substream *substream, struct ichdev *ichdev)
  544. {
  545. static unsigned int rates[] = { 8000, 9600, 12000, 16000 };
  546. static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  547. .count = ARRAY_SIZE(rates),
  548. .list = rates,
  549. .mask = 0,
  550. };
  551. struct snd_pcm_runtime *runtime = substream->runtime;
  552. int err;
  553. ichdev->substream = substream;
  554. runtime->hw = snd_intel8x0m_stream;
  555. err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  556. &hw_constraints_rates);
  557. if ( err < 0 )
  558. return err;
  559. runtime->private_data = ichdev;
  560. return 0;
  561. }
  562. static int snd_intel8x0m_playback_open(struct snd_pcm_substream *substream)
  563. {
  564. struct intel8x0m *chip = snd_pcm_substream_chip(substream);
  565. return snd_intel8x0m_pcm_open(substream, &chip->ichd[ICHD_MDMOUT]);
  566. }
  567. static int snd_intel8x0m_playback_close(struct snd_pcm_substream *substream)
  568. {
  569. struct intel8x0m *chip = snd_pcm_substream_chip(substream);
  570. chip->ichd[ICHD_MDMOUT].substream = NULL;
  571. return 0;
  572. }
  573. static int snd_intel8x0m_capture_open(struct snd_pcm_substream *substream)
  574. {
  575. struct intel8x0m *chip = snd_pcm_substream_chip(substream);
  576. return snd_intel8x0m_pcm_open(substream, &chip->ichd[ICHD_MDMIN]);
  577. }
  578. static int snd_intel8x0m_capture_close(struct snd_pcm_substream *substream)
  579. {
  580. struct intel8x0m *chip = snd_pcm_substream_chip(substream);
  581. chip->ichd[ICHD_MDMIN].substream = NULL;
  582. return 0;
  583. }
  584. static struct snd_pcm_ops snd_intel8x0m_playback_ops = {
  585. .open = snd_intel8x0m_playback_open,
  586. .close = snd_intel8x0m_playback_close,
  587. .ioctl = snd_pcm_lib_ioctl,
  588. .hw_params = snd_intel8x0_hw_params,
  589. .hw_free = snd_intel8x0_hw_free,
  590. .prepare = snd_intel8x0m_pcm_prepare,
  591. .trigger = snd_intel8x0_pcm_trigger,
  592. .pointer = snd_intel8x0_pcm_pointer,
  593. };
  594. static struct snd_pcm_ops snd_intel8x0m_capture_ops = {
  595. .open = snd_intel8x0m_capture_open,
  596. .close = snd_intel8x0m_capture_close,
  597. .ioctl = snd_pcm_lib_ioctl,
  598. .hw_params = snd_intel8x0_hw_params,
  599. .hw_free = snd_intel8x0_hw_free,
  600. .prepare = snd_intel8x0m_pcm_prepare,
  601. .trigger = snd_intel8x0_pcm_trigger,
  602. .pointer = snd_intel8x0_pcm_pointer,
  603. };
  604. struct ich_pcm_table {
  605. char *suffix;
  606. struct snd_pcm_ops *playback_ops;
  607. struct snd_pcm_ops *capture_ops;
  608. size_t prealloc_size;
  609. size_t prealloc_max_size;
  610. int ac97_idx;
  611. };
  612. static int __devinit snd_intel8x0_pcm1(struct intel8x0m *chip, int device,
  613. struct ich_pcm_table *rec)
  614. {
  615. struct snd_pcm *pcm;
  616. int err;
  617. char name[32];
  618. if (rec->suffix)
  619. sprintf(name, "Intel ICH - %s", rec->suffix);
  620. else
  621. strcpy(name, "Intel ICH");
  622. err = snd_pcm_new(chip->card, name, device,
  623. rec->playback_ops ? 1 : 0,
  624. rec->capture_ops ? 1 : 0, &pcm);
  625. if (err < 0)
  626. return err;
  627. if (rec->playback_ops)
  628. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, rec->playback_ops);
  629. if (rec->capture_ops)
  630. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, rec->capture_ops);
  631. pcm->private_data = chip;
  632. pcm->info_flags = 0;
  633. pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
  634. if (rec->suffix)
  635. sprintf(pcm->name, "%s - %s", chip->card->shortname, rec->suffix);
  636. else
  637. strcpy(pcm->name, chip->card->shortname);
  638. chip->pcm[device] = pcm;
  639. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  640. snd_dma_pci_data(chip->pci),
  641. rec->prealloc_size,
  642. rec->prealloc_max_size);
  643. return 0;
  644. }
  645. static struct ich_pcm_table intel_pcms[] __devinitdata = {
  646. {
  647. .suffix = "Modem",
  648. .playback_ops = &snd_intel8x0m_playback_ops,
  649. .capture_ops = &snd_intel8x0m_capture_ops,
  650. .prealloc_size = 32 * 1024,
  651. .prealloc_max_size = 64 * 1024,
  652. },
  653. };
  654. static int __devinit snd_intel8x0_pcm(struct intel8x0m *chip)
  655. {
  656. int i, tblsize, device, err;
  657. struct ich_pcm_table *tbl, *rec;
  658. #if 1
  659. tbl = intel_pcms;
  660. tblsize = 1;
  661. #else
  662. switch (chip->device_type) {
  663. case DEVICE_NFORCE:
  664. tbl = nforce_pcms;
  665. tblsize = ARRAY_SIZE(nforce_pcms);
  666. break;
  667. case DEVICE_ALI:
  668. tbl = ali_pcms;
  669. tblsize = ARRAY_SIZE(ali_pcms);
  670. break;
  671. default:
  672. tbl = intel_pcms;
  673. tblsize = 2;
  674. break;
  675. }
  676. #endif
  677. device = 0;
  678. for (i = 0; i < tblsize; i++) {
  679. rec = tbl + i;
  680. if (i > 0 && rec->ac97_idx) {
  681. /* activate PCM only when associated AC'97 codec */
  682. if (! chip->ichd[rec->ac97_idx].ac97)
  683. continue;
  684. }
  685. err = snd_intel8x0_pcm1(chip, device, rec);
  686. if (err < 0)
  687. return err;
  688. device++;
  689. }
  690. chip->pcm_devs = device;
  691. return 0;
  692. }
  693. /*
  694. * Mixer part
  695. */
  696. static void snd_intel8x0_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
  697. {
  698. struct intel8x0m *chip = bus->private_data;
  699. chip->ac97_bus = NULL;
  700. }
  701. static void snd_intel8x0_mixer_free_ac97(struct snd_ac97 *ac97)
  702. {
  703. struct intel8x0m *chip = ac97->private_data;
  704. chip->ac97 = NULL;
  705. }
  706. static int __devinit snd_intel8x0_mixer(struct intel8x0m *chip, int ac97_clock)
  707. {
  708. struct snd_ac97_bus *pbus;
  709. struct snd_ac97_template ac97;
  710. struct snd_ac97 *x97;
  711. int err;
  712. unsigned int glob_sta = 0;
  713. static struct snd_ac97_bus_ops ops = {
  714. .write = snd_intel8x0_codec_write,
  715. .read = snd_intel8x0_codec_read,
  716. };
  717. chip->in_ac97_init = 1;
  718. memset(&ac97, 0, sizeof(ac97));
  719. ac97.private_data = chip;
  720. ac97.private_free = snd_intel8x0_mixer_free_ac97;
  721. ac97.scaps = AC97_SCAP_SKIP_AUDIO | AC97_SCAP_POWER_SAVE;
  722. glob_sta = igetdword(chip, ICHREG(GLOB_STA));
  723. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  724. goto __err;
  725. pbus->private_free = snd_intel8x0_mixer_free_ac97_bus;
  726. if (ac97_clock >= 8000 && ac97_clock <= 48000)
  727. pbus->clock = ac97_clock;
  728. chip->ac97_bus = pbus;
  729. ac97.pci = chip->pci;
  730. ac97.num = glob_sta & ICH_SCR ? 1 : 0;
  731. if ((err = snd_ac97_mixer(pbus, &ac97, &x97)) < 0) {
  732. snd_printk(KERN_ERR "Unable to initialize codec #%d\n", ac97.num);
  733. if (ac97.num == 0)
  734. goto __err;
  735. return err;
  736. }
  737. chip->ac97 = x97;
  738. if(ac97_is_modem(x97) && !chip->ichd[ICHD_MDMIN].ac97) {
  739. chip->ichd[ICHD_MDMIN].ac97 = x97;
  740. chip->ichd[ICHD_MDMOUT].ac97 = x97;
  741. }
  742. chip->in_ac97_init = 0;
  743. return 0;
  744. __err:
  745. /* clear the cold-reset bit for the next chance */
  746. if (chip->device_type != DEVICE_ALI)
  747. iputdword(chip, ICHREG(GLOB_CNT),
  748. igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_AC97COLD);
  749. return err;
  750. }
  751. /*
  752. *
  753. */
  754. static int snd_intel8x0m_ich_chip_init(struct intel8x0m *chip, int probing)
  755. {
  756. unsigned long end_time;
  757. unsigned int cnt, status, nstatus;
  758. /* put logic to right state */
  759. /* first clear status bits */
  760. status = ICH_RCS | ICH_MIINT | ICH_MOINT;
  761. cnt = igetdword(chip, ICHREG(GLOB_STA));
  762. iputdword(chip, ICHREG(GLOB_STA), cnt & status);
  763. /* ACLink on, 2 channels */
  764. cnt = igetdword(chip, ICHREG(GLOB_CNT));
  765. cnt &= ~(ICH_ACLINK);
  766. /* finish cold or do warm reset */
  767. cnt |= (cnt & ICH_AC97COLD) == 0 ? ICH_AC97COLD : ICH_AC97WARM;
  768. iputdword(chip, ICHREG(GLOB_CNT), cnt);
  769. end_time = (jiffies + (HZ / 4)) + 1;
  770. do {
  771. if ((igetdword(chip, ICHREG(GLOB_CNT)) & ICH_AC97WARM) == 0)
  772. goto __ok;
  773. schedule_timeout_uninterruptible(1);
  774. } while (time_after_eq(end_time, jiffies));
  775. snd_printk(KERN_ERR "AC'97 warm reset still in progress? [0x%x]\n",
  776. igetdword(chip, ICHREG(GLOB_CNT)));
  777. return -EIO;
  778. __ok:
  779. if (probing) {
  780. /* wait for any codec ready status.
  781. * Once it becomes ready it should remain ready
  782. * as long as we do not disable the ac97 link.
  783. */
  784. end_time = jiffies + HZ;
  785. do {
  786. status = igetdword(chip, ICHREG(GLOB_STA)) &
  787. (ICH_PCR | ICH_SCR | ICH_TCR);
  788. if (status)
  789. break;
  790. schedule_timeout_uninterruptible(1);
  791. } while (time_after_eq(end_time, jiffies));
  792. if (! status) {
  793. /* no codec is found */
  794. snd_printk(KERN_ERR "codec_ready: codec is not ready [0x%x]\n",
  795. igetdword(chip, ICHREG(GLOB_STA)));
  796. return -EIO;
  797. }
  798. /* up to two codecs (modem cannot be tertiary with ICH4) */
  799. nstatus = ICH_PCR | ICH_SCR;
  800. /* wait for other codecs ready status. */
  801. end_time = jiffies + HZ / 4;
  802. while (status != nstatus && time_after_eq(end_time, jiffies)) {
  803. schedule_timeout_uninterruptible(1);
  804. status |= igetdword(chip, ICHREG(GLOB_STA)) & nstatus;
  805. }
  806. } else {
  807. /* resume phase */
  808. status = 0;
  809. if (chip->ac97)
  810. status |= get_ich_codec_bit(chip, chip->ac97->num);
  811. /* wait until all the probed codecs are ready */
  812. end_time = jiffies + HZ;
  813. do {
  814. nstatus = igetdword(chip, ICHREG(GLOB_STA)) &
  815. (ICH_PCR | ICH_SCR | ICH_TCR);
  816. if (status == nstatus)
  817. break;
  818. schedule_timeout_uninterruptible(1);
  819. } while (time_after_eq(end_time, jiffies));
  820. }
  821. if (chip->device_type == DEVICE_SIS) {
  822. /* unmute the output on SIS7012 */
  823. iputword(chip, 0x4c, igetword(chip, 0x4c) | 1);
  824. }
  825. return 0;
  826. }
  827. static int snd_intel8x0_chip_init(struct intel8x0m *chip, int probing)
  828. {
  829. unsigned int i;
  830. int err;
  831. if ((err = snd_intel8x0m_ich_chip_init(chip, probing)) < 0)
  832. return err;
  833. iagetword(chip, 0); /* clear semaphore flag */
  834. /* disable interrupts */
  835. for (i = 0; i < chip->bdbars_count; i++)
  836. iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
  837. /* reset channels */
  838. for (i = 0; i < chip->bdbars_count; i++)
  839. iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
  840. /* initialize Buffer Descriptor Lists */
  841. for (i = 0; i < chip->bdbars_count; i++)
  842. iputdword(chip, ICH_REG_OFF_BDBAR + chip->ichd[i].reg_offset, chip->ichd[i].bdbar_addr);
  843. return 0;
  844. }
  845. static int snd_intel8x0_free(struct intel8x0m *chip)
  846. {
  847. unsigned int i;
  848. if (chip->irq < 0)
  849. goto __hw_end;
  850. /* disable interrupts */
  851. for (i = 0; i < chip->bdbars_count; i++)
  852. iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
  853. /* reset channels */
  854. for (i = 0; i < chip->bdbars_count; i++)
  855. iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
  856. __hw_end:
  857. if (chip->irq >= 0)
  858. free_irq(chip->irq, chip);
  859. if (chip->bdbars.area)
  860. snd_dma_free_pages(&chip->bdbars);
  861. if (chip->addr)
  862. pci_iounmap(chip->pci, chip->addr);
  863. if (chip->bmaddr)
  864. pci_iounmap(chip->pci, chip->bmaddr);
  865. pci_release_regions(chip->pci);
  866. pci_disable_device(chip->pci);
  867. kfree(chip);
  868. return 0;
  869. }
  870. #ifdef CONFIG_PM
  871. /*
  872. * power management
  873. */
  874. static int intel8x0m_suspend(struct pci_dev *pci, pm_message_t state)
  875. {
  876. struct snd_card *card = pci_get_drvdata(pci);
  877. struct intel8x0m *chip = card->private_data;
  878. int i;
  879. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  880. for (i = 0; i < chip->pcm_devs; i++)
  881. snd_pcm_suspend_all(chip->pcm[i]);
  882. snd_ac97_suspend(chip->ac97);
  883. if (chip->irq >= 0) {
  884. free_irq(chip->irq, chip);
  885. chip->irq = -1;
  886. }
  887. pci_disable_device(pci);
  888. pci_save_state(pci);
  889. pci_set_power_state(pci, pci_choose_state(pci, state));
  890. return 0;
  891. }
  892. static int intel8x0m_resume(struct pci_dev *pci)
  893. {
  894. struct snd_card *card = pci_get_drvdata(pci);
  895. struct intel8x0m *chip = card->private_data;
  896. pci_set_power_state(pci, PCI_D0);
  897. pci_restore_state(pci);
  898. if (pci_enable_device(pci) < 0) {
  899. printk(KERN_ERR "intel8x0m: pci_enable_device failed, "
  900. "disabling device\n");
  901. snd_card_disconnect(card);
  902. return -EIO;
  903. }
  904. pci_set_master(pci);
  905. if (request_irq(pci->irq, snd_intel8x0_interrupt,
  906. IRQF_SHARED, card->shortname, chip)) {
  907. printk(KERN_ERR "intel8x0m: unable to grab IRQ %d, "
  908. "disabling device\n", pci->irq);
  909. snd_card_disconnect(card);
  910. return -EIO;
  911. }
  912. chip->irq = pci->irq;
  913. snd_intel8x0_chip_init(chip, 0);
  914. snd_ac97_resume(chip->ac97);
  915. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  916. return 0;
  917. }
  918. #endif /* CONFIG_PM */
  919. #ifdef CONFIG_PROC_FS
  920. static void snd_intel8x0m_proc_read(struct snd_info_entry * entry,
  921. struct snd_info_buffer *buffer)
  922. {
  923. struct intel8x0m *chip = entry->private_data;
  924. unsigned int tmp;
  925. snd_iprintf(buffer, "Intel8x0m\n\n");
  926. if (chip->device_type == DEVICE_ALI)
  927. return;
  928. tmp = igetdword(chip, ICHREG(GLOB_STA));
  929. snd_iprintf(buffer, "Global control : 0x%08x\n",
  930. igetdword(chip, ICHREG(GLOB_CNT)));
  931. snd_iprintf(buffer, "Global status : 0x%08x\n", tmp);
  932. snd_iprintf(buffer, "AC'97 codecs ready :%s%s%s%s\n",
  933. tmp & ICH_PCR ? " primary" : "",
  934. tmp & ICH_SCR ? " secondary" : "",
  935. tmp & ICH_TCR ? " tertiary" : "",
  936. (tmp & (ICH_PCR | ICH_SCR | ICH_TCR)) == 0 ? " none" : "");
  937. }
  938. static void __devinit snd_intel8x0m_proc_init(struct intel8x0m * chip)
  939. {
  940. struct snd_info_entry *entry;
  941. if (! snd_card_proc_new(chip->card, "intel8x0m", &entry))
  942. snd_info_set_text_ops(entry, chip, snd_intel8x0m_proc_read);
  943. }
  944. #else /* !CONFIG_PROC_FS */
  945. #define snd_intel8x0m_proc_init(chip)
  946. #endif /* CONFIG_PROC_FS */
  947. static int snd_intel8x0_dev_free(struct snd_device *device)
  948. {
  949. struct intel8x0m *chip = device->device_data;
  950. return snd_intel8x0_free(chip);
  951. }
  952. struct ich_reg_info {
  953. unsigned int int_sta_mask;
  954. unsigned int offset;
  955. };
  956. static int __devinit snd_intel8x0m_create(struct snd_card *card,
  957. struct pci_dev *pci,
  958. unsigned long device_type,
  959. struct intel8x0m ** r_intel8x0)
  960. {
  961. struct intel8x0m *chip;
  962. int err;
  963. unsigned int i;
  964. unsigned int int_sta_masks;
  965. struct ichdev *ichdev;
  966. static struct snd_device_ops ops = {
  967. .dev_free = snd_intel8x0_dev_free,
  968. };
  969. static struct ich_reg_info intel_regs[2] = {
  970. { ICH_MIINT, 0 },
  971. { ICH_MOINT, 0x10 },
  972. };
  973. struct ich_reg_info *tbl;
  974. *r_intel8x0 = NULL;
  975. if ((err = pci_enable_device(pci)) < 0)
  976. return err;
  977. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  978. if (chip == NULL) {
  979. pci_disable_device(pci);
  980. return -ENOMEM;
  981. }
  982. spin_lock_init(&chip->reg_lock);
  983. chip->device_type = device_type;
  984. chip->card = card;
  985. chip->pci = pci;
  986. chip->irq = -1;
  987. if ((err = pci_request_regions(pci, card->shortname)) < 0) {
  988. kfree(chip);
  989. pci_disable_device(pci);
  990. return err;
  991. }
  992. if (device_type == DEVICE_ALI) {
  993. /* ALI5455 has no ac97 region */
  994. chip->bmaddr = pci_iomap(pci, 0, 0);
  995. goto port_inited;
  996. }
  997. if (pci_resource_flags(pci, 2) & IORESOURCE_MEM) /* ICH4 and Nforce */
  998. chip->addr = pci_iomap(pci, 2, 0);
  999. else
  1000. chip->addr = pci_iomap(pci, 0, 0);
  1001. if (!chip->addr) {
  1002. snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
  1003. snd_intel8x0_free(chip);
  1004. return -EIO;
  1005. }
  1006. if (pci_resource_flags(pci, 3) & IORESOURCE_MEM) /* ICH4 */
  1007. chip->bmaddr = pci_iomap(pci, 3, 0);
  1008. else
  1009. chip->bmaddr = pci_iomap(pci, 1, 0);
  1010. if (!chip->bmaddr) {
  1011. snd_printk(KERN_ERR "Controller space ioremap problem\n");
  1012. snd_intel8x0_free(chip);
  1013. return -EIO;
  1014. }
  1015. port_inited:
  1016. if (request_irq(pci->irq, snd_intel8x0_interrupt, IRQF_SHARED,
  1017. card->shortname, chip)) {
  1018. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1019. snd_intel8x0_free(chip);
  1020. return -EBUSY;
  1021. }
  1022. chip->irq = pci->irq;
  1023. pci_set_master(pci);
  1024. synchronize_irq(chip->irq);
  1025. /* initialize offsets */
  1026. chip->bdbars_count = 2;
  1027. tbl = intel_regs;
  1028. for (i = 0; i < chip->bdbars_count; i++) {
  1029. ichdev = &chip->ichd[i];
  1030. ichdev->ichd = i;
  1031. ichdev->reg_offset = tbl[i].offset;
  1032. ichdev->int_sta_mask = tbl[i].int_sta_mask;
  1033. if (device_type == DEVICE_SIS) {
  1034. /* SiS 7013 swaps the registers */
  1035. ichdev->roff_sr = ICH_REG_OFF_PICB;
  1036. ichdev->roff_picb = ICH_REG_OFF_SR;
  1037. } else {
  1038. ichdev->roff_sr = ICH_REG_OFF_SR;
  1039. ichdev->roff_picb = ICH_REG_OFF_PICB;
  1040. }
  1041. if (device_type == DEVICE_ALI)
  1042. ichdev->ali_slot = (ichdev->reg_offset - 0x40) / 0x10;
  1043. }
  1044. /* SIS7013 handles the pcm data in bytes, others are in words */
  1045. chip->pcm_pos_shift = (device_type == DEVICE_SIS) ? 0 : 1;
  1046. /* allocate buffer descriptor lists */
  1047. /* the start of each lists must be aligned to 8 bytes */
  1048. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1049. chip->bdbars_count * sizeof(u32) * ICH_MAX_FRAGS * 2,
  1050. &chip->bdbars) < 0) {
  1051. snd_intel8x0_free(chip);
  1052. return -ENOMEM;
  1053. }
  1054. /* tables must be aligned to 8 bytes here, but the kernel pages
  1055. are much bigger, so we don't care (on i386) */
  1056. int_sta_masks = 0;
  1057. for (i = 0; i < chip->bdbars_count; i++) {
  1058. ichdev = &chip->ichd[i];
  1059. ichdev->bdbar = ((u32 *)chip->bdbars.area) + (i * ICH_MAX_FRAGS * 2);
  1060. ichdev->bdbar_addr = chip->bdbars.addr + (i * sizeof(u32) * ICH_MAX_FRAGS * 2);
  1061. int_sta_masks |= ichdev->int_sta_mask;
  1062. }
  1063. chip->int_sta_reg = ICH_REG_GLOB_STA;
  1064. chip->int_sta_mask = int_sta_masks;
  1065. if ((err = snd_intel8x0_chip_init(chip, 1)) < 0) {
  1066. snd_intel8x0_free(chip);
  1067. return err;
  1068. }
  1069. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1070. snd_intel8x0_free(chip);
  1071. return err;
  1072. }
  1073. snd_card_set_dev(card, &pci->dev);
  1074. *r_intel8x0 = chip;
  1075. return 0;
  1076. }
  1077. static struct shortname_table {
  1078. unsigned int id;
  1079. const char *s;
  1080. } shortnames[] __devinitdata = {
  1081. { PCI_DEVICE_ID_INTEL_82801AA_6, "Intel 82801AA-ICH" },
  1082. { PCI_DEVICE_ID_INTEL_82801AB_6, "Intel 82901AB-ICH0" },
  1083. { PCI_DEVICE_ID_INTEL_82801BA_6, "Intel 82801BA-ICH2" },
  1084. { PCI_DEVICE_ID_INTEL_440MX_6, "Intel 440MX" },
  1085. { PCI_DEVICE_ID_INTEL_82801CA_6, "Intel 82801CA-ICH3" },
  1086. { PCI_DEVICE_ID_INTEL_82801DB_6, "Intel 82801DB-ICH4" },
  1087. { PCI_DEVICE_ID_INTEL_82801EB_6, "Intel ICH5" },
  1088. { PCI_DEVICE_ID_INTEL_ICH6_17, "Intel ICH6" },
  1089. { PCI_DEVICE_ID_INTEL_ICH7_19, "Intel ICH7" },
  1090. { 0x7446, "AMD AMD768" },
  1091. { PCI_DEVICE_ID_SI_7013, "SiS SI7013" },
  1092. { PCI_DEVICE_ID_NVIDIA_MCP1_MODEM, "NVidia nForce" },
  1093. { PCI_DEVICE_ID_NVIDIA_MCP2_MODEM, "NVidia nForce2" },
  1094. { PCI_DEVICE_ID_NVIDIA_MCP2S_MODEM, "NVidia nForce2s" },
  1095. { PCI_DEVICE_ID_NVIDIA_MCP3_MODEM, "NVidia nForce3" },
  1096. #if 0
  1097. { 0x5455, "ALi M5455" },
  1098. { 0x746d, "AMD AMD8111" },
  1099. #endif
  1100. { 0 },
  1101. };
  1102. static int __devinit snd_intel8x0m_probe(struct pci_dev *pci,
  1103. const struct pci_device_id *pci_id)
  1104. {
  1105. struct snd_card *card;
  1106. struct intel8x0m *chip;
  1107. int err;
  1108. struct shortname_table *name;
  1109. card = snd_card_new(index, id, THIS_MODULE, 0);
  1110. if (card == NULL)
  1111. return -ENOMEM;
  1112. strcpy(card->driver, "ICH-MODEM");
  1113. strcpy(card->shortname, "Intel ICH");
  1114. for (name = shortnames; name->id; name++) {
  1115. if (pci->device == name->id) {
  1116. strcpy(card->shortname, name->s);
  1117. break;
  1118. }
  1119. }
  1120. strcat(card->shortname," Modem");
  1121. if ((err = snd_intel8x0m_create(card, pci, pci_id->driver_data, &chip)) < 0) {
  1122. snd_card_free(card);
  1123. return err;
  1124. }
  1125. card->private_data = chip;
  1126. if ((err = snd_intel8x0_mixer(chip, ac97_clock)) < 0) {
  1127. snd_card_free(card);
  1128. return err;
  1129. }
  1130. if ((err = snd_intel8x0_pcm(chip)) < 0) {
  1131. snd_card_free(card);
  1132. return err;
  1133. }
  1134. snd_intel8x0m_proc_init(chip);
  1135. sprintf(card->longname, "%s at irq %i",
  1136. card->shortname, chip->irq);
  1137. if ((err = snd_card_register(card)) < 0) {
  1138. snd_card_free(card);
  1139. return err;
  1140. }
  1141. pci_set_drvdata(pci, card);
  1142. return 0;
  1143. }
  1144. static void __devexit snd_intel8x0m_remove(struct pci_dev *pci)
  1145. {
  1146. snd_card_free(pci_get_drvdata(pci));
  1147. pci_set_drvdata(pci, NULL);
  1148. }
  1149. static struct pci_driver driver = {
  1150. .name = "Intel ICH Modem",
  1151. .id_table = snd_intel8x0m_ids,
  1152. .probe = snd_intel8x0m_probe,
  1153. .remove = __devexit_p(snd_intel8x0m_remove),
  1154. #ifdef CONFIG_PM
  1155. .suspend = intel8x0m_suspend,
  1156. .resume = intel8x0m_resume,
  1157. #endif
  1158. };
  1159. static int __init alsa_card_intel8x0m_init(void)
  1160. {
  1161. return pci_register_driver(&driver);
  1162. }
  1163. static void __exit alsa_card_intel8x0m_exit(void)
  1164. {
  1165. pci_unregister_driver(&driver);
  1166. }
  1167. module_init(alsa_card_intel8x0m_init)
  1168. module_exit(alsa_card_intel8x0m_exit)