xref: /netbsd-src/sys/arch/mips/cavium/dev/octeon_mpi.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: octeon_mpi.c,v 1.7 2021/08/07 16:18:59 thorpej Exp $	*/
2f693c922Shikaru 
3f693c922Shikaru /*
4f693c922Shikaru  * Copyright (c) 2007 Internet Initiative Japan, Inc.
5f693c922Shikaru  * All rights reserved.
6f693c922Shikaru  *
7f693c922Shikaru  * Redistribution and use in source and binary forms, with or without
8f693c922Shikaru  * modification, are permitted provided that the following conditions
9f693c922Shikaru  * are met:
10f693c922Shikaru  * 1. Redistributions of source code must retain the above copyright
11f693c922Shikaru  *    notice, this list of conditions and the following disclaimer.
12f693c922Shikaru  * 2. Redistributions in binary form must reproduce the above copyright
13f693c922Shikaru  *    notice, this list of conditions and the following disclaimer in the
14f693c922Shikaru  *    documentation and/or other materials provided with the distribution.
15f693c922Shikaru  *
16f693c922Shikaru  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17f693c922Shikaru  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18f693c922Shikaru  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f693c922Shikaru  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20f693c922Shikaru  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21f693c922Shikaru  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22f693c922Shikaru  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23f693c922Shikaru  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24f693c922Shikaru  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f693c922Shikaru  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f693c922Shikaru  * SUCH DAMAGE.
27f693c922Shikaru  */
28f693c922Shikaru 
29f693c922Shikaru 
30f693c922Shikaru #include <sys/cdefs.h>
31*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: octeon_mpi.c,v 1.7 2021/08/07 16:18:59 thorpej Exp $");
32f693c922Shikaru 
33f693c922Shikaru #include "opt_octeon.h"
34f693c922Shikaru 
35f693c922Shikaru #include <sys/param.h>
36f693c922Shikaru #include <sys/systm.h>
37f693c922Shikaru #include <sys/types.h>
38f693c922Shikaru #include <sys/device.h>
39f693c922Shikaru #include <sys/lock.h>
40f693c922Shikaru #include <sys/cdefs.h>
41f693c922Shikaru 
42f693c922Shikaru #include <mips/locore.h>
43f693c922Shikaru #include <sys/bus.h>
44f693c922Shikaru 
45f693c922Shikaru #include <mips/cavium/include/iobusvar.h>
46f693c922Shikaru #include <mips/cavium/dev/octeon_mpireg.h>
47f693c922Shikaru #include <mips/cavium/dev/octeon_mpivar.h>
48f693c922Shikaru #include <mips/cavium/dev/octeon_ciureg.h>
49f693c922Shikaru 
503f508e4dSsimonb struct octmpi_softc {
51f693c922Shikaru 	device_t		sc_dev;
52f693c922Shikaru 
53f693c922Shikaru 	bus_space_tag_t		sc_regt;
54f693c922Shikaru 	bus_space_handle_t	sc_regh;
55f693c922Shikaru 
56f693c922Shikaru 	void *sc_ih;				/* XXX Interrupt Handler */
57f693c922Shikaru 
58f693c922Shikaru 	/* board-specific chip-select hook ops */
59f693c922Shikaru 	void			(*sc_ops_cs_on)(void);
60f693c922Shikaru 	void			(*sc_ops_cs_off)(void);
613f508e4dSsimonb 	struct octmpi_controller ctrl;
62f693c922Shikaru 
63f693c922Shikaru };
64f693c922Shikaru 
653f508e4dSsimonb static int		octmpi_match(device_t, struct cfdata *, void *);
663f508e4dSsimonb static void		octmpi_attach(device_t, device_t, void *);
67f693c922Shikaru #if 0
683f508e4dSsimonb static int		octmpi_intr(void *);
69f693c922Shikaru #endif
703f508e4dSsimonb void			octmpi_read(void *, u_int, u_int, size_t, uint8_t *);
713f508e4dSsimonb void			octmpi_write(void *, u_int, u_int, size_t, uint8_t *);
723f508e4dSsimonb static void		octmpi_xfer(struct octmpi_softc *, size_t, size_t);
733f508e4dSsimonb static void		octmpi_wait(struct octmpi_softc *);
743f508e4dSsimonb static inline uint64_t	octmpi_reg_rd(struct octmpi_softc *, int);
753f508e4dSsimonb static inline void	octmpi_reg_wr(struct octmpi_softc *, int, uint64_t);
76f693c922Shikaru 
77f693c922Shikaru /* SPI service routines */
783f508e4dSsimonb int octmpi_configure(void *, void *, void *);
79f693c922Shikaru 
80f693c922Shikaru #define GETREG(sc, x)	\
81f693c922Shikaru 	bus_space_read_8(sc->sc_regt, sc->sc_regh, x)
82f693c922Shikaru #define PUTREG(sc, x, v)	\
83f693c922Shikaru 	bus_space_write_8(sc->sc_regt, sc->sc_regh, x, v)
84f693c922Shikaru 
853f508e4dSsimonb CFATTACH_DECL_NEW(octeon_mpi, sizeof(struct octmpi_softc),
863f508e4dSsimonb     octmpi_match, octmpi_attach, NULL, NULL);
87f693c922Shikaru 
88f693c922Shikaru 
89f693c922Shikaru static int
spi_print(void * aux,const char * pnp)90f693c922Shikaru spi_print(void *aux, const char *pnp)
91f693c922Shikaru {
92f693c922Shikaru 	aprint_normal(" spi");
93f693c922Shikaru 	return (UNCONF);
94f693c922Shikaru }
95f693c922Shikaru 
96f693c922Shikaru static int
octmpi_match(device_t parent,struct cfdata * cf,void * aux)973f508e4dSsimonb octmpi_match(device_t parent, struct cfdata *cf, void *aux)
98f693c922Shikaru {
99f693c922Shikaru 	struct iobus_attach_args *aa = aux;
100f693c922Shikaru 
101f693c922Shikaru 	if (strcmp(cf->cf_name, aa->aa_name) != 0)
102f693c922Shikaru 		return 0;
103f693c922Shikaru 	return 1;
104f693c922Shikaru }
105f693c922Shikaru 
106f693c922Shikaru static void
octmpi_attach(device_t parent,device_t self,void * aux)1073f508e4dSsimonb octmpi_attach(device_t parent, device_t self, void *aux)
108f693c922Shikaru {
1093f508e4dSsimonb 	struct octmpi_softc *sc = device_private(self);
110f693c922Shikaru 	struct iobus_attach_args *aa = aux;
1113f508e4dSsimonb 	struct octmpi_attach_args pa;
112f693c922Shikaru 	int status;
113f693c922Shikaru 
114f693c922Shikaru 	sc->sc_regt = aa->aa_bust;
115f693c922Shikaru 
116f693c922Shikaru 	/*
117f693c922Shikaru 	 * Map registers.
118f693c922Shikaru 	 */
119f693c922Shikaru 	status = bus_space_map(sc->sc_regt, MPI_BASE, MPI_SIZE, 0,
120f693c922Shikaru 	    &sc->sc_regh);
121f693c922Shikaru 	if (status != 0)
122f693c922Shikaru 		panic(": can't map register");
123f693c922Shikaru 
124f693c922Shikaru 	aprint_normal(": Octeon MPI/SPI Controller\n");
125f693c922Shikaru 
126f693c922Shikaru 	/*
127f693c922Shikaru 	 * Initialize MPI/SPI Controller
128f693c922Shikaru 	 */
129f693c922Shikaru 	sc->ctrl.sc_bust = sc->sc_regt;
130f693c922Shikaru 	sc->ctrl.sc_bush = sc->sc_regh;
131f693c922Shikaru 	sc->ctrl.sct_cookie = sc;
1323f508e4dSsimonb 	sc->ctrl.sct_configure = octmpi_configure;
1333f508e4dSsimonb 	sc->ctrl.sct_read = octmpi_read;
1343f508e4dSsimonb 	sc->ctrl.sct_write = octmpi_write;
1353f508e4dSsimonb 	pa.octmpi_ctrl = &(sc->ctrl);
136f693c922Shikaru 
137f693c922Shikaru 	/* Enable SPI mode */
138f693c922Shikaru #if 0
1393f508e4dSsimonb 	octmpi_reg_wr(sc, MPI_CFG_OFFSET,
140f693c922Shikaru 	    (0x7d << MPI_CFG_CLKDIV_SHIFT) | MPI_CFG_CSENA | MPI_CFG_ENABLE | MPI_CFG_INT_ENA);
141f693c922Shikaru 	/* Enable device interrupts */
14282a25ec3Ssimonb 	sc->sc_ih = octeon_intr_establish(CIU_INT_MPI, IPL_SERIAL, octmpi_intr, sc);
143f693c922Shikaru 	if (sc->sc_ih == NULL)
144f693c922Shikaru 		panic("l2sw: can't establish interrupt\n");
145f693c922Shikaru #else
1463f508e4dSsimonb 	octmpi_reg_wr(sc, MPI_CFG_OFFSET,
147f693c922Shikaru 	    (0x7d << MPI_CFG_CLKDIV_SHIFT) | MPI_CFG_CSENA | MPI_CFG_ENABLE);
148f693c922Shikaru #endif
1493f508e4dSsimonb 	octmpi_reg_wr(sc, MPI_TX_OFFSET, 0);
150f693c922Shikaru 
1512685996bSthorpej 	config_found(&sc->sc_dev, &pa, spi_print,
152*c7fb772bSthorpej 	    CFARGS(.iattr = "octmpi"));
153f693c922Shikaru }
154f693c922Shikaru 
155f693c922Shikaru #if 0
156f693c922Shikaru static int
1573f508e4dSsimonb octmpi_intr(void *arg)
158f693c922Shikaru {
1593f508e4dSsimonb 	struct octmpi_softc *sc = arg;
160f693c922Shikaru 
1613f508e4dSsimonb 	octmpi_recv(sc);
162f693c922Shikaru 
163f693c922Shikaru 	/* Clear interrupts? */
164f693c922Shikaru 
165f693c922Shikaru 	return 1;
166f693c922Shikaru }
167f693c922Shikaru #endif
168f693c922Shikaru 
169f693c922Shikaru void
octmpi_read(void * parent,u_int cmd,u_int addr,size_t len,uint8_t * data)1703f508e4dSsimonb octmpi_read(void *parent, u_int cmd, u_int addr, size_t len, uint8_t *data)
171f693c922Shikaru {
1723f508e4dSsimonb 	struct octmpi_softc *sc = (void *)parent;
173f693c922Shikaru 	int i;
174f693c922Shikaru 
1753f508e4dSsimonb 	octmpi_reg_wr(sc, MPI_DAT0_OFFSET, cmd);
1763f508e4dSsimonb 	octmpi_reg_wr(sc, MPI_DAT1_OFFSET, addr);
177f693c922Shikaru 
1783f508e4dSsimonb 	octmpi_xfer(sc, 2, 2 + len);
179f693c922Shikaru 
180f693c922Shikaru 	for (i = 0; i < (int)len; i++)
1813f508e4dSsimonb 		data[i] = octmpi_reg_rd(sc, MPI_DAT2_OFFSET + i * 0x8);
182f693c922Shikaru }
183f693c922Shikaru 
184f693c922Shikaru void
octmpi_write(void * parent,u_int cmd,u_int addr,size_t len,uint8_t * data)1853f508e4dSsimonb octmpi_write(void *parent, u_int cmd, u_int addr, size_t len, uint8_t *data)
186f693c922Shikaru {
1873f508e4dSsimonb 	struct octmpi_softc *sc = (void *)parent;
188f693c922Shikaru 	int i;
189f693c922Shikaru 
1903f508e4dSsimonb 	octmpi_reg_wr(sc, MPI_DAT0_OFFSET, cmd);
1913f508e4dSsimonb 	octmpi_reg_wr(sc, MPI_DAT1_OFFSET, addr);
192f693c922Shikaru 
193f693c922Shikaru 	for (i = 0; i < (int)len; i++)
1943f508e4dSsimonb 		octmpi_reg_wr(sc, MPI_DAT2_OFFSET + i * 0x8, data[i]);
195f693c922Shikaru 
1963f508e4dSsimonb 	octmpi_xfer(sc, 2 + len, 2 + len);
197f693c922Shikaru }
198f693c922Shikaru 
199f693c922Shikaru static void
octmpi_xfer(struct octmpi_softc * sc,size_t tx,size_t total)2003f508e4dSsimonb octmpi_xfer(struct octmpi_softc *sc, size_t tx, size_t total)
201f693c922Shikaru {
202f693c922Shikaru 	if (sc->sc_ops_cs_on != NULL)
203f693c922Shikaru 		(*sc->sc_ops_cs_on)();
204f693c922Shikaru 
2053f508e4dSsimonb 	octmpi_reg_wr(sc, MPI_TX_OFFSET,
206f693c922Shikaru 	    (tx << MPI_TX_TXNUM_SHIFT) | (total << MPI_TX_TOTNUM_SHIFT));
2073f508e4dSsimonb 	octmpi_wait(sc);
208f693c922Shikaru 
209f693c922Shikaru 	if (sc->sc_ops_cs_off != NULL)
210f693c922Shikaru 		(*sc->sc_ops_cs_off)();
211f693c922Shikaru }
212f693c922Shikaru 
213f693c922Shikaru static void
octmpi_wait(struct octmpi_softc * sc)2143f508e4dSsimonb octmpi_wait(struct octmpi_softc *sc)
215f693c922Shikaru {
216f693c922Shikaru 	uint64_t tmp;
217f693c922Shikaru 
218f693c922Shikaru 	/* XXX ltsleep & interrupt */
2193f508e4dSsimonb 	tmp = octmpi_reg_rd(sc, MPI_STS_OFFSET);
220f693c922Shikaru 	while (ISSET(tmp, MPI_STS_BUSY)) {
221f693c922Shikaru 		delay(10);
2223f508e4dSsimonb 		tmp = octmpi_reg_rd(sc, MPI_STS_OFFSET);
223f693c922Shikaru 	}
224f693c922Shikaru }
225f693c922Shikaru 
226f693c922Shikaru static inline uint64_t
octmpi_reg_rd(struct octmpi_softc * sc,int offset)2273f508e4dSsimonb octmpi_reg_rd(struct octmpi_softc *sc, int offset)
228f693c922Shikaru {
2293f508e4dSsimonb 
230f693c922Shikaru 	return GETREG(sc, offset);
231f693c922Shikaru }
232f693c922Shikaru 
233f693c922Shikaru static inline void
octmpi_reg_wr(struct octmpi_softc * sc,int offset,uint64_t datum)2343f508e4dSsimonb octmpi_reg_wr(struct octmpi_softc *sc, int offset, uint64_t datum)
235f693c922Shikaru {
2363f508e4dSsimonb 
237f693c922Shikaru 	PUTREG(sc, offset, datum);
238f693c922Shikaru }
239f693c922Shikaru 
240f693c922Shikaru int
octmpi_configure(void * arg,void * cs_on,void * cs_off)2413f508e4dSsimonb octmpi_configure(void *arg, void *cs_on, void *cs_off)
242f693c922Shikaru {
2433f508e4dSsimonb 	struct octmpi_softc *sc = arg;
244f693c922Shikaru 
245f693c922Shikaru 	sc->sc_ops_cs_on = cs_on;
246f693c922Shikaru 	sc->sc_ops_cs_off = cs_off;
247f693c922Shikaru 
248f693c922Shikaru 	return 0;
249f693c922Shikaru }
250