lan743x_ptp.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /* Copyright (C) 2018 Microchip Technology Inc. */
  3. #include <linux/netdevice.h>
  4. #include <linux/ptp_clock_kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/pci.h>
  7. #include <linux/net_tstamp.h>
  8. #include "lan743x_main.h"
  9. #include "lan743x_ptp.h"
  10. #define LAN743X_LED0_ENABLE 20 /* LED0 offset in HW_CFG */
  11. #define LAN743X_LED_ENABLE(pin) BIT(LAN743X_LED0_ENABLE + (pin))
  12. #define LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB (31249999)
  13. #define LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM (2047999934)
  14. static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter);
  15. static void lan743x_ptp_enable(struct lan743x_adapter *adapter);
  16. static void lan743x_ptp_disable(struct lan743x_adapter *adapter);
  17. static void lan743x_ptp_reset(struct lan743x_adapter *adapter);
  18. static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
  19. u32 seconds, u32 nano_seconds,
  20. u32 sub_nano_seconds);
  21. static int lan743x_get_channel(u32 ch_map)
  22. {
  23. int idx;
  24. for (idx = 0; idx < 32; idx++) {
  25. if (ch_map & (0x1 << idx))
  26. return idx;
  27. }
  28. return -EINVAL;
  29. }
  30. int lan743x_gpio_init(struct lan743x_adapter *adapter)
  31. {
  32. struct lan743x_gpio *gpio = &adapter->gpio;
  33. spin_lock_init(&gpio->gpio_lock);
  34. gpio->gpio_cfg0 = 0; /* set all direction to input, data = 0 */
  35. gpio->gpio_cfg1 = 0x0FFF0000;/* disable all gpio, set to open drain */
  36. gpio->gpio_cfg2 = 0;/* set all to 1588 low polarity level */
  37. gpio->gpio_cfg3 = 0;/* disable all 1588 output */
  38. lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
  39. lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
  40. lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
  41. lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);
  42. return 0;
  43. }
  44. static void lan743x_ptp_wait_till_cmd_done(struct lan743x_adapter *adapter,
  45. u32 bit_mask)
  46. {
  47. int timeout = PTP_CMD_CTL_TIMEOUT_CNT;
  48. u32 data = 0;
  49. while (timeout &&
  50. (data = (lan743x_csr_read(adapter, PTP_CMD_CTL) &
  51. bit_mask))) {
  52. usleep_range(1000, 20000);
  53. timeout--;
  54. }
  55. if (data) {
  56. netif_err(adapter, drv, adapter->netdev,
  57. "timeout waiting for cmd to be done, cmd = 0x%08X\n",
  58. bit_mask);
  59. }
  60. }
  61. static void lan743x_ptp_tx_ts_enqueue_ts(struct lan743x_adapter *adapter,
  62. u32 seconds, u32 nano_seconds,
  63. u32 header)
  64. {
  65. struct lan743x_ptp *ptp = &adapter->ptp;
  66. spin_lock_bh(&ptp->tx_ts_lock);
  67. if (ptp->tx_ts_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
  68. ptp->tx_ts_seconds_queue[ptp->tx_ts_queue_size] = seconds;
  69. ptp->tx_ts_nseconds_queue[ptp->tx_ts_queue_size] = nano_seconds;
  70. ptp->tx_ts_header_queue[ptp->tx_ts_queue_size] = header;
  71. ptp->tx_ts_queue_size++;
  72. } else {
  73. netif_err(adapter, drv, adapter->netdev,
  74. "tx ts queue overflow\n");
  75. }
  76. spin_unlock_bh(&ptp->tx_ts_lock);
  77. }
  78. static void lan743x_ptp_tx_ts_complete(struct lan743x_adapter *adapter)
  79. {
  80. struct lan743x_ptp *ptp = &adapter->ptp;
  81. struct skb_shared_hwtstamps tstamps;
  82. u32 header, nseconds, seconds;
  83. bool ignore_sync = false;
  84. struct sk_buff *skb;
  85. int c, i;
  86. spin_lock_bh(&ptp->tx_ts_lock);
  87. c = ptp->tx_ts_skb_queue_size;
  88. if (c > ptp->tx_ts_queue_size)
  89. c = ptp->tx_ts_queue_size;
  90. if (c <= 0)
  91. goto done;
  92. for (i = 0; i < c; i++) {
  93. ignore_sync = ((ptp->tx_ts_ignore_sync_queue &
  94. BIT(i)) != 0);
  95. skb = ptp->tx_ts_skb_queue[i];
  96. nseconds = ptp->tx_ts_nseconds_queue[i];
  97. seconds = ptp->tx_ts_seconds_queue[i];
  98. header = ptp->tx_ts_header_queue[i];
  99. memset(&tstamps, 0, sizeof(tstamps));
  100. tstamps.hwtstamp = ktime_set(seconds, nseconds);
  101. if (!ignore_sync ||
  102. ((header & PTP_TX_MSG_HEADER_MSG_TYPE_) !=
  103. PTP_TX_MSG_HEADER_MSG_TYPE_SYNC_))
  104. skb_tstamp_tx(skb, &tstamps);
  105. dev_kfree_skb(skb);
  106. ptp->tx_ts_skb_queue[i] = NULL;
  107. ptp->tx_ts_seconds_queue[i] = 0;
  108. ptp->tx_ts_nseconds_queue[i] = 0;
  109. ptp->tx_ts_header_queue[i] = 0;
  110. }
  111. /* shift queue */
  112. ptp->tx_ts_ignore_sync_queue >>= c;
  113. for (i = c; i < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS; i++) {
  114. ptp->tx_ts_skb_queue[i - c] = ptp->tx_ts_skb_queue[i];
  115. ptp->tx_ts_seconds_queue[i - c] = ptp->tx_ts_seconds_queue[i];
  116. ptp->tx_ts_nseconds_queue[i - c] = ptp->tx_ts_nseconds_queue[i];
  117. ptp->tx_ts_header_queue[i - c] = ptp->tx_ts_header_queue[i];
  118. ptp->tx_ts_skb_queue[i] = NULL;
  119. ptp->tx_ts_seconds_queue[i] = 0;
  120. ptp->tx_ts_nseconds_queue[i] = 0;
  121. ptp->tx_ts_header_queue[i] = 0;
  122. }
  123. ptp->tx_ts_skb_queue_size -= c;
  124. ptp->tx_ts_queue_size -= c;
  125. done:
  126. ptp->pending_tx_timestamps -= c;
  127. spin_unlock_bh(&ptp->tx_ts_lock);
  128. }
  129. static int lan743x_ptp_reserve_event_ch(struct lan743x_adapter *adapter,
  130. int event_channel)
  131. {
  132. struct lan743x_ptp *ptp = &adapter->ptp;
  133. int result = -ENODEV;
  134. mutex_lock(&ptp->command_lock);
  135. if (!(test_bit(event_channel, &ptp->used_event_ch))) {
  136. ptp->used_event_ch |= BIT(event_channel);
  137. result = event_channel;
  138. } else {
  139. netif_warn(adapter, drv, adapter->netdev,
  140. "attempted to reserved a used event_channel = %d\n",
  141. event_channel);
  142. }
  143. mutex_unlock(&ptp->command_lock);
  144. return result;
  145. }
  146. static void lan743x_ptp_release_event_ch(struct lan743x_adapter *adapter,
  147. int event_channel)
  148. {
  149. struct lan743x_ptp *ptp = &adapter->ptp;
  150. mutex_lock(&ptp->command_lock);
  151. if (test_bit(event_channel, &ptp->used_event_ch)) {
  152. ptp->used_event_ch &= ~BIT(event_channel);
  153. } else {
  154. netif_warn(adapter, drv, adapter->netdev,
  155. "attempted release on a not used event_channel = %d\n",
  156. event_channel);
  157. }
  158. mutex_unlock(&ptp->command_lock);
  159. }
  160. static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
  161. u32 *seconds, u32 *nano_seconds,
  162. u32 *sub_nano_seconds);
  163. static void lan743x_ptp_io_clock_get(struct lan743x_adapter *adapter,
  164. u32 *sec, u32 *nsec, u32 *sub_nsec);
  165. static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
  166. s64 time_step_ns);
  167. static void lan743x_led_mux_enable(struct lan743x_adapter *adapter,
  168. int pin, bool enable)
  169. {
  170. struct lan743x_ptp *ptp = &adapter->ptp;
  171. if (ptp->leds_multiplexed &&
  172. ptp->led_enabled[pin]) {
  173. u32 val = lan743x_csr_read(adapter, HW_CFG);
  174. if (enable)
  175. val |= LAN743X_LED_ENABLE(pin);
  176. else
  177. val &= ~LAN743X_LED_ENABLE(pin);
  178. lan743x_csr_write(adapter, HW_CFG, val);
  179. }
  180. }
  181. static void lan743x_led_mux_save(struct lan743x_adapter *adapter)
  182. {
  183. struct lan743x_ptp *ptp = &adapter->ptp;
  184. u32 id_rev = adapter->csr.id_rev & ID_REV_ID_MASK_;
  185. if (id_rev == ID_REV_ID_LAN7430_) {
  186. int i;
  187. u32 val = lan743x_csr_read(adapter, HW_CFG);
  188. for (i = 0; i < LAN7430_N_LED; i++) {
  189. bool led_enabled = (val & LAN743X_LED_ENABLE(i)) != 0;
  190. ptp->led_enabled[i] = led_enabled;
  191. }
  192. ptp->leds_multiplexed = true;
  193. } else {
  194. ptp->leds_multiplexed = false;
  195. }
  196. }
  197. static void lan743x_led_mux_restore(struct lan743x_adapter *adapter)
  198. {
  199. u32 id_rev = adapter->csr.id_rev & ID_REV_ID_MASK_;
  200. if (id_rev == ID_REV_ID_LAN7430_) {
  201. int i;
  202. for (i = 0; i < LAN7430_N_LED; i++)
  203. lan743x_led_mux_enable(adapter, i, true);
  204. }
  205. }
  206. static int lan743x_gpio_rsrv_ptp_out(struct lan743x_adapter *adapter,
  207. int pin, int event_channel)
  208. {
  209. struct lan743x_gpio *gpio = &adapter->gpio;
  210. unsigned long irq_flags = 0;
  211. int bit_mask = BIT(pin);
  212. int ret = -EBUSY;
  213. spin_lock_irqsave(&gpio->gpio_lock, irq_flags);
  214. if (!(gpio->used_bits & bit_mask)) {
  215. gpio->used_bits |= bit_mask;
  216. gpio->output_bits |= bit_mask;
  217. gpio->ptp_bits |= bit_mask;
  218. /* assign pin to GPIO function */
  219. lan743x_led_mux_enable(adapter, pin, false);
  220. /* set as output, and zero initial value */
  221. gpio->gpio_cfg0 |= GPIO_CFG0_GPIO_DIR_BIT_(pin);
  222. gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(pin);
  223. lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
  224. /* enable gpio, and set buffer type to push pull */
  225. gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOEN_BIT_(pin);
  226. gpio->gpio_cfg1 |= GPIO_CFG1_GPIOBUF_BIT_(pin);
  227. lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
  228. /* set 1588 polarity to high */
  229. gpio->gpio_cfg2 |= GPIO_CFG2_1588_POL_BIT_(pin);
  230. lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
  231. if (event_channel == 0) {
  232. /* use channel A */
  233. gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_CH_SEL_BIT_(pin);
  234. } else {
  235. /* use channel B */
  236. gpio->gpio_cfg3 |= GPIO_CFG3_1588_CH_SEL_BIT_(pin);
  237. }
  238. gpio->gpio_cfg3 |= GPIO_CFG3_1588_OE_BIT_(pin);
  239. lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);
  240. ret = pin;
  241. }
  242. spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
  243. return ret;
  244. }
  245. static void lan743x_gpio_release(struct lan743x_adapter *adapter, int pin)
  246. {
  247. struct lan743x_gpio *gpio = &adapter->gpio;
  248. unsigned long irq_flags = 0;
  249. int bit_mask = BIT(pin);
  250. spin_lock_irqsave(&gpio->gpio_lock, irq_flags);
  251. if (gpio->used_bits & bit_mask) {
  252. gpio->used_bits &= ~bit_mask;
  253. if (gpio->output_bits & bit_mask) {
  254. gpio->output_bits &= ~bit_mask;
  255. if (gpio->ptp_bits & bit_mask) {
  256. gpio->ptp_bits &= ~bit_mask;
  257. /* disable ptp output */
  258. gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_OE_BIT_(pin);
  259. lan743x_csr_write(adapter, GPIO_CFG3,
  260. gpio->gpio_cfg3);
  261. }
  262. /* release gpio output */
  263. /* disable gpio */
  264. gpio->gpio_cfg1 |= GPIO_CFG1_GPIOEN_BIT_(pin);
  265. gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOBUF_BIT_(pin);
  266. lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
  267. /* reset back to input */
  268. gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DIR_BIT_(pin);
  269. gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(pin);
  270. lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
  271. /* assign pin to original function */
  272. lan743x_led_mux_enable(adapter, pin, true);
  273. }
  274. }
  275. spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
  276. }
  277. static int lan743x_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
  278. {
  279. struct lan743x_ptp *ptp =
  280. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  281. struct lan743x_adapter *adapter =
  282. container_of(ptp, struct lan743x_adapter, ptp);
  283. u32 lan743x_rate_adj = 0;
  284. u64 u64_delta;
  285. if ((scaled_ppm < (-LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM)) ||
  286. scaled_ppm > LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM) {
  287. return -EINVAL;
  288. }
  289. /* diff_by_scaled_ppm returns true if the difference is negative */
  290. if (diff_by_scaled_ppm(1ULL << 35, scaled_ppm, &u64_delta))
  291. lan743x_rate_adj = (u32)u64_delta;
  292. else
  293. lan743x_rate_adj = (u32)u64_delta | PTP_CLOCK_RATE_ADJ_DIR_;
  294. lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
  295. lan743x_rate_adj);
  296. return 0;
  297. }
  298. static int lan743x_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
  299. {
  300. struct lan743x_ptp *ptp =
  301. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  302. struct lan743x_adapter *adapter =
  303. container_of(ptp, struct lan743x_adapter, ptp);
  304. lan743x_ptp_clock_step(adapter, delta);
  305. return 0;
  306. }
  307. static int lan743x_ptpci_gettime64(struct ptp_clock_info *ptpci,
  308. struct timespec64 *ts)
  309. {
  310. struct lan743x_ptp *ptp =
  311. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  312. struct lan743x_adapter *adapter =
  313. container_of(ptp, struct lan743x_adapter, ptp);
  314. u32 nano_seconds = 0;
  315. u32 seconds = 0;
  316. if (adapter->is_pci11x1x)
  317. lan743x_ptp_io_clock_get(adapter, &seconds, &nano_seconds,
  318. NULL);
  319. else
  320. lan743x_ptp_clock_get(adapter, &seconds, &nano_seconds, NULL);
  321. ts->tv_sec = seconds;
  322. ts->tv_nsec = nano_seconds;
  323. return 0;
  324. }
  325. static int lan743x_ptpci_settime64(struct ptp_clock_info *ptpci,
  326. const struct timespec64 *ts)
  327. {
  328. struct lan743x_ptp *ptp =
  329. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  330. struct lan743x_adapter *adapter =
  331. container_of(ptp, struct lan743x_adapter, ptp);
  332. u32 nano_seconds = 0;
  333. u32 seconds = 0;
  334. if (ts->tv_sec > 0xFFFFFFFFLL) {
  335. netif_warn(adapter, drv, adapter->netdev,
  336. "ts->tv_sec out of range, %lld\n",
  337. ts->tv_sec);
  338. return -ERANGE;
  339. }
  340. if (ts->tv_nsec < 0) {
  341. netif_warn(adapter, drv, adapter->netdev,
  342. "ts->tv_nsec out of range, %ld\n",
  343. ts->tv_nsec);
  344. return -ERANGE;
  345. }
  346. seconds = ts->tv_sec;
  347. nano_seconds = ts->tv_nsec;
  348. lan743x_ptp_clock_set(adapter, seconds, nano_seconds, 0);
  349. return 0;
  350. }
  351. static void lan743x_ptp_perout_off(struct lan743x_adapter *adapter,
  352. unsigned int index)
  353. {
  354. struct lan743x_ptp *ptp = &adapter->ptp;
  355. u32 general_config = 0;
  356. struct lan743x_ptp_perout *perout = &ptp->perout[index];
  357. if (perout->gpio_pin >= 0) {
  358. lan743x_gpio_release(adapter, perout->gpio_pin);
  359. perout->gpio_pin = -1;
  360. }
  361. if (perout->event_ch >= 0) {
  362. /* set target to far in the future, effectively disabling it */
  363. lan743x_csr_write(adapter,
  364. PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
  365. 0xFFFF0000);
  366. lan743x_csr_write(adapter,
  367. PTP_CLOCK_TARGET_NS_X(perout->event_ch),
  368. 0);
  369. general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
  370. general_config |= PTP_GENERAL_CONFIG_RELOAD_ADD_X_
  371. (perout->event_ch);
  372. lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
  373. lan743x_ptp_release_event_ch(adapter, perout->event_ch);
  374. perout->event_ch = -1;
  375. }
  376. }
  377. static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
  378. struct ptp_perout_request *perout_request)
  379. {
  380. struct lan743x_ptp *ptp = &adapter->ptp;
  381. u32 period_sec = 0, period_nsec = 0;
  382. u32 start_sec = 0, start_nsec = 0;
  383. u32 general_config = 0;
  384. int pulse_width = 0;
  385. int perout_pin = 0;
  386. unsigned int index = perout_request->index;
  387. struct lan743x_ptp_perout *perout = &ptp->perout[index];
  388. int ret = 0;
  389. if (on) {
  390. perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT,
  391. perout_request->index);
  392. if (perout_pin < 0)
  393. return -EBUSY;
  394. } else {
  395. lan743x_ptp_perout_off(adapter, index);
  396. return 0;
  397. }
  398. if (perout->event_ch >= 0 ||
  399. perout->gpio_pin >= 0) {
  400. /* already on, turn off first */
  401. lan743x_ptp_perout_off(adapter, index);
  402. }
  403. perout->event_ch = lan743x_ptp_reserve_event_ch(adapter, index);
  404. if (perout->event_ch < 0) {
  405. netif_warn(adapter, drv, adapter->netdev,
  406. "Failed to reserve event channel %d for PEROUT\n",
  407. index);
  408. ret = -EBUSY;
  409. goto failed;
  410. }
  411. perout->gpio_pin = lan743x_gpio_rsrv_ptp_out(adapter,
  412. perout_pin,
  413. perout->event_ch);
  414. if (perout->gpio_pin < 0) {
  415. netif_warn(adapter, drv, adapter->netdev,
  416. "Failed to reserve gpio %d for PEROUT\n",
  417. perout_pin);
  418. ret = -EBUSY;
  419. goto failed;
  420. }
  421. start_sec = perout_request->start.sec;
  422. start_sec += perout_request->start.nsec / 1000000000;
  423. start_nsec = perout_request->start.nsec % 1000000000;
  424. period_sec = perout_request->period.sec;
  425. period_sec += perout_request->period.nsec / 1000000000;
  426. period_nsec = perout_request->period.nsec % 1000000000;
  427. if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) {
  428. struct timespec64 ts_on, ts_period;
  429. s64 wf_high, period64, half;
  430. s32 reminder;
  431. ts_on.tv_sec = perout_request->on.sec;
  432. ts_on.tv_nsec = perout_request->on.nsec;
  433. wf_high = timespec64_to_ns(&ts_on);
  434. ts_period.tv_sec = perout_request->period.sec;
  435. ts_period.tv_nsec = perout_request->period.nsec;
  436. period64 = timespec64_to_ns(&ts_period);
  437. if (period64 < 200) {
  438. netif_warn(adapter, drv, adapter->netdev,
  439. "perout period too small, minimum is 200nS\n");
  440. ret = -EOPNOTSUPP;
  441. goto failed;
  442. }
  443. if (wf_high >= period64) {
  444. netif_warn(adapter, drv, adapter->netdev,
  445. "pulse width must be smaller than period\n");
  446. ret = -EINVAL;
  447. goto failed;
  448. }
  449. /* Check if we can do 50% toggle on an even value of period.
  450. * If the period number is odd, then check if the requested
  451. * pulse width is the same as one of pre-defined width values.
  452. * Otherwise, return failure.
  453. */
  454. half = div_s64_rem(period64, 2, &reminder);
  455. if (!reminder) {
  456. if (half == wf_high) {
  457. /* It's 50% match. Use the toggle option */
  458. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_TOGGLE_;
  459. /* In this case, divide period value by 2 */
  460. ts_period = ns_to_timespec64(div_s64(period64, 2));
  461. period_sec = ts_period.tv_sec;
  462. period_nsec = ts_period.tv_nsec;
  463. goto program;
  464. }
  465. }
  466. /* if we can't do toggle, then the width option needs to be the exact match */
  467. if (wf_high == 200000000) {
  468. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
  469. } else if (wf_high == 10000000) {
  470. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
  471. } else if (wf_high == 1000000) {
  472. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
  473. } else if (wf_high == 100000) {
  474. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
  475. } else if (wf_high == 10000) {
  476. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
  477. } else if (wf_high == 100) {
  478. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
  479. } else {
  480. netif_warn(adapter, drv, adapter->netdev,
  481. "duty cycle specified is not supported\n");
  482. ret = -EOPNOTSUPP;
  483. goto failed;
  484. }
  485. } else {
  486. if (period_sec == 0) {
  487. if (period_nsec >= 400000000) {
  488. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
  489. } else if (period_nsec >= 20000000) {
  490. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
  491. } else if (period_nsec >= 2000000) {
  492. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
  493. } else if (period_nsec >= 200000) {
  494. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
  495. } else if (period_nsec >= 20000) {
  496. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
  497. } else if (period_nsec >= 200) {
  498. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
  499. } else {
  500. netif_warn(adapter, drv, adapter->netdev,
  501. "perout period too small, minimum is 200nS\n");
  502. ret = -EOPNOTSUPP;
  503. goto failed;
  504. }
  505. } else {
  506. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
  507. }
  508. }
  509. program:
  510. /* turn off by setting target far in future */
  511. lan743x_csr_write(adapter,
  512. PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
  513. 0xFFFF0000);
  514. lan743x_csr_write(adapter,
  515. PTP_CLOCK_TARGET_NS_X(perout->event_ch), 0);
  516. /* Configure to pulse every period */
  517. general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
  518. general_config &= ~(PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
  519. (perout->event_ch));
  520. general_config |= PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_
  521. (perout->event_ch, pulse_width);
  522. general_config &= ~PTP_GENERAL_CONFIG_RELOAD_ADD_X_
  523. (perout->event_ch);
  524. lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
  525. /* set the reload to one toggle cycle */
  526. lan743x_csr_write(adapter,
  527. PTP_CLOCK_TARGET_RELOAD_SEC_X(perout->event_ch),
  528. period_sec);
  529. lan743x_csr_write(adapter,
  530. PTP_CLOCK_TARGET_RELOAD_NS_X(perout->event_ch),
  531. period_nsec);
  532. /* set the start time */
  533. lan743x_csr_write(adapter,
  534. PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
  535. start_sec);
  536. lan743x_csr_write(adapter,
  537. PTP_CLOCK_TARGET_NS_X(perout->event_ch),
  538. start_nsec);
  539. return 0;
  540. failed:
  541. lan743x_ptp_perout_off(adapter, index);
  542. return ret;
  543. }
  544. static void lan743x_ptp_io_perout_off(struct lan743x_adapter *adapter,
  545. u32 index)
  546. {
  547. struct lan743x_ptp *ptp = &adapter->ptp;
  548. int perout_pin;
  549. int event_ch;
  550. u32 gen_cfg;
  551. int val;
  552. event_ch = ptp->ptp_io_perout[index];
  553. if (event_ch >= 0) {
  554. /* set target to far in the future, effectively disabling it */
  555. lan743x_csr_write(adapter,
  556. PTP_CLOCK_TARGET_SEC_X(event_ch),
  557. 0xFFFF0000);
  558. lan743x_csr_write(adapter,
  559. PTP_CLOCK_TARGET_NS_X(event_ch),
  560. 0);
  561. gen_cfg = lan743x_csr_read(adapter, HS_PTP_GENERAL_CONFIG);
  562. gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
  563. (event_ch));
  564. gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_EVENT_POL_X_(event_ch));
  565. gen_cfg |= HS_PTP_GENERAL_CONFIG_RELOAD_ADD_X_(event_ch);
  566. lan743x_csr_write(adapter, HS_PTP_GENERAL_CONFIG, gen_cfg);
  567. if (event_ch)
  568. lan743x_csr_write(adapter, PTP_INT_STS,
  569. PTP_INT_TIMER_INT_B_);
  570. else
  571. lan743x_csr_write(adapter, PTP_INT_STS,
  572. PTP_INT_TIMER_INT_A_);
  573. lan743x_ptp_release_event_ch(adapter, event_ch);
  574. ptp->ptp_io_perout[index] = -1;
  575. }
  576. perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT, index);
  577. /* Deselect Event output */
  578. val = lan743x_csr_read(adapter, PTP_IO_EVENT_OUTPUT_CFG);
  579. /* Disables the output of Local Time Target compare events */
  580. val &= ~PTP_IO_EVENT_OUTPUT_CFG_EN_(perout_pin);
  581. lan743x_csr_write(adapter, PTP_IO_EVENT_OUTPUT_CFG, val);
  582. /* Configured as an opendrain driver*/
  583. val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG);
  584. val &= ~PTP_IO_PIN_CFG_OBUF_TYPE_(perout_pin);
  585. lan743x_csr_write(adapter, PTP_IO_PIN_CFG, val);
  586. /* Dummy read to make sure write operation success */
  587. val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG);
  588. }
  589. static int lan743x_ptp_io_perout(struct lan743x_adapter *adapter, int on,
  590. struct ptp_perout_request *perout_request)
  591. {
  592. struct lan743x_ptp *ptp = &adapter->ptp;
  593. u32 period_sec, period_nsec;
  594. u32 start_sec, start_nsec;
  595. u32 pulse_sec, pulse_nsec;
  596. int pulse_width;
  597. int perout_pin;
  598. int event_ch;
  599. u32 gen_cfg;
  600. u32 index;
  601. int val;
  602. index = perout_request->index;
  603. event_ch = ptp->ptp_io_perout[index];
  604. if (on) {
  605. perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT, index);
  606. if (perout_pin < 0)
  607. return -EBUSY;
  608. } else {
  609. lan743x_ptp_io_perout_off(adapter, index);
  610. return 0;
  611. }
  612. if (event_ch >= LAN743X_PTP_N_EVENT_CHAN) {
  613. /* already on, turn off first */
  614. lan743x_ptp_io_perout_off(adapter, index);
  615. }
  616. event_ch = lan743x_ptp_reserve_event_ch(adapter, index);
  617. if (event_ch < 0) {
  618. netif_warn(adapter, drv, adapter->netdev,
  619. "Failed to reserve event channel %d for PEROUT\n",
  620. index);
  621. goto failed;
  622. }
  623. ptp->ptp_io_perout[index] = event_ch;
  624. if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) {
  625. pulse_sec = perout_request->on.sec;
  626. pulse_sec += perout_request->on.nsec / 1000000000;
  627. pulse_nsec = perout_request->on.nsec % 1000000000;
  628. } else {
  629. pulse_sec = perout_request->period.sec;
  630. pulse_sec += perout_request->period.nsec / 1000000000;
  631. pulse_nsec = perout_request->period.nsec % 1000000000;
  632. }
  633. if (pulse_sec == 0) {
  634. if (pulse_nsec >= 400000000) {
  635. pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
  636. } else if (pulse_nsec >= 200000000) {
  637. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100MS_;
  638. } else if (pulse_nsec >= 100000000) {
  639. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_50MS_;
  640. } else if (pulse_nsec >= 20000000) {
  641. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
  642. } else if (pulse_nsec >= 10000000) {
  643. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_5MS_;
  644. } else if (pulse_nsec >= 2000000) {
  645. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
  646. } else if (pulse_nsec >= 1000000) {
  647. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_500US_;
  648. } else if (pulse_nsec >= 200000) {
  649. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
  650. } else if (pulse_nsec >= 100000) {
  651. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_50US_;
  652. } else if (pulse_nsec >= 20000) {
  653. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
  654. } else if (pulse_nsec >= 10000) {
  655. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_5US_;
  656. } else if (pulse_nsec >= 2000) {
  657. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_1US_;
  658. } else if (pulse_nsec >= 1000) {
  659. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_500NS_;
  660. } else if (pulse_nsec >= 200) {
  661. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
  662. } else {
  663. netif_warn(adapter, drv, adapter->netdev,
  664. "perout period too small, min is 200nS\n");
  665. goto failed;
  666. }
  667. } else {
  668. pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
  669. }
  670. /* turn off by setting target far in future */
  671. lan743x_csr_write(adapter,
  672. PTP_CLOCK_TARGET_SEC_X(event_ch),
  673. 0xFFFF0000);
  674. lan743x_csr_write(adapter,
  675. PTP_CLOCK_TARGET_NS_X(event_ch), 0);
  676. /* Configure to pulse every period */
  677. gen_cfg = lan743x_csr_read(adapter, HS_PTP_GENERAL_CONFIG);
  678. gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_(event_ch));
  679. gen_cfg |= HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_
  680. (event_ch, pulse_width);
  681. gen_cfg |= HS_PTP_GENERAL_CONFIG_EVENT_POL_X_(event_ch);
  682. gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_RELOAD_ADD_X_(event_ch));
  683. lan743x_csr_write(adapter, HS_PTP_GENERAL_CONFIG, gen_cfg);
  684. /* set the reload to one toggle cycle */
  685. period_sec = perout_request->period.sec;
  686. period_sec += perout_request->period.nsec / 1000000000;
  687. period_nsec = perout_request->period.nsec % 1000000000;
  688. lan743x_csr_write(adapter,
  689. PTP_CLOCK_TARGET_RELOAD_SEC_X(event_ch),
  690. period_sec);
  691. lan743x_csr_write(adapter,
  692. PTP_CLOCK_TARGET_RELOAD_NS_X(event_ch),
  693. period_nsec);
  694. start_sec = perout_request->start.sec;
  695. start_sec += perout_request->start.nsec / 1000000000;
  696. start_nsec = perout_request->start.nsec % 1000000000;
  697. /* set the start time */
  698. lan743x_csr_write(adapter,
  699. PTP_CLOCK_TARGET_SEC_X(event_ch),
  700. start_sec);
  701. lan743x_csr_write(adapter,
  702. PTP_CLOCK_TARGET_NS_X(event_ch),
  703. start_nsec);
  704. /* Enable LTC Target Read */
  705. val = lan743x_csr_read(adapter, PTP_CMD_CTL);
  706. val |= PTP_CMD_CTL_PTP_LTC_TARGET_READ_;
  707. lan743x_csr_write(adapter, PTP_CMD_CTL, val);
  708. /* Configure as an push/pull driver */
  709. val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG);
  710. val |= PTP_IO_PIN_CFG_OBUF_TYPE_(perout_pin);
  711. lan743x_csr_write(adapter, PTP_IO_PIN_CFG, val);
  712. /* Select Event output */
  713. val = lan743x_csr_read(adapter, PTP_IO_EVENT_OUTPUT_CFG);
  714. if (event_ch)
  715. /* Channel B as the output */
  716. val |= PTP_IO_EVENT_OUTPUT_CFG_SEL_(perout_pin);
  717. else
  718. /* Channel A as the output */
  719. val &= ~PTP_IO_EVENT_OUTPUT_CFG_SEL_(perout_pin);
  720. /* Enables the output of Local Time Target compare events */
  721. val |= PTP_IO_EVENT_OUTPUT_CFG_EN_(perout_pin);
  722. lan743x_csr_write(adapter, PTP_IO_EVENT_OUTPUT_CFG, val);
  723. return 0;
  724. failed:
  725. lan743x_ptp_io_perout_off(adapter, index);
  726. return -ENODEV;
  727. }
  728. static void lan743x_ptp_io_extts_off(struct lan743x_adapter *adapter,
  729. u32 index)
  730. {
  731. struct lan743x_ptp *ptp = &adapter->ptp;
  732. struct lan743x_extts *extts;
  733. int val;
  734. extts = &ptp->extts[index];
  735. /* PTP Interrupt Enable Clear Register */
  736. if (extts->flags & PTP_FALLING_EDGE)
  737. val = PTP_INT_EN_FE_EN_CLR_(index);
  738. else
  739. val = PTP_INT_EN_RE_EN_CLR_(index);
  740. lan743x_csr_write(adapter, PTP_INT_EN_CLR, val);
  741. /* Disables PTP-IO edge lock */
  742. val = lan743x_csr_read(adapter, PTP_IO_CAP_CONFIG);
  743. if (extts->flags & PTP_FALLING_EDGE) {
  744. val &= ~PTP_IO_CAP_CONFIG_LOCK_FE_(index);
  745. val &= ~PTP_IO_CAP_CONFIG_FE_CAP_EN_(index);
  746. } else {
  747. val &= ~PTP_IO_CAP_CONFIG_LOCK_RE_(index);
  748. val &= ~PTP_IO_CAP_CONFIG_RE_CAP_EN_(index);
  749. }
  750. lan743x_csr_write(adapter, PTP_IO_CAP_CONFIG, val);
  751. /* PTP-IO De-select register */
  752. val = lan743x_csr_read(adapter, PTP_IO_SEL);
  753. val &= ~PTP_IO_SEL_MASK_;
  754. lan743x_csr_write(adapter, PTP_IO_SEL, val);
  755. /* Clear timestamp */
  756. memset(&extts->ts, 0, sizeof(struct timespec64));
  757. extts->flags = 0;
  758. }
  759. static int lan743x_ptp_io_event_cap_en(struct lan743x_adapter *adapter,
  760. u32 flags, u32 channel)
  761. {
  762. struct lan743x_ptp *ptp = &adapter->ptp;
  763. int val;
  764. if ((flags & PTP_EXTTS_EDGES) == PTP_EXTTS_EDGES)
  765. return -EOPNOTSUPP;
  766. mutex_lock(&ptp->command_lock);
  767. /* PTP-IO Event Capture Enable */
  768. val = lan743x_csr_read(adapter, PTP_IO_CAP_CONFIG);
  769. if (flags & PTP_FALLING_EDGE) {
  770. val &= ~PTP_IO_CAP_CONFIG_LOCK_RE_(channel);
  771. val &= ~PTP_IO_CAP_CONFIG_RE_CAP_EN_(channel);
  772. val |= PTP_IO_CAP_CONFIG_LOCK_FE_(channel);
  773. val |= PTP_IO_CAP_CONFIG_FE_CAP_EN_(channel);
  774. } else {
  775. /* Rising eventing as Default */
  776. val &= ~PTP_IO_CAP_CONFIG_LOCK_FE_(channel);
  777. val &= ~PTP_IO_CAP_CONFIG_FE_CAP_EN_(channel);
  778. val |= PTP_IO_CAP_CONFIG_LOCK_RE_(channel);
  779. val |= PTP_IO_CAP_CONFIG_RE_CAP_EN_(channel);
  780. }
  781. lan743x_csr_write(adapter, PTP_IO_CAP_CONFIG, val);
  782. /* PTP-IO Select */
  783. val = lan743x_csr_read(adapter, PTP_IO_SEL);
  784. val &= ~PTP_IO_SEL_MASK_;
  785. val |= channel << PTP_IO_SEL_SHIFT_;
  786. lan743x_csr_write(adapter, PTP_IO_SEL, val);
  787. /* PTP Interrupt Enable Register */
  788. if (flags & PTP_FALLING_EDGE)
  789. val = PTP_INT_EN_FE_EN_SET_(channel);
  790. else
  791. val = PTP_INT_EN_RE_EN_SET_(channel);
  792. lan743x_csr_write(adapter, PTP_INT_EN_SET, val);
  793. mutex_unlock(&ptp->command_lock);
  794. return 0;
  795. }
  796. static int lan743x_ptp_io_extts(struct lan743x_adapter *adapter, int on,
  797. struct ptp_extts_request *extts_request)
  798. {
  799. struct lan743x_ptp *ptp = &adapter->ptp;
  800. u32 flags = extts_request->flags;
  801. u32 index = extts_request->index;
  802. struct lan743x_extts *extts;
  803. int extts_pin;
  804. int ret = 0;
  805. extts = &ptp->extts[index];
  806. if (on) {
  807. extts_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_EXTTS, index);
  808. if (extts_pin < 0)
  809. return -EBUSY;
  810. ret = lan743x_ptp_io_event_cap_en(adapter, flags, index);
  811. if (!ret)
  812. extts->flags = flags;
  813. } else {
  814. lan743x_ptp_io_extts_off(adapter, index);
  815. }
  816. return ret;
  817. }
  818. static int lan743x_ptpci_enable(struct ptp_clock_info *ptpci,
  819. struct ptp_clock_request *request, int on)
  820. {
  821. struct lan743x_ptp *ptp =
  822. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  823. struct lan743x_adapter *adapter =
  824. container_of(ptp, struct lan743x_adapter, ptp);
  825. if (request) {
  826. switch (request->type) {
  827. case PTP_CLK_REQ_EXTTS:
  828. if (request->extts.index < ptpci->n_ext_ts)
  829. return lan743x_ptp_io_extts(adapter, on,
  830. &request->extts);
  831. return -EINVAL;
  832. case PTP_CLK_REQ_PEROUT:
  833. if (request->perout.index < ptpci->n_per_out) {
  834. if (adapter->is_pci11x1x)
  835. return lan743x_ptp_io_perout(adapter, on,
  836. &request->perout);
  837. else
  838. return lan743x_ptp_perout(adapter, on,
  839. &request->perout);
  840. }
  841. return -EINVAL;
  842. case PTP_CLK_REQ_PPS:
  843. return -EINVAL;
  844. default:
  845. netif_err(adapter, drv, adapter->netdev,
  846. "request->type == %d, Unknown\n",
  847. request->type);
  848. break;
  849. }
  850. } else {
  851. netif_err(adapter, drv, adapter->netdev, "request == NULL\n");
  852. }
  853. return 0;
  854. }
  855. static int lan743x_ptpci_verify_pin_config(struct ptp_clock_info *ptp,
  856. unsigned int pin,
  857. enum ptp_pin_function func,
  858. unsigned int chan)
  859. {
  860. struct lan743x_ptp *lan_ptp =
  861. container_of(ptp, struct lan743x_ptp, ptp_clock_info);
  862. struct lan743x_adapter *adapter =
  863. container_of(lan_ptp, struct lan743x_adapter, ptp);
  864. int result = 0;
  865. /* Confirm the requested function is supported. Parameter
  866. * validation is done by the caller.
  867. */
  868. switch (func) {
  869. case PTP_PF_NONE:
  870. case PTP_PF_PEROUT:
  871. break;
  872. case PTP_PF_EXTTS:
  873. if (!adapter->is_pci11x1x)
  874. result = -1;
  875. break;
  876. case PTP_PF_PHYSYNC:
  877. default:
  878. result = -1;
  879. break;
  880. }
  881. return result;
  882. }
  883. static void lan743x_ptp_io_event_clock_get(struct lan743x_adapter *adapter,
  884. bool fe, u8 channel,
  885. struct timespec64 *ts)
  886. {
  887. struct lan743x_ptp *ptp = &adapter->ptp;
  888. struct lan743x_extts *extts;
  889. u32 sec, nsec;
  890. mutex_lock(&ptp->command_lock);
  891. if (fe) {
  892. sec = lan743x_csr_read(adapter, PTP_IO_FE_LTC_SEC_CAP_X);
  893. nsec = lan743x_csr_read(adapter, PTP_IO_FE_LTC_NS_CAP_X);
  894. } else {
  895. sec = lan743x_csr_read(adapter, PTP_IO_RE_LTC_SEC_CAP_X);
  896. nsec = lan743x_csr_read(adapter, PTP_IO_RE_LTC_NS_CAP_X);
  897. }
  898. mutex_unlock(&ptp->command_lock);
  899. /* Update Local timestamp */
  900. extts = &ptp->extts[channel];
  901. extts->ts.tv_sec = sec;
  902. extts->ts.tv_nsec = nsec;
  903. ts->tv_sec = sec;
  904. ts->tv_nsec = nsec;
  905. }
  906. static long lan743x_ptpci_do_aux_work(struct ptp_clock_info *ptpci)
  907. {
  908. struct lan743x_ptp *ptp =
  909. container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
  910. struct lan743x_adapter *adapter =
  911. container_of(ptp, struct lan743x_adapter, ptp);
  912. u32 cap_info, cause, header, nsec, seconds;
  913. bool new_timestamp_available = false;
  914. struct ptp_clock_event ptp_event;
  915. struct timespec64 ts;
  916. int ptp_int_sts;
  917. int count = 0;
  918. int channel;
  919. s64 ns;
  920. ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
  921. while ((count < 100) && ptp_int_sts) {
  922. count++;
  923. if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
  924. cap_info = lan743x_csr_read(adapter, PTP_CAP_INFO);
  925. if (PTP_CAP_INFO_TX_TS_CNT_GET_(cap_info) > 0) {
  926. seconds = lan743x_csr_read(adapter,
  927. PTP_TX_EGRESS_SEC);
  928. nsec = lan743x_csr_read(adapter,
  929. PTP_TX_EGRESS_NS);
  930. cause = (nsec &
  931. PTP_TX_EGRESS_NS_CAPTURE_CAUSE_MASK_);
  932. header = lan743x_csr_read(adapter,
  933. PTP_TX_MSG_HEADER);
  934. if (cause ==
  935. PTP_TX_EGRESS_NS_CAPTURE_CAUSE_SW_) {
  936. nsec &= PTP_TX_EGRESS_NS_TS_NS_MASK_;
  937. lan743x_ptp_tx_ts_enqueue_ts(adapter,
  938. seconds,
  939. nsec,
  940. header);
  941. new_timestamp_available = true;
  942. } else if (cause ==
  943. PTP_TX_EGRESS_NS_CAPTURE_CAUSE_AUTO_) {
  944. netif_err(adapter, drv, adapter->netdev,
  945. "Auto capture cause not supported\n");
  946. } else {
  947. netif_warn(adapter, drv, adapter->netdev,
  948. "unknown tx timestamp capture cause\n");
  949. }
  950. } else {
  951. netif_warn(adapter, drv, adapter->netdev,
  952. "TX TS INT but no TX TS CNT\n");
  953. }
  954. lan743x_csr_write(adapter, PTP_INT_STS,
  955. PTP_INT_BIT_TX_TS_);
  956. }
  957. if (ptp_int_sts & PTP_INT_IO_FE_MASK_) {
  958. do {
  959. channel = lan743x_get_channel((ptp_int_sts &
  960. PTP_INT_IO_FE_MASK_) >>
  961. PTP_INT_IO_FE_SHIFT_);
  962. if (channel >= 0 &&
  963. channel < PCI11X1X_PTP_IO_MAX_CHANNELS) {
  964. lan743x_ptp_io_event_clock_get(adapter,
  965. true,
  966. channel,
  967. &ts);
  968. /* PTP Falling Event post */
  969. ns = timespec64_to_ns(&ts);
  970. ptp_event.timestamp = ns;
  971. ptp_event.index = channel;
  972. ptp_event.type = PTP_CLOCK_EXTTS;
  973. ptp_clock_event(ptp->ptp_clock,
  974. &ptp_event);
  975. lan743x_csr_write(adapter, PTP_INT_STS,
  976. PTP_INT_IO_FE_SET_
  977. (channel));
  978. ptp_int_sts &= ~(1 <<
  979. (PTP_INT_IO_FE_SHIFT_ +
  980. channel));
  981. } else {
  982. /* Clear falling event interrupts */
  983. lan743x_csr_write(adapter, PTP_INT_STS,
  984. PTP_INT_IO_FE_MASK_);
  985. ptp_int_sts &= ~PTP_INT_IO_FE_MASK_;
  986. }
  987. } while (ptp_int_sts & PTP_INT_IO_FE_MASK_);
  988. }
  989. if (ptp_int_sts & PTP_INT_IO_RE_MASK_) {
  990. do {
  991. channel = lan743x_get_channel((ptp_int_sts &
  992. PTP_INT_IO_RE_MASK_) >>
  993. PTP_INT_IO_RE_SHIFT_);
  994. if (channel >= 0 &&
  995. channel < PCI11X1X_PTP_IO_MAX_CHANNELS) {
  996. lan743x_ptp_io_event_clock_get(adapter,
  997. false,
  998. channel,
  999. &ts);
  1000. /* PTP Rising Event post */
  1001. ns = timespec64_to_ns(&ts);
  1002. ptp_event.timestamp = ns;
  1003. ptp_event.index = channel;
  1004. ptp_event.type = PTP_CLOCK_EXTTS;
  1005. ptp_clock_event(ptp->ptp_clock,
  1006. &ptp_event);
  1007. lan743x_csr_write(adapter, PTP_INT_STS,
  1008. PTP_INT_IO_RE_SET_
  1009. (channel));
  1010. ptp_int_sts &= ~(1 <<
  1011. (PTP_INT_IO_RE_SHIFT_ +
  1012. channel));
  1013. } else {
  1014. /* Clear Rising event interrupt */
  1015. lan743x_csr_write(adapter, PTP_INT_STS,
  1016. PTP_INT_IO_RE_MASK_);
  1017. ptp_int_sts &= ~PTP_INT_IO_RE_MASK_;
  1018. }
  1019. } while (ptp_int_sts & PTP_INT_IO_RE_MASK_);
  1020. }
  1021. ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
  1022. }
  1023. if (new_timestamp_available)
  1024. lan743x_ptp_tx_ts_complete(adapter);
  1025. lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
  1026. return -1;
  1027. }
  1028. static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
  1029. u32 *seconds, u32 *nano_seconds,
  1030. u32 *sub_nano_seconds)
  1031. {
  1032. struct lan743x_ptp *ptp = &adapter->ptp;
  1033. mutex_lock(&ptp->command_lock);
  1034. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
  1035. lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);
  1036. if (seconds)
  1037. (*seconds) = lan743x_csr_read(adapter, PTP_CLOCK_SEC);
  1038. if (nano_seconds)
  1039. (*nano_seconds) = lan743x_csr_read(adapter, PTP_CLOCK_NS);
  1040. if (sub_nano_seconds)
  1041. (*sub_nano_seconds) =
  1042. lan743x_csr_read(adapter, PTP_CLOCK_SUBNS);
  1043. mutex_unlock(&ptp->command_lock);
  1044. }
  1045. static void lan743x_ptp_io_clock_get(struct lan743x_adapter *adapter,
  1046. u32 *sec, u32 *nsec, u32 *sub_nsec)
  1047. {
  1048. struct lan743x_ptp *ptp = &adapter->ptp;
  1049. mutex_lock(&ptp->command_lock);
  1050. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
  1051. lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);
  1052. if (sec)
  1053. (*sec) = lan743x_csr_read(adapter, PTP_LTC_RD_SEC_LO);
  1054. if (nsec)
  1055. (*nsec) = lan743x_csr_read(adapter, PTP_LTC_RD_NS);
  1056. if (sub_nsec)
  1057. (*sub_nsec) =
  1058. lan743x_csr_read(adapter, PTP_LTC_RD_SUBNS);
  1059. mutex_unlock(&ptp->command_lock);
  1060. }
  1061. static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
  1062. s64 time_step_ns)
  1063. {
  1064. struct lan743x_ptp *ptp = &adapter->ptp;
  1065. u32 nano_seconds_step = 0;
  1066. u64 abs_time_step_ns = 0;
  1067. u32 unsigned_seconds = 0;
  1068. u32 nano_seconds = 0;
  1069. u32 remainder = 0;
  1070. s32 seconds = 0;
  1071. if (time_step_ns > 15000000000LL) {
  1072. /* convert to clock set */
  1073. if (adapter->is_pci11x1x)
  1074. lan743x_ptp_io_clock_get(adapter, &unsigned_seconds,
  1075. &nano_seconds, NULL);
  1076. else
  1077. lan743x_ptp_clock_get(adapter, &unsigned_seconds,
  1078. &nano_seconds, NULL);
  1079. unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
  1080. &remainder);
  1081. nano_seconds += remainder;
  1082. if (nano_seconds >= 1000000000) {
  1083. unsigned_seconds++;
  1084. nano_seconds -= 1000000000;
  1085. }
  1086. lan743x_ptp_clock_set(adapter, unsigned_seconds,
  1087. nano_seconds, 0);
  1088. return;
  1089. } else if (time_step_ns < -15000000000LL) {
  1090. /* convert to clock set */
  1091. time_step_ns = -time_step_ns;
  1092. if (adapter->is_pci11x1x) {
  1093. lan743x_ptp_io_clock_get(adapter, &unsigned_seconds,
  1094. &nano_seconds, NULL);
  1095. } else {
  1096. lan743x_ptp_clock_get(adapter, &unsigned_seconds,
  1097. &nano_seconds, NULL);
  1098. }
  1099. unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
  1100. &remainder);
  1101. nano_seconds_step = remainder;
  1102. if (nano_seconds < nano_seconds_step) {
  1103. unsigned_seconds--;
  1104. nano_seconds += 1000000000;
  1105. }
  1106. nano_seconds -= nano_seconds_step;
  1107. lan743x_ptp_clock_set(adapter, unsigned_seconds,
  1108. nano_seconds, 0);
  1109. return;
  1110. }
  1111. /* do clock step */
  1112. if (time_step_ns >= 0) {
  1113. abs_time_step_ns = (u64)(time_step_ns);
  1114. seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
  1115. &remainder);
  1116. nano_seconds = (u32)remainder;
  1117. } else {
  1118. abs_time_step_ns = (u64)(-time_step_ns);
  1119. seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
  1120. &remainder));
  1121. nano_seconds = (u32)remainder;
  1122. if (nano_seconds > 0) {
  1123. /* subtracting nano seconds is not allowed
  1124. * convert to subtracting from seconds,
  1125. * and adding to nanoseconds
  1126. */
  1127. seconds--;
  1128. nano_seconds = (1000000000 - nano_seconds);
  1129. }
  1130. }
  1131. if (nano_seconds > 0) {
  1132. /* add 8 ns to cover the likely normal increment */
  1133. nano_seconds += 8;
  1134. }
  1135. if (nano_seconds >= 1000000000) {
  1136. /* carry into seconds */
  1137. seconds++;
  1138. nano_seconds -= 1000000000;
  1139. }
  1140. while (seconds) {
  1141. mutex_lock(&ptp->command_lock);
  1142. if (seconds > 0) {
  1143. u32 adjustment_value = (u32)seconds;
  1144. if (adjustment_value > 0xF)
  1145. adjustment_value = 0xF;
  1146. lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
  1147. PTP_CLOCK_STEP_ADJ_DIR_ |
  1148. adjustment_value);
  1149. seconds -= ((s32)adjustment_value);
  1150. } else {
  1151. u32 adjustment_value = (u32)(-seconds);
  1152. if (adjustment_value > 0xF)
  1153. adjustment_value = 0xF;
  1154. lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
  1155. adjustment_value);
  1156. seconds += ((s32)adjustment_value);
  1157. }
  1158. lan743x_csr_write(adapter, PTP_CMD_CTL,
  1159. PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
  1160. lan743x_ptp_wait_till_cmd_done(adapter,
  1161. PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
  1162. mutex_unlock(&ptp->command_lock);
  1163. }
  1164. if (nano_seconds) {
  1165. mutex_lock(&ptp->command_lock);
  1166. lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
  1167. PTP_CLOCK_STEP_ADJ_DIR_ |
  1168. (nano_seconds &
  1169. PTP_CLOCK_STEP_ADJ_VALUE_MASK_));
  1170. lan743x_csr_write(adapter, PTP_CMD_CTL,
  1171. PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
  1172. lan743x_ptp_wait_till_cmd_done(adapter,
  1173. PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
  1174. mutex_unlock(&ptp->command_lock);
  1175. }
  1176. }
  1177. void lan743x_ptp_isr(void *context)
  1178. {
  1179. struct lan743x_adapter *adapter = (struct lan743x_adapter *)context;
  1180. struct lan743x_ptp *ptp = NULL;
  1181. int enable_flag = 1;
  1182. u32 ptp_int_sts = 0;
  1183. ptp = &adapter->ptp;
  1184. lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
  1185. ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
  1186. ptp_int_sts &= lan743x_csr_read(adapter, PTP_INT_EN_SET);
  1187. if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
  1188. ptp_schedule_worker(ptp->ptp_clock, 0);
  1189. enable_flag = 0;/* tasklet will re-enable later */
  1190. }
  1191. if (ptp_int_sts & PTP_INT_BIT_TX_SWTS_ERR_) {
  1192. netif_err(adapter, drv, adapter->netdev,
  1193. "PTP TX Software Timestamp Error\n");
  1194. /* clear int status bit */
  1195. lan743x_csr_write(adapter, PTP_INT_STS,
  1196. PTP_INT_BIT_TX_SWTS_ERR_);
  1197. }
  1198. if (ptp_int_sts & PTP_INT_BIT_TIMER_B_) {
  1199. /* clear int status bit */
  1200. lan743x_csr_write(adapter, PTP_INT_STS,
  1201. PTP_INT_BIT_TIMER_B_);
  1202. }
  1203. if (ptp_int_sts & PTP_INT_BIT_TIMER_A_) {
  1204. /* clear int status bit */
  1205. lan743x_csr_write(adapter, PTP_INT_STS,
  1206. PTP_INT_BIT_TIMER_A_);
  1207. }
  1208. if (enable_flag) {
  1209. /* re-enable isr */
  1210. lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
  1211. }
  1212. }
  1213. static void lan743x_ptp_tx_ts_enqueue_skb(struct lan743x_adapter *adapter,
  1214. struct sk_buff *skb, bool ignore_sync)
  1215. {
  1216. struct lan743x_ptp *ptp = &adapter->ptp;
  1217. spin_lock_bh(&ptp->tx_ts_lock);
  1218. if (ptp->tx_ts_skb_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
  1219. ptp->tx_ts_skb_queue[ptp->tx_ts_skb_queue_size] = skb;
  1220. if (ignore_sync)
  1221. ptp->tx_ts_ignore_sync_queue |=
  1222. BIT(ptp->tx_ts_skb_queue_size);
  1223. ptp->tx_ts_skb_queue_size++;
  1224. } else {
  1225. /* this should never happen, so long as the tx channel
  1226. * calls and honors the result from
  1227. * lan743x_ptp_request_tx_timestamp
  1228. */
  1229. netif_err(adapter, drv, adapter->netdev,
  1230. "tx ts skb queue overflow\n");
  1231. dev_kfree_skb(skb);
  1232. }
  1233. spin_unlock_bh(&ptp->tx_ts_lock);
  1234. }
  1235. static void lan743x_ptp_sync_to_system_clock(struct lan743x_adapter *adapter)
  1236. {
  1237. struct timespec64 ts;
  1238. ktime_get_clocktai_ts64(&ts);
  1239. lan743x_ptp_clock_set(adapter, ts.tv_sec, ts.tv_nsec, 0);
  1240. }
  1241. void lan743x_ptp_update_latency(struct lan743x_adapter *adapter,
  1242. u32 link_speed)
  1243. {
  1244. switch (link_speed) {
  1245. case 10:
  1246. lan743x_csr_write(adapter, PTP_LATENCY,
  1247. PTP_LATENCY_TX_SET_(0) |
  1248. PTP_LATENCY_RX_SET_(0));
  1249. break;
  1250. case 100:
  1251. lan743x_csr_write(adapter, PTP_LATENCY,
  1252. PTP_LATENCY_TX_SET_(181) |
  1253. PTP_LATENCY_RX_SET_(594));
  1254. break;
  1255. case 1000:
  1256. lan743x_csr_write(adapter, PTP_LATENCY,
  1257. PTP_LATENCY_TX_SET_(30) |
  1258. PTP_LATENCY_RX_SET_(525));
  1259. break;
  1260. }
  1261. }
  1262. int lan743x_ptp_init(struct lan743x_adapter *adapter)
  1263. {
  1264. struct lan743x_ptp *ptp = &adapter->ptp;
  1265. int i;
  1266. mutex_init(&ptp->command_lock);
  1267. spin_lock_init(&ptp->tx_ts_lock);
  1268. ptp->used_event_ch = 0;
  1269. for (i = 0; i < LAN743X_PTP_N_EVENT_CHAN; i++) {
  1270. ptp->perout[i].event_ch = -1;
  1271. ptp->perout[i].gpio_pin = -1;
  1272. }
  1273. lan743x_led_mux_save(adapter);
  1274. return 0;
  1275. }
  1276. int lan743x_ptp_open(struct lan743x_adapter *adapter)
  1277. {
  1278. struct lan743x_ptp *ptp = &adapter->ptp;
  1279. int ret = -ENODEV;
  1280. u32 temp;
  1281. int i;
  1282. int n_pins;
  1283. lan743x_ptp_reset(adapter);
  1284. lan743x_ptp_sync_to_system_clock(adapter);
  1285. temp = lan743x_csr_read(adapter, PTP_TX_MOD2);
  1286. temp |= PTP_TX_MOD2_TX_PTP_CLR_UDPV4_CHKSUM_;
  1287. lan743x_csr_write(adapter, PTP_TX_MOD2, temp);
  1288. /* Default Timestamping */
  1289. lan743x_rx_set_tstamp_mode(adapter, HWTSTAMP_FILTER_NONE);
  1290. lan743x_ptp_enable(adapter);
  1291. lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
  1292. lan743x_csr_write(adapter, PTP_INT_EN_SET,
  1293. PTP_INT_BIT_TX_SWTS_ERR_ | PTP_INT_BIT_TX_TS_);
  1294. ptp->flags |= PTP_FLAG_ISR_ENABLED;
  1295. if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK))
  1296. return 0;
  1297. switch (adapter->csr.id_rev & ID_REV_ID_MASK_) {
  1298. case ID_REV_ID_LAN7430_:
  1299. n_pins = LAN7430_N_GPIO;
  1300. break;
  1301. case ID_REV_ID_LAN7431_:
  1302. case ID_REV_ID_A011_:
  1303. case ID_REV_ID_A041_:
  1304. n_pins = LAN7431_N_GPIO;
  1305. break;
  1306. default:
  1307. netif_warn(adapter, drv, adapter->netdev,
  1308. "Unknown LAN743x (%08x). Assuming no GPIO\n",
  1309. adapter->csr.id_rev);
  1310. n_pins = 0;
  1311. break;
  1312. }
  1313. if (n_pins > LAN743X_PTP_N_GPIO)
  1314. n_pins = LAN743X_PTP_N_GPIO;
  1315. for (i = 0; i < n_pins; i++) {
  1316. struct ptp_pin_desc *ptp_pin = &ptp->pin_config[i];
  1317. snprintf(ptp_pin->name,
  1318. sizeof(ptp_pin->name), "lan743x_ptp_pin_%02d", i);
  1319. ptp_pin->index = i;
  1320. ptp_pin->func = PTP_PF_NONE;
  1321. }
  1322. ptp->ptp_clock_info.owner = THIS_MODULE;
  1323. snprintf(ptp->ptp_clock_info.name, 16, "%pm",
  1324. adapter->netdev->dev_addr);
  1325. ptp->ptp_clock_info.max_adj = LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB;
  1326. ptp->ptp_clock_info.n_alarm = 0;
  1327. ptp->ptp_clock_info.n_ext_ts = LAN743X_PTP_N_EXTTS;
  1328. ptp->ptp_clock_info.n_per_out = LAN743X_PTP_N_EVENT_CHAN;
  1329. ptp->ptp_clock_info.n_pins = n_pins;
  1330. ptp->ptp_clock_info.pps = LAN743X_PTP_N_PPS;
  1331. ptp->ptp_clock_info.supported_extts_flags = PTP_RISING_EDGE |
  1332. PTP_FALLING_EDGE |
  1333. PTP_STRICT_FLAGS;
  1334. ptp->ptp_clock_info.supported_perout_flags = PTP_PEROUT_DUTY_CYCLE;
  1335. ptp->ptp_clock_info.pin_config = ptp->pin_config;
  1336. ptp->ptp_clock_info.adjfine = lan743x_ptpci_adjfine;
  1337. ptp->ptp_clock_info.adjtime = lan743x_ptpci_adjtime;
  1338. ptp->ptp_clock_info.gettime64 = lan743x_ptpci_gettime64;
  1339. ptp->ptp_clock_info.getcrosststamp = NULL;
  1340. ptp->ptp_clock_info.settime64 = lan743x_ptpci_settime64;
  1341. ptp->ptp_clock_info.enable = lan743x_ptpci_enable;
  1342. ptp->ptp_clock_info.do_aux_work = lan743x_ptpci_do_aux_work;
  1343. ptp->ptp_clock_info.verify = lan743x_ptpci_verify_pin_config;
  1344. ptp->ptp_clock = ptp_clock_register(&ptp->ptp_clock_info,
  1345. &adapter->pdev->dev);
  1346. if (IS_ERR(ptp->ptp_clock)) {
  1347. netif_err(adapter, ifup, adapter->netdev,
  1348. "ptp_clock_register failed\n");
  1349. goto done;
  1350. }
  1351. ptp->flags |= PTP_FLAG_PTP_CLOCK_REGISTERED;
  1352. netif_info(adapter, ifup, adapter->netdev,
  1353. "successfully registered ptp clock\n");
  1354. return 0;
  1355. done:
  1356. lan743x_ptp_close(adapter);
  1357. return ret;
  1358. }
  1359. void lan743x_ptp_close(struct lan743x_adapter *adapter)
  1360. {
  1361. struct lan743x_ptp *ptp = &adapter->ptp;
  1362. int index;
  1363. if (IS_ENABLED(CONFIG_PTP_1588_CLOCK) &&
  1364. (ptp->flags & PTP_FLAG_PTP_CLOCK_REGISTERED)) {
  1365. ptp_clock_unregister(ptp->ptp_clock);
  1366. ptp->ptp_clock = NULL;
  1367. ptp->flags &= ~PTP_FLAG_PTP_CLOCK_REGISTERED;
  1368. netif_info(adapter, drv, adapter->netdev,
  1369. "ptp clock unregister\n");
  1370. }
  1371. if (ptp->flags & PTP_FLAG_ISR_ENABLED) {
  1372. lan743x_csr_write(adapter, PTP_INT_EN_CLR,
  1373. PTP_INT_BIT_TX_SWTS_ERR_ |
  1374. PTP_INT_BIT_TX_TS_);
  1375. lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
  1376. ptp->flags &= ~PTP_FLAG_ISR_ENABLED;
  1377. }
  1378. /* clean up pending timestamp requests */
  1379. lan743x_ptp_tx_ts_complete(adapter);
  1380. spin_lock_bh(&ptp->tx_ts_lock);
  1381. for (index = 0;
  1382. index < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS;
  1383. index++) {
  1384. struct sk_buff *skb = ptp->tx_ts_skb_queue[index];
  1385. dev_kfree_skb(skb);
  1386. ptp->tx_ts_skb_queue[index] = NULL;
  1387. ptp->tx_ts_seconds_queue[index] = 0;
  1388. ptp->tx_ts_nseconds_queue[index] = 0;
  1389. }
  1390. ptp->tx_ts_skb_queue_size = 0;
  1391. ptp->tx_ts_queue_size = 0;
  1392. ptp->pending_tx_timestamps = 0;
  1393. spin_unlock_bh(&ptp->tx_ts_lock);
  1394. lan743x_led_mux_restore(adapter);
  1395. lan743x_ptp_disable(adapter);
  1396. }
  1397. static void lan743x_ptp_set_sync_ts_insert(struct lan743x_adapter *adapter,
  1398. bool ts_insert_enable)
  1399. {
  1400. u32 ptp_tx_mod = lan743x_csr_read(adapter, PTP_TX_MOD);
  1401. if (ts_insert_enable)
  1402. ptp_tx_mod |= PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
  1403. else
  1404. ptp_tx_mod &= ~PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
  1405. lan743x_csr_write(adapter, PTP_TX_MOD, ptp_tx_mod);
  1406. }
  1407. static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter)
  1408. {
  1409. if (lan743x_csr_read(adapter, PTP_CMD_CTL) & PTP_CMD_CTL_PTP_ENABLE_)
  1410. return true;
  1411. return false;
  1412. }
  1413. static void lan743x_ptp_enable(struct lan743x_adapter *adapter)
  1414. {
  1415. struct lan743x_ptp *ptp = &adapter->ptp;
  1416. mutex_lock(&ptp->command_lock);
  1417. if (lan743x_ptp_is_enabled(adapter)) {
  1418. netif_warn(adapter, drv, adapter->netdev,
  1419. "PTP already enabled\n");
  1420. goto done;
  1421. }
  1422. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_ENABLE_);
  1423. done:
  1424. mutex_unlock(&ptp->command_lock);
  1425. }
  1426. static void lan743x_ptp_disable(struct lan743x_adapter *adapter)
  1427. {
  1428. struct lan743x_ptp *ptp = &adapter->ptp;
  1429. /* Disable Timestamping */
  1430. lan743x_rx_set_tstamp_mode(adapter, HWTSTAMP_FILTER_NONE);
  1431. mutex_lock(&ptp->command_lock);
  1432. if (!lan743x_ptp_is_enabled(adapter)) {
  1433. netif_warn(adapter, drv, adapter->netdev,
  1434. "PTP already disabled\n");
  1435. goto done;
  1436. }
  1437. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_DISABLE_);
  1438. lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_ENABLE_);
  1439. done:
  1440. mutex_unlock(&ptp->command_lock);
  1441. }
  1442. static void lan743x_ptp_reset(struct lan743x_adapter *adapter)
  1443. {
  1444. struct lan743x_ptp *ptp = &adapter->ptp;
  1445. mutex_lock(&ptp->command_lock);
  1446. if (lan743x_ptp_is_enabled(adapter)) {
  1447. netif_err(adapter, drv, adapter->netdev,
  1448. "Attempting reset while enabled\n");
  1449. goto done;
  1450. }
  1451. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_RESET_);
  1452. lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_RESET_);
  1453. done:
  1454. mutex_unlock(&ptp->command_lock);
  1455. }
  1456. static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
  1457. u32 seconds, u32 nano_seconds,
  1458. u32 sub_nano_seconds)
  1459. {
  1460. struct lan743x_ptp *ptp = &adapter->ptp;
  1461. mutex_lock(&ptp->command_lock);
  1462. lan743x_csr_write(adapter, PTP_CLOCK_SEC, seconds);
  1463. lan743x_csr_write(adapter, PTP_CLOCK_NS, nano_seconds);
  1464. lan743x_csr_write(adapter, PTP_CLOCK_SUBNS, sub_nano_seconds);
  1465. lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
  1466. lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
  1467. mutex_unlock(&ptp->command_lock);
  1468. }
  1469. bool lan743x_ptp_request_tx_timestamp(struct lan743x_adapter *adapter)
  1470. {
  1471. struct lan743x_ptp *ptp = &adapter->ptp;
  1472. bool result = false;
  1473. spin_lock(&ptp->tx_ts_lock);
  1474. if (ptp->pending_tx_timestamps < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
  1475. /* request granted */
  1476. ptp->pending_tx_timestamps++;
  1477. result = true;
  1478. }
  1479. spin_unlock(&ptp->tx_ts_lock);
  1480. return result;
  1481. }
  1482. void lan743x_ptp_unrequest_tx_timestamp(struct lan743x_adapter *adapter)
  1483. {
  1484. struct lan743x_ptp *ptp = &adapter->ptp;
  1485. spin_lock_bh(&ptp->tx_ts_lock);
  1486. if (ptp->pending_tx_timestamps > 0)
  1487. ptp->pending_tx_timestamps--;
  1488. else
  1489. netif_err(adapter, drv, adapter->netdev,
  1490. "unrequest failed, pending_tx_timestamps==0\n");
  1491. spin_unlock_bh(&ptp->tx_ts_lock);
  1492. }
  1493. void lan743x_ptp_tx_timestamp_skb(struct lan743x_adapter *adapter,
  1494. struct sk_buff *skb, bool ignore_sync)
  1495. {
  1496. lan743x_ptp_tx_ts_enqueue_skb(adapter, skb, ignore_sync);
  1497. lan743x_ptp_tx_ts_complete(adapter);
  1498. }
  1499. int lan743x_ptp_hwtstamp_get(struct net_device *netdev,
  1500. struct kernel_hwtstamp_config *config)
  1501. {
  1502. struct lan743x_adapter *adapter = netdev_priv(netdev);
  1503. struct lan743x_tx *tx = &adapter->tx[0];
  1504. if (tx->ts_flags & TX_TS_FLAG_ONE_STEP_SYNC)
  1505. config->tx_type = HWTSTAMP_TX_ONESTEP_SYNC;
  1506. else if (tx->ts_flags & TX_TS_FLAG_TIMESTAMPING_ENABLED)
  1507. config->tx_type = HWTSTAMP_TX_ON;
  1508. else
  1509. config->tx_type = HWTSTAMP_TX_OFF;
  1510. config->rx_filter = adapter->rx_tstamp_filter;
  1511. return 0;
  1512. }
  1513. int lan743x_ptp_hwtstamp_set(struct net_device *netdev,
  1514. struct kernel_hwtstamp_config *config,
  1515. struct netlink_ext_ack *extack)
  1516. {
  1517. struct lan743x_adapter *adapter = netdev_priv(netdev);
  1518. int index;
  1519. switch (config->tx_type) {
  1520. case HWTSTAMP_TX_OFF:
  1521. for (index = 0; index < adapter->used_tx_channels;
  1522. index++)
  1523. lan743x_tx_set_timestamping_mode(&adapter->tx[index],
  1524. false, false);
  1525. lan743x_ptp_set_sync_ts_insert(adapter, false);
  1526. break;
  1527. case HWTSTAMP_TX_ON:
  1528. for (index = 0; index < adapter->used_tx_channels;
  1529. index++)
  1530. lan743x_tx_set_timestamping_mode(&adapter->tx[index],
  1531. true, false);
  1532. lan743x_ptp_set_sync_ts_insert(adapter, false);
  1533. break;
  1534. case HWTSTAMP_TX_ONESTEP_SYNC:
  1535. for (index = 0; index < adapter->used_tx_channels;
  1536. index++)
  1537. lan743x_tx_set_timestamping_mode(&adapter->tx[index],
  1538. true, true);
  1539. lan743x_ptp_set_sync_ts_insert(adapter, true);
  1540. break;
  1541. case HWTSTAMP_TX_ONESTEP_P2P:
  1542. return -ERANGE;
  1543. default:
  1544. netif_warn(adapter, drv, adapter->netdev,
  1545. " tx_type = %d, UNKNOWN\n", config->tx_type);
  1546. return -EINVAL;
  1547. }
  1548. return lan743x_rx_set_tstamp_mode(adapter, config->rx_filter);
  1549. }