hdac_controller.c 13 KB

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