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