blob: 00cbcbf9fc0af946dc304572b713edcc352f115e [file] [log] [blame]
Ben Warren04a9e112008-01-16 22:37:35 -05001/*
2 * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc.
Stefan Roesea47a12b2010-04-15 16:07:28 +02003 * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers
Ben Warren04a9e112008-01-16 22:37:35 -05004 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Ben Warren04a9e112008-01-16 22:37:35 -05006 */
7
8#include <common.h>
Ben Warren8931ab12008-01-26 23:41:19 -05009
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020010#include <malloc.h>
Ben Warren04a9e112008-01-16 22:37:35 -050011#include <spi.h>
12#include <asm/mpc8xxx_spi.h>
13
Kim Phillips2956acd2008-01-17 12:48:00 -060014#define SPI_EV_NE (0x80000000 >> 22) /* Receiver Not Empty */
15#define SPI_EV_NF (0x80000000 >> 23) /* Transmitter Not Full */
Ben Warren04a9e112008-01-16 22:37:35 -050016
Kim Phillips2956acd2008-01-17 12:48:00 -060017#define SPI_MODE_LOOP (0x80000000 >> 1) /* Loopback mode */
18#define SPI_MODE_REV (0x80000000 >> 5) /* Reverse mode - MSB first */
19#define SPI_MODE_MS (0x80000000 >> 6) /* Always master */
20#define SPI_MODE_EN (0x80000000 >> 7) /* Enable interface */
Ben Warren04a9e112008-01-16 22:37:35 -050021
22#define SPI_TIMEOUT 1000
23
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020024struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
25 unsigned int max_hz, unsigned int mode)
26{
27 struct spi_slave *slave;
28
29 if (!spi_cs_is_valid(bus, cs))
30 return NULL;
31
Simon Glassd3504fe2013-03-18 19:23:40 +000032 slave = spi_alloc_slave_base(bus, cs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020033 if (!slave)
34 return NULL;
35
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020036 /*
37 * TODO: Some of the code in spi_init() should probably move
38 * here, or into spi_claim_bus() below.
39 */
40
41 return slave;
42}
43
44void spi_free_slave(struct spi_slave *slave)
45{
46 free(slave);
47}
48
Ben Warren04a9e112008-01-16 22:37:35 -050049void spi_init(void)
50{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051 volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi;
Ben Warren04a9e112008-01-16 22:37:35 -050052
Kim Phillips2956acd2008-01-17 12:48:00 -060053 /*
Ben Warren04a9e112008-01-16 22:37:35 -050054 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
55 * some registers
Kim Phillips2956acd2008-01-17 12:48:00 -060056 */
Ben Warren04a9e112008-01-16 22:37:35 -050057 spi->mode = SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN;
Jagan Tekia048d4b2015-10-23 01:38:07 +053058 spi->mode = (spi->mode & 0xfff0ffff) | BIT(16); /* Use SYSCLK / 8
Kim Phillips2956acd2008-01-17 12:48:00 -060059 (16.67MHz typ.) */
Ben Warren04a9e112008-01-16 22:37:35 -050060 spi->event = 0xffffffff; /* Clear all SPI events */
61 spi->mask = 0x00000000; /* Mask all SPI interrupts */
62 spi->com = 0; /* LST bit doesn't do anything, so disregard */
63}
64
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020065int spi_claim_bus(struct spi_slave *slave)
66{
67 return 0;
68}
69
70void spi_release_bus(struct spi_slave *slave)
71{
72
73}
74
75int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
76 void *din, unsigned long flags)
Ben Warren04a9e112008-01-16 22:37:35 -050077{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020078 volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi;
Ben Warren04a9e112008-01-16 22:37:35 -050079 unsigned int tmpdout, tmpdin, event;
Axel Lin583fe6c2013-07-12 17:42:15 +080080 int numBlks = DIV_ROUND_UP(bitlen, 32);
Ben Warren04a9e112008-01-16 22:37:35 -050081 int tm, isRead = 0;
82 unsigned char charSize = 32;
83
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020084 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
85 slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen);
Ben Warren04a9e112008-01-16 22:37:35 -050086
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020087 if (flags & SPI_XFER_BEGIN)
88 spi_cs_activate(slave);
Ben Warren04a9e112008-01-16 22:37:35 -050089
90 spi->event = 0xffffffff; /* Clear all SPI events */
91
92 /* handle data in 32-bit chunks */
93 while (numBlks--) {
94 tmpdout = 0;
95 charSize = (bitlen >= 32 ? 32 : bitlen);
96
97 /* Shift data so it's msb-justified */
98 tmpdout = *(u32 *) dout >> (32 - charSize);
99
100 /* The LEN field of the SPMODE register is set as follows:
101 *
Kim Phillips2956acd2008-01-17 12:48:00 -0600102 * Bit length setting
103 * len <= 4 3
104 * 4 < len <= 16 len - 1
105 * len > 16 0
Ben Warren04a9e112008-01-16 22:37:35 -0500106 */
107
Ira W. Snyderf138ca12012-09-12 14:17:31 -0700108 spi->mode &= ~SPI_MODE_EN;
109
Kim Phillips2956acd2008-01-17 12:48:00 -0600110 if (bitlen <= 16) {
111 if (bitlen <= 4)
112 spi->mode = (spi->mode & 0xff0fffff) |
Wolfgang Denk93e14592013-10-04 17:43:24 +0200113 (3 << 20);
Kim Phillips2956acd2008-01-17 12:48:00 -0600114 else
115 spi->mode = (spi->mode & 0xff0fffff) |
Wolfgang Denk93e14592013-10-04 17:43:24 +0200116 ((bitlen - 1) << 20);
Kim Phillips2956acd2008-01-17 12:48:00 -0600117 } else {
118 spi->mode = (spi->mode & 0xff0fffff);
Ben Warren04a9e112008-01-16 22:37:35 -0500119 /* Set up the next iteration if sending > 32 bits */
120 bitlen -= 32;
121 dout += 4;
122 }
123
Ira W. Snyderf138ca12012-09-12 14:17:31 -0700124 spi->mode |= SPI_MODE_EN;
125
Ben Warren04a9e112008-01-16 22:37:35 -0500126 spi->tx = tmpdout; /* Write the data out */
127 debug("*** spi_xfer: ... %08x written\n", tmpdout);
128
Kim Phillips2956acd2008-01-17 12:48:00 -0600129 /*
Ben Warren04a9e112008-01-16 22:37:35 -0500130 * Wait for SPI transmit to get out
131 * or time out (1 second = 1000 ms)
132 * The NE event must be read and cleared first
Kim Phillips2956acd2008-01-17 12:48:00 -0600133 */
Ben Warren04a9e112008-01-16 22:37:35 -0500134 for (tm = 0, isRead = 0; tm < SPI_TIMEOUT; ++tm) {
135 event = spi->event;
136 if (event & SPI_EV_NE) {
137 tmpdin = spi->rx;
138 spi->event |= SPI_EV_NE;
139 isRead = 1;
140
141 *(u32 *) din = (tmpdin << (32 - charSize));
142 if (charSize == 32) {
143 /* Advance output buffer by 32 bits */
144 din += 4;
145 }
146 }
Kim Phillips2956acd2008-01-17 12:48:00 -0600147 /*
148 * Only bail when we've had both NE and NF events.
Ben Warren04a9e112008-01-16 22:37:35 -0500149 * This will cause timeouts on RO devices, so maybe
150 * in the future put an arbitrary delay after writing
Kim Phillips2956acd2008-01-17 12:48:00 -0600151 * the device. Arbitrary delays suck, though...
152 */
Ben Warren04a9e112008-01-16 22:37:35 -0500153 if (isRead && (event & SPI_EV_NF))
154 break;
155 }
156 if (tm >= SPI_TIMEOUT)
157 puts("*** spi_xfer: Time out during SPI transfer");
158
159 debug("*** spi_xfer: transfer ended. Value=%08x\n", tmpdin);
160 }
161
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200162 if (flags & SPI_XFER_END)
163 spi_cs_deactivate(slave);
Kim Phillips2956acd2008-01-17 12:48:00 -0600164
Ben Warren04a9e112008-01-16 22:37:35 -0500165 return 0;
166}