blob: 1dd5bd97990812785d337cac2581d62b1d40dc53 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ben Warren7efe9272008-01-16 22:37:35 -05002/*
3 * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc.
Stefan Roese88fbf932010-04-15 16:07:28 +02004 * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers
Ben Warren7efe9272008-01-16 22:37:35 -05005 */
6
7#include <common.h>
Ben Warren37531402008-01-26 23:41:19 -05008
Haavard Skinnemoend74084a2008-05-16 11:10:31 +02009#include <malloc.h>
Ben Warren7efe9272008-01-16 22:37:35 -050010#include <spi.h>
11#include <asm/mpc8xxx_spi.h>
12
Mario Six10f300a2019-04-29 01:58:41 +053013enum {
14 SPI_EV_NE = BIT(31 - 22), /* Receiver Not Empty */
15 SPI_EV_NF = BIT(31 - 23), /* Transmitter Not Full */
16};
17
18enum {
19 SPI_MODE_LOOP = BIT(31 - 1), /* Loopback mode */
20 SPI_MODE_CI = BIT(31 - 2), /* Clock invert */
21 SPI_MODE_CP = BIT(31 - 3), /* Clock phase */
22 SPI_MODE_DIV16 = BIT(31 - 4), /* Divide clock source by 16 */
23 SPI_MODE_REV = BIT(31 - 5), /* Reverse mode - MSB first */
24 SPI_MODE_MS = BIT(31 - 6), /* Always master */
25 SPI_MODE_EN = BIT(31 - 7), /* Enable interface */
26
27 SPI_MODE_LEN_MASK = 0xf00000,
28 SPI_MODE_PM_MASK = 0xf0000,
Ben Warren7efe9272008-01-16 22:37:35 -050029
Mario Six10f300a2019-04-29 01:58:41 +053030 SPI_COM_LST = BIT(31 - 9),
31};
Ben Warren7efe9272008-01-16 22:37:35 -050032
Mario Sixeb2533a2019-04-29 01:58:47 +053033static inline u32 to_prescale_mod(u32 val)
34{
35 return (min(val, (u32)15) << 16);
36}
37
38static void set_char_len(spi8xxx_t *spi, u32 val)
39{
40 clrsetbits_be32(&spi->mode, SPI_MODE_LEN_MASK, (val << 20));
41}
42
Ben Warren7efe9272008-01-16 22:37:35 -050043#define SPI_TIMEOUT 1000
44
Mario Six4f4936e2019-04-29 01:58:36 +053045struct spi_slave *spi_setup_slave(uint bus, uint cs, uint max_hz, uint mode)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020046{
47 struct spi_slave *slave;
48
49 if (!spi_cs_is_valid(bus, cs))
50 return NULL;
51
Simon Glassd034a952013-03-18 19:23:40 +000052 slave = spi_alloc_slave_base(bus, cs);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020053 if (!slave)
54 return NULL;
55
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020056 /*
57 * TODO: Some of the code in spi_init() should probably move
58 * here, or into spi_claim_bus() below.
59 */
60
61 return slave;
62}
63
64void spi_free_slave(struct spi_slave *slave)
65{
66 free(slave);
67}
68
Ben Warren7efe9272008-01-16 22:37:35 -050069void spi_init(void)
70{
Mario Sixdee99492019-04-29 01:58:42 +053071 spi8xxx_t *spi = &((immap_t *)(CONFIG_SYS_IMMR))->spi;
Ben Warren7efe9272008-01-16 22:37:35 -050072
Kim Phillipsb8e25202008-01-17 12:48:00 -060073 /*
Ben Warren7efe9272008-01-16 22:37:35 -050074 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
75 * some registers
Kim Phillipsb8e25202008-01-17 12:48:00 -060076 */
Mario Sixdee99492019-04-29 01:58:42 +053077 out_be32(&spi->mode, SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN);
Mario Six4d3082b2019-04-29 01:58:37 +053078 /* Use SYSCLK / 8 (16.67MHz typ.) */
Mario Sixeb2533a2019-04-29 01:58:47 +053079 clrsetbits_be32(&spi->mode, SPI_MODE_PM_MASK, to_prescale_mod(1));
Mario Six4d3082b2019-04-29 01:58:37 +053080 /* Clear all SPI events */
Mario Sixdee99492019-04-29 01:58:42 +053081 setbits_be32(&spi->event, 0xffffffff);
Mario Six4d3082b2019-04-29 01:58:37 +053082 /* Mask all SPI interrupts */
Mario Sixdee99492019-04-29 01:58:42 +053083 clrbits_be32(&spi->mask, 0xffffffff);
Mario Six4d3082b2019-04-29 01:58:37 +053084 /* LST bit doesn't do anything, so disregard */
Mario Sixdee99492019-04-29 01:58:42 +053085 out_be32(&spi->com, 0);
Ben Warren7efe9272008-01-16 22:37:35 -050086}
87
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020088int spi_claim_bus(struct spi_slave *slave)
89{
90 return 0;
91}
92
93void spi_release_bus(struct spi_slave *slave)
94{
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020095}
96
Mario Six4f4936e2019-04-29 01:58:36 +053097int spi_xfer(struct spi_slave *slave, uint bitlen, const void *dout, void *din,
98 ulong flags)
Ben Warren7efe9272008-01-16 22:37:35 -050099{
Mario Sixdee99492019-04-29 01:58:42 +0530100 spi8xxx_t *spi = &((immap_t *)(CONFIG_SYS_IMMR))->spi;
Mario Six8d684ec2019-04-29 01:58:46 +0530101 u32 tmpdin;
Mario Six56edb8b2019-04-29 01:58:38 +0530102 int num_blks = DIV_ROUND_UP(bitlen, 32);
Ben Warren7efe9272008-01-16 22:37:35 -0500103
Mario Sixf9d5ca22019-04-29 01:58:40 +0530104 debug("%s: slave %u:%u dout %08X din %08X bitlen %u\n", __func__,
Mario Six6a644f02019-04-29 01:58:39 +0530105 slave->bus, slave->cs, *(uint *)dout, *(uint *)din, bitlen);
Ben Warren7efe9272008-01-16 22:37:35 -0500106
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200107 if (flags & SPI_XFER_BEGIN)
108 spi_cs_activate(slave);
Ben Warren7efe9272008-01-16 22:37:35 -0500109
Mario Six4d3082b2019-04-29 01:58:37 +0530110 /* Clear all SPI events */
Mario Sixdee99492019-04-29 01:58:42 +0530111 setbits_be32(&spi->event, 0xffffffff);
Ben Warren7efe9272008-01-16 22:37:35 -0500112
Mario Six4d3082b2019-04-29 01:58:37 +0530113 /* Handle data in 32-bit chunks */
Mario Six56edb8b2019-04-29 01:58:38 +0530114 while (num_blks--) {
Mario Six8d684ec2019-04-29 01:58:46 +0530115 int tm;
116 u32 tmpdout = 0;
Mario Sixa67510f2019-04-29 01:58:48 +0530117 uchar xfer_bitlen = (bitlen >= 32 ? 32 : bitlen);
Ben Warren7efe9272008-01-16 22:37:35 -0500118
119 /* Shift data so it's msb-justified */
Mario Sixa67510f2019-04-29 01:58:48 +0530120 tmpdout = *(u32 *)dout >> (32 - xfer_bitlen);
Ben Warren7efe9272008-01-16 22:37:35 -0500121
Mario Sixdee99492019-04-29 01:58:42 +0530122 clrbits_be32(&spi->mode, SPI_MODE_EN);
Ira W. Snyder5261b002012-09-12 14:17:31 -0700123
Mario Six64b07ff2019-04-29 01:58:49 +0530124 /* Set up length for this transfer */
125
126 if (bitlen <= 4) /* 4 bits or less */
Mario Sixeb2533a2019-04-29 01:58:47 +0530127 set_char_len(spi, 3);
Mario Six64b07ff2019-04-29 01:58:49 +0530128 else if (bitlen <= 16) /* at most 16 bits */
Mario Sixeb2533a2019-04-29 01:58:47 +0530129 set_char_len(spi, bitlen - 1);
Mario Six64b07ff2019-04-29 01:58:49 +0530130 else /* more than 16 bits -> full 32 bit transfer */
Mario Sixeb2533a2019-04-29 01:58:47 +0530131 set_char_len(spi, 0);
132
133 if (bitlen > 16) {
Ben Warren7efe9272008-01-16 22:37:35 -0500134 /* Set up the next iteration if sending > 32 bits */
135 bitlen -= 32;
136 dout += 4;
137 }
138
Mario Sixdee99492019-04-29 01:58:42 +0530139 setbits_be32(&spi->mode, SPI_MODE_EN);
Ira W. Snyder5261b002012-09-12 14:17:31 -0700140
Mario Six4d3082b2019-04-29 01:58:37 +0530141 /* Write the data out */
Mario Sixdee99492019-04-29 01:58:42 +0530142 out_be32(&spi->tx, tmpdout);
Mario Six4d3082b2019-04-29 01:58:37 +0530143
Mario Sixf9d5ca22019-04-29 01:58:40 +0530144 debug("*** %s: ... %08x written\n", __func__, tmpdout);
Ben Warren7efe9272008-01-16 22:37:35 -0500145
Kim Phillipsb8e25202008-01-17 12:48:00 -0600146 /*
Ben Warren7efe9272008-01-16 22:37:35 -0500147 * Wait for SPI transmit to get out
148 * or time out (1 second = 1000 ms)
149 * The NE event must be read and cleared first
Kim Phillipsb8e25202008-01-17 12:48:00 -0600150 */
Mario Six4b671e12019-04-29 01:58:44 +0530151 for (tm = 0; tm < SPI_TIMEOUT; ++tm) {
Mario Six8d684ec2019-04-29 01:58:46 +0530152 u32 event = in_be32(&spi->event);
Mario Six4b671e12019-04-29 01:58:44 +0530153 bool have_ne = event & SPI_EV_NE;
154 bool have_nf = event & SPI_EV_NF;
155
Mario Six2afedfe2019-04-29 01:58:45 +0530156 if (!have_ne)
157 continue;
Ben Warren7efe9272008-01-16 22:37:35 -0500158
Mario Six2afedfe2019-04-29 01:58:45 +0530159 tmpdin = in_be32(&spi->rx);
160 setbits_be32(&spi->event, SPI_EV_NE);
161
Mario Sixa67510f2019-04-29 01:58:48 +0530162 *(u32 *)din = (tmpdin << (32 - xfer_bitlen));
163 if (xfer_bitlen == 32) {
Mario Six2afedfe2019-04-29 01:58:45 +0530164 /* Advance output buffer by 32 bits */
165 din += 4;
Ben Warren7efe9272008-01-16 22:37:35 -0500166 }
Mario Six2afedfe2019-04-29 01:58:45 +0530167
Kim Phillipsb8e25202008-01-17 12:48:00 -0600168 /*
169 * Only bail when we've had both NE and NF events.
Ben Warren7efe9272008-01-16 22:37:35 -0500170 * This will cause timeouts on RO devices, so maybe
171 * in the future put an arbitrary delay after writing
Kim Phillipsb8e25202008-01-17 12:48:00 -0600172 * the device. Arbitrary delays suck, though...
173 */
Mario Six2afedfe2019-04-29 01:58:45 +0530174 if (have_nf)
Ben Warren7efe9272008-01-16 22:37:35 -0500175 break;
176 }
Mario Six2afedfe2019-04-29 01:58:45 +0530177
Ben Warren7efe9272008-01-16 22:37:35 -0500178 if (tm >= SPI_TIMEOUT)
Mario Sixf9d5ca22019-04-29 01:58:40 +0530179 debug("*** %s: Time out during SPI transfer\n",
180 __func__);
Ben Warren7efe9272008-01-16 22:37:35 -0500181
Mario Sixf9d5ca22019-04-29 01:58:40 +0530182 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
Ben Warren7efe9272008-01-16 22:37:35 -0500183 }
184
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200185 if (flags & SPI_XFER_END)
186 spi_cs_deactivate(slave);
Kim Phillipsb8e25202008-01-17 12:48:00 -0600187
Ben Warren7efe9272008-01-16 22:37:35 -0500188 return 0;
189}