blob: dc277aae01b841ede89ef002811fa5de3b72ccfe [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>
Faiz Abbas618ba9f2020-09-14 12:11:15 +053025#include <omap3_spi.h>
Jagan Tekic2cca6c2016-03-01 15:16:20 +010026
Jagan Tekiea172422016-03-14 22:41:24 +053027DECLARE_GLOBAL_DATA_PTR;
28
Martin Hejnfeltd74ea962016-05-19 09:11:58 +020029struct omap2_mcspi_platform_config {
30 unsigned int regs_offset;
31};
32
Jagan Tekiea172422016-03-14 22:41:24 +053033struct omap3_spi_priv {
Jagan Tekic2cca6c2016-03-01 15:16:20 +010034 struct mcspi *regs;
Jagan Tekiea172422016-03-14 22:41:24 +053035 unsigned int cs;
Jagan Tekic2cca6c2016-03-01 15:16:20 +010036 unsigned int freq;
37 unsigned int mode;
Jagan Tekiea172422016-03-14 22:41:24 +053038 unsigned int wordlen;
39 unsigned int pin_dir:1;
Jagan Tekic2cca6c2016-03-01 15:16:20 +010040};
Dirk Behme78cd9ac2010-12-11 11:01:00 -050041
Jagan Tekiea172422016-03-14 22:41:24 +053042static void omap3_spi_write_chconf(struct omap3_spi_priv *priv, int val)
Jagan Tekic2cca6c2016-03-01 15:16:20 +010043{
Jagan Tekiea172422016-03-14 22:41:24 +053044 writel(val, &priv->regs->channel[priv->cs].chconf);
ajoy260ab202012-11-17 21:10:15 +000045 /* Flash post writes to make immediate effect */
Jagan Tekiea172422016-03-14 22:41:24 +053046 readl(&priv->regs->channel[priv->cs].chconf);
ajoy260ab202012-11-17 21:10:15 +000047}
48
Jagan Tekiea172422016-03-14 22:41:24 +053049static void omap3_spi_set_enable(struct omap3_spi_priv *priv, int enable)
ajoy260ab202012-11-17 21:10:15 +000050{
Jagan Tekiea172422016-03-14 22:41:24 +053051 writel(enable, &priv->regs->channel[priv->cs].chctrl);
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020052 /* Flash post writes to make immediate effect */
Jagan Tekiea172422016-03-14 22:41:24 +053053 readl(&priv->regs->channel[priv->cs].chctrl);
Dirk Behme78cd9ac2010-12-11 11:01:00 -050054}
55
Jagan Tekiea172422016-03-14 22:41:24 +053056static int omap3_spi_write(struct omap3_spi_priv *priv, unsigned int len,
Jagan Teki40a55df2016-03-01 15:16:21 +010057 const void *txp, unsigned long flags)
Dirk Behme78cd9ac2010-12-11 11:01:00 -050058{
David Duecka11b2a62015-04-01 14:20:24 +020059 ulong start;
Jagan Tekiea172422016-03-14 22:41:24 +053060 int i, chconf;
61
62 chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -050063
ajoy260ab202012-11-17 21:10:15 +000064 /* Enable the channel */
Jagan Tekiea172422016-03-14 22:41:24 +053065 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme78cd9ac2010-12-11 11:01:00 -050066
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +030067 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
Jagan Tekiea172422016-03-14 22:41:24 +053068 chconf |= (priv->wordlen - 1) << 7;
Dirk Behme78cd9ac2010-12-11 11:01:00 -050069 chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
70 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +053071 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -050072
73 for (i = 0; i < len; i++) {
74 /* wait till TX register is empty (TXS == 1) */
David Duecka11b2a62015-04-01 14:20:24 +020075 start = get_timer(0);
Jagan Tekiea172422016-03-14 22:41:24 +053076 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
Dirk Behme78cd9ac2010-12-11 11:01:00 -050077 OMAP3_MCSPI_CHSTAT_TXS)) {
David Duecka11b2a62015-04-01 14:20:24 +020078 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
Dirk Behme78cd9ac2010-12-11 11:01:00 -050079 printf("SPI TXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +053080 readl(&priv->regs->channel[priv->cs].chstat));
Dirk Behme78cd9ac2010-12-11 11:01:00 -050081 return -1;
82 }
83 }
84 /* Write the data */
Jagan Tekiea172422016-03-14 22:41:24 +053085 unsigned int *tx = &priv->regs->channel[priv->cs].tx;
86 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +030087 writel(((u32 *)txp)[i], tx);
Jagan Tekiea172422016-03-14 22:41:24 +053088 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +030089 writel(((u16 *)txp)[i], tx);
90 else
91 writel(((u8 *)txp)[i], tx);
Dirk Behme78cd9ac2010-12-11 11:01:00 -050092 }
93
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020094 /* wait to finish of transfer */
Jagan Tekiea172422016-03-14 22:41:24 +053095 while ((readl(&priv->regs->channel[priv->cs].chstat) &
96 (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS)) !=
97 (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS))
98 ;
ajoy260ab202012-11-17 21:10:15 +000099
100 /* Disable the channel otherwise the next immediate RX will get affected */
Jagan Tekiea172422016-03-14 22:41:24 +0530101 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
ajoy260ab202012-11-17 21:10:15 +0000102
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500103 if (flags & SPI_XFER_END) {
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500104
105 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530106 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500107 }
108 return 0;
109}
110
Jagan Tekiea172422016-03-14 22:41:24 +0530111static int omap3_spi_read(struct omap3_spi_priv *priv, unsigned int len,
Jagan Teki40a55df2016-03-01 15:16:21 +0100112 void *rxp, unsigned long flags)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500113{
Jagan Tekiea172422016-03-14 22:41:24 +0530114 int i, chconf;
David Duecka11b2a62015-04-01 14:20:24 +0200115 ulong start;
Jagan Tekiea172422016-03-14 22:41:24 +0530116
117 chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500118
ajoy260ab202012-11-17 21:10:15 +0000119 /* Enable the channel */
Jagan Tekiea172422016-03-14 22:41:24 +0530120 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500121
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300122 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
Jagan Tekiea172422016-03-14 22:41:24 +0530123 chconf |= (priv->wordlen - 1) << 7;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500124 chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
125 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530126 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500127
Jagan Tekiea172422016-03-14 22:41:24 +0530128 writel(0, &priv->regs->channel[priv->cs].tx);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500129
130 for (i = 0; i < len; i++) {
David Duecka11b2a62015-04-01 14:20:24 +0200131 start = get_timer(0);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500132 /* Wait till RX register contains data (RXS == 1) */
Jagan Tekiea172422016-03-14 22:41:24 +0530133 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500134 OMAP3_MCSPI_CHSTAT_RXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200135 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500136 printf("SPI RXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530137 readl(&priv->regs->channel[priv->cs].chstat));
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500138 return -1;
139 }
140 }
ajoy260ab202012-11-17 21:10:15 +0000141
142 /* Disable the channel to prevent furher receiving */
Jagan Tekiea172422016-03-14 22:41:24 +0530143 if (i == (len - 1))
144 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
ajoy260ab202012-11-17 21:10:15 +0000145
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500146 /* Read the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530147 unsigned int *rx = &priv->regs->channel[priv->cs].rx;
148 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300149 ((u32 *)rxp)[i] = readl(rx);
Jagan Tekiea172422016-03-14 22:41:24 +0530150 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300151 ((u16 *)rxp)[i] = (u16)readl(rx);
152 else
153 ((u8 *)rxp)[i] = (u8)readl(rx);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500154 }
155
156 if (flags & SPI_XFER_END) {
157 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530158 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500159 }
160
161 return 0;
162}
163
jacopo mondife8793e2011-03-02 05:13:22 +0000164/*McSPI Transmit Receive Mode*/
Jagan Tekiea172422016-03-14 22:41:24 +0530165static int omap3_spi_txrx(struct omap3_spi_priv *priv, unsigned int len,
Jagan Teki40a55df2016-03-01 15:16:21 +0100166 const void *txp, void *rxp, unsigned long flags)
jacopo mondife8793e2011-03-02 05:13:22 +0000167{
David Duecka11b2a62015-04-01 14:20:24 +0200168 ulong start;
Jagan Tekiea172422016-03-14 22:41:24 +0530169 int chconf, i = 0;
170
171 chconf = readl(&priv->regs->channel[priv->cs].chconf);
jacopo mondife8793e2011-03-02 05:13:22 +0000172
173 /*Enable SPI channel*/
Jagan Tekiea172422016-03-14 22:41:24 +0530174 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
jacopo mondife8793e2011-03-02 05:13:22 +0000175
176 /*set TRANSMIT-RECEIVE Mode*/
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300177 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
Jagan Tekiea172422016-03-14 22:41:24 +0530178 chconf |= (priv->wordlen - 1) << 7;
jacopo mondife8793e2011-03-02 05:13:22 +0000179 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530180 omap3_spi_write_chconf(priv, chconf);
jacopo mondife8793e2011-03-02 05:13:22 +0000181
182 /*Shift in and out 1 byte at time*/
183 for (i=0; i < len; i++){
184 /* Write: wait for TX empty (TXS == 1)*/
David Duecka11b2a62015-04-01 14:20:24 +0200185 start = get_timer(0);
Jagan Tekiea172422016-03-14 22:41:24 +0530186 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
jacopo mondife8793e2011-03-02 05:13:22 +0000187 OMAP3_MCSPI_CHSTAT_TXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200188 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
jacopo mondife8793e2011-03-02 05:13:22 +0000189 printf("SPI TXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530190 readl(&priv->regs->channel[priv->cs].chstat));
jacopo mondife8793e2011-03-02 05:13:22 +0000191 return -1;
192 }
193 }
194 /* Write the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530195 unsigned int *tx = &priv->regs->channel[priv->cs].tx;
196 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300197 writel(((u32 *)txp)[i], tx);
Jagan Tekiea172422016-03-14 22:41:24 +0530198 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300199 writel(((u16 *)txp)[i], tx);
200 else
201 writel(((u8 *)txp)[i], tx);
jacopo mondife8793e2011-03-02 05:13:22 +0000202
203 /*Read: wait for RX containing data (RXS == 1)*/
David Duecka11b2a62015-04-01 14:20:24 +0200204 start = get_timer(0);
Jagan Tekiea172422016-03-14 22:41:24 +0530205 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
jacopo mondife8793e2011-03-02 05:13:22 +0000206 OMAP3_MCSPI_CHSTAT_RXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200207 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
jacopo mondife8793e2011-03-02 05:13:22 +0000208 printf("SPI RXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530209 readl(&priv->regs->channel[priv->cs].chstat));
jacopo mondife8793e2011-03-02 05:13:22 +0000210 return -1;
211 }
212 }
213 /* Read the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530214 unsigned int *rx = &priv->regs->channel[priv->cs].rx;
215 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300216 ((u32 *)rxp)[i] = readl(rx);
Jagan Tekiea172422016-03-14 22:41:24 +0530217 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300218 ((u16 *)rxp)[i] = (u16)readl(rx);
219 else
220 ((u8 *)rxp)[i] = (u8)readl(rx);
jacopo mondife8793e2011-03-02 05:13:22 +0000221 }
ajoy260ab202012-11-17 21:10:15 +0000222 /* Disable the channel */
Jagan Tekiea172422016-03-14 22:41:24 +0530223 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
jacopo mondife8793e2011-03-02 05:13:22 +0000224
225 /*if transfer must be terminated disable the channel*/
226 if (flags & SPI_XFER_END) {
227 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530228 omap3_spi_write_chconf(priv, chconf);
jacopo mondife8793e2011-03-02 05:13:22 +0000229 }
230
231 return 0;
232}
233
Jagan Tekiea172422016-03-14 22:41:24 +0530234static int _spi_xfer(struct omap3_spi_priv *priv, unsigned int bitlen,
235 const void *dout, void *din, unsigned long flags)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500236{
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500237 unsigned int len;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500238 int ret = -1;
239
Jagan Tekiea172422016-03-14 22:41:24 +0530240 if (priv->wordlen < 4 || priv->wordlen > 32) {
241 printf("omap3_spi: invalid wordlen %d\n", priv->wordlen);
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300242 return -1;
243 }
244
Jagan Tekiea172422016-03-14 22:41:24 +0530245 if (bitlen % priv->wordlen)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500246 return -1;
247
Jagan Tekiea172422016-03-14 22:41:24 +0530248 len = bitlen / priv->wordlen;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500249
250 if (bitlen == 0) { /* only change CS */
Jagan Tekiea172422016-03-14 22:41:24 +0530251 int chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500252
253 if (flags & SPI_XFER_BEGIN) {
Jagan Tekiea172422016-03-14 22:41:24 +0530254 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500255 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530256 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500257 }
258 if (flags & SPI_XFER_END) {
259 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530260 omap3_spi_write_chconf(priv, chconf);
261 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500262 }
263 ret = 0;
264 } else {
jacopo mondife8793e2011-03-02 05:13:22 +0000265 if (dout != NULL && din != NULL)
Jagan Tekiea172422016-03-14 22:41:24 +0530266 ret = omap3_spi_txrx(priv, len, dout, din, flags);
jacopo mondife8793e2011-03-02 05:13:22 +0000267 else if (dout != NULL)
Jagan Tekiea172422016-03-14 22:41:24 +0530268 ret = omap3_spi_write(priv, len, dout, flags);
jacopo mondife8793e2011-03-02 05:13:22 +0000269 else if (din != NULL)
Jagan Tekiea172422016-03-14 22:41:24 +0530270 ret = omap3_spi_read(priv, len, din, flags);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500271 }
272 return ret;
273}
274
Jagan Tekiea172422016-03-14 22:41:24 +0530275static void _omap3_spi_set_speed(struct omap3_spi_priv *priv)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500276{
Jagan Tekiea172422016-03-14 22:41:24 +0530277 uint32_t confr, div = 0;
278
279 confr = readl(&priv->regs->channel[priv->cs].chconf);
280
281 /* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */
282 if (priv->freq) {
283 while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div))
284 > priv->freq)
285 div++;
286 } else {
287 div = 0xC;
288 }
289
290 /* set clock divisor */
291 confr &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
292 confr |= div << 2;
293
294 omap3_spi_write_chconf(priv, confr);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500295}
296
Jagan Tekiea172422016-03-14 22:41:24 +0530297static void _omap3_spi_set_mode(struct omap3_spi_priv *priv)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500298{
Jagan Tekiea172422016-03-14 22:41:24 +0530299 uint32_t confr;
300
301 confr = readl(&priv->regs->channel[priv->cs].chconf);
302
303 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
304 * REVISIT: this controller could support SPI_3WIRE mode.
305 */
306 if (priv->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
307 confr &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
308 confr |= OMAP3_MCSPI_CHCONF_DPE0;
309 } else {
310 confr &= ~OMAP3_MCSPI_CHCONF_DPE0;
311 confr |= OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1;
312 }
313
314 /* set SPI mode 0..3 */
315 confr &= ~(OMAP3_MCSPI_CHCONF_POL | OMAP3_MCSPI_CHCONF_PHA);
316 if (priv->mode & SPI_CPHA)
317 confr |= OMAP3_MCSPI_CHCONF_PHA;
318 if (priv->mode & SPI_CPOL)
319 confr |= OMAP3_MCSPI_CHCONF_POL;
320
321 /* set chipselect polarity; manage with FORCE */
322 if (!(priv->mode & SPI_CS_HIGH))
323 confr |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
324 else
325 confr &= ~OMAP3_MCSPI_CHCONF_EPOL;
326
327 /* Transmit & receive mode */
328 confr &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
329
330 omap3_spi_write_chconf(priv, confr);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500331}
332
Jagan Tekiea172422016-03-14 22:41:24 +0530333static void _omap3_spi_set_wordlen(struct omap3_spi_priv *priv)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500334{
Jagan Tekiea172422016-03-14 22:41:24 +0530335 unsigned int confr;
336
337 /* McSPI individual channel configuration */
David Rivshin7aacc912019-02-18 18:04:29 -0500338 confr = readl(&priv->regs->channel[priv->cs].chconf);
Jagan Tekiea172422016-03-14 22:41:24 +0530339
340 /* wordlength */
341 confr &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
342 confr |= (priv->wordlen - 1) << 7;
343
344 omap3_spi_write_chconf(priv, confr);
345}
346
347static void spi_reset(struct mcspi *regs)
348{
349 unsigned int tmp;
350
351 writel(OMAP3_MCSPI_SYSCONFIG_SOFTRESET, &regs->sysconfig);
352 do {
353 tmp = readl(&regs->sysstatus);
354 } while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE));
355
356 writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE |
357 OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP |
358 OMAP3_MCSPI_SYSCONFIG_SMARTIDLE, &regs->sysconfig);
359
360 writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, &regs->wakeupenable);
361}
362
363static void _omap3_spi_claim_bus(struct omap3_spi_priv *priv)
364{
365 unsigned int conf;
Jagan Tekiea172422016-03-14 22:41:24 +0530366 /*
367 * setup when switching from (reset default) slave mode
368 * to single-channel master mode
369 */
370 conf = readl(&priv->regs->modulctrl);
371 conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS);
372 conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;
373
374 writel(conf, &priv->regs->modulctrl);
Jagan Tekiea172422016-03-14 22:41:24 +0530375}
376
Jagan Tekiea172422016-03-14 22:41:24 +0530377static int omap3_spi_claim_bus(struct udevice *dev)
378{
379 struct udevice *bus = dev->parent;
380 struct omap3_spi_priv *priv = dev_get_priv(bus);
Simon Glass71fa5b42020-12-03 16:55:18 -0700381 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_plat(dev);
Jagan Tekiea172422016-03-14 22:41:24 +0530382
383 priv->cs = slave_plat->cs;
Hannes Schmelzer9e778802018-06-02 08:06:47 +0200384 priv->freq = slave_plat->max_hz;
385
Jagan Tekiea172422016-03-14 22:41:24 +0530386 _omap3_spi_claim_bus(priv);
387
388 return 0;
389}
390
391static int omap3_spi_release_bus(struct udevice *dev)
392{
393 struct udevice *bus = dev->parent;
394 struct omap3_spi_priv *priv = dev_get_priv(bus);
395
Hannes Schmelzer202f9a42018-06-26 16:08:39 +0200396 writel(OMAP3_MCSPI_MODULCTRL_MS, &priv->regs->modulctrl);
Jagan Tekiea172422016-03-14 22:41:24 +0530397
398 return 0;
399}
400
401static int omap3_spi_set_wordlen(struct udevice *dev, unsigned int wordlen)
402{
403 struct udevice *bus = dev->parent;
404 struct omap3_spi_priv *priv = dev_get_priv(bus);
Simon Glass71fa5b42020-12-03 16:55:18 -0700405 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_plat(dev);
Jagan Tekiea172422016-03-14 22:41:24 +0530406
407 priv->cs = slave_plat->cs;
408 priv->wordlen = wordlen;
409 _omap3_spi_set_wordlen(priv);
410
411 return 0;
412}
413
414static int omap3_spi_probe(struct udevice *dev)
415{
416 struct omap3_spi_priv *priv = dev_get_priv(dev);
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530417 struct omap3_spi_plat *plat = dev_get_platdata(dev);
Jagan Tekiea172422016-03-14 22:41:24 +0530418
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530419 priv->regs = plat->regs;
420 priv->pin_dir = plat->pin_dir;
Jagan Tekiea172422016-03-14 22:41:24 +0530421 priv->wordlen = SPI_DEFAULT_WORDLEN;
Hannes Schmelzer202f9a42018-06-26 16:08:39 +0200422
423 spi_reset(priv->regs);
424
Jagan Tekiea172422016-03-14 22:41:24 +0530425 return 0;
426}
427
428static int omap3_spi_xfer(struct udevice *dev, unsigned int bitlen,
429 const void *dout, void *din, unsigned long flags)
430{
431 struct udevice *bus = dev->parent;
432 struct omap3_spi_priv *priv = dev_get_priv(bus);
433
434 return _spi_xfer(priv, bitlen, dout, din, flags);
435}
436
Jagan Teki55865a92018-03-14 23:07:31 +0530437static int omap3_spi_set_speed(struct udevice *dev, unsigned int speed)
Jagan Tekiea172422016-03-14 22:41:24 +0530438{
Jagan Teki96a95ee2018-03-07 10:30:45 +0530439
Hannes Schmelzer7e4f8332018-06-02 08:06:48 +0200440 struct omap3_spi_priv *priv = dev_get_priv(dev);
441
442 priv->freq = speed;
Jagan Teki96a95ee2018-03-07 10:30:45 +0530443 _omap3_spi_set_speed(priv);
444
Jagan Tekiea172422016-03-14 22:41:24 +0530445 return 0;
446}
447
Jagan Teki55865a92018-03-14 23:07:31 +0530448static int omap3_spi_set_mode(struct udevice *dev, uint mode)
Jagan Tekiea172422016-03-14 22:41:24 +0530449{
Hannes Schmelzer7e4f8332018-06-02 08:06:48 +0200450 struct omap3_spi_priv *priv = dev_get_priv(dev);
451
452 priv->mode = mode;
Jagan Teki96a95ee2018-03-07 10:30:45 +0530453
Jagan Teki96a95ee2018-03-07 10:30:45 +0530454 _omap3_spi_set_mode(priv);
455
Jagan Tekiea172422016-03-14 22:41:24 +0530456 return 0;
457}
458
459static const struct dm_spi_ops omap3_spi_ops = {
460 .claim_bus = omap3_spi_claim_bus,
461 .release_bus = omap3_spi_release_bus,
462 .set_wordlen = omap3_spi_set_wordlen,
463 .xfer = omap3_spi_xfer,
464 .set_speed = omap3_spi_set_speed,
465 .set_mode = omap3_spi_set_mode,
466 /*
467 * cs_info is not needed, since we require all chip selects to be
468 * in the device tree explicitly
469 */
470};
471
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530472#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Martin Hejnfeltd74ea962016-05-19 09:11:58 +0200473static struct omap2_mcspi_platform_config omap2_pdata = {
474 .regs_offset = 0,
475};
476
477static struct omap2_mcspi_platform_config omap4_pdata = {
478 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
479};
480
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530481static int omap3_spi_ofdata_to_platdata(struct udevice *dev)
482{
483 struct omap2_mcspi_platform_config *data =
484 (struct omap2_mcspi_platform_config *)dev_get_driver_data(dev);
485 struct omap3_spi_plat *plat = dev_get_platdata(dev);
486
487 plat->regs = (struct mcspi *)(dev_read_addr(dev) + data->regs_offset);
488
489 if (dev_read_bool(dev, "ti,pindir-d0-out-d1-in"))
490 plat->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
491 else
492 plat->pin_dir = MCSPI_PINDIR_D0_IN_D1_OUT;
493
494 return 0;
495}
496
Jagan Tekiea172422016-03-14 22:41:24 +0530497static const struct udevice_id omap3_spi_ids[] = {
Martin Hejnfeltd74ea962016-05-19 09:11:58 +0200498 { .compatible = "ti,omap2-mcspi", .data = (ulong)&omap2_pdata },
499 { .compatible = "ti,omap4-mcspi", .data = (ulong)&omap4_pdata },
Jagan Tekiea172422016-03-14 22:41:24 +0530500 { }
501};
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530502#endif
Jagan Tekiea172422016-03-14 22:41:24 +0530503U_BOOT_DRIVER(omap3_spi) = {
504 .name = "omap3_spi",
505 .id = UCLASS_SPI,
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530506 .flags = DM_FLAG_PRE_RELOC,
507#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Jagan Tekiea172422016-03-14 22:41:24 +0530508 .of_match = omap3_spi_ids,
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530509 .ofdata_to_platdata = omap3_spi_ofdata_to_platdata,
Simon Glass71fa5b42020-12-03 16:55:18 -0700510 .plat_auto = sizeof(struct omap3_spi_plat),
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530511#endif
Jagan Tekiea172422016-03-14 22:41:24 +0530512 .probe = omap3_spi_probe,
513 .ops = &omap3_spi_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700514 .priv_auto = sizeof(struct omap3_spi_priv),
Jagan Tekiea172422016-03-14 22:41:24 +0530515};