suni.c 9.8 KB

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