zynq_spi.c 8.09 KB
Newer Older
1
/*
2
 * (C) Copyright 2013 Xilinx, Inc.
3
 * (C) Copyright 2015 Jagan Teki <jteki@openedev.com>
4 5 6 7 8 9 10
 *
 * Xilinx Zynq PS SPI controller driver (master mode only)
 *
 * SPDX-License-Identifier:     GPL-2.0+
 */

#include <common.h>
11
#include <dm.h>
12 13 14 15
#include <malloc.h>
#include <spi.h>
#include <asm/io.h>

16 17
DECLARE_GLOBAL_DATA_PTR;

18
/* zynq spi register bit masks ZYNQ_SPI_<REG>_<BIT>_MASK */
19 20
#define ZYNQ_SPI_CR_MSA_MASK		BIT(15)	/* Manual start enb */
#define ZYNQ_SPI_CR_MCS_MASK		BIT(14)	/* Manual chip select */
21 22
#define ZYNQ_SPI_CR_CS_MASK		GENMASK(13, 10)	/* Chip select */
#define ZYNQ_SPI_CR_BAUD_MASK		GENMASK(5, 3)	/* Baud rate div */
23 24 25 26 27
#define ZYNQ_SPI_CR_CPHA_MASK		BIT(2)	/* Clock phase */
#define ZYNQ_SPI_CR_CPOL_MASK		BIT(1)	/* Clock polarity */
#define ZYNQ_SPI_CR_MSTREN_MASK		BIT(0)	/* Mode select */
#define ZYNQ_SPI_IXR_RXNEMPTY_MASK	BIT(4)	/* RX_FIFO_not_empty */
#define ZYNQ_SPI_IXR_TXOW_MASK		BIT(2)	/* TX_FIFO_not_full */
28
#define ZYNQ_SPI_IXR_ALL_MASK		GENMASK(6, 0)	/* All IXR bits */
29
#define ZYNQ_SPI_ENR_SPI_EN_MASK	BIT(0)	/* SPI Enable */
30

31 32 33 34
#define ZYNQ_SPI_CR_BAUD_MAX		8	/* Baud rate divisor max val */
#define ZYNQ_SPI_CR_BAUD_SHIFT		3	/* Baud rate divisor shift */
#define ZYNQ_SPI_CR_SS_SHIFT		10	/* Slave select shift */

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#define ZYNQ_SPI_FIFO_DEPTH		128
#ifndef CONFIG_SYS_ZYNQ_SPI_WAIT
#define CONFIG_SYS_ZYNQ_SPI_WAIT	(CONFIG_SYS_HZ/100)	/* 10 ms */
#endif

/* zynq spi register set */
struct zynq_spi_regs {
	u32 cr;		/* 0x00 */
	u32 isr;	/* 0x04 */
	u32 ier;	/* 0x08 */
	u32 idr;	/* 0x0C */
	u32 imr;	/* 0x10 */
	u32 enr;	/* 0x14 */
	u32 dr;		/* 0x18 */
	u32 txdr;	/* 0x1C */
	u32 rxdr;	/* 0x20 */
};

53 54 55 56 57

/* zynq spi platform data */
struct zynq_spi_platdata {
	struct zynq_spi_regs *regs;
	u32 frequency;		/* input frequency */
58 59 60
	u32 speed_hz;
};

61 62 63
/* zynq spi priv */
struct zynq_spi_priv {
	struct zynq_spi_regs *regs;
64
	u8 cs;
65 66 67 68
	u8 mode;
	u8 fifo_depth;
	u32 freq;		/* required frequency */
};
69

70
static int zynq_spi_ofdata_to_platdata(struct udevice *bus)
71
{
72
	struct zynq_spi_platdata *plat = bus->platdata;
73 74 75
	const void *blob = gd->fdt_blob;
	int node = bus->of_offset;

76
	plat->regs = (struct zynq_spi_regs *)dev_get_addr(bus);
77

78 79 80
	/* FIXME: Use 250MHz as a suitable default */
	plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
					250000000);
81 82
	plat->speed_hz = plat->frequency / 2;

83
	debug("%s: regs=%p max-frequency=%d\n", __func__,
84 85
	      plat->regs, plat->frequency);

86 87 88 89 90 91
	return 0;
}

static void zynq_spi_init_hw(struct zynq_spi_priv *priv)
{
	struct zynq_spi_regs *regs = priv->regs;
92 93 94
	u32 confr;

	/* Disable SPI */
95
	writel(~ZYNQ_SPI_ENR_SPI_EN_MASK, &regs->enr);
96 97

	/* Disable Interrupts */
98
	writel(ZYNQ_SPI_IXR_ALL_MASK, &regs->idr);
99 100

	/* Clear RX FIFO */
101
	while (readl(&regs->isr) &
102
			ZYNQ_SPI_IXR_RXNEMPTY_MASK)
103
		readl(&regs->rxdr);
104 105

	/* Clear Interrupts */
106
	writel(ZYNQ_SPI_IXR_ALL_MASK, &regs->isr);
107 108 109 110 111

	/* Manual slave select and Auto start */
	confr = ZYNQ_SPI_CR_MCS_MASK | ZYNQ_SPI_CR_CS_MASK |
		ZYNQ_SPI_CR_MSTREN_MASK;
	confr &= ~ZYNQ_SPI_CR_MSA_MASK;
112
	writel(confr, &regs->cr);
113 114

	/* Enable SPI */
115
	writel(ZYNQ_SPI_ENR_SPI_EN_MASK, &regs->enr);
116 117
}

118
static int zynq_spi_probe(struct udevice *bus)
119
{
120 121 122 123 124 125 126 127 128 129
	struct zynq_spi_platdata *plat = dev_get_platdata(bus);
	struct zynq_spi_priv *priv = dev_get_priv(bus);

	priv->regs = plat->regs;
	priv->fifo_depth = ZYNQ_SPI_FIFO_DEPTH;

	/* init the zynq spi hw */
	zynq_spi_init_hw(priv);

	return 0;
130 131
}

132
static void spi_cs_activate(struct udevice *dev)
133
{
134 135 136
	struct udevice *bus = dev->parent;
	struct zynq_spi_priv *priv = dev_get_priv(bus);
	struct zynq_spi_regs *regs = priv->regs;
137 138
	u32 cr;

139 140
	clrbits_le32(&regs->cr, ZYNQ_SPI_CR_CS_MASK);
	cr = readl(&regs->cr);
141 142 143 144 145 146
	/*
	 * CS cal logic: CS[13:10]
	 * xxx0	- cs0
	 * xx01	- cs1
	 * x011 - cs2
	 */
147
	cr |= (~(1 << priv->cs) << ZYNQ_SPI_CR_SS_SHIFT) & ZYNQ_SPI_CR_CS_MASK;
148
	writel(cr, &regs->cr);
149 150
}

151
static void spi_cs_deactivate(struct udevice *dev)
152
{
153 154 155
	struct udevice *bus = dev->parent;
	struct zynq_spi_priv *priv = dev_get_priv(bus);
	struct zynq_spi_regs *regs = priv->regs;
156

157
	setbits_le32(&regs->cr, ZYNQ_SPI_CR_CS_MASK);
158 159
}

160
static int zynq_spi_claim_bus(struct udevice *dev)
161
{
162 163 164
	struct udevice *bus = dev->parent;
	struct zynq_spi_priv *priv = dev_get_priv(bus);
	struct zynq_spi_regs *regs = priv->regs;
165

166
	writel(ZYNQ_SPI_ENR_SPI_EN_MASK, &regs->enr);
167

168
	return 0;
169 170
}

171
static int zynq_spi_release_bus(struct udevice *dev)
172
{
173 174 175
	struct udevice *bus = dev->parent;
	struct zynq_spi_priv *priv = dev_get_priv(bus);
	struct zynq_spi_regs *regs = priv->regs;
176

177
	writel(~ZYNQ_SPI_ENR_SPI_EN_MASK, &regs->enr);
178 179 180 181

	return 0;
}

182 183
static int zynq_spi_xfer(struct udevice *dev, unsigned int bitlen,
			    const void *dout, void *din, unsigned long flags)
184
{
185 186 187 188
	struct udevice *bus = dev->parent;
	struct zynq_spi_priv *priv = dev_get_priv(bus);
	struct zynq_spi_regs *regs = priv->regs;
	struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
189 190 191 192 193 194 195
	u32 len = bitlen / 8;
	u32 tx_len = len, rx_len = len, tx_tvl;
	const u8 *tx_buf = dout;
	u8 *rx_buf = din, buf;
	u32 ts, status;

	debug("spi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n",
196
	      bus->seq, slave_plat->cs, bitlen, len, flags);
197 198 199 200 201 202

	if (bitlen % 8) {
		debug("spi_xfer: Non byte aligned SPI transfer\n");
		return -1;
	}

203
	priv->cs = slave_plat->cs;
204
	if (flags & SPI_XFER_BEGIN)
205
		spi_cs_activate(dev);
206 207 208 209

	while (rx_len > 0) {
		/* Write the data into TX FIFO - tx threshold is fifo_depth */
		tx_tvl = 0;
210
		while ((tx_tvl < priv->fifo_depth) && tx_len) {
211 212 213 214
			if (tx_buf)
				buf = *tx_buf++;
			else
				buf = 0;
215
			writel(buf, &regs->txdr);
216 217 218 219 220 221
			tx_len--;
			tx_tvl++;
		}

		/* Check TX FIFO completion */
		ts = get_timer(0);
222
		status = readl(&regs->isr);
223 224 225 226 227
		while (!(status & ZYNQ_SPI_IXR_TXOW_MASK)) {
			if (get_timer(ts) > CONFIG_SYS_ZYNQ_SPI_WAIT) {
				printf("spi_xfer: Timeout! TX FIFO not full\n");
				return -1;
			}
228
			status = readl(&regs->isr);
229 230 231
		}

		/* Read the data from RX FIFO */
232
		status = readl(&regs->isr);
233
		while (status & ZYNQ_SPI_IXR_RXNEMPTY_MASK) {
234
			buf = readl(&regs->rxdr);
235 236
			if (rx_buf)
				*rx_buf++ = buf;
237
			status = readl(&regs->isr);
238 239 240 241 242
			rx_len--;
		}
	}

	if (flags & SPI_XFER_END)
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
		spi_cs_deactivate(dev);

	return 0;
}

static int zynq_spi_set_speed(struct udevice *bus, uint speed)
{
	struct zynq_spi_platdata *plat = bus->platdata;
	struct zynq_spi_priv *priv = dev_get_priv(bus);
	struct zynq_spi_regs *regs = priv->regs;
	uint32_t confr;
	u8 baud_rate_val = 0;

	if (speed > plat->frequency)
		speed = plat->frequency;

	/* Set the clock frequency */
	confr = readl(&regs->cr);
	if (speed == 0) {
		/* Set baudrate x8, if the freq is 0 */
		baud_rate_val = 0x2;
	} else if (plat->speed_hz != speed) {
265
		while ((baud_rate_val < ZYNQ_SPI_CR_BAUD_MAX) &&
266 267 268 269 270
				((plat->frequency /
				(2 << baud_rate_val)) > speed))
			baud_rate_val++;
		plat->speed_hz = speed / (2 << baud_rate_val);
	}
271
	confr &= ~ZYNQ_SPI_CR_BAUD_MASK;
272
	confr |= (baud_rate_val << ZYNQ_SPI_CR_BAUD_SHIFT);
273 274 275 276

	writel(confr, &regs->cr);
	priv->freq = speed;

277 278
	debug("zynq_spi_set_speed: regs=%p, speed=%d\n",
	      priv->regs, priv->freq);
279 280 281 282 283 284 285 286 287 288 289 290 291 292

	return 0;
}

static int zynq_spi_set_mode(struct udevice *bus, uint mode)
{
	struct zynq_spi_priv *priv = dev_get_priv(bus);
	struct zynq_spi_regs *regs = priv->regs;
	uint32_t confr;

	/* Set the SPI Clock phase and polarities */
	confr = readl(&regs->cr);
	confr &= ~(ZYNQ_SPI_CR_CPHA_MASK | ZYNQ_SPI_CR_CPOL_MASK);

293
	if (mode & SPI_CPHA)
294
		confr |= ZYNQ_SPI_CR_CPHA_MASK;
295
	if (mode & SPI_CPOL)
296 297 298 299 300 301
		confr |= ZYNQ_SPI_CR_CPOL_MASK;

	writel(confr, &regs->cr);
	priv->mode = mode;

	debug("zynq_spi_set_mode: regs=%p, mode=%d\n", priv->regs, priv->mode);
302 303 304

	return 0;
}
305 306 307 308 309 310 311 312 313 314

static const struct dm_spi_ops zynq_spi_ops = {
	.claim_bus	= zynq_spi_claim_bus,
	.release_bus	= zynq_spi_release_bus,
	.xfer		= zynq_spi_xfer,
	.set_speed	= zynq_spi_set_speed,
	.set_mode	= zynq_spi_set_mode,
};

static const struct udevice_id zynq_spi_ids[] = {
Michal Simek's avatar
Michal Simek committed
315
	{ .compatible = "xlnx,zynq-spi-r1p6" },
316
	{ .compatible = "cdns,spi-r1p6" },
317 318 319 320 321 322 323 324 325 326 327 328 329
	{ }
};

U_BOOT_DRIVER(zynq_spi) = {
	.name	= "zynq_spi",
	.id	= UCLASS_SPI,
	.of_match = zynq_spi_ids,
	.ops	= &zynq_spi_ops,
	.ofdata_to_platdata = zynq_spi_ofdata_to_platdata,
	.platdata_auto_alloc_size = sizeof(struct zynq_spi_platdata),
	.priv_auto_alloc_size = sizeof(struct zynq_spi_priv),
	.probe	= zynq_spi_probe,
};