1 // SPDX-License-Identifier: GPL-2.0-only
2 /* drivers/atm/uPD98402.c - NEC uPD98402 (PHY) declarations */
4 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
7 #include <linux/module.h>
9 #include <linux/errno.h>
10 #include <linux/atmdev.h>
11 #include <linux/sonet.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/uaccess.h>
15 #include <linux/atomic.h>
21 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
23 #define DPRINTK(format,args...)
27 struct uPD98402_priv
{
28 struct k_sonet_stats sonet_stats
;/* link diagnostics */
29 unsigned char framing
; /* SONET/SDH framing */
30 int loop_mode
; /* loopback mode */
35 #define PRIV(dev) ((struct uPD98402_priv *) dev->phy_data)
37 #define PUT(val,reg) dev->ops->phy_put(dev,val,uPD98402_##reg)
38 #define GET(reg) dev->ops->phy_get(dev,uPD98402_##reg)
41 static int fetch_stats(struct atm_dev
*dev
,struct sonet_stats __user
*arg
,int zero
)
43 struct sonet_stats tmp
;
46 atomic_add(GET(HECCT
),&PRIV(dev
)->sonet_stats
.uncorr_hcs
);
47 sonet_copy_stats(&PRIV(dev
)->sonet_stats
,&tmp
);
48 if (arg
) error
= copy_to_user(arg
,&tmp
,sizeof(tmp
));
50 /* unused fields are reported as -1, but we must not "adjust"
52 tmp
.corr_hcs
= tmp
.tx_cells
= tmp
.rx_cells
= 0;
53 sonet_subtract_stats(&PRIV(dev
)->sonet_stats
,&tmp
);
55 return error
? -EFAULT
: 0;
59 static int set_framing(struct atm_dev
*dev
,unsigned char framing
)
61 static const unsigned char sonet
[] = { 1,2,3,0 };
62 static const unsigned char sdh
[] = { 1,0,0,2 };
67 case SONET_FRAME_SONET
:
76 spin_lock_irqsave(&PRIV(dev
)->lock
, flags
);
80 PUT((GET(MDR
) & ~uPD98402_MDR_SS_MASK
) | (set
[3] <<
81 uPD98402_MDR_SS_SHIFT
),MDR
);
82 spin_unlock_irqrestore(&PRIV(dev
)->lock
, flags
);
87 static int get_sense(struct atm_dev
*dev
,u8 __user
*arg
)
92 spin_lock_irqsave(&PRIV(dev
)->lock
, flags
);
96 spin_unlock_irqrestore(&PRIV(dev
)->lock
, flags
);
97 return (put_user(s
[0], arg
) || put_user(s
[1], arg
+1) ||
98 put_user(s
[2], arg
+2) || put_user(0xff, arg
+3) ||
99 put_user(0xff, arg
+4) || put_user(0xff, arg
+5)) ? -EFAULT
: 0;
103 static int set_loopback(struct atm_dev
*dev
,int mode
)
105 unsigned char mode_reg
;
107 mode_reg
= GET(MDR
) & ~(uPD98402_MDR_TPLP
| uPD98402_MDR_ALP
|
109 switch (__ATM_LM_XTLOC(mode
)) {
113 mode_reg
|= uPD98402_MDR_TPLP
;
116 mode_reg
|= uPD98402_MDR_ALP
;
121 switch (__ATM_LM_XTRMT(mode
)) {
125 mode_reg
|= uPD98402_MDR_RPLP
;
131 PRIV(dev
)->loop_mode
= mode
;
136 static int uPD98402_ioctl(struct atm_dev
*dev
,unsigned int cmd
,void __user
*arg
)
142 return fetch_stats(dev
,arg
, cmd
== SONET_GETSTATZ
);
143 case SONET_SETFRAMING
:
144 return set_framing(dev
, (int)(unsigned long)arg
);
145 case SONET_GETFRAMING
:
146 return put_user(PRIV(dev
)->framing
,(int __user
*)arg
) ?
148 case SONET_GETFRSENSE
:
149 return get_sense(dev
,arg
);
151 return set_loopback(dev
, (int)(unsigned long)arg
);
153 return put_user(PRIV(dev
)->loop_mode
,(int __user
*)arg
) ?
156 return put_user(ATM_LM_LOC_PHY
| ATM_LM_LOC_ATM
|
157 ATM_LM_RMT_PHY
,(int __user
*)arg
) ? -EFAULT
: 0;
164 #define ADD_LIMITED(s,v) \
165 { atomic_add(GET(v),&PRIV(dev)->sonet_stats.s); \
166 if (atomic_read(&PRIV(dev)->sonet_stats.s) < 0) \
167 atomic_set(&PRIV(dev)->sonet_stats.s,INT_MAX); }
170 static void stat_event(struct atm_dev
*dev
)
172 unsigned char events
;
175 if (events
& uPD98402_PFM_PFEB
) ADD_LIMITED(path_febe
,PFECB
);
176 if (events
& uPD98402_PFM_LFEB
) ADD_LIMITED(line_febe
,LECCT
);
177 if (events
& uPD98402_PFM_B3E
) ADD_LIMITED(path_bip
,B3ECT
);
178 if (events
& uPD98402_PFM_B2E
) ADD_LIMITED(line_bip
,B2ECT
);
179 if (events
& uPD98402_PFM_B1E
) ADD_LIMITED(section_bip
,B1ECT
);
186 static void uPD98402_int(struct atm_dev
*dev
)
188 static unsigned long silence
= 0;
189 unsigned char reason
;
191 while ((reason
= GET(PICR
))) {
192 if (reason
& uPD98402_INT_LOS
)
193 printk(KERN_NOTICE
"%s(itf %d): signal lost\n",
194 dev
->type
,dev
->number
);
195 if (reason
& uPD98402_INT_PFM
) stat_event(dev
);
196 if (reason
& uPD98402_INT_PCO
) {
197 (void) GET(PCOCR
); /* clear interrupt cause */
198 atomic_add(GET(HECCT
),
199 &PRIV(dev
)->sonet_stats
.uncorr_hcs
);
201 if ((reason
& uPD98402_INT_RFO
) &&
202 (time_after(jiffies
, silence
) || silence
== 0)) {
203 printk(KERN_WARNING
"%s(itf %d): uPD98402 receive "
204 "FIFO overflow\n",dev
->type
,dev
->number
);
205 silence
= (jiffies
+HZ
/2)|1;
211 static int uPD98402_start(struct atm_dev
*dev
)
213 DPRINTK("phy_start\n");
214 if (!(dev
->dev_data
= kmalloc(sizeof(struct uPD98402_priv
),GFP_KERNEL
)))
216 spin_lock_init(&PRIV(dev
)->lock
);
217 memset(&PRIV(dev
)->sonet_stats
,0,sizeof(struct k_sonet_stats
));
218 (void) GET(PCR
); /* clear performance events */
219 PUT(uPD98402_PFM_FJ
,PCMR
); /* ignore frequency adj */
220 (void) GET(PCOCR
); /* clear overflows */
221 PUT(~uPD98402_PCO_HECC
,PCOMR
);
222 (void) GET(PICR
); /* clear interrupts */
223 PUT(~(uPD98402_INT_PFM
| uPD98402_INT_ALM
| uPD98402_INT_RFO
|
224 uPD98402_INT_LOS
),PIMR
); /* enable them */
225 (void) fetch_stats(dev
,NULL
,1); /* clear kernel counters */
226 atomic_set(&PRIV(dev
)->sonet_stats
.corr_hcs
,-1);
227 atomic_set(&PRIV(dev
)->sonet_stats
.tx_cells
,-1);
228 atomic_set(&PRIV(dev
)->sonet_stats
.rx_cells
,-1);
233 static int uPD98402_stop(struct atm_dev
*dev
)
235 /* let SAR driver worry about stopping interrupts */
241 static const struct atmphy_ops uPD98402_ops
= {
242 .start
= uPD98402_start
,
243 .ioctl
= uPD98402_ioctl
,
244 .interrupt
= uPD98402_int
,
245 .stop
= uPD98402_stop
,
249 int uPD98402_init(struct atm_dev
*dev
)
251 DPRINTK("phy_init\n");
252 dev
->phy
= &uPD98402_ops
;
257 MODULE_LICENSE("GPL");
259 EXPORT_SYMBOL(uPD98402_init
);
261 static __init
int uPD98402_module_init(void)
265 module_init(uPD98402_module_init
);
266 /* module_exit not defined so not unloadable */