blob: 8d6d86d2b0a1728aaf832b321b9e2352b4d72b5f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ben Warren04a9e112008-01-16 22:37:35 -05002/*
3 * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc.
Stefan Roesea47a12b2010-04-15 16:07:28 +02004 * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers
Ben Warren04a9e112008-01-16 22:37:35 -05005 */
6
7#include <common.h>
Ben Warren8931ab12008-01-26 23:41:19 -05008
Haavard Skinnemoend255bb02008-05-16 11:10:31 +02009#include <malloc.h>
Ben Warren04a9e112008-01-16 22:37:35 -050010#include <spi.h>
11#include <asm/mpc8xxx_spi.h>
12
Kim Phillips2956acd2008-01-17 12:48:00 -060013#define SPI_EV_NE (0x80000000 >> 22) /* Receiver Not Empty */
14#define SPI_EV_NF (0x80000000 >> 23) /* Transmitter Not Full */
Ben Warren04a9e112008-01-16 22:37:35 -050015
Kim Phillips2956acd2008-01-17 12:48:00 -060016#define SPI_MODE_LOOP (0x80000000 >> 1) /* Loopback mode */
17#define SPI_MODE_REV (0x80000000 >> 5) /* Reverse mode - MSB first */
18#define SPI_MODE_MS (0x80000000 >> 6) /* Always master */
19#define SPI_MODE_EN (0x80000000 >> 7) /* Enable interface */
Ben Warren04a9e112008-01-16 22:37:35 -050020
21#define SPI_TIMEOUT 1000
22
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020023struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
24 unsigned int max_hz, unsigned int mode)
25{
26 struct spi_slave *slave;
27
28 if (!spi_cs_is_valid(bus, cs))
29 return NULL;
30
Simon Glassd3504fe2013-03-18 19:23:40 +000031 slave = spi_alloc_slave_base(bus, cs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020032 if (!slave)
33 return NULL;
34
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020035 /*
36 * TODO: Some of the code in spi_init() should probably move
37 * here, or into spi_claim_bus() below.
38 */
39
40 return slave;
41}
42
43void spi_free_slave(struct spi_slave *slave)
44{
45 free(slave);
46}
47
Ben Warren04a9e112008-01-16 22:37:35 -050048void spi_init(void)
49{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020050 volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi;
Ben Warren04a9e112008-01-16 22:37:35 -050051
Kim Phillips2956acd2008-01-17 12:48:00 -060052 /*
Ben Warren04a9e112008-01-16 22:37:35 -050053 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
54 * some registers
Kim Phillips2956acd2008-01-17 12:48:00 -060055 */
Ben Warren04a9e112008-01-16 22:37:35 -050056 spi->mode = SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN;
Jagan Tekia048d4b2015-10-23 01:38:07 +053057 spi->mode = (spi->mode & 0xfff0ffff) | BIT(16); /* Use SYSCLK / 8
Kim Phillips2956acd2008-01-17 12:48:00 -060058 (16.67MHz typ.) */
Ben Warren04a9e112008-01-16 22:37:35 -050059 spi->event = 0xffffffff; /* Clear all SPI events */
60 spi->mask = 0x00000000; /* Mask all SPI interrupts */
61 spi->com = 0; /* LST bit doesn't do anything, so disregard */
62}
63
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020064int spi_claim_bus(struct spi_slave *slave)
65{
66 return 0;
67}
68
69void spi_release_bus(struct spi_slave *slave)
70{
71
72}
73
74int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
75 void *din, unsigned long flags)
Ben Warren04a9e112008-01-16 22:37:35 -050076{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020077 volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi;
Ben Warren04a9e112008-01-16 22:37:35 -050078 unsigned int tmpdout, tmpdin, event;
Axel Lin583fe6c2013-07-12 17:42:15 +080079 int numBlks = DIV_ROUND_UP(bitlen, 32);
Ben Warren04a9e112008-01-16 22:37:35 -050080 int tm, isRead = 0;
81 unsigned char charSize = 32;
82
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020083 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
84 slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen);
Ben Warren04a9e112008-01-16 22:37:35 -050085
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020086 if (flags & SPI_XFER_BEGIN)
87 spi_cs_activate(slave);
Ben Warren04a9e112008-01-16 22:37:35 -050088
89 spi->event = 0xffffffff; /* Clear all SPI events */
90
91 /* handle data in 32-bit chunks */
92 while (numBlks--) {
93 tmpdout = 0;
94 charSize = (bitlen >= 32 ? 32 : bitlen);
95
96 /* Shift data so it's msb-justified */
97 tmpdout = *(u32 *) dout >> (32 - charSize);
98
99 /* The LEN field of the SPMODE register is set as follows:
100 *
Kim Phillips2956acd2008-01-17 12:48:00 -0600101 * Bit length setting
102 * len <= 4 3
103 * 4 < len <= 16 len - 1
104 * len > 16 0
Ben Warren04a9e112008-01-16 22:37:35 -0500105 */
106
Ira W. Snyderf138ca12012-09-12 14:17:31 -0700107 spi->mode &= ~SPI_MODE_EN;
108
Kim Phillips2956acd2008-01-17 12:48:00 -0600109 if (bitlen <= 16) {
110 if (bitlen <= 4)
111 spi->mode = (spi->mode & 0xff0fffff) |
Wolfgang Denk93e14592013-10-04 17:43:24 +0200112 (3 << 20);
Kim Phillips2956acd2008-01-17 12:48:00 -0600113 else
114 spi->mode = (spi->mode & 0xff0fffff) |
Wolfgang Denk93e14592013-10-04 17:43:24 +0200115 ((bitlen - 1) << 20);
Kim Phillips2956acd2008-01-17 12:48:00 -0600116 } else {
117 spi->mode = (spi->mode & 0xff0fffff);
Ben Warren04a9e112008-01-16 22:37:35 -0500118 /* Set up the next iteration if sending > 32 bits */
119 bitlen -= 32;
120 dout += 4;
121 }
122
Ira W. Snyderf138ca12012-09-12 14:17:31 -0700123 spi->mode |= SPI_MODE_EN;
124
Ben Warren04a9e112008-01-16 22:37:35 -0500125 spi->tx = tmpdout; /* Write the data out */
126 debug("*** spi_xfer: ... %08x written\n", tmpdout);
127
Kim Phillips2956acd2008-01-17 12:48:00 -0600128 /*
Ben Warren04a9e112008-01-16 22:37:35 -0500129 * Wait for SPI transmit to get out
130 * or time out (1 second = 1000 ms)
131 * The NE event must be read and cleared first
Kim Phillips2956acd2008-01-17 12:48:00 -0600132 */
Ben Warren04a9e112008-01-16 22:37:35 -0500133 for (tm = 0, isRead = 0; tm < SPI_TIMEOUT; ++tm) {
134 event = spi->event;
135 if (event & SPI_EV_NE) {
136 tmpdin = spi->rx;
137 spi->event |= SPI_EV_NE;
138 isRead = 1;
139
140 *(u32 *) din = (tmpdin << (32 - charSize));
141 if (charSize == 32) {
142 /* Advance output buffer by 32 bits */
143 din += 4;
144 }
145 }
Kim Phillips2956acd2008-01-17 12:48:00 -0600146 /*
147 * Only bail when we've had both NE and NF events.
Ben Warren04a9e112008-01-16 22:37:35 -0500148 * This will cause timeouts on RO devices, so maybe
149 * in the future put an arbitrary delay after writing
Kim Phillips2956acd2008-01-17 12:48:00 -0600150 * the device. Arbitrary delays suck, though...
151 */
Ben Warren04a9e112008-01-16 22:37:35 -0500152 if (isRead && (event & SPI_EV_NF))
153 break;
154 }
155 if (tm >= SPI_TIMEOUT)
156 puts("*** spi_xfer: Time out during SPI transfer");
157
158 debug("*** spi_xfer: transfer ended. Value=%08x\n", tmpdin);
159 }
160
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200161 if (flags & SPI_XFER_END)
162 spi_cs_deactivate(slave);
Kim Phillips2956acd2008-01-17 12:48:00 -0600163
Ben Warren04a9e112008-01-16 22:37:35 -0500164 return 0;
165}