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