omap3_spi.c 17.5 KB
Newer Older
Dirk Behme's avatar
Dirk Behme committed
1
/*
2 3 4
 * Copyright (C) 2016 Jagan Teki <jteki@openedev.com>
 *		      Christophe Ricard <christophe.ricard@gmail.com>
 *
Dirk Behme's avatar
Dirk Behme committed
5 6 7 8 9 10 11 12 13 14 15 16
 * Copyright (C) 2010 Dirk Behme <dirk.behme@googlemail.com>
 *
 * Driver for McSPI controller on OMAP3. Based on davinci_spi.c
 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
 *
 * Copyright (C) 2007 Atmel Corporation
 *
 * Parts taken from linux/drivers/spi/omap2_mcspi.c
 * Copyright (C) 2005, 2006 Nokia Corporation
 *
 * Modified by Ruslan Araslanov <ruslan.araslanov@vitecmm.com>
 *
17
 * SPDX-License-Identifier:	GPL-2.0+
Dirk Behme's avatar
Dirk Behme committed
18 19 20
 */

#include <common.h>
21
#include <dm.h>
Dirk Behme's avatar
Dirk Behme committed
22 23 24 25
#include <spi.h>
#include <malloc.h>
#include <asm/io.h>

26 27
DECLARE_GLOBAL_DATA_PTR;

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
#if defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
#define OMAP3_MCSPI1_BASE	0x48030100
#define OMAP3_MCSPI2_BASE	0x481A0100
#else
#define OMAP3_MCSPI1_BASE	0x48098000
#define OMAP3_MCSPI2_BASE	0x4809A000
#define OMAP3_MCSPI3_BASE	0x480B8000
#define OMAP3_MCSPI4_BASE	0x480BA000
#endif

/* per-register bitmasks */
#define OMAP3_MCSPI_SYSCONFIG_SMARTIDLE (2 << 3)
#define OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP BIT(2)
#define OMAP3_MCSPI_SYSCONFIG_AUTOIDLE	BIT(0)
#define OMAP3_MCSPI_SYSCONFIG_SOFTRESET BIT(1)

#define OMAP3_MCSPI_SYSSTATUS_RESETDONE BIT(0)

#define OMAP3_MCSPI_MODULCTRL_SINGLE	BIT(0)
#define OMAP3_MCSPI_MODULCTRL_MS	BIT(2)
#define OMAP3_MCSPI_MODULCTRL_STEST	BIT(3)

#define OMAP3_MCSPI_CHCONF_PHA		BIT(0)
#define OMAP3_MCSPI_CHCONF_POL		BIT(1)
#define OMAP3_MCSPI_CHCONF_CLKD_MASK	GENMASK(5, 2)
#define OMAP3_MCSPI_CHCONF_EPOL		BIT(6)
#define OMAP3_MCSPI_CHCONF_WL_MASK	GENMASK(11, 7)
#define OMAP3_MCSPI_CHCONF_TRM_RX_ONLY	BIT(12)
#define OMAP3_MCSPI_CHCONF_TRM_TX_ONLY	BIT(13)
#define OMAP3_MCSPI_CHCONF_TRM_MASK	GENMASK(13, 12)
#define OMAP3_MCSPI_CHCONF_DMAW		BIT(14)
#define OMAP3_MCSPI_CHCONF_DMAR		BIT(15)
#define OMAP3_MCSPI_CHCONF_DPE0		BIT(16)
#define OMAP3_MCSPI_CHCONF_DPE1		BIT(17)
#define OMAP3_MCSPI_CHCONF_IS		BIT(18)
#define OMAP3_MCSPI_CHCONF_TURBO	BIT(19)
#define OMAP3_MCSPI_CHCONF_FORCE	BIT(20)

#define OMAP3_MCSPI_CHSTAT_RXS		BIT(0)
#define OMAP3_MCSPI_CHSTAT_TXS		BIT(1)
#define OMAP3_MCSPI_CHSTAT_EOT		BIT(2)

#define OMAP3_MCSPI_CHCTRL_EN		BIT(0)
#define OMAP3_MCSPI_CHCTRL_DIS		(0 << 0)

#define OMAP3_MCSPI_WAKEUPENABLE_WKEN	BIT(0)
74 75
#define MCSPI_PINDIR_D0_IN_D1_OUT	0
#define MCSPI_PINDIR_D0_OUT_D1_IN	1
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

#define OMAP3_MCSPI_MAX_FREQ		48000000
#define SPI_WAIT_TIMEOUT		10

/* OMAP3 McSPI registers */
struct mcspi_channel {
	unsigned int chconf;		/* 0x2C, 0x40, 0x54, 0x68 */
	unsigned int chstat;		/* 0x30, 0x44, 0x58, 0x6C */
	unsigned int chctrl;		/* 0x34, 0x48, 0x5C, 0x70 */
	unsigned int tx;		/* 0x38, 0x4C, 0x60, 0x74 */
	unsigned int rx;		/* 0x3C, 0x50, 0x64, 0x78 */
};

struct mcspi {
	unsigned char res1[0x10];
	unsigned int sysconfig;		/* 0x10 */
	unsigned int sysstatus;		/* 0x14 */
	unsigned int irqstatus;		/* 0x18 */
	unsigned int irqenable;		/* 0x1C */
	unsigned int wakeupenable;	/* 0x20 */
	unsigned int syst;		/* 0x24 */
	unsigned int modulctrl;		/* 0x28 */
	struct mcspi_channel channel[4];
	/* channel0: 0x2C - 0x3C, bus 0 & 1 & 2 & 3 */
	/* channel1: 0x40 - 0x50, bus 0 & 1 */
	/* channel2: 0x54 - 0x64, bus 0 & 1 */
	/* channel3: 0x68 - 0x78, bus 0 */
};

105
struct omap3_spi_priv {
106 107 108
#ifndef CONFIG_DM_SPI
	struct spi_slave slave;
#endif
109
	struct mcspi *regs;
110
	unsigned int cs;
111 112
	unsigned int freq;
	unsigned int mode;
113 114
	unsigned int wordlen;
	unsigned int pin_dir:1;
115 116
};

117
static void omap3_spi_write_chconf(struct omap3_spi_priv *priv, int val)
118
{
119
	writel(val, &priv->regs->channel[priv->cs].chconf);
120
	/* Flash post writes to make immediate effect */
121
	readl(&priv->regs->channel[priv->cs].chconf);
122 123
}

124
static void omap3_spi_set_enable(struct omap3_spi_priv *priv, int enable)
125
{
126
	writel(enable, &priv->regs->channel[priv->cs].chctrl);
127
	/* Flash post writes to make immediate effect */
128
	readl(&priv->regs->channel[priv->cs].chctrl);
Dirk Behme's avatar
Dirk Behme committed
129 130
}

131
static int omap3_spi_write(struct omap3_spi_priv *priv, unsigned int len,
132
			   const void *txp, unsigned long flags)
Dirk Behme's avatar
Dirk Behme committed
133
{
134
	ulong start;
135 136 137
	int i, chconf;

	chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme's avatar
Dirk Behme committed
138

139
	/* Enable the channel */
140
	omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme's avatar
Dirk Behme committed
141

142
	chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
143
	chconf |= (priv->wordlen - 1) << 7;
Dirk Behme's avatar
Dirk Behme committed
144 145
	chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
	chconf |= OMAP3_MCSPI_CHCONF_FORCE;
146
	omap3_spi_write_chconf(priv, chconf);
Dirk Behme's avatar
Dirk Behme committed
147 148 149

	for (i = 0; i < len; i++) {
		/* wait till TX register is empty (TXS == 1) */
150
		start = get_timer(0);
151
		while (!(readl(&priv->regs->channel[priv->cs].chstat) &
Dirk Behme's avatar
Dirk Behme committed
152
			 OMAP3_MCSPI_CHSTAT_TXS)) {
153
			if (get_timer(start) > SPI_WAIT_TIMEOUT) {
Dirk Behme's avatar
Dirk Behme committed
154
				printf("SPI TXS timed out, status=0x%08x\n",
155
					readl(&priv->regs->channel[priv->cs].chstat));
Dirk Behme's avatar
Dirk Behme committed
156 157 158 159
				return -1;
			}
		}
		/* Write the data */
160 161
		unsigned int *tx = &priv->regs->channel[priv->cs].tx;
		if (priv->wordlen > 16)
162
			writel(((u32 *)txp)[i], tx);
163
		else if (priv->wordlen > 8)
164 165 166
			writel(((u16 *)txp)[i], tx);
		else
			writel(((u8 *)txp)[i], tx);
Dirk Behme's avatar
Dirk Behme committed
167 168
	}

169
	/* wait to finish of transfer */
170 171 172 173
	while ((readl(&priv->regs->channel[priv->cs].chstat) &
			(OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS)) !=
			(OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS))
		;
174 175

	/* Disable the channel otherwise the next immediate RX will get affected */
176
	omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
177

Dirk Behme's avatar
Dirk Behme committed
178 179 180
	if (flags & SPI_XFER_END) {

		chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
181
		omap3_spi_write_chconf(priv, chconf);
Dirk Behme's avatar
Dirk Behme committed
182 183 184 185
	}
	return 0;
}

186
static int omap3_spi_read(struct omap3_spi_priv *priv, unsigned int len,
187
			  void *rxp, unsigned long flags)
Dirk Behme's avatar
Dirk Behme committed
188
{
189
	int i, chconf;
190
	ulong start;
191 192

	chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme's avatar
Dirk Behme committed
193

194
	/* Enable the channel */
195
	omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme's avatar
Dirk Behme committed
196

197
	chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
198
	chconf |= (priv->wordlen - 1) << 7;
Dirk Behme's avatar
Dirk Behme committed
199 200
	chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
	chconf |= OMAP3_MCSPI_CHCONF_FORCE;
201
	omap3_spi_write_chconf(priv, chconf);
Dirk Behme's avatar
Dirk Behme committed
202

203
	writel(0, &priv->regs->channel[priv->cs].tx);
Dirk Behme's avatar
Dirk Behme committed
204 205

	for (i = 0; i < len; i++) {
206
		start = get_timer(0);
Dirk Behme's avatar
Dirk Behme committed
207
		/* Wait till RX register contains data (RXS == 1) */
208
		while (!(readl(&priv->regs->channel[priv->cs].chstat) &
Dirk Behme's avatar
Dirk Behme committed
209
			 OMAP3_MCSPI_CHSTAT_RXS)) {
210
			if (get_timer(start) > SPI_WAIT_TIMEOUT) {
Dirk Behme's avatar
Dirk Behme committed
211
				printf("SPI RXS timed out, status=0x%08x\n",
212
					readl(&priv->regs->channel[priv->cs].chstat));
Dirk Behme's avatar
Dirk Behme committed
213 214 215
				return -1;
			}
		}
216 217

		/* Disable the channel to prevent furher receiving */
218 219
		if (i == (len - 1))
			omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
220

Dirk Behme's avatar
Dirk Behme committed
221
		/* Read the data */
222 223
		unsigned int *rx = &priv->regs->channel[priv->cs].rx;
		if (priv->wordlen > 16)
224
			((u32 *)rxp)[i] = readl(rx);
225
		else if (priv->wordlen > 8)
226 227 228
			((u16 *)rxp)[i] = (u16)readl(rx);
		else
			((u8 *)rxp)[i] = (u8)readl(rx);
Dirk Behme's avatar
Dirk Behme committed
229 230 231 232
	}

	if (flags & SPI_XFER_END) {
		chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
233
		omap3_spi_write_chconf(priv, chconf);
Dirk Behme's avatar
Dirk Behme committed
234 235 236 237 238
	}

	return 0;
}

239
/*McSPI Transmit Receive Mode*/
240
static int omap3_spi_txrx(struct omap3_spi_priv *priv, unsigned int len,
241
			  const void *txp, void *rxp, unsigned long flags)
242
{
243
	ulong start;
244 245 246
	int chconf, i = 0;

	chconf = readl(&priv->regs->channel[priv->cs].chconf);
247 248

	/*Enable SPI channel*/
249
	omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
250 251

	/*set TRANSMIT-RECEIVE Mode*/
252
	chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
253
	chconf |= (priv->wordlen - 1) << 7;
254
	chconf |= OMAP3_MCSPI_CHCONF_FORCE;
255
	omap3_spi_write_chconf(priv, chconf);
256 257 258 259

	/*Shift in and out 1 byte at time*/
	for (i=0; i < len; i++){
		/* Write: wait for TX empty (TXS == 1)*/
260
		start = get_timer(0);
261
		while (!(readl(&priv->regs->channel[priv->cs].chstat) &
262
			 OMAP3_MCSPI_CHSTAT_TXS)) {
263
			if (get_timer(start) > SPI_WAIT_TIMEOUT) {
264
				printf("SPI TXS timed out, status=0x%08x\n",
265
					readl(&priv->regs->channel[priv->cs].chstat));
266 267 268 269
				return -1;
			}
		}
		/* Write the data */
270 271
		unsigned int *tx = &priv->regs->channel[priv->cs].tx;
		if (priv->wordlen > 16)
272
			writel(((u32 *)txp)[i], tx);
273
		else if (priv->wordlen > 8)
274 275 276
			writel(((u16 *)txp)[i], tx);
		else
			writel(((u8 *)txp)[i], tx);
277 278

		/*Read: wait for RX containing data (RXS == 1)*/
279
		start = get_timer(0);
280
		while (!(readl(&priv->regs->channel[priv->cs].chstat) &
281
			 OMAP3_MCSPI_CHSTAT_RXS)) {
282
			if (get_timer(start) > SPI_WAIT_TIMEOUT) {
283
				printf("SPI RXS timed out, status=0x%08x\n",
284
					readl(&priv->regs->channel[priv->cs].chstat));
285 286 287 288
				return -1;
			}
		}
		/* Read the data */
289 290
		unsigned int *rx = &priv->regs->channel[priv->cs].rx;
		if (priv->wordlen > 16)
291
			((u32 *)rxp)[i] = readl(rx);
292
		else if (priv->wordlen > 8)
293 294 295
			((u16 *)rxp)[i] = (u16)readl(rx);
		else
			((u8 *)rxp)[i] = (u8)readl(rx);
296
	}
297
	/* Disable the channel */
298
	omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
299 300 301 302

	/*if transfer must be terminated disable the channel*/
	if (flags & SPI_XFER_END) {
		chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
303
		omap3_spi_write_chconf(priv, chconf);
304 305 306 307 308
	}

	return 0;
}

309 310
static int _spi_xfer(struct omap3_spi_priv *priv, unsigned int bitlen,
		     const void *dout, void *din, unsigned long flags)
Dirk Behme's avatar
Dirk Behme committed
311 312 313 314
{
	unsigned int	len;
	int ret = -1;

315 316
	if (priv->wordlen < 4 || priv->wordlen > 32) {
		printf("omap3_spi: invalid wordlen %d\n", priv->wordlen);
317 318 319
		return -1;
	}

320
	if (bitlen % priv->wordlen)
Dirk Behme's avatar
Dirk Behme committed
321 322
		return -1;

323
	len = bitlen / priv->wordlen;
Dirk Behme's avatar
Dirk Behme committed
324 325

	if (bitlen == 0) {	 /* only change CS */
326
		int chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme's avatar
Dirk Behme committed
327 328

		if (flags & SPI_XFER_BEGIN) {
329
			omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme's avatar
Dirk Behme committed
330
			chconf |= OMAP3_MCSPI_CHCONF_FORCE;
331
			omap3_spi_write_chconf(priv, chconf);
Dirk Behme's avatar
Dirk Behme committed
332 333 334
		}
		if (flags & SPI_XFER_END) {
			chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
335 336
			omap3_spi_write_chconf(priv, chconf);
			omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
Dirk Behme's avatar
Dirk Behme committed
337 338 339
		}
		ret = 0;
	} else {
340
		if (dout != NULL && din != NULL)
341
			ret = omap3_spi_txrx(priv, len, dout, din, flags);
342
		else if (dout != NULL)
343
			ret = omap3_spi_write(priv, len, dout, flags);
344
		else if (din != NULL)
345
			ret = omap3_spi_read(priv, len, din, flags);
Dirk Behme's avatar
Dirk Behme committed
346 347 348 349
	}
	return ret;
}

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
static void _omap3_spi_set_speed(struct omap3_spi_priv *priv)
{
	uint32_t confr, div = 0;

	confr = readl(&priv->regs->channel[priv->cs].chconf);

	/* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */
	if (priv->freq) {
		while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div))
					> priv->freq)
			div++;
	} else {
		 div = 0xC;
	}

	/* set clock divisor */
	confr &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
	confr |= div << 2;

	omap3_spi_write_chconf(priv, confr);
}

static void _omap3_spi_set_mode(struct omap3_spi_priv *priv)
{
	uint32_t confr;

	confr = readl(&priv->regs->channel[priv->cs].chconf);

	/* standard 4-wire master mode:  SCK, MOSI/out, MISO/in, nCS
	 * REVISIT: this controller could support SPI_3WIRE mode.
	 */
	if (priv->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
		confr &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
		confr |= OMAP3_MCSPI_CHCONF_DPE0;
	} else {
		confr &= ~OMAP3_MCSPI_CHCONF_DPE0;
		confr |= OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1;
	}

	/* set SPI mode 0..3 */
	confr &= ~(OMAP3_MCSPI_CHCONF_POL | OMAP3_MCSPI_CHCONF_PHA);
	if (priv->mode & SPI_CPHA)
		confr |= OMAP3_MCSPI_CHCONF_PHA;
	if (priv->mode & SPI_CPOL)
		confr |= OMAP3_MCSPI_CHCONF_POL;

	/* set chipselect polarity; manage with FORCE */
	if (!(priv->mode & SPI_CS_HIGH))
		confr |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
	else
		confr &= ~OMAP3_MCSPI_CHCONF_EPOL;

	/* Transmit & receive mode */
	confr &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;

	omap3_spi_write_chconf(priv, confr);
}

static void _omap3_spi_set_wordlen(struct omap3_spi_priv *priv)
{
	unsigned int confr;

	/* McSPI individual channel configuration */
	confr = readl(&priv->regs->channel[priv->wordlen].chconf);

	/* wordlength */
	confr &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
	confr |= (priv->wordlen - 1) << 7;

	omap3_spi_write_chconf(priv, confr);
}

static void spi_reset(struct mcspi *regs)
{
	unsigned int tmp;

	writel(OMAP3_MCSPI_SYSCONFIG_SOFTRESET, &regs->sysconfig);
	do {
		tmp = readl(&regs->sysstatus);
	} while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE));

	writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE |
	       OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP |
	       OMAP3_MCSPI_SYSCONFIG_SMARTIDLE, &regs->sysconfig);

	writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, &regs->wakeupenable);
}

static void _omap3_spi_claim_bus(struct omap3_spi_priv *priv)
{
	unsigned int conf;

	spi_reset(priv->regs);

	/*
	 * setup when switching from (reset default) slave mode
	 * to single-channel master mode
	 */
	conf = readl(&priv->regs->modulctrl);
	conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS);
	conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;

	writel(conf, &priv->regs->modulctrl);

	_omap3_spi_set_mode(priv);
	_omap3_spi_set_speed(priv);
}

#ifndef CONFIG_DM_SPI

460
static inline struct omap3_spi_priv *to_omap3_spi(struct spi_slave *slave)
461
{
462
	return container_of(slave, struct omap3_spi_priv, slave);
463 464 465
}

void spi_init(void)
Dirk Behme's avatar
Dirk Behme committed
466
{
467
	/* do nothing */
Dirk Behme's avatar
Dirk Behme committed
468 469
}

470
void spi_free_slave(struct spi_slave *slave)
Dirk Behme's avatar
Dirk Behme committed
471
{
472
	struct omap3_spi_priv *priv = to_omap3_spi(slave);
473

474
	free(priv);
475 476 477 478
}

int spi_claim_bus(struct spi_slave *slave)
{
479 480
	struct omap3_spi_priv *priv = to_omap3_spi(slave);

481 482 483 484 485 486
	_omap3_spi_claim_bus(priv);
	_omap3_spi_set_wordlen(priv);
	_omap3_spi_set_mode(priv);
	_omap3_spi_set_speed(priv);

	return 0;
Dirk Behme's avatar
Dirk Behme committed
487 488
}

489
void spi_release_bus(struct spi_slave *slave)
Dirk Behme's avatar
Dirk Behme committed
490
{
491 492
	struct omap3_spi_priv *priv = to_omap3_spi(slave);

493 494
	/* Reset the SPI hardware */
	spi_reset(priv->regs);
Dirk Behme's avatar
Dirk Behme committed
495
}
496 497 498 499

struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
				     unsigned int max_hz, unsigned int mode)
{
500
	struct omap3_spi_priv *priv;
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
	struct mcspi *regs;

	/*
	 * OMAP3 McSPI (MultiChannel SPI) has 4 busses (modules)
	 * with different number of chip selects (CS, channels):
	 * McSPI1 has 4 CS (bus 0, cs 0 - 3)
	 * McSPI2 has 2 CS (bus 1, cs 0 - 1)
	 * McSPI3 has 2 CS (bus 2, cs 0 - 1)
	 * McSPI4 has 1 CS (bus 3, cs 0)
	 */

	switch (bus) {
	case 0:
		 regs = (struct mcspi *)OMAP3_MCSPI1_BASE;
		 break;
#ifdef OMAP3_MCSPI2_BASE
	case 1:
		 regs = (struct mcspi *)OMAP3_MCSPI2_BASE;
		 break;
#endif
#ifdef OMAP3_MCSPI3_BASE
	case 2:
		 regs = (struct mcspi *)OMAP3_MCSPI3_BASE;
		 break;
#endif
#ifdef OMAP3_MCSPI4_BASE
	case 3:
		 regs = (struct mcspi *)OMAP3_MCSPI4_BASE;
		 break;
#endif
	default:
		 printf("SPI error: unsupported bus %i.  Supported busses 0 - 3\n", bus);
		 return NULL;
	}

	if (((bus == 0) && (cs > 3)) ||
	    ((bus == 1) && (cs > 1)) ||
	    ((bus == 2) && (cs > 1)) ||
	    ((bus == 3) && (cs > 0))) {
		printf("SPI error: unsupported chip select %i on bus %i\n", cs, bus);
		return NULL;
	}

	if (max_hz > OMAP3_MCSPI_MAX_FREQ) {
		printf("SPI error: unsupported frequency %i Hz. Max frequency is 48 Mhz\n", max_hz);
		return NULL;
	}

	if (mode > SPI_MODE_3) {
		printf("SPI error: unsupported SPI mode %i\n", mode);
		return NULL;
	}

554 555
	priv = spi_alloc_slave(struct omap3_spi_priv, bus, cs);
	if (!priv) {
556 557 558 559 560 561 562 563
		printf("SPI error: malloc of SPI structure failed\n");
		return NULL;
	}

	priv->regs = regs;
	priv->cs = cs;
	priv->freq = max_hz;
	priv->mode = mode;
564
	priv->wordlen = priv->slave.wordlen;
565 566 567 568
#ifdef CONFIG_OMAP3_SPI_D0_D1_SWAPPED
	priv->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
#endif

569
	return &priv->slave;
570 571 572 573
}

int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
	     const void *dout, void *din, unsigned long flags)
574 575 576 577 578
{
	struct omap3_spi_priv *priv = to_omap3_spi(slave);

	return _spi_xfer(priv, bitlen, dout, din, flags);
}
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680

#else

static int omap3_spi_claim_bus(struct udevice *dev)
{
	struct udevice *bus = dev->parent;
	struct omap3_spi_priv *priv = dev_get_priv(bus);
	struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);

	priv->cs = slave_plat->cs;
	priv->mode = slave_plat->mode;
	priv->freq = slave_plat->max_hz;
	_omap3_spi_claim_bus(priv);

	return 0;
}

static int omap3_spi_release_bus(struct udevice *dev)
{
	struct udevice *bus = dev->parent;
	struct omap3_spi_priv *priv = dev_get_priv(bus);

	/* Reset the SPI hardware */
	spi_reset(priv->regs);

	return 0;
}

static int omap3_spi_set_wordlen(struct udevice *dev, unsigned int wordlen)
{
	struct udevice *bus = dev->parent;
	struct omap3_spi_priv *priv = dev_get_priv(bus);
	struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);

	priv->cs = slave_plat->cs;
	priv->wordlen = wordlen;
	_omap3_spi_set_wordlen(priv);

	return 0;
}

static int omap3_spi_probe(struct udevice *dev)
{
	struct omap3_spi_priv *priv = dev_get_priv(dev);
	const void *blob = gd->fdt_blob;
	int node = dev->of_offset;

	priv->regs = (struct mcspi *)dev_get_addr(dev);
	priv->pin_dir = fdtdec_get_uint(blob, node, "ti,pindir-d0-out-d1-in",
					    MCSPI_PINDIR_D0_IN_D1_OUT);
	priv->wordlen = SPI_DEFAULT_WORDLEN;
	return 0;
}

static int omap3_spi_xfer(struct udevice *dev, unsigned int bitlen,
			    const void *dout, void *din, unsigned long flags)
{
	struct udevice *bus = dev->parent;
	struct omap3_spi_priv *priv = dev_get_priv(bus);

	return _spi_xfer(priv, bitlen, dout, din, flags);
}

static int omap3_spi_set_speed(struct udevice *bus, unsigned int speed)
{
	return 0;
}

static int omap3_spi_set_mode(struct udevice *bus, uint mode)
{
	return 0;
}

static const struct dm_spi_ops omap3_spi_ops = {
	.claim_bus      = omap3_spi_claim_bus,
	.release_bus    = omap3_spi_release_bus,
	.set_wordlen    = omap3_spi_set_wordlen,
	.xfer	    = omap3_spi_xfer,
	.set_speed      = omap3_spi_set_speed,
	.set_mode	= omap3_spi_set_mode,
	/*
	 * cs_info is not needed, since we require all chip selects to be
	 * in the device tree explicitly
	 */
};

static const struct udevice_id omap3_spi_ids[] = {
	{ .compatible = "ti,omap2-mcspi" },
	{ .compatible = "ti,omap4-mcspi" },
	{ }
};

U_BOOT_DRIVER(omap3_spi) = {
	.name   = "omap3_spi",
	.id     = UCLASS_SPI,
	.of_match = omap3_spi_ids,
	.probe = omap3_spi_probe,
	.ops    = &omap3_spi_ops,
	.priv_auto_alloc_size = sizeof(struct omap3_spi_priv),
	.probe = omap3_spi_probe,
};
#endif