Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 2 | /* |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 3 | * Copyright (c) 2010-2013 NVIDIA Corporation |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 4 | * With help from the mpc8xxx SPI driver |
| 5 | * With more help from omap3_spi SPI driver |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 1045315 | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 12 | #include <time.h> |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | #include <asm/gpio.h> |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 15 | #include <asm/arch/clock.h> |
| 16 | #include <asm/arch/pinmux.h> |
Tom Warren | 150c249 | 2012-09-19 15:50:56 -0700 | [diff] [blame] | 17 | #include <asm/arch-tegra/clk_rst.h> |
Tom Warren | 150c249 | 2012-09-19 15:50:56 -0700 | [diff] [blame] | 18 | #include <spi.h> |
Allen Martin | 8f1b46b | 2013-01-29 13:51:24 +0000 | [diff] [blame] | 19 | #include <fdtdec.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 20 | #include <linux/bitops.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 21 | #include <linux/delay.h> |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 22 | #include "tegra_spi.h" |
Allen Martin | 8f1b46b | 2013-01-29 13:51:24 +0000 | [diff] [blame] | 23 | |
| 24 | DECLARE_GLOBAL_DATA_PTR; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 25 | |
Jagan Teki | f692248 | 2015-10-23 01:39:06 +0530 | [diff] [blame] | 26 | #define SPI_CMD_GO BIT(30) |
Allen Martin | 7a49ba6 | 2013-03-16 18:58:05 +0000 | [diff] [blame] | 27 | #define SPI_CMD_ACTIVE_SCLK_SHIFT 26 |
| 28 | #define SPI_CMD_ACTIVE_SCLK_MASK (3 << SPI_CMD_ACTIVE_SCLK_SHIFT) |
Jagan Teki | f692248 | 2015-10-23 01:39:06 +0530 | [diff] [blame] | 29 | #define SPI_CMD_CK_SDA BIT(21) |
Allen Martin | 7a49ba6 | 2013-03-16 18:58:05 +0000 | [diff] [blame] | 30 | #define SPI_CMD_ACTIVE_SDA_SHIFT 18 |
| 31 | #define SPI_CMD_ACTIVE_SDA_MASK (3 << SPI_CMD_ACTIVE_SDA_SHIFT) |
Jagan Teki | f692248 | 2015-10-23 01:39:06 +0530 | [diff] [blame] | 32 | #define SPI_CMD_CS_POL BIT(16) |
| 33 | #define SPI_CMD_TXEN BIT(15) |
| 34 | #define SPI_CMD_RXEN BIT(14) |
| 35 | #define SPI_CMD_CS_VAL BIT(13) |
| 36 | #define SPI_CMD_CS_SOFT BIT(12) |
| 37 | #define SPI_CMD_CS_DELAY BIT(9) |
| 38 | #define SPI_CMD_CS3_EN BIT(8) |
| 39 | #define SPI_CMD_CS2_EN BIT(7) |
| 40 | #define SPI_CMD_CS1_EN BIT(6) |
| 41 | #define SPI_CMD_CS0_EN BIT(5) |
| 42 | #define SPI_CMD_BIT_LENGTH BIT(4) |
Jagan Teki | 76538ec | 2015-10-23 01:03:10 +0530 | [diff] [blame] | 43 | #define SPI_CMD_BIT_LENGTH_MASK GENMASK(4, 0) |
Allen Martin | 7a49ba6 | 2013-03-16 18:58:05 +0000 | [diff] [blame] | 44 | |
Jagan Teki | f692248 | 2015-10-23 01:39:06 +0530 | [diff] [blame] | 45 | #define SPI_STAT_BSY BIT(31) |
| 46 | #define SPI_STAT_RDY BIT(30) |
| 47 | #define SPI_STAT_RXF_FLUSH BIT(29) |
| 48 | #define SPI_STAT_TXF_FLUSH BIT(28) |
| 49 | #define SPI_STAT_RXF_UNR BIT(27) |
| 50 | #define SPI_STAT_TXF_OVF BIT(26) |
| 51 | #define SPI_STAT_RXF_EMPTY BIT(25) |
| 52 | #define SPI_STAT_RXF_FULL BIT(24) |
| 53 | #define SPI_STAT_TXF_EMPTY BIT(23) |
| 54 | #define SPI_STAT_TXF_FULL BIT(22) |
| 55 | #define SPI_STAT_SEL_TXRX_N BIT(16) |
| 56 | #define SPI_STAT_CUR_BLKCNT BIT(15) |
Allen Martin | 7a49ba6 | 2013-03-16 18:58:05 +0000 | [diff] [blame] | 57 | |
| 58 | #define SPI_TIMEOUT 1000 |
| 59 | #define TEGRA_SPI_MAX_FREQ 52000000 |
| 60 | |
| 61 | struct spi_regs { |
| 62 | u32 command; /* SPI_COMMAND_0 register */ |
| 63 | u32 status; /* SPI_STATUS_0 register */ |
| 64 | u32 rx_cmp; /* SPI_RX_CMP_0 register */ |
| 65 | u32 dma_ctl; /* SPI_DMA_CTL_0 register */ |
| 66 | u32 tx_fifo; /* SPI_TX_FIFO_0 register */ |
| 67 | u32 rsvd[3]; /* offsets 0x14 to 0x1F reserved */ |
| 68 | u32 rx_fifo; /* SPI_RX_FIFO_0 register */ |
| 69 | }; |
| 70 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 71 | struct tegra20_sflash_priv { |
Allen Martin | 7a49ba6 | 2013-03-16 18:58:05 +0000 | [diff] [blame] | 72 | struct spi_regs *regs; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 73 | unsigned int freq; |
| 74 | unsigned int mode; |
Allen Martin | 8f1b46b | 2013-01-29 13:51:24 +0000 | [diff] [blame] | 75 | int periph_id; |
Allen Martin | 6b3a03e | 2013-03-16 18:58:06 +0000 | [diff] [blame] | 76 | int valid; |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 77 | int last_transaction_us; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 80 | int tegra20_sflash_cs_info(struct udevice *bus, unsigned int cs, |
| 81 | struct spi_cs_info *info) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 82 | { |
Allen Martin | 00a2749 | 2012-08-31 08:30:00 +0000 | [diff] [blame] | 83 | /* Tegra20 SPI-Flash - only 1 device ('bus/cs') */ |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 84 | if (cs != 0) |
Bin Meng | 4b06000 | 2019-09-09 06:00:01 -0700 | [diff] [blame] | 85 | return -EINVAL; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 86 | else |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 87 | return 0; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 90 | static int tegra20_sflash_of_to_plat(struct udevice *bus) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 91 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 92 | struct tegra_spi_plat *plat = dev_get_plat(bus); |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 93 | const void *blob = gd->fdt_blob; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 94 | int node = dev_of_offset(bus); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 95 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 96 | plat->base = dev_read_addr(bus); |
Simon Glass | 000f15f | 2017-07-25 08:30:00 -0600 | [diff] [blame] | 97 | plat->periph_id = clock_decode_periph_id(bus); |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 98 | |
| 99 | if (plat->periph_id == PERIPH_ID_NONE) { |
| 100 | debug("%s: could not decode periph id %d\n", __func__, |
| 101 | plat->periph_id); |
| 102 | return -FDT_ERR_NOTFOUND; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 105 | /* Use 500KHz as a suitable default */ |
| 106 | plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", |
| 107 | 500000); |
| 108 | plat->deactivate_delay_us = fdtdec_get_int(blob, node, |
| 109 | "spi-deactivate-delay", 0); |
| 110 | debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n", |
| 111 | __func__, plat->base, plat->periph_id, plat->frequency, |
| 112 | plat->deactivate_delay_us); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 113 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 114 | return 0; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 117 | static int tegra20_sflash_probe(struct udevice *bus) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 118 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 119 | struct tegra_spi_plat *plat = dev_get_plat(bus); |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 120 | struct tegra20_sflash_priv *priv = dev_get_priv(bus); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 121 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 122 | priv->regs = (struct spi_regs *)plat->base; |
| 123 | |
| 124 | priv->last_transaction_us = timer_get_us(); |
| 125 | priv->freq = plat->frequency; |
| 126 | priv->periph_id = plat->periph_id; |
| 127 | |
Stephen Warren | 4832c7f | 2016-08-18 10:53:33 -0600 | [diff] [blame] | 128 | /* Change SPI clock to correct frequency, PLLP_OUT0 source */ |
| 129 | clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH, |
| 130 | priv->freq); |
| 131 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 132 | return 0; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Simon Glass | 9694b72 | 2015-04-19 09:05:40 -0600 | [diff] [blame] | 135 | static int tegra20_sflash_claim_bus(struct udevice *dev) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 136 | { |
Simon Glass | 9694b72 | 2015-04-19 09:05:40 -0600 | [diff] [blame] | 137 | struct udevice *bus = dev->parent; |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 138 | struct tegra20_sflash_priv *priv = dev_get_priv(bus); |
| 139 | struct spi_regs *regs = priv->regs; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 140 | u32 reg; |
| 141 | |
| 142 | /* Change SPI clock to correct frequency, PLLP_OUT0 source */ |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 143 | clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH, |
| 144 | priv->freq); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 145 | |
| 146 | /* Clear stale status here */ |
| 147 | reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \ |
| 148 | SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF; |
| 149 | writel(reg, ®s->status); |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 150 | debug("%s: STATUS = %08x\n", __func__, readl(®s->status)); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * Use sw-controlled CS, so we can clock in data after ReadID, etc. |
| 154 | */ |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 155 | reg = (priv->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT; |
| 156 | if (priv->mode & 2) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 157 | reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT; |
| 158 | clrsetbits_le32(®s->command, SPI_CMD_ACTIVE_SCLK_MASK | |
| 159 | SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg); |
Allen Martin | 78f47b7 | 2013-03-16 18:58:07 +0000 | [diff] [blame] | 160 | debug("%s: COMMAND = %08x\n", __func__, readl(®s->command)); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 161 | |
| 162 | /* |
Allen Martin | 00a2749 | 2012-08-31 08:30:00 +0000 | [diff] [blame] | 163 | * SPI pins on Tegra20 are muxed - change pinmux later due to UART |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 164 | * issue. |
| 165 | */ |
Stephen Warren | 70ad375 | 2014-03-21 12:28:58 -0600 | [diff] [blame] | 166 | pinmux_set_func(PMUX_PINGRP_GMD, PMUX_FUNC_SFLASH); |
| 167 | pinmux_tristate_disable(PMUX_PINGRP_LSPI); |
| 168 | pinmux_set_func(PMUX_PINGRP_GMC, PMUX_FUNC_SFLASH); |
Simon Glass | 4560c7d | 2011-11-05 04:46:50 +0000 | [diff] [blame] | 169 | |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 173 | static void spi_cs_activate(struct udevice *dev) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 174 | { |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 175 | struct udevice *bus = dev->parent; |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 176 | struct tegra_spi_plat *pdata = dev_get_plat(bus); |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 177 | struct tegra20_sflash_priv *priv = dev_get_priv(bus); |
| 178 | |
| 179 | /* If it's too soon to do another transaction, wait */ |
| 180 | if (pdata->deactivate_delay_us && |
| 181 | priv->last_transaction_us) { |
| 182 | ulong delay_us; /* The delay completed so far */ |
| 183 | delay_us = timer_get_us() - priv->last_transaction_us; |
| 184 | if (delay_us < pdata->deactivate_delay_us) |
| 185 | udelay(pdata->deactivate_delay_us - delay_us); |
| 186 | } |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 187 | |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 188 | /* CS is negated on Tegra, so drive a 1 to get a 0 */ |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 189 | setbits_le32(&priv->regs->command, SPI_CMD_CS_VAL); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 192 | static void spi_cs_deactivate(struct udevice *dev) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 193 | { |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 194 | struct udevice *bus = dev->parent; |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 195 | struct tegra_spi_plat *pdata = dev_get_plat(bus); |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 196 | struct tegra20_sflash_priv *priv = dev_get_priv(bus); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 197 | |
| 198 | /* CS is negated on Tegra, so drive a 0 to get a 1 */ |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 199 | clrbits_le32(&priv->regs->command, SPI_CMD_CS_VAL); |
| 200 | |
| 201 | /* Remember time of this transaction so we can honour the bus delay */ |
| 202 | if (pdata->deactivate_delay_us) |
| 203 | priv->last_transaction_us = timer_get_us(); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 206 | static int tegra20_sflash_xfer(struct udevice *dev, unsigned int bitlen, |
| 207 | const void *data_out, void *data_in, |
| 208 | unsigned long flags) |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 209 | { |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 210 | struct udevice *bus = dev->parent; |
| 211 | struct tegra20_sflash_priv *priv = dev_get_priv(bus); |
| 212 | struct spi_regs *regs = priv->regs; |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 213 | u32 reg, tmpdout, tmpdin = 0; |
| 214 | const u8 *dout = data_out; |
| 215 | u8 *din = data_in; |
| 216 | int num_bytes; |
| 217 | int ret; |
| 218 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 219 | debug("%s: slave %u:%u dout %p din %p bitlen %u\n", |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 220 | __func__, dev_seq(bus), spi_chip_select(dev), dout, din, bitlen); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 221 | if (bitlen % 8) |
| 222 | return -1; |
| 223 | num_bytes = bitlen / 8; |
| 224 | |
| 225 | ret = 0; |
| 226 | |
| 227 | reg = readl(®s->status); |
| 228 | writel(reg, ®s->status); /* Clear all SPI events via R/W */ |
| 229 | debug("spi_xfer entry: STATUS = %08x\n", reg); |
| 230 | |
| 231 | reg = readl(®s->command); |
| 232 | reg |= SPI_CMD_TXEN | SPI_CMD_RXEN; |
| 233 | writel(reg, ®s->command); |
| 234 | debug("spi_xfer: COMMAND = %08x\n", readl(®s->command)); |
| 235 | |
| 236 | if (flags & SPI_XFER_BEGIN) |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 237 | spi_cs_activate(dev); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 238 | |
| 239 | /* handle data in 32-bit chunks */ |
| 240 | while (num_bytes > 0) { |
| 241 | int bytes; |
| 242 | int is_read = 0; |
| 243 | int tm, i; |
| 244 | |
| 245 | tmpdout = 0; |
| 246 | bytes = (num_bytes > 4) ? 4 : num_bytes; |
| 247 | |
| 248 | if (dout != NULL) { |
| 249 | for (i = 0; i < bytes; ++i) |
| 250 | tmpdout = (tmpdout << 8) | dout[i]; |
| 251 | } |
| 252 | |
| 253 | num_bytes -= bytes; |
| 254 | if (dout) |
| 255 | dout += bytes; |
| 256 | |
| 257 | clrsetbits_le32(®s->command, SPI_CMD_BIT_LENGTH_MASK, |
| 258 | bytes * 8 - 1); |
| 259 | writel(tmpdout, ®s->tx_fifo); |
| 260 | setbits_le32(®s->command, SPI_CMD_GO); |
| 261 | |
| 262 | /* |
| 263 | * Wait for SPI transmit FIFO to empty, or to time out. |
| 264 | * The RX FIFO status will be read and cleared last |
| 265 | */ |
| 266 | for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) { |
| 267 | u32 status; |
| 268 | |
| 269 | status = readl(®s->status); |
| 270 | |
| 271 | /* We can exit when we've had both RX and TX activity */ |
| 272 | if (is_read && (status & SPI_STAT_TXF_EMPTY)) |
| 273 | break; |
| 274 | |
| 275 | if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) != |
| 276 | SPI_STAT_RDY) |
| 277 | tm++; |
| 278 | |
| 279 | else if (!(status & SPI_STAT_RXF_EMPTY)) { |
| 280 | tmpdin = readl(®s->rx_fifo); |
| 281 | is_read = 1; |
| 282 | |
| 283 | /* swap bytes read in */ |
| 284 | if (din != NULL) { |
| 285 | for (i = bytes - 1; i >= 0; --i) { |
| 286 | din[i] = tmpdin & 0xff; |
| 287 | tmpdin >>= 8; |
| 288 | } |
| 289 | din += bytes; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if (tm >= SPI_TIMEOUT) |
| 295 | ret = tm; |
| 296 | |
| 297 | /* clear ACK RDY, etc. bits */ |
| 298 | writel(readl(®s->status), ®s->status); |
| 299 | } |
| 300 | |
| 301 | if (flags & SPI_XFER_END) |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 302 | spi_cs_deactivate(dev); |
Tom Warren | 9112ef8 | 2011-11-05 09:48:11 +0000 | [diff] [blame] | 303 | |
| 304 | debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n", |
| 305 | tmpdin, readl(®s->status)); |
| 306 | |
| 307 | if (ret) { |
| 308 | printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret); |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | } |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 314 | |
| 315 | static int tegra20_sflash_set_speed(struct udevice *bus, uint speed) |
| 316 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 317 | struct tegra_spi_plat *plat = dev_get_plat(bus); |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 318 | struct tegra20_sflash_priv *priv = dev_get_priv(bus); |
| 319 | |
| 320 | if (speed > plat->frequency) |
| 321 | speed = plat->frequency; |
| 322 | priv->freq = speed; |
| 323 | debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq); |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static int tegra20_sflash_set_mode(struct udevice *bus, uint mode) |
| 329 | { |
| 330 | struct tegra20_sflash_priv *priv = dev_get_priv(bus); |
| 331 | |
| 332 | priv->mode = mode; |
| 333 | debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode); |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static const struct dm_spi_ops tegra20_sflash_ops = { |
| 339 | .claim_bus = tegra20_sflash_claim_bus, |
| 340 | .xfer = tegra20_sflash_xfer, |
| 341 | .set_speed = tegra20_sflash_set_speed, |
| 342 | .set_mode = tegra20_sflash_set_mode, |
| 343 | .cs_info = tegra20_sflash_cs_info, |
| 344 | }; |
| 345 | |
| 346 | static const struct udevice_id tegra20_sflash_ids[] = { |
| 347 | { .compatible = "nvidia,tegra20-sflash" }, |
| 348 | { } |
| 349 | }; |
| 350 | |
| 351 | U_BOOT_DRIVER(tegra20_sflash) = { |
| 352 | .name = "tegra20_sflash", |
| 353 | .id = UCLASS_SPI, |
| 354 | .of_match = tegra20_sflash_ids, |
| 355 | .ops = &tegra20_sflash_ops, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 356 | .of_to_plat = tegra20_sflash_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 357 | .plat_auto = sizeof(struct tegra_spi_plat), |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 358 | .priv_auto = sizeof(struct tegra20_sflash_priv), |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 359 | .probe = tegra20_sflash_probe, |
| 360 | }; |