blob: 6d9ab61769a782b8bf6ee4cf039503839c9d5b9a [file] [log] [blame]
Vignesh R4e341d32019-02-05 11:29:15 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
Nishanth Menoneaa39c62023-11-01 15:56:03 -05003 * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
Vignesh R4e341d32019-02-05 11:29:15 +05304 */
5
Tom Rinie8ea29d2023-11-01 12:28:10 -04006#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -07008#include <malloc.h>
Vignesh R4e341d32019-02-05 11:29:15 +05309#include <spi.h>
10#include <spi-mem.h>
11
12int spi_mem_exec_op(struct spi_slave *slave,
13 const struct spi_mem_op *op)
14{
15 unsigned int pos = 0;
16 const u8 *tx_buf = NULL;
17 u8 *rx_buf = NULL;
18 u8 *op_buf;
19 int op_len;
20 u32 flag;
21 int ret;
22 int i;
23
24 if (op->data.nbytes) {
25 if (op->data.dir == SPI_MEM_DATA_IN)
26 rx_buf = op->data.buf.in;
27 else
28 tx_buf = op->data.buf.out;
29 }
30
Pratyush Yadaved084852021-06-26 00:47:04 +053031 op_len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
Vignesh R4e341d32019-02-05 11:29:15 +053032 op_buf = calloc(1, op_len);
33
34 ret = spi_claim_bus(slave);
35 if (ret < 0)
36 return ret;
37
38 op_buf[pos++] = op->cmd.opcode;
39
40 if (op->addr.nbytes) {
41 for (i = 0; i < op->addr.nbytes; i++)
42 op_buf[pos + i] = op->addr.val >>
43 (8 * (op->addr.nbytes - i - 1));
44
45 pos += op->addr.nbytes;
46 }
47
48 if (op->dummy.nbytes)
49 memset(op_buf + pos, 0xff, op->dummy.nbytes);
50
51 /* 1st transfer: opcode + address + dummy cycles */
52 flag = SPI_XFER_BEGIN;
53 /* Make sure to set END bit if no tx or rx data messages follow */
54 if (!tx_buf && !rx_buf)
55 flag |= SPI_XFER_END;
56
57 ret = spi_xfer(slave, op_len * 8, op_buf, NULL, flag);
58 if (ret)
59 return ret;
60
61 /* 2nd transfer: rx or tx data path */
62 if (tx_buf || rx_buf) {
63 ret = spi_xfer(slave, op->data.nbytes * 8, tx_buf,
64 rx_buf, SPI_XFER_END);
65 if (ret)
66 return ret;
67 }
68
69 spi_release_bus(slave);
70
71 for (i = 0; i < pos; i++)
72 debug("%02x ", op_buf[i]);
73 debug("| [%dB %s] ",
74 tx_buf || rx_buf ? op->data.nbytes : 0,
75 tx_buf || rx_buf ? (tx_buf ? "out" : "in") : "-");
76 for (i = 0; i < op->data.nbytes; i++)
77 debug("%02x ", tx_buf ? tx_buf[i] : rx_buf[i]);
78 debug("[ret %d]\n", ret);
79
80 free(op_buf);
81
82 if (ret < 0)
83 return ret;
84
85 return 0;
86}
87
88int spi_mem_adjust_op_size(struct spi_slave *slave,
89 struct spi_mem_op *op)
90{
91 unsigned int len;
92
Pratyush Yadaved084852021-06-26 00:47:04 +053093 len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
Vignesh R4e341d32019-02-05 11:29:15 +053094 if (slave->max_write_size && len > slave->max_write_size)
95 return -EINVAL;
96
Bin Meng475be5a2021-07-28 20:50:14 +080097 if (op->data.dir == SPI_MEM_DATA_IN) {
98 if (slave->max_read_size)
99 op->data.nbytes = min(op->data.nbytes,
100 slave->max_read_size);
101 } else if (slave->max_write_size) {
Vignesh R4e341d32019-02-05 11:29:15 +0530102 op->data.nbytes = min(op->data.nbytes,
103 slave->max_write_size - len);
Bin Meng475be5a2021-07-28 20:50:14 +0800104 }
Vignesh R4e341d32019-02-05 11:29:15 +0530105
106 if (!op->data.nbytes)
107 return -EINVAL;
108
109 return 0;
110}
Pratyush Yadav0b0a2992021-06-26 00:47:14 +0530111
112static int spi_check_buswidth_req(struct spi_slave *slave, u8 buswidth, bool tx)
113{
114 u32 mode = slave->mode;
115
116 switch (buswidth) {
117 case 1:
118 return 0;
119
120 case 2:
121 if ((tx && (mode & (SPI_TX_DUAL | SPI_TX_QUAD))) ||
122 (!tx && (mode & (SPI_RX_DUAL | SPI_RX_QUAD))))
123 return 0;
124
125 break;
126
127 case 4:
128 if ((tx && (mode & SPI_TX_QUAD)) ||
129 (!tx && (mode & SPI_RX_QUAD)))
130 return 0;
131
132 break;
133 case 8:
134 if ((tx && (mode & SPI_TX_OCTAL)) ||
135 (!tx && (mode & SPI_RX_OCTAL)))
136 return 0;
137
138 break;
139
140 default:
141 break;
142 }
143
144 return -ENOTSUPP;
145}
146
147bool spi_mem_supports_op(struct spi_slave *slave, const struct spi_mem_op *op)
148{
149 if (spi_check_buswidth_req(slave, op->cmd.buswidth, true))
150 return false;
151
152 if (op->addr.nbytes &&
153 spi_check_buswidth_req(slave, op->addr.buswidth, true))
154 return false;
155
156 if (op->dummy.nbytes &&
157 spi_check_buswidth_req(slave, op->dummy.buswidth, true))
158 return false;
159
160 if (op->data.nbytes &&
161 spi_check_buswidth_req(slave, op->data.buswidth,
162 op->data.dir == SPI_MEM_DATA_OUT))
163 return false;
164
165 if (op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr)
166 return false;
167
168 if (op->cmd.nbytes != 1)
169 return false;
170
171 return true;
172}