blob: 0aac0c065daa5442630ba29289b5cdfea04503fd [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.
5 *
6 * Influenced by code from:
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenke3093092002-10-01 01:07:28 +00008 */
9
10#include <common.h>
11#include <spi.h>
12
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020013#include <malloc.h>
14
wdenke3093092002-10-01 01:07:28 +000015/*-----------------------------------------------------------------------
16 * Definitions
17 */
18
19#ifdef DEBUG_SPI
20#define PRINTD(fmt,args...) printf (fmt ,##args)
21#else
22#define PRINTD(fmt,args...)
23#endif
24
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020025struct soft_spi_slave {
26 struct spi_slave slave;
27 unsigned int mode;
28};
29
30static inline struct soft_spi_slave *to_soft_spi(struct spi_slave *slave)
31{
32 return container_of(slave, struct soft_spi_slave, slave);
33}
wdenke3093092002-10-01 01:07:28 +000034
wdenke3093092002-10-01 01:07:28 +000035/*=====================================================================*/
36/* Public Functions */
37/*=====================================================================*/
38
39/*-----------------------------------------------------------------------
40 * Initialization
41 */
42void spi_init (void)
43{
wdenke3093092002-10-01 01:07:28 +000044}
45
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020046struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
47 unsigned int max_hz, unsigned int mode)
48{
49 struct soft_spi_slave *ss;
50
51 if (!spi_cs_is_valid(bus, cs))
52 return NULL;
53
Simon Glassd3504fe2013-03-18 19:23:40 +000054 ss = spi_alloc_slave(struct soft_spi_slave, bus, cs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020055 if (!ss)
56 return NULL;
57
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020058 ss->mode = mode;
59
60 /* TODO: Use max_hz to limit the SCK rate */
61
62 return &ss->slave;
63}
64
65void spi_free_slave(struct spi_slave *slave)
66{
67 struct soft_spi_slave *ss = to_soft_spi(slave);
68
69 free(ss);
70}
71
72int spi_claim_bus(struct spi_slave *slave)
73{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020074#ifdef CONFIG_SYS_IMMR
75 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020076#endif
77 struct soft_spi_slave *ss = to_soft_spi(slave);
78
79 /*
80 * Make sure the SPI clock is in idle state as defined for
81 * this slave.
82 */
83 if (ss->mode & SPI_CPOL)
84 SPI_SCL(1);
85 else
86 SPI_SCL(0);
87
88 return 0;
89}
90
91void spi_release_bus(struct spi_slave *slave)
92{
93 /* Nothing to do */
94}
wdenke3093092002-10-01 01:07:28 +000095
96/*-----------------------------------------------------------------------
97 * SPI transfer
98 *
99 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
100 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
101 *
102 * The source of the outgoing bits is the "dout" parameter and the
103 * destination of the input bits is the "din" parameter. Note that "dout"
104 * and "din" can point to the same memory location, in which case the
105 * input data overwrites the output data (since both are buffered by
106 * temporary variables, this is OK).
wdenke3093092002-10-01 01:07:28 +0000107 */
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200108int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
109 const void *dout, void *din, unsigned long flags)
wdenke3093092002-10-01 01:07:28 +0000110{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200111#ifdef CONFIG_SYS_IMMR
112 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
John Otkend4024bb2007-07-26 17:49:11 +0200113#endif
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200114 struct soft_spi_slave *ss = to_soft_spi(slave);
115 uchar tmpdin = 0;
116 uchar tmpdout = 0;
117 const u8 *txd = dout;
118 u8 *rxd = din;
119 int cpol = ss->mode & SPI_CPOL;
120 int cpha = ss->mode & SPI_CPHA;
121 unsigned int j;
wdenke3093092002-10-01 01:07:28 +0000122
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200123 PRINTD("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
124 slave->bus, slave->cs, *(uint *)txd, *(uint *)rxd, bitlen);
wdenke3093092002-10-01 01:07:28 +0000125
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200126 if (flags & SPI_XFER_BEGIN)
127 spi_cs_activate(slave);
wdenke3093092002-10-01 01:07:28 +0000128
129 for(j = 0; j < bitlen; j++) {
130 /*
131 * Check if it is time to work on a new byte.
132 */
Andrew Ruderc1c0dd22014-04-24 13:39:32 -0500133 if ((j % 8) == 0) {
134 if (txd)
135 tmpdout = *txd++;
136 else
137 tmpdout = 0;
wdenke3093092002-10-01 01:07:28 +0000138 if(j != 0) {
Andrew Ruderc1c0dd22014-04-24 13:39:32 -0500139 if (rxd)
140 *rxd++ = tmpdin;
wdenke3093092002-10-01 01:07:28 +0000141 }
142 tmpdin = 0;
143 }
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200144
145 if (!cpha)
146 SPI_SCL(!cpol);
wdenke3093092002-10-01 01:07:28 +0000147 SPI_SDA(tmpdout & 0x80);
148 SPI_DELAY;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200149 if (cpha)
150 SPI_SCL(!cpol);
151 else
152 SPI_SCL(cpol);
153 tmpdin <<= 1;
154 tmpdin |= SPI_READ;
155 tmpdout <<= 1;
wdenke3093092002-10-01 01:07:28 +0000156 SPI_DELAY;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200157 if (cpha)
158 SPI_SCL(cpol);
wdenke3093092002-10-01 01:07:28 +0000159 }
160 /*
161 * If the number of bits isn't a multiple of 8, shift the last
162 * bits over to left-justify them. Then store the last byte
163 * read in.
164 */
Andrew Ruderc1c0dd22014-04-24 13:39:32 -0500165 if (rxd) {
166 if ((bitlen % 8) != 0)
167 tmpdin <<= 8 - (bitlen % 8);
168 *rxd++ = tmpdin;
169 }
wdenke3093092002-10-01 01:07:28 +0000170
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200171 if (flags & SPI_XFER_END)
172 spi_cs_deactivate(slave);
wdenke3093092002-10-01 01:07:28 +0000173
174 return(0);
175}