blob: fc8cb07c24779033dd95bfa1e7d3c343bd50836c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* drivers/atm/uPD98402.c - NEC uPD98402 (PHY) declarations */
2
3/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
5
6#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/mm.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>
14
15#include "uPD98402.h"
16
17
18#if 0
19#define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
20#else
21#define DPRINTK(format,args...)
22#endif
23
24
25struct uPD98402_priv {
26 struct k_sonet_stats sonet_stats;/* link diagnostics */
27 unsigned char framing; /* SONET/SDH framing */
28 int loop_mode; /* loopback mode */
29 spinlock_t lock;
30};
31
32
33#define PRIV(dev) ((struct uPD98402_priv *) dev->phy_data)
34
35#define PUT(val,reg) dev->ops->phy_put(dev,val,uPD98402_##reg)
36#define GET(reg) dev->ops->phy_get(dev,uPD98402_##reg)
37
38
39static int fetch_stats(struct atm_dev *dev,struct sonet_stats __user *arg,int zero)
40{
41 struct sonet_stats tmp;
42 int error = 0;
43
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));
47 if (zero && !error) {
48 /* unused fields are reported as -1, but we must not "adjust"
49 them */
50 tmp.corr_hcs = tmp.tx_cells = tmp.rx_cells = 0;
51 sonet_subtract_stats(&PRIV(dev)->sonet_stats,&tmp);
52 }
53 return error ? -EFAULT : 0;
54}
55
56
57static int set_framing(struct atm_dev *dev,unsigned char framing)
58{
59 static const unsigned char sonet[] = { 1,2,3,0 };
60 static const unsigned char sdh[] = { 1,0,0,2 };
61 const char *set;
62 unsigned long flags;
63
64 switch (framing) {
65 case SONET_FRAME_SONET:
66 set = sonet;
67 break;
68 case SONET_FRAME_SDH:
69 set = sdh;
70 break;
71 default:
72 return -EINVAL;
73 }
74 spin_lock_irqsave(&PRIV(dev)->lock, flags);
75 PUT(set[0],C11T);
76 PUT(set[1],C12T);
77 PUT(set[2],C13T);
78 PUT((GET(MDR) & ~uPD98402_MDR_SS_MASK) | (set[3] <<
79 uPD98402_MDR_SS_SHIFT),MDR);
80 spin_unlock_irqrestore(&PRIV(dev)->lock, flags);
81 return 0;
82}
83
84
85static int get_sense(struct atm_dev *dev,u8 __user *arg)
86{
87 unsigned long flags;
88 unsigned char s[3];
89
90 spin_lock_irqsave(&PRIV(dev)->lock, flags);
91 s[0] = GET(C11R);
92 s[1] = GET(C12R);
93 s[2] = GET(C13R);
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;
98}
99
100
101static int set_loopback(struct atm_dev *dev,int mode)
102{
103 unsigned char mode_reg;
104
105 mode_reg = GET(MDR) & ~(uPD98402_MDR_TPLP | uPD98402_MDR_ALP |
106 uPD98402_MDR_RPLP);
107 switch (__ATM_LM_XTLOC(mode)) {
108 case __ATM_LM_NONE:
109 break;
110 case __ATM_LM_PHY:
111 mode_reg |= uPD98402_MDR_TPLP;
112 break;
113 case __ATM_LM_ATM:
114 mode_reg |= uPD98402_MDR_ALP;
115 break;
116 default:
117 return -EINVAL;
118 }
119 switch (__ATM_LM_XTRMT(mode)) {
120 case __ATM_LM_NONE:
121 break;
122 case __ATM_LM_PHY:
123 mode_reg |= uPD98402_MDR_RPLP;
124 break;
125 default:
126 return -EINVAL;
127 }
128 PUT(mode_reg,MDR);
129 PRIV(dev)->loop_mode = mode;
130 return 0;
131}
132
133
134static int uPD98402_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg)
135{
136 switch (cmd) {
137
138 case SONET_GETSTATZ:
139 case SONET_GETSTAT:
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) ?
145 -EFAULT : 0;
146 case SONET_GETFRSENSE:
147 return get_sense(dev,arg);
148 case ATM_SETLOOP:
149 return set_loopback(dev, (int)(unsigned long)arg);
150 case ATM_GETLOOP:
151 return put_user(PRIV(dev)->loop_mode,(int __user *)arg) ?
152 -EFAULT : 0;
153 case ATM_QUERYLOOP:
154 return put_user(ATM_LM_LOC_PHY | ATM_LM_LOC_ATM |
155 ATM_LM_RMT_PHY,(int __user *)arg) ? -EFAULT : 0;
156 default:
157 return -ENOIOCTLCMD;
158 }
159}
160
161
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); }
166
167
168static void stat_event(struct atm_dev *dev)
169{
170 unsigned char events;
171
172 events = GET(PCR);
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);
178}
179
180
181#undef ADD_LIMITED
182
183
184static void uPD98402_int(struct atm_dev *dev)
185{
186 static unsigned long silence = 0;
187 unsigned char reason;
188
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);
198 }
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;
204 }
205 }
206}
207
208
209static int uPD98402_start(struct atm_dev *dev)
210{
211 DPRINTK("phy_start\n");
212 if (!(dev->dev_data = kmalloc(sizeof(struct uPD98402_priv),GFP_KERNEL)))
213 return -ENOMEM;
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);
227 return 0;
228}
229
230
231static int uPD98402_stop(struct atm_dev *dev)
232{
233 /* let SAR driver worry about stopping interrupts */
234 kfree(PRIV(dev));
235 return 0;
236}
237
238
239static const struct atmphy_ops uPD98402_ops = {
240 .start = uPD98402_start,
241 .ioctl = uPD98402_ioctl,
242 .interrupt = uPD98402_int,
243 .stop = uPD98402_stop,
244};
245
246
247int uPD98402_init(struct atm_dev *dev)
248{
249DPRINTK("phy_init\n");
250 dev->phy = &uPD98402_ops;
251 return 0;
252}
253
254
255MODULE_LICENSE("GPL");
256
257EXPORT_SYMBOL(uPD98402_init);
258
259static __init int uPD98402_module_init(void)
260{
261 return 0;
262}
263module_init(uPD98402_module_init);
264/* module_exit not defined so not unloadable */