Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

spi: add multi-lane support

Merge series from David Lechner <dlechner@baylibre.com>:

This series is adding support for SPI controllers and peripherals that
have multiple SPI data lanes (data lanes being independent sets of
SDI/SDO lines, each with their own serializer/deserializer).

This series covers this specific use case:

+--------------+ +---------+
| SPI | | SPI |
| Controller | | ADC |
| | | |
| CS0 |--->| CS |
| SCLK |--->| SCLK |
| SDO |--->| SDI |
| SDI0 |<---| SDOA |
| SDI1 |<---| SDOB |
| SDI2 |<---| SDOC |
| SDI3 |<---| SDOD |
+--------------+ +--------+

The ADC is a simultaneous sampling ADC that can convert 4 samples at the
same time. It has 4 data output lines (SDOA-D) that each contain the
data of one of the 4 channels. So it requires a SPI controller with 4
separate deserializers in order to receive all of the information at the
same time.

This should also work for the use case in [1] as well. (Some of the
patches in this series were already submitted there). In that case the
SPI controller is used kind of like it is two separate SPI controllers,
each with its own chip select, clock, and data lines.

[1]: https://lore.kernel.org/linux-spi/20250616220054.3968946-1-sean.anderson@linux.dev/

The DT bindings are a fairly straight-forward mapping of which pins on
the peripheral are connected to which pins on the controller. The SPI
core code parses this and makes the information available to drivers.
When a peripheral driver sees that multiple data lanes are wired up, it
can chose to use them when sending messages.

The SPI message API is a bit higher-level than just specifying the
number of data lines for a SPI transfer though. I did some research on
other SPI controllers that have this feature. They tend to be the kind
meant for connecting to two flash memory chips at the same time but can
be used more generically as well. They generally have the option to
either use one lane at a time (Sean's use case), or can mirror the same
data on multiple lanes (no users of this yet) or can perform striping
of a single data FIFO/DMA stream to/from the two lanes (our use case).

For now, the API assumes that if you want to do mirror/striping, then
you want to use all available data lanes. Otherwise, it just uses the
first data lane for "normal" SPI transfers.

+641 -27
+3 -2
Documentation/devicetree/bindings/display/panel/sitronix,st7789v.yaml
··· 34 34 spi-cpol: true 35 35 36 36 spi-rx-bus-width: 37 - minimum: 0 38 - maximum: 1 37 + items: 38 + minimum: 0 39 + maximum: 1 39 40 40 41 dc-gpios: 41 42 maxItems: 1
+41 -1
Documentation/devicetree/bindings/iio/adc/adi,ad4030.yaml
··· 37 37 maximum: 102040816 38 38 39 39 spi-rx-bus-width: 40 - enum: [1, 2, 4] 40 + maxItems: 2 41 + # all lanes must have the same width 42 + oneOf: 43 + - contains: 44 + const: 1 45 + - contains: 46 + const: 2 47 + - contains: 48 + const: 4 41 49 42 50 vdd-5v-supply: true 43 51 vdd-1v8-supply: true ··· 96 88 97 89 unevaluatedProperties: false 98 90 91 + allOf: 92 + - if: 93 + properties: 94 + compatible: 95 + enum: 96 + - adi,ad4030-24 97 + - adi,ad4032-24 98 + then: 99 + properties: 100 + spi-rx-bus-width: 101 + maxItems: 1 102 + 99 103 examples: 100 104 - | 101 105 #include <dt-bindings/gpio/gpio.h> ··· 120 100 compatible = "adi,ad4030-24"; 121 101 reg = <0>; 122 102 spi-max-frequency = <80000000>; 103 + vdd-5v-supply = <&supply_5V>; 104 + vdd-1v8-supply = <&supply_1_8V>; 105 + vio-supply = <&supply_1_8V>; 106 + ref-supply = <&supply_5V>; 107 + cnv-gpios = <&gpio0 0 GPIO_ACTIVE_HIGH>; 108 + reset-gpios = <&gpio0 1 GPIO_ACTIVE_LOW>; 109 + }; 110 + }; 111 + - | 112 + #include <dt-bindings/gpio/gpio.h> 113 + 114 + spi { 115 + #address-cells = <1>; 116 + #size-cells = <0>; 117 + 118 + adc@0 { 119 + compatible = "adi,ad4630-24"; 120 + reg = <0>; 121 + spi-max-frequency = <80000000>; 122 + spi-rx-bus-width = <4>, <4>; 123 123 vdd-5v-supply = <&supply_5V>; 124 124 vdd-1v8-supply = <&supply_1_8V>; 125 125 vio-supply = <&supply_1_8V>;
+3 -2
Documentation/devicetree/bindings/iio/adc/adi,ad4695.yaml
··· 38 38 spi-cpha: true 39 39 40 40 spi-rx-bus-width: 41 - minimum: 1 42 - maximum: 4 41 + items: 42 + minimum: 1 43 + maximum: 4 43 44 44 45 avdd-supply: 45 46 description: Analog power supply.
+15
Documentation/devicetree/bindings/spi/adi,axi-spi-engine.yaml
··· 70 70 71 71 unevaluatedProperties: false 72 72 73 + patternProperties: 74 + "^.*@[0-9a-f]+": 75 + type: object 76 + 77 + properties: 78 + spi-rx-bus-width: 79 + maxItems: 8 80 + items: 81 + enum: [0, 1] 82 + 83 + spi-tx-bus-width: 84 + maxItems: 8 85 + items: 86 + enum: [0, 1] 87 + 73 88 examples: 74 89 - | 75 90 spi@44a00000 {
+4 -2
Documentation/devicetree/bindings/spi/allwinner,sun4i-a10-spi.yaml
··· 55 55 maximum: 4 56 56 57 57 spi-rx-bus-width: 58 - const: 1 58 + items: 59 + - const: 1 59 60 60 61 spi-tx-bus-width: 61 - const: 1 62 + items: 63 + - const: 1 62 64 63 65 required: 64 66 - compatible
+4 -2
Documentation/devicetree/bindings/spi/allwinner,sun6i-a31-spi.yaml
··· 81 81 maximum: 4 82 82 83 83 spi-rx-bus-width: 84 - const: 1 84 + items: 85 + - const: 1 85 86 86 87 spi-tx-bus-width: 87 - const: 1 88 + items: 89 + - const: 1 88 90 89 91 required: 90 92 - compatible
+4 -2
Documentation/devicetree/bindings/spi/andestech,ae350-spi.yaml
··· 45 45 46 46 properties: 47 47 spi-rx-bus-width: 48 - enum: [1, 4] 48 + items: 49 + - enum: [1, 4] 49 50 50 51 spi-tx-bus-width: 51 - enum: [1, 4] 52 + items: 53 + - enum: [1, 4] 52 54 53 55 allOf: 54 56 - $ref: spi-controller.yaml#
+4 -2
Documentation/devicetree/bindings/spi/nvidia,tegra210-quad.yaml
··· 54 54 55 55 properties: 56 56 spi-rx-bus-width: 57 - enum: [1, 2, 4] 57 + items: 58 + - enum: [1, 2, 4] 58 59 59 60 spi-tx-bus-width: 60 - enum: [1, 2, 4] 61 + items: 62 + - enum: [1, 2, 4] 61 63 62 64 required: 63 65 - compatible
+34 -6
Documentation/devicetree/bindings/spi/spi-peripheral-props.yaml
··· 64 64 description: 65 65 Bus width to the SPI bus used for read transfers. 66 66 If 0 is provided, then no RX will be possible on this device. 67 - $ref: /schemas/types.yaml#/definitions/uint32 68 - enum: [0, 1, 2, 4, 8] 69 - default: 1 67 + 68 + Some SPI peripherals and controllers may have multiple data lanes for 69 + receiving two or more words at the same time. If this is the case, each 70 + index in the array represents the lane on both the SPI peripheral and 71 + controller. Additional mapping properties may be needed if a lane is 72 + skipped on either side. 73 + $ref: /schemas/types.yaml#/definitions/uint32-array 74 + items: 75 + enum: [0, 1, 2, 4, 8] 76 + default: [1] 77 + 78 + spi-rx-lane-map: 79 + description: Mapping of peripheral SDO lanes to controller SDI lanes. 80 + Each index in the array represents a peripheral SDO lane, and the value 81 + at that index represents the corresponding controller SDI lane. 82 + $ref: /schemas/types.yaml#/definitions/uint32-array 83 + default: [0, 1, 2, 3, 4, 5, 6, 7] 70 84 71 85 spi-rx-delay-us: 72 86 description: ··· 95 81 description: 96 82 Bus width to the SPI bus used for write transfers. 97 83 If 0 is provided, then no TX will be possible on this device. 98 - $ref: /schemas/types.yaml#/definitions/uint32 99 - enum: [0, 1, 2, 4, 8] 100 - default: 1 84 + 85 + Some SPI peripherals and controllers may have multiple data lanes for 86 + transmitting two or more words at the same time. If this is the case, each 87 + index in the array represents the lane on both the SPI peripheral and 88 + controller. Additional mapping properties may be needed if a lane is 89 + skipped on either side. 90 + $ref: /schemas/types.yaml#/definitions/uint32-array 91 + items: 92 + enum: [0, 1, 2, 4, 8] 93 + default: [1] 94 + 95 + spi-tx-lane-map: 96 + description: Mapping of peripheral SDI lanes to controller SDO lanes. 97 + Each index in the array represents a peripheral SDI lane, and the value 98 + at that index represents the corresponding controller SDO lane. 99 + $ref: /schemas/types.yaml#/definitions/uint32-array 100 + default: [0, 1, 2, 3, 4, 5, 6, 7] 101 101 102 102 spi-tx-delay-us: 103 103 description:
+1
Documentation/spi/index.rst
··· 9 9 10 10 spi-summary 11 11 spidev 12 + multiple-data-lanes 12 13 butterfly 13 14 spi-lm70llp 14 15 spi-sc18is602
+217
Documentation/spi/multiple-data-lanes.rst
··· 1 + ==================================== 2 + SPI devices with multiple data lanes 3 + ==================================== 4 + 5 + Some specialized SPI controllers and peripherals support multiple data lanes 6 + that allow reading more than one word at a time in parallel. This is different 7 + from dual/quad/octal SPI where multiple bits of a single word are transferred 8 + simultaneously. 9 + 10 + For example, controllers that support parallel flash memories have this feature 11 + as do some simultaneous-sampling ADCs where each channel has its own data lane. 12 + 13 + --------------------- 14 + Describing the wiring 15 + --------------------- 16 + 17 + The ``spi-tx-bus-width`` and ``spi-rx-bus-width`` properties in the devicetree 18 + are used to describe how many data lanes are connected between the controller 19 + and how wide each lane is. The number of items in the array indicates how many 20 + lanes there are, and the value of each item indicates how many bits wide that 21 + lane is. 22 + 23 + For example, a dual-simultaneous-sampling ADC with two 4-bit lanes might be 24 + wired up like this:: 25 + 26 + +--------------+ +----------+ 27 + | SPI | | AD4630 | 28 + | Controller | | ADC | 29 + | | | | 30 + | CS0 |--->| CS | 31 + | SCK |--->| SCK | 32 + | SDO |--->| SDI | 33 + | | | | 34 + | SDIA0 |<---| SDOA0 | 35 + | SDIA1 |<---| SDOA1 | 36 + | SDIA2 |<---| SDOA2 | 37 + | SDIA3 |<---| SDOA3 | 38 + | | | | 39 + | SDIB0 |<---| SDOB0 | 40 + | SDIB1 |<---| SDOB1 | 41 + | SDIB2 |<---| SDOB2 | 42 + | SDIB3 |<---| SDOB3 | 43 + | | | | 44 + +--------------+ +----------+ 45 + 46 + It is described in a devicetree like this:: 47 + 48 + spi { 49 + compatible = "my,spi-controller"; 50 + 51 + ... 52 + 53 + adc@0 { 54 + compatible = "adi,ad4630"; 55 + reg = <0>; 56 + ... 57 + spi-rx-bus-width = <4>, <4>; /* 2 lanes of 4 bits each */ 58 + ... 59 + }; 60 + }; 61 + 62 + In most cases, lanes will be wired up symmetrically (A to A, B to B, etc). If 63 + this isn't the case, extra ``spi-rx-lane-map`` and ``spi-tx-lane-map`` 64 + properties are needed to provide a mapping between controller lanes and the 65 + physical lane wires. 66 + 67 + Here is an example where a multi-lane SPI controller has each lane wired to 68 + separate single-lane peripherals:: 69 + 70 + +--------------+ +----------+ 71 + | SPI | | Thing 1 | 72 + | Controller | | | 73 + | | | | 74 + | CS0 |--->| CS | 75 + | SDO0 |--->| SDI | 76 + | SDI0 |<---| SDO | 77 + | SCLK0 |--->| SCLK | 78 + | | | | 79 + | | +----------+ 80 + | | 81 + | | +----------+ 82 + | | | Thing 2 | 83 + | | | | 84 + | CS1 |--->| CS | 85 + | SDO1 |--->| SDI | 86 + | SDI1 |<---| SDO | 87 + | SCLK1 |--->| SCLK | 88 + | | | | 89 + +--------------+ +----------+ 90 + 91 + This is described in a devicetree like this:: 92 + 93 + spi { 94 + compatible = "my,spi-controller"; 95 + 96 + ... 97 + 98 + thing1@0 { 99 + compatible = "my,thing1"; 100 + reg = <0>; 101 + ... 102 + }; 103 + 104 + thing2@1 { 105 + compatible = "my,thing2"; 106 + reg = <1>; 107 + ... 108 + spi-tx-lane-map = <1>; /* lane 0 is not used, lane 1 is used for tx wire */ 109 + spi-rx-lane-map = <1>; /* lane 0 is not used, lane 1 is used for rx wire */ 110 + ... 111 + }; 112 + }; 113 + 114 + 115 + The default values of ``spi-rx-bus-width`` and ``spi-tx-bus-width`` are ``<1>``, 116 + so these properties can still be omitted even when ``spi-rx-lane-map`` and 117 + ``spi-tx-lane-map`` are used. 118 + 119 + ---------------------------- 120 + Usage in a peripheral driver 121 + ---------------------------- 122 + 123 + These types of SPI controllers generally do not support arbitrary use of the 124 + multiple lanes. Instead, they operate in one of a few defined modes. Peripheral 125 + drivers should set the :c:type:`struct spi_transfer.multi_lane_mode <spi_transfer>` 126 + field to indicate which mode they want to use for a given transfer. 127 + 128 + The possible values for this field have the following semantics: 129 + 130 + - :c:macro:`SPI_MULTI_BUS_MODE_SINGLE`: Only use the first lane. Other lanes are 131 + ignored. This means that it is operating just like a conventional SPI 132 + peripheral. This is the default, so it does not need to be explicitly set. 133 + 134 + Example:: 135 + 136 + tx_buf[0] = 0x88; 137 + 138 + struct spi_transfer xfer = { 139 + .tx_buf = tx_buf, 140 + .len = 1, 141 + }; 142 + 143 + spi_sync_transfer(spi, &xfer, 1); 144 + 145 + Assuming the controller is sending the MSB first, the sequence of bits 146 + sent over the tx wire would be (right-most bit is sent first):: 147 + 148 + controller > data bits > peripheral 149 + ---------- ---------------- ---------- 150 + SDO 0 0-0-0-1-0-0-0-1 SDI 0 151 + 152 + - :c:macro:`SPI_MULTI_BUS_MODE_MIRROR`: Send a single data word over all of the 153 + lanes at the same time. This only makes sense for writes and not 154 + for reads. 155 + 156 + Example:: 157 + 158 + tx_buf[0] = 0x88; 159 + 160 + struct spi_transfer xfer = { 161 + .tx_buf = tx_buf, 162 + .len = 1, 163 + .multi_lane_mode = SPI_MULTI_BUS_MODE_MIRROR, 164 + }; 165 + 166 + spi_sync_transfer(spi, &xfer, 1); 167 + 168 + The data is mirrored on each tx wire:: 169 + 170 + controller > data bits > peripheral 171 + ---------- ---------------- ---------- 172 + SDO 0 0-0-0-1-0-0-0-1 SDI 0 173 + SDO 1 0-0-0-1-0-0-0-1 SDI 1 174 + 175 + - :c:macro:`SPI_MULTI_BUS_MODE_STRIPE`: Send or receive two different data words 176 + at the same time, one on each lane. This means that the buffer needs to be 177 + sized to hold data for all lanes. Data is interleaved in the buffer, with 178 + the first word corresponding to lane 0, the second to lane 1, and so on. 179 + Once the last lane is used, the next word in the buffer corresponds to lane 180 + 0 again. Accordingly, the buffer size must be a multiple of the number of 181 + lanes. This mode works for both reads and writes. 182 + 183 + Example:: 184 + 185 + struct spi_transfer xfer = { 186 + .rx_buf = rx_buf, 187 + .len = 2, 188 + .multi_lane_mode = SPI_MULTI_BUS_MODE_STRIPE, 189 + }; 190 + 191 + spi_sync_transfer(spi, &xfer, 1); 192 + 193 + Each rx wire has a different data word sent simultaneously:: 194 + 195 + controller < data bits < peripheral 196 + ---------- ---------------- ---------- 197 + SDI 0 0-0-0-1-0-0-0-1 SDO 0 198 + SDI 1 1-0-0-0-1-0-0-0 SDO 1 199 + 200 + After the transfer, ``rx_buf[0] == 0x11`` (word from SDO 0) and 201 + ``rx_buf[1] == 0x88`` (word from SDO 1). 202 + 203 + 204 + ----------------------------- 205 + SPI controller driver support 206 + ----------------------------- 207 + 208 + To support multiple data lanes, SPI controller drivers need to set 209 + :c:type:`struct spi_controller.num_data_lanes <spi_controller>` to a value 210 + greater than 1. 211 + 212 + Then the part of the driver that handles SPI transfers needs to check the 213 + :c:type:`struct spi_transfer.multi_lane_mode <spi_transfer>` field and implement 214 + the appropriate behavior for each supported mode and return an error for 215 + unsupported modes. 216 + 217 + The core SPI code should handle the rest.
+141 -4
drivers/spi/spi-axi-spi-engine.c
··· 23 23 #include <linux/spi/spi.h> 24 24 #include <trace/events/spi.h> 25 25 26 + #define SPI_ENGINE_REG_DATA_WIDTH 0x0C 27 + #define SPI_ENGINE_REG_DATA_WIDTH_NUM_OF_SDIO_MASK GENMASK(23, 16) 28 + #define SPI_ENGINE_REG_DATA_WIDTH_MASK GENMASK(15, 0) 26 29 #define SPI_ENGINE_REG_OFFLOAD_MEM_ADDR_WIDTH 0x10 27 30 #define SPI_ENGINE_REG_RESET 0x40 28 31 ··· 78 75 #define SPI_ENGINE_CMD_REG_CLK_DIV 0x0 79 76 #define SPI_ENGINE_CMD_REG_CONFIG 0x1 80 77 #define SPI_ENGINE_CMD_REG_XFER_BITS 0x2 78 + #define SPI_ENGINE_CMD_REG_SDI_MASK 0x3 79 + #define SPI_ENGINE_CMD_REG_SDO_MASK 0x4 81 80 82 81 #define SPI_ENGINE_MISC_SYNC 0x0 83 82 #define SPI_ENGINE_MISC_SLEEP 0x1 ··· 109 104 /* default sizes - can be changed when SPI Engine firmware is compiled */ 110 105 #define SPI_ENGINE_OFFLOAD_CMD_FIFO_SIZE 16 111 106 #define SPI_ENGINE_OFFLOAD_SDO_FIFO_SIZE 16 107 + 108 + /* Extending SPI_MULTI_LANE_MODE values for optimizing messages. */ 109 + #define SPI_ENGINE_MULTI_BUS_MODE_UNKNOWN -1 110 + #define SPI_ENGINE_MULTI_BUS_MODE_CONFLICTING -2 112 111 113 112 struct spi_engine_program { 114 113 unsigned int length; ··· 151 142 unsigned long flags; 152 143 unsigned int offload_num; 153 144 unsigned int spi_mode_config; 145 + unsigned int multi_lane_mode; 146 + u8 rx_primary_lane_mask; 147 + u8 tx_primary_lane_mask; 148 + u8 rx_all_lanes_mask; 149 + u8 tx_all_lanes_mask; 154 150 u8 bits_per_word; 155 151 }; 156 152 ··· 178 164 u32 offload_caps; 179 165 bool offload_requires_sync; 180 166 }; 167 + 168 + static void spi_engine_primary_lane_flag(struct spi_device *spi, 169 + u8 *rx_lane_flags, u8 *tx_lane_flags) 170 + { 171 + *rx_lane_flags = BIT(spi->rx_lane_map[0]); 172 + *tx_lane_flags = BIT(spi->tx_lane_map[0]); 173 + } 174 + 175 + static void spi_engine_all_lanes_flags(struct spi_device *spi, 176 + u8 *rx_lane_flags, u8 *tx_lane_flags) 177 + { 178 + int i; 179 + 180 + for (i = 0; i < spi->num_rx_lanes; i++) 181 + *rx_lane_flags |= BIT(spi->rx_lane_map[i]); 182 + 183 + for (i = 0; i < spi->num_tx_lanes; i++) 184 + *tx_lane_flags |= BIT(spi->tx_lane_map[i]); 185 + } 181 186 182 187 static void spi_engine_program_add_cmd(struct spi_engine_program *p, 183 188 bool dry, uint16_t cmd) ··· 226 193 } 227 194 228 195 static void spi_engine_gen_xfer(struct spi_engine_program *p, bool dry, 229 - struct spi_transfer *xfer) 196 + struct spi_transfer *xfer, u32 num_lanes) 230 197 { 231 198 unsigned int len; 232 199 ··· 236 203 len = xfer->len / 2; 237 204 else 238 205 len = xfer->len / 4; 206 + 207 + if (xfer->multi_lane_mode == SPI_MULTI_LANE_MODE_STRIPE) 208 + len /= num_lanes; 239 209 240 210 while (len) { 241 211 unsigned int n = min(len, 256U); ··· 305 269 { 306 270 unsigned int clk_div, max_hz = msg->spi->controller->max_speed_hz; 307 271 struct spi_transfer *xfer; 272 + int multi_lane_mode = SPI_ENGINE_MULTI_BUS_MODE_UNKNOWN; 308 273 u8 min_bits_per_word = U8_MAX; 309 274 u8 max_bits_per_word = 0; 310 275 ··· 321 284 min_bits_per_word = min(min_bits_per_word, xfer->bits_per_word); 322 285 max_bits_per_word = max(max_bits_per_word, xfer->bits_per_word); 323 286 } 287 + 288 + if (xfer->rx_buf || xfer->offload_flags & SPI_OFFLOAD_XFER_RX_STREAM || 289 + xfer->tx_buf || xfer->offload_flags & SPI_OFFLOAD_XFER_TX_STREAM) { 290 + switch (xfer->multi_lane_mode) { 291 + case SPI_MULTI_LANE_MODE_SINGLE: 292 + case SPI_MULTI_LANE_MODE_STRIPE: 293 + break; 294 + default: 295 + /* Other modes, like mirror not supported */ 296 + return -EINVAL; 297 + } 298 + 299 + /* If all xfers have the same multi-lane mode, we can optimize. */ 300 + if (multi_lane_mode == SPI_ENGINE_MULTI_BUS_MODE_UNKNOWN) 301 + multi_lane_mode = xfer->multi_lane_mode; 302 + else if (multi_lane_mode != xfer->multi_lane_mode) 303 + multi_lane_mode = SPI_ENGINE_MULTI_BUS_MODE_CONFLICTING; 304 + } 324 305 } 325 306 326 307 /* ··· 352 297 priv->bits_per_word = min_bits_per_word; 353 298 else 354 299 priv->bits_per_word = 0; 300 + 301 + priv->multi_lane_mode = multi_lane_mode; 302 + spi_engine_primary_lane_flag(msg->spi, 303 + &priv->rx_primary_lane_mask, 304 + &priv->tx_primary_lane_mask); 305 + spi_engine_all_lanes_flags(msg->spi, 306 + &priv->rx_all_lanes_mask, 307 + &priv->tx_all_lanes_mask); 355 308 } 356 309 357 310 return 0; ··· 373 310 struct spi_engine_offload *priv; 374 311 struct spi_transfer *xfer; 375 312 int clk_div, new_clk_div, inst_ns; 313 + int prev_multi_lane_mode = SPI_MULTI_LANE_MODE_SINGLE; 376 314 bool keep_cs = false; 377 315 u8 bits_per_word = 0; 378 316 ··· 398 334 * in the same way. 399 335 */ 400 336 bits_per_word = priv->bits_per_word; 337 + prev_multi_lane_mode = priv->multi_lane_mode; 401 338 } else { 402 339 spi_engine_program_add_cmd(p, dry, 403 340 SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_CONFIG, ··· 409 344 spi_engine_gen_cs(p, dry, spi, !xfer->cs_off); 410 345 411 346 list_for_each_entry(xfer, &msg->transfers, transfer_list) { 347 + if (xfer->rx_buf || xfer->offload_flags & SPI_OFFLOAD_XFER_RX_STREAM || 348 + xfer->tx_buf || xfer->offload_flags & SPI_OFFLOAD_XFER_TX_STREAM) { 349 + if (xfer->multi_lane_mode != prev_multi_lane_mode) { 350 + u8 tx_lane_flags, rx_lane_flags; 351 + 352 + if (xfer->multi_lane_mode == SPI_MULTI_LANE_MODE_STRIPE) 353 + spi_engine_all_lanes_flags(spi, &rx_lane_flags, 354 + &tx_lane_flags); 355 + else 356 + spi_engine_primary_lane_flag(spi, &rx_lane_flags, 357 + &tx_lane_flags); 358 + 359 + spi_engine_program_add_cmd(p, dry, 360 + SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_SDI_MASK, 361 + rx_lane_flags)); 362 + spi_engine_program_add_cmd(p, dry, 363 + SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_SDO_MASK, 364 + tx_lane_flags)); 365 + } 366 + prev_multi_lane_mode = xfer->multi_lane_mode; 367 + } 368 + 412 369 new_clk_div = host->max_speed_hz / xfer->effective_speed_hz; 413 370 if (new_clk_div != clk_div) { 414 371 clk_div = new_clk_div; ··· 447 360 bits_per_word)); 448 361 } 449 362 450 - spi_engine_gen_xfer(p, dry, xfer); 363 + spi_engine_gen_xfer(p, dry, xfer, spi->num_rx_lanes); 451 364 spi_engine_gen_sleep(p, dry, spi_delay_to_ns(&xfer->delay, xfer), 452 365 inst_ns, xfer->effective_speed_hz); 453 366 ··· 481 394 if (clk_div != 1) 482 395 spi_engine_program_add_cmd(p, dry, 483 396 SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_CLK_DIV, 0)); 397 + 398 + /* Restore single lane mode unless offload disable will restore it later. */ 399 + if (prev_multi_lane_mode == SPI_MULTI_LANE_MODE_STRIPE && 400 + (!msg->offload || priv->multi_lane_mode != SPI_MULTI_LANE_MODE_STRIPE)) { 401 + u8 rx_lane_flags, tx_lane_flags; 402 + 403 + spi_engine_primary_lane_flag(spi, &rx_lane_flags, &tx_lane_flags); 404 + 405 + spi_engine_program_add_cmd(p, dry, 406 + SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_SDI_MASK, rx_lane_flags)); 407 + spi_engine_program_add_cmd(p, dry, 408 + SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_SDO_MASK, tx_lane_flags)); 409 + } 484 410 } 485 411 486 412 static void spi_engine_xfer_next(struct spi_message *msg, ··· 899 799 writel_relaxed(SPI_ENGINE_CMD_CS_INV(spi_engine->cs_inv), 900 800 spi_engine->base + SPI_ENGINE_REG_CMD_FIFO); 901 801 802 + if (host->num_data_lanes > 1) { 803 + u8 rx_lane_flags, tx_lane_flags; 804 + 805 + spi_engine_primary_lane_flag(device, &rx_lane_flags, &tx_lane_flags); 806 + 807 + writel_relaxed(SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_SDI_MASK, 808 + rx_lane_flags), 809 + spi_engine->base + SPI_ENGINE_REG_CMD_FIFO); 810 + writel_relaxed(SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_SDO_MASK, 811 + tx_lane_flags), 812 + spi_engine->base + SPI_ENGINE_REG_CMD_FIFO); 813 + } 814 + 902 815 /* 903 816 * In addition to setting the flags, we have to do a CS assert command 904 817 * to make the new setting actually take effect. ··· 1015 902 priv->bits_per_word), 1016 903 spi_engine->base + SPI_ENGINE_REG_CMD_FIFO); 1017 904 905 + if (priv->multi_lane_mode == SPI_MULTI_LANE_MODE_STRIPE) { 906 + writel_relaxed(SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_SDI_MASK, 907 + priv->rx_all_lanes_mask), 908 + spi_engine->base + SPI_ENGINE_REG_CMD_FIFO); 909 + writel_relaxed(SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_SDO_MASK, 910 + priv->tx_all_lanes_mask), 911 + spi_engine->base + SPI_ENGINE_REG_CMD_FIFO); 912 + } 913 + 1018 914 writel_relaxed(SPI_ENGINE_CMD_SYNC(1), 1019 915 spi_engine->base + SPI_ENGINE_REG_CMD_FIFO); 1020 916 ··· 1051 929 reg &= ~SPI_ENGINE_OFFLOAD_CTRL_ENABLE; 1052 930 writel_relaxed(reg, spi_engine->base + 1053 931 SPI_ENGINE_REG_OFFLOAD_CTRL(priv->offload_num)); 932 + 933 + /* Restore single-lane mode. */ 934 + if (priv->multi_lane_mode == SPI_MULTI_LANE_MODE_STRIPE) { 935 + writel_relaxed(SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_SDI_MASK, 936 + priv->rx_primary_lane_mask), 937 + spi_engine->base + SPI_ENGINE_REG_CMD_FIFO); 938 + writel_relaxed(SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_SDO_MASK, 939 + priv->tx_primary_lane_mask), 940 + spi_engine->base + SPI_ENGINE_REG_CMD_FIFO); 941 + } 1054 942 } 1055 943 1056 944 static struct dma_chan ··· 1105 973 { 1106 974 struct spi_engine *spi_engine; 1107 975 struct spi_controller *host; 1108 - unsigned int version; 976 + unsigned int version, data_width_reg_val; 1109 977 int irq, ret; 1110 978 1111 979 irq = platform_get_irq(pdev, 0); ··· 1174 1042 return PTR_ERR(spi_engine->base); 1175 1043 1176 1044 version = readl(spi_engine->base + ADI_AXI_REG_VERSION); 1177 - if (ADI_AXI_PCORE_VER_MAJOR(version) != 1) { 1045 + if (ADI_AXI_PCORE_VER_MAJOR(version) > 2) { 1178 1046 dev_err(&pdev->dev, "Unsupported peripheral version %u.%u.%u\n", 1179 1047 ADI_AXI_PCORE_VER_MAJOR(version), 1180 1048 ADI_AXI_PCORE_VER_MINOR(version), 1181 1049 ADI_AXI_PCORE_VER_PATCH(version)); 1182 1050 return -ENODEV; 1183 1051 } 1052 + 1053 + data_width_reg_val = readl(spi_engine->base + SPI_ENGINE_REG_DATA_WIDTH); 1184 1054 1185 1055 if (adi_axi_pcore_ver_gteq(version, 1, 1)) { 1186 1056 unsigned int sizes = readl(spi_engine->base + ··· 1230 1096 } 1231 1097 if (adi_axi_pcore_ver_gteq(version, 1, 3)) 1232 1098 host->mode_bits |= SPI_MOSI_IDLE_LOW | SPI_MOSI_IDLE_HIGH; 1099 + if (adi_axi_pcore_ver_gteq(version, 2, 0)) 1100 + host->num_data_lanes = FIELD_GET(SPI_ENGINE_REG_DATA_WIDTH_NUM_OF_SDIO_MASK, 1101 + data_width_reg_val); 1233 1102 1234 1103 if (host->max_speed_hz == 0) 1235 1104 return dev_err_probe(&pdev->dev, -EINVAL, "spi_clk rate is 0");
+140 -4
drivers/spi/spi.c
··· 2354 2354 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, 2355 2355 struct device_node *nc) 2356 2356 { 2357 - u32 value, cs[SPI_DEVICE_CS_CNT_MAX]; 2358 - int rc, idx; 2357 + u32 value, cs[SPI_DEVICE_CS_CNT_MAX], map[SPI_DEVICE_DATA_LANE_CNT_MAX]; 2358 + int rc, idx, max_num_data_lanes; 2359 2359 2360 2360 /* Mode (clock phase/polarity/etc.) */ 2361 2361 if (of_property_read_bool(nc, "spi-cpha")) ··· 2370 2370 spi->mode |= SPI_CS_HIGH; 2371 2371 2372 2372 /* Device DUAL/QUAD mode */ 2373 - if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { 2373 + 2374 + rc = of_property_read_variable_u32_array(nc, "spi-tx-lane-map", map, 1, 2375 + ARRAY_SIZE(map)); 2376 + if (rc >= 0) { 2377 + max_num_data_lanes = rc; 2378 + for (idx = 0; idx < max_num_data_lanes; idx++) 2379 + spi->tx_lane_map[idx] = map[idx]; 2380 + } else if (rc == -EINVAL) { 2381 + /* Default lane map is identity mapping. */ 2382 + max_num_data_lanes = ARRAY_SIZE(spi->tx_lane_map); 2383 + for (idx = 0; idx < max_num_data_lanes; idx++) 2384 + spi->tx_lane_map[idx] = idx; 2385 + } else { 2386 + dev_err(&ctlr->dev, 2387 + "failed to read spi-tx-lane-map property: %d\n", rc); 2388 + return rc; 2389 + } 2390 + 2391 + rc = of_property_count_u32_elems(nc, "spi-tx-bus-width"); 2392 + if (rc < 0 && rc != -EINVAL) { 2393 + dev_err(&ctlr->dev, 2394 + "failed to read spi-tx-bus-width property: %d\n", rc); 2395 + return rc; 2396 + } 2397 + if (rc > max_num_data_lanes) { 2398 + dev_err(&ctlr->dev, 2399 + "spi-tx-bus-width has more elements (%d) than spi-tx-lane-map (%d)\n", 2400 + rc, max_num_data_lanes); 2401 + return -EINVAL; 2402 + } 2403 + 2404 + if (rc == -EINVAL) { 2405 + /* Default when property is not present. */ 2406 + spi->num_tx_lanes = 1; 2407 + } else { 2408 + u32 first_value; 2409 + 2410 + spi->num_tx_lanes = rc; 2411 + 2412 + for (idx = 0; idx < spi->num_tx_lanes; idx++) { 2413 + rc = of_property_read_u32_index(nc, "spi-tx-bus-width", 2414 + idx, &value); 2415 + if (rc) 2416 + return rc; 2417 + 2418 + /* 2419 + * For now, we only support all lanes having the same 2420 + * width so we can keep using the existing mode flags. 2421 + */ 2422 + if (!idx) 2423 + first_value = value; 2424 + else if (first_value != value) { 2425 + dev_err(&ctlr->dev, 2426 + "spi-tx-bus-width has inconsistent values: first %d vs later %d\n", 2427 + first_value, value); 2428 + return -EINVAL; 2429 + } 2430 + } 2431 + 2374 2432 switch (value) { 2375 2433 case 0: 2376 2434 spi->mode |= SPI_NO_TX; ··· 2452 2394 } 2453 2395 } 2454 2396 2455 - if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { 2397 + for (idx = 0; idx < spi->num_tx_lanes; idx++) { 2398 + if (spi->tx_lane_map[idx] >= spi->controller->num_data_lanes) { 2399 + dev_err(&ctlr->dev, 2400 + "spi-tx-lane-map has invalid value %d (num_data_lanes=%d)\n", 2401 + spi->tx_lane_map[idx], 2402 + spi->controller->num_data_lanes); 2403 + return -EINVAL; 2404 + } 2405 + } 2406 + 2407 + rc = of_property_read_variable_u32_array(nc, "spi-rx-lane-map", map, 1, 2408 + ARRAY_SIZE(map)); 2409 + if (rc >= 0) { 2410 + max_num_data_lanes = rc; 2411 + for (idx = 0; idx < max_num_data_lanes; idx++) 2412 + spi->rx_lane_map[idx] = map[idx]; 2413 + } else if (rc == -EINVAL) { 2414 + /* Default lane map is identity mapping. */ 2415 + max_num_data_lanes = ARRAY_SIZE(spi->rx_lane_map); 2416 + for (idx = 0; idx < max_num_data_lanes; idx++) 2417 + spi->rx_lane_map[idx] = idx; 2418 + } else { 2419 + dev_err(&ctlr->dev, 2420 + "failed to read spi-rx-lane-map property: %d\n", rc); 2421 + return rc; 2422 + } 2423 + 2424 + rc = of_property_count_u32_elems(nc, "spi-rx-bus-width"); 2425 + if (rc < 0 && rc != -EINVAL) { 2426 + dev_err(&ctlr->dev, 2427 + "failed to read spi-rx-bus-width property: %d\n", rc); 2428 + return rc; 2429 + } 2430 + if (rc > max_num_data_lanes) { 2431 + dev_err(&ctlr->dev, 2432 + "spi-rx-bus-width has more elements (%d) than spi-rx-lane-map (%d)\n", 2433 + rc, max_num_data_lanes); 2434 + return -EINVAL; 2435 + } 2436 + 2437 + if (rc == -EINVAL) { 2438 + /* Default when property is not present. */ 2439 + spi->num_rx_lanes = 1; 2440 + } else { 2441 + u32 first_value; 2442 + 2443 + spi->num_rx_lanes = rc; 2444 + 2445 + for (idx = 0; idx < spi->num_rx_lanes; idx++) { 2446 + rc = of_property_read_u32_index(nc, "spi-rx-bus-width", 2447 + idx, &value); 2448 + if (rc) 2449 + return rc; 2450 + 2451 + /* 2452 + * For now, we only support all lanes having the same 2453 + * width so we can keep using the existing mode flags. 2454 + */ 2455 + if (!idx) 2456 + first_value = value; 2457 + else if (first_value != value) { 2458 + dev_err(&ctlr->dev, 2459 + "spi-rx-bus-width has inconsistent values: first %d vs later %d\n", 2460 + first_value, value); 2461 + return -EINVAL; 2462 + } 2463 + } 2464 + 2456 2465 switch (value) { 2457 2466 case 0: 2458 2467 spi->mode |= SPI_NO_RX; ··· 2540 2415 "spi-rx-bus-width %d not supported\n", 2541 2416 value); 2542 2417 break; 2418 + } 2419 + } 2420 + 2421 + for (idx = 0; idx < spi->num_rx_lanes; idx++) { 2422 + if (spi->rx_lane_map[idx] >= spi->controller->num_data_lanes) { 2423 + dev_err(&ctlr->dev, 2424 + "spi-rx-lane-map has invalid value %d (num_data_lanes=%d)\n", 2425 + spi->rx_lane_map[idx], 2426 + spi->controller->num_data_lanes); 2427 + return -EINVAL; 2543 2428 } 2544 2429 } 2545 2430 ··· 3201 3066 mutex_init(&ctlr->add_lock); 3202 3067 ctlr->bus_num = -1; 3203 3068 ctlr->num_chipselect = 1; 3069 + ctlr->num_data_lanes = 1; 3204 3070 ctlr->target = target; 3205 3071 if (IS_ENABLED(CONFIG_SPI_SLAVE) && target) 3206 3072 ctlr->dev.class = &spi_target_class;
+30
include/linux/spi/spi.h
··· 23 23 /* Max no. of CS supported per spi device */ 24 24 #define SPI_DEVICE_CS_CNT_MAX 4 25 25 26 + /* Max no. of data lanes supported per spi device */ 27 + #define SPI_DEVICE_DATA_LANE_CNT_MAX 8 28 + 26 29 struct dma_chan; 27 30 struct software_node; 28 31 struct ptp_system_timestamp; ··· 177 174 * @cs_index_mask: Bit mask of the active chipselect(s) in the chipselect array 178 175 * @cs_gpiod: Array of GPIO descriptors of the corresponding chipselect lines 179 176 * (optional, NULL when not using a GPIO line) 177 + * @tx_lane_map: Map of peripheral lanes (index) to controller lanes (value). 178 + * @num_tx_lanes: Number of transmit lanes wired up. 179 + * @rx_lane_map: Map of peripheral lanes (index) to controller lanes (value). 180 + * @num_rx_lanes: Number of receive lanes wired up. 180 181 * 181 182 * A @spi_device is used to interchange data between an SPI target device 182 183 * (usually a discrete chip) and CPU memory. ··· 248 241 u32 cs_index_mask : SPI_DEVICE_CS_CNT_MAX; 249 242 250 243 struct gpio_desc *cs_gpiod[SPI_DEVICE_CS_CNT_MAX]; /* Chip select gpio desc */ 244 + 245 + /* Multi-lane SPI controller support. */ 246 + u8 tx_lane_map[SPI_DEVICE_DATA_LANE_CNT_MAX]; 247 + u8 num_tx_lanes; 248 + u8 rx_lane_map[SPI_DEVICE_DATA_LANE_CNT_MAX]; 249 + u8 num_rx_lanes; 251 250 252 251 /* 253 252 * Likely need more hooks for more protocol options affecting how ··· 414 401 * SPI targets, and are numbered from zero to num_chipselects. 415 402 * each target has a chipselect signal, but it's common that not 416 403 * every chipselect is connected to a target. 404 + * @num_data_lanes: Number of data lanes supported by this controller. Default is 1. 417 405 * @dma_alignment: SPI controller constraint on DMA buffers alignment. 418 406 * @mode_bits: flags understood by this controller driver 419 407 * @buswidth_override_bits: flags to override for this controller driver ··· 589 575 * might use board-specific GPIOs. 590 576 */ 591 577 u16 num_chipselect; 578 + 579 + /* 580 + * Some specialized SPI controllers can have more than one physical 581 + * data lane interface per controller (each having it's own serializer). 582 + * This specifies the number of data lanes in that case. Other 583 + * controllers do not need to set this (defaults to 1). 584 + */ 585 + u16 num_data_lanes; 592 586 593 587 /* Some SPI controllers pose alignment requirements on DMAable 594 588 * buffers; let protocol drivers know about these requirements. ··· 981 959 * (SPI_NBITS_SINGLE) is used. 982 960 * @rx_nbits: number of bits used for reading. If 0 the default 983 961 * (SPI_NBITS_SINGLE) is used. 962 + * @multi_lane_mode: How to serialize data on multiple lanes. One of the 963 + * SPI_MULTI_LANE_MODE_* values. 984 964 * @len: size of rx and tx buffers (in bytes) 985 965 * @speed_hz: Select a speed other than the device default for this 986 966 * transfer. If 0 the default (from @spi_device) is used. ··· 1119 1095 unsigned cs_change:1; 1120 1096 unsigned tx_nbits:4; 1121 1097 unsigned rx_nbits:4; 1098 + 1099 + #define SPI_MULTI_LANE_MODE_SINGLE 0 /* only use single lane */ 1100 + #define SPI_MULTI_LANE_MODE_STRIPE 1 /* one data word per lane */ 1101 + #define SPI_MULTI_LANE_MODE_MIRROR 2 /* same word sent on all lanes */ 1102 + unsigned multi_lane_mode: 2; 1103 + 1122 1104 unsigned timestamped:1; 1123 1105 bool dtr_mode; 1124 1106 #define SPI_NBITS_SINGLE 0x01 /* 1-bit transfer */