blob: cc5ab5f991dae090880a0ecedbc3e4a31f7477c1 [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
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020039struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
40 unsigned int max_hz, unsigned int mode)
41{
42 struct soft_spi_slave *ss;
43
44 if (!spi_cs_is_valid(bus, cs))
45 return NULL;
46
Simon Glassd3504fe2013-03-18 19:23:40 +000047 ss = spi_alloc_slave(struct soft_spi_slave, bus, cs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020048 if (!ss)
49 return NULL;
50
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020051 ss->mode = mode;
52
53 /* TODO: Use max_hz to limit the SCK rate */
54
55 return &ss->slave;
56}
57
58void spi_free_slave(struct spi_slave *slave)
59{
60 struct soft_spi_slave *ss = to_soft_spi(slave);
61
62 free(ss);
63}
64
65int spi_claim_bus(struct spi_slave *slave)
66{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020067#ifdef CONFIG_SYS_IMMR
68 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020069#endif
70 struct soft_spi_slave *ss = to_soft_spi(slave);
71
72 /*
73 * Make sure the SPI clock is in idle state as defined for
74 * this slave.
75 */
76 if (ss->mode & SPI_CPOL)
77 SPI_SCL(1);
78 else
79 SPI_SCL(0);
80
81 return 0;
82}
83
84void spi_release_bus(struct spi_slave *slave)
85{
86 /* Nothing to do */
87}
wdenke3093092002-10-01 01:07:28 +000088
89/*-----------------------------------------------------------------------
90 * SPI transfer
91 *
92 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
93 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
94 *
95 * The source of the outgoing bits is the "dout" parameter and the
96 * destination of the input bits is the "din" parameter. Note that "dout"
97 * and "din" can point to the same memory location, in which case the
98 * input data overwrites the output data (since both are buffered by
99 * temporary variables, this is OK).
wdenke3093092002-10-01 01:07:28 +0000100 */
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200101int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
102 const void *dout, void *din, unsigned long flags)
wdenke3093092002-10-01 01:07:28 +0000103{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200104#ifdef CONFIG_SYS_IMMR
105 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
John Otkend4024bb2007-07-26 17:49:11 +0200106#endif
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200107 struct soft_spi_slave *ss = to_soft_spi(slave);
108 uchar tmpdin = 0;
109 uchar tmpdout = 0;
110 const u8 *txd = dout;
111 u8 *rxd = din;
112 int cpol = ss->mode & SPI_CPOL;
113 int cpha = ss->mode & SPI_CPHA;
114 unsigned int j;
wdenke3093092002-10-01 01:07:28 +0000115
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200116 PRINTD("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
117 slave->bus, slave->cs, *(uint *)txd, *(uint *)rxd, bitlen);
wdenke3093092002-10-01 01:07:28 +0000118
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200119 if (flags & SPI_XFER_BEGIN)
120 spi_cs_activate(slave);
wdenke3093092002-10-01 01:07:28 +0000121
122 for(j = 0; j < bitlen; j++) {
123 /*
124 * Check if it is time to work on a new byte.
125 */
Andrew Ruderc1c0dd22014-04-24 13:39:32 -0500126 if ((j % 8) == 0) {
127 if (txd)
128 tmpdout = *txd++;
129 else
130 tmpdout = 0;
wdenke3093092002-10-01 01:07:28 +0000131 if(j != 0) {
Andrew Ruderc1c0dd22014-04-24 13:39:32 -0500132 if (rxd)
133 *rxd++ = tmpdin;
wdenke3093092002-10-01 01:07:28 +0000134 }
135 tmpdin = 0;
136 }
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200137
138 if (!cpha)
139 SPI_SCL(!cpol);
wdenke3093092002-10-01 01:07:28 +0000140 SPI_SDA(tmpdout & 0x80);
141 SPI_DELAY;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200142 if (cpha)
143 SPI_SCL(!cpol);
144 else
145 SPI_SCL(cpol);
146 tmpdin <<= 1;
147 tmpdin |= SPI_READ;
148 tmpdout <<= 1;
wdenke3093092002-10-01 01:07:28 +0000149 SPI_DELAY;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200150 if (cpha)
151 SPI_SCL(cpol);
wdenke3093092002-10-01 01:07:28 +0000152 }
153 /*
154 * If the number of bits isn't a multiple of 8, shift the last
155 * bits over to left-justify them. Then store the last byte
156 * read in.
157 */
Andrew Ruderc1c0dd22014-04-24 13:39:32 -0500158 if (rxd) {
159 if ((bitlen % 8) != 0)
160 tmpdin <<= 8 - (bitlen % 8);
161 *rxd++ = tmpdin;
162 }
wdenke3093092002-10-01 01:07:28 +0000163
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200164 if (flags & SPI_XFER_END)
165 spi_cs_deactivate(slave);
wdenke3093092002-10-01 01:07:28 +0000166
167 return(0);
168}