1 /* drivers/atm/idt77105.c - IDT77105 (PHY) driver */
3 /* Written 1999 by Greg Banks, NEC Australia <gnb@linuxfan.com>. Based on suni.c */
6 #include <linux/module.h>
7 #include <linux/kernel.h>
9 #include <linux/errno.h>
10 #include <linux/atmdev.h>
11 #include <linux/sonet.h>
12 #include <linux/delay.h>
13 #include <linux/timer.h>
14 #include <linux/init.h>
15 #include <linux/capability.h>
16 #include <linux/atm_idt77105.h>
17 #include <linux/spinlock.h>
18 #include <linux/slab.h>
19 #include <asm/param.h>
20 #include <linux/uaccess.h>
27 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
29 #define DPRINTK(format,args...)
33 struct idt77105_priv {
34 struct idt77105_stats stats; /* link diagnostics */
35 struct atm_dev *dev; /* device back-pointer */
36 struct idt77105_priv *next;
38 unsigned char old_mcr; /* storage of MCR reg while signal lost */
41 static DEFINE_SPINLOCK(idt77105_priv_lock);
43 #define PRIV(dev) ((struct idt77105_priv *) dev->phy_data)
45 #define PUT(val,reg) dev->ops->phy_put(dev,val,IDT77105_##reg)
46 #define GET(reg) dev->ops->phy_get(dev,IDT77105_##reg)
48 static void idt77105_stats_timer_func(unsigned long);
49 static void idt77105_restart_timer_func(unsigned long);
52 static DEFINE_TIMER(stats_timer, idt77105_stats_timer_func);
53 static DEFINE_TIMER(restart_timer, idt77105_restart_timer_func);
54 static int start_timer = 1;
55 static struct idt77105_priv *idt77105_all = NULL;
58 * Retrieve the value of one of the IDT77105's counters.
59 * `counter' is one of the IDT77105_CTRSEL_* constants.
61 static u16 get_counter(struct atm_dev *dev, int counter)
65 /* write the counter bit into PHY register 6 */
67 /* read the low 8 bits from register 4 */
69 /* read the high 8 bits from register 5 */
76 * Timer function called every second to gather statistics
77 * from the 77105. This is done because the h/w registers
78 * will overflow if not read at least once per second. The
79 * kernel's stats are much higher precision. Also, having
80 * a separate copy of the stats allows implementation of
81 * an ioctl which gathers the stats *without* zero'ing them.
83 static void idt77105_stats_timer_func(unsigned long dummy)
85 struct idt77105_priv *walk;
87 struct idt77105_stats *stats;
89 DPRINTK("IDT77105 gathering statistics\n");
90 for (walk = idt77105_all; walk; walk = walk->next) {
94 stats->symbol_errors += get_counter(dev, IDT77105_CTRSEL_SEC);
95 stats->tx_cells += get_counter(dev, IDT77105_CTRSEL_TCC);
96 stats->rx_cells += get_counter(dev, IDT77105_CTRSEL_RCC);
97 stats->rx_hec_errors += get_counter(dev, IDT77105_CTRSEL_RHEC);
99 if (!start_timer) mod_timer(&stats_timer,jiffies+IDT77105_STATS_TIMER_PERIOD);
104 * A separate timer func which handles restarting PHY chips which
105 * have had the cable re-inserted after being pulled out. This is
106 * done by polling the Good Signal Bit in the Interrupt Status
107 * register every 5 seconds. The other technique (checking Good
108 * Signal Bit in the interrupt handler) cannot be used because PHY
109 * interrupts need to be disabled when the cable is pulled out
110 * to avoid lots of spurious cell error interrupts.
112 static void idt77105_restart_timer_func(unsigned long dummy)
114 struct idt77105_priv *walk;
118 DPRINTK("IDT77105 checking for cable re-insertion\n");
119 for (walk = idt77105_all; walk; walk = walk->next) {
122 if (dev->signal != ATM_PHY_SIG_LOST)
125 istat = GET(ISTAT); /* side effect: clears all interrupt status bits */
126 if (istat & IDT77105_ISTAT_GOODSIG) {
127 /* Found signal again */
128 atm_dev_signal_change(dev, ATM_PHY_SIG_FOUND);
129 printk(KERN_NOTICE "%s(itf %d): signal detected again\n",
130 dev->type,dev->number);
131 /* flush the receive FIFO */
132 PUT( GET(DIAG) | IDT77105_DIAG_RFLUSH, DIAG);
133 /* re-enable interrupts */
134 PUT( walk->old_mcr ,MCR);
137 if (!start_timer) mod_timer(&restart_timer,jiffies+IDT77105_RESTART_TIMER_PERIOD);
141 static int fetch_stats(struct atm_dev *dev,struct idt77105_stats __user *arg,int zero)
144 struct idt77105_stats stats;
146 spin_lock_irqsave(&idt77105_priv_lock, flags);
147 memcpy(&stats, &PRIV(dev)->stats, sizeof(struct idt77105_stats));
149 memset(&PRIV(dev)->stats, 0, sizeof(struct idt77105_stats));
150 spin_unlock_irqrestore(&idt77105_priv_lock, flags);
153 return copy_to_user(arg, &stats,
154 sizeof(struct idt77105_stats)) ? -EFAULT : 0;
158 static int set_loopback(struct atm_dev *dev,int mode)
162 diag = GET(DIAG) & ~IDT77105_DIAG_LCMASK;
167 diag |= IDT77105_DIAG_LC_PHY_LOOPBACK;
170 diag |= IDT77105_DIAG_LC_LINE_LOOPBACK;
176 printk(KERN_NOTICE "%s(%d) Loopback mode is: %s\n", dev->type,
178 (mode == ATM_LM_NONE ? "NONE" :
179 (mode == ATM_LM_LOC_ATM ? "DIAG (local)" :
180 (mode == IDT77105_DIAG_LC_LINE_LOOPBACK ? "LOOP (remote)" :
183 PRIV(dev)->loop_mode = mode;
188 static int idt77105_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg)
190 printk(KERN_NOTICE "%s(%d) idt77105_ioctl() called\n",dev->type,dev->number);
192 case IDT77105_GETSTATZ:
193 if (!capable(CAP_NET_ADMIN)) return -EPERM;
195 case IDT77105_GETSTAT:
196 return fetch_stats(dev, arg, cmd == IDT77105_GETSTATZ);
198 return set_loopback(dev,(int)(unsigned long) arg);
200 return put_user(PRIV(dev)->loop_mode,(int __user *)arg) ?
203 return put_user(ATM_LM_LOC_ATM | ATM_LM_RMT_ATM,
204 (int __user *) arg) ? -EFAULT : 0;
212 static void idt77105_int(struct atm_dev *dev)
216 istat = GET(ISTAT); /* side effect: clears all interrupt status bits */
218 DPRINTK("IDT77105 generated an interrupt, istat=%02x\n", (unsigned)istat);
220 if (istat & IDT77105_ISTAT_RSCC) {
221 /* Rx Signal Condition Change - line went up or down */
222 if (istat & IDT77105_ISTAT_GOODSIG) { /* signal detected again */
223 /* This should not happen (restart timer does it) but JIC */
224 atm_dev_signal_change(dev, ATM_PHY_SIG_FOUND);
225 } else { /* signal lost */
227 * Disable interrupts and stop all transmission and
228 * reception - the restart timer will restore these.
230 PRIV(dev)->old_mcr = GET(MCR);
236 ) & ~IDT77105_MCR_EIP, MCR);
237 atm_dev_signal_change(dev, ATM_PHY_SIG_LOST);
238 printk(KERN_NOTICE "%s(itf %d): signal lost\n",
239 dev->type,dev->number);
243 if (istat & IDT77105_ISTAT_RFO) {
244 /* Rx FIFO Overrun -- perform a FIFO flush */
245 PUT( GET(DIAG) | IDT77105_DIAG_RFLUSH, DIAG);
246 printk(KERN_NOTICE "%s(itf %d): receive FIFO overrun\n",
247 dev->type,dev->number);
250 if (istat & (IDT77105_ISTAT_HECERR | IDT77105_ISTAT_SCR |
251 IDT77105_ISTAT_RSE)) {
252 /* normally don't care - just report in stats */
253 printk(KERN_NOTICE "%s(itf %d): received cell with error\n",
254 dev->type,dev->number);
260 static int idt77105_start(struct atm_dev *dev)
264 if (!(dev->dev_data = kmalloc(sizeof(struct idt77105_priv),GFP_KERNEL)))
266 PRIV(dev)->dev = dev;
267 spin_lock_irqsave(&idt77105_priv_lock, flags);
268 PRIV(dev)->next = idt77105_all;
269 idt77105_all = PRIV(dev);
270 spin_unlock_irqrestore(&idt77105_priv_lock, flags);
271 memset(&PRIV(dev)->stats,0,sizeof(struct idt77105_stats));
273 /* initialise dev->signal from Good Signal Bit */
274 atm_dev_signal_change(dev,
275 GET(ISTAT) & IDT77105_ISTAT_GOODSIG ?
276 ATM_PHY_SIG_FOUND : ATM_PHY_SIG_LOST);
277 if (dev->signal == ATM_PHY_SIG_LOST)
278 printk(KERN_WARNING "%s(itf %d): no signal\n",dev->type,
281 /* initialise loop mode from hardware */
282 switch ( GET(DIAG) & IDT77105_DIAG_LCMASK ) {
283 case IDT77105_DIAG_LC_NORMAL:
284 PRIV(dev)->loop_mode = ATM_LM_NONE;
286 case IDT77105_DIAG_LC_PHY_LOOPBACK:
287 PRIV(dev)->loop_mode = ATM_LM_LOC_ATM;
289 case IDT77105_DIAG_LC_LINE_LOOPBACK:
290 PRIV(dev)->loop_mode = ATM_LM_RMT_ATM;
294 /* enable interrupts, e.g. on loss of signal */
295 PRIV(dev)->old_mcr = GET(MCR);
296 if (dev->signal == ATM_PHY_SIG_FOUND) {
297 PRIV(dev)->old_mcr |= IDT77105_MCR_EIP;
298 PUT(PRIV(dev)->old_mcr, MCR);
302 idt77105_stats_timer_func(0); /* clear 77105 counters */
303 (void) fetch_stats(dev,NULL,1); /* clear kernel counters */
305 spin_lock_irqsave(&idt77105_priv_lock, flags);
309 setup_timer(&stats_timer, idt77105_stats_timer_func, 0UL);
310 stats_timer.expires = jiffies+IDT77105_STATS_TIMER_PERIOD;
311 add_timer(&stats_timer);
313 setup_timer(&restart_timer, idt77105_restart_timer_func, 0UL);
314 restart_timer.expires = jiffies+IDT77105_RESTART_TIMER_PERIOD;
315 add_timer(&restart_timer);
317 spin_unlock_irqrestore(&idt77105_priv_lock, flags);
322 static int idt77105_stop(struct atm_dev *dev)
324 struct idt77105_priv *walk, *prev;
326 DPRINTK("%s(itf %d): stopping IDT77105\n",dev->type,dev->number);
328 /* disable interrupts */
329 PUT( GET(MCR) & ~IDT77105_MCR_EIP, MCR );
331 /* detach private struct from atm_dev & free */
332 for (prev = NULL, walk = idt77105_all ;
334 prev = walk, walk = walk->next) {
335 if (walk->dev == dev) {
337 prev->next = walk->next;
339 idt77105_all = walk->next;
341 dev->dev_data = NULL;
351 static const struct atmphy_ops idt77105_ops = {
352 .start = idt77105_start,
353 .ioctl = idt77105_ioctl,
354 .interrupt = idt77105_int,
355 .stop = idt77105_stop,
359 int idt77105_init(struct atm_dev *dev)
361 dev->phy = &idt77105_ops;
365 EXPORT_SYMBOL(idt77105_init);
367 static void __exit idt77105_exit(void)
369 /* turn off timers */
370 del_timer_sync(&stats_timer);
371 del_timer_sync(&restart_timer);
374 module_exit(idt77105_exit);
376 MODULE_LICENSE("GPL");