Blame view

drivers/spi/mpc8xxx_spi.c 4.06 KB
04a9e1180   Ben Warren   Add support for a...
1
2
  /*
   * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc.
a47a12bec   Stefan Roese   Move arch/ppc to ...
3
   * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers
04a9e1180   Ben Warren   Add support for a...
4
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
5
   * SPDX-License-Identifier:	GPL-2.0+
04a9e1180   Ben Warren   Add support for a...
6
7
8
   */
  
  #include <common.h>
8931ab176   Ben Warren   Fix conditional c...
9

d255bb0e7   Haavard Skinnemoen   SPI API improvements
10
  #include <malloc.h>
04a9e1180   Ben Warren   Add support for a...
11
12
  #include <spi.h>
  #include <asm/mpc8xxx_spi.h>
2956acd5e   Kim Phillips   codingstyle clean...
13
14
  #define SPI_EV_NE	(0x80000000 >> 22)	/* Receiver Not Empty */
  #define SPI_EV_NF	(0x80000000 >> 23)	/* Transmitter Not Full */
04a9e1180   Ben Warren   Add support for a...
15

2956acd5e   Kim Phillips   codingstyle clean...
16
17
18
19
  #define SPI_MODE_LOOP	(0x80000000 >> 1)	/* Loopback mode */
  #define SPI_MODE_REV	(0x80000000 >> 5)	/* Reverse mode - MSB first */
  #define SPI_MODE_MS	(0x80000000 >> 6)	/* Always master */
  #define SPI_MODE_EN	(0x80000000 >> 7)	/* Enable interface */
04a9e1180   Ben Warren   Add support for a...
20
21
  
  #define SPI_TIMEOUT	1000
d255bb0e7   Haavard Skinnemoen   SPI API improvements
22
23
24
25
26
27
28
  struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  		unsigned int max_hz, unsigned int mode)
  {
  	struct spi_slave *slave;
  
  	if (!spi_cs_is_valid(bus, cs))
  		return NULL;
d3504fee7   Simon Glass   spi: Use spi_allo...
29
  	slave = spi_alloc_slave_base(bus, cs);
d255bb0e7   Haavard Skinnemoen   SPI API improvements
30
31
  	if (!slave)
  		return NULL;
d255bb0e7   Haavard Skinnemoen   SPI API improvements
32
33
34
35
36
37
38
39
40
41
42
43
  	/*
  	 * TODO: Some of the code in spi_init() should probably move
  	 * here, or into spi_claim_bus() below.
  	 */
  
  	return slave;
  }
  
  void spi_free_slave(struct spi_slave *slave)
  {
  	free(slave);
  }
04a9e1180   Ben Warren   Add support for a...
44
45
  void spi_init(void)
  {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
46
  	volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi;
04a9e1180   Ben Warren   Add support for a...
47

2956acd5e   Kim Phillips   codingstyle clean...
48
  	/*
04a9e1180   Ben Warren   Add support for a...
49
50
  	 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
  	 * some registers
2956acd5e   Kim Phillips   codingstyle clean...
51
  	 */
04a9e1180   Ben Warren   Add support for a...
52
  	spi->mode = SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN;
a048d4bbb   Jagan Teki   spi: mpc8xxx_spi:...
53
  	spi->mode = (spi->mode & 0xfff0ffff) | BIT(16); /* Use SYSCLK / 8
2956acd5e   Kim Phillips   codingstyle clean...
54
  							     (16.67MHz typ.) */
04a9e1180   Ben Warren   Add support for a...
55
56
57
58
  	spi->event = 0xffffffff;	/* Clear all SPI events */
  	spi->mask = 0x00000000;	/* Mask  all SPI interrupts */
  	spi->com = 0;		/* LST bit doesn't do anything, so disregard */
  }
d255bb0e7   Haavard Skinnemoen   SPI API improvements
59
60
61
62
63
64
65
66
67
68
69
70
  int spi_claim_bus(struct spi_slave *slave)
  {
  	return 0;
  }
  
  void spi_release_bus(struct spi_slave *slave)
  {
  
  }
  
  int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  		void *din, unsigned long flags)
04a9e1180   Ben Warren   Add support for a...
71
  {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
72
  	volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi;
04a9e1180   Ben Warren   Add support for a...
73
  	unsigned int tmpdout, tmpdin, event;
583fe6c3d   Axel Lin   spi: mpc8xxx_spi:...
74
  	int numBlks = DIV_ROUND_UP(bitlen, 32);
04a9e1180   Ben Warren   Add support for a...
75
76
  	int tm, isRead = 0;
  	unsigned char charSize = 32;
d255bb0e7   Haavard Skinnemoen   SPI API improvements
77
78
79
  	debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u
  ",
  	      slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen);
04a9e1180   Ben Warren   Add support for a...
80

d255bb0e7   Haavard Skinnemoen   SPI API improvements
81
82
  	if (flags & SPI_XFER_BEGIN)
  		spi_cs_activate(slave);
04a9e1180   Ben Warren   Add support for a...
83
84
85
86
87
88
89
90
91
92
93
94
95
  
  	spi->event = 0xffffffff;	/* Clear all SPI events */
  
  	/* handle data in 32-bit chunks */
  	while (numBlks--) {
  		tmpdout = 0;
  		charSize = (bitlen >= 32 ? 32 : bitlen);
  
  		/* Shift data so it's msb-justified */
  		tmpdout = *(u32 *) dout >> (32 - charSize);
  
  		/* The LEN field of the SPMODE register is set as follows:
  		 *
2956acd5e   Kim Phillips   codingstyle clean...
96
97
98
99
  		 * Bit length             setting
  		 * len <= 4               3
  		 * 4 < len <= 16          len - 1
  		 * len > 16               0
04a9e1180   Ben Warren   Add support for a...
100
  		 */
f138ca137   Ira W. Snyder   mpc8xxx_spi: fix ...
101
  		spi->mode &= ~SPI_MODE_EN;
2956acd5e   Kim Phillips   codingstyle clean...
102
103
104
  		if (bitlen <= 16) {
  			if (bitlen <= 4)
  				spi->mode = (spi->mode & 0xff0fffff) |
93e145964   Wolfgang Denk   Coding Style clea...
105
  					    (3 << 20);
2956acd5e   Kim Phillips   codingstyle clean...
106
107
  			else
  				spi->mode = (spi->mode & 0xff0fffff) |
93e145964   Wolfgang Denk   Coding Style clea...
108
  					    ((bitlen - 1) << 20);
2956acd5e   Kim Phillips   codingstyle clean...
109
110
  		} else {
  			spi->mode = (spi->mode & 0xff0fffff);
04a9e1180   Ben Warren   Add support for a...
111
112
113
114
  			/* Set up the next iteration if sending > 32 bits */
  			bitlen -= 32;
  			dout += 4;
  		}
f138ca137   Ira W. Snyder   mpc8xxx_spi: fix ...
115
  		spi->mode |= SPI_MODE_EN;
04a9e1180   Ben Warren   Add support for a...
116
117
118
  		spi->tx = tmpdout;	/* Write the data out */
  		debug("*** spi_xfer: ... %08x written
  ", tmpdout);
2956acd5e   Kim Phillips   codingstyle clean...
119
  		/*
04a9e1180   Ben Warren   Add support for a...
120
121
122
  		 * Wait for SPI transmit to get out
  		 * or time out (1 second = 1000 ms)
  		 * The NE event must be read and cleared first
2956acd5e   Kim Phillips   codingstyle clean...
123
  		 */
04a9e1180   Ben Warren   Add support for a...
124
125
126
127
128
129
130
131
132
133
134
135
136
  		for (tm = 0, isRead = 0; tm < SPI_TIMEOUT; ++tm) {
  			event = spi->event;
  			if (event & SPI_EV_NE) {
  				tmpdin = spi->rx;
  				spi->event |= SPI_EV_NE;
  				isRead = 1;
  
  				*(u32 *) din = (tmpdin << (32 - charSize));
  				if (charSize == 32) {
  					/* Advance output buffer by 32 bits */
  					din += 4;
  				}
  			}
2956acd5e   Kim Phillips   codingstyle clean...
137
138
  			/*
  			 * Only bail when we've had both NE and NF events.
04a9e1180   Ben Warren   Add support for a...
139
140
  			 * This will cause timeouts on RO devices, so maybe
  			 * in the future put an arbitrary delay after writing
2956acd5e   Kim Phillips   codingstyle clean...
141
142
  			 * the device.  Arbitrary delays suck, though...
  			 */
04a9e1180   Ben Warren   Add support for a...
143
144
145
146
147
148
149
150
151
  			if (isRead && (event & SPI_EV_NF))
  				break;
  		}
  		if (tm >= SPI_TIMEOUT)
  			puts("*** spi_xfer: Time out during SPI transfer");
  
  		debug("*** spi_xfer: transfer ended. Value=%08x
  ", tmpdin);
  	}
d255bb0e7   Haavard Skinnemoen   SPI API improvements
152
153
  	if (flags & SPI_XFER_END)
  		spi_cs_deactivate(slave);
2956acd5e   Kim Phillips   codingstyle clean...
154

04a9e1180   Ben Warren   Add support for a...
155
156
  	return 0;
  }