am65-cpts.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* TI K3 AM65x Common Platform Time Sync
  3. *
  4. * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/err.h>
  10. #include <linux/if_vlan.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/net_tstamp.h>
  15. #include <linux/of.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/ptp_classify.h>
  20. #include <linux/ptp_clock_kernel.h>
  21. #include "am65-cpts.h"
  22. struct am65_genf_regs {
  23. u32 comp_lo; /* Comparison Low Value 0:31 */
  24. u32 comp_hi; /* Comparison High Value 32:63 */
  25. u32 control; /* control */
  26. u32 length; /* Length */
  27. u32 ppm_low; /* PPM Load Low Value 0:31 */
  28. u32 ppm_hi; /* PPM Load High Value 32:63 */
  29. u32 ts_nudge; /* Nudge value */
  30. } __aligned(32) __packed;
  31. #define AM65_CPTS_GENF_MAX_NUM 9
  32. #define AM65_CPTS_ESTF_MAX_NUM 8
  33. struct am65_cpts_regs {
  34. u32 idver; /* Identification and version */
  35. u32 control; /* Time sync control */
  36. u32 rftclk_sel; /* Reference Clock Select Register */
  37. u32 ts_push; /* Time stamp event push */
  38. u32 ts_load_val_lo; /* Time Stamp Load Low Value 0:31 */
  39. u32 ts_load_en; /* Time stamp load enable */
  40. u32 ts_comp_lo; /* Time Stamp Comparison Low Value 0:31 */
  41. u32 ts_comp_length; /* Time Stamp Comparison Length */
  42. u32 intstat_raw; /* Time sync interrupt status raw */
  43. u32 intstat_masked; /* Time sync interrupt status masked */
  44. u32 int_enable; /* Time sync interrupt enable */
  45. u32 ts_comp_nudge; /* Time Stamp Comparison Nudge Value */
  46. u32 event_pop; /* Event interrupt pop */
  47. u32 event_0; /* Event Time Stamp lo 0:31 */
  48. u32 event_1; /* Event Type Fields */
  49. u32 event_2; /* Event Type Fields domain */
  50. u32 event_3; /* Event Time Stamp hi 32:63 */
  51. u32 ts_load_val_hi; /* Time Stamp Load High Value 32:63 */
  52. u32 ts_comp_hi; /* Time Stamp Comparison High Value 32:63 */
  53. u32 ts_add_val; /* Time Stamp Add value */
  54. u32 ts_ppm_low; /* Time Stamp PPM Load Low Value 0:31 */
  55. u32 ts_ppm_hi; /* Time Stamp PPM Load High Value 32:63 */
  56. u32 ts_nudge; /* Time Stamp Nudge value */
  57. u32 reserv[33];
  58. struct am65_genf_regs genf[AM65_CPTS_GENF_MAX_NUM];
  59. struct am65_genf_regs estf[AM65_CPTS_ESTF_MAX_NUM];
  60. };
  61. /* CONTROL_REG */
  62. #define AM65_CPTS_CONTROL_EN BIT(0)
  63. #define AM65_CPTS_CONTROL_INT_TEST BIT(1)
  64. #define AM65_CPTS_CONTROL_TS_COMP_POLARITY BIT(2)
  65. #define AM65_CPTS_CONTROL_TSTAMP_EN BIT(3)
  66. #define AM65_CPTS_CONTROL_SEQUENCE_EN BIT(4)
  67. #define AM65_CPTS_CONTROL_64MODE BIT(5)
  68. #define AM65_CPTS_CONTROL_TS_COMP_TOG BIT(6)
  69. #define AM65_CPTS_CONTROL_TS_PPM_DIR BIT(7)
  70. #define AM65_CPTS_CONTROL_HW1_TS_PUSH_EN BIT(8)
  71. #define AM65_CPTS_CONTROL_HW2_TS_PUSH_EN BIT(9)
  72. #define AM65_CPTS_CONTROL_HW3_TS_PUSH_EN BIT(10)
  73. #define AM65_CPTS_CONTROL_HW4_TS_PUSH_EN BIT(11)
  74. #define AM65_CPTS_CONTROL_HW5_TS_PUSH_EN BIT(12)
  75. #define AM65_CPTS_CONTROL_HW6_TS_PUSH_EN BIT(13)
  76. #define AM65_CPTS_CONTROL_HW7_TS_PUSH_EN BIT(14)
  77. #define AM65_CPTS_CONTROL_HW8_TS_PUSH_EN BIT(15)
  78. #define AM65_CPTS_CONTROL_HW1_TS_PUSH_OFFSET (8)
  79. #define AM65_CPTS_CONTROL_TX_GENF_CLR_EN BIT(17)
  80. #define AM65_CPTS_CONTROL_TS_SYNC_SEL_MASK (0xF)
  81. #define AM65_CPTS_CONTROL_TS_SYNC_SEL_SHIFT (28)
  82. /* RFTCLK_SEL_REG */
  83. #define AM65_CPTS_RFTCLK_SEL_MASK (0x1F)
  84. /* TS_PUSH_REG */
  85. #define AM65_CPTS_TS_PUSH BIT(0)
  86. /* TS_LOAD_EN_REG */
  87. #define AM65_CPTS_TS_LOAD_EN BIT(0)
  88. /* INTSTAT_RAW_REG */
  89. #define AM65_CPTS_INTSTAT_RAW_TS_PEND BIT(0)
  90. /* INTSTAT_MASKED_REG */
  91. #define AM65_CPTS_INTSTAT_MASKED_TS_PEND BIT(0)
  92. /* INT_ENABLE_REG */
  93. #define AM65_CPTS_INT_ENABLE_TS_PEND_EN BIT(0)
  94. /* TS_COMP_NUDGE_REG */
  95. #define AM65_CPTS_TS_COMP_NUDGE_MASK (0xFF)
  96. /* EVENT_POP_REG */
  97. #define AM65_CPTS_EVENT_POP BIT(0)
  98. /* EVENT_1_REG */
  99. #define AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK GENMASK(15, 0)
  100. #define AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK GENMASK(19, 16)
  101. #define AM65_CPTS_EVENT_1_MESSAGE_TYPE_SHIFT (16)
  102. #define AM65_CPTS_EVENT_1_EVENT_TYPE_MASK GENMASK(23, 20)
  103. #define AM65_CPTS_EVENT_1_EVENT_TYPE_SHIFT (20)
  104. #define AM65_CPTS_EVENT_1_PORT_NUMBER_MASK GENMASK(28, 24)
  105. #define AM65_CPTS_EVENT_1_PORT_NUMBER_SHIFT (24)
  106. /* EVENT_2_REG */
  107. #define AM65_CPTS_EVENT_2_REG_DOMAIN_MASK (0xFF)
  108. #define AM65_CPTS_EVENT_2_REG_DOMAIN_SHIFT (0)
  109. enum {
  110. AM65_CPTS_EV_PUSH, /* Time Stamp Push Event */
  111. AM65_CPTS_EV_ROLL, /* Time Stamp Rollover Event */
  112. AM65_CPTS_EV_HALF, /* Time Stamp Half Rollover Event */
  113. AM65_CPTS_EV_HW, /* Hardware Time Stamp Push Event */
  114. AM65_CPTS_EV_RX, /* Ethernet Receive Event */
  115. AM65_CPTS_EV_TX, /* Ethernet Transmit Event */
  116. AM65_CPTS_EV_TS_COMP, /* Time Stamp Compare Event */
  117. AM65_CPTS_EV_HOST, /* Host Transmit Event */
  118. };
  119. struct am65_cpts_event {
  120. struct list_head list;
  121. unsigned long tmo;
  122. u32 event1;
  123. u32 event2;
  124. u64 timestamp;
  125. };
  126. #define AM65_CPTS_FIFO_DEPTH (16)
  127. #define AM65_CPTS_MAX_EVENTS (32)
  128. #define AM65_CPTS_EVENT_RX_TX_TIMEOUT (20) /* ms */
  129. #define AM65_CPTS_SKB_TX_WORK_TIMEOUT 1 /* jiffies */
  130. #define AM65_CPTS_MIN_PPM 0x400
  131. struct am65_cpts {
  132. struct device *dev;
  133. struct am65_cpts_regs __iomem *reg;
  134. struct ptp_clock_info ptp_info;
  135. struct ptp_clock *ptp_clock;
  136. int phc_index;
  137. struct clk_hw *clk_mux_hw;
  138. struct device_node *clk_mux_np;
  139. struct clk *refclk;
  140. u32 refclk_freq;
  141. /* separate lists to handle TX and RX timestamp independently */
  142. struct list_head events_tx;
  143. struct list_head events_rx;
  144. struct list_head pool;
  145. struct am65_cpts_event pool_data[AM65_CPTS_MAX_EVENTS];
  146. spinlock_t lock; /* protects events lists*/
  147. u32 ext_ts_inputs;
  148. u32 genf_num;
  149. u32 ts_add_val;
  150. int irq;
  151. struct mutex ptp_clk_lock; /* PHC access sync */
  152. u64 timestamp;
  153. u32 genf_enable;
  154. u32 hw_ts_enable;
  155. u32 estf_enable;
  156. struct sk_buff_head txq;
  157. bool pps_enabled;
  158. bool pps_present;
  159. u32 pps_hw_ts_idx;
  160. u32 pps_genf_idx;
  161. /* context save/restore */
  162. u64 sr_cpts_ns;
  163. u64 sr_ktime_ns;
  164. u32 sr_control;
  165. u32 sr_int_enable;
  166. u32 sr_rftclk_sel;
  167. u32 sr_ts_ppm_hi;
  168. u32 sr_ts_ppm_low;
  169. struct am65_genf_regs sr_genf[AM65_CPTS_GENF_MAX_NUM];
  170. struct am65_genf_regs sr_estf[AM65_CPTS_ESTF_MAX_NUM];
  171. };
  172. struct am65_cpts_skb_cb_data {
  173. unsigned long tmo;
  174. u32 skb_mtype_seqid;
  175. };
  176. #define am65_cpts_write32(c, v, r) writel(v, &(c)->reg->r)
  177. #define am65_cpts_read32(c, r) readl(&(c)->reg->r)
  178. static void am65_cpts_settime(struct am65_cpts *cpts, u64 start_tstamp)
  179. {
  180. u32 val;
  181. val = upper_32_bits(start_tstamp);
  182. am65_cpts_write32(cpts, val, ts_load_val_hi);
  183. val = lower_32_bits(start_tstamp);
  184. am65_cpts_write32(cpts, val, ts_load_val_lo);
  185. am65_cpts_write32(cpts, AM65_CPTS_TS_LOAD_EN, ts_load_en);
  186. }
  187. static void am65_cpts_set_add_val(struct am65_cpts *cpts)
  188. {
  189. /* select coefficient according to the rate */
  190. cpts->ts_add_val = (NSEC_PER_SEC / cpts->refclk_freq - 1) & 0x7;
  191. am65_cpts_write32(cpts, cpts->ts_add_val, ts_add_val);
  192. }
  193. static void am65_cpts_disable(struct am65_cpts *cpts)
  194. {
  195. am65_cpts_write32(cpts, 0, control);
  196. am65_cpts_write32(cpts, 0, int_enable);
  197. }
  198. static int am65_cpts_purge_event_list(struct am65_cpts *cpts,
  199. struct list_head *events)
  200. {
  201. struct list_head *this, *next;
  202. struct am65_cpts_event *event;
  203. int removed = 0;
  204. list_for_each_safe(this, next, events) {
  205. event = list_entry(this, struct am65_cpts_event, list);
  206. if (time_after(jiffies, event->tmo)) {
  207. list_del_init(&event->list);
  208. list_add(&event->list, &cpts->pool);
  209. ++removed;
  210. }
  211. }
  212. return removed;
  213. }
  214. static int am65_cpts_event_get_port(struct am65_cpts_event *event)
  215. {
  216. return (event->event1 & AM65_CPTS_EVENT_1_PORT_NUMBER_MASK) >>
  217. AM65_CPTS_EVENT_1_PORT_NUMBER_SHIFT;
  218. }
  219. static int am65_cpts_event_get_type(struct am65_cpts_event *event)
  220. {
  221. return (event->event1 & AM65_CPTS_EVENT_1_EVENT_TYPE_MASK) >>
  222. AM65_CPTS_EVENT_1_EVENT_TYPE_SHIFT;
  223. }
  224. static int am65_cpts_purge_events(struct am65_cpts *cpts)
  225. {
  226. int removed = 0;
  227. removed += am65_cpts_purge_event_list(cpts, &cpts->events_tx);
  228. removed += am65_cpts_purge_event_list(cpts, &cpts->events_rx);
  229. if (removed)
  230. dev_dbg(cpts->dev, "event pool cleaned up %d\n", removed);
  231. return removed ? 0 : -1;
  232. }
  233. static bool am65_cpts_fifo_pop_event(struct am65_cpts *cpts,
  234. struct am65_cpts_event *event)
  235. {
  236. u32 r = am65_cpts_read32(cpts, intstat_raw);
  237. if (r & AM65_CPTS_INTSTAT_RAW_TS_PEND) {
  238. event->timestamp = am65_cpts_read32(cpts, event_0);
  239. event->event1 = am65_cpts_read32(cpts, event_1);
  240. event->event2 = am65_cpts_read32(cpts, event_2);
  241. event->timestamp |= (u64)am65_cpts_read32(cpts, event_3) << 32;
  242. am65_cpts_write32(cpts, AM65_CPTS_EVENT_POP, event_pop);
  243. return false;
  244. }
  245. return true;
  246. }
  247. static int __am65_cpts_fifo_read(struct am65_cpts *cpts)
  248. {
  249. struct ptp_clock_event pevent;
  250. struct am65_cpts_event *event;
  251. bool schedule = false;
  252. int i, type, ret = 0;
  253. for (i = 0; i < AM65_CPTS_FIFO_DEPTH; i++) {
  254. event = list_first_entry_or_null(&cpts->pool,
  255. struct am65_cpts_event, list);
  256. if (!event) {
  257. if (am65_cpts_purge_events(cpts)) {
  258. dev_err(cpts->dev, "cpts: event pool empty\n");
  259. ret = -1;
  260. goto out;
  261. }
  262. continue;
  263. }
  264. if (am65_cpts_fifo_pop_event(cpts, event))
  265. break;
  266. type = am65_cpts_event_get_type(event);
  267. switch (type) {
  268. case AM65_CPTS_EV_PUSH:
  269. cpts->timestamp = event->timestamp;
  270. dev_dbg(cpts->dev, "AM65_CPTS_EV_PUSH t:%llu\n",
  271. cpts->timestamp);
  272. break;
  273. case AM65_CPTS_EV_RX:
  274. event->tmo = jiffies +
  275. msecs_to_jiffies(AM65_CPTS_EVENT_RX_TX_TIMEOUT);
  276. list_move_tail(&event->list, &cpts->events_rx);
  277. dev_dbg(cpts->dev,
  278. "AM65_CPTS_EV_RX e1:%08x e2:%08x t:%lld\n",
  279. event->event1, event->event2,
  280. event->timestamp);
  281. break;
  282. case AM65_CPTS_EV_TX:
  283. event->tmo = jiffies +
  284. msecs_to_jiffies(AM65_CPTS_EVENT_RX_TX_TIMEOUT);
  285. list_move_tail(&event->list, &cpts->events_tx);
  286. dev_dbg(cpts->dev,
  287. "AM65_CPTS_EV_TX e1:%08x e2:%08x t:%lld\n",
  288. event->event1, event->event2,
  289. event->timestamp);
  290. schedule = true;
  291. break;
  292. case AM65_CPTS_EV_HW:
  293. pevent.index = am65_cpts_event_get_port(event) - 1;
  294. pevent.timestamp = event->timestamp;
  295. if (cpts->pps_enabled && pevent.index == cpts->pps_hw_ts_idx) {
  296. pevent.type = PTP_CLOCK_PPSUSR;
  297. pevent.pps_times.ts_real = ns_to_timespec64(pevent.timestamp);
  298. } else {
  299. pevent.type = PTP_CLOCK_EXTTS;
  300. }
  301. dev_dbg(cpts->dev, "AM65_CPTS_EV_HW:%s p:%d t:%llu\n",
  302. pevent.type == PTP_CLOCK_EXTTS ?
  303. "extts" : "pps",
  304. pevent.index, event->timestamp);
  305. ptp_clock_event(cpts->ptp_clock, &pevent);
  306. break;
  307. case AM65_CPTS_EV_HOST:
  308. break;
  309. case AM65_CPTS_EV_ROLL:
  310. case AM65_CPTS_EV_HALF:
  311. case AM65_CPTS_EV_TS_COMP:
  312. dev_dbg(cpts->dev,
  313. "AM65_CPTS_EVT: %d e1:%08x e2:%08x t:%lld\n",
  314. type,
  315. event->event1, event->event2,
  316. event->timestamp);
  317. break;
  318. default:
  319. dev_err(cpts->dev, "cpts: unknown event type\n");
  320. ret = -1;
  321. goto out;
  322. }
  323. }
  324. out:
  325. if (schedule)
  326. ptp_schedule_worker(cpts->ptp_clock, 0);
  327. return ret;
  328. }
  329. static int am65_cpts_fifo_read(struct am65_cpts *cpts)
  330. {
  331. unsigned long flags;
  332. int ret = 0;
  333. spin_lock_irqsave(&cpts->lock, flags);
  334. ret = __am65_cpts_fifo_read(cpts);
  335. spin_unlock_irqrestore(&cpts->lock, flags);
  336. return ret;
  337. }
  338. static u64 am65_cpts_gettime(struct am65_cpts *cpts,
  339. struct ptp_system_timestamp *sts)
  340. {
  341. unsigned long flags;
  342. u64 val = 0;
  343. /* temporarily disable cpts interrupt to avoid intentional
  344. * doubled read. Interrupt can be in-flight - it's Ok.
  345. */
  346. am65_cpts_write32(cpts, 0, int_enable);
  347. /* use spin_lock_irqsave() here as it has to run very fast */
  348. spin_lock_irqsave(&cpts->lock, flags);
  349. ptp_read_system_prets(sts);
  350. am65_cpts_write32(cpts, AM65_CPTS_TS_PUSH, ts_push);
  351. am65_cpts_read32(cpts, ts_push);
  352. ptp_read_system_postts(sts);
  353. spin_unlock_irqrestore(&cpts->lock, flags);
  354. am65_cpts_fifo_read(cpts);
  355. am65_cpts_write32(cpts, AM65_CPTS_INT_ENABLE_TS_PEND_EN, int_enable);
  356. val = cpts->timestamp;
  357. return val;
  358. }
  359. static irqreturn_t am65_cpts_interrupt(int irq, void *dev_id)
  360. {
  361. struct am65_cpts *cpts = dev_id;
  362. if (am65_cpts_fifo_read(cpts))
  363. dev_dbg(cpts->dev, "cpts: unable to obtain a time stamp\n");
  364. return IRQ_HANDLED;
  365. }
  366. /* PTP clock operations */
  367. static int am65_cpts_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
  368. {
  369. struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info);
  370. u32 estf_ctrl_val = 0, estf_ppm_hi = 0, estf_ppm_low = 0;
  371. s32 ppb = scaled_ppm_to_ppb(scaled_ppm);
  372. int pps_index = cpts->pps_genf_idx;
  373. u64 adj_period, pps_adj_period;
  374. u32 ctrl_val, ppm_hi, ppm_low;
  375. unsigned long flags;
  376. int neg_adj = 0, i;
  377. if (ppb < 0) {
  378. neg_adj = 1;
  379. ppb = -ppb;
  380. }
  381. /* base freq = 1GHz = 1 000 000 000
  382. * ppb_norm = ppb * base_freq / clock_freq;
  383. * ppm_norm = ppb_norm / 1000
  384. * adj_period = 1 000 000 / ppm_norm
  385. * adj_period = 1 000 000 000 / ppb_norm
  386. * adj_period = 1 000 000 000 / (ppb * base_freq / clock_freq)
  387. * adj_period = (1 000 000 000 * clock_freq) / (ppb * base_freq)
  388. * adj_period = clock_freq / ppb
  389. */
  390. adj_period = div_u64(cpts->refclk_freq, ppb);
  391. mutex_lock(&cpts->ptp_clk_lock);
  392. ctrl_val = am65_cpts_read32(cpts, control);
  393. if (neg_adj)
  394. ctrl_val |= AM65_CPTS_CONTROL_TS_PPM_DIR;
  395. else
  396. ctrl_val &= ~AM65_CPTS_CONTROL_TS_PPM_DIR;
  397. ppm_hi = upper_32_bits(adj_period) & 0x3FF;
  398. ppm_low = lower_32_bits(adj_period);
  399. if (cpts->pps_enabled) {
  400. estf_ctrl_val = am65_cpts_read32(cpts, genf[pps_index].control);
  401. if (neg_adj)
  402. estf_ctrl_val &= ~BIT(1);
  403. else
  404. estf_ctrl_val |= BIT(1);
  405. /* GenF PPM will do correction using cpts refclk tick which is
  406. * (cpts->ts_add_val + 1) ns, so GenF length PPM adj period
  407. * need to be corrected.
  408. */
  409. pps_adj_period = adj_period * (cpts->ts_add_val + 1);
  410. estf_ppm_hi = upper_32_bits(pps_adj_period) & 0x3FF;
  411. estf_ppm_low = lower_32_bits(pps_adj_period);
  412. }
  413. spin_lock_irqsave(&cpts->lock, flags);
  414. /* All below writes must be done extremely fast:
  415. * - delay between PPM dir and PPM value changes can cause err due old
  416. * PPM correction applied in wrong direction
  417. * - delay between CPTS-clock PPM cfg and GenF PPM cfg can cause err
  418. * due CPTS-clock PPM working with new cfg while GenF PPM cfg still
  419. * with old for short period of time
  420. */
  421. am65_cpts_write32(cpts, ctrl_val, control);
  422. am65_cpts_write32(cpts, ppm_hi, ts_ppm_hi);
  423. am65_cpts_write32(cpts, ppm_low, ts_ppm_low);
  424. if (cpts->pps_enabled) {
  425. am65_cpts_write32(cpts, estf_ctrl_val, genf[pps_index].control);
  426. am65_cpts_write32(cpts, estf_ppm_hi, genf[pps_index].ppm_hi);
  427. am65_cpts_write32(cpts, estf_ppm_low, genf[pps_index].ppm_low);
  428. }
  429. for (i = 0; i < AM65_CPTS_ESTF_MAX_NUM; i++) {
  430. if (cpts->estf_enable & BIT(i)) {
  431. am65_cpts_write32(cpts, estf_ctrl_val, estf[i].control);
  432. am65_cpts_write32(cpts, estf_ppm_hi, estf[i].ppm_hi);
  433. am65_cpts_write32(cpts, estf_ppm_low, estf[i].ppm_low);
  434. }
  435. }
  436. /* All GenF/EstF can be updated here the same way */
  437. spin_unlock_irqrestore(&cpts->lock, flags);
  438. mutex_unlock(&cpts->ptp_clk_lock);
  439. return 0;
  440. }
  441. static int am65_cpts_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  442. {
  443. struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info);
  444. s64 ns;
  445. mutex_lock(&cpts->ptp_clk_lock);
  446. ns = am65_cpts_gettime(cpts, NULL);
  447. ns += delta;
  448. am65_cpts_settime(cpts, ns);
  449. mutex_unlock(&cpts->ptp_clk_lock);
  450. return 0;
  451. }
  452. static int am65_cpts_ptp_gettimex(struct ptp_clock_info *ptp,
  453. struct timespec64 *ts,
  454. struct ptp_system_timestamp *sts)
  455. {
  456. struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info);
  457. u64 ns;
  458. mutex_lock(&cpts->ptp_clk_lock);
  459. ns = am65_cpts_gettime(cpts, sts);
  460. mutex_unlock(&cpts->ptp_clk_lock);
  461. *ts = ns_to_timespec64(ns);
  462. return 0;
  463. }
  464. u64 am65_cpts_ns_gettime(struct am65_cpts *cpts)
  465. {
  466. u64 ns;
  467. /* reuse ptp_clk_lock as it serialize ts push */
  468. mutex_lock(&cpts->ptp_clk_lock);
  469. ns = am65_cpts_gettime(cpts, NULL);
  470. mutex_unlock(&cpts->ptp_clk_lock);
  471. return ns;
  472. }
  473. EXPORT_SYMBOL_GPL(am65_cpts_ns_gettime);
  474. static int am65_cpts_ptp_settime(struct ptp_clock_info *ptp,
  475. const struct timespec64 *ts)
  476. {
  477. struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info);
  478. u64 ns;
  479. ns = timespec64_to_ns(ts);
  480. mutex_lock(&cpts->ptp_clk_lock);
  481. am65_cpts_settime(cpts, ns);
  482. mutex_unlock(&cpts->ptp_clk_lock);
  483. return 0;
  484. }
  485. static void am65_cpts_extts_enable_hw(struct am65_cpts *cpts, u32 index, int on)
  486. {
  487. u32 v;
  488. v = am65_cpts_read32(cpts, control);
  489. if (on) {
  490. v |= BIT(AM65_CPTS_CONTROL_HW1_TS_PUSH_OFFSET + index);
  491. cpts->hw_ts_enable |= BIT(index);
  492. } else {
  493. v &= ~BIT(AM65_CPTS_CONTROL_HW1_TS_PUSH_OFFSET + index);
  494. cpts->hw_ts_enable &= ~BIT(index);
  495. }
  496. am65_cpts_write32(cpts, v, control);
  497. }
  498. static int am65_cpts_extts_enable(struct am65_cpts *cpts, u32 index, int on)
  499. {
  500. if (index >= cpts->ptp_info.n_ext_ts)
  501. return -ENXIO;
  502. if (cpts->pps_present && index == cpts->pps_hw_ts_idx)
  503. return -EINVAL;
  504. if (((cpts->hw_ts_enable & BIT(index)) >> index) == on)
  505. return 0;
  506. mutex_lock(&cpts->ptp_clk_lock);
  507. am65_cpts_extts_enable_hw(cpts, index, on);
  508. mutex_unlock(&cpts->ptp_clk_lock);
  509. dev_dbg(cpts->dev, "%s: ExtTS:%u %s\n",
  510. __func__, index, on ? "enabled" : "disabled");
  511. return 0;
  512. }
  513. int am65_cpts_estf_enable(struct am65_cpts *cpts, int idx,
  514. struct am65_cpts_estf_cfg *cfg)
  515. {
  516. u64 cycles;
  517. u32 val;
  518. cycles = cfg->ns_period * cpts->refclk_freq;
  519. cycles = DIV_ROUND_UP(cycles, NSEC_PER_SEC);
  520. if (cycles > U32_MAX)
  521. return -EINVAL;
  522. /* according to TRM should be zeroed */
  523. am65_cpts_write32(cpts, 0, estf[idx].length);
  524. val = upper_32_bits(cfg->ns_start);
  525. am65_cpts_write32(cpts, val, estf[idx].comp_hi);
  526. val = lower_32_bits(cfg->ns_start);
  527. am65_cpts_write32(cpts, val, estf[idx].comp_lo);
  528. val = lower_32_bits(cycles);
  529. am65_cpts_write32(cpts, val, estf[idx].length);
  530. am65_cpts_write32(cpts, 0, estf[idx].control);
  531. am65_cpts_write32(cpts, 0, estf[idx].ppm_hi);
  532. am65_cpts_write32(cpts, 0, estf[idx].ppm_low);
  533. cpts->estf_enable |= BIT(idx);
  534. dev_dbg(cpts->dev, "%s: ESTF:%u enabled\n", __func__, idx);
  535. return 0;
  536. }
  537. EXPORT_SYMBOL_GPL(am65_cpts_estf_enable);
  538. void am65_cpts_estf_disable(struct am65_cpts *cpts, int idx)
  539. {
  540. am65_cpts_write32(cpts, 0, estf[idx].length);
  541. cpts->estf_enable &= ~BIT(idx);
  542. dev_dbg(cpts->dev, "%s: ESTF:%u disabled\n", __func__, idx);
  543. }
  544. EXPORT_SYMBOL_GPL(am65_cpts_estf_disable);
  545. static void am65_cpts_perout_enable_hw(struct am65_cpts *cpts,
  546. struct ptp_perout_request *req, int on)
  547. {
  548. u64 ns_period, ns_start, cycles;
  549. struct timespec64 ts;
  550. u32 val;
  551. if (on) {
  552. ts.tv_sec = req->period.sec;
  553. ts.tv_nsec = req->period.nsec;
  554. ns_period = timespec64_to_ns(&ts);
  555. cycles = (ns_period * cpts->refclk_freq) / NSEC_PER_SEC;
  556. ts.tv_sec = req->start.sec;
  557. ts.tv_nsec = req->start.nsec;
  558. ns_start = timespec64_to_ns(&ts);
  559. val = upper_32_bits(ns_start);
  560. am65_cpts_write32(cpts, val, genf[req->index].comp_hi);
  561. val = lower_32_bits(ns_start);
  562. am65_cpts_write32(cpts, val, genf[req->index].comp_lo);
  563. val = lower_32_bits(cycles);
  564. am65_cpts_write32(cpts, val, genf[req->index].length);
  565. am65_cpts_write32(cpts, 0, genf[req->index].control);
  566. am65_cpts_write32(cpts, 0, genf[req->index].ppm_hi);
  567. am65_cpts_write32(cpts, 0, genf[req->index].ppm_low);
  568. cpts->genf_enable |= BIT(req->index);
  569. } else {
  570. am65_cpts_write32(cpts, 0, genf[req->index].length);
  571. cpts->genf_enable &= ~BIT(req->index);
  572. }
  573. }
  574. static int am65_cpts_perout_enable(struct am65_cpts *cpts,
  575. struct ptp_perout_request *req, int on)
  576. {
  577. if (req->index >= cpts->ptp_info.n_per_out)
  578. return -ENXIO;
  579. if (cpts->pps_present && req->index == cpts->pps_genf_idx)
  580. return -EINVAL;
  581. if (!!(cpts->genf_enable & BIT(req->index)) == !!on)
  582. return 0;
  583. mutex_lock(&cpts->ptp_clk_lock);
  584. am65_cpts_perout_enable_hw(cpts, req, on);
  585. mutex_unlock(&cpts->ptp_clk_lock);
  586. dev_dbg(cpts->dev, "%s: GenF:%u %s\n",
  587. __func__, req->index, on ? "enabled" : "disabled");
  588. return 0;
  589. }
  590. static int am65_cpts_pps_enable(struct am65_cpts *cpts, int on)
  591. {
  592. int ret = 0;
  593. struct timespec64 ts;
  594. struct ptp_clock_request rq;
  595. u64 ns;
  596. if (!cpts->pps_present)
  597. return -EINVAL;
  598. if (cpts->pps_enabled == !!on)
  599. return 0;
  600. mutex_lock(&cpts->ptp_clk_lock);
  601. if (on) {
  602. am65_cpts_extts_enable_hw(cpts, cpts->pps_hw_ts_idx, on);
  603. ns = am65_cpts_gettime(cpts, NULL);
  604. ts = ns_to_timespec64(ns);
  605. rq.perout.period.sec = 1;
  606. rq.perout.period.nsec = 0;
  607. rq.perout.start.sec = ts.tv_sec + 2;
  608. rq.perout.start.nsec = 0;
  609. rq.perout.index = cpts->pps_genf_idx;
  610. am65_cpts_perout_enable_hw(cpts, &rq.perout, on);
  611. cpts->pps_enabled = true;
  612. } else {
  613. rq.perout.index = cpts->pps_genf_idx;
  614. am65_cpts_perout_enable_hw(cpts, &rq.perout, on);
  615. am65_cpts_extts_enable_hw(cpts, cpts->pps_hw_ts_idx, on);
  616. cpts->pps_enabled = false;
  617. }
  618. mutex_unlock(&cpts->ptp_clk_lock);
  619. dev_dbg(cpts->dev, "%s: pps: %s\n",
  620. __func__, on ? "enabled" : "disabled");
  621. return ret;
  622. }
  623. static int am65_cpts_ptp_enable(struct ptp_clock_info *ptp,
  624. struct ptp_clock_request *rq, int on)
  625. {
  626. struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info);
  627. switch (rq->type) {
  628. case PTP_CLK_REQ_EXTTS:
  629. return am65_cpts_extts_enable(cpts, rq->extts.index, on);
  630. case PTP_CLK_REQ_PEROUT:
  631. return am65_cpts_perout_enable(cpts, &rq->perout, on);
  632. case PTP_CLK_REQ_PPS:
  633. return am65_cpts_pps_enable(cpts, on);
  634. default:
  635. break;
  636. }
  637. return -EOPNOTSUPP;
  638. }
  639. static long am65_cpts_ts_work(struct ptp_clock_info *ptp);
  640. static struct ptp_clock_info am65_ptp_info = {
  641. .owner = THIS_MODULE,
  642. .name = "CTPS timer",
  643. .adjfine = am65_cpts_ptp_adjfine,
  644. .adjtime = am65_cpts_ptp_adjtime,
  645. .gettimex64 = am65_cpts_ptp_gettimex,
  646. .settime64 = am65_cpts_ptp_settime,
  647. .enable = am65_cpts_ptp_enable,
  648. .do_aux_work = am65_cpts_ts_work,
  649. };
  650. static bool am65_cpts_match_tx_ts(struct am65_cpts *cpts,
  651. struct am65_cpts_event *event)
  652. {
  653. struct sk_buff_head txq_list;
  654. struct sk_buff *skb, *tmp;
  655. unsigned long flags;
  656. bool found = false;
  657. u32 mtype_seqid;
  658. mtype_seqid = event->event1 &
  659. (AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK |
  660. AM65_CPTS_EVENT_1_EVENT_TYPE_MASK |
  661. AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK);
  662. __skb_queue_head_init(&txq_list);
  663. spin_lock_irqsave(&cpts->txq.lock, flags);
  664. skb_queue_splice_init(&cpts->txq, &txq_list);
  665. spin_unlock_irqrestore(&cpts->txq.lock, flags);
  666. /* no need to grab txq.lock as access is always done under cpts->lock */
  667. skb_queue_walk_safe(&txq_list, skb, tmp) {
  668. struct skb_shared_hwtstamps ssh;
  669. struct am65_cpts_skb_cb_data *skb_cb =
  670. (struct am65_cpts_skb_cb_data *)skb->cb;
  671. if ((ptp_classify_raw(skb) & PTP_CLASS_V1) &&
  672. ((mtype_seqid & AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK) ==
  673. (skb_cb->skb_mtype_seqid & AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK)))
  674. mtype_seqid = skb_cb->skb_mtype_seqid;
  675. if (mtype_seqid == skb_cb->skb_mtype_seqid) {
  676. u64 ns = event->timestamp;
  677. memset(&ssh, 0, sizeof(ssh));
  678. ssh.hwtstamp = ns_to_ktime(ns);
  679. skb_tstamp_tx(skb, &ssh);
  680. found = true;
  681. __skb_unlink(skb, &txq_list);
  682. dev_consume_skb_any(skb);
  683. dev_dbg(cpts->dev,
  684. "match tx timestamp mtype_seqid %08x\n",
  685. mtype_seqid);
  686. break;
  687. }
  688. if (time_after(jiffies, skb_cb->tmo)) {
  689. /* timeout any expired skbs over 100 ms */
  690. dev_dbg(cpts->dev,
  691. "expiring tx timestamp mtype_seqid %08x\n",
  692. mtype_seqid);
  693. __skb_unlink(skb, &txq_list);
  694. dev_consume_skb_any(skb);
  695. }
  696. }
  697. spin_lock_irqsave(&cpts->txq.lock, flags);
  698. skb_queue_splice(&txq_list, &cpts->txq);
  699. spin_unlock_irqrestore(&cpts->txq.lock, flags);
  700. return found;
  701. }
  702. static void am65_cpts_find_tx_ts(struct am65_cpts *cpts)
  703. {
  704. struct am65_cpts_event *event;
  705. struct list_head *this, *next;
  706. LIST_HEAD(events_free);
  707. unsigned long flags;
  708. LIST_HEAD(events);
  709. spin_lock_irqsave(&cpts->lock, flags);
  710. list_splice_init(&cpts->events_tx, &events);
  711. spin_unlock_irqrestore(&cpts->lock, flags);
  712. list_for_each_safe(this, next, &events) {
  713. event = list_entry(this, struct am65_cpts_event, list);
  714. if (am65_cpts_match_tx_ts(cpts, event) ||
  715. time_after(jiffies, event->tmo)) {
  716. list_del_init(&event->list);
  717. list_add(&event->list, &events_free);
  718. }
  719. }
  720. spin_lock_irqsave(&cpts->lock, flags);
  721. list_splice_tail(&events, &cpts->events_tx);
  722. list_splice_tail(&events_free, &cpts->pool);
  723. spin_unlock_irqrestore(&cpts->lock, flags);
  724. }
  725. static long am65_cpts_ts_work(struct ptp_clock_info *ptp)
  726. {
  727. struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info);
  728. unsigned long flags;
  729. long delay = -1;
  730. am65_cpts_find_tx_ts(cpts);
  731. spin_lock_irqsave(&cpts->txq.lock, flags);
  732. if (!skb_queue_empty(&cpts->txq))
  733. delay = AM65_CPTS_SKB_TX_WORK_TIMEOUT;
  734. spin_unlock_irqrestore(&cpts->txq.lock, flags);
  735. return delay;
  736. }
  737. static int am65_skb_get_mtype_seqid(struct sk_buff *skb, u32 *mtype_seqid)
  738. {
  739. unsigned int ptp_class = ptp_classify_raw(skb);
  740. struct ptp_header *hdr;
  741. u8 msgtype;
  742. u16 seqid;
  743. if (ptp_class == PTP_CLASS_NONE)
  744. return 0;
  745. hdr = ptp_parse_header(skb, ptp_class);
  746. if (!hdr)
  747. return 0;
  748. msgtype = ptp_get_msgtype(hdr, ptp_class);
  749. seqid = ntohs(hdr->sequence_id);
  750. *mtype_seqid = (msgtype << AM65_CPTS_EVENT_1_MESSAGE_TYPE_SHIFT) &
  751. AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK;
  752. *mtype_seqid |= (seqid & AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK);
  753. return 1;
  754. }
  755. static u64 am65_cpts_find_rx_ts(struct am65_cpts *cpts, u32 skb_mtype_seqid)
  756. {
  757. struct list_head *this, *next;
  758. struct am65_cpts_event *event;
  759. unsigned long flags;
  760. u32 mtype_seqid;
  761. u64 ns = 0;
  762. spin_lock_irqsave(&cpts->lock, flags);
  763. __am65_cpts_fifo_read(cpts);
  764. list_for_each_safe(this, next, &cpts->events_rx) {
  765. event = list_entry(this, struct am65_cpts_event, list);
  766. if (time_after(jiffies, event->tmo)) {
  767. list_move(&event->list, &cpts->pool);
  768. continue;
  769. }
  770. mtype_seqid = event->event1 &
  771. (AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK |
  772. AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK |
  773. AM65_CPTS_EVENT_1_EVENT_TYPE_MASK);
  774. if (mtype_seqid == skb_mtype_seqid) {
  775. ns = event->timestamp;
  776. list_move(&event->list, &cpts->pool);
  777. break;
  778. }
  779. }
  780. spin_unlock_irqrestore(&cpts->lock, flags);
  781. return ns;
  782. }
  783. void am65_cpts_rx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb)
  784. {
  785. struct am65_cpts_skb_cb_data *skb_cb = (struct am65_cpts_skb_cb_data *)skb->cb;
  786. struct skb_shared_hwtstamps *ssh;
  787. int ret;
  788. u64 ns;
  789. /* am65_cpts_rx_timestamp() is called before eth_type_trans(), so
  790. * skb MAC Hdr properties are not configured yet. Hence need to
  791. * reset skb MAC header here
  792. */
  793. skb_reset_mac_header(skb);
  794. ret = am65_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid);
  795. if (!ret)
  796. return; /* if not PTP class packet */
  797. skb_cb->skb_mtype_seqid |= (AM65_CPTS_EV_RX << AM65_CPTS_EVENT_1_EVENT_TYPE_SHIFT);
  798. dev_dbg(cpts->dev, "%s mtype seqid %08x\n", __func__, skb_cb->skb_mtype_seqid);
  799. ns = am65_cpts_find_rx_ts(cpts, skb_cb->skb_mtype_seqid);
  800. if (!ns)
  801. return;
  802. ssh = skb_hwtstamps(skb);
  803. memset(ssh, 0, sizeof(*ssh));
  804. ssh->hwtstamp = ns_to_ktime(ns);
  805. }
  806. EXPORT_SYMBOL_GPL(am65_cpts_rx_timestamp);
  807. /**
  808. * am65_cpts_tx_timestamp - save tx packet for timestamping
  809. * @cpts: cpts handle
  810. * @skb: packet
  811. *
  812. * This functions saves tx packet for timestamping if packet can be timestamped.
  813. * The future processing is done in from PTP auxiliary worker.
  814. */
  815. void am65_cpts_tx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb)
  816. {
  817. struct am65_cpts_skb_cb_data *skb_cb = (void *)skb->cb;
  818. if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  819. return;
  820. /* add frame to queue for processing later.
  821. * The periodic FIFO check will handle this.
  822. */
  823. skb_get(skb);
  824. /* get the timestamp for timeouts */
  825. skb_cb->tmo = jiffies + msecs_to_jiffies(100);
  826. skb_queue_tail(&cpts->txq, skb);
  827. ptp_schedule_worker(cpts->ptp_clock, 0);
  828. }
  829. EXPORT_SYMBOL_GPL(am65_cpts_tx_timestamp);
  830. /**
  831. * am65_cpts_prep_tx_timestamp - check and prepare tx packet for timestamping
  832. * @cpts: cpts handle
  833. * @skb: packet
  834. *
  835. * This functions should be called from .xmit().
  836. * It checks if packet can be timestamped, fills internal cpts data
  837. * in skb-cb and marks packet as SKBTX_IN_PROGRESS.
  838. */
  839. void am65_cpts_prep_tx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb)
  840. {
  841. struct am65_cpts_skb_cb_data *skb_cb = (void *)skb->cb;
  842. int ret;
  843. if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
  844. return;
  845. ret = am65_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid);
  846. if (!ret)
  847. return;
  848. skb_cb->skb_mtype_seqid |= (AM65_CPTS_EV_TX <<
  849. AM65_CPTS_EVENT_1_EVENT_TYPE_SHIFT);
  850. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  851. }
  852. EXPORT_SYMBOL_GPL(am65_cpts_prep_tx_timestamp);
  853. int am65_cpts_phc_index(struct am65_cpts *cpts)
  854. {
  855. return cpts->phc_index;
  856. }
  857. EXPORT_SYMBOL_GPL(am65_cpts_phc_index);
  858. static void cpts_free_clk_mux(void *data)
  859. {
  860. struct am65_cpts *cpts = data;
  861. of_clk_del_provider(cpts->clk_mux_np);
  862. clk_hw_unregister_mux(cpts->clk_mux_hw);
  863. of_node_put(cpts->clk_mux_np);
  864. }
  865. static int cpts_of_mux_clk_setup(struct am65_cpts *cpts,
  866. struct device_node *node)
  867. {
  868. unsigned int num_parents;
  869. const char **parent_names;
  870. char *clk_mux_name;
  871. void __iomem *reg;
  872. int ret = -EINVAL;
  873. cpts->clk_mux_np = of_get_child_by_name(node, "refclk-mux");
  874. if (!cpts->clk_mux_np)
  875. return 0;
  876. num_parents = of_clk_get_parent_count(cpts->clk_mux_np);
  877. if (num_parents < 1) {
  878. dev_err(cpts->dev, "mux-clock %pOF must have parents\n",
  879. cpts->clk_mux_np);
  880. goto mux_fail;
  881. }
  882. parent_names = devm_kcalloc(cpts->dev, sizeof(char *), num_parents,
  883. GFP_KERNEL);
  884. if (!parent_names) {
  885. ret = -ENOMEM;
  886. goto mux_fail;
  887. }
  888. of_clk_parent_fill(cpts->clk_mux_np, parent_names, num_parents);
  889. clk_mux_name = devm_kasprintf(cpts->dev, GFP_KERNEL, "%s.%pOFn",
  890. dev_name(cpts->dev), cpts->clk_mux_np);
  891. if (!clk_mux_name) {
  892. ret = -ENOMEM;
  893. goto mux_fail;
  894. }
  895. reg = &cpts->reg->rftclk_sel;
  896. /* dev must be NULL to avoid recursive incrementing
  897. * of module refcnt
  898. */
  899. cpts->clk_mux_hw = clk_hw_register_mux(NULL, clk_mux_name,
  900. parent_names, num_parents,
  901. 0, reg, 0, 5, 0, NULL);
  902. if (IS_ERR(cpts->clk_mux_hw)) {
  903. ret = PTR_ERR(cpts->clk_mux_hw);
  904. goto mux_fail;
  905. }
  906. ret = of_clk_add_hw_provider(cpts->clk_mux_np, of_clk_hw_simple_get,
  907. cpts->clk_mux_hw);
  908. if (ret)
  909. goto clk_hw_register;
  910. ret = devm_add_action_or_reset(cpts->dev, cpts_free_clk_mux, cpts);
  911. if (ret)
  912. dev_err(cpts->dev, "failed to add clkmux reset action %d", ret);
  913. return ret;
  914. clk_hw_register:
  915. clk_hw_unregister_mux(cpts->clk_mux_hw);
  916. mux_fail:
  917. of_node_put(cpts->clk_mux_np);
  918. return ret;
  919. }
  920. static int am65_cpts_of_parse(struct am65_cpts *cpts, struct device_node *node)
  921. {
  922. u32 prop[2];
  923. if (!of_property_read_u32(node, "ti,cpts-ext-ts-inputs", &prop[0]))
  924. cpts->ext_ts_inputs = prop[0];
  925. if (!of_property_read_u32(node, "ti,cpts-periodic-outputs", &prop[0]))
  926. cpts->genf_num = prop[0];
  927. if (!of_property_read_u32_array(node, "ti,pps", prop, 2)) {
  928. cpts->pps_present = true;
  929. if (prop[0] > 7) {
  930. dev_err(cpts->dev, "invalid HWx_TS_PUSH index: %u provided\n", prop[0]);
  931. cpts->pps_present = false;
  932. }
  933. if (prop[1] > 1) {
  934. dev_err(cpts->dev, "invalid GENFy index: %u provided\n", prop[1]);
  935. cpts->pps_present = false;
  936. }
  937. if (cpts->pps_present) {
  938. cpts->pps_hw_ts_idx = prop[0];
  939. cpts->pps_genf_idx = prop[1];
  940. }
  941. }
  942. return cpts_of_mux_clk_setup(cpts, node);
  943. }
  944. void am65_cpts_release(struct am65_cpts *cpts)
  945. {
  946. ptp_clock_unregister(cpts->ptp_clock);
  947. am65_cpts_disable(cpts);
  948. clk_disable_unprepare(cpts->refclk);
  949. }
  950. EXPORT_SYMBOL_GPL(am65_cpts_release);
  951. struct am65_cpts *am65_cpts_create(struct device *dev, void __iomem *regs,
  952. struct device_node *node)
  953. {
  954. struct am65_cpts *cpts;
  955. int ret, i;
  956. cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL);
  957. if (!cpts)
  958. return ERR_PTR(-ENOMEM);
  959. cpts->dev = dev;
  960. cpts->reg = (struct am65_cpts_regs __iomem *)regs;
  961. cpts->irq = of_irq_get_byname(node, "cpts");
  962. if (cpts->irq <= 0) {
  963. ret = cpts->irq ?: -ENXIO;
  964. dev_err_probe(dev, ret, "Failed to get IRQ number\n");
  965. return ERR_PTR(ret);
  966. }
  967. ret = am65_cpts_of_parse(cpts, node);
  968. if (ret)
  969. return ERR_PTR(ret);
  970. mutex_init(&cpts->ptp_clk_lock);
  971. INIT_LIST_HEAD(&cpts->events_tx);
  972. INIT_LIST_HEAD(&cpts->events_rx);
  973. INIT_LIST_HEAD(&cpts->pool);
  974. spin_lock_init(&cpts->lock);
  975. skb_queue_head_init(&cpts->txq);
  976. for (i = 0; i < AM65_CPTS_MAX_EVENTS; i++)
  977. list_add(&cpts->pool_data[i].list, &cpts->pool);
  978. cpts->refclk = devm_get_clk_from_child(dev, node, "cpts");
  979. if (IS_ERR(cpts->refclk)) {
  980. ret = PTR_ERR(cpts->refclk);
  981. dev_err_probe(dev, ret, "Failed to get refclk\n");
  982. return ERR_PTR(ret);
  983. }
  984. ret = clk_prepare_enable(cpts->refclk);
  985. if (ret) {
  986. dev_err(dev, "Failed to enable refclk %d\n", ret);
  987. return ERR_PTR(ret);
  988. }
  989. cpts->refclk_freq = clk_get_rate(cpts->refclk);
  990. am65_ptp_info.max_adj = cpts->refclk_freq / AM65_CPTS_MIN_PPM;
  991. cpts->ptp_info = am65_ptp_info;
  992. if (cpts->ext_ts_inputs)
  993. cpts->ptp_info.n_ext_ts = cpts->ext_ts_inputs;
  994. if (cpts->genf_num)
  995. cpts->ptp_info.n_per_out = cpts->genf_num;
  996. if (cpts->pps_present)
  997. cpts->ptp_info.pps = 1;
  998. am65_cpts_set_add_val(cpts);
  999. am65_cpts_write32(cpts, AM65_CPTS_CONTROL_EN |
  1000. AM65_CPTS_CONTROL_64MODE |
  1001. AM65_CPTS_CONTROL_TX_GENF_CLR_EN,
  1002. control);
  1003. am65_cpts_write32(cpts, AM65_CPTS_INT_ENABLE_TS_PEND_EN, int_enable);
  1004. /* set time to the current system time */
  1005. am65_cpts_settime(cpts, ktime_to_ns(ktime_get_real()));
  1006. cpts->ptp_clock = ptp_clock_register(&cpts->ptp_info, cpts->dev);
  1007. if (IS_ERR_OR_NULL(cpts->ptp_clock)) {
  1008. dev_err(dev, "Failed to register ptp clk %ld\n",
  1009. PTR_ERR(cpts->ptp_clock));
  1010. ret = cpts->ptp_clock ? PTR_ERR(cpts->ptp_clock) : -ENODEV;
  1011. goto refclk_disable;
  1012. }
  1013. cpts->phc_index = ptp_clock_index(cpts->ptp_clock);
  1014. ret = devm_request_threaded_irq(dev, cpts->irq, NULL,
  1015. am65_cpts_interrupt,
  1016. IRQF_ONESHOT, dev_name(dev), cpts);
  1017. if (ret < 0) {
  1018. dev_err(cpts->dev, "error attaching irq %d\n", ret);
  1019. goto reset_ptpclk;
  1020. }
  1021. dev_info(dev, "CPTS ver 0x%08x, freq:%u, add_val:%u pps:%d\n",
  1022. am65_cpts_read32(cpts, idver),
  1023. cpts->refclk_freq, cpts->ts_add_val, cpts->pps_present);
  1024. return cpts;
  1025. reset_ptpclk:
  1026. am65_cpts_release(cpts);
  1027. refclk_disable:
  1028. clk_disable_unprepare(cpts->refclk);
  1029. return ERR_PTR(ret);
  1030. }
  1031. EXPORT_SYMBOL_GPL(am65_cpts_create);
  1032. void am65_cpts_suspend(struct am65_cpts *cpts)
  1033. {
  1034. /* save state and disable CPTS */
  1035. cpts->sr_control = am65_cpts_read32(cpts, control);
  1036. cpts->sr_int_enable = am65_cpts_read32(cpts, int_enable);
  1037. cpts->sr_rftclk_sel = am65_cpts_read32(cpts, rftclk_sel);
  1038. cpts->sr_ts_ppm_hi = am65_cpts_read32(cpts, ts_ppm_hi);
  1039. cpts->sr_ts_ppm_low = am65_cpts_read32(cpts, ts_ppm_low);
  1040. cpts->sr_cpts_ns = am65_cpts_gettime(cpts, NULL);
  1041. cpts->sr_ktime_ns = ktime_to_ns(ktime_get_real());
  1042. am65_cpts_disable(cpts);
  1043. clk_disable(cpts->refclk);
  1044. /* Save GENF state */
  1045. memcpy_fromio(&cpts->sr_genf, &cpts->reg->genf, sizeof(cpts->sr_genf));
  1046. /* Save ESTF state */
  1047. memcpy_fromio(&cpts->sr_estf, &cpts->reg->estf, sizeof(cpts->sr_estf));
  1048. }
  1049. EXPORT_SYMBOL_GPL(am65_cpts_suspend);
  1050. void am65_cpts_resume(struct am65_cpts *cpts)
  1051. {
  1052. int i;
  1053. s64 ktime_ns;
  1054. /* restore state and enable CPTS */
  1055. clk_enable(cpts->refclk);
  1056. am65_cpts_write32(cpts, cpts->sr_rftclk_sel, rftclk_sel);
  1057. am65_cpts_set_add_val(cpts);
  1058. am65_cpts_write32(cpts, cpts->sr_control, control);
  1059. am65_cpts_write32(cpts, cpts->sr_int_enable, int_enable);
  1060. /* Restore time to saved CPTS time + time in suspend/resume */
  1061. ktime_ns = ktime_to_ns(ktime_get_real());
  1062. ktime_ns -= cpts->sr_ktime_ns;
  1063. am65_cpts_settime(cpts, cpts->sr_cpts_ns + ktime_ns);
  1064. /* Restore compensation (PPM) */
  1065. am65_cpts_write32(cpts, cpts->sr_ts_ppm_hi, ts_ppm_hi);
  1066. am65_cpts_write32(cpts, cpts->sr_ts_ppm_low, ts_ppm_low);
  1067. /* Restore GENF state */
  1068. for (i = 0; i < AM65_CPTS_GENF_MAX_NUM; i++) {
  1069. am65_cpts_write32(cpts, 0, genf[i].length); /* TRM sequence */
  1070. am65_cpts_write32(cpts, cpts->sr_genf[i].comp_hi, genf[i].comp_hi);
  1071. am65_cpts_write32(cpts, cpts->sr_genf[i].comp_lo, genf[i].comp_lo);
  1072. am65_cpts_write32(cpts, cpts->sr_genf[i].length, genf[i].length);
  1073. am65_cpts_write32(cpts, cpts->sr_genf[i].control, genf[i].control);
  1074. am65_cpts_write32(cpts, cpts->sr_genf[i].ppm_hi, genf[i].ppm_hi);
  1075. am65_cpts_write32(cpts, cpts->sr_genf[i].ppm_low, genf[i].ppm_low);
  1076. }
  1077. /* Restore ESTTF state */
  1078. for (i = 0; i < AM65_CPTS_ESTF_MAX_NUM; i++) {
  1079. am65_cpts_write32(cpts, 0, estf[i].length); /* TRM sequence */
  1080. am65_cpts_write32(cpts, cpts->sr_estf[i].comp_hi, estf[i].comp_hi);
  1081. am65_cpts_write32(cpts, cpts->sr_estf[i].comp_lo, estf[i].comp_lo);
  1082. am65_cpts_write32(cpts, cpts->sr_estf[i].length, estf[i].length);
  1083. am65_cpts_write32(cpts, cpts->sr_estf[i].control, estf[i].control);
  1084. am65_cpts_write32(cpts, cpts->sr_estf[i].ppm_hi, estf[i].ppm_hi);
  1085. am65_cpts_write32(cpts, cpts->sr_estf[i].ppm_low, estf[i].ppm_low);
  1086. }
  1087. }
  1088. EXPORT_SYMBOL_GPL(am65_cpts_resume);
  1089. static int am65_cpts_probe(struct platform_device *pdev)
  1090. {
  1091. struct device_node *node = pdev->dev.of_node;
  1092. struct device *dev = &pdev->dev;
  1093. struct am65_cpts *cpts;
  1094. void __iomem *base;
  1095. base = devm_platform_ioremap_resource_byname(pdev, "cpts");
  1096. if (IS_ERR(base))
  1097. return PTR_ERR(base);
  1098. cpts = am65_cpts_create(dev, base, node);
  1099. return PTR_ERR_OR_ZERO(cpts);
  1100. }
  1101. static const struct of_device_id am65_cpts_of_match[] = {
  1102. { .compatible = "ti,am65-cpts", },
  1103. { .compatible = "ti,j721e-cpts", },
  1104. {},
  1105. };
  1106. MODULE_DEVICE_TABLE(of, am65_cpts_of_match);
  1107. static struct platform_driver am65_cpts_driver = {
  1108. .probe = am65_cpts_probe,
  1109. .driver = {
  1110. .name = "am65-cpts",
  1111. .of_match_table = am65_cpts_of_match,
  1112. },
  1113. };
  1114. module_platform_driver(am65_cpts_driver);
  1115. MODULE_LICENSE("GPL v2");
  1116. MODULE_AUTHOR("Grygorii Strashko <grygorii.strashko@ti.com>");
  1117. MODULE_DESCRIPTION("TI K3 AM65 CPTS driver");