blob: 83fd8b3cc1e3aa6d4ac4601612ce6db33a4851df [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;
117 uchar char_size = (bitlen >= 32 ? 32 : bitlen);
Ben Warren7efe9272008-01-16 22:37:35 -0500118
119 /* Shift data so it's msb-justified */
Mario Six6a644f02019-04-29 01:58:39 +0530120 tmpdout = *(u32 *)dout >> (32 - char_size);
Ben Warren7efe9272008-01-16 22:37:35 -0500121
122 /* The LEN field of the SPMODE register is set as follows:
123 *
Kim Phillipsb8e25202008-01-17 12:48:00 -0600124 * Bit length setting
125 * len <= 4 3
126 * 4 < len <= 16 len - 1
127 * len > 16 0
Ben Warren7efe9272008-01-16 22:37:35 -0500128 */
129
Mario Sixdee99492019-04-29 01:58:42 +0530130 clrbits_be32(&spi->mode, SPI_MODE_EN);
Ira W. Snyder5261b002012-09-12 14:17:31 -0700131
Mario Sixeb2533a2019-04-29 01:58:47 +0530132 if (bitlen <= 4)
133 set_char_len(spi, 3);
134 else if (bitlen <= 16)
135 set_char_len(spi, bitlen - 1);
136 else
137 set_char_len(spi, 0);
138
139 if (bitlen > 16) {
Ben Warren7efe9272008-01-16 22:37:35 -0500140 /* Set up the next iteration if sending > 32 bits */
141 bitlen -= 32;
142 dout += 4;
143 }
144
Mario Sixdee99492019-04-29 01:58:42 +0530145 setbits_be32(&spi->mode, SPI_MODE_EN);
Ira W. Snyder5261b002012-09-12 14:17:31 -0700146
Mario Six4d3082b2019-04-29 01:58:37 +0530147 /* Write the data out */
Mario Sixdee99492019-04-29 01:58:42 +0530148 out_be32(&spi->tx, tmpdout);
Mario Six4d3082b2019-04-29 01:58:37 +0530149
Mario Sixf9d5ca22019-04-29 01:58:40 +0530150 debug("*** %s: ... %08x written\n", __func__, tmpdout);
Ben Warren7efe9272008-01-16 22:37:35 -0500151
Kim Phillipsb8e25202008-01-17 12:48:00 -0600152 /*
Ben Warren7efe9272008-01-16 22:37:35 -0500153 * Wait for SPI transmit to get out
154 * or time out (1 second = 1000 ms)
155 * The NE event must be read and cleared first
Kim Phillipsb8e25202008-01-17 12:48:00 -0600156 */
Mario Six4b671e12019-04-29 01:58:44 +0530157 for (tm = 0; tm < SPI_TIMEOUT; ++tm) {
Mario Six8d684ec2019-04-29 01:58:46 +0530158 u32 event = in_be32(&spi->event);
Mario Six4b671e12019-04-29 01:58:44 +0530159 bool have_ne = event & SPI_EV_NE;
160 bool have_nf = event & SPI_EV_NF;
161
Mario Six2afedfe2019-04-29 01:58:45 +0530162 if (!have_ne)
163 continue;
Ben Warren7efe9272008-01-16 22:37:35 -0500164
Mario Six2afedfe2019-04-29 01:58:45 +0530165 tmpdin = in_be32(&spi->rx);
166 setbits_be32(&spi->event, SPI_EV_NE);
167
168 *(u32 *)din = (tmpdin << (32 - char_size));
169 if (char_size == 32) {
170 /* Advance output buffer by 32 bits */
171 din += 4;
Ben Warren7efe9272008-01-16 22:37:35 -0500172 }
Mario Six2afedfe2019-04-29 01:58:45 +0530173
Kim Phillipsb8e25202008-01-17 12:48:00 -0600174 /*
175 * Only bail when we've had both NE and NF events.
Ben Warren7efe9272008-01-16 22:37:35 -0500176 * This will cause timeouts on RO devices, so maybe
177 * in the future put an arbitrary delay after writing
Kim Phillipsb8e25202008-01-17 12:48:00 -0600178 * the device. Arbitrary delays suck, though...
179 */
Mario Six2afedfe2019-04-29 01:58:45 +0530180 if (have_nf)
Ben Warren7efe9272008-01-16 22:37:35 -0500181 break;
182 }
Mario Six2afedfe2019-04-29 01:58:45 +0530183
Ben Warren7efe9272008-01-16 22:37:35 -0500184 if (tm >= SPI_TIMEOUT)
Mario Sixf9d5ca22019-04-29 01:58:40 +0530185 debug("*** %s: Time out during SPI transfer\n",
186 __func__);
Ben Warren7efe9272008-01-16 22:37:35 -0500187
Mario Sixf9d5ca22019-04-29 01:58:40 +0530188 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
Ben Warren7efe9272008-01-16 22:37:35 -0500189 }
190
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200191 if (flags & SPI_XFER_END)
192 spi_cs_deactivate(slave);
Kim Phillipsb8e25202008-01-17 12:48:00 -0600193
Ben Warren7efe9272008-01-16 22:37:35 -0500194 return 0;
195}