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 #if defined(CONFIG_SYS_KW_SPI_MPP)
87 u32 spi_mpp_backup[4];
90 int spi_claim_bus(struct spi_slave *slave)
92 #if defined(CONFIG_SYS_KW_SPI_MPP)
94 u32 spi_mpp_config[4];
96 config = CONFIG_SYS_KW_SPI_MPP;
98 if (config & MOSI_MPP6)
99 spi_mpp_config[0] = MPP6_SPI_MOSI;
101 spi_mpp_config[0] = MPP1_SPI_MOSI;
103 if (config & SCK_MPP10)
104 spi_mpp_config[1] = MPP10_SPI_SCK;
106 spi_mpp_config[1] = MPP2_SPI_SCK;
108 if (config & MISO_MPP11)
109 spi_mpp_config[2] = MPP11_SPI_MISO;
111 spi_mpp_config[2] = MPP3_SPI_MISO;
113 spi_mpp_config[3] = 0;
114 spi_mpp_backup[3] = 0;
116 /* set new spi mpp and save current mpp config */
117 kirkwood_mpp_conf(spi_mpp_config, spi_mpp_backup);
124 void spi_release_bus(struct spi_slave *slave)
126 #if defined(CONFIG_SYS_KW_SPI_MPP)
127 kirkwood_mpp_conf(spi_mpp_backup, NULL);
131 #ifndef CONFIG_SPI_CS_IS_VALID
133 * you can define this function board specific
134 * define above CONFIG in board specific config file and
135 * provide the function in board specific src file
137 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
139 return (bus == 0 && (cs == 0 || cs == 1));
147 void spi_cs_activate(struct spi_slave *slave)
149 writel(readl(&spireg->ctrl) | KWSPI_IRQUNMASK, &spireg->ctrl);
152 void spi_cs_deactivate(struct spi_slave *slave)
154 writel(readl(&spireg->ctrl) & KWSPI_IRQMASK, &spireg->ctrl);
157 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
158 void *din, unsigned long flags)
160 unsigned int tmpdout, tmpdin;
163 debug("spi_xfer: slave %u:%u dout %p din %p bitlen %u\n",
164 slave->bus, slave->cs, dout, din, bitlen);
166 if (flags & SPI_XFER_BEGIN)
167 spi_cs_activate(slave);
170 * handle data in 8-bit chunks
171 * TBD: 2byte xfer mode to be enabled
173 writel(((readl(&spireg->cfg) & ~KWSPI_XFERLEN_MASK) |
174 KWSPI_XFERLEN_1BYTE), &spireg->cfg);
177 debug("loopstart bitlen %d\n", bitlen);
180 /* Shift data so it's msb-justified */
182 tmpdout = *(u32 *) dout & 0x0ff;
184 writel(~KWSPI_SMEMRDIRQ, &spireg->irq_cause);
185 writel(tmpdout, &spireg->dout); /* Write the data out */
186 debug("*** spi_xfer: ... %08x written, bitlen %d\n",
190 * Wait for SPI transmit to get out
191 * or time out (1 second = 1000 ms)
192 * The NE event must be read and cleared first
194 for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
195 if (readl(&spireg->irq_cause) & KWSPI_SMEMRDIRQ) {
197 tmpdin = readl(&spireg->din);
199 ("spi_xfer: din %p..%08x read\n",
203 *((u8 *) din) = (u8) tmpdin;
213 if (tm >= KWSPI_TIMEOUT)
214 printf("*** spi_xfer: Time out during SPI transfer\n");
216 debug("loopend bitlen %d\n", bitlen);
219 if (flags & SPI_XFER_END)
220 spi_cs_deactivate(slave);