intel8x0m.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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. if (reg == AC97_GPIO_STATUS)
  443. iagetword(chip, 0); /* clear semaphore */
  444. return res;
  445. }
  446. /*
  447. * DMA I/O
  448. */
  449. static void snd_intel8x0_setup_periods(intel8x0_t *chip, ichdev_t *ichdev)
  450. {
  451. int idx;
  452. u32 *bdbar = ichdev->bdbar;
  453. unsigned long port = ichdev->reg_offset;
  454. iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
  455. if (ichdev->size == ichdev->fragsize) {
  456. ichdev->ack_reload = ichdev->ack = 2;
  457. ichdev->fragsize1 = ichdev->fragsize >> 1;
  458. for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 4) {
  459. bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf);
  460. bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
  461. ichdev->fragsize1 >> chip->pcm_pos_shift);
  462. bdbar[idx + 2] = cpu_to_le32(ichdev->physbuf + (ichdev->size >> 1));
  463. bdbar[idx + 3] = cpu_to_le32(0x80000000 | /* interrupt on completion */
  464. ichdev->fragsize1 >> chip->pcm_pos_shift);
  465. }
  466. ichdev->frags = 2;
  467. } else {
  468. ichdev->ack_reload = ichdev->ack = 1;
  469. ichdev->fragsize1 = ichdev->fragsize;
  470. for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 2) {
  471. bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf + (((idx >> 1) * ichdev->fragsize) % ichdev->size));
  472. bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
  473. ichdev->fragsize >> chip->pcm_pos_shift);
  474. // printk("bdbar[%i] = 0x%x [0x%x]\n", idx + 0, bdbar[idx + 0], bdbar[idx + 1]);
  475. }
  476. ichdev->frags = ichdev->size / ichdev->fragsize;
  477. }
  478. iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi = ICH_REG_LVI_MASK);
  479. ichdev->civ = 0;
  480. iputbyte(chip, port + ICH_REG_OFF_CIV, 0);
  481. ichdev->lvi_frag = ICH_REG_LVI_MASK % ichdev->frags;
  482. ichdev->position = 0;
  483. #if 0
  484. printk("lvi_frag = %i, frags = %i, period_size = 0x%x, period_size1 = 0x%x\n",
  485. ichdev->lvi_frag, ichdev->frags, ichdev->fragsize, ichdev->fragsize1);
  486. #endif
  487. /* clear interrupts */
  488. iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
  489. }
  490. /*
  491. * Interrupt handler
  492. */
  493. static inline void snd_intel8x0_update(intel8x0_t *chip, ichdev_t *ichdev)
  494. {
  495. unsigned long port = ichdev->reg_offset;
  496. int civ, i, step;
  497. int ack = 0;
  498. civ = igetbyte(chip, port + ICH_REG_OFF_CIV);
  499. if (civ == ichdev->civ) {
  500. // snd_printd("civ same %d\n", civ);
  501. step = 1;
  502. ichdev->civ++;
  503. ichdev->civ &= ICH_REG_LVI_MASK;
  504. } else {
  505. step = civ - ichdev->civ;
  506. if (step < 0)
  507. step += ICH_REG_LVI_MASK + 1;
  508. // if (step != 1)
  509. // snd_printd("step = %d, %d -> %d\n", step, ichdev->civ, civ);
  510. ichdev->civ = civ;
  511. }
  512. ichdev->position += step * ichdev->fragsize1;
  513. ichdev->position %= ichdev->size;
  514. ichdev->lvi += step;
  515. ichdev->lvi &= ICH_REG_LVI_MASK;
  516. iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
  517. for (i = 0; i < step; i++) {
  518. ichdev->lvi_frag++;
  519. ichdev->lvi_frag %= ichdev->frags;
  520. ichdev->bdbar[ichdev->lvi * 2] = cpu_to_le32(ichdev->physbuf + ichdev->lvi_frag * ichdev->fragsize1);
  521. // 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));
  522. if (--ichdev->ack == 0) {
  523. ichdev->ack = ichdev->ack_reload;
  524. ack = 1;
  525. }
  526. }
  527. if (ack && ichdev->substream) {
  528. spin_unlock(&chip->reg_lock);
  529. snd_pcm_period_elapsed(ichdev->substream);
  530. spin_lock(&chip->reg_lock);
  531. }
  532. iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
  533. }
  534. static irqreturn_t snd_intel8x0_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  535. {
  536. intel8x0_t *chip = dev_id;
  537. ichdev_t *ichdev;
  538. unsigned int status;
  539. unsigned int i;
  540. spin_lock(&chip->reg_lock);
  541. status = igetdword(chip, chip->int_sta_reg);
  542. if (status == 0xffffffff) { /* we are not yet resumed */
  543. spin_unlock(&chip->reg_lock);
  544. return IRQ_NONE;
  545. }
  546. if ((status & chip->int_sta_mask) == 0) {
  547. if (status)
  548. iputdword(chip, chip->int_sta_reg, status);
  549. spin_unlock(&chip->reg_lock);
  550. return IRQ_NONE;
  551. }
  552. for (i = 0; i < chip->bdbars_count; i++) {
  553. ichdev = &chip->ichd[i];
  554. if (status & ichdev->int_sta_mask)
  555. snd_intel8x0_update(chip, ichdev);
  556. }
  557. /* ack them */
  558. iputdword(chip, chip->int_sta_reg, status & chip->int_sta_mask);
  559. spin_unlock(&chip->reg_lock);
  560. return IRQ_HANDLED;
  561. }
  562. /*
  563. * PCM part
  564. */
  565. static int snd_intel8x0_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
  566. {
  567. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  568. ichdev_t *ichdev = get_ichdev(substream);
  569. unsigned char val = 0;
  570. unsigned long port = ichdev->reg_offset;
  571. switch (cmd) {
  572. case SNDRV_PCM_TRIGGER_START:
  573. case SNDRV_PCM_TRIGGER_RESUME:
  574. val = ICH_IOCE | ICH_STARTBM;
  575. break;
  576. case SNDRV_PCM_TRIGGER_STOP:
  577. case SNDRV_PCM_TRIGGER_SUSPEND:
  578. val = 0;
  579. break;
  580. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  581. val = ICH_IOCE;
  582. break;
  583. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  584. val = ICH_IOCE | ICH_STARTBM;
  585. break;
  586. default:
  587. return -EINVAL;
  588. }
  589. iputbyte(chip, port + ICH_REG_OFF_CR, val);
  590. if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  591. /* wait until DMA stopped */
  592. while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH)) ;
  593. /* reset whole DMA things */
  594. iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
  595. }
  596. return 0;
  597. }
  598. static int snd_intel8x0_hw_params(snd_pcm_substream_t * substream,
  599. snd_pcm_hw_params_t * hw_params)
  600. {
  601. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  602. }
  603. static int snd_intel8x0_hw_free(snd_pcm_substream_t * substream)
  604. {
  605. return snd_pcm_lib_free_pages(substream);
  606. }
  607. static snd_pcm_uframes_t snd_intel8x0_pcm_pointer(snd_pcm_substream_t * substream)
  608. {
  609. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  610. ichdev_t *ichdev = get_ichdev(substream);
  611. size_t ptr1, ptr;
  612. ptr1 = igetword(chip, ichdev->reg_offset + ichdev->roff_picb) << chip->pcm_pos_shift;
  613. if (ptr1 != 0)
  614. ptr = ichdev->fragsize1 - ptr1;
  615. else
  616. ptr = 0;
  617. ptr += ichdev->position;
  618. if (ptr >= ichdev->size)
  619. return 0;
  620. return bytes_to_frames(substream->runtime, ptr);
  621. }
  622. static int snd_intel8x0m_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
  623. {
  624. /* hook off/on on start/stop */
  625. /* Moved this to mixer control */
  626. switch (cmd) {
  627. case SNDRV_PCM_TRIGGER_START:
  628. break;
  629. case SNDRV_PCM_TRIGGER_STOP:
  630. break;
  631. default:
  632. return -EINVAL;
  633. }
  634. return snd_intel8x0_pcm_trigger(substream,cmd);
  635. }
  636. static int snd_intel8x0m_pcm_prepare(snd_pcm_substream_t * substream)
  637. {
  638. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  639. snd_pcm_runtime_t *runtime = substream->runtime;
  640. ichdev_t *ichdev = get_ichdev(substream);
  641. ichdev->physbuf = runtime->dma_addr;
  642. ichdev->size = snd_pcm_lib_buffer_bytes(substream);
  643. ichdev->fragsize = snd_pcm_lib_period_bytes(substream);
  644. snd_ac97_write(ichdev->ac97, AC97_LINE1_RATE, runtime->rate);
  645. snd_ac97_write(ichdev->ac97, AC97_LINE1_LEVEL, 0);
  646. snd_intel8x0_setup_periods(chip, ichdev);
  647. return 0;
  648. }
  649. static snd_pcm_hardware_t snd_intel8x0m_stream =
  650. {
  651. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  652. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  653. SNDRV_PCM_INFO_MMAP_VALID |
  654. SNDRV_PCM_INFO_PAUSE |
  655. SNDRV_PCM_INFO_RESUME),
  656. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  657. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
  658. .rate_min = 8000,
  659. .rate_max = 16000,
  660. .channels_min = 1,
  661. .channels_max = 1,
  662. .buffer_bytes_max = 64 * 1024,
  663. .period_bytes_min = 32,
  664. .period_bytes_max = 64 * 1024,
  665. .periods_min = 1,
  666. .periods_max = 1024,
  667. .fifo_size = 0,
  668. };
  669. static int snd_intel8x0m_pcm_open(snd_pcm_substream_t * substream, ichdev_t *ichdev)
  670. {
  671. static unsigned int rates[] = { 8000, 9600, 12000, 16000 };
  672. static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
  673. .count = ARRAY_SIZE(rates),
  674. .list = rates,
  675. .mask = 0,
  676. };
  677. snd_pcm_runtime_t *runtime = substream->runtime;
  678. int err;
  679. ichdev->substream = substream;
  680. runtime->hw = snd_intel8x0m_stream;
  681. err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  682. if ( err < 0 )
  683. return err;
  684. runtime->private_data = ichdev;
  685. return 0;
  686. }
  687. static int snd_intel8x0m_playback_open(snd_pcm_substream_t * substream)
  688. {
  689. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  690. return snd_intel8x0m_pcm_open(substream, &chip->ichd[ICHD_MDMOUT]);
  691. }
  692. static int snd_intel8x0m_playback_close(snd_pcm_substream_t * substream)
  693. {
  694. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  695. chip->ichd[ICHD_MDMOUT].substream = NULL;
  696. return 0;
  697. }
  698. static int snd_intel8x0m_capture_open(snd_pcm_substream_t * substream)
  699. {
  700. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  701. return snd_intel8x0m_pcm_open(substream, &chip->ichd[ICHD_MDMIN]);
  702. }
  703. static int snd_intel8x0m_capture_close(snd_pcm_substream_t * substream)
  704. {
  705. intel8x0_t *chip = snd_pcm_substream_chip(substream);
  706. chip->ichd[ICHD_MDMIN].substream = NULL;
  707. return 0;
  708. }
  709. static snd_pcm_ops_t snd_intel8x0m_playback_ops = {
  710. .open = snd_intel8x0m_playback_open,
  711. .close = snd_intel8x0m_playback_close,
  712. .ioctl = snd_pcm_lib_ioctl,
  713. .hw_params = snd_intel8x0_hw_params,
  714. .hw_free = snd_intel8x0_hw_free,
  715. .prepare = snd_intel8x0m_pcm_prepare,
  716. .trigger = snd_intel8x0m_pcm_trigger,
  717. .pointer = snd_intel8x0_pcm_pointer,
  718. };
  719. static snd_pcm_ops_t snd_intel8x0m_capture_ops = {
  720. .open = snd_intel8x0m_capture_open,
  721. .close = snd_intel8x0m_capture_close,
  722. .ioctl = snd_pcm_lib_ioctl,
  723. .hw_params = snd_intel8x0_hw_params,
  724. .hw_free = snd_intel8x0_hw_free,
  725. .prepare = snd_intel8x0m_pcm_prepare,
  726. .trigger = snd_intel8x0m_pcm_trigger,
  727. .pointer = snd_intel8x0_pcm_pointer,
  728. };
  729. struct ich_pcm_table {
  730. char *suffix;
  731. snd_pcm_ops_t *playback_ops;
  732. snd_pcm_ops_t *capture_ops;
  733. size_t prealloc_size;
  734. size_t prealloc_max_size;
  735. int ac97_idx;
  736. };
  737. static int __devinit snd_intel8x0_pcm1(intel8x0_t *chip, int device, struct ich_pcm_table *rec)
  738. {
  739. snd_pcm_t *pcm;
  740. int err;
  741. char name[32];
  742. if (rec->suffix)
  743. sprintf(name, "Intel ICH - %s", rec->suffix);
  744. else
  745. strcpy(name, "Intel ICH");
  746. err = snd_pcm_new(chip->card, name, device,
  747. rec->playback_ops ? 1 : 0,
  748. rec->capture_ops ? 1 : 0, &pcm);
  749. if (err < 0)
  750. return err;
  751. if (rec->playback_ops)
  752. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, rec->playback_ops);
  753. if (rec->capture_ops)
  754. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, rec->capture_ops);
  755. pcm->private_data = chip;
  756. pcm->info_flags = 0;
  757. if (rec->suffix)
  758. sprintf(pcm->name, "%s - %s", chip->card->shortname, rec->suffix);
  759. else
  760. strcpy(pcm->name, chip->card->shortname);
  761. chip->pcm[device] = pcm;
  762. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  763. snd_dma_pci_data(chip->pci),
  764. rec->prealloc_size,
  765. rec->prealloc_max_size);
  766. return 0;
  767. }
  768. static struct ich_pcm_table intel_pcms[] __devinitdata = {
  769. {
  770. .suffix = "Modem",
  771. .playback_ops = &snd_intel8x0m_playback_ops,
  772. .capture_ops = &snd_intel8x0m_capture_ops,
  773. .prealloc_size = 32 * 1024,
  774. .prealloc_max_size = 64 * 1024,
  775. },
  776. };
  777. static int __devinit snd_intel8x0_pcm(intel8x0_t *chip)
  778. {
  779. int i, tblsize, device, err;
  780. struct ich_pcm_table *tbl, *rec;
  781. #if 1
  782. tbl = intel_pcms;
  783. tblsize = 1;
  784. #else
  785. switch (chip->device_type) {
  786. case DEVICE_NFORCE:
  787. tbl = nforce_pcms;
  788. tblsize = ARRAY_SIZE(nforce_pcms);
  789. break;
  790. case DEVICE_ALI:
  791. tbl = ali_pcms;
  792. tblsize = ARRAY_SIZE(ali_pcms);
  793. break;
  794. default:
  795. tbl = intel_pcms;
  796. tblsize = 2;
  797. break;
  798. }
  799. #endif
  800. device = 0;
  801. for (i = 0; i < tblsize; i++) {
  802. rec = tbl + i;
  803. if (i > 0 && rec->ac97_idx) {
  804. /* activate PCM only when associated AC'97 codec */
  805. if (! chip->ichd[rec->ac97_idx].ac97)
  806. continue;
  807. }
  808. err = snd_intel8x0_pcm1(chip, device, rec);
  809. if (err < 0)
  810. return err;
  811. device++;
  812. }
  813. chip->pcm_devs = device;
  814. return 0;
  815. }
  816. /*
  817. * Mixer part
  818. */
  819. static void snd_intel8x0_mixer_free_ac97_bus(ac97_bus_t *bus)
  820. {
  821. intel8x0_t *chip = bus->private_data;
  822. chip->ac97_bus = NULL;
  823. }
  824. static void snd_intel8x0_mixer_free_ac97(ac97_t *ac97)
  825. {
  826. intel8x0_t *chip = ac97->private_data;
  827. chip->ac97 = NULL;
  828. }
  829. static int __devinit snd_intel8x0_mixer(intel8x0_t *chip, int ac97_clock)
  830. {
  831. ac97_bus_t *pbus;
  832. ac97_template_t ac97;
  833. ac97_t *x97;
  834. int err;
  835. unsigned int glob_sta = 0;
  836. unsigned int idx;
  837. static ac97_bus_ops_t ops = {
  838. .write = snd_intel8x0_codec_write,
  839. .read = snd_intel8x0_codec_read,
  840. };
  841. chip->in_ac97_init = 1;
  842. memset(&ac97, 0, sizeof(ac97));
  843. ac97.private_data = chip;
  844. ac97.private_free = snd_intel8x0_mixer_free_ac97;
  845. ac97.scaps = AC97_SCAP_SKIP_AUDIO;
  846. glob_sta = igetdword(chip, ICHREG(GLOB_STA));
  847. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  848. goto __err;
  849. pbus->private_free = snd_intel8x0_mixer_free_ac97_bus;
  850. pbus->shared_type = AC97_SHARED_TYPE_ICH; /* shared with audio driver */
  851. if (ac97_clock >= 8000 && ac97_clock <= 48000)
  852. pbus->clock = ac97_clock;
  853. chip->ac97_bus = pbus;
  854. ac97.pci = chip->pci;
  855. ac97.num = glob_sta & ICH_SCR ? 1 : 0;
  856. if ((err = snd_ac97_mixer(pbus, &ac97, &x97)) < 0) {
  857. snd_printk(KERN_ERR "Unable to initialize codec #%d\n", ac97.num);
  858. if (ac97.num == 0)
  859. goto __err;
  860. return err;
  861. }
  862. chip->ac97 = x97;
  863. if(ac97_is_modem(x97) && !chip->ichd[ICHD_MDMIN].ac97) {
  864. chip->ichd[ICHD_MDMIN].ac97 = x97;
  865. chip->ichd[ICHD_MDMOUT].ac97 = x97;
  866. }
  867. for (idx = 0; idx < ARRAY_SIZE(snd_intel8x0m_mixer_switches); idx++) {
  868. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_intel8x0m_mixer_switches[idx], chip))) < 0)
  869. goto __err;
  870. }
  871. chip->in_ac97_init = 0;
  872. return 0;
  873. __err:
  874. /* clear the cold-reset bit for the next chance */
  875. if (chip->device_type != DEVICE_ALI)
  876. iputdword(chip, ICHREG(GLOB_CNT), igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_AC97COLD);
  877. return err;
  878. }
  879. /*
  880. *
  881. */
  882. #define do_delay(chip) do {\
  883. set_current_state(TASK_UNINTERRUPTIBLE);\
  884. schedule_timeout(1);\
  885. } while (0)
  886. static int snd_intel8x0m_ich_chip_init(intel8x0_t *chip, int probing)
  887. {
  888. unsigned long end_time;
  889. unsigned int cnt, status, nstatus;
  890. /* put logic to right state */
  891. /* first clear status bits */
  892. status = ICH_RCS | ICH_MIINT | ICH_MOINT;
  893. cnt = igetdword(chip, ICHREG(GLOB_STA));
  894. iputdword(chip, ICHREG(GLOB_STA), cnt & status);
  895. /* ACLink on, 2 channels */
  896. cnt = igetdword(chip, ICHREG(GLOB_CNT));
  897. cnt &= ~(ICH_ACLINK);
  898. /* finish cold or do warm reset */
  899. cnt |= (cnt & ICH_AC97COLD) == 0 ? ICH_AC97COLD : ICH_AC97WARM;
  900. iputdword(chip, ICHREG(GLOB_CNT), cnt);
  901. end_time = (jiffies + (HZ / 4)) + 1;
  902. do {
  903. if ((igetdword(chip, ICHREG(GLOB_CNT)) & ICH_AC97WARM) == 0)
  904. goto __ok;
  905. do_delay(chip);
  906. } while (time_after_eq(end_time, jiffies));
  907. snd_printk("AC'97 warm reset still in progress? [0x%x]\n", igetdword(chip, ICHREG(GLOB_CNT)));
  908. return -EIO;
  909. __ok:
  910. if (probing) {
  911. /* wait for any codec ready status.
  912. * Once it becomes ready it should remain ready
  913. * as long as we do not disable the ac97 link.
  914. */
  915. end_time = jiffies + HZ;
  916. do {
  917. status = igetdword(chip, ICHREG(GLOB_STA)) & (ICH_PCR | ICH_SCR | ICH_TCR);
  918. if (status)
  919. break;
  920. do_delay(chip);
  921. } while (time_after_eq(end_time, jiffies));
  922. if (! status) {
  923. /* no codec is found */
  924. snd_printk(KERN_ERR "codec_ready: codec is not ready [0x%x]\n", igetdword(chip, ICHREG(GLOB_STA)));
  925. return -EIO;
  926. }
  927. /* up to two codecs (modem cannot be tertiary with ICH4) */
  928. nstatus = ICH_PCR | ICH_SCR;
  929. /* wait for other codecs ready status. */
  930. end_time = jiffies + HZ / 4;
  931. while (status != nstatus && time_after_eq(end_time, jiffies)) {
  932. do_delay(chip);
  933. status |= igetdword(chip, ICHREG(GLOB_STA)) & nstatus;
  934. }
  935. } else {
  936. /* resume phase */
  937. status = 0;
  938. if (chip->ac97)
  939. status |= get_ich_codec_bit(chip, chip->ac97->num);
  940. /* wait until all the probed codecs are ready */
  941. end_time = jiffies + HZ;
  942. do {
  943. nstatus = igetdword(chip, ICHREG(GLOB_STA)) & (ICH_PCR | ICH_SCR | ICH_TCR);
  944. if (status == nstatus)
  945. break;
  946. do_delay(chip);
  947. } while (time_after_eq(end_time, jiffies));
  948. }
  949. if (chip->device_type == DEVICE_SIS) {
  950. /* unmute the output on SIS7012 */
  951. iputword(chip, 0x4c, igetword(chip, 0x4c) | 1);
  952. }
  953. return 0;
  954. }
  955. static int snd_intel8x0_chip_init(intel8x0_t *chip, int probing)
  956. {
  957. unsigned int i;
  958. int err;
  959. if ((err = snd_intel8x0m_ich_chip_init(chip, probing)) < 0)
  960. return err;
  961. iagetword(chip, 0); /* clear semaphore flag */
  962. /* disable interrupts */
  963. for (i = 0; i < chip->bdbars_count; i++)
  964. iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
  965. /* reset channels */
  966. for (i = 0; i < chip->bdbars_count; i++)
  967. iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
  968. /* initialize Buffer Descriptor Lists */
  969. for (i = 0; i < chip->bdbars_count; i++)
  970. iputdword(chip, ICH_REG_OFF_BDBAR + chip->ichd[i].reg_offset, chip->ichd[i].bdbar_addr);
  971. return 0;
  972. }
  973. static int snd_intel8x0_free(intel8x0_t *chip)
  974. {
  975. unsigned int i;
  976. if (chip->irq < 0)
  977. goto __hw_end;
  978. /* disable interrupts */
  979. for (i = 0; i < chip->bdbars_count; i++)
  980. iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
  981. /* reset channels */
  982. for (i = 0; i < chip->bdbars_count; i++)
  983. iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
  984. /* --- */
  985. synchronize_irq(chip->irq);
  986. __hw_end:
  987. if (chip->bdbars.area)
  988. snd_dma_free_pages(&chip->bdbars);
  989. if (chip->remap_addr)
  990. iounmap(chip->remap_addr);
  991. if (chip->remap_bmaddr)
  992. iounmap(chip->remap_bmaddr);
  993. if (chip->irq >= 0)
  994. free_irq(chip->irq, (void *)chip);
  995. pci_release_regions(chip->pci);
  996. pci_disable_device(chip->pci);
  997. kfree(chip);
  998. return 0;
  999. }
  1000. #ifdef CONFIG_PM
  1001. /*
  1002. * power management
  1003. */
  1004. static int intel8x0m_suspend(snd_card_t *card, pm_message_t state)
  1005. {
  1006. intel8x0_t *chip = card->pm_private_data;
  1007. int i;
  1008. for (i = 0; i < chip->pcm_devs; i++)
  1009. snd_pcm_suspend_all(chip->pcm[i]);
  1010. if (chip->ac97)
  1011. snd_ac97_suspend(chip->ac97);
  1012. pci_disable_device(chip->pci);
  1013. return 0;
  1014. }
  1015. static int intel8x0m_resume(snd_card_t *card)
  1016. {
  1017. intel8x0_t *chip = card->pm_private_data;
  1018. pci_enable_device(chip->pci);
  1019. pci_set_master(chip->pci);
  1020. snd_intel8x0_chip_init(chip, 0);
  1021. if (chip->ac97)
  1022. snd_ac97_resume(chip->ac97);
  1023. return 0;
  1024. }
  1025. #endif /* CONFIG_PM */
  1026. static void snd_intel8x0m_proc_read(snd_info_entry_t * entry,
  1027. snd_info_buffer_t * buffer)
  1028. {
  1029. intel8x0_t *chip = entry->private_data;
  1030. unsigned int tmp;
  1031. snd_iprintf(buffer, "Intel8x0m\n\n");
  1032. if (chip->device_type == DEVICE_ALI)
  1033. return;
  1034. tmp = igetdword(chip, ICHREG(GLOB_STA));
  1035. snd_iprintf(buffer, "Global control : 0x%08x\n", igetdword(chip, ICHREG(GLOB_CNT)));
  1036. snd_iprintf(buffer, "Global status : 0x%08x\n", tmp);
  1037. snd_iprintf(buffer, "AC'97 codecs ready :%s%s%s%s\n",
  1038. tmp & ICH_PCR ? " primary" : "",
  1039. tmp & ICH_SCR ? " secondary" : "",
  1040. tmp & ICH_TCR ? " tertiary" : "",
  1041. (tmp & (ICH_PCR | ICH_SCR | ICH_TCR)) == 0 ? " none" : "");
  1042. }
  1043. static void __devinit snd_intel8x0m_proc_init(intel8x0_t * chip)
  1044. {
  1045. snd_info_entry_t *entry;
  1046. if (! snd_card_proc_new(chip->card, "intel8x0m", &entry))
  1047. snd_info_set_text_ops(entry, chip, 1024, snd_intel8x0m_proc_read);
  1048. }
  1049. static int snd_intel8x0_dev_free(snd_device_t *device)
  1050. {
  1051. intel8x0_t *chip = device->device_data;
  1052. return snd_intel8x0_free(chip);
  1053. }
  1054. struct ich_reg_info {
  1055. unsigned int int_sta_mask;
  1056. unsigned int offset;
  1057. };
  1058. static int __devinit snd_intel8x0m_create(snd_card_t * card,
  1059. struct pci_dev *pci,
  1060. unsigned long device_type,
  1061. intel8x0_t ** r_intel8x0)
  1062. {
  1063. intel8x0_t *chip;
  1064. int err;
  1065. unsigned int i;
  1066. unsigned int int_sta_masks;
  1067. ichdev_t *ichdev;
  1068. static snd_device_ops_t ops = {
  1069. .dev_free = snd_intel8x0_dev_free,
  1070. };
  1071. static struct ich_reg_info intel_regs[2] = {
  1072. { ICH_MIINT, 0 },
  1073. { ICH_MOINT, 0x10 },
  1074. };
  1075. struct ich_reg_info *tbl;
  1076. *r_intel8x0 = NULL;
  1077. if ((err = pci_enable_device(pci)) < 0)
  1078. return err;
  1079. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  1080. if (chip == NULL) {
  1081. pci_disable_device(pci);
  1082. return -ENOMEM;
  1083. }
  1084. spin_lock_init(&chip->reg_lock);
  1085. chip->device_type = device_type;
  1086. chip->card = card;
  1087. chip->pci = pci;
  1088. chip->irq = -1;
  1089. if ((err = pci_request_regions(pci, card->shortname)) < 0) {
  1090. kfree(chip);
  1091. pci_disable_device(pci);
  1092. return err;
  1093. }
  1094. if (device_type == DEVICE_ALI) {
  1095. /* ALI5455 has no ac97 region */
  1096. chip->bmaddr = pci_resource_start(pci, 0);
  1097. goto port_inited;
  1098. }
  1099. if (pci_resource_flags(pci, 2) & IORESOURCE_MEM) { /* ICH4 and Nforce */
  1100. chip->mmio = 1;
  1101. chip->addr = pci_resource_start(pci, 2);
  1102. chip->remap_addr = ioremap_nocache(chip->addr,
  1103. pci_resource_len(pci, 2));
  1104. if (chip->remap_addr == NULL) {
  1105. snd_printk("AC'97 space ioremap problem\n");
  1106. snd_intel8x0_free(chip);
  1107. return -EIO;
  1108. }
  1109. } else {
  1110. chip->addr = pci_resource_start(pci, 0);
  1111. }
  1112. if (pci_resource_flags(pci, 3) & IORESOURCE_MEM) { /* ICH4 */
  1113. chip->bm_mmio = 1;
  1114. chip->bmaddr = pci_resource_start(pci, 3);
  1115. chip->remap_bmaddr = ioremap_nocache(chip->bmaddr,
  1116. pci_resource_len(pci, 3));
  1117. if (chip->remap_bmaddr == NULL) {
  1118. snd_printk("Controller space ioremap problem\n");
  1119. snd_intel8x0_free(chip);
  1120. return -EIO;
  1121. }
  1122. } else {
  1123. chip->bmaddr = pci_resource_start(pci, 1);
  1124. }
  1125. port_inited:
  1126. if (request_irq(pci->irq, snd_intel8x0_interrupt, SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip)) {
  1127. snd_printk("unable to grab IRQ %d\n", pci->irq);
  1128. snd_intel8x0_free(chip);
  1129. return -EBUSY;
  1130. }
  1131. chip->irq = pci->irq;
  1132. pci_set_master(pci);
  1133. synchronize_irq(chip->irq);
  1134. /* initialize offsets */
  1135. chip->bdbars_count = 2;
  1136. tbl = intel_regs;
  1137. for (i = 0; i < chip->bdbars_count; i++) {
  1138. ichdev = &chip->ichd[i];
  1139. ichdev->ichd = i;
  1140. ichdev->reg_offset = tbl[i].offset;
  1141. ichdev->int_sta_mask = tbl[i].int_sta_mask;
  1142. if (device_type == DEVICE_SIS) {
  1143. /* SiS 7013 swaps the registers */
  1144. ichdev->roff_sr = ICH_REG_OFF_PICB;
  1145. ichdev->roff_picb = ICH_REG_OFF_SR;
  1146. } else {
  1147. ichdev->roff_sr = ICH_REG_OFF_SR;
  1148. ichdev->roff_picb = ICH_REG_OFF_PICB;
  1149. }
  1150. if (device_type == DEVICE_ALI)
  1151. ichdev->ali_slot = (ichdev->reg_offset - 0x40) / 0x10;
  1152. }
  1153. /* SIS7013 handles the pcm data in bytes, others are in words */
  1154. chip->pcm_pos_shift = (device_type == DEVICE_SIS) ? 0 : 1;
  1155. /* allocate buffer descriptor lists */
  1156. /* the start of each lists must be aligned to 8 bytes */
  1157. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1158. chip->bdbars_count * sizeof(u32) * ICH_MAX_FRAGS * 2,
  1159. &chip->bdbars) < 0) {
  1160. snd_intel8x0_free(chip);
  1161. return -ENOMEM;
  1162. }
  1163. /* tables must be aligned to 8 bytes here, but the kernel pages
  1164. are much bigger, so we don't care (on i386) */
  1165. int_sta_masks = 0;
  1166. for (i = 0; i < chip->bdbars_count; i++) {
  1167. ichdev = &chip->ichd[i];
  1168. ichdev->bdbar = ((u32 *)chip->bdbars.area) + (i * ICH_MAX_FRAGS * 2);
  1169. ichdev->bdbar_addr = chip->bdbars.addr + (i * sizeof(u32) * ICH_MAX_FRAGS * 2);
  1170. int_sta_masks |= ichdev->int_sta_mask;
  1171. }
  1172. chip->int_sta_reg = ICH_REG_GLOB_STA;
  1173. chip->int_sta_mask = int_sta_masks;
  1174. if ((err = snd_intel8x0_chip_init(chip, 1)) < 0) {
  1175. snd_intel8x0_free(chip);
  1176. return err;
  1177. }
  1178. snd_card_set_pm_callback(card, intel8x0m_suspend, intel8x0m_resume, chip);
  1179. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1180. snd_intel8x0_free(chip);
  1181. return err;
  1182. }
  1183. snd_card_set_dev(card, &pci->dev);
  1184. *r_intel8x0 = chip;
  1185. return 0;
  1186. }
  1187. static struct shortname_table {
  1188. unsigned int id;
  1189. const char *s;
  1190. } shortnames[] __devinitdata = {
  1191. { PCI_DEVICE_ID_INTEL_82801_6, "Intel 82801AA-ICH" },
  1192. { PCI_DEVICE_ID_INTEL_82901_6, "Intel 82901AB-ICH0" },
  1193. { PCI_DEVICE_ID_INTEL_82801BA_6, "Intel 82801BA-ICH2" },
  1194. { PCI_DEVICE_ID_INTEL_440MX_6, "Intel 440MX" },
  1195. { PCI_DEVICE_ID_INTEL_ICH3_6, "Intel 82801CA-ICH3" },
  1196. { PCI_DEVICE_ID_INTEL_ICH4_6, "Intel 82801DB-ICH4" },
  1197. { PCI_DEVICE_ID_INTEL_ICH5_6, "Intel ICH5" },
  1198. { PCI_DEVICE_ID_INTEL_ICH6_6, "Intel ICH6" },
  1199. { PCI_DEVICE_ID_INTEL_ICH7_6, "Intel ICH7" },
  1200. { 0x7446, "AMD AMD768" },
  1201. { PCI_DEVICE_ID_SI_7013, "SiS SI7013" },
  1202. { PCI_DEVICE_ID_NVIDIA_MCP_MODEM, "NVidia nForce" },
  1203. { PCI_DEVICE_ID_NVIDIA_MCP2_MODEM, "NVidia nForce2" },
  1204. { PCI_DEVICE_ID_NVIDIA_MCP2S_MODEM, "NVidia nForce2s" },
  1205. { PCI_DEVICE_ID_NVIDIA_MCP3_MODEM, "NVidia nForce3" },
  1206. #if 0
  1207. { 0x5455, "ALi M5455" },
  1208. { 0x746d, "AMD AMD8111" },
  1209. #endif
  1210. { 0 },
  1211. };
  1212. static int __devinit snd_intel8x0m_probe(struct pci_dev *pci,
  1213. const struct pci_device_id *pci_id)
  1214. {
  1215. static int dev;
  1216. snd_card_t *card;
  1217. intel8x0_t *chip;
  1218. int err;
  1219. struct shortname_table *name;
  1220. if (dev >= SNDRV_CARDS)
  1221. return -ENODEV;
  1222. if (!enable[dev]) {
  1223. dev++;
  1224. return -ENOENT;
  1225. }
  1226. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1227. if (card == NULL)
  1228. return -ENOMEM;
  1229. strcpy(card->driver, "ICH-MODEM");
  1230. strcpy(card->shortname, "Intel ICH");
  1231. for (name = shortnames; name->id; name++) {
  1232. if (pci->device == name->id) {
  1233. strcpy(card->shortname, name->s);
  1234. break;
  1235. }
  1236. }
  1237. strcat(card->shortname," Modem");
  1238. if ((err = snd_intel8x0m_create(card, pci, pci_id->driver_data, &chip)) < 0) {
  1239. snd_card_free(card);
  1240. return err;
  1241. }
  1242. if ((err = snd_intel8x0_mixer(chip, ac97_clock[dev])) < 0) {
  1243. snd_card_free(card);
  1244. return err;
  1245. }
  1246. if ((err = snd_intel8x0_pcm(chip)) < 0) {
  1247. snd_card_free(card);
  1248. return err;
  1249. }
  1250. snd_intel8x0m_proc_init(chip);
  1251. sprintf(card->longname, "%s at 0x%lx, irq %i",
  1252. card->shortname, chip->addr, chip->irq);
  1253. if ((err = snd_card_register(card)) < 0) {
  1254. snd_card_free(card);
  1255. return err;
  1256. }
  1257. pci_set_drvdata(pci, card);
  1258. dev++;
  1259. return 0;
  1260. }
  1261. static void __devexit snd_intel8x0m_remove(struct pci_dev *pci)
  1262. {
  1263. snd_card_free(pci_get_drvdata(pci));
  1264. pci_set_drvdata(pci, NULL);
  1265. }
  1266. static struct pci_driver driver = {
  1267. .name = "Intel ICH Modem",
  1268. .id_table = snd_intel8x0m_ids,
  1269. .probe = snd_intel8x0m_probe,
  1270. .remove = __devexit_p(snd_intel8x0m_remove),
  1271. SND_PCI_PM_CALLBACKS
  1272. };
  1273. static int __init alsa_card_intel8x0m_init(void)
  1274. {
  1275. return pci_register_driver(&driver);
  1276. }
  1277. static void __exit alsa_card_intel8x0m_exit(void)
  1278. {
  1279. pci_unregister_driver(&driver);
  1280. }
  1281. module_init(alsa_card_intel8x0m_init)
  1282. module_exit(alsa_card_intel8x0m_exit)