hda_controller.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Common functionality for the alsa driver code base for HD Audio.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #ifndef __SOUND_HDA_CONTROLLER_H
  15. #define __SOUND_HDA_CONTROLLER_H
  16. #include <linux/timecounter.h>
  17. #include <linux/interrupt.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/initval.h>
  21. #include "hda_codec.h"
  22. /*
  23. * registers
  24. */
  25. #define AZX_REG_GCAP 0x00
  26. #define AZX_GCAP_64OK (1 << 0) /* 64bit address support */
  27. #define AZX_GCAP_NSDO (3 << 1) /* # of serial data out signals */
  28. #define AZX_GCAP_BSS (31 << 3) /* # of bidirectional streams */
  29. #define AZX_GCAP_ISS (15 << 8) /* # of input streams */
  30. #define AZX_GCAP_OSS (15 << 12) /* # of output streams */
  31. #define AZX_REG_VMIN 0x02
  32. #define AZX_REG_VMAJ 0x03
  33. #define AZX_REG_OUTPAY 0x04
  34. #define AZX_REG_INPAY 0x06
  35. #define AZX_REG_GCTL 0x08
  36. #define AZX_GCTL_RESET (1 << 0) /* controller reset */
  37. #define AZX_GCTL_FCNTRL (1 << 1) /* flush control */
  38. #define AZX_GCTL_UNSOL (1 << 8) /* accept unsol. response enable */
  39. #define AZX_REG_WAKEEN 0x0c
  40. #define AZX_REG_STATESTS 0x0e
  41. #define AZX_REG_GSTS 0x10
  42. #define AZX_GSTS_FSTS (1 << 1) /* flush status */
  43. #define AZX_REG_INTCTL 0x20
  44. #define AZX_REG_INTSTS 0x24
  45. #define AZX_REG_WALLCLK 0x30 /* 24Mhz source */
  46. #define AZX_REG_OLD_SSYNC 0x34 /* SSYNC for old ICH */
  47. #define AZX_REG_SSYNC 0x38
  48. #define AZX_REG_CORBLBASE 0x40
  49. #define AZX_REG_CORBUBASE 0x44
  50. #define AZX_REG_CORBWP 0x48
  51. #define AZX_REG_CORBRP 0x4a
  52. #define AZX_CORBRP_RST (1 << 15) /* read pointer reset */
  53. #define AZX_REG_CORBCTL 0x4c
  54. #define AZX_CORBCTL_RUN (1 << 1) /* enable DMA */
  55. #define AZX_CORBCTL_CMEIE (1 << 0) /* enable memory error irq */
  56. #define AZX_REG_CORBSTS 0x4d
  57. #define AZX_CORBSTS_CMEI (1 << 0) /* memory error indication */
  58. #define AZX_REG_CORBSIZE 0x4e
  59. #define AZX_REG_RIRBLBASE 0x50
  60. #define AZX_REG_RIRBUBASE 0x54
  61. #define AZX_REG_RIRBWP 0x58
  62. #define AZX_RIRBWP_RST (1 << 15) /* write pointer reset */
  63. #define AZX_REG_RINTCNT 0x5a
  64. #define AZX_REG_RIRBCTL 0x5c
  65. #define AZX_RBCTL_IRQ_EN (1 << 0) /* enable IRQ */
  66. #define AZX_RBCTL_DMA_EN (1 << 1) /* enable DMA */
  67. #define AZX_RBCTL_OVERRUN_EN (1 << 2) /* enable overrun irq */
  68. #define AZX_REG_RIRBSTS 0x5d
  69. #define AZX_RBSTS_IRQ (1 << 0) /* response irq */
  70. #define AZX_RBSTS_OVERRUN (1 << 2) /* overrun irq */
  71. #define AZX_REG_RIRBSIZE 0x5e
  72. #define AZX_REG_IC 0x60
  73. #define AZX_REG_IR 0x64
  74. #define AZX_REG_IRS 0x68
  75. #define AZX_IRS_VALID (1<<1)
  76. #define AZX_IRS_BUSY (1<<0)
  77. #define AZX_REG_DPLBASE 0x70
  78. #define AZX_REG_DPUBASE 0x74
  79. #define AZX_DPLBASE_ENABLE 0x1 /* Enable position buffer */
  80. /* SD offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
  81. enum { SDI0, SDI1, SDI2, SDI3, SDO0, SDO1, SDO2, SDO3 };
  82. /* stream register offsets from stream base */
  83. #define AZX_REG_SD_CTL 0x00
  84. #define AZX_REG_SD_STS 0x03
  85. #define AZX_REG_SD_LPIB 0x04
  86. #define AZX_REG_SD_CBL 0x08
  87. #define AZX_REG_SD_LVI 0x0c
  88. #define AZX_REG_SD_FIFOW 0x0e
  89. #define AZX_REG_SD_FIFOSIZE 0x10
  90. #define AZX_REG_SD_FORMAT 0x12
  91. #define AZX_REG_SD_BDLPL 0x18
  92. #define AZX_REG_SD_BDLPU 0x1c
  93. /* PCI space */
  94. #define AZX_PCIREG_TCSEL 0x44
  95. /*
  96. * other constants
  97. */
  98. /* max number of fragments - we may use more if allocating more pages for BDL */
  99. #define BDL_SIZE 4096
  100. #define AZX_MAX_BDL_ENTRIES (BDL_SIZE / 16)
  101. #define AZX_MAX_FRAG 32
  102. /* max buffer size - no h/w limit, you can increase as you like */
  103. #define AZX_MAX_BUF_SIZE (1024*1024*1024)
  104. /* RIRB int mask: overrun[2], response[0] */
  105. #define RIRB_INT_RESPONSE 0x01
  106. #define RIRB_INT_OVERRUN 0x04
  107. #define RIRB_INT_MASK 0x05
  108. /* STATESTS int mask: S3,SD2,SD1,SD0 */
  109. #define AZX_MAX_CODECS 8
  110. #define AZX_DEFAULT_CODECS 4
  111. #define STATESTS_INT_MASK ((1 << AZX_MAX_CODECS) - 1)
  112. /* SD_CTL bits */
  113. #define SD_CTL_STREAM_RESET 0x01 /* stream reset bit */
  114. #define SD_CTL_DMA_START 0x02 /* stream DMA start bit */
  115. #define SD_CTL_STRIPE (3 << 16) /* stripe control */
  116. #define SD_CTL_TRAFFIC_PRIO (1 << 18) /* traffic priority */
  117. #define SD_CTL_DIR (1 << 19) /* bi-directional stream */
  118. #define SD_CTL_STREAM_TAG_MASK (0xf << 20)
  119. #define SD_CTL_STREAM_TAG_SHIFT 20
  120. /* SD_CTL and SD_STS */
  121. #define SD_INT_DESC_ERR 0x10 /* descriptor error interrupt */
  122. #define SD_INT_FIFO_ERR 0x08 /* FIFO error interrupt */
  123. #define SD_INT_COMPLETE 0x04 /* completion interrupt */
  124. #define SD_INT_MASK (SD_INT_DESC_ERR|SD_INT_FIFO_ERR|\
  125. SD_INT_COMPLETE)
  126. /* SD_STS */
  127. #define SD_STS_FIFO_READY 0x20 /* FIFO ready */
  128. /* INTCTL and INTSTS */
  129. #define AZX_INT_ALL_STREAM 0xff /* all stream interrupts */
  130. #define AZX_INT_CTRL_EN 0x40000000 /* controller interrupt enable bit */
  131. #define AZX_INT_GLOBAL_EN 0x80000000 /* global interrupt enable bit */
  132. /* below are so far hardcoded - should read registers in future */
  133. #define AZX_MAX_CORB_ENTRIES 256
  134. #define AZX_MAX_RIRB_ENTRIES 256
  135. /* driver quirks (capabilities) */
  136. /* bits 0-7 are used for indicating driver type */
  137. #define AZX_DCAPS_NO_TCSEL (1 << 8) /* No Intel TCSEL bit */
  138. #define AZX_DCAPS_NO_MSI (1 << 9) /* No MSI support */
  139. #define AZX_DCAPS_SNOOP_MASK (3 << 10) /* snoop type mask */
  140. #define AZX_DCAPS_SNOOP_OFF (1 << 12) /* snoop default off */
  141. #define AZX_DCAPS_RIRB_DELAY (1 << 13) /* Long delay in read loop */
  142. #define AZX_DCAPS_RIRB_PRE_DELAY (1 << 14) /* Put a delay before read */
  143. #define AZX_DCAPS_CTX_WORKAROUND (1 << 15) /* X-Fi workaround */
  144. #define AZX_DCAPS_POSFIX_LPIB (1 << 16) /* Use LPIB as default */
  145. #define AZX_DCAPS_POSFIX_VIA (1 << 17) /* Use VIACOMBO as default */
  146. #define AZX_DCAPS_NO_64BIT (1 << 18) /* No 64bit address */
  147. #define AZX_DCAPS_SYNC_WRITE (1 << 19) /* sync each cmd write */
  148. #define AZX_DCAPS_OLD_SSYNC (1 << 20) /* Old SSYNC reg for ICH */
  149. #define AZX_DCAPS_NO_ALIGN_BUFSIZE (1 << 21) /* no buffer size alignment */
  150. /* 22 unused */
  151. #define AZX_DCAPS_4K_BDLE_BOUNDARY (1 << 23) /* BDLE in 4k boundary */
  152. #define AZX_DCAPS_REVERSE_ASSIGN (1 << 24) /* Assign devices in reverse order */
  153. #define AZX_DCAPS_COUNT_LPIB_DELAY (1 << 25) /* Take LPIB as delay */
  154. #define AZX_DCAPS_PM_RUNTIME (1 << 26) /* runtime PM support */
  155. #define AZX_DCAPS_I915_POWERWELL (1 << 27) /* HSW i915 powerwell support */
  156. #define AZX_DCAPS_CORBRP_SELF_CLEAR (1 << 28) /* CORBRP clears itself after reset */
  157. #define AZX_DCAPS_NO_MSI64 (1 << 29) /* Stick to 32-bit MSIs */
  158. #define AZX_DCAPS_SEPARATE_STREAM_TAG (1 << 30) /* capture and playback use separate stream tag */
  159. enum {
  160. AZX_SNOOP_TYPE_NONE,
  161. AZX_SNOOP_TYPE_SCH,
  162. AZX_SNOOP_TYPE_ATI,
  163. AZX_SNOOP_TYPE_NVIDIA,
  164. };
  165. /* HD Audio class code */
  166. #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
  167. struct azx_dev {
  168. struct snd_dma_buffer bdl; /* BDL buffer */
  169. u32 *posbuf; /* position buffer pointer */
  170. unsigned int bufsize; /* size of the play buffer in bytes */
  171. unsigned int period_bytes; /* size of the period in bytes */
  172. unsigned int frags; /* number for period in the play buffer */
  173. unsigned int fifo_size; /* FIFO size */
  174. unsigned long start_wallclk; /* start + minimum wallclk */
  175. unsigned long period_wallclk; /* wallclk for period */
  176. void __iomem *sd_addr; /* stream descriptor pointer */
  177. u32 sd_int_sta_mask; /* stream int status mask */
  178. /* pcm support */
  179. struct snd_pcm_substream *substream; /* assigned substream,
  180. * set in PCM open
  181. */
  182. unsigned int format_val; /* format value to be set in the
  183. * controller and the codec
  184. */
  185. unsigned char stream_tag; /* assigned stream */
  186. unsigned char index; /* stream index */
  187. int assigned_key; /* last device# key assigned to */
  188. unsigned int opened:1;
  189. unsigned int running:1;
  190. unsigned int irq_pending:1;
  191. unsigned int prepared:1;
  192. unsigned int locked:1;
  193. /*
  194. * For VIA:
  195. * A flag to ensure DMA position is 0
  196. * when link position is not greater than FIFO size
  197. */
  198. unsigned int insufficient:1;
  199. unsigned int wc_marked:1;
  200. unsigned int no_period_wakeup:1;
  201. struct timecounter azx_tc;
  202. struct cyclecounter azx_cc;
  203. int delay_negative_threshold;
  204. #ifdef CONFIG_SND_HDA_DSP_LOADER
  205. /* Allows dsp load to have sole access to the playback stream. */
  206. struct mutex dsp_mutex;
  207. #endif
  208. };
  209. /* CORB/RIRB */
  210. struct azx_rb {
  211. u32 *buf; /* CORB/RIRB buffer
  212. * Each CORB entry is 4byte, RIRB is 8byte
  213. */
  214. dma_addr_t addr; /* physical address of CORB/RIRB buffer */
  215. /* for RIRB */
  216. unsigned short rp, wp; /* read/write pointers */
  217. int cmds[AZX_MAX_CODECS]; /* number of pending requests */
  218. u32 res[AZX_MAX_CODECS]; /* last read value */
  219. };
  220. struct azx;
  221. /* Functions to read/write to hda registers. */
  222. struct hda_controller_ops {
  223. /* Register Access */
  224. void (*reg_writel)(u32 value, u32 __iomem *addr);
  225. u32 (*reg_readl)(u32 __iomem *addr);
  226. void (*reg_writew)(u16 value, u16 __iomem *addr);
  227. u16 (*reg_readw)(u16 __iomem *addr);
  228. void (*reg_writeb)(u8 value, u8 __iomem *addr);
  229. u8 (*reg_readb)(u8 __iomem *addr);
  230. /* Disable msi if supported, PCI only */
  231. int (*disable_msi_reset_irq)(struct azx *);
  232. /* Allocation ops */
  233. int (*dma_alloc_pages)(struct azx *chip,
  234. int type,
  235. size_t size,
  236. struct snd_dma_buffer *buf);
  237. void (*dma_free_pages)(struct azx *chip, struct snd_dma_buffer *buf);
  238. int (*substream_alloc_pages)(struct azx *chip,
  239. struct snd_pcm_substream *substream,
  240. size_t size);
  241. int (*substream_free_pages)(struct azx *chip,
  242. struct snd_pcm_substream *substream);
  243. void (*pcm_mmap_prepare)(struct snd_pcm_substream *substream,
  244. struct vm_area_struct *area);
  245. /* Check if current position is acceptable */
  246. int (*position_check)(struct azx *chip, struct azx_dev *azx_dev);
  247. };
  248. struct azx_pcm {
  249. struct azx *chip;
  250. struct snd_pcm *pcm;
  251. struct hda_codec *codec;
  252. struct hda_pcm *info;
  253. struct list_head list;
  254. };
  255. typedef unsigned int (*azx_get_pos_callback_t)(struct azx *, struct azx_dev *);
  256. typedef int (*azx_get_delay_callback_t)(struct azx *, struct azx_dev *, unsigned int pos);
  257. struct azx {
  258. struct snd_card *card;
  259. struct pci_dev *pci;
  260. int dev_index;
  261. /* chip type specific */
  262. int driver_type;
  263. unsigned int driver_caps;
  264. int playback_streams;
  265. int playback_index_offset;
  266. int capture_streams;
  267. int capture_index_offset;
  268. int num_streams;
  269. const int *jackpoll_ms; /* per-card jack poll interval */
  270. /* Register interaction. */
  271. const struct hda_controller_ops *ops;
  272. /* position adjustment callbacks */
  273. azx_get_pos_callback_t get_position[2];
  274. azx_get_delay_callback_t get_delay[2];
  275. /* pci resources */
  276. unsigned long addr;
  277. void __iomem *remap_addr;
  278. int irq;
  279. /* locks */
  280. spinlock_t reg_lock;
  281. struct mutex open_mutex; /* Prevents concurrent open/close operations */
  282. /* streams (x num_streams) */
  283. struct azx_dev *azx_dev;
  284. /* PCM */
  285. struct list_head pcm_list; /* azx_pcm list */
  286. /* HD codec */
  287. unsigned short codec_mask;
  288. int codec_probe_mask; /* copied from probe_mask option */
  289. struct hda_bus *bus;
  290. unsigned int beep_mode;
  291. /* CORB/RIRB */
  292. struct azx_rb corb;
  293. struct azx_rb rirb;
  294. /* CORB/RIRB and position buffers */
  295. struct snd_dma_buffer rb;
  296. struct snd_dma_buffer posbuf;
  297. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  298. const struct firmware *fw;
  299. #endif
  300. /* flags */
  301. const int *bdl_pos_adj;
  302. int poll_count;
  303. unsigned int running:1;
  304. unsigned int initialized:1;
  305. unsigned int single_cmd:1;
  306. unsigned int polling_mode:1;
  307. unsigned int msi:1;
  308. unsigned int probing:1; /* codec probing phase */
  309. unsigned int snoop:1;
  310. unsigned int align_buffer_size:1;
  311. unsigned int region_requested:1;
  312. unsigned int disabled:1; /* disabled by VGA-switcher */
  313. /* for debugging */
  314. unsigned int last_cmd[AZX_MAX_CODECS];
  315. #ifdef CONFIG_SND_HDA_DSP_LOADER
  316. struct azx_dev saved_azx_dev;
  317. #endif
  318. };
  319. #ifdef CONFIG_X86
  320. #define azx_snoop(chip) ((chip)->snoop)
  321. #else
  322. #define azx_snoop(chip) true
  323. #endif
  324. /*
  325. * macros for easy use
  326. */
  327. #define azx_writel(chip, reg, value) \
  328. ((chip)->ops->reg_writel(value, (chip)->remap_addr + AZX_REG_##reg))
  329. #define azx_readl(chip, reg) \
  330. ((chip)->ops->reg_readl((chip)->remap_addr + AZX_REG_##reg))
  331. #define azx_writew(chip, reg, value) \
  332. ((chip)->ops->reg_writew(value, (chip)->remap_addr + AZX_REG_##reg))
  333. #define azx_readw(chip, reg) \
  334. ((chip)->ops->reg_readw((chip)->remap_addr + AZX_REG_##reg))
  335. #define azx_writeb(chip, reg, value) \
  336. ((chip)->ops->reg_writeb(value, (chip)->remap_addr + AZX_REG_##reg))
  337. #define azx_readb(chip, reg) \
  338. ((chip)->ops->reg_readb((chip)->remap_addr + AZX_REG_##reg))
  339. #define azx_sd_writel(chip, dev, reg, value) \
  340. ((chip)->ops->reg_writel(value, (dev)->sd_addr + AZX_REG_##reg))
  341. #define azx_sd_readl(chip, dev, reg) \
  342. ((chip)->ops->reg_readl((dev)->sd_addr + AZX_REG_##reg))
  343. #define azx_sd_writew(chip, dev, reg, value) \
  344. ((chip)->ops->reg_writew(value, (dev)->sd_addr + AZX_REG_##reg))
  345. #define azx_sd_readw(chip, dev, reg) \
  346. ((chip)->ops->reg_readw((dev)->sd_addr + AZX_REG_##reg))
  347. #define azx_sd_writeb(chip, dev, reg, value) \
  348. ((chip)->ops->reg_writeb(value, (dev)->sd_addr + AZX_REG_##reg))
  349. #define azx_sd_readb(chip, dev, reg) \
  350. ((chip)->ops->reg_readb((dev)->sd_addr + AZX_REG_##reg))
  351. #define azx_has_pm_runtime(chip) \
  352. ((chip)->driver_caps & AZX_DCAPS_PM_RUNTIME)
  353. /* PCM setup */
  354. static inline struct azx_dev *get_azx_dev(struct snd_pcm_substream *substream)
  355. {
  356. return substream->runtime->private_data;
  357. }
  358. unsigned int azx_get_position(struct azx *chip, struct azx_dev *azx_dev);
  359. unsigned int azx_get_pos_lpib(struct azx *chip, struct azx_dev *azx_dev);
  360. unsigned int azx_get_pos_posbuf(struct azx *chip, struct azx_dev *azx_dev);
  361. /* Stream control. */
  362. void azx_stream_stop(struct azx *chip, struct azx_dev *azx_dev);
  363. /* Allocation functions. */
  364. int azx_alloc_stream_pages(struct azx *chip);
  365. void azx_free_stream_pages(struct azx *chip);
  366. /* Low level azx interface */
  367. void azx_init_chip(struct azx *chip, bool full_reset);
  368. void azx_stop_chip(struct azx *chip);
  369. void azx_enter_link_reset(struct azx *chip);
  370. irqreturn_t azx_interrupt(int irq, void *dev_id);
  371. /* Codec interface */
  372. int azx_bus_create(struct azx *chip, const char *model);
  373. int azx_probe_codecs(struct azx *chip, unsigned int max_slots);
  374. int azx_codec_configure(struct azx *chip);
  375. int azx_init_stream(struct azx *chip);
  376. #endif /* __SOUND_HDA_CONTROLLER_H */