lan743x_ptp.c 51 KB

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