lan743x_ptp.c 50 KB

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