3 * Marvell Semiconductor <www.marvell.com>
4 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
6 * Derived from drivers/spi/mpc8xxx_spi.c
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
31 #include <asm/arch/kirkwood.h>
32 #include <asm/arch/spi.h>
33 #include <asm/arch/mpp.h>
35 static struct kwspi_registers *spireg = (struct kwspi_registers *)KW_SPI_BASE;
37 u32 cs_spi_mpp_back[2];
39 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
40 unsigned int max_hz, unsigned int mode)
42 struct spi_slave *slave;
44 u32 kwspi_mpp_config[] = { 0, 0 };
46 if (!spi_cs_is_valid(bus, cs))
49 slave = malloc(sizeof(struct spi_slave));
56 writel(~KWSPI_CSN_ACT | KWSPI_SMEMRDY, &spireg->ctrl);
58 /* calculate spi clock prescaller using max_hz */
59 data = ((CONFIG_SYS_TCLK / 2) / max_hz) & KWSPI_CLKPRESCL_MASK;
62 /* program spi clock prescaller using max_hz */
63 writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg);
64 debug("data = 0x%08x \n", data);
66 writel(KWSPI_SMEMRDIRQ, &spireg->irq_cause);
67 writel(KWSPI_IRQMASK, &spireg->irq_mask);
69 /* program mpp registers to select SPI_CSn */
71 kwspi_mpp_config[0] = MPP7_SPI_SCn;
73 kwspi_mpp_config[0] = MPP0_SPI_SCn;
75 kirkwood_mpp_conf(kwspi_mpp_config, cs_spi_mpp_back);
80 void spi_free_slave(struct spi_slave *slave)
82 kirkwood_mpp_conf(cs_spi_mpp_back, NULL);
86 int spi_claim_bus(struct spi_slave *slave)
91 void spi_release_bus(struct spi_slave *slave)
95 #ifndef CONFIG_SPI_CS_IS_VALID
97 * you can define this function board specific
98 * define above CONFIG in board specific config file and
99 * provide the function in board specific src file
101 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
103 return (bus == 0 && (cs == 0 || cs == 1));
111 void spi_cs_activate(struct spi_slave *slave)
113 writel(readl(&spireg->ctrl) | KWSPI_IRQUNMASK, &spireg->ctrl);
116 void spi_cs_deactivate(struct spi_slave *slave)
118 writel(readl(&spireg->ctrl) & KWSPI_IRQMASK, &spireg->ctrl);
121 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
122 void *din, unsigned long flags)
124 unsigned int tmpdout, tmpdin;
127 debug("spi_xfer: slave %u:%u dout %p din %p bitlen %u\n",
128 slave->bus, slave->cs, dout, din, bitlen);
130 if (flags & SPI_XFER_BEGIN)
131 spi_cs_activate(slave);
134 * handle data in 8-bit chunks
135 * TBD: 2byte xfer mode to be enabled
137 writel(((readl(&spireg->cfg) & ~KWSPI_XFERLEN_MASK) |
138 KWSPI_XFERLEN_1BYTE), &spireg->cfg);
141 debug("loopstart bitlen %d\n", bitlen);
144 /* Shift data so it's msb-justified */
146 tmpdout = *(u32 *) dout & 0x0ff;
148 writel(~KWSPI_SMEMRDIRQ, &spireg->irq_cause);
149 writel(tmpdout, &spireg->dout); /* Write the data out */
150 debug("*** spi_xfer: ... %08x written, bitlen %d\n",
154 * Wait for SPI transmit to get out
155 * or time out (1 second = 1000 ms)
156 * The NE event must be read and cleared first
158 for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
159 if (readl(&spireg->irq_cause) & KWSPI_SMEMRDIRQ) {
161 tmpdin = readl(&spireg->din);
163 ("spi_xfer: din %p..%08x read\n",
167 *((u8 *) din) = (u8) tmpdin;
177 if (tm >= KWSPI_TIMEOUT)
178 printf("*** spi_xfer: Time out during SPI transfer\n");
180 debug("loopend bitlen %d\n", bitlen);
183 if (flags & SPI_XFER_END)
184 spi_cs_deactivate(slave);