blob: f16ef9a5db554077179a3ef5cbac00ce7e773b09 [file] [log] [blame]
wdenke3093092002-10-01 01:07:28 +00001/*
2 * (C) Copyright 2002
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenke3093092002-10-01 01:07:28 +00006 */
7
8/*
9 * SPI Read/Write Utilities
10 */
11
12#include <common.h>
13#include <command.h>
Simon Glass1157a162014-10-13 23:41:56 -060014#include <dm.h>
Simon Glassdf3b23a2014-09-15 06:33:22 -060015#include <errno.h>
wdenke3093092002-10-01 01:07:28 +000016#include <spi.h>
wdenke3093092002-10-01 01:07:28 +000017
wdenkeb9401e2002-11-11 02:11:37 +000018/*-----------------------------------------------------------------------
19 * Definitions
20 */
21
22#ifndef MAX_SPI_BYTES
23# define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */
24#endif
wdenke3093092002-10-01 01:07:28 +000025
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020026#ifndef CONFIG_DEFAULT_SPI_BUS
27# define CONFIG_DEFAULT_SPI_BUS 0
28#endif
29#ifndef CONFIG_DEFAULT_SPI_MODE
30# define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0
31#endif
wdenke3093092002-10-01 01:07:28 +000032
33/*
34 * Values from last command.
35 */
Reinhard Meyer21032b32010-08-26 10:57:27 +020036static unsigned int bus;
37static unsigned int cs;
38static unsigned int mode;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020039static int bitlen;
40static uchar dout[MAX_SPI_BYTES];
41static uchar din[MAX_SPI_BYTES];
wdenke3093092002-10-01 01:07:28 +000042
Simon Glassdf3b23a2014-09-15 06:33:22 -060043static int do_spi_xfer(int bus, int cs)
44{
45 struct spi_slave *slave;
Simon Glass1157a162014-10-13 23:41:56 -060046 int ret = 0;
Simon Glassdf3b23a2014-09-15 06:33:22 -060047
Simon Glass1157a162014-10-13 23:41:56 -060048#ifdef CONFIG_DM_SPI
49 char name[30], *str;
50 struct udevice *dev;
51
52 snprintf(name, sizeof(name), "generic_%d:%d", bus, cs);
53 str = strdup(name);
Peng Fan9caeb262016-03-20 21:21:36 +080054 if (!str)
55 return -ENOMEM;
Simon Glass1157a162014-10-13 23:41:56 -060056 ret = spi_get_bus_and_cs(bus, cs, 1000000, mode, "spi_generic_drv",
57 str, &dev, &slave);
58 if (ret)
59 return ret;
60#else
Simon Glassdf3b23a2014-09-15 06:33:22 -060061 slave = spi_setup_slave(bus, cs, 1000000, mode);
62 if (!slave) {
63 printf("Invalid device %d:%d\n", bus, cs);
64 return -EINVAL;
65 }
Simon Glass1157a162014-10-13 23:41:56 -060066#endif
Simon Glassdf3b23a2014-09-15 06:33:22 -060067
Simon Glass1157a162014-10-13 23:41:56 -060068 ret = spi_claim_bus(slave);
69 if (ret)
70 goto done;
71 ret = spi_xfer(slave, bitlen, dout, din,
72 SPI_XFER_BEGIN | SPI_XFER_END);
73#ifndef CONFIG_DM_SPI
74 /* We don't get an error code in this case */
75 if (ret)
76 ret = -EIO;
77#endif
78 if (ret) {
79 printf("Error %d during SPI transaction\n", ret);
Simon Glassdf3b23a2014-09-15 06:33:22 -060080 } else {
81 int j;
82
83 for (j = 0; j < ((bitlen + 7) / 8); j++)
84 printf("%02X", din[j]);
85 printf("\n");
86 }
Simon Glass1157a162014-10-13 23:41:56 -060087done:
Simon Glassdf3b23a2014-09-15 06:33:22 -060088 spi_release_bus(slave);
Simon Glass1157a162014-10-13 23:41:56 -060089#ifndef CONFIG_DM_SPI
Simon Glassdf3b23a2014-09-15 06:33:22 -060090 spi_free_slave(slave);
Simon Glass1157a162014-10-13 23:41:56 -060091#endif
Simon Glassdf3b23a2014-09-15 06:33:22 -060092
Simon Glass1157a162014-10-13 23:41:56 -060093 return ret;
Simon Glassdf3b23a2014-09-15 06:33:22 -060094}
95
wdenke3093092002-10-01 01:07:28 +000096/*
97 * SPI read/write
98 *
99 * Syntax:
100 * spi {dev} {num_bits} {dout}
101 * {dev} is the device number for controlling chip select (see TBD)
102 * {num_bits} is the number of bits to send & receive (base 10)
103 * {dout} is a hexadecimal string of data to send
104 * The command prints out the hexadecimal string received via SPI.
105 */
106
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200107int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke3093092002-10-01 01:07:28 +0000108{
109 char *cp = 0;
110 uchar tmp;
111 int j;
wdenke3093092002-10-01 01:07:28 +0000112
113 /*
114 * We use the last specified parameters, unless new ones are
115 * entered.
116 */
117
118 if ((flag & CMD_FLAG_REPEAT) == 0)
119 {
Reinhard Meyer21032b32010-08-26 10:57:27 +0200120 if (argc >= 2) {
121 mode = CONFIG_DEFAULT_SPI_MODE;
122 bus = simple_strtoul(argv[1], &cp, 10);
123 if (*cp == ':') {
124 cs = simple_strtoul(cp+1, &cp, 10);
125 } else {
126 cs = bus;
127 bus = CONFIG_DEFAULT_SPI_BUS;
128 }
Marek Vasut2d5e7c72012-08-01 15:12:15 +0200129 if (*cp == '.')
Reinhard Meyer21032b32010-08-26 10:57:27 +0200130 mode = simple_strtoul(cp+1, NULL, 10);
131 }
wdenke3093092002-10-01 01:07:28 +0000132 if (argc >= 3)
133 bitlen = simple_strtoul(argv[2], NULL, 10);
wdenkeb9401e2002-11-11 02:11:37 +0000134 if (argc >= 4) {
135 cp = argv[3];
136 for(j = 0; *cp; j++, cp++) {
137 tmp = *cp - '0';
138 if(tmp > 9)
139 tmp -= ('A' - '0') - 10;
140 if(tmp > 15)
141 tmp -= ('a' - 'A');
142 if(tmp > 15) {
Reinhard Meyer21032b32010-08-26 10:57:27 +0200143 printf("Hex conversion error on %c\n", *cp);
wdenkeb9401e2002-11-11 02:11:37 +0000144 return 1;
145 }
146 if((j % 2) == 0)
147 dout[j / 2] = (tmp << 4);
148 else
149 dout[j / 2] |= tmp;
wdenke3093092002-10-01 01:07:28 +0000150 }
wdenke3093092002-10-01 01:07:28 +0000151 }
152 }
153
wdenkeb9401e2002-11-11 02:11:37 +0000154 if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) {
Reinhard Meyer21032b32010-08-26 10:57:27 +0200155 printf("Invalid bitlen %d\n", bitlen);
wdenkeb9401e2002-11-11 02:11:37 +0000156 return 1;
wdenk8bde7f72003-06-27 21:31:46 +0000157 }
wdenkeb9401e2002-11-11 02:11:37 +0000158
Simon Glassdf3b23a2014-09-15 06:33:22 -0600159 if (do_spi_xfer(bus, cs))
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200160 return 1;
wdenkeb9401e2002-11-11 02:11:37 +0000161
Simon Glassdf3b23a2014-09-15 06:33:22 -0600162 return 0;
wdenke3093092002-10-01 01:07:28 +0000163}
164
wdenk8bde7f72003-06-27 21:31:46 +0000165/***************************************************/
166
wdenk0d498392003-07-01 21:06:45 +0000167U_BOOT_CMD(
168 sspi, 5, 1, do_spi,
Reinhard Meyer21032b32010-08-26 10:57:27 +0200169 "SPI utility command",
170 "[<bus>:]<cs>[.<mode>] <bit_len> <dout> - Send and receive bits\n"
171 "<bus> - Identifies the SPI bus\n"
172 "<cs> - Identifies the chip select\n"
173 "<mode> - Identifies the SPI mode to use\n"
wdenk8bde7f72003-06-27 21:31:46 +0000174 "<bit_len> - Number of bits to send (base 10)\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200175 "<dout> - Hexadecimal string that gets sent"
wdenk8bde7f72003-06-27 21:31:46 +0000176);