blob: 01e1d11972374bbb56b54f4f7e525e60def46e26 [file] [log] [blame]
Prafulla Wadaskar5710de42009-05-30 01:13:33 +05301/*
2 * (C) Copyright 2009
3 * Marvell Semiconductor <www.marvell.com>
4 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
5 *
6 * Derived from drivers/spi/mpc8xxx_spi.c
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
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.
15 *
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.
20 *
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,
24 * MA 02110-1301 USA
25 */
26
27#include <common.h>
28#include <malloc.h>
29#include <spi.h>
Lei Wena7efd712011-10-18 20:11:42 +053030#include <asm/io.h>
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053031#include <asm/arch/kirkwood.h>
32#include <asm/arch/spi.h>
33#include <asm/arch/mpp.h>
34
35static struct kwspi_registers *spireg = (struct kwspi_registers *)KW_SPI_BASE;
36
Valentin Longchampca880672012-06-01 01:31:01 +000037u32 cs_spi_mpp_back[2];
38
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053039struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
40 unsigned int max_hz, unsigned int mode)
41{
42 struct spi_slave *slave;
43 u32 data;
Valentin Longchampca880672012-06-01 01:31:01 +000044 u32 kwspi_mpp_config[] = { 0, 0 };
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053045
46 if (!spi_cs_is_valid(bus, cs))
47 return NULL;
48
49 slave = malloc(sizeof(struct spi_slave));
50 if (!slave)
51 return NULL;
52
53 slave->bus = bus;
54 slave->cs = cs;
55
56 writel(~KWSPI_CSN_ACT | KWSPI_SMEMRDY, &spireg->ctrl);
57
58 /* calculate spi clock prescaller using max_hz */
59 data = ((CONFIG_SYS_TCLK / 2) / max_hz) & KWSPI_CLKPRESCL_MASK;
60 data |= 0x10;
61
62 /* program spi clock prescaller using max_hz */
63 writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg);
64 debug("data = 0x%08x \n", data);
65
66 writel(KWSPI_SMEMRDIRQ, &spireg->irq_cause);
Ian Campbell3f843552012-01-12 06:10:22 +000067 writel(KWSPI_IRQMASK, &spireg->irq_mask);
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053068
69 /* program mpp registers to select SPI_CSn */
70 if (cs) {
Valentin Longchampca880672012-06-01 01:31:01 +000071 kwspi_mpp_config[0] = MPP7_SPI_SCn;
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053072 } else {
73 kwspi_mpp_config[0] = MPP0_SPI_SCn;
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053074 }
Valentin Longchampca880672012-06-01 01:31:01 +000075 kirkwood_mpp_conf(kwspi_mpp_config, cs_spi_mpp_back);
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053076
77 return slave;
78}
79
80void spi_free_slave(struct spi_slave *slave)
81{
Valentin Longchampca880672012-06-01 01:31:01 +000082 kirkwood_mpp_conf(cs_spi_mpp_back, NULL);
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053083 free(slave);
84}
85
86int spi_claim_bus(struct spi_slave *slave)
87{
88 return 0;
89}
90
91void spi_release_bus(struct spi_slave *slave)
92{
93}
94
95#ifndef CONFIG_SPI_CS_IS_VALID
96/*
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
100 */
101int spi_cs_is_valid(unsigned int bus, unsigned int cs)
102{
103 return (bus == 0 && (cs == 0 || cs == 1));
104}
105#endif
106
Michael Walleefa4e432011-10-18 20:12:00 +0530107void spi_init(void)
108{
109}
110
Prafulla Wadaskar5710de42009-05-30 01:13:33 +0530111void spi_cs_activate(struct spi_slave *slave)
112{
113 writel(readl(&spireg->ctrl) | KWSPI_IRQUNMASK, &spireg->ctrl);
114}
115
116void spi_cs_deactivate(struct spi_slave *slave)
117{
118 writel(readl(&spireg->ctrl) & KWSPI_IRQMASK, &spireg->ctrl);
119}
120
121int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
122 void *din, unsigned long flags)
123{
124 unsigned int tmpdout, tmpdin;
125 int tm, isread = 0;
126
Marek Vasut2e8f6412011-10-24 23:39:59 +0000127 debug("spi_xfer: slave %u:%u dout %p din %p bitlen %u\n",
Prafulla Wadaskar5710de42009-05-30 01:13:33 +0530128 slave->bus, slave->cs, dout, din, bitlen);
129
130 if (flags & SPI_XFER_BEGIN)
131 spi_cs_activate(slave);
132
133 /*
134 * handle data in 8-bit chunks
135 * TBD: 2byte xfer mode to be enabled
136 */
137 writel(((readl(&spireg->cfg) & ~KWSPI_XFERLEN_MASK) |
138 KWSPI_XFERLEN_1BYTE), &spireg->cfg);
139
140 while (bitlen > 4) {
141 debug("loopstart bitlen %d\n", bitlen);
142 tmpdout = 0;
143
144 /* Shift data so it's msb-justified */
145 if (dout)
146 tmpdout = *(u32 *) dout & 0x0ff;
147
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",
151 tmpdout, bitlen);
152
153 /*
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
157 */
158 for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
159 if (readl(&spireg->irq_cause) & KWSPI_SMEMRDIRQ) {
160 isread = 1;
161 tmpdin = readl(&spireg->din);
162 debug
Marek Vasut2e8f6412011-10-24 23:39:59 +0000163 ("spi_xfer: din %p..%08x read\n",
Prafulla Wadaskar5710de42009-05-30 01:13:33 +0530164 din, tmpdin);
165
166 if (din) {
167 *((u8 *) din) = (u8) tmpdin;
168 din += 1;
169 }
170 if (dout)
171 dout += 1;
172 bitlen -= 8;
173 }
174 if (isread)
175 break;
176 }
177 if (tm >= KWSPI_TIMEOUT)
178 printf("*** spi_xfer: Time out during SPI transfer\n");
179
180 debug("loopend bitlen %d\n", bitlen);
181 }
182
183 if (flags & SPI_XFER_END)
184 spi_cs_deactivate(slave);
185
186 return 0;
187}