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