xref: /OK3568_Linux_fs/u-boot/cmd/spi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2002
3*4882a593Smuzhiyun  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun  * SPI Read/Write Utilities
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <common.h>
13*4882a593Smuzhiyun #include <command.h>
14*4882a593Smuzhiyun #include <dm.h>
15*4882a593Smuzhiyun #include <errno.h>
16*4882a593Smuzhiyun #include <spi.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*-----------------------------------------------------------------------
19*4882a593Smuzhiyun  * Definitions
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #ifndef MAX_SPI_BYTES
23*4882a593Smuzhiyun #   define MAX_SPI_BYTES 32	/* Maximum number of bytes we can handle */
24*4882a593Smuzhiyun #endif
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #ifndef CONFIG_DEFAULT_SPI_BUS
27*4882a593Smuzhiyun #   define CONFIG_DEFAULT_SPI_BUS	0
28*4882a593Smuzhiyun #endif
29*4882a593Smuzhiyun #ifndef CONFIG_DEFAULT_SPI_MODE
30*4882a593Smuzhiyun #   define CONFIG_DEFAULT_SPI_MODE	SPI_MODE_0
31*4882a593Smuzhiyun #endif
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /*
34*4882a593Smuzhiyun  * Values from last command.
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun static unsigned int	bus;
37*4882a593Smuzhiyun static unsigned int	cs;
38*4882a593Smuzhiyun static unsigned int	mode;
39*4882a593Smuzhiyun static int   		bitlen;
40*4882a593Smuzhiyun static uchar 		dout[MAX_SPI_BYTES];
41*4882a593Smuzhiyun static uchar 		din[MAX_SPI_BYTES];
42*4882a593Smuzhiyun 
do_spi_xfer(int bus,int cs)43*4882a593Smuzhiyun static int do_spi_xfer(int bus, int cs)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	struct spi_slave *slave;
46*4882a593Smuzhiyun 	int ret = 0;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #ifdef CONFIG_DM_SPI
49*4882a593Smuzhiyun 	char name[30], *str;
50*4882a593Smuzhiyun 	struct udevice *dev;
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	snprintf(name, sizeof(name), "generic_%d:%d", bus, cs);
53*4882a593Smuzhiyun 	str = strdup(name);
54*4882a593Smuzhiyun 	if (!str)
55*4882a593Smuzhiyun 		return -ENOMEM;
56*4882a593Smuzhiyun 	ret = spi_get_bus_and_cs(bus, cs, 1000000, mode, "spi_generic_drv",
57*4882a593Smuzhiyun 				 str, &dev, &slave);
58*4882a593Smuzhiyun 	if (ret)
59*4882a593Smuzhiyun 		return ret;
60*4882a593Smuzhiyun #else
61*4882a593Smuzhiyun 	slave = spi_setup_slave(bus, cs, 1000000, mode);
62*4882a593Smuzhiyun 	if (!slave) {
63*4882a593Smuzhiyun 		printf("Invalid device %d:%d\n", bus, cs);
64*4882a593Smuzhiyun 		return -EINVAL;
65*4882a593Smuzhiyun 	}
66*4882a593Smuzhiyun #endif
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	ret = spi_claim_bus(slave);
69*4882a593Smuzhiyun 	if (ret)
70*4882a593Smuzhiyun 		goto done;
71*4882a593Smuzhiyun 	ret = spi_xfer(slave, bitlen, dout, din,
72*4882a593Smuzhiyun 		       SPI_XFER_BEGIN | SPI_XFER_END);
73*4882a593Smuzhiyun #ifndef CONFIG_DM_SPI
74*4882a593Smuzhiyun 	/* We don't get an error code in this case */
75*4882a593Smuzhiyun 	if (ret)
76*4882a593Smuzhiyun 		ret = -EIO;
77*4882a593Smuzhiyun #endif
78*4882a593Smuzhiyun 	if (ret) {
79*4882a593Smuzhiyun 		printf("Error %d during SPI transaction\n", ret);
80*4882a593Smuzhiyun 	} else {
81*4882a593Smuzhiyun 		int j;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 		for (j = 0; j < ((bitlen + 7) / 8); j++)
84*4882a593Smuzhiyun 			printf("%02X", din[j]);
85*4882a593Smuzhiyun 		printf("\n");
86*4882a593Smuzhiyun 	}
87*4882a593Smuzhiyun done:
88*4882a593Smuzhiyun 	spi_release_bus(slave);
89*4882a593Smuzhiyun #ifndef CONFIG_DM_SPI
90*4882a593Smuzhiyun 	spi_free_slave(slave);
91*4882a593Smuzhiyun #endif
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	return ret;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun  * SPI read/write
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * Syntax:
100*4882a593Smuzhiyun  *   spi {dev} {num_bits} {dout}
101*4882a593Smuzhiyun  *     {dev} is the device number for controlling chip select (see TBD)
102*4882a593Smuzhiyun  *     {num_bits} is the number of bits to send & receive (base 10)
103*4882a593Smuzhiyun  *     {dout} is a hexadecimal string of data to send
104*4882a593Smuzhiyun  * The command prints out the hexadecimal string received via SPI.
105*4882a593Smuzhiyun  */
106*4882a593Smuzhiyun 
do_spi(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])107*4882a593Smuzhiyun int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	char  *cp = 0;
110*4882a593Smuzhiyun 	uchar tmp;
111*4882a593Smuzhiyun 	int   j;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/*
114*4882a593Smuzhiyun 	 * We use the last specified parameters, unless new ones are
115*4882a593Smuzhiyun 	 * entered.
116*4882a593Smuzhiyun 	 */
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	if ((flag & CMD_FLAG_REPEAT) == 0)
119*4882a593Smuzhiyun 	{
120*4882a593Smuzhiyun 		if (argc >= 2) {
121*4882a593Smuzhiyun 			mode = CONFIG_DEFAULT_SPI_MODE;
122*4882a593Smuzhiyun 			bus = simple_strtoul(argv[1], &cp, 10);
123*4882a593Smuzhiyun 			if (*cp == ':') {
124*4882a593Smuzhiyun 				cs = simple_strtoul(cp+1, &cp, 10);
125*4882a593Smuzhiyun 			} else {
126*4882a593Smuzhiyun 				cs = bus;
127*4882a593Smuzhiyun 				bus = CONFIG_DEFAULT_SPI_BUS;
128*4882a593Smuzhiyun 			}
129*4882a593Smuzhiyun 			if (*cp == '.')
130*4882a593Smuzhiyun 				mode = simple_strtoul(cp+1, NULL, 10);
131*4882a593Smuzhiyun 		}
132*4882a593Smuzhiyun 		if (argc >= 3)
133*4882a593Smuzhiyun 			bitlen = simple_strtoul(argv[2], NULL, 10);
134*4882a593Smuzhiyun 		if (argc >= 4) {
135*4882a593Smuzhiyun 			cp = argv[3];
136*4882a593Smuzhiyun 			for(j = 0; *cp; j++, cp++) {
137*4882a593Smuzhiyun 				tmp = *cp - '0';
138*4882a593Smuzhiyun 				if(tmp > 9)
139*4882a593Smuzhiyun 					tmp -= ('A' - '0') - 10;
140*4882a593Smuzhiyun 				if(tmp > 15)
141*4882a593Smuzhiyun 					tmp -= ('a' - 'A');
142*4882a593Smuzhiyun 				if(tmp > 15) {
143*4882a593Smuzhiyun 					printf("Hex conversion error on %c\n", *cp);
144*4882a593Smuzhiyun 					return 1;
145*4882a593Smuzhiyun 				}
146*4882a593Smuzhiyun 				if((j % 2) == 0)
147*4882a593Smuzhiyun 					dout[j / 2] = (tmp << 4);
148*4882a593Smuzhiyun 				else
149*4882a593Smuzhiyun 					dout[j / 2] |= tmp;
150*4882a593Smuzhiyun 			}
151*4882a593Smuzhiyun 		}
152*4882a593Smuzhiyun 	}
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	if ((bitlen < 0) || (bitlen >  (MAX_SPI_BYTES * 8))) {
155*4882a593Smuzhiyun 		printf("Invalid bitlen %d\n", bitlen);
156*4882a593Smuzhiyun 		return 1;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	if (do_spi_xfer(bus, cs))
160*4882a593Smuzhiyun 		return 1;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	return 0;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun /***************************************************/
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun U_BOOT_CMD(
168*4882a593Smuzhiyun 	sspi,	5,	1,	do_spi,
169*4882a593Smuzhiyun 	"SPI utility command",
170*4882a593Smuzhiyun 	"[<bus>:]<cs>[.<mode>] <bit_len> <dout> - Send and receive bits\n"
171*4882a593Smuzhiyun 	"<bus>     - Identifies the SPI bus\n"
172*4882a593Smuzhiyun 	"<cs>      - Identifies the chip select\n"
173*4882a593Smuzhiyun 	"<mode>    - Identifies the SPI mode to use\n"
174*4882a593Smuzhiyun 	"<bit_len> - Number of bits to send (base 10)\n"
175*4882a593Smuzhiyun 	"<dout>    - Hexadecimal string that gets sent"
176*4882a593Smuzhiyun );
177