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