]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mediatek/mtk_spi_v1.c
Since contrib/libcxxrt's ancestry was never correct, subversion 1.8 and
[FreeBSD/FreeBSD.git] / sys / mips / mediatek / mtk_spi_v1.c
1 /*-
2  * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3  * Copyright (c) 2011, Aleksandr Rybalko <ray@FreeBSD.org>
4  * Copyright (c) 2013, Alexander A. Mityaev <sansan@adm.ua>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/rman.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42
43 #include <machine/bus.h>
44 #include <machine/cpu.h>
45 //#include <machine/pmap.h>
46
47 #include <dev/spibus/spi.h>
48 #include <dev/spibus/spibusvar.h>
49 #include "spibus_if.h"
50
51 #include "opt_platform.h"
52
53 #include <dev/ofw/openfirm.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_bus_subr.h>
56
57 #include <mips/mediatek/mtk_spi_v1.h>
58 #include <dev/flash/mx25lreg.h>
59
60 #undef MTK_SPI_DEBUG
61 #ifdef MTK_SPI_DEBUG
62 #define dprintf printf
63 #else
64 #define dprintf(x, arg...)
65 #endif
66
67 /*
68  * register space access macros
69  */
70 #define SPI_WRITE(sc, reg, val) do {    \
71                 bus_write_4(sc->sc_mem_res, (reg), (val)); \
72         } while (0)
73
74 #define SPI_READ(sc, reg)        bus_read_4(sc->sc_mem_res, (reg))
75
76 #define SPI_SET_BITS(sc, reg, bits)     \
77         SPI_WRITE(sc, reg, SPI_READ(sc, (reg)) | (bits))
78
79 #define SPI_CLEAR_BITS(sc, reg, bits)   \
80         SPI_WRITE(sc, reg, SPI_READ(sc, (reg)) & ~(bits))
81
82 struct mtk_spi_softc {
83         device_t                sc_dev;
84         struct resource         *sc_mem_res;
85 };
86
87 static int      mtk_spi_probe(device_t);
88 static int      mtk_spi_attach(device_t);
89 static int      mtk_spi_detach(device_t);
90 static int      mtk_spi_wait(struct mtk_spi_softc *);
91 static void     mtk_spi_chip_activate(struct mtk_spi_softc *);
92 static void     mtk_spi_chip_deactivate(struct mtk_spi_softc *);
93 static uint8_t  mtk_spi_txrx(struct mtk_spi_softc *, uint8_t *, int);
94 static int      mtk_spi_transfer(device_t, device_t, struct spi_command *);
95 static phandle_t mtk_spi_get_node(device_t, device_t);
96
97 static struct ofw_compat_data compat_data[] = {
98         { "ralink,rt2880-spi",  1 },
99         { "ralink,rt3050-spi",  1 },
100         { "ralink,rt3352-spi",  1 },
101         { "ralink,rt3883-spi",  1 },
102         { "ralink,rt5350-spi",  1 },
103         { "ralink,mt7620a-spi", 1 },
104         { NULL,                 0 }
105 };
106
107 static int
108 mtk_spi_probe(device_t dev)
109 {
110
111         if (!ofw_bus_status_okay(dev))
112                 return (ENXIO);
113
114         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
115                 return(ENXIO);
116
117         device_set_desc(dev, "MTK SPI Controller (v1)");
118
119         return (0);
120 }
121
122 static int
123 mtk_spi_attach(device_t dev)
124 {
125         struct mtk_spi_softc *sc = device_get_softc(dev);
126         int rid;
127
128         sc->sc_dev = dev;
129         rid = 0;
130         sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
131             RF_ACTIVE);
132         if (!sc->sc_mem_res) {
133                 device_printf(dev, "Could not map memory\n");
134                 return (ENXIO);
135         }
136
137         if (mtk_spi_wait(sc)) {
138                 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
139                 return (EBUSY);
140         }
141
142         SPI_WRITE(sc, MTK_SPICFG, MSBFIRST | SPICLKPOL | TX_ON_CLK_FALL |
143             SPI_CLK_DIV8); /* XXX: make it configurable */
144             /*
145              * W25Q64CV max 104MHz, bus 120-192 MHz, so divide by 2.
146              * Update: divide by 4, DEV2 to fast for flash.
147              */
148
149         device_add_child(dev, "spibus", 0);
150         return (bus_generic_attach(dev));
151 }
152
153 static int
154 mtk_spi_detach(device_t dev)
155 {
156         struct mtk_spi_softc *sc = device_get_softc(dev);
157
158         SPI_SET_BITS(sc, MTK_SPICTL, HIZSMOSI | CS_HIGH);
159
160         if (sc->sc_mem_res)
161                 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
162
163         return (0);
164 }
165
166 static void
167 mtk_spi_chip_activate(struct mtk_spi_softc *sc)
168 {
169 //        printf("%s\n", __func__);
170         mtk_spi_wait(sc);
171         /*
172          * Put all CSx to low
173          */
174         SPI_CLEAR_BITS(sc, MTK_SPICTL, CS_HIGH | HIZSMOSI);
175 }
176
177 static void
178 mtk_spi_chip_deactivate(struct mtk_spi_softc *sc)
179 {
180 //        printf("%s\n", __func__);
181         mtk_spi_wait(sc);
182         /*
183          * Put all CSx to high
184          */
185         SPI_SET_BITS(sc, MTK_SPICTL, CS_HIGH | HIZSMOSI);
186 }
187
188 static int
189 mtk_spi_wait(struct mtk_spi_softc *sc)
190 {
191         int i = 1000;
192
193         while (i--) {
194                 if (!SPI_READ(sc, MTK_SPIBUSY))
195                         break;
196         }
197         if (i == 0) {
198                 printf("busy\n");
199                 return (1);
200         }
201
202         return (0);
203 }
204
205 static uint8_t
206 mtk_spi_txrx(struct mtk_spi_softc *sc, uint8_t *data, int write)
207 {
208
209         if (mtk_spi_wait(sc))
210                 return (EBUSY);
211
212         if (write == MTK_SPI_WRITE) {
213                 SPI_WRITE(sc, MTK_SPIDATA, *data);
214                 SPI_SET_BITS(sc, MTK_SPICTL, START_WRITE);
215                 //printf("%s(W:%d)\n", __func__, *data);
216         } else {/* MTK_SPI_READ */
217                 SPI_SET_BITS(sc, MTK_SPICTL, START_READ);
218                 if (mtk_spi_wait(sc))
219                         return (EBUSY);
220
221                 *data = SPI_READ(sc, MTK_SPIDATA) & 0xff;
222                 //printf("%s(R:%d)\n", __func__, *data);
223         }
224         return (0);
225 }
226
227 static int
228 mtk_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
229 {
230         struct mtk_spi_softc *sc;
231         uint8_t *buf, byte, *tx_buf;
232         struct spibus_ivar *devi = SPIBUS_IVAR(child);
233         int i, sz, error = 0, write = 0;
234
235         sc = device_get_softc(dev);
236
237         if (devi->cs != 0)
238                 /* Only 1 CS */
239                 return (ENXIO);
240
241         /* There is always a command to transfer. */
242         tx_buf = (uint8_t *)(cmd->tx_cmd);
243         
244         /* Perform some fixup because MTK dont support duplex SPI */
245         switch(tx_buf[0]) {
246                 case CMD_READ_IDENT:
247                         cmd->tx_cmd_sz = 1;
248                         cmd->rx_cmd_sz = 3;
249                         break;
250                 case CMD_ENTER_4B_MODE:
251                 case CMD_EXIT_4B_MODE:
252                 case CMD_WRITE_ENABLE:
253                 case CMD_WRITE_DISABLE:
254                         cmd->tx_cmd_sz = 1;
255                         cmd->rx_cmd_sz = 0;
256                         break;
257                 case CMD_READ_STATUS:
258                         cmd->tx_cmd_sz = 1;
259                         cmd->rx_cmd_sz = 1;
260                         break;
261                 case CMD_READ:
262                 case CMD_FAST_READ:
263                         cmd->rx_cmd_sz = cmd->tx_data_sz = 0;
264                         break;
265                 case CMD_SECTOR_ERASE:
266                         cmd->rx_cmd_sz = 0;
267                         break;
268                 case CMD_PAGE_PROGRAM:
269                         cmd->rx_cmd_sz = cmd->rx_data_sz = 0;
270                         break;
271         }      
272         
273         mtk_spi_chip_activate(sc);
274
275         if (cmd->tx_cmd_sz + cmd->rx_cmd_sz) {
276                 buf = (uint8_t *)(cmd->rx_cmd);
277                 tx_buf = (uint8_t *)(cmd->tx_cmd);
278                 sz = cmd->tx_cmd_sz + cmd->rx_cmd_sz;
279
280                 for (i = 0; i < sz; i++) {
281                         if(i < cmd->tx_cmd_sz) {
282                                 byte = tx_buf[i];
283                                 error = mtk_spi_txrx(sc, &byte,
284                                     MTK_SPI_WRITE);
285                                 if (error)
286                                         goto mtk_spi_transfer_fail;
287                                 continue;
288                         }
289                         error = mtk_spi_txrx(sc, &byte,
290                             MTK_SPI_READ);
291                         if (error)
292                                 goto mtk_spi_transfer_fail;
293                         buf[i] = byte;
294                 }
295         }
296         
297         /*
298          * Transfer/Receive data
299          */
300         
301         if (cmd->tx_data_sz + cmd->rx_data_sz) {
302                 write = (cmd->tx_data_sz > 0)?1:0;
303                 buf = (uint8_t *)(write ? cmd->tx_data : cmd->rx_data);
304                 sz = write ? cmd->tx_data_sz : cmd->rx_data_sz;
305
306                 for (i = 0; i < sz; i++) {
307                         byte = buf[i];
308                         error = mtk_spi_txrx(sc, &byte,
309                             write ? MTK_SPI_WRITE : MTK_SPI_READ);
310                         if (error)
311                                 goto mtk_spi_transfer_fail;
312                         buf[i] = byte;
313                 }
314         }
315 mtk_spi_transfer_fail:
316         mtk_spi_chip_deactivate(sc);
317
318         return (error);
319 }
320
321 static phandle_t
322 mtk_spi_get_node(device_t bus, device_t dev)
323 {
324
325         /* We only have one child, the SPI bus, which needs our own node. */
326         return (ofw_bus_get_node(bus));
327 }
328
329 static device_method_t mtk_spi_methods[] = {
330         /* Device interface */
331         DEVMETHOD(device_probe,         mtk_spi_probe),
332         DEVMETHOD(device_attach,        mtk_spi_attach),
333         DEVMETHOD(device_detach,        mtk_spi_detach),
334
335         DEVMETHOD(spibus_transfer,      mtk_spi_transfer),
336
337         /* ofw_bus interface */
338         DEVMETHOD(ofw_bus_get_node,     mtk_spi_get_node),
339
340         DEVMETHOD_END
341 };
342
343 static driver_t mtk_spi_driver = {
344         .name = "spi",
345         .methods = mtk_spi_methods,
346         .size = sizeof(struct mtk_spi_softc),
347 };
348
349 static devclass_t mtk_spi_devclass;
350
351 DRIVER_MODULE(mtk_spi_v1, simplebus, mtk_spi_driver, mtk_spi_devclass, 0, 0);