blob: 942a208c2cfcb6349a6af684945ce04dbace310f [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskar5710de42009-05-30 01:13:33 +05309 */
10
11#include <common.h>
12#include <malloc.h>
13#include <spi.h>
Lei Wena7efd712011-10-18 20:11:42 +053014#include <asm/io.h>
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053015#include <asm/arch/kirkwood.h>
16#include <asm/arch/spi.h>
17#include <asm/arch/mpp.h>
18
19static struct kwspi_registers *spireg = (struct kwspi_registers *)KW_SPI_BASE;
20
Valentin Longchampca880672012-06-01 01:31:01 +000021u32 cs_spi_mpp_back[2];
22
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053023struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
24 unsigned int max_hz, unsigned int mode)
25{
26 struct spi_slave *slave;
27 u32 data;
Albert ARIBAUD9d86f0c2012-11-26 11:27:36 +000028 static const u32 kwspi_mpp_config[2][2] = {
29 { MPP0_SPI_SCn, 0 }, /* if cs == 0 */
30 { MPP7_SPI_SCn, 0 } /* if cs != 0 */
31 };
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053032
33 if (!spi_cs_is_valid(bus, cs))
34 return NULL;
35
Simon Glassd3504fe2013-03-18 19:23:40 +000036 slave = spi_alloc_slave_base(bus, cs);
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053037 if (!slave)
38 return NULL;
39
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053040 writel(~KWSPI_CSN_ACT | KWSPI_SMEMRDY, &spireg->ctrl);
41
42 /* calculate spi clock prescaller using max_hz */
Valentin Longchamp8203b202012-08-15 05:31:49 +000043 data = ((CONFIG_SYS_TCLK / 2) / max_hz) + 0x10;
44 data = data < KWSPI_CLKPRESCL_MIN ? KWSPI_CLKPRESCL_MIN : data;
45 data = data > KWSPI_CLKPRESCL_MASK ? KWSPI_CLKPRESCL_MASK : data;
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053046
47 /* program spi clock prescaller using max_hz */
48 writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg);
49 debug("data = 0x%08x \n", data);
50
51 writel(KWSPI_SMEMRDIRQ, &spireg->irq_cause);
Ian Campbell3f843552012-01-12 06:10:22 +000052 writel(KWSPI_IRQMASK, &spireg->irq_mask);
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053053
54 /* program mpp registers to select SPI_CSn */
Albert ARIBAUD9d86f0c2012-11-26 11:27:36 +000055 kirkwood_mpp_conf(kwspi_mpp_config[cs ? 1 : 0], cs_spi_mpp_back);
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053056
57 return slave;
58}
59
60void spi_free_slave(struct spi_slave *slave)
61{
Valentin Longchampca880672012-06-01 01:31:01 +000062 kirkwood_mpp_conf(cs_spi_mpp_back, NULL);
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053063 free(slave);
64}
65
Valentin Longchampac486e32012-06-01 01:31:02 +000066#if defined(CONFIG_SYS_KW_SPI_MPP)
67u32 spi_mpp_backup[4];
68#endif
69
Valentin Longchamp24934fe2012-06-01 01:31:03 +000070__attribute__((weak)) int board_spi_claim_bus(struct spi_slave *slave)
71{
72 return 0;
73}
74
Prafulla Wadaskar5710de42009-05-30 01:13:33 +053075int spi_claim_bus(struct spi_slave *slave)
76{
Valentin Longchampac486e32012-06-01 01:31:02 +000077#if defined(CONFIG_SYS_KW_SPI_MPP)
78 u32 config;
79 u32 spi_mpp_config[4];
80
81 config = CONFIG_SYS_KW_SPI_MPP;
82
83 if (config & MOSI_MPP6)
84 spi_mpp_config[0] = MPP6_SPI_MOSI;
85 else
86 spi_mpp_config[0] = MPP1_SPI_MOSI;
87
88 if (config & SCK_MPP10)
89 spi_mpp_config[1] = MPP10_SPI_SCK;
90 else
91 spi_mpp_config[1] = MPP2_SPI_SCK;
92
93 if (config & MISO_MPP11)
94 spi_mpp_config[2] = MPP11_SPI_MISO;
95 else
96 spi_mpp_config[2] = MPP3_SPI_MISO;
97
98 spi_mpp_config[3] = 0;
99 spi_mpp_backup[3] = 0;
100
101 /* set new spi mpp and save current mpp config */
102 kirkwood_mpp_conf(spi_mpp_config, spi_mpp_backup);
103
104#endif
105
Valentin Longchamp24934fe2012-06-01 01:31:03 +0000106 return board_spi_claim_bus(slave);
107}
108
109__attribute__((weak)) void board_spi_release_bus(struct spi_slave *slave)
110{
Prafulla Wadaskar5710de42009-05-30 01:13:33 +0530111}
112
113void spi_release_bus(struct spi_slave *slave)
114{
Valentin Longchampac486e32012-06-01 01:31:02 +0000115#if defined(CONFIG_SYS_KW_SPI_MPP)
116 kirkwood_mpp_conf(spi_mpp_backup, NULL);
117#endif
Valentin Longchamp24934fe2012-06-01 01:31:03 +0000118
119 board_spi_release_bus(slave);
Prafulla Wadaskar5710de42009-05-30 01:13:33 +0530120}
121
122#ifndef CONFIG_SPI_CS_IS_VALID
123/*
124 * you can define this function board specific
125 * define above CONFIG in board specific config file and
126 * provide the function in board specific src file
127 */
128int spi_cs_is_valid(unsigned int bus, unsigned int cs)
129{
130 return (bus == 0 && (cs == 0 || cs == 1));
131}
132#endif
133
Michael Walleefa4e432011-10-18 20:12:00 +0530134void spi_init(void)
135{
136}
137
Prafulla Wadaskar5710de42009-05-30 01:13:33 +0530138void spi_cs_activate(struct spi_slave *slave)
139{
140 writel(readl(&spireg->ctrl) | KWSPI_IRQUNMASK, &spireg->ctrl);
141}
142
143void spi_cs_deactivate(struct spi_slave *slave)
144{
145 writel(readl(&spireg->ctrl) & KWSPI_IRQMASK, &spireg->ctrl);
146}
147
148int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
149 void *din, unsigned long flags)
150{
151 unsigned int tmpdout, tmpdin;
152 int tm, isread = 0;
153
Marek Vasut2e8f6412011-10-24 23:39:59 +0000154 debug("spi_xfer: slave %u:%u dout %p din %p bitlen %u\n",
Prafulla Wadaskar5710de42009-05-30 01:13:33 +0530155 slave->bus, slave->cs, dout, din, bitlen);
156
157 if (flags & SPI_XFER_BEGIN)
158 spi_cs_activate(slave);
159
160 /*
161 * handle data in 8-bit chunks
162 * TBD: 2byte xfer mode to be enabled
163 */
164 writel(((readl(&spireg->cfg) & ~KWSPI_XFERLEN_MASK) |
165 KWSPI_XFERLEN_1BYTE), &spireg->cfg);
166
167 while (bitlen > 4) {
168 debug("loopstart bitlen %d\n", bitlen);
169 tmpdout = 0;
170
171 /* Shift data so it's msb-justified */
172 if (dout)
173 tmpdout = *(u32 *) dout & 0x0ff;
174
175 writel(~KWSPI_SMEMRDIRQ, &spireg->irq_cause);
176 writel(tmpdout, &spireg->dout); /* Write the data out */
177 debug("*** spi_xfer: ... %08x written, bitlen %d\n",
178 tmpdout, bitlen);
179
180 /*
181 * Wait for SPI transmit to get out
182 * or time out (1 second = 1000 ms)
183 * The NE event must be read and cleared first
184 */
185 for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
186 if (readl(&spireg->irq_cause) & KWSPI_SMEMRDIRQ) {
187 isread = 1;
188 tmpdin = readl(&spireg->din);
189 debug
Marek Vasut2e8f6412011-10-24 23:39:59 +0000190 ("spi_xfer: din %p..%08x read\n",
Prafulla Wadaskar5710de42009-05-30 01:13:33 +0530191 din, tmpdin);
192
193 if (din) {
194 *((u8 *) din) = (u8) tmpdin;
195 din += 1;
196 }
197 if (dout)
198 dout += 1;
199 bitlen -= 8;
200 }
201 if (isread)
202 break;
203 }
204 if (tm >= KWSPI_TIMEOUT)
205 printf("*** spi_xfer: Time out during SPI transfer\n");
206
207 debug("loopend bitlen %d\n", bitlen);
208 }
209
210 if (flags & SPI_XFER_END)
211 spi_cs_deactivate(slave);
212
213 return 0;
214}