1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // Register map access API - SPI AVMM support
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Copyright (C) 2018-2020 Intel Corporation. All rights reserved.
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/module.h>
8*4882a593Smuzhiyun #include <linux/regmap.h>
9*4882a593Smuzhiyun #include <linux/spi/spi.h>
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun * This driver implements the regmap operations for a generic SPI
13*4882a593Smuzhiyun * master to access the registers of the spi slave chip which has an
14*4882a593Smuzhiyun * Avalone bus in it.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * The "SPI slave to Avalon Master Bridge" (spi-avmm) IP should be integrated
17*4882a593Smuzhiyun * in the spi slave chip. The IP acts as a bridge to convert encoded streams of
18*4882a593Smuzhiyun * bytes from the host to the internal register read/write on Avalon bus. In
19*4882a593Smuzhiyun * order to issue register access requests to the slave chip, the host should
20*4882a593Smuzhiyun * send formatted bytes that conform to the transfer protocol.
21*4882a593Smuzhiyun * The transfer protocol contains 3 layers: transaction layer, packet layer
22*4882a593Smuzhiyun * and physical layer.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Reference Documents could be found at:
25*4882a593Smuzhiyun * https://www.intel.com/content/www/us/en/programmable/documentation/sfo1400787952932.html
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * Chapter "SPI Slave/JTAG to Avalon Master Bridge Cores" is a general
28*4882a593Smuzhiyun * introduction to the protocol.
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * Chapter "Avalon Packets to Transactions Converter Core" describes
31*4882a593Smuzhiyun * the transaction layer.
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * Chapter "Avalon-ST Bytes to Packets and Packets to Bytes Converter Cores"
34*4882a593Smuzhiyun * describes the packet layer.
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * Chapter "Avalon-ST Serial Peripheral Interface Core" describes the
37*4882a593Smuzhiyun * physical layer.
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * When host issues a regmap read/write, the driver will transform the request
41*4882a593Smuzhiyun * to byte stream layer by layer. It formats the register addr, value and
42*4882a593Smuzhiyun * length to the transaction layer request, then converts the request to packet
43*4882a593Smuzhiyun * layer bytes stream and then to physical layer bytes stream. Finally the
44*4882a593Smuzhiyun * driver sends the formatted byte stream over SPI bus to the slave chip.
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * The spi-avmm IP on the slave chip decodes the byte stream and initiates
47*4882a593Smuzhiyun * register read/write on its internal Avalon bus, and then encodes the
48*4882a593Smuzhiyun * response to byte stream and sends back to host.
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun * The driver receives the byte stream, reverses the 3 layers transformation,
51*4882a593Smuzhiyun * and finally gets the response value (read out data for register read,
52*4882a593Smuzhiyun * successful written size for register write).
53*4882a593Smuzhiyun */
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun #define PKT_SOP 0x7a
56*4882a593Smuzhiyun #define PKT_EOP 0x7b
57*4882a593Smuzhiyun #define PKT_CHANNEL 0x7c
58*4882a593Smuzhiyun #define PKT_ESC 0x7d
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun #define PHY_IDLE 0x4a
61*4882a593Smuzhiyun #define PHY_ESC 0x4d
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun #define TRANS_CODE_WRITE 0x0
64*4882a593Smuzhiyun #define TRANS_CODE_SEQ_WRITE 0x4
65*4882a593Smuzhiyun #define TRANS_CODE_READ 0x10
66*4882a593Smuzhiyun #define TRANS_CODE_SEQ_READ 0x14
67*4882a593Smuzhiyun #define TRANS_CODE_NO_TRANS 0x7f
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #define SPI_AVMM_XFER_TIMEOUT (msecs_to_jiffies(200))
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /* slave's register addr is 32 bits */
72*4882a593Smuzhiyun #define SPI_AVMM_REG_SIZE 4UL
73*4882a593Smuzhiyun /* slave's register value is 32 bits */
74*4882a593Smuzhiyun #define SPI_AVMM_VAL_SIZE 4UL
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun * max rx size could be larger. But considering the buffer consuming,
78*4882a593Smuzhiyun * it is proper that we limit 1KB xfer at max.
79*4882a593Smuzhiyun */
80*4882a593Smuzhiyun #define MAX_READ_CNT 256UL
81*4882a593Smuzhiyun #define MAX_WRITE_CNT 1UL
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun struct trans_req_header {
84*4882a593Smuzhiyun u8 code;
85*4882a593Smuzhiyun u8 rsvd;
86*4882a593Smuzhiyun __be16 size;
87*4882a593Smuzhiyun __be32 addr;
88*4882a593Smuzhiyun } __packed;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun struct trans_resp_header {
91*4882a593Smuzhiyun u8 r_code;
92*4882a593Smuzhiyun u8 rsvd;
93*4882a593Smuzhiyun __be16 size;
94*4882a593Smuzhiyun } __packed;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun #define TRANS_REQ_HD_SIZE (sizeof(struct trans_req_header))
97*4882a593Smuzhiyun #define TRANS_RESP_HD_SIZE (sizeof(struct trans_resp_header))
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /*
100*4882a593Smuzhiyun * In transaction layer,
101*4882a593Smuzhiyun * the write request format is: Transaction request header + data
102*4882a593Smuzhiyun * the read request format is: Transaction request header
103*4882a593Smuzhiyun * the write response format is: Transaction response header
104*4882a593Smuzhiyun * the read response format is: pure data, no Transaction response header
105*4882a593Smuzhiyun */
106*4882a593Smuzhiyun #define TRANS_WR_TX_SIZE(n) (TRANS_REQ_HD_SIZE + SPI_AVMM_VAL_SIZE * (n))
107*4882a593Smuzhiyun #define TRANS_RD_TX_SIZE TRANS_REQ_HD_SIZE
108*4882a593Smuzhiyun #define TRANS_TX_MAX TRANS_WR_TX_SIZE(MAX_WRITE_CNT)
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun #define TRANS_RD_RX_SIZE(n) (SPI_AVMM_VAL_SIZE * (n))
111*4882a593Smuzhiyun #define TRANS_WR_RX_SIZE TRANS_RESP_HD_SIZE
112*4882a593Smuzhiyun #define TRANS_RX_MAX TRANS_RD_RX_SIZE(MAX_READ_CNT)
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* tx & rx share one transaction layer buffer */
115*4882a593Smuzhiyun #define TRANS_BUF_SIZE ((TRANS_TX_MAX > TRANS_RX_MAX) ? \
116*4882a593Smuzhiyun TRANS_TX_MAX : TRANS_RX_MAX)
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun /*
119*4882a593Smuzhiyun * In tx phase, the host prepares all the phy layer bytes of a request in the
120*4882a593Smuzhiyun * phy buffer and sends them in a batch.
121*4882a593Smuzhiyun *
122*4882a593Smuzhiyun * The packet layer and physical layer defines several special chars for
123*4882a593Smuzhiyun * various purpose, when a transaction layer byte hits one of these special
124*4882a593Smuzhiyun * chars, it should be escaped. The escape rule is, "Escape char first,
125*4882a593Smuzhiyun * following the byte XOR'ed with 0x20".
126*4882a593Smuzhiyun *
127*4882a593Smuzhiyun * This macro defines the max possible length of the phy data. In the worst
128*4882a593Smuzhiyun * case, all transaction layer bytes need to be escaped (so the data length
129*4882a593Smuzhiyun * doubles), plus 4 special chars (SOP, CHANNEL, CHANNEL_NUM, EOP). Finally
130*4882a593Smuzhiyun * we should make sure the length is aligned to SPI BPW.
131*4882a593Smuzhiyun */
132*4882a593Smuzhiyun #define PHY_TX_MAX ALIGN(2 * TRANS_TX_MAX + 4, 4)
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /*
135*4882a593Smuzhiyun * Unlike tx, phy rx is affected by possible PHY_IDLE bytes from slave, the max
136*4882a593Smuzhiyun * length of the rx bit stream is unpredictable. So the driver reads the words
137*4882a593Smuzhiyun * one by one, and parses each word immediately into transaction layer buffer.
138*4882a593Smuzhiyun * Only one word length of phy buffer is used for rx.
139*4882a593Smuzhiyun */
140*4882a593Smuzhiyun #define PHY_BUF_SIZE PHY_TX_MAX
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /**
143*4882a593Smuzhiyun * struct spi_avmm_bridge - SPI slave to AVMM bus master bridge
144*4882a593Smuzhiyun *
145*4882a593Smuzhiyun * @spi: spi slave associated with this bridge.
146*4882a593Smuzhiyun * @word_len: bytes of word for spi transfer.
147*4882a593Smuzhiyun * @trans_len: length of valid data in trans_buf.
148*4882a593Smuzhiyun * @phy_len: length of valid data in phy_buf.
149*4882a593Smuzhiyun * @trans_buf: the bridge buffer for transaction layer data.
150*4882a593Smuzhiyun * @phy_buf: the bridge buffer for physical layer data.
151*4882a593Smuzhiyun * @swap_words: the word swapping cb for phy data. NULL if not needed.
152*4882a593Smuzhiyun *
153*4882a593Smuzhiyun * As a device's registers are implemented on the AVMM bus address space, it
154*4882a593Smuzhiyun * requires the driver to issue formatted requests to spi slave to AVMM bus
155*4882a593Smuzhiyun * master bridge to perform register access.
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun struct spi_avmm_bridge {
158*4882a593Smuzhiyun struct spi_device *spi;
159*4882a593Smuzhiyun unsigned char word_len;
160*4882a593Smuzhiyun unsigned int trans_len;
161*4882a593Smuzhiyun unsigned int phy_len;
162*4882a593Smuzhiyun /* bridge buffer used in translation between protocol layers */
163*4882a593Smuzhiyun char trans_buf[TRANS_BUF_SIZE];
164*4882a593Smuzhiyun char phy_buf[PHY_BUF_SIZE];
165*4882a593Smuzhiyun void (*swap_words)(char *buf, unsigned int len);
166*4882a593Smuzhiyun };
167*4882a593Smuzhiyun
br_swap_words_32(char * buf,unsigned int len)168*4882a593Smuzhiyun static void br_swap_words_32(char *buf, unsigned int len)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun u32 *p = (u32 *)buf;
171*4882a593Smuzhiyun unsigned int count;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun count = len / 4;
174*4882a593Smuzhiyun while (count--) {
175*4882a593Smuzhiyun *p = swab32p(p);
176*4882a593Smuzhiyun p++;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /*
181*4882a593Smuzhiyun * Format transaction layer data in br->trans_buf according to the register
182*4882a593Smuzhiyun * access request, Store valid transaction layer data length in br->trans_len.
183*4882a593Smuzhiyun */
br_trans_tx_prepare(struct spi_avmm_bridge * br,bool is_read,u32 reg,u32 * wr_val,u32 count)184*4882a593Smuzhiyun static int br_trans_tx_prepare(struct spi_avmm_bridge *br, bool is_read, u32 reg,
185*4882a593Smuzhiyun u32 *wr_val, u32 count)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun struct trans_req_header *header;
188*4882a593Smuzhiyun unsigned int trans_len;
189*4882a593Smuzhiyun u8 code;
190*4882a593Smuzhiyun __le32 *data;
191*4882a593Smuzhiyun int i;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun if (is_read) {
194*4882a593Smuzhiyun if (count == 1)
195*4882a593Smuzhiyun code = TRANS_CODE_READ;
196*4882a593Smuzhiyun else
197*4882a593Smuzhiyun code = TRANS_CODE_SEQ_READ;
198*4882a593Smuzhiyun } else {
199*4882a593Smuzhiyun if (count == 1)
200*4882a593Smuzhiyun code = TRANS_CODE_WRITE;
201*4882a593Smuzhiyun else
202*4882a593Smuzhiyun code = TRANS_CODE_SEQ_WRITE;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun header = (struct trans_req_header *)br->trans_buf;
206*4882a593Smuzhiyun header->code = code;
207*4882a593Smuzhiyun header->rsvd = 0;
208*4882a593Smuzhiyun header->size = cpu_to_be16((u16)count * SPI_AVMM_VAL_SIZE);
209*4882a593Smuzhiyun header->addr = cpu_to_be32(reg);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun trans_len = TRANS_REQ_HD_SIZE;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun if (!is_read) {
214*4882a593Smuzhiyun trans_len += SPI_AVMM_VAL_SIZE * count;
215*4882a593Smuzhiyun if (trans_len > sizeof(br->trans_buf))
216*4882a593Smuzhiyun return -ENOMEM;
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun data = (__le32 *)(br->trans_buf + TRANS_REQ_HD_SIZE);
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun for (i = 0; i < count; i++)
221*4882a593Smuzhiyun *data++ = cpu_to_le32(*wr_val++);
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /* Store valid trans data length for next layer */
225*4882a593Smuzhiyun br->trans_len = trans_len;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun return 0;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /*
231*4882a593Smuzhiyun * Convert transaction layer data (in br->trans_buf) to phy layer data, store
232*4882a593Smuzhiyun * them in br->phy_buf. Pad the phy_buf aligned with SPI's BPW. Store valid phy
233*4882a593Smuzhiyun * layer data length in br->phy_len.
234*4882a593Smuzhiyun *
235*4882a593Smuzhiyun * phy_buf len should be aligned with SPI's BPW. Spare bytes should be padded
236*4882a593Smuzhiyun * with PHY_IDLE, then the slave will just drop them.
237*4882a593Smuzhiyun *
238*4882a593Smuzhiyun * The driver will not simply pad 4a at the tail. The concern is that driver
239*4882a593Smuzhiyun * will not store MISO data during tx phase, if the driver pads 4a at the tail,
240*4882a593Smuzhiyun * it is possible that if the slave is fast enough to response at the padding
241*4882a593Smuzhiyun * time. As a result these rx bytes are lost. In the following case, 7a,7c,00
242*4882a593Smuzhiyun * will lost.
243*4882a593Smuzhiyun * MOSI ...|7a|7c|00|10| |00|00|04|02| |4b|7d|5a|7b| |40|4a|4a|4a| |XX|XX|...
244*4882a593Smuzhiyun * MISO ...|4a|4a|4a|4a| |4a|4a|4a|4a| |4a|4a|4a|4a| |4a|7a|7c|00| |78|56|...
245*4882a593Smuzhiyun *
246*4882a593Smuzhiyun * So the driver moves EOP and bytes after EOP to the end of the aligned size,
247*4882a593Smuzhiyun * then fill the hole with PHY_IDLE. As following:
248*4882a593Smuzhiyun * before pad ...|7a|7c|00|10| |00|00|04|02| |4b|7d|5a|7b| |40|
249*4882a593Smuzhiyun * after pad ...|7a|7c|00|10| |00|00|04|02| |4b|7d|5a|4a| |4a|4a|7b|40|
250*4882a593Smuzhiyun * Then if the slave will not get the entire packet before the tx phase is
251*4882a593Smuzhiyun * over, it can't responsed to anything either.
252*4882a593Smuzhiyun */
br_pkt_phy_tx_prepare(struct spi_avmm_bridge * br)253*4882a593Smuzhiyun static int br_pkt_phy_tx_prepare(struct spi_avmm_bridge *br)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun char *tb, *tb_end, *pb, *pb_limit, *pb_eop = NULL;
256*4882a593Smuzhiyun unsigned int aligned_phy_len, move_size;
257*4882a593Smuzhiyun bool need_esc = false;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun tb = br->trans_buf;
260*4882a593Smuzhiyun tb_end = tb + br->trans_len;
261*4882a593Smuzhiyun pb = br->phy_buf;
262*4882a593Smuzhiyun pb_limit = pb + ARRAY_SIZE(br->phy_buf);
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun *pb++ = PKT_SOP;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun /*
267*4882a593Smuzhiyun * The driver doesn't support multiple channels so the channel number
268*4882a593Smuzhiyun * is always 0.
269*4882a593Smuzhiyun */
270*4882a593Smuzhiyun *pb++ = PKT_CHANNEL;
271*4882a593Smuzhiyun *pb++ = 0x0;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun for (; pb < pb_limit && tb < tb_end; pb++) {
274*4882a593Smuzhiyun if (need_esc) {
275*4882a593Smuzhiyun *pb = *tb++ ^ 0x20;
276*4882a593Smuzhiyun need_esc = false;
277*4882a593Smuzhiyun continue;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun /* EOP should be inserted before the last valid char */
281*4882a593Smuzhiyun if (tb == tb_end - 1 && !pb_eop) {
282*4882a593Smuzhiyun *pb = PKT_EOP;
283*4882a593Smuzhiyun pb_eop = pb;
284*4882a593Smuzhiyun continue;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /*
288*4882a593Smuzhiyun * insert an ESCAPE char if the data value equals any special
289*4882a593Smuzhiyun * char.
290*4882a593Smuzhiyun */
291*4882a593Smuzhiyun switch (*tb) {
292*4882a593Smuzhiyun case PKT_SOP:
293*4882a593Smuzhiyun case PKT_EOP:
294*4882a593Smuzhiyun case PKT_CHANNEL:
295*4882a593Smuzhiyun case PKT_ESC:
296*4882a593Smuzhiyun *pb = PKT_ESC;
297*4882a593Smuzhiyun need_esc = true;
298*4882a593Smuzhiyun break;
299*4882a593Smuzhiyun case PHY_IDLE:
300*4882a593Smuzhiyun case PHY_ESC:
301*4882a593Smuzhiyun *pb = PHY_ESC;
302*4882a593Smuzhiyun need_esc = true;
303*4882a593Smuzhiyun break;
304*4882a593Smuzhiyun default:
305*4882a593Smuzhiyun *pb = *tb++;
306*4882a593Smuzhiyun break;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun /* The phy buffer is used out but transaction layer data remains */
311*4882a593Smuzhiyun if (tb < tb_end)
312*4882a593Smuzhiyun return -ENOMEM;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun /* Store valid phy data length for spi transfer */
315*4882a593Smuzhiyun br->phy_len = pb - br->phy_buf;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun if (br->word_len == 1)
318*4882a593Smuzhiyun return 0;
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun /* Do phy buf padding if word_len > 1 byte. */
321*4882a593Smuzhiyun aligned_phy_len = ALIGN(br->phy_len, br->word_len);
322*4882a593Smuzhiyun if (aligned_phy_len > sizeof(br->phy_buf))
323*4882a593Smuzhiyun return -ENOMEM;
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun if (aligned_phy_len == br->phy_len)
326*4882a593Smuzhiyun return 0;
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun /* move EOP and bytes after EOP to the end of aligned size */
329*4882a593Smuzhiyun move_size = pb - pb_eop;
330*4882a593Smuzhiyun memmove(&br->phy_buf[aligned_phy_len - move_size], pb_eop, move_size);
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun /* fill the hole with PHY_IDLEs */
333*4882a593Smuzhiyun memset(pb_eop, PHY_IDLE, aligned_phy_len - br->phy_len);
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /* update the phy data length */
336*4882a593Smuzhiyun br->phy_len = aligned_phy_len;
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun return 0;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun /*
342*4882a593Smuzhiyun * In tx phase, the slave only returns PHY_IDLE (0x4a). So the driver will
343*4882a593Smuzhiyun * ignore rx in tx phase.
344*4882a593Smuzhiyun */
br_do_tx(struct spi_avmm_bridge * br)345*4882a593Smuzhiyun static int br_do_tx(struct spi_avmm_bridge *br)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun /* reorder words for spi transfer */
348*4882a593Smuzhiyun if (br->swap_words)
349*4882a593Smuzhiyun br->swap_words(br->phy_buf, br->phy_len);
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /* send all data in phy_buf */
352*4882a593Smuzhiyun return spi_write(br->spi, br->phy_buf, br->phy_len);
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun /*
356*4882a593Smuzhiyun * This function read the rx byte stream from SPI word by word and convert
357*4882a593Smuzhiyun * them to transaction layer data in br->trans_buf. It also stores the length
358*4882a593Smuzhiyun * of rx transaction layer data in br->trans_len
359*4882a593Smuzhiyun *
360*4882a593Smuzhiyun * The slave may send an unknown number of PHY_IDLEs in rx phase, so we cannot
361*4882a593Smuzhiyun * prepare a fixed length buffer to receive all of the rx data in a batch. We
362*4882a593Smuzhiyun * have to read word by word and convert them to transaction layer data at
363*4882a593Smuzhiyun * once.
364*4882a593Smuzhiyun */
br_do_rx_and_pkt_phy_parse(struct spi_avmm_bridge * br)365*4882a593Smuzhiyun static int br_do_rx_and_pkt_phy_parse(struct spi_avmm_bridge *br)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun bool eop_found = false, channel_found = false, esc_found = false;
368*4882a593Smuzhiyun bool valid_word = false, last_try = false;
369*4882a593Smuzhiyun struct device *dev = &br->spi->dev;
370*4882a593Smuzhiyun char *pb, *tb_limit, *tb = NULL;
371*4882a593Smuzhiyun unsigned long poll_timeout;
372*4882a593Smuzhiyun int ret, i;
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun tb_limit = br->trans_buf + ARRAY_SIZE(br->trans_buf);
375*4882a593Smuzhiyun pb = br->phy_buf;
376*4882a593Smuzhiyun poll_timeout = jiffies + SPI_AVMM_XFER_TIMEOUT;
377*4882a593Smuzhiyun while (tb < tb_limit) {
378*4882a593Smuzhiyun ret = spi_read(br->spi, pb, br->word_len);
379*4882a593Smuzhiyun if (ret)
380*4882a593Smuzhiyun return ret;
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun /* reorder the word back */
383*4882a593Smuzhiyun if (br->swap_words)
384*4882a593Smuzhiyun br->swap_words(pb, br->word_len);
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun valid_word = false;
387*4882a593Smuzhiyun for (i = 0; i < br->word_len; i++) {
388*4882a593Smuzhiyun /* drop everything before first SOP */
389*4882a593Smuzhiyun if (!tb && pb[i] != PKT_SOP)
390*4882a593Smuzhiyun continue;
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun /* drop PHY_IDLE */
393*4882a593Smuzhiyun if (pb[i] == PHY_IDLE)
394*4882a593Smuzhiyun continue;
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun valid_word = true;
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun /*
399*4882a593Smuzhiyun * We don't support multiple channels, so error out if
400*4882a593Smuzhiyun * a non-zero channel number is found.
401*4882a593Smuzhiyun */
402*4882a593Smuzhiyun if (channel_found) {
403*4882a593Smuzhiyun if (pb[i] != 0) {
404*4882a593Smuzhiyun dev_err(dev, "%s channel num != 0\n",
405*4882a593Smuzhiyun __func__);
406*4882a593Smuzhiyun return -EFAULT;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun channel_found = false;
410*4882a593Smuzhiyun continue;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun switch (pb[i]) {
414*4882a593Smuzhiyun case PKT_SOP:
415*4882a593Smuzhiyun /*
416*4882a593Smuzhiyun * reset the parsing if a second SOP appears.
417*4882a593Smuzhiyun */
418*4882a593Smuzhiyun tb = br->trans_buf;
419*4882a593Smuzhiyun eop_found = false;
420*4882a593Smuzhiyun channel_found = false;
421*4882a593Smuzhiyun esc_found = false;
422*4882a593Smuzhiyun break;
423*4882a593Smuzhiyun case PKT_EOP:
424*4882a593Smuzhiyun /*
425*4882a593Smuzhiyun * No special char is expected after ESC char.
426*4882a593Smuzhiyun * No special char (except ESC & PHY_IDLE) is
427*4882a593Smuzhiyun * expected after EOP char.
428*4882a593Smuzhiyun *
429*4882a593Smuzhiyun * The special chars are all dropped.
430*4882a593Smuzhiyun */
431*4882a593Smuzhiyun if (esc_found || eop_found)
432*4882a593Smuzhiyun return -EFAULT;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun eop_found = true;
435*4882a593Smuzhiyun break;
436*4882a593Smuzhiyun case PKT_CHANNEL:
437*4882a593Smuzhiyun if (esc_found || eop_found)
438*4882a593Smuzhiyun return -EFAULT;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun channel_found = true;
441*4882a593Smuzhiyun break;
442*4882a593Smuzhiyun case PKT_ESC:
443*4882a593Smuzhiyun case PHY_ESC:
444*4882a593Smuzhiyun if (esc_found)
445*4882a593Smuzhiyun return -EFAULT;
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun esc_found = true;
448*4882a593Smuzhiyun break;
449*4882a593Smuzhiyun default:
450*4882a593Smuzhiyun /* Record the normal byte in trans_buf. */
451*4882a593Smuzhiyun if (esc_found) {
452*4882a593Smuzhiyun *tb++ = pb[i] ^ 0x20;
453*4882a593Smuzhiyun esc_found = false;
454*4882a593Smuzhiyun } else {
455*4882a593Smuzhiyun *tb++ = pb[i];
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun /*
459*4882a593Smuzhiyun * We get the last normal byte after EOP, it is
460*4882a593Smuzhiyun * time we finish. Normally the function should
461*4882a593Smuzhiyun * return here.
462*4882a593Smuzhiyun */
463*4882a593Smuzhiyun if (eop_found) {
464*4882a593Smuzhiyun br->trans_len = tb - br->trans_buf;
465*4882a593Smuzhiyun return 0;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun if (valid_word) {
471*4882a593Smuzhiyun /* update poll timeout when we get valid word */
472*4882a593Smuzhiyun poll_timeout = jiffies + SPI_AVMM_XFER_TIMEOUT;
473*4882a593Smuzhiyun last_try = false;
474*4882a593Smuzhiyun } else {
475*4882a593Smuzhiyun /*
476*4882a593Smuzhiyun * We timeout when rx keeps invalid for some time. But
477*4882a593Smuzhiyun * it is possible we are scheduled out for long time
478*4882a593Smuzhiyun * after a spi_read. So when we are scheduled in, a SW
479*4882a593Smuzhiyun * timeout happens. But actually HW may have worked fine and
480*4882a593Smuzhiyun * has been ready long time ago. So we need to do an extra
481*4882a593Smuzhiyun * read, if we get a valid word then we could continue rx,
482*4882a593Smuzhiyun * otherwise real a HW issue happens.
483*4882a593Smuzhiyun */
484*4882a593Smuzhiyun if (last_try)
485*4882a593Smuzhiyun return -ETIMEDOUT;
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun if (time_after(jiffies, poll_timeout))
488*4882a593Smuzhiyun last_try = true;
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun /*
493*4882a593Smuzhiyun * We have used out all transfer layer buffer but cannot find the end
494*4882a593Smuzhiyun * of the byte stream.
495*4882a593Smuzhiyun */
496*4882a593Smuzhiyun dev_err(dev, "%s transfer buffer is full but rx doesn't end\n",
497*4882a593Smuzhiyun __func__);
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun return -EFAULT;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun /*
503*4882a593Smuzhiyun * For read transactions, the avmm bus will directly return register values
504*4882a593Smuzhiyun * without transaction response header.
505*4882a593Smuzhiyun */
br_rd_trans_rx_parse(struct spi_avmm_bridge * br,u32 * val,unsigned int expected_count)506*4882a593Smuzhiyun static int br_rd_trans_rx_parse(struct spi_avmm_bridge *br,
507*4882a593Smuzhiyun u32 *val, unsigned int expected_count)
508*4882a593Smuzhiyun {
509*4882a593Smuzhiyun unsigned int i, trans_len = br->trans_len;
510*4882a593Smuzhiyun __le32 *data;
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun if (expected_count * SPI_AVMM_VAL_SIZE != trans_len)
513*4882a593Smuzhiyun return -EFAULT;
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun data = (__le32 *)br->trans_buf;
516*4882a593Smuzhiyun for (i = 0; i < expected_count; i++)
517*4882a593Smuzhiyun *val++ = le32_to_cpu(*data++);
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun return 0;
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun /*
523*4882a593Smuzhiyun * For write transactions, the slave will return a transaction response
524*4882a593Smuzhiyun * header.
525*4882a593Smuzhiyun */
br_wr_trans_rx_parse(struct spi_avmm_bridge * br,unsigned int expected_count)526*4882a593Smuzhiyun static int br_wr_trans_rx_parse(struct spi_avmm_bridge *br,
527*4882a593Smuzhiyun unsigned int expected_count)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun unsigned int trans_len = br->trans_len;
530*4882a593Smuzhiyun struct trans_resp_header *resp;
531*4882a593Smuzhiyun u8 code;
532*4882a593Smuzhiyun u16 val_len;
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun if (trans_len != TRANS_RESP_HD_SIZE)
535*4882a593Smuzhiyun return -EFAULT;
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun resp = (struct trans_resp_header *)br->trans_buf;
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun code = resp->r_code ^ 0x80;
540*4882a593Smuzhiyun val_len = be16_to_cpu(resp->size);
541*4882a593Smuzhiyun if (!val_len || val_len != expected_count * SPI_AVMM_VAL_SIZE)
542*4882a593Smuzhiyun return -EFAULT;
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun /* error out if the trans code doesn't align with the val size */
545*4882a593Smuzhiyun if ((val_len == SPI_AVMM_VAL_SIZE && code != TRANS_CODE_WRITE) ||
546*4882a593Smuzhiyun (val_len > SPI_AVMM_VAL_SIZE && code != TRANS_CODE_SEQ_WRITE))
547*4882a593Smuzhiyun return -EFAULT;
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun return 0;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun
do_reg_access(void * context,bool is_read,unsigned int reg,unsigned int * value,unsigned int count)552*4882a593Smuzhiyun static int do_reg_access(void *context, bool is_read, unsigned int reg,
553*4882a593Smuzhiyun unsigned int *value, unsigned int count)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun struct spi_avmm_bridge *br = context;
556*4882a593Smuzhiyun int ret;
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun /* invalidate bridge buffers first */
559*4882a593Smuzhiyun br->trans_len = 0;
560*4882a593Smuzhiyun br->phy_len = 0;
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun ret = br_trans_tx_prepare(br, is_read, reg, value, count);
563*4882a593Smuzhiyun if (ret)
564*4882a593Smuzhiyun return ret;
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun ret = br_pkt_phy_tx_prepare(br);
567*4882a593Smuzhiyun if (ret)
568*4882a593Smuzhiyun return ret;
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun ret = br_do_tx(br);
571*4882a593Smuzhiyun if (ret)
572*4882a593Smuzhiyun return ret;
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun ret = br_do_rx_and_pkt_phy_parse(br);
575*4882a593Smuzhiyun if (ret)
576*4882a593Smuzhiyun return ret;
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun if (is_read)
579*4882a593Smuzhiyun return br_rd_trans_rx_parse(br, value, count);
580*4882a593Smuzhiyun else
581*4882a593Smuzhiyun return br_wr_trans_rx_parse(br, count);
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun
regmap_spi_avmm_gather_write(void * context,const void * reg_buf,size_t reg_len,const void * val_buf,size_t val_len)584*4882a593Smuzhiyun static int regmap_spi_avmm_gather_write(void *context,
585*4882a593Smuzhiyun const void *reg_buf, size_t reg_len,
586*4882a593Smuzhiyun const void *val_buf, size_t val_len)
587*4882a593Smuzhiyun {
588*4882a593Smuzhiyun if (reg_len != SPI_AVMM_REG_SIZE)
589*4882a593Smuzhiyun return -EINVAL;
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun if (!IS_ALIGNED(val_len, SPI_AVMM_VAL_SIZE))
592*4882a593Smuzhiyun return -EINVAL;
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun return do_reg_access(context, false, *(u32 *)reg_buf, (u32 *)val_buf,
595*4882a593Smuzhiyun val_len / SPI_AVMM_VAL_SIZE);
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun
regmap_spi_avmm_write(void * context,const void * data,size_t bytes)598*4882a593Smuzhiyun static int regmap_spi_avmm_write(void *context, const void *data, size_t bytes)
599*4882a593Smuzhiyun {
600*4882a593Smuzhiyun if (bytes < SPI_AVMM_REG_SIZE + SPI_AVMM_VAL_SIZE)
601*4882a593Smuzhiyun return -EINVAL;
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun return regmap_spi_avmm_gather_write(context, data, SPI_AVMM_REG_SIZE,
604*4882a593Smuzhiyun data + SPI_AVMM_REG_SIZE,
605*4882a593Smuzhiyun bytes - SPI_AVMM_REG_SIZE);
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun
regmap_spi_avmm_read(void * context,const void * reg_buf,size_t reg_len,void * val_buf,size_t val_len)608*4882a593Smuzhiyun static int regmap_spi_avmm_read(void *context,
609*4882a593Smuzhiyun const void *reg_buf, size_t reg_len,
610*4882a593Smuzhiyun void *val_buf, size_t val_len)
611*4882a593Smuzhiyun {
612*4882a593Smuzhiyun if (reg_len != SPI_AVMM_REG_SIZE)
613*4882a593Smuzhiyun return -EINVAL;
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun if (!IS_ALIGNED(val_len, SPI_AVMM_VAL_SIZE))
616*4882a593Smuzhiyun return -EINVAL;
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun return do_reg_access(context, true, *(u32 *)reg_buf, val_buf,
619*4882a593Smuzhiyun (val_len / SPI_AVMM_VAL_SIZE));
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun static struct spi_avmm_bridge *
spi_avmm_bridge_ctx_gen(struct spi_device * spi)623*4882a593Smuzhiyun spi_avmm_bridge_ctx_gen(struct spi_device *spi)
624*4882a593Smuzhiyun {
625*4882a593Smuzhiyun struct spi_avmm_bridge *br;
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun if (!spi)
628*4882a593Smuzhiyun return ERR_PTR(-ENODEV);
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun /* Only support BPW == 8 or 32 now. Try 32 BPW first. */
631*4882a593Smuzhiyun spi->mode = SPI_MODE_1;
632*4882a593Smuzhiyun spi->bits_per_word = 32;
633*4882a593Smuzhiyun if (spi_setup(spi)) {
634*4882a593Smuzhiyun spi->bits_per_word = 8;
635*4882a593Smuzhiyun if (spi_setup(spi))
636*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun br = kzalloc(sizeof(*br), GFP_KERNEL);
640*4882a593Smuzhiyun if (!br)
641*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun br->spi = spi;
644*4882a593Smuzhiyun br->word_len = spi->bits_per_word / 8;
645*4882a593Smuzhiyun if (br->word_len == 4) {
646*4882a593Smuzhiyun /*
647*4882a593Smuzhiyun * The protocol requires little endian byte order but MSB
648*4882a593Smuzhiyun * first. So driver needs to swap the byte order word by word
649*4882a593Smuzhiyun * if word length > 1.
650*4882a593Smuzhiyun */
651*4882a593Smuzhiyun br->swap_words = br_swap_words_32;
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun return br;
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun
spi_avmm_bridge_ctx_free(void * context)657*4882a593Smuzhiyun static void spi_avmm_bridge_ctx_free(void *context)
658*4882a593Smuzhiyun {
659*4882a593Smuzhiyun kfree(context);
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun static const struct regmap_bus regmap_spi_avmm_bus = {
663*4882a593Smuzhiyun .write = regmap_spi_avmm_write,
664*4882a593Smuzhiyun .gather_write = regmap_spi_avmm_gather_write,
665*4882a593Smuzhiyun .read = regmap_spi_avmm_read,
666*4882a593Smuzhiyun .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
667*4882a593Smuzhiyun .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
668*4882a593Smuzhiyun .max_raw_read = SPI_AVMM_VAL_SIZE * MAX_READ_CNT,
669*4882a593Smuzhiyun .max_raw_write = SPI_AVMM_VAL_SIZE * MAX_WRITE_CNT,
670*4882a593Smuzhiyun .free_context = spi_avmm_bridge_ctx_free,
671*4882a593Smuzhiyun };
672*4882a593Smuzhiyun
__regmap_init_spi_avmm(struct spi_device * spi,const struct regmap_config * config,struct lock_class_key * lock_key,const char * lock_name)673*4882a593Smuzhiyun struct regmap *__regmap_init_spi_avmm(struct spi_device *spi,
674*4882a593Smuzhiyun const struct regmap_config *config,
675*4882a593Smuzhiyun struct lock_class_key *lock_key,
676*4882a593Smuzhiyun const char *lock_name)
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun struct spi_avmm_bridge *bridge;
679*4882a593Smuzhiyun struct regmap *map;
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun bridge = spi_avmm_bridge_ctx_gen(spi);
682*4882a593Smuzhiyun if (IS_ERR(bridge))
683*4882a593Smuzhiyun return ERR_CAST(bridge);
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun map = __regmap_init(&spi->dev, ®map_spi_avmm_bus,
686*4882a593Smuzhiyun bridge, config, lock_key, lock_name);
687*4882a593Smuzhiyun if (IS_ERR(map)) {
688*4882a593Smuzhiyun spi_avmm_bridge_ctx_free(bridge);
689*4882a593Smuzhiyun return ERR_CAST(map);
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun return map;
693*4882a593Smuzhiyun }
694*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__regmap_init_spi_avmm);
695*4882a593Smuzhiyun
__devm_regmap_init_spi_avmm(struct spi_device * spi,const struct regmap_config * config,struct lock_class_key * lock_key,const char * lock_name)696*4882a593Smuzhiyun struct regmap *__devm_regmap_init_spi_avmm(struct spi_device *spi,
697*4882a593Smuzhiyun const struct regmap_config *config,
698*4882a593Smuzhiyun struct lock_class_key *lock_key,
699*4882a593Smuzhiyun const char *lock_name)
700*4882a593Smuzhiyun {
701*4882a593Smuzhiyun struct spi_avmm_bridge *bridge;
702*4882a593Smuzhiyun struct regmap *map;
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun bridge = spi_avmm_bridge_ctx_gen(spi);
705*4882a593Smuzhiyun if (IS_ERR(bridge))
706*4882a593Smuzhiyun return ERR_CAST(bridge);
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun map = __devm_regmap_init(&spi->dev, ®map_spi_avmm_bus,
709*4882a593Smuzhiyun bridge, config, lock_key, lock_name);
710*4882a593Smuzhiyun if (IS_ERR(map)) {
711*4882a593Smuzhiyun spi_avmm_bridge_ctx_free(bridge);
712*4882a593Smuzhiyun return ERR_CAST(map);
713*4882a593Smuzhiyun }
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun return map;
716*4882a593Smuzhiyun }
717*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__devm_regmap_init_spi_avmm);
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
720