summaryrefslogtreecommitdiffstats
path: root/drivers/spi/spi-sifive.c
blob: fbe80718e41416de6182eaf26d9b78105857d5fa (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright 2018 SiFive, Inc.
 * Copyright 2019 Bhargav Shah <bhargavshah1988@gmail.com>
 *
 * SiFive SPI controller driver (master mode only)
 */

#include <common.h>
#include <linux/clk.h>
#include <driver.h>
#include <init.h>
#include <errno.h>
#include <linux/reset.h>
#include <spi/spi.h>
#include <linux/spi/spi-mem.h>
#include <linux/bitops.h>
#include <clock.h>
#include <gpio.h>
#include <of_gpio.h>
#include <linux/bitfield.h>
#include <linux/iopoll.h>
#include <linux/log2.h>

#define SIFIVE_SPI_MAX_CS		32

#define SIFIVE_SPI_DEFAULT_DEPTH	8
#define SIFIVE_SPI_DEFAULT_BITS		8

/* register offsets */
#define SIFIVE_SPI_REG_SCKDIV            0x00 /* Serial clock divisor */
#define SIFIVE_SPI_REG_SCKMODE           0x04 /* Serial clock mode */
#define SIFIVE_SPI_REG_CSID              0x10 /* Chip select ID */
#define SIFIVE_SPI_REG_CSDEF             0x14 /* Chip select default */
#define SIFIVE_SPI_REG_CSMODE            0x18 /* Chip select mode */
#define SIFIVE_SPI_REG_DELAY0            0x28 /* Delay control 0 */
#define SIFIVE_SPI_REG_DELAY1            0x2c /* Delay control 1 */
#define SIFIVE_SPI_REG_FMT               0x40 /* Frame format */
#define SIFIVE_SPI_REG_TXDATA            0x48 /* Tx FIFO data */
#define SIFIVE_SPI_REG_RXDATA            0x4c /* Rx FIFO data */
#define SIFIVE_SPI_REG_TXMARK            0x50 /* Tx FIFO watermark */
#define SIFIVE_SPI_REG_RXMARK            0x54 /* Rx FIFO watermark */
#define SIFIVE_SPI_REG_FCTRL             0x60 /* SPI flash interface control */
#define SIFIVE_SPI_REG_FFMT              0x64 /* SPI flash instruction format */
#define SIFIVE_SPI_REG_IE                0x70 /* Interrupt Enable Register */
#define SIFIVE_SPI_REG_IP                0x74 /* Interrupt Pendings Register */

/* sckdiv bits */
#define SIFIVE_SPI_SCKDIV_DIV_MASK       0xfffU

/* sckmode bits */
#define SIFIVE_SPI_SCKMODE_PHA           BIT(0)
#define SIFIVE_SPI_SCKMODE_POL           BIT(1)
#define SIFIVE_SPI_SCKMODE_MODE_MASK     (SIFIVE_SPI_SCKMODE_PHA | \
					  SIFIVE_SPI_SCKMODE_POL)

/* csmode bits */
#define SIFIVE_SPI_CSMODE_MODE_AUTO      0U
#define SIFIVE_SPI_CSMODE_MODE_HOLD      2U
#define SIFIVE_SPI_CSMODE_MODE_OFF       3U

/* delay0 bits */
#define SIFIVE_SPI_DELAY0_CSSCK(x)       ((u32)(x))
#define SIFIVE_SPI_DELAY0_CSSCK_MASK     0xffU
#define SIFIVE_SPI_DELAY0_SCKCS(x)       ((u32)(x) << 16)
#define SIFIVE_SPI_DELAY0_SCKCS_MASK     (0xffU << 16)

/* delay1 bits */
#define SIFIVE_SPI_DELAY1_INTERCS(x)     ((u32)(x))
#define SIFIVE_SPI_DELAY1_INTERCS_MASK   0xffU
#define SIFIVE_SPI_DELAY1_INTERXFR(x)    ((u32)(x) << 16)
#define SIFIVE_SPI_DELAY1_INTERXFR_MASK  (0xffU << 16)

/* fmt bits */
#define SIFIVE_SPI_FMT_PROTO_SINGLE      0U
#define SIFIVE_SPI_FMT_PROTO_DUAL        1U
#define SIFIVE_SPI_FMT_PROTO_QUAD        2U
#define SIFIVE_SPI_FMT_PROTO_MASK        3U
#define SIFIVE_SPI_FMT_ENDIAN            BIT(2)
#define SIFIVE_SPI_FMT_DIR               BIT(3)
#define SIFIVE_SPI_FMT_LEN(x)            ((u32)(x) << 16)
#define SIFIVE_SPI_FMT_LEN_MASK          (0xfU << 16)

/* txdata bits */
#define SIFIVE_SPI_TXDATA_DATA_MASK      0xffU
#define SIFIVE_SPI_TXDATA_FULL           BIT(31)

/* rxdata bits */
#define SIFIVE_SPI_RXDATA_DATA_MASK      0xffU
#define SIFIVE_SPI_RXDATA_EMPTY          BIT(31)

/* ie and ip bits */
#define SIFIVE_SPI_IP_TXWM               BIT(0)
#define SIFIVE_SPI_IP_RXWM               BIT(1)

/* format protocol */
#define SIFIVE_SPI_PROTO_QUAD		4 /* 4 lines I/O protocol transfer */
#define SIFIVE_SPI_PROTO_DUAL		2 /* 2 lines I/O protocol transfer */
#define SIFIVE_SPI_PROTO_SINGLE		1 /* 1 line I/O protocol transfer */

#define SPI_XFER_BEGIN  0x01                    /* Assert CS before transfer */
#define SPI_XFER_END    0x02                    /* Deassert CS after transfer */

struct sifive_spi {
	struct spi_controller ctlr;
	void __iomem	*regs;		/* base address of the registers */
	u32		fifo_depth;
	u32		bits_per_word;
	u32		cs_inactive;	/* Level of the CS pins when inactive*/
	u32		freq;
	u8		fmt_proto;
};

static inline struct sifive_spi *to_sifive_spi(struct spi_controller *ctlr)
{
	return container_of(ctlr, struct sifive_spi, ctlr);
}

static void sifive_spi_prep_device(struct sifive_spi *spi,
				   struct spi_device *spi_dev)
{
	/* Update the chip select polarity */
	if (spi_dev->mode & SPI_CS_HIGH)
		spi->cs_inactive &= ~BIT(spi_dev->chip_select);
	else
		spi->cs_inactive |= BIT(spi_dev->chip_select);
	writel(spi->cs_inactive, spi->regs + SIFIVE_SPI_REG_CSDEF);

	/* Select the correct device */
	writel(spi_dev->chip_select, spi->regs + SIFIVE_SPI_REG_CSID);
}

static void sifive_spi_set_cs(struct sifive_spi *spi,
			     struct spi_device *spi_dev)
{
	u32 cs_mode = SIFIVE_SPI_CSMODE_MODE_HOLD;

	if (spi_dev->mode & SPI_CS_HIGH)
		cs_mode = SIFIVE_SPI_CSMODE_MODE_AUTO;

	writel(cs_mode, spi->regs + SIFIVE_SPI_REG_CSMODE);
}

static void sifive_spi_clear_cs(struct sifive_spi *spi)
{
	writel(SIFIVE_SPI_CSMODE_MODE_AUTO, spi->regs + SIFIVE_SPI_REG_CSMODE);
}

static void sifive_spi_prep_transfer(struct sifive_spi *spi,
				     struct spi_device *spi_dev,
				     u8 *rx_ptr)
{
	u32 cr;

	/* Modify the SPI protocol mode */
	cr = readl(spi->regs + SIFIVE_SPI_REG_FMT);

	/* Bits per word ? */
	cr &= ~SIFIVE_SPI_FMT_LEN_MASK;
	cr |= SIFIVE_SPI_FMT_LEN(spi->bits_per_word);

	/* LSB first? */
	cr &= ~SIFIVE_SPI_FMT_ENDIAN;
	if (spi_dev->mode & SPI_LSB_FIRST)
		cr |= SIFIVE_SPI_FMT_ENDIAN;

	/* Number of wires ? */
	cr &= ~SIFIVE_SPI_FMT_PROTO_MASK;
	switch (spi->fmt_proto) {
	case SIFIVE_SPI_PROTO_QUAD:
		cr |= SIFIVE_SPI_FMT_PROTO_QUAD;
		break;
	case SIFIVE_SPI_PROTO_DUAL:
		cr |= SIFIVE_SPI_FMT_PROTO_DUAL;
		break;
	default:
		cr |= SIFIVE_SPI_FMT_PROTO_SINGLE;
		break;
	}

	/* SPI direction in/out ? */
	cr &= ~SIFIVE_SPI_FMT_DIR;
	if (!rx_ptr)
		cr |= SIFIVE_SPI_FMT_DIR;

	writel(cr, spi->regs + SIFIVE_SPI_REG_FMT);
}

static void sifive_spi_rx(struct sifive_spi *spi, u8 *rx_ptr)
{
	u32 data;

	do {
		data = readl(spi->regs + SIFIVE_SPI_REG_RXDATA);
	} while (data & SIFIVE_SPI_RXDATA_EMPTY);

	if (rx_ptr)
		*rx_ptr = data & SIFIVE_SPI_RXDATA_DATA_MASK;
}

static void sifive_spi_tx(struct sifive_spi *spi, const u8 *tx_ptr)
{
	u32 data;
	u8 tx_data = (tx_ptr) ? *tx_ptr & SIFIVE_SPI_TXDATA_DATA_MASK :
				SIFIVE_SPI_TXDATA_DATA_MASK;

	do {
		data = readl(spi->regs + SIFIVE_SPI_REG_TXDATA);
	} while (data & SIFIVE_SPI_TXDATA_FULL);

	writel(tx_data, spi->regs + SIFIVE_SPI_REG_TXDATA);
}

static int sifive_spi_wait(struct sifive_spi *spi, u32 mask)
{
	u32 val;

	return readl_poll_timeout(spi->regs + SIFIVE_SPI_REG_IP, val,
				  (val & mask) == mask, 100 * USEC_PER_MSEC);
}

static int sifive_spi_transfer_one(struct spi_device *spi_dev, unsigned int nbytes,
				   const void *dout, void *din)
{
	struct sifive_spi *spi = to_sifive_spi(spi_dev->controller);
	const u8 *tx_ptr = dout;
	u8 *rx_ptr = din;
	int ret;

	sifive_spi_prep_transfer(spi, spi_dev, rx_ptr);

	while (nbytes) {
		unsigned int n_words = min(nbytes, spi->fifo_depth);
		unsigned int tx_words, rx_words;

		/* Enqueue n_words for transmission */
		for (tx_words = 0; tx_words < n_words; tx_words++) {
			if (!tx_ptr)
				sifive_spi_tx(spi, NULL);
			else
				sifive_spi_tx(spi, tx_ptr++);
		}

		if (rx_ptr) {
			/* Wait for transmission + reception to complete */
			writel(n_words - 1, spi->regs + SIFIVE_SPI_REG_RXMARK);
			ret = sifive_spi_wait(spi, SIFIVE_SPI_IP_RXWM);
			if (ret)
				return ret;

			/* Read out all the data from the RX FIFO */
			for (rx_words = 0; rx_words < n_words; rx_words++)
				sifive_spi_rx(spi, rx_ptr++);
		} else {
			/* Wait for transmission to complete */
			ret = sifive_spi_wait(spi, SIFIVE_SPI_IP_TXWM);
			if (ret)
				return ret;
		}

		nbytes -= n_words;
	}

	return 0;
}

static int sifive_spi_transfer(struct spi_device *spi_dev, struct spi_message *msg)
{
	struct spi_controller *ctlr = spi_dev->controller;
	struct sifive_spi *spi = to_sifive_spi(ctlr);
	struct spi_transfer *t;
	int ret = 0;

	if (list_empty(&msg->transfers))
		return 0;

	msg->actual_length = 0;

	sifive_spi_prep_device(spi, spi_dev);
	sifive_spi_set_cs(spi, spi_dev);

	dev_dbg(ctlr->dev, "transfer start actual_length=%i\n", msg->actual_length);
	list_for_each_entry(t, &msg->transfers, transfer_list) {
		dev_dbg(ctlr->dev, "  xfer %p: len %u tx %p rx %p\n",
			t, t->len, t->tx_buf, t->rx_buf);

		ret = sifive_spi_transfer_one(spi_dev, t->len,
					      t->tx_buf, t->rx_buf);
		if (ret < 0)
			goto out;
		msg->actual_length += t->len;
	}
	dev_dbg(ctlr->dev, "transfer done actual_length=%i\n", msg->actual_length);

out:
	sifive_spi_clear_cs(spi);
	return ret;
}

static int sifive_spi_exec_op(struct spi_mem *mem,
			      const struct spi_mem_op *op)
{
	struct spi_device *spi_dev = mem->spi;
	struct device *dev = &spi_dev->dev;
	struct sifive_spi *spi = spi_controller_get_devdata(spi_dev->controller);
	u8 opcode = op->cmd.opcode;
	int ret;

	spi->fmt_proto = op->cmd.buswidth;

	sifive_spi_prep_device(spi, spi_dev);
	sifive_spi_set_cs(spi, spi_dev);

	/* send the opcode */
	ret = sifive_spi_transfer_one(spi_dev, 1, &opcode, NULL);
	if (ret < 0) {
		dev_err(dev, "failed to xfer opcode\n");
		goto out;
	}

	if (!op->addr.nbytes && !op->data.nbytes)
		goto out;

	/* send the addr + dummy */
	if (op->addr.nbytes) {
		int i, op_len = op->addr.nbytes + op->dummy.nbytes;
		u8 *op_buf;

		op_buf = malloc(op_len);
		if (!op_buf) {
			ret = -ENOMEM;
			goto out;
		}

		/* fill address */
		for (i = 0; i < op->addr.nbytes; i++)
			op_buf[i] = op->addr.val >>
				(8 * (op->addr.nbytes - i - 1));

		/* fill dummy */
		memset(op_buf + op->addr.nbytes, 0xff, op->dummy.nbytes);

		spi->fmt_proto = op->addr.buswidth;

		ret = sifive_spi_transfer_one(spi_dev, op_len, op_buf, NULL);
		free(op_buf);
		if (ret < 0) {
			dev_err(dev, "failed to xfer addr + dummy\n");
			goto out;
		}
	}

	/* send/received the data */
	if (op->data.nbytes) {
		const void *tx_buf = NULL;
		void *rx_buf = NULL;

		if (op->data.dir == SPI_MEM_DATA_IN)
			rx_buf = op->data.buf.in;
		else
			tx_buf = op->data.buf.out;

		spi->fmt_proto = op->data.buswidth;

		ret = sifive_spi_transfer_one(spi_dev, op->data.nbytes,
				      tx_buf, rx_buf);
		if (ret) {
			dev_err(dev, "failed to xfer data\n");
			goto out;
		}
	}

out:
	sifive_spi_clear_cs(spi);
	return ret;
}

static void sifive_spi_set_speed(struct sifive_spi *spi, uint speed)
{
	u32 scale;

	if (speed > spi->freq)
		speed = spi->freq;

	/* Cofigure max speed */
	scale = (DIV_ROUND_UP(spi->freq >> 1, speed) - 1)
					& SIFIVE_SPI_SCKDIV_DIV_MASK;
	writel(scale, spi->regs + SIFIVE_SPI_REG_SCKDIV);
}

static void sifive_spi_set_mode(struct sifive_spi *spi, uint mode)
{
	u32 cr;

	/* Switch clock mode bits */
	cr = readl(spi->regs + SIFIVE_SPI_REG_SCKMODE) &
				~SIFIVE_SPI_SCKMODE_MODE_MASK;
	if (mode & SPI_CPHA)
		cr |= SIFIVE_SPI_SCKMODE_PHA;
	if (mode & SPI_CPOL)
		cr |= SIFIVE_SPI_SCKMODE_POL;

	writel(cr, spi->regs + SIFIVE_SPI_REG_SCKMODE);
}

static int sifive_spi_setup(struct spi_device *spi_dev)
{
	struct sifive_spi *spi = to_sifive_spi(spi_dev->controller);

	sifive_spi_set_mode(spi, spi_dev->mode);
	sifive_spi_set_speed(spi, spi_dev->max_speed_hz);

	return 0;
}

static void sifive_spi_init_hw(struct sifive_spi *spi)
{
	struct device *dev = spi->ctlr.dev;
	u32 cs_bits;

	/* probe the number of CS lines */
	spi->cs_inactive = readl(spi->regs + SIFIVE_SPI_REG_CSDEF);
	writel(0xffffffffU, spi->regs + SIFIVE_SPI_REG_CSDEF);
	cs_bits = readl(spi->regs + SIFIVE_SPI_REG_CSDEF);
	writel(spi->cs_inactive, spi->regs + SIFIVE_SPI_REG_CSDEF);
	if (!cs_bits) {
		dev_warn(dev, "Could not auto probe CS lines\n");
		return;
	}

	spi->ctlr.num_chipselect = ilog2(cs_bits) + 1;
	if (spi->ctlr.num_chipselect > SIFIVE_SPI_MAX_CS) {
		dev_warn(dev, "Invalid number of spi slaves\n");
		return;
	}

	/* Watermark interrupts are disabled by default */
	writel(0, spi->regs + SIFIVE_SPI_REG_IE);

	/* Default watermark FIFO threshold values */
	writel(1, spi->regs + SIFIVE_SPI_REG_TXMARK);
	writel(0, spi->regs + SIFIVE_SPI_REG_RXMARK);

	/* Set CS/SCK Delays and Inactive Time to defaults */
	writel(SIFIVE_SPI_DELAY0_CSSCK(1) | SIFIVE_SPI_DELAY0_SCKCS(1),
	       spi->regs + SIFIVE_SPI_REG_DELAY0);
	writel(SIFIVE_SPI_DELAY1_INTERCS(1) | SIFIVE_SPI_DELAY1_INTERXFR(0),
	       spi->regs + SIFIVE_SPI_REG_DELAY1);

	/* Exit specialized memory-mapped SPI flash mode */
	writel(0, spi->regs + SIFIVE_SPI_REG_FCTRL);
}

static const struct spi_controller_mem_ops sifive_spi_mem_ops = {
	.exec_op	= sifive_spi_exec_op,
};

static void sifive_spi_dt_probe(struct sifive_spi *spi)
{
	struct device_node *node = spi->ctlr.dev->of_node;

	spi->fifo_depth = SIFIVE_SPI_DEFAULT_DEPTH;
	of_property_read_u32(node, "sifive,fifo-depth", &spi->fifo_depth);

	spi->bits_per_word = SIFIVE_SPI_DEFAULT_BITS;
	of_property_read_u32(node, "sifive,max-bits-per-word", &spi->bits_per_word);
}

static int sifive_spi_probe(struct device *dev)
{
	struct sifive_spi *spi;
	struct resource *iores;
	struct spi_controller *ctlr;
	struct clk *clkdev;

	iores = dev_request_mem_resource(dev, 0);
	if (IS_ERR(iores))
		return PTR_ERR(iores);

	spi = xzalloc(sizeof(*spi));

	spi->regs = IOMEM(iores->start);
	if (!spi->regs)
		return -ENODEV;

	ctlr = &spi->ctlr;
	ctlr->dev = dev;

	ctlr->setup = sifive_spi_setup;
	ctlr->transfer = sifive_spi_transfer;
	ctlr->mem_ops = &sifive_spi_mem_ops;

	ctlr->bus_num = -1;

	spi_controller_set_devdata(ctlr, spi);

	sifive_spi_dt_probe(spi);

	clkdev = clk_get(dev, NULL);
	if (IS_ERR(clkdev))
		return PTR_ERR(clkdev);

	spi->freq = clk_get_rate(clkdev);

	/* init the sifive spi hw */
	sifive_spi_init_hw(spi);

	return spi_register_master(ctlr);
}

static const struct of_device_id sifive_spi_ids[] = {
	{ .compatible = "sifive,spi0" },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, sifive_spi_ids);

static struct driver sifive_spi_driver = {
	.name  = "sifive_spi",
	.probe = sifive_spi_probe,
	.of_compatible = sifive_spi_ids,
};
coredevice_platform_driver(sifive_spi_driver);