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