controller.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HD-audio controller helpers
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/delay.h>
  7. #include <linux/export.h>
  8. #include <sound/core.h>
  9. #include <sound/hdaudio.h>
  10. #include <sound/hda_register.h>
  11. #include "local.h"
  12. /* clear CORB read pointer properly */
  13. static void azx_clear_corbrp(struct hdac_bus *bus)
  14. {
  15. int timeout;
  16. for (timeout = 1000; timeout > 0; timeout--) {
  17. if (snd_hdac_chip_readw(bus, CORBRP) & AZX_CORBRP_RST)
  18. break;
  19. udelay(1);
  20. }
  21. if (timeout <= 0)
  22. dev_err(bus->dev, "CORB reset timeout#1, CORBRP = %d\n",
  23. snd_hdac_chip_readw(bus, CORBRP));
  24. snd_hdac_chip_writew(bus, CORBRP, 0);
  25. for (timeout = 1000; timeout > 0; timeout--) {
  26. if (snd_hdac_chip_readw(bus, CORBRP) == 0)
  27. break;
  28. udelay(1);
  29. }
  30. if (timeout <= 0)
  31. dev_err(bus->dev, "CORB reset timeout#2, CORBRP = %d\n",
  32. snd_hdac_chip_readw(bus, CORBRP));
  33. }
  34. /**
  35. * snd_hdac_bus_init_cmd_io - set up CORB/RIRB buffers
  36. * @bus: HD-audio core bus
  37. */
  38. void snd_hdac_bus_init_cmd_io(struct hdac_bus *bus)
  39. {
  40. WARN_ON_ONCE(!bus->rb.area);
  41. guard(spinlock_irq)(&bus->reg_lock);
  42. /* CORB set up */
  43. bus->corb.addr = bus->rb.addr;
  44. bus->corb.buf = (__le32 *)bus->rb.area;
  45. snd_hdac_chip_writel(bus, CORBLBASE, (u32)(bus->corb.addr + bus->addr_offset));
  46. snd_hdac_chip_writel(bus, CORBUBASE, upper_32_bits(bus->corb.addr + bus->addr_offset));
  47. /* set the corb size to 256 entries (ULI requires explicitly) */
  48. snd_hdac_chip_writeb(bus, CORBSIZE, 0x02);
  49. /* set the corb write pointer to 0 */
  50. snd_hdac_chip_writew(bus, CORBWP, 0);
  51. /* reset the corb hw read pointer */
  52. snd_hdac_chip_writew(bus, CORBRP, AZX_CORBRP_RST);
  53. if (!bus->corbrp_self_clear)
  54. azx_clear_corbrp(bus);
  55. /* enable corb dma */
  56. if (!bus->use_pio_for_commands)
  57. snd_hdac_chip_writeb(bus, CORBCTL, AZX_CORBCTL_RUN);
  58. /* RIRB set up */
  59. bus->rirb.addr = bus->rb.addr + 2048;
  60. bus->rirb.buf = (__le32 *)(bus->rb.area + 2048);
  61. bus->rirb.wp = bus->rirb.rp = 0;
  62. memset(bus->rirb.cmds, 0, sizeof(bus->rirb.cmds));
  63. snd_hdac_chip_writel(bus, RIRBLBASE, (u32)(bus->rirb.addr + bus->addr_offset));
  64. snd_hdac_chip_writel(bus, RIRBUBASE, upper_32_bits(bus->rirb.addr + bus->addr_offset));
  65. /* set the rirb size to 256 entries (ULI requires explicitly) */
  66. snd_hdac_chip_writeb(bus, RIRBSIZE, 0x02);
  67. /* reset the rirb hw write pointer */
  68. snd_hdac_chip_writew(bus, RIRBWP, AZX_RIRBWP_RST);
  69. /* set N=1, get RIRB response interrupt for new entry */
  70. snd_hdac_chip_writew(bus, RINTCNT, 1);
  71. /* enable rirb dma and response irq */
  72. if (bus->not_use_interrupts)
  73. snd_hdac_chip_writeb(bus, RIRBCTL, AZX_RBCTL_DMA_EN);
  74. else
  75. snd_hdac_chip_writeb(bus, RIRBCTL, AZX_RBCTL_DMA_EN | AZX_RBCTL_IRQ_EN);
  76. /* Accept unsolicited responses */
  77. snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, AZX_GCTL_UNSOL);
  78. }
  79. EXPORT_SYMBOL_GPL(snd_hdac_bus_init_cmd_io);
  80. /* wait for cmd dmas till they are stopped */
  81. static void hdac_wait_for_cmd_dmas(struct hdac_bus *bus)
  82. {
  83. unsigned long timeout;
  84. timeout = jiffies + msecs_to_jiffies(100);
  85. while ((snd_hdac_chip_readb(bus, RIRBCTL) & AZX_RBCTL_DMA_EN)
  86. && time_before(jiffies, timeout))
  87. udelay(10);
  88. timeout = jiffies + msecs_to_jiffies(100);
  89. while ((snd_hdac_chip_readb(bus, CORBCTL) & AZX_CORBCTL_RUN)
  90. && time_before(jiffies, timeout))
  91. udelay(10);
  92. }
  93. /**
  94. * snd_hdac_bus_stop_cmd_io - clean up CORB/RIRB buffers
  95. * @bus: HD-audio core bus
  96. */
  97. void snd_hdac_bus_stop_cmd_io(struct hdac_bus *bus)
  98. {
  99. scoped_guard(spinlock_irq, &bus->reg_lock) {
  100. /* disable ringbuffer DMAs */
  101. snd_hdac_chip_writeb(bus, RIRBCTL, 0);
  102. snd_hdac_chip_writeb(bus, CORBCTL, 0);
  103. }
  104. hdac_wait_for_cmd_dmas(bus);
  105. guard(spinlock_irq)(&bus->reg_lock);
  106. /* disable unsolicited responses */
  107. snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, 0);
  108. }
  109. EXPORT_SYMBOL_GPL(snd_hdac_bus_stop_cmd_io);
  110. static unsigned int azx_command_addr(u32 cmd)
  111. {
  112. unsigned int addr = cmd >> 28;
  113. if (snd_BUG_ON(addr >= HDA_MAX_CODECS))
  114. addr = 0;
  115. return addr;
  116. }
  117. /* receive an Immediate Response with PIO */
  118. static int snd_hdac_bus_wait_for_pio_response(struct hdac_bus *bus,
  119. unsigned int addr)
  120. {
  121. int timeout = 50;
  122. while (timeout--) {
  123. /* check IRV bit */
  124. if (snd_hdac_chip_readw(bus, IRS) & AZX_IRS_VALID) {
  125. /* reuse rirb.res as the response return value */
  126. bus->rirb.res[addr] = snd_hdac_chip_readl(bus, IR);
  127. return 0;
  128. }
  129. udelay(1);
  130. }
  131. dev_dbg_ratelimited(bus->dev, "get_response_pio timeout: IRS=%#x\n",
  132. snd_hdac_chip_readw(bus, IRS));
  133. bus->rirb.res[addr] = -1;
  134. return -EIO;
  135. }
  136. /**
  137. * snd_hdac_bus_send_cmd_pio - send a command verb via Immediate Command
  138. * @bus: HD-audio core bus
  139. * @val: encoded verb value to send
  140. *
  141. * Returns zero for success or a negative error code.
  142. */
  143. static int snd_hdac_bus_send_cmd_pio(struct hdac_bus *bus, unsigned int val)
  144. {
  145. unsigned int addr = azx_command_addr(val);
  146. int timeout = 50;
  147. guard(spinlock_irq)(&bus->reg_lock);
  148. while (timeout--) {
  149. /* check ICB bit */
  150. if (!((snd_hdac_chip_readw(bus, IRS) & AZX_IRS_BUSY))) {
  151. /* Clear IRV bit */
  152. snd_hdac_chip_updatew(bus, IRS, AZX_IRS_VALID, AZX_IRS_VALID);
  153. snd_hdac_chip_writel(bus, IC, val);
  154. /* Set ICB bit */
  155. snd_hdac_chip_updatew(bus, IRS, AZX_IRS_BUSY, AZX_IRS_BUSY);
  156. return snd_hdac_bus_wait_for_pio_response(bus, addr);
  157. }
  158. udelay(1);
  159. }
  160. dev_dbg_ratelimited(bus->dev, "send_cmd_pio timeout: IRS=%#x, val=%#x\n",
  161. snd_hdac_chip_readw(bus, IRS), val);
  162. return -EIO;
  163. }
  164. /**
  165. * snd_hdac_bus_get_response_pio - receive a response via Immediate Response
  166. * @bus: HD-audio core bus
  167. * @addr: codec address
  168. * @res: pointer to store the value, NULL when not needed
  169. *
  170. * Returns zero if a value is read, or a negative error code.
  171. */
  172. static int snd_hdac_bus_get_response_pio(struct hdac_bus *bus,
  173. unsigned int addr, unsigned int *res)
  174. {
  175. if (res)
  176. *res = bus->rirb.res[addr];
  177. return 0;
  178. }
  179. /**
  180. * snd_hdac_bus_send_cmd_corb - send a command verb via CORB
  181. * @bus: HD-audio core bus
  182. * @val: encoded verb value to send
  183. *
  184. * Returns zero for success or a negative error code.
  185. */
  186. static int snd_hdac_bus_send_cmd_corb(struct hdac_bus *bus, unsigned int val)
  187. {
  188. unsigned int addr = azx_command_addr(val);
  189. unsigned int wp, rp;
  190. guard(spinlock_irq)(&bus->reg_lock);
  191. bus->last_cmd[azx_command_addr(val)] = val;
  192. /* add command to corb */
  193. wp = snd_hdac_chip_readw(bus, CORBWP);
  194. if (wp == 0xffff) {
  195. /* something wrong, controller likely turned to D3 */
  196. return -EIO;
  197. }
  198. wp++;
  199. wp %= AZX_MAX_CORB_ENTRIES;
  200. rp = snd_hdac_chip_readw(bus, CORBRP);
  201. if (wp == rp) {
  202. /* oops, it's full */
  203. return -EAGAIN;
  204. }
  205. bus->rirb.cmds[addr]++;
  206. bus->corb.buf[wp] = cpu_to_le32(val);
  207. snd_hdac_chip_writew(bus, CORBWP, wp);
  208. return 0;
  209. }
  210. #define AZX_RIRB_EX_UNSOL_EV (1<<4)
  211. /**
  212. * snd_hdac_bus_update_rirb - retrieve RIRB entries
  213. * @bus: HD-audio core bus
  214. *
  215. * Usually called from interrupt handler.
  216. * The caller needs bus->reg_lock spinlock before calling this.
  217. */
  218. void snd_hdac_bus_update_rirb(struct hdac_bus *bus)
  219. {
  220. unsigned int rp, wp;
  221. unsigned int addr;
  222. u32 res, res_ex;
  223. wp = snd_hdac_chip_readw(bus, RIRBWP);
  224. if (wp == 0xffff) {
  225. /* something wrong, controller likely turned to D3 */
  226. return;
  227. }
  228. if (wp == bus->rirb.wp)
  229. return;
  230. bus->rirb.wp = wp;
  231. while (bus->rirb.rp != wp) {
  232. bus->rirb.rp++;
  233. bus->rirb.rp %= AZX_MAX_RIRB_ENTRIES;
  234. rp = bus->rirb.rp << 1; /* an RIRB entry is 8-bytes */
  235. res_ex = le32_to_cpu(bus->rirb.buf[rp + 1]);
  236. res = le32_to_cpu(bus->rirb.buf[rp]);
  237. addr = res_ex & 0xf;
  238. if (addr >= HDA_MAX_CODECS) {
  239. dev_err(bus->dev,
  240. "spurious response %#x:%#x, rp = %d, wp = %d",
  241. res, res_ex, bus->rirb.rp, wp);
  242. snd_BUG();
  243. } else if (res_ex & AZX_RIRB_EX_UNSOL_EV)
  244. snd_hdac_bus_queue_event(bus, res, res_ex);
  245. else if (bus->rirb.cmds[addr]) {
  246. bus->rirb.res[addr] = res;
  247. bus->rirb.cmds[addr]--;
  248. if (!bus->rirb.cmds[addr] &&
  249. waitqueue_active(&bus->rirb_wq))
  250. wake_up(&bus->rirb_wq);
  251. } else {
  252. dev_err_ratelimited(bus->dev,
  253. "spurious response %#x:%#x, last cmd=%#08x\n",
  254. res, res_ex, bus->last_cmd[addr]);
  255. }
  256. }
  257. }
  258. EXPORT_SYMBOL_GPL(snd_hdac_bus_update_rirb);
  259. /**
  260. * snd_hdac_bus_get_response_rirb - receive a response via RIRB
  261. * @bus: HD-audio core bus
  262. * @addr: codec address
  263. * @res: pointer to store the value, NULL when not needed
  264. *
  265. * Returns zero if a value is read, or a negative error code.
  266. */
  267. static int snd_hdac_bus_get_response_rirb(struct hdac_bus *bus,
  268. unsigned int addr, unsigned int *res)
  269. {
  270. unsigned long timeout;
  271. unsigned long loopcounter;
  272. wait_queue_entry_t wait;
  273. bool warned = false;
  274. init_wait_entry(&wait, 0);
  275. timeout = jiffies + msecs_to_jiffies(1000);
  276. for (loopcounter = 0;; loopcounter++) {
  277. scoped_guard(spinlock_irq, &bus->reg_lock) {
  278. if (!bus->polling_mode)
  279. prepare_to_wait(&bus->rirb_wq, &wait,
  280. TASK_UNINTERRUPTIBLE);
  281. if (bus->polling_mode)
  282. snd_hdac_bus_update_rirb(bus);
  283. if (!bus->rirb.cmds[addr]) {
  284. if (res)
  285. *res = bus->rirb.res[addr]; /* the last value */
  286. if (!bus->polling_mode)
  287. finish_wait(&bus->rirb_wq, &wait);
  288. return 0;
  289. }
  290. }
  291. if (time_after(jiffies, timeout))
  292. break;
  293. #define LOOP_COUNT_MAX 3000
  294. if (!bus->polling_mode) {
  295. schedule_timeout(msecs_to_jiffies(2));
  296. } else if (bus->needs_damn_long_delay ||
  297. loopcounter > LOOP_COUNT_MAX) {
  298. if (loopcounter > LOOP_COUNT_MAX && !warned) {
  299. dev_dbg_ratelimited(bus->dev,
  300. "too slow response, last cmd=%#08x\n",
  301. bus->last_cmd[addr]);
  302. warned = true;
  303. }
  304. msleep(2); /* temporary workaround */
  305. } else {
  306. udelay(10);
  307. cond_resched();
  308. }
  309. }
  310. if (!bus->polling_mode)
  311. finish_wait(&bus->rirb_wq, &wait);
  312. return -EIO;
  313. }
  314. /**
  315. * snd_hdac_bus_send_cmd - send a command verb via CORB or PIO
  316. * @bus: HD-audio core bus
  317. * @val: encoded verb value to send
  318. *
  319. * Returns zero for success or a negative error code.
  320. */
  321. int snd_hdac_bus_send_cmd(struct hdac_bus *bus, unsigned int val)
  322. {
  323. if (bus->use_pio_for_commands)
  324. return snd_hdac_bus_send_cmd_pio(bus, val);
  325. return snd_hdac_bus_send_cmd_corb(bus, val);
  326. }
  327. EXPORT_SYMBOL_GPL(snd_hdac_bus_send_cmd);
  328. /**
  329. * snd_hdac_bus_get_response - receive a response via RIRB or PIO
  330. * @bus: HD-audio core bus
  331. * @addr: codec address
  332. * @res: pointer to store the value, NULL when not needed
  333. *
  334. * Returns zero if a value is read, or a negative error code.
  335. */
  336. int snd_hdac_bus_get_response(struct hdac_bus *bus, unsigned int addr,
  337. unsigned int *res)
  338. {
  339. if (bus->use_pio_for_commands)
  340. return snd_hdac_bus_get_response_pio(bus, addr, res);
  341. return snd_hdac_bus_get_response_rirb(bus, addr, res);
  342. }
  343. EXPORT_SYMBOL_GPL(snd_hdac_bus_get_response);
  344. #define HDAC_MAX_CAPS 10
  345. /**
  346. * snd_hdac_bus_parse_capabilities - parse capability structure
  347. * @bus: the pointer to bus object
  348. *
  349. * Returns 0 if successful, or a negative error code.
  350. */
  351. int snd_hdac_bus_parse_capabilities(struct hdac_bus *bus)
  352. {
  353. unsigned int cur_cap;
  354. unsigned int offset;
  355. unsigned int counter = 0;
  356. offset = snd_hdac_chip_readw(bus, LLCH);
  357. /* Lets walk the linked capabilities list */
  358. do {
  359. cur_cap = _snd_hdac_chip_readl(bus, offset);
  360. dev_dbg(bus->dev, "Capability version: 0x%x\n",
  361. (cur_cap & AZX_CAP_HDR_VER_MASK) >> AZX_CAP_HDR_VER_OFF);
  362. dev_dbg(bus->dev, "HDA capability ID: 0x%x\n",
  363. (cur_cap & AZX_CAP_HDR_ID_MASK) >> AZX_CAP_HDR_ID_OFF);
  364. if (cur_cap == -1) {
  365. dev_dbg(bus->dev, "Invalid capability reg read\n");
  366. break;
  367. }
  368. switch ((cur_cap & AZX_CAP_HDR_ID_MASK) >> AZX_CAP_HDR_ID_OFF) {
  369. case AZX_ML_CAP_ID:
  370. dev_dbg(bus->dev, "Found ML capability\n");
  371. bus->mlcap = bus->remap_addr + offset;
  372. break;
  373. case AZX_GTS_CAP_ID:
  374. dev_dbg(bus->dev, "Found GTS capability offset=%x\n", offset);
  375. bus->gtscap = bus->remap_addr + offset;
  376. break;
  377. case AZX_PP_CAP_ID:
  378. /* PP capability found, the Audio DSP is present */
  379. dev_dbg(bus->dev, "Found PP capability offset=%x\n", offset);
  380. bus->ppcap = bus->remap_addr + offset;
  381. break;
  382. case AZX_SPB_CAP_ID:
  383. /* SPIB capability found, handler function */
  384. dev_dbg(bus->dev, "Found SPB capability\n");
  385. bus->spbcap = bus->remap_addr + offset;
  386. break;
  387. case AZX_DRSM_CAP_ID:
  388. /* DMA resume capability found, handler function */
  389. dev_dbg(bus->dev, "Found DRSM capability\n");
  390. bus->drsmcap = bus->remap_addr + offset;
  391. break;
  392. default:
  393. dev_err(bus->dev, "Unknown capability %d\n", cur_cap);
  394. cur_cap = 0;
  395. break;
  396. }
  397. counter++;
  398. if (counter > HDAC_MAX_CAPS) {
  399. dev_err(bus->dev, "We exceeded HDAC capabilities!!!\n");
  400. break;
  401. }
  402. /* read the offset of next capability */
  403. offset = cur_cap & AZX_CAP_HDR_NXT_PTR_MASK;
  404. } while (offset);
  405. return 0;
  406. }
  407. EXPORT_SYMBOL_GPL(snd_hdac_bus_parse_capabilities);
  408. /*
  409. * Lowlevel interface
  410. */
  411. /**
  412. * snd_hdac_bus_enter_link_reset - enter link reset
  413. * @bus: HD-audio core bus
  414. *
  415. * Enter to the link reset state.
  416. */
  417. void snd_hdac_bus_enter_link_reset(struct hdac_bus *bus)
  418. {
  419. unsigned long timeout;
  420. /* reset controller */
  421. snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_RESET, 0);
  422. timeout = jiffies + msecs_to_jiffies(100);
  423. while ((snd_hdac_chip_readb(bus, GCTL) & AZX_GCTL_RESET) &&
  424. time_before(jiffies, timeout))
  425. usleep_range(500, 1000);
  426. }
  427. EXPORT_SYMBOL_GPL(snd_hdac_bus_enter_link_reset);
  428. /**
  429. * snd_hdac_bus_exit_link_reset - exit link reset
  430. * @bus: HD-audio core bus
  431. *
  432. * Exit from the link reset state.
  433. */
  434. void snd_hdac_bus_exit_link_reset(struct hdac_bus *bus)
  435. {
  436. unsigned long timeout;
  437. snd_hdac_chip_updateb(bus, GCTL, AZX_GCTL_RESET, AZX_GCTL_RESET);
  438. timeout = jiffies + msecs_to_jiffies(100);
  439. while (!snd_hdac_chip_readb(bus, GCTL) && time_before(jiffies, timeout))
  440. usleep_range(500, 1000);
  441. }
  442. EXPORT_SYMBOL_GPL(snd_hdac_bus_exit_link_reset);
  443. /* reset codec link */
  444. int snd_hdac_bus_reset_link(struct hdac_bus *bus, bool full_reset)
  445. {
  446. if (!full_reset)
  447. goto skip_reset;
  448. /* clear STATESTS if not in reset */
  449. if (snd_hdac_chip_readb(bus, GCTL) & AZX_GCTL_RESET)
  450. snd_hdac_chip_writew(bus, STATESTS, STATESTS_INT_MASK);
  451. /* reset controller */
  452. snd_hdac_bus_enter_link_reset(bus);
  453. /* delay for >= 100us for codec PLL to settle per spec
  454. * Rev 0.9 section 5.5.1
  455. */
  456. usleep_range(500, 1000);
  457. /* Bring controller out of reset */
  458. snd_hdac_bus_exit_link_reset(bus);
  459. /* Brent Chartrand said to wait >= 540us for codecs to initialize */
  460. usleep_range(1000, 1200);
  461. skip_reset:
  462. /* check to see if controller is ready */
  463. if (!snd_hdac_chip_readb(bus, GCTL)) {
  464. dev_dbg(bus->dev, "controller not ready!\n");
  465. return -EBUSY;
  466. }
  467. /* detect codecs */
  468. if (!bus->codec_mask) {
  469. bus->codec_mask = snd_hdac_chip_readw(bus, STATESTS);
  470. dev_dbg(bus->dev, "codec_mask = 0x%lx\n", bus->codec_mask);
  471. }
  472. return 0;
  473. }
  474. EXPORT_SYMBOL_GPL(snd_hdac_bus_reset_link);
  475. /* enable interrupts */
  476. static void azx_int_enable(struct hdac_bus *bus)
  477. {
  478. /* enable controller CIE and GIE */
  479. snd_hdac_chip_updatel(bus, INTCTL,
  480. AZX_INT_CTRL_EN | AZX_INT_GLOBAL_EN,
  481. AZX_INT_CTRL_EN | AZX_INT_GLOBAL_EN);
  482. }
  483. /* disable interrupts */
  484. static void azx_int_disable(struct hdac_bus *bus)
  485. {
  486. struct hdac_stream *azx_dev;
  487. /* disable interrupts in stream descriptor */
  488. list_for_each_entry(azx_dev, &bus->stream_list, list)
  489. snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_INT_MASK, 0);
  490. /* disable SIE for all streams & disable controller CIE and GIE */
  491. snd_hdac_chip_writel(bus, INTCTL, 0);
  492. }
  493. /* clear interrupts */
  494. static void azx_int_clear(struct hdac_bus *bus)
  495. {
  496. struct hdac_stream *azx_dev;
  497. /* clear stream status */
  498. list_for_each_entry(azx_dev, &bus->stream_list, list)
  499. snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK);
  500. /* clear STATESTS */
  501. snd_hdac_chip_writew(bus, STATESTS, STATESTS_INT_MASK);
  502. /* clear rirb status */
  503. snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
  504. /* clear int status */
  505. snd_hdac_chip_writel(bus, INTSTS, AZX_INT_CTRL_EN | AZX_INT_ALL_STREAM);
  506. }
  507. /**
  508. * snd_hdac_bus_init_chip - reset and start the controller registers
  509. * @bus: HD-audio core bus
  510. * @full_reset: Do full reset
  511. */
  512. bool snd_hdac_bus_init_chip(struct hdac_bus *bus, bool full_reset)
  513. {
  514. if (bus->chip_init)
  515. return false;
  516. /* reset controller */
  517. snd_hdac_bus_reset_link(bus, full_reset);
  518. /* clear interrupts */
  519. azx_int_clear(bus);
  520. /* initialize the codec command I/O */
  521. snd_hdac_bus_init_cmd_io(bus);
  522. /* enable interrupts after CORB/RIRB buffers are initialized above */
  523. azx_int_enable(bus);
  524. /* program the position buffer */
  525. if (bus->use_posbuf && bus->posbuf.addr) {
  526. snd_hdac_chip_writel(bus, DPLBASE, (u32)(bus->posbuf.addr + bus->addr_offset));
  527. snd_hdac_chip_writel(bus, DPUBASE, upper_32_bits(bus->posbuf.addr + bus->addr_offset));
  528. }
  529. bus->chip_init = true;
  530. return true;
  531. }
  532. EXPORT_SYMBOL_GPL(snd_hdac_bus_init_chip);
  533. /**
  534. * snd_hdac_bus_stop_chip - disable the whole IRQ and I/Os
  535. * @bus: HD-audio core bus
  536. */
  537. void snd_hdac_bus_stop_chip(struct hdac_bus *bus)
  538. {
  539. if (!bus->chip_init)
  540. return;
  541. /* disable interrupts */
  542. azx_int_disable(bus);
  543. azx_int_clear(bus);
  544. /* disable CORB/RIRB */
  545. snd_hdac_bus_stop_cmd_io(bus);
  546. /* disable position buffer */
  547. if (bus->posbuf.addr) {
  548. snd_hdac_chip_writel(bus, DPLBASE, 0);
  549. snd_hdac_chip_writel(bus, DPUBASE, 0);
  550. }
  551. bus->chip_init = false;
  552. }
  553. EXPORT_SYMBOL_GPL(snd_hdac_bus_stop_chip);
  554. /**
  555. * snd_hdac_bus_handle_stream_irq - interrupt handler for streams
  556. * @bus: HD-audio core bus
  557. * @status: INTSTS register value
  558. * @ack: callback to be called for woken streams
  559. *
  560. * Returns the bits of handled streams, or zero if no stream is handled.
  561. */
  562. int snd_hdac_bus_handle_stream_irq(struct hdac_bus *bus, unsigned int status,
  563. void (*ack)(struct hdac_bus *,
  564. struct hdac_stream *))
  565. {
  566. struct hdac_stream *azx_dev;
  567. u8 sd_status;
  568. int handled = 0;
  569. list_for_each_entry(azx_dev, &bus->stream_list, list) {
  570. if (status & azx_dev->sd_int_sta_mask) {
  571. sd_status = snd_hdac_stream_readb(azx_dev, SD_STS);
  572. snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK);
  573. handled |= 1 << azx_dev->index;
  574. if ((!azx_dev->substream && !azx_dev->cstream) ||
  575. !azx_dev->running || !(sd_status & SD_INT_COMPLETE))
  576. continue;
  577. if (ack)
  578. ack(bus, azx_dev);
  579. }
  580. }
  581. return handled;
  582. }
  583. EXPORT_SYMBOL_GPL(snd_hdac_bus_handle_stream_irq);
  584. /**
  585. * snd_hdac_bus_alloc_stream_pages - allocate BDL and other buffers
  586. * @bus: HD-audio core bus
  587. *
  588. * Call this after assigning the all streams.
  589. * Returns zero for success, or a negative error code.
  590. */
  591. int snd_hdac_bus_alloc_stream_pages(struct hdac_bus *bus)
  592. {
  593. struct hdac_stream *s;
  594. int num_streams = 0;
  595. int dma_type = bus->dma_type ? bus->dma_type : SNDRV_DMA_TYPE_DEV;
  596. int err;
  597. list_for_each_entry(s, &bus->stream_list, list) {
  598. /* allocate memory for the BDL for each stream */
  599. err = snd_dma_alloc_pages(dma_type, bus->dev,
  600. BDL_SIZE, &s->bdl);
  601. num_streams++;
  602. if (err < 0)
  603. return -ENOMEM;
  604. }
  605. if (WARN_ON(!num_streams))
  606. return -EINVAL;
  607. /* allocate memory for the position buffer */
  608. err = snd_dma_alloc_pages(dma_type, bus->dev,
  609. num_streams * 8, &bus->posbuf);
  610. if (err < 0)
  611. return -ENOMEM;
  612. list_for_each_entry(s, &bus->stream_list, list)
  613. s->posbuf = (__le32 *)(bus->posbuf.area + s->index * 8);
  614. /* single page (at least 4096 bytes) must suffice for both ringbuffes */
  615. return snd_dma_alloc_pages(dma_type, bus->dev, PAGE_SIZE, &bus->rb);
  616. }
  617. EXPORT_SYMBOL_GPL(snd_hdac_bus_alloc_stream_pages);
  618. /**
  619. * snd_hdac_bus_free_stream_pages - release BDL and other buffers
  620. * @bus: HD-audio core bus
  621. */
  622. void snd_hdac_bus_free_stream_pages(struct hdac_bus *bus)
  623. {
  624. struct hdac_stream *s;
  625. list_for_each_entry(s, &bus->stream_list, list) {
  626. if (s->bdl.area)
  627. snd_dma_free_pages(&s->bdl);
  628. }
  629. if (bus->rb.area)
  630. snd_dma_free_pages(&bus->rb);
  631. if (bus->posbuf.area)
  632. snd_dma_free_pages(&bus->posbuf);
  633. }
  634. EXPORT_SYMBOL_GPL(snd_hdac_bus_free_stream_pages);
  635. /**
  636. * snd_hdac_bus_link_power - power up/down codec link
  637. * @codec: HD-audio device
  638. * @enable: whether to power-up the link
  639. */
  640. void snd_hdac_bus_link_power(struct hdac_device *codec, bool enable)
  641. {
  642. if (enable)
  643. set_bit(codec->addr, &codec->bus->codec_powered);
  644. else
  645. clear_bit(codec->addr, &codec->bus->codec_powered);
  646. }
  647. EXPORT_SYMBOL_GPL(snd_hdac_bus_link_power);