Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 2 | /* |
Jagan Teki | 86e99b9 | 2015-09-02 11:39:45 +0530 | [diff] [blame] | 3 | * (C) Copyright 2013 Xilinx, Inc. |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 4 | * (C) Copyright 2015 Jagan Teki <jteki@openedev.com> |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 5 | * |
| 6 | * Xilinx Zynq PS SPI controller driver (master mode only) |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 7 | */ |
| 8 | |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 9 | #include <common.h> |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 12 | #include <malloc.h> |
| 13 | #include <spi.h> |
Simon Glass | 1045315 | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 14 | #include <time.h> |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 15 | #include <asm/io.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame^] | 16 | #include <linux/delay.h> |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 17 | |
Jagan Teki | cdc9dd0 | 2015-06-27 00:51:34 +0530 | [diff] [blame] | 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 20 | /* zynq spi register bit masks ZYNQ_SPI_<REG>_<BIT>_MASK */ |
Jagan Teki | 736b4df | 2015-10-22 20:40:16 +0530 | [diff] [blame] | 21 | #define ZYNQ_SPI_CR_MSA_MASK BIT(15) /* Manual start enb */ |
| 22 | #define ZYNQ_SPI_CR_MCS_MASK BIT(14) /* Manual chip select */ |
Jagan Teki | 9cf2ffb | 2015-10-22 21:06:37 +0530 | [diff] [blame] | 23 | #define ZYNQ_SPI_CR_CS_MASK GENMASK(13, 10) /* Chip select */ |
| 24 | #define ZYNQ_SPI_CR_BAUD_MASK GENMASK(5, 3) /* Baud rate div */ |
Jagan Teki | 736b4df | 2015-10-22 20:40:16 +0530 | [diff] [blame] | 25 | #define ZYNQ_SPI_CR_CPHA_MASK BIT(2) /* Clock phase */ |
| 26 | #define ZYNQ_SPI_CR_CPOL_MASK BIT(1) /* Clock polarity */ |
| 27 | #define ZYNQ_SPI_CR_MSTREN_MASK BIT(0) /* Mode select */ |
| 28 | #define ZYNQ_SPI_IXR_RXNEMPTY_MASK BIT(4) /* RX_FIFO_not_empty */ |
| 29 | #define ZYNQ_SPI_IXR_TXOW_MASK BIT(2) /* TX_FIFO_not_full */ |
Jagan Teki | 9cf2ffb | 2015-10-22 21:06:37 +0530 | [diff] [blame] | 30 | #define ZYNQ_SPI_IXR_ALL_MASK GENMASK(6, 0) /* All IXR bits */ |
Jagan Teki | 736b4df | 2015-10-22 20:40:16 +0530 | [diff] [blame] | 31 | #define ZYNQ_SPI_ENR_SPI_EN_MASK BIT(0) /* SPI Enable */ |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 32 | |
Jagan Teki | 46ab8a6 | 2015-08-17 18:25:03 +0530 | [diff] [blame] | 33 | #define ZYNQ_SPI_CR_BAUD_MAX 8 /* Baud rate divisor max val */ |
| 34 | #define ZYNQ_SPI_CR_BAUD_SHIFT 3 /* Baud rate divisor shift */ |
| 35 | #define ZYNQ_SPI_CR_SS_SHIFT 10 /* Slave select shift */ |
| 36 | |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 37 | #define ZYNQ_SPI_FIFO_DEPTH 128 |
| 38 | #ifndef CONFIG_SYS_ZYNQ_SPI_WAIT |
| 39 | #define CONFIG_SYS_ZYNQ_SPI_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */ |
| 40 | #endif |
| 41 | |
| 42 | /* zynq spi register set */ |
| 43 | struct zynq_spi_regs { |
| 44 | u32 cr; /* 0x00 */ |
| 45 | u32 isr; /* 0x04 */ |
| 46 | u32 ier; /* 0x08 */ |
| 47 | u32 idr; /* 0x0C */ |
| 48 | u32 imr; /* 0x10 */ |
| 49 | u32 enr; /* 0x14 */ |
| 50 | u32 dr; /* 0x18 */ |
| 51 | u32 txdr; /* 0x1C */ |
| 52 | u32 rxdr; /* 0x20 */ |
| 53 | }; |
| 54 | |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 55 | |
| 56 | /* zynq spi platform data */ |
| 57 | struct zynq_spi_platdata { |
| 58 | struct zynq_spi_regs *regs; |
| 59 | u32 frequency; /* input frequency */ |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 60 | u32 speed_hz; |
Moritz Fischer | ac6991f | 2016-12-08 12:11:09 -0800 | [diff] [blame] | 61 | uint deactivate_delay_us; /* Delay to wait after deactivate */ |
| 62 | uint activate_delay_us; /* Delay to wait after activate */ |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 63 | }; |
| 64 | |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 65 | /* zynq spi priv */ |
| 66 | struct zynq_spi_priv { |
| 67 | struct zynq_spi_regs *regs; |
Jagan Teki | 1912699 | 2015-08-17 18:31:39 +0530 | [diff] [blame] | 68 | u8 cs; |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 69 | u8 mode; |
Moritz Fischer | ac6991f | 2016-12-08 12:11:09 -0800 | [diff] [blame] | 70 | ulong last_transaction_us; /* Time of last transaction end */ |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 71 | u8 fifo_depth; |
| 72 | u32 freq; /* required frequency */ |
| 73 | }; |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 74 | |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 75 | static int zynq_spi_ofdata_to_platdata(struct udevice *bus) |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 76 | { |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 77 | struct zynq_spi_platdata *plat = bus->platdata; |
Jagan Teki | cdc9dd0 | 2015-06-27 00:51:34 +0530 | [diff] [blame] | 78 | const void *blob = gd->fdt_blob; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 79 | int node = dev_of_offset(bus); |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 80 | |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 81 | plat->regs = (struct zynq_spi_regs *)devfdt_get_addr(bus); |
Jagan Teki | cdc9dd0 | 2015-06-27 00:51:34 +0530 | [diff] [blame] | 82 | |
| 83 | /* FIXME: Use 250MHz as a suitable default */ |
| 84 | plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", |
| 85 | 250000000); |
Moritz Fischer | ac6991f | 2016-12-08 12:11:09 -0800 | [diff] [blame] | 86 | plat->deactivate_delay_us = fdtdec_get_int(blob, node, |
| 87 | "spi-deactivate-delay", 0); |
| 88 | plat->activate_delay_us = fdtdec_get_int(blob, node, |
| 89 | "spi-activate-delay", 0); |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 90 | plat->speed_hz = plat->frequency / 2; |
| 91 | |
Michal Simek | 80fd979 | 2015-07-21 07:54:11 +0200 | [diff] [blame] | 92 | debug("%s: regs=%p max-frequency=%d\n", __func__, |
Jagan Teki | cdc9dd0 | 2015-06-27 00:51:34 +0530 | [diff] [blame] | 93 | plat->regs, plat->frequency); |
| 94 | |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static void zynq_spi_init_hw(struct zynq_spi_priv *priv) |
| 99 | { |
| 100 | struct zynq_spi_regs *regs = priv->regs; |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 101 | u32 confr; |
| 102 | |
| 103 | /* Disable SPI */ |
Michal Simek | 5f647c2 | 2016-09-01 12:51:27 +0200 | [diff] [blame] | 104 | confr = ZYNQ_SPI_ENR_SPI_EN_MASK; |
| 105 | writel(~confr, ®s->enr); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 106 | |
| 107 | /* Disable Interrupts */ |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 108 | writel(ZYNQ_SPI_IXR_ALL_MASK, ®s->idr); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 109 | |
| 110 | /* Clear RX FIFO */ |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 111 | while (readl(®s->isr) & |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 112 | ZYNQ_SPI_IXR_RXNEMPTY_MASK) |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 113 | readl(®s->rxdr); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 114 | |
| 115 | /* Clear Interrupts */ |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 116 | writel(ZYNQ_SPI_IXR_ALL_MASK, ®s->isr); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 117 | |
| 118 | /* Manual slave select and Auto start */ |
| 119 | confr = ZYNQ_SPI_CR_MCS_MASK | ZYNQ_SPI_CR_CS_MASK | |
| 120 | ZYNQ_SPI_CR_MSTREN_MASK; |
| 121 | confr &= ~ZYNQ_SPI_CR_MSA_MASK; |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 122 | writel(confr, ®s->cr); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 123 | |
| 124 | /* Enable SPI */ |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 125 | writel(ZYNQ_SPI_ENR_SPI_EN_MASK, ®s->enr); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 126 | } |
| 127 | |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 128 | static int zynq_spi_probe(struct udevice *bus) |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 129 | { |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 130 | struct zynq_spi_platdata *plat = dev_get_platdata(bus); |
| 131 | struct zynq_spi_priv *priv = dev_get_priv(bus); |
| 132 | |
| 133 | priv->regs = plat->regs; |
| 134 | priv->fifo_depth = ZYNQ_SPI_FIFO_DEPTH; |
| 135 | |
| 136 | /* init the zynq spi hw */ |
| 137 | zynq_spi_init_hw(priv); |
| 138 | |
| 139 | return 0; |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 140 | } |
| 141 | |
Jagan Teki | 1912699 | 2015-08-17 18:31:39 +0530 | [diff] [blame] | 142 | static void spi_cs_activate(struct udevice *dev) |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 143 | { |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 144 | struct udevice *bus = dev->parent; |
Moritz Fischer | ac6991f | 2016-12-08 12:11:09 -0800 | [diff] [blame] | 145 | struct zynq_spi_platdata *plat = bus->platdata; |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 146 | struct zynq_spi_priv *priv = dev_get_priv(bus); |
| 147 | struct zynq_spi_regs *regs = priv->regs; |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 148 | u32 cr; |
| 149 | |
Moritz Fischer | ac6991f | 2016-12-08 12:11:09 -0800 | [diff] [blame] | 150 | /* If it's too soon to do another transaction, wait */ |
| 151 | if (plat->deactivate_delay_us && priv->last_transaction_us) { |
| 152 | ulong delay_us; /* The delay completed so far */ |
| 153 | delay_us = timer_get_us() - priv->last_transaction_us; |
| 154 | if (delay_us < plat->deactivate_delay_us) |
| 155 | udelay(plat->deactivate_delay_us - delay_us); |
| 156 | } |
| 157 | |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 158 | clrbits_le32(®s->cr, ZYNQ_SPI_CR_CS_MASK); |
| 159 | cr = readl(®s->cr); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 160 | /* |
| 161 | * CS cal logic: CS[13:10] |
| 162 | * xxx0 - cs0 |
| 163 | * xx01 - cs1 |
| 164 | * x011 - cs2 |
| 165 | */ |
Jagan Teki | 1912699 | 2015-08-17 18:31:39 +0530 | [diff] [blame] | 166 | cr |= (~(1 << priv->cs) << ZYNQ_SPI_CR_SS_SHIFT) & ZYNQ_SPI_CR_CS_MASK; |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 167 | writel(cr, ®s->cr); |
Moritz Fischer | ac6991f | 2016-12-08 12:11:09 -0800 | [diff] [blame] | 168 | |
| 169 | if (plat->activate_delay_us) |
| 170 | udelay(plat->activate_delay_us); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 171 | } |
| 172 | |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 173 | static void spi_cs_deactivate(struct udevice *dev) |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 174 | { |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 175 | struct udevice *bus = dev->parent; |
Moritz Fischer | ac6991f | 2016-12-08 12:11:09 -0800 | [diff] [blame] | 176 | struct zynq_spi_platdata *plat = bus->platdata; |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 177 | struct zynq_spi_priv *priv = dev_get_priv(bus); |
| 178 | struct zynq_spi_regs *regs = priv->regs; |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 179 | |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 180 | setbits_le32(®s->cr, ZYNQ_SPI_CR_CS_MASK); |
Moritz Fischer | ac6991f | 2016-12-08 12:11:09 -0800 | [diff] [blame] | 181 | |
| 182 | /* Remember time of this transaction so we can honour the bus delay */ |
| 183 | if (plat->deactivate_delay_us) |
| 184 | priv->last_transaction_us = timer_get_us(); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 185 | } |
| 186 | |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 187 | static int zynq_spi_claim_bus(struct udevice *dev) |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 188 | { |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 189 | struct udevice *bus = dev->parent; |
| 190 | struct zynq_spi_priv *priv = dev_get_priv(bus); |
| 191 | struct zynq_spi_regs *regs = priv->regs; |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 192 | |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 193 | writel(ZYNQ_SPI_ENR_SPI_EN_MASK, ®s->enr); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 198 | static int zynq_spi_release_bus(struct udevice *dev) |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 199 | { |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 200 | struct udevice *bus = dev->parent; |
| 201 | struct zynq_spi_priv *priv = dev_get_priv(bus); |
| 202 | struct zynq_spi_regs *regs = priv->regs; |
Michal Simek | 5f647c2 | 2016-09-01 12:51:27 +0200 | [diff] [blame] | 203 | u32 confr; |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 204 | |
Michal Simek | 5f647c2 | 2016-09-01 12:51:27 +0200 | [diff] [blame] | 205 | confr = ZYNQ_SPI_ENR_SPI_EN_MASK; |
| 206 | writel(~confr, ®s->enr); |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 207 | |
| 208 | return 0; |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 209 | } |
| 210 | |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 211 | static int zynq_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 212 | const void *dout, void *din, unsigned long flags) |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 213 | { |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 214 | struct udevice *bus = dev->parent; |
| 215 | struct zynq_spi_priv *priv = dev_get_priv(bus); |
| 216 | struct zynq_spi_regs *regs = priv->regs; |
| 217 | struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 218 | u32 len = bitlen / 8; |
| 219 | u32 tx_len = len, rx_len = len, tx_tvl; |
| 220 | const u8 *tx_buf = dout; |
| 221 | u8 *rx_buf = din, buf; |
| 222 | u32 ts, status; |
| 223 | |
| 224 | debug("spi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n", |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 225 | bus->seq, slave_plat->cs, bitlen, len, flags); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 226 | |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 227 | if (bitlen % 8) { |
| 228 | debug("spi_xfer: Non byte aligned SPI transfer\n"); |
| 229 | return -1; |
| 230 | } |
| 231 | |
Jagan Teki | 1912699 | 2015-08-17 18:31:39 +0530 | [diff] [blame] | 232 | priv->cs = slave_plat->cs; |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 233 | if (flags & SPI_XFER_BEGIN) |
Jagan Teki | 1912699 | 2015-08-17 18:31:39 +0530 | [diff] [blame] | 234 | spi_cs_activate(dev); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 235 | |
| 236 | while (rx_len > 0) { |
| 237 | /* Write the data into TX FIFO - tx threshold is fifo_depth */ |
| 238 | tx_tvl = 0; |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 239 | while ((tx_tvl < priv->fifo_depth) && tx_len) { |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 240 | if (tx_buf) |
| 241 | buf = *tx_buf++; |
| 242 | else |
| 243 | buf = 0; |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 244 | writel(buf, ®s->txdr); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 245 | tx_len--; |
| 246 | tx_tvl++; |
| 247 | } |
| 248 | |
| 249 | /* Check TX FIFO completion */ |
| 250 | ts = get_timer(0); |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 251 | status = readl(®s->isr); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 252 | while (!(status & ZYNQ_SPI_IXR_TXOW_MASK)) { |
| 253 | if (get_timer(ts) > CONFIG_SYS_ZYNQ_SPI_WAIT) { |
| 254 | printf("spi_xfer: Timeout! TX FIFO not full\n"); |
| 255 | return -1; |
| 256 | } |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 257 | status = readl(®s->isr); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* Read the data from RX FIFO */ |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 261 | status = readl(®s->isr); |
Lad, Prabhakar | d299828 | 2016-07-30 22:28:24 +0100 | [diff] [blame] | 262 | while ((status & ZYNQ_SPI_IXR_RXNEMPTY_MASK) && rx_len) { |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 263 | buf = readl(®s->rxdr); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 264 | if (rx_buf) |
| 265 | *rx_buf++ = buf; |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 266 | status = readl(®s->isr); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 267 | rx_len--; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if (flags & SPI_XFER_END) |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 272 | spi_cs_deactivate(dev); |
Jagannadha Sutradharudu Teki | 1465d05 | 2013-07-29 23:45:16 +0530 | [diff] [blame] | 273 | |
| 274 | return 0; |
| 275 | } |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 276 | |
| 277 | static int zynq_spi_set_speed(struct udevice *bus, uint speed) |
| 278 | { |
| 279 | struct zynq_spi_platdata *plat = bus->platdata; |
| 280 | struct zynq_spi_priv *priv = dev_get_priv(bus); |
| 281 | struct zynq_spi_regs *regs = priv->regs; |
| 282 | uint32_t confr; |
| 283 | u8 baud_rate_val = 0; |
| 284 | |
| 285 | if (speed > plat->frequency) |
| 286 | speed = plat->frequency; |
| 287 | |
| 288 | /* Set the clock frequency */ |
| 289 | confr = readl(®s->cr); |
| 290 | if (speed == 0) { |
| 291 | /* Set baudrate x8, if the freq is 0 */ |
| 292 | baud_rate_val = 0x2; |
| 293 | } else if (plat->speed_hz != speed) { |
Jagan Teki | 46ab8a6 | 2015-08-17 18:25:03 +0530 | [diff] [blame] | 294 | while ((baud_rate_val < ZYNQ_SPI_CR_BAUD_MAX) && |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 295 | ((plat->frequency / |
| 296 | (2 << baud_rate_val)) > speed)) |
| 297 | baud_rate_val++; |
| 298 | plat->speed_hz = speed / (2 << baud_rate_val); |
| 299 | } |
Jagan Teki | dda6241 | 2015-08-17 18:27:47 +0530 | [diff] [blame] | 300 | confr &= ~ZYNQ_SPI_CR_BAUD_MASK; |
Jagan Teki | 46ab8a6 | 2015-08-17 18:25:03 +0530 | [diff] [blame] | 301 | confr |= (baud_rate_val << ZYNQ_SPI_CR_BAUD_SHIFT); |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 302 | |
| 303 | writel(confr, ®s->cr); |
| 304 | priv->freq = speed; |
| 305 | |
Jagan Teki | a22bba8 | 2015-09-08 01:38:50 +0530 | [diff] [blame] | 306 | debug("zynq_spi_set_speed: regs=%p, speed=%d\n", |
| 307 | priv->regs, priv->freq); |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int zynq_spi_set_mode(struct udevice *bus, uint mode) |
| 313 | { |
| 314 | struct zynq_spi_priv *priv = dev_get_priv(bus); |
| 315 | struct zynq_spi_regs *regs = priv->regs; |
| 316 | uint32_t confr; |
| 317 | |
| 318 | /* Set the SPI Clock phase and polarities */ |
| 319 | confr = readl(®s->cr); |
| 320 | confr &= ~(ZYNQ_SPI_CR_CPHA_MASK | ZYNQ_SPI_CR_CPOL_MASK); |
| 321 | |
Jagan Teki | a22bba8 | 2015-09-08 01:38:50 +0530 | [diff] [blame] | 322 | if (mode & SPI_CPHA) |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 323 | confr |= ZYNQ_SPI_CR_CPHA_MASK; |
Jagan Teki | a22bba8 | 2015-09-08 01:38:50 +0530 | [diff] [blame] | 324 | if (mode & SPI_CPOL) |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 325 | confr |= ZYNQ_SPI_CR_CPOL_MASK; |
| 326 | |
| 327 | writel(confr, ®s->cr); |
| 328 | priv->mode = mode; |
| 329 | |
| 330 | debug("zynq_spi_set_mode: regs=%p, mode=%d\n", priv->regs, priv->mode); |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static const struct dm_spi_ops zynq_spi_ops = { |
| 336 | .claim_bus = zynq_spi_claim_bus, |
| 337 | .release_bus = zynq_spi_release_bus, |
| 338 | .xfer = zynq_spi_xfer, |
| 339 | .set_speed = zynq_spi_set_speed, |
| 340 | .set_mode = zynq_spi_set_mode, |
| 341 | }; |
| 342 | |
| 343 | static const struct udevice_id zynq_spi_ids[] = { |
Michal Simek | 40b383f | 2015-07-22 10:47:33 +0200 | [diff] [blame] | 344 | { .compatible = "xlnx,zynq-spi-r1p6" }, |
Michal Simek | 23ef5ae | 2015-12-07 13:06:54 +0100 | [diff] [blame] | 345 | { .compatible = "cdns,spi-r1p6" }, |
Jagan Teki | b1c82da | 2015-06-27 00:51:31 +0530 | [diff] [blame] | 346 | { } |
| 347 | }; |
| 348 | |
| 349 | U_BOOT_DRIVER(zynq_spi) = { |
| 350 | .name = "zynq_spi", |
| 351 | .id = UCLASS_SPI, |
| 352 | .of_match = zynq_spi_ids, |
| 353 | .ops = &zynq_spi_ops, |
| 354 | .ofdata_to_platdata = zynq_spi_ofdata_to_platdata, |
| 355 | .platdata_auto_alloc_size = sizeof(struct zynq_spi_platdata), |
| 356 | .priv_auto_alloc_size = sizeof(struct zynq_spi_priv), |
| 357 | .probe = zynq_spi_probe, |
| 358 | }; |