]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/spi/kirkwood_spi.c
kw_spi: backup and reset the MPP of the chosen CS pin
[karo-tx-uboot.git] / drivers / spi / kirkwood_spi.c
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         u32 kwspi_mpp_config[] = { 0, 0 };
45
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);
67         writel(KWSPI_IRQMASK, &spireg->irq_mask);
68
69         /* program mpp registers to select  SPI_CSn */
70         if (cs) {
71                 kwspi_mpp_config[0] = MPP7_SPI_SCn;
72         } else {
73                 kwspi_mpp_config[0] = MPP0_SPI_SCn;
74         }
75         kirkwood_mpp_conf(kwspi_mpp_config, cs_spi_mpp_back);
76
77         return slave;
78 }
79
80 void spi_free_slave(struct spi_slave *slave)
81 {
82         kirkwood_mpp_conf(cs_spi_mpp_back, NULL);
83         free(slave);
84 }
85
86 int spi_claim_bus(struct spi_slave *slave)
87 {
88         return 0;
89 }
90
91 void 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  */
101 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
102 {
103         return (bus == 0 && (cs == 0 || cs == 1));
104 }
105 #endif
106
107 void spi_init(void)
108 {
109 }
110
111 void spi_cs_activate(struct spi_slave *slave)
112 {
113         writel(readl(&spireg->ctrl) | KWSPI_IRQUNMASK, &spireg->ctrl);
114 }
115
116 void spi_cs_deactivate(struct spi_slave *slave)
117 {
118         writel(readl(&spireg->ctrl) & KWSPI_IRQMASK, &spireg->ctrl);
119 }
120
121 int 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
127         debug("spi_xfer: slave %u:%u dout %p din %p bitlen %u\n",
128               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
163                                         ("spi_xfer: din %p..%08x read\n",
164                                         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 }