uPD98402.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* drivers/atm/uPD98402.c - NEC uPD98402 (PHY) declarations */
  2. /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
  3. #include <linux/module.h>
  4. #include <linux/mm.h>
  5. #include <linux/errno.h>
  6. #include <linux/atmdev.h>
  7. #include <linux/sonet.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <asm/uaccess.h>
  11. #include <linux/atomic.h>
  12. #include "uPD98402.h"
  13. #if 0
  14. #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
  15. #else
  16. #define DPRINTK(format,args...)
  17. #endif
  18. struct uPD98402_priv {
  19. struct k_sonet_stats sonet_stats;/* link diagnostics */
  20. unsigned char framing; /* SONET/SDH framing */
  21. int loop_mode; /* loopback mode */
  22. spinlock_t lock;
  23. };
  24. #define PRIV(dev) ((struct uPD98402_priv *) dev->phy_data)
  25. #define PUT(val,reg) dev->ops->phy_put(dev,val,uPD98402_##reg)
  26. #define GET(reg) dev->ops->phy_get(dev,uPD98402_##reg)
  27. static int fetch_stats(struct atm_dev *dev,struct sonet_stats __user *arg,int zero)
  28. {
  29. struct sonet_stats tmp;
  30. int error = 0;
  31. atomic_add(GET(HECCT),&PRIV(dev)->sonet_stats.uncorr_hcs);
  32. sonet_copy_stats(&PRIV(dev)->sonet_stats,&tmp);
  33. if (arg) error = copy_to_user(arg,&tmp,sizeof(tmp));
  34. if (zero && !error) {
  35. /* unused fields are reported as -1, but we must not "adjust"
  36. them */
  37. tmp.corr_hcs = tmp.tx_cells = tmp.rx_cells = 0;
  38. sonet_subtract_stats(&PRIV(dev)->sonet_stats,&tmp);
  39. }
  40. return error ? -EFAULT : 0;
  41. }
  42. static int set_framing(struct atm_dev *dev,unsigned char framing)
  43. {
  44. static const unsigned char sonet[] = { 1,2,3,0 };
  45. static const unsigned char sdh[] = { 1,0,0,2 };
  46. const char *set;
  47. unsigned long flags;
  48. switch (framing) {
  49. case SONET_FRAME_SONET:
  50. set = sonet;
  51. break;
  52. case SONET_FRAME_SDH:
  53. set = sdh;
  54. break;
  55. default:
  56. return -EINVAL;
  57. }
  58. spin_lock_irqsave(&PRIV(dev)->lock, flags);
  59. PUT(set[0],C11T);
  60. PUT(set[1],C12T);
  61. PUT(set[2],C13T);
  62. PUT((GET(MDR) & ~uPD98402_MDR_SS_MASK) | (set[3] <<
  63. uPD98402_MDR_SS_SHIFT),MDR);
  64. spin_unlock_irqrestore(&PRIV(dev)->lock, flags);
  65. return 0;
  66. }
  67. static int get_sense(struct atm_dev *dev,u8 __user *arg)
  68. {
  69. unsigned long flags;
  70. unsigned char s[3];
  71. spin_lock_irqsave(&PRIV(dev)->lock, flags);
  72. s[0] = GET(C11R);
  73. s[1] = GET(C12R);
  74. s[2] = GET(C13R);
  75. spin_unlock_irqrestore(&PRIV(dev)->lock, flags);
  76. return (put_user(s[0], arg) || put_user(s[1], arg+1) ||
  77. put_user(s[2], arg+2) || put_user(0xff, arg+3) ||
  78. put_user(0xff, arg+4) || put_user(0xff, arg+5)) ? -EFAULT : 0;
  79. }
  80. static int set_loopback(struct atm_dev *dev,int mode)
  81. {
  82. unsigned char mode_reg;
  83. mode_reg = GET(MDR) & ~(uPD98402_MDR_TPLP | uPD98402_MDR_ALP |
  84. uPD98402_MDR_RPLP);
  85. switch (__ATM_LM_XTLOC(mode)) {
  86. case __ATM_LM_NONE:
  87. break;
  88. case __ATM_LM_PHY:
  89. mode_reg |= uPD98402_MDR_TPLP;
  90. break;
  91. case __ATM_LM_ATM:
  92. mode_reg |= uPD98402_MDR_ALP;
  93. break;
  94. default:
  95. return -EINVAL;
  96. }
  97. switch (__ATM_LM_XTRMT(mode)) {
  98. case __ATM_LM_NONE:
  99. break;
  100. case __ATM_LM_PHY:
  101. mode_reg |= uPD98402_MDR_RPLP;
  102. break;
  103. default:
  104. return -EINVAL;
  105. }
  106. PUT(mode_reg,MDR);
  107. PRIV(dev)->loop_mode = mode;
  108. return 0;
  109. }
  110. static int uPD98402_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg)
  111. {
  112. switch (cmd) {
  113. case SONET_GETSTATZ:
  114. case SONET_GETSTAT:
  115. return fetch_stats(dev,arg, cmd == SONET_GETSTATZ);
  116. case SONET_SETFRAMING:
  117. return set_framing(dev, (int)(unsigned long)arg);
  118. case SONET_GETFRAMING:
  119. return put_user(PRIV(dev)->framing,(int __user *)arg) ?
  120. -EFAULT : 0;
  121. case SONET_GETFRSENSE:
  122. return get_sense(dev,arg);
  123. case ATM_SETLOOP:
  124. return set_loopback(dev, (int)(unsigned long)arg);
  125. case ATM_GETLOOP:
  126. return put_user(PRIV(dev)->loop_mode,(int __user *)arg) ?
  127. -EFAULT : 0;
  128. case ATM_QUERYLOOP:
  129. return put_user(ATM_LM_LOC_PHY | ATM_LM_LOC_ATM |
  130. ATM_LM_RMT_PHY,(int __user *)arg) ? -EFAULT : 0;
  131. default:
  132. return -ENOIOCTLCMD;
  133. }
  134. }
  135. #define ADD_LIMITED(s,v) \
  136. { atomic_add(GET(v),&PRIV(dev)->sonet_stats.s); \
  137. if (atomic_read(&PRIV(dev)->sonet_stats.s) < 0) \
  138. atomic_set(&PRIV(dev)->sonet_stats.s,INT_MAX); }
  139. static void stat_event(struct atm_dev *dev)
  140. {
  141. unsigned char events;
  142. events = GET(PCR);
  143. if (events & uPD98402_PFM_PFEB) ADD_LIMITED(path_febe,PFECB);
  144. if (events & uPD98402_PFM_LFEB) ADD_LIMITED(line_febe,LECCT);
  145. if (events & uPD98402_PFM_B3E) ADD_LIMITED(path_bip,B3ECT);
  146. if (events & uPD98402_PFM_B2E) ADD_LIMITED(line_bip,B2ECT);
  147. if (events & uPD98402_PFM_B1E) ADD_LIMITED(section_bip,B1ECT);
  148. }
  149. #undef ADD_LIMITED
  150. static void uPD98402_int(struct atm_dev *dev)
  151. {
  152. static unsigned long silence = 0;
  153. unsigned char reason;
  154. while ((reason = GET(PICR))) {
  155. if (reason & uPD98402_INT_LOS)
  156. printk(KERN_NOTICE "%s(itf %d): signal lost\n",
  157. dev->type,dev->number);
  158. if (reason & uPD98402_INT_PFM) stat_event(dev);
  159. if (reason & uPD98402_INT_PCO) {
  160. (void) GET(PCOCR); /* clear interrupt cause */
  161. atomic_add(GET(HECCT),
  162. &PRIV(dev)->sonet_stats.uncorr_hcs);
  163. }
  164. if ((reason & uPD98402_INT_RFO) &&
  165. (time_after(jiffies, silence) || silence == 0)) {
  166. printk(KERN_WARNING "%s(itf %d): uPD98402 receive "
  167. "FIFO overflow\n",dev->type,dev->number);
  168. silence = (jiffies+HZ/2)|1;
  169. }
  170. }
  171. }
  172. static int uPD98402_start(struct atm_dev *dev)
  173. {
  174. DPRINTK("phy_start\n");
  175. if (!(dev->dev_data = kmalloc(sizeof(struct uPD98402_priv),GFP_KERNEL)))
  176. return -ENOMEM;
  177. spin_lock_init(&PRIV(dev)->lock);
  178. memset(&PRIV(dev)->sonet_stats,0,sizeof(struct k_sonet_stats));
  179. (void) GET(PCR); /* clear performance events */
  180. PUT(uPD98402_PFM_FJ,PCMR); /* ignore frequency adj */
  181. (void) GET(PCOCR); /* clear overflows */
  182. PUT(~uPD98402_PCO_HECC,PCOMR);
  183. (void) GET(PICR); /* clear interrupts */
  184. PUT(~(uPD98402_INT_PFM | uPD98402_INT_ALM | uPD98402_INT_RFO |
  185. uPD98402_INT_LOS),PIMR); /* enable them */
  186. (void) fetch_stats(dev,NULL,1); /* clear kernel counters */
  187. atomic_set(&PRIV(dev)->sonet_stats.corr_hcs,-1);
  188. atomic_set(&PRIV(dev)->sonet_stats.tx_cells,-1);
  189. atomic_set(&PRIV(dev)->sonet_stats.rx_cells,-1);
  190. return 0;
  191. }
  192. static int uPD98402_stop(struct atm_dev *dev)
  193. {
  194. /* let SAR driver worry about stopping interrupts */
  195. kfree(PRIV(dev));
  196. return 0;
  197. }
  198. static const struct atmphy_ops uPD98402_ops = {
  199. .start = uPD98402_start,
  200. .ioctl = uPD98402_ioctl,
  201. .interrupt = uPD98402_int,
  202. .stop = uPD98402_stop,
  203. };
  204. int uPD98402_init(struct atm_dev *dev)
  205. {
  206. DPRINTK("phy_init\n");
  207. dev->phy = &uPD98402_ops;
  208. return 0;
  209. }
  210. MODULE_LICENSE("GPL");
  211. EXPORT_SYMBOL(uPD98402_init);
  212. static __init int uPD98402_module_init(void)
  213. {
  214. return 0;
  215. }
  216. module_init(uPD98402_module_init);
  217. /* module_exit not defined so not unloadable */