blob: ff7b55f8707e8bb9a1ff124ed5f15482ea76988f [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060023#include <asm/global_data.h>
Dirk Behme78cd9ac2010-12-11 11:01:00 -050024#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#include <linux/bitops.h>
Faiz Abbas618ba9f2020-09-14 12:11:15 +053026#include <omap3_spi.h>
Jagan Tekic2cca6c2016-03-01 15:16:20 +010027
Jagan Tekiea172422016-03-14 22:41:24 +053028DECLARE_GLOBAL_DATA_PTR;
29
Martin Hejnfeltd74ea962016-05-19 09:11:58 +020030struct omap2_mcspi_platform_config {
31 unsigned int regs_offset;
32};
33
Jagan Tekiea172422016-03-14 22:41:24 +053034struct omap3_spi_priv {
Jagan Tekic2cca6c2016-03-01 15:16:20 +010035 struct mcspi *regs;
Jagan Tekiea172422016-03-14 22:41:24 +053036 unsigned int cs;
Jagan Tekic2cca6c2016-03-01 15:16:20 +010037 unsigned int freq;
38 unsigned int mode;
Jagan Tekiea172422016-03-14 22:41:24 +053039 unsigned int wordlen;
40 unsigned int pin_dir:1;
Vignesh Raghavendraab34b892020-11-29 12:53:05 +053041
42 bool bus_claimed;
Jagan Tekic2cca6c2016-03-01 15:16:20 +010043};
Dirk Behme78cd9ac2010-12-11 11:01:00 -050044
Jagan Tekiea172422016-03-14 22:41:24 +053045static void omap3_spi_write_chconf(struct omap3_spi_priv *priv, int val)
Jagan Tekic2cca6c2016-03-01 15:16:20 +010046{
Jagan Tekiea172422016-03-14 22:41:24 +053047 writel(val, &priv->regs->channel[priv->cs].chconf);
ajoy260ab202012-11-17 21:10:15 +000048 /* Flash post writes to make immediate effect */
Jagan Tekiea172422016-03-14 22:41:24 +053049 readl(&priv->regs->channel[priv->cs].chconf);
ajoy260ab202012-11-17 21:10:15 +000050}
51
Jagan Tekiea172422016-03-14 22:41:24 +053052static void omap3_spi_set_enable(struct omap3_spi_priv *priv, int enable)
ajoy260ab202012-11-17 21:10:15 +000053{
Jagan Tekiea172422016-03-14 22:41:24 +053054 writel(enable, &priv->regs->channel[priv->cs].chctrl);
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020055 /* Flash post writes to make immediate effect */
Jagan Tekiea172422016-03-14 22:41:24 +053056 readl(&priv->regs->channel[priv->cs].chctrl);
Dirk Behme78cd9ac2010-12-11 11:01:00 -050057}
58
Jagan Tekiea172422016-03-14 22:41:24 +053059static int omap3_spi_write(struct omap3_spi_priv *priv, unsigned int len,
Jagan Teki40a55df2016-03-01 15:16:21 +010060 const void *txp, unsigned long flags)
Dirk Behme78cd9ac2010-12-11 11:01:00 -050061{
David Duecka11b2a62015-04-01 14:20:24 +020062 ulong start;
Jagan Tekiea172422016-03-14 22:41:24 +053063 int i, chconf;
64
65 chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -050066
ajoy260ab202012-11-17 21:10:15 +000067 /* Enable the channel */
Jagan Tekiea172422016-03-14 22:41:24 +053068 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme78cd9ac2010-12-11 11:01:00 -050069
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +030070 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
Jagan Tekiea172422016-03-14 22:41:24 +053071 chconf |= (priv->wordlen - 1) << 7;
Dirk Behme78cd9ac2010-12-11 11:01:00 -050072 chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
73 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +053074 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -050075
76 for (i = 0; i < len; i++) {
77 /* wait till TX register is empty (TXS == 1) */
David Duecka11b2a62015-04-01 14:20:24 +020078 start = get_timer(0);
Jagan Tekiea172422016-03-14 22:41:24 +053079 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
Dirk Behme78cd9ac2010-12-11 11:01:00 -050080 OMAP3_MCSPI_CHSTAT_TXS)) {
David Duecka11b2a62015-04-01 14:20:24 +020081 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
Dirk Behme78cd9ac2010-12-11 11:01:00 -050082 printf("SPI TXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +053083 readl(&priv->regs->channel[priv->cs].chstat));
Dirk Behme78cd9ac2010-12-11 11:01:00 -050084 return -1;
85 }
86 }
87 /* Write the data */
Jagan Tekiea172422016-03-14 22:41:24 +053088 unsigned int *tx = &priv->regs->channel[priv->cs].tx;
89 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +030090 writel(((u32 *)txp)[i], tx);
Jagan Tekiea172422016-03-14 22:41:24 +053091 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +030092 writel(((u16 *)txp)[i], tx);
93 else
94 writel(((u8 *)txp)[i], tx);
Dirk Behme78cd9ac2010-12-11 11:01:00 -050095 }
96
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020097 /* wait to finish of transfer */
Jagan Tekiea172422016-03-14 22:41:24 +053098 while ((readl(&priv->regs->channel[priv->cs].chstat) &
99 (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS)) !=
100 (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS))
101 ;
ajoy260ab202012-11-17 21:10:15 +0000102
103 /* Disable the channel otherwise the next immediate RX will get affected */
Jagan Tekiea172422016-03-14 22:41:24 +0530104 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
ajoy260ab202012-11-17 21:10:15 +0000105
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500106 if (flags & SPI_XFER_END) {
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500107
108 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530109 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500110 }
111 return 0;
112}
113
Jagan Tekiea172422016-03-14 22:41:24 +0530114static int omap3_spi_read(struct omap3_spi_priv *priv, unsigned int len,
Jagan Teki40a55df2016-03-01 15:16:21 +0100115 void *rxp, unsigned long flags)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500116{
Jagan Tekiea172422016-03-14 22:41:24 +0530117 int i, chconf;
David Duecka11b2a62015-04-01 14:20:24 +0200118 ulong start;
Jagan Tekiea172422016-03-14 22:41:24 +0530119
120 chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500121
ajoy260ab202012-11-17 21:10:15 +0000122 /* Enable the channel */
Jagan Tekiea172422016-03-14 22:41:24 +0530123 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500124
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300125 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
Jagan Tekiea172422016-03-14 22:41:24 +0530126 chconf |= (priv->wordlen - 1) << 7;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500127 chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
128 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530129 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500130
Jagan Tekiea172422016-03-14 22:41:24 +0530131 writel(0, &priv->regs->channel[priv->cs].tx);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500132
133 for (i = 0; i < len; i++) {
David Duecka11b2a62015-04-01 14:20:24 +0200134 start = get_timer(0);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500135 /* Wait till RX register contains data (RXS == 1) */
Jagan Tekiea172422016-03-14 22:41:24 +0530136 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500137 OMAP3_MCSPI_CHSTAT_RXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200138 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500139 printf("SPI RXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530140 readl(&priv->regs->channel[priv->cs].chstat));
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500141 return -1;
142 }
143 }
ajoy260ab202012-11-17 21:10:15 +0000144
Pengfei Fan746271d2022-12-09 09:39:50 +0800145 /* Disable the channel to prevent further receiving */
Jagan Tekiea172422016-03-14 22:41:24 +0530146 if (i == (len - 1))
147 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
ajoy260ab202012-11-17 21:10:15 +0000148
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500149 /* Read the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530150 unsigned int *rx = &priv->regs->channel[priv->cs].rx;
151 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300152 ((u32 *)rxp)[i] = readl(rx);
Jagan Tekiea172422016-03-14 22:41:24 +0530153 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300154 ((u16 *)rxp)[i] = (u16)readl(rx);
155 else
156 ((u8 *)rxp)[i] = (u8)readl(rx);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500157 }
158
159 if (flags & SPI_XFER_END) {
160 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530161 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500162 }
163
164 return 0;
165}
166
jacopo mondife8793e2011-03-02 05:13:22 +0000167/*McSPI Transmit Receive Mode*/
Jagan Tekiea172422016-03-14 22:41:24 +0530168static int omap3_spi_txrx(struct omap3_spi_priv *priv, unsigned int len,
Jagan Teki40a55df2016-03-01 15:16:21 +0100169 const void *txp, void *rxp, unsigned long flags)
jacopo mondife8793e2011-03-02 05:13:22 +0000170{
David Duecka11b2a62015-04-01 14:20:24 +0200171 ulong start;
Jagan Tekiea172422016-03-14 22:41:24 +0530172 int chconf, i = 0;
173
174 chconf = readl(&priv->regs->channel[priv->cs].chconf);
jacopo mondife8793e2011-03-02 05:13:22 +0000175
176 /*Enable SPI channel*/
Jagan Tekiea172422016-03-14 22:41:24 +0530177 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
jacopo mondife8793e2011-03-02 05:13:22 +0000178
179 /*set TRANSMIT-RECEIVE Mode*/
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300180 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
Jagan Tekiea172422016-03-14 22:41:24 +0530181 chconf |= (priv->wordlen - 1) << 7;
jacopo mondife8793e2011-03-02 05:13:22 +0000182 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530183 omap3_spi_write_chconf(priv, chconf);
jacopo mondife8793e2011-03-02 05:13:22 +0000184
185 /*Shift in and out 1 byte at time*/
186 for (i=0; i < len; i++){
187 /* Write: wait for TX empty (TXS == 1)*/
David Duecka11b2a62015-04-01 14:20:24 +0200188 start = get_timer(0);
Jagan Tekiea172422016-03-14 22:41:24 +0530189 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
jacopo mondife8793e2011-03-02 05:13:22 +0000190 OMAP3_MCSPI_CHSTAT_TXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200191 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
jacopo mondife8793e2011-03-02 05:13:22 +0000192 printf("SPI TXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530193 readl(&priv->regs->channel[priv->cs].chstat));
jacopo mondife8793e2011-03-02 05:13:22 +0000194 return -1;
195 }
196 }
197 /* Write the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530198 unsigned int *tx = &priv->regs->channel[priv->cs].tx;
199 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300200 writel(((u32 *)txp)[i], tx);
Jagan Tekiea172422016-03-14 22:41:24 +0530201 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300202 writel(((u16 *)txp)[i], tx);
203 else
204 writel(((u8 *)txp)[i], tx);
jacopo mondife8793e2011-03-02 05:13:22 +0000205
206 /*Read: wait for RX containing data (RXS == 1)*/
David Duecka11b2a62015-04-01 14:20:24 +0200207 start = get_timer(0);
Jagan Tekiea172422016-03-14 22:41:24 +0530208 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
jacopo mondife8793e2011-03-02 05:13:22 +0000209 OMAP3_MCSPI_CHSTAT_RXS)) {
David Duecka11b2a62015-04-01 14:20:24 +0200210 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
jacopo mondife8793e2011-03-02 05:13:22 +0000211 printf("SPI RXS timed out, status=0x%08x\n",
Jagan Tekiea172422016-03-14 22:41:24 +0530212 readl(&priv->regs->channel[priv->cs].chstat));
jacopo mondife8793e2011-03-02 05:13:22 +0000213 return -1;
214 }
215 }
216 /* Read the data */
Jagan Tekiea172422016-03-14 22:41:24 +0530217 unsigned int *rx = &priv->regs->channel[priv->cs].rx;
218 if (priv->wordlen > 16)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300219 ((u32 *)rxp)[i] = readl(rx);
Jagan Tekiea172422016-03-14 22:41:24 +0530220 else if (priv->wordlen > 8)
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300221 ((u16 *)rxp)[i] = (u16)readl(rx);
222 else
223 ((u8 *)rxp)[i] = (u8)readl(rx);
jacopo mondife8793e2011-03-02 05:13:22 +0000224 }
ajoy260ab202012-11-17 21:10:15 +0000225 /* Disable the channel */
Jagan Tekiea172422016-03-14 22:41:24 +0530226 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
jacopo mondife8793e2011-03-02 05:13:22 +0000227
228 /*if transfer must be terminated disable the channel*/
229 if (flags & SPI_XFER_END) {
230 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530231 omap3_spi_write_chconf(priv, chconf);
jacopo mondife8793e2011-03-02 05:13:22 +0000232 }
233
234 return 0;
235}
236
Jagan Tekiea172422016-03-14 22:41:24 +0530237static int _spi_xfer(struct omap3_spi_priv *priv, unsigned int bitlen,
238 const void *dout, void *din, unsigned long flags)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500239{
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500240 unsigned int len;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500241 int ret = -1;
242
Jagan Tekiea172422016-03-14 22:41:24 +0530243 if (priv->wordlen < 4 || priv->wordlen > 32) {
244 printf("omap3_spi: invalid wordlen %d\n", priv->wordlen);
Nikita Kiryanov18dd07c2013-10-16 17:23:25 +0300245 return -1;
246 }
247
Jagan Tekiea172422016-03-14 22:41:24 +0530248 if (bitlen % priv->wordlen)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500249 return -1;
250
Jagan Tekiea172422016-03-14 22:41:24 +0530251 len = bitlen / priv->wordlen;
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500252
253 if (bitlen == 0) { /* only change CS */
Jagan Tekiea172422016-03-14 22:41:24 +0530254 int chconf = readl(&priv->regs->channel[priv->cs].chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500255
256 if (flags & SPI_XFER_BEGIN) {
Jagan Tekiea172422016-03-14 22:41:24 +0530257 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500258 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530259 omap3_spi_write_chconf(priv, chconf);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500260 }
261 if (flags & SPI_XFER_END) {
262 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
Jagan Tekiea172422016-03-14 22:41:24 +0530263 omap3_spi_write_chconf(priv, chconf);
264 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500265 }
266 ret = 0;
267 } else {
jacopo mondife8793e2011-03-02 05:13:22 +0000268 if (dout != NULL && din != NULL)
Jagan Tekiea172422016-03-14 22:41:24 +0530269 ret = omap3_spi_txrx(priv, len, dout, din, flags);
jacopo mondife8793e2011-03-02 05:13:22 +0000270 else if (dout != NULL)
Jagan Tekiea172422016-03-14 22:41:24 +0530271 ret = omap3_spi_write(priv, len, dout, flags);
jacopo mondife8793e2011-03-02 05:13:22 +0000272 else if (din != NULL)
Jagan Tekiea172422016-03-14 22:41:24 +0530273 ret = omap3_spi_read(priv, len, din, flags);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500274 }
275 return ret;
276}
277
Jagan Tekiea172422016-03-14 22:41:24 +0530278static void _omap3_spi_set_speed(struct omap3_spi_priv *priv)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500279{
Jagan Tekiea172422016-03-14 22:41:24 +0530280 uint32_t confr, div = 0;
281
282 confr = readl(&priv->regs->channel[priv->cs].chconf);
283
284 /* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */
285 if (priv->freq) {
286 while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div))
287 > priv->freq)
288 div++;
289 } else {
290 div = 0xC;
291 }
292
293 /* set clock divisor */
294 confr &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
295 confr |= div << 2;
296
297 omap3_spi_write_chconf(priv, confr);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500298}
299
Jagan Tekiea172422016-03-14 22:41:24 +0530300static void _omap3_spi_set_mode(struct omap3_spi_priv *priv)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500301{
Jagan Tekiea172422016-03-14 22:41:24 +0530302 uint32_t confr;
303
304 confr = readl(&priv->regs->channel[priv->cs].chconf);
305
306 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
307 * REVISIT: this controller could support SPI_3WIRE mode.
308 */
309 if (priv->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
310 confr &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
311 confr |= OMAP3_MCSPI_CHCONF_DPE0;
312 } else {
313 confr &= ~OMAP3_MCSPI_CHCONF_DPE0;
314 confr |= OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1;
315 }
316
317 /* set SPI mode 0..3 */
318 confr &= ~(OMAP3_MCSPI_CHCONF_POL | OMAP3_MCSPI_CHCONF_PHA);
319 if (priv->mode & SPI_CPHA)
320 confr |= OMAP3_MCSPI_CHCONF_PHA;
321 if (priv->mode & SPI_CPOL)
322 confr |= OMAP3_MCSPI_CHCONF_POL;
323
324 /* set chipselect polarity; manage with FORCE */
325 if (!(priv->mode & SPI_CS_HIGH))
326 confr |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
327 else
328 confr &= ~OMAP3_MCSPI_CHCONF_EPOL;
329
330 /* Transmit & receive mode */
331 confr &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
332
333 omap3_spi_write_chconf(priv, confr);
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500334}
335
Jagan Tekiea172422016-03-14 22:41:24 +0530336static void _omap3_spi_set_wordlen(struct omap3_spi_priv *priv)
Dirk Behme78cd9ac2010-12-11 11:01:00 -0500337{
Jagan Tekiea172422016-03-14 22:41:24 +0530338 unsigned int confr;
339
340 /* McSPI individual channel configuration */
David Rivshin7aacc912019-02-18 18:04:29 -0500341 confr = readl(&priv->regs->channel[priv->cs].chconf);
Jagan Tekiea172422016-03-14 22:41:24 +0530342
343 /* wordlength */
344 confr &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
345 confr |= (priv->wordlen - 1) << 7;
346
347 omap3_spi_write_chconf(priv, confr);
348}
349
Julien Panis0e026962023-05-29 15:42:28 +0200350static void spi_reset(struct omap3_spi_priv *priv)
Jagan Tekiea172422016-03-14 22:41:24 +0530351{
352 unsigned int tmp;
353
Julien Panis0e026962023-05-29 15:42:28 +0200354 writel(OMAP3_MCSPI_SYSCONFIG_SOFTRESET, &priv->regs->sysconfig);
Jagan Tekiea172422016-03-14 22:41:24 +0530355 do {
Julien Panis0e026962023-05-29 15:42:28 +0200356 tmp = readl(&priv->regs->sysstatus);
Jagan Tekiea172422016-03-14 22:41:24 +0530357 } while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE));
358
359 writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE |
360 OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP |
Julien Panis0e026962023-05-29 15:42:28 +0200361 OMAP3_MCSPI_SYSCONFIG_SMARTIDLE, &priv->regs->sysconfig);
Jagan Tekiea172422016-03-14 22:41:24 +0530362
Julien Panis0e026962023-05-29 15:42:28 +0200363 writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, &priv->regs->wakeupenable);
364
365 /*
366 * Set the same default mode for each channel, especially CS polarity
367 * which must be common for all SPI slaves before any transfer.
368 */
369 for (priv->cs = 0 ; priv->cs < OMAP4_MCSPI_CHAN_NB ; priv->cs++)
370 _omap3_spi_set_mode(priv);
371 priv->cs = 0;
Jagan Tekiea172422016-03-14 22:41:24 +0530372}
373
374static void _omap3_spi_claim_bus(struct omap3_spi_priv *priv)
375{
376 unsigned int conf;
Jagan Tekiea172422016-03-14 22:41:24 +0530377 /*
378 * setup when switching from (reset default) slave mode
379 * to single-channel master mode
380 */
381 conf = readl(&priv->regs->modulctrl);
382 conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS);
383 conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;
384
385 writel(conf, &priv->regs->modulctrl);
Vignesh Raghavendraab34b892020-11-29 12:53:05 +0530386
387 priv->bus_claimed = true;
Jagan Tekiea172422016-03-14 22:41:24 +0530388}
389
Jagan Tekiea172422016-03-14 22:41:24 +0530390static int omap3_spi_claim_bus(struct udevice *dev)
391{
392 struct udevice *bus = dev->parent;
393 struct omap3_spi_priv *priv = dev_get_priv(bus);
Simon Glassb75b15b2020-12-03 16:55:23 -0700394 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Jagan Tekiea172422016-03-14 22:41:24 +0530395
396 priv->cs = slave_plat->cs;
Vignesh Raghavendraab34b892020-11-29 12:53:05 +0530397 if (!priv->freq)
398 priv->freq = slave_plat->max_hz;
Hannes Schmelzer9e778802018-06-02 08:06:47 +0200399
Jagan Tekiea172422016-03-14 22:41:24 +0530400 _omap3_spi_claim_bus(priv);
Vignesh Raghavendraab34b892020-11-29 12:53:05 +0530401 _omap3_spi_set_speed(priv);
402 _omap3_spi_set_mode(priv);
Jagan Tekiea172422016-03-14 22:41:24 +0530403
404 return 0;
405}
406
407static int omap3_spi_release_bus(struct udevice *dev)
408{
409 struct udevice *bus = dev->parent;
410 struct omap3_spi_priv *priv = dev_get_priv(bus);
411
Hannes Schmelzer202f9a42018-06-26 16:08:39 +0200412 writel(OMAP3_MCSPI_MODULCTRL_MS, &priv->regs->modulctrl);
Jagan Tekiea172422016-03-14 22:41:24 +0530413
Vignesh Raghavendraab34b892020-11-29 12:53:05 +0530414 priv->bus_claimed = false;
415
Jagan Tekiea172422016-03-14 22:41:24 +0530416 return 0;
417}
418
419static int omap3_spi_set_wordlen(struct udevice *dev, unsigned int wordlen)
420{
421 struct udevice *bus = dev->parent;
422 struct omap3_spi_priv *priv = dev_get_priv(bus);
Simon Glassb75b15b2020-12-03 16:55:23 -0700423 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Jagan Tekiea172422016-03-14 22:41:24 +0530424
425 priv->cs = slave_plat->cs;
426 priv->wordlen = wordlen;
427 _omap3_spi_set_wordlen(priv);
428
429 return 0;
430}
431
432static int omap3_spi_probe(struct udevice *dev)
433{
434 struct omap3_spi_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700435 struct omap3_spi_plat *plat = dev_get_plat(dev);
Jagan Tekiea172422016-03-14 22:41:24 +0530436
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530437 priv->regs = plat->regs;
438 priv->pin_dir = plat->pin_dir;
Jagan Tekiea172422016-03-14 22:41:24 +0530439 priv->wordlen = SPI_DEFAULT_WORDLEN;
Hannes Schmelzer202f9a42018-06-26 16:08:39 +0200440
Julien Panis0e026962023-05-29 15:42:28 +0200441 spi_reset(priv);
Hannes Schmelzer202f9a42018-06-26 16:08:39 +0200442
Jagan Tekiea172422016-03-14 22:41:24 +0530443 return 0;
444}
445
446static int omap3_spi_xfer(struct udevice *dev, unsigned int bitlen,
447 const void *dout, void *din, unsigned long flags)
448{
449 struct udevice *bus = dev->parent;
450 struct omap3_spi_priv *priv = dev_get_priv(bus);
451
452 return _spi_xfer(priv, bitlen, dout, din, flags);
453}
454
Jagan Teki55865a92018-03-14 23:07:31 +0530455static int omap3_spi_set_speed(struct udevice *dev, unsigned int speed)
Jagan Tekiea172422016-03-14 22:41:24 +0530456{
Jagan Teki96a95ee2018-03-07 10:30:45 +0530457
Hannes Schmelzer7e4f8332018-06-02 08:06:48 +0200458 struct omap3_spi_priv *priv = dev_get_priv(dev);
459
460 priv->freq = speed;
Vignesh Raghavendraab34b892020-11-29 12:53:05 +0530461 if (priv->bus_claimed)
462 _omap3_spi_set_speed(priv);
Jagan Teki96a95ee2018-03-07 10:30:45 +0530463
Jagan Tekiea172422016-03-14 22:41:24 +0530464 return 0;
465}
466
Jagan Teki55865a92018-03-14 23:07:31 +0530467static int omap3_spi_set_mode(struct udevice *dev, uint mode)
Jagan Tekiea172422016-03-14 22:41:24 +0530468{
Hannes Schmelzer7e4f8332018-06-02 08:06:48 +0200469 struct omap3_spi_priv *priv = dev_get_priv(dev);
470
471 priv->mode = mode;
Jagan Teki96a95ee2018-03-07 10:30:45 +0530472
Vignesh Raghavendraab34b892020-11-29 12:53:05 +0530473 if (priv->bus_claimed)
474 _omap3_spi_set_mode(priv);
Jagan Teki96a95ee2018-03-07 10:30:45 +0530475
Jagan Tekiea172422016-03-14 22:41:24 +0530476 return 0;
477}
478
479static const struct dm_spi_ops omap3_spi_ops = {
480 .claim_bus = omap3_spi_claim_bus,
481 .release_bus = omap3_spi_release_bus,
482 .set_wordlen = omap3_spi_set_wordlen,
483 .xfer = omap3_spi_xfer,
484 .set_speed = omap3_spi_set_speed,
485 .set_mode = omap3_spi_set_mode,
486 /*
487 * cs_info is not needed, since we require all chip selects to be
488 * in the device tree explicitly
489 */
490};
491
Simon Glass3580f6d2021-08-07 07:24:03 -0600492#if CONFIG_IS_ENABLED(OF_REAL)
Martin Hejnfeltd74ea962016-05-19 09:11:58 +0200493static struct omap2_mcspi_platform_config omap2_pdata = {
494 .regs_offset = 0,
495};
496
497static struct omap2_mcspi_platform_config omap4_pdata = {
498 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
499};
500
Simon Glassaad29ae2020-12-03 16:55:21 -0700501static int omap3_spi_of_to_plat(struct udevice *dev)
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530502{
503 struct omap2_mcspi_platform_config *data =
504 (struct omap2_mcspi_platform_config *)dev_get_driver_data(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700505 struct omap3_spi_plat *plat = dev_get_plat(dev);
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530506
507 plat->regs = (struct mcspi *)(dev_read_addr(dev) + data->regs_offset);
508
509 if (dev_read_bool(dev, "ti,pindir-d0-out-d1-in"))
510 plat->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
511 else
512 plat->pin_dir = MCSPI_PINDIR_D0_IN_D1_OUT;
513
514 return 0;
515}
516
Jagan Tekiea172422016-03-14 22:41:24 +0530517static const struct udevice_id omap3_spi_ids[] = {
Martin Hejnfeltd74ea962016-05-19 09:11:58 +0200518 { .compatible = "ti,omap2-mcspi", .data = (ulong)&omap2_pdata },
519 { .compatible = "ti,omap4-mcspi", .data = (ulong)&omap4_pdata },
Jagan Tekiea172422016-03-14 22:41:24 +0530520 { }
521};
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530522#endif
Jagan Tekiea172422016-03-14 22:41:24 +0530523U_BOOT_DRIVER(omap3_spi) = {
524 .name = "omap3_spi",
525 .id = UCLASS_SPI,
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530526 .flags = DM_FLAG_PRE_RELOC,
Simon Glass3580f6d2021-08-07 07:24:03 -0600527#if CONFIG_IS_ENABLED(OF_REAL)
Jagan Tekiea172422016-03-14 22:41:24 +0530528 .of_match = omap3_spi_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700529 .of_to_plat = omap3_spi_of_to_plat,
Simon Glass71fa5b42020-12-03 16:55:18 -0700530 .plat_auto = sizeof(struct omap3_spi_plat),
Faiz Abbas12b2fb22020-09-14 12:11:16 +0530531#endif
Jagan Tekiea172422016-03-14 22:41:24 +0530532 .probe = omap3_spi_probe,
533 .ops = &omap3_spi_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700534 .priv_auto = sizeof(struct omap3_spi_priv),
Jagan Tekiea172422016-03-14 22:41:24 +0530535};