blob: 941daa730b0a7c6e6b9b7c1326d8526dd480e559 [file] [log] [blame]
wdenke3093092002-10-01 01:07:28 +00001/*
2 * (C) Copyright 2002
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
4 *
5 * Influenced by code from:
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenke3093092002-10-01 01:07:28 +00009 */
10
11#include <common.h>
12#include <spi.h>
13
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020014#include <malloc.h>
15
wdenke3093092002-10-01 01:07:28 +000016/*-----------------------------------------------------------------------
17 * Definitions
18 */
19
20#ifdef DEBUG_SPI
21#define PRINTD(fmt,args...) printf (fmt ,##args)
22#else
23#define PRINTD(fmt,args...)
24#endif
25
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020026struct soft_spi_slave {
27 struct spi_slave slave;
28 unsigned int mode;
29};
30
31static inline struct soft_spi_slave *to_soft_spi(struct spi_slave *slave)
32{
33 return container_of(slave, struct soft_spi_slave, slave);
34}
wdenke3093092002-10-01 01:07:28 +000035
wdenke3093092002-10-01 01:07:28 +000036/*=====================================================================*/
37/* Public Functions */
38/*=====================================================================*/
39
40/*-----------------------------------------------------------------------
41 * Initialization
42 */
43void spi_init (void)
44{
wdenke3093092002-10-01 01:07:28 +000045}
46
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020047struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
48 unsigned int max_hz, unsigned int mode)
49{
50 struct soft_spi_slave *ss;
51
52 if (!spi_cs_is_valid(bus, cs))
53 return NULL;
54
Simon Glassd3504fe2013-03-18 19:23:40 +000055 ss = spi_alloc_slave(struct soft_spi_slave, bus, cs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020056 if (!ss)
57 return NULL;
58
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020059 ss->mode = mode;
60
61 /* TODO: Use max_hz to limit the SCK rate */
62
63 return &ss->slave;
64}
65
66void spi_free_slave(struct spi_slave *slave)
67{
68 struct soft_spi_slave *ss = to_soft_spi(slave);
69
70 free(ss);
71}
72
73int spi_claim_bus(struct spi_slave *slave)
74{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020075#ifdef CONFIG_SYS_IMMR
76 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020077#endif
78 struct soft_spi_slave *ss = to_soft_spi(slave);
79
80 /*
81 * Make sure the SPI clock is in idle state as defined for
82 * this slave.
83 */
84 if (ss->mode & SPI_CPOL)
85 SPI_SCL(1);
86 else
87 SPI_SCL(0);
88
89 return 0;
90}
91
92void spi_release_bus(struct spi_slave *slave)
93{
94 /* Nothing to do */
95}
wdenke3093092002-10-01 01:07:28 +000096
97/*-----------------------------------------------------------------------
98 * SPI transfer
99 *
100 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
101 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
102 *
103 * The source of the outgoing bits is the "dout" parameter and the
104 * destination of the input bits is the "din" parameter. Note that "dout"
105 * and "din" can point to the same memory location, in which case the
106 * input data overwrites the output data (since both are buffered by
107 * temporary variables, this is OK).
wdenke3093092002-10-01 01:07:28 +0000108 */
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200109int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
110 const void *dout, void *din, unsigned long flags)
wdenke3093092002-10-01 01:07:28 +0000111{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200112#ifdef CONFIG_SYS_IMMR
113 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
John Otkend4024bb2007-07-26 17:49:11 +0200114#endif
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200115 struct soft_spi_slave *ss = to_soft_spi(slave);
116 uchar tmpdin = 0;
117 uchar tmpdout = 0;
118 const u8 *txd = dout;
119 u8 *rxd = din;
120 int cpol = ss->mode & SPI_CPOL;
121 int cpha = ss->mode & SPI_CPHA;
122 unsigned int j;
wdenke3093092002-10-01 01:07:28 +0000123
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200124 PRINTD("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
125 slave->bus, slave->cs, *(uint *)txd, *(uint *)rxd, bitlen);
wdenke3093092002-10-01 01:07:28 +0000126
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200127 if (flags & SPI_XFER_BEGIN)
128 spi_cs_activate(slave);
wdenke3093092002-10-01 01:07:28 +0000129
130 for(j = 0; j < bitlen; j++) {
131 /*
132 * Check if it is time to work on a new byte.
133 */
Andrew Ruderc1c0dd22014-04-24 13:39:32 -0500134 if ((j % 8) == 0) {
135 if (txd)
136 tmpdout = *txd++;
137 else
138 tmpdout = 0;
wdenke3093092002-10-01 01:07:28 +0000139 if(j != 0) {
Andrew Ruderc1c0dd22014-04-24 13:39:32 -0500140 if (rxd)
141 *rxd++ = tmpdin;
wdenke3093092002-10-01 01:07:28 +0000142 }
143 tmpdin = 0;
144 }
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200145
146 if (!cpha)
147 SPI_SCL(!cpol);
wdenke3093092002-10-01 01:07:28 +0000148 SPI_SDA(tmpdout & 0x80);
149 SPI_DELAY;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200150 if (cpha)
151 SPI_SCL(!cpol);
152 else
153 SPI_SCL(cpol);
154 tmpdin <<= 1;
155 tmpdin |= SPI_READ;
156 tmpdout <<= 1;
wdenke3093092002-10-01 01:07:28 +0000157 SPI_DELAY;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200158 if (cpha)
159 SPI_SCL(cpol);
wdenke3093092002-10-01 01:07:28 +0000160 }
161 /*
162 * If the number of bits isn't a multiple of 8, shift the last
163 * bits over to left-justify them. Then store the last byte
164 * read in.
165 */
Andrew Ruderc1c0dd22014-04-24 13:39:32 -0500166 if (rxd) {
167 if ((bitlen % 8) != 0)
168 tmpdin <<= 8 - (bitlen % 8);
169 *rxd++ = tmpdin;
170 }
wdenke3093092002-10-01 01:07:28 +0000171
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200172 if (flags & SPI_XFER_END)
173 spi_cs_deactivate(slave);
wdenke3093092002-10-01 01:07:28 +0000174
175 return(0);
176}