blob: 75226fd36884cbd310477ca5adbc7eb93c8ef325 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenke3093092002-10-01 01:07:28 +00002/*
3 * (C) Copyright 2002
4 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
wdenke3093092002-10-01 01:07:28 +00005 */
6
7/*
8 * SPI Read/Write Utilities
9 */
10
11#include <common.h>
12#include <command.h>
Simon Glass1157a162014-10-13 23:41:56 -060013#include <dm.h>
Simon Glassdf3b23a2014-09-15 06:33:22 -060014#include <errno.h>
wdenke3093092002-10-01 01:07:28 +000015#include <spi.h>
wdenke3093092002-10-01 01:07:28 +000016
wdenkeb9401e2002-11-11 02:11:37 +000017/*-----------------------------------------------------------------------
18 * Definitions
19 */
20
21#ifndef MAX_SPI_BYTES
22# define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */
23#endif
wdenke3093092002-10-01 01:07:28 +000024
wdenke3093092002-10-01 01:07:28 +000025/*
26 * Values from last command.
27 */
Reinhard Meyer21032b32010-08-26 10:57:27 +020028static unsigned int bus;
29static unsigned int cs;
30static unsigned int mode;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020031static int bitlen;
32static uchar dout[MAX_SPI_BYTES];
33static uchar din[MAX_SPI_BYTES];
wdenke3093092002-10-01 01:07:28 +000034
Simon Glassdf3b23a2014-09-15 06:33:22 -060035static int do_spi_xfer(int bus, int cs)
36{
37 struct spi_slave *slave;
Simon Glass1157a162014-10-13 23:41:56 -060038 int ret = 0;
Simon Glassdf3b23a2014-09-15 06:33:22 -060039
Simon Glass1157a162014-10-13 23:41:56 -060040#ifdef CONFIG_DM_SPI
41 char name[30], *str;
42 struct udevice *dev;
43
44 snprintf(name, sizeof(name), "generic_%d:%d", bus, cs);
45 str = strdup(name);
Peng Fan9caeb262016-03-20 21:21:36 +080046 if (!str)
47 return -ENOMEM;
Simon Glass1157a162014-10-13 23:41:56 -060048 ret = spi_get_bus_and_cs(bus, cs, 1000000, mode, "spi_generic_drv",
49 str, &dev, &slave);
50 if (ret)
51 return ret;
52#else
Simon Glassdf3b23a2014-09-15 06:33:22 -060053 slave = spi_setup_slave(bus, cs, 1000000, mode);
54 if (!slave) {
55 printf("Invalid device %d:%d\n", bus, cs);
56 return -EINVAL;
57 }
Simon Glass1157a162014-10-13 23:41:56 -060058#endif
Simon Glassdf3b23a2014-09-15 06:33:22 -060059
Simon Glass1157a162014-10-13 23:41:56 -060060 ret = spi_claim_bus(slave);
61 if (ret)
62 goto done;
63 ret = spi_xfer(slave, bitlen, dout, din,
64 SPI_XFER_BEGIN | SPI_XFER_END);
65#ifndef CONFIG_DM_SPI
66 /* We don't get an error code in this case */
67 if (ret)
68 ret = -EIO;
69#endif
70 if (ret) {
71 printf("Error %d during SPI transaction\n", ret);
Simon Glassdf3b23a2014-09-15 06:33:22 -060072 } else {
73 int j;
74
75 for (j = 0; j < ((bitlen + 7) / 8); j++)
76 printf("%02X", din[j]);
77 printf("\n");
78 }
Simon Glass1157a162014-10-13 23:41:56 -060079done:
Simon Glassdf3b23a2014-09-15 06:33:22 -060080 spi_release_bus(slave);
Simon Glass1157a162014-10-13 23:41:56 -060081#ifndef CONFIG_DM_SPI
Simon Glassdf3b23a2014-09-15 06:33:22 -060082 spi_free_slave(slave);
Simon Glass1157a162014-10-13 23:41:56 -060083#endif
Simon Glassdf3b23a2014-09-15 06:33:22 -060084
Simon Glass1157a162014-10-13 23:41:56 -060085 return ret;
Simon Glassdf3b23a2014-09-15 06:33:22 -060086}
87
wdenke3093092002-10-01 01:07:28 +000088/*
89 * SPI read/write
90 *
91 * Syntax:
92 * spi {dev} {num_bits} {dout}
93 * {dev} is the device number for controlling chip select (see TBD)
94 * {num_bits} is the number of bits to send & receive (base 10)
95 * {dout} is a hexadecimal string of data to send
96 * The command prints out the hexadecimal string received via SPI.
97 */
98
Wolfgang Denk54841ab2010-06-28 22:00:46 +020099int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke3093092002-10-01 01:07:28 +0000100{
101 char *cp = 0;
102 uchar tmp;
103 int j;
wdenke3093092002-10-01 01:07:28 +0000104
105 /*
106 * We use the last specified parameters, unless new ones are
107 * entered.
108 */
109
110 if ((flag & CMD_FLAG_REPEAT) == 0)
111 {
Reinhard Meyer21032b32010-08-26 10:57:27 +0200112 if (argc >= 2) {
113 mode = CONFIG_DEFAULT_SPI_MODE;
114 bus = simple_strtoul(argv[1], &cp, 10);
115 if (*cp == ':') {
116 cs = simple_strtoul(cp+1, &cp, 10);
117 } else {
118 cs = bus;
119 bus = CONFIG_DEFAULT_SPI_BUS;
120 }
Marek Vasut2d5e7c72012-08-01 15:12:15 +0200121 if (*cp == '.')
Reinhard Meyer21032b32010-08-26 10:57:27 +0200122 mode = simple_strtoul(cp+1, NULL, 10);
123 }
wdenke3093092002-10-01 01:07:28 +0000124 if (argc >= 3)
125 bitlen = simple_strtoul(argv[2], NULL, 10);
wdenkeb9401e2002-11-11 02:11:37 +0000126 if (argc >= 4) {
127 cp = argv[3];
128 for(j = 0; *cp; j++, cp++) {
129 tmp = *cp - '0';
130 if(tmp > 9)
131 tmp -= ('A' - '0') - 10;
132 if(tmp > 15)
133 tmp -= ('a' - 'A');
134 if(tmp > 15) {
Reinhard Meyer21032b32010-08-26 10:57:27 +0200135 printf("Hex conversion error on %c\n", *cp);
wdenkeb9401e2002-11-11 02:11:37 +0000136 return 1;
137 }
138 if((j % 2) == 0)
139 dout[j / 2] = (tmp << 4);
140 else
141 dout[j / 2] |= tmp;
wdenke3093092002-10-01 01:07:28 +0000142 }
wdenke3093092002-10-01 01:07:28 +0000143 }
144 }
145
wdenkeb9401e2002-11-11 02:11:37 +0000146 if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) {
Reinhard Meyer21032b32010-08-26 10:57:27 +0200147 printf("Invalid bitlen %d\n", bitlen);
wdenkeb9401e2002-11-11 02:11:37 +0000148 return 1;
wdenk8bde7f72003-06-27 21:31:46 +0000149 }
wdenkeb9401e2002-11-11 02:11:37 +0000150
Simon Glassdf3b23a2014-09-15 06:33:22 -0600151 if (do_spi_xfer(bus, cs))
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200152 return 1;
wdenkeb9401e2002-11-11 02:11:37 +0000153
Simon Glassdf3b23a2014-09-15 06:33:22 -0600154 return 0;
wdenke3093092002-10-01 01:07:28 +0000155}
156
wdenk8bde7f72003-06-27 21:31:46 +0000157/***************************************************/
158
wdenk0d498392003-07-01 21:06:45 +0000159U_BOOT_CMD(
160 sspi, 5, 1, do_spi,
Reinhard Meyer21032b32010-08-26 10:57:27 +0200161 "SPI utility command",
162 "[<bus>:]<cs>[.<mode>] <bit_len> <dout> - Send and receive bits\n"
163 "<bus> - Identifies the SPI bus\n"
164 "<cs> - Identifies the chip select\n"
165 "<mode> - Identifies the SPI mode to use\n"
wdenk8bde7f72003-06-27 21:31:46 +0000166 "<bit_len> - Number of bits to send (base 10)\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200167 "<dout> - Hexadecimal string that gets sent"
wdenk8bde7f72003-06-27 21:31:46 +0000168);