1 /* drivers/atm/suni.c - PMC PM5346 SUNI (PHY) driver */
2
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
5
6 #include <linux/module.h>
7 #include <linux/sched.h>
8 #include <linux/kernel.h>
9 #include <linux/mm.h>
10 #include <linux/errno.h>
11 #include <linux/atmdev.h>
12 #include <linux/sonet.h>
13 #include <linux/delay.h>
14 #include <linux/timer.h>
15 #include <linux/init.h>
16 #include <linux/capability.h>
17 #include <linux/atm_suni.h>
18 #include <asm/system.h>
19 #include <asm/param.h>
20 #include <asm/uaccess.h>
21 #include <asm/atomic.h>
22
23 #include "suni.h"
24
25
26 #if 0
27 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
28 #else
29 #define DPRINTK(format,args...)
30 #endif
31
32
33 struct suni_priv {
34 struct k_sonet_stats sonet_stats; /* link diagnostics */
35 int loop_mode; /* loopback mode */
36 struct atm_dev *dev; /* device back-pointer */
37 struct suni_priv *next; /* next SUNI */
38 };
39
40
41 #define PRIV(dev) ((struct suni_priv *) dev->phy_data)
42
43 #define PUT(val,reg) dev->ops->phy_put(dev,val,SUNI_##reg)
44 #define GET(reg) dev->ops->phy_get(dev,SUNI_##reg)
45 #define REG_CHANGE(mask,shift,value,reg) \
46 PUT((GET(reg) & ~(mask)) | ((value) << (shift)),reg)
47
48
49 static struct timer_list poll_timer;
50 static struct suni_priv *sunis = NULL;
51 static spinlock_t sunis_lock = SPIN_LOCK_UNLOCKED;
52
53
54 #define ADD_LIMITED(s,v) \
55 atomic_add((v),&stats->s); \
56 if (atomic_read(&stats->s) < 0) atomic_set(&stats->s,INT_MAX);
57
58
suni_hz(unsigned long from_timer)59 static void suni_hz(unsigned long from_timer)
60 {
61 struct suni_priv *walk;
62 struct atm_dev *dev;
63 struct k_sonet_stats *stats;
64
65 for (walk = sunis; walk; walk = walk->next) {
66 dev = walk->dev;
67 stats = &walk->sonet_stats;
68 PUT(0,MRI); /* latch counters */
69 udelay(1);
70 ADD_LIMITED(section_bip,(GET(RSOP_SBL) & 0xff) |
71 ((GET(RSOP_SBM) & 0xff) << 8));
72 ADD_LIMITED(line_bip,(GET(RLOP_LBL) & 0xff) |
73 ((GET(RLOP_LB) & 0xff) << 8) |
74 ((GET(RLOP_LBM) & 0xf) << 16));
75 ADD_LIMITED(path_bip,(GET(RPOP_PBL) & 0xff) |
76 ((GET(RPOP_PBM) & 0xff) << 8));
77 ADD_LIMITED(line_febe,(GET(RLOP_LFL) & 0xff) |
78 ((GET(RLOP_LF) & 0xff) << 8) |
79 ((GET(RLOP_LFM) & 0xf) << 16));
80 ADD_LIMITED(path_febe,(GET(RPOP_PFL) & 0xff) |
81 ((GET(RPOP_PFM) & 0xff) << 8));
82 ADD_LIMITED(corr_hcs,GET(RACP_CHEC) & 0xff);
83 ADD_LIMITED(uncorr_hcs,GET(RACP_UHEC) & 0xff);
84 ADD_LIMITED(rx_cells,(GET(RACP_RCCL) & 0xff) |
85 ((GET(RACP_RCC) & 0xff) << 8) |
86 ((GET(RACP_RCCM) & 7) << 16));
87 ADD_LIMITED(tx_cells,(GET(TACP_TCCL) & 0xff) |
88 ((GET(TACP_TCC) & 0xff) << 8) |
89 ((GET(TACP_TCCM) & 7) << 16));
90 }
91 if (from_timer) mod_timer(&poll_timer,jiffies+HZ);
92 }
93
94
95 #undef ADD_LIMITED
96
97
fetch_stats(struct atm_dev * dev,struct sonet_stats * arg,int zero)98 static int fetch_stats(struct atm_dev *dev,struct sonet_stats *arg,int zero)
99 {
100 struct sonet_stats tmp;
101 int error = 0;
102
103 sonet_copy_stats(&PRIV(dev)->sonet_stats,&tmp);
104 if (arg) error = copy_to_user(arg,&tmp,sizeof(tmp));
105 if (zero && !error) sonet_subtract_stats(&PRIV(dev)->sonet_stats,&tmp);
106 return error ? -EFAULT : 0;
107 }
108
109
110 #define HANDLE_FLAG(flag,reg,bit) \
111 if (todo & flag) { \
112 if (set) PUT(GET(reg) | bit,reg); \
113 else PUT(GET(reg) & ~bit,reg); \
114 todo &= ~flag; \
115 }
116
117
change_diag(struct atm_dev * dev,void * arg,int set)118 static int change_diag(struct atm_dev *dev,void *arg,int set)
119 {
120 int todo;
121
122 if (get_user(todo,(int *) arg)) return -EFAULT;
123 HANDLE_FLAG(SONET_INS_SBIP,TSOP_DIAG,SUNI_TSOP_DIAG_DBIP8);
124 HANDLE_FLAG(SONET_INS_LBIP,TLOP_DIAG,SUNI_TLOP_DIAG_DBIP);
125 HANDLE_FLAG(SONET_INS_PBIP,TPOP_CD,SUNI_TPOP_DIAG_DB3);
126 HANDLE_FLAG(SONET_INS_FRAME,RSOP_CIE,SUNI_RSOP_CIE_FOOF);
127 HANDLE_FLAG(SONET_INS_LAIS,TSOP_CTRL,SUNI_TSOP_CTRL_LAIS);
128 HANDLE_FLAG(SONET_INS_PAIS,TPOP_CD,SUNI_TPOP_DIAG_PAIS);
129 HANDLE_FLAG(SONET_INS_LOS,TSOP_DIAG,SUNI_TSOP_DIAG_DLOS);
130 HANDLE_FLAG(SONET_INS_HCS,TACP_CS,SUNI_TACP_CS_DHCS);
131 return put_user(todo,(int *) arg) ? -EFAULT : 0;
132 }
133
134
135 #undef HANDLE_FLAG
136
137
get_diag(struct atm_dev * dev,void * arg)138 static int get_diag(struct atm_dev *dev,void *arg)
139 {
140 int set;
141
142 set = 0;
143 if (GET(TSOP_DIAG) & SUNI_TSOP_DIAG_DBIP8) set |= SONET_INS_SBIP;
144 if (GET(TLOP_DIAG) & SUNI_TLOP_DIAG_DBIP) set |= SONET_INS_LBIP;
145 if (GET(TPOP_CD) & SUNI_TPOP_DIAG_DB3) set |= SONET_INS_PBIP;
146 /* SONET_INS_FRAME is one-shot only */
147 if (GET(TSOP_CTRL) & SUNI_TSOP_CTRL_LAIS) set |= SONET_INS_LAIS;
148 if (GET(TPOP_CD) & SUNI_TPOP_DIAG_PAIS) set |= SONET_INS_PAIS;
149 if (GET(TSOP_DIAG) & SUNI_TSOP_DIAG_DLOS) set |= SONET_INS_LOS;
150 if (GET(TACP_CS) & SUNI_TACP_CS_DHCS) set |= SONET_INS_HCS;
151 return put_user(set,(int *) arg) ? -EFAULT : 0;
152 }
153
154
set_loopback(struct atm_dev * dev,int mode)155 static int set_loopback(struct atm_dev *dev,int mode)
156 {
157 unsigned char control;
158
159 control = GET(MCT) & ~(SUNI_MCT_DLE | SUNI_MCT_LLE);
160 switch (mode) {
161 case ATM_LM_NONE:
162 break;
163 case ATM_LM_LOC_PHY:
164 control |= SUNI_MCT_DLE;
165 break;
166 case ATM_LM_RMT_PHY:
167 control |= SUNI_MCT_LLE;
168 break;
169 default:
170 return -EINVAL;
171 }
172 PUT(control,MCT);
173 PRIV(dev)->loop_mode = mode;
174 return 0;
175 }
176
177
suni_ioctl(struct atm_dev * dev,unsigned int cmd,void * arg)178 static int suni_ioctl(struct atm_dev *dev,unsigned int cmd,void *arg)
179 {
180 switch (cmd) {
181 case SONET_GETSTATZ:
182 case SONET_GETSTAT:
183 return fetch_stats(dev,(struct sonet_stats *) arg,
184 cmd == SONET_GETSTATZ);
185 case SONET_SETDIAG:
186 return change_diag(dev,arg,1);
187 case SONET_CLRDIAG:
188 return change_diag(dev,arg,0);
189 case SONET_GETDIAG:
190 return get_diag(dev,arg);
191 case SONET_SETFRAMING:
192 if (arg != SONET_FRAME_SONET) return -EINVAL;
193 return 0;
194 case SONET_GETFRAMING:
195 return put_user(SONET_FRAME_SONET,(int *) arg) ?
196 -EFAULT : 0;
197 case SONET_GETFRSENSE:
198 return -EINVAL;
199 case ATM_SETLOOP:
200 return set_loopback(dev,(int) (long) arg);
201 case ATM_GETLOOP:
202 return put_user(PRIV(dev)->loop_mode,(int *) arg) ?
203 -EFAULT : 0;
204 case ATM_QUERYLOOP:
205 return put_user(ATM_LM_LOC_PHY | ATM_LM_RMT_PHY,
206 (int *) arg) ? -EFAULT : 0;
207 default:
208 return -ENOIOCTLCMD;
209 }
210 }
211
212
poll_los(struct atm_dev * dev)213 static void poll_los(struct atm_dev *dev)
214 {
215 dev->signal = GET(RSOP_SIS) & SUNI_RSOP_SIS_LOSV ? ATM_PHY_SIG_LOST :
216 ATM_PHY_SIG_FOUND;
217 }
218
219
suni_int(struct atm_dev * dev)220 static void suni_int(struct atm_dev *dev)
221 {
222 poll_los(dev);
223 printk(KERN_NOTICE "%s(itf %d): signal %s\n",dev->type,dev->number,
224 dev->signal == ATM_PHY_SIG_LOST ? "lost" : "detected again");
225 }
226
227
suni_start(struct atm_dev * dev)228 static int suni_start(struct atm_dev *dev)
229 {
230 unsigned long flags;
231 int first;
232
233 if (!(dev->phy_data = kmalloc(sizeof(struct suni_priv),GFP_KERNEL)))
234 return -ENOMEM;
235
236 PRIV(dev)->dev = dev;
237 spin_lock_irqsave(&sunis_lock,flags);
238 first = !sunis;
239 PRIV(dev)->next = sunis;
240 sunis = PRIV(dev);
241 spin_unlock_irqrestore(&sunis_lock,flags);
242 memset(&PRIV(dev)->sonet_stats,0,sizeof(struct k_sonet_stats));
243 PUT(GET(RSOP_CIE) | SUNI_RSOP_CIE_LOSE,RSOP_CIE);
244 /* interrupt on loss of signal */
245 poll_los(dev); /* ... and clear SUNI interrupts */
246 if (dev->signal == ATM_PHY_SIG_LOST)
247 printk(KERN_WARNING "%s(itf %d): no signal\n",dev->type,
248 dev->number);
249 PRIV(dev)->loop_mode = ATM_LM_NONE;
250 suni_hz(0); /* clear SUNI counters */
251 (void) fetch_stats(dev,NULL,1); /* clear kernel counters */
252 if (first) {
253 init_timer(&poll_timer);
254 poll_timer.expires = jiffies+HZ;
255 poll_timer.function = suni_hz;
256 poll_timer.data = 1;
257 #if 0
258 printk(KERN_DEBUG "[u] p=0x%lx,n=0x%lx\n",(unsigned long) poll_timer.list.prev,
259 (unsigned long) poll_timer.list.next);
260 #endif
261 add_timer(&poll_timer);
262 }
263 return 0;
264 }
265
266
suni_stop(struct atm_dev * dev)267 static int suni_stop(struct atm_dev *dev)
268 {
269 struct suni_priv **walk;
270 unsigned long flags;
271
272 /* let SAR driver worry about stopping interrupts */
273 spin_lock_irqsave(&sunis_lock,flags);
274 for (walk = &sunis; *walk != PRIV(dev);
275 walk = &PRIV((*walk)->dev)->next);
276 *walk = PRIV((*walk)->dev)->next;
277 if (!sunis) del_timer_sync(&poll_timer);
278 spin_unlock_irqrestore(&sunis_lock,flags);
279 kfree(PRIV(dev));
280
281 return 0;
282 }
283
284
285 static const struct atmphy_ops suni_ops = {
286 start: suni_start,
287 ioctl: suni_ioctl,
288 interrupt: suni_int,
289 stop: suni_stop,
290 };
291
292
suni_init(struct atm_dev * dev)293 int suni_init(struct atm_dev *dev)
294 {
295 unsigned char mri;
296
297 mri = GET(MRI); /* reset SUNI */
298 PUT(mri | SUNI_MRI_RESET,MRI);
299 PUT(mri,MRI);
300 PUT((GET(MT) & SUNI_MT_DS27_53),MT); /* disable all tests */
301 REG_CHANGE(SUNI_TPOP_APM_S,SUNI_TPOP_APM_S_SHIFT,SUNI_TPOP_S_SONET,
302 TPOP_APM); /* use SONET */
303 REG_CHANGE(SUNI_TACP_IUCHP_CLP,0,SUNI_TACP_IUCHP_CLP,
304 TACP_IUCHP); /* idle cells */
305 PUT(SUNI_IDLE_PATTERN,TACP_IUCPOP);
306 dev->phy = &suni_ops;
307 return 0;
308 }
309
310 EXPORT_SYMBOL(suni_init);
311
312 MODULE_LICENSE("GPL");
313