ac97.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2015 Michael Gerow
  5. * Copyright (C) 2015-2018 K. Lange
  6. *
  7. * Driver for the Intel AC'97.
  8. *
  9. * See <http://www.intel.com/design/chipsets/manuals/29802801.pdf>.
  10. */
  11. #include <kernel/logging.h>
  12. #include <kernel/mem.h>
  13. #include <kernel/module.h>
  14. #include <kernel/mod/snd.h>
  15. #include <kernel/printf.h>
  16. #include <kernel/pci.h>
  17. #include <kernel/system.h>
  18. /* Utility macros */
  19. #define N_ELEMENTS(arr) (sizeof(arr) / sizeof((arr)[0]))
  20. /* BARs! */
  21. #define AC97_NAMBAR 0x10 /* Native Audio Mixer Base Address Register */
  22. #define AC97_NABMBAR 0x14 /* Native Audio Bus Mastering Base Address Register */
  23. /* Bus mastering IO port offsets */
  24. #define AC97_PO_BDBAR 0x10 /* PCM out buffer descriptor BAR */
  25. #define AC97_PO_CIV 0x14 /* PCM out current index value */
  26. #define AC97_PO_LVI 0x15 /* PCM out last valid index */
  27. #define AC97_PO_SR 0x16 /* PCM out status register */
  28. #define AC97_PO_PICB 0x18 /* PCM out position in current buffer register */
  29. #define AC97_PO_CR 0x1B /* PCM out control register */
  30. /* Bus mastering misc */
  31. /* Buffer descriptor list constants */
  32. #define AC97_BDL_LEN 32 /* Buffer descriptor list length */
  33. #define AC97_BDL_BUFFER_LEN 0x1000 /* Length of buffer in BDL */
  34. #define AC97_CL_GET_LENGTH(cl) ((cl) & 0xFFFF) /* Decode length from cl */
  35. #define AC97_CL_SET_LENGTH(cl, v) ((cl) = (v) & 0xFFFF) /* Encode length to cl */
  36. #define AC97_CL_BUP ((uint32_t)1 << 30) /* Buffer underrun policy in cl */
  37. #define AC97_CL_IOC ((uint32_t)1 << 31) /* Interrupt on completion flag in cl */
  38. /* PCM out control register flags */
  39. #define AC97_X_CR_RPBM (1 << 0) /* Run/pause bus master */
  40. #define AC97_X_CR_RR (1 << 1) /* Reset registers */
  41. #define AC97_X_CR_LVBIE (1 << 2) /* Last valid buffer interrupt enable */
  42. #define AC97_X_CR_FEIE (1 << 3) /* FIFO error interrupt enable */
  43. #define AC97_X_CR_IOCE (1 << 4) /* Interrupt on completion enable */
  44. /* Status register flags */
  45. #define AC97_X_SR_DCH (1 << 0) /* DMA controller halted */
  46. #define AC97_X_SR_CELV (1 << 1) /* Current equals last valid */
  47. #define AC97_X_SR_LVBCI (1 << 2) /* Last valid buffer completion interrupt */
  48. #define AC97_X_SR_BCIS (1 << 3) /* Buffer completion interrupt status */
  49. #define AC97_X_SR_FIFOE (1 << 3) /* FIFO error */
  50. /* Mixer IO port offsets */
  51. #define AC97_RESET 0x00
  52. #define AC97_MASTER_VOLUME 0x02
  53. #define AC97_AUX_OUT_VOLUME 0x04
  54. #define AC97_MONO_VOLUME 0x06
  55. #define AC97_PCM_OUT_VOLUME 0x18
  56. /* snd values */
  57. #define AC97_SND_NAME "Intel AC'97"
  58. #define AC97_PLAYBACK_SPEED 48000
  59. #define AC97_PLAYBACK_FORMAT SND_FORMAT_L16SLE
  60. /* An entry in a buffer dscriptor list */
  61. typedef struct {
  62. uint32_t pointer; /* Pointer to buffer */
  63. uint32_t cl; /* Control values and buffer length */
  64. } __attribute__((packed)) ac97_bdl_entry_t;
  65. typedef struct {
  66. uint32_t pci_device;
  67. uint16_t nabmbar; /* Native audio bus mastring BAR */
  68. uint16_t nambar; /* Native audio mixing BAR */
  69. size_t irq; /* This ac97's irq */
  70. uint8_t lvi; /* The currently set last valid index */
  71. uint8_t bits; /* How many bits of volume are supported (5 or 6) */
  72. ac97_bdl_entry_t * bdl; /* Buffer descriptor list */
  73. uint16_t * bufs[AC97_BDL_LEN]; /* Virtual addresses for buffers in BDL */
  74. uint32_t bdl_p;
  75. uint32_t mask;
  76. } ac97_device_t;
  77. static ac97_device_t _device;
  78. #define AC97_KNOB_PCM_OUT (SND_KNOB_VENDOR + 0)
  79. static snd_knob_t _knobs[] = {
  80. {
  81. "Master",
  82. SND_KNOB_MASTER
  83. },
  84. {
  85. "PCM Out",
  86. SND_KNOB_VENDOR + 0
  87. }
  88. };
  89. static int ac97_mixer_read(uint32_t knob_id, uint32_t *val);
  90. static int ac97_mixer_write(uint32_t knob_id, uint32_t val);
  91. static snd_device_t _snd = {
  92. .name = AC97_SND_NAME,
  93. .device = &_device,
  94. .playback_speed = AC97_PLAYBACK_SPEED,
  95. .playback_format = AC97_PLAYBACK_FORMAT,
  96. .knobs = _knobs,
  97. .num_knobs = N_ELEMENTS(_knobs),
  98. .mixer_read = ac97_mixer_read,
  99. .mixer_write = ac97_mixer_write,
  100. };
  101. /*
  102. * This could be unnecessary if we instead allocate just two buffers and make
  103. * the ac97 think there are more.
  104. */
  105. static void find_ac97(uint32_t device, uint16_t vendorid, uint16_t deviceid, void * extra) {
  106. ac97_device_t * ac97 = extra;
  107. if ((vendorid == 0x8086) && (deviceid == 0x2415)) {
  108. ac97->pci_device = device;
  109. }
  110. }
  111. #define DIVISION 0x1000
  112. static int irq_handler(struct regs * regs) {
  113. uint16_t sr = inports(_device.nabmbar + AC97_PO_SR);
  114. if (sr & AC97_X_SR_LVBCI) {
  115. outports(_device.nabmbar + AC97_PO_SR, AC97_X_SR_LVBCI);
  116. } else if (sr & AC97_X_SR_BCIS) {
  117. size_t f = (_device.lvi + 2) % AC97_BDL_LEN;
  118. for (size_t i = 0; i < AC97_BDL_BUFFER_LEN * sizeof(*_device.bufs[0]); i += DIVISION) {
  119. snd_request_buf(&_snd, DIVISION, (uint8_t *)_device.bufs[f] + i);
  120. switch_task(1);
  121. }
  122. _device.lvi = (_device.lvi + 1) % AC97_BDL_LEN;
  123. outportb(_device.nabmbar + AC97_PO_LVI, _device.lvi);
  124. outports(_device.nabmbar + AC97_PO_SR, AC97_X_SR_BCIS);
  125. } else if (sr & AC97_X_SR_FIFOE) {
  126. outports(_device.nabmbar + AC97_PO_SR, AC97_X_SR_FIFOE);
  127. } else {
  128. return 0;
  129. }
  130. irq_ack(_device.irq);
  131. return 1;
  132. }
  133. /* Currently we just assume right and left are the same */
  134. static int ac97_mixer_read(uint32_t knob_id, uint32_t *val) {
  135. uint16_t tmp;
  136. switch (knob_id) {
  137. case SND_KNOB_MASTER:
  138. tmp = inports(_device.nambar + AC97_MASTER_VOLUME);
  139. if (tmp == 0x8000) {
  140. *val = 0;
  141. } else {
  142. /* 6 bit value */
  143. *val = (tmp & _device.mask) << (sizeof(*val) * 8 - _device.bits);
  144. *val = ~*val;
  145. *val &= (uint32_t)_device.mask << (sizeof(*val) * 8 - _device.bits);
  146. }
  147. break;
  148. case AC97_KNOB_PCM_OUT:
  149. tmp = inports(_device.nambar + AC97_PCM_OUT_VOLUME);
  150. if (tmp == 0x8000) {
  151. *val = 0;
  152. } else {
  153. /* 5 bit value */
  154. *val = (tmp & 0x1f) << (sizeof(*val) * 8 - 5);
  155. *val = ~*val;
  156. *val &= 0x1f << (sizeof(*val) * 8 - 5);
  157. }
  158. break;
  159. default:
  160. return -1;
  161. }
  162. return 0;
  163. }
  164. static int ac97_mixer_write(uint32_t knob_id, uint32_t val) {
  165. switch (knob_id) {
  166. case SND_KNOB_MASTER: {
  167. uint16_t encoded;
  168. if (val == 0x0) {
  169. encoded = 0x8000;
  170. } else {
  171. /* 0 is the highest volume */
  172. val = ~val;
  173. /* 6 bit value */
  174. val >>= (sizeof(val) * 8 - _device.bits);
  175. encoded = (val & 0xFF) | (val << 8);
  176. }
  177. outports(_device.nambar + AC97_MASTER_VOLUME, encoded);
  178. break;
  179. }
  180. case AC97_KNOB_PCM_OUT: {
  181. uint16_t encoded;
  182. if (val == 0x0) {
  183. encoded = 0x8000;
  184. } else {
  185. /* 0 is the highest volume */
  186. val = ~val;
  187. /* 5 bit value */
  188. val >>= (sizeof(val) * 8 - 5);
  189. encoded = (val & 0xFF) | (val << 8);
  190. }
  191. outports(_device.nambar + AC97_PCM_OUT_VOLUME, encoded);
  192. break;
  193. }
  194. default:
  195. return -1;
  196. }
  197. return 0;
  198. }
  199. static int init(void) {
  200. debug_print(NOTICE, "Initializing AC97");
  201. pci_scan(&find_ac97, -1, &_device);
  202. if (!_device.pci_device) {
  203. return 1;
  204. }
  205. _device.nabmbar = pci_read_field(_device.pci_device, AC97_NABMBAR, 2) & ((uint32_t) -1) << 1;
  206. _device.nambar = pci_read_field(_device.pci_device, PCI_BAR0, 4) & ((uint32_t) -1) << 1;
  207. _device.irq = pci_read_field(_device.pci_device, PCI_INTERRUPT_LINE, 1);
  208. irq_install_handler(_device.irq, irq_handler, "ac97");
  209. /* Enable all matter of interrupts */
  210. outportb(_device.nabmbar + AC97_PO_CR, AC97_X_CR_FEIE | AC97_X_CR_IOCE);
  211. /* Enable bus mastering and disable memory mapped space */
  212. pci_write_field(_device.pci_device, PCI_COMMAND, 2, 0x5);
  213. /* Default the PCM output to full volume. */
  214. outports(_device.nambar + AC97_PCM_OUT_VOLUME, 0x0000);
  215. /* Allocate our BDL and our buffers */
  216. _device.bdl = (void *)kmalloc_p(AC97_BDL_LEN * sizeof(*_device.bdl), &_device.bdl_p);
  217. memset(_device.bdl, 0, AC97_BDL_LEN * sizeof(*_device.bdl));
  218. for (int i = 0; i < AC97_BDL_LEN; i++) {
  219. _device.bufs[i] = (uint16_t *)kmalloc_p(AC97_BDL_BUFFER_LEN * sizeof(*_device.bufs[0]),
  220. &_device.bdl[i].pointer);
  221. memset(_device.bufs[i], 0, AC97_BDL_BUFFER_LEN * sizeof(*_device.bufs[0]));
  222. AC97_CL_SET_LENGTH(_device.bdl[i].cl, AC97_BDL_BUFFER_LEN);
  223. /* Set all buffers to interrupt */
  224. _device.bdl[i].cl |= AC97_CL_IOC;
  225. }
  226. /* Tell the ac97 where our BDL is */
  227. outportl(_device.nabmbar + AC97_PO_BDBAR, _device.bdl_p);
  228. /* Set the LVI to be the last index */
  229. _device.lvi = 2;
  230. outportb(_device.nabmbar + AC97_PO_LVI, _device.lvi);
  231. /* detect whether device supports MSB */
  232. outports(_device.nambar + AC97_MASTER_VOLUME, 0x2020);
  233. uint16_t t = inports(_device.nambar + AC97_MASTER_VOLUME) & 0x1f;
  234. if (t == 0x1f) {
  235. debug_print(WARNING, "This device only supports 5 bits of audio volume.");
  236. _device.bits = 5;
  237. _device.mask = 0x1f;
  238. outports(_device.nambar + AC97_MASTER_VOLUME, 0x0f0f);
  239. } else {
  240. _device.bits = 6;
  241. _device.mask = 0x3f;
  242. outports(_device.nambar + AC97_MASTER_VOLUME, 0x1f1f);
  243. }
  244. snd_register(&_snd);
  245. /* Start things playing */
  246. outportb(_device.nabmbar + AC97_PO_CR, inportb(_device.nabmbar + AC97_PO_CR) | AC97_X_CR_RPBM);
  247. debug_print(NOTICE, "AC97 initialized successfully");
  248. return 0;
  249. }
  250. static int fini(void) {
  251. snd_unregister(&_snd);
  252. free(_device.bdl);
  253. for (int i = 0; i < AC97_BDL_LEN; i++) {
  254. free(_device.bufs[i]);
  255. }
  256. return 0;
  257. }
  258. MODULE_DEF(ac97, init, fini);
  259. MODULE_DEPENDS(snd);