intel8x0m.c 38 KB

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