summaryrefslogtreecommitdiffstats
path: root/sys/dev/pcf/pcfvar.h
blob: 8e4d5d82ea2cf322796a145953e5a8e1b6c6f886 (plain)
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
/*-
 * Copyright (c) 1998 Nicolas Souchu, Marc Bouget
 * Copyright (c) 2004 Joerg Wunsch
 * 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
 *
 * $FreeBSD$
 */

#ifndef __PCFVAR_H__
#define	__PCFVAR_H__

#define IO_PCFSIZE	2

#define TIMEOUT	9999					/* XXX */

/* Status bits of S1 register (read only) */
#define nBB	0x01		/* busy when low set/reset by STOP/START*/
#define LAB	0x02		/* lost arbitration bit in multi-master mode */
#define AAS	0x04		/* addressed as slave */
#define LRB	0x08		/* last received byte when not AAS */
#define AD0	0x08		/* general call received when AAS */
#define BER	0x10		/* bus error, misplaced START or STOP */
#define STS	0x20		/* STOP detected in slave receiver mode */
#define PIN	0x80		/* pending interrupt not (r/w) */

/* Control bits of S1 register (write only) */
#define ACK	0x01
#define STO	0x02
#define STA	0x04
#define ENI	0x08
#define ES2	0x10
#define ES1	0x20
#define ESO	0x40

#define BUFSIZE 2048

#define SLAVE_TRANSMITTER	0x1
#define SLAVE_RECEIVER		0x2

#define PCF_DEFAULT_ADDR	0xaa

struct pcf_softc {
	u_char	pcf_addr;		/* interface I2C address */
	int	pcf_flags;		/* IIC_POLLED? */
	int	pcf_slave_mode;		/* receiver or transmitter */
	int	pcf_started;		/* 1 if start condition sent */

	struct mtx pcf_lock;
	device_t iicbus;		/* the corresponding iicbus */

	/* Resource handling stuff. */
	struct resource		*res_ioport;
	int			rid_ioport;
	struct resource		*res_irq;
	int			rid_irq;
	void			*intr_cookie;
};
#define DEVTOSOFTC(dev) ((struct pcf_softc *)device_get_softc(dev))

#define	PCF_LOCK(sc)		mtx_lock(&(sc)->pcf_lock)
#define	PCF_UNLOCK(sc)		mtx_unlock(&(sc)->pcf_lock)
#define	PCF_ASSERT_LOCKED(sc)	mtx_assert(&(sc)->pcf_lock, MA_OWNED)

/*
 * PCF8584 datasheet : when operate at 8 MHz or more, a minimun time of
 * 6 clocks cycles must be left between two consecutives access
 */
#define pcf_nops()	DELAY(10)

#define dummy_read(sc)	pcf_get_S0(sc)
#define dummy_write(sc)	pcf_set_S0(sc, 0)

/*
 * Specific register access to PCF8584
 */
static __inline void
pcf_set_S0(struct pcf_softc *sc, int data)
{

	bus_write_1(sc->res_ioport, 0, data);
	pcf_nops();
}

static __inline void
pcf_set_S1(struct pcf_softc *sc, int data)
{

	bus_write_1(sc->res_ioport, 1, data);
	pcf_nops();
}

static __inline char
pcf_get_S0(struct pcf_softc *sc)
{
	char data;

	data = bus_read_1(sc->res_ioport, 0);
	pcf_nops();

	return (data);
}

static __inline char
pcf_get_S1(struct pcf_softc *sc)
{
	char data;

	data = bus_read_1(sc->res_ioport, 1);
	pcf_nops();

	return (data);
}

extern int pcf_repeated_start(device_t, u_char, int);
extern int pcf_start(device_t, u_char, int);
extern int pcf_stop(device_t);
extern int pcf_write(device_t, char *, int, int *, int);
extern int pcf_read(device_t, char *, int, int *, int, int);
extern int pcf_rst_card(device_t, u_char, u_char, u_char *);
extern driver_intr_t pcf_intr;

#define PCF_MODVER	1
#define PCF_MINVER	1
#define PCF_MAXVER	1
#define PCF_PREFVER	PCF_MODVER

#endif /* !__PCFVAR_H__ */
OpenPOWER on IntegriCloud