1169689Skan/*-
2169689Skan * Copyright (c) 2003
3169689Skan *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4169689Skan * 	All rights reserved.
5169689Skan *
6169689Skan * Redistribution and use in source and binary forms, with or without
7169689Skan * modification, are permitted provided that the following conditions
8169689Skan * are met:
9169689Skan * 1. Redistributions of source code must retain the above copyright
10169689Skan *    notice, this list of conditions and the following disclaimer.
11169689Skan * 2. Redistributions in binary form must reproduce the above copyright
12169689Skan *    notice, this list of conditions and the following disclaimer in the
13169689Skan *    documentation and/or other materials provided with the distribution.
14169689Skan *
15169689Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18169689Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25169689Skan * SUCH DAMAGE.
26169689Skan *
27169689Skan * Author: Hartmut Brandt <harti@freebsd.org>
28169689Skan *
29169689Skan * $FreeBSD$
30169689Skan */
31169689Skan#ifndef _DEV_UTOPIA_UTOPIA_H
32169689Skan#define	_DEV_UTOPIA_UTOPIA_H
33169689Skan
34169689Skan/* Structure for user-level register formatting */
35169689Skanstruct utopia_print {
36169689Skan	uint8_t		type;	/* register type */
37169689Skan	uint8_t		reg;	/* register number */
38169689Skan	const char	*name;	/* register name */
39169689Skan	const char	*fmt;	/* format for printing */
40169689Skan};
41169689Skan
42169689Skan/*
43169689Skan * Types of registers
44169689Skan */
45169689Skan#define	UTP_REGT_BITS		0x0	/* use printb to print */
46169689Skan#define	UTP_REGT_INT8		0x1	/* 8 bit hex number */
47169689Skan#define	UTP_REGT_INT10BITS	0x2	/* 10 bit hex number + 6 bit printb */
48169689Skan#define	UTP_REGT_INT12		0x3	/* 12 bit LE hex */
49169689Skan#define	UTP_REGT_INT16		0x4	/* 16 bit LE hex */
50169689Skan#define	UTP_REGT_INT19		0x5	/* 19 bit LE hex */
51169689Skan#define	UTP_REGT_INT20		0x6	/* 20 bit LE hex */
52169689Skan#define	UTP_REGT_INT21		0x7	/* 21 bit LE hex */
53169689Skan#define	UTP_REGT_INT18		0x8	/* 18 bit LE hex */
54169689Skan
55169689Skan/* number of additional registers per type */
56169689Skan#define	UTP_REG_ADD	0, 0, 1, 1, 1, 2, 2, 2, 2
57169689Skan
58169689Skan/* flags field */
59169689Skan#define	UTP_FL_NORESET		0x0001	/* cannot write MRESET register */
60169689Skan#define	UTP_FL_POLL_CARRIER	0x0002	/* need to poll for carrier */
61169689Skan
62169689Skan/* state field */
63169689Skan#define	UTP_ST_ACTIVE		0x0001	/* registers accessible */
64169689Skan#define	UTP_ST_SDH		0x0002	/* SDH or SONET */
65169689Skan#define	UTP_ST_UNASS		0x0004	/* produce unassigned cells */
66169689Skan#define	UTP_ST_NOSCRAMB		0x0008	/* no scrambling */
67169689Skan#define	UTP_ST_DETACH		0x0010	/* detaching */
68169689Skan#define	UTP_ST_ATTACHED		0x0020	/* successful attached */
69169689Skan
70169689Skan/* carrier field */
71169689Skan#define	UTP_CARR_UNKNOWN	0
72169689Skan#define	UTP_CARR_OK		1
73169689Skan#define	UTP_CARR_LOST		2
74169689Skan
75169689Skan/* loopback field */
76169689Skan#define	UTP_LOOP_NONE		0x0000
77169710Skan#define	UTP_LOOP_TIME		0x0001	/* timing source loopback */
78169689Skan#define	UTP_LOOP_DIAG		0x0002	/* diagnostic loopback */
79169689Skan#define	UTP_LOOP_LINE		0x0004	/* serial line loopback */
80169689Skan#define	UTP_LOOP_PARAL		0x0008	/* parallel diagnostic loopback */
81169689Skan#define	UTP_LOOP_TWIST		0x0010	/* twisted pair diagnostic loopback */
82169710Skan#define	UTP_LOOP_PATH		0x0020	/* diagnostic path loopback */
83169689Skan
84169689Skan/* type */
85169689Skan#define	UTP_TYPE_UNKNOWN	0
86169689Skan#define	UTP_TYPE_SUNI_LITE	1
87169689Skan#define	UTP_TYPE_SUNI_ULTRA	2
88169689Skan#define	UTP_TYPE_SUNI_622	3
89169689Skan#define	UTP_TYPE_IDT77105	4
90169689Skan#define	UTP_TYPE_IDT77155	5
91169689Skan#define	UTP_TYPE_CX28250	6
92169689Skan
93169689Skan/*
94169689Skan * Statistics. These structures are versioned.
95169689Skan */
96169689Skanstruct utopia_stats1 {
97169689Skan	uint32_t	version;	/* version of this statistics struct */
98169689Skan	uint32_t	fill;
99169689Skan
100169689Skan	uint64_t	rx_sbip;	/* rx section BIP errors */
101169689Skan	uint64_t	rx_lbip;	/* rx line BIP errors */
102169689Skan	uint64_t	rx_lfebe;	/* rx line far end block errors */
103169689Skan	uint64_t	rx_pbip;	/* rx path BIP errors */
104169689Skan	uint64_t	rx_pfebe;	/* rx path far end block errors */
105169689Skan	uint64_t	rx_cells;	/* received cells */
106169689Skan	uint64_t	rx_corr;	/* correctable cell errors */
107169689Skan	uint64_t	rx_uncorr;	/* uncorrectable cell errors */
108169689Skan	uint64_t	rx_symerr;	/* symbol errors */
109169689Skan
110169689Skan	uint64_t	tx_cells;	/* transmitted cells */
111169689Skan};
112169689Skan
113169689Skan#ifdef _KERNEL
114169689Skan
115169689Skan#include <sys/queue.h>
116169689Skan
117169689Skan/*
118169689Skan * These must be implemented by the card driver
119169689Skan */
120169689Skanstruct utopia_methods {
121169689Skan	/* read at most n PHY registers starting at reg into val */
122169689Skan	int	(*readregs)(struct ifatm *, u_int reg, uint8_t *val, u_int *n);
123169689Skan
124169689Skan	/* change the bits given by mask to them in val in register reg */
125169689Skan	int	(*writereg)(struct ifatm *, u_int reg, u_int mask, u_int val);
126169689Skan};
127169689Skan
128169689Skan/*
129169689Skan * Public state
130169689Skan */
131169689Skanstruct utopia {
132169689Skan	struct ifatm	*ifatm;		/* driver data */
133169689Skan	struct ifmedia	*media;		/* driver supplied */
134169689Skan	struct mtx	*lock;		/* driver supplied */
135169689Skan	const struct utopia_methods *methods;
136169689Skan	LIST_ENTRY(utopia) link;	/* list of these structures */
137169689Skan	u_int		flags;		/* flags set by the driver */
138169689Skan	u_int		state;		/* current state */
139169689Skan	u_int		carrier;	/* carrier state */
140169689Skan	u_int		loopback;	/* loopback mode */
141169689Skan	const struct utopia_chip *chip;	/* chip operations */
142169689Skan	struct utopia_stats1 stats;	/* statistics */
143169689Skan};
144169689Skan
145169689Skanstruct utopia_chip {
146169689Skan	/* type and name of the chip */
147169689Skan	u_int	type;
148169689Skan	const char *const name;
149169689Skan
150169689Skan	/* number of registers */
151169689Skan	u_int	nregs;
152169689Skan
153169689Skan	/* reset chip to known state */
154169689Skan	int	(*reset)(struct utopia *);
155169689Skan
156169689Skan	/* set SONET/SDH mode */
157169689Skan	int	(*set_sdh)(struct utopia *, int sdh);
158169689Skan
159169689Skan	/* set idle/unassigned cells */
160169689Skan	int	(*set_unass)(struct utopia *, int unass);
161169689Skan
162169689Skan	/* enable/disable scrambling */
163169689Skan	int	(*set_noscramb)(struct utopia *, int noscramb);
164169689Skan
165169689Skan	/* update carrier status */
166169689Skan	int	(*update_carrier)(struct utopia *);
167169689Skan
168169689Skan	/* set loopback mode */
169169689Skan	int	(*set_loopback)(struct utopia *, u_int mode);
170169689Skan
171169689Skan	/* handle interrupt */
172169689Skan	void	(*intr)(struct utopia *);
173169689Skan
174169689Skan	/* update statistics */
175169689Skan	void	(*update_stats)(struct utopia *);
176169689Skan};
177169689Skan
178169689Skan/*
179169689Skan * These are implemented in the common utopia code
180169689Skan */
181169689Skanint utopia_attach(struct utopia *, struct ifatm *, struct ifmedia *,
182169689Skan    struct mtx *, struct sysctl_ctx_list *, struct sysctl_oid_list *,
183169689Skan    const struct utopia_methods *);
184169689Skanvoid utopia_detach(struct utopia *);
185169689Skan
186169689Skanint utopia_start(struct utopia *);
187169689Skanvoid utopia_stop(struct utopia *);
188169689Skan
189169689Skanvoid utopia_init_media(struct utopia *);
190169689Skanvoid utopia_reset_media(struct utopia *);
191169689Skan
192169689Skan#define	utopia_reset(S)			((S)->chip->reset((S)))
193169689Skan#define	utopia_set_sdh(S, SDH)		((S)->chip->set_sdh((S), (SDH)))
194169689Skan#define	utopia_set_unass(S, U)		((S)->chip->set_unass((S), (U)))
195169689Skan#define	utopia_set_noscramb(S, N)	((S)->chip->set_noscramb((S), (N)))
196169689Skan#define	utopia_update_carrier(S)	((S)->chip->update_carrier((S)))
197169689Skan#define	utopia_update_stats(S)		((S)->chip->update_stats((S)))
198169689Skan#define	utopia_set_loopback(S, L)	((S)->chip->set_loopback((S), (L)))
199169689Skan#define	utopia_intr(S)			((S)->chip->intr((S)))
200169689Skan
201169689Skan#endif /* _KERNEL */
202169689Skan
203169689Skan#endif	/* _DEV_UTOPIA_UTOPIA_H */
204169689Skan