xref: /rk3399_rockchip-uboot/drivers/spi/spi-mem-nodm.c (revision 95d92f5885bb79fd6e8597f0952b567170e39111)
1*95d92f58SVignesh R // SPDX-License-Identifier: GPL-2.0+
2*95d92f58SVignesh R /*
3*95d92f58SVignesh R  * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
4*95d92f58SVignesh R  */
5*95d92f58SVignesh R 
6*95d92f58SVignesh R #include <spi.h>
7*95d92f58SVignesh R #include <spi-mem.h>
8*95d92f58SVignesh R 
spi_mem_exec_op(struct spi_slave * slave,const struct spi_mem_op * op)9*95d92f58SVignesh R int spi_mem_exec_op(struct spi_slave *slave,
10*95d92f58SVignesh R 		    const struct spi_mem_op *op)
11*95d92f58SVignesh R {
12*95d92f58SVignesh R 	unsigned int pos = 0;
13*95d92f58SVignesh R 	const u8 *tx_buf = NULL;
14*95d92f58SVignesh R 	u8 *rx_buf = NULL;
15*95d92f58SVignesh R 	u8 *op_buf;
16*95d92f58SVignesh R 	int op_len;
17*95d92f58SVignesh R 	u32 flag;
18*95d92f58SVignesh R 	int ret;
19*95d92f58SVignesh R 	int i;
20*95d92f58SVignesh R 
21*95d92f58SVignesh R 	if (op->data.nbytes) {
22*95d92f58SVignesh R 		if (op->data.dir == SPI_MEM_DATA_IN)
23*95d92f58SVignesh R 			rx_buf = op->data.buf.in;
24*95d92f58SVignesh R 		else
25*95d92f58SVignesh R 			tx_buf = op->data.buf.out;
26*95d92f58SVignesh R 	}
27*95d92f58SVignesh R 
28*95d92f58SVignesh R 	op_len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
29*95d92f58SVignesh R 	op_buf = calloc(1, op_len);
30*95d92f58SVignesh R 
31*95d92f58SVignesh R 	ret = spi_claim_bus(slave);
32*95d92f58SVignesh R 	if (ret < 0)
33*95d92f58SVignesh R 		return ret;
34*95d92f58SVignesh R 
35*95d92f58SVignesh R 	op_buf[pos++] = op->cmd.opcode;
36*95d92f58SVignesh R 
37*95d92f58SVignesh R 	if (op->addr.nbytes) {
38*95d92f58SVignesh R 		for (i = 0; i < op->addr.nbytes; i++)
39*95d92f58SVignesh R 			op_buf[pos + i] = op->addr.val >>
40*95d92f58SVignesh R 				(8 * (op->addr.nbytes - i - 1));
41*95d92f58SVignesh R 
42*95d92f58SVignesh R 		pos += op->addr.nbytes;
43*95d92f58SVignesh R 	}
44*95d92f58SVignesh R 
45*95d92f58SVignesh R 	if (op->dummy.nbytes)
46*95d92f58SVignesh R 		memset(op_buf + pos, 0xff, op->dummy.nbytes);
47*95d92f58SVignesh R 
48*95d92f58SVignesh R 	/* 1st transfer: opcode + address + dummy cycles */
49*95d92f58SVignesh R 	flag = SPI_XFER_BEGIN;
50*95d92f58SVignesh R 	/* Make sure to set END bit if no tx or rx data messages follow */
51*95d92f58SVignesh R 	if (!tx_buf && !rx_buf)
52*95d92f58SVignesh R 		flag |= SPI_XFER_END;
53*95d92f58SVignesh R 
54*95d92f58SVignesh R 	ret = spi_xfer(slave, op_len * 8, op_buf, NULL, flag);
55*95d92f58SVignesh R 	if (ret)
56*95d92f58SVignesh R 		return ret;
57*95d92f58SVignesh R 
58*95d92f58SVignesh R 	/* 2nd transfer: rx or tx data path */
59*95d92f58SVignesh R 	if (tx_buf || rx_buf) {
60*95d92f58SVignesh R 		ret = spi_xfer(slave, op->data.nbytes * 8, tx_buf,
61*95d92f58SVignesh R 			       rx_buf, SPI_XFER_END);
62*95d92f58SVignesh R 		if (ret)
63*95d92f58SVignesh R 			return ret;
64*95d92f58SVignesh R 	}
65*95d92f58SVignesh R 
66*95d92f58SVignesh R 	spi_release_bus(slave);
67*95d92f58SVignesh R 
68*95d92f58SVignesh R 	for (i = 0; i < pos; i++)
69*95d92f58SVignesh R 		debug("%02x ", op_buf[i]);
70*95d92f58SVignesh R 	debug("| [%dB %s] ",
71*95d92f58SVignesh R 	      tx_buf || rx_buf ? op->data.nbytes : 0,
72*95d92f58SVignesh R 	      tx_buf || rx_buf ? (tx_buf ? "out" : "in") : "-");
73*95d92f58SVignesh R 	for (i = 0; i < op->data.nbytes; i++)
74*95d92f58SVignesh R 		debug("%02x ", tx_buf ? tx_buf[i] : rx_buf[i]);
75*95d92f58SVignesh R 	debug("[ret %d]\n", ret);
76*95d92f58SVignesh R 
77*95d92f58SVignesh R 	free(op_buf);
78*95d92f58SVignesh R 
79*95d92f58SVignesh R 	if (ret < 0)
80*95d92f58SVignesh R 		return ret;
81*95d92f58SVignesh R 
82*95d92f58SVignesh R 	return 0;
83*95d92f58SVignesh R }
84*95d92f58SVignesh R 
spi_mem_adjust_op_size(struct spi_slave * slave,struct spi_mem_op * op)85*95d92f58SVignesh R int spi_mem_adjust_op_size(struct spi_slave *slave,
86*95d92f58SVignesh R 			   struct spi_mem_op *op)
87*95d92f58SVignesh R {
88*95d92f58SVignesh R 	unsigned int len;
89*95d92f58SVignesh R 
90*95d92f58SVignesh R 	len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
91*95d92f58SVignesh R 	if (slave->max_write_size && len > slave->max_write_size)
92*95d92f58SVignesh R 		return -EINVAL;
93*95d92f58SVignesh R 
94*95d92f58SVignesh R 	if (op->data.dir == SPI_MEM_DATA_IN && slave->max_read_size)
95*95d92f58SVignesh R 		op->data.nbytes = min(op->data.nbytes,
96*95d92f58SVignesh R 				      slave->max_read_size);
97*95d92f58SVignesh R 	else if (slave->max_write_size)
98*95d92f58SVignesh R 		op->data.nbytes = min(op->data.nbytes,
99*95d92f58SVignesh R 				      slave->max_write_size - len);
100*95d92f58SVignesh R 
101*95d92f58SVignesh R 	if (!op->data.nbytes)
102*95d92f58SVignesh R 		return -EINVAL;
103*95d92f58SVignesh R 
104*95d92f58SVignesh R 	return 0;
105*95d92f58SVignesh R }
106