blob: 2a0f3cc06a9b5007f3335c39ccef16941c3e6ad1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ben Warren04a9e112008-01-16 22:37:35 -05002/*
3 * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc.
Stefan Roesea47a12b2010-04-15 16:07:28 +02004 * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers
Ben Warren04a9e112008-01-16 22:37:35 -05005 */
6
7#include <common.h>
Ben Warren8931ab12008-01-26 23:41:19 -05008
Haavard Skinnemoend255bb02008-05-16 11:10:31 +02009#include <malloc.h>
Ben Warren04a9e112008-01-16 22:37:35 -050010#include <spi.h>
11#include <asm/mpc8xxx_spi.h>
12
Mario Six6ea93952019-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};
Ben Warren04a9e112008-01-16 22:37:35 -050017
Mario Six6ea93952019-04-29 01:58:41 +053018enum {
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,
29
30 SPI_COM_LST = BIT(31 - 9),
31};
Ben Warren04a9e112008-01-16 22:37:35 -050032
33#define SPI_TIMEOUT 1000
34
Mario Sixd896b7b2019-04-29 01:58:36 +053035struct spi_slave *spi_setup_slave(uint bus, uint cs, uint max_hz, uint mode)
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020036{
37 struct spi_slave *slave;
38
39 if (!spi_cs_is_valid(bus, cs))
40 return NULL;
41
Simon Glassd3504fe2013-03-18 19:23:40 +000042 slave = spi_alloc_slave_base(bus, cs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020043 if (!slave)
44 return NULL;
45
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020046 /*
47 * TODO: Some of the code in spi_init() should probably move
48 * here, or into spi_claim_bus() below.
49 */
50
51 return slave;
52}
53
54void spi_free_slave(struct spi_slave *slave)
55{
56 free(slave);
57}
58
Ben Warren04a9e112008-01-16 22:37:35 -050059void spi_init(void)
60{
Mario Six1a907e42019-04-29 01:58:42 +053061 spi8xxx_t *spi = &((immap_t *)(CONFIG_SYS_IMMR))->spi;
Ben Warren04a9e112008-01-16 22:37:35 -050062
Kim Phillips2956acd2008-01-17 12:48:00 -060063 /*
Ben Warren04a9e112008-01-16 22:37:35 -050064 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
65 * some registers
Kim Phillips2956acd2008-01-17 12:48:00 -060066 */
Mario Six1a907e42019-04-29 01:58:42 +053067 out_be32(&spi->mode, SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN);
Mario Sixd93fe312019-04-29 01:58:37 +053068 /* Use SYSCLK / 8 (16.67MHz typ.) */
Mario Six1a907e42019-04-29 01:58:42 +053069 clrsetbits_be32(&spi->mode, 0x000f0000, BIT(16));
Mario Sixd93fe312019-04-29 01:58:37 +053070 /* Clear all SPI events */
Mario Six1a907e42019-04-29 01:58:42 +053071 setbits_be32(&spi->event, 0xffffffff);
Mario Sixd93fe312019-04-29 01:58:37 +053072 /* Mask all SPI interrupts */
Mario Six1a907e42019-04-29 01:58:42 +053073 clrbits_be32(&spi->mask, 0xffffffff);
Mario Sixd93fe312019-04-29 01:58:37 +053074 /* LST bit doesn't do anything, so disregard */
Mario Six1a907e42019-04-29 01:58:42 +053075 out_be32(&spi->com, 0);
Ben Warren04a9e112008-01-16 22:37:35 -050076}
77
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020078int spi_claim_bus(struct spi_slave *slave)
79{
80 return 0;
81}
82
83void spi_release_bus(struct spi_slave *slave)
84{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020085}
86
Mario Sixd896b7b2019-04-29 01:58:36 +053087int spi_xfer(struct spi_slave *slave, uint bitlen, const void *dout, void *din,
88 ulong flags)
Ben Warren04a9e112008-01-16 22:37:35 -050089{
Mario Six1a907e42019-04-29 01:58:42 +053090 spi8xxx_t *spi = &((immap_t *)(CONFIG_SYS_IMMR))->spi;
Mario Six65f88e02019-04-29 01:58:46 +053091 u32 tmpdin;
Mario Six01ac1e12019-04-29 01:58:38 +053092 int num_blks = DIV_ROUND_UP(bitlen, 32);
Ben Warren04a9e112008-01-16 22:37:35 -050093
Mario Sixfabe6c42019-04-29 01:58:40 +053094 debug("%s: slave %u:%u dout %08X din %08X bitlen %u\n", __func__,
Mario Six6f3ac072019-04-29 01:58:39 +053095 slave->bus, slave->cs, *(uint *)dout, *(uint *)din, bitlen);
Ben Warren04a9e112008-01-16 22:37:35 -050096
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020097 if (flags & SPI_XFER_BEGIN)
98 spi_cs_activate(slave);
Ben Warren04a9e112008-01-16 22:37:35 -050099
Mario Sixd93fe312019-04-29 01:58:37 +0530100 /* Clear all SPI events */
Mario Six1a907e42019-04-29 01:58:42 +0530101 setbits_be32(&spi->event, 0xffffffff);
Ben Warren04a9e112008-01-16 22:37:35 -0500102
Mario Sixd93fe312019-04-29 01:58:37 +0530103 /* Handle data in 32-bit chunks */
Mario Six01ac1e12019-04-29 01:58:38 +0530104 while (num_blks--) {
Mario Six65f88e02019-04-29 01:58:46 +0530105 int tm;
106 u32 tmpdout = 0;
107 uchar char_size = (bitlen >= 32 ? 32 : bitlen);
Ben Warren04a9e112008-01-16 22:37:35 -0500108
109 /* Shift data so it's msb-justified */
Mario Six6f3ac072019-04-29 01:58:39 +0530110 tmpdout = *(u32 *)dout >> (32 - char_size);
Ben Warren04a9e112008-01-16 22:37:35 -0500111
112 /* The LEN field of the SPMODE register is set as follows:
113 *
Kim Phillips2956acd2008-01-17 12:48:00 -0600114 * Bit length setting
115 * len <= 4 3
116 * 4 < len <= 16 len - 1
117 * len > 16 0
Ben Warren04a9e112008-01-16 22:37:35 -0500118 */
119
Mario Six1a907e42019-04-29 01:58:42 +0530120 clrbits_be32(&spi->mode, SPI_MODE_EN);
Ira W. Snyderf138ca12012-09-12 14:17:31 -0700121
Mario Six76c82af2019-04-29 01:58:43 +0530122 if (bitlen <= 4) {
123 clrsetbits_be32(&spi->mode, 0x00f00000, (3 << 20));
124 } else if (bitlen <= 16) {
125 clrsetbits_be32(&spi->mode, 0x00f00000,
126 ((bitlen - 1) << 20));
Kim Phillips2956acd2008-01-17 12:48:00 -0600127 } else {
Mario Six1a907e42019-04-29 01:58:42 +0530128 clrbits_be32(&spi->mode, 0x00f00000);
Ben Warren04a9e112008-01-16 22:37:35 -0500129 /* Set up the next iteration if sending > 32 bits */
130 bitlen -= 32;
131 dout += 4;
132 }
133
Mario Six1a907e42019-04-29 01:58:42 +0530134 setbits_be32(&spi->mode, SPI_MODE_EN);
Ira W. Snyderf138ca12012-09-12 14:17:31 -0700135
Mario Sixd93fe312019-04-29 01:58:37 +0530136 /* Write the data out */
Mario Six1a907e42019-04-29 01:58:42 +0530137 out_be32(&spi->tx, tmpdout);
Mario Sixd93fe312019-04-29 01:58:37 +0530138
Mario Sixfabe6c42019-04-29 01:58:40 +0530139 debug("*** %s: ... %08x written\n", __func__, tmpdout);
Ben Warren04a9e112008-01-16 22:37:35 -0500140
Kim Phillips2956acd2008-01-17 12:48:00 -0600141 /*
Ben Warren04a9e112008-01-16 22:37:35 -0500142 * Wait for SPI transmit to get out
143 * or time out (1 second = 1000 ms)
144 * The NE event must be read and cleared first
Kim Phillips2956acd2008-01-17 12:48:00 -0600145 */
Mario Six6409c612019-04-29 01:58:44 +0530146 for (tm = 0; tm < SPI_TIMEOUT; ++tm) {
Mario Six65f88e02019-04-29 01:58:46 +0530147 u32 event = in_be32(&spi->event);
Mario Six6409c612019-04-29 01:58:44 +0530148 bool have_ne = event & SPI_EV_NE;
149 bool have_nf = event & SPI_EV_NF;
150
Mario Sixe4da4c22019-04-29 01:58:45 +0530151 if (!have_ne)
152 continue;
Ben Warren04a9e112008-01-16 22:37:35 -0500153
Mario Sixe4da4c22019-04-29 01:58:45 +0530154 tmpdin = in_be32(&spi->rx);
155 setbits_be32(&spi->event, SPI_EV_NE);
156
157 *(u32 *)din = (tmpdin << (32 - char_size));
158 if (char_size == 32) {
159 /* Advance output buffer by 32 bits */
160 din += 4;
Ben Warren04a9e112008-01-16 22:37:35 -0500161 }
Mario Sixe4da4c22019-04-29 01:58:45 +0530162
Kim Phillips2956acd2008-01-17 12:48:00 -0600163 /*
164 * Only bail when we've had both NE and NF events.
Ben Warren04a9e112008-01-16 22:37:35 -0500165 * This will cause timeouts on RO devices, so maybe
166 * in the future put an arbitrary delay after writing
Kim Phillips2956acd2008-01-17 12:48:00 -0600167 * the device. Arbitrary delays suck, though...
168 */
Mario Sixe4da4c22019-04-29 01:58:45 +0530169 if (have_nf)
Ben Warren04a9e112008-01-16 22:37:35 -0500170 break;
171 }
Mario Sixe4da4c22019-04-29 01:58:45 +0530172
Ben Warren04a9e112008-01-16 22:37:35 -0500173 if (tm >= SPI_TIMEOUT)
Mario Sixfabe6c42019-04-29 01:58:40 +0530174 debug("*** %s: Time out during SPI transfer\n",
175 __func__);
Ben Warren04a9e112008-01-16 22:37:35 -0500176
Mario Sixfabe6c42019-04-29 01:58:40 +0530177 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
Ben Warren04a9e112008-01-16 22:37:35 -0500178 }
179
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200180 if (flags & SPI_XFER_END)
181 spi_cs_deactivate(slave);
Kim Phillips2956acd2008-01-17 12:48:00 -0600182
Ben Warren04a9e112008-01-16 22:37:35 -0500183 return 0;
184}