dwmac-qcom-pps.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2023-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  3. */
  4. #include <linux/module.h>
  5. #include <linux/of.h>
  6. #include <linux/of_device.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/phy.h>
  9. #include <linux/regulator/consumer.h>
  10. #include <linux/of_gpio.h>
  11. #include <linux/io.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/mii.h>
  14. #include <linux/of_mdio.h>
  15. #include <linux/slab.h>
  16. #include <linux/ipc_logging.h>
  17. #include <linux/poll.h>
  18. #include <linux/debugfs.h>
  19. #include "stmmac.h"
  20. #include "stmmac_platform.h"
  21. #include "stmmac_ptp.h"
  22. #include "dwmac-qcom-ethqos.h"
  23. extern struct qcom_ethqos *pethqos;
  24. static bool avb_class_a_msg_wq_flag;
  25. static bool avb_class_b_msg_wq_flag;
  26. static DECLARE_WAIT_QUEUE_HEAD(avb_class_a_msg_wq);
  27. static DECLARE_WAIT_QUEUE_HEAD(avb_class_b_msg_wq);
  28. static int strlcmp(const char *s, const char *t, size_t n)
  29. {
  30. int ret;
  31. while (n-- && *t != '\0') {
  32. if (*s != *t) {
  33. ret = ((unsigned char)*s - (unsigned char)*t);
  34. n = 0;
  35. } else {
  36. ++s, ++t;
  37. ret = (unsigned char)*s;
  38. }
  39. }
  40. return ret;
  41. }
  42. static u32 pps_config_sub_second_increment(void __iomem *ioaddr,
  43. u32 ptp_clock, int gmac4)
  44. {
  45. u32 value = readl_relaxed(ioaddr + PTP_TCR);
  46. unsigned long data;
  47. unsigned int sns_inc = 0;
  48. u32 reg_value;
  49. u32 reg_value2;
  50. /* For GMAC3.x, 4.x versions, convert the ptp_clock to nano second
  51. * formula = (1/ptp_clock) * 1000000000
  52. * where ptp_clock is 50MHz if fine method is used to update system
  53. */
  54. if (value & PTP_TCR_TSCFUPDT) {
  55. data = (1000000000ULL / ptp_clock);
  56. sns_inc = 1000000000ull - (data * ptp_clock);
  57. sns_inc = (sns_inc * 256) / ptp_clock;
  58. } else {
  59. data = (1000000000ULL / ptp_clock);
  60. }
  61. /* 0.465ns accuracy */
  62. if (!(value & PTP_TCR_TSCTRLSSR))
  63. data = (data * 1000) / 465;
  64. data &= PTP_SSIR_SSINC_MAX;
  65. reg_value = data;
  66. if (gmac4)
  67. reg_value <<= GMAC4_PTP_SSIR_SSINC_SHIFT;
  68. sns_inc &= PTP_SSIR_SNSINC_MASK;
  69. reg_value2 = sns_inc;
  70. if (gmac4)
  71. reg_value2 <<= GMAC4_PTP_SSIR_SNSINC_SHIFT;
  72. writel_relaxed(reg_value + reg_value2, ioaddr + PTP_SSIR);
  73. return data;
  74. }
  75. static u32 pps_config_default_addend(void __iomem *ioaddr,
  76. struct stmmac_priv *priv, u32 ptp_clock)
  77. {
  78. u64 temp;
  79. /* formula is :
  80. * addend = 2^32/freq_div_ratio;
  81. *
  82. * where, freq_div_ratio = DWC_ETH_QOS_SYSCLOCK/50MHz
  83. *
  84. * hence, addend = ((2^32) * 50MHz)/DWC_ETH_QOS_SYSCLOCK;
  85. *
  86. * NOTE: DWC_ETH_QOS_SYSCLOCK should be >= 50MHz to
  87. * achive 20ns accuracy.
  88. *
  89. * 2^x * y == (y << x), hence
  90. * 2^32 * 50000000 ==> (50000000 << 32)
  91. */
  92. if (ptp_clock == 250000000) {
  93. // If PTP_CLOCK == SYS_CLOCK, best we can do is 2^32 - 1
  94. priv->default_addend = 0xFFFFFFFF;
  95. } else {
  96. temp = (u64)((u64)ptp_clock << 32);
  97. priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
  98. }
  99. priv->hw->ptp->config_addend(ioaddr, priv->default_addend);
  100. return 1;
  101. }
  102. int ppsout_stop(struct stmmac_priv *priv, struct pps_cfg *eth_pps_cfg)
  103. {
  104. u32 val;
  105. void __iomem *ioaddr = priv->ioaddr;
  106. val = readl_relaxed(ioaddr + MAC_PPS_CONTROL);
  107. val |= PPSCMDX(eth_pps_cfg->ppsout_ch, 0x5);
  108. val |= TRGTMODSELX(eth_pps_cfg->ppsout_ch, 0x3);
  109. val |= PPSEN0;
  110. writel_relaxed(val, ioaddr + MAC_PPS_CONTROL);
  111. return 0;
  112. }
  113. static irqreturn_t ethqos_pps_avb_class_a(int irq, void *dev_id)
  114. {
  115. struct stmmac_priv *priv =
  116. (struct stmmac_priv *)dev_id;
  117. struct qcom_ethqos *ethqos = priv->plat->bsp_priv;
  118. ethqos->avb_class_a_intr_cnt++;
  119. avb_class_a_msg_wq_flag = true;
  120. wake_up_interruptible(&avb_class_a_msg_wq);
  121. return IRQ_HANDLED;
  122. }
  123. static irqreturn_t ethqos_pps_avb_class_b(int irq, void *dev_id)
  124. {
  125. struct stmmac_priv *priv =
  126. (struct stmmac_priv *)dev_id;
  127. struct qcom_ethqos *ethqos = priv->plat->bsp_priv;
  128. ethqos->avb_class_b_intr_cnt++;
  129. avb_class_b_msg_wq_flag = true;
  130. wake_up_interruptible(&avb_class_b_msg_wq);
  131. return IRQ_HANDLED;
  132. }
  133. static void ethqos_register_pps_isr(struct stmmac_priv *priv, int ch)
  134. {
  135. int ret;
  136. struct qcom_ethqos *ethqos = priv->plat->bsp_priv;
  137. if (ch == DWC_ETH_QOS_PPS_CH_2) {
  138. ret = request_irq(ethqos->pps_class_a_irq,
  139. ethqos_pps_avb_class_a,
  140. IRQF_TRIGGER_RISING, "stmmac_pps", priv);
  141. if (ret)
  142. ETHQOSERR("pps_avb_class_a_irq Failed ret=%d\n", ret);
  143. else
  144. ETHQOSDBG("pps_avb_class_a_irq pass\n");
  145. } else if (ch == DWC_ETH_QOS_PPS_CH_3) {
  146. ret = request_irq(ethqos->pps_class_b_irq,
  147. ethqos_pps_avb_class_b,
  148. IRQF_TRIGGER_RISING, "stmmac_pps", priv);
  149. if (ret)
  150. ETHQOSERR("pps_avb_class_b_irq Failed ret=%d\n", ret);
  151. else
  152. ETHQOSDBG("pps_avb_class_b_irq pass\n");
  153. }
  154. }
  155. static void ethqos_unregister_pps_isr(struct stmmac_priv *priv, int ch)
  156. {
  157. struct qcom_ethqos *ethqos = priv->plat->bsp_priv;
  158. if (ch == DWC_ETH_QOS_PPS_CH_2)
  159. free_irq(ethqos->pps_class_a_irq, priv);
  160. else if (ch == DWC_ETH_QOS_PPS_CH_3)
  161. free_irq(ethqos->pps_class_b_irq, priv);
  162. }
  163. int ppsout_config(struct stmmac_priv *priv, struct ifr_data_struct *req)
  164. {
  165. int interval, width;
  166. u32 sub_second_inc, value, val;
  167. void __iomem *ioaddr = priv->ioaddr;
  168. struct pps_cfg eth_pps_cfg;
  169. if (copy_from_user(&eth_pps_cfg, (void __user *)req->ptr,
  170. sizeof(struct pps_cfg)))
  171. return -EFAULT;
  172. if (!eth_pps_cfg.ppsout_start) {
  173. ppsout_stop(priv, &eth_pps_cfg);
  174. if (eth_pps_cfg.ppsout_ch == DWC_ETH_QOS_PPS_CH_2 ||
  175. eth_pps_cfg.ppsout_ch == DWC_ETH_QOS_PPS_CH_3)
  176. ethqos_unregister_pps_isr(priv, eth_pps_cfg.ppsout_ch);
  177. return 0;
  178. }
  179. value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSUPDT);
  180. priv->hw->ptp->config_hw_tstamping(priv->ptpaddr, value);
  181. priv->hw->ptp->init_systime(priv->ptpaddr, 0, 0);
  182. priv->hw->ptp->adjust_systime(priv->ptpaddr, 0, 0, 0, 1);
  183. val = readl_relaxed(ioaddr + MAC_PPS_CONTROL);
  184. sub_second_inc = pps_config_sub_second_increment
  185. (priv->ptpaddr, eth_pps_cfg.ptpclk_freq,
  186. priv->plat->has_gmac4);
  187. pps_config_default_addend(priv->ptpaddr, priv,
  188. eth_pps_cfg.ptpclk_freq);
  189. val &= ~PPSX_MASK(eth_pps_cfg.ppsout_ch);
  190. val |= PPSCMDX(eth_pps_cfg.ppsout_ch, 0x2);
  191. val |= TRGTMODSELX(eth_pps_cfg.ppsout_ch, 0x2);
  192. val |= PPSEN0;
  193. if (eth_pps_cfg.ppsout_ch == DWC_ETH_QOS_PPS_CH_2 ||
  194. eth_pps_cfg.ppsout_ch == DWC_ETH_QOS_PPS_CH_3)
  195. ethqos_register_pps_isr(priv, eth_pps_cfg.ppsout_ch);
  196. writel_relaxed(0, ioaddr +
  197. MAC_PPSX_TARGET_TIME_SEC(eth_pps_cfg.ppsout_ch));
  198. writel_relaxed(0, ioaddr +
  199. MAC_PPSX_TARGET_TIME_NSEC(eth_pps_cfg.ppsout_ch));
  200. interval = ((eth_pps_cfg.ptpclk_freq + eth_pps_cfg.ppsout_freq / 2)
  201. / eth_pps_cfg.ppsout_freq);
  202. width = ((interval * eth_pps_cfg.ppsout_duty) + 50) / 100 - 1;
  203. if (width >= interval)
  204. width = interval - 1;
  205. if (width < 0)
  206. width = 0;
  207. writel_relaxed(interval, ioaddr + MAC_PPSX_INTERVAL(eth_pps_cfg.ppsout_ch));
  208. writel_relaxed(width, ioaddr + MAC_PPSX_WIDTH(eth_pps_cfg.ppsout_ch));
  209. writel_relaxed(val, ioaddr + MAC_PPS_CONTROL);
  210. return 0;
  211. }
  212. int ethqos_init_pps(void *priv_n)
  213. {
  214. struct stmmac_priv *priv;
  215. u32 value;
  216. struct ifr_data_struct req = {0};
  217. struct pps_cfg eth_pps_cfg = {0};
  218. if (!priv_n)
  219. return -ENODEV;
  220. priv = priv_n;
  221. priv->ptpaddr = priv->ioaddr + PTP_GMAC4_OFFSET;
  222. value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSUPDT);
  223. priv->hw->ptp->config_hw_tstamping(priv->ptpaddr, value);
  224. priv->hw->ptp->init_systime(priv->ptpaddr, 0, 0);
  225. priv->hw->ptp->adjust_systime(priv->ptpaddr, 0, 0, 0, 1);
  226. /*Configuaring PPS0 PPS output frequency to default 19.2 Mhz*/
  227. eth_pps_cfg.ppsout_ch = 0;
  228. eth_pps_cfg.ptpclk_freq = 62500000;
  229. eth_pps_cfg.ppsout_freq = 19200000;
  230. eth_pps_cfg.ppsout_start = 1;
  231. eth_pps_cfg.ppsout_duty = 50;
  232. req.ptr = (void *)&eth_pps_cfg;
  233. ppsout_config(priv, &req);
  234. return 0;
  235. }
  236. static ssize_t pps_fops_read(struct file *filp, char __user *buf,
  237. size_t count, loff_t *f_pos)
  238. {
  239. unsigned int len = 0, buf_len = 5000;
  240. char *temp_buf;
  241. ssize_t ret_cnt = 0;
  242. struct pps_info *info;
  243. info = filp->private_data;
  244. if (info->channel_no == AVB_CLASS_A_CHANNEL_NUM) {
  245. avb_class_a_msg_wq_flag = false;
  246. temp_buf = kzalloc(buf_len, GFP_KERNEL);
  247. if (!temp_buf)
  248. return -ENOMEM;
  249. if (pethqos)
  250. len = scnprintf(temp_buf, buf_len,
  251. "%ld\n", pethqos->avb_class_a_intr_cnt);
  252. else
  253. len = scnprintf(temp_buf, buf_len, "0\n");
  254. ret_cnt = simple_read_from_buffer(buf, count, f_pos,
  255. temp_buf, len);
  256. kfree(temp_buf);
  257. if (pethqos)
  258. ETHQOSERR("poll pps2intr info=%d sent by kernel\n",
  259. pethqos->avb_class_a_intr_cnt);
  260. } else if (info->channel_no == AVB_CLASS_B_CHANNEL_NUM) {
  261. avb_class_b_msg_wq_flag = false;
  262. temp_buf = kzalloc(buf_len, GFP_KERNEL);
  263. if (!temp_buf)
  264. return -ENOMEM;
  265. if (pethqos)
  266. len = scnprintf(temp_buf, buf_len,
  267. "%ld\n", pethqos->avb_class_b_intr_cnt);
  268. else
  269. len = scnprintf(temp_buf, buf_len, "0\n");
  270. ret_cnt = simple_read_from_buffer
  271. (buf, count, f_pos, temp_buf, len);
  272. kfree(temp_buf);
  273. } else {
  274. ETHQOSERR("invalid channel %d\n", info->channel_no);
  275. }
  276. return ret_cnt;
  277. }
  278. static unsigned int pps_fops_poll(struct file *file, poll_table *wait)
  279. {
  280. unsigned int mask = 0;
  281. struct pps_info *info;
  282. info = file->private_data;
  283. if (info->channel_no == AVB_CLASS_A_CHANNEL_NUM) {
  284. ETHQOSERR("avb_class_a_fops_poll wait\n");
  285. poll_wait(file, &avb_class_a_msg_wq, wait);
  286. if (avb_class_a_msg_wq_flag) {
  287. //Sending read mask
  288. mask |= POLLIN | POLLRDNORM;
  289. }
  290. } else if (info->channel_no == AVB_CLASS_B_CHANNEL_NUM) {
  291. poll_wait(file, &avb_class_b_msg_wq, wait);
  292. if (avb_class_b_msg_wq_flag) {
  293. //Sending read mask
  294. mask |= POLLIN | POLLRDNORM;
  295. }
  296. } else {
  297. ETHQOSERR("invalid channel %d\n", info->channel_no);
  298. }
  299. return mask;
  300. }
  301. static int pps_open(struct inode *inode, struct file *file)
  302. {
  303. struct pps_info *info;
  304. info = kmalloc(sizeof(*info), GFP_KERNEL);
  305. if (!info)
  306. return -ENOMEM;
  307. if (!strlcmp(file->f_path.dentry->d_iname,
  308. AVB_CLASS_A_POLL_DEV_NODE,
  309. strlen(AVB_CLASS_A_POLL_DEV_NODE))) {
  310. ETHQOSERR("pps open file name =%s\n",
  311. file->f_path.dentry->d_iname);
  312. info->channel_no = AVB_CLASS_A_CHANNEL_NUM;
  313. } else if (!strlcmp(file->f_path.dentry->d_iname,
  314. AVB_CLASS_B_POLL_DEV_NODE,
  315. strlen(AVB_CLASS_B_POLL_DEV_NODE))) {
  316. ETHQOSERR("pps open file name =%s\n",
  317. file->f_path.dentry->d_iname);
  318. info->channel_no = AVB_CLASS_B_CHANNEL_NUM;
  319. } else {
  320. ETHQOSERR("stsrncmp failed for %s\n",
  321. file->f_path.dentry->d_iname);
  322. }
  323. file->private_data = info;
  324. return 0;
  325. }
  326. static int pps_release(struct inode *inode, struct file *file)
  327. {
  328. kfree(file->private_data);
  329. return 0;
  330. }
  331. static const struct file_operations pps_fops = {
  332. .owner = THIS_MODULE,
  333. .open = pps_open,
  334. .release = pps_release,
  335. .read = pps_fops_read,
  336. .poll = pps_fops_poll,
  337. };
  338. int create_pps_interrupt_device_node(dev_t *pps_dev_t,
  339. struct cdev **pps_cdev,
  340. struct class **pps_class,
  341. char *pps_dev_node_name)
  342. {
  343. int ret;
  344. ret = alloc_chrdev_region(pps_dev_t, 0, 1,
  345. pps_dev_node_name);
  346. if (ret) {
  347. ETHQOSERR("alloc_chrdev_region error for node %s\n",
  348. pps_dev_node_name);
  349. goto alloc_chrdev1_region_fail;
  350. }
  351. *pps_cdev = cdev_alloc();
  352. if (!*pps_cdev) {
  353. ret = -ENOMEM;
  354. ETHQOSERR("failed to alloc cdev\n");
  355. goto fail_alloc_cdev;
  356. }
  357. cdev_init(*pps_cdev, &pps_fops);
  358. ret = cdev_add(*pps_cdev, *pps_dev_t, 1);
  359. if (ret < 0) {
  360. ETHQOSERR(":cdev_add err=%d\n", -ret);
  361. goto cdev1_add_fail;
  362. }
  363. *pps_class = class_create(THIS_MODULE, pps_dev_node_name);
  364. if (!*pps_class) {
  365. ret = -ENODEV;
  366. ETHQOSERR("failed to create class\n");
  367. goto fail_create_class;
  368. }
  369. if (!device_create(*pps_class, NULL,
  370. *pps_dev_t, NULL, pps_dev_node_name)) {
  371. ret = -EINVAL;
  372. ETHQOSERR("failed to create device_create\n");
  373. goto fail_create_device;
  374. }
  375. return 0;
  376. fail_create_device:
  377. class_destroy(*pps_class);
  378. fail_create_class:
  379. cdev_del(*pps_cdev);
  380. cdev1_add_fail:
  381. fail_alloc_cdev:
  382. unregister_chrdev_region(*pps_dev_t, 1);
  383. alloc_chrdev1_region_fail:
  384. return ret;
  385. }