blob: da9e1e3f98c840526b0b38637edec55e8bebe531 [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 Sixd896b7b2019-04-29 01:58:36 +053091 uint tmpdout, tmpdin, event;
Mario Six01ac1e12019-04-29 01:58:38 +053092 int num_blks = DIV_ROUND_UP(bitlen, 32);
93 int tm, is_read = 0;
94 uchar char_size = 32;
Ben Warren04a9e112008-01-16 22:37:35 -050095
Mario Sixfabe6c42019-04-29 01:58:40 +053096 debug("%s: slave %u:%u dout %08X din %08X bitlen %u\n", __func__,
Mario Six6f3ac072019-04-29 01:58:39 +053097 slave->bus, slave->cs, *(uint *)dout, *(uint *)din, bitlen);
Ben Warren04a9e112008-01-16 22:37:35 -050098
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020099 if (flags & SPI_XFER_BEGIN)
100 spi_cs_activate(slave);
Ben Warren04a9e112008-01-16 22:37:35 -0500101
Mario Sixd93fe312019-04-29 01:58:37 +0530102 /* Clear all SPI events */
Mario Six1a907e42019-04-29 01:58:42 +0530103 setbits_be32(&spi->event, 0xffffffff);
Ben Warren04a9e112008-01-16 22:37:35 -0500104
Mario Sixd93fe312019-04-29 01:58:37 +0530105 /* Handle data in 32-bit chunks */
Mario Six01ac1e12019-04-29 01:58:38 +0530106 while (num_blks--) {
Ben Warren04a9e112008-01-16 22:37:35 -0500107 tmpdout = 0;
Mario Six01ac1e12019-04-29 01:58:38 +0530108 char_size = (bitlen >= 32 ? 32 : bitlen);
Ben Warren04a9e112008-01-16 22:37:35 -0500109
110 /* Shift data so it's msb-justified */
Mario Six6f3ac072019-04-29 01:58:39 +0530111 tmpdout = *(u32 *)dout >> (32 - char_size);
Ben Warren04a9e112008-01-16 22:37:35 -0500112
113 /* The LEN field of the SPMODE register is set as follows:
114 *
Kim Phillips2956acd2008-01-17 12:48:00 -0600115 * Bit length setting
116 * len <= 4 3
117 * 4 < len <= 16 len - 1
118 * len > 16 0
Ben Warren04a9e112008-01-16 22:37:35 -0500119 */
120
Mario Six1a907e42019-04-29 01:58:42 +0530121 clrbits_be32(&spi->mode, SPI_MODE_EN);
Ira W. Snyderf138ca12012-09-12 14:17:31 -0700122
Kim Phillips2956acd2008-01-17 12:48:00 -0600123 if (bitlen <= 16) {
124 if (bitlen <= 4)
Mario Six1a907e42019-04-29 01:58:42 +0530125 clrsetbits_be32(&spi->mode, 0x00f00000,
126 (3 << 20));
Kim Phillips2956acd2008-01-17 12:48:00 -0600127 else
Mario Six1a907e42019-04-29 01:58:42 +0530128 clrsetbits_be32(&spi->mode, 0x00f00000,
129 ((bitlen - 1) << 20));
Kim Phillips2956acd2008-01-17 12:48:00 -0600130 } else {
Mario Six1a907e42019-04-29 01:58:42 +0530131 clrbits_be32(&spi->mode, 0x00f00000);
Ben Warren04a9e112008-01-16 22:37:35 -0500132 /* Set up the next iteration if sending > 32 bits */
133 bitlen -= 32;
134 dout += 4;
135 }
136
Mario Six1a907e42019-04-29 01:58:42 +0530137 setbits_be32(&spi->mode, SPI_MODE_EN);
Ira W. Snyderf138ca12012-09-12 14:17:31 -0700138
Mario Sixd93fe312019-04-29 01:58:37 +0530139 /* Write the data out */
Mario Six1a907e42019-04-29 01:58:42 +0530140 out_be32(&spi->tx, tmpdout);
Mario Sixd93fe312019-04-29 01:58:37 +0530141
Mario Sixfabe6c42019-04-29 01:58:40 +0530142 debug("*** %s: ... %08x written\n", __func__, tmpdout);
Ben Warren04a9e112008-01-16 22:37:35 -0500143
Kim Phillips2956acd2008-01-17 12:48:00 -0600144 /*
Ben Warren04a9e112008-01-16 22:37:35 -0500145 * Wait for SPI transmit to get out
146 * or time out (1 second = 1000 ms)
147 * The NE event must be read and cleared first
Kim Phillips2956acd2008-01-17 12:48:00 -0600148 */
Mario Six01ac1e12019-04-29 01:58:38 +0530149 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
Mario Six1a907e42019-04-29 01:58:42 +0530150 event = in_be32(&spi->event);
Ben Warren04a9e112008-01-16 22:37:35 -0500151 if (event & SPI_EV_NE) {
Mario Six1a907e42019-04-29 01:58:42 +0530152 tmpdin = in_be32(&spi->rx);
153 setbits_be32(&spi->event, SPI_EV_NE);
Mario Six01ac1e12019-04-29 01:58:38 +0530154 is_read = 1;
Ben Warren04a9e112008-01-16 22:37:35 -0500155
Mario Six6f3ac072019-04-29 01:58:39 +0530156 *(u32 *)din = (tmpdin << (32 - char_size));
Mario Six01ac1e12019-04-29 01:58:38 +0530157 if (char_size == 32) {
Ben Warren04a9e112008-01-16 22:37:35 -0500158 /* Advance output buffer by 32 bits */
159 din += 4;
160 }
161 }
Kim Phillips2956acd2008-01-17 12:48:00 -0600162 /*
163 * Only bail when we've had both NE and NF events.
Ben Warren04a9e112008-01-16 22:37:35 -0500164 * This will cause timeouts on RO devices, so maybe
165 * in the future put an arbitrary delay after writing
Kim Phillips2956acd2008-01-17 12:48:00 -0600166 * the device. Arbitrary delays suck, though...
167 */
Mario Six01ac1e12019-04-29 01:58:38 +0530168 if (is_read && (event & SPI_EV_NF))
Ben Warren04a9e112008-01-16 22:37:35 -0500169 break;
170 }
171 if (tm >= SPI_TIMEOUT)
Mario Sixfabe6c42019-04-29 01:58:40 +0530172 debug("*** %s: Time out during SPI transfer\n",
173 __func__);
Ben Warren04a9e112008-01-16 22:37:35 -0500174
Mario Sixfabe6c42019-04-29 01:58:40 +0530175 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
Ben Warren04a9e112008-01-16 22:37:35 -0500176 }
177
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200178 if (flags & SPI_XFER_END)
179 spi_cs_deactivate(slave);
Kim Phillips2956acd2008-01-17 12:48:00 -0600180
Ben Warren04a9e112008-01-16 22:37:35 -0500181 return 0;
182}