xref: /netbsd-src/sys/dev/marvell/gttwsi.c (revision 92b1a40bcb4311ef82accebc963331d1c4e241d0)
1*92b1a40bSthorpej /*	$NetBSD: gttwsi.c,v 1.12 2020/01/12 17:48:42 thorpej Exp $	*/
2a748aedcSkiyohara /*
3a748aedcSkiyohara  * Copyright (c) 2008 Eiji Kawauchi.
4a748aedcSkiyohara  * All rights reserved.
5a748aedcSkiyohara  *
6a748aedcSkiyohara  * Redistribution and use in source and binary forms, with or without
7a748aedcSkiyohara  * modification, are permitted provided that the following conditions
8a748aedcSkiyohara  * are met:
9a748aedcSkiyohara  * 1. Redistributions of source code must retain the above copyright
10a748aedcSkiyohara  *    notice, this list of conditions and the following disclaimer.
11a748aedcSkiyohara  * 2. Redistributions in binary form must reproduce the above copyright
12a748aedcSkiyohara  *    notice, this list of conditions and the following disclaimer in the
13a748aedcSkiyohara  *    documentation and/or other materials provided with the distribution.
14a748aedcSkiyohara  * 3. All advertising materials mentioning features or use of this software
15a748aedcSkiyohara  *    must display the following acknowledgement:
16a748aedcSkiyohara  *      This product includes software developed for the NetBSD Project by
17a748aedcSkiyohara  *      Eiji Kawauchi.
18a748aedcSkiyohara  * 4. The name of the author may not be used to endorse or promote products
19a748aedcSkiyohara  *    derived from this software without specific prior written permission
20a748aedcSkiyohara  *
21a748aedcSkiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22a748aedcSkiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23a748aedcSkiyohara  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24a748aedcSkiyohara  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25a748aedcSkiyohara  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26a748aedcSkiyohara  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27a748aedcSkiyohara  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28a748aedcSkiyohara  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29a748aedcSkiyohara  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30a748aedcSkiyohara  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31a748aedcSkiyohara  */
32a748aedcSkiyohara /*
33a748aedcSkiyohara  * Copyright (c) 2005 Brocade Communcations, inc.
34a748aedcSkiyohara  * All rights reserved.
35a748aedcSkiyohara  *
36a748aedcSkiyohara  * Written by Matt Thomas for Brocade Communcations, Inc.
37a748aedcSkiyohara  *
38a748aedcSkiyohara  * Redistribution and use in source and binary forms, with or without
39a748aedcSkiyohara  * modification, are permitted provided that the following conditions
40a748aedcSkiyohara  * are met:
41a748aedcSkiyohara  * 1. Redistributions of source code must retain the above copyright
42a748aedcSkiyohara  *    notice, this list of conditions and the following disclaimer.
43a748aedcSkiyohara  * 2. Redistributions in binary form must reproduce the above copyright
44a748aedcSkiyohara  *    notice, this list of conditions and the following disclaimer in the
45a748aedcSkiyohara  *    documentation and/or other materials provided with the distribution.
46a748aedcSkiyohara  * 3. The name of Brocade Communications, Inc. may not be used to endorse
47a748aedcSkiyohara  *    or promote products derived from this software without specific prior
48a748aedcSkiyohara  *    written permission.
49a748aedcSkiyohara  *
50a748aedcSkiyohara  * THIS SOFTWARE IS PROVIDED BY BROCADE COMMUNICATIONS, INC. ``AS IS'' AND
51a748aedcSkiyohara  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52a748aedcSkiyohara  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53a748aedcSkiyohara  * ARE DISCLAIMED.  IN NO EVENT SHALL EITHER BROCADE COMMUNICATIONS, INC. BE
54a748aedcSkiyohara  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55a748aedcSkiyohara  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56a748aedcSkiyohara  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57a748aedcSkiyohara  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58a748aedcSkiyohara  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59a748aedcSkiyohara  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
60a748aedcSkiyohara  * OF THE POSSIBILITY OF SUCH DAMAGE.
61a748aedcSkiyohara  */
62a748aedcSkiyohara //#define TWSI_DEBUG
63a748aedcSkiyohara 
64a748aedcSkiyohara /*
65a748aedcSkiyohara  * Marvell Two-Wire Serial Interface (aka I2C) master driver
66a748aedcSkiyohara  */
67a748aedcSkiyohara 
68a748aedcSkiyohara #include <sys/cdefs.h>
69*92b1a40bSthorpej __KERNEL_RCSID(0, "$NetBSD: gttwsi.c,v 1.12 2020/01/12 17:48:42 thorpej Exp $");
70a748aedcSkiyohara #include "locators.h"
71a748aedcSkiyohara 
72a748aedcSkiyohara #include <sys/param.h>
73a748aedcSkiyohara #include <sys/bus.h>
74a748aedcSkiyohara #include <sys/condvar.h>
75a748aedcSkiyohara #include <sys/device.h>
76a748aedcSkiyohara #include <sys/errno.h>
77a748aedcSkiyohara #include <sys/kernel.h>
78a748aedcSkiyohara #include <sys/mutex.h>
79a748aedcSkiyohara #include <sys/systm.h>
80a748aedcSkiyohara 
81a748aedcSkiyohara #include <machine/cpu.h>
82a748aedcSkiyohara #include <machine/param.h>
83a748aedcSkiyohara 
84a748aedcSkiyohara #include <dev/i2c/i2cvar.h>
8533c1c575Smatt #include <dev/i2c/gttwsireg.h>
8633c1c575Smatt #include <dev/i2c/gttwsivar.h>
87a748aedcSkiyohara 
88a748aedcSkiyohara #include <dev/marvell/marvellvar.h>
89a748aedcSkiyohara 
90*92b1a40bSthorpej static const bus_size_t marvell_twsi_regmap[GTTWSI_NREGS] = {
91*92b1a40bSthorpej 	[TWSI_SLAVEADDR]	= TWSI_MARVELL_SLAVEADDR,
92*92b1a40bSthorpej 	[TWSI_EXTEND_SLAVEADDR]	= TWSI_MARVELL_EXTEND_SLAVEADDR,
93*92b1a40bSthorpej 	[TWSI_DATA]		= TWSI_MARVELL_DATA,
94*92b1a40bSthorpej 	[TWSI_CONTROL]		= TWSI_MARVELL_CONTROL,
95*92b1a40bSthorpej 	[TWSI_STATUS]		= TWSI_MARVELL_STATUS,
96*92b1a40bSthorpej 	[TWSI_BAUDRATE]		= TWSI_MARVELL_BAUDRATE,
97*92b1a40bSthorpej 	[TWSI_SOFTRESET]	= TWSI_MARVELL_SOFTRESET,
98*92b1a40bSthorpej };
99*92b1a40bSthorpej 
100a748aedcSkiyohara static int	gttwsi_match(device_t, cfdata_t, void *);
101a748aedcSkiyohara static void	gttwsi_attach(device_t, device_t, void *);
102a748aedcSkiyohara 
103a748aedcSkiyohara CFATTACH_DECL_NEW(gttwsi_gt, sizeof(struct gttwsi_softc),
104a748aedcSkiyohara     gttwsi_match, gttwsi_attach, NULL, NULL);
105a748aedcSkiyohara CFATTACH_DECL_NEW(gttwsi_mbus, sizeof(struct gttwsi_softc),
106a748aedcSkiyohara     gttwsi_match, gttwsi_attach, NULL, NULL);
107a748aedcSkiyohara 
108a748aedcSkiyohara /* ARGSUSED */
109a748aedcSkiyohara static int
gttwsi_match(device_t parent,cfdata_t match,void * aux)110a748aedcSkiyohara gttwsi_match(device_t parent, cfdata_t match, void *aux)
111a748aedcSkiyohara {
112a748aedcSkiyohara 	struct marvell_attach_args *mva = aux;
113a748aedcSkiyohara 
114a748aedcSkiyohara 	if (strcmp(mva->mva_name, match->cf_name) != 0)
115a748aedcSkiyohara 		return 0;
1162c444bf7Skiyohara 	if (mva->mva_offset == MVA_OFFSET_DEFAULT ||
1172c444bf7Skiyohara 	    mva->mva_irq == MVA_IRQ_DEFAULT)
118a748aedcSkiyohara 		return 0;
119a748aedcSkiyohara 
120a748aedcSkiyohara 	mva->mva_size = GTTWSI_SIZE;
121a748aedcSkiyohara 	return 1;
122a748aedcSkiyohara }
123a748aedcSkiyohara 
124a748aedcSkiyohara /* ARGSUSED */
125a748aedcSkiyohara static void
gttwsi_attach(device_t parent,device_t self,void * args)126a748aedcSkiyohara gttwsi_attach(device_t parent, device_t self, void *args)
127a748aedcSkiyohara {
128a748aedcSkiyohara 	struct marvell_attach_args *mva = args;
12933c1c575Smatt 	bus_space_handle_t ioh;
130a748aedcSkiyohara 
131a748aedcSkiyohara 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
13233c1c575Smatt 	    mva->mva_size, &ioh)) {
13333c1c575Smatt 		aprint_error(": cannot map registers\n");
134a748aedcSkiyohara 		return;
135a748aedcSkiyohara 	}
136a748aedcSkiyohara 
137*92b1a40bSthorpej 	gttwsi_attach_subr(self, mva->mva_iot, ioh, marvell_twsi_regmap);
138a748aedcSkiyohara 
13933c1c575Smatt 	marvell_intr_establish(mva->mva_irq, IPL_BIO, gttwsi_intr,
14033c1c575Smatt 	    device_private(self));
141a748aedcSkiyohara 
14233c1c575Smatt 	gttwsi_config_children(self);
143a748aedcSkiyohara }
144