lan743x_ptp.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /* Copyright (C) 2018 Microchip Technology Inc. */
  3. #include <linux/netdevice.h>
  4. #include "lan743x_main.h"
  5. #include <linux/ptp_clock_kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/pci.h>
  8. #include <linux/net_tstamp.h>
  9. #include "lan743x_ptp.h"
  10. #define LAN743X_NUMBER_OF_GPIO (12)
  11. #define LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB (31249999)
  12. #define LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM (2047999934)
  13. static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter);
  14. static void lan743x_ptp_enable(struct lan743x_adapter *adapter);
  15. static void lan743x_ptp_disable(struct lan743x_adapter *adapter);
  16. static void lan743x_ptp_reset(struct lan743x_adapter *adapter);
  17. static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
  18. u32 seconds, u32 nano_seconds,
  19. u32 sub_nano_seconds);
  20. int lan743x_gpio_init(struct lan743x_adapter *adapter)
  21. {
  22. struct lan743x_gpio *gpio = &adapter->gpio;
  23. spin_lock_init(&gpio->gpio_lock);
  24. gpio->gpio_cfg0 = 0; /* set all direction to input, data = 0 */
  25. gpio->gpio_cfg1 = 0x0FFF0000;/* disable all gpio, set to open drain */
  26. gpio->gpio_cfg2 = 0;/* set all to 1588 low polarity level */
  27. gpio->gpio_cfg3 = 0;/* disable all 1588 output */
  28. lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
  29. lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
  30. lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
  31. lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);
  32. return 0;
  33. }
  34. static void lan743x_ptp_wait_till_cmd_done(struct lan743x_adapter *adapter,
  35. u32 bit_mask)
  36. {
  37. int timeout = 1000;
  38. u32 data = 0;
  39. while (timeout &&
  40. (data = (lan743x_csr_read(adapter, PTP_CMD_CTL) &
  41. bit_mask))) {
  42. usleep_range(1000, 20000);
  43. timeout--;
  44. }
  45. if (data) {
  46. netif_err(adapter, drv, adapter->netdev,
  47. "timeout waiting for cmd to be done, cmd = 0x%08X\n",
  48. bit_mask);
  49. }
  50. }
  51. static void lan743x_ptp_tx_ts_enqueue_ts(struct lan743x_adapter *adapter,
  52. u32 seconds, u32 nano_seconds,
  53. u32 header)
  54. {
  55. struct lan743x_ptp *ptp = &adapter->ptp;
  56. spin_lock_bh(&ptp->tx_ts_lock);
  57. if (ptp->tx_ts_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
  58. ptp->tx_ts_seconds_queue[ptp->tx_ts_queue_size] = seconds;
  59. ptp->tx_ts_nseconds_queue[ptp->tx_ts_queue_size] = nano_seconds;
  60. ptp->tx_ts_header_queue[ptp->tx_ts_queue_size] = header;
  61. ptp->tx_ts_queue_size++;
  62. } else {
  63. netif_err(adapter, drv, adapter->netdev,
  64. "tx ts queue overflow\n");
  65. }
  66. spin_unlock_bh(&ptp->tx_ts_lock);
  67. }
  68. static void lan743x_ptp_tx_ts_complete(struct lan743x_adapter *adapter)
  69. {
  70. struct lan743x_ptp *ptp = &adapter->ptp;
  71. struct skb_shared_hwtstamps tstamps;
  72. u32 header, nseconds, seconds;
  73. bool ignore_sync = false;
  74. struct sk_buff *skb;
  75. int c, i;
  76. spin_lock_bh(&ptp->tx_ts_lock);
  77. c = ptp->tx_ts_skb_queue_size;
  78. if (c > ptp->tx_ts_queue_size)
  79. c = ptp->tx_ts_queue_size;
  80. if (c <= 0)
  81. goto done;
  82. for (i = 0; i < c; i++) {
  83. ignore_sync = ((ptp->tx_ts_ignore_sync_queue &
  84. BIT(i)) != 0);
  85. skb = ptp->tx_ts_skb_queue[i];
  86. nseconds = ptp->tx_ts_nseconds_queue[i];
  87. seconds = ptp->tx_ts_seconds_queue[i];
  88. header = ptp->tx_ts_header_queue[i];
  89. memset(&tstamps, 0, sizeof(tstamps));
  90. tstamps.hwtstamp = ktime_set(seconds, nseconds);
  91. if (!ignore_sync ||
  92. ((header & PTP_TX_MSG_HEADER_MSG_TYPE_) !=
  93. PTP_TX_MSG_HEADER_MSG_TYPE_SYNC_))
  94. skb_tstamp_tx(skb, &tstamps);
  95. dev_kfree_skb(skb);
  96. ptp->tx_ts_skb_queue[i] = NULL;
  97. ptp->tx_ts_seconds_queue[i] = 0;
  98. ptp->tx_ts_nseconds_queue[i] = 0;
  99. ptp->tx_ts_header_queue[i] = 0;
  100. }
  101. /* shift queue */
  102. ptp->tx_ts_ignore_sync_queue >>= c;
  103. for (i = c; i < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS; i++) {
  104. ptp->tx_ts_skb_queue[i - c] = ptp->tx_ts_skb_queue[i];
  105. ptp->tx_ts_seconds_queue[i - c] = ptp->tx_ts_seconds_queue[i];
  106. ptp->tx_ts_nseconds_queue[i - c] = ptp->tx_ts_nseconds_queue[i];
  107. ptp->tx_ts_header_queue[i - c] = ptp->tx_ts_header_queue[i];
  108. ptp->tx_ts_skb_queue[i] = NULL;
  109. ptp->tx_ts_seconds_queue[i] = 0;
  110. ptp->tx_ts_nseconds_queue[i] = 0;
  111. ptp->tx_ts_header_queue[i] = 0;
  112. }
  113. ptp->tx_ts_skb_queue_size -= c;
  114. ptp->tx_ts_queue_size -= c;
  115. done:
  116. ptp->pending_tx_timestamps -= c;
  117. spin_unlock_bh(&ptp->tx_ts_lock);
  118. }
  119. static int lan743x_ptp_reserve_event_ch(struct lan743x_adapter *adapter)
  120. {
  121. struct lan743x_ptp *ptp = &adapter->ptp;
  122. int result = -ENODEV;
  123. int index = 0;
  124. mutex_lock(&ptp->command_lock);
  125. for (index = 0; index < LAN743X_PTP_NUMBER_OF_EVENT_CHANNELS; index++) {
  126. if (!(test_bit(index, &ptp->used_event_ch))) {
  127. ptp->used_event_ch |= BIT(index);
  128. result = index;
  129. break;
  130. }
  131. }
  132. mutex_unlock(&ptp->command_lock);
  133. return result;
  134. }
  135. static void lan743x_ptp_release_event_ch(struct lan743x_adapter *adapter,
  136. int event_channel)
  137. {
  138. struct lan743x_ptp *ptp = &adapter->ptp;
  139. mutex_lock(&ptp->command_lock);
  140. if (test_bit(event_channel, &ptp->used_event_ch)) {
  141. ptp->used_event_ch &= ~BIT(event_channel);
  142. } else {
  143. netif_warn(adapter, drv, adapter->netdev,
  144. "attempted release on a not used event_channel = %d\n",
  145. event_channel);
  146. }
  147. mutex_unlock(&ptp->command_lock);
  148. }
  149. static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
  150. u32 *seconds, u32 *nano_seconds,
  151. u32 *sub_nano_seconds);
  152. static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
  153. s64 time_step_ns);
  154. static int lan743x_gpio_rsrv_ptp_out(struct lan743x_adapter *adapter,
  155. int bit, int ptp_channel)
  156. {
  157. struct lan743x_gpio *gpio = &adapter->gpio;
  158. unsigned long irq_flags = 0;
  159. int bit_mask = BIT(bit);
  160. int ret = -EBUSY;
  161. spin_lock_irqsave(&gpio->gpio_lock, irq_flags);
  162. if (!(gpio->used_bits & bit_mask)) {
  163. gpio->used_bits |= bit_mask;
  164. gpio->output_bits |= bit_mask;
  165. gpio->ptp_bits |= bit_mask;
  166. /* set as output, and zero initial value */
  167. gpio->gpio_cfg0 |= GPIO_CFG0_GPIO_DIR_BIT_(bit);
  168. gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(bit);
  169. lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
  170. /* enable gpio, and set buffer type to push pull */
  171. gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOEN_BIT_(bit);
  172. gpio->gpio_cfg1 |= GPIO_CFG1_GPIOBUF_BIT_(bit);
  173. lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
  174. /* set 1588 polarity to high */
  175. gpio->gpio_cfg2 |= GPIO_CFG2_1588_POL_BIT_(bit);
  176. lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
  177. if (!ptp_channel) {
  178. /* use channel A */
  179. gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_CH_SEL_BIT_(bit);
  180. } else {
  181. /* use channel B */
  182. gpio->gpio_cfg3 |= GPIO_CFG3_1588_CH_SEL_BIT_(bit);
  183. }
  184. gpio->gpio_cfg3 |= GPIO_CFG3_1588_OE_BIT_(bit);
  185. lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);
  186. ret = bit;
  187. }
  188. spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
  189. return ret;
  190. }
  191. static void lan743x_gpio_release(struct lan743x_adapter *adapter, int bit)
  192. {
  193. struct lan743x_gpio *gpio = &adapter->gpio;
  194. unsigned long irq_flags = 0;
  195. int bit_mask = BIT(bit);
  196. spin_lock_irqsave(&gpio->gpio_lock, irq_flags);
  197. if (gpio->used_bits & bit_mask) {
  198. gpio->used_bits &= ~bit_mask;
  199. if (gpio->output_bits & bit_mask) {
  200. gpio->output_bits &= ~bit_mask;
  201. if (gpio->ptp_bits & bit_mask) {
  202. gpio->ptp_bits &= ~bit_mask;
  203. /* disable ptp output */
  204. gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_OE_BIT_(bit);
  205. lan743x_csr_write(adapter, GPIO_CFG3,
  206. gpio->gpio_cfg3);
  207. }
  208. /* release gpio output */
  209. /* disable gpio */
  210. gpio->gpio_cfg1 |= GPIO_CFG1_GPIOEN_BIT_(bit);
  211. gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOBUF_BIT_(bit);
  212. lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
  213. /* reset back to input */
  214. gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DIR_BIT_(bit);
  215. gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(bit);
  216. lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
  217. }
  218. }
  219. spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
  220. }
  221. static int lan743x_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
  222. {
  223. struct lan743x_ptp *ptp =
  224. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  225. struct lan743x_adapter *adapter =
  226. container_of(ptp, struct lan743x_adapter, ptp);
  227. u32 lan743x_rate_adj = 0;
  228. bool positive = true;
  229. u64 u64_delta = 0;
  230. if ((scaled_ppm < (-LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM)) ||
  231. scaled_ppm > LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM) {
  232. return -EINVAL;
  233. }
  234. if (scaled_ppm > 0) {
  235. u64_delta = (u64)scaled_ppm;
  236. positive = true;
  237. } else {
  238. u64_delta = (u64)(-scaled_ppm);
  239. positive = false;
  240. }
  241. u64_delta = (u64_delta << 19);
  242. lan743x_rate_adj = div_u64(u64_delta, 1000000);
  243. if (positive)
  244. lan743x_rate_adj |= PTP_CLOCK_RATE_ADJ_DIR_;
  245. lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
  246. lan743x_rate_adj);
  247. return 0;
  248. }
  249. static int lan743x_ptpci_adjfreq(struct ptp_clock_info *ptpci, s32 delta_ppb)
  250. {
  251. struct lan743x_ptp *ptp =
  252. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  253. struct lan743x_adapter *adapter =
  254. container_of(ptp, struct lan743x_adapter, ptp);
  255. u32 lan743x_rate_adj = 0;
  256. bool positive = true;
  257. u32 u32_delta = 0;
  258. u64 u64_delta = 0;
  259. if ((delta_ppb < (-LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB)) ||
  260. delta_ppb > LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB) {
  261. return -EINVAL;
  262. }
  263. if (delta_ppb > 0) {
  264. u32_delta = (u32)delta_ppb;
  265. positive = true;
  266. } else {
  267. u32_delta = (u32)(-delta_ppb);
  268. positive = false;
  269. }
  270. u64_delta = (((u64)u32_delta) << 35);
  271. lan743x_rate_adj = div_u64(u64_delta, 1000000000);
  272. if (positive)
  273. lan743x_rate_adj |= PTP_CLOCK_RATE_ADJ_DIR_;
  274. lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
  275. lan743x_rate_adj);
  276. return 0;
  277. }
  278. static int lan743x_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
  279. {
  280. struct lan743x_ptp *ptp =
  281. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  282. struct lan743x_adapter *adapter =
  283. container_of(ptp, struct lan743x_adapter, ptp);
  284. lan743x_ptp_clock_step(adapter, delta);
  285. return 0;
  286. }
  287. static int lan743x_ptpci_gettime64(struct ptp_clock_info *ptpci,
  288. struct timespec64 *ts)
  289. {
  290. struct lan743x_ptp *ptp =
  291. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  292. struct lan743x_adapter *adapter =
  293. container_of(ptp, struct lan743x_adapter, ptp);
  294. u32 nano_seconds = 0;
  295. u32 seconds = 0;
  296. lan743x_ptp_clock_get(adapter, &seconds, &nano_seconds, NULL);
  297. ts->tv_sec = seconds;
  298. ts->tv_nsec = nano_seconds;
  299. return 0;
  300. }
  301. static int lan743x_ptpci_settime64(struct ptp_clock_info *ptpci,
  302. const struct timespec64 *ts)
  303. {
  304. struct lan743x_ptp *ptp =
  305. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  306. struct lan743x_adapter *adapter =
  307. container_of(ptp, struct lan743x_adapter, ptp);
  308. u32 nano_seconds = 0;
  309. u32 seconds = 0;
  310. if (ts) {
  311. if (ts->tv_sec > 0xFFFFFFFFLL ||
  312. ts->tv_sec < 0) {
  313. netif_warn(adapter, drv, adapter->netdev,
  314. "ts->tv_sec out of range, %lld\n",
  315. ts->tv_sec);
  316. return -ERANGE;
  317. }
  318. if (ts->tv_nsec >= 1000000000L ||
  319. ts->tv_nsec < 0) {
  320. netif_warn(adapter, drv, adapter->netdev,
  321. "ts->tv_nsec out of range, %ld\n",
  322. ts->tv_nsec);
  323. return -ERANGE;
  324. }
  325. seconds = ts->tv_sec;
  326. nano_seconds = ts->tv_nsec;
  327. lan743x_ptp_clock_set(adapter, seconds, nano_seconds, 0);
  328. } else {
  329. netif_warn(adapter, drv, adapter->netdev, "ts == NULL\n");
  330. return -EINVAL;
  331. }
  332. return 0;
  333. }
  334. static void lan743x_ptp_perout_off(struct lan743x_adapter *adapter)
  335. {
  336. struct lan743x_ptp *ptp = &adapter->ptp;
  337. u32 general_config = 0;
  338. if (ptp->perout_gpio_bit >= 0) {
  339. lan743x_gpio_release(adapter, ptp->perout_gpio_bit);
  340. ptp->perout_gpio_bit = -1;
  341. }
  342. if (ptp->perout_event_ch >= 0) {
  343. /* set target to far in the future, effectively disabling it */
  344. lan743x_csr_write(adapter,
  345. PTP_CLOCK_TARGET_SEC_X(ptp->perout_event_ch),
  346. 0xFFFF0000);
  347. lan743x_csr_write(adapter,
  348. PTP_CLOCK_TARGET_NS_X(ptp->perout_event_ch),
  349. 0);
  350. general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
  351. general_config |= PTP_GENERAL_CONFIG_RELOAD_ADD_X_
  352. (ptp->perout_event_ch);
  353. lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
  354. lan743x_ptp_release_event_ch(adapter, ptp->perout_event_ch);
  355. ptp->perout_event_ch = -1;
  356. }
  357. }
  358. static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
  359. struct ptp_perout_request *perout)
  360. {
  361. struct lan743x_ptp *ptp = &adapter->ptp;
  362. u32 period_sec = 0, period_nsec = 0;
  363. u32 start_sec = 0, start_nsec = 0;
  364. u32 general_config = 0;
  365. int pulse_width = 0;
  366. int perout_bit = 0;
  367. /* Reject requests with unsupported flags */
  368. if (perout->flags)
  369. return -EOPNOTSUPP;
  370. if (!on) {
  371. lan743x_ptp_perout_off(adapter);
  372. return 0;
  373. }
  374. if (ptp->perout_event_ch >= 0 ||
  375. ptp->perout_gpio_bit >= 0) {
  376. /* already on, turn off first */
  377. lan743x_ptp_perout_off(adapter);
  378. }
  379. ptp->perout_event_ch = lan743x_ptp_reserve_event_ch(adapter);
  380. if (ptp->perout_event_ch < 0) {
  381. netif_warn(adapter, drv, adapter->netdev,
  382. "Failed to reserve event channel for PEROUT\n");
  383. goto failed;
  384. }
  385. switch (adapter->csr.id_rev & ID_REV_ID_MASK_) {
  386. case ID_REV_ID_LAN7430_:
  387. perout_bit = 2;/* GPIO 2 is preferred on EVB LAN7430 */
  388. break;
  389. case ID_REV_ID_LAN7431_:
  390. perout_bit = 4;/* GPIO 4 is preferred on EVB LAN7431 */
  391. break;
  392. }
  393. ptp->perout_gpio_bit = lan743x_gpio_rsrv_ptp_out(adapter,
  394. perout_bit,
  395. ptp->perout_event_ch);
  396. if (ptp->perout_gpio_bit < 0) {
  397. netif_warn(adapter, drv, adapter->netdev,
  398. "Failed to reserve gpio %d for PEROUT\n",
  399. perout_bit);
  400. goto failed;
  401. }
  402. start_sec = perout->start.sec;
  403. start_sec += perout->start.nsec / 1000000000;
  404. start_nsec = perout->start.nsec % 1000000000;
  405. period_sec = perout->period.sec;
  406. period_sec += perout->period.nsec / 1000000000;
  407. period_nsec = perout->period.nsec % 1000000000;
  408. if (period_sec == 0) {
  409. if (period_nsec >= 400000000) {
  410. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
  411. } else if (period_nsec >= 20000000) {
  412. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
  413. } else if (period_nsec >= 2000000) {
  414. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
  415. } else if (period_nsec >= 200000) {
  416. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
  417. } else if (period_nsec >= 20000) {
  418. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
  419. } else if (period_nsec >= 200) {
  420. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
  421. } else {
  422. netif_warn(adapter, drv, adapter->netdev,
  423. "perout period too small, minimum is 200nS\n");
  424. goto failed;
  425. }
  426. } else {
  427. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
  428. }
  429. /* turn off by setting target far in future */
  430. lan743x_csr_write(adapter,
  431. PTP_CLOCK_TARGET_SEC_X(ptp->perout_event_ch),
  432. 0xFFFF0000);
  433. lan743x_csr_write(adapter,
  434. PTP_CLOCK_TARGET_NS_X(ptp->perout_event_ch), 0);
  435. /* Configure to pulse every period */
  436. general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
  437. general_config &= ~(PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
  438. (ptp->perout_event_ch));
  439. general_config |= PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_
  440. (ptp->perout_event_ch, pulse_width);
  441. general_config &= ~PTP_GENERAL_CONFIG_RELOAD_ADD_X_
  442. (ptp->perout_event_ch);
  443. lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
  444. /* set the reload to one toggle cycle */
  445. lan743x_csr_write(adapter,
  446. PTP_CLOCK_TARGET_RELOAD_SEC_X(ptp->perout_event_ch),
  447. period_sec);
  448. lan743x_csr_write(adapter,
  449. PTP_CLOCK_TARGET_RELOAD_NS_X(ptp->perout_event_ch),
  450. period_nsec);
  451. /* set the start time */
  452. lan743x_csr_write(adapter,
  453. PTP_CLOCK_TARGET_SEC_X(ptp->perout_event_ch),
  454. start_sec);
  455. lan743x_csr_write(adapter,
  456. PTP_CLOCK_TARGET_NS_X(ptp->perout_event_ch),
  457. start_nsec);
  458. return 0;
  459. failed:
  460. lan743x_ptp_perout_off(adapter);
  461. return -ENODEV;
  462. }
  463. static int lan743x_ptpci_enable(struct ptp_clock_info *ptpci,
  464. struct ptp_clock_request *request, int on)
  465. {
  466. struct lan743x_ptp *ptp =
  467. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  468. struct lan743x_adapter *adapter =
  469. container_of(ptp, struct lan743x_adapter, ptp);
  470. if (request) {
  471. switch (request->type) {
  472. case PTP_CLK_REQ_EXTTS:
  473. return -EINVAL;
  474. case PTP_CLK_REQ_PEROUT:
  475. if (request->perout.index == 0)
  476. return lan743x_ptp_perout(adapter, on,
  477. &request->perout);
  478. return -EINVAL;
  479. case PTP_CLK_REQ_PPS:
  480. return -EINVAL;
  481. default:
  482. netif_err(adapter, drv, adapter->netdev,
  483. "request->type == %d, Unknown\n",
  484. request->type);
  485. break;
  486. }
  487. } else {
  488. netif_err(adapter, drv, adapter->netdev, "request == NULL\n");
  489. }
  490. return 0;
  491. }
  492. static long lan743x_ptpci_do_aux_work(struct ptp_clock_info *ptpci)
  493. {
  494. struct lan743x_ptp *ptp =
  495. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  496. struct lan743x_adapter *adapter =
  497. container_of(ptp, struct lan743x_adapter, ptp);
  498. u32 cap_info, cause, header, nsec, seconds;
  499. bool new_timestamp_available = false;
  500. int count = 0;
  501. while ((count < 100) &&
  502. (lan743x_csr_read(adapter, PTP_INT_STS) & PTP_INT_BIT_TX_TS_)) {
  503. count++;
  504. cap_info = lan743x_csr_read(adapter, PTP_CAP_INFO);
  505. if (PTP_CAP_INFO_TX_TS_CNT_GET_(cap_info) > 0) {
  506. seconds = lan743x_csr_read(adapter,
  507. PTP_TX_EGRESS_SEC);
  508. nsec = lan743x_csr_read(adapter, PTP_TX_EGRESS_NS);
  509. cause = (nsec &
  510. PTP_TX_EGRESS_NS_CAPTURE_CAUSE_MASK_);
  511. header = lan743x_csr_read(adapter,
  512. PTP_TX_MSG_HEADER);
  513. if (cause == PTP_TX_EGRESS_NS_CAPTURE_CAUSE_SW_) {
  514. nsec &= PTP_TX_EGRESS_NS_TS_NS_MASK_;
  515. lan743x_ptp_tx_ts_enqueue_ts(adapter,
  516. seconds, nsec,
  517. header);
  518. new_timestamp_available = true;
  519. } else if (cause ==
  520. PTP_TX_EGRESS_NS_CAPTURE_CAUSE_AUTO_) {
  521. netif_err(adapter, drv, adapter->netdev,
  522. "Auto capture cause not supported\n");
  523. } else {
  524. netif_warn(adapter, drv, adapter->netdev,
  525. "unknown tx timestamp capture cause\n");
  526. }
  527. } else {
  528. netif_warn(adapter, drv, adapter->netdev,
  529. "TX TS INT but no TX TS CNT\n");
  530. }
  531. lan743x_csr_write(adapter, PTP_INT_STS, PTP_INT_BIT_TX_TS_);
  532. }
  533. if (new_timestamp_available)
  534. lan743x_ptp_tx_ts_complete(adapter);
  535. lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
  536. return -1;
  537. }
  538. static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
  539. u32 *seconds, u32 *nano_seconds,
  540. u32 *sub_nano_seconds)
  541. {
  542. struct lan743x_ptp *ptp = &adapter->ptp;
  543. mutex_lock(&ptp->command_lock);
  544. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
  545. lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);
  546. if (seconds)
  547. (*seconds) = lan743x_csr_read(adapter, PTP_CLOCK_SEC);
  548. if (nano_seconds)
  549. (*nano_seconds) = lan743x_csr_read(adapter, PTP_CLOCK_NS);
  550. if (sub_nano_seconds)
  551. (*sub_nano_seconds) =
  552. lan743x_csr_read(adapter, PTP_CLOCK_SUBNS);
  553. mutex_unlock(&ptp->command_lock);
  554. }
  555. static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
  556. s64 time_step_ns)
  557. {
  558. struct lan743x_ptp *ptp = &adapter->ptp;
  559. u32 nano_seconds_step = 0;
  560. u64 abs_time_step_ns = 0;
  561. u32 unsigned_seconds = 0;
  562. u32 nano_seconds = 0;
  563. u32 remainder = 0;
  564. s32 seconds = 0;
  565. if (time_step_ns > 15000000000LL) {
  566. /* convert to clock set */
  567. lan743x_ptp_clock_get(adapter, &unsigned_seconds,
  568. &nano_seconds, NULL);
  569. unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
  570. &remainder);
  571. nano_seconds += remainder;
  572. if (nano_seconds >= 1000000000) {
  573. unsigned_seconds++;
  574. nano_seconds -= 1000000000;
  575. }
  576. lan743x_ptp_clock_set(adapter, unsigned_seconds,
  577. nano_seconds, 0);
  578. return;
  579. } else if (time_step_ns < -15000000000LL) {
  580. /* convert to clock set */
  581. time_step_ns = -time_step_ns;
  582. lan743x_ptp_clock_get(adapter, &unsigned_seconds,
  583. &nano_seconds, NULL);
  584. unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
  585. &remainder);
  586. nano_seconds_step = remainder;
  587. if (nano_seconds < nano_seconds_step) {
  588. unsigned_seconds--;
  589. nano_seconds += 1000000000;
  590. }
  591. nano_seconds -= nano_seconds_step;
  592. lan743x_ptp_clock_set(adapter, unsigned_seconds,
  593. nano_seconds, 0);
  594. return;
  595. }
  596. /* do clock step */
  597. if (time_step_ns >= 0) {
  598. abs_time_step_ns = (u64)(time_step_ns);
  599. seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
  600. &remainder);
  601. nano_seconds = (u32)remainder;
  602. } else {
  603. abs_time_step_ns = (u64)(-time_step_ns);
  604. seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
  605. &remainder));
  606. nano_seconds = (u32)remainder;
  607. if (nano_seconds > 0) {
  608. /* subtracting nano seconds is not allowed
  609. * convert to subtracting from seconds,
  610. * and adding to nanoseconds
  611. */
  612. seconds--;
  613. nano_seconds = (1000000000 - nano_seconds);
  614. }
  615. }
  616. if (nano_seconds > 0) {
  617. /* add 8 ns to cover the likely normal increment */
  618. nano_seconds += 8;
  619. }
  620. if (nano_seconds >= 1000000000) {
  621. /* carry into seconds */
  622. seconds++;
  623. nano_seconds -= 1000000000;
  624. }
  625. while (seconds) {
  626. mutex_lock(&ptp->command_lock);
  627. if (seconds > 0) {
  628. u32 adjustment_value = (u32)seconds;
  629. if (adjustment_value > 0xF)
  630. adjustment_value = 0xF;
  631. lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
  632. PTP_CLOCK_STEP_ADJ_DIR_ |
  633. adjustment_value);
  634. seconds -= ((s32)adjustment_value);
  635. } else {
  636. u32 adjustment_value = (u32)(-seconds);
  637. if (adjustment_value > 0xF)
  638. adjustment_value = 0xF;
  639. lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
  640. adjustment_value);
  641. seconds += ((s32)adjustment_value);
  642. }
  643. lan743x_csr_write(adapter, PTP_CMD_CTL,
  644. PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
  645. lan743x_ptp_wait_till_cmd_done(adapter,
  646. PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
  647. mutex_unlock(&ptp->command_lock);
  648. }
  649. if (nano_seconds) {
  650. mutex_lock(&ptp->command_lock);
  651. lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
  652. PTP_CLOCK_STEP_ADJ_DIR_ |
  653. (nano_seconds &
  654. PTP_CLOCK_STEP_ADJ_VALUE_MASK_));
  655. lan743x_csr_write(adapter, PTP_CMD_CTL,
  656. PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
  657. lan743x_ptp_wait_till_cmd_done(adapter,
  658. PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
  659. mutex_unlock(&ptp->command_lock);
  660. }
  661. }
  662. void lan743x_ptp_isr(void *context)
  663. {
  664. struct lan743x_adapter *adapter = (struct lan743x_adapter *)context;
  665. struct lan743x_ptp *ptp = NULL;
  666. int enable_flag = 1;
  667. u32 ptp_int_sts = 0;
  668. ptp = &adapter->ptp;
  669. lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
  670. ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
  671. ptp_int_sts &= lan743x_csr_read(adapter, PTP_INT_EN_SET);
  672. if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
  673. ptp_schedule_worker(ptp->ptp_clock, 0);
  674. enable_flag = 0;/* tasklet will re-enable later */
  675. }
  676. if (ptp_int_sts & PTP_INT_BIT_TX_SWTS_ERR_) {
  677. netif_err(adapter, drv, adapter->netdev,
  678. "PTP TX Software Timestamp Error\n");
  679. /* clear int status bit */
  680. lan743x_csr_write(adapter, PTP_INT_STS,
  681. PTP_INT_BIT_TX_SWTS_ERR_);
  682. }
  683. if (ptp_int_sts & PTP_INT_BIT_TIMER_B_) {
  684. /* clear int status bit */
  685. lan743x_csr_write(adapter, PTP_INT_STS,
  686. PTP_INT_BIT_TIMER_B_);
  687. }
  688. if (ptp_int_sts & PTP_INT_BIT_TIMER_A_) {
  689. /* clear int status bit */
  690. lan743x_csr_write(adapter, PTP_INT_STS,
  691. PTP_INT_BIT_TIMER_A_);
  692. }
  693. if (enable_flag) {
  694. /* re-enable isr */
  695. lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
  696. }
  697. }
  698. static void lan743x_ptp_tx_ts_enqueue_skb(struct lan743x_adapter *adapter,
  699. struct sk_buff *skb, bool ignore_sync)
  700. {
  701. struct lan743x_ptp *ptp = &adapter->ptp;
  702. spin_lock_bh(&ptp->tx_ts_lock);
  703. if (ptp->tx_ts_skb_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
  704. ptp->tx_ts_skb_queue[ptp->tx_ts_skb_queue_size] = skb;
  705. if (ignore_sync)
  706. ptp->tx_ts_ignore_sync_queue |=
  707. BIT(ptp->tx_ts_skb_queue_size);
  708. ptp->tx_ts_skb_queue_size++;
  709. } else {
  710. /* this should never happen, so long as the tx channel
  711. * calls and honors the result from
  712. * lan743x_ptp_request_tx_timestamp
  713. */
  714. netif_err(adapter, drv, adapter->netdev,
  715. "tx ts skb queue overflow\n");
  716. dev_kfree_skb(skb);
  717. }
  718. spin_unlock_bh(&ptp->tx_ts_lock);
  719. }
  720. static void lan743x_ptp_sync_to_system_clock(struct lan743x_adapter *adapter)
  721. {
  722. struct timespec64 ts;
  723. ktime_get_clocktai_ts64(&ts);
  724. lan743x_ptp_clock_set(adapter, ts.tv_sec, ts.tv_nsec, 0);
  725. }
  726. void lan743x_ptp_update_latency(struct lan743x_adapter *adapter,
  727. u32 link_speed)
  728. {
  729. switch (link_speed) {
  730. case 10:
  731. lan743x_csr_write(adapter, PTP_LATENCY,
  732. PTP_LATENCY_TX_SET_(0) |
  733. PTP_LATENCY_RX_SET_(0));
  734. break;
  735. case 100:
  736. lan743x_csr_write(adapter, PTP_LATENCY,
  737. PTP_LATENCY_TX_SET_(181) |
  738. PTP_LATENCY_RX_SET_(594));
  739. break;
  740. case 1000:
  741. lan743x_csr_write(adapter, PTP_LATENCY,
  742. PTP_LATENCY_TX_SET_(30) |
  743. PTP_LATENCY_RX_SET_(525));
  744. break;
  745. }
  746. }
  747. int lan743x_ptp_init(struct lan743x_adapter *adapter)
  748. {
  749. struct lan743x_ptp *ptp = &adapter->ptp;
  750. mutex_init(&ptp->command_lock);
  751. spin_lock_init(&ptp->tx_ts_lock);
  752. ptp->used_event_ch = 0;
  753. ptp->perout_event_ch = -1;
  754. ptp->perout_gpio_bit = -1;
  755. return 0;
  756. }
  757. int lan743x_ptp_open(struct lan743x_adapter *adapter)
  758. {
  759. struct lan743x_ptp *ptp = &adapter->ptp;
  760. int ret = -ENODEV;
  761. u32 temp;
  762. lan743x_ptp_reset(adapter);
  763. lan743x_ptp_sync_to_system_clock(adapter);
  764. temp = lan743x_csr_read(adapter, PTP_TX_MOD2);
  765. temp |= PTP_TX_MOD2_TX_PTP_CLR_UDPV4_CHKSUM_;
  766. lan743x_csr_write(adapter, PTP_TX_MOD2, temp);
  767. lan743x_ptp_enable(adapter);
  768. lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
  769. lan743x_csr_write(adapter, PTP_INT_EN_SET,
  770. PTP_INT_BIT_TX_SWTS_ERR_ | PTP_INT_BIT_TX_TS_);
  771. ptp->flags |= PTP_FLAG_ISR_ENABLED;
  772. if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK))
  773. return 0;
  774. snprintf(ptp->pin_config[0].name, 32, "lan743x_ptp_pin_0");
  775. ptp->pin_config[0].index = 0;
  776. ptp->pin_config[0].func = PTP_PF_PEROUT;
  777. ptp->pin_config[0].chan = 0;
  778. ptp->ptp_clock_info.owner = THIS_MODULE;
  779. snprintf(ptp->ptp_clock_info.name, 16, "%pm",
  780. adapter->netdev->dev_addr);
  781. ptp->ptp_clock_info.max_adj = LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB;
  782. ptp->ptp_clock_info.n_alarm = 0;
  783. ptp->ptp_clock_info.n_ext_ts = 0;
  784. ptp->ptp_clock_info.n_per_out = 1;
  785. ptp->ptp_clock_info.n_pins = 0;
  786. ptp->ptp_clock_info.pps = 0;
  787. ptp->ptp_clock_info.pin_config = NULL;
  788. ptp->ptp_clock_info.adjfine = lan743x_ptpci_adjfine;
  789. ptp->ptp_clock_info.adjfreq = lan743x_ptpci_adjfreq;
  790. ptp->ptp_clock_info.adjtime = lan743x_ptpci_adjtime;
  791. ptp->ptp_clock_info.gettime64 = lan743x_ptpci_gettime64;
  792. ptp->ptp_clock_info.getcrosststamp = NULL;
  793. ptp->ptp_clock_info.settime64 = lan743x_ptpci_settime64;
  794. ptp->ptp_clock_info.enable = lan743x_ptpci_enable;
  795. ptp->ptp_clock_info.do_aux_work = lan743x_ptpci_do_aux_work;
  796. ptp->ptp_clock_info.verify = NULL;
  797. ptp->ptp_clock = ptp_clock_register(&ptp->ptp_clock_info,
  798. &adapter->pdev->dev);
  799. if (IS_ERR(ptp->ptp_clock)) {
  800. netif_err(adapter, ifup, adapter->netdev,
  801. "ptp_clock_register failed\n");
  802. goto done;
  803. }
  804. ptp->flags |= PTP_FLAG_PTP_CLOCK_REGISTERED;
  805. netif_info(adapter, ifup, adapter->netdev,
  806. "successfully registered ptp clock\n");
  807. return 0;
  808. done:
  809. lan743x_ptp_close(adapter);
  810. return ret;
  811. }
  812. void lan743x_ptp_close(struct lan743x_adapter *adapter)
  813. {
  814. struct lan743x_ptp *ptp = &adapter->ptp;
  815. int index;
  816. if (IS_ENABLED(CONFIG_PTP_1588_CLOCK) &&
  817. ptp->flags & PTP_FLAG_PTP_CLOCK_REGISTERED) {
  818. ptp_clock_unregister(ptp->ptp_clock);
  819. ptp->ptp_clock = NULL;
  820. ptp->flags &= ~PTP_FLAG_PTP_CLOCK_REGISTERED;
  821. netif_info(adapter, drv, adapter->netdev,
  822. "ptp clock unregister\n");
  823. }
  824. if (ptp->flags & PTP_FLAG_ISR_ENABLED) {
  825. lan743x_csr_write(adapter, PTP_INT_EN_CLR,
  826. PTP_INT_BIT_TX_SWTS_ERR_ |
  827. PTP_INT_BIT_TX_TS_);
  828. lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
  829. ptp->flags &= ~PTP_FLAG_ISR_ENABLED;
  830. }
  831. /* clean up pending timestamp requests */
  832. lan743x_ptp_tx_ts_complete(adapter);
  833. spin_lock_bh(&ptp->tx_ts_lock);
  834. for (index = 0;
  835. index < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS;
  836. index++) {
  837. struct sk_buff *skb = ptp->tx_ts_skb_queue[index];
  838. dev_kfree_skb(skb);
  839. ptp->tx_ts_skb_queue[index] = NULL;
  840. ptp->tx_ts_seconds_queue[index] = 0;
  841. ptp->tx_ts_nseconds_queue[index] = 0;
  842. }
  843. ptp->tx_ts_skb_queue_size = 0;
  844. ptp->tx_ts_queue_size = 0;
  845. ptp->pending_tx_timestamps = 0;
  846. spin_unlock_bh(&ptp->tx_ts_lock);
  847. lan743x_ptp_disable(adapter);
  848. }
  849. static void lan743x_ptp_set_sync_ts_insert(struct lan743x_adapter *adapter,
  850. bool ts_insert_enable)
  851. {
  852. u32 ptp_tx_mod = lan743x_csr_read(adapter, PTP_TX_MOD);
  853. if (ts_insert_enable)
  854. ptp_tx_mod |= PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
  855. else
  856. ptp_tx_mod &= ~PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
  857. lan743x_csr_write(adapter, PTP_TX_MOD, ptp_tx_mod);
  858. }
  859. static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter)
  860. {
  861. if (lan743x_csr_read(adapter, PTP_CMD_CTL) & PTP_CMD_CTL_PTP_ENABLE_)
  862. return true;
  863. return false;
  864. }
  865. static void lan743x_ptp_enable(struct lan743x_adapter *adapter)
  866. {
  867. struct lan743x_ptp *ptp = &adapter->ptp;
  868. mutex_lock(&ptp->command_lock);
  869. if (lan743x_ptp_is_enabled(adapter)) {
  870. netif_warn(adapter, drv, adapter->netdev,
  871. "PTP already enabled\n");
  872. goto done;
  873. }
  874. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_ENABLE_);
  875. done:
  876. mutex_unlock(&ptp->command_lock);
  877. }
  878. static void lan743x_ptp_disable(struct lan743x_adapter *adapter)
  879. {
  880. struct lan743x_ptp *ptp = &adapter->ptp;
  881. mutex_lock(&ptp->command_lock);
  882. if (!lan743x_ptp_is_enabled(adapter)) {
  883. netif_warn(adapter, drv, adapter->netdev,
  884. "PTP already disabled\n");
  885. goto done;
  886. }
  887. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_DISABLE_);
  888. lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_ENABLE_);
  889. done:
  890. mutex_unlock(&ptp->command_lock);
  891. }
  892. static void lan743x_ptp_reset(struct lan743x_adapter *adapter)
  893. {
  894. struct lan743x_ptp *ptp = &adapter->ptp;
  895. mutex_lock(&ptp->command_lock);
  896. if (lan743x_ptp_is_enabled(adapter)) {
  897. netif_err(adapter, drv, adapter->netdev,
  898. "Attempting reset while enabled\n");
  899. goto done;
  900. }
  901. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_RESET_);
  902. lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_RESET_);
  903. done:
  904. mutex_unlock(&ptp->command_lock);
  905. }
  906. static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
  907. u32 seconds, u32 nano_seconds,
  908. u32 sub_nano_seconds)
  909. {
  910. struct lan743x_ptp *ptp = &adapter->ptp;
  911. mutex_lock(&ptp->command_lock);
  912. lan743x_csr_write(adapter, PTP_CLOCK_SEC, seconds);
  913. lan743x_csr_write(adapter, PTP_CLOCK_NS, nano_seconds);
  914. lan743x_csr_write(adapter, PTP_CLOCK_SUBNS, sub_nano_seconds);
  915. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
  916. lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
  917. mutex_unlock(&ptp->command_lock);
  918. }
  919. bool lan743x_ptp_request_tx_timestamp(struct lan743x_adapter *adapter)
  920. {
  921. struct lan743x_ptp *ptp = &adapter->ptp;
  922. bool result = false;
  923. spin_lock_bh(&ptp->tx_ts_lock);
  924. if (ptp->pending_tx_timestamps < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
  925. /* request granted */
  926. ptp->pending_tx_timestamps++;
  927. result = true;
  928. }
  929. spin_unlock_bh(&ptp->tx_ts_lock);
  930. return result;
  931. }
  932. void lan743x_ptp_unrequest_tx_timestamp(struct lan743x_adapter *adapter)
  933. {
  934. struct lan743x_ptp *ptp = &adapter->ptp;
  935. spin_lock_bh(&ptp->tx_ts_lock);
  936. if (ptp->pending_tx_timestamps > 0)
  937. ptp->pending_tx_timestamps--;
  938. else
  939. netif_err(adapter, drv, adapter->netdev,
  940. "unrequest failed, pending_tx_timestamps==0\n");
  941. spin_unlock_bh(&ptp->tx_ts_lock);
  942. }
  943. void lan743x_ptp_tx_timestamp_skb(struct lan743x_adapter *adapter,
  944. struct sk_buff *skb, bool ignore_sync)
  945. {
  946. lan743x_ptp_tx_ts_enqueue_skb(adapter, skb, ignore_sync);
  947. lan743x_ptp_tx_ts_complete(adapter);
  948. }
  949. int lan743x_ptp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  950. {
  951. struct lan743x_adapter *adapter = netdev_priv(netdev);
  952. struct hwtstamp_config config;
  953. int ret = 0;
  954. int index;
  955. if (!ifr) {
  956. netif_err(adapter, drv, adapter->netdev,
  957. "SIOCSHWTSTAMP, ifr == NULL\n");
  958. return -EINVAL;
  959. }
  960. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  961. return -EFAULT;
  962. if (config.flags) {
  963. netif_warn(adapter, drv, adapter->netdev,
  964. "ignoring hwtstamp_config.flags == 0x%08X, expected 0\n",
  965. config.flags);
  966. }
  967. switch (config.tx_type) {
  968. case HWTSTAMP_TX_OFF:
  969. for (index = 0; index < LAN743X_MAX_TX_CHANNELS;
  970. index++)
  971. lan743x_tx_set_timestamping_mode(&adapter->tx[index],
  972. false, false);
  973. lan743x_ptp_set_sync_ts_insert(adapter, false);
  974. break;
  975. case HWTSTAMP_TX_ON:
  976. for (index = 0; index < LAN743X_MAX_TX_CHANNELS;
  977. index++)
  978. lan743x_tx_set_timestamping_mode(&adapter->tx[index],
  979. true, false);
  980. lan743x_ptp_set_sync_ts_insert(adapter, false);
  981. break;
  982. case HWTSTAMP_TX_ONESTEP_SYNC:
  983. for (index = 0; index < LAN743X_MAX_TX_CHANNELS;
  984. index++)
  985. lan743x_tx_set_timestamping_mode(&adapter->tx[index],
  986. true, true);
  987. lan743x_ptp_set_sync_ts_insert(adapter, true);
  988. break;
  989. default:
  990. netif_warn(adapter, drv, adapter->netdev,
  991. " tx_type = %d, UNKNOWN\n", config.tx_type);
  992. ret = -EINVAL;
  993. break;
  994. }
  995. if (!ret)
  996. return copy_to_user(ifr->ifr_data, &config,
  997. sizeof(config)) ? -EFAULT : 0;
  998. return ret;
  999. }