blob: db4bb0ae6151b23c95b56a43a7d0b1c7c6e74067 [file] [log] [blame]
Prafulla Wadaskar1fd6a9b2009-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 Wen298ae912011-10-18 20:11:42 +053030#include <asm/io.h>
Prafulla Wadaskar1fd6a9b2009-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 Longchamp1ad0acd2012-06-01 01:31:01 +000037u32 cs_spi_mpp_back[2];
38
Prafulla Wadaskar1fd6a9b2009-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 Longchamp1ad0acd2012-06-01 01:31:01 +000044 u32 kwspi_mpp_config[] = { 0, 0 };
Prafulla Wadaskar1fd6a9b2009-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 Campbell573b62b2012-01-12 06:10:22 +000067 writel(KWSPI_IRQMASK, &spireg->irq_mask);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +053068
69 /* program mpp registers to select SPI_CSn */
70 if (cs) {
Valentin Longchamp1ad0acd2012-06-01 01:31:01 +000071 kwspi_mpp_config[0] = MPP7_SPI_SCn;
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +053072 } else {
73 kwspi_mpp_config[0] = MPP0_SPI_SCn;
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +053074 }
Valentin Longchamp1ad0acd2012-06-01 01:31:01 +000075 kirkwood_mpp_conf(kwspi_mpp_config, cs_spi_mpp_back);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +053076
77 return slave;
78}
79
80void spi_free_slave(struct spi_slave *slave)
81{
Valentin Longchamp1ad0acd2012-06-01 01:31:01 +000082 kirkwood_mpp_conf(cs_spi_mpp_back, NULL);
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +053083 free(slave);
84}
85
Valentin Longchampdb22ce72012-06-01 01:31:02 +000086#if defined(CONFIG_SYS_KW_SPI_MPP)
87u32 spi_mpp_backup[4];
88#endif
89
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +053090int spi_claim_bus(struct spi_slave *slave)
91{
Valentin Longchampdb22ce72012-06-01 01:31:02 +000092#if defined(CONFIG_SYS_KW_SPI_MPP)
93 u32 config;
94 u32 spi_mpp_config[4];
95
96 config = CONFIG_SYS_KW_SPI_MPP;
97
98 if (config & MOSI_MPP6)
99 spi_mpp_config[0] = MPP6_SPI_MOSI;
100 else
101 spi_mpp_config[0] = MPP1_SPI_MOSI;
102
103 if (config & SCK_MPP10)
104 spi_mpp_config[1] = MPP10_SPI_SCK;
105 else
106 spi_mpp_config[1] = MPP2_SPI_SCK;
107
108 if (config & MISO_MPP11)
109 spi_mpp_config[2] = MPP11_SPI_MISO;
110 else
111 spi_mpp_config[2] = MPP3_SPI_MISO;
112
113 spi_mpp_config[3] = 0;
114 spi_mpp_backup[3] = 0;
115
116 /* set new spi mpp and save current mpp config */
117 kirkwood_mpp_conf(spi_mpp_config, spi_mpp_backup);
118
119#endif
120
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530121 return 0;
122}
123
124void spi_release_bus(struct spi_slave *slave)
125{
Valentin Longchampdb22ce72012-06-01 01:31:02 +0000126#if defined(CONFIG_SYS_KW_SPI_MPP)
127 kirkwood_mpp_conf(spi_mpp_backup, NULL);
128#endif
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530129}
130
131#ifndef CONFIG_SPI_CS_IS_VALID
132/*
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
136 */
137int spi_cs_is_valid(unsigned int bus, unsigned int cs)
138{
139 return (bus == 0 && (cs == 0 || cs == 1));
140}
141#endif
142
Michael Walle86ea45b2011-10-18 20:12:00 +0530143void spi_init(void)
144{
145}
146
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530147void spi_cs_activate(struct spi_slave *slave)
148{
149 writel(readl(&spireg->ctrl) | KWSPI_IRQUNMASK, &spireg->ctrl);
150}
151
152void spi_cs_deactivate(struct spi_slave *slave)
153{
154 writel(readl(&spireg->ctrl) & KWSPI_IRQMASK, &spireg->ctrl);
155}
156
157int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
158 void *din, unsigned long flags)
159{
160 unsigned int tmpdout, tmpdin;
161 int tm, isread = 0;
162
Marek Vasut922a3a92011-10-24 23:39:59 +0000163 debug("spi_xfer: slave %u:%u dout %p din %p bitlen %u\n",
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530164 slave->bus, slave->cs, dout, din, bitlen);
165
166 if (flags & SPI_XFER_BEGIN)
167 spi_cs_activate(slave);
168
169 /*
170 * handle data in 8-bit chunks
171 * TBD: 2byte xfer mode to be enabled
172 */
173 writel(((readl(&spireg->cfg) & ~KWSPI_XFERLEN_MASK) |
174 KWSPI_XFERLEN_1BYTE), &spireg->cfg);
175
176 while (bitlen > 4) {
177 debug("loopstart bitlen %d\n", bitlen);
178 tmpdout = 0;
179
180 /* Shift data so it's msb-justified */
181 if (dout)
182 tmpdout = *(u32 *) dout & 0x0ff;
183
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",
187 tmpdout, bitlen);
188
189 /*
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
193 */
194 for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
195 if (readl(&spireg->irq_cause) & KWSPI_SMEMRDIRQ) {
196 isread = 1;
197 tmpdin = readl(&spireg->din);
198 debug
Marek Vasut922a3a92011-10-24 23:39:59 +0000199 ("spi_xfer: din %p..%08x read\n",
Prafulla Wadaskar1fd6a9b2009-05-30 01:13:33 +0530200 din, tmpdin);
201
202 if (din) {
203 *((u8 *) din) = (u8) tmpdin;
204 din += 1;
205 }
206 if (dout)
207 dout += 1;
208 bitlen -= 8;
209 }
210 if (isread)
211 break;
212 }
213 if (tm >= KWSPI_TIMEOUT)
214 printf("*** spi_xfer: Time out during SPI transfer\n");
215
216 debug("loopend bitlen %d\n", bitlen);
217 }
218
219 if (flags & SPI_XFER_END)
220 spi_cs_deactivate(slave);
221
222 return 0;
223}