idt77105.c 11 KB

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