intel8x0m.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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 const 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 const unsigned int rates[] = { 8000, 9600, 12000, 16000 };
  560. static const 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 const 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 const 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. const struct snd_pcm_ops *playback_ops;
  621. const 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. /* initialize offsets */
  1023. chip->bdbars_count = 2;
  1024. tbl = intel_regs;
  1025. for (i = 0; i < chip->bdbars_count; i++) {
  1026. ichdev = &chip->ichd[i];
  1027. ichdev->ichd = i;
  1028. ichdev->reg_offset = tbl[i].offset;
  1029. ichdev->int_sta_mask = tbl[i].int_sta_mask;
  1030. if (device_type == DEVICE_SIS) {
  1031. /* SiS 7013 swaps the registers */
  1032. ichdev->roff_sr = ICH_REG_OFF_PICB;
  1033. ichdev->roff_picb = ICH_REG_OFF_SR;
  1034. } else {
  1035. ichdev->roff_sr = ICH_REG_OFF_SR;
  1036. ichdev->roff_picb = ICH_REG_OFF_PICB;
  1037. }
  1038. if (device_type == DEVICE_ALI)
  1039. ichdev->ali_slot = (ichdev->reg_offset - 0x40) / 0x10;
  1040. }
  1041. /* SIS7013 handles the pcm data in bytes, others are in words */
  1042. chip->pcm_pos_shift = (device_type == DEVICE_SIS) ? 0 : 1;
  1043. /* allocate buffer descriptor lists */
  1044. /* the start of each lists must be aligned to 8 bytes */
  1045. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1046. chip->bdbars_count * sizeof(u32) * ICH_MAX_FRAGS * 2,
  1047. &chip->bdbars) < 0) {
  1048. snd_intel8x0m_free(chip);
  1049. return -ENOMEM;
  1050. }
  1051. /* tables must be aligned to 8 bytes here, but the kernel pages
  1052. are much bigger, so we don't care (on i386) */
  1053. int_sta_masks = 0;
  1054. for (i = 0; i < chip->bdbars_count; i++) {
  1055. ichdev = &chip->ichd[i];
  1056. ichdev->bdbar = ((u32 *)chip->bdbars.area) + (i * ICH_MAX_FRAGS * 2);
  1057. ichdev->bdbar_addr = chip->bdbars.addr + (i * sizeof(u32) * ICH_MAX_FRAGS * 2);
  1058. int_sta_masks |= ichdev->int_sta_mask;
  1059. }
  1060. chip->int_sta_reg = ICH_REG_GLOB_STA;
  1061. chip->int_sta_mask = int_sta_masks;
  1062. pci_set_master(pci);
  1063. if ((err = snd_intel8x0m_chip_init(chip, 1)) < 0) {
  1064. snd_intel8x0m_free(chip);
  1065. return err;
  1066. }
  1067. if (request_irq(pci->irq, snd_intel8x0m_interrupt, IRQF_SHARED,
  1068. KBUILD_MODNAME, chip)) {
  1069. dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
  1070. snd_intel8x0m_free(chip);
  1071. return -EBUSY;
  1072. }
  1073. chip->irq = pci->irq;
  1074. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1075. snd_intel8x0m_free(chip);
  1076. return err;
  1077. }
  1078. *r_intel8x0m = chip;
  1079. return 0;
  1080. }
  1081. static struct shortname_table {
  1082. unsigned int id;
  1083. const char *s;
  1084. } shortnames[] = {
  1085. { PCI_DEVICE_ID_INTEL_82801AA_6, "Intel 82801AA-ICH" },
  1086. { PCI_DEVICE_ID_INTEL_82801AB_6, "Intel 82901AB-ICH0" },
  1087. { PCI_DEVICE_ID_INTEL_82801BA_6, "Intel 82801BA-ICH2" },
  1088. { PCI_DEVICE_ID_INTEL_440MX_6, "Intel 440MX" },
  1089. { PCI_DEVICE_ID_INTEL_82801CA_6, "Intel 82801CA-ICH3" },
  1090. { PCI_DEVICE_ID_INTEL_82801DB_6, "Intel 82801DB-ICH4" },
  1091. { PCI_DEVICE_ID_INTEL_82801EB_6, "Intel ICH5" },
  1092. { PCI_DEVICE_ID_INTEL_ICH6_17, "Intel ICH6" },
  1093. { PCI_DEVICE_ID_INTEL_ICH7_19, "Intel ICH7" },
  1094. { 0x7446, "AMD AMD768" },
  1095. { PCI_DEVICE_ID_SI_7013, "SiS SI7013" },
  1096. { PCI_DEVICE_ID_NVIDIA_MCP1_MODEM, "NVidia nForce" },
  1097. { PCI_DEVICE_ID_NVIDIA_MCP2_MODEM, "NVidia nForce2" },
  1098. { PCI_DEVICE_ID_NVIDIA_MCP2S_MODEM, "NVidia nForce2s" },
  1099. { PCI_DEVICE_ID_NVIDIA_MCP3_MODEM, "NVidia nForce3" },
  1100. { 0x746e, "AMD AMD8111" },
  1101. #if 0
  1102. { 0x5455, "ALi M5455" },
  1103. #endif
  1104. { 0 },
  1105. };
  1106. static int snd_intel8x0m_probe(struct pci_dev *pci,
  1107. const struct pci_device_id *pci_id)
  1108. {
  1109. struct snd_card *card;
  1110. struct intel8x0m *chip;
  1111. int err;
  1112. struct shortname_table *name;
  1113. err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card);
  1114. if (err < 0)
  1115. return err;
  1116. strcpy(card->driver, "ICH-MODEM");
  1117. strcpy(card->shortname, "Intel ICH");
  1118. for (name = shortnames; name->id; name++) {
  1119. if (pci->device == name->id) {
  1120. strcpy(card->shortname, name->s);
  1121. break;
  1122. }
  1123. }
  1124. strcat(card->shortname," Modem");
  1125. if ((err = snd_intel8x0m_create(card, pci, pci_id->driver_data, &chip)) < 0) {
  1126. snd_card_free(card);
  1127. return err;
  1128. }
  1129. card->private_data = chip;
  1130. if ((err = snd_intel8x0m_mixer(chip, ac97_clock)) < 0) {
  1131. snd_card_free(card);
  1132. return err;
  1133. }
  1134. if ((err = snd_intel8x0m_pcm(chip)) < 0) {
  1135. snd_card_free(card);
  1136. return err;
  1137. }
  1138. snd_intel8x0m_proc_init(chip);
  1139. sprintf(card->longname, "%s at irq %i",
  1140. card->shortname, chip->irq);
  1141. if ((err = snd_card_register(card)) < 0) {
  1142. snd_card_free(card);
  1143. return err;
  1144. }
  1145. pci_set_drvdata(pci, card);
  1146. return 0;
  1147. }
  1148. static void snd_intel8x0m_remove(struct pci_dev *pci)
  1149. {
  1150. snd_card_free(pci_get_drvdata(pci));
  1151. }
  1152. static struct pci_driver intel8x0m_driver = {
  1153. .name = KBUILD_MODNAME,
  1154. .id_table = snd_intel8x0m_ids,
  1155. .probe = snd_intel8x0m_probe,
  1156. .remove = snd_intel8x0m_remove,
  1157. .driver = {
  1158. .pm = INTEL8X0M_PM_OPS,
  1159. },
  1160. };
  1161. module_pci_driver(intel8x0m_driver);