suni.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * drivers/atm/suni.c - S/UNI PHY driver
  3. *
  4. * Supports the following:
  5. * PMC PM5346 S/UNI LITE
  6. * PMC PM5350 S/UNI 155 ULTRA
  7. * PMC PM5355 S/UNI 622
  8. */
  9. /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
  10. #include <linux/module.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/errno.h>
  15. #include <linux/atmdev.h>
  16. #include <linux/sonet.h>
  17. #include <linux/delay.h>
  18. #include <linux/timer.h>
  19. #include <linux/init.h>
  20. #include <linux/capability.h>
  21. #include <linux/atm_suni.h>
  22. #include <linux/slab.h>
  23. #include <asm/param.h>
  24. #include <asm/uaccess.h>
  25. #include <linux/atomic.h>
  26. #include "suni.h"
  27. #if 0
  28. #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
  29. #else
  30. #define DPRINTK(format,args...)
  31. #endif
  32. #define PRIV(dev) ((struct suni_priv *) dev->phy_data)
  33. #define PUT(val,reg) dev->ops->phy_put(dev,val,SUNI_##reg)
  34. #define GET(reg) dev->ops->phy_get(dev,SUNI_##reg)
  35. #define REG_CHANGE(mask,shift,value,reg) \
  36. PUT((GET(reg) & ~(mask)) | ((value) << (shift)),reg)
  37. static struct timer_list poll_timer;
  38. static struct suni_priv *sunis = NULL;
  39. static DEFINE_SPINLOCK(sunis_lock);
  40. #define ADD_LIMITED(s,v) \
  41. atomic_add((v),&stats->s); \
  42. if (atomic_read(&stats->s) < 0) atomic_set(&stats->s,INT_MAX);
  43. static void suni_hz(unsigned long from_timer)
  44. {
  45. struct suni_priv *walk;
  46. struct atm_dev *dev;
  47. struct k_sonet_stats *stats;
  48. for (walk = sunis; walk; walk = walk->next) {
  49. dev = walk->dev;
  50. stats = &walk->sonet_stats;
  51. PUT(0,MRI); /* latch counters */
  52. udelay(1);
  53. ADD_LIMITED(section_bip,(GET(RSOP_SBL) & 0xff) |
  54. ((GET(RSOP_SBM) & 0xff) << 8));
  55. ADD_LIMITED(line_bip,(GET(RLOP_LBL) & 0xff) |
  56. ((GET(RLOP_LB) & 0xff) << 8) |
  57. ((GET(RLOP_LBM) & 0xf) << 16));
  58. ADD_LIMITED(path_bip,(GET(RPOP_PBL) & 0xff) |
  59. ((GET(RPOP_PBM) & 0xff) << 8));
  60. ADD_LIMITED(line_febe,(GET(RLOP_LFL) & 0xff) |
  61. ((GET(RLOP_LF) & 0xff) << 8) |
  62. ((GET(RLOP_LFM) & 0xf) << 16));
  63. ADD_LIMITED(path_febe,(GET(RPOP_PFL) & 0xff) |
  64. ((GET(RPOP_PFM) & 0xff) << 8));
  65. ADD_LIMITED(corr_hcs,GET(RACP_CHEC) & 0xff);
  66. ADD_LIMITED(uncorr_hcs,GET(RACP_UHEC) & 0xff);
  67. ADD_LIMITED(rx_cells,(GET(RACP_RCCL) & 0xff) |
  68. ((GET(RACP_RCC) & 0xff) << 8) |
  69. ((GET(RACP_RCCM) & 7) << 16));
  70. ADD_LIMITED(tx_cells,(GET(TACP_TCCL) & 0xff) |
  71. ((GET(TACP_TCC) & 0xff) << 8) |
  72. ((GET(TACP_TCCM) & 7) << 16));
  73. }
  74. if (from_timer) mod_timer(&poll_timer,jiffies+HZ);
  75. }
  76. #undef ADD_LIMITED
  77. static int fetch_stats(struct atm_dev *dev,struct sonet_stats __user *arg,int zero)
  78. {
  79. struct sonet_stats tmp;
  80. int error = 0;
  81. sonet_copy_stats(&PRIV(dev)->sonet_stats,&tmp);
  82. if (arg) error = copy_to_user(arg,&tmp,sizeof(tmp));
  83. if (zero && !error) sonet_subtract_stats(&PRIV(dev)->sonet_stats,&tmp);
  84. return error ? -EFAULT : 0;
  85. }
  86. #define HANDLE_FLAG(flag,reg,bit) \
  87. if (todo & flag) { \
  88. if (set) PUT(GET(reg) | bit,reg); \
  89. else PUT(GET(reg) & ~bit,reg); \
  90. todo &= ~flag; \
  91. }
  92. static int change_diag(struct atm_dev *dev,void __user *arg,int set)
  93. {
  94. int todo;
  95. if (get_user(todo,(int __user *)arg)) return -EFAULT;
  96. HANDLE_FLAG(SONET_INS_SBIP,TSOP_DIAG,SUNI_TSOP_DIAG_DBIP8);
  97. HANDLE_FLAG(SONET_INS_LBIP,TLOP_DIAG,SUNI_TLOP_DIAG_DBIP);
  98. HANDLE_FLAG(SONET_INS_PBIP,TPOP_CD,SUNI_TPOP_DIAG_DB3);
  99. HANDLE_FLAG(SONET_INS_FRAME,RSOP_CIE,SUNI_RSOP_CIE_FOOF);
  100. HANDLE_FLAG(SONET_INS_LAIS,TSOP_CTRL,SUNI_TSOP_CTRL_LAIS);
  101. HANDLE_FLAG(SONET_INS_PAIS,TPOP_CD,SUNI_TPOP_DIAG_PAIS);
  102. HANDLE_FLAG(SONET_INS_LOS,TSOP_DIAG,SUNI_TSOP_DIAG_DLOS);
  103. HANDLE_FLAG(SONET_INS_HCS,TACP_CS,SUNI_TACP_CS_DHCS);
  104. return put_user(todo,(int __user *)arg) ? -EFAULT : 0;
  105. }
  106. #undef HANDLE_FLAG
  107. static int get_diag(struct atm_dev *dev,void __user *arg)
  108. {
  109. int set;
  110. set = 0;
  111. if (GET(TSOP_DIAG) & SUNI_TSOP_DIAG_DBIP8) set |= SONET_INS_SBIP;
  112. if (GET(TLOP_DIAG) & SUNI_TLOP_DIAG_DBIP) set |= SONET_INS_LBIP;
  113. if (GET(TPOP_CD) & SUNI_TPOP_DIAG_DB3) set |= SONET_INS_PBIP;
  114. /* SONET_INS_FRAME is one-shot only */
  115. if (GET(TSOP_CTRL) & SUNI_TSOP_CTRL_LAIS) set |= SONET_INS_LAIS;
  116. if (GET(TPOP_CD) & SUNI_TPOP_DIAG_PAIS) set |= SONET_INS_PAIS;
  117. if (GET(TSOP_DIAG) & SUNI_TSOP_DIAG_DLOS) set |= SONET_INS_LOS;
  118. if (GET(TACP_CS) & SUNI_TACP_CS_DHCS) set |= SONET_INS_HCS;
  119. return put_user(set,(int __user *)arg) ? -EFAULT : 0;
  120. }
  121. static int set_loopback(struct atm_dev *dev,int mode)
  122. {
  123. unsigned char control;
  124. int reg, dle, lle;
  125. if (PRIV(dev)->type == SUNI_MRI_TYPE_PM5355) {
  126. reg = SUNI_MCM;
  127. dle = SUNI_MCM_DLE;
  128. lle = SUNI_MCM_LLE;
  129. } else {
  130. reg = SUNI_MCT;
  131. dle = SUNI_MCT_DLE;
  132. lle = SUNI_MCT_LLE;
  133. }
  134. control = dev->ops->phy_get(dev, reg) & ~(dle | lle);
  135. switch (mode) {
  136. case ATM_LM_NONE:
  137. break;
  138. case ATM_LM_LOC_PHY:
  139. control |= dle;
  140. break;
  141. case ATM_LM_RMT_PHY:
  142. control |= lle;
  143. break;
  144. default:
  145. return -EINVAL;
  146. }
  147. dev->ops->phy_put(dev, control, reg);
  148. PRIV(dev)->loop_mode = mode;
  149. return 0;
  150. }
  151. /*
  152. * SONET vs. SDH Configuration
  153. *
  154. * Z0INS (register 0x06): 0 for SONET, 1 for SDH
  155. * ENSS (register 0x3D): 0 for SONET, 1 for SDH
  156. * LEN16 (register 0x28): 0 for SONET, 1 for SDH (n/a for S/UNI 155 QUAD)
  157. * LEN16 (register 0x50): 0 for SONET, 1 for SDH (n/a for S/UNI 155 QUAD)
  158. * S[1:0] (register 0x46): 00 for SONET, 10 for SDH
  159. */
  160. static int set_sonet(struct atm_dev *dev)
  161. {
  162. if (PRIV(dev)->type == SUNI_MRI_TYPE_PM5355) {
  163. PUT(GET(RPOP_RC) & ~SUNI_RPOP_RC_ENSS, RPOP_RC);
  164. PUT(GET(SSTB_CTRL) & ~SUNI_SSTB_CTRL_LEN16, SSTB_CTRL);
  165. PUT(GET(SPTB_CTRL) & ~SUNI_SPTB_CTRL_LEN16, SPTB_CTRL);
  166. }
  167. REG_CHANGE(SUNI_TPOP_APM_S, SUNI_TPOP_APM_S_SHIFT,
  168. SUNI_TPOP_S_SONET, TPOP_APM);
  169. return 0;
  170. }
  171. static int set_sdh(struct atm_dev *dev)
  172. {
  173. if (PRIV(dev)->type == SUNI_MRI_TYPE_PM5355) {
  174. PUT(GET(RPOP_RC) | SUNI_RPOP_RC_ENSS, RPOP_RC);
  175. PUT(GET(SSTB_CTRL) | SUNI_SSTB_CTRL_LEN16, SSTB_CTRL);
  176. PUT(GET(SPTB_CTRL) | SUNI_SPTB_CTRL_LEN16, SPTB_CTRL);
  177. }
  178. REG_CHANGE(SUNI_TPOP_APM_S, SUNI_TPOP_APM_S_SHIFT,
  179. SUNI_TPOP_S_SDH, TPOP_APM);
  180. return 0;
  181. }
  182. static int get_framing(struct atm_dev *dev, void __user *arg)
  183. {
  184. int framing;
  185. unsigned char s;
  186. s = (GET(TPOP_APM) & SUNI_TPOP_APM_S) >> SUNI_TPOP_APM_S_SHIFT;
  187. if (s == SUNI_TPOP_S_SONET)
  188. framing = SONET_FRAME_SONET;
  189. else
  190. framing = SONET_FRAME_SDH;
  191. return put_user(framing, (int __user *) arg) ? -EFAULT : 0;
  192. }
  193. static int set_framing(struct atm_dev *dev, void __user *arg)
  194. {
  195. int mode;
  196. if (get_user(mode, (int __user *) arg))
  197. return -EFAULT;
  198. if (mode == SONET_FRAME_SONET)
  199. return set_sonet(dev);
  200. else if (mode == SONET_FRAME_SDH)
  201. return set_sdh(dev);
  202. return -EINVAL;
  203. }
  204. static int suni_ioctl(struct atm_dev *dev,unsigned int cmd,void __user *arg)
  205. {
  206. switch (cmd) {
  207. case SONET_GETSTATZ:
  208. case SONET_GETSTAT:
  209. return fetch_stats(dev, arg, cmd == SONET_GETSTATZ);
  210. case SONET_SETDIAG:
  211. return change_diag(dev,arg,1);
  212. case SONET_CLRDIAG:
  213. return change_diag(dev,arg,0);
  214. case SONET_GETDIAG:
  215. return get_diag(dev,arg);
  216. case SONET_SETFRAMING:
  217. if (!capable(CAP_NET_ADMIN))
  218. return -EPERM;
  219. return set_framing(dev, arg);
  220. case SONET_GETFRAMING:
  221. return get_framing(dev, arg);
  222. case SONET_GETFRSENSE:
  223. return -EINVAL;
  224. case ATM_SETLOOP:
  225. if (!capable(CAP_NET_ADMIN))
  226. return -EPERM;
  227. return set_loopback(dev,(int)(unsigned long)arg);
  228. case ATM_GETLOOP:
  229. return put_user(PRIV(dev)->loop_mode,(int __user *)arg) ?
  230. -EFAULT : 0;
  231. case ATM_QUERYLOOP:
  232. return put_user(ATM_LM_LOC_PHY | ATM_LM_RMT_PHY,
  233. (int __user *) arg) ? -EFAULT : 0;
  234. default:
  235. return -ENOIOCTLCMD;
  236. }
  237. }
  238. static void poll_los(struct atm_dev *dev)
  239. {
  240. atm_dev_signal_change(dev,
  241. GET(RSOP_SIS) & SUNI_RSOP_SIS_LOSV ?
  242. ATM_PHY_SIG_LOST : ATM_PHY_SIG_FOUND);
  243. }
  244. static void suni_int(struct atm_dev *dev)
  245. {
  246. poll_los(dev);
  247. printk(KERN_NOTICE "%s(itf %d): signal %s\n",dev->type,dev->number,
  248. dev->signal == ATM_PHY_SIG_LOST ? "lost" : "detected again");
  249. }
  250. static int suni_start(struct atm_dev *dev)
  251. {
  252. unsigned long flags;
  253. int first;
  254. spin_lock_irqsave(&sunis_lock,flags);
  255. first = !sunis;
  256. PRIV(dev)->next = sunis;
  257. sunis = PRIV(dev);
  258. spin_unlock_irqrestore(&sunis_lock,flags);
  259. memset(&PRIV(dev)->sonet_stats,0,sizeof(struct k_sonet_stats));
  260. PUT(GET(RSOP_CIE) | SUNI_RSOP_CIE_LOSE,RSOP_CIE);
  261. /* interrupt on loss of signal */
  262. poll_los(dev); /* ... and clear SUNI interrupts */
  263. if (dev->signal == ATM_PHY_SIG_LOST)
  264. printk(KERN_WARNING "%s(itf %d): no signal\n",dev->type,
  265. dev->number);
  266. PRIV(dev)->loop_mode = ATM_LM_NONE;
  267. suni_hz(0); /* clear SUNI counters */
  268. (void) fetch_stats(dev,NULL,1); /* clear kernel counters */
  269. if (first) {
  270. init_timer(&poll_timer);
  271. poll_timer.expires = jiffies+HZ;
  272. poll_timer.function = suni_hz;
  273. poll_timer.data = 1;
  274. #if 0
  275. printk(KERN_DEBUG "[u] p=0x%lx,n=0x%lx\n",(unsigned long) poll_timer.list.prev,
  276. (unsigned long) poll_timer.list.next);
  277. #endif
  278. add_timer(&poll_timer);
  279. }
  280. return 0;
  281. }
  282. static int suni_stop(struct atm_dev *dev)
  283. {
  284. struct suni_priv **walk;
  285. unsigned long flags;
  286. /* let SAR driver worry about stopping interrupts */
  287. spin_lock_irqsave(&sunis_lock,flags);
  288. for (walk = &sunis; *walk != PRIV(dev);
  289. walk = &PRIV((*walk)->dev)->next);
  290. *walk = PRIV((*walk)->dev)->next;
  291. if (!sunis) del_timer_sync(&poll_timer);
  292. spin_unlock_irqrestore(&sunis_lock,flags);
  293. kfree(PRIV(dev));
  294. return 0;
  295. }
  296. static const struct atmphy_ops suni_ops = {
  297. .start = suni_start,
  298. .ioctl = suni_ioctl,
  299. .interrupt = suni_int,
  300. .stop = suni_stop,
  301. };
  302. int suni_init(struct atm_dev *dev)
  303. {
  304. unsigned char mri;
  305. if (!(dev->phy_data = kmalloc(sizeof(struct suni_priv),GFP_KERNEL)))
  306. return -ENOMEM;
  307. PRIV(dev)->dev = dev;
  308. mri = GET(MRI); /* reset SUNI */
  309. PRIV(dev)->type = (mri & SUNI_MRI_TYPE) >> SUNI_MRI_TYPE_SHIFT;
  310. PUT(mri | SUNI_MRI_RESET,MRI);
  311. PUT(mri,MRI);
  312. PUT((GET(MT) & SUNI_MT_DS27_53),MT); /* disable all tests */
  313. set_sonet(dev);
  314. REG_CHANGE(SUNI_TACP_IUCHP_CLP,0,SUNI_TACP_IUCHP_CLP,
  315. TACP_IUCHP); /* idle cells */
  316. PUT(SUNI_IDLE_PATTERN,TACP_IUCPOP);
  317. dev->phy = &suni_ops;
  318. return 0;
  319. }
  320. EXPORT_SYMBOL(suni_init);
  321. MODULE_LICENSE("GPL");