fec_ptp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * Fast Ethernet Controller (ENET) PTP driver for MX6x.
  3. *
  4. * Copyright (C) 2012 Freescale Semiconductor, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/string.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/errno.h>
  24. #include <linux/ioport.h>
  25. #include <linux/slab.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/pci.h>
  28. #include <linux/init.h>
  29. #include <linux/delay.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/bitops.h>
  36. #include <linux/io.h>
  37. #include <linux/irq.h>
  38. #include <linux/clk.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/phy.h>
  41. #include <linux/fec.h>
  42. #include <linux/of.h>
  43. #include <linux/of_device.h>
  44. #include <linux/of_gpio.h>
  45. #include <linux/of_net.h>
  46. #include "fec.h"
  47. /* FEC 1588 register bits */
  48. #define FEC_T_CTRL_SLAVE 0x00002000
  49. #define FEC_T_CTRL_CAPTURE 0x00000800
  50. #define FEC_T_CTRL_RESTART 0x00000200
  51. #define FEC_T_CTRL_PERIOD_RST 0x00000030
  52. #define FEC_T_CTRL_PERIOD_EN 0x00000010
  53. #define FEC_T_CTRL_ENABLE 0x00000001
  54. #define FEC_T_INC_MASK 0x0000007f
  55. #define FEC_T_INC_OFFSET 0
  56. #define FEC_T_INC_CORR_MASK 0x00007f00
  57. #define FEC_T_INC_CORR_OFFSET 8
  58. #define FEC_ATIME_CTRL 0x400
  59. #define FEC_ATIME 0x404
  60. #define FEC_ATIME_EVT_OFFSET 0x408
  61. #define FEC_ATIME_EVT_PERIOD 0x40c
  62. #define FEC_ATIME_CORR 0x410
  63. #define FEC_ATIME_INC 0x414
  64. #define FEC_TS_TIMESTAMP 0x418
  65. #define FEC_CC_MULT (1 << 31)
  66. /**
  67. * fec_ptp_read - read raw cycle counter (to be used by time counter)
  68. * @cc: the cyclecounter structure
  69. *
  70. * this function reads the cyclecounter registers and is called by the
  71. * cyclecounter structure used to construct a ns counter from the
  72. * arbitrary fixed point registers
  73. */
  74. static cycle_t fec_ptp_read(const struct cyclecounter *cc)
  75. {
  76. struct fec_enet_private *fep =
  77. container_of(cc, struct fec_enet_private, cc);
  78. u32 tempval;
  79. tempval = readl(fep->hwp + FEC_ATIME_CTRL);
  80. tempval |= FEC_T_CTRL_CAPTURE;
  81. writel(tempval, fep->hwp + FEC_ATIME_CTRL);
  82. return readl(fep->hwp + FEC_ATIME);
  83. }
  84. /**
  85. * fec_ptp_start_cyclecounter - create the cycle counter from hw
  86. * @ndev: network device
  87. *
  88. * this function initializes the timecounter and cyclecounter
  89. * structures for use in generated a ns counter from the arbitrary
  90. * fixed point cycles registers in the hardware.
  91. */
  92. void fec_ptp_start_cyclecounter(struct net_device *ndev)
  93. {
  94. struct fec_enet_private *fep = netdev_priv(ndev);
  95. unsigned long flags;
  96. int inc;
  97. inc = 1000000000 / clk_get_rate(fep->clk_ptp);
  98. /* grab the ptp lock */
  99. spin_lock_irqsave(&fep->tmreg_lock, flags);
  100. /* 1ns counter */
  101. writel(inc << FEC_T_INC_OFFSET, fep->hwp + FEC_ATIME_INC);
  102. /* use free running count */
  103. writel(0, fep->hwp + FEC_ATIME_EVT_PERIOD);
  104. writel(FEC_T_CTRL_ENABLE, fep->hwp + FEC_ATIME_CTRL);
  105. memset(&fep->cc, 0, sizeof(fep->cc));
  106. fep->cc.read = fec_ptp_read;
  107. fep->cc.mask = CLOCKSOURCE_MASK(32);
  108. fep->cc.shift = 31;
  109. fep->cc.mult = FEC_CC_MULT;
  110. /* reset the ns time counter */
  111. timecounter_init(&fep->tc, &fep->cc, ktime_to_ns(ktime_get_real()));
  112. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  113. }
  114. /**
  115. * fec_ptp_adjfreq - adjust ptp cycle frequency
  116. * @ptp: the ptp clock structure
  117. * @ppb: parts per billion adjustment from base
  118. *
  119. * Adjust the frequency of the ptp cycle counter by the
  120. * indicated ppb from the base frequency.
  121. *
  122. * Because ENET hardware frequency adjust is complex,
  123. * using software method to do that.
  124. */
  125. static int fec_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  126. {
  127. u64 diff;
  128. unsigned long flags;
  129. int neg_adj = 0;
  130. struct fec_enet_private *fep =
  131. container_of(ptp, struct fec_enet_private, ptp_caps);
  132. if (ppb < 0) {
  133. ppb = -ppb;
  134. neg_adj = 1;
  135. }
  136. spin_lock_irqsave(&fep->tmreg_lock, flags);
  137. /*
  138. * dummy read to set cycle_last in tc to now.
  139. * So use adjusted mult to calculate when next call
  140. * timercounter_read.
  141. */
  142. timecounter_read(&fep->tc);
  143. fep->cc.mult = FEC_CC_MULT;
  144. diff = fep->cc.mult;
  145. diff *= ppb;
  146. diff = div_u64(diff, 1000000000ULL);
  147. if (neg_adj)
  148. fep->cc.mult -= diff;
  149. else
  150. fep->cc.mult += diff;
  151. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  152. return 0;
  153. }
  154. /**
  155. * fec_ptp_adjtime
  156. * @ptp: the ptp clock structure
  157. * @delta: offset to adjust the cycle counter by
  158. *
  159. * adjust the timer by resetting the timecounter structure.
  160. */
  161. static int fec_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  162. {
  163. struct fec_enet_private *fep =
  164. container_of(ptp, struct fec_enet_private, ptp_caps);
  165. unsigned long flags;
  166. u64 now;
  167. spin_lock_irqsave(&fep->tmreg_lock, flags);
  168. now = timecounter_read(&fep->tc);
  169. now += delta;
  170. /* reset the timecounter */
  171. timecounter_init(&fep->tc, &fep->cc, now);
  172. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  173. return 0;
  174. }
  175. /**
  176. * fec_ptp_gettime
  177. * @ptp: the ptp clock structure
  178. * @ts: timespec structure to hold the current time value
  179. *
  180. * read the timecounter and return the correct value on ns,
  181. * after converting it into a struct timespec.
  182. */
  183. static int fec_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  184. {
  185. struct fec_enet_private *adapter =
  186. container_of(ptp, struct fec_enet_private, ptp_caps);
  187. u64 ns;
  188. u32 remainder;
  189. unsigned long flags;
  190. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  191. ns = timecounter_read(&adapter->tc);
  192. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  193. ts->tv_sec = div_u64_rem(ns, 1000000000ULL, &remainder);
  194. ts->tv_nsec = remainder;
  195. return 0;
  196. }
  197. /**
  198. * fec_ptp_settime
  199. * @ptp: the ptp clock structure
  200. * @ts: the timespec containing the new time for the cycle counter
  201. *
  202. * reset the timecounter to use a new base value instead of the kernel
  203. * wall timer value.
  204. */
  205. static int fec_ptp_settime(struct ptp_clock_info *ptp,
  206. const struct timespec *ts)
  207. {
  208. struct fec_enet_private *fep =
  209. container_of(ptp, struct fec_enet_private, ptp_caps);
  210. u64 ns;
  211. unsigned long flags;
  212. ns = ts->tv_sec * 1000000000ULL;
  213. ns += ts->tv_nsec;
  214. spin_lock_irqsave(&fep->tmreg_lock, flags);
  215. timecounter_init(&fep->tc, &fep->cc, ns);
  216. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  217. return 0;
  218. }
  219. /**
  220. * fec_ptp_enable
  221. * @ptp: the ptp clock structure
  222. * @rq: the requested feature to change
  223. * @on: whether to enable or disable the feature
  224. *
  225. */
  226. static int fec_ptp_enable(struct ptp_clock_info *ptp,
  227. struct ptp_clock_request *rq, int on)
  228. {
  229. return -EOPNOTSUPP;
  230. }
  231. /**
  232. * fec_ptp_hwtstamp_ioctl - control hardware time stamping
  233. * @ndev: pointer to net_device
  234. * @ifreq: ioctl data
  235. * @cmd: particular ioctl requested
  236. */
  237. int fec_ptp_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
  238. {
  239. struct fec_enet_private *fep = netdev_priv(ndev);
  240. struct hwtstamp_config config;
  241. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  242. return -EFAULT;
  243. /* reserved for future extensions */
  244. if (config.flags)
  245. return -EINVAL;
  246. switch (config.tx_type) {
  247. case HWTSTAMP_TX_OFF:
  248. fep->hwts_tx_en = 0;
  249. break;
  250. case HWTSTAMP_TX_ON:
  251. fep->hwts_tx_en = 1;
  252. break;
  253. default:
  254. return -ERANGE;
  255. }
  256. switch (config.rx_filter) {
  257. case HWTSTAMP_FILTER_NONE:
  258. if (fep->hwts_rx_en)
  259. fep->hwts_rx_en = 0;
  260. config.rx_filter = HWTSTAMP_FILTER_NONE;
  261. break;
  262. default:
  263. /*
  264. * register RXMTRL must be set in order to do V1 packets,
  265. * therefore it is not possible to time stamp both V1 Sync and
  266. * Delay_Req messages and hardware does not support
  267. * timestamping all packets => return error
  268. */
  269. fep->hwts_rx_en = 1;
  270. config.rx_filter = HWTSTAMP_FILTER_ALL;
  271. break;
  272. }
  273. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  274. -EFAULT : 0;
  275. }
  276. /**
  277. * fec_time_keep - call timecounter_read every second to avoid timer overrun
  278. * because ENET just support 32bit counter, will timeout in 4s
  279. */
  280. static void fec_time_keep(unsigned long _data)
  281. {
  282. struct fec_enet_private *fep = (struct fec_enet_private *)_data;
  283. u64 ns;
  284. unsigned long flags;
  285. spin_lock_irqsave(&fep->tmreg_lock, flags);
  286. ns = timecounter_read(&fep->tc);
  287. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  288. mod_timer(&fep->time_keep, jiffies + HZ);
  289. }
  290. /**
  291. * fec_ptp_init
  292. * @ndev: The FEC network adapter
  293. *
  294. * This function performs the required steps for enabling ptp
  295. * support. If ptp support has already been loaded it simply calls the
  296. * cyclecounter init routine and exits.
  297. */
  298. void fec_ptp_init(struct net_device *ndev, struct platform_device *pdev)
  299. {
  300. struct fec_enet_private *fep = netdev_priv(ndev);
  301. fep->ptp_caps.owner = THIS_MODULE;
  302. snprintf(fep->ptp_caps.name, 16, "fec ptp");
  303. fep->ptp_caps.max_adj = 250000000;
  304. fep->ptp_caps.n_alarm = 0;
  305. fep->ptp_caps.n_ext_ts = 0;
  306. fep->ptp_caps.n_per_out = 0;
  307. fep->ptp_caps.pps = 0;
  308. fep->ptp_caps.adjfreq = fec_ptp_adjfreq;
  309. fep->ptp_caps.adjtime = fec_ptp_adjtime;
  310. fep->ptp_caps.gettime = fec_ptp_gettime;
  311. fep->ptp_caps.settime = fec_ptp_settime;
  312. fep->ptp_caps.enable = fec_ptp_enable;
  313. spin_lock_init(&fep->tmreg_lock);
  314. fec_ptp_start_cyclecounter(ndev);
  315. init_timer(&fep->time_keep);
  316. fep->time_keep.data = (unsigned long)fep;
  317. fep->time_keep.function = fec_time_keep;
  318. fep->time_keep.expires = jiffies + HZ;
  319. add_timer(&fep->time_keep);
  320. fep->ptp_clock = ptp_clock_register(&fep->ptp_caps, &pdev->dev);
  321. if (IS_ERR(fep->ptp_clock)) {
  322. fep->ptp_clock = NULL;
  323. pr_err("ptp_clock_register failed\n");
  324. } else {
  325. pr_info("registered PHC device on %s\n", ndev->name);
  326. }
  327. }