1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/atm/suni.c - S/UNI PHY driver
5 * Supports the following:
6 * PMC PM5346 S/UNI LITE
7 * PMC PM5350 S/UNI 155 ULTRA
11 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
13 #include <linux/module.h>
14 #include <linux/jiffies.h>
15 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/atmdev.h>
19 #include <linux/sonet.h>
20 #include <linux/delay.h>
21 #include <linux/timer.h>
22 #include <linux/init.h>
23 #include <linux/capability.h>
24 #include <linux/atm_suni.h>
25 #include <linux/slab.h>
26 #include <asm/param.h>
27 #include <linux/uaccess.h>
28 #include <linux/atomic.h>
34 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
36 #define DPRINTK(format,args...)
39 #define PRIV(dev) ((struct suni_priv *) dev->phy_data)
41 #define PUT(val,reg) dev->ops->phy_put(dev,val,SUNI_##reg)
42 #define GET(reg) dev->ops->phy_get(dev,SUNI_##reg)
43 #define REG_CHANGE(mask,shift,value,reg) \
44 PUT((GET(reg) & ~(mask)) | ((value) << (shift)),reg)
47 static struct timer_list poll_timer
;
48 static struct suni_priv
*sunis
= NULL
;
49 static DEFINE_SPINLOCK(sunis_lock
);
52 #define ADD_LIMITED(s,v) \
53 atomic_add((v),&stats->s); \
54 if (atomic_read(&stats->s) < 0) atomic_set(&stats->s,INT_MAX);
57 static void suni_hz(struct timer_list
*timer
)
59 struct suni_priv
*walk
;
61 struct k_sonet_stats
*stats
;
63 for (walk
= sunis
; walk
; walk
= walk
->next
) {
65 stats
= &walk
->sonet_stats
;
66 PUT(0,MRI
); /* latch counters */
68 ADD_LIMITED(section_bip
,(GET(RSOP_SBL
) & 0xff) |
69 ((GET(RSOP_SBM
) & 0xff) << 8));
70 ADD_LIMITED(line_bip
,(GET(RLOP_LBL
) & 0xff) |
71 ((GET(RLOP_LB
) & 0xff) << 8) |
72 ((GET(RLOP_LBM
) & 0xf) << 16));
73 ADD_LIMITED(path_bip
,(GET(RPOP_PBL
) & 0xff) |
74 ((GET(RPOP_PBM
) & 0xff) << 8));
75 ADD_LIMITED(line_febe
,(GET(RLOP_LFL
) & 0xff) |
76 ((GET(RLOP_LF
) & 0xff) << 8) |
77 ((GET(RLOP_LFM
) & 0xf) << 16));
78 ADD_LIMITED(path_febe
,(GET(RPOP_PFL
) & 0xff) |
79 ((GET(RPOP_PFM
) & 0xff) << 8));
80 ADD_LIMITED(corr_hcs
,GET(RACP_CHEC
) & 0xff);
81 ADD_LIMITED(uncorr_hcs
,GET(RACP_UHEC
) & 0xff);
82 ADD_LIMITED(rx_cells
,(GET(RACP_RCCL
) & 0xff) |
83 ((GET(RACP_RCC
) & 0xff) << 8) |
84 ((GET(RACP_RCCM
) & 7) << 16));
85 ADD_LIMITED(tx_cells
,(GET(TACP_TCCL
) & 0xff) |
86 ((GET(TACP_TCC
) & 0xff) << 8) |
87 ((GET(TACP_TCCM
) & 7) << 16));
89 if (timer
) mod_timer(&poll_timer
,jiffies
+HZ
);
96 static int fetch_stats(struct atm_dev
*dev
,struct sonet_stats __user
*arg
,int zero
)
98 struct sonet_stats tmp
;
101 sonet_copy_stats(&PRIV(dev
)->sonet_stats
,&tmp
);
102 if (arg
) error
= copy_to_user(arg
,&tmp
,sizeof(tmp
));
103 if (zero
&& !error
) sonet_subtract_stats(&PRIV(dev
)->sonet_stats
,&tmp
);
104 return error
? -EFAULT
: 0;
108 #define HANDLE_FLAG(flag,reg,bit) \
110 if (set) PUT(GET(reg) | bit,reg); \
111 else PUT(GET(reg) & ~bit,reg); \
116 static int change_diag(struct atm_dev
*dev
,void __user
*arg
,int set
)
120 if (get_user(todo
,(int __user
*)arg
)) return -EFAULT
;
121 HANDLE_FLAG(SONET_INS_SBIP
,TSOP_DIAG
,SUNI_TSOP_DIAG_DBIP8
);
122 HANDLE_FLAG(SONET_INS_LBIP
,TLOP_DIAG
,SUNI_TLOP_DIAG_DBIP
);
123 HANDLE_FLAG(SONET_INS_PBIP
,TPOP_CD
,SUNI_TPOP_DIAG_DB3
);
124 HANDLE_FLAG(SONET_INS_FRAME
,RSOP_CIE
,SUNI_RSOP_CIE_FOOF
);
125 HANDLE_FLAG(SONET_INS_LAIS
,TSOP_CTRL
,SUNI_TSOP_CTRL_LAIS
);
126 HANDLE_FLAG(SONET_INS_PAIS
,TPOP_CD
,SUNI_TPOP_DIAG_PAIS
);
127 HANDLE_FLAG(SONET_INS_LOS
,TSOP_DIAG
,SUNI_TSOP_DIAG_DLOS
);
128 HANDLE_FLAG(SONET_INS_HCS
,TACP_CS
,SUNI_TACP_CS_DHCS
);
129 return put_user(todo
,(int __user
*)arg
) ? -EFAULT
: 0;
136 static int get_diag(struct atm_dev
*dev
,void __user
*arg
)
141 if (GET(TSOP_DIAG
) & SUNI_TSOP_DIAG_DBIP8
) set
|= SONET_INS_SBIP
;
142 if (GET(TLOP_DIAG
) & SUNI_TLOP_DIAG_DBIP
) set
|= SONET_INS_LBIP
;
143 if (GET(TPOP_CD
) & SUNI_TPOP_DIAG_DB3
) set
|= SONET_INS_PBIP
;
144 /* SONET_INS_FRAME is one-shot only */
145 if (GET(TSOP_CTRL
) & SUNI_TSOP_CTRL_LAIS
) set
|= SONET_INS_LAIS
;
146 if (GET(TPOP_CD
) & SUNI_TPOP_DIAG_PAIS
) set
|= SONET_INS_PAIS
;
147 if (GET(TSOP_DIAG
) & SUNI_TSOP_DIAG_DLOS
) set
|= SONET_INS_LOS
;
148 if (GET(TACP_CS
) & SUNI_TACP_CS_DHCS
) set
|= SONET_INS_HCS
;
149 return put_user(set
,(int __user
*)arg
) ? -EFAULT
: 0;
153 static int set_loopback(struct atm_dev
*dev
,int mode
)
155 unsigned char control
;
158 if (PRIV(dev
)->type
== SUNI_MRI_TYPE_PM5355
) {
168 control
= dev
->ops
->phy_get(dev
, reg
) & ~(dle
| lle
);
181 dev
->ops
->phy_put(dev
, control
, reg
);
182 PRIV(dev
)->loop_mode
= mode
;
187 * SONET vs. SDH Configuration
189 * Z0INS (register 0x06): 0 for SONET, 1 for SDH
190 * ENSS (register 0x3D): 0 for SONET, 1 for SDH
191 * LEN16 (register 0x28): 0 for SONET, 1 for SDH (n/a for S/UNI 155 QUAD)
192 * LEN16 (register 0x50): 0 for SONET, 1 for SDH (n/a for S/UNI 155 QUAD)
193 * S[1:0] (register 0x46): 00 for SONET, 10 for SDH
196 static int set_sonet(struct atm_dev
*dev
)
198 if (PRIV(dev
)->type
== SUNI_MRI_TYPE_PM5355
) {
199 PUT(GET(RPOP_RC
) & ~SUNI_RPOP_RC_ENSS
, RPOP_RC
);
200 PUT(GET(SSTB_CTRL
) & ~SUNI_SSTB_CTRL_LEN16
, SSTB_CTRL
);
201 PUT(GET(SPTB_CTRL
) & ~SUNI_SPTB_CTRL_LEN16
, SPTB_CTRL
);
204 REG_CHANGE(SUNI_TPOP_APM_S
, SUNI_TPOP_APM_S_SHIFT
,
205 SUNI_TPOP_S_SONET
, TPOP_APM
);
210 static int set_sdh(struct atm_dev
*dev
)
212 if (PRIV(dev
)->type
== SUNI_MRI_TYPE_PM5355
) {
213 PUT(GET(RPOP_RC
) | SUNI_RPOP_RC_ENSS
, RPOP_RC
);
214 PUT(GET(SSTB_CTRL
) | SUNI_SSTB_CTRL_LEN16
, SSTB_CTRL
);
215 PUT(GET(SPTB_CTRL
) | SUNI_SPTB_CTRL_LEN16
, SPTB_CTRL
);
218 REG_CHANGE(SUNI_TPOP_APM_S
, SUNI_TPOP_APM_S_SHIFT
,
219 SUNI_TPOP_S_SDH
, TPOP_APM
);
225 static int get_framing(struct atm_dev
*dev
, void __user
*arg
)
231 s
= (GET(TPOP_APM
) & SUNI_TPOP_APM_S
) >> SUNI_TPOP_APM_S_SHIFT
;
232 if (s
== SUNI_TPOP_S_SONET
)
233 framing
= SONET_FRAME_SONET
;
235 framing
= SONET_FRAME_SDH
;
237 return put_user(framing
, (int __user
*) arg
) ? -EFAULT
: 0;
240 static int set_framing(struct atm_dev
*dev
, void __user
*arg
)
244 if (get_user(mode
, (int __user
*) arg
))
247 if (mode
== SONET_FRAME_SONET
)
248 return set_sonet(dev
);
249 else if (mode
== SONET_FRAME_SDH
)
256 static int suni_ioctl(struct atm_dev
*dev
,unsigned int cmd
,void __user
*arg
)
261 return fetch_stats(dev
, arg
, cmd
== SONET_GETSTATZ
);
263 return change_diag(dev
,arg
,1);
265 return change_diag(dev
,arg
,0);
267 return get_diag(dev
,arg
);
268 case SONET_SETFRAMING
:
269 if (!capable(CAP_NET_ADMIN
))
271 return set_framing(dev
, arg
);
272 case SONET_GETFRAMING
:
273 return get_framing(dev
, arg
);
274 case SONET_GETFRSENSE
:
277 if (!capable(CAP_NET_ADMIN
))
279 return set_loopback(dev
,(int)(unsigned long)arg
);
281 return put_user(PRIV(dev
)->loop_mode
,(int __user
*)arg
) ?
284 return put_user(ATM_LM_LOC_PHY
| ATM_LM_RMT_PHY
,
285 (int __user
*) arg
) ? -EFAULT
: 0;
292 static void poll_los(struct atm_dev
*dev
)
294 atm_dev_signal_change(dev
,
295 GET(RSOP_SIS
) & SUNI_RSOP_SIS_LOSV
?
296 ATM_PHY_SIG_LOST
: ATM_PHY_SIG_FOUND
);
300 static void suni_int(struct atm_dev
*dev
)
303 printk(KERN_NOTICE
"%s(itf %d): signal %s\n",dev
->type
,dev
->number
,
304 dev
->signal
== ATM_PHY_SIG_LOST
? "lost" : "detected again");
308 static int suni_start(struct atm_dev
*dev
)
313 spin_lock_irqsave(&sunis_lock
,flags
);
315 PRIV(dev
)->next
= sunis
;
317 spin_unlock_irqrestore(&sunis_lock
,flags
);
318 memset(&PRIV(dev
)->sonet_stats
,0,sizeof(struct k_sonet_stats
));
319 PUT(GET(RSOP_CIE
) | SUNI_RSOP_CIE_LOSE
,RSOP_CIE
);
320 /* interrupt on loss of signal */
321 poll_los(dev
); /* ... and clear SUNI interrupts */
322 if (dev
->signal
== ATM_PHY_SIG_LOST
)
323 printk(KERN_WARNING
"%s(itf %d): no signal\n",dev
->type
,
325 PRIV(dev
)->loop_mode
= ATM_LM_NONE
;
326 suni_hz(NULL
); /* clear SUNI counters */
327 (void) fetch_stats(dev
,NULL
,1); /* clear kernel counters */
329 timer_setup(&poll_timer
, suni_hz
, 0);
330 poll_timer
.expires
= jiffies
+HZ
;
332 printk(KERN_DEBUG
"[u] p=0x%lx,n=0x%lx\n",(unsigned long) poll_timer
.list
.prev
,
333 (unsigned long) poll_timer
.list
.next
);
335 add_timer(&poll_timer
);
341 static int suni_stop(struct atm_dev
*dev
)
343 struct suni_priv
**walk
;
346 /* let SAR driver worry about stopping interrupts */
347 spin_lock_irqsave(&sunis_lock
,flags
);
348 for (walk
= &sunis
; *walk
!= PRIV(dev
);
349 walk
= &PRIV((*walk
)->dev
)->next
);
350 *walk
= PRIV((*walk
)->dev
)->next
;
351 if (!sunis
) del_timer_sync(&poll_timer
);
352 spin_unlock_irqrestore(&sunis_lock
,flags
);
359 static const struct atmphy_ops suni_ops
= {
362 .interrupt
= suni_int
,
367 int suni_init(struct atm_dev
*dev
)
371 if (!(dev
->phy_data
= kmalloc(sizeof(struct suni_priv
),GFP_KERNEL
)))
373 PRIV(dev
)->dev
= dev
;
375 mri
= GET(MRI
); /* reset SUNI */
376 PRIV(dev
)->type
= (mri
& SUNI_MRI_TYPE
) >> SUNI_MRI_TYPE_SHIFT
;
377 PUT(mri
| SUNI_MRI_RESET
,MRI
);
379 PUT((GET(MT
) & SUNI_MT_DS27_53
),MT
); /* disable all tests */
381 REG_CHANGE(SUNI_TACP_IUCHP_CLP
,0,SUNI_TACP_IUCHP_CLP
,
382 TACP_IUCHP
); /* idle cells */
383 PUT(SUNI_IDLE_PATTERN
,TACP_IUCPOP
);
384 dev
->phy
= &suni_ops
;
389 EXPORT_SYMBOL(suni_init
);
391 MODULE_LICENSE("GPL");