Add linux-next specific files for 20110728
[linux-2.6/next.git] / drivers / atm / suni.c
blob90f1ccca9e5215824ab531da5b8eaa1d662015c2
1 /*
2 * drivers/atm/suni.c - S/UNI PHY driver
4 * Supports the following:
5 * PMC PM5346 S/UNI LITE
6 * PMC PM5350 S/UNI 155 ULTRA
7 * PMC PM5355 S/UNI 622
8 */
10 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
12 #include <linux/module.h>
13 #include <linux/jiffies.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/errno.h>
17 #include <linux/atmdev.h>
18 #include <linux/sonet.h>
19 #include <linux/delay.h>
20 #include <linux/timer.h>
21 #include <linux/init.h>
22 #include <linux/capability.h>
23 #include <linux/atm_suni.h>
24 #include <linux/slab.h>
25 #include <asm/system.h>
26 #include <asm/param.h>
27 #include <asm/uaccess.h>
28 #include <linux/atomic.h>
30 #include "suni.h"
33 #if 0
34 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
35 #else
36 #define DPRINTK(format,args...)
37 #endif
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(unsigned long from_timer)
59 struct suni_priv *walk;
60 struct atm_dev *dev;
61 struct k_sonet_stats *stats;
63 for (walk = sunis; walk; walk = walk->next) {
64 dev = walk->dev;
65 stats = &walk->sonet_stats;
66 PUT(0,MRI); /* latch counters */
67 udelay(1);
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 (from_timer) mod_timer(&poll_timer,jiffies+HZ);
93 #undef ADD_LIMITED
96 static int fetch_stats(struct atm_dev *dev,struct sonet_stats __user *arg,int zero)
98 struct sonet_stats tmp;
99 int error = 0;
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) \
109 if (todo & flag) { \
110 if (set) PUT(GET(reg) | bit,reg); \
111 else PUT(GET(reg) & ~bit,reg); \
112 todo &= ~flag; \
116 static int change_diag(struct atm_dev *dev,void __user *arg,int set)
118 int todo;
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;
133 #undef HANDLE_FLAG
136 static int get_diag(struct atm_dev *dev,void __user *arg)
138 int set;
140 set = 0;
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;
156 int reg, dle, lle;
158 if (PRIV(dev)->type == SUNI_MRI_TYPE_PM5355) {
159 reg = SUNI_MCM;
160 dle = SUNI_MCM_DLE;
161 lle = SUNI_MCM_LLE;
162 } else {
163 reg = SUNI_MCT;
164 dle = SUNI_MCT_DLE;
165 lle = SUNI_MCT_LLE;
168 control = dev->ops->phy_get(dev, reg) & ~(dle | lle);
169 switch (mode) {
170 case ATM_LM_NONE:
171 break;
172 case ATM_LM_LOC_PHY:
173 control |= dle;
174 break;
175 case ATM_LM_RMT_PHY:
176 control |= lle;
177 break;
178 default:
179 return -EINVAL;
181 dev->ops->phy_put(dev, control, reg);
182 PRIV(dev)->loop_mode = mode;
183 return 0;
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);
207 return 0;
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);
221 return 0;
225 static int get_framing(struct atm_dev *dev, void __user *arg)
227 int framing;
228 unsigned char s;
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;
234 else
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)
242 int mode;
244 if (get_user(mode, (int __user *) arg))
245 return -EFAULT;
247 if (mode == SONET_FRAME_SONET)
248 return set_sonet(dev);
249 else if (mode == SONET_FRAME_SDH)
250 return set_sdh(dev);
252 return -EINVAL;
256 static int suni_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg)
258 switch (cmd) {
259 case SONET_GETSTATZ:
260 case SONET_GETSTAT:
261 return fetch_stats(dev, arg, cmd == SONET_GETSTATZ);
262 case SONET_SETDIAG:
263 return change_diag(dev,arg,1);
264 case SONET_CLRDIAG:
265 return change_diag(dev,arg,0);
266 case SONET_GETDIAG:
267 return get_diag(dev,arg);
268 case SONET_SETFRAMING:
269 if (!capable(CAP_NET_ADMIN))
270 return -EPERM;
271 return set_framing(dev, arg);
272 case SONET_GETFRAMING:
273 return get_framing(dev, arg);
274 case SONET_GETFRSENSE:
275 return -EINVAL;
276 case ATM_SETLOOP:
277 if (!capable(CAP_NET_ADMIN))
278 return -EPERM;
279 return set_loopback(dev,(int)(unsigned long)arg);
280 case ATM_GETLOOP:
281 return put_user(PRIV(dev)->loop_mode,(int __user *)arg) ?
282 -EFAULT : 0;
283 case ATM_QUERYLOOP:
284 return put_user(ATM_LM_LOC_PHY | ATM_LM_RMT_PHY,
285 (int __user *) arg) ? -EFAULT : 0;
286 default:
287 return -ENOIOCTLCMD;
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)
302 poll_los(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)
310 unsigned long flags;
311 int first;
313 spin_lock_irqsave(&sunis_lock,flags);
314 first = !sunis;
315 PRIV(dev)->next = sunis;
316 sunis = PRIV(dev);
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,
324 dev->number);
325 PRIV(dev)->loop_mode = ATM_LM_NONE;
326 suni_hz(0); /* clear SUNI counters */
327 (void) fetch_stats(dev,NULL,1); /* clear kernel counters */
328 if (first) {
329 init_timer(&poll_timer);
330 poll_timer.expires = jiffies+HZ;
331 poll_timer.function = suni_hz;
332 poll_timer.data = 1;
333 #if 0
334 printk(KERN_DEBUG "[u] p=0x%lx,n=0x%lx\n",(unsigned long) poll_timer.list.prev,
335 (unsigned long) poll_timer.list.next);
336 #endif
337 add_timer(&poll_timer);
339 return 0;
343 static int suni_stop(struct atm_dev *dev)
345 struct suni_priv **walk;
346 unsigned long flags;
348 /* let SAR driver worry about stopping interrupts */
349 spin_lock_irqsave(&sunis_lock,flags);
350 for (walk = &sunis; *walk != PRIV(dev);
351 walk = &PRIV((*walk)->dev)->next);
352 *walk = PRIV((*walk)->dev)->next;
353 if (!sunis) del_timer_sync(&poll_timer);
354 spin_unlock_irqrestore(&sunis_lock,flags);
355 kfree(PRIV(dev));
357 return 0;
361 static const struct atmphy_ops suni_ops = {
362 .start = suni_start,
363 .ioctl = suni_ioctl,
364 .interrupt = suni_int,
365 .stop = suni_stop,
369 int suni_init(struct atm_dev *dev)
371 unsigned char mri;
373 if (!(dev->phy_data = kmalloc(sizeof(struct suni_priv),GFP_KERNEL)))
374 return -ENOMEM;
375 PRIV(dev)->dev = dev;
377 mri = GET(MRI); /* reset SUNI */
378 PRIV(dev)->type = (mri & SUNI_MRI_TYPE) >> SUNI_MRI_TYPE_SHIFT;
379 PUT(mri | SUNI_MRI_RESET,MRI);
380 PUT(mri,MRI);
381 PUT((GET(MT) & SUNI_MT_DS27_53),MT); /* disable all tests */
382 set_sonet(dev);
383 REG_CHANGE(SUNI_TACP_IUCHP_CLP,0,SUNI_TACP_IUCHP_CLP,
384 TACP_IUCHP); /* idle cells */
385 PUT(SUNI_IDLE_PATTERN,TACP_IUCPOP);
386 dev->phy = &suni_ops;
388 return 0;
391 EXPORT_SYMBOL(suni_init);
393 MODULE_LICENSE("GPL");