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 <asm/uaccess.h>
13 #include <asm/atomic.h>
19 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
21 #define DPRINTK(format,args...)
25 struct uPD98402_priv
{
26 struct k_sonet_stats sonet_stats
;/* link diagnostics */
27 unsigned char framing
; /* SONET/SDH framing */
28 int loop_mode
; /* loopback mode */
33 #define PRIV(dev) ((struct uPD98402_priv *) dev->phy_data)
35 #define PUT(val,reg) dev->ops->phy_put(dev,val,uPD98402_##reg)
36 #define GET(reg) dev->ops->phy_get(dev,uPD98402_##reg)
39 static int fetch_stats(struct atm_dev
*dev
,struct sonet_stats __user
*arg
,int zero
)
41 struct sonet_stats tmp
;
44 atomic_add(GET(HECCT
),&PRIV(dev
)->sonet_stats
.uncorr_hcs
);
45 sonet_copy_stats(&PRIV(dev
)->sonet_stats
,&tmp
);
46 if (arg
) error
= copy_to_user(arg
,&tmp
,sizeof(tmp
));
48 /* unused fields are reported as -1, but we must not "adjust"
50 tmp
.corr_hcs
= tmp
.tx_cells
= tmp
.rx_cells
= 0;
51 sonet_subtract_stats(&PRIV(dev
)->sonet_stats
,&tmp
);
53 return error
? -EFAULT
: 0;
57 static int set_framing(struct atm_dev
*dev
,unsigned char framing
)
59 static const unsigned char sonet
[] = { 1,2,3,0 };
60 static const unsigned char sdh
[] = { 1,0,0,2 };
65 case SONET_FRAME_SONET
:
74 spin_lock_irqsave(&PRIV(dev
)->lock
, flags
);
78 PUT((GET(MDR
) & ~uPD98402_MDR_SS_MASK
) | (set
[3] <<
79 uPD98402_MDR_SS_SHIFT
),MDR
);
80 spin_unlock_irqrestore(&PRIV(dev
)->lock
, flags
);
85 static int get_sense(struct atm_dev
*dev
,u8 __user
*arg
)
90 spin_lock_irqsave(&PRIV(dev
)->lock
, flags
);
94 spin_unlock_irqrestore(&PRIV(dev
)->lock
, flags
);
95 return (put_user(s
[0], arg
) || put_user(s
[1], arg
+1) ||
96 put_user(s
[2], arg
+2) || put_user(0xff, arg
+3) ||
97 put_user(0xff, arg
+4) || put_user(0xff, arg
+5)) ? -EFAULT
: 0;
101 static int set_loopback(struct atm_dev
*dev
,int mode
)
103 unsigned char mode_reg
;
105 mode_reg
= GET(MDR
) & ~(uPD98402_MDR_TPLP
| uPD98402_MDR_ALP
|
107 switch (__ATM_LM_XTLOC(mode
)) {
111 mode_reg
|= uPD98402_MDR_TPLP
;
114 mode_reg
|= uPD98402_MDR_ALP
;
119 switch (__ATM_LM_XTRMT(mode
)) {
123 mode_reg
|= uPD98402_MDR_RPLP
;
129 PRIV(dev
)->loop_mode
= mode
;
134 static int uPD98402_ioctl(struct atm_dev
*dev
,unsigned int cmd
,void __user
*arg
)
140 return fetch_stats(dev
,arg
, cmd
== SONET_GETSTATZ
);
141 case SONET_SETFRAMING
:
142 return set_framing(dev
, (int)(unsigned long)arg
);
143 case SONET_GETFRAMING
:
144 return put_user(PRIV(dev
)->framing
,(int __user
*)arg
) ?
146 case SONET_GETFRSENSE
:
147 return get_sense(dev
,arg
);
149 return set_loopback(dev
, (int)(unsigned long)arg
);
151 return put_user(PRIV(dev
)->loop_mode
,(int __user
*)arg
) ?
154 return put_user(ATM_LM_LOC_PHY
| ATM_LM_LOC_ATM
|
155 ATM_LM_RMT_PHY
,(int __user
*)arg
) ? -EFAULT
: 0;
162 #define ADD_LIMITED(s,v) \
163 { atomic_add(GET(v),&PRIV(dev)->sonet_stats.s); \
164 if (atomic_read(&PRIV(dev)->sonet_stats.s) < 0) \
165 atomic_set(&PRIV(dev)->sonet_stats.s,INT_MAX); }
168 static void stat_event(struct atm_dev
*dev
)
170 unsigned char events
;
173 if (events
& uPD98402_PFM_PFEB
) ADD_LIMITED(path_febe
,PFECB
);
174 if (events
& uPD98402_PFM_LFEB
) ADD_LIMITED(line_febe
,LECCT
);
175 if (events
& uPD98402_PFM_B3E
) ADD_LIMITED(path_bip
,B3ECT
);
176 if (events
& uPD98402_PFM_B2E
) ADD_LIMITED(line_bip
,B2ECT
);
177 if (events
& uPD98402_PFM_B1E
) ADD_LIMITED(section_bip
,B1ECT
);
184 static void uPD98402_int(struct atm_dev
*dev
)
186 static unsigned long silence
= 0;
187 unsigned char reason
;
189 while ((reason
= GET(PICR
))) {
190 if (reason
& uPD98402_INT_LOS
)
191 printk(KERN_NOTICE
"%s(itf %d): signal lost\n",
192 dev
->type
,dev
->number
);
193 if (reason
& uPD98402_INT_PFM
) stat_event(dev
);
194 if (reason
& uPD98402_INT_PCO
) {
195 (void) GET(PCOCR
); /* clear interrupt cause */
196 atomic_add(GET(HECCT
),
197 &PRIV(dev
)->sonet_stats
.uncorr_hcs
);
199 if ((reason
& uPD98402_INT_RFO
) &&
200 (time_after(jiffies
, silence
) || silence
== 0)) {
201 printk(KERN_WARNING
"%s(itf %d): uPD98402 receive "
202 "FIFO overflow\n",dev
->type
,dev
->number
);
203 silence
= (jiffies
+HZ
/2)|1;
209 static int uPD98402_start(struct atm_dev
*dev
)
211 DPRINTK("phy_start\n");
212 if (!(dev
->dev_data
= kmalloc(sizeof(struct uPD98402_priv
),GFP_KERNEL
)))
214 spin_lock_init(&PRIV(dev
)->lock
);
215 memset(&PRIV(dev
)->sonet_stats
,0,sizeof(struct k_sonet_stats
));
216 (void) GET(PCR
); /* clear performance events */
217 PUT(uPD98402_PFM_FJ
,PCMR
); /* ignore frequency adj */
218 (void) GET(PCOCR
); /* clear overflows */
219 PUT(~uPD98402_PCO_HECC
,PCOMR
);
220 (void) GET(PICR
); /* clear interrupts */
221 PUT(~(uPD98402_INT_PFM
| uPD98402_INT_ALM
| uPD98402_INT_RFO
|
222 uPD98402_INT_LOS
),PIMR
); /* enable them */
223 (void) fetch_stats(dev
,NULL
,1); /* clear kernel counters */
224 atomic_set(&PRIV(dev
)->sonet_stats
.corr_hcs
,-1);
225 atomic_set(&PRIV(dev
)->sonet_stats
.tx_cells
,-1);
226 atomic_set(&PRIV(dev
)->sonet_stats
.rx_cells
,-1);
231 static int uPD98402_stop(struct atm_dev
*dev
)
233 /* let SAR driver worry about stopping interrupts */
239 static const struct atmphy_ops uPD98402_ops
= {
240 .start
= uPD98402_start
,
241 .ioctl
= uPD98402_ioctl
,
242 .interrupt
= uPD98402_int
,
243 .stop
= uPD98402_stop
,
247 int uPD98402_init(struct atm_dev
*dev
)
249 DPRINTK("phy_init\n");
250 dev
->phy
= &uPD98402_ops
;
255 MODULE_LICENSE("GPL");
257 EXPORT_SYMBOL(uPD98402_init
);
259 static __init
int uPD98402_module_init(void)
263 module_init(uPD98402_module_init
);
264 /* module_exit not defined so not unloadable */