1 // SPDX-License-Identifier: GPL-2.0
3 // Register map access API - SPI AVMM support
5 // Copyright (C) 2018-2020 Intel Corporation. All rights reserved.
7 #include <linux/module.h>
8 #include <linux/regmap.h>
9 #include <linux/spi/spi.h>
12 * This driver implements the regmap operations for a generic SPI
13 * master to access the registers of the spi slave chip which has an
16 * The "SPI slave to Avalon Master Bridge" (spi-avmm) IP should be integrated
17 * in the spi slave chip. The IP acts as a bridge to convert encoded streams of
18 * bytes from the host to the internal register read/write on Avalon bus. In
19 * order to issue register access requests to the slave chip, the host should
20 * send formatted bytes that conform to the transfer protocol.
21 * The transfer protocol contains 3 layers: transaction layer, packet layer
24 * Reference Documents could be found at:
25 * https://www.intel.com/content/www/us/en/programmable/documentation/sfo1400787952932.html
27 * Chapter "SPI Slave/JTAG to Avalon Master Bridge Cores" is a general
28 * introduction to the protocol.
30 * Chapter "Avalon Packets to Transactions Converter Core" describes
31 * the transaction layer.
33 * Chapter "Avalon-ST Bytes to Packets and Packets to Bytes Converter Cores"
34 * describes the packet layer.
36 * Chapter "Avalon-ST Serial Peripheral Interface Core" describes the
40 * When host issues a regmap read/write, the driver will transform the request
41 * to byte stream layer by layer. It formats the register addr, value and
42 * length to the transaction layer request, then converts the request to packet
43 * layer bytes stream and then to physical layer bytes stream. Finally the
44 * driver sends the formatted byte stream over SPI bus to the slave chip.
46 * The spi-avmm IP on the slave chip decodes the byte stream and initiates
47 * register read/write on its internal Avalon bus, and then encodes the
48 * response to byte stream and sends back to host.
50 * The driver receives the byte stream, reverses the 3 layers transformation,
51 * and finally gets the response value (read out data for register read,
52 * successful written size for register write).
57 #define PKT_CHANNEL 0x7c
63 #define TRANS_CODE_WRITE 0x0
64 #define TRANS_CODE_SEQ_WRITE 0x4
65 #define TRANS_CODE_READ 0x10
66 #define TRANS_CODE_SEQ_READ 0x14
67 #define TRANS_CODE_NO_TRANS 0x7f
69 #define SPI_AVMM_XFER_TIMEOUT (msecs_to_jiffies(200))
71 /* slave's register addr is 32 bits */
72 #define SPI_AVMM_REG_SIZE 4UL
73 /* slave's register value is 32 bits */
74 #define SPI_AVMM_VAL_SIZE 4UL
77 * max rx size could be larger. But considering the buffer consuming,
78 * it is proper that we limit 1KB xfer at max.
80 #define MAX_READ_CNT 256UL
81 #define MAX_WRITE_CNT 1UL
83 struct trans_req_header
{
90 struct trans_resp_header
{
96 #define TRANS_REQ_HD_SIZE (sizeof(struct trans_req_header))
97 #define TRANS_RESP_HD_SIZE (sizeof(struct trans_resp_header))
100 * In transaction layer,
101 * the write request format is: Transaction request header + data
102 * the read request format is: Transaction request header
103 * the write response format is: Transaction response header
104 * the read response format is: pure data, no Transaction response header
106 #define TRANS_WR_TX_SIZE(n) (TRANS_REQ_HD_SIZE + SPI_AVMM_VAL_SIZE * (n))
107 #define TRANS_RD_TX_SIZE TRANS_REQ_HD_SIZE
108 #define TRANS_TX_MAX TRANS_WR_TX_SIZE(MAX_WRITE_CNT)
110 #define TRANS_RD_RX_SIZE(n) (SPI_AVMM_VAL_SIZE * (n))
111 #define TRANS_WR_RX_SIZE TRANS_RESP_HD_SIZE
112 #define TRANS_RX_MAX TRANS_RD_RX_SIZE(MAX_READ_CNT)
114 /* tx & rx share one transaction layer buffer */
115 #define TRANS_BUF_SIZE ((TRANS_TX_MAX > TRANS_RX_MAX) ? \
116 TRANS_TX_MAX : TRANS_RX_MAX)
119 * In tx phase, the host prepares all the phy layer bytes of a request in the
120 * phy buffer and sends them in a batch.
122 * The packet layer and physical layer defines several special chars for
123 * various purpose, when a transaction layer byte hits one of these special
124 * chars, it should be escaped. The escape rule is, "Escape char first,
125 * following the byte XOR'ed with 0x20".
127 * This macro defines the max possible length of the phy data. In the worst
128 * case, all transaction layer bytes need to be escaped (so the data length
129 * doubles), plus 4 special chars (SOP, CHANNEL, CHANNEL_NUM, EOP). Finally
130 * we should make sure the length is aligned to SPI BPW.
132 #define PHY_TX_MAX ALIGN(2 * TRANS_TX_MAX + 4, 4)
135 * Unlike tx, phy rx is affected by possible PHY_IDLE bytes from slave, the max
136 * length of the rx bit stream is unpredictable. So the driver reads the words
137 * one by one, and parses each word immediately into transaction layer buffer.
138 * Only one word length of phy buffer is used for rx.
140 #define PHY_BUF_SIZE PHY_TX_MAX
143 * struct spi_avmm_bridge - SPI slave to AVMM bus master bridge
145 * @spi: spi slave associated with this bridge.
146 * @word_len: bytes of word for spi transfer.
147 * @trans_len: length of valid data in trans_buf.
148 * @phy_len: length of valid data in phy_buf.
149 * @trans_buf: the bridge buffer for transaction layer data.
150 * @phy_buf: the bridge buffer for physical layer data.
151 * @swap_words: the word swapping cb for phy data. NULL if not needed.
153 * As a device's registers are implemented on the AVMM bus address space, it
154 * requires the driver to issue formatted requests to spi slave to AVMM bus
155 * master bridge to perform register access.
157 struct spi_avmm_bridge
{
158 struct spi_device
*spi
;
159 unsigned char word_len
;
160 unsigned int trans_len
;
161 unsigned int phy_len
;
162 /* bridge buffer used in translation between protocol layers */
163 char trans_buf
[TRANS_BUF_SIZE
];
164 char phy_buf
[PHY_BUF_SIZE
];
165 void (*swap_words
)(char *buf
, unsigned int len
);
168 static void br_swap_words_32(char *buf
, unsigned int len
)
181 * Format transaction layer data in br->trans_buf according to the register
182 * access request, Store valid transaction layer data length in br->trans_len.
184 static int br_trans_tx_prepare(struct spi_avmm_bridge
*br
, bool is_read
, u32 reg
,
185 u32
*wr_val
, u32 count
)
187 struct trans_req_header
*header
;
188 unsigned int trans_len
;
195 code
= TRANS_CODE_READ
;
197 code
= TRANS_CODE_SEQ_READ
;
200 code
= TRANS_CODE_WRITE
;
202 code
= TRANS_CODE_SEQ_WRITE
;
205 header
= (struct trans_req_header
*)br
->trans_buf
;
208 header
->size
= cpu_to_be16((u16
)count
* SPI_AVMM_VAL_SIZE
);
209 header
->addr
= cpu_to_be32(reg
);
211 trans_len
= TRANS_REQ_HD_SIZE
;
214 trans_len
+= SPI_AVMM_VAL_SIZE
* count
;
215 if (trans_len
> sizeof(br
->trans_buf
))
218 data
= (__le32
*)(br
->trans_buf
+ TRANS_REQ_HD_SIZE
);
220 for (i
= 0; i
< count
; i
++)
221 *data
++ = cpu_to_le32(*wr_val
++);
224 /* Store valid trans data length for next layer */
225 br
->trans_len
= trans_len
;
231 * Convert transaction layer data (in br->trans_buf) to phy layer data, store
232 * them in br->phy_buf. Pad the phy_buf aligned with SPI's BPW. Store valid phy
233 * layer data length in br->phy_len.
235 * phy_buf len should be aligned with SPI's BPW. Spare bytes should be padded
236 * with PHY_IDLE, then the slave will just drop them.
238 * The driver will not simply pad 4a at the tail. The concern is that driver
239 * will not store MISO data during tx phase, if the driver pads 4a at the tail,
240 * it is possible that if the slave is fast enough to response at the padding
241 * time. As a result these rx bytes are lost. In the following case, 7a,7c,00
243 * MOSI ...|7a|7c|00|10| |00|00|04|02| |4b|7d|5a|7b| |40|4a|4a|4a| |XX|XX|...
244 * MISO ...|4a|4a|4a|4a| |4a|4a|4a|4a| |4a|4a|4a|4a| |4a|7a|7c|00| |78|56|...
246 * So the driver moves EOP and bytes after EOP to the end of the aligned size,
247 * then fill the hole with PHY_IDLE. As following:
248 * before pad ...|7a|7c|00|10| |00|00|04|02| |4b|7d|5a|7b| |40|
249 * after pad ...|7a|7c|00|10| |00|00|04|02| |4b|7d|5a|4a| |4a|4a|7b|40|
250 * Then if the slave will not get the entire packet before the tx phase is
251 * over, it can't responsed to anything either.
253 static int br_pkt_phy_tx_prepare(struct spi_avmm_bridge
*br
)
255 char *tb
, *tb_end
, *pb
, *pb_limit
, *pb_eop
= NULL
;
256 unsigned int aligned_phy_len
, move_size
;
257 bool need_esc
= false;
260 tb_end
= tb
+ br
->trans_len
;
262 pb_limit
= pb
+ ARRAY_SIZE(br
->phy_buf
);
267 * The driver doesn't support multiple channels so the channel number
273 for (; pb
< pb_limit
&& tb
< tb_end
; pb
++) {
280 /* EOP should be inserted before the last valid char */
281 if (tb
== tb_end
- 1 && !pb_eop
) {
288 * insert an ESCAPE char if the data value equals any special
310 /* The phy buffer is used out but transaction layer data remains */
314 /* Store valid phy data length for spi transfer */
315 br
->phy_len
= pb
- br
->phy_buf
;
317 if (br
->word_len
== 1)
320 /* Do phy buf padding if word_len > 1 byte. */
321 aligned_phy_len
= ALIGN(br
->phy_len
, br
->word_len
);
322 if (aligned_phy_len
> sizeof(br
->phy_buf
))
325 if (aligned_phy_len
== br
->phy_len
)
328 /* move EOP and bytes after EOP to the end of aligned size */
329 move_size
= pb
- pb_eop
;
330 memmove(&br
->phy_buf
[aligned_phy_len
- move_size
], pb_eop
, move_size
);
332 /* fill the hole with PHY_IDLEs */
333 memset(pb_eop
, PHY_IDLE
, aligned_phy_len
- br
->phy_len
);
335 /* update the phy data length */
336 br
->phy_len
= aligned_phy_len
;
342 * In tx phase, the slave only returns PHY_IDLE (0x4a). So the driver will
343 * ignore rx in tx phase.
345 static int br_do_tx(struct spi_avmm_bridge
*br
)
347 /* reorder words for spi transfer */
349 br
->swap_words(br
->phy_buf
, br
->phy_len
);
351 /* send all data in phy_buf */
352 return spi_write(br
->spi
, br
->phy_buf
, br
->phy_len
);
356 * This function read the rx byte stream from SPI word by word and convert
357 * them to transaction layer data in br->trans_buf. It also stores the length
358 * of rx transaction layer data in br->trans_len
360 * The slave may send an unknown number of PHY_IDLEs in rx phase, so we cannot
361 * prepare a fixed length buffer to receive all of the rx data in a batch. We
362 * have to read word by word and convert them to transaction layer data at
365 static int br_do_rx_and_pkt_phy_parse(struct spi_avmm_bridge
*br
)
367 bool eop_found
= false, channel_found
= false, esc_found
= false;
368 bool valid_word
= false, last_try
= false;
369 struct device
*dev
= &br
->spi
->dev
;
370 char *pb
, *tb_limit
, *tb
= NULL
;
371 unsigned long poll_timeout
;
374 tb_limit
= br
->trans_buf
+ ARRAY_SIZE(br
->trans_buf
);
376 poll_timeout
= jiffies
+ SPI_AVMM_XFER_TIMEOUT
;
377 while (tb
< tb_limit
) {
378 ret
= spi_read(br
->spi
, pb
, br
->word_len
);
382 /* reorder the word back */
384 br
->swap_words(pb
, br
->word_len
);
387 for (i
= 0; i
< br
->word_len
; i
++) {
388 /* drop everything before first SOP */
389 if (!tb
&& pb
[i
] != PKT_SOP
)
393 if (pb
[i
] == PHY_IDLE
)
399 * We don't support multiple channels, so error out if
400 * a non-zero channel number is found.
404 dev_err(dev
, "%s channel num != 0\n",
409 channel_found
= false;
416 * reset the parsing if a second SOP appears.
420 channel_found
= false;
425 * No special char is expected after ESC char.
426 * No special char (except ESC & PHY_IDLE) is
427 * expected after EOP char.
429 * The special chars are all dropped.
431 if (esc_found
|| eop_found
)
437 if (esc_found
|| eop_found
)
440 channel_found
= true;
450 /* Record the normal byte in trans_buf. */
452 *tb
++ = pb
[i
] ^ 0x20;
459 * We get the last normal byte after EOP, it is
460 * time we finish. Normally the function should
464 br
->trans_len
= tb
- br
->trans_buf
;
471 /* update poll timeout when we get valid word */
472 poll_timeout
= jiffies
+ SPI_AVMM_XFER_TIMEOUT
;
476 * We timeout when rx keeps invalid for some time. But
477 * it is possible we are scheduled out for long time
478 * after a spi_read. So when we are scheduled in, a SW
479 * timeout happens. But actually HW may have worked fine and
480 * has been ready long time ago. So we need to do an extra
481 * read, if we get a valid word then we could continue rx,
482 * otherwise real a HW issue happens.
487 if (time_after(jiffies
, poll_timeout
))
493 * We have used out all transfer layer buffer but cannot find the end
494 * of the byte stream.
496 dev_err(dev
, "%s transfer buffer is full but rx doesn't end\n",
503 * For read transactions, the avmm bus will directly return register values
504 * without transaction response header.
506 static int br_rd_trans_rx_parse(struct spi_avmm_bridge
*br
,
507 u32
*val
, unsigned int expected_count
)
509 unsigned int i
, trans_len
= br
->trans_len
;
512 if (expected_count
* SPI_AVMM_VAL_SIZE
!= trans_len
)
515 data
= (__le32
*)br
->trans_buf
;
516 for (i
= 0; i
< expected_count
; i
++)
517 *val
++ = le32_to_cpu(*data
++);
523 * For write transactions, the slave will return a transaction response
526 static int br_wr_trans_rx_parse(struct spi_avmm_bridge
*br
,
527 unsigned int expected_count
)
529 unsigned int trans_len
= br
->trans_len
;
530 struct trans_resp_header
*resp
;
534 if (trans_len
!= TRANS_RESP_HD_SIZE
)
537 resp
= (struct trans_resp_header
*)br
->trans_buf
;
539 code
= resp
->r_code
^ 0x80;
540 val_len
= be16_to_cpu(resp
->size
);
541 if (!val_len
|| val_len
!= expected_count
* SPI_AVMM_VAL_SIZE
)
544 /* error out if the trans code doesn't align with the val size */
545 if ((val_len
== SPI_AVMM_VAL_SIZE
&& code
!= TRANS_CODE_WRITE
) ||
546 (val_len
> SPI_AVMM_VAL_SIZE
&& code
!= TRANS_CODE_SEQ_WRITE
))
552 static int do_reg_access(void *context
, bool is_read
, unsigned int reg
,
553 unsigned int *value
, unsigned int count
)
555 struct spi_avmm_bridge
*br
= context
;
558 /* invalidate bridge buffers first */
562 ret
= br_trans_tx_prepare(br
, is_read
, reg
, value
, count
);
566 ret
= br_pkt_phy_tx_prepare(br
);
574 ret
= br_do_rx_and_pkt_phy_parse(br
);
579 return br_rd_trans_rx_parse(br
, value
, count
);
581 return br_wr_trans_rx_parse(br
, count
);
584 static int regmap_spi_avmm_gather_write(void *context
,
585 const void *reg_buf
, size_t reg_len
,
586 const void *val_buf
, size_t val_len
)
588 if (reg_len
!= SPI_AVMM_REG_SIZE
)
591 if (!IS_ALIGNED(val_len
, SPI_AVMM_VAL_SIZE
))
594 return do_reg_access(context
, false, *(u32
*)reg_buf
, (u32
*)val_buf
,
595 val_len
/ SPI_AVMM_VAL_SIZE
);
598 static int regmap_spi_avmm_write(void *context
, const void *data
, size_t bytes
)
600 if (bytes
< SPI_AVMM_REG_SIZE
+ SPI_AVMM_VAL_SIZE
)
603 return regmap_spi_avmm_gather_write(context
, data
, SPI_AVMM_REG_SIZE
,
604 data
+ SPI_AVMM_REG_SIZE
,
605 bytes
- SPI_AVMM_REG_SIZE
);
608 static int regmap_spi_avmm_read(void *context
,
609 const void *reg_buf
, size_t reg_len
,
610 void *val_buf
, size_t val_len
)
612 if (reg_len
!= SPI_AVMM_REG_SIZE
)
615 if (!IS_ALIGNED(val_len
, SPI_AVMM_VAL_SIZE
))
618 return do_reg_access(context
, true, *(u32
*)reg_buf
, val_buf
,
619 (val_len
/ SPI_AVMM_VAL_SIZE
));
622 static struct spi_avmm_bridge
*
623 spi_avmm_bridge_ctx_gen(struct spi_device
*spi
)
625 struct spi_avmm_bridge
*br
;
628 return ERR_PTR(-ENODEV
);
630 /* Only support BPW == 8 or 32 now. Try 32 BPW first. */
631 spi
->mode
= SPI_MODE_1
;
632 spi
->bits_per_word
= 32;
633 if (spi_setup(spi
)) {
634 spi
->bits_per_word
= 8;
636 return ERR_PTR(-EINVAL
);
639 br
= kzalloc(sizeof(*br
), GFP_KERNEL
);
641 return ERR_PTR(-ENOMEM
);
644 br
->word_len
= spi
->bits_per_word
/ 8;
645 if (br
->word_len
== 4) {
647 * The protocol requires little endian byte order but MSB
648 * first. So driver needs to swap the byte order word by word
649 * if word length > 1.
651 br
->swap_words
= br_swap_words_32
;
657 static void spi_avmm_bridge_ctx_free(void *context
)
662 static const struct regmap_bus regmap_spi_avmm_bus
= {
663 .write
= regmap_spi_avmm_write
,
664 .gather_write
= regmap_spi_avmm_gather_write
,
665 .read
= regmap_spi_avmm_read
,
666 .reg_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
667 .val_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
668 .max_raw_read
= SPI_AVMM_VAL_SIZE
* MAX_READ_CNT
,
669 .max_raw_write
= SPI_AVMM_VAL_SIZE
* MAX_WRITE_CNT
,
670 .free_context
= spi_avmm_bridge_ctx_free
,
673 struct regmap
*__regmap_init_spi_avmm(struct spi_device
*spi
,
674 const struct regmap_config
*config
,
675 struct lock_class_key
*lock_key
,
676 const char *lock_name
)
678 struct spi_avmm_bridge
*bridge
;
681 bridge
= spi_avmm_bridge_ctx_gen(spi
);
683 return ERR_CAST(bridge
);
685 map
= __regmap_init(&spi
->dev
, ®map_spi_avmm_bus
,
686 bridge
, config
, lock_key
, lock_name
);
688 spi_avmm_bridge_ctx_free(bridge
);
689 return ERR_CAST(map
);
694 EXPORT_SYMBOL_GPL(__regmap_init_spi_avmm
);
696 struct regmap
*__devm_regmap_init_spi_avmm(struct spi_device
*spi
,
697 const struct regmap_config
*config
,
698 struct lock_class_key
*lock_key
,
699 const char *lock_name
)
701 struct spi_avmm_bridge
*bridge
;
704 bridge
= spi_avmm_bridge_ctx_gen(spi
);
706 return ERR_CAST(bridge
);
708 map
= __devm_regmap_init(&spi
->dev
, ®map_spi_avmm_bus
,
709 bridge
, config
, lock_key
, lock_name
);
711 spi_avmm_bridge_ctx_free(bridge
);
712 return ERR_CAST(map
);
717 EXPORT_SYMBOL_GPL(__devm_regmap_init_spi_avmm
);
719 MODULE_LICENSE("GPL v2");