blob: 7c845827627bc2a276e326f7119fab2c485fffc9 [file] [log] [blame]
Mingkai Hu799efd92011-04-26 16:31:16 +08001/*
2 * eSPI controller driver.
3 *
4 * Copyright 2010-2011 Freescale Semiconductor, Inc.
5 * Author: Mingkai Hu (Mingkai.hu@freescale.com)
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Mingkai Hu799efd92011-04-26 16:31:16 +08008 */
9
10#include <common.h>
11
12#include <malloc.h>
13#include <spi.h>
14#include <asm/immap_85xx.h>
15
16struct fsl_spi_slave {
17 struct spi_slave slave;
18 unsigned int div16;
19 unsigned int pm;
20 unsigned int mode;
21 size_t cmd_len;
22 u8 cmd_buf[16];
23 size_t data_len;
24 unsigned int max_transfer_length;
25};
26
27#define to_fsl_spi_slave(s) container_of(s, struct fsl_spi_slave, slave)
28
29#define ESPI_MAX_CS_NUM 4
30
31#define ESPI_EV_RNE (1 << 9)
32#define ESPI_EV_TNF (1 << 8)
33
34#define ESPI_MODE_EN (1 << 31) /* Enable interface */
35#define ESPI_MODE_TXTHR(x) ((x) << 8) /* Tx FIFO threshold */
36#define ESPI_MODE_RXTHR(x) ((x) << 0) /* Rx FIFO threshold */
37
38#define ESPI_COM_CS(x) ((x) << 30)
39#define ESPI_COM_TRANLEN(x) ((x) << 0)
40
41#define ESPI_CSMODE_CI_INACTIVEHIGH (1 << 31)
42#define ESPI_CSMODE_CP_BEGIN_EDGCLK (1 << 30)
43#define ESPI_CSMODE_REV_MSB_FIRST (1 << 29)
44#define ESPI_CSMODE_DIV16 (1 << 28)
45#define ESPI_CSMODE_PM(x) ((x) << 24)
46#define ESPI_CSMODE_POL_ASSERTED_LOW (1 << 20)
47#define ESPI_CSMODE_LEN(x) ((x) << 16)
48#define ESPI_CSMODE_CSBEF(x) ((x) << 12)
49#define ESPI_CSMODE_CSAFT(x) ((x) << 8)
50#define ESPI_CSMODE_CSCG(x) ((x) << 3)
51
52#define ESPI_CSMODE_INIT_VAL (ESPI_CSMODE_POL_ASSERTED_LOW | \
53 ESPI_CSMODE_CSBEF(0) | ESPI_CSMODE_CSAFT(0) | \
54 ESPI_CSMODE_CSCG(1))
55
56#define ESPI_MAX_DATA_TRANSFER_LEN 0xFFF0
57
58struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
59 unsigned int max_hz, unsigned int mode)
60{
61 struct fsl_spi_slave *fsl;
62 sys_info_t sysinfo;
63 unsigned long spibrg = 0;
64 unsigned char pm = 0;
65
66 if (!spi_cs_is_valid(bus, cs))
67 return NULL;
68
Simon Glassd034a952013-03-18 19:23:40 +000069 fsl = spi_alloc_slave(struct fsl_spi_slave, bus, cs);
Mingkai Hu799efd92011-04-26 16:31:16 +080070 if (!fsl)
71 return NULL;
72
Mingkai Hu799efd92011-04-26 16:31:16 +080073 fsl->mode = mode;
74 fsl->max_transfer_length = ESPI_MAX_DATA_TRANSFER_LEN;
75
76 /* Set eSPI BRG clock source */
77 get_sys_info(&sysinfo);
Prabhakar Kushwahad1698082013-08-16 14:52:26 +053078 spibrg = sysinfo.freq_systembus / 2;
Mingkai Hu799efd92011-04-26 16:31:16 +080079 fsl->div16 = 0;
80 if ((spibrg / max_hz) > 32) {
81 fsl->div16 = ESPI_CSMODE_DIV16;
82 pm = spibrg / (max_hz * 16 * 2);
83 if (pm > 16) {
84 pm = 16;
Marek Vasutd36e8c32011-10-21 14:17:20 +000085 debug("Requested speed is too low: %d Hz, %ld Hz "
86 "is used.\n", max_hz, spibrg / (32 * 16));
Mingkai Hu799efd92011-04-26 16:31:16 +080087 }
88 } else
89 pm = spibrg / (max_hz * 2);
90 if (pm)
91 pm--;
92 fsl->pm = pm;
93
94 return &fsl->slave;
95}
96
97void spi_free_slave(struct spi_slave *slave)
98{
99 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
100 free(fsl);
101}
102
103void spi_init(void)
104{
105
106}
107
108int spi_claim_bus(struct spi_slave *slave)
109{
110 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
111 ccsr_espi_t *espi = (void *)(CONFIG_SYS_MPC85xx_ESPI_ADDR);
112 unsigned char pm = fsl->pm;
113 unsigned int cs = slave->cs;
114 unsigned int mode = fsl->mode;
115 unsigned int div16 = fsl->div16;
116 int i;
117
118 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, cs);
119
120 /* Enable eSPI interface */
121 out_be32(&espi->mode, ESPI_MODE_RXTHR(3)
122 | ESPI_MODE_TXTHR(4) | ESPI_MODE_EN);
123
124 out_be32(&espi->event, 0xffffffff); /* Clear all eSPI events */
125 out_be32(&espi->mask, 0x00000000); /* Mask all eSPI interrupts */
126
127 /* Init CS mode interface */
128 for (i = 0; i < ESPI_MAX_CS_NUM; i++)
129 out_be32(&espi->csmode[i], ESPI_CSMODE_INIT_VAL);
130
131 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs]) &
132 ~(ESPI_CSMODE_PM(0xF) | ESPI_CSMODE_DIV16
133 | ESPI_CSMODE_CI_INACTIVEHIGH | ESPI_CSMODE_CP_BEGIN_EDGCLK
134 | ESPI_CSMODE_REV_MSB_FIRST | ESPI_CSMODE_LEN(0xF)));
135
136 /* Set eSPI BRG clock source */
137 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
138 | ESPI_CSMODE_PM(pm) | div16);
139
140 /* Set eSPI mode */
141 if (mode & SPI_CPHA)
142 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
143 | ESPI_CSMODE_CP_BEGIN_EDGCLK);
144 if (mode & SPI_CPOL)
145 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
146 | ESPI_CSMODE_CI_INACTIVEHIGH);
147
148 /* Character bit order: msb first */
149 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
150 | ESPI_CSMODE_REV_MSB_FIRST);
151
152 /* Character length in bits, between 0x3~0xf, i.e. 4bits~16bits */
153 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
154 | ESPI_CSMODE_LEN(7));
155
156 return 0;
157}
158
159void spi_release_bus(struct spi_slave *slave)
160{
161
162}
163
164int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *data_out,
165 void *data_in, unsigned long flags)
166{
167 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
168 ccsr_espi_t *espi = (void *)(CONFIG_SYS_MPC85xx_ESPI_ADDR);
169 unsigned int tmpdout, tmpdin, event;
170 const void *dout = NULL;
171 void *din = NULL;
172 int len = 0;
173 int num_blks, num_chunks, max_tran_len, tran_len;
174 int num_bytes;
175 unsigned char *ch;
176 unsigned char *buffer = NULL;
177 size_t buf_len;
178 u8 *cmd_buf = fsl->cmd_buf;
179 size_t cmd_len = fsl->cmd_len;
180 size_t data_len = bitlen / 8;
181 size_t rx_offset = 0;
182
183 max_tran_len = fsl->max_transfer_length;
184 switch (flags) {
185 case SPI_XFER_BEGIN:
186 cmd_len = fsl->cmd_len = data_len;
187 memcpy(cmd_buf, data_out, cmd_len);
188 return 0;
189 case 0:
190 case SPI_XFER_END:
191 if (bitlen == 0) {
192 spi_cs_deactivate(slave);
193 return 0;
194 }
195 buf_len = 2 * cmd_len + min(data_len, max_tran_len);
196 len = cmd_len + data_len;
197 rx_offset = cmd_len;
198 buffer = (unsigned char *)malloc(buf_len);
199 if (!buffer) {
200 debug("SF: Failed to malloc memory.\n");
201 return 1;
202 }
203 memcpy(buffer, cmd_buf, cmd_len);
Shaohui Xie45cead82012-10-11 20:31:46 +0000204 if (data_in == NULL)
205 memcpy(buffer + cmd_len, data_out, data_len);
Mingkai Hu799efd92011-04-26 16:31:16 +0800206 break;
207 case SPI_XFER_BEGIN | SPI_XFER_END:
208 len = data_len;
209 buffer = (unsigned char *)malloc(len * 2);
210 if (!buffer) {
211 debug("SF: Failed to malloc memory.\n");
212 return 1;
213 }
214 memcpy(buffer, data_out, len);
215 rx_offset = len;
216 cmd_len = 0;
217 break;
218 }
219
Marek Vasutd36e8c32011-10-21 14:17:20 +0000220 debug("spi_xfer: slave %u:%u dout %08X(%p) din %08X(%p) len %u\n",
Mingkai Hu799efd92011-04-26 16:31:16 +0800221 slave->bus, slave->cs, *(uint *) dout,
222 dout, *(uint *) din, din, len);
223
Axel Linafcab282013-07-12 17:41:08 +0800224 num_chunks = DIV_ROUND_UP(data_len, max_tran_len);
Mingkai Hu799efd92011-04-26 16:31:16 +0800225 while (num_chunks--) {
226 if (data_in)
227 din = buffer + rx_offset;
228 dout = buffer;
229 tran_len = min(data_len , max_tran_len);
Axel Linafcab282013-07-12 17:41:08 +0800230 num_blks = DIV_ROUND_UP(tran_len + cmd_len, 4);
Mingkai Hu799efd92011-04-26 16:31:16 +0800231 num_bytes = (tran_len + cmd_len) % 4;
232 fsl->data_len = tran_len + cmd_len;
233 spi_cs_activate(slave);
234
235 /* Clear all eSPI events */
236 out_be32(&espi->event , 0xffffffff);
237 /* handle data in 32-bit chunks */
238 while (num_blks--) {
239
240 event = in_be32(&espi->event);
241 if (event & ESPI_EV_TNF) {
242 tmpdout = *(u32 *)dout;
243
244 /* Set up the next iteration */
245 if (len > 4) {
246 len -= 4;
247 dout += 4;
248 }
249
250 out_be32(&espi->tx, tmpdout);
251 out_be32(&espi->event, ESPI_EV_TNF);
252 debug("***spi_xfer:...%08x written\n", tmpdout);
253 }
254
255 /* Wait for eSPI transmit to get out */
256 udelay(80);
257
258 event = in_be32(&espi->event);
259 if (event & ESPI_EV_RNE) {
260 tmpdin = in_be32(&espi->rx);
261 if (num_blks == 0 && num_bytes != 0) {
262 ch = (unsigned char *)&tmpdin;
263 while (num_bytes--)
264 *(unsigned char *)din++ = *ch++;
265 } else {
266 *(u32 *) din = tmpdin;
267 din += 4;
268 }
269
270 out_be32(&espi->event, in_be32(&espi->event)
271 | ESPI_EV_RNE);
272 debug("***spi_xfer:...%08x readed\n", tmpdin);
273 }
274 }
275 if (data_in) {
276 memcpy(data_in, buffer + 2 * cmd_len, tran_len);
277 if (*buffer == 0x0b) {
278 data_in += tran_len;
279 data_len -= tran_len;
280 *(int *)buffer += tran_len;
281 }
282 }
283 spi_cs_deactivate(slave);
284 }
285
286 free(buffer);
287 return 0;
288}
289
290int spi_cs_is_valid(unsigned int bus, unsigned int cs)
291{
292 return bus == 0 && cs < ESPI_MAX_CS_NUM;
293}
294
295void spi_cs_activate(struct spi_slave *slave)
296{
297 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
298 ccsr_espi_t *espi = (void *)(CONFIG_SYS_MPC85xx_ESPI_ADDR);
299 unsigned int com = 0;
300 size_t data_len = fsl->data_len;
301
302 com &= ~(ESPI_COM_CS(0x3) | ESPI_COM_TRANLEN(0xFFFF));
303 com |= ESPI_COM_CS(slave->cs);
304 com |= ESPI_COM_TRANLEN(data_len - 1);
305 out_be32(&espi->com, com);
306}
307
308void spi_cs_deactivate(struct spi_slave *slave)
309{
310 ccsr_espi_t *espi = (void *)(CONFIG_SYS_MPC85xx_ESPI_ADDR);
311
312 /* clear the RXCNT and TXCNT */
313 out_be32(&espi->mode, in_be32(&espi->mode) & (~ESPI_MODE_EN));
314 out_be32(&espi->mode, in_be32(&espi->mode) | ESPI_MODE_EN);
315}