summaryrefslogtreecommitdiff
path: root/hsspi.c
blob: 42903274b4a266e030a48adc305633e65630e107 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// SPDX-License-Identifier: GPL-2.0

/*
 * This file is part of the QM35 UCI stack for linux.
 *
 * Copyright (c) 2021 Qorvo US, Inc.
 *
 * This software is provided under the GNU General Public License, version 2
 * (GPLv2), as well as under a Qorvo commercial license.
 *
 * You may choose to use this software under the terms of the GPLv2 License,
 * version 2 ("GPLv2"), as published by the Free Software Foundation.
 * You should have received a copy of the GPLv2 along with this program.  If
 * not, see <http://www.gnu.org/licenses/>.
 *
 * This program is distributed under the GPLv2 in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GPLv2 for more
 * details.
 *
 * If you cannot meet the requirements of the GPLv2, you may not use this
 * software for any purpose without first obtaining a commercial license from
 * Qorvo.
 * Please contact Qorvo to inquire about licensing terms.
 *
 * QM35 HSSPI Protocol
 */

#include <linux/kernel.h>

#include "qm35-trace.h"
#include "hsspi.h"

/* STC HOST flags */
#define STC_HOST_WR     BIT(7)
#define STC_HOST_PRD    BIT(6)
#define STC_HOST_RD     BIT(5)

/* STC SOC flags */
#define STC_SOC_ODW     BIT(7)
#define STC_SOC_OA      BIT(6)
#define STC_SOC_RDY     BIT(5)
#define STC_SOC_ERR     BIT(4)

#define SS_READY_TIMEOUT_MS (250)

struct hsspi_work {
	struct list_head list;
	enum hsspi_work_type type;
	union {
		struct {
			struct hsspi_block *blk;
			struct hsspi_layer *layer;
		} tx;
		struct completion *completion;
	};
};

static inline bool layer_id_is_valid(struct hsspi *hsspi, u8 ul)
{
	return (ul < ARRAY_SIZE(hsspi->layers));
}

/**
 * get_work() - get a work from the list
 *
 * @hsspi: &struct hsspi
 *
 * Return: a &struct hsspi_work
 * The work can be:
 *  - a TX work enqueued by hsspi_send
 *  - a COMPLETION work used for synchronization (always allocated on stack)
 */
static struct hsspi_work *get_work(struct hsspi *hsspi)
{
	struct hsspi_work *hw;

	spin_lock(&hsspi->lock);

	hw = list_first_entry_or_null(&hsspi->work_list,
				      struct hsspi_work, list);
	if (hw)
		list_del(&hw->list);

	spin_unlock(&hsspi->lock);

	trace_hsspi_get_work(&hsspi->spi->dev, hw ? hw->type : -1);
	return hw;
}

/**
 * is_txrx_waiting() - is there something to do
 *
 * @hsspi: &struct hsspi
 *
 * Return: True if there is a TX work available or if the SS_IRQ flag
 * is set. False otherwise.
 */
static bool is_txrx_waiting(struct hsspi *hsspi)
{
	enum hsspi_state state;
	bool is_empty;

	spin_lock(&hsspi->lock);

	is_empty = list_empty(&hsspi->work_list);
	state = hsspi->state;

	spin_unlock(&hsspi->lock);

	trace_hsspi_is_txrx_waiting(&hsspi->spi->dev, is_empty, state);
	/*
	 * There is no work in the list but we must check SS_IRQ to
	 * know if we need to make an empty TX transfer with PRE_READ
	 * flag set.
	 */
	return !is_empty || ((state == HSSPI_RUNNING)
			     && test_bit(HSSPI_FLAGS_SS_IRQ, hsspi->flags));
}

/**
 * spi_xfer() - Single SPI transfer
 *
 * @hsspi: &struct hsspi
 * @tx: tx payload
 * @rx: rx payload
 * @length: payload length
 */
static int spi_xfer(struct hsspi *hsspi, const void *tx, void *rx,
		    size_t length)
{
	struct spi_transfer xfers[2] = {
		{
			.tx_buf = hsspi->host,
			.rx_buf = hsspi->soc,
			.len = sizeof(*(hsspi->host)),
		},
		{
			.tx_buf = tx,
			.rx_buf = rx,
			.len = length,
		},
	};
	int ret;

	hsspi->soc->flags = 0;
	hsspi->soc->ul = 0;
	hsspi->soc->length = 0;

	if (hsspi->spi_error)
		return hsspi->spi_error;

	ret = wait_event_interruptible_timeout(
		hsspi->wq_ready,
		test_and_clear_bit(HSSPI_FLAGS_SS_READY, hsspi->flags),
		msecs_to_jiffies(SS_READY_TIMEOUT_MS));
	if (ret <= 0) {
		dev_err(&hsspi->spi->dev, "not able to caught ss_ready: %d\n", ret);
		return -EAGAIN;
	}

	ret = spi_sync_transfer(hsspi->spi, xfers, length ? 2 : 1);

	trace_hsspi_spi_xfer(&hsspi->spi->dev, hsspi->host, hsspi->soc, ret);

	if (ret)
		dev_err(&hsspi->spi->dev, "spi_sync_transfer: %d\n", ret);
	else if (!(hsspi->soc->flags & STC_SOC_RDY)) {
		ret = -EAGAIN;
		dev_err(&hsspi->spi->dev, "FW not ready\n");
	}

	return ret;
}

/**
 * hsspi_rx() - request data from the QM35 on the HSSPI
 *
 * @hsspi: &struct hsspi
 * @ul: upper layer id
 * @length: length of data requested
 */
static int hsspi_rx(struct hsspi *hsspi, u8 ul, u16 length)
{
	struct hsspi_layer *layer;
	struct hsspi_block *blk;
	int ret;

	hsspi->host->flags = STC_HOST_RD;
	hsspi->host->ul = ul;
	hsspi->host->length = length;

	if (layer_id_is_valid(hsspi, ul)) {
		spin_lock(&hsspi->lock);
		layer = hsspi->layers[ul];
		spin_unlock(&hsspi->lock);
	} else
		layer = NULL;

	blk = layer ? layer->ops->get(layer, length) : NULL;
	if (blk) {
		ret = spi_xfer(hsspi, NULL, blk->data, blk->size);

		layer->ops->received(layer, blk, ret);
	} else
		ret = spi_xfer(hsspi, NULL, NULL, 0);

	if (!(hsspi->soc->flags & STC_SOC_ODW)
	    && test_and_clear_bit(HSSPI_FLAGS_SS_IRQ, hsspi->flags))
		hsspi->odw_cleared(hsspi);

	return ret;
}

/**
 * hsspi_tx() - send a hsspi block to the QM35 on the HSSPI
 *
 * @hsspi: &struct hsspi
 * @layer: &struct hsspi_layer
 * @blk: &struct hsspi_block
 *
 * It also adds PRD flag if SS_IRQ is set. Therefore it will try a RX
 * transfer accordingly.
 */
static int hsspi_tx(struct hsspi *hsspi, struct hsspi_layer *layer,
		    struct hsspi_block *blk)
{
	int ret;

	hsspi->host->flags = STC_HOST_WR;
	hsspi->host->ul = layer->id;
	hsspi->host->length = blk->length;

	if (test_bit(HSSPI_FLAGS_SS_IRQ, hsspi->flags))
		hsspi->host->flags |= STC_HOST_PRD;

	ret = spi_xfer(hsspi, blk->data, NULL, blk->size);

	layer->ops->sent(layer, blk, ret);

	if (ret)
		return ret;

	if (hsspi->host->flags & STC_HOST_PRD)
		return hsspi_rx(hsspi, hsspi->soc->ul, hsspi->soc->length);

	return 0;
}

/**
 * hsspi_pre_read() - send a PRE_READ with no TX and do RX
 *
 * @hsspi: &struct hsspi
 *
 * This function is a particular case of hsspi_tx with no layer or
 * blk.
 *
 */
static int hsspi_pre_read(struct hsspi *hsspi)
{
	int ret;

	hsspi->host->flags = STC_HOST_PRD;
	hsspi->host->ul = 0;
	hsspi->host->length = 0;

	ret = spi_xfer(hsspi, NULL, NULL, 0);
	if (ret)
		return ret;

	return hsspi_rx(hsspi, hsspi->soc->ul, hsspi->soc->length);
}

/**
 * hsspi_thread_fn() - the thread that manage all SPI transfers
 * @data: the &struct hsspi
 *
 */
static int hsspi_thread_fn(void *data)
{
	struct hsspi *hsspi = data;

	while (1) {
		struct hsspi_work *hw;
		int ret;

		ret = wait_event_interruptible(
			hsspi->wq,
			is_txrx_waiting(hsspi) || kthread_should_stop());
		if (ret)
			return ret;

		if (kthread_should_stop())
			break;

		hw = get_work(hsspi);
		if (hw) {
			if (hw->type == HSSPI_WORK_COMPLETION) {
				complete(hw->completion);
				/* on the stack no need to free */
				continue;
			} else if (hw->type == HSSPI_WORK_TX) {
				ret = hsspi_tx(hsspi, hw->tx.layer, hw->tx.blk);
				kfree(hw);
			} else {
				dev_err(&hsspi->spi->dev,
					"unknown hsspi_work type: %d\n", hw->type);
				ret = -EINVAL;
			}
		} else
			/* If there is no work, we are here because
			 * SS_IRQ is set.
			 */
			ret = hsspi_pre_read(hsspi);

		if (ret) {
			spin_lock(&hsspi->lock);
			hsspi->state = HSSPI_ERROR;
			spin_unlock(&hsspi->lock);

			hsspi->spi_error = ret;
		}
	}
	return 0;
}

int hsspi_init(struct hsspi *hsspi, struct spi_device *spi)
{
	memset(hsspi, 0, sizeof(*hsspi));

	spin_lock_init(&hsspi->lock);
	INIT_LIST_HEAD(&hsspi->work_list);

	hsspi->state = HSSPI_STOPPED;
	hsspi->spi = spi;
	hsspi->spi_error = -EAGAIN;

	init_waitqueue_head(&hsspi->wq);
	init_waitqueue_head(&hsspi->wq_ready);

	hsspi->host = kmalloc(sizeof(*(hsspi->host)), GFP_KERNEL | GFP_DMA);
	hsspi->soc = kmalloc(sizeof(*(hsspi->soc)), GFP_KERNEL | GFP_DMA);

	hsspi->thread = kthread_create(hsspi_thread_fn, hsspi, "hsspi");
	if (IS_ERR(hsspi->thread))
		return PTR_ERR(hsspi->thread);

	wake_up_process(hsspi->thread);

	dev_info(&hsspi->spi->dev, "HSSPI initialized\n");
	return 0;
}

int hsspi_deinit(struct hsspi *hsspi)
{
	int i;

	spin_lock(&hsspi->lock);

	if (hsspi->state == HSSPI_RUNNING) {
		spin_unlock(&hsspi->lock);
		return -EBUSY;
	}

	for (i = 0; i < ARRAY_SIZE(hsspi->layers); i++) {
		if (!hsspi->layers[i])
			continue;

		dev_err(&hsspi->spi->dev,
			"HSSPI upper layer '%s' not unregistered\n",
			hsspi->layers[i]->name);
		spin_unlock(&hsspi->lock);
		return -EBUSY;
	}

	spin_unlock(&hsspi->lock);

	kthread_stop(hsspi->thread);

	kfree(hsspi->host);
	kfree(hsspi->soc);

	dev_info(&hsspi->spi->dev, "HSSPI uninitialized\n");
	return 0;
}

int hsspi_register(struct hsspi *hsspi, struct hsspi_layer *layer)
{
	int ret = 0;

	if (!layer_id_is_valid(hsspi, layer->id))
		return -EINVAL;

	spin_lock(&hsspi->lock);

	if (hsspi->layers[layer->id])
		ret = -EBUSY;
	else
		hsspi->layers[layer->id] = layer;

	spin_unlock(&hsspi->lock);

	if (ret) {
		dev_err(&hsspi->spi->dev, "%s: '%s' ret: %d\n", __func__,
			layer->name, ret);
		return ret;
	}

	dev_dbg(&hsspi->spi->dev, "HSSPI upper layer '%s' registered\n",
		layer->name);
	return 0;
}

int hsspi_unregister(struct hsspi *hsspi, struct hsspi_layer *layer)
{
	DECLARE_COMPLETION_ONSTACK(complete);
	struct hsspi_work complete_work = {
		.type = HSSPI_WORK_COMPLETION,
		.completion = &complete,
	};
	int ret = 0;

	if (!layer_id_is_valid(hsspi, layer->id))
		return -EINVAL;

	spin_lock(&hsspi->lock);

	if (hsspi->layers[layer->id] == layer) {
		hsspi->layers[layer->id] = NULL;

		list_add_tail(&complete_work.list, &hsspi->work_list);
	} else
		ret = -EINVAL;

	spin_unlock(&hsspi->lock);

	if (ret) {
		dev_err(&hsspi->spi->dev, "%s: '%s' ret: %d\n", __func__,
			layer->name, ret);
		return ret;
	}

	wake_up_interruptible(&hsspi->wq);

	/* when completed there is no more reference to layer in the
	 * work_list or in the hsspi_thread_fn
	 */
	wait_for_completion(&complete);

	dev_dbg(&hsspi->spi->dev, "HSSPI upper layer '%s' unregistered\n",
		layer->name);
	return 0;
}

void hsspi_set_spi_slave_ready(struct hsspi *hsspi)
{
	set_bit(HSSPI_FLAGS_SS_READY, hsspi->flags);

	wake_up_interruptible(&hsspi->wq_ready);
}

void hsspi_set_output_data_waiting(struct hsspi *hsspi)
{
	set_bit(HSSPI_FLAGS_SS_IRQ, hsspi->flags);

	wake_up_interruptible(&hsspi->wq);
}

int hsspi_init_block(struct hsspi_block *blk, u16 length)
{
	void *data;

	data = krealloc(blk->data, length, GFP_KERNEL | GFP_DMA);
	if (!data)
		return -ENOMEM;

	blk->data = data;
	blk->length = length;
	blk->size = length;

	return 0;
}

void hsspi_deinit_block(struct hsspi_block *blk)
{
	kfree(blk->data);
	blk->data = NULL;
}

int hsspi_send(struct hsspi *hsspi, struct hsspi_layer *layer,
	       struct hsspi_block *blk)
{
	struct hsspi_work *tx_work;
	int ret = 0;

	if (!layer || !blk)
		return -EINVAL;

	if (!layer_id_is_valid(hsspi, layer->id))
		return -EINVAL;

	tx_work = kzalloc(sizeof(*tx_work), GFP_KERNEL);
	if (!tx_work)
		return -ENOMEM;

	tx_work->type = HSSPI_WORK_TX;
	tx_work->tx.blk = blk;
	tx_work->tx.layer = layer;

	spin_lock(&hsspi->lock);

	if (hsspi->state == HSSPI_RUNNING) {
		if (hsspi->layers[layer->id] == layer)
			list_add_tail(&tx_work->list, &hsspi->work_list);
		else
			ret = -EINVAL;
	} else
		ret = -EAGAIN;

	spin_unlock(&hsspi->lock);

	if (ret) {
		kfree(tx_work);
		dev_err(&hsspi->spi->dev, "hsspi_send: %d\n", ret);
		return ret;
	}

	wake_up_interruptible(&hsspi->wq);

	dev_dbg(&hsspi->spi->dev, "send %d bytes on HSSPI '%s' layer\n",
		blk->length, layer->name);
	return 0;
}

void hsspi_start(struct hsspi *hsspi)
{
	spin_lock(&hsspi->lock);

	hsspi->state = HSSPI_RUNNING;
	hsspi->spi_error = 0;

	spin_unlock(&hsspi->lock);

	wake_up_interruptible(&hsspi->wq);

	dev_dbg(&hsspi->spi->dev, "HSSPI started\n");
}

void hsspi_stop(struct hsspi *hsspi)
{
	DECLARE_COMPLETION_ONSTACK(complete);
	struct hsspi_work complete_work = {
		.type = HSSPI_WORK_COMPLETION,
		.completion = &complete,
	};

	spin_lock(&hsspi->lock);

	hsspi->state = HSSPI_STOPPED;

	list_add_tail(&complete_work.list, &hsspi->work_list);

	spin_unlock(&hsspi->lock);

	wake_up_interruptible(&hsspi->wq);

	wait_for_completion(&complete);

	dev_dbg(&hsspi->spi->dev, "HSSPI stopped\n");
}