summaryrefslogtreecommitdiffstats
path: root/drivers/atm/uPD98402.c
blob: 01b8f042509021a92d55a1d2f0fad62d5cc4a328 (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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* drivers/atm/uPD98402.c - NEC uPD98402 (PHY) declarations */
 
/* Written 1995-1998 by Werner Almesberger, EPFL LRC/ICA */
 

#include <linux/module.h>
#include <linux/sched.h> /* for jiffies */
#include <linux/mm.h>
#include <linux/errno.h>
#include <linux/atmdev.h>
#include <linux/sonet.h>
#include <linux/init.h>
#include <asm/uaccess.h>

#include "uPD98402.h"


#if 0
#define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
#else
#define DPRINTK(format,args...)
#endif


struct uPD98402_priv {
	struct sonet_stats sonet_stats;	/* link diagnostics */
	unsigned char framing;		/* SONET/SDH framing */
};


#define PRIV(dev) ((struct uPD98402_priv *) dev->phy_data)

#define PUT(val,reg) dev->ops->phy_put(dev,val,uPD98402_##reg)
#define GET(reg) dev->ops->phy_get(dev,uPD98402_##reg)


static int fetch_stats(struct atm_dev *dev,struct sonet_stats *arg,int zero)
{
	unsigned long flags;
 	int error;

	error = 0;
	save_flags(flags);
	cli();
	PRIV(dev)->sonet_stats.uncorr_hcs += GET(HECCT);
	if (arg)
		error = copy_to_user(arg,&PRIV(dev)->sonet_stats,
		    sizeof(struct sonet_stats));
	if (zero && !error) {
		memset(&PRIV(dev)->sonet_stats,0,sizeof(struct sonet_stats));
		PRIV(dev)->sonet_stats.corr_hcs = -1;
		PRIV(dev)->sonet_stats.tx_cells = -1;
		PRIV(dev)->sonet_stats.rx_cells = -1;
	}
	restore_flags(flags);
	return error ? -EFAULT : 0;
}


static int set_framing(struct atm_dev *dev,unsigned char framing)
{
	static const unsigned char sonet[] = { 1,2,3,0 };
	static const unsigned char sdh[] = { 1,0,0,2 };
	const char *set;
	unsigned long flags;
 
	switch (framing) {
		case SONET_FRAME_SONET:
			set = sonet;
			break;
		case SONET_FRAME_SDH:
			set = sdh;
			break;
		default:
			return -EINVAL;
	}
	save_flags(flags);
	cli();
	PUT(set[0],C11T);
	PUT(set[1],C12T);
	PUT(set[2],C13T);
	PUT((GET(MDR) & ~uPD98402_MDR_SS_MASK) | (set[3] <<
	    uPD98402_MDR_SS_SHIFT),MDR);
	restore_flags(flags);
	return 0;
}


static int get_sense(struct atm_dev *dev,u8 *arg)
{
	unsigned long flags;
 	int error;

	save_flags(flags);
	cli();
	error = put_user(GET(C11R),arg) || put_user(GET(C12R),arg+1) ||
	    put_user(GET(C13R),arg+2);
	restore_flags(flags);
	error = error || put_user(0xff,arg+3) || put_user(0xff,arg+4) ||
	    put_user(0xff,arg+5);
	return error ? -EFAULT : 0;
}


static int uPD98402_ioctl(struct atm_dev *dev,unsigned int cmd,void *arg)
{
	switch (cmd) {

		case SONET_GETSTATZ:
                case SONET_GETSTAT:
			return fetch_stats(dev,(struct sonet_stats *) arg,
			    cmd == SONET_GETSTATZ);
		case SONET_SETFRAMING:
			return set_framing(dev,(int) arg);
		case SONET_GETFRAMING:
			return put_user(PRIV(dev)->framing,(int *) arg) ?
			    -EFAULT : 0;
		case SONET_GETFRSENSE:
			return get_sense(dev,arg);
		default:
		    return -EINVAL;
	}
}


static void stat_event(struct atm_dev *dev)
{
	unsigned char events;

	events = GET(PCR);
	if (events & uPD98402_PFM_PFEB)
		if ((PRIV(dev)->sonet_stats.path_febe += GET(PFECB)) < 0)
			PRIV(dev)->sonet_stats.path_febe = LONG_MAX;
	if (events & uPD98402_PFM_LFEB)
		if ((PRIV(dev)->sonet_stats.line_febe += GET(LECCT)) < 0)
			PRIV(dev)->sonet_stats.line_febe = LONG_MAX;
	if (events & uPD98402_PFM_B3E)
		if ((PRIV(dev)->sonet_stats.path_bip += GET(B3ECT)) < 0)
			PRIV(dev)->sonet_stats.path_bip = LONG_MAX;
	if (events & uPD98402_PFM_B2E)
		if ((PRIV(dev)->sonet_stats.line_bip += GET(B2ECT)) < 0)
			PRIV(dev)->sonet_stats.line_bip = LONG_MAX;
	if (events & uPD98402_PFM_B1E)
		if ((PRIV(dev)->sonet_stats.section_bip += GET(B1ECT)) < 0)
			PRIV(dev)->sonet_stats.section_bip = LONG_MAX;
}


static void uPD98402_int(struct atm_dev *dev)
{
	static unsigned long silence = 0;
	unsigned char reason;

	while ((reason = GET(PICR))) {
		if (reason & uPD98402_INT_LOS)
			printk(KERN_NOTICE "%s(itf %d): signal lost\n",
			    dev->type,dev->number);
		if (reason & uPD98402_INT_PFM) stat_event(dev);
		if (reason & uPD98402_INT_PCO) {
			(void) GET(PCOCR); /* clear interrupt cause */
			PRIV(dev)->sonet_stats.uncorr_hcs += GET(HECCT);
		}
		if ((reason & uPD98402_INT_RFO) && 
		    (time_after(jiffies, silence) || silence == 0)) {
			printk(KERN_WARNING "%s(itf %d): uPD98402 receive "
			    "FIFO overflow\n",dev->type,dev->number);
			silence = (jiffies+HZ/2)|1;
		}
	}
}


static int uPD98402_start(struct atm_dev *dev)
{
DPRINTK("phy_start\n");
	if (!(PRIV(dev) = kmalloc(sizeof(struct uPD98402_priv),GFP_KERNEL)))
		return -ENOMEM;
	memset(&PRIV(dev)->sonet_stats,0,sizeof(struct sonet_stats));
	(void) GET(PCR); /* clear performance events */
	PUT(uPD98402_PFM_FJ,PCMR); /* ignore frequency adj */
	(void) GET(PCOCR); /* clear overflows */
	PUT(~uPD98402_PCO_HECC,PCOMR);
	(void) GET(PICR); /* clear interrupts */
	PUT(~(uPD98402_INT_PFM | uPD98402_INT_ALM | uPD98402_INT_RFO |
	  uPD98402_INT_LOS),PIMR); /* enable them */
	(void) fetch_stats(dev,NULL,1); /* clear kernel counters */
	return 0;
}



static const struct atmphy_ops uPD98402_ops = {
	uPD98402_start,
	uPD98402_ioctl,	/* no ioctl yet */
	uPD98402_int
};


int __init uPD98402_init(struct atm_dev *dev)
{
DPRINTK("phy_init\n");
	dev->phy = &uPD98402_ops;
	return 0;
}


#ifdef MODULE

EXPORT_SYMBOL(uPD98402_init);

 
int init_module(void)
{
	MOD_INC_USE_COUNT;
	return 0;
}


void cleanup_module(void)
{
	/* Nay */
}
 
#endif