intel8x0m.c 38 KB

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