mtd: nandsim: switch to exec_op interface
[linux/fpc-iii.git] / drivers / atm / idt77105.c
blob0a67487c0b1d3f0fbc85215eeb9d733966482b53
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>
8 #include <linux/mm.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>
22 #include "idt77105.h"
24 #undef GENERAL_DEBUG
26 #ifdef GENERAL_DEBUG
27 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
28 #else
29 #define DPRINTK(format,args...)
30 #endif
33 struct idt77105_priv {
34 struct idt77105_stats stats; /* link diagnostics */
35 struct atm_dev *dev; /* device back-pointer */
36 struct idt77105_priv *next;
37 int loop_mode;
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(struct timer_list *);
49 static void idt77105_restart_timer_func(struct timer_list *);
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)
63 u16 val;
65 /* write the counter bit into PHY register 6 */
66 PUT(counter, CTRSEL);
67 /* read the low 8 bits from register 4 */
68 val = GET(CTRLO);
69 /* read the high 8 bits from register 5 */
70 val |= GET(CTRHI)<<8;
72 return val;
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(struct timer_list *unused)
85 struct idt77105_priv *walk;
86 struct atm_dev *dev;
87 struct idt77105_stats *stats;
89 DPRINTK("IDT77105 gathering statistics\n");
90 for (walk = idt77105_all; walk; walk = walk->next) {
91 dev = walk->dev;
93 stats = &walk->stats;
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(struct timer_list *unused)
114 struct idt77105_priv *walk;
115 struct atm_dev *dev;
116 unsigned char istat;
118 DPRINTK("IDT77105 checking for cable re-insertion\n");
119 for (walk = idt77105_all; walk; walk = walk->next) {
120 dev = walk->dev;
122 if (dev->signal != ATM_PHY_SIG_LOST)
123 continue;
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)
143 unsigned long flags;
144 struct idt77105_stats stats;
146 spin_lock_irqsave(&idt77105_priv_lock, flags);
147 memcpy(&stats, &PRIV(dev)->stats, sizeof(struct idt77105_stats));
148 if (zero)
149 memset(&PRIV(dev)->stats, 0, sizeof(struct idt77105_stats));
150 spin_unlock_irqrestore(&idt77105_priv_lock, flags);
151 if (arg == NULL)
152 return 0;
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)
160 int diag;
162 diag = GET(DIAG) & ~IDT77105_DIAG_LCMASK;
163 switch (mode) {
164 case ATM_LM_NONE:
165 break;
166 case ATM_LM_LOC_ATM:
167 diag |= IDT77105_DIAG_LC_PHY_LOOPBACK;
168 break;
169 case ATM_LM_RMT_ATM:
170 diag |= IDT77105_DIAG_LC_LINE_LOOPBACK;
171 break;
172 default:
173 return -EINVAL;
175 PUT(diag,DIAG);
176 printk(KERN_NOTICE "%s(%d) Loopback mode is: %s\n", dev->type,
177 dev->number,
178 (mode == ATM_LM_NONE ? "NONE" :
179 (mode == ATM_LM_LOC_ATM ? "DIAG (local)" :
180 (mode == IDT77105_DIAG_LC_LINE_LOOPBACK ? "LOOP (remote)" :
181 "unknown")))
183 PRIV(dev)->loop_mode = mode;
184 return 0;
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);
191 switch (cmd) {
192 case IDT77105_GETSTATZ:
193 if (!capable(CAP_NET_ADMIN)) return -EPERM;
194 /* fall through */
195 case IDT77105_GETSTAT:
196 return fetch_stats(dev, arg, cmd == IDT77105_GETSTATZ);
197 case ATM_SETLOOP:
198 return set_loopback(dev,(int)(unsigned long) arg);
199 case ATM_GETLOOP:
200 return put_user(PRIV(dev)->loop_mode,(int __user *)arg) ?
201 -EFAULT : 0;
202 case ATM_QUERYLOOP:
203 return put_user(ATM_LM_LOC_ATM | ATM_LM_RMT_ATM,
204 (int __user *) arg) ? -EFAULT : 0;
205 default:
206 return -ENOIOCTLCMD;
212 static void idt77105_int(struct atm_dev *dev)
214 unsigned char istat;
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);
231 PUT(
232 (PRIV(dev)->old_mcr|
233 IDT77105_MCR_DREC|
234 IDT77105_MCR_DRIC|
235 IDT77105_MCR_HALTTX
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);
249 #ifdef GENERAL_DEBUG
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);
256 #endif
260 static int idt77105_start(struct atm_dev *dev)
262 unsigned long flags;
264 if (!(dev->dev_data = kmalloc(sizeof(struct idt77105_priv),GFP_KERNEL)))
265 return -ENOMEM;
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,
279 dev->number);
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;
285 break;
286 case IDT77105_DIAG_LC_PHY_LOOPBACK:
287 PRIV(dev)->loop_mode = ATM_LM_LOC_ATM;
288 break;
289 case IDT77105_DIAG_LC_LINE_LOOPBACK:
290 PRIV(dev)->loop_mode = ATM_LM_RMT_ATM;
291 break;
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);
306 if (start_timer) {
307 start_timer = 0;
309 stats_timer.expires = jiffies+IDT77105_STATS_TIMER_PERIOD;
310 add_timer(&stats_timer);
312 restart_timer.expires = jiffies+IDT77105_RESTART_TIMER_PERIOD;
313 add_timer(&restart_timer);
315 spin_unlock_irqrestore(&idt77105_priv_lock, flags);
316 return 0;
320 static int idt77105_stop(struct atm_dev *dev)
322 struct idt77105_priv *walk, *prev;
324 DPRINTK("%s(itf %d): stopping IDT77105\n",dev->type,dev->number);
326 /* disable interrupts */
327 PUT( GET(MCR) & ~IDT77105_MCR_EIP, MCR );
329 /* detach private struct from atm_dev & free */
330 for (prev = NULL, walk = idt77105_all ;
331 walk != NULL;
332 prev = walk, walk = walk->next) {
333 if (walk->dev == dev) {
334 if (prev != NULL)
335 prev->next = walk->next;
336 else
337 idt77105_all = walk->next;
338 dev->phy = NULL;
339 dev->dev_data = NULL;
340 kfree(walk);
341 break;
345 return 0;
349 static const struct atmphy_ops idt77105_ops = {
350 .start = idt77105_start,
351 .ioctl = idt77105_ioctl,
352 .interrupt = idt77105_int,
353 .stop = idt77105_stop,
357 int idt77105_init(struct atm_dev *dev)
359 dev->phy = &idt77105_ops;
360 return 0;
363 EXPORT_SYMBOL(idt77105_init);
365 static void __exit idt77105_exit(void)
367 /* turn off timers */
368 del_timer_sync(&stats_timer);
369 del_timer_sync(&restart_timer);
372 module_exit(idt77105_exit);
374 MODULE_LICENSE("GPL");