blob: ae08531f1ed22d7fd3b5dee5a878f70efcdb7281 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dirk Behme78cd9ac2010-12-11 11:01:00 -05002/*
Jagan Tekiea172422016-03-14 22:41:24 +05303 * Copyright (C) 2016 Jagan Teki <jteki@openedev.com>
4 * Christophe Ricard <christophe.ricard@gmail.com>
5 *
Dirk Behme78cd9ac2010-12-11 11:01:00 -05006 * Copyright (C) 2010 Dirk Behme <dirk.behme@googlemail.com>
7 *
8 * Driver for McSPI controller on OMAP3. Based on davinci_spi.c
9 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
10 *
11 * Copyright (C) 2007 Atmel Corporation
12 *
13 * Parts taken from linux/drivers/spi/omap2_mcspi.c
14 * Copyright (C) 2005, 2006 Nokia Corporation
15 *
16 * Modified by Ruslan Araslanov <ruslan.araslanov@vitecmm.com>
Dirk Behme78cd9ac2010-12-11 11:01:00 -050017 */
18
19#include <common.h>
Jagan Tekiea172422016-03-14 22:41:24 +053020#include <dm.h>
Dirk Behme78cd9ac2010-12-11 11:01:00 -050021#include <spi.h>
22#include <malloc.h>
23#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Jagan Tekic2cca6c2016-03-01 15:16:20 +010025
Jagan Tekiea172422016-03-14 22:41:24 +053026DECLARE_GLOBAL_DATA_PTR;
27
Jagan Tekic2cca6c2016-03-01 15:16:20 +010028#if defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
29#define OMAP3_MCSPI1_BASE 0x48030100
30#define OMAP3_MCSPI2_BASE 0x481A0100
31#else
32#define OMAP3_MCSPI1_BASE 0x48098000
33#define OMAP3_MCSPI2_BASE 0x4809A000
34#define OMAP3_MCSPI3_BASE 0x480B8000
35#define OMAP3_MCSPI4_BASE 0x480BA000
36#endif
37
Martin Hejnfeltd74ea962016-05-19 09:11:58 +020038#define OMAP4_MCSPI_REG_OFFSET 0x100
39
40struct omap2_mcspi_platform_config {
41 unsigned int regs_offset;
42};
43
Jagan Tekic2cca6c2016-03-01 15:16:20 +010044/* per-register bitmasks */
45#define OMAP3_MCSPI_SYSCONFIG_SMARTIDLE (2 << 3)
46#define OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP BIT(2)
47#define OMAP3_MCSPI_SYSCONFIG_AUTOIDLE BIT(0)
48#define OMAP3_MCSPI_SYSCONFIG_SOFTRESET BIT(1)
49
50#define OMAP3_MCSPI_SYSSTATUS_RESETDONE BIT(0)
51
52#define OMAP3_MCSPI_MODULCTRL_SINGLE BIT(0)
53#define OMAP3_MCSPI_MODULCTRL_MS BIT(2)
54#define OMAP3_MCSPI_MODULCTRL_STEST BIT(3)
55
56#define OMAP3_MCSPI_CHCONF_PHA BIT(0)
57#define OMAP3_MCSPI_CHCONF_POL BIT(1)
58#define OMAP3_MCSPI_CHCONF_CLKD_MASK GENMASK(5, 2)
59#define OMAP3_MCSPI_CHCONF_EPOL BIT(6)
60#define OMAP3_MCSPI_CHCONF_WL_MASK GENMASK(11, 7)
61#define OMAP3_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
62#define OMAP3_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
63#define OMAP3_MCSPI_CHCONF_TRM_MASK GENMASK(13, 12)
64#define OMAP3_MCSPI_CHCONF_DMAW BIT(14)
65#define OMAP3_MCSPI_CHCONF_DMAR BIT(15)
66#define OMAP3_MCSPI_CHCONF_DPE0 BIT(16)
67#define OMAP3_MCSPI_CHCONF_DPE1 BIT(17)
68#define OMAP3_MCSPI_CHCONF_IS BIT(18)
69#define OMAP3_MCSPI_CHCONF_TURBO BIT(19)
70#define OMAP3_MCSPI_CHCONF_FORCE BIT(20)
71
72#define OMAP3_MCSPI_CHSTAT_RXS BIT(0)
73#define OMAP3_MCSPI_CHSTAT_TXS BIT(1)
74#define OMAP3_MCSPI_CHSTAT_EOT BIT(2)
75
76#define OMAP3_MCSPI_CHCTRL_EN BIT(0)
77#define OMAP3_MCSPI_CHCTRL_DIS (0 << 0)
78
79#define OMAP3_MCSPI_WAKEUPENABLE_WKEN BIT(0)
Jagan Tekiea172422016-03-14 22:41:24 +053080#define MCSPI_PINDIR_D0_IN_D1_OUT 0
81#define MCSPI_PINDIR_D0_OUT_D1_IN 1
Jagan Tekic2cca6c2016-03-01 15:16:20 +010082
83#define OMAP3_MCSPI_MAX_FREQ 48000000
84#define SPI_WAIT_TIMEOUT 10
85
86/* OMAP3 McSPI registers */
87struct mcspi_channel {
88 unsigned int chconf; /* 0x2C, 0x40, 0x54, 0x68 */
89 unsigned int chstat; /* 0x30, 0x44, 0x58, 0x6C */
90 unsigned int chctrl; /* 0x34, 0x48, 0x5C, 0x70 */
91 unsigned int tx; /* 0x38, 0x4C, 0x60, 0x74 */
92 unsigned int rx; /* 0x3C, 0x50, 0x64, 0x78 */
93};
94
95struct mcspi {
96 unsigned char res1[0x10];
97 unsigned int sysconfig; /* 0x10 */
98 unsigned int sysstatus; /* 0x14 */
99 unsigned int irqstatus; /* 0x18 */
100 unsigned int irqenable; /* 0x1C */
101 unsigned int wakeupenable; /* 0x20 */
102 unsigned int syst; /* 0x24 */
103 unsigned int modulctrl; /* 0x28 */
104 struct mcspi_channel channel[4];
105 /* channel0: 0x2C - 0x3C, bus 0 & 1 & 2 & 3 */
106 /* channel1: 0x40 - 0x50, bus 0 & 1 */
107 /* channel2: 0x54 - 0x64, bus 0 & 1 */
108 /* channel3: 0x68 - 0x78, bus 0 */
109};
110
Jagan Tekiea172422016-03-14 22:41:24 +0530111struct omap3_spi_priv {
Lukasz Majewski76f442982020-06-04 23:11:53 +0800112#if !CONFIG_IS_ENABLED(DM_SPI)
Jagan Tekie7ac5702016-03-15 23:56:33 +0530113 struct spi_slave slave;
114#endif
Jagan Tekic2cca6c2016-03-01 15:16:20 +0100115 struct mcspi *regs;
Jagan Tekiea172422016-03-14 22:41:24 +0530116 unsigned int cs;
Jagan Tekic2cca6c2016-03-01 15:16:20 +0100117 unsigned int freq;
118 unsigned int mode;
Jagan Tekiea172422016-03-14 22:41:24 +0530119 unsigned int wordlen;
120 unsigned int pin_dir:1;
Jagan Tekic2cca6c2016-03-01 15:16:20 +0100121};
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500122
Jagan Tekiea172422016-03-14 22:41:24 +0530123static void omap3_spi_write_chconf(struct omap3_spi_priv *priv, int val)
Jagan Tekic2cca6c2016-03-01 15:16:20 +0100124{
Jagan Tekiea172422016-03-14 22:41:24 +0530125 writel(val, &priv->regs->channel[priv->cs].chconf);
ajoy260ab202012-11-17 21:10:15 +0000126 /* Flash post writes to make immediate effect */
Jagan Tekiea172422016-03-14 22:41:24 +0530127 readl(&priv->regs->channel[priv->cs].chconf);
ajoy260ab202012-11-17 21:10:15 +0000128}
129
Jagan Tekiea172422016-03-14 22:41:24 +0530130static void omap3_spi_set_enable(struct omap3_spi_priv *priv, int enable)
ajoy260ab202012-11-17 21:10:15 +0000131{
Jagan Tekiea172422016-03-14 22:41:24 +0530132 writel(enable, &priv->regs->channel[priv->cs].chctrl);
Wolfgang Denkec7fbf52013-10-04 17:43:24 +0200133 /* Flash post writes to make immediate effect */
Jagan Tekiea172422016-03-14 22:41:24 +0530134 readl(&priv->regs->channel[priv->cs].chctrl);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500135}
136
Jagan Tekiea172422016-03-14 22:41:24 +0530137static int omap3_spi_write(struct omap3_spi_priv *priv, unsigned int len,
Jagan Teki40a55df2016-03-01 15:16:21 +0100138 const void *txp, unsigned long flags)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500139{
David Duecka11b2a62015-04-01 14:20:24 +0200140 ulong start;
Jagan Tekiea172422016-03-14 22:41:24 +0530141 int i, chconf;
142
143 chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500144
ajoy260ab202012-11-17 21:10:15 +0000145 /* Enable the channel */
Jagan Tekiea172422016-03-14 22:41:24 +0530146 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500147
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300148 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
Jagan Tekiea172422016-03-14 22:41:24 +0530149 chconf |= (priv->wordlen - 1) << 7;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500150 chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
151 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530152 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500153
154 for (i = 0; i < len; i++) {
155 /* wait till TX register is empty (TXS == 1) */
David Duecka11b2a62015-04-01 14:20:24 +0200156 start = get_timer(0);
Jagan Tekiea172422016-03-14 22:41:24 +0530157 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500158 OMAP3_MCSPI_CHSTAT_TXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200159 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500160 printf("SPI TXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530161 readl(&priv->regs->channel[priv->cs].chstat));
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500162 return -1;
163 }
164 }
165 /* Write the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530166 unsigned int *tx = &priv->regs->channel[priv->cs].tx;
167 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300168 writel(((u32 *)txp)[i], tx);
Jagan Tekiea172422016-03-14 22:41:24 +0530169 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300170 writel(((u16 *)txp)[i], tx);
171 else
172 writel(((u8 *)txp)[i], tx);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500173 }
174
Wolfgang Denkec7fbf52013-10-04 17:43:24 +0200175 /* wait to finish of transfer */
Jagan Tekiea172422016-03-14 22:41:24 +0530176 while ((readl(&priv->regs->channel[priv->cs].chstat) &
177 (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS)) !=
178 (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS))
179 ;
ajoy260ab202012-11-17 21:10:15 +0000180
181 /* Disable the channel otherwise the next immediate RX will get affected */
Jagan Tekiea172422016-03-14 22:41:24 +0530182 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
ajoy260ab202012-11-17 21:10:15 +0000183
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500184 if (flags & SPI_XFER_END) {
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500185
186 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530187 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500188 }
189 return 0;
190}
191
Jagan Tekiea172422016-03-14 22:41:24 +0530192static int omap3_spi_read(struct omap3_spi_priv *priv, unsigned int len,
Jagan Teki40a55df2016-03-01 15:16:21 +0100193 void *rxp, unsigned long flags)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500194{
Jagan Tekiea172422016-03-14 22:41:24 +0530195 int i, chconf;
David Duecka11b2a62015-04-01 14:20:24 +0200196 ulong start;
Jagan Tekiea172422016-03-14 22:41:24 +0530197
198 chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500199
ajoy260ab202012-11-17 21:10:15 +0000200 /* Enable the channel */
Jagan Tekiea172422016-03-14 22:41:24 +0530201 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500202
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300203 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
Jagan Tekiea172422016-03-14 22:41:24 +0530204 chconf |= (priv->wordlen - 1) << 7;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500205 chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
206 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530207 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500208
Jagan Tekiea172422016-03-14 22:41:24 +0530209 writel(0, &priv->regs->channel[priv->cs].tx);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500210
211 for (i = 0; i < len; i++) {
David Duecka11b2a62015-04-01 14:20:24 +0200212 start = get_timer(0);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500213 /* Wait till RX register contains data (RXS == 1) */
Jagan Tekiea172422016-03-14 22:41:24 +0530214 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500215 OMAP3_MCSPI_CHSTAT_RXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200216 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500217 printf("SPI RXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530218 readl(&priv->regs->channel[priv->cs].chstat));
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500219 return -1;
220 }
221 }
ajoy260ab202012-11-17 21:10:15 +0000222
223 /* Disable the channel to prevent furher receiving */
Jagan Tekiea172422016-03-14 22:41:24 +0530224 if (i == (len - 1))
225 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
ajoy260ab202012-11-17 21:10:15 +0000226
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500227 /* Read the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530228 unsigned int *rx = &priv->regs->channel[priv->cs].rx;
229 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300230 ((u32 *)rxp)[i] = readl(rx);
Jagan Tekiea172422016-03-14 22:41:24 +0530231 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300232 ((u16 *)rxp)[i] = (u16)readl(rx);
233 else
234 ((u8 *)rxp)[i] = (u8)readl(rx);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500235 }
236
237 if (flags & SPI_XFER_END) {
238 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530239 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500240 }
241
242 return 0;
243}
244
jacopo mondife8793e2011-03-02 05:13:22 +0000245/*McSPI Transmit Receive Mode*/
Jagan Tekiea172422016-03-14 22:41:24 +0530246static int omap3_spi_txrx(struct omap3_spi_priv *priv, unsigned int len,
Jagan Teki40a55df2016-03-01 15:16:21 +0100247 const void *txp, void *rxp, unsigned long flags)
jacopo mondife8793e2011-03-02 05:13:22 +0000248{
David Duecka11b2a62015-04-01 14:20:24 +0200249 ulong start;
Jagan Tekiea172422016-03-14 22:41:24 +0530250 int chconf, i = 0;
251
252 chconf = readl(&priv->regs->channel[priv->cs].chconf);
jacopo mondife8793e2011-03-02 05:13:22 +0000253
254 /*Enable SPI channel*/
Jagan Tekiea172422016-03-14 22:41:24 +0530255 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
jacopo mondife8793e2011-03-02 05:13:22 +0000256
257 /*set TRANSMIT-RECEIVE Mode*/
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300258 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
Jagan Tekiea172422016-03-14 22:41:24 +0530259 chconf |= (priv->wordlen - 1) << 7;
jacopo mondife8793e2011-03-02 05:13:22 +0000260 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530261 omap3_spi_write_chconf(priv, chconf);
jacopo mondife8793e2011-03-02 05:13:22 +0000262
263 /*Shift in and out 1 byte at time*/
264 for (i=0; i < len; i++){
265 /* Write: wait for TX empty (TXS == 1)*/
David Duecka11b2a62015-04-01 14:20:24 +0200266 start = get_timer(0);
Jagan Tekiea172422016-03-14 22:41:24 +0530267 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
jacopo mondife8793e2011-03-02 05:13:22 +0000268 OMAP3_MCSPI_CHSTAT_TXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200269 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
jacopo mondife8793e2011-03-02 05:13:22 +0000270 printf("SPI TXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530271 readl(&priv->regs->channel[priv->cs].chstat));
jacopo mondife8793e2011-03-02 05:13:22 +0000272 return -1;
273 }
274 }
275 /* Write the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530276 unsigned int *tx = &priv->regs->channel[priv->cs].tx;
277 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300278 writel(((u32 *)txp)[i], tx);
Jagan Tekiea172422016-03-14 22:41:24 +0530279 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300280 writel(((u16 *)txp)[i], tx);
281 else
282 writel(((u8 *)txp)[i], tx);
jacopo mondife8793e2011-03-02 05:13:22 +0000283
284 /*Read: wait for RX containing data (RXS == 1)*/
David Duecka11b2a62015-04-01 14:20:24 +0200285 start = get_timer(0);
Jagan Tekiea172422016-03-14 22:41:24 +0530286 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
jacopo mondife8793e2011-03-02 05:13:22 +0000287 OMAP3_MCSPI_CHSTAT_RXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200288 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
jacopo mondife8793e2011-03-02 05:13:22 +0000289 printf("SPI RXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530290 readl(&priv->regs->channel[priv->cs].chstat));
jacopo mondife8793e2011-03-02 05:13:22 +0000291 return -1;
292 }
293 }
294 /* Read the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530295 unsigned int *rx = &priv->regs->channel[priv->cs].rx;
296 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300297 ((u32 *)rxp)[i] = readl(rx);
Jagan Tekiea172422016-03-14 22:41:24 +0530298 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300299 ((u16 *)rxp)[i] = (u16)readl(rx);
300 else
301 ((u8 *)rxp)[i] = (u8)readl(rx);
jacopo mondife8793e2011-03-02 05:13:22 +0000302 }
ajoy260ab202012-11-17 21:10:15 +0000303 /* Disable the channel */
Jagan Tekiea172422016-03-14 22:41:24 +0530304 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
jacopo mondife8793e2011-03-02 05:13:22 +0000305
306 /*if transfer must be terminated disable the channel*/
307 if (flags & SPI_XFER_END) {
308 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530309 omap3_spi_write_chconf(priv, chconf);
jacopo mondife8793e2011-03-02 05:13:22 +0000310 }
311
312 return 0;
313}
314
Jagan Tekiea172422016-03-14 22:41:24 +0530315static int _spi_xfer(struct omap3_spi_priv *priv, unsigned int bitlen,
316 const void *dout, void *din, unsigned long flags)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500317{
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500318 unsigned int len;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500319 int ret = -1;
320
Jagan Tekiea172422016-03-14 22:41:24 +0530321 if (priv->wordlen < 4 || priv->wordlen > 32) {
322 printf("omap3_spi: invalid wordlen %d\n", priv->wordlen);
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300323 return -1;
324 }
325
Jagan Tekiea172422016-03-14 22:41:24 +0530326 if (bitlen % priv->wordlen)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500327 return -1;
328
Jagan Tekiea172422016-03-14 22:41:24 +0530329 len = bitlen / priv->wordlen;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500330
331 if (bitlen == 0) { /* only change CS */
Jagan Tekiea172422016-03-14 22:41:24 +0530332 int chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500333
334 if (flags & SPI_XFER_BEGIN) {
Jagan Tekiea172422016-03-14 22:41:24 +0530335 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500336 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530337 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500338 }
339 if (flags & SPI_XFER_END) {
340 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530341 omap3_spi_write_chconf(priv, chconf);
342 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500343 }
344 ret = 0;
345 } else {
jacopo mondife8793e2011-03-02 05:13:22 +0000346 if (dout != NULL && din != NULL)
Jagan Tekiea172422016-03-14 22:41:24 +0530347 ret = omap3_spi_txrx(priv, len, dout, din, flags);
jacopo mondife8793e2011-03-02 05:13:22 +0000348 else if (dout != NULL)
Jagan Tekiea172422016-03-14 22:41:24 +0530349 ret = omap3_spi_write(priv, len, dout, flags);
jacopo mondife8793e2011-03-02 05:13:22 +0000350 else if (din != NULL)
Jagan Tekiea172422016-03-14 22:41:24 +0530351 ret = omap3_spi_read(priv, len, din, flags);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500352 }
353 return ret;
354}
355
Jagan Tekiea172422016-03-14 22:41:24 +0530356static void _omap3_spi_set_speed(struct omap3_spi_priv *priv)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500357{
Jagan Tekiea172422016-03-14 22:41:24 +0530358 uint32_t confr, div = 0;
359
360 confr = readl(&priv->regs->channel[priv->cs].chconf);
361
362 /* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */
363 if (priv->freq) {
364 while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div))
365 > priv->freq)
366 div++;
367 } else {
368 div = 0xC;
369 }
370
371 /* set clock divisor */
372 confr &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
373 confr |= div << 2;
374
375 omap3_spi_write_chconf(priv, confr);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500376}
377
Jagan Tekiea172422016-03-14 22:41:24 +0530378static void _omap3_spi_set_mode(struct omap3_spi_priv *priv)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500379{
Jagan Tekiea172422016-03-14 22:41:24 +0530380 uint32_t confr;
381
382 confr = readl(&priv->regs->channel[priv->cs].chconf);
383
384 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
385 * REVISIT: this controller could support SPI_3WIRE mode.
386 */
387 if (priv->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
388 confr &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
389 confr |= OMAP3_MCSPI_CHCONF_DPE0;
390 } else {
391 confr &= ~OMAP3_MCSPI_CHCONF_DPE0;
392 confr |= OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1;
393 }
394
395 /* set SPI mode 0..3 */
396 confr &= ~(OMAP3_MCSPI_CHCONF_POL | OMAP3_MCSPI_CHCONF_PHA);
397 if (priv->mode & SPI_CPHA)
398 confr |= OMAP3_MCSPI_CHCONF_PHA;
399 if (priv->mode & SPI_CPOL)
400 confr |= OMAP3_MCSPI_CHCONF_POL;
401
402 /* set chipselect polarity; manage with FORCE */
403 if (!(priv->mode & SPI_CS_HIGH))
404 confr |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
405 else
406 confr &= ~OMAP3_MCSPI_CHCONF_EPOL;
407
408 /* Transmit & receive mode */
409 confr &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
410
411 omap3_spi_write_chconf(priv, confr);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500412}
413
Jagan Tekiea172422016-03-14 22:41:24 +0530414static void _omap3_spi_set_wordlen(struct omap3_spi_priv *priv)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500415{
Jagan Tekiea172422016-03-14 22:41:24 +0530416 unsigned int confr;
417
418 /* McSPI individual channel configuration */
David Rivshin7aacc912019-02-18 18:04:29 -0500419 confr = readl(&priv->regs->channel[priv->cs].chconf);
Jagan Tekiea172422016-03-14 22:41:24 +0530420
421 /* wordlength */
422 confr &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
423 confr |= (priv->wordlen - 1) << 7;
424
425 omap3_spi_write_chconf(priv, confr);
426}
427
428static void spi_reset(struct mcspi *regs)
429{
430 unsigned int tmp;
431
432 writel(OMAP3_MCSPI_SYSCONFIG_SOFTRESET, &regs->sysconfig);
433 do {
434 tmp = readl(&regs->sysstatus);
435 } while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE));
436
437 writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE |
438 OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP |
439 OMAP3_MCSPI_SYSCONFIG_SMARTIDLE, &regs->sysconfig);
440
441 writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, &regs->wakeupenable);
442}
443
444static void _omap3_spi_claim_bus(struct omap3_spi_priv *priv)
445{
446 unsigned int conf;
Jagan Tekiea172422016-03-14 22:41:24 +0530447 /*
448 * setup when switching from (reset default) slave mode
449 * to single-channel master mode
450 */
451 conf = readl(&priv->regs->modulctrl);
452 conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS);
453 conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;
454
455 writel(conf, &priv->regs->modulctrl);
Jagan Tekiea172422016-03-14 22:41:24 +0530456}
457
Lukasz Majewski76f442982020-06-04 23:11:53 +0800458#if !CONFIG_IS_ENABLED(DM_SPI)
Jagan Tekiea172422016-03-14 22:41:24 +0530459
Jagan Tekie7ac5702016-03-15 23:56:33 +0530460static inline struct omap3_spi_priv *to_omap3_spi(struct spi_slave *slave)
Jagan Tekiea172422016-03-14 22:41:24 +0530461{
Jagan Tekie7ac5702016-03-15 23:56:33 +0530462 return container_of(slave, struct omap3_spi_priv, slave);
Jagan Tekiea172422016-03-14 22:41:24 +0530463}
464
Jagan Tekiea172422016-03-14 22:41:24 +0530465void spi_free_slave(struct spi_slave *slave)
466{
Jagan Tekie7ac5702016-03-15 23:56:33 +0530467 struct omap3_spi_priv *priv = to_omap3_spi(slave);
Jagan Tekiea172422016-03-14 22:41:24 +0530468
Jagan Tekie7ac5702016-03-15 23:56:33 +0530469 free(priv);
Jagan Tekiea172422016-03-14 22:41:24 +0530470}
471
472int spi_claim_bus(struct spi_slave *slave)
473{
Jagan Tekie7ac5702016-03-15 23:56:33 +0530474 struct omap3_spi_priv *priv = to_omap3_spi(slave);
475
Hannes Schmelzer202f9a42018-06-26 16:08:39 +0200476 spi_reset(priv->regs);
477
Jagan Tekiea172422016-03-14 22:41:24 +0530478 _omap3_spi_claim_bus(priv);
479 _omap3_spi_set_wordlen(priv);
480 _omap3_spi_set_mode(priv);
481 _omap3_spi_set_speed(priv);
482
483 return 0;
484}
485
486void spi_release_bus(struct spi_slave *slave)
487{
Jagan Tekie7ac5702016-03-15 23:56:33 +0530488 struct omap3_spi_priv *priv = to_omap3_spi(slave);
489
Hannes Schmelzer202f9a42018-06-26 16:08:39 +0200490 writel(OMAP3_MCSPI_MODULCTRL_MS, &priv->regs->modulctrl);
Jagan Tekiea172422016-03-14 22:41:24 +0530491}
492
493struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
494 unsigned int max_hz, unsigned int mode)
495{
Jagan Tekie7ac5702016-03-15 23:56:33 +0530496 struct omap3_spi_priv *priv;
Jagan Tekiea172422016-03-14 22:41:24 +0530497 struct mcspi *regs;
498
499 /*
500 * OMAP3 McSPI (MultiChannel SPI) has 4 busses (modules)
501 * with different number of chip selects (CS, channels):
502 * McSPI1 has 4 CS (bus 0, cs 0 - 3)
503 * McSPI2 has 2 CS (bus 1, cs 0 - 1)
504 * McSPI3 has 2 CS (bus 2, cs 0 - 1)
505 * McSPI4 has 1 CS (bus 3, cs 0)
506 */
507
508 switch (bus) {
509 case 0:
510 regs = (struct mcspi *)OMAP3_MCSPI1_BASE;
511 break;
512#ifdef OMAP3_MCSPI2_BASE
513 case 1:
514 regs = (struct mcspi *)OMAP3_MCSPI2_BASE;
515 break;
516#endif
517#ifdef OMAP3_MCSPI3_BASE
518 case 2:
519 regs = (struct mcspi *)OMAP3_MCSPI3_BASE;
520 break;
521#endif
522#ifdef OMAP3_MCSPI4_BASE
523 case 3:
524 regs = (struct mcspi *)OMAP3_MCSPI4_BASE;
525 break;
526#endif
527 default:
528 printf("SPI error: unsupported bus %i. Supported busses 0 - 3\n", bus);
529 return NULL;
530 }
531
532 if (((bus == 0) && (cs > 3)) ||
533 ((bus == 1) && (cs > 1)) ||
534 ((bus == 2) && (cs > 1)) ||
535 ((bus == 3) && (cs > 0))) {
536 printf("SPI error: unsupported chip select %i on bus %i\n", cs, bus);
537 return NULL;
538 }
539
540 if (max_hz > OMAP3_MCSPI_MAX_FREQ) {
Heinrich Schuchardt5f066542017-11-12 21:02:52 +0100541 printf("SPI error: unsupported frequency %i Hz. Max frequency is 48 MHz\n",
542 max_hz);
Jagan Tekiea172422016-03-14 22:41:24 +0530543 return NULL;
544 }
545
546 if (mode > SPI_MODE_3) {
547 printf("SPI error: unsupported SPI mode %i\n", mode);
548 return NULL;
549 }
550
Jagan Tekie7ac5702016-03-15 23:56:33 +0530551 priv = spi_alloc_slave(struct omap3_spi_priv, bus, cs);
552 if (!priv) {
Jagan Tekiea172422016-03-14 22:41:24 +0530553 printf("SPI error: malloc of SPI structure failed\n");
554 return NULL;
555 }
556
Jagan Tekiea172422016-03-14 22:41:24 +0530557 priv->regs = regs;
558 priv->cs = cs;
559 priv->freq = max_hz;
560 priv->mode = mode;
Jagan Tekie7ac5702016-03-15 23:56:33 +0530561 priv->wordlen = priv->slave.wordlen;
Tom Rinia285ea62017-05-12 22:33:23 -0400562#if 0
563 /* Please migrate to DM_SPI support for this feature. */
Jagan Tekiea172422016-03-14 22:41:24 +0530564 priv->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
565#endif
566
Jagan Tekie7ac5702016-03-15 23:56:33 +0530567 return &priv->slave;
Jagan Tekiea172422016-03-14 22:41:24 +0530568}
569
570int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
571 const void *dout, void *din, unsigned long flags)
Jagan Tekie7ac5702016-03-15 23:56:33 +0530572{
573 struct omap3_spi_priv *priv = to_omap3_spi(slave);
574
575 return _spi_xfer(priv, bitlen, dout, din, flags);
576}
Jagan Tekiea172422016-03-14 22:41:24 +0530577
578#else
579
580static int omap3_spi_claim_bus(struct udevice *dev)
581{
582 struct udevice *bus = dev->parent;
583 struct omap3_spi_priv *priv = dev_get_priv(bus);
584 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
585
586 priv->cs = slave_plat->cs;
Hannes Schmelzer9e778802018-06-02 08:06:47 +0200587 priv->freq = slave_plat->max_hz;
588
Jagan Tekiea172422016-03-14 22:41:24 +0530589 _omap3_spi_claim_bus(priv);
590
591 return 0;
592}
593
594static int omap3_spi_release_bus(struct udevice *dev)
595{
596 struct udevice *bus = dev->parent;
597 struct omap3_spi_priv *priv = dev_get_priv(bus);
598
Hannes Schmelzer202f9a42018-06-26 16:08:39 +0200599 writel(OMAP3_MCSPI_MODULCTRL_MS, &priv->regs->modulctrl);
Jagan Tekiea172422016-03-14 22:41:24 +0530600
601 return 0;
602}
603
604static int omap3_spi_set_wordlen(struct udevice *dev, unsigned int wordlen)
605{
606 struct udevice *bus = dev->parent;
607 struct omap3_spi_priv *priv = dev_get_priv(bus);
608 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
609
610 priv->cs = slave_plat->cs;
611 priv->wordlen = wordlen;
612 _omap3_spi_set_wordlen(priv);
613
614 return 0;
615}
616
617static int omap3_spi_probe(struct udevice *dev)
618{
619 struct omap3_spi_priv *priv = dev_get_priv(dev);
620 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700621 int node = dev_of_offset(dev);
Jagan Tekiea172422016-03-14 22:41:24 +0530622
Martin Hejnfeltd74ea962016-05-19 09:11:58 +0200623 struct omap2_mcspi_platform_config* data =
624 (struct omap2_mcspi_platform_config*)dev_get_driver_data(dev);
625
Simon Glassba1dea42017-05-17 17:18:05 -0600626 priv->regs = (struct mcspi *)(devfdt_get_addr(dev) + data->regs_offset);
Sjoerd Simons74ac81c2018-03-22 22:55:02 +0100627 if (fdtdec_get_bool(blob, node, "ti,pindir-d0-out-d1-in"))
628 priv->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
629 else
630 priv->pin_dir = MCSPI_PINDIR_D0_IN_D1_OUT;
Jagan Tekiea172422016-03-14 22:41:24 +0530631 priv->wordlen = SPI_DEFAULT_WORDLEN;
Hannes Schmelzer202f9a42018-06-26 16:08:39 +0200632
633 spi_reset(priv->regs);
634
Jagan Tekiea172422016-03-14 22:41:24 +0530635 return 0;
636}
637
638static int omap3_spi_xfer(struct udevice *dev, unsigned int bitlen,
639 const void *dout, void *din, unsigned long flags)
640{
641 struct udevice *bus = dev->parent;
642 struct omap3_spi_priv *priv = dev_get_priv(bus);
643
644 return _spi_xfer(priv, bitlen, dout, din, flags);
645}
646
Jagan Teki55865a92018-03-14 23:07:31 +0530647static int omap3_spi_set_speed(struct udevice *dev, unsigned int speed)
Jagan Tekiea172422016-03-14 22:41:24 +0530648{
Jagan Teki96a95ee2018-03-07 10:30:45 +0530649
Hannes Schmelzer7e4f8332018-06-02 08:06:48 +0200650 struct omap3_spi_priv *priv = dev_get_priv(dev);
651
652 priv->freq = speed;
Jagan Teki96a95ee2018-03-07 10:30:45 +0530653 _omap3_spi_set_speed(priv);
654
Jagan Tekiea172422016-03-14 22:41:24 +0530655 return 0;
656}
657
Jagan Teki55865a92018-03-14 23:07:31 +0530658static int omap3_spi_set_mode(struct udevice *dev, uint mode)
Jagan Tekiea172422016-03-14 22:41:24 +0530659{
Hannes Schmelzer7e4f8332018-06-02 08:06:48 +0200660 struct omap3_spi_priv *priv = dev_get_priv(dev);
661
662 priv->mode = mode;
Jagan Teki96a95ee2018-03-07 10:30:45 +0530663
Jagan Teki96a95ee2018-03-07 10:30:45 +0530664 _omap3_spi_set_mode(priv);
665
Jagan Tekiea172422016-03-14 22:41:24 +0530666 return 0;
667}
668
669static const struct dm_spi_ops omap3_spi_ops = {
670 .claim_bus = omap3_spi_claim_bus,
671 .release_bus = omap3_spi_release_bus,
672 .set_wordlen = omap3_spi_set_wordlen,
673 .xfer = omap3_spi_xfer,
674 .set_speed = omap3_spi_set_speed,
675 .set_mode = omap3_spi_set_mode,
676 /*
677 * cs_info is not needed, since we require all chip selects to be
678 * in the device tree explicitly
679 */
680};
681
Martin Hejnfeltd74ea962016-05-19 09:11:58 +0200682static struct omap2_mcspi_platform_config omap2_pdata = {
683 .regs_offset = 0,
684};
685
686static struct omap2_mcspi_platform_config omap4_pdata = {
687 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
688};
689
Jagan Tekiea172422016-03-14 22:41:24 +0530690static const struct udevice_id omap3_spi_ids[] = {
Martin Hejnfeltd74ea962016-05-19 09:11:58 +0200691 { .compatible = "ti,omap2-mcspi", .data = (ulong)&omap2_pdata },
692 { .compatible = "ti,omap4-mcspi", .data = (ulong)&omap4_pdata },
Jagan Tekiea172422016-03-14 22:41:24 +0530693 { }
694};
695
696U_BOOT_DRIVER(omap3_spi) = {
697 .name = "omap3_spi",
698 .id = UCLASS_SPI,
699 .of_match = omap3_spi_ids,
700 .probe = omap3_spi_probe,
701 .ops = &omap3_spi_ops,
702 .priv_auto_alloc_size = sizeof(struct omap3_spi_priv),
Jagan Tekiea172422016-03-14 22:41:24 +0530703};
704#endif