1 /* drivers/atm/uPD98402.c - NEC uPD98402 (PHY) declarations */
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
6 #include <linux/module.h>
8 #include <linux/errno.h>
9 #include <linux/atmdev.h>
10 #include <linux/sonet.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <asm/uaccess.h>
14 #include <linux/atomic.h>
20 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
22 #define DPRINTK(format,args...)
26 struct uPD98402_priv {
27 struct k_sonet_stats sonet_stats;/* link diagnostics */
28 unsigned char framing; /* SONET/SDH framing */
29 int loop_mode; /* loopback mode */
34 #define PRIV(dev) ((struct uPD98402_priv *) dev->phy_data)
36 #define PUT(val,reg) dev->ops->phy_put(dev,val,uPD98402_##reg)
37 #define GET(reg) dev->ops->phy_get(dev,uPD98402_##reg)
40 static int fetch_stats(struct atm_dev *dev,struct sonet_stats __user *arg,int zero)
42 struct sonet_stats tmp;
45 atomic_add(GET(HECCT),&PRIV(dev)->sonet_stats.uncorr_hcs);
46 sonet_copy_stats(&PRIV(dev)->sonet_stats,&tmp);
47 if (arg) error = copy_to_user(arg,&tmp,sizeof(tmp));
49 /* unused fields are reported as -1, but we must not "adjust"
51 tmp.corr_hcs = tmp.tx_cells = tmp.rx_cells = 0;
52 sonet_subtract_stats(&PRIV(dev)->sonet_stats,&tmp);
54 return error ? -EFAULT : 0;
58 static int set_framing(struct atm_dev *dev,unsigned char framing)
60 static const unsigned char sonet[] = { 1,2,3,0 };
61 static const unsigned char sdh[] = { 1,0,0,2 };
66 case SONET_FRAME_SONET:
75 spin_lock_irqsave(&PRIV(dev)->lock, flags);
79 PUT((GET(MDR) & ~uPD98402_MDR_SS_MASK) | (set[3] <<
80 uPD98402_MDR_SS_SHIFT),MDR);
81 spin_unlock_irqrestore(&PRIV(dev)->lock, flags);
86 static int get_sense(struct atm_dev *dev,u8 __user *arg)
91 spin_lock_irqsave(&PRIV(dev)->lock, flags);
95 spin_unlock_irqrestore(&PRIV(dev)->lock, flags);
96 return (put_user(s[0], arg) || put_user(s[1], arg+1) ||
97 put_user(s[2], arg+2) || put_user(0xff, arg+3) ||
98 put_user(0xff, arg+4) || put_user(0xff, arg+5)) ? -EFAULT : 0;
102 static int set_loopback(struct atm_dev *dev,int mode)
104 unsigned char mode_reg;
106 mode_reg = GET(MDR) & ~(uPD98402_MDR_TPLP | uPD98402_MDR_ALP |
108 switch (__ATM_LM_XTLOC(mode)) {
112 mode_reg |= uPD98402_MDR_TPLP;
115 mode_reg |= uPD98402_MDR_ALP;
120 switch (__ATM_LM_XTRMT(mode)) {
124 mode_reg |= uPD98402_MDR_RPLP;
130 PRIV(dev)->loop_mode = mode;
135 static int uPD98402_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg)
141 return fetch_stats(dev,arg, cmd == SONET_GETSTATZ);
142 case SONET_SETFRAMING:
143 return set_framing(dev, (int)(unsigned long)arg);
144 case SONET_GETFRAMING:
145 return put_user(PRIV(dev)->framing,(int __user *)arg) ?
147 case SONET_GETFRSENSE:
148 return get_sense(dev,arg);
150 return set_loopback(dev, (int)(unsigned long)arg);
152 return put_user(PRIV(dev)->loop_mode,(int __user *)arg) ?
155 return put_user(ATM_LM_LOC_PHY | ATM_LM_LOC_ATM |
156 ATM_LM_RMT_PHY,(int __user *)arg) ? -EFAULT : 0;
163 #define ADD_LIMITED(s,v) \
164 { atomic_add(GET(v),&PRIV(dev)->sonet_stats.s); \
165 if (atomic_read(&PRIV(dev)->sonet_stats.s) < 0) \
166 atomic_set(&PRIV(dev)->sonet_stats.s,INT_MAX); }
169 static void stat_event(struct atm_dev *dev)
171 unsigned char events;
174 if (events & uPD98402_PFM_PFEB) ADD_LIMITED(path_febe,PFECB);
175 if (events & uPD98402_PFM_LFEB) ADD_LIMITED(line_febe,LECCT);
176 if (events & uPD98402_PFM_B3E) ADD_LIMITED(path_bip,B3ECT);
177 if (events & uPD98402_PFM_B2E) ADD_LIMITED(line_bip,B2ECT);
178 if (events & uPD98402_PFM_B1E) ADD_LIMITED(section_bip,B1ECT);
185 static void uPD98402_int(struct atm_dev *dev)
187 static unsigned long silence = 0;
188 unsigned char reason;
190 while ((reason = GET(PICR))) {
191 if (reason & uPD98402_INT_LOS)
192 printk(KERN_NOTICE "%s(itf %d): signal lost\n",
193 dev->type,dev->number);
194 if (reason & uPD98402_INT_PFM) stat_event(dev);
195 if (reason & uPD98402_INT_PCO) {
196 (void) GET(PCOCR); /* clear interrupt cause */
197 atomic_add(GET(HECCT),
198 &PRIV(dev)->sonet_stats.uncorr_hcs);
200 if ((reason & uPD98402_INT_RFO) &&
201 (time_after(jiffies, silence) || silence == 0)) {
202 printk(KERN_WARNING "%s(itf %d): uPD98402 receive "
203 "FIFO overflow\n",dev->type,dev->number);
204 silence = (jiffies+HZ/2)|1;
210 static int uPD98402_start(struct atm_dev *dev)
212 DPRINTK("phy_start\n");
213 if (!(dev->dev_data = kmalloc(sizeof(struct uPD98402_priv),GFP_KERNEL)))
215 spin_lock_init(&PRIV(dev)->lock);
216 memset(&PRIV(dev)->sonet_stats,0,sizeof(struct k_sonet_stats));
217 (void) GET(PCR); /* clear performance events */
218 PUT(uPD98402_PFM_FJ,PCMR); /* ignore frequency adj */
219 (void) GET(PCOCR); /* clear overflows */
220 PUT(~uPD98402_PCO_HECC,PCOMR);
221 (void) GET(PICR); /* clear interrupts */
222 PUT(~(uPD98402_INT_PFM | uPD98402_INT_ALM | uPD98402_INT_RFO |
223 uPD98402_INT_LOS),PIMR); /* enable them */
224 (void) fetch_stats(dev,NULL,1); /* clear kernel counters */
225 atomic_set(&PRIV(dev)->sonet_stats.corr_hcs,-1);
226 atomic_set(&PRIV(dev)->sonet_stats.tx_cells,-1);
227 atomic_set(&PRIV(dev)->sonet_stats.rx_cells,-1);
232 static int uPD98402_stop(struct atm_dev *dev)
234 /* let SAR driver worry about stopping interrupts */
240 static const struct atmphy_ops uPD98402_ops = {
241 .start = uPD98402_start,
242 .ioctl = uPD98402_ioctl,
243 .interrupt = uPD98402_int,
244 .stop = uPD98402_stop,
248 int uPD98402_init(struct atm_dev *dev)
250 DPRINTK("phy_init\n");
251 dev->phy = &uPD98402_ops;
256 MODULE_LICENSE("GPL");
258 EXPORT_SYMBOL(uPD98402_init);
260 static __init int uPD98402_module_init(void)
264 module_init(uPD98402_module_init);
265 /* module_exit not defined so not unloadable */