lan743x_ptp.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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. if (!on) {
  368. lan743x_ptp_perout_off(adapter);
  369. return 0;
  370. }
  371. if (ptp->perout_event_ch >= 0 ||
  372. ptp->perout_gpio_bit >= 0) {
  373. /* already on, turn off first */
  374. lan743x_ptp_perout_off(adapter);
  375. }
  376. ptp->perout_event_ch = lan743x_ptp_reserve_event_ch(adapter);
  377. if (ptp->perout_event_ch < 0) {
  378. netif_warn(adapter, drv, adapter->netdev,
  379. "Failed to reserve event channel for PEROUT\n");
  380. goto failed;
  381. }
  382. switch (adapter->csr.id_rev & ID_REV_ID_MASK_) {
  383. case ID_REV_ID_LAN7430_:
  384. perout_bit = 2;/* GPIO 2 is preferred on EVB LAN7430 */
  385. break;
  386. case ID_REV_ID_LAN7431_:
  387. perout_bit = 4;/* GPIO 4 is preferred on EVB LAN7431 */
  388. break;
  389. }
  390. ptp->perout_gpio_bit = lan743x_gpio_rsrv_ptp_out(adapter,
  391. perout_bit,
  392. ptp->perout_event_ch);
  393. if (ptp->perout_gpio_bit < 0) {
  394. netif_warn(adapter, drv, adapter->netdev,
  395. "Failed to reserve gpio %d for PEROUT\n",
  396. perout_bit);
  397. goto failed;
  398. }
  399. start_sec = perout->start.sec;
  400. start_sec += perout->start.nsec / 1000000000;
  401. start_nsec = perout->start.nsec % 1000000000;
  402. period_sec = perout->period.sec;
  403. period_sec += perout->period.nsec / 1000000000;
  404. period_nsec = perout->period.nsec % 1000000000;
  405. if (period_sec == 0) {
  406. if (period_nsec >= 400000000) {
  407. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
  408. } else if (period_nsec >= 20000000) {
  409. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
  410. } else if (period_nsec >= 2000000) {
  411. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
  412. } else if (period_nsec >= 200000) {
  413. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
  414. } else if (period_nsec >= 20000) {
  415. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
  416. } else if (period_nsec >= 200) {
  417. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
  418. } else {
  419. netif_warn(adapter, drv, adapter->netdev,
  420. "perout period too small, minimum is 200nS\n");
  421. goto failed;
  422. }
  423. } else {
  424. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
  425. }
  426. /* turn off by setting target far in future */
  427. lan743x_csr_write(adapter,
  428. PTP_CLOCK_TARGET_SEC_X(ptp->perout_event_ch),
  429. 0xFFFF0000);
  430. lan743x_csr_write(adapter,
  431. PTP_CLOCK_TARGET_NS_X(ptp->perout_event_ch), 0);
  432. /* Configure to pulse every period */
  433. general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
  434. general_config &= ~(PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
  435. (ptp->perout_event_ch));
  436. general_config |= PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_
  437. (ptp->perout_event_ch, pulse_width);
  438. general_config &= ~PTP_GENERAL_CONFIG_RELOAD_ADD_X_
  439. (ptp->perout_event_ch);
  440. lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
  441. /* set the reload to one toggle cycle */
  442. lan743x_csr_write(adapter,
  443. PTP_CLOCK_TARGET_RELOAD_SEC_X(ptp->perout_event_ch),
  444. period_sec);
  445. lan743x_csr_write(adapter,
  446. PTP_CLOCK_TARGET_RELOAD_NS_X(ptp->perout_event_ch),
  447. period_nsec);
  448. /* set the start time */
  449. lan743x_csr_write(adapter,
  450. PTP_CLOCK_TARGET_SEC_X(ptp->perout_event_ch),
  451. start_sec);
  452. lan743x_csr_write(adapter,
  453. PTP_CLOCK_TARGET_NS_X(ptp->perout_event_ch),
  454. start_nsec);
  455. return 0;
  456. failed:
  457. lan743x_ptp_perout_off(adapter);
  458. return -ENODEV;
  459. }
  460. static int lan743x_ptpci_enable(struct ptp_clock_info *ptpci,
  461. struct ptp_clock_request *request, int on)
  462. {
  463. struct lan743x_ptp *ptp =
  464. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  465. struct lan743x_adapter *adapter =
  466. container_of(ptp, struct lan743x_adapter, ptp);
  467. if (request) {
  468. switch (request->type) {
  469. case PTP_CLK_REQ_EXTTS:
  470. return -EINVAL;
  471. case PTP_CLK_REQ_PEROUT:
  472. if (request->perout.index == 0)
  473. return lan743x_ptp_perout(adapter, on,
  474. &request->perout);
  475. return -EINVAL;
  476. case PTP_CLK_REQ_PPS:
  477. return -EINVAL;
  478. default:
  479. netif_err(adapter, drv, adapter->netdev,
  480. "request->type == %d, Unknown\n",
  481. request->type);
  482. break;
  483. }
  484. } else {
  485. netif_err(adapter, drv, adapter->netdev, "request == NULL\n");
  486. }
  487. return 0;
  488. }
  489. static long lan743x_ptpci_do_aux_work(struct ptp_clock_info *ptpci)
  490. {
  491. struct lan743x_ptp *ptp =
  492. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  493. struct lan743x_adapter *adapter =
  494. container_of(ptp, struct lan743x_adapter, ptp);
  495. u32 cap_info, cause, header, nsec, seconds;
  496. bool new_timestamp_available = false;
  497. int count = 0;
  498. while ((count < 100) &&
  499. (lan743x_csr_read(adapter, PTP_INT_STS) & PTP_INT_BIT_TX_TS_)) {
  500. count++;
  501. cap_info = lan743x_csr_read(adapter, PTP_CAP_INFO);
  502. if (PTP_CAP_INFO_TX_TS_CNT_GET_(cap_info) > 0) {
  503. seconds = lan743x_csr_read(adapter,
  504. PTP_TX_EGRESS_SEC);
  505. nsec = lan743x_csr_read(adapter, PTP_TX_EGRESS_NS);
  506. cause = (nsec &
  507. PTP_TX_EGRESS_NS_CAPTURE_CAUSE_MASK_);
  508. header = lan743x_csr_read(adapter,
  509. PTP_TX_MSG_HEADER);
  510. if (cause == PTP_TX_EGRESS_NS_CAPTURE_CAUSE_SW_) {
  511. nsec &= PTP_TX_EGRESS_NS_TS_NS_MASK_;
  512. lan743x_ptp_tx_ts_enqueue_ts(adapter,
  513. seconds, nsec,
  514. header);
  515. new_timestamp_available = true;
  516. } else if (cause ==
  517. PTP_TX_EGRESS_NS_CAPTURE_CAUSE_AUTO_) {
  518. netif_err(adapter, drv, adapter->netdev,
  519. "Auto capture cause not supported\n");
  520. } else {
  521. netif_warn(adapter, drv, adapter->netdev,
  522. "unknown tx timestamp capture cause\n");
  523. }
  524. } else {
  525. netif_warn(adapter, drv, adapter->netdev,
  526. "TX TS INT but no TX TS CNT\n");
  527. }
  528. lan743x_csr_write(adapter, PTP_INT_STS, PTP_INT_BIT_TX_TS_);
  529. }
  530. if (new_timestamp_available)
  531. lan743x_ptp_tx_ts_complete(adapter);
  532. lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
  533. return -1;
  534. }
  535. static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
  536. u32 *seconds, u32 *nano_seconds,
  537. u32 *sub_nano_seconds)
  538. {
  539. struct lan743x_ptp *ptp = &adapter->ptp;
  540. mutex_lock(&ptp->command_lock);
  541. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
  542. lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);
  543. if (seconds)
  544. (*seconds) = lan743x_csr_read(adapter, PTP_CLOCK_SEC);
  545. if (nano_seconds)
  546. (*nano_seconds) = lan743x_csr_read(adapter, PTP_CLOCK_NS);
  547. if (sub_nano_seconds)
  548. (*sub_nano_seconds) =
  549. lan743x_csr_read(adapter, PTP_CLOCK_SUBNS);
  550. mutex_unlock(&ptp->command_lock);
  551. }
  552. static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
  553. s64 time_step_ns)
  554. {
  555. struct lan743x_ptp *ptp = &adapter->ptp;
  556. u32 nano_seconds_step = 0;
  557. u64 abs_time_step_ns = 0;
  558. u32 unsigned_seconds = 0;
  559. u32 nano_seconds = 0;
  560. u32 remainder = 0;
  561. s32 seconds = 0;
  562. if (time_step_ns > 15000000000LL) {
  563. /* convert to clock set */
  564. lan743x_ptp_clock_get(adapter, &unsigned_seconds,
  565. &nano_seconds, NULL);
  566. unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
  567. &remainder);
  568. nano_seconds += remainder;
  569. if (nano_seconds >= 1000000000) {
  570. unsigned_seconds++;
  571. nano_seconds -= 1000000000;
  572. }
  573. lan743x_ptp_clock_set(adapter, unsigned_seconds,
  574. nano_seconds, 0);
  575. return;
  576. } else if (time_step_ns < -15000000000LL) {
  577. /* convert to clock set */
  578. time_step_ns = -time_step_ns;
  579. lan743x_ptp_clock_get(adapter, &unsigned_seconds,
  580. &nano_seconds, NULL);
  581. unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
  582. &remainder);
  583. nano_seconds_step = remainder;
  584. if (nano_seconds < nano_seconds_step) {
  585. unsigned_seconds--;
  586. nano_seconds += 1000000000;
  587. }
  588. nano_seconds -= nano_seconds_step;
  589. lan743x_ptp_clock_set(adapter, unsigned_seconds,
  590. nano_seconds, 0);
  591. return;
  592. }
  593. /* do clock step */
  594. if (time_step_ns >= 0) {
  595. abs_time_step_ns = (u64)(time_step_ns);
  596. seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
  597. &remainder);
  598. nano_seconds = (u32)remainder;
  599. } else {
  600. abs_time_step_ns = (u64)(-time_step_ns);
  601. seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
  602. &remainder));
  603. nano_seconds = (u32)remainder;
  604. if (nano_seconds > 0) {
  605. /* subtracting nano seconds is not allowed
  606. * convert to subtracting from seconds,
  607. * and adding to nanoseconds
  608. */
  609. seconds--;
  610. nano_seconds = (1000000000 - nano_seconds);
  611. }
  612. }
  613. if (nano_seconds > 0) {
  614. /* add 8 ns to cover the likely normal increment */
  615. nano_seconds += 8;
  616. }
  617. if (nano_seconds >= 1000000000) {
  618. /* carry into seconds */
  619. seconds++;
  620. nano_seconds -= 1000000000;
  621. }
  622. while (seconds) {
  623. mutex_lock(&ptp->command_lock);
  624. if (seconds > 0) {
  625. u32 adjustment_value = (u32)seconds;
  626. if (adjustment_value > 0xF)
  627. adjustment_value = 0xF;
  628. lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
  629. PTP_CLOCK_STEP_ADJ_DIR_ |
  630. adjustment_value);
  631. seconds -= ((s32)adjustment_value);
  632. } else {
  633. u32 adjustment_value = (u32)(-seconds);
  634. if (adjustment_value > 0xF)
  635. adjustment_value = 0xF;
  636. lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
  637. adjustment_value);
  638. seconds += ((s32)adjustment_value);
  639. }
  640. lan743x_csr_write(adapter, PTP_CMD_CTL,
  641. PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
  642. lan743x_ptp_wait_till_cmd_done(adapter,
  643. PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
  644. mutex_unlock(&ptp->command_lock);
  645. }
  646. if (nano_seconds) {
  647. mutex_lock(&ptp->command_lock);
  648. lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
  649. PTP_CLOCK_STEP_ADJ_DIR_ |
  650. (nano_seconds &
  651. PTP_CLOCK_STEP_ADJ_VALUE_MASK_));
  652. lan743x_csr_write(adapter, PTP_CMD_CTL,
  653. PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
  654. lan743x_ptp_wait_till_cmd_done(adapter,
  655. PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
  656. mutex_unlock(&ptp->command_lock);
  657. }
  658. }
  659. void lan743x_ptp_isr(void *context)
  660. {
  661. struct lan743x_adapter *adapter = (struct lan743x_adapter *)context;
  662. struct lan743x_ptp *ptp = NULL;
  663. int enable_flag = 1;
  664. u32 ptp_int_sts = 0;
  665. ptp = &adapter->ptp;
  666. lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
  667. ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
  668. ptp_int_sts &= lan743x_csr_read(adapter, PTP_INT_EN_SET);
  669. if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
  670. ptp_schedule_worker(ptp->ptp_clock, 0);
  671. enable_flag = 0;/* tasklet will re-enable later */
  672. }
  673. if (ptp_int_sts & PTP_INT_BIT_TX_SWTS_ERR_) {
  674. netif_err(adapter, drv, adapter->netdev,
  675. "PTP TX Software Timestamp Error\n");
  676. /* clear int status bit */
  677. lan743x_csr_write(adapter, PTP_INT_STS,
  678. PTP_INT_BIT_TX_SWTS_ERR_);
  679. }
  680. if (ptp_int_sts & PTP_INT_BIT_TIMER_B_) {
  681. /* clear int status bit */
  682. lan743x_csr_write(adapter, PTP_INT_STS,
  683. PTP_INT_BIT_TIMER_B_);
  684. }
  685. if (ptp_int_sts & PTP_INT_BIT_TIMER_A_) {
  686. /* clear int status bit */
  687. lan743x_csr_write(adapter, PTP_INT_STS,
  688. PTP_INT_BIT_TIMER_A_);
  689. }
  690. if (enable_flag) {
  691. /* re-enable isr */
  692. lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
  693. }
  694. }
  695. static void lan743x_ptp_tx_ts_enqueue_skb(struct lan743x_adapter *adapter,
  696. struct sk_buff *skb, bool ignore_sync)
  697. {
  698. struct lan743x_ptp *ptp = &adapter->ptp;
  699. spin_lock_bh(&ptp->tx_ts_lock);
  700. if (ptp->tx_ts_skb_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
  701. ptp->tx_ts_skb_queue[ptp->tx_ts_skb_queue_size] = skb;
  702. if (ignore_sync)
  703. ptp->tx_ts_ignore_sync_queue |=
  704. BIT(ptp->tx_ts_skb_queue_size);
  705. ptp->tx_ts_skb_queue_size++;
  706. } else {
  707. /* this should never happen, so long as the tx channel
  708. * calls and honors the result from
  709. * lan743x_ptp_request_tx_timestamp
  710. */
  711. netif_err(adapter, drv, adapter->netdev,
  712. "tx ts skb queue overflow\n");
  713. dev_kfree_skb(skb);
  714. }
  715. spin_unlock_bh(&ptp->tx_ts_lock);
  716. }
  717. static void lan743x_ptp_sync_to_system_clock(struct lan743x_adapter *adapter)
  718. {
  719. struct timespec64 ts;
  720. ktime_get_clocktai_ts64(&ts);
  721. lan743x_ptp_clock_set(adapter, ts.tv_sec, ts.tv_nsec, 0);
  722. }
  723. void lan743x_ptp_update_latency(struct lan743x_adapter *adapter,
  724. u32 link_speed)
  725. {
  726. switch (link_speed) {
  727. case 10:
  728. lan743x_csr_write(adapter, PTP_LATENCY,
  729. PTP_LATENCY_TX_SET_(0) |
  730. PTP_LATENCY_RX_SET_(0));
  731. break;
  732. case 100:
  733. lan743x_csr_write(adapter, PTP_LATENCY,
  734. PTP_LATENCY_TX_SET_(181) |
  735. PTP_LATENCY_RX_SET_(594));
  736. break;
  737. case 1000:
  738. lan743x_csr_write(adapter, PTP_LATENCY,
  739. PTP_LATENCY_TX_SET_(30) |
  740. PTP_LATENCY_RX_SET_(525));
  741. break;
  742. }
  743. }
  744. int lan743x_ptp_init(struct lan743x_adapter *adapter)
  745. {
  746. struct lan743x_ptp *ptp = &adapter->ptp;
  747. mutex_init(&ptp->command_lock);
  748. spin_lock_init(&ptp->tx_ts_lock);
  749. ptp->used_event_ch = 0;
  750. ptp->perout_event_ch = -1;
  751. ptp->perout_gpio_bit = -1;
  752. return 0;
  753. }
  754. int lan743x_ptp_open(struct lan743x_adapter *adapter)
  755. {
  756. struct lan743x_ptp *ptp = &adapter->ptp;
  757. int ret = -ENODEV;
  758. u32 temp;
  759. lan743x_ptp_reset(adapter);
  760. lan743x_ptp_sync_to_system_clock(adapter);
  761. temp = lan743x_csr_read(adapter, PTP_TX_MOD2);
  762. temp |= PTP_TX_MOD2_TX_PTP_CLR_UDPV4_CHKSUM_;
  763. lan743x_csr_write(adapter, PTP_TX_MOD2, temp);
  764. lan743x_ptp_enable(adapter);
  765. lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
  766. lan743x_csr_write(adapter, PTP_INT_EN_SET,
  767. PTP_INT_BIT_TX_SWTS_ERR_ | PTP_INT_BIT_TX_TS_);
  768. ptp->flags |= PTP_FLAG_ISR_ENABLED;
  769. if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK))
  770. return 0;
  771. snprintf(ptp->pin_config[0].name, 32, "lan743x_ptp_pin_0");
  772. ptp->pin_config[0].index = 0;
  773. ptp->pin_config[0].func = PTP_PF_PEROUT;
  774. ptp->pin_config[0].chan = 0;
  775. ptp->ptp_clock_info.owner = THIS_MODULE;
  776. snprintf(ptp->ptp_clock_info.name, 16, "%pm",
  777. adapter->netdev->dev_addr);
  778. ptp->ptp_clock_info.max_adj = LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB;
  779. ptp->ptp_clock_info.n_alarm = 0;
  780. ptp->ptp_clock_info.n_ext_ts = 0;
  781. ptp->ptp_clock_info.n_per_out = 1;
  782. ptp->ptp_clock_info.n_pins = 0;
  783. ptp->ptp_clock_info.pps = 0;
  784. ptp->ptp_clock_info.pin_config = NULL;
  785. ptp->ptp_clock_info.adjfine = lan743x_ptpci_adjfine;
  786. ptp->ptp_clock_info.adjfreq = lan743x_ptpci_adjfreq;
  787. ptp->ptp_clock_info.adjtime = lan743x_ptpci_adjtime;
  788. ptp->ptp_clock_info.gettime64 = lan743x_ptpci_gettime64;
  789. ptp->ptp_clock_info.getcrosststamp = NULL;
  790. ptp->ptp_clock_info.settime64 = lan743x_ptpci_settime64;
  791. ptp->ptp_clock_info.enable = lan743x_ptpci_enable;
  792. ptp->ptp_clock_info.do_aux_work = lan743x_ptpci_do_aux_work;
  793. ptp->ptp_clock_info.verify = NULL;
  794. ptp->ptp_clock = ptp_clock_register(&ptp->ptp_clock_info,
  795. &adapter->pdev->dev);
  796. if (IS_ERR(ptp->ptp_clock)) {
  797. netif_err(adapter, ifup, adapter->netdev,
  798. "ptp_clock_register failed\n");
  799. goto done;
  800. }
  801. ptp->flags |= PTP_FLAG_PTP_CLOCK_REGISTERED;
  802. netif_info(adapter, ifup, adapter->netdev,
  803. "successfully registered ptp clock\n");
  804. return 0;
  805. done:
  806. lan743x_ptp_close(adapter);
  807. return ret;
  808. }
  809. void lan743x_ptp_close(struct lan743x_adapter *adapter)
  810. {
  811. struct lan743x_ptp *ptp = &adapter->ptp;
  812. int index;
  813. if (IS_ENABLED(CONFIG_PTP_1588_CLOCK) &&
  814. ptp->flags & PTP_FLAG_PTP_CLOCK_REGISTERED) {
  815. ptp_clock_unregister(ptp->ptp_clock);
  816. ptp->ptp_clock = NULL;
  817. ptp->flags &= ~PTP_FLAG_PTP_CLOCK_REGISTERED;
  818. netif_info(adapter, drv, adapter->netdev,
  819. "ptp clock unregister\n");
  820. }
  821. if (ptp->flags & PTP_FLAG_ISR_ENABLED) {
  822. lan743x_csr_write(adapter, PTP_INT_EN_CLR,
  823. PTP_INT_BIT_TX_SWTS_ERR_ |
  824. PTP_INT_BIT_TX_TS_);
  825. lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
  826. ptp->flags &= ~PTP_FLAG_ISR_ENABLED;
  827. }
  828. /* clean up pending timestamp requests */
  829. lan743x_ptp_tx_ts_complete(adapter);
  830. spin_lock_bh(&ptp->tx_ts_lock);
  831. for (index = 0;
  832. index < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS;
  833. index++) {
  834. struct sk_buff *skb = ptp->tx_ts_skb_queue[index];
  835. if (skb)
  836. dev_kfree_skb(skb);
  837. ptp->tx_ts_skb_queue[index] = NULL;
  838. ptp->tx_ts_seconds_queue[index] = 0;
  839. ptp->tx_ts_nseconds_queue[index] = 0;
  840. }
  841. ptp->tx_ts_skb_queue_size = 0;
  842. ptp->tx_ts_queue_size = 0;
  843. ptp->pending_tx_timestamps = 0;
  844. spin_unlock_bh(&ptp->tx_ts_lock);
  845. lan743x_ptp_disable(adapter);
  846. }
  847. void lan743x_ptp_set_sync_ts_insert(struct lan743x_adapter *adapter,
  848. bool ts_insert_enable)
  849. {
  850. u32 ptp_tx_mod = lan743x_csr_read(adapter, PTP_TX_MOD);
  851. if (ts_insert_enable)
  852. ptp_tx_mod |= PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
  853. else
  854. ptp_tx_mod &= ~PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
  855. lan743x_csr_write(adapter, PTP_TX_MOD, ptp_tx_mod);
  856. }
  857. static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter)
  858. {
  859. if (lan743x_csr_read(adapter, PTP_CMD_CTL) & PTP_CMD_CTL_PTP_ENABLE_)
  860. return true;
  861. return false;
  862. }
  863. static void lan743x_ptp_enable(struct lan743x_adapter *adapter)
  864. {
  865. struct lan743x_ptp *ptp = &adapter->ptp;
  866. mutex_lock(&ptp->command_lock);
  867. if (lan743x_ptp_is_enabled(adapter)) {
  868. netif_warn(adapter, drv, adapter->netdev,
  869. "PTP already enabled\n");
  870. goto done;
  871. }
  872. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_ENABLE_);
  873. done:
  874. mutex_unlock(&ptp->command_lock);
  875. }
  876. static void lan743x_ptp_disable(struct lan743x_adapter *adapter)
  877. {
  878. struct lan743x_ptp *ptp = &adapter->ptp;
  879. mutex_lock(&ptp->command_lock);
  880. if (!lan743x_ptp_is_enabled(adapter)) {
  881. netif_warn(adapter, drv, adapter->netdev,
  882. "PTP already disabled\n");
  883. goto done;
  884. }
  885. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_DISABLE_);
  886. lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_ENABLE_);
  887. done:
  888. mutex_unlock(&ptp->command_lock);
  889. }
  890. static void lan743x_ptp_reset(struct lan743x_adapter *adapter)
  891. {
  892. struct lan743x_ptp *ptp = &adapter->ptp;
  893. mutex_lock(&ptp->command_lock);
  894. if (lan743x_ptp_is_enabled(adapter)) {
  895. netif_err(adapter, drv, adapter->netdev,
  896. "Attempting reset while enabled\n");
  897. goto done;
  898. }
  899. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_RESET_);
  900. lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_RESET_);
  901. done:
  902. mutex_unlock(&ptp->command_lock);
  903. }
  904. static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
  905. u32 seconds, u32 nano_seconds,
  906. u32 sub_nano_seconds)
  907. {
  908. struct lan743x_ptp *ptp = &adapter->ptp;
  909. mutex_lock(&ptp->command_lock);
  910. lan743x_csr_write(adapter, PTP_CLOCK_SEC, seconds);
  911. lan743x_csr_write(adapter, PTP_CLOCK_NS, nano_seconds);
  912. lan743x_csr_write(adapter, PTP_CLOCK_SUBNS, sub_nano_seconds);
  913. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
  914. lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
  915. mutex_unlock(&ptp->command_lock);
  916. }
  917. bool lan743x_ptp_request_tx_timestamp(struct lan743x_adapter *adapter)
  918. {
  919. struct lan743x_ptp *ptp = &adapter->ptp;
  920. bool result = false;
  921. spin_lock_bh(&ptp->tx_ts_lock);
  922. if (ptp->pending_tx_timestamps < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
  923. /* request granted */
  924. ptp->pending_tx_timestamps++;
  925. result = true;
  926. }
  927. spin_unlock_bh(&ptp->tx_ts_lock);
  928. return result;
  929. }
  930. void lan743x_ptp_unrequest_tx_timestamp(struct lan743x_adapter *adapter)
  931. {
  932. struct lan743x_ptp *ptp = &adapter->ptp;
  933. spin_lock_bh(&ptp->tx_ts_lock);
  934. if (ptp->pending_tx_timestamps > 0)
  935. ptp->pending_tx_timestamps--;
  936. else
  937. netif_err(adapter, drv, adapter->netdev,
  938. "unrequest failed, pending_tx_timestamps==0\n");
  939. spin_unlock_bh(&ptp->tx_ts_lock);
  940. }
  941. void lan743x_ptp_tx_timestamp_skb(struct lan743x_adapter *adapter,
  942. struct sk_buff *skb, bool ignore_sync)
  943. {
  944. lan743x_ptp_tx_ts_enqueue_skb(adapter, skb, ignore_sync);
  945. lan743x_ptp_tx_ts_complete(adapter);
  946. }
  947. int lan743x_ptp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  948. {
  949. struct lan743x_adapter *adapter = netdev_priv(netdev);
  950. struct hwtstamp_config config;
  951. int ret = 0;
  952. int index;
  953. if (!ifr) {
  954. netif_err(adapter, drv, adapter->netdev,
  955. "SIOCSHWTSTAMP, ifr == NULL\n");
  956. return -EINVAL;
  957. }
  958. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  959. return -EFAULT;
  960. if (config.flags) {
  961. netif_warn(adapter, drv, adapter->netdev,
  962. "ignoring hwtstamp_config.flags == 0x%08X, expected 0\n",
  963. config.flags);
  964. }
  965. switch (config.tx_type) {
  966. case HWTSTAMP_TX_OFF:
  967. for (index = 0; index < LAN743X_MAX_TX_CHANNELS;
  968. index++)
  969. lan743x_tx_set_timestamping_mode(&adapter->tx[index],
  970. false, false);
  971. lan743x_ptp_set_sync_ts_insert(adapter, false);
  972. break;
  973. case HWTSTAMP_TX_ON:
  974. for (index = 0; index < LAN743X_MAX_TX_CHANNELS;
  975. index++)
  976. lan743x_tx_set_timestamping_mode(&adapter->tx[index],
  977. true, false);
  978. lan743x_ptp_set_sync_ts_insert(adapter, false);
  979. break;
  980. case HWTSTAMP_TX_ONESTEP_SYNC:
  981. for (index = 0; index < LAN743X_MAX_TX_CHANNELS;
  982. index++)
  983. lan743x_tx_set_timestamping_mode(&adapter->tx[index],
  984. true, true);
  985. lan743x_ptp_set_sync_ts_insert(adapter, true);
  986. break;
  987. default:
  988. netif_warn(adapter, drv, adapter->netdev,
  989. " tx_type = %d, UNKNOWN\n", config.tx_type);
  990. ret = -EINVAL;
  991. break;
  992. }
  993. if (!ret)
  994. return copy_to_user(ifr->ifr_data, &config,
  995. sizeof(config)) ? -EFAULT : 0;
  996. return ret;
  997. }