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