2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2010-12-11 16:01:00 +00:00
|
|
|
/*
|
2016-03-14 17:11:24 +00:00
|
|
|
* Copyright (C) 2016 Jagan Teki <jteki@openedev.com>
|
|
|
|
* Christophe Ricard <christophe.ricard@gmail.com>
|
|
|
|
*
|
2010-12-11 16:01:00 +00:00
|
|
|
* 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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2016-03-14 17:11:24 +00:00
|
|
|
#include <dm.h>
|
2010-12-11 16:01:00 +00:00
|
|
|
#include <spi.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <asm/io.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2020-09-14 06:41:15 +00:00
|
|
|
#include <omap3_spi.h>
|
2010-12-11 16:01:00 +00:00
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2016-05-19 07:11:58 +00:00
|
|
|
struct omap2_mcspi_platform_config {
|
|
|
|
unsigned int regs_offset;
|
|
|
|
};
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
struct omap3_spi_priv {
|
2016-03-01 14:16:20 +00:00
|
|
|
struct mcspi *regs;
|
2016-03-14 17:11:24 +00:00
|
|
|
unsigned int cs;
|
2016-03-01 14:16:20 +00:00
|
|
|
unsigned int freq;
|
|
|
|
unsigned int mode;
|
2016-03-14 17:11:24 +00:00
|
|
|
unsigned int wordlen;
|
|
|
|
unsigned int pin_dir:1;
|
2016-03-01 14:16:20 +00:00
|
|
|
};
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
static void omap3_spi_write_chconf(struct omap3_spi_priv *priv, int val)
|
2016-03-01 14:16:20 +00:00
|
|
|
{
|
2016-03-14 17:11:24 +00:00
|
|
|
writel(val, &priv->regs->channel[priv->cs].chconf);
|
2012-11-17 21:10:15 +00:00
|
|
|
/* Flash post writes to make immediate effect */
|
2016-03-14 17:11:24 +00:00
|
|
|
readl(&priv->regs->channel[priv->cs].chconf);
|
2012-11-17 21:10:15 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
static void omap3_spi_set_enable(struct omap3_spi_priv *priv, int enable)
|
2012-11-17 21:10:15 +00:00
|
|
|
{
|
2016-03-14 17:11:24 +00:00
|
|
|
writel(enable, &priv->regs->channel[priv->cs].chctrl);
|
2013-10-04 15:43:24 +00:00
|
|
|
/* Flash post writes to make immediate effect */
|
2016-03-14 17:11:24 +00:00
|
|
|
readl(&priv->regs->channel[priv->cs].chctrl);
|
2010-12-11 16:01:00 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
static int omap3_spi_write(struct omap3_spi_priv *priv, unsigned int len,
|
2016-03-01 14:16:21 +00:00
|
|
|
const void *txp, unsigned long flags)
|
2010-12-11 16:01:00 +00:00
|
|
|
{
|
2015-04-01 12:20:24 +00:00
|
|
|
ulong start;
|
2016-03-14 17:11:24 +00:00
|
|
|
int i, chconf;
|
|
|
|
|
|
|
|
chconf = readl(&priv->regs->channel[priv->cs].chconf);
|
2010-12-11 16:01:00 +00:00
|
|
|
|
2012-11-17 21:10:15 +00:00
|
|
|
/* Enable the channel */
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
|
2010-12-11 16:01:00 +00:00
|
|
|
|
2013-10-16 14:23:25 +00:00
|
|
|
chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
|
2016-03-14 17:11:24 +00:00
|
|
|
chconf |= (priv->wordlen - 1) << 7;
|
2010-12-11 16:01:00 +00:00
|
|
|
chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
|
|
|
|
chconf |= OMAP3_MCSPI_CHCONF_FORCE;
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_write_chconf(priv, chconf);
|
2010-12-11 16:01:00 +00:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
/* wait till TX register is empty (TXS == 1) */
|
2015-04-01 12:20:24 +00:00
|
|
|
start = get_timer(0);
|
2016-03-14 17:11:24 +00:00
|
|
|
while (!(readl(&priv->regs->channel[priv->cs].chstat) &
|
2010-12-11 16:01:00 +00:00
|
|
|
OMAP3_MCSPI_CHSTAT_TXS)) {
|
2015-04-01 12:20:24 +00:00
|
|
|
if (get_timer(start) > SPI_WAIT_TIMEOUT) {
|
2010-12-11 16:01:00 +00:00
|
|
|
printf("SPI TXS timed out, status=0x%08x\n",
|
2016-03-14 17:11:24 +00:00
|
|
|
readl(&priv->regs->channel[priv->cs].chstat));
|
2010-12-11 16:01:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Write the data */
|
2016-03-14 17:11:24 +00:00
|
|
|
unsigned int *tx = &priv->regs->channel[priv->cs].tx;
|
|
|
|
if (priv->wordlen > 16)
|
2013-10-16 14:23:25 +00:00
|
|
|
writel(((u32 *)txp)[i], tx);
|
2016-03-14 17:11:24 +00:00
|
|
|
else if (priv->wordlen > 8)
|
2013-10-16 14:23:25 +00:00
|
|
|
writel(((u16 *)txp)[i], tx);
|
|
|
|
else
|
|
|
|
writel(((u8 *)txp)[i], tx);
|
2010-12-11 16:01:00 +00:00
|
|
|
}
|
|
|
|
|
2013-10-04 15:43:24 +00:00
|
|
|
/* wait to finish of transfer */
|
2016-03-14 17:11:24 +00:00
|
|
|
while ((readl(&priv->regs->channel[priv->cs].chstat) &
|
|
|
|
(OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS)) !=
|
|
|
|
(OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS))
|
|
|
|
;
|
2012-11-17 21:10:15 +00:00
|
|
|
|
|
|
|
/* Disable the channel otherwise the next immediate RX will get affected */
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
|
2012-11-17 21:10:15 +00:00
|
|
|
|
2010-12-11 16:01:00 +00:00
|
|
|
if (flags & SPI_XFER_END) {
|
|
|
|
|
|
|
|
chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_write_chconf(priv, chconf);
|
2010-12-11 16:01:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
static int omap3_spi_read(struct omap3_spi_priv *priv, unsigned int len,
|
2016-03-01 14:16:21 +00:00
|
|
|
void *rxp, unsigned long flags)
|
2010-12-11 16:01:00 +00:00
|
|
|
{
|
2016-03-14 17:11:24 +00:00
|
|
|
int i, chconf;
|
2015-04-01 12:20:24 +00:00
|
|
|
ulong start;
|
2016-03-14 17:11:24 +00:00
|
|
|
|
|
|
|
chconf = readl(&priv->regs->channel[priv->cs].chconf);
|
2010-12-11 16:01:00 +00:00
|
|
|
|
2012-11-17 21:10:15 +00:00
|
|
|
/* Enable the channel */
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
|
2010-12-11 16:01:00 +00:00
|
|
|
|
2013-10-16 14:23:25 +00:00
|
|
|
chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
|
2016-03-14 17:11:24 +00:00
|
|
|
chconf |= (priv->wordlen - 1) << 7;
|
2010-12-11 16:01:00 +00:00
|
|
|
chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
|
|
|
|
chconf |= OMAP3_MCSPI_CHCONF_FORCE;
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_write_chconf(priv, chconf);
|
2010-12-11 16:01:00 +00:00
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
writel(0, &priv->regs->channel[priv->cs].tx);
|
2010-12-11 16:01:00 +00:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
2015-04-01 12:20:24 +00:00
|
|
|
start = get_timer(0);
|
2010-12-11 16:01:00 +00:00
|
|
|
/* Wait till RX register contains data (RXS == 1) */
|
2016-03-14 17:11:24 +00:00
|
|
|
while (!(readl(&priv->regs->channel[priv->cs].chstat) &
|
2010-12-11 16:01:00 +00:00
|
|
|
OMAP3_MCSPI_CHSTAT_RXS)) {
|
2015-04-01 12:20:24 +00:00
|
|
|
if (get_timer(start) > SPI_WAIT_TIMEOUT) {
|
2010-12-11 16:01:00 +00:00
|
|
|
printf("SPI RXS timed out, status=0x%08x\n",
|
2016-03-14 17:11:24 +00:00
|
|
|
readl(&priv->regs->channel[priv->cs].chstat));
|
2010-12-11 16:01:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2012-11-17 21:10:15 +00:00
|
|
|
|
|
|
|
/* Disable the channel to prevent furher receiving */
|
2016-03-14 17:11:24 +00:00
|
|
|
if (i == (len - 1))
|
|
|
|
omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
|
2012-11-17 21:10:15 +00:00
|
|
|
|
2010-12-11 16:01:00 +00:00
|
|
|
/* Read the data */
|
2016-03-14 17:11:24 +00:00
|
|
|
unsigned int *rx = &priv->regs->channel[priv->cs].rx;
|
|
|
|
if (priv->wordlen > 16)
|
2013-10-16 14:23:25 +00:00
|
|
|
((u32 *)rxp)[i] = readl(rx);
|
2016-03-14 17:11:24 +00:00
|
|
|
else if (priv->wordlen > 8)
|
2013-10-16 14:23:25 +00:00
|
|
|
((u16 *)rxp)[i] = (u16)readl(rx);
|
|
|
|
else
|
|
|
|
((u8 *)rxp)[i] = (u8)readl(rx);
|
2010-12-11 16:01:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SPI_XFER_END) {
|
|
|
|
chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_write_chconf(priv, chconf);
|
2010-12-11 16:01:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-02 05:13:22 +00:00
|
|
|
/*McSPI Transmit Receive Mode*/
|
2016-03-14 17:11:24 +00:00
|
|
|
static int omap3_spi_txrx(struct omap3_spi_priv *priv, unsigned int len,
|
2016-03-01 14:16:21 +00:00
|
|
|
const void *txp, void *rxp, unsigned long flags)
|
2011-03-02 05:13:22 +00:00
|
|
|
{
|
2015-04-01 12:20:24 +00:00
|
|
|
ulong start;
|
2016-03-14 17:11:24 +00:00
|
|
|
int chconf, i = 0;
|
|
|
|
|
|
|
|
chconf = readl(&priv->regs->channel[priv->cs].chconf);
|
2011-03-02 05:13:22 +00:00
|
|
|
|
|
|
|
/*Enable SPI channel*/
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
|
2011-03-02 05:13:22 +00:00
|
|
|
|
|
|
|
/*set TRANSMIT-RECEIVE Mode*/
|
2013-10-16 14:23:25 +00:00
|
|
|
chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
|
2016-03-14 17:11:24 +00:00
|
|
|
chconf |= (priv->wordlen - 1) << 7;
|
2011-03-02 05:13:22 +00:00
|
|
|
chconf |= OMAP3_MCSPI_CHCONF_FORCE;
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_write_chconf(priv, chconf);
|
2011-03-02 05:13:22 +00:00
|
|
|
|
|
|
|
/*Shift in and out 1 byte at time*/
|
|
|
|
for (i=0; i < len; i++){
|
|
|
|
/* Write: wait for TX empty (TXS == 1)*/
|
2015-04-01 12:20:24 +00:00
|
|
|
start = get_timer(0);
|
2016-03-14 17:11:24 +00:00
|
|
|
while (!(readl(&priv->regs->channel[priv->cs].chstat) &
|
2011-03-02 05:13:22 +00:00
|
|
|
OMAP3_MCSPI_CHSTAT_TXS)) {
|
2015-04-01 12:20:24 +00:00
|
|
|
if (get_timer(start) > SPI_WAIT_TIMEOUT) {
|
2011-03-02 05:13:22 +00:00
|
|
|
printf("SPI TXS timed out, status=0x%08x\n",
|
2016-03-14 17:11:24 +00:00
|
|
|
readl(&priv->regs->channel[priv->cs].chstat));
|
2011-03-02 05:13:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Write the data */
|
2016-03-14 17:11:24 +00:00
|
|
|
unsigned int *tx = &priv->regs->channel[priv->cs].tx;
|
|
|
|
if (priv->wordlen > 16)
|
2013-10-16 14:23:25 +00:00
|
|
|
writel(((u32 *)txp)[i], tx);
|
2016-03-14 17:11:24 +00:00
|
|
|
else if (priv->wordlen > 8)
|
2013-10-16 14:23:25 +00:00
|
|
|
writel(((u16 *)txp)[i], tx);
|
|
|
|
else
|
|
|
|
writel(((u8 *)txp)[i], tx);
|
2011-03-02 05:13:22 +00:00
|
|
|
|
|
|
|
/*Read: wait for RX containing data (RXS == 1)*/
|
2015-04-01 12:20:24 +00:00
|
|
|
start = get_timer(0);
|
2016-03-14 17:11:24 +00:00
|
|
|
while (!(readl(&priv->regs->channel[priv->cs].chstat) &
|
2011-03-02 05:13:22 +00:00
|
|
|
OMAP3_MCSPI_CHSTAT_RXS)) {
|
2015-04-01 12:20:24 +00:00
|
|
|
if (get_timer(start) > SPI_WAIT_TIMEOUT) {
|
2011-03-02 05:13:22 +00:00
|
|
|
printf("SPI RXS timed out, status=0x%08x\n",
|
2016-03-14 17:11:24 +00:00
|
|
|
readl(&priv->regs->channel[priv->cs].chstat));
|
2011-03-02 05:13:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Read the data */
|
2016-03-14 17:11:24 +00:00
|
|
|
unsigned int *rx = &priv->regs->channel[priv->cs].rx;
|
|
|
|
if (priv->wordlen > 16)
|
2013-10-16 14:23:25 +00:00
|
|
|
((u32 *)rxp)[i] = readl(rx);
|
2016-03-14 17:11:24 +00:00
|
|
|
else if (priv->wordlen > 8)
|
2013-10-16 14:23:25 +00:00
|
|
|
((u16 *)rxp)[i] = (u16)readl(rx);
|
|
|
|
else
|
|
|
|
((u8 *)rxp)[i] = (u8)readl(rx);
|
2011-03-02 05:13:22 +00:00
|
|
|
}
|
2012-11-17 21:10:15 +00:00
|
|
|
/* Disable the channel */
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
|
2011-03-02 05:13:22 +00:00
|
|
|
|
|
|
|
/*if transfer must be terminated disable the channel*/
|
|
|
|
if (flags & SPI_XFER_END) {
|
|
|
|
chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_write_chconf(priv, chconf);
|
2011-03-02 05:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
static int _spi_xfer(struct omap3_spi_priv *priv, unsigned int bitlen,
|
|
|
|
const void *dout, void *din, unsigned long flags)
|
2010-12-11 16:01:00 +00:00
|
|
|
{
|
|
|
|
unsigned int len;
|
|
|
|
int ret = -1;
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
if (priv->wordlen < 4 || priv->wordlen > 32) {
|
|
|
|
printf("omap3_spi: invalid wordlen %d\n", priv->wordlen);
|
2013-10-16 14:23:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
if (bitlen % priv->wordlen)
|
2010-12-11 16:01:00 +00:00
|
|
|
return -1;
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
len = bitlen / priv->wordlen;
|
2010-12-11 16:01:00 +00:00
|
|
|
|
|
|
|
if (bitlen == 0) { /* only change CS */
|
2016-03-14 17:11:24 +00:00
|
|
|
int chconf = readl(&priv->regs->channel[priv->cs].chconf);
|
2010-12-11 16:01:00 +00:00
|
|
|
|
|
|
|
if (flags & SPI_XFER_BEGIN) {
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
|
2010-12-11 16:01:00 +00:00
|
|
|
chconf |= OMAP3_MCSPI_CHCONF_FORCE;
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_write_chconf(priv, chconf);
|
2010-12-11 16:01:00 +00:00
|
|
|
}
|
|
|
|
if (flags & SPI_XFER_END) {
|
|
|
|
chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
|
2016-03-14 17:11:24 +00:00
|
|
|
omap3_spi_write_chconf(priv, chconf);
|
|
|
|
omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
|
2010-12-11 16:01:00 +00:00
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
2011-03-02 05:13:22 +00:00
|
|
|
if (dout != NULL && din != NULL)
|
2016-03-14 17:11:24 +00:00
|
|
|
ret = omap3_spi_txrx(priv, len, dout, din, flags);
|
2011-03-02 05:13:22 +00:00
|
|
|
else if (dout != NULL)
|
2016-03-14 17:11:24 +00:00
|
|
|
ret = omap3_spi_write(priv, len, dout, flags);
|
2011-03-02 05:13:22 +00:00
|
|
|
else if (din != NULL)
|
2016-03-14 17:11:24 +00:00
|
|
|
ret = omap3_spi_read(priv, len, din, flags);
|
2010-12-11 16:01:00 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
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 */
|
2019-02-18 23:04:29 +00:00
|
|
|
confr = readl(&priv->regs->channel[priv->cs].chconf);
|
2016-03-14 17:11:24 +00:00
|
|
|
|
|
|
|
/* 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, ®s->sysconfig);
|
|
|
|
do {
|
|
|
|
tmp = readl(®s->sysstatus);
|
|
|
|
} while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE));
|
|
|
|
|
|
|
|
writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE |
|
|
|
|
OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP |
|
|
|
|
OMAP3_MCSPI_SYSCONFIG_SMARTIDLE, ®s->sysconfig);
|
|
|
|
|
|
|
|
writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, ®s->wakeupenable);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _omap3_spi_claim_bus(struct omap3_spi_priv *priv)
|
|
|
|
{
|
|
|
|
unsigned int conf;
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int omap3_spi_claim_bus(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct udevice *bus = dev->parent;
|
|
|
|
struct omap3_spi_priv *priv = dev_get_priv(bus);
|
2020-12-03 23:55:23 +00:00
|
|
|
struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
|
2016-03-14 17:11:24 +00:00
|
|
|
|
|
|
|
priv->cs = slave_plat->cs;
|
2018-06-02 06:06:47 +00:00
|
|
|
priv->freq = slave_plat->max_hz;
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
_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);
|
|
|
|
|
2018-06-26 14:08:39 +00:00
|
|
|
writel(OMAP3_MCSPI_MODULCTRL_MS, &priv->regs->modulctrl);
|
2016-03-14 17:11:24 +00:00
|
|
|
|
|
|
|
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);
|
2020-12-03 23:55:23 +00:00
|
|
|
struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
|
2016-03-14 17:11:24 +00:00
|
|
|
|
|
|
|
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);
|
2020-12-03 23:55:20 +00:00
|
|
|
struct omap3_spi_plat *plat = dev_get_plat(dev);
|
2016-03-14 17:11:24 +00:00
|
|
|
|
2020-09-14 06:41:16 +00:00
|
|
|
priv->regs = plat->regs;
|
|
|
|
priv->pin_dir = plat->pin_dir;
|
2016-03-14 17:11:24 +00:00
|
|
|
priv->wordlen = SPI_DEFAULT_WORDLEN;
|
2018-06-26 14:08:39 +00:00
|
|
|
|
|
|
|
spi_reset(priv->regs);
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-03-14 17:37:31 +00:00
|
|
|
static int omap3_spi_set_speed(struct udevice *dev, unsigned int speed)
|
2016-03-14 17:11:24 +00:00
|
|
|
{
|
2018-03-07 05:00:45 +00:00
|
|
|
|
2018-06-02 06:06:48 +00:00
|
|
|
struct omap3_spi_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
priv->freq = speed;
|
2018-03-07 05:00:45 +00:00
|
|
|
_omap3_spi_set_speed(priv);
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-14 17:37:31 +00:00
|
|
|
static int omap3_spi_set_mode(struct udevice *dev, uint mode)
|
2016-03-14 17:11:24 +00:00
|
|
|
{
|
2018-06-02 06:06:48 +00:00
|
|
|
struct omap3_spi_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
priv->mode = mode;
|
2018-03-07 05:00:45 +00:00
|
|
|
|
|
|
|
_omap3_spi_set_mode(priv);
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
|
2020-09-14 06:41:16 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
|
2016-05-19 07:11:58 +00:00
|
|
|
static struct omap2_mcspi_platform_config omap2_pdata = {
|
|
|
|
.regs_offset = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct omap2_mcspi_platform_config omap4_pdata = {
|
|
|
|
.regs_offset = OMAP4_MCSPI_REG_OFFSET,
|
|
|
|
};
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int omap3_spi_of_to_plat(struct udevice *dev)
|
2020-09-14 06:41:16 +00:00
|
|
|
{
|
|
|
|
struct omap2_mcspi_platform_config *data =
|
|
|
|
(struct omap2_mcspi_platform_config *)dev_get_driver_data(dev);
|
2020-12-03 23:55:20 +00:00
|
|
|
struct omap3_spi_plat *plat = dev_get_plat(dev);
|
2020-09-14 06:41:16 +00:00
|
|
|
|
|
|
|
plat->regs = (struct mcspi *)(dev_read_addr(dev) + data->regs_offset);
|
|
|
|
|
|
|
|
if (dev_read_bool(dev, "ti,pindir-d0-out-d1-in"))
|
|
|
|
plat->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
|
|
|
|
else
|
|
|
|
plat->pin_dir = MCSPI_PINDIR_D0_IN_D1_OUT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-14 17:11:24 +00:00
|
|
|
static const struct udevice_id omap3_spi_ids[] = {
|
2016-05-19 07:11:58 +00:00
|
|
|
{ .compatible = "ti,omap2-mcspi", .data = (ulong)&omap2_pdata },
|
|
|
|
{ .compatible = "ti,omap4-mcspi", .data = (ulong)&omap4_pdata },
|
2016-03-14 17:11:24 +00:00
|
|
|
{ }
|
|
|
|
};
|
2020-09-14 06:41:16 +00:00
|
|
|
#endif
|
2016-03-14 17:11:24 +00:00
|
|
|
U_BOOT_DRIVER(omap3_spi) = {
|
|
|
|
.name = "omap3_spi",
|
|
|
|
.id = UCLASS_SPI,
|
2020-09-14 06:41:16 +00:00
|
|
|
.flags = DM_FLAG_PRE_RELOC,
|
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
|
2016-03-14 17:11:24 +00:00
|
|
|
.of_match = omap3_spi_ids,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = omap3_spi_of_to_plat,
|
2020-12-03 23:55:18 +00:00
|
|
|
.plat_auto = sizeof(struct omap3_spi_plat),
|
2020-09-14 06:41:16 +00:00
|
|
|
#endif
|
2016-03-14 17:11:24 +00:00
|
|
|
.probe = omap3_spi_probe,
|
|
|
|
.ops = &omap3_spi_ops,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct omap3_spi_priv),
|
2016-03-14 17:11:24 +00:00
|
|
|
};
|