aboutsummaryrefslogtreecommitdiff
path: root/sys/arm/xscale/ixp425/ixp425_iic.c
blob: 080ebf651d585de9291d3e89c2f9e2b2a81a054b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Copyright (c) 2006 Kevin Lo. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/uio.h>

#include <arm/xscale/ixp425/ixp425reg.h>
#include <arm/xscale/ixp425/ixp425var.h>
#include <arm/xscale/ixp425/ixdp425reg.h>

#include <dev/iicbus/iiconf.h>
#include <dev/iicbus/iicbus.h>

#include "iicbb_if.h"

#define I2C_DELAY	10

/* bit clr/set shorthands */
#define	GPIO_CONF_CLR(sc, reg, mask)	\
	GPIO_CONF_WRITE_4(sc, reg, GPIO_CONF_READ_4(sc, reg) &~ (mask))
#define	GPIO_CONF_SET(sc, reg, mask)	\
	GPIO_CONF_WRITE_4(sc, reg, GPIO_CONF_READ_4(sc, reg) | (mask))

struct ixpiic_softc {
	device_t		sc_dev;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_gpio_ioh;

	device_t		iicbb;
};

static struct ixpiic_softc *ixpiic_sc = NULL;

static int
ixpiic_probe(device_t dev)
{
	device_set_desc(dev, "IXP4XX GPIO-Based I2C Interface");
	return (0);
}

static int
ixpiic_attach(device_t dev)
{
	struct ixpiic_softc *sc = device_get_softc(dev);
	struct ixp425_softc *sa = device_get_softc(device_get_parent(dev));

	ixpiic_sc = sc;

	sc->sc_dev = dev;
	sc->sc_iot = sa->sc_iot;
	sc->sc_gpio_ioh = sa->sc_gpio_ioh;

	GPIO_CONF_SET(sc, IXP425_GPIO_GPOER,
		GPIO_I2C_SCL_BIT | GPIO_I2C_SDA_BIT);
	GPIO_CONF_CLR(sc, IXP425_GPIO_GPOUTR,
		GPIO_I2C_SCL_BIT | GPIO_I2C_SDA_BIT);

	/* add generic bit-banging code */	
	if ((sc->iicbb = device_add_child(dev, "iicbb", -1)) == NULL)
		device_printf(dev, "could not add iicbb\n");

	/* probe and attach the bit-banging code */
	device_probe_and_attach(sc->iicbb);

	return (0);
}

static int
ixpiic_callback(device_t dev, int index, caddr_t *data)
{
	return (0);
}

static int 
ixpiic_getscl(device_t dev)
{
	struct ixpiic_softc *sc = ixpiic_sc;
	uint32_t reg;

	mtx_lock(&Giant);
	GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SCL_BIT);

	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR);
	mtx_unlock(&Giant);
	return (reg & GPIO_I2C_SCL_BIT);
}

static int 
ixpiic_getsda(device_t dev)
{
	struct ixpiic_softc *sc = ixpiic_sc;
	uint32_t reg;

	mtx_lock(&Giant);
	GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);

	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR);
	mtx_unlock(&Giant);
	return (reg & GPIO_I2C_SDA_BIT);
}

static void 
ixpiic_setsda(device_t dev, char val)
{
	struct ixpiic_softc *sc = ixpiic_sc;

	mtx_lock(&Giant);
	GPIO_CONF_CLR(sc, IXP425_GPIO_GPOUTR, GPIO_I2C_SDA_BIT);
	if (val)
		GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
	else
		GPIO_CONF_CLR(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
	mtx_unlock(&Giant);
	DELAY(I2C_DELAY);
}

static void 
ixpiic_setscl(device_t dev, char val)
{
	struct ixpiic_softc *sc = ixpiic_sc;

	mtx_lock(&Giant);
	GPIO_CONF_CLR(sc, IXP425_GPIO_GPOUTR, GPIO_I2C_SCL_BIT);
	if (val)
		GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SCL_BIT);
	else
		GPIO_CONF_CLR(sc, IXP425_GPIO_GPOER, GPIO_I2C_SCL_BIT);
	mtx_unlock(&Giant);
	DELAY(I2C_DELAY);
}

static int
ixpiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
{
	/* reset bus */
	ixpiic_setsda(dev, 1);
	ixpiic_setscl(dev, 1);

	return (IIC_ENOADDR);
}

static device_method_t ixpiic_methods[] = {
	/* device interface */
	DEVMETHOD(device_probe,		ixpiic_probe),
	DEVMETHOD(device_attach,	ixpiic_attach),

	/* iicbb interface */
	DEVMETHOD(iicbb_callback,	ixpiic_callback),
	DEVMETHOD(iicbb_setsda,		ixpiic_setsda),
	DEVMETHOD(iicbb_setscl,		ixpiic_setscl),
	DEVMETHOD(iicbb_getsda,		ixpiic_getsda),
	DEVMETHOD(iicbb_getscl,		ixpiic_getscl),
	DEVMETHOD(iicbb_reset,		ixpiic_reset),

	{ 0, 0 }
};

static driver_t ixpiic_driver = {
	"ixpiic",
	ixpiic_methods,
	sizeof(struct ixpiic_softc),
};
static devclass_t ixpiic_devclass;

DRIVER_MODULE(ixpiic, ixp, ixpiic_driver, ixpiic_devclass, 0, 0);
DRIVER_MODULE(iicbb, ixpiic, iicbb_driver, iicbb_devclass, 0, 0);