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