summaryrefslogtreecommitdiffstats
path: root/src/soc/qualcomm/common/qup_se_handler.c
blob: 7dd4f452af40c813a55bf9d70c08e5d658400e14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* SPDX-License-Identifier: GPL-2.0-only */

#include <console/console.h>
#include <soc/qup_se_handlers_common.h>
#include <soc/qcom_qup_se.h>

u32 qup_wait_for_m_irq(unsigned int bus)
{
	struct stopwatch sw;
	unsigned int m_irq = 0;
	struct qup_regs *regs = qup[bus].regs;

	stopwatch_init_usecs_expire(&sw, 25);
	while (!stopwatch_expired(&sw)) {
		m_irq = read32(&regs->geni_m_irq_status);
		if (m_irq)
			break;
	}
	return m_irq;
}

u32 qup_wait_for_s_irq(unsigned int bus)
{
	struct stopwatch sw;
	unsigned int s_irq = 0;
	struct qup_regs *regs = qup[bus].regs;

	stopwatch_init_usecs_expire(&sw, 25);
	while (!stopwatch_expired(&sw)) {
		s_irq = read32(&regs->geni_s_irq_status);
		if (s_irq)
			break;
	}
	return s_irq;
}

static int handle_tx(unsigned int bus, const u8 *dout,
	unsigned int tx_rem_bytes)
{
	int max_bytes = 0;
	struct qup_regs *regs = qup[bus].regs;

	max_bytes = (FIFO_DEPTH - TX_WATERMARK) * BYTES_PER_FIFO_WORD;
	max_bytes = MIN(tx_rem_bytes, max_bytes);

	buffer_to_fifo32((void *)dout, max_bytes, &regs->geni_tx_fifon,
					0, BYTES_PER_FIFO_WORD);

	if (tx_rem_bytes == max_bytes)
		write32(&regs->geni_tx_watermark_reg, 0);
	return max_bytes;
}

static int handle_rx(unsigned int bus, u8 *din, unsigned int rx_rem_bytes)
{
	struct qup_regs *regs = qup[bus].regs;
	u32 rx_fifo_status = read32(&regs->geni_rx_fifo_status);
	int rx_bytes = 0;

	rx_bytes = (rx_fifo_status & RX_FIFO_WC_MSK) * BYTES_PER_FIFO_WORD;
	rx_bytes = MIN(rx_rem_bytes, rx_bytes);

	buffer_from_fifo32(din, rx_bytes, &regs->geni_rx_fifon,
					0, BYTES_PER_FIFO_WORD);
	return rx_bytes;
}

void qup_m_cancel_and_abort(unsigned int bus)
{
	struct qup_regs *regs = qup[bus].regs;
	struct stopwatch sw;
	unsigned int m_irq;

	write32(&regs->geni_tx_watermark_reg, 0);
	write32(&regs->geni_m_cmd_ctrl_reg, M_GENI_CMD_CANCEL);

	stopwatch_init_msecs_expire(&sw, 100);
	do {
		m_irq = qup_wait_for_m_irq(bus);
		if (m_irq & M_CMD_CANCEL_EN) {
			write32(&regs->geni_m_irq_clear, m_irq);
			break;
		}
		write32(&regs->geni_m_irq_clear, m_irq);
	} while (!stopwatch_expired(&sw));

	if (!(m_irq & M_CMD_CANCEL_EN)) {
		printk(BIOS_INFO, "%s:Cancel failed, Abort the operation\n",
								__func__);

		write32(&regs->geni_m_cmd_ctrl_reg, M_GENI_CMD_ABORT);
		stopwatch_init_msecs_expire(&sw, 100);
		do {
			m_irq = qup_wait_for_m_irq(bus);
			if (m_irq & M_CMD_ABORT_EN) {
				write32(&regs->geni_m_irq_clear, m_irq);
				break;
			}
			write32(&regs->geni_m_irq_clear, m_irq);
		} while (!stopwatch_expired(&sw));

		if (!(m_irq & M_CMD_ABORT_EN))
			printk(BIOS_INFO, "%s:Abort failed\n", __func__);
	}
}

void qup_s_cancel_and_abort(unsigned int bus)
{
	struct qup_regs *regs = qup[bus].regs;
	struct stopwatch sw;
	unsigned int s_irq;
	u32 rx_fifo_status;
	u8 buf[64]; /* FIFO size */

	write32(&regs->geni_tx_watermark_reg, 0);
	write32(&regs->geni_s_cmd_ctrl_reg, S_GENI_CMD_CANCEL);

	stopwatch_init_msecs_expire(&sw, 100);
	do {
		s_irq = qup_wait_for_s_irq(bus);
		rx_fifo_status  = read32(&regs->geni_rx_fifo_status);
		if (rx_fifo_status & RX_LAST)
			/* Read whatever data available in FIFO */
			handle_rx(bus, buf, 64);
		if (s_irq & S_CMD_CANCEL_EN) {
			write32(&regs->geni_s_irq_clear, s_irq);
			break;
		}
		write32(&regs->geni_s_irq_clear, s_irq);
	} while (!stopwatch_expired(&sw));

	if (!(s_irq & S_CMD_CANCEL_EN)) {
		printk(BIOS_INFO, "%s:Cancel failed, Abort the operation\n",
								__func__);

		write32(&regs->geni_s_cmd_ctrl_reg, S_GENI_CMD_ABORT);
		stopwatch_init_msecs_expire(&sw, 100);
		do {
			s_irq = qup_wait_for_s_irq(bus);
			if (s_irq & S_CMD_ABORT_EN) {
				write32(&regs->geni_s_irq_clear, s_irq);
				break;
			}
			write32(&regs->geni_s_irq_clear, s_irq);
		} while (!stopwatch_expired(&sw));

		if (!(s_irq & S_CMD_ABORT_EN))
			printk(BIOS_INFO, "%s:Abort failed\n", __func__);
	}
}

int qup_handle_transfer(unsigned int bus, const void *dout, void *din, int size,
			struct stopwatch *timeout)
{
	unsigned int m_irq;
	unsigned int rx_rem_bytes = din ? size : 0;
	unsigned int tx_rem_bytes = dout ? size : 0;
	struct qup_regs *regs = qup[bus].regs;

	do {
		m_irq = qup_wait_for_m_irq(bus);
		if ((m_irq & M_RX_FIFO_WATERMARK_EN) ||
					(m_irq & M_RX_FIFO_LAST_EN))
			rx_rem_bytes -= handle_rx(bus, din + size
						- rx_rem_bytes, rx_rem_bytes);
		if (m_irq & M_TX_FIFO_WATERMARK_EN)
			tx_rem_bytes -= handle_tx(bus, dout + size
						- tx_rem_bytes, tx_rem_bytes);
		if (m_irq & M_CMD_DONE_EN) {
			write32(&regs->geni_m_irq_clear, m_irq);
			break;
		}
		write32(&regs->geni_m_irq_clear, m_irq);
	} while (!stopwatch_expired(timeout));

	if (!(m_irq & M_CMD_DONE_EN) || tx_rem_bytes || rx_rem_bytes) {
		printk(BIOS_INFO, "%s:Error: Transfer failed\n", __func__);
		qup_m_cancel_and_abort(bus);
		return -1;
	}
	return 0;
}