intel8x0m.c 41 KB

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