blob: 39e6813469d452c3742f36e6944402d5fb9ec8f2 [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
Martin Hejnfeltd74ea962016-05-19 09:11:58 +020028#define OMAP4_MCSPI_REG_OFFSET 0x100
29
30struct omap2_mcspi_platform_config {
31 unsigned int regs_offset;
32};
33
Jagan Tekic2cca6c2016-03-01 15:16:20 +010034/* per-register bitmasks */
35#define OMAP3_MCSPI_SYSCONFIG_SMARTIDLE (2 << 3)
36#define OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP BIT(2)
37#define OMAP3_MCSPI_SYSCONFIG_AUTOIDLE BIT(0)
38#define OMAP3_MCSPI_SYSCONFIG_SOFTRESET BIT(1)
39
40#define OMAP3_MCSPI_SYSSTATUS_RESETDONE BIT(0)
41
42#define OMAP3_MCSPI_MODULCTRL_SINGLE BIT(0)
43#define OMAP3_MCSPI_MODULCTRL_MS BIT(2)
44#define OMAP3_MCSPI_MODULCTRL_STEST BIT(3)
45
46#define OMAP3_MCSPI_CHCONF_PHA BIT(0)
47#define OMAP3_MCSPI_CHCONF_POL BIT(1)
48#define OMAP3_MCSPI_CHCONF_CLKD_MASK GENMASK(5, 2)
49#define OMAP3_MCSPI_CHCONF_EPOL BIT(6)
50#define OMAP3_MCSPI_CHCONF_WL_MASK GENMASK(11, 7)
51#define OMAP3_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
52#define OMAP3_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
53#define OMAP3_MCSPI_CHCONF_TRM_MASK GENMASK(13, 12)
54#define OMAP3_MCSPI_CHCONF_DMAW BIT(14)
55#define OMAP3_MCSPI_CHCONF_DMAR BIT(15)
56#define OMAP3_MCSPI_CHCONF_DPE0 BIT(16)
57#define OMAP3_MCSPI_CHCONF_DPE1 BIT(17)
58#define OMAP3_MCSPI_CHCONF_IS BIT(18)
59#define OMAP3_MCSPI_CHCONF_TURBO BIT(19)
60#define OMAP3_MCSPI_CHCONF_FORCE BIT(20)
61
62#define OMAP3_MCSPI_CHSTAT_RXS BIT(0)
63#define OMAP3_MCSPI_CHSTAT_TXS BIT(1)
64#define OMAP3_MCSPI_CHSTAT_EOT BIT(2)
65
66#define OMAP3_MCSPI_CHCTRL_EN BIT(0)
67#define OMAP3_MCSPI_CHCTRL_DIS (0 << 0)
68
69#define OMAP3_MCSPI_WAKEUPENABLE_WKEN BIT(0)
Jagan Tekiea172422016-03-14 22:41:24 +053070#define MCSPI_PINDIR_D0_IN_D1_OUT 0
71#define MCSPI_PINDIR_D0_OUT_D1_IN 1
Jagan Tekic2cca6c2016-03-01 15:16:20 +010072
73#define OMAP3_MCSPI_MAX_FREQ 48000000
74#define SPI_WAIT_TIMEOUT 10
75
76/* OMAP3 McSPI registers */
77struct mcspi_channel {
78 unsigned int chconf; /* 0x2C, 0x40, 0x54, 0x68 */
79 unsigned int chstat; /* 0x30, 0x44, 0x58, 0x6C */
80 unsigned int chctrl; /* 0x34, 0x48, 0x5C, 0x70 */
81 unsigned int tx; /* 0x38, 0x4C, 0x60, 0x74 */
82 unsigned int rx; /* 0x3C, 0x50, 0x64, 0x78 */
83};
84
85struct mcspi {
86 unsigned char res1[0x10];
87 unsigned int sysconfig; /* 0x10 */
88 unsigned int sysstatus; /* 0x14 */
89 unsigned int irqstatus; /* 0x18 */
90 unsigned int irqenable; /* 0x1C */
91 unsigned int wakeupenable; /* 0x20 */
92 unsigned int syst; /* 0x24 */
93 unsigned int modulctrl; /* 0x28 */
94 struct mcspi_channel channel[4];
95 /* channel0: 0x2C - 0x3C, bus 0 & 1 & 2 & 3 */
96 /* channel1: 0x40 - 0x50, bus 0 & 1 */
97 /* channel2: 0x54 - 0x64, bus 0 & 1 */
98 /* channel3: 0x68 - 0x78, bus 0 */
99};
100
Jagan Tekiea172422016-03-14 22:41:24 +0530101struct omap3_spi_priv {
Jagan Tekic2cca6c2016-03-01 15:16:20 +0100102 struct mcspi *regs;
Jagan Tekiea172422016-03-14 22:41:24 +0530103 unsigned int cs;
Jagan Tekic2cca6c2016-03-01 15:16:20 +0100104 unsigned int freq;
105 unsigned int mode;
Jagan Tekiea172422016-03-14 22:41:24 +0530106 unsigned int wordlen;
107 unsigned int pin_dir:1;
Jagan Tekic2cca6c2016-03-01 15:16:20 +0100108};
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500109
Jagan Tekiea172422016-03-14 22:41:24 +0530110static void omap3_spi_write_chconf(struct omap3_spi_priv *priv, int val)
Jagan Tekic2cca6c2016-03-01 15:16:20 +0100111{
Jagan Tekiea172422016-03-14 22:41:24 +0530112 writel(val, &priv->regs->channel[priv->cs].chconf);
ajoy260ab202012-11-17 21:10:15 +0000113 /* Flash post writes to make immediate effect */
Jagan Tekiea172422016-03-14 22:41:24 +0530114 readl(&priv->regs->channel[priv->cs].chconf);
ajoy260ab202012-11-17 21:10:15 +0000115}
116
Jagan Tekiea172422016-03-14 22:41:24 +0530117static void omap3_spi_set_enable(struct omap3_spi_priv *priv, int enable)
ajoy260ab202012-11-17 21:10:15 +0000118{
Jagan Tekiea172422016-03-14 22:41:24 +0530119 writel(enable, &priv->regs->channel[priv->cs].chctrl);
Wolfgang Denkec7fbf52013-10-04 17:43:24 +0200120 /* Flash post writes to make immediate effect */
Jagan Tekiea172422016-03-14 22:41:24 +0530121 readl(&priv->regs->channel[priv->cs].chctrl);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500122}
123
Jagan Tekiea172422016-03-14 22:41:24 +0530124static int omap3_spi_write(struct omap3_spi_priv *priv, unsigned int len,
Jagan Teki40a55df2016-03-01 15:16:21 +0100125 const void *txp, unsigned long flags)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500126{
David Duecka11b2a62015-04-01 14:20:24 +0200127 ulong start;
Jagan Tekiea172422016-03-14 22:41:24 +0530128 int i, chconf;
129
130 chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500131
ajoy260ab202012-11-17 21:10:15 +0000132 /* Enable the channel */
Jagan Tekiea172422016-03-14 22:41:24 +0530133 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500134
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300135 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
Jagan Tekiea172422016-03-14 22:41:24 +0530136 chconf |= (priv->wordlen - 1) << 7;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500137 chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
138 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530139 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500140
141 for (i = 0; i < len; i++) {
142 /* wait till TX register is empty (TXS == 1) */
David Duecka11b2a62015-04-01 14:20:24 +0200143 start = get_timer(0);
Jagan Tekiea172422016-03-14 22:41:24 +0530144 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500145 OMAP3_MCSPI_CHSTAT_TXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200146 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500147 printf("SPI TXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530148 readl(&priv->regs->channel[priv->cs].chstat));
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500149 return -1;
150 }
151 }
152 /* Write the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530153 unsigned int *tx = &priv->regs->channel[priv->cs].tx;
154 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300155 writel(((u32 *)txp)[i], tx);
Jagan Tekiea172422016-03-14 22:41:24 +0530156 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300157 writel(((u16 *)txp)[i], tx);
158 else
159 writel(((u8 *)txp)[i], tx);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500160 }
161
Wolfgang Denkec7fbf52013-10-04 17:43:24 +0200162 /* wait to finish of transfer */
Jagan Tekiea172422016-03-14 22:41:24 +0530163 while ((readl(&priv->regs->channel[priv->cs].chstat) &
164 (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS)) !=
165 (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS))
166 ;
ajoy260ab202012-11-17 21:10:15 +0000167
168 /* Disable the channel otherwise the next immediate RX will get affected */
Jagan Tekiea172422016-03-14 22:41:24 +0530169 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
ajoy260ab202012-11-17 21:10:15 +0000170
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500171 if (flags & SPI_XFER_END) {
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500172
173 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530174 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500175 }
176 return 0;
177}
178
Jagan Tekiea172422016-03-14 22:41:24 +0530179static int omap3_spi_read(struct omap3_spi_priv *priv, unsigned int len,
Jagan Teki40a55df2016-03-01 15:16:21 +0100180 void *rxp, unsigned long flags)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500181{
Jagan Tekiea172422016-03-14 22:41:24 +0530182 int i, chconf;
David Duecka11b2a62015-04-01 14:20:24 +0200183 ulong start;
Jagan Tekiea172422016-03-14 22:41:24 +0530184
185 chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500186
ajoy260ab202012-11-17 21:10:15 +0000187 /* Enable the channel */
Jagan Tekiea172422016-03-14 22:41:24 +0530188 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500189
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300190 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
Jagan Tekiea172422016-03-14 22:41:24 +0530191 chconf |= (priv->wordlen - 1) << 7;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500192 chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
193 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530194 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500195
Jagan Tekiea172422016-03-14 22:41:24 +0530196 writel(0, &priv->regs->channel[priv->cs].tx);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500197
198 for (i = 0; i < len; i++) {
David Duecka11b2a62015-04-01 14:20:24 +0200199 start = get_timer(0);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500200 /* Wait till RX register contains data (RXS == 1) */
Jagan Tekiea172422016-03-14 22:41:24 +0530201 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500202 OMAP3_MCSPI_CHSTAT_RXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200203 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500204 printf("SPI RXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530205 readl(&priv->regs->channel[priv->cs].chstat));
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500206 return -1;
207 }
208 }
ajoy260ab202012-11-17 21:10:15 +0000209
210 /* Disable the channel to prevent furher receiving */
Jagan Tekiea172422016-03-14 22:41:24 +0530211 if (i == (len - 1))
212 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
ajoy260ab202012-11-17 21:10:15 +0000213
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500214 /* Read the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530215 unsigned int *rx = &priv->regs->channel[priv->cs].rx;
216 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300217 ((u32 *)rxp)[i] = readl(rx);
Jagan Tekiea172422016-03-14 22:41:24 +0530218 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300219 ((u16 *)rxp)[i] = (u16)readl(rx);
220 else
221 ((u8 *)rxp)[i] = (u8)readl(rx);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500222 }
223
224 if (flags & SPI_XFER_END) {
225 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530226 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500227 }
228
229 return 0;
230}
231
jacopo mondife8793e2011-03-02 05:13:22 +0000232/*McSPI Transmit Receive Mode*/
Jagan Tekiea172422016-03-14 22:41:24 +0530233static int omap3_spi_txrx(struct omap3_spi_priv *priv, unsigned int len,
Jagan Teki40a55df2016-03-01 15:16:21 +0100234 const void *txp, void *rxp, unsigned long flags)
jacopo mondife8793e2011-03-02 05:13:22 +0000235{
David Duecka11b2a62015-04-01 14:20:24 +0200236 ulong start;
Jagan Tekiea172422016-03-14 22:41:24 +0530237 int chconf, i = 0;
238
239 chconf = readl(&priv->regs->channel[priv->cs].chconf);
jacopo mondife8793e2011-03-02 05:13:22 +0000240
241 /*Enable SPI channel*/
Jagan Tekiea172422016-03-14 22:41:24 +0530242 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
jacopo mondife8793e2011-03-02 05:13:22 +0000243
244 /*set TRANSMIT-RECEIVE Mode*/
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300245 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
Jagan Tekiea172422016-03-14 22:41:24 +0530246 chconf |= (priv->wordlen - 1) << 7;
jacopo mondife8793e2011-03-02 05:13:22 +0000247 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530248 omap3_spi_write_chconf(priv, chconf);
jacopo mondife8793e2011-03-02 05:13:22 +0000249
250 /*Shift in and out 1 byte at time*/
251 for (i=0; i < len; i++){
252 /* Write: wait for TX empty (TXS == 1)*/
David Duecka11b2a62015-04-01 14:20:24 +0200253 start = get_timer(0);
Jagan Tekiea172422016-03-14 22:41:24 +0530254 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
jacopo mondife8793e2011-03-02 05:13:22 +0000255 OMAP3_MCSPI_CHSTAT_TXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200256 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
jacopo mondife8793e2011-03-02 05:13:22 +0000257 printf("SPI TXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530258 readl(&priv->regs->channel[priv->cs].chstat));
jacopo mondife8793e2011-03-02 05:13:22 +0000259 return -1;
260 }
261 }
262 /* Write the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530263 unsigned int *tx = &priv->regs->channel[priv->cs].tx;
264 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300265 writel(((u32 *)txp)[i], tx);
Jagan Tekiea172422016-03-14 22:41:24 +0530266 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300267 writel(((u16 *)txp)[i], tx);
268 else
269 writel(((u8 *)txp)[i], tx);
jacopo mondife8793e2011-03-02 05:13:22 +0000270
271 /*Read: wait for RX containing data (RXS == 1)*/
David Duecka11b2a62015-04-01 14:20:24 +0200272 start = get_timer(0);
Jagan Tekiea172422016-03-14 22:41:24 +0530273 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
jacopo mondife8793e2011-03-02 05:13:22 +0000274 OMAP3_MCSPI_CHSTAT_RXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200275 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
jacopo mondife8793e2011-03-02 05:13:22 +0000276 printf("SPI RXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530277 readl(&priv->regs->channel[priv->cs].chstat));
jacopo mondife8793e2011-03-02 05:13:22 +0000278 return -1;
279 }
280 }
281 /* Read the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530282 unsigned int *rx = &priv->regs->channel[priv->cs].rx;
283 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300284 ((u32 *)rxp)[i] = readl(rx);
Jagan Tekiea172422016-03-14 22:41:24 +0530285 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300286 ((u16 *)rxp)[i] = (u16)readl(rx);
287 else
288 ((u8 *)rxp)[i] = (u8)readl(rx);
jacopo mondife8793e2011-03-02 05:13:22 +0000289 }
ajoy260ab202012-11-17 21:10:15 +0000290 /* Disable the channel */
Jagan Tekiea172422016-03-14 22:41:24 +0530291 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
jacopo mondife8793e2011-03-02 05:13:22 +0000292
293 /*if transfer must be terminated disable the channel*/
294 if (flags & SPI_XFER_END) {
295 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530296 omap3_spi_write_chconf(priv, chconf);
jacopo mondife8793e2011-03-02 05:13:22 +0000297 }
298
299 return 0;
300}
301
Jagan Tekiea172422016-03-14 22:41:24 +0530302static int _spi_xfer(struct omap3_spi_priv *priv, unsigned int bitlen,
303 const void *dout, void *din, unsigned long flags)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500304{
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500305 unsigned int len;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500306 int ret = -1;
307
Jagan Tekiea172422016-03-14 22:41:24 +0530308 if (priv->wordlen < 4 || priv->wordlen > 32) {
309 printf("omap3_spi: invalid wordlen %d\n", priv->wordlen);
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300310 return -1;
311 }
312
Jagan Tekiea172422016-03-14 22:41:24 +0530313 if (bitlen % priv->wordlen)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500314 return -1;
315
Jagan Tekiea172422016-03-14 22:41:24 +0530316 len = bitlen / priv->wordlen;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500317
318 if (bitlen == 0) { /* only change CS */
Jagan Tekiea172422016-03-14 22:41:24 +0530319 int chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500320
321 if (flags & SPI_XFER_BEGIN) {
Jagan Tekiea172422016-03-14 22:41:24 +0530322 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500323 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530324 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500325 }
326 if (flags & SPI_XFER_END) {
327 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530328 omap3_spi_write_chconf(priv, chconf);
329 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500330 }
331 ret = 0;
332 } else {
jacopo mondife8793e2011-03-02 05:13:22 +0000333 if (dout != NULL && din != NULL)
Jagan Tekiea172422016-03-14 22:41:24 +0530334 ret = omap3_spi_txrx(priv, len, dout, din, flags);
jacopo mondife8793e2011-03-02 05:13:22 +0000335 else if (dout != NULL)
Jagan Tekiea172422016-03-14 22:41:24 +0530336 ret = omap3_spi_write(priv, len, dout, flags);
jacopo mondife8793e2011-03-02 05:13:22 +0000337 else if (din != NULL)
Jagan Tekiea172422016-03-14 22:41:24 +0530338 ret = omap3_spi_read(priv, len, din, flags);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500339 }
340 return ret;
341}
342
Jagan Tekiea172422016-03-14 22:41:24 +0530343static void _omap3_spi_set_speed(struct omap3_spi_priv *priv)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500344{
Jagan Tekiea172422016-03-14 22:41:24 +0530345 uint32_t confr, div = 0;
346
347 confr = readl(&priv->regs->channel[priv->cs].chconf);
348
349 /* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */
350 if (priv->freq) {
351 while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div))
352 > priv->freq)
353 div++;
354 } else {
355 div = 0xC;
356 }
357
358 /* set clock divisor */
359 confr &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
360 confr |= div << 2;
361
362 omap3_spi_write_chconf(priv, confr);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500363}
364
Jagan Tekiea172422016-03-14 22:41:24 +0530365static void _omap3_spi_set_mode(struct omap3_spi_priv *priv)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500366{
Jagan Tekiea172422016-03-14 22:41:24 +0530367 uint32_t confr;
368
369 confr = readl(&priv->regs->channel[priv->cs].chconf);
370
371 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
372 * REVISIT: this controller could support SPI_3WIRE mode.
373 */
374 if (priv->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
375 confr &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
376 confr |= OMAP3_MCSPI_CHCONF_DPE0;
377 } else {
378 confr &= ~OMAP3_MCSPI_CHCONF_DPE0;
379 confr |= OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1;
380 }
381
382 /* set SPI mode 0..3 */
383 confr &= ~(OMAP3_MCSPI_CHCONF_POL | OMAP3_MCSPI_CHCONF_PHA);
384 if (priv->mode & SPI_CPHA)
385 confr |= OMAP3_MCSPI_CHCONF_PHA;
386 if (priv->mode & SPI_CPOL)
387 confr |= OMAP3_MCSPI_CHCONF_POL;
388
389 /* set chipselect polarity; manage with FORCE */
390 if (!(priv->mode & SPI_CS_HIGH))
391 confr |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
392 else
393 confr &= ~OMAP3_MCSPI_CHCONF_EPOL;
394
395 /* Transmit & receive mode */
396 confr &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
397
398 omap3_spi_write_chconf(priv, confr);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500399}
400
Jagan Tekiea172422016-03-14 22:41:24 +0530401static void _omap3_spi_set_wordlen(struct omap3_spi_priv *priv)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500402{
Jagan Tekiea172422016-03-14 22:41:24 +0530403 unsigned int confr;
404
405 /* McSPI individual channel configuration */
David Rivshin7aacc912019-02-18 18:04:29 -0500406 confr = readl(&priv->regs->channel[priv->cs].chconf);
Jagan Tekiea172422016-03-14 22:41:24 +0530407
408 /* wordlength */
409 confr &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
410 confr |= (priv->wordlen - 1) << 7;
411
412 omap3_spi_write_chconf(priv, confr);
413}
414
415static void spi_reset(struct mcspi *regs)
416{
417 unsigned int tmp;
418
419 writel(OMAP3_MCSPI_SYSCONFIG_SOFTRESET, &regs->sysconfig);
420 do {
421 tmp = readl(&regs->sysstatus);
422 } while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE));
423
424 writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE |
425 OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP |
426 OMAP3_MCSPI_SYSCONFIG_SMARTIDLE, &regs->sysconfig);
427
428 writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, &regs->wakeupenable);
429}
430
431static void _omap3_spi_claim_bus(struct omap3_spi_priv *priv)
432{
433 unsigned int conf;
Jagan Tekiea172422016-03-14 22:41:24 +0530434 /*
435 * setup when switching from (reset default) slave mode
436 * to single-channel master mode
437 */
438 conf = readl(&priv->regs->modulctrl);
439 conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS);
440 conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;
441
442 writel(conf, &priv->regs->modulctrl);
Jagan Tekiea172422016-03-14 22:41:24 +0530443}
444
Jagan Tekiea172422016-03-14 22:41:24 +0530445static int omap3_spi_claim_bus(struct udevice *dev)
446{
447 struct udevice *bus = dev->parent;
448 struct omap3_spi_priv *priv = dev_get_priv(bus);
449 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
450
451 priv->cs = slave_plat->cs;
Hannes Schmelzer9e778802018-06-02 08:06:47 +0200452 priv->freq = slave_plat->max_hz;
453
Jagan Tekiea172422016-03-14 22:41:24 +0530454 _omap3_spi_claim_bus(priv);
455
456 return 0;
457}
458
459static int omap3_spi_release_bus(struct udevice *dev)
460{
461 struct udevice *bus = dev->parent;
462 struct omap3_spi_priv *priv = dev_get_priv(bus);
463
Hannes Schmelzer202f9a42018-06-26 16:08:39 +0200464 writel(OMAP3_MCSPI_MODULCTRL_MS, &priv->regs->modulctrl);
Jagan Tekiea172422016-03-14 22:41:24 +0530465
466 return 0;
467}
468
469static int omap3_spi_set_wordlen(struct udevice *dev, unsigned int wordlen)
470{
471 struct udevice *bus = dev->parent;
472 struct omap3_spi_priv *priv = dev_get_priv(bus);
473 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
474
475 priv->cs = slave_plat->cs;
476 priv->wordlen = wordlen;
477 _omap3_spi_set_wordlen(priv);
478
479 return 0;
480}
481
482static int omap3_spi_probe(struct udevice *dev)
483{
484 struct omap3_spi_priv *priv = dev_get_priv(dev);
485 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700486 int node = dev_of_offset(dev);
Jagan Tekiea172422016-03-14 22:41:24 +0530487
Martin Hejnfeltd74ea962016-05-19 09:11:58 +0200488 struct omap2_mcspi_platform_config* data =
489 (struct omap2_mcspi_platform_config*)dev_get_driver_data(dev);
490
Tom Rini5a9ecb22020-07-24 08:42:06 -0400491 priv->regs = (struct mcspi *)(devfdt_get_addr(dev) + data->regs_offset);
Sjoerd Simons74ac81c2018-03-22 22:55:02 +0100492 if (fdtdec_get_bool(blob, node, "ti,pindir-d0-out-d1-in"))
493 priv->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
494 else
495 priv->pin_dir = MCSPI_PINDIR_D0_IN_D1_OUT;
Jagan Tekiea172422016-03-14 22:41:24 +0530496 priv->wordlen = SPI_DEFAULT_WORDLEN;
Hannes Schmelzer202f9a42018-06-26 16:08:39 +0200497
498 spi_reset(priv->regs);
499
Jagan Tekiea172422016-03-14 22:41:24 +0530500 return 0;
501}
502
503static int omap3_spi_xfer(struct udevice *dev, unsigned int bitlen,
504 const void *dout, void *din, unsigned long flags)
505{
506 struct udevice *bus = dev->parent;
507 struct omap3_spi_priv *priv = dev_get_priv(bus);
508
509 return _spi_xfer(priv, bitlen, dout, din, flags);
510}
511
Jagan Teki55865a92018-03-14 23:07:31 +0530512static int omap3_spi_set_speed(struct udevice *dev, unsigned int speed)
Jagan Tekiea172422016-03-14 22:41:24 +0530513{
Jagan Teki96a95ee2018-03-07 10:30:45 +0530514
Hannes Schmelzer7e4f8332018-06-02 08:06:48 +0200515 struct omap3_spi_priv *priv = dev_get_priv(dev);
516
517 priv->freq = speed;
Jagan Teki96a95ee2018-03-07 10:30:45 +0530518 _omap3_spi_set_speed(priv);
519
Jagan Tekiea172422016-03-14 22:41:24 +0530520 return 0;
521}
522
Jagan Teki55865a92018-03-14 23:07:31 +0530523static int omap3_spi_set_mode(struct udevice *dev, uint mode)
Jagan Tekiea172422016-03-14 22:41:24 +0530524{
Hannes Schmelzer7e4f8332018-06-02 08:06:48 +0200525 struct omap3_spi_priv *priv = dev_get_priv(dev);
526
527 priv->mode = mode;
Jagan Teki96a95ee2018-03-07 10:30:45 +0530528
Jagan Teki96a95ee2018-03-07 10:30:45 +0530529 _omap3_spi_set_mode(priv);
530
Jagan Tekiea172422016-03-14 22:41:24 +0530531 return 0;
532}
533
534static const struct dm_spi_ops omap3_spi_ops = {
535 .claim_bus = omap3_spi_claim_bus,
536 .release_bus = omap3_spi_release_bus,
537 .set_wordlen = omap3_spi_set_wordlen,
538 .xfer = omap3_spi_xfer,
539 .set_speed = omap3_spi_set_speed,
540 .set_mode = omap3_spi_set_mode,
541 /*
542 * cs_info is not needed, since we require all chip selects to be
543 * in the device tree explicitly
544 */
545};
546
Martin Hejnfeltd74ea962016-05-19 09:11:58 +0200547static struct omap2_mcspi_platform_config omap2_pdata = {
548 .regs_offset = 0,
549};
550
551static struct omap2_mcspi_platform_config omap4_pdata = {
552 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
553};
554
Jagan Tekiea172422016-03-14 22:41:24 +0530555static const struct udevice_id omap3_spi_ids[] = {
Martin Hejnfeltd74ea962016-05-19 09:11:58 +0200556 { .compatible = "ti,omap2-mcspi", .data = (ulong)&omap2_pdata },
557 { .compatible = "ti,omap4-mcspi", .data = (ulong)&omap4_pdata },
Jagan Tekiea172422016-03-14 22:41:24 +0530558 { }
559};
560
561U_BOOT_DRIVER(omap3_spi) = {
562 .name = "omap3_spi",
563 .id = UCLASS_SPI,
564 .of_match = omap3_spi_ids,
565 .probe = omap3_spi_probe,
566 .ops = &omap3_spi_ops,
567 .priv_auto_alloc_size = sizeof(struct omap3_spi_priv),
Jagan Tekiea172422016-03-14 22:41:24 +0530568};