intel8x0m.c 38 KB

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