hdac_controller.c 20 KB

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