idt77105.c 11 KB

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