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