aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/omap_hsi/hsi_driver_fifo.c
blob: aa33a1a91c7fba655e10731dcf297810f06c8e40 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * hsi_driver_fifo.c
 *
 * Implements HSI module fifo management.
 *
 * Copyright (C) 2009 Texas Instruments, Inc.
 *
 * Author: Sebastien JAN <s-jan@ti.com>
 *
 * This package is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <linux/err.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/list.h>
#include "hsi_driver.h"

/**
 * hsi_fifo_get_id - Get fifo index corresponding to (port, channel)
 * @hsi_ctrl - HSI controler data
 * @channel - channel used
 * @port - HSI port used
 *
 * Returns the fifo index associated to the provided (port, channel).
 * Notes: 1) The fifo <=> (port, channel) correspondance depends on the selected
 * SW strategy for channels mapping (fifo management).
 * 2) the mapping is identical for Read and Write path.
 * This exclusively applies to HSI devices.
 */
int hsi_fifo_get_id(struct hsi_dev *hsi_ctrl, unsigned int channel,
		    unsigned int port)
{
	int fifo_index = 0;
	int err = 0;

	if (unlikely((channel >= HSI_CHANNELS_MAX) || (port < 1) ||
						      (port > 2))) {
		err = -EINVAL;
		goto fifo_id_bk;
	}

	if (hsi_ctrl->fifo_mapping_strategy == HSI_FIFO_MAPPING_ALL_PORT1) {
		if (unlikely(port != 1)) {
			err = -EINVAL;
			goto fifo_id_bk;
		} else {
			fifo_index = channel;
		}
	} else if (hsi_ctrl->fifo_mapping_strategy == HSI_FIFO_MAPPING_SSI) {
		if (unlikely(channel >= 8)) {
			err = -EINVAL;
			goto fifo_id_bk;
		} else {
			fifo_index = channel + 8 * (port - 1);
		}
	} else {
		err = -EPERM;
		goto fifo_id_bk;
	}

fifo_id_bk:
	if (unlikely(err < 0)) {
		fifo_index = err;
		dev_err(hsi_ctrl->dev, "Cannot map a FIFO to the requested "
			"params: channel:%d, port:%d; ERR=%d\n", channel, port,
			err);
	}

	return fifo_index;
}

/**
 * hsi_fifo_get_chan - Get (port, channel) from a fifo index
 * @hsi_ctrl - HSI controler data
 * @fifo - HSI fifo used (0..HSI_HST_FIFO_COUNT)
 * @channel - related channel if any (0..)
 * @port - related port if any (1..2)
 *
 * Returns 0 in case of success, and errocode (< 0) else
 * Notes: 1) The fifo <=> (port, channel) correspondance depends on the selected
 * SW strategy for channels mapping (fifo management).
 * 2) the mapping is identical for Read and Write path.
 * This exclusively applies to HSI devices.
 */
int hsi_fifo_get_chan(struct hsi_dev *hsi_ctrl, unsigned int fifo,
		      unsigned int *channel, unsigned int *port)
{
	int err = 0;

	if (unlikely(fifo >= HSI_HST_FIFO_COUNT)) {
		err = -EINVAL;
		goto fifo_id_bk;
	}

	if (hsi_ctrl->fifo_mapping_strategy == HSI_FIFO_MAPPING_ALL_PORT1) {
		*channel = fifo;
		*port = 1;
	} else if (hsi_ctrl->fifo_mapping_strategy == HSI_FIFO_MAPPING_SSI) {
		if (fifo < 8) {
			*channel = fifo;
			*port = 1;
		} else {
			*channel = fifo - 8;
			*port = 2;
		}
	} else {
		err = -EPERM;
		goto fifo_id_bk;
	}

fifo_id_bk:
	if (unlikely(err < 0))
		dev_err(hsi_ctrl->dev, "Cannot map a channel / port to the "
			"requested params: fifo:%d; ERR=%d\n", fifo, err);

	return err;
}

/**
 * hsi_fifo_mapping - Configures the HSI FIFO mapping registers.
 * @hsi_ctrl - HSI controler data
 * @mtype - mapping strategy
 *
 * Returns 0 in case of success, and errocode (< 0) else
 * Configures the HSI FIFO mapping registers. Several mapping strategies are
 * proposed.
 * Note: The mapping is identical for Read and Write path.
 * This exclusively applies to HSI devices.
 */
int hsi_fifo_mapping(struct hsi_dev *hsi_ctrl, unsigned int mtype)
{
	int err = 0;
	void __iomem *base = hsi_ctrl->base;
	int i;
	unsigned int channel, port;

	if (mtype == HSI_FIFO_MAPPING_ALL_PORT1) {
		channel = 0;
		for (i = 0; i < HSI_HST_FIFO_COUNT; i++) {
			hsi_outl(HSI_MAPPING_ENABLE |
				 (channel << HSI_MAPPING_CH_NUMBER_OFFSET) |
				 (0 << HSI_MAPPING_PORT_NUMBER_OFFSET) |
				 HSI_HST_MAPPING_THRESH_VALUE,
				 base, HSI_HST_MAPPING_FIFO_REG(i));
			hsi_outl(HSI_MAPPING_ENABLE |
				 (channel << HSI_MAPPING_CH_NUMBER_OFFSET) |
				 (0 << HSI_MAPPING_PORT_NUMBER_OFFSET),
				 base, HSI_HSR_MAPPING_FIFO_REG(i));
			channel++;
		}
		if (hsi_ctrl->fifo_mapping_strategy == HSI_FIFO_MAPPING_UNDEF)
			dev_dbg(hsi_ctrl->dev, "Fifo mapping : All FIFOs for "
						"Port1\n");
		hsi_ctrl->fifo_mapping_strategy = HSI_FIFO_MAPPING_ALL_PORT1;
	} else if (mtype == HSI_FIFO_MAPPING_SSI) {
		channel = 0;
		port = 0;
		for (i = 0; i < HSI_HST_FIFO_COUNT; i++) {
			hsi_outl(HSI_MAPPING_ENABLE |
				 (channel << HSI_MAPPING_CH_NUMBER_OFFSET) |
				 (port << HSI_MAPPING_PORT_NUMBER_OFFSET) |
				 HSI_HST_MAPPING_THRESH_VALUE,
				 base, HSI_HST_MAPPING_FIFO_REG(i));
			hsi_outl(HSI_MAPPING_ENABLE |
				 (channel << HSI_MAPPING_CH_NUMBER_OFFSET) |
				 (port << HSI_MAPPING_PORT_NUMBER_OFFSET),
				 base, HSI_HSR_MAPPING_FIFO_REG(i));
			channel++;
			if (channel == 8) {
				channel = 0;
				port = 1;
			}
		}

		if (hsi_ctrl->fifo_mapping_strategy == HSI_FIFO_MAPPING_UNDEF)
			dev_dbg(hsi_ctrl->dev, "Fifo mapping : 8 FIFOs per Port"
						" (SSI compatible mode)\n");
		hsi_ctrl->fifo_mapping_strategy = HSI_FIFO_MAPPING_SSI;
	} else {
		hsi_ctrl->fifo_mapping_strategy = HSI_FIFO_MAPPING_UNDEF;
		dev_err(hsi_ctrl->dev, "Bad Fifo strategy request : %d\n",
			mtype);
		err = -EINVAL;
	}

	return err;
}

/**
 * hsi_hst_bufstate_f_reg - Return the proper HSI_HST_BUFSTATE register offset
 * @hsi_ctrl - HSI controler data
 * @port - HSI port used
 * @channel - channel used
 *
 * Returns the HSI_HST_BUFSTATE register offset
 * Note: indexing of BUFSTATE registers is different on SSI and HSI:
 * On SSI: it is linked to the ports
 * On HSI: it is linked to the FIFOs (and depend on the SW strategy)
 */
long hsi_hst_bufstate_f_reg(struct hsi_dev *hsi_ctrl,
			    unsigned int port, unsigned int channel)
{
	int fifo;
	if (hsi_driver_device_is_hsi(to_platform_device(hsi_ctrl->dev))) {
		fifo = hsi_fifo_get_id(hsi_ctrl, channel, port);
		if (unlikely(fifo < 0)) {
			dev_err(hsi_ctrl->dev,
				"hsi_hst_bufstate_f_reg  ERROR : %d\n", fifo);
			return fifo;
		} else
			return HSI_HST_BUFSTATE_FIFO_REG(fifo);
	} else {
		return HSI_HST_BUFSTATE_REG(port);
	}
}

/**
 * hsi_hsr_bufstate_f_reg - Return the proper HSI_HSR_BUFSTATE register offset
 * @hsi_ctrl - HSI controler data
 * @port - HSI port used
 * @channel - channel used
 *
 * Returns the HSI_HSR_BUFSTATE register offset
 * Note: indexing of BUFSTATE registers is different on SSI and HSI:
 * On SSI: it is linked to the ports
 * On HSI: it is linked to the FIFOs (and depend on the SW strategy)
 */
long hsi_hsr_bufstate_f_reg(struct hsi_dev *hsi_ctrl,
			    unsigned int port, unsigned int channel)
{
	int fifo;
	if (hsi_driver_device_is_hsi(to_platform_device(hsi_ctrl->dev))) {
		fifo = hsi_fifo_get_id(hsi_ctrl, channel, port);
		if (unlikely(fifo < 0)) {
			dev_err(hsi_ctrl->dev,
				"hsi_hsr_bufstate_f_reg  ERROR : %d\n", fifo);
			return fifo;
		} else
			return HSI_HSR_BUFSTATE_FIFO_REG(fifo);
	} else {
		return HSI_HSR_BUFSTATE_REG(port);
	}
}

/**
 * hsi_hst_buffer_f_reg - Return the proper HSI_HST_BUFFER register offset
 * @hsi_ctrl - HSI controler data
 * @port - HSI port used
 * @channel - channel used
 *
 * Returns the HSI_HST_BUFFER register offset
 * Note: indexing of BUFFER registers is different on SSI and HSI:
 * On SSI: it is linked to the ports
 * On HSI: it is linked to the FIFOs (and depend on the SW strategy)
 */
long hsi_hst_buffer_reg(struct hsi_dev *hsi_ctrl,
			unsigned int port, unsigned int channel)
{
	int fifo;
	if (hsi_driver_device_is_hsi(to_platform_device(hsi_ctrl->dev))) {
		fifo = hsi_fifo_get_id(hsi_ctrl, channel, port);
		if (unlikely(fifo < 0)) {
			dev_err(hsi_ctrl->dev,
				"hsi_hst_bufstate_f_reg   ERROR : %d\n", fifo);
			return fifo;
		} else
			return HSI_HST_BUFFER_FIFO_REG(fifo);
	} else {
		return HSI_HST_BUFFER_CH_REG(port, channel);
	}
}

/**
 * hsi_hsr_buffer_f_reg - Return the proper HSI_HSR_BUFFER register offset
 * @hsi_ctrl - HSI controler data
 * @port - HSI port used
 * @channel - channel used
 *
 * Returns the HSI_HSR_BUFFER register offset
 * Note: indexing of BUFFER registers is different on SSI and HSI:
 * On SSI: it is linked to the ports
 * On HSI: it is linked to the FIFOs (and depend on the SW strategy)
 */
long hsi_hsr_buffer_reg(struct hsi_dev *hsi_ctrl,
			unsigned int port, unsigned int channel)
{
	int fifo;
	if (hsi_driver_device_is_hsi(to_platform_device(hsi_ctrl->dev))) {
		fifo = hsi_fifo_get_id(hsi_ctrl, channel, port);
		if (unlikely(fifo < 0)) {
			dev_err(hsi_ctrl->dev,
				"hsi_hsr_bufstate_f_reg  ERROR : %d\n", fifo);
			return fifo;
		} else
			return HSI_HSR_BUFFER_FIFO_REG(fifo);
	} else {
		return HSI_HSR_BUFFER_CH_REG(port, channel);
	}
}

/**
 * hsi_get_rx_fifo_occupancy - Return the size of data remaining
 *				in the given FIFO
 * @hsi_ctrl - HSI controler data
 * @fifo - FIFO to look at
 *
 * Returns the number of frames (32bits) remaining in the FIFO
 */
u8 hsi_get_rx_fifo_occupancy(struct hsi_dev *hsi_ctrl, u8 fifo)
{
	void __iomem *base = hsi_ctrl->base;
	int hsr_mapping, mapping_words;

	hsr_mapping = hsi_inl(base, HSI_HSR_MAPPING_FIFO_REG(fifo));
	mapping_words = (hsr_mapping >> HSI_HST_MAPPING_THRESH_OFFSET) & 0xF;
	return mapping_words;
}