raw/ifpga/base: align send buffer for SPI
[dpdk.git] / drivers / raw / ifpga / base / opae_spi_transaction.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2019 Intel Corporation
3  */
4
5 #include "opae_spi.h"
6 #include "ifpga_compat.h"
7
8 /*transaction opcodes*/
9 #define SPI_TRAN_SEQ_WRITE 0x04 /* SPI transaction sequential write */
10 #define SPI_TRAN_SEQ_READ  0x14 /* SPI transaction sequential read */
11 #define SPI_TRAN_NON_SEQ_WRITE 0x00 /* SPI transaction non-sequential write */
12 #define SPI_TRAN_NON_SEQ_READ  0x10 /* SPI transaction non-sequential read*/
13
14 /*specail packet characters*/
15 #define SPI_PACKET_SOP     0x7a
16 #define SPI_PACKET_EOP     0x7b
17 #define SPI_PACKET_CHANNEL 0x7c
18 #define SPI_PACKET_ESC     0x7d
19
20 /*special byte characters*/
21 #define SPI_BYTE_IDLE 0x4a
22 #define SPI_BYTE_ESC  0x4d
23
24 #define SPI_REG_BYTES 4
25
26 #define INIT_SPI_TRAN_HEADER(trans_type, size, address) \
27 ({ \
28         header.trans_type = trans_type; \
29         header.reserve = 0; \
30         header.size = cpu_to_be16(size); \
31         header.addr = cpu_to_be32(addr); \
32 })
33
34 #ifdef OPAE_SPI_DEBUG
35 static void print_buffer(const char *string, void *buffer, int len)
36 {
37         int i;
38         unsigned char *p = buffer;
39
40         printf("%s print buffer, len=%d\n", string, len);
41
42         for (i = 0; i < len; i++)
43                 printf("%x ", *(p+i));
44         printf("\n");
45 }
46 #else
47 static void print_buffer(const char *string, void *buffer, int len)
48 {
49         UNUSED(string);
50         UNUSED(buffer);
51         UNUSED(len);
52 }
53 #endif
54
55 static unsigned char xor_20(unsigned char val)
56 {
57         return val^0x20;
58 }
59
60 static void reorder_phy_data(u8 bits_per_word,
61                 void *buf, unsigned int len)
62 {
63         unsigned int count = len / (bits_per_word/8);
64         u32 *p;
65
66         if (bits_per_word == 32) {
67                 p = (u32 *)buf;
68                 while (count--) {
69                         *p = cpu_to_be32(*p);
70                         p++;
71                 }
72         }
73 }
74
75 enum {
76         SPI_FOUND_SOP,
77         SPI_FOUND_EOP,
78         SPI_NOT_FOUND,
79 };
80
81 static int resp_find_sop_eop(unsigned char *resp, unsigned int len,
82                 int flags)
83 {
84         int ret = SPI_NOT_FOUND;
85
86         unsigned char *b = resp;
87
88         /* find SOP */
89         if (flags != SPI_FOUND_SOP) {
90                 while (b < resp + len && *b != SPI_PACKET_SOP)
91                         b++;
92
93                 if (*b != SPI_PACKET_SOP)
94                         goto done;
95
96                 ret = SPI_FOUND_SOP;
97         }
98
99         /* find EOP */
100         while (b < resp + len && *b != SPI_PACKET_EOP)
101                 b++;
102
103         if (*b != SPI_PACKET_EOP)
104                 goto done;
105
106         ret = SPI_FOUND_EOP;
107
108 done:
109         return ret;
110 }
111
112 static void phy_tx_pad(unsigned char *phy_buf, unsigned int phy_buf_len,
113                 unsigned int *aligned_len)
114 {
115         unsigned char *p = &phy_buf[phy_buf_len - 1], *dst_p;
116
117         *aligned_len = IFPGA_ALIGN(phy_buf_len, 4);
118
119         if (*aligned_len == phy_buf_len)
120                 return;
121
122         dst_p = &phy_buf[*aligned_len - 1];
123
124         /* move EOP and bytes after EOP to the end of aligned size */
125         while (p > phy_buf) {
126                 *dst_p = *p;
127
128                 if (*p == SPI_PACKET_EOP)
129                         break;
130
131                 p--;
132                 dst_p--;
133         }
134
135         /* fill the hole with PHY_IDLE */
136         while (p < dst_p)
137                 *p++ = SPI_BYTE_IDLE;
138 }
139
140 static int byte_to_core_convert(struct spi_transaction_dev *dev,
141                 unsigned int send_len, unsigned char *send_data,
142                 unsigned int resp_len, unsigned char *resp_data,
143                 unsigned int *valid_resp_len)
144 {
145         unsigned int i;
146         int ret = 0;
147         unsigned char *send_packet = dev->buffer->bytes_send;
148         unsigned char *resp_packet = dev->buffer->bytes_resp;
149         unsigned char *p;
150         unsigned char current_byte;
151         unsigned char *tx_buffer;
152         unsigned int tx_len = 0;
153         unsigned char *rx_buffer;
154         unsigned int rx_len = 0;
155         int retry = 0;
156         int spi_flags;
157         unsigned int resp_max_len = 2 * resp_len;
158
159         print_buffer("before bytes:", send_data, send_len);
160
161         p = send_packet;
162
163         for (i = 0; i < send_len; i++) {
164                 current_byte = send_data[i];
165                 switch (current_byte) {
166                 case SPI_BYTE_IDLE:
167                         *p++ = SPI_BYTE_IDLE;
168                         *p++ = xor_20(current_byte);
169                         break;
170                 case SPI_BYTE_ESC:
171                         *p++ = SPI_BYTE_ESC;
172                         *p++ = xor_20(current_byte);
173                         break;
174                 default:
175                         *p++ = current_byte;
176                         break;
177                 }
178         }
179
180         tx_len = p - send_packet;
181
182         print_buffer("before spi:", send_packet, tx_len);
183
184         phy_tx_pad(send_packet, tx_len, &tx_len);
185         print_buffer("after pad:", send_packet, tx_len);
186
187         reorder_phy_data(32, send_packet, tx_len);
188
189         print_buffer("after order to spi:", send_packet, tx_len);
190
191         /* call spi */
192         tx_buffer = send_packet;
193         rx_buffer = resp_packet;
194         rx_len = resp_max_len;
195         spi_flags = SPI_NOT_FOUND;
196
197 read_again:
198         ret = spi_command(dev->dev, dev->chipselect, tx_len, tx_buffer,
199                         rx_len, rx_buffer);
200         if (ret)
201                 return -EBUSY;
202
203         print_buffer("read from spi:", rx_buffer, rx_len);
204
205         /* look for SOP firstly*/
206         ret = resp_find_sop_eop(rx_buffer, rx_len - 1, spi_flags);
207         if (ret != SPI_FOUND_EOP) {
208                 tx_buffer = NULL;
209                 tx_len = 0;
210                 if (retry++ > 10) {
211                         dev_err(NULL, "cannot found valid data from SPI\n");
212                         return -EBUSY;
213                 }
214
215                 if (ret == SPI_FOUND_SOP) {
216                         rx_buffer += rx_len;
217                         resp_max_len += rx_len;
218                 }
219
220                 spi_flags = ret;
221                 goto read_again;
222         }
223
224         print_buffer("found valid data:", resp_packet, resp_max_len);
225
226         /* analyze response packet */
227         i = 0;
228         p = resp_data;
229         while (i < resp_max_len) {
230                 current_byte = resp_packet[i];
231                 switch (current_byte) {
232                 case SPI_BYTE_IDLE:
233                         i++;
234                         break;
235                 case SPI_BYTE_ESC:
236                         i++;
237                         current_byte = resp_packet[i];
238                         *p++ = xor_20(current_byte);
239                         i++;
240                         break;
241                 default:
242                         *p++ = current_byte;
243                         i++;
244                         break;
245                 }
246         }
247
248         /* receive "4a" means the SPI is idle, not valid data */
249         *valid_resp_len = p - resp_data;
250         if (*valid_resp_len == 0) {
251                 dev_err(NULL, "error: repond package without valid data\n");
252                 return -EINVAL;
253         }
254
255         return 0;
256 }
257
258 static int packet_to_byte_conver(struct spi_transaction_dev *dev,
259                 unsigned int send_len, unsigned char *send_buf,
260                 unsigned int resp_len, unsigned char *resp_buf,
261                 unsigned int *valid)
262 {
263         int ret = 0;
264         unsigned int i;
265         unsigned char current_byte;
266         unsigned int resp_max_len;
267         unsigned char *send_packet = dev->buffer->packet_send;
268         unsigned char *resp_packet = dev->buffer->packet_resp;
269         unsigned char *p;
270         unsigned int valid_resp_len = 0;
271
272         print_buffer("before packet:", send_buf, send_len);
273
274         resp_max_len = 2 * resp_len + 4;
275
276         p = send_packet;
277
278         /* SOP header */
279         *p++ = SPI_PACKET_SOP;
280
281         *p++ = SPI_PACKET_CHANNEL;
282         *p++ = 0;
283
284         /* append the data into a packet */
285         for (i = 0; i < send_len; i++) {
286                 current_byte = send_buf[i];
287
288                 /* EOP for last byte */
289                 if (i == send_len - 1)
290                         *p++ = SPI_PACKET_EOP;
291
292                 switch (current_byte) {
293                 case SPI_PACKET_SOP:
294                 case SPI_PACKET_EOP:
295                 case SPI_PACKET_CHANNEL:
296                 case SPI_PACKET_ESC:
297                         *p++ = SPI_PACKET_ESC;
298                         *p++ = xor_20(current_byte);
299                         break;
300                 default:
301                         *p++ = current_byte;
302                 }
303         }
304
305         ret = byte_to_core_convert(dev, p - send_packet,
306                         send_packet, resp_max_len, resp_packet,
307                         &valid_resp_len);
308         if (ret)
309                 return -EBUSY;
310
311         print_buffer("after byte conver:", resp_packet, valid_resp_len);
312
313         /* analyze the response packet */
314         p = resp_buf;
315
316         /* look for SOP */
317         for (i = 0; i < valid_resp_len; i++) {
318                 if (resp_packet[i] == SPI_PACKET_SOP)
319                         break;
320         }
321
322         if (i == valid_resp_len) {
323                 dev_err(NULL, "error on analyze response packet 0x%x\n",
324                                 resp_packet[i]);
325                 return -EINVAL;
326         }
327
328         i++;
329
330         /* continue parsing data after SOP */
331         while (i < valid_resp_len) {
332                 current_byte = resp_packet[i];
333
334                 switch (current_byte) {
335                 case SPI_PACKET_ESC:
336                 case SPI_PACKET_CHANNEL:
337                 case SPI_PACKET_SOP:
338                         i++;
339                         current_byte = resp_packet[i];
340                         *p++ = xor_20(current_byte);
341                         i++;
342                         break;
343                 case SPI_PACKET_EOP:
344                         i++;
345                         current_byte = resp_packet[i];
346                         if (current_byte == SPI_PACKET_ESC ||
347                                         current_byte == SPI_PACKET_CHANNEL ||
348                                         current_byte == SPI_PACKET_SOP) {
349                                 i++;
350                                 current_byte = resp_packet[i];
351                                 *p++ = xor_20(current_byte);
352                         } else
353                                 *p++ = current_byte;
354                         i = valid_resp_len;
355                         break;
356                 default:
357                         *p++ = current_byte;
358                         i++;
359                 }
360
361         }
362
363         *valid = p - resp_buf;
364
365         print_buffer("after packet:", resp_buf, *valid);
366
367         return ret;
368 }
369
370 static int do_transaction(struct spi_transaction_dev *dev, unsigned int addr,
371                 unsigned int size, unsigned char *data,
372                 unsigned int trans_type)
373 {
374
375         struct spi_tran_header header;
376         unsigned char *transaction = dev->buffer->tran_send;
377         unsigned char *response = dev->buffer->tran_resp;
378         unsigned char *p;
379         int ret = 0;
380         unsigned int i;
381         unsigned int valid_len = 0;
382
383         /* make transacation header */
384         INIT_SPI_TRAN_HEADER(trans_type, size, addr);
385
386         /* fill the header */
387         p = transaction;
388         opae_memcpy(p, &header, sizeof(struct spi_tran_header));
389         p = p + sizeof(struct spi_tran_header);
390
391         switch (trans_type) {
392         case SPI_TRAN_SEQ_WRITE:
393         case SPI_TRAN_NON_SEQ_WRITE:
394                 for (i = 0; i < size; i++)
395                         *p++ = *data++;
396
397                 ret = packet_to_byte_conver(dev, size + HEADER_LEN,
398                                 transaction, RESPONSE_LEN, response,
399                                 &valid_len);
400                 if (ret)
401                         return -EBUSY;
402
403                 /* check the result */
404                 if (size != ((unsigned int)(response[2] & 0xff) << 8 |
405                         (unsigned int)(response[3] & 0xff)))
406                         ret = -EBUSY;
407
408                 break;
409         case SPI_TRAN_SEQ_READ:
410         case SPI_TRAN_NON_SEQ_READ:
411                 ret = packet_to_byte_conver(dev, HEADER_LEN,
412                                 transaction, size, response,
413                                 &valid_len);
414                 if (ret || valid_len != size)
415                         return -EBUSY;
416
417                 for (i = 0; i < size; i++)
418                         *data++ = *response++;
419
420                 ret = 0;
421                 break;
422         }
423
424         return ret;
425 }
426
427 int spi_transaction_read(struct spi_transaction_dev *dev, unsigned int addr,
428                 unsigned int size, unsigned char *data)
429 {
430         return do_transaction(dev, addr, size, data,
431                         (size > SPI_REG_BYTES) ?
432                         SPI_TRAN_SEQ_READ : SPI_TRAN_NON_SEQ_READ);
433 }
434
435 int spi_transaction_write(struct spi_transaction_dev *dev, unsigned int addr,
436                 unsigned int size, unsigned char *data)
437 {
438         return do_transaction(dev, addr, size, data,
439                         (size > SPI_REG_BYTES) ?
440                         SPI_TRAN_SEQ_WRITE : SPI_TRAN_NON_SEQ_WRITE);
441 }
442
443 struct spi_transaction_dev *spi_transaction_init(struct altera_spi_device *dev,
444                 int chipselect)
445 {
446         struct spi_transaction_dev *spi_tran_dev;
447
448         spi_tran_dev = opae_malloc(sizeof(struct spi_transaction_dev));
449         if (!spi_tran_dev)
450                 return NULL;
451
452         spi_tran_dev->dev = dev;
453         spi_tran_dev->chipselect = chipselect;
454
455         spi_tran_dev->buffer = opae_malloc(sizeof(struct spi_tran_buffer));
456         if (!spi_tran_dev->buffer) {
457                 opae_free(spi_tran_dev);
458                 return NULL;
459         }
460
461         return spi_tran_dev;
462 }
463
464 void spi_transaction_remove(struct spi_transaction_dev *dev)
465 {
466         if (dev && dev->buffer)
467                 opae_free(dev->buffer);
468         if (dev)
469                 opae_free(dev);
470 }