blob: f4523a39291decba35409cfb79ab2f513b978c43 [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
Valentin Longchampac486e32012-06-01 01:31:02 +000086#if defined(CONFIG_SYS_KW_SPI_MPP)
87u32 spi_mpp_backup[4];
88#endif
89
Valentin Longchamp24934fe2012-06-01 01:31:03 +000090__attribute__((weak)) int board_spi_claim_bus(struct spi_slave *slave)
91{
92 return 0;
93}
94
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053095int spi_claim_bus(struct spi_slave *slave)
96{
Valentin Longchampac486e32012-06-01 01:31:02 +000097#if defined(CONFIG_SYS_KW_SPI_MPP)
98 u32 config;
99 u32 spi_mpp_config[4];
100
101 config = CONFIG_SYS_KW_SPI_MPP;
102
103 if (config & MOSI_MPP6)
104 spi_mpp_config[0] = MPP6_SPI_MOSI;
105 else
106 spi_mpp_config[0] = MPP1_SPI_MOSI;
107
108 if (config & SCK_MPP10)
109 spi_mpp_config[1] = MPP10_SPI_SCK;
110 else
111 spi_mpp_config[1] = MPP2_SPI_SCK;
112
113 if (config & MISO_MPP11)
114 spi_mpp_config[2] = MPP11_SPI_MISO;
115 else
116 spi_mpp_config[2] = MPP3_SPI_MISO;
117
118 spi_mpp_config[3] = 0;
119 spi_mpp_backup[3] = 0;
120
121 /* set new spi mpp and save current mpp config */
122 kirkwood_mpp_conf(spi_mpp_config, spi_mpp_backup);
123
124#endif
125
Valentin Longchamp24934fe2012-06-01 01:31:03 +0000126 return board_spi_claim_bus(slave);
127}
128
129__attribute__((weak)) void board_spi_release_bus(struct spi_slave *slave)
130{
Prafulla Wadaskar5710de42009-05-30 01:13:33 +0530131}
132
133void spi_release_bus(struct spi_slave *slave)
134{
Valentin Longchampac486e32012-06-01 01:31:02 +0000135#if defined(CONFIG_SYS_KW_SPI_MPP)
136 kirkwood_mpp_conf(spi_mpp_backup, NULL);
137#endif
Valentin Longchamp24934fe2012-06-01 01:31:03 +0000138
139 board_spi_release_bus(slave);
Prafulla Wadaskar5710de42009-05-30 01:13:33 +0530140}
141
142#ifndef CONFIG_SPI_CS_IS_VALID
143/*
144 * you can define this function board specific
145 * define above CONFIG in board specific config file and
146 * provide the function in board specific src file
147 */
148int spi_cs_is_valid(unsigned int bus, unsigned int cs)
149{
150 return (bus == 0 && (cs == 0 || cs == 1));
151}
152#endif
153
Michael Walleefa4e432011-10-18 20:12:00 +0530154void spi_init(void)
155{
156}
157
Prafulla Wadaskar5710de42009-05-30 01:13:33 +0530158void spi_cs_activate(struct spi_slave *slave)
159{
160 writel(readl(&spireg->ctrl) | KWSPI_IRQUNMASK, &spireg->ctrl);
161}
162
163void spi_cs_deactivate(struct spi_slave *slave)
164{
165 writel(readl(&spireg->ctrl) & KWSPI_IRQMASK, &spireg->ctrl);
166}
167
168int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
169 void *din, unsigned long flags)
170{
171 unsigned int tmpdout, tmpdin;
172 int tm, isread = 0;
173
Marek Vasut2e8f6412011-10-24 23:39:59 +0000174 debug("spi_xfer: slave %u:%u dout %p din %p bitlen %u\n",
Prafulla Wadaskar5710de42009-05-30 01:13:33 +0530175 slave->bus, slave->cs, dout, din, bitlen);
176
177 if (flags & SPI_XFER_BEGIN)
178 spi_cs_activate(slave);
179
180 /*
181 * handle data in 8-bit chunks
182 * TBD: 2byte xfer mode to be enabled
183 */
184 writel(((readl(&spireg->cfg) & ~KWSPI_XFERLEN_MASK) |
185 KWSPI_XFERLEN_1BYTE), &spireg->cfg);
186
187 while (bitlen > 4) {
188 debug("loopstart bitlen %d\n", bitlen);
189 tmpdout = 0;
190
191 /* Shift data so it's msb-justified */
192 if (dout)
193 tmpdout = *(u32 *) dout & 0x0ff;
194
195 writel(~KWSPI_SMEMRDIRQ, &spireg->irq_cause);
196 writel(tmpdout, &spireg->dout); /* Write the data out */
197 debug("*** spi_xfer: ... %08x written, bitlen %d\n",
198 tmpdout, bitlen);
199
200 /*
201 * Wait for SPI transmit to get out
202 * or time out (1 second = 1000 ms)
203 * The NE event must be read and cleared first
204 */
205 for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
206 if (readl(&spireg->irq_cause) & KWSPI_SMEMRDIRQ) {
207 isread = 1;
208 tmpdin = readl(&spireg->din);
209 debug
Marek Vasut2e8f6412011-10-24 23:39:59 +0000210 ("spi_xfer: din %p..%08x read\n",
Prafulla Wadaskar5710de42009-05-30 01:13:33 +0530211 din, tmpdin);
212
213 if (din) {
214 *((u8 *) din) = (u8) tmpdin;
215 din += 1;
216 }
217 if (dout)
218 dout += 1;
219 bitlen -= 8;
220 }
221 if (isread)
222 break;
223 }
224 if (tm >= KWSPI_TIMEOUT)
225 printf("*** spi_xfer: Time out during SPI transfer\n");
226
227 debug("loopend bitlen %d\n", bitlen);
228 }
229
230 if (flags & SPI_XFER_END)
231 spi_cs_deactivate(slave);
232
233 return 0;
234}