ice_dpll.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2022, Intel Corporation. */
  3. #include "ice.h"
  4. #include "ice_lib.h"
  5. #include "ice_trace.h"
  6. #include <linux/dpll.h>
  7. #define ICE_CGU_STATE_ACQ_ERR_THRESHOLD 50
  8. #define ICE_DPLL_PIN_IDX_INVALID 0xff
  9. #define ICE_DPLL_RCLK_NUM_PER_PF 1
  10. #define ICE_DPLL_PIN_ESYNC_PULSE_HIGH_PERCENT 25
  11. #define ICE_DPLL_PIN_GEN_RCLK_FREQ 1953125
  12. /**
  13. * enum ice_dpll_pin_type - enumerate ice pin types:
  14. * @ICE_DPLL_PIN_INVALID: invalid pin type
  15. * @ICE_DPLL_PIN_TYPE_INPUT: input pin
  16. * @ICE_DPLL_PIN_TYPE_OUTPUT: output pin
  17. * @ICE_DPLL_PIN_TYPE_RCLK_INPUT: recovery clock input pin
  18. */
  19. enum ice_dpll_pin_type {
  20. ICE_DPLL_PIN_INVALID,
  21. ICE_DPLL_PIN_TYPE_INPUT,
  22. ICE_DPLL_PIN_TYPE_OUTPUT,
  23. ICE_DPLL_PIN_TYPE_RCLK_INPUT,
  24. };
  25. static const char * const pin_type_name[] = {
  26. [ICE_DPLL_PIN_TYPE_INPUT] = "input",
  27. [ICE_DPLL_PIN_TYPE_OUTPUT] = "output",
  28. [ICE_DPLL_PIN_TYPE_RCLK_INPUT] = "rclk-input",
  29. };
  30. static const struct dpll_pin_frequency ice_esync_range[] = {
  31. DPLL_PIN_FREQUENCY_RANGE(0, DPLL_PIN_FREQUENCY_1_HZ),
  32. };
  33. /**
  34. * ice_dpll_is_reset - check if reset is in progress
  35. * @pf: private board structure
  36. * @extack: error reporting
  37. *
  38. * If reset is in progress, fill extack with error.
  39. *
  40. * Return:
  41. * * false - no reset in progress
  42. * * true - reset in progress
  43. */
  44. static bool ice_dpll_is_reset(struct ice_pf *pf, struct netlink_ext_ack *extack)
  45. {
  46. if (ice_is_reset_in_progress(pf->state)) {
  47. NL_SET_ERR_MSG(extack, "PF reset in progress");
  48. return true;
  49. }
  50. return false;
  51. }
  52. /**
  53. * ice_dpll_pin_freq_set - set pin's frequency
  54. * @pf: private board structure
  55. * @pin: pointer to a pin
  56. * @pin_type: type of pin being configured
  57. * @freq: frequency to be set
  58. * @extack: error reporting
  59. *
  60. * Set requested frequency on a pin.
  61. *
  62. * Context: Called under pf->dplls.lock
  63. * Return:
  64. * * 0 - success
  65. * * negative - error on AQ or wrong pin type given
  66. */
  67. static int
  68. ice_dpll_pin_freq_set(struct ice_pf *pf, struct ice_dpll_pin *pin,
  69. enum ice_dpll_pin_type pin_type, const u32 freq,
  70. struct netlink_ext_ack *extack)
  71. {
  72. u8 flags;
  73. int ret;
  74. switch (pin_type) {
  75. case ICE_DPLL_PIN_TYPE_INPUT:
  76. flags = ICE_AQC_SET_CGU_IN_CFG_FLG1_UPDATE_FREQ;
  77. ret = ice_aq_set_input_pin_cfg(&pf->hw, pin->idx, flags,
  78. pin->flags[0], freq, 0);
  79. break;
  80. case ICE_DPLL_PIN_TYPE_OUTPUT:
  81. flags = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_FREQ;
  82. ret = ice_aq_set_output_pin_cfg(&pf->hw, pin->idx, flags,
  83. 0, freq, 0);
  84. break;
  85. default:
  86. return -EINVAL;
  87. }
  88. if (ret) {
  89. NL_SET_ERR_MSG_FMT(extack,
  90. "err:%d %s failed to set pin freq:%u on pin:%u\n",
  91. ret,
  92. ice_aq_str(pf->hw.adminq.sq_last_status),
  93. freq, pin->idx);
  94. return ret;
  95. }
  96. pin->freq = freq;
  97. return 0;
  98. }
  99. /**
  100. * ice_dpll_frequency_set - wrapper for pin callback for set frequency
  101. * @pin: pointer to a pin
  102. * @pin_priv: private data pointer passed on pin registration
  103. * @dpll: pointer to dpll
  104. * @dpll_priv: private data pointer passed on dpll registration
  105. * @frequency: frequency to be set
  106. * @extack: error reporting
  107. * @pin_type: type of pin being configured
  108. *
  109. * Wraps internal set frequency command on a pin.
  110. *
  111. * Context: Acquires pf->dplls.lock
  112. * Return:
  113. * * 0 - success
  114. * * negative - error pin not found or couldn't set in hw
  115. */
  116. static int
  117. ice_dpll_frequency_set(const struct dpll_pin *pin, void *pin_priv,
  118. const struct dpll_device *dpll, void *dpll_priv,
  119. const u32 frequency,
  120. struct netlink_ext_ack *extack,
  121. enum ice_dpll_pin_type pin_type)
  122. {
  123. struct ice_dpll_pin *p = pin_priv;
  124. struct ice_dpll *d = dpll_priv;
  125. struct ice_pf *pf = d->pf;
  126. int ret;
  127. if (ice_dpll_is_reset(pf, extack))
  128. return -EBUSY;
  129. mutex_lock(&pf->dplls.lock);
  130. ret = ice_dpll_pin_freq_set(pf, p, pin_type, frequency, extack);
  131. mutex_unlock(&pf->dplls.lock);
  132. return ret;
  133. }
  134. /**
  135. * ice_dpll_input_frequency_set - input pin callback for set frequency
  136. * @pin: pointer to a pin
  137. * @pin_priv: private data pointer passed on pin registration
  138. * @dpll: pointer to dpll
  139. * @dpll_priv: private data pointer passed on dpll registration
  140. * @frequency: frequency to be set
  141. * @extack: error reporting
  142. *
  143. * Wraps internal set frequency command on a pin.
  144. *
  145. * Context: Calls a function which acquires pf->dplls.lock
  146. * Return:
  147. * * 0 - success
  148. * * negative - error pin not found or couldn't set in hw
  149. */
  150. static int
  151. ice_dpll_input_frequency_set(const struct dpll_pin *pin, void *pin_priv,
  152. const struct dpll_device *dpll, void *dpll_priv,
  153. u64 frequency, struct netlink_ext_ack *extack)
  154. {
  155. return ice_dpll_frequency_set(pin, pin_priv, dpll, dpll_priv, frequency,
  156. extack, ICE_DPLL_PIN_TYPE_INPUT);
  157. }
  158. /**
  159. * ice_dpll_output_frequency_set - output pin callback for set frequency
  160. * @pin: pointer to a pin
  161. * @pin_priv: private data pointer passed on pin registration
  162. * @dpll: pointer to dpll
  163. * @dpll_priv: private data pointer passed on dpll registration
  164. * @frequency: frequency to be set
  165. * @extack: error reporting
  166. *
  167. * Wraps internal set frequency command on a pin.
  168. *
  169. * Context: Calls a function which acquires pf->dplls.lock
  170. * Return:
  171. * * 0 - success
  172. * * negative - error pin not found or couldn't set in hw
  173. */
  174. static int
  175. ice_dpll_output_frequency_set(const struct dpll_pin *pin, void *pin_priv,
  176. const struct dpll_device *dpll, void *dpll_priv,
  177. u64 frequency, struct netlink_ext_ack *extack)
  178. {
  179. return ice_dpll_frequency_set(pin, pin_priv, dpll, dpll_priv, frequency,
  180. extack, ICE_DPLL_PIN_TYPE_OUTPUT);
  181. }
  182. /**
  183. * ice_dpll_frequency_get - wrapper for pin callback for get frequency
  184. * @pin: pointer to a pin
  185. * @pin_priv: private data pointer passed on pin registration
  186. * @dpll: pointer to dpll
  187. * @dpll_priv: private data pointer passed on dpll registration
  188. * @frequency: on success holds pin's frequency
  189. * @extack: error reporting
  190. * @pin_type: type of pin being configured
  191. *
  192. * Wraps internal get frequency command of a pin.
  193. *
  194. * Context: Acquires pf->dplls.lock
  195. * Return:
  196. * * 0 - success
  197. * * negative - error pin not found or couldn't get from hw
  198. */
  199. static int
  200. ice_dpll_frequency_get(const struct dpll_pin *pin, void *pin_priv,
  201. const struct dpll_device *dpll, void *dpll_priv,
  202. u64 *frequency, struct netlink_ext_ack *extack,
  203. enum ice_dpll_pin_type pin_type)
  204. {
  205. struct ice_dpll_pin *p = pin_priv;
  206. struct ice_dpll *d = dpll_priv;
  207. struct ice_pf *pf = d->pf;
  208. mutex_lock(&pf->dplls.lock);
  209. *frequency = p->freq;
  210. mutex_unlock(&pf->dplls.lock);
  211. return 0;
  212. }
  213. /**
  214. * ice_dpll_input_frequency_get - input pin callback for get frequency
  215. * @pin: pointer to a pin
  216. * @pin_priv: private data pointer passed on pin registration
  217. * @dpll: pointer to dpll
  218. * @dpll_priv: private data pointer passed on dpll registration
  219. * @frequency: on success holds pin's frequency
  220. * @extack: error reporting
  221. *
  222. * Wraps internal get frequency command of a input pin.
  223. *
  224. * Context: Calls a function which acquires pf->dplls.lock
  225. * Return:
  226. * * 0 - success
  227. * * negative - error pin not found or couldn't get from hw
  228. */
  229. static int
  230. ice_dpll_input_frequency_get(const struct dpll_pin *pin, void *pin_priv,
  231. const struct dpll_device *dpll, void *dpll_priv,
  232. u64 *frequency, struct netlink_ext_ack *extack)
  233. {
  234. return ice_dpll_frequency_get(pin, pin_priv, dpll, dpll_priv, frequency,
  235. extack, ICE_DPLL_PIN_TYPE_INPUT);
  236. }
  237. /**
  238. * ice_dpll_output_frequency_get - output pin callback for get frequency
  239. * @pin: pointer to a pin
  240. * @pin_priv: private data pointer passed on pin registration
  241. * @dpll: pointer to dpll
  242. * @dpll_priv: private data pointer passed on dpll registration
  243. * @frequency: on success holds pin's frequency
  244. * @extack: error reporting
  245. *
  246. * Wraps internal get frequency command of a pin.
  247. *
  248. * Context: Calls a function which acquires pf->dplls.lock
  249. * Return:
  250. * * 0 - success
  251. * * negative - error pin not found or couldn't get from hw
  252. */
  253. static int
  254. ice_dpll_output_frequency_get(const struct dpll_pin *pin, void *pin_priv,
  255. const struct dpll_device *dpll, void *dpll_priv,
  256. u64 *frequency, struct netlink_ext_ack *extack)
  257. {
  258. return ice_dpll_frequency_get(pin, pin_priv, dpll, dpll_priv, frequency,
  259. extack, ICE_DPLL_PIN_TYPE_OUTPUT);
  260. }
  261. /**
  262. * ice_dpll_pin_enable - enable a pin on dplls
  263. * @hw: board private hw structure
  264. * @pin: pointer to a pin
  265. * @dpll_idx: dpll index to connect to output pin
  266. * @pin_type: type of pin being enabled
  267. * @extack: error reporting
  268. *
  269. * Enable a pin on both dplls. Store current state in pin->flags.
  270. *
  271. * Context: Called under pf->dplls.lock
  272. * Return:
  273. * * 0 - OK
  274. * * negative - error
  275. */
  276. static int
  277. ice_dpll_pin_enable(struct ice_hw *hw, struct ice_dpll_pin *pin,
  278. u8 dpll_idx, enum ice_dpll_pin_type pin_type,
  279. struct netlink_ext_ack *extack)
  280. {
  281. u8 flags = 0;
  282. int ret;
  283. switch (pin_type) {
  284. case ICE_DPLL_PIN_TYPE_INPUT:
  285. if (pin->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
  286. flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
  287. flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
  288. ret = ice_aq_set_input_pin_cfg(hw, pin->idx, 0, flags, 0, 0);
  289. break;
  290. case ICE_DPLL_PIN_TYPE_OUTPUT:
  291. flags = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_SRC_SEL;
  292. if (pin->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
  293. flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
  294. flags |= ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
  295. ret = ice_aq_set_output_pin_cfg(hw, pin->idx, flags, dpll_idx,
  296. 0, 0);
  297. break;
  298. default:
  299. return -EINVAL;
  300. }
  301. if (ret)
  302. NL_SET_ERR_MSG_FMT(extack,
  303. "err:%d %s failed to enable %s pin:%u\n",
  304. ret, ice_aq_str(hw->adminq.sq_last_status),
  305. pin_type_name[pin_type], pin->idx);
  306. return ret;
  307. }
  308. /**
  309. * ice_dpll_pin_disable - disable a pin on dplls
  310. * @hw: board private hw structure
  311. * @pin: pointer to a pin
  312. * @pin_type: type of pin being disabled
  313. * @extack: error reporting
  314. *
  315. * Disable a pin on both dplls. Store current state in pin->flags.
  316. *
  317. * Context: Called under pf->dplls.lock
  318. * Return:
  319. * * 0 - OK
  320. * * negative - error
  321. */
  322. static int
  323. ice_dpll_pin_disable(struct ice_hw *hw, struct ice_dpll_pin *pin,
  324. enum ice_dpll_pin_type pin_type,
  325. struct netlink_ext_ack *extack)
  326. {
  327. u8 flags = 0;
  328. int ret;
  329. switch (pin_type) {
  330. case ICE_DPLL_PIN_TYPE_INPUT:
  331. if (pin->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
  332. flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
  333. ret = ice_aq_set_input_pin_cfg(hw, pin->idx, 0, flags, 0, 0);
  334. break;
  335. case ICE_DPLL_PIN_TYPE_OUTPUT:
  336. if (pin->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
  337. flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
  338. ret = ice_aq_set_output_pin_cfg(hw, pin->idx, flags, 0, 0, 0);
  339. break;
  340. default:
  341. return -EINVAL;
  342. }
  343. if (ret)
  344. NL_SET_ERR_MSG_FMT(extack,
  345. "err:%d %s failed to disable %s pin:%u\n",
  346. ret, ice_aq_str(hw->adminq.sq_last_status),
  347. pin_type_name[pin_type], pin->idx);
  348. return ret;
  349. }
  350. /**
  351. * ice_dpll_pin_state_update - update pin's state
  352. * @pf: private board struct
  353. * @pin: structure with pin attributes to be updated
  354. * @pin_type: type of pin being updated
  355. * @extack: error reporting
  356. *
  357. * Determine pin current state and frequency, then update struct
  358. * holding the pin info. For input pin states are separated for each
  359. * dpll, for rclk pins states are separated for each parent.
  360. *
  361. * Context: Called under pf->dplls.lock
  362. * Return:
  363. * * 0 - OK
  364. * * negative - error
  365. */
  366. static int
  367. ice_dpll_pin_state_update(struct ice_pf *pf, struct ice_dpll_pin *pin,
  368. enum ice_dpll_pin_type pin_type,
  369. struct netlink_ext_ack *extack)
  370. {
  371. u8 parent, port_num = ICE_AQC_SET_PHY_REC_CLK_OUT_CURR_PORT;
  372. int ret;
  373. switch (pin_type) {
  374. case ICE_DPLL_PIN_TYPE_INPUT:
  375. ret = ice_aq_get_input_pin_cfg(&pf->hw, pin->idx, &pin->status,
  376. NULL, NULL, &pin->flags[0],
  377. &pin->freq, &pin->phase_adjust);
  378. if (ret)
  379. goto err;
  380. if (ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN & pin->flags[0]) {
  381. if (pin->pin) {
  382. pin->state[pf->dplls.eec.dpll_idx] =
  383. pin->pin == pf->dplls.eec.active_input ?
  384. DPLL_PIN_STATE_CONNECTED :
  385. DPLL_PIN_STATE_SELECTABLE;
  386. pin->state[pf->dplls.pps.dpll_idx] =
  387. pin->pin == pf->dplls.pps.active_input ?
  388. DPLL_PIN_STATE_CONNECTED :
  389. DPLL_PIN_STATE_SELECTABLE;
  390. } else {
  391. pin->state[pf->dplls.eec.dpll_idx] =
  392. DPLL_PIN_STATE_SELECTABLE;
  393. pin->state[pf->dplls.pps.dpll_idx] =
  394. DPLL_PIN_STATE_SELECTABLE;
  395. }
  396. } else {
  397. pin->state[pf->dplls.eec.dpll_idx] =
  398. DPLL_PIN_STATE_DISCONNECTED;
  399. pin->state[pf->dplls.pps.dpll_idx] =
  400. DPLL_PIN_STATE_DISCONNECTED;
  401. }
  402. break;
  403. case ICE_DPLL_PIN_TYPE_OUTPUT:
  404. ret = ice_aq_get_output_pin_cfg(&pf->hw, pin->idx,
  405. &pin->flags[0], &parent,
  406. &pin->freq, NULL);
  407. if (ret)
  408. goto err;
  409. parent &= ICE_AQC_GET_CGU_OUT_CFG_DPLL_SRC_SEL;
  410. if (ICE_AQC_GET_CGU_OUT_CFG_OUT_EN & pin->flags[0]) {
  411. pin->state[pf->dplls.eec.dpll_idx] =
  412. parent == pf->dplls.eec.dpll_idx ?
  413. DPLL_PIN_STATE_CONNECTED :
  414. DPLL_PIN_STATE_DISCONNECTED;
  415. pin->state[pf->dplls.pps.dpll_idx] =
  416. parent == pf->dplls.pps.dpll_idx ?
  417. DPLL_PIN_STATE_CONNECTED :
  418. DPLL_PIN_STATE_DISCONNECTED;
  419. } else {
  420. pin->state[pf->dplls.eec.dpll_idx] =
  421. DPLL_PIN_STATE_DISCONNECTED;
  422. pin->state[pf->dplls.pps.dpll_idx] =
  423. DPLL_PIN_STATE_DISCONNECTED;
  424. }
  425. break;
  426. case ICE_DPLL_PIN_TYPE_RCLK_INPUT:
  427. for (parent = 0; parent < pf->dplls.rclk.num_parents;
  428. parent++) {
  429. u8 p = parent;
  430. ret = ice_aq_get_phy_rec_clk_out(&pf->hw, &p,
  431. &port_num,
  432. &pin->flags[parent],
  433. NULL);
  434. if (ret)
  435. goto err;
  436. if (ICE_AQC_GET_PHY_REC_CLK_OUT_OUT_EN &
  437. pin->flags[parent])
  438. pin->state[parent] = DPLL_PIN_STATE_CONNECTED;
  439. else
  440. pin->state[parent] =
  441. DPLL_PIN_STATE_DISCONNECTED;
  442. }
  443. break;
  444. default:
  445. return -EINVAL;
  446. }
  447. return 0;
  448. err:
  449. if (extack)
  450. NL_SET_ERR_MSG_FMT(extack,
  451. "err:%d %s failed to update %s pin:%u\n",
  452. ret,
  453. ice_aq_str(pf->hw.adminq.sq_last_status),
  454. pin_type_name[pin_type], pin->idx);
  455. else
  456. dev_err_ratelimited(ice_pf_to_dev(pf),
  457. "err:%d %s failed to update %s pin:%u\n",
  458. ret,
  459. ice_aq_str(pf->hw.adminq.sq_last_status),
  460. pin_type_name[pin_type], pin->idx);
  461. return ret;
  462. }
  463. /**
  464. * ice_dpll_hw_input_prio_set - set input priority value in hardware
  465. * @pf: board private structure
  466. * @dpll: ice dpll pointer
  467. * @pin: ice pin pointer
  468. * @prio: priority value being set on a dpll
  469. * @extack: error reporting
  470. *
  471. * Internal wrapper for setting the priority in the hardware.
  472. *
  473. * Context: Called under pf->dplls.lock
  474. * Return:
  475. * * 0 - success
  476. * * negative - failure
  477. */
  478. static int
  479. ice_dpll_hw_input_prio_set(struct ice_pf *pf, struct ice_dpll *dpll,
  480. struct ice_dpll_pin *pin, const u32 prio,
  481. struct netlink_ext_ack *extack)
  482. {
  483. int ret;
  484. ret = ice_aq_set_cgu_ref_prio(&pf->hw, dpll->dpll_idx, pin->idx,
  485. (u8)prio);
  486. if (ret)
  487. NL_SET_ERR_MSG_FMT(extack,
  488. "err:%d %s failed to set pin prio:%u on pin:%u\n",
  489. ret,
  490. ice_aq_str(pf->hw.adminq.sq_last_status),
  491. prio, pin->idx);
  492. else
  493. dpll->input_prio[pin->idx] = prio;
  494. return ret;
  495. }
  496. /**
  497. * ice_dpll_lock_status_get - get dpll lock status callback
  498. * @dpll: registered dpll pointer
  499. * @dpll_priv: private data pointer passed on dpll registration
  500. * @status: on success holds dpll's lock status
  501. * @status_error: status error value
  502. * @extack: error reporting
  503. *
  504. * Dpll subsystem callback, provides dpll's lock status.
  505. *
  506. * Context: Acquires pf->dplls.lock
  507. * Return:
  508. * * 0 - success
  509. * * negative - failure
  510. */
  511. static int
  512. ice_dpll_lock_status_get(const struct dpll_device *dpll, void *dpll_priv,
  513. enum dpll_lock_status *status,
  514. enum dpll_lock_status_error *status_error,
  515. struct netlink_ext_ack *extack)
  516. {
  517. struct ice_dpll *d = dpll_priv;
  518. struct ice_pf *pf = d->pf;
  519. mutex_lock(&pf->dplls.lock);
  520. *status = d->dpll_state;
  521. mutex_unlock(&pf->dplls.lock);
  522. return 0;
  523. }
  524. /**
  525. * ice_dpll_mode_get - get dpll's working mode
  526. * @dpll: registered dpll pointer
  527. * @dpll_priv: private data pointer passed on dpll registration
  528. * @mode: on success holds current working mode of dpll
  529. * @extack: error reporting
  530. *
  531. * Dpll subsystem callback. Provides working mode of dpll.
  532. *
  533. * Context: Acquires pf->dplls.lock
  534. * Return:
  535. * * 0 - success
  536. * * negative - failure
  537. */
  538. static int ice_dpll_mode_get(const struct dpll_device *dpll, void *dpll_priv,
  539. enum dpll_mode *mode,
  540. struct netlink_ext_ack *extack)
  541. {
  542. struct ice_dpll *d = dpll_priv;
  543. struct ice_pf *pf = d->pf;
  544. mutex_lock(&pf->dplls.lock);
  545. *mode = d->mode;
  546. mutex_unlock(&pf->dplls.lock);
  547. return 0;
  548. }
  549. /**
  550. * ice_dpll_pin_state_set - set pin's state on dpll
  551. * @pin: pointer to a pin
  552. * @pin_priv: private data pointer passed on pin registration
  553. * @dpll: registered dpll pointer
  554. * @dpll_priv: private data pointer passed on dpll registration
  555. * @enable: if pin shalll be enabled
  556. * @extack: error reporting
  557. * @pin_type: type of a pin
  558. *
  559. * Set pin state on a pin.
  560. *
  561. * Context: Acquires pf->dplls.lock
  562. * Return:
  563. * * 0 - OK or no change required
  564. * * negative - error
  565. */
  566. static int
  567. ice_dpll_pin_state_set(const struct dpll_pin *pin, void *pin_priv,
  568. const struct dpll_device *dpll, void *dpll_priv,
  569. bool enable, struct netlink_ext_ack *extack,
  570. enum ice_dpll_pin_type pin_type)
  571. {
  572. struct ice_dpll_pin *p = pin_priv;
  573. struct ice_dpll *d = dpll_priv;
  574. struct ice_pf *pf = d->pf;
  575. int ret;
  576. if (ice_dpll_is_reset(pf, extack))
  577. return -EBUSY;
  578. mutex_lock(&pf->dplls.lock);
  579. if (enable)
  580. ret = ice_dpll_pin_enable(&pf->hw, p, d->dpll_idx, pin_type,
  581. extack);
  582. else
  583. ret = ice_dpll_pin_disable(&pf->hw, p, pin_type, extack);
  584. if (!ret)
  585. ret = ice_dpll_pin_state_update(pf, p, pin_type, extack);
  586. mutex_unlock(&pf->dplls.lock);
  587. return ret;
  588. }
  589. /**
  590. * ice_dpll_output_state_set - enable/disable output pin on dpll device
  591. * @pin: pointer to a pin
  592. * @pin_priv: private data pointer passed on pin registration
  593. * @dpll: dpll being configured
  594. * @dpll_priv: private data pointer passed on dpll registration
  595. * @state: state of pin to be set
  596. * @extack: error reporting
  597. *
  598. * Dpll subsystem callback. Set given state on output type pin.
  599. *
  600. * Context: Calls a function which acquires pf->dplls.lock
  601. * Return:
  602. * * 0 - successfully enabled mode
  603. * * negative - failed to enable mode
  604. */
  605. static int
  606. ice_dpll_output_state_set(const struct dpll_pin *pin, void *pin_priv,
  607. const struct dpll_device *dpll, void *dpll_priv,
  608. enum dpll_pin_state state,
  609. struct netlink_ext_ack *extack)
  610. {
  611. bool enable = state == DPLL_PIN_STATE_CONNECTED;
  612. struct ice_dpll_pin *p = pin_priv;
  613. struct ice_dpll *d = dpll_priv;
  614. if (state == DPLL_PIN_STATE_SELECTABLE)
  615. return -EINVAL;
  616. if (!enable && p->state[d->dpll_idx] == DPLL_PIN_STATE_DISCONNECTED)
  617. return 0;
  618. return ice_dpll_pin_state_set(pin, pin_priv, dpll, dpll_priv, enable,
  619. extack, ICE_DPLL_PIN_TYPE_OUTPUT);
  620. }
  621. /**
  622. * ice_dpll_input_state_set - enable/disable input pin on dpll levice
  623. * @pin: pointer to a pin
  624. * @pin_priv: private data pointer passed on pin registration
  625. * @dpll: dpll being configured
  626. * @dpll_priv: private data pointer passed on dpll registration
  627. * @state: state of pin to be set
  628. * @extack: error reporting
  629. *
  630. * Dpll subsystem callback. Enables given mode on input type pin.
  631. *
  632. * Context: Calls a function which acquires pf->dplls.lock
  633. * Return:
  634. * * 0 - successfully enabled mode
  635. * * negative - failed to enable mode
  636. */
  637. static int
  638. ice_dpll_input_state_set(const struct dpll_pin *pin, void *pin_priv,
  639. const struct dpll_device *dpll, void *dpll_priv,
  640. enum dpll_pin_state state,
  641. struct netlink_ext_ack *extack)
  642. {
  643. bool enable = state == DPLL_PIN_STATE_SELECTABLE;
  644. return ice_dpll_pin_state_set(pin, pin_priv, dpll, dpll_priv, enable,
  645. extack, ICE_DPLL_PIN_TYPE_INPUT);
  646. }
  647. /**
  648. * ice_dpll_pin_state_get - set pin's state on dpll
  649. * @pin: pointer to a pin
  650. * @pin_priv: private data pointer passed on pin registration
  651. * @dpll: registered dpll pointer
  652. * @dpll_priv: private data pointer passed on dpll registration
  653. * @state: on success holds state of the pin
  654. * @extack: error reporting
  655. * @pin_type: type of questioned pin
  656. *
  657. * Determine pin state set it on a pin.
  658. *
  659. * Context: Acquires pf->dplls.lock
  660. * Return:
  661. * * 0 - success
  662. * * negative - failed to get state
  663. */
  664. static int
  665. ice_dpll_pin_state_get(const struct dpll_pin *pin, void *pin_priv,
  666. const struct dpll_device *dpll, void *dpll_priv,
  667. enum dpll_pin_state *state,
  668. struct netlink_ext_ack *extack,
  669. enum ice_dpll_pin_type pin_type)
  670. {
  671. struct ice_dpll_pin *p = pin_priv;
  672. struct ice_dpll *d = dpll_priv;
  673. struct ice_pf *pf = d->pf;
  674. int ret;
  675. if (ice_dpll_is_reset(pf, extack))
  676. return -EBUSY;
  677. mutex_lock(&pf->dplls.lock);
  678. ret = ice_dpll_pin_state_update(pf, p, pin_type, extack);
  679. if (ret)
  680. goto unlock;
  681. if (pin_type == ICE_DPLL_PIN_TYPE_INPUT ||
  682. pin_type == ICE_DPLL_PIN_TYPE_OUTPUT)
  683. *state = p->state[d->dpll_idx];
  684. ret = 0;
  685. unlock:
  686. mutex_unlock(&pf->dplls.lock);
  687. return ret;
  688. }
  689. /**
  690. * ice_dpll_output_state_get - get output pin state on dpll device
  691. * @pin: pointer to a pin
  692. * @pin_priv: private data pointer passed on pin registration
  693. * @dpll: registered dpll pointer
  694. * @dpll_priv: private data pointer passed on dpll registration
  695. * @state: on success holds state of the pin
  696. * @extack: error reporting
  697. *
  698. * Dpll subsystem callback. Check state of a pin.
  699. *
  700. * Context: Calls a function which acquires pf->dplls.lock
  701. * Return:
  702. * * 0 - success
  703. * * negative - failed to get state
  704. */
  705. static int
  706. ice_dpll_output_state_get(const struct dpll_pin *pin, void *pin_priv,
  707. const struct dpll_device *dpll, void *dpll_priv,
  708. enum dpll_pin_state *state,
  709. struct netlink_ext_ack *extack)
  710. {
  711. return ice_dpll_pin_state_get(pin, pin_priv, dpll, dpll_priv, state,
  712. extack, ICE_DPLL_PIN_TYPE_OUTPUT);
  713. }
  714. /**
  715. * ice_dpll_input_state_get - get input pin state on dpll device
  716. * @pin: pointer to a pin
  717. * @pin_priv: private data pointer passed on pin registration
  718. * @dpll: registered dpll pointer
  719. * @dpll_priv: private data pointer passed on dpll registration
  720. * @state: on success holds state of the pin
  721. * @extack: error reporting
  722. *
  723. * Dpll subsystem callback. Check state of a input pin.
  724. *
  725. * Context: Calls a function which acquires pf->dplls.lock
  726. * Return:
  727. * * 0 - success
  728. * * negative - failed to get state
  729. */
  730. static int
  731. ice_dpll_input_state_get(const struct dpll_pin *pin, void *pin_priv,
  732. const struct dpll_device *dpll, void *dpll_priv,
  733. enum dpll_pin_state *state,
  734. struct netlink_ext_ack *extack)
  735. {
  736. return ice_dpll_pin_state_get(pin, pin_priv, dpll, dpll_priv, state,
  737. extack, ICE_DPLL_PIN_TYPE_INPUT);
  738. }
  739. /**
  740. * ice_dpll_input_prio_get - get dpll's input prio
  741. * @pin: pointer to a pin
  742. * @pin_priv: private data pointer passed on pin registration
  743. * @dpll: registered dpll pointer
  744. * @dpll_priv: private data pointer passed on dpll registration
  745. * @prio: on success - returns input priority on dpll
  746. * @extack: error reporting
  747. *
  748. * Dpll subsystem callback. Handler for getting priority of a input pin.
  749. *
  750. * Context: Acquires pf->dplls.lock
  751. * Return:
  752. * * 0 - success
  753. * * negative - failure
  754. */
  755. static int
  756. ice_dpll_input_prio_get(const struct dpll_pin *pin, void *pin_priv,
  757. const struct dpll_device *dpll, void *dpll_priv,
  758. u32 *prio, struct netlink_ext_ack *extack)
  759. {
  760. struct ice_dpll_pin *p = pin_priv;
  761. struct ice_dpll *d = dpll_priv;
  762. struct ice_pf *pf = d->pf;
  763. mutex_lock(&pf->dplls.lock);
  764. *prio = d->input_prio[p->idx];
  765. mutex_unlock(&pf->dplls.lock);
  766. return 0;
  767. }
  768. /**
  769. * ice_dpll_input_prio_set - set dpll input prio
  770. * @pin: pointer to a pin
  771. * @pin_priv: private data pointer passed on pin registration
  772. * @dpll: registered dpll pointer
  773. * @dpll_priv: private data pointer passed on dpll registration
  774. * @prio: input priority to be set on dpll
  775. * @extack: error reporting
  776. *
  777. * Dpll subsystem callback. Handler for setting priority of a input pin.
  778. *
  779. * Context: Acquires pf->dplls.lock
  780. * Return:
  781. * * 0 - success
  782. * * negative - failure
  783. */
  784. static int
  785. ice_dpll_input_prio_set(const struct dpll_pin *pin, void *pin_priv,
  786. const struct dpll_device *dpll, void *dpll_priv,
  787. u32 prio, struct netlink_ext_ack *extack)
  788. {
  789. struct ice_dpll_pin *p = pin_priv;
  790. struct ice_dpll *d = dpll_priv;
  791. struct ice_pf *pf = d->pf;
  792. int ret;
  793. if (ice_dpll_is_reset(pf, extack))
  794. return -EBUSY;
  795. mutex_lock(&pf->dplls.lock);
  796. ret = ice_dpll_hw_input_prio_set(pf, d, p, prio, extack);
  797. mutex_unlock(&pf->dplls.lock);
  798. return ret;
  799. }
  800. /**
  801. * ice_dpll_input_direction - callback for get input pin direction
  802. * @pin: pointer to a pin
  803. * @pin_priv: private data pointer passed on pin registration
  804. * @dpll: registered dpll pointer
  805. * @dpll_priv: private data pointer passed on dpll registration
  806. * @direction: holds input pin direction
  807. * @extack: error reporting
  808. *
  809. * Dpll subsystem callback. Handler for getting direction of a input pin.
  810. *
  811. * Return:
  812. * * 0 - success
  813. */
  814. static int
  815. ice_dpll_input_direction(const struct dpll_pin *pin, void *pin_priv,
  816. const struct dpll_device *dpll, void *dpll_priv,
  817. enum dpll_pin_direction *direction,
  818. struct netlink_ext_ack *extack)
  819. {
  820. *direction = DPLL_PIN_DIRECTION_INPUT;
  821. return 0;
  822. }
  823. /**
  824. * ice_dpll_output_direction - callback for get output pin direction
  825. * @pin: pointer to a pin
  826. * @pin_priv: private data pointer passed on pin registration
  827. * @dpll: registered dpll pointer
  828. * @dpll_priv: private data pointer passed on dpll registration
  829. * @direction: holds output pin direction
  830. * @extack: error reporting
  831. *
  832. * Dpll subsystem callback. Handler for getting direction of an output pin.
  833. *
  834. * Return:
  835. * * 0 - success
  836. */
  837. static int
  838. ice_dpll_output_direction(const struct dpll_pin *pin, void *pin_priv,
  839. const struct dpll_device *dpll, void *dpll_priv,
  840. enum dpll_pin_direction *direction,
  841. struct netlink_ext_ack *extack)
  842. {
  843. *direction = DPLL_PIN_DIRECTION_OUTPUT;
  844. return 0;
  845. }
  846. /**
  847. * ice_dpll_pin_phase_adjust_get - callback for get pin phase adjust value
  848. * @pin: pointer to a pin
  849. * @pin_priv: private data pointer passed on pin registration
  850. * @dpll: registered dpll pointer
  851. * @dpll_priv: private data pointer passed on dpll registration
  852. * @phase_adjust: on success holds pin phase_adjust value
  853. * @extack: error reporting
  854. *
  855. * Dpll subsystem callback. Handler for getting phase adjust value of a pin.
  856. *
  857. * Context: Acquires pf->dplls.lock
  858. * Return:
  859. * * 0 - success
  860. * * negative - error
  861. */
  862. static int
  863. ice_dpll_pin_phase_adjust_get(const struct dpll_pin *pin, void *pin_priv,
  864. const struct dpll_device *dpll, void *dpll_priv,
  865. s32 *phase_adjust,
  866. struct netlink_ext_ack *extack)
  867. {
  868. struct ice_dpll_pin *p = pin_priv;
  869. struct ice_pf *pf = p->pf;
  870. mutex_lock(&pf->dplls.lock);
  871. *phase_adjust = p->phase_adjust;
  872. mutex_unlock(&pf->dplls.lock);
  873. return 0;
  874. }
  875. /**
  876. * ice_dpll_pin_phase_adjust_set - helper for setting a pin phase adjust value
  877. * @pin: pointer to a pin
  878. * @pin_priv: private data pointer passed on pin registration
  879. * @dpll: registered dpll pointer
  880. * @dpll_priv: private data pointer passed on dpll registration
  881. * @phase_adjust: phase_adjust to be set
  882. * @extack: error reporting
  883. * @type: type of a pin
  884. *
  885. * Helper for dpll subsystem callback. Handler for setting phase adjust value
  886. * of a pin.
  887. *
  888. * Context: Acquires pf->dplls.lock
  889. * Return:
  890. * * 0 - success
  891. * * negative - error
  892. */
  893. static int
  894. ice_dpll_pin_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
  895. const struct dpll_device *dpll, void *dpll_priv,
  896. s32 phase_adjust,
  897. struct netlink_ext_ack *extack,
  898. enum ice_dpll_pin_type type)
  899. {
  900. struct ice_dpll_pin *p = pin_priv;
  901. struct ice_dpll *d = dpll_priv;
  902. struct ice_pf *pf = d->pf;
  903. u8 flag, flags_en = 0;
  904. int ret;
  905. if (ice_dpll_is_reset(pf, extack))
  906. return -EBUSY;
  907. mutex_lock(&pf->dplls.lock);
  908. switch (type) {
  909. case ICE_DPLL_PIN_TYPE_INPUT:
  910. flag = ICE_AQC_SET_CGU_IN_CFG_FLG1_UPDATE_DELAY;
  911. if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
  912. flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
  913. if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN)
  914. flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
  915. ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, flag, flags_en,
  916. 0, phase_adjust);
  917. break;
  918. case ICE_DPLL_PIN_TYPE_OUTPUT:
  919. flag = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_PHASE;
  920. if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_OUT_EN)
  921. flag |= ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
  922. if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
  923. flag |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
  924. ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flag, 0, 0,
  925. phase_adjust);
  926. break;
  927. default:
  928. ret = -EINVAL;
  929. }
  930. if (!ret)
  931. p->phase_adjust = phase_adjust;
  932. mutex_unlock(&pf->dplls.lock);
  933. if (ret)
  934. NL_SET_ERR_MSG_FMT(extack,
  935. "err:%d %s failed to set pin phase_adjust:%d for pin:%u on dpll:%u\n",
  936. ret,
  937. ice_aq_str(pf->hw.adminq.sq_last_status),
  938. phase_adjust, p->idx, d->dpll_idx);
  939. return ret;
  940. }
  941. /**
  942. * ice_dpll_input_phase_adjust_set - callback for set input pin phase adjust
  943. * @pin: pointer to a pin
  944. * @pin_priv: private data pointer passed on pin registration
  945. * @dpll: registered dpll pointer
  946. * @dpll_priv: private data pointer passed on dpll registration
  947. * @phase_adjust: phase_adjust to be set
  948. * @extack: error reporting
  949. *
  950. * Dpll subsystem callback. Wraps a handler for setting phase adjust on input
  951. * pin.
  952. *
  953. * Context: Calls a function which acquires pf->dplls.lock
  954. * Return:
  955. * * 0 - success
  956. * * negative - error
  957. */
  958. static int
  959. ice_dpll_input_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
  960. const struct dpll_device *dpll, void *dpll_priv,
  961. s32 phase_adjust,
  962. struct netlink_ext_ack *extack)
  963. {
  964. return ice_dpll_pin_phase_adjust_set(pin, pin_priv, dpll, dpll_priv,
  965. phase_adjust, extack,
  966. ICE_DPLL_PIN_TYPE_INPUT);
  967. }
  968. /**
  969. * ice_dpll_output_phase_adjust_set - callback for set output pin phase adjust
  970. * @pin: pointer to a pin
  971. * @pin_priv: private data pointer passed on pin registration
  972. * @dpll: registered dpll pointer
  973. * @dpll_priv: private data pointer passed on dpll registration
  974. * @phase_adjust: phase_adjust to be set
  975. * @extack: error reporting
  976. *
  977. * Dpll subsystem callback. Wraps a handler for setting phase adjust on output
  978. * pin.
  979. *
  980. * Context: Calls a function which acquires pf->dplls.lock
  981. * Return:
  982. * * 0 - success
  983. * * negative - error
  984. */
  985. static int
  986. ice_dpll_output_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
  987. const struct dpll_device *dpll, void *dpll_priv,
  988. s32 phase_adjust,
  989. struct netlink_ext_ack *extack)
  990. {
  991. return ice_dpll_pin_phase_adjust_set(pin, pin_priv, dpll, dpll_priv,
  992. phase_adjust, extack,
  993. ICE_DPLL_PIN_TYPE_OUTPUT);
  994. }
  995. #define ICE_DPLL_PHASE_OFFSET_DIVIDER 100
  996. #define ICE_DPLL_PHASE_OFFSET_FACTOR \
  997. (DPLL_PHASE_OFFSET_DIVIDER / ICE_DPLL_PHASE_OFFSET_DIVIDER)
  998. /**
  999. * ice_dpll_phase_offset_get - callback for get dpll phase shift value
  1000. * @pin: pointer to a pin
  1001. * @pin_priv: private data pointer passed on pin registration
  1002. * @dpll: registered dpll pointer
  1003. * @dpll_priv: private data pointer passed on dpll registration
  1004. * @phase_offset: on success holds pin phase_offset value
  1005. * @extack: error reporting
  1006. *
  1007. * Dpll subsystem callback. Handler for getting phase shift value between
  1008. * dpll's input and output.
  1009. *
  1010. * Context: Acquires pf->dplls.lock
  1011. * Return:
  1012. * * 0 - success
  1013. * * negative - error
  1014. */
  1015. static int
  1016. ice_dpll_phase_offset_get(const struct dpll_pin *pin, void *pin_priv,
  1017. const struct dpll_device *dpll, void *dpll_priv,
  1018. s64 *phase_offset, struct netlink_ext_ack *extack)
  1019. {
  1020. struct ice_dpll *d = dpll_priv;
  1021. struct ice_pf *pf = d->pf;
  1022. mutex_lock(&pf->dplls.lock);
  1023. if (d->active_input == pin)
  1024. *phase_offset = d->phase_offset * ICE_DPLL_PHASE_OFFSET_FACTOR;
  1025. else
  1026. *phase_offset = 0;
  1027. mutex_unlock(&pf->dplls.lock);
  1028. return 0;
  1029. }
  1030. /**
  1031. * ice_dpll_output_esync_set - callback for setting embedded sync
  1032. * @pin: pointer to a pin
  1033. * @pin_priv: private data pointer passed on pin registration
  1034. * @dpll: registered dpll pointer
  1035. * @dpll_priv: private data pointer passed on dpll registration
  1036. * @freq: requested embedded sync frequency
  1037. * @extack: error reporting
  1038. *
  1039. * Dpll subsystem callback. Handler for setting embedded sync frequency value
  1040. * on output pin.
  1041. *
  1042. * Context: Acquires pf->dplls.lock
  1043. * Return:
  1044. * * 0 - success
  1045. * * negative - error
  1046. */
  1047. static int
  1048. ice_dpll_output_esync_set(const struct dpll_pin *pin, void *pin_priv,
  1049. const struct dpll_device *dpll, void *dpll_priv,
  1050. u64 freq, struct netlink_ext_ack *extack)
  1051. {
  1052. struct ice_dpll_pin *p = pin_priv;
  1053. struct ice_dpll *d = dpll_priv;
  1054. struct ice_pf *pf = d->pf;
  1055. u8 flags = 0;
  1056. int ret;
  1057. if (ice_dpll_is_reset(pf, extack))
  1058. return -EBUSY;
  1059. mutex_lock(&pf->dplls.lock);
  1060. if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_OUT_EN)
  1061. flags = ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
  1062. if (freq == DPLL_PIN_FREQUENCY_1_HZ) {
  1063. if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN) {
  1064. ret = 0;
  1065. } else {
  1066. flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
  1067. ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flags,
  1068. 0, 0, 0);
  1069. }
  1070. } else {
  1071. if (!(p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)) {
  1072. ret = 0;
  1073. } else {
  1074. flags &= ~ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
  1075. ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flags,
  1076. 0, 0, 0);
  1077. }
  1078. }
  1079. mutex_unlock(&pf->dplls.lock);
  1080. return ret;
  1081. }
  1082. /**
  1083. * ice_dpll_output_esync_get - callback for getting embedded sync config
  1084. * @pin: pointer to a pin
  1085. * @pin_priv: private data pointer passed on pin registration
  1086. * @dpll: registered dpll pointer
  1087. * @dpll_priv: private data pointer passed on dpll registration
  1088. * @esync: on success holds embedded sync pin properties
  1089. * @extack: error reporting
  1090. *
  1091. * Dpll subsystem callback. Handler for getting embedded sync frequency value
  1092. * and capabilities on output pin.
  1093. *
  1094. * Context: Acquires pf->dplls.lock
  1095. * Return:
  1096. * * 0 - success
  1097. * * negative - error
  1098. */
  1099. static int
  1100. ice_dpll_output_esync_get(const struct dpll_pin *pin, void *pin_priv,
  1101. const struct dpll_device *dpll, void *dpll_priv,
  1102. struct dpll_pin_esync *esync,
  1103. struct netlink_ext_ack *extack)
  1104. {
  1105. struct ice_dpll_pin *p = pin_priv;
  1106. struct ice_dpll *d = dpll_priv;
  1107. struct ice_pf *pf = d->pf;
  1108. if (ice_dpll_is_reset(pf, extack))
  1109. return -EBUSY;
  1110. mutex_lock(&pf->dplls.lock);
  1111. if (!(p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_ABILITY) ||
  1112. p->freq != DPLL_PIN_FREQUENCY_10_MHZ) {
  1113. mutex_unlock(&pf->dplls.lock);
  1114. return -EOPNOTSUPP;
  1115. }
  1116. esync->range = ice_esync_range;
  1117. esync->range_num = ARRAY_SIZE(ice_esync_range);
  1118. if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN) {
  1119. esync->freq = DPLL_PIN_FREQUENCY_1_HZ;
  1120. esync->pulse = ICE_DPLL_PIN_ESYNC_PULSE_HIGH_PERCENT;
  1121. } else {
  1122. esync->freq = 0;
  1123. esync->pulse = 0;
  1124. }
  1125. mutex_unlock(&pf->dplls.lock);
  1126. return 0;
  1127. }
  1128. /**
  1129. * ice_dpll_input_esync_set - callback for setting embedded sync
  1130. * @pin: pointer to a pin
  1131. * @pin_priv: private data pointer passed on pin registration
  1132. * @dpll: registered dpll pointer
  1133. * @dpll_priv: private data pointer passed on dpll registration
  1134. * @freq: requested embedded sync frequency
  1135. * @extack: error reporting
  1136. *
  1137. * Dpll subsystem callback. Handler for setting embedded sync frequency value
  1138. * on input pin.
  1139. *
  1140. * Context: Acquires pf->dplls.lock
  1141. * Return:
  1142. * * 0 - success
  1143. * * negative - error
  1144. */
  1145. static int
  1146. ice_dpll_input_esync_set(const struct dpll_pin *pin, void *pin_priv,
  1147. const struct dpll_device *dpll, void *dpll_priv,
  1148. u64 freq, struct netlink_ext_ack *extack)
  1149. {
  1150. struct ice_dpll_pin *p = pin_priv;
  1151. struct ice_dpll *d = dpll_priv;
  1152. struct ice_pf *pf = d->pf;
  1153. u8 flags_en = 0;
  1154. int ret;
  1155. if (ice_dpll_is_reset(pf, extack))
  1156. return -EBUSY;
  1157. mutex_lock(&pf->dplls.lock);
  1158. if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN)
  1159. flags_en = ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
  1160. if (freq == DPLL_PIN_FREQUENCY_1_HZ) {
  1161. if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN) {
  1162. ret = 0;
  1163. } else {
  1164. flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
  1165. ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, 0,
  1166. flags_en, 0, 0);
  1167. }
  1168. } else {
  1169. if (!(p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)) {
  1170. ret = 0;
  1171. } else {
  1172. flags_en &= ~ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
  1173. ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, 0,
  1174. flags_en, 0, 0);
  1175. }
  1176. }
  1177. mutex_unlock(&pf->dplls.lock);
  1178. return ret;
  1179. }
  1180. /**
  1181. * ice_dpll_input_esync_get - callback for getting embedded sync config
  1182. * @pin: pointer to a pin
  1183. * @pin_priv: private data pointer passed on pin registration
  1184. * @dpll: registered dpll pointer
  1185. * @dpll_priv: private data pointer passed on dpll registration
  1186. * @esync: on success holds embedded sync pin properties
  1187. * @extack: error reporting
  1188. *
  1189. * Dpll subsystem callback. Handler for getting embedded sync frequency value
  1190. * and capabilities on input pin.
  1191. *
  1192. * Context: Acquires pf->dplls.lock
  1193. * Return:
  1194. * * 0 - success
  1195. * * negative - error
  1196. */
  1197. static int
  1198. ice_dpll_input_esync_get(const struct dpll_pin *pin, void *pin_priv,
  1199. const struct dpll_device *dpll, void *dpll_priv,
  1200. struct dpll_pin_esync *esync,
  1201. struct netlink_ext_ack *extack)
  1202. {
  1203. struct ice_dpll_pin *p = pin_priv;
  1204. struct ice_dpll *d = dpll_priv;
  1205. struct ice_pf *pf = d->pf;
  1206. if (ice_dpll_is_reset(pf, extack))
  1207. return -EBUSY;
  1208. mutex_lock(&pf->dplls.lock);
  1209. if (!(p->status & ICE_AQC_GET_CGU_IN_CFG_STATUS_ESYNC_CAP) ||
  1210. p->freq != DPLL_PIN_FREQUENCY_10_MHZ) {
  1211. mutex_unlock(&pf->dplls.lock);
  1212. return -EOPNOTSUPP;
  1213. }
  1214. esync->range = ice_esync_range;
  1215. esync->range_num = ARRAY_SIZE(ice_esync_range);
  1216. if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN) {
  1217. esync->freq = DPLL_PIN_FREQUENCY_1_HZ;
  1218. esync->pulse = ICE_DPLL_PIN_ESYNC_PULSE_HIGH_PERCENT;
  1219. } else {
  1220. esync->freq = 0;
  1221. esync->pulse = 0;
  1222. }
  1223. mutex_unlock(&pf->dplls.lock);
  1224. return 0;
  1225. }
  1226. /**
  1227. * ice_dpll_rclk_state_on_pin_set - set a state on rclk pin
  1228. * @pin: pointer to a pin
  1229. * @pin_priv: private data pointer passed on pin registration
  1230. * @parent_pin: pin parent pointer
  1231. * @parent_pin_priv: parent private data pointer passed on pin registration
  1232. * @state: state to be set on pin
  1233. * @extack: error reporting
  1234. *
  1235. * Dpll subsystem callback, set a state of a rclk pin on a parent pin
  1236. *
  1237. * Context: Acquires pf->dplls.lock
  1238. * Return:
  1239. * * 0 - success
  1240. * * negative - failure
  1241. */
  1242. static int
  1243. ice_dpll_rclk_state_on_pin_set(const struct dpll_pin *pin, void *pin_priv,
  1244. const struct dpll_pin *parent_pin,
  1245. void *parent_pin_priv,
  1246. enum dpll_pin_state state,
  1247. struct netlink_ext_ack *extack)
  1248. {
  1249. struct ice_dpll_pin *p = pin_priv, *parent = parent_pin_priv;
  1250. bool enable = state == DPLL_PIN_STATE_CONNECTED;
  1251. struct ice_pf *pf = p->pf;
  1252. int ret = -EINVAL;
  1253. u32 hw_idx;
  1254. if (ice_dpll_is_reset(pf, extack))
  1255. return -EBUSY;
  1256. mutex_lock(&pf->dplls.lock);
  1257. hw_idx = parent->idx - pf->dplls.base_rclk_idx;
  1258. if (hw_idx >= pf->dplls.num_inputs)
  1259. goto unlock;
  1260. if ((enable && p->state[hw_idx] == DPLL_PIN_STATE_CONNECTED) ||
  1261. (!enable && p->state[hw_idx] == DPLL_PIN_STATE_DISCONNECTED)) {
  1262. NL_SET_ERR_MSG_FMT(extack,
  1263. "pin:%u state:%u on parent:%u already set",
  1264. p->idx, state, parent->idx);
  1265. goto unlock;
  1266. }
  1267. ret = ice_aq_set_phy_rec_clk_out(&pf->hw, hw_idx, enable,
  1268. &p->freq);
  1269. if (ret)
  1270. NL_SET_ERR_MSG_FMT(extack,
  1271. "err:%d %s failed to set pin state:%u for pin:%u on parent:%u\n",
  1272. ret,
  1273. ice_aq_str(pf->hw.adminq.sq_last_status),
  1274. state, p->idx, parent->idx);
  1275. unlock:
  1276. mutex_unlock(&pf->dplls.lock);
  1277. return ret;
  1278. }
  1279. /**
  1280. * ice_dpll_rclk_state_on_pin_get - get a state of rclk pin
  1281. * @pin: pointer to a pin
  1282. * @pin_priv: private data pointer passed on pin registration
  1283. * @parent_pin: pin parent pointer
  1284. * @parent_pin_priv: pin parent priv data pointer passed on pin registration
  1285. * @state: on success holds pin state on parent pin
  1286. * @extack: error reporting
  1287. *
  1288. * dpll subsystem callback, get a state of a recovered clock pin.
  1289. *
  1290. * Context: Acquires pf->dplls.lock
  1291. * Return:
  1292. * * 0 - success
  1293. * * negative - failure
  1294. */
  1295. static int
  1296. ice_dpll_rclk_state_on_pin_get(const struct dpll_pin *pin, void *pin_priv,
  1297. const struct dpll_pin *parent_pin,
  1298. void *parent_pin_priv,
  1299. enum dpll_pin_state *state,
  1300. struct netlink_ext_ack *extack)
  1301. {
  1302. struct ice_dpll_pin *p = pin_priv, *parent = parent_pin_priv;
  1303. struct ice_pf *pf = p->pf;
  1304. int ret = -EINVAL;
  1305. u32 hw_idx;
  1306. if (ice_dpll_is_reset(pf, extack))
  1307. return -EBUSY;
  1308. mutex_lock(&pf->dplls.lock);
  1309. hw_idx = parent->idx - pf->dplls.base_rclk_idx;
  1310. if (hw_idx >= pf->dplls.num_inputs)
  1311. goto unlock;
  1312. ret = ice_dpll_pin_state_update(pf, p, ICE_DPLL_PIN_TYPE_RCLK_INPUT,
  1313. extack);
  1314. if (ret)
  1315. goto unlock;
  1316. *state = p->state[hw_idx];
  1317. ret = 0;
  1318. unlock:
  1319. mutex_unlock(&pf->dplls.lock);
  1320. return ret;
  1321. }
  1322. static const struct dpll_pin_ops ice_dpll_rclk_ops = {
  1323. .state_on_pin_set = ice_dpll_rclk_state_on_pin_set,
  1324. .state_on_pin_get = ice_dpll_rclk_state_on_pin_get,
  1325. .direction_get = ice_dpll_input_direction,
  1326. };
  1327. static const struct dpll_pin_ops ice_dpll_input_ops = {
  1328. .frequency_get = ice_dpll_input_frequency_get,
  1329. .frequency_set = ice_dpll_input_frequency_set,
  1330. .state_on_dpll_get = ice_dpll_input_state_get,
  1331. .state_on_dpll_set = ice_dpll_input_state_set,
  1332. .prio_get = ice_dpll_input_prio_get,
  1333. .prio_set = ice_dpll_input_prio_set,
  1334. .direction_get = ice_dpll_input_direction,
  1335. .phase_adjust_get = ice_dpll_pin_phase_adjust_get,
  1336. .phase_adjust_set = ice_dpll_input_phase_adjust_set,
  1337. .phase_offset_get = ice_dpll_phase_offset_get,
  1338. .esync_set = ice_dpll_input_esync_set,
  1339. .esync_get = ice_dpll_input_esync_get,
  1340. };
  1341. static const struct dpll_pin_ops ice_dpll_output_ops = {
  1342. .frequency_get = ice_dpll_output_frequency_get,
  1343. .frequency_set = ice_dpll_output_frequency_set,
  1344. .state_on_dpll_get = ice_dpll_output_state_get,
  1345. .state_on_dpll_set = ice_dpll_output_state_set,
  1346. .direction_get = ice_dpll_output_direction,
  1347. .phase_adjust_get = ice_dpll_pin_phase_adjust_get,
  1348. .phase_adjust_set = ice_dpll_output_phase_adjust_set,
  1349. .esync_set = ice_dpll_output_esync_set,
  1350. .esync_get = ice_dpll_output_esync_get,
  1351. };
  1352. static const struct dpll_device_ops ice_dpll_ops = {
  1353. .lock_status_get = ice_dpll_lock_status_get,
  1354. .mode_get = ice_dpll_mode_get,
  1355. };
  1356. /**
  1357. * ice_generate_clock_id - generates unique clock_id for registering dpll.
  1358. * @pf: board private structure
  1359. *
  1360. * Generates unique (per board) clock_id for allocation and search of dpll
  1361. * devices in Linux dpll subsystem.
  1362. *
  1363. * Return: generated clock id for the board
  1364. */
  1365. static u64 ice_generate_clock_id(struct ice_pf *pf)
  1366. {
  1367. return pci_get_dsn(pf->pdev);
  1368. }
  1369. /**
  1370. * ice_dpll_notify_changes - notify dpll subsystem about changes
  1371. * @d: pointer do dpll
  1372. *
  1373. * Once change detected appropriate event is submitted to the dpll subsystem.
  1374. */
  1375. static void ice_dpll_notify_changes(struct ice_dpll *d)
  1376. {
  1377. bool pin_notified = false;
  1378. if (d->prev_dpll_state != d->dpll_state) {
  1379. d->prev_dpll_state = d->dpll_state;
  1380. dpll_device_change_ntf(d->dpll);
  1381. }
  1382. if (d->prev_input != d->active_input) {
  1383. if (d->prev_input)
  1384. dpll_pin_change_ntf(d->prev_input);
  1385. d->prev_input = d->active_input;
  1386. if (d->active_input) {
  1387. dpll_pin_change_ntf(d->active_input);
  1388. pin_notified = true;
  1389. }
  1390. }
  1391. if (d->prev_phase_offset != d->phase_offset) {
  1392. d->prev_phase_offset = d->phase_offset;
  1393. if (!pin_notified && d->active_input)
  1394. dpll_pin_change_ntf(d->active_input);
  1395. }
  1396. }
  1397. /**
  1398. * ice_dpll_update_state - update dpll state
  1399. * @pf: pf private structure
  1400. * @d: pointer to queried dpll device
  1401. * @init: if function called on initialization of ice dpll
  1402. *
  1403. * Poll current state of dpll from hw and update ice_dpll struct.
  1404. *
  1405. * Context: Called by kworker under pf->dplls.lock
  1406. * Return:
  1407. * * 0 - success
  1408. * * negative - AQ failure
  1409. */
  1410. static int
  1411. ice_dpll_update_state(struct ice_pf *pf, struct ice_dpll *d, bool init)
  1412. {
  1413. struct ice_dpll_pin *p = NULL;
  1414. int ret;
  1415. ret = ice_get_cgu_state(&pf->hw, d->dpll_idx, d->prev_dpll_state,
  1416. &d->input_idx, &d->ref_state, &d->eec_mode,
  1417. &d->phase_offset, &d->dpll_state);
  1418. dev_dbg(ice_pf_to_dev(pf),
  1419. "update dpll=%d, prev_src_idx:%u, src_idx:%u, state:%d, prev:%d mode:%d\n",
  1420. d->dpll_idx, d->prev_input_idx, d->input_idx,
  1421. d->dpll_state, d->prev_dpll_state, d->mode);
  1422. if (ret) {
  1423. dev_err(ice_pf_to_dev(pf),
  1424. "update dpll=%d state failed, ret=%d %s\n",
  1425. d->dpll_idx, ret,
  1426. ice_aq_str(pf->hw.adminq.sq_last_status));
  1427. return ret;
  1428. }
  1429. if (init) {
  1430. if (d->dpll_state == DPLL_LOCK_STATUS_LOCKED ||
  1431. d->dpll_state == DPLL_LOCK_STATUS_LOCKED_HO_ACQ)
  1432. d->active_input = pf->dplls.inputs[d->input_idx].pin;
  1433. p = &pf->dplls.inputs[d->input_idx];
  1434. return ice_dpll_pin_state_update(pf, p,
  1435. ICE_DPLL_PIN_TYPE_INPUT, NULL);
  1436. }
  1437. if (d->dpll_state == DPLL_LOCK_STATUS_HOLDOVER ||
  1438. d->dpll_state == DPLL_LOCK_STATUS_UNLOCKED) {
  1439. d->active_input = NULL;
  1440. if (d->input_idx != ICE_DPLL_PIN_IDX_INVALID)
  1441. p = &pf->dplls.inputs[d->input_idx];
  1442. d->prev_input_idx = ICE_DPLL_PIN_IDX_INVALID;
  1443. d->input_idx = ICE_DPLL_PIN_IDX_INVALID;
  1444. if (!p)
  1445. return 0;
  1446. ret = ice_dpll_pin_state_update(pf, p,
  1447. ICE_DPLL_PIN_TYPE_INPUT, NULL);
  1448. } else if (d->input_idx != d->prev_input_idx) {
  1449. if (d->prev_input_idx != ICE_DPLL_PIN_IDX_INVALID) {
  1450. p = &pf->dplls.inputs[d->prev_input_idx];
  1451. ice_dpll_pin_state_update(pf, p,
  1452. ICE_DPLL_PIN_TYPE_INPUT,
  1453. NULL);
  1454. }
  1455. if (d->input_idx != ICE_DPLL_PIN_IDX_INVALID) {
  1456. p = &pf->dplls.inputs[d->input_idx];
  1457. d->active_input = p->pin;
  1458. ice_dpll_pin_state_update(pf, p,
  1459. ICE_DPLL_PIN_TYPE_INPUT,
  1460. NULL);
  1461. }
  1462. d->prev_input_idx = d->input_idx;
  1463. }
  1464. return ret;
  1465. }
  1466. /**
  1467. * ice_dpll_periodic_work - DPLLs periodic worker
  1468. * @work: pointer to kthread_work structure
  1469. *
  1470. * DPLLs periodic worker is responsible for polling state of dpll.
  1471. * Context: Holds pf->dplls.lock
  1472. */
  1473. static void ice_dpll_periodic_work(struct kthread_work *work)
  1474. {
  1475. struct ice_dplls *d = container_of(work, struct ice_dplls, work.work);
  1476. struct ice_pf *pf = container_of(d, struct ice_pf, dplls);
  1477. struct ice_dpll *de = &pf->dplls.eec;
  1478. struct ice_dpll *dp = &pf->dplls.pps;
  1479. int ret = 0;
  1480. if (ice_is_reset_in_progress(pf->state))
  1481. goto resched;
  1482. mutex_lock(&pf->dplls.lock);
  1483. ret = ice_dpll_update_state(pf, de, false);
  1484. if (!ret)
  1485. ret = ice_dpll_update_state(pf, dp, false);
  1486. if (ret) {
  1487. d->cgu_state_acq_err_num++;
  1488. /* stop rescheduling this worker */
  1489. if (d->cgu_state_acq_err_num >
  1490. ICE_CGU_STATE_ACQ_ERR_THRESHOLD) {
  1491. dev_err(ice_pf_to_dev(pf),
  1492. "EEC/PPS DPLLs periodic work disabled\n");
  1493. mutex_unlock(&pf->dplls.lock);
  1494. return;
  1495. }
  1496. }
  1497. mutex_unlock(&pf->dplls.lock);
  1498. ice_dpll_notify_changes(de);
  1499. ice_dpll_notify_changes(dp);
  1500. resched:
  1501. /* Run twice a second or reschedule if update failed */
  1502. kthread_queue_delayed_work(d->kworker, &d->work,
  1503. ret ? msecs_to_jiffies(10) :
  1504. msecs_to_jiffies(500));
  1505. }
  1506. /**
  1507. * ice_dpll_release_pins - release pins resources from dpll subsystem
  1508. * @pins: pointer to pins array
  1509. * @count: number of pins
  1510. *
  1511. * Release resources of given pins array in the dpll subsystem.
  1512. */
  1513. static void ice_dpll_release_pins(struct ice_dpll_pin *pins, int count)
  1514. {
  1515. int i;
  1516. for (i = 0; i < count; i++)
  1517. dpll_pin_put(pins[i].pin);
  1518. }
  1519. /**
  1520. * ice_dpll_get_pins - get pins from dpll subsystem
  1521. * @pf: board private structure
  1522. * @pins: pointer to pins array
  1523. * @start_idx: get starts from this pin idx value
  1524. * @count: number of pins
  1525. * @clock_id: clock_id of dpll device
  1526. *
  1527. * Get pins - allocate - in dpll subsystem, store them in pin field of given
  1528. * pins array.
  1529. *
  1530. * Return:
  1531. * * 0 - success
  1532. * * negative - allocation failure reason
  1533. */
  1534. static int
  1535. ice_dpll_get_pins(struct ice_pf *pf, struct ice_dpll_pin *pins,
  1536. int start_idx, int count, u64 clock_id)
  1537. {
  1538. int i, ret;
  1539. for (i = 0; i < count; i++) {
  1540. pins[i].pin = dpll_pin_get(clock_id, i + start_idx, THIS_MODULE,
  1541. &pins[i].prop);
  1542. if (IS_ERR(pins[i].pin)) {
  1543. ret = PTR_ERR(pins[i].pin);
  1544. goto release_pins;
  1545. }
  1546. }
  1547. return 0;
  1548. release_pins:
  1549. while (--i >= 0)
  1550. dpll_pin_put(pins[i].pin);
  1551. return ret;
  1552. }
  1553. /**
  1554. * ice_dpll_unregister_pins - unregister pins from a dpll
  1555. * @dpll: dpll device pointer
  1556. * @pins: pointer to pins array
  1557. * @ops: callback ops registered with the pins
  1558. * @count: number of pins
  1559. *
  1560. * Unregister pins of a given array of pins from given dpll device registered in
  1561. * dpll subsystem.
  1562. */
  1563. static void
  1564. ice_dpll_unregister_pins(struct dpll_device *dpll, struct ice_dpll_pin *pins,
  1565. const struct dpll_pin_ops *ops, int count)
  1566. {
  1567. int i;
  1568. for (i = 0; i < count; i++)
  1569. dpll_pin_unregister(dpll, pins[i].pin, ops, &pins[i]);
  1570. }
  1571. /**
  1572. * ice_dpll_register_pins - register pins with a dpll
  1573. * @dpll: dpll pointer to register pins with
  1574. * @pins: pointer to pins array
  1575. * @ops: callback ops registered with the pins
  1576. * @count: number of pins
  1577. *
  1578. * Register pins of a given array with given dpll in dpll subsystem.
  1579. *
  1580. * Return:
  1581. * * 0 - success
  1582. * * negative - registration failure reason
  1583. */
  1584. static int
  1585. ice_dpll_register_pins(struct dpll_device *dpll, struct ice_dpll_pin *pins,
  1586. const struct dpll_pin_ops *ops, int count)
  1587. {
  1588. int ret, i;
  1589. for (i = 0; i < count; i++) {
  1590. ret = dpll_pin_register(dpll, pins[i].pin, ops, &pins[i]);
  1591. if (ret)
  1592. goto unregister_pins;
  1593. }
  1594. return 0;
  1595. unregister_pins:
  1596. while (--i >= 0)
  1597. dpll_pin_unregister(dpll, pins[i].pin, ops, &pins[i]);
  1598. return ret;
  1599. }
  1600. /**
  1601. * ice_dpll_deinit_direct_pins - deinitialize direct pins
  1602. * @cgu: if cgu is present and controlled by this NIC
  1603. * @pins: pointer to pins array
  1604. * @count: number of pins
  1605. * @ops: callback ops registered with the pins
  1606. * @first: dpll device pointer
  1607. * @second: dpll device pointer
  1608. *
  1609. * If cgu is owned unregister pins from given dplls.
  1610. * Release pins resources to the dpll subsystem.
  1611. */
  1612. static void
  1613. ice_dpll_deinit_direct_pins(bool cgu, struct ice_dpll_pin *pins, int count,
  1614. const struct dpll_pin_ops *ops,
  1615. struct dpll_device *first,
  1616. struct dpll_device *second)
  1617. {
  1618. if (cgu) {
  1619. ice_dpll_unregister_pins(first, pins, ops, count);
  1620. ice_dpll_unregister_pins(second, pins, ops, count);
  1621. }
  1622. ice_dpll_release_pins(pins, count);
  1623. }
  1624. /**
  1625. * ice_dpll_init_direct_pins - initialize direct pins
  1626. * @pf: board private structure
  1627. * @cgu: if cgu is present and controlled by this NIC
  1628. * @pins: pointer to pins array
  1629. * @start_idx: on which index shall allocation start in dpll subsystem
  1630. * @count: number of pins
  1631. * @ops: callback ops registered with the pins
  1632. * @first: dpll device pointer
  1633. * @second: dpll device pointer
  1634. *
  1635. * Allocate directly connected pins of a given array in dpll subsystem.
  1636. * If cgu is owned register allocated pins with given dplls.
  1637. *
  1638. * Return:
  1639. * * 0 - success
  1640. * * negative - registration failure reason
  1641. */
  1642. static int
  1643. ice_dpll_init_direct_pins(struct ice_pf *pf, bool cgu,
  1644. struct ice_dpll_pin *pins, int start_idx, int count,
  1645. const struct dpll_pin_ops *ops,
  1646. struct dpll_device *first, struct dpll_device *second)
  1647. {
  1648. int ret;
  1649. ret = ice_dpll_get_pins(pf, pins, start_idx, count, pf->dplls.clock_id);
  1650. if (ret)
  1651. return ret;
  1652. if (cgu) {
  1653. ret = ice_dpll_register_pins(first, pins, ops, count);
  1654. if (ret)
  1655. goto release_pins;
  1656. ret = ice_dpll_register_pins(second, pins, ops, count);
  1657. if (ret)
  1658. goto unregister_first;
  1659. }
  1660. return 0;
  1661. unregister_first:
  1662. ice_dpll_unregister_pins(first, pins, ops, count);
  1663. release_pins:
  1664. ice_dpll_release_pins(pins, count);
  1665. return ret;
  1666. }
  1667. /**
  1668. * ice_dpll_deinit_rclk_pin - release rclk pin resources
  1669. * @pf: board private structure
  1670. *
  1671. * Deregister rclk pin from parent pins and release resources in dpll subsystem.
  1672. */
  1673. static void ice_dpll_deinit_rclk_pin(struct ice_pf *pf)
  1674. {
  1675. struct ice_dpll_pin *rclk = &pf->dplls.rclk;
  1676. struct ice_vsi *vsi = ice_get_main_vsi(pf);
  1677. struct dpll_pin *parent;
  1678. int i;
  1679. for (i = 0; i < rclk->num_parents; i++) {
  1680. parent = pf->dplls.inputs[rclk->parent_idx[i]].pin;
  1681. if (!parent)
  1682. continue;
  1683. dpll_pin_on_pin_unregister(parent, rclk->pin,
  1684. &ice_dpll_rclk_ops, rclk);
  1685. }
  1686. if (WARN_ON_ONCE(!vsi || !vsi->netdev))
  1687. return;
  1688. dpll_netdev_pin_clear(vsi->netdev);
  1689. dpll_pin_put(rclk->pin);
  1690. }
  1691. /**
  1692. * ice_dpll_init_rclk_pins - initialize recovered clock pin
  1693. * @pf: board private structure
  1694. * @pin: pin to register
  1695. * @start_idx: on which index shall allocation start in dpll subsystem
  1696. * @ops: callback ops registered with the pins
  1697. *
  1698. * Allocate resource for recovered clock pin in dpll subsystem. Register the
  1699. * pin with the parents it has in the info. Register pin with the pf's main vsi
  1700. * netdev.
  1701. *
  1702. * Return:
  1703. * * 0 - success
  1704. * * negative - registration failure reason
  1705. */
  1706. static int
  1707. ice_dpll_init_rclk_pins(struct ice_pf *pf, struct ice_dpll_pin *pin,
  1708. int start_idx, const struct dpll_pin_ops *ops)
  1709. {
  1710. struct ice_vsi *vsi = ice_get_main_vsi(pf);
  1711. struct dpll_pin *parent;
  1712. int ret, i;
  1713. if (WARN_ON((!vsi || !vsi->netdev)))
  1714. return -EINVAL;
  1715. ret = ice_dpll_get_pins(pf, pin, start_idx, ICE_DPLL_RCLK_NUM_PER_PF,
  1716. pf->dplls.clock_id);
  1717. if (ret)
  1718. return ret;
  1719. for (i = 0; i < pf->dplls.rclk.num_parents; i++) {
  1720. parent = pf->dplls.inputs[pf->dplls.rclk.parent_idx[i]].pin;
  1721. if (!parent) {
  1722. ret = -ENODEV;
  1723. goto unregister_pins;
  1724. }
  1725. ret = dpll_pin_on_pin_register(parent, pf->dplls.rclk.pin,
  1726. ops, &pf->dplls.rclk);
  1727. if (ret)
  1728. goto unregister_pins;
  1729. }
  1730. dpll_netdev_pin_set(vsi->netdev, pf->dplls.rclk.pin);
  1731. return 0;
  1732. unregister_pins:
  1733. while (i) {
  1734. parent = pf->dplls.inputs[pf->dplls.rclk.parent_idx[--i]].pin;
  1735. dpll_pin_on_pin_unregister(parent, pf->dplls.rclk.pin,
  1736. &ice_dpll_rclk_ops, &pf->dplls.rclk);
  1737. }
  1738. ice_dpll_release_pins(pin, ICE_DPLL_RCLK_NUM_PER_PF);
  1739. return ret;
  1740. }
  1741. /**
  1742. * ice_dpll_deinit_pins - deinitialize direct pins
  1743. * @pf: board private structure
  1744. * @cgu: if cgu is controlled by this pf
  1745. *
  1746. * If cgu is owned unregister directly connected pins from the dplls.
  1747. * Release resources of directly connected pins from the dpll subsystem.
  1748. */
  1749. static void ice_dpll_deinit_pins(struct ice_pf *pf, bool cgu)
  1750. {
  1751. struct ice_dpll_pin *outputs = pf->dplls.outputs;
  1752. struct ice_dpll_pin *inputs = pf->dplls.inputs;
  1753. int num_outputs = pf->dplls.num_outputs;
  1754. int num_inputs = pf->dplls.num_inputs;
  1755. struct ice_dplls *d = &pf->dplls;
  1756. struct ice_dpll *de = &d->eec;
  1757. struct ice_dpll *dp = &d->pps;
  1758. ice_dpll_deinit_rclk_pin(pf);
  1759. if (cgu) {
  1760. ice_dpll_unregister_pins(dp->dpll, inputs, &ice_dpll_input_ops,
  1761. num_inputs);
  1762. ice_dpll_unregister_pins(de->dpll, inputs, &ice_dpll_input_ops,
  1763. num_inputs);
  1764. }
  1765. ice_dpll_release_pins(inputs, num_inputs);
  1766. if (cgu) {
  1767. ice_dpll_unregister_pins(dp->dpll, outputs,
  1768. &ice_dpll_output_ops, num_outputs);
  1769. ice_dpll_unregister_pins(de->dpll, outputs,
  1770. &ice_dpll_output_ops, num_outputs);
  1771. ice_dpll_release_pins(outputs, num_outputs);
  1772. }
  1773. }
  1774. /**
  1775. * ice_dpll_init_pins - init pins and register pins with a dplls
  1776. * @pf: board private structure
  1777. * @cgu: if cgu is present and controlled by this NIC
  1778. *
  1779. * Initialize directly connected pf's pins within pf's dplls in a Linux dpll
  1780. * subsystem.
  1781. *
  1782. * Return:
  1783. * * 0 - success
  1784. * * negative - initialization failure reason
  1785. */
  1786. static int ice_dpll_init_pins(struct ice_pf *pf, bool cgu)
  1787. {
  1788. u32 rclk_idx;
  1789. int ret;
  1790. ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.inputs, 0,
  1791. pf->dplls.num_inputs,
  1792. &ice_dpll_input_ops,
  1793. pf->dplls.eec.dpll, pf->dplls.pps.dpll);
  1794. if (ret)
  1795. return ret;
  1796. if (cgu) {
  1797. ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.outputs,
  1798. pf->dplls.num_inputs,
  1799. pf->dplls.num_outputs,
  1800. &ice_dpll_output_ops,
  1801. pf->dplls.eec.dpll,
  1802. pf->dplls.pps.dpll);
  1803. if (ret)
  1804. goto deinit_inputs;
  1805. }
  1806. rclk_idx = pf->dplls.num_inputs + pf->dplls.num_outputs + pf->hw.pf_id;
  1807. ret = ice_dpll_init_rclk_pins(pf, &pf->dplls.rclk, rclk_idx,
  1808. &ice_dpll_rclk_ops);
  1809. if (ret)
  1810. goto deinit_outputs;
  1811. return 0;
  1812. deinit_outputs:
  1813. ice_dpll_deinit_direct_pins(cgu, pf->dplls.outputs,
  1814. pf->dplls.num_outputs,
  1815. &ice_dpll_output_ops, pf->dplls.pps.dpll,
  1816. pf->dplls.eec.dpll);
  1817. deinit_inputs:
  1818. ice_dpll_deinit_direct_pins(cgu, pf->dplls.inputs, pf->dplls.num_inputs,
  1819. &ice_dpll_input_ops, pf->dplls.pps.dpll,
  1820. pf->dplls.eec.dpll);
  1821. return ret;
  1822. }
  1823. /**
  1824. * ice_dpll_deinit_dpll - deinitialize dpll device
  1825. * @pf: board private structure
  1826. * @d: pointer to ice_dpll
  1827. * @cgu: if cgu is present and controlled by this NIC
  1828. *
  1829. * If cgu is owned unregister the dpll from dpll subsystem.
  1830. * Release resources of dpll device from dpll subsystem.
  1831. */
  1832. static void
  1833. ice_dpll_deinit_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu)
  1834. {
  1835. if (cgu)
  1836. dpll_device_unregister(d->dpll, &ice_dpll_ops, d);
  1837. dpll_device_put(d->dpll);
  1838. }
  1839. /**
  1840. * ice_dpll_init_dpll - initialize dpll device in dpll subsystem
  1841. * @pf: board private structure
  1842. * @d: dpll to be initialized
  1843. * @cgu: if cgu is present and controlled by this NIC
  1844. * @type: type of dpll being initialized
  1845. *
  1846. * Allocate dpll instance for this board in dpll subsystem, if cgu is controlled
  1847. * by this NIC, register dpll with the callback ops.
  1848. *
  1849. * Return:
  1850. * * 0 - success
  1851. * * negative - initialization failure reason
  1852. */
  1853. static int
  1854. ice_dpll_init_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu,
  1855. enum dpll_type type)
  1856. {
  1857. u64 clock_id = pf->dplls.clock_id;
  1858. int ret;
  1859. d->dpll = dpll_device_get(clock_id, d->dpll_idx, THIS_MODULE);
  1860. if (IS_ERR(d->dpll)) {
  1861. ret = PTR_ERR(d->dpll);
  1862. dev_err(ice_pf_to_dev(pf),
  1863. "dpll_device_get failed (%p) err=%d\n", d, ret);
  1864. return ret;
  1865. }
  1866. d->pf = pf;
  1867. if (cgu) {
  1868. ice_dpll_update_state(pf, d, true);
  1869. ret = dpll_device_register(d->dpll, type, &ice_dpll_ops, d);
  1870. if (ret) {
  1871. dpll_device_put(d->dpll);
  1872. return ret;
  1873. }
  1874. }
  1875. return 0;
  1876. }
  1877. /**
  1878. * ice_dpll_deinit_worker - deinitialize dpll kworker
  1879. * @pf: board private structure
  1880. *
  1881. * Stop dpll's kworker, release it's resources.
  1882. */
  1883. static void ice_dpll_deinit_worker(struct ice_pf *pf)
  1884. {
  1885. struct ice_dplls *d = &pf->dplls;
  1886. kthread_cancel_delayed_work_sync(&d->work);
  1887. kthread_destroy_worker(d->kworker);
  1888. }
  1889. /**
  1890. * ice_dpll_init_worker - Initialize DPLLs periodic worker
  1891. * @pf: board private structure
  1892. *
  1893. * Create and start DPLLs periodic worker.
  1894. *
  1895. * Context: Shall be called after pf->dplls.lock is initialized.
  1896. * Return:
  1897. * * 0 - success
  1898. * * negative - create worker failure
  1899. */
  1900. static int ice_dpll_init_worker(struct ice_pf *pf)
  1901. {
  1902. struct ice_dplls *d = &pf->dplls;
  1903. struct kthread_worker *kworker;
  1904. kthread_init_delayed_work(&d->work, ice_dpll_periodic_work);
  1905. kworker = kthread_create_worker(0, "ice-dplls-%s",
  1906. dev_name(ice_pf_to_dev(pf)));
  1907. if (IS_ERR(kworker))
  1908. return PTR_ERR(kworker);
  1909. d->kworker = kworker;
  1910. d->cgu_state_acq_err_num = 0;
  1911. kthread_queue_delayed_work(d->kworker, &d->work, 0);
  1912. return 0;
  1913. }
  1914. /**
  1915. * ice_dpll_phase_range_set - initialize phase adjust range helper
  1916. * @range: pointer to phase adjust range struct to be initialized
  1917. * @phase_adj: a value to be used as min(-)/max(+) boundary
  1918. */
  1919. static void ice_dpll_phase_range_set(struct dpll_pin_phase_adjust_range *range,
  1920. u32 phase_adj)
  1921. {
  1922. range->min = -phase_adj;
  1923. range->max = phase_adj;
  1924. }
  1925. /**
  1926. * ice_dpll_init_info_pins_generic - initializes generic pins info
  1927. * @pf: board private structure
  1928. * @input: if input pins initialized
  1929. *
  1930. * Init information for generic pins, cache them in PF's pins structures.
  1931. *
  1932. * Return:
  1933. * * 0 - success
  1934. * * negative - init failure reason
  1935. */
  1936. static int ice_dpll_init_info_pins_generic(struct ice_pf *pf, bool input)
  1937. {
  1938. struct ice_dpll *de = &pf->dplls.eec, *dp = &pf->dplls.pps;
  1939. static const char labels[][sizeof("99")] = {
  1940. "0", "1", "2", "3", "4", "5", "6", "7", "8",
  1941. "9", "10", "11", "12", "13", "14", "15" };
  1942. u32 cap = DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
  1943. enum ice_dpll_pin_type pin_type;
  1944. int i, pin_num, ret = -EINVAL;
  1945. struct ice_dpll_pin *pins;
  1946. u32 phase_adj_max;
  1947. if (input) {
  1948. pin_num = pf->dplls.num_inputs;
  1949. pins = pf->dplls.inputs;
  1950. phase_adj_max = pf->dplls.input_phase_adj_max;
  1951. pin_type = ICE_DPLL_PIN_TYPE_INPUT;
  1952. cap |= DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE;
  1953. } else {
  1954. pin_num = pf->dplls.num_outputs;
  1955. pins = pf->dplls.outputs;
  1956. phase_adj_max = pf->dplls.output_phase_adj_max;
  1957. pin_type = ICE_DPLL_PIN_TYPE_OUTPUT;
  1958. }
  1959. if (pin_num > ARRAY_SIZE(labels))
  1960. return ret;
  1961. for (i = 0; i < pin_num; i++) {
  1962. pins[i].idx = i;
  1963. pins[i].prop.board_label = labels[i];
  1964. ice_dpll_phase_range_set(&pins[i].prop.phase_range,
  1965. phase_adj_max);
  1966. pins[i].prop.capabilities = cap;
  1967. pins[i].pf = pf;
  1968. ret = ice_dpll_pin_state_update(pf, &pins[i], pin_type, NULL);
  1969. if (ret)
  1970. break;
  1971. if (input && pins[i].freq == ICE_DPLL_PIN_GEN_RCLK_FREQ)
  1972. pins[i].prop.type = DPLL_PIN_TYPE_MUX;
  1973. else
  1974. pins[i].prop.type = DPLL_PIN_TYPE_EXT;
  1975. if (!input)
  1976. continue;
  1977. ret = ice_aq_get_cgu_ref_prio(&pf->hw, de->dpll_idx, i,
  1978. &de->input_prio[i]);
  1979. if (ret)
  1980. break;
  1981. ret = ice_aq_get_cgu_ref_prio(&pf->hw, dp->dpll_idx, i,
  1982. &dp->input_prio[i]);
  1983. if (ret)
  1984. break;
  1985. }
  1986. return ret;
  1987. }
  1988. /**
  1989. * ice_dpll_init_info_direct_pins - initializes direct pins info
  1990. * @pf: board private structure
  1991. * @pin_type: type of pins being initialized
  1992. *
  1993. * Init information for directly connected pins, cache them in pf's pins
  1994. * structures.
  1995. *
  1996. * Return:
  1997. * * 0 - success
  1998. * * negative - init failure reason
  1999. */
  2000. static int
  2001. ice_dpll_init_info_direct_pins(struct ice_pf *pf,
  2002. enum ice_dpll_pin_type pin_type)
  2003. {
  2004. struct ice_dpll *de = &pf->dplls.eec, *dp = &pf->dplls.pps;
  2005. int num_pins, i, ret = -EINVAL;
  2006. struct ice_hw *hw = &pf->hw;
  2007. struct ice_dpll_pin *pins;
  2008. unsigned long caps;
  2009. u32 phase_adj_max;
  2010. u8 freq_supp_num;
  2011. bool input;
  2012. switch (pin_type) {
  2013. case ICE_DPLL_PIN_TYPE_INPUT:
  2014. pins = pf->dplls.inputs;
  2015. num_pins = pf->dplls.num_inputs;
  2016. phase_adj_max = pf->dplls.input_phase_adj_max;
  2017. input = true;
  2018. break;
  2019. case ICE_DPLL_PIN_TYPE_OUTPUT:
  2020. pins = pf->dplls.outputs;
  2021. num_pins = pf->dplls.num_outputs;
  2022. phase_adj_max = pf->dplls.output_phase_adj_max;
  2023. input = false;
  2024. break;
  2025. default:
  2026. return -EINVAL;
  2027. }
  2028. if (num_pins != ice_cgu_get_num_pins(hw, input))
  2029. return ice_dpll_init_info_pins_generic(pf, input);
  2030. for (i = 0; i < num_pins; i++) {
  2031. caps = 0;
  2032. pins[i].idx = i;
  2033. pins[i].prop.board_label = ice_cgu_get_pin_name(hw, i, input);
  2034. pins[i].prop.type = ice_cgu_get_pin_type(hw, i, input);
  2035. if (input) {
  2036. ret = ice_aq_get_cgu_ref_prio(hw, de->dpll_idx, i,
  2037. &de->input_prio[i]);
  2038. if (ret)
  2039. return ret;
  2040. ret = ice_aq_get_cgu_ref_prio(hw, dp->dpll_idx, i,
  2041. &dp->input_prio[i]);
  2042. if (ret)
  2043. return ret;
  2044. caps |= (DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE |
  2045. DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE);
  2046. } else {
  2047. ret = ice_cgu_get_output_pin_state_caps(hw, i, &caps);
  2048. if (ret)
  2049. return ret;
  2050. }
  2051. ice_dpll_phase_range_set(&pins[i].prop.phase_range,
  2052. phase_adj_max);
  2053. pins[i].prop.capabilities = caps;
  2054. ret = ice_dpll_pin_state_update(pf, &pins[i], pin_type, NULL);
  2055. if (ret)
  2056. return ret;
  2057. pins[i].prop.freq_supported =
  2058. ice_cgu_get_pin_freq_supp(hw, i, input, &freq_supp_num);
  2059. pins[i].prop.freq_supported_num = freq_supp_num;
  2060. pins[i].pf = pf;
  2061. }
  2062. return ret;
  2063. }
  2064. /**
  2065. * ice_dpll_init_info_rclk_pin - initializes rclk pin information
  2066. * @pf: board private structure
  2067. *
  2068. * Init information for rclk pin, cache them in pf->dplls.rclk.
  2069. *
  2070. * Return:
  2071. * * 0 - success
  2072. * * negative - init failure reason
  2073. */
  2074. static int ice_dpll_init_info_rclk_pin(struct ice_pf *pf)
  2075. {
  2076. struct ice_dpll_pin *pin = &pf->dplls.rclk;
  2077. pin->prop.type = DPLL_PIN_TYPE_SYNCE_ETH_PORT;
  2078. pin->prop.capabilities |= DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
  2079. pin->pf = pf;
  2080. return ice_dpll_pin_state_update(pf, pin,
  2081. ICE_DPLL_PIN_TYPE_RCLK_INPUT, NULL);
  2082. }
  2083. /**
  2084. * ice_dpll_init_pins_info - init pins info wrapper
  2085. * @pf: board private structure
  2086. * @pin_type: type of pins being initialized
  2087. *
  2088. * Wraps functions for pin initialization.
  2089. *
  2090. * Return:
  2091. * * 0 - success
  2092. * * negative - init failure reason
  2093. */
  2094. static int
  2095. ice_dpll_init_pins_info(struct ice_pf *pf, enum ice_dpll_pin_type pin_type)
  2096. {
  2097. switch (pin_type) {
  2098. case ICE_DPLL_PIN_TYPE_INPUT:
  2099. case ICE_DPLL_PIN_TYPE_OUTPUT:
  2100. return ice_dpll_init_info_direct_pins(pf, pin_type);
  2101. case ICE_DPLL_PIN_TYPE_RCLK_INPUT:
  2102. return ice_dpll_init_info_rclk_pin(pf);
  2103. default:
  2104. return -EINVAL;
  2105. }
  2106. }
  2107. /**
  2108. * ice_dpll_deinit_info - release memory allocated for pins info
  2109. * @pf: board private structure
  2110. *
  2111. * Release memory allocated for pins by ice_dpll_init_info function.
  2112. */
  2113. static void ice_dpll_deinit_info(struct ice_pf *pf)
  2114. {
  2115. kfree(pf->dplls.inputs);
  2116. kfree(pf->dplls.outputs);
  2117. kfree(pf->dplls.eec.input_prio);
  2118. kfree(pf->dplls.pps.input_prio);
  2119. }
  2120. /**
  2121. * ice_dpll_init_info - prepare pf's dpll information structure
  2122. * @pf: board private structure
  2123. * @cgu: if cgu is present and controlled by this NIC
  2124. *
  2125. * Acquire (from HW) and set basic dpll information (on pf->dplls struct).
  2126. *
  2127. * Return:
  2128. * * 0 - success
  2129. * * negative - init failure reason
  2130. */
  2131. static int ice_dpll_init_info(struct ice_pf *pf, bool cgu)
  2132. {
  2133. struct ice_aqc_get_cgu_abilities abilities;
  2134. struct ice_dpll *de = &pf->dplls.eec;
  2135. struct ice_dpll *dp = &pf->dplls.pps;
  2136. struct ice_dplls *d = &pf->dplls;
  2137. struct ice_hw *hw = &pf->hw;
  2138. int ret, alloc_size, i;
  2139. d->clock_id = ice_generate_clock_id(pf);
  2140. ret = ice_aq_get_cgu_abilities(hw, &abilities);
  2141. if (ret) {
  2142. dev_err(ice_pf_to_dev(pf),
  2143. "err:%d %s failed to read cgu abilities\n",
  2144. ret, ice_aq_str(hw->adminq.sq_last_status));
  2145. return ret;
  2146. }
  2147. de->dpll_idx = abilities.eec_dpll_idx;
  2148. dp->dpll_idx = abilities.pps_dpll_idx;
  2149. d->num_inputs = abilities.num_inputs;
  2150. d->num_outputs = abilities.num_outputs;
  2151. d->input_phase_adj_max = le32_to_cpu(abilities.max_in_phase_adj) &
  2152. ICE_AQC_GET_CGU_MAX_PHASE_ADJ;
  2153. d->output_phase_adj_max = le32_to_cpu(abilities.max_out_phase_adj) &
  2154. ICE_AQC_GET_CGU_MAX_PHASE_ADJ;
  2155. alloc_size = sizeof(*d->inputs) * d->num_inputs;
  2156. d->inputs = kzalloc(alloc_size, GFP_KERNEL);
  2157. if (!d->inputs)
  2158. return -ENOMEM;
  2159. alloc_size = sizeof(*de->input_prio) * d->num_inputs;
  2160. de->input_prio = kzalloc(alloc_size, GFP_KERNEL);
  2161. if (!de->input_prio)
  2162. return -ENOMEM;
  2163. dp->input_prio = kzalloc(alloc_size, GFP_KERNEL);
  2164. if (!dp->input_prio)
  2165. return -ENOMEM;
  2166. ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_INPUT);
  2167. if (ret)
  2168. goto deinit_info;
  2169. if (cgu) {
  2170. alloc_size = sizeof(*d->outputs) * d->num_outputs;
  2171. d->outputs = kzalloc(alloc_size, GFP_KERNEL);
  2172. if (!d->outputs) {
  2173. ret = -ENOMEM;
  2174. goto deinit_info;
  2175. }
  2176. ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_OUTPUT);
  2177. if (ret)
  2178. goto deinit_info;
  2179. }
  2180. ret = ice_get_cgu_rclk_pin_info(&pf->hw, &d->base_rclk_idx,
  2181. &pf->dplls.rclk.num_parents);
  2182. if (ret)
  2183. return ret;
  2184. for (i = 0; i < pf->dplls.rclk.num_parents; i++)
  2185. pf->dplls.rclk.parent_idx[i] = d->base_rclk_idx + i;
  2186. ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_RCLK_INPUT);
  2187. if (ret)
  2188. return ret;
  2189. de->mode = DPLL_MODE_AUTOMATIC;
  2190. dp->mode = DPLL_MODE_AUTOMATIC;
  2191. dev_dbg(ice_pf_to_dev(pf),
  2192. "%s - success, inputs:%u, outputs:%u rclk-parents:%u\n",
  2193. __func__, d->num_inputs, d->num_outputs, d->rclk.num_parents);
  2194. return 0;
  2195. deinit_info:
  2196. dev_err(ice_pf_to_dev(pf),
  2197. "%s - fail: d->inputs:%p, de->input_prio:%p, dp->input_prio:%p, d->outputs:%p\n",
  2198. __func__, d->inputs, de->input_prio,
  2199. dp->input_prio, d->outputs);
  2200. ice_dpll_deinit_info(pf);
  2201. return ret;
  2202. }
  2203. /**
  2204. * ice_dpll_deinit - Disable the driver/HW support for dpll subsystem
  2205. * the dpll device.
  2206. * @pf: board private structure
  2207. *
  2208. * Handles the cleanup work required after dpll initialization, freeing
  2209. * resources and unregistering the dpll, pin and all resources used for
  2210. * handling them.
  2211. *
  2212. * Context: Destroys pf->dplls.lock mutex. Call only if ICE_FLAG_DPLL was set.
  2213. */
  2214. void ice_dpll_deinit(struct ice_pf *pf)
  2215. {
  2216. bool cgu = ice_is_feature_supported(pf, ICE_F_CGU);
  2217. clear_bit(ICE_FLAG_DPLL, pf->flags);
  2218. if (cgu)
  2219. ice_dpll_deinit_worker(pf);
  2220. ice_dpll_deinit_pins(pf, cgu);
  2221. ice_dpll_deinit_dpll(pf, &pf->dplls.pps, cgu);
  2222. ice_dpll_deinit_dpll(pf, &pf->dplls.eec, cgu);
  2223. ice_dpll_deinit_info(pf);
  2224. mutex_destroy(&pf->dplls.lock);
  2225. }
  2226. /**
  2227. * ice_dpll_init - initialize support for dpll subsystem
  2228. * @pf: board private structure
  2229. *
  2230. * Set up the device dplls, register them and pins connected within Linux dpll
  2231. * subsystem. Allow userspace to obtain state of DPLL and handling of DPLL
  2232. * configuration requests.
  2233. *
  2234. * Context: Initializes pf->dplls.lock mutex.
  2235. */
  2236. void ice_dpll_init(struct ice_pf *pf)
  2237. {
  2238. bool cgu = ice_is_feature_supported(pf, ICE_F_CGU);
  2239. struct ice_dplls *d = &pf->dplls;
  2240. int err = 0;
  2241. mutex_init(&d->lock);
  2242. err = ice_dpll_init_info(pf, cgu);
  2243. if (err)
  2244. goto err_exit;
  2245. err = ice_dpll_init_dpll(pf, &pf->dplls.eec, cgu, DPLL_TYPE_EEC);
  2246. if (err)
  2247. goto deinit_info;
  2248. err = ice_dpll_init_dpll(pf, &pf->dplls.pps, cgu, DPLL_TYPE_PPS);
  2249. if (err)
  2250. goto deinit_eec;
  2251. err = ice_dpll_init_pins(pf, cgu);
  2252. if (err)
  2253. goto deinit_pps;
  2254. if (cgu) {
  2255. err = ice_dpll_init_worker(pf);
  2256. if (err)
  2257. goto deinit_pins;
  2258. }
  2259. set_bit(ICE_FLAG_DPLL, pf->flags);
  2260. return;
  2261. deinit_pins:
  2262. ice_dpll_deinit_pins(pf, cgu);
  2263. deinit_pps:
  2264. ice_dpll_deinit_dpll(pf, &pf->dplls.pps, cgu);
  2265. deinit_eec:
  2266. ice_dpll_deinit_dpll(pf, &pf->dplls.eec, cgu);
  2267. deinit_info:
  2268. ice_dpll_deinit_info(pf);
  2269. err_exit:
  2270. mutex_destroy(&d->lock);
  2271. dev_warn(ice_pf_to_dev(pf), "DPLLs init failure err:%d\n", err);
  2272. }