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