Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* drivers/atm/idt77105.c - IDT77105 (PHY) driver */ |
| 2 | |
| 3 | /* Written 1999 by Greg Banks, NEC Australia <gnb@linuxfan.com>. Based on suni.c */ |
| 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_idt77105.h> |
| 18 | #include <linux/spinlock.h> |
| 19 | #include <asm/system.h> |
| 20 | #include <asm/param.h> |
| 21 | #include <asm/uaccess.h> |
| 22 | |
| 23 | #include "idt77105.h" |
| 24 | |
| 25 | #undef GENERAL_DEBUG |
| 26 | |
| 27 | #ifdef GENERAL_DEBUG |
| 28 | #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args) |
| 29 | #else |
| 30 | #define DPRINTK(format,args...) |
| 31 | #endif |
| 32 | |
| 33 | |
| 34 | struct idt77105_priv { |
| 35 | struct idt77105_stats stats; /* link diagnostics */ |
| 36 | struct atm_dev *dev; /* device back-pointer */ |
| 37 | struct idt77105_priv *next; |
| 38 | int loop_mode; |
| 39 | unsigned char old_mcr; /* storage of MCR reg while signal lost */ |
| 40 | }; |
| 41 | |
| 42 | static DEFINE_SPINLOCK(idt77105_priv_lock); |
| 43 | |
| 44 | #define PRIV(dev) ((struct idt77105_priv *) dev->phy_data) |
| 45 | |
| 46 | #define PUT(val,reg) dev->ops->phy_put(dev,val,IDT77105_##reg) |
| 47 | #define GET(reg) dev->ops->phy_get(dev,IDT77105_##reg) |
| 48 | |
| 49 | static void idt77105_stats_timer_func(unsigned long); |
| 50 | static void idt77105_restart_timer_func(unsigned long); |
| 51 | |
| 52 | |
| 53 | static struct timer_list stats_timer = |
| 54 | TIMER_INITIALIZER(idt77105_stats_timer_func, 0, 0); |
| 55 | static struct timer_list restart_timer = |
| 56 | TIMER_INITIALIZER(idt77105_restart_timer_func, 0, 0); |
| 57 | static int start_timer = 1; |
| 58 | static struct idt77105_priv *idt77105_all = NULL; |
| 59 | |
| 60 | /* |
| 61 | * Retrieve the value of one of the IDT77105's counters. |
| 62 | * `counter' is one of the IDT77105_CTRSEL_* constants. |
| 63 | */ |
| 64 | static u16 get_counter(struct atm_dev *dev, int counter) |
| 65 | { |
| 66 | u16 val; |
| 67 | |
| 68 | /* write the counter bit into PHY register 6 */ |
| 69 | PUT(counter, CTRSEL); |
| 70 | /* read the low 8 bits from register 4 */ |
| 71 | val = GET(CTRLO); |
| 72 | /* read the high 8 bits from register 5 */ |
| 73 | val |= GET(CTRHI)<<8; |
| 74 | |
| 75 | return val; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Timer function called every second to gather statistics |
| 80 | * from the 77105. This is done because the h/w registers |
| 81 | * will overflow if not read at least once per second. The |
| 82 | * kernel's stats are much higher precision. Also, having |
| 83 | * a separate copy of the stats allows implementation of |
| 84 | * an ioctl which gathers the stats *without* zero'ing them. |
| 85 | */ |
| 86 | static void idt77105_stats_timer_func(unsigned long dummy) |
| 87 | { |
| 88 | struct idt77105_priv *walk; |
| 89 | struct atm_dev *dev; |
| 90 | struct idt77105_stats *stats; |
| 91 | |
| 92 | DPRINTK("IDT77105 gathering statistics\n"); |
| 93 | for (walk = idt77105_all; walk; walk = walk->next) { |
| 94 | dev = walk->dev; |
| 95 | |
| 96 | stats = &walk->stats; |
| 97 | stats->symbol_errors += get_counter(dev, IDT77105_CTRSEL_SEC); |
| 98 | stats->tx_cells += get_counter(dev, IDT77105_CTRSEL_TCC); |
| 99 | stats->rx_cells += get_counter(dev, IDT77105_CTRSEL_RCC); |
| 100 | stats->rx_hec_errors += get_counter(dev, IDT77105_CTRSEL_RHEC); |
| 101 | } |
| 102 | if (!start_timer) mod_timer(&stats_timer,jiffies+IDT77105_STATS_TIMER_PERIOD); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /* |
| 107 | * A separate timer func which handles restarting PHY chips which |
| 108 | * have had the cable re-inserted after being pulled out. This is |
| 109 | * done by polling the Good Signal Bit in the Interrupt Status |
| 110 | * register every 5 seconds. The other technique (checking Good |
| 111 | * Signal Bit in the interrupt handler) cannot be used because PHY |
| 112 | * interrupts need to be disabled when the cable is pulled out |
| 113 | * to avoid lots of spurious cell error interrupts. |
| 114 | */ |
| 115 | static void idt77105_restart_timer_func(unsigned long dummy) |
| 116 | { |
| 117 | struct idt77105_priv *walk; |
| 118 | struct atm_dev *dev; |
| 119 | unsigned char istat; |
| 120 | |
| 121 | DPRINTK("IDT77105 checking for cable re-insertion\n"); |
| 122 | for (walk = idt77105_all; walk; walk = walk->next) { |
| 123 | dev = walk->dev; |
| 124 | |
| 125 | if (dev->signal != ATM_PHY_SIG_LOST) |
| 126 | continue; |
| 127 | |
| 128 | istat = GET(ISTAT); /* side effect: clears all interrupt status bits */ |
| 129 | if (istat & IDT77105_ISTAT_GOODSIG) { |
| 130 | /* Found signal again */ |
| 131 | dev->signal = ATM_PHY_SIG_FOUND; |
| 132 | printk(KERN_NOTICE "%s(itf %d): signal detected again\n", |
| 133 | dev->type,dev->number); |
| 134 | /* flush the receive FIFO */ |
| 135 | PUT( GET(DIAG) | IDT77105_DIAG_RFLUSH, DIAG); |
| 136 | /* re-enable interrupts */ |
| 137 | PUT( walk->old_mcr ,MCR); |
| 138 | } |
| 139 | } |
| 140 | if (!start_timer) mod_timer(&restart_timer,jiffies+IDT77105_RESTART_TIMER_PERIOD); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | static int fetch_stats(struct atm_dev *dev,struct idt77105_stats __user *arg,int zero) |
| 145 | { |
| 146 | unsigned long flags; |
| 147 | struct idt77105_stats stats; |
| 148 | |
| 149 | spin_lock_irqsave(&idt77105_priv_lock, flags); |
| 150 | memcpy(&stats, &PRIV(dev)->stats, sizeof(struct idt77105_stats)); |
| 151 | if (zero) |
| 152 | memset(&PRIV(dev)->stats, 0, sizeof(struct idt77105_stats)); |
| 153 | spin_unlock_irqrestore(&idt77105_priv_lock, flags); |
| 154 | if (arg == NULL) |
| 155 | return 0; |
| 156 | return copy_to_user(arg, &PRIV(dev)->stats, |
| 157 | sizeof(struct idt77105_stats)) ? -EFAULT : 0; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | static int set_loopback(struct atm_dev *dev,int mode) |
| 162 | { |
| 163 | int diag; |
| 164 | |
| 165 | diag = GET(DIAG) & ~IDT77105_DIAG_LCMASK; |
| 166 | switch (mode) { |
| 167 | case ATM_LM_NONE: |
| 168 | break; |
| 169 | case ATM_LM_LOC_ATM: |
| 170 | diag |= IDT77105_DIAG_LC_PHY_LOOPBACK; |
| 171 | break; |
| 172 | case ATM_LM_RMT_ATM: |
| 173 | diag |= IDT77105_DIAG_LC_LINE_LOOPBACK; |
| 174 | break; |
| 175 | default: |
| 176 | return -EINVAL; |
| 177 | } |
| 178 | PUT(diag,DIAG); |
| 179 | printk(KERN_NOTICE "%s(%d) Loopback mode is: %s\n", dev->type, |
| 180 | dev->number, |
| 181 | (mode == ATM_LM_NONE ? "NONE" : |
| 182 | (mode == ATM_LM_LOC_ATM ? "DIAG (local)" : |
| 183 | (mode == IDT77105_DIAG_LC_LINE_LOOPBACK ? "LOOP (remote)" : |
| 184 | "unknown"))) |
| 185 | ); |
| 186 | PRIV(dev)->loop_mode = mode; |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | static int idt77105_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg) |
| 192 | { |
| 193 | printk(KERN_NOTICE "%s(%d) idt77105_ioctl() called\n",dev->type,dev->number); |
| 194 | switch (cmd) { |
| 195 | case IDT77105_GETSTATZ: |
| 196 | if (!capable(CAP_NET_ADMIN)) return -EPERM; |
| 197 | /* fall through */ |
| 198 | case IDT77105_GETSTAT: |
| 199 | return fetch_stats(dev, arg, cmd == IDT77105_GETSTATZ); |
| 200 | case ATM_SETLOOP: |
| 201 | return set_loopback(dev,(int)(unsigned long) arg); |
| 202 | case ATM_GETLOOP: |
| 203 | return put_user(PRIV(dev)->loop_mode,(int __user *)arg) ? |
| 204 | -EFAULT : 0; |
| 205 | case ATM_QUERYLOOP: |
| 206 | return put_user(ATM_LM_LOC_ATM | ATM_LM_RMT_ATM, |
| 207 | (int __user *) arg) ? -EFAULT : 0; |
| 208 | default: |
| 209 | return -ENOIOCTLCMD; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | |
| 214 | |
| 215 | static void idt77105_int(struct atm_dev *dev) |
| 216 | { |
| 217 | unsigned char istat; |
| 218 | |
| 219 | istat = GET(ISTAT); /* side effect: clears all interrupt status bits */ |
| 220 | |
| 221 | DPRINTK("IDT77105 generated an interrupt, istat=%02x\n", (unsigned)istat); |
| 222 | |
| 223 | if (istat & IDT77105_ISTAT_RSCC) { |
| 224 | /* Rx Signal Condition Change - line went up or down */ |
| 225 | if (istat & IDT77105_ISTAT_GOODSIG) { /* signal detected again */ |
| 226 | /* This should not happen (restart timer does it) but JIC */ |
| 227 | dev->signal = ATM_PHY_SIG_FOUND; |
| 228 | } else { /* signal lost */ |
| 229 | /* |
| 230 | * Disable interrupts and stop all transmission and |
| 231 | * reception - the restart timer will restore these. |
| 232 | */ |
| 233 | PRIV(dev)->old_mcr = GET(MCR); |
| 234 | PUT( |
| 235 | (PRIV(dev)->old_mcr| |
| 236 | IDT77105_MCR_DREC| |
| 237 | IDT77105_MCR_DRIC| |
| 238 | IDT77105_MCR_HALTTX |
| 239 | ) & ~IDT77105_MCR_EIP, MCR); |
| 240 | dev->signal = ATM_PHY_SIG_LOST; |
| 241 | printk(KERN_NOTICE "%s(itf %d): signal lost\n", |
| 242 | dev->type,dev->number); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (istat & IDT77105_ISTAT_RFO) { |
| 247 | /* Rx FIFO Overrun -- perform a FIFO flush */ |
| 248 | PUT( GET(DIAG) | IDT77105_DIAG_RFLUSH, DIAG); |
| 249 | printk(KERN_NOTICE "%s(itf %d): receive FIFO overrun\n", |
| 250 | dev->type,dev->number); |
| 251 | } |
| 252 | #ifdef GENERAL_DEBUG |
| 253 | if (istat & (IDT77105_ISTAT_HECERR | IDT77105_ISTAT_SCR | |
| 254 | IDT77105_ISTAT_RSE)) { |
| 255 | /* normally don't care - just report in stats */ |
| 256 | printk(KERN_NOTICE "%s(itf %d): received cell with error\n", |
| 257 | dev->type,dev->number); |
| 258 | } |
| 259 | #endif |
| 260 | } |
| 261 | |
| 262 | |
| 263 | static int idt77105_start(struct atm_dev *dev) |
| 264 | { |
| 265 | unsigned long flags; |
| 266 | |
| 267 | if (!(dev->dev_data = kmalloc(sizeof(struct idt77105_priv),GFP_KERNEL))) |
| 268 | return -ENOMEM; |
| 269 | PRIV(dev)->dev = dev; |
| 270 | spin_lock_irqsave(&idt77105_priv_lock, flags); |
| 271 | PRIV(dev)->next = idt77105_all; |
| 272 | idt77105_all = PRIV(dev); |
| 273 | spin_unlock_irqrestore(&idt77105_priv_lock, flags); |
| 274 | memset(&PRIV(dev)->stats,0,sizeof(struct idt77105_stats)); |
| 275 | |
| 276 | /* initialise dev->signal from Good Signal Bit */ |
| 277 | dev->signal = GET(ISTAT) & IDT77105_ISTAT_GOODSIG ? ATM_PHY_SIG_FOUND : |
| 278 | ATM_PHY_SIG_LOST; |
| 279 | if (dev->signal == ATM_PHY_SIG_LOST) |
| 280 | printk(KERN_WARNING "%s(itf %d): no signal\n",dev->type, |
| 281 | dev->number); |
| 282 | |
| 283 | /* initialise loop mode from hardware */ |
| 284 | switch ( GET(DIAG) & IDT77105_DIAG_LCMASK ) { |
| 285 | case IDT77105_DIAG_LC_NORMAL: |
| 286 | PRIV(dev)->loop_mode = ATM_LM_NONE; |
| 287 | break; |
| 288 | case IDT77105_DIAG_LC_PHY_LOOPBACK: |
| 289 | PRIV(dev)->loop_mode = ATM_LM_LOC_ATM; |
| 290 | break; |
| 291 | case IDT77105_DIAG_LC_LINE_LOOPBACK: |
| 292 | PRIV(dev)->loop_mode = ATM_LM_RMT_ATM; |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | /* enable interrupts, e.g. on loss of signal */ |
| 297 | PRIV(dev)->old_mcr = GET(MCR); |
| 298 | if (dev->signal == ATM_PHY_SIG_FOUND) { |
| 299 | PRIV(dev)->old_mcr |= IDT77105_MCR_EIP; |
| 300 | PUT(PRIV(dev)->old_mcr, MCR); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | idt77105_stats_timer_func(0); /* clear 77105 counters */ |
| 305 | (void) fetch_stats(dev,NULL,1); /* clear kernel counters */ |
| 306 | |
| 307 | spin_lock_irqsave(&idt77105_priv_lock, flags); |
| 308 | if (start_timer) { |
| 309 | start_timer = 0; |
| 310 | |
| 311 | init_timer(&stats_timer); |
| 312 | stats_timer.expires = jiffies+IDT77105_STATS_TIMER_PERIOD; |
| 313 | stats_timer.function = idt77105_stats_timer_func; |
| 314 | add_timer(&stats_timer); |
| 315 | |
| 316 | init_timer(&restart_timer); |
| 317 | restart_timer.expires = jiffies+IDT77105_RESTART_TIMER_PERIOD; |
| 318 | restart_timer.function = idt77105_restart_timer_func; |
| 319 | add_timer(&restart_timer); |
| 320 | } |
| 321 | spin_unlock_irqrestore(&idt77105_priv_lock, flags); |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | |
| 326 | static int idt77105_stop(struct atm_dev *dev) |
| 327 | { |
| 328 | struct idt77105_priv *walk, *prev; |
| 329 | |
| 330 | DPRINTK("%s(itf %d): stopping IDT77105\n",dev->type,dev->number); |
| 331 | |
| 332 | /* disable interrupts */ |
| 333 | PUT( GET(MCR) & ~IDT77105_MCR_EIP, MCR ); |
| 334 | |
| 335 | /* detach private struct from atm_dev & free */ |
| 336 | for (prev = NULL, walk = idt77105_all ; |
| 337 | walk != NULL; |
| 338 | prev = walk, walk = walk->next) { |
| 339 | if (walk->dev == dev) { |
| 340 | if (prev != NULL) |
| 341 | prev->next = walk->next; |
| 342 | else |
| 343 | idt77105_all = walk->next; |
| 344 | dev->phy = NULL; |
| 345 | dev->dev_data = NULL; |
| 346 | kfree(walk); |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | static const struct atmphy_ops idt77105_ops = { |
| 356 | .start = idt77105_start, |
| 357 | .ioctl = idt77105_ioctl, |
| 358 | .interrupt = idt77105_int, |
| 359 | .stop = idt77105_stop, |
| 360 | }; |
| 361 | |
| 362 | |
| 363 | int idt77105_init(struct atm_dev *dev) |
| 364 | { |
| 365 | dev->phy = &idt77105_ops; |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | EXPORT_SYMBOL(idt77105_init); |
| 370 | |
| 371 | static void __exit idt77105_exit(void) |
| 372 | { |
| 373 | /* turn off timers */ |
| 374 | del_timer(&stats_timer); |
| 375 | del_timer(&restart_timer); |
| 376 | } |
| 377 | |
| 378 | module_exit(idt77105_exit); |
| 379 | |
| 380 | MODULE_LICENSE("GPL"); |