1 /* drivers/atm/idt77105.c - IDT77105 (PHY) driver */
2 
3 /* Written 1999 by Greg Banks, NEC Australia <gnb@linuxfan.com>. Based on suni.c */
4 
5 
6 #include <linux/module.h>
7 #include <linux/sched.h>
8 #include <linux/kernel.h>
9 #include <linux/mm.h>
10 #include <linux/errno.h>
11 #include <linux/atmdev.h>
12 #include <linux/sonet.h>
13 #include <linux/delay.h>
14 #include <linux/timer.h>
15 #include <linux/init.h>
16 #include <linux/capability.h>
17 #include <linux/atm_idt77105.h>
18 #include <linux/spinlock.h>
19 #include <asm/system.h>
20 #include <asm/param.h>
21 #include <asm/uaccess.h>
22 
23 #include "idt77105.h"
24 
25 #undef GENERAL_DEBUG
26 
27 #ifdef GENERAL_DEBUG
28 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
29 #else
30 #define DPRINTK(format,args...)
31 #endif
32 
33 
34 struct idt77105_priv {
35 	struct idt77105_stats stats;    /* link diagnostics */
36 	struct atm_dev *dev;		/* device back-pointer */
37 	struct idt77105_priv *next;
38         int loop_mode;
39         unsigned char old_mcr;          /* storage of MCR reg while signal lost */
40 };
41 
42 static spinlock_t idt77105_priv_lock = SPIN_LOCK_UNLOCKED;
43 
44 #define PRIV(dev) ((struct idt77105_priv *) dev->phy_data)
45 
46 #define PUT(val,reg) dev->ops->phy_put(dev,val,IDT77105_##reg)
47 #define GET(reg) dev->ops->phy_get(dev,IDT77105_##reg)
48 
49 static void idt77105_stats_timer_func(unsigned long);
50 static void idt77105_restart_timer_func(unsigned long);
51 
52 
53 static struct timer_list stats_timer = {
54     function:	&idt77105_stats_timer_func
55 };
56 static struct timer_list restart_timer = {
57     function:	&idt77105_restart_timer_func
58 };
59 static int start_timer = 1;
60 static struct idt77105_priv *idt77105_all = NULL;
61 
62 /*
63  * Retrieve the value of one of the IDT77105's counters.
64  * `counter' is one of the IDT77105_CTRSEL_* constants.
65  */
get_counter(struct atm_dev * dev,int counter)66 static u16 get_counter(struct atm_dev *dev, int counter)
67 {
68         u16 val;
69 
70         /* write the counter bit into PHY register 6 */
71         PUT(counter, CTRSEL);
72         /* read the low 8 bits from register 4 */
73         val = GET(CTRLO);
74         /* read the high 8 bits from register 5 */
75         val |= GET(CTRHI)<<8;
76 
77         return val;
78 }
79 
80 /*
81  * Timer function called every second to gather statistics
82  * from the 77105. This is done because the h/w registers
83  * will overflow if not read at least once per second. The
84  * kernel's stats are much higher precision. Also, having
85  * a separate copy of the stats allows implementation of
86  * an ioctl which gathers the stats *without* zero'ing them.
87  */
idt77105_stats_timer_func(unsigned long dummy)88 static void idt77105_stats_timer_func(unsigned long dummy)
89 {
90 	struct idt77105_priv *walk;
91 	struct atm_dev *dev;
92 	struct idt77105_stats *stats;
93 
94         DPRINTK("IDT77105 gathering statistics\n");
95 	for (walk = idt77105_all; walk; walk = walk->next) {
96 		dev = walk->dev;
97 
98 		stats = &walk->stats;
99                 stats->symbol_errors += get_counter(dev, IDT77105_CTRSEL_SEC);
100                 stats->tx_cells += get_counter(dev, IDT77105_CTRSEL_TCC);
101                 stats->rx_cells += get_counter(dev, IDT77105_CTRSEL_RCC);
102                 stats->rx_hec_errors += get_counter(dev, IDT77105_CTRSEL_RHEC);
103 	}
104         if (!start_timer) mod_timer(&stats_timer,jiffies+IDT77105_STATS_TIMER_PERIOD);
105 }
106 
107 
108 /*
109  * A separate timer func which handles restarting PHY chips which
110  * have had the cable re-inserted after being pulled out. This is
111  * done by polling the Good Signal Bit in the Interrupt Status
112  * register every 5 seconds. The other technique (checking Good
113  * Signal Bit in the interrupt handler) cannot be used because PHY
114  * interrupts need to be disabled when the cable is pulled out
115  * to avoid lots of spurious cell error interrupts.
116  */
idt77105_restart_timer_func(unsigned long dummy)117 static void idt77105_restart_timer_func(unsigned long dummy)
118 {
119 	struct idt77105_priv *walk;
120 	struct atm_dev *dev;
121         unsigned char istat;
122 
123         DPRINTK("IDT77105 checking for cable re-insertion\n");
124 	for (walk = idt77105_all; walk; walk = walk->next) {
125 		dev = walk->dev;
126 
127                 if (dev->signal != ATM_PHY_SIG_LOST)
128                     continue;
129 
130                 istat = GET(ISTAT); /* side effect: clears all interrupt status bits */
131                 if (istat & IDT77105_ISTAT_GOODSIG) {
132                     /* Found signal again */
133                     dev->signal = ATM_PHY_SIG_FOUND;
134 	            printk(KERN_NOTICE "%s(itf %d): signal detected again\n",
135                         dev->type,dev->number);
136                     /* flush the receive FIFO */
137                     PUT( GET(DIAG) | IDT77105_DIAG_RFLUSH, DIAG);
138                     /* re-enable interrupts */
139 	            PUT( walk->old_mcr ,MCR);
140                 }
141 	}
142         if (!start_timer) mod_timer(&restart_timer,jiffies+IDT77105_RESTART_TIMER_PERIOD);
143 }
144 
145 
fetch_stats(struct atm_dev * dev,struct idt77105_stats * arg,int zero)146 static int fetch_stats(struct atm_dev *dev,struct idt77105_stats *arg,int zero)
147 {
148 	unsigned long flags;
149 	struct idt77105_stats stats;
150 
151 	spin_lock_irqsave(&idt77105_priv_lock, flags);
152 	memcpy(&stats, &PRIV(dev)->stats, sizeof(struct idt77105_stats));
153 	if (zero)
154 		memset(&PRIV(dev)->stats, 0, sizeof(struct idt77105_stats));
155 	spin_unlock_irqrestore(&idt77105_priv_lock, flags);
156 	if (arg == NULL)
157 		return 0;
158 	return copy_to_user(arg, &PRIV(dev)->stats,
159 		    sizeof(struct idt77105_stats)) ? -EFAULT : 0;
160 }
161 
162 
set_loopback(struct atm_dev * dev,int mode)163 static int set_loopback(struct atm_dev *dev,int mode)
164 {
165 	int diag;
166 
167 	diag = GET(DIAG) & ~IDT77105_DIAG_LCMASK;
168 	switch (mode) {
169 		case ATM_LM_NONE:
170 			break;
171 		case ATM_LM_LOC_ATM:
172 			diag |= IDT77105_DIAG_LC_PHY_LOOPBACK;
173 			break;
174 		case ATM_LM_RMT_ATM:
175 			diag |= IDT77105_DIAG_LC_LINE_LOOPBACK;
176 			break;
177 		default:
178 			return -EINVAL;
179 	}
180 	PUT(diag,DIAG);
181 	printk(KERN_NOTICE "%s(%d) Loopback mode is: %s\n", dev->type,
182 	    dev->number,
183 	    (mode == ATM_LM_NONE ? "NONE" :
184 	      (mode == ATM_LM_LOC_ATM ? "DIAG (local)" :
185 		(mode == IDT77105_DIAG_LC_LINE_LOOPBACK ? "LOOP (remote)" :
186 		  "unknown")))
187 		    );
188 	PRIV(dev)->loop_mode = mode;
189 	return 0;
190 }
191 
192 
idt77105_ioctl(struct atm_dev * dev,unsigned int cmd,void * arg)193 static int idt77105_ioctl(struct atm_dev *dev,unsigned int cmd,void *arg)
194 {
195         printk(KERN_NOTICE "%s(%d) idt77105_ioctl() called\n",dev->type,dev->number);
196 	switch (cmd) {
197 		case IDT77105_GETSTATZ:
198 			if (!capable(CAP_NET_ADMIN)) return -EPERM;
199 			/* fall through */
200 		case IDT77105_GETSTAT:
201 			return fetch_stats(dev,(struct idt77105_stats *) arg,
202 			    cmd == IDT77105_GETSTATZ);
203 		case ATM_SETLOOP:
204 			return set_loopback(dev,(int) (long) arg);
205 		case ATM_GETLOOP:
206 			return put_user(PRIV(dev)->loop_mode,(int *) arg) ?
207 			    -EFAULT : 0;
208 		case ATM_QUERYLOOP:
209 			return put_user(ATM_LM_LOC_ATM | ATM_LM_RMT_ATM,
210 			    (int *) arg) ? -EFAULT : 0;
211 		default:
212 			return -ENOIOCTLCMD;
213 	}
214 }
215 
216 
217 
idt77105_int(struct atm_dev * dev)218 static void idt77105_int(struct atm_dev *dev)
219 {
220         unsigned char istat;
221 
222         istat = GET(ISTAT); /* side effect: clears all interrupt status bits */
223 
224         DPRINTK("IDT77105 generated an interrupt, istat=%02x\n", (unsigned)istat);
225 
226         if (istat & IDT77105_ISTAT_RSCC) {
227             /* Rx Signal Condition Change - line went up or down */
228             if (istat & IDT77105_ISTAT_GOODSIG) {   /* signal detected again */
229                 /* This should not happen (restart timer does it) but JIC */
230                 dev->signal = ATM_PHY_SIG_FOUND;
231             } else {    /* signal lost */
232                 /*
233                  * Disable interrupts and stop all transmission and
234                  * reception - the restart timer will restore these.
235                  */
236                 PRIV(dev)->old_mcr = GET(MCR);
237 	        PUT(
238                     (PRIV(dev)->old_mcr|
239                     IDT77105_MCR_DREC|
240                     IDT77105_MCR_DRIC|
241                     IDT77105_MCR_HALTTX
242                     ) & ~IDT77105_MCR_EIP, MCR);
243                 dev->signal = ATM_PHY_SIG_LOST;
244 	        printk(KERN_NOTICE "%s(itf %d): signal lost\n",
245                     dev->type,dev->number);
246             }
247         }
248 
249         if (istat & IDT77105_ISTAT_RFO) {
250             /* Rx FIFO Overrun -- perform a FIFO flush */
251             PUT( GET(DIAG) | IDT77105_DIAG_RFLUSH, DIAG);
252 	    printk(KERN_NOTICE "%s(itf %d): receive FIFO overrun\n",
253                 dev->type,dev->number);
254         }
255 #ifdef GENERAL_DEBUG
256         if (istat & (IDT77105_ISTAT_HECERR | IDT77105_ISTAT_SCR |
257                      IDT77105_ISTAT_RSE)) {
258             /* normally don't care - just report in stats */
259 	    printk(KERN_NOTICE "%s(itf %d): received cell with error\n",
260                 dev->type,dev->number);
261         }
262 #endif
263 }
264 
265 
idt77105_start(struct atm_dev * dev)266 static int idt77105_start(struct atm_dev *dev)
267 {
268 	unsigned long flags;
269 
270 	if (!(dev->phy_data = kmalloc(sizeof(struct idt77105_priv),GFP_KERNEL)))
271 		return -ENOMEM;
272 	PRIV(dev)->dev = dev;
273 	spin_lock_irqsave(&idt77105_priv_lock, flags);
274 	PRIV(dev)->next = idt77105_all;
275 	idt77105_all = PRIV(dev);
276 	spin_unlock_irqrestore(&idt77105_priv_lock, flags);
277 	memset(&PRIV(dev)->stats,0,sizeof(struct idt77105_stats));
278 
279         /* initialise dev->signal from Good Signal Bit */
280         dev->signal = GET(ISTAT) & IDT77105_ISTAT_GOODSIG ? ATM_PHY_SIG_FOUND :
281 	  ATM_PHY_SIG_LOST;
282 	if (dev->signal == ATM_PHY_SIG_LOST)
283 		printk(KERN_WARNING "%s(itf %d): no signal\n",dev->type,
284 		    dev->number);
285 
286         /* initialise loop mode from hardware */
287         switch ( GET(DIAG) & IDT77105_DIAG_LCMASK ) {
288         case IDT77105_DIAG_LC_NORMAL:
289             PRIV(dev)->loop_mode = ATM_LM_NONE;
290             break;
291         case IDT77105_DIAG_LC_PHY_LOOPBACK:
292             PRIV(dev)->loop_mode = ATM_LM_LOC_ATM;
293             break;
294         case IDT77105_DIAG_LC_LINE_LOOPBACK:
295             PRIV(dev)->loop_mode = ATM_LM_RMT_ATM;
296             break;
297         }
298 
299         /* enable interrupts, e.g. on loss of signal */
300         PRIV(dev)->old_mcr = GET(MCR);
301         if (dev->signal == ATM_PHY_SIG_FOUND) {
302             PRIV(dev)->old_mcr |= IDT77105_MCR_EIP;
303 	    PUT(PRIV(dev)->old_mcr, MCR);
304         }
305 
306 
307 	idt77105_stats_timer_func(0); /* clear 77105 counters */
308 	(void) fetch_stats(dev,NULL,1); /* clear kernel counters */
309 
310 	spin_lock_irqsave(&idt77105_priv_lock, flags);
311 	if (start_timer) {
312 		start_timer = 0;
313 
314 		init_timer(&stats_timer);
315 		stats_timer.expires = jiffies+IDT77105_STATS_TIMER_PERIOD;
316 		stats_timer.function = idt77105_stats_timer_func;
317 		add_timer(&stats_timer);
318 
319 		init_timer(&restart_timer);
320 		restart_timer.expires = jiffies+IDT77105_RESTART_TIMER_PERIOD;
321 		restart_timer.function = idt77105_restart_timer_func;
322 		add_timer(&restart_timer);
323 	}
324 	spin_unlock_irqrestore(&idt77105_priv_lock, flags);
325 	return 0;
326 }
327 
328 
idt77105_stop(struct atm_dev * dev)329 int idt77105_stop(struct atm_dev *dev)
330 {
331 	struct idt77105_priv *walk, *prev;
332 
333         DPRINTK("%s(itf %d): stopping IDT77105\n",dev->type,dev->number);
334 
335         /* disable interrupts */
336 	PUT( GET(MCR) & ~IDT77105_MCR_EIP, MCR );
337 
338         /* detach private struct from atm_dev & free */
339 	for (prev = NULL, walk = idt77105_all ;
340              walk != NULL;
341              prev = walk, walk = walk->next) {
342             if (walk->dev == dev) {
343                 if (prev != NULL)
344                     prev->next = walk->next;
345                 else
346                     idt77105_all = walk->next;
347 	        dev->phy = NULL;
348                 dev->phy_data = NULL;
349                 kfree(walk);
350                 break;
351             }
352         }
353 
354 	return 0;
355 }
356 
357 
358 static const struct atmphy_ops idt77105_ops = {
359 	.start = 	idt77105_start,
360 	.ioctl =	idt77105_ioctl,
361 	.interrupt =	idt77105_int,
362 	.stop =		idt77105_stop,
363 };
364 
365 
idt77105_init(struct atm_dev * dev)366 int idt77105_init(struct atm_dev *dev)
367 {
368 	dev->phy = &idt77105_ops;
369 	return 0;
370 }
371 
372 EXPORT_SYMBOL(idt77105_init);
373 
idt77105_exit(void)374 static void __exit idt77105_exit(void)
375 {
376         /* turn off timers */
377         del_timer(&stats_timer);
378         del_timer(&restart_timer);
379 }
380 
381 module_exit(idt77105_exit);
382 
383 MODULE_LICENSE("GPL");
384