Merge branch 'next'
[u-boot/qq2440-u-boot.git] / drivers / spi / armada100_spi.c
blob53aaf95340f3fa4a9624da1616d2f0d6d60064e9
1 /*
2 * (C) Copyright 2011
3 * eInfochips Ltd. <www.einfochips.com>
4 * Written-by: Ajay Bhargav <ajay.bhargav@einfochips.com>
6 * (C) Copyright 2009
7 * Marvell Semiconductor <www.marvell.com>
8 * Based on SSP driver
9 * Written-by: Lei Wen <leiwen@marvell.com>
11 * SPDX-License-Identifier: GPL-2.0+
15 #include <common.h>
16 #include <malloc.h>
17 #include <spi.h>
19 #include <asm/io.h>
20 #include <asm/arch/spi.h>
21 #include <asm/gpio.h>
23 #define to_armd_spi_slave(s) container_of(s, struct armd_spi_slave, slave)
25 struct armd_spi_slave {
26 struct spi_slave slave;
27 struct ssp_reg *spi_reg;
28 u32 cr0, cr1;
29 u32 int_cr1;
30 u32 clear_sr;
31 const void *tx;
32 void *rx;
33 int gpio_cs_inverted;
36 static int spi_armd_write(struct armd_spi_slave *pss)
38 int wait_timeout = SSP_FLUSH_NUM;
39 while (--wait_timeout && !(readl(&pss->spi_reg->sssr) & SSSR_TNF))
41 if (!wait_timeout) {
42 debug("%s: timeout error\n", __func__);
43 return -1;
46 if (pss->tx != NULL) {
47 writel(*(u8 *)pss->tx, &pss->spi_reg->ssdr);
48 ++pss->tx;
49 } else {
50 writel(0, &pss->spi_reg->ssdr);
52 return 0;
55 static int spi_armd_read(struct armd_spi_slave *pss)
57 int wait_timeout = SSP_FLUSH_NUM;
58 while (--wait_timeout && !(readl(&pss->spi_reg->sssr) & SSSR_RNE))
60 if (!wait_timeout) {
61 debug("%s: timeout error\n", __func__);
62 return -1;
65 if (pss->rx != NULL) {
66 *(u8 *)pss->rx = readl(&pss->spi_reg->ssdr);
67 ++pss->rx;
68 } else {
69 readl(&pss->spi_reg->ssdr);
71 return 0;
74 static int spi_armd_flush(struct armd_spi_slave *pss)
76 unsigned long limit = SSP_FLUSH_NUM;
78 do {
79 while (readl(&pss->spi_reg->sssr) & SSSR_RNE)
80 readl(&pss->spi_reg->ssdr);
81 } while ((readl(&pss->spi_reg->sssr) & SSSR_BSY) && limit--);
83 writel(SSSR_ROR, &pss->spi_reg->sssr);
85 return limit;
88 void spi_cs_activate(struct spi_slave *slave)
90 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
92 gpio_set_value(slave->cs, pss->gpio_cs_inverted);
95 void spi_cs_deactivate(struct spi_slave *slave)
97 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
99 gpio_set_value(slave->cs, !pss->gpio_cs_inverted);
102 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
103 unsigned int max_hz, unsigned int mode)
105 struct armd_spi_slave *pss;
107 pss = spi_alloc_slave(struct armd_spi_slave, bus, cs);
108 if (!pss)
109 return NULL;
111 pss->spi_reg = (struct ssp_reg *)SSP_REG_BASE(CONFIG_SYS_SSP_PORT);
113 pss->cr0 = SSCR0_MOTO | SSCR0_DATASIZE(DEFAULT_WORD_LEN) | SSCR0_SSE;
115 pss->cr1 = (SSCR1_RXTRESH(RX_THRESH_DEF) & SSCR1_RFT) |
116 (SSCR1_TXTRESH(TX_THRESH_DEF) & SSCR1_TFT);
117 pss->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
118 pss->cr1 |= (((mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
119 | (((mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
121 pss->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
122 pss->clear_sr = SSSR_ROR | SSSR_TINT;
124 pss->gpio_cs_inverted = mode & SPI_CS_HIGH;
125 gpio_set_value(cs, !pss->gpio_cs_inverted);
127 return &pss->slave;
130 void spi_free_slave(struct spi_slave *slave)
132 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
134 free(pss);
137 int spi_claim_bus(struct spi_slave *slave)
139 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
141 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
142 if (spi_armd_flush(pss) == 0)
143 return -1;
145 return 0;
148 void spi_release_bus(struct spi_slave *slave)
152 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
153 void *din, unsigned long flags)
155 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
156 uint bytes = bitlen / 8;
157 unsigned long limit;
158 int ret = 0;
160 if (bitlen == 0)
161 goto done;
163 /* we can only do 8 bit transfers */
164 if (bitlen % 8) {
165 flags |= SPI_XFER_END;
166 goto done;
169 pss->tx = dout;
170 pss->rx = din;
172 if (flags & SPI_XFER_BEGIN) {
173 spi_cs_activate(slave);
174 writel(pss->cr1 | pss->int_cr1, &pss->spi_reg->sscr1);
175 writel(TIMEOUT_DEF, &pss->spi_reg->ssto);
176 writel(pss->cr0, &pss->spi_reg->sscr0);
179 while (bytes--) {
180 limit = SSP_FLUSH_NUM;
181 ret = spi_armd_write(pss);
182 if (ret)
183 break;
185 while ((readl(&pss->spi_reg->sssr) & SSSR_BSY) && limit--)
186 udelay(1);
188 ret = spi_armd_read(pss);
189 if (ret)
190 break;
193 done:
194 if (flags & SPI_XFER_END) {
195 /* Stop SSP */
196 writel(pss->clear_sr, &pss->spi_reg->sssr);
197 clrbits_le32(&pss->spi_reg->sscr1, pss->int_cr1);
198 writel(0, &pss->spi_reg->ssto);
199 spi_cs_deactivate(slave);
202 return ret;