xref: /openbmc/u-boot/drivers/spi/kirkwood_spi.c (revision 37544a6d)
1 /*
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>
30 #include <asm/io.h>
31 #include <asm/arch/kirkwood.h>
32 #include <asm/arch/spi.h>
33 #include <asm/arch/mpp.h>
34 
35 static struct kwspi_registers *spireg = (struct kwspi_registers *)KW_SPI_BASE;
36 
37 u32 cs_spi_mpp_back[2];
38 
39 struct 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;
44 	static const u32 kwspi_mpp_config[2][2] = {
45 		{ MPP0_SPI_SCn, 0 }, /* if cs == 0 */
46 		{ MPP7_SPI_SCn, 0 } /* if cs != 0 */
47 	};
48 
49 	if (!spi_cs_is_valid(bus, cs))
50 		return NULL;
51 
52 	slave = spi_alloc_slave_base(bus, cs);
53 	if (!slave)
54 		return NULL;
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) + 0x10;
60 	data = data < KWSPI_CLKPRESCL_MIN ? KWSPI_CLKPRESCL_MIN : data;
61 	data = data > KWSPI_CLKPRESCL_MASK ? KWSPI_CLKPRESCL_MASK : data;
62 
63 	/* program spi clock prescaller using max_hz */
64 	writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg);
65 	debug("data = 0x%08x \n", data);
66 
67 	writel(KWSPI_SMEMRDIRQ, &spireg->irq_cause);
68 	writel(KWSPI_IRQMASK, &spireg->irq_mask);
69 
70 	/* program mpp registers to select  SPI_CSn */
71 	kirkwood_mpp_conf(kwspi_mpp_config[cs ? 1 : 0], cs_spi_mpp_back);
72 
73 	return slave;
74 }
75 
76 void spi_free_slave(struct spi_slave *slave)
77 {
78 	kirkwood_mpp_conf(cs_spi_mpp_back, NULL);
79 	free(slave);
80 }
81 
82 #if defined(CONFIG_SYS_KW_SPI_MPP)
83 u32 spi_mpp_backup[4];
84 #endif
85 
86 __attribute__((weak)) int board_spi_claim_bus(struct spi_slave *slave)
87 {
88 	return 0;
89 }
90 
91 int spi_claim_bus(struct spi_slave *slave)
92 {
93 #if defined(CONFIG_SYS_KW_SPI_MPP)
94 	u32 config;
95 	u32 spi_mpp_config[4];
96 
97 	config = CONFIG_SYS_KW_SPI_MPP;
98 
99 	if (config & MOSI_MPP6)
100 		spi_mpp_config[0] = MPP6_SPI_MOSI;
101 	else
102 		spi_mpp_config[0] = MPP1_SPI_MOSI;
103 
104 	if (config & SCK_MPP10)
105 		spi_mpp_config[1] = MPP10_SPI_SCK;
106 	else
107 		spi_mpp_config[1] = MPP2_SPI_SCK;
108 
109 	if (config & MISO_MPP11)
110 		spi_mpp_config[2] = MPP11_SPI_MISO;
111 	else
112 		spi_mpp_config[2] = MPP3_SPI_MISO;
113 
114 	spi_mpp_config[3] = 0;
115 	spi_mpp_backup[3] = 0;
116 
117 	/* set new spi mpp and save current mpp config */
118 	kirkwood_mpp_conf(spi_mpp_config, spi_mpp_backup);
119 
120 #endif
121 
122 	return board_spi_claim_bus(slave);
123 }
124 
125 __attribute__((weak)) void board_spi_release_bus(struct spi_slave *slave)
126 {
127 }
128 
129 void spi_release_bus(struct spi_slave *slave)
130 {
131 #if defined(CONFIG_SYS_KW_SPI_MPP)
132 	kirkwood_mpp_conf(spi_mpp_backup, NULL);
133 #endif
134 
135 	board_spi_release_bus(slave);
136 }
137 
138 #ifndef CONFIG_SPI_CS_IS_VALID
139 /*
140  * you can define this function board specific
141  * define above CONFIG in board specific config file and
142  * provide the function in board specific src file
143  */
144 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
145 {
146 	return (bus == 0 && (cs == 0 || cs == 1));
147 }
148 #endif
149 
150 void spi_init(void)
151 {
152 }
153 
154 void spi_cs_activate(struct spi_slave *slave)
155 {
156 	writel(readl(&spireg->ctrl) | KWSPI_IRQUNMASK, &spireg->ctrl);
157 }
158 
159 void spi_cs_deactivate(struct spi_slave *slave)
160 {
161 	writel(readl(&spireg->ctrl) & KWSPI_IRQMASK, &spireg->ctrl);
162 }
163 
164 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
165 	     void *din, unsigned long flags)
166 {
167 	unsigned int tmpdout, tmpdin;
168 	int tm, isread = 0;
169 
170 	debug("spi_xfer: slave %u:%u dout %p din %p bitlen %u\n",
171 	      slave->bus, slave->cs, dout, din, bitlen);
172 
173 	if (flags & SPI_XFER_BEGIN)
174 		spi_cs_activate(slave);
175 
176 	/*
177 	 * handle data in 8-bit chunks
178 	 * TBD: 2byte xfer mode to be enabled
179 	 */
180 	writel(((readl(&spireg->cfg) & ~KWSPI_XFERLEN_MASK) |
181 		KWSPI_XFERLEN_1BYTE), &spireg->cfg);
182 
183 	while (bitlen > 4) {
184 		debug("loopstart bitlen %d\n", bitlen);
185 		tmpdout = 0;
186 
187 		/* Shift data so it's msb-justified */
188 		if (dout)
189 			tmpdout = *(u32 *) dout & 0x0ff;
190 
191 		writel(~KWSPI_SMEMRDIRQ, &spireg->irq_cause);
192 		writel(tmpdout, &spireg->dout);	/* Write the data out */
193 		debug("*** spi_xfer: ... %08x written, bitlen %d\n",
194 		      tmpdout, bitlen);
195 
196 		/*
197 		 * Wait for SPI transmit to get out
198 		 * or time out (1 second = 1000 ms)
199 		 * The NE event must be read and cleared first
200 		 */
201 		for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
202 			if (readl(&spireg->irq_cause) & KWSPI_SMEMRDIRQ) {
203 				isread = 1;
204 				tmpdin = readl(&spireg->din);
205 				debug
206 					("spi_xfer: din %p..%08x read\n",
207 					din, tmpdin);
208 
209 				if (din) {
210 					*((u8 *) din) = (u8) tmpdin;
211 					din += 1;
212 				}
213 				if (dout)
214 					dout += 1;
215 				bitlen -= 8;
216 			}
217 			if (isread)
218 				break;
219 		}
220 		if (tm >= KWSPI_TIMEOUT)
221 			printf("*** spi_xfer: Time out during SPI transfer\n");
222 
223 		debug("loopend bitlen %d\n", bitlen);
224 	}
225 
226 	if (flags & SPI_XFER_END)
227 		spi_cs_deactivate(slave);
228 
229 	return 0;
230 }
231