3 * eInfochips Ltd. <www.einfochips.com>
4 * Written-by: Ajay Bhargav <ajay.bhargav@einfochips.com>
7 * Marvell Semiconductor <www.marvell.com>
9 * Written-by: Lei Wen <leiwen@marvell.com>
11 * See file CREDITS for list of people who contributed to this
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
36 #include <asm/arch/spi.h>
39 #define to_armd_spi_slave(s) container_of(s, struct armd_spi_slave, slave)
41 struct armd_spi_slave {
42 struct spi_slave slave;
43 struct ssp_reg *spi_reg;
52 static int spi_armd_write(struct armd_spi_slave *pss)
54 int wait_timeout = SSP_FLUSH_NUM;
55 while (--wait_timeout && !(readl(&pss->spi_reg->sssr) & SSSR_TNF))
58 debug("%s: timeout error\n", __func__);
62 if (pss->tx != NULL) {
63 writel(*(u8 *)pss->tx, &pss->spi_reg->ssdr);
66 writel(0, &pss->spi_reg->ssdr);
71 static int spi_armd_read(struct armd_spi_slave *pss)
73 int wait_timeout = SSP_FLUSH_NUM;
74 while (--wait_timeout && !(readl(&pss->spi_reg->sssr) & SSSR_RNE))
77 debug("%s: timeout error\n", __func__);
81 if (pss->rx != NULL) {
82 *(u8 *)pss->rx = readl(&pss->spi_reg->ssdr);
85 readl(&pss->spi_reg->ssdr);
90 static int spi_armd_flush(struct armd_spi_slave *pss)
92 unsigned long limit = SSP_FLUSH_NUM;
95 while (readl(&pss->spi_reg->sssr) & SSSR_RNE)
96 readl(&pss->spi_reg->ssdr);
97 } while ((readl(&pss->spi_reg->sssr) & SSSR_BSY) && limit--);
99 writel(SSSR_ROR, &pss->spi_reg->sssr);
104 void spi_cs_activate(struct spi_slave *slave)
106 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
108 gpio_set_value(slave->cs, pss->gpio_cs_inverted);
111 void spi_cs_deactivate(struct spi_slave *slave)
113 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
115 gpio_set_value(slave->cs, !pss->gpio_cs_inverted);
118 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
119 unsigned int max_hz, unsigned int mode)
121 struct armd_spi_slave *pss;
123 pss = malloc(sizeof(*pss));
127 pss->slave.bus = bus;
129 pss->spi_reg = (struct ssp_reg *)SSP_REG_BASE(CONFIG_SYS_SSP_PORT);
131 pss->cr0 = SSCR0_MOTO | SSCR0_DATASIZE(DEFAULT_WORD_LEN) | SSCR0_SSE;
133 pss->cr1 = (SSCR1_RXTRESH(RX_THRESH_DEF) & SSCR1_RFT) |
134 (SSCR1_TXTRESH(TX_THRESH_DEF) & SSCR1_TFT);
135 pss->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
136 pss->cr1 |= (((mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
137 | (((mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
139 pss->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
140 pss->clear_sr = SSSR_ROR | SSSR_TINT;
142 pss->gpio_cs_inverted = mode & SPI_CS_HIGH;
143 gpio_set_value(cs, !pss->gpio_cs_inverted);
148 void spi_free_slave(struct spi_slave *slave)
150 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
155 int spi_claim_bus(struct spi_slave *slave)
157 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
159 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
160 if (spi_armd_flush(pss) == 0)
166 void spi_release_bus(struct spi_slave *slave)
170 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
171 void *din, unsigned long flags)
173 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
174 uint bytes = bitlen / 8;
181 /* we can only do 8 bit transfers */
183 flags |= SPI_XFER_END;
197 if (flags & SPI_XFER_BEGIN) {
198 spi_cs_activate(slave);
199 writel(pss->cr1 | pss->int_cr1, &pss->spi_reg->sscr1);
200 writel(TIMEOUT_DEF, &pss->spi_reg->ssto);
201 writel(pss->cr0, &pss->spi_reg->sscr0);
205 limit = SSP_FLUSH_NUM;
206 ret = spi_armd_write(pss);
210 while ((readl(&pss->spi_reg->sssr) & SSSR_BSY) && limit--)
213 ret = spi_armd_read(pss);
219 if (flags & SPI_XFER_END) {
221 writel(pss->clear_sr, &pss->spi_reg->sssr);
222 clrbits_le32(&pss->spi_reg->sscr1, pss->int_cr1);
223 writel(0, &pss->spi_reg->ssto);
224 spi_cs_deactivate(slave);