hdac_controller.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * HD-audio controller helpers
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/delay.h>
  6. #include <linux/export.h>
  7. #include <sound/core.h>
  8. #include <sound/hdaudio.h>
  9. #include <sound/hda_register.h>
  10. /* clear CORB read pointer properly */
  11. static void azx_clear_corbrp(struct hdac_bus *bus)
  12. {
  13. int timeout;
  14. for (timeout = 1000; timeout > 0; timeout--) {
  15. if (snd_hdac_chip_readw(bus, CORBRP) & AZX_CORBRP_RST)
  16. break;
  17. udelay(1);
  18. }
  19. if (timeout <= 0)
  20. dev_err(bus->dev, "CORB reset timeout#1, CORBRP = %d\n",
  21. snd_hdac_chip_readw(bus, CORBRP));
  22. snd_hdac_chip_writew(bus, CORBRP, 0);
  23. for (timeout = 1000; timeout > 0; timeout--) {
  24. if (snd_hdac_chip_readw(bus, CORBRP) == 0)
  25. break;
  26. udelay(1);
  27. }
  28. if (timeout <= 0)
  29. dev_err(bus->dev, "CORB reset timeout#2, CORBRP = %d\n",
  30. snd_hdac_chip_readw(bus, CORBRP));
  31. }
  32. /**
  33. * snd_hdac_bus_init_cmd_io - set up CORB/RIRB buffers
  34. * @bus: HD-audio core bus
  35. */
  36. void snd_hdac_bus_init_cmd_io(struct hdac_bus *bus)
  37. {
  38. WARN_ON_ONCE(!bus->rb.area);
  39. spin_lock_irq(&bus->reg_lock);
  40. /* CORB set up */
  41. bus->corb.addr = bus->rb.addr;
  42. bus->corb.buf = (__le32 *)bus->rb.area;
  43. snd_hdac_chip_writel(bus, CORBLBASE, (u32)bus->corb.addr);
  44. snd_hdac_chip_writel(bus, CORBUBASE, upper_32_bits(bus->corb.addr));
  45. /* set the corb size to 256 entries (ULI requires explicitly) */
  46. snd_hdac_chip_writeb(bus, CORBSIZE, 0x02);
  47. /* set the corb write pointer to 0 */
  48. snd_hdac_chip_writew(bus, CORBWP, 0);
  49. /* reset the corb hw read pointer */
  50. snd_hdac_chip_writew(bus, CORBRP, AZX_CORBRP_RST);
  51. if (!bus->corbrp_self_clear)
  52. azx_clear_corbrp(bus);
  53. /* enable corb dma */
  54. snd_hdac_chip_writeb(bus, CORBCTL, AZX_CORBCTL_RUN);
  55. /* RIRB set up */
  56. bus->rirb.addr = bus->rb.addr + 2048;
  57. bus->rirb.buf = (__le32 *)(bus->rb.area + 2048);
  58. bus->rirb.wp = bus->rirb.rp = 0;
  59. memset(bus->rirb.cmds, 0, sizeof(bus->rirb.cmds));
  60. snd_hdac_chip_writel(bus, RIRBLBASE, (u32)bus->rirb.addr);
  61. snd_hdac_chip_writel(bus, RIRBUBASE, upper_32_bits(bus->rirb.addr));
  62. /* set the rirb size to 256 entries (ULI requires explicitly) */
  63. snd_hdac_chip_writeb(bus, RIRBSIZE, 0x02);
  64. /* reset the rirb hw write pointer */
  65. snd_hdac_chip_writew(bus, RIRBWP, AZX_RIRBWP_RST);
  66. /* set N=1, get RIRB response interrupt for new entry */
  67. snd_hdac_chip_writew(bus, RINTCNT, 1);
  68. /* enable rirb dma and response irq */
  69. snd_hdac_chip_writeb(bus, RIRBCTL, AZX_RBCTL_DMA_EN | AZX_RBCTL_IRQ_EN);
  70. spin_unlock_irq(&bus->reg_lock);
  71. }
  72. EXPORT_SYMBOL_GPL(snd_hdac_bus_init_cmd_io);
  73. /**
  74. * snd_hdac_bus_stop_cmd_io - clean up CORB/RIRB buffers
  75. * @bus: HD-audio core bus
  76. */
  77. void snd_hdac_bus_stop_cmd_io(struct hdac_bus *bus)
  78. {
  79. spin_lock_irq(&bus->reg_lock);
  80. /* disable ringbuffer DMAs */
  81. snd_hdac_chip_writeb(bus, RIRBCTL, 0);
  82. snd_hdac_chip_writeb(bus, CORBCTL, 0);
  83. /* disable unsolicited responses */
  84. snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, 0);
  85. spin_unlock_irq(&bus->reg_lock);
  86. }
  87. EXPORT_SYMBOL_GPL(snd_hdac_bus_stop_cmd_io);
  88. static unsigned int azx_command_addr(u32 cmd)
  89. {
  90. unsigned int addr = cmd >> 28;
  91. if (snd_BUG_ON(addr >= HDA_MAX_CODECS))
  92. addr = 0;
  93. return addr;
  94. }
  95. /**
  96. * snd_hdac_bus_send_cmd - send a command verb via CORB
  97. * @bus: HD-audio core bus
  98. * @val: encoded verb value to send
  99. *
  100. * Returns zero for success or a negative error code.
  101. */
  102. int snd_hdac_bus_send_cmd(struct hdac_bus *bus, unsigned int val)
  103. {
  104. unsigned int addr = azx_command_addr(val);
  105. unsigned int wp, rp;
  106. spin_lock_irq(&bus->reg_lock);
  107. bus->last_cmd[azx_command_addr(val)] = val;
  108. /* add command to corb */
  109. wp = snd_hdac_chip_readw(bus, CORBWP);
  110. if (wp == 0xffff) {
  111. /* something wrong, controller likely turned to D3 */
  112. spin_unlock_irq(&bus->reg_lock);
  113. return -EIO;
  114. }
  115. wp++;
  116. wp %= AZX_MAX_CORB_ENTRIES;
  117. rp = snd_hdac_chip_readw(bus, CORBRP);
  118. if (wp == rp) {
  119. /* oops, it's full */
  120. spin_unlock_irq(&bus->reg_lock);
  121. return -EAGAIN;
  122. }
  123. bus->rirb.cmds[addr]++;
  124. bus->corb.buf[wp] = cpu_to_le32(val);
  125. snd_hdac_chip_writew(bus, CORBWP, wp);
  126. spin_unlock_irq(&bus->reg_lock);
  127. return 0;
  128. }
  129. EXPORT_SYMBOL_GPL(snd_hdac_bus_send_cmd);
  130. #define AZX_RIRB_EX_UNSOL_EV (1<<4)
  131. /**
  132. * snd_hdac_bus_update_rirb - retrieve RIRB entries
  133. * @bus: HD-audio core bus
  134. *
  135. * Usually called from interrupt handler.
  136. */
  137. void snd_hdac_bus_update_rirb(struct hdac_bus *bus)
  138. {
  139. unsigned int rp, wp;
  140. unsigned int addr;
  141. u32 res, res_ex;
  142. wp = snd_hdac_chip_readw(bus, RIRBWP);
  143. if (wp == 0xffff) {
  144. /* something wrong, controller likely turned to D3 */
  145. return;
  146. }
  147. if (wp == bus->rirb.wp)
  148. return;
  149. bus->rirb.wp = wp;
  150. while (bus->rirb.rp != wp) {
  151. bus->rirb.rp++;
  152. bus->rirb.rp %= AZX_MAX_RIRB_ENTRIES;
  153. rp = bus->rirb.rp << 1; /* an RIRB entry is 8-bytes */
  154. res_ex = le32_to_cpu(bus->rirb.buf[rp + 1]);
  155. res = le32_to_cpu(bus->rirb.buf[rp]);
  156. addr = res_ex & 0xf;
  157. if (addr >= HDA_MAX_CODECS) {
  158. dev_err(bus->dev,
  159. "spurious response %#x:%#x, rp = %d, wp = %d",
  160. res, res_ex, bus->rirb.rp, wp);
  161. snd_BUG();
  162. } else if (res_ex & AZX_RIRB_EX_UNSOL_EV)
  163. snd_hdac_bus_queue_event(bus, res, res_ex);
  164. else if (bus->rirb.cmds[addr]) {
  165. bus->rirb.res[addr] = res;
  166. bus->rirb.cmds[addr]--;
  167. } else {
  168. dev_err_ratelimited(bus->dev,
  169. "spurious response %#x:%#x, last cmd=%#08x\n",
  170. res, res_ex, bus->last_cmd[addr]);
  171. }
  172. }
  173. }
  174. EXPORT_SYMBOL_GPL(snd_hdac_bus_update_rirb);
  175. /**
  176. * snd_hdac_bus_get_response - receive a response via RIRB
  177. * @bus: HD-audio core bus
  178. * @addr: codec address
  179. * @res: pointer to store the value, NULL when not needed
  180. *
  181. * Returns zero if a value is read, or a negative error code.
  182. */
  183. int snd_hdac_bus_get_response(struct hdac_bus *bus, unsigned int addr,
  184. unsigned int *res)
  185. {
  186. unsigned long timeout;
  187. unsigned long loopcounter;
  188. timeout = jiffies + msecs_to_jiffies(1000);
  189. for (loopcounter = 0;; loopcounter++) {
  190. spin_lock_irq(&bus->reg_lock);
  191. if (!bus->rirb.cmds[addr]) {
  192. if (res)
  193. *res = bus->rirb.res[addr]; /* the last value */
  194. spin_unlock_irq(&bus->reg_lock);
  195. return 0;
  196. }
  197. spin_unlock_irq(&bus->reg_lock);
  198. if (time_after(jiffies, timeout))
  199. break;
  200. if (loopcounter > 3000)
  201. msleep(2); /* temporary workaround */
  202. else {
  203. udelay(10);
  204. cond_resched();
  205. }
  206. }
  207. return -EIO;
  208. }
  209. EXPORT_SYMBOL_GPL(snd_hdac_bus_get_response);
  210. /*
  211. * Lowlevel interface
  212. */
  213. /**
  214. * snd_hdac_bus_enter_link_reset - enter link reset
  215. * @bus: HD-audio core bus
  216. *
  217. * Enter to the link reset state.
  218. */
  219. void snd_hdac_bus_enter_link_reset(struct hdac_bus *bus)
  220. {
  221. unsigned long timeout;
  222. /* reset controller */
  223. snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_RESET, 0);
  224. timeout = jiffies + msecs_to_jiffies(100);
  225. while ((snd_hdac_chip_readb(bus, GCTL) & AZX_GCTL_RESET) &&
  226. time_before(jiffies, timeout))
  227. usleep_range(500, 1000);
  228. }
  229. EXPORT_SYMBOL_GPL(snd_hdac_bus_enter_link_reset);
  230. /**
  231. * snd_hdac_bus_exit_link_reset - exit link reset
  232. * @bus: HD-audio core bus
  233. *
  234. * Exit from the link reset state.
  235. */
  236. void snd_hdac_bus_exit_link_reset(struct hdac_bus *bus)
  237. {
  238. unsigned long timeout;
  239. snd_hdac_chip_updateb(bus, GCTL, 0, AZX_GCTL_RESET);
  240. timeout = jiffies + msecs_to_jiffies(100);
  241. while (!snd_hdac_chip_readb(bus, GCTL) && time_before(jiffies, timeout))
  242. usleep_range(500, 1000);
  243. }
  244. EXPORT_SYMBOL_GPL(snd_hdac_bus_exit_link_reset);
  245. /* reset codec link */
  246. static int azx_reset(struct hdac_bus *bus, bool full_reset)
  247. {
  248. if (!full_reset)
  249. goto skip_reset;
  250. /* clear STATESTS */
  251. snd_hdac_chip_writew(bus, STATESTS, STATESTS_INT_MASK);
  252. /* reset controller */
  253. snd_hdac_bus_enter_link_reset(bus);
  254. /* delay for >= 100us for codec PLL to settle per spec
  255. * Rev 0.9 section 5.5.1
  256. */
  257. usleep_range(500, 1000);
  258. /* Bring controller out of reset */
  259. snd_hdac_bus_exit_link_reset(bus);
  260. /* Brent Chartrand said to wait >= 540us for codecs to initialize */
  261. usleep_range(1000, 1200);
  262. skip_reset:
  263. /* check to see if controller is ready */
  264. if (!snd_hdac_chip_readb(bus, GCTL)) {
  265. dev_dbg(bus->dev, "azx_reset: controller not ready!\n");
  266. return -EBUSY;
  267. }
  268. /* Accept unsolicited responses */
  269. snd_hdac_chip_updatel(bus, GCTL, 0, AZX_GCTL_UNSOL);
  270. /* detect codecs */
  271. if (!bus->codec_mask) {
  272. bus->codec_mask = snd_hdac_chip_readw(bus, STATESTS);
  273. dev_dbg(bus->dev, "codec_mask = 0x%lx\n", bus->codec_mask);
  274. }
  275. return 0;
  276. }
  277. /* enable interrupts */
  278. static void azx_int_enable(struct hdac_bus *bus)
  279. {
  280. /* enable controller CIE and GIE */
  281. snd_hdac_chip_updatel(bus, INTCTL, 0, AZX_INT_CTRL_EN | AZX_INT_GLOBAL_EN);
  282. }
  283. /* disable interrupts */
  284. static void azx_int_disable(struct hdac_bus *bus)
  285. {
  286. struct hdac_stream *azx_dev;
  287. /* disable interrupts in stream descriptor */
  288. list_for_each_entry(azx_dev, &bus->stream_list, list)
  289. snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_INT_MASK, 0);
  290. /* disable SIE for all streams */
  291. snd_hdac_chip_writeb(bus, INTCTL, 0);
  292. /* disable controller CIE and GIE */
  293. snd_hdac_chip_updatel(bus, INTCTL, AZX_INT_CTRL_EN | AZX_INT_GLOBAL_EN, 0);
  294. }
  295. /* clear interrupts */
  296. static void azx_int_clear(struct hdac_bus *bus)
  297. {
  298. struct hdac_stream *azx_dev;
  299. /* clear stream status */
  300. list_for_each_entry(azx_dev, &bus->stream_list, list)
  301. snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK);
  302. /* clear STATESTS */
  303. snd_hdac_chip_writew(bus, STATESTS, STATESTS_INT_MASK);
  304. /* clear rirb status */
  305. snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
  306. /* clear int status */
  307. snd_hdac_chip_writel(bus, INTSTS, AZX_INT_CTRL_EN | AZX_INT_ALL_STREAM);
  308. }
  309. /**
  310. * snd_hdac_bus_init_chip - reset and start the controller registers
  311. * @bus: HD-audio core bus
  312. * @full_reset: Do full reset
  313. */
  314. bool snd_hdac_bus_init_chip(struct hdac_bus *bus, bool full_reset)
  315. {
  316. if (bus->chip_init)
  317. return false;
  318. /* reset controller */
  319. azx_reset(bus, full_reset);
  320. /* clear interrupts */
  321. azx_int_clear(bus);
  322. /* initialize the codec command I/O */
  323. snd_hdac_bus_init_cmd_io(bus);
  324. /* enable interrupts after CORB/RIRB buffers are initialized above */
  325. azx_int_enable(bus);
  326. /* program the position buffer */
  327. if (bus->use_posbuf && bus->posbuf.addr) {
  328. snd_hdac_chip_writel(bus, DPLBASE, (u32)bus->posbuf.addr);
  329. snd_hdac_chip_writel(bus, DPUBASE, upper_32_bits(bus->posbuf.addr));
  330. }
  331. bus->chip_init = true;
  332. return true;
  333. }
  334. EXPORT_SYMBOL_GPL(snd_hdac_bus_init_chip);
  335. /**
  336. * snd_hdac_bus_stop_chip - disable the whole IRQ and I/Os
  337. * @bus: HD-audio core bus
  338. */
  339. void snd_hdac_bus_stop_chip(struct hdac_bus *bus)
  340. {
  341. if (!bus->chip_init)
  342. return;
  343. /* disable interrupts */
  344. azx_int_disable(bus);
  345. azx_int_clear(bus);
  346. /* disable CORB/RIRB */
  347. snd_hdac_bus_stop_cmd_io(bus);
  348. /* disable position buffer */
  349. if (bus->posbuf.addr) {
  350. snd_hdac_chip_writel(bus, DPLBASE, 0);
  351. snd_hdac_chip_writel(bus, DPUBASE, 0);
  352. }
  353. bus->chip_init = false;
  354. }
  355. EXPORT_SYMBOL_GPL(snd_hdac_bus_stop_chip);
  356. /**
  357. * snd_hdac_bus_handle_stream_irq - interrupt handler for streams
  358. * @bus: HD-audio core bus
  359. * @status: INTSTS register value
  360. * @ask: callback to be called for woken streams
  361. */
  362. void snd_hdac_bus_handle_stream_irq(struct hdac_bus *bus, unsigned int status,
  363. void (*ack)(struct hdac_bus *,
  364. struct hdac_stream *))
  365. {
  366. struct hdac_stream *azx_dev;
  367. u8 sd_status;
  368. list_for_each_entry(azx_dev, &bus->stream_list, list) {
  369. if (status & azx_dev->sd_int_sta_mask) {
  370. sd_status = snd_hdac_stream_readb(azx_dev, SD_STS);
  371. snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK);
  372. if (!azx_dev->substream || !azx_dev->running ||
  373. !(sd_status & SD_INT_COMPLETE))
  374. continue;
  375. if (ack)
  376. ack(bus, azx_dev);
  377. }
  378. }
  379. }
  380. EXPORT_SYMBOL_GPL(snd_hdac_bus_handle_stream_irq);
  381. /**
  382. * snd_hdac_bus_alloc_stream_pages - allocate BDL and other buffers
  383. * @bus: HD-audio core bus
  384. *
  385. * Call this after assigning the all streams.
  386. * Returns zero for success, or a negative error code.
  387. */
  388. int snd_hdac_bus_alloc_stream_pages(struct hdac_bus *bus)
  389. {
  390. struct hdac_stream *s;
  391. int num_streams = 0;
  392. int err;
  393. list_for_each_entry(s, &bus->stream_list, list) {
  394. /* allocate memory for the BDL for each stream */
  395. err = bus->io_ops->dma_alloc_pages(bus, SNDRV_DMA_TYPE_DEV,
  396. BDL_SIZE, &s->bdl);
  397. num_streams++;
  398. if (err < 0)
  399. return -ENOMEM;
  400. }
  401. if (WARN_ON(!num_streams))
  402. return -EINVAL;
  403. /* allocate memory for the position buffer */
  404. err = bus->io_ops->dma_alloc_pages(bus, SNDRV_DMA_TYPE_DEV,
  405. num_streams * 8, &bus->posbuf);
  406. if (err < 0)
  407. return -ENOMEM;
  408. list_for_each_entry(s, &bus->stream_list, list)
  409. s->posbuf = (__le32 *)(bus->posbuf.area + s->index * 8);
  410. /* single page (at least 4096 bytes) must suffice for both ringbuffes */
  411. return bus->io_ops->dma_alloc_pages(bus, SNDRV_DMA_TYPE_DEV,
  412. PAGE_SIZE, &bus->rb);
  413. }
  414. EXPORT_SYMBOL_GPL(snd_hdac_bus_alloc_stream_pages);
  415. /**
  416. * snd_hdac_bus_free_stream_pages - release BDL and other buffers
  417. * @bus: HD-audio core bus
  418. */
  419. void snd_hdac_bus_free_stream_pages(struct hdac_bus *bus)
  420. {
  421. struct hdac_stream *s;
  422. list_for_each_entry(s, &bus->stream_list, list) {
  423. if (s->bdl.area)
  424. bus->io_ops->dma_free_pages(bus, &s->bdl);
  425. }
  426. if (bus->rb.area)
  427. bus->io_ops->dma_free_pages(bus, &bus->rb);
  428. if (bus->posbuf.area)
  429. bus->io_ops->dma_free_pages(bus, &bus->posbuf);
  430. }
  431. EXPORT_SYMBOL_GPL(snd_hdac_bus_free_stream_pages);