Linux 2.6.26-rc5
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / atm / uPD98402.c
blobfc8cb07c24779033dd95bfa1e7d3c343bd50836c
1 /* drivers/atm/uPD98402.c - NEC uPD98402 (PHY) declarations */
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
6 #include <linux/module.h>
7 #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>
15 #include "uPD98402.h"
18 #if 0
19 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
20 #else
21 #define DPRINTK(format,args...)
22 #endif
25 struct 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;
33 #define PRIV(dev) ((struct uPD98402_priv *) dev->phy_data)
35 #define PUT(val,reg) dev->ops->phy_put(dev,val,uPD98402_##reg)
36 #define GET(reg) dev->ops->phy_get(dev,uPD98402_##reg)
39 static int fetch_stats(struct atm_dev *dev,struct sonet_stats __user *arg,int zero)
41 struct sonet_stats tmp;
42 int error = 0;
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);
53 return error ? -EFAULT : 0;
57 static int set_framing(struct atm_dev *dev,unsigned char framing)
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;
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;
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;
85 static int get_sense(struct atm_dev *dev,u8 __user *arg)
87 unsigned long flags;
88 unsigned char s[3];
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;
101 static int set_loopback(struct atm_dev *dev,int mode)
103 unsigned char mode_reg;
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;
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;
128 PUT(mode_reg,MDR);
129 PRIV(dev)->loop_mode = mode;
130 return 0;
134 static int uPD98402_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg)
136 switch (cmd) {
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;
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); }
168 static void stat_event(struct atm_dev *dev)
170 unsigned char events;
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);
181 #undef ADD_LIMITED
184 static void uPD98402_int(struct atm_dev *dev)
186 static unsigned long silence = 0;
187 unsigned char reason;
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);
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;
209 static int uPD98402_start(struct atm_dev *dev)
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;
231 static int uPD98402_stop(struct atm_dev *dev)
233 /* let SAR driver worry about stopping interrupts */
234 kfree(PRIV(dev));
235 return 0;
239 static const struct atmphy_ops uPD98402_ops = {
240 .start = uPD98402_start,
241 .ioctl = uPD98402_ioctl,
242 .interrupt = uPD98402_int,
243 .stop = uPD98402_stop,
247 int uPD98402_init(struct atm_dev *dev)
249 DPRINTK("phy_init\n");
250 dev->phy = &uPD98402_ops;
251 return 0;
255 MODULE_LICENSE("GPL");
257 EXPORT_SYMBOL(uPD98402_init);
259 static __init int uPD98402_module_init(void)
261 return 0;
263 module_init(uPD98402_module_init);
264 /* module_exit not defined so not unloadable */