blob: a9503aea527f54853720eb8aeb457f4720e7eb5b [file] [log] [blame]
Yangbo Lu9bdf43b2018-04-23 11:55:00 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2013-2016 Freescale Semiconductor Inc.
4 * Copyright 2016-2018 NXP
5 */
6
7#include <linux/module.h>
Yangbo Lud346c9e2019-06-14 18:40:51 +08008#include <linux/of.h>
9#include <linux/of_address.h>
Yangbo Lu8893a842019-06-14 18:40:54 +080010#include <linux/msi.h>
Yangbo Lu9bdf43b2018-04-23 11:55:00 +080011#include <linux/fsl/mc.h>
Yangbo Lud346c9e2019-06-14 18:40:51 +080012#include <linux/fsl/ptp_qoriq.h>
Yangbo Lu9bdf43b2018-04-23 11:55:00 +080013
Yangbo Lu0a006a22018-10-08 15:44:25 +080014#include "dpaa2-ptp.h"
Yangbo Lu9bdf43b2018-04-23 11:55:00 +080015
Yangbo Lu8893a842019-06-14 18:40:54 +080016static int dpaa2_ptp_enable(struct ptp_clock_info *ptp,
17 struct ptp_clock_request *rq, int on)
18{
19 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
20 struct fsl_mc_device *mc_dev;
21 struct device *dev;
22 u32 mask = 0;
23 u32 bit;
24 int err;
25
26 dev = ptp_qoriq->dev;
27 mc_dev = to_fsl_mc_device(dev);
28
29 switch (rq->type) {
30 case PTP_CLK_REQ_PPS:
31 bit = DPRTC_EVENT_PPS;
32 break;
33 default:
34 return -EOPNOTSUPP;
35 }
36
37 err = dprtc_get_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle,
38 DPRTC_IRQ_INDEX, &mask);
39 if (err < 0) {
40 dev_err(dev, "dprtc_get_irq_mask(): %d\n", err);
41 return err;
42 }
43
44 if (on)
45 mask |= bit;
46 else
47 mask &= ~bit;
48
49 err = dprtc_set_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle,
50 DPRTC_IRQ_INDEX, mask);
51 if (err < 0) {
52 dev_err(dev, "dprtc_set_irq_mask(): %d\n", err);
53 return err;
54 }
55
56 return 0;
57}
58
Yangbo Lud346c9e2019-06-14 18:40:51 +080059static const struct ptp_clock_info dpaa2_ptp_caps = {
Yangbo Lu9bdf43b2018-04-23 11:55:00 +080060 .owner = THIS_MODULE,
61 .name = "DPAA2 PTP Clock",
62 .max_adj = 512000,
63 .n_alarm = 2,
64 .n_ext_ts = 2,
65 .n_per_out = 3,
66 .n_pins = 0,
67 .pps = 1,
Yangbo Lud346c9e2019-06-14 18:40:51 +080068 .adjfine = ptp_qoriq_adjfine,
69 .adjtime = ptp_qoriq_adjtime,
70 .gettime64 = ptp_qoriq_gettime,
71 .settime64 = ptp_qoriq_settime,
Yangbo Lu8893a842019-06-14 18:40:54 +080072 .enable = dpaa2_ptp_enable,
Yangbo Lu9bdf43b2018-04-23 11:55:00 +080073};
74
Yangbo Lu8893a842019-06-14 18:40:54 +080075static irqreturn_t dpaa2_ptp_irq_handler_thread(int irq, void *priv)
76{
77 struct ptp_qoriq *ptp_qoriq = priv;
78 struct ptp_clock_event event;
79 struct fsl_mc_device *mc_dev;
80 struct device *dev;
81 u32 status = 0;
82 int err;
83
84 dev = ptp_qoriq->dev;
85 mc_dev = to_fsl_mc_device(dev);
86
87 err = dprtc_get_irq_status(mc_dev->mc_io, 0, mc_dev->mc_handle,
88 DPRTC_IRQ_INDEX, &status);
89 if (unlikely(err)) {
90 dev_err(dev, "dprtc_get_irq_status err %d\n", err);
91 return IRQ_NONE;
92 }
93
94 if (status & DPRTC_EVENT_PPS) {
95 event.type = PTP_CLOCK_PPS;
96 ptp_clock_event(ptp_qoriq->clock, &event);
97 }
98
99 err = dprtc_clear_irq_status(mc_dev->mc_io, 0, mc_dev->mc_handle,
100 DPRTC_IRQ_INDEX, status);
101 if (unlikely(err)) {
102 dev_err(dev, "dprtc_clear_irq_status err %d\n", err);
103 return IRQ_NONE;
104 }
105
106 return IRQ_HANDLED;
107}
108
Yangbo Lu180f5392018-10-08 15:44:28 +0800109static int dpaa2_ptp_probe(struct fsl_mc_device *mc_dev)
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800110{
111 struct device *dev = &mc_dev->dev;
Yangbo Lu8893a842019-06-14 18:40:54 +0800112 struct fsl_mc_device_irq *irq;
Yangbo Lud346c9e2019-06-14 18:40:51 +0800113 struct ptp_qoriq *ptp_qoriq;
114 struct device_node *node;
115 void __iomem *base;
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800116 int err;
117
Yangbo Lud346c9e2019-06-14 18:40:51 +0800118 ptp_qoriq = devm_kzalloc(dev, sizeof(*ptp_qoriq), GFP_KERNEL);
119 if (!ptp_qoriq)
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800120 return -ENOMEM;
121
122 err = fsl_mc_portal_allocate(mc_dev, 0, &mc_dev->mc_io);
123 if (err) {
Ioana Ciornei55005982018-11-09 15:26:46 +0000124 if (err == -ENXIO)
125 err = -EPROBE_DEFER;
126 else
127 dev_err(dev, "fsl_mc_portal_allocate err %d\n", err);
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800128 goto err_exit;
129 }
130
131 err = dprtc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
132 &mc_dev->mc_handle);
133 if (err) {
134 dev_err(dev, "dprtc_open err %d\n", err);
135 goto err_free_mcp;
136 }
137
Yangbo Lud346c9e2019-06-14 18:40:51 +0800138 ptp_qoriq->dev = dev;
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800139
Yangbo Lud346c9e2019-06-14 18:40:51 +0800140 node = of_find_compatible_node(NULL, NULL, "fsl,dpaa2-ptp");
141 if (!node) {
142 err = -ENODEV;
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800143 goto err_close;
144 }
145
Yangbo Lud346c9e2019-06-14 18:40:51 +0800146 dev->of_node = node;
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800147
Yangbo Lud346c9e2019-06-14 18:40:51 +0800148 base = of_iomap(node, 0);
149 if (!base) {
150 err = -ENOMEM;
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800151 goto err_close;
152 }
153
Yangbo Lu8893a842019-06-14 18:40:54 +0800154 err = fsl_mc_allocate_irqs(mc_dev);
155 if (err) {
156 dev_err(dev, "MC irqs allocation failed\n");
157 goto err_unmap;
158 }
159
160 irq = mc_dev->irqs[0];
161 ptp_qoriq->irq = irq->msi_desc->irq;
162
163 err = devm_request_threaded_irq(dev, ptp_qoriq->irq, NULL,
164 dpaa2_ptp_irq_handler_thread,
165 IRQF_NO_SUSPEND | IRQF_ONESHOT,
166 dev_name(dev), ptp_qoriq);
167 if (err < 0) {
168 dev_err(dev, "devm_request_threaded_irq(): %d\n", err);
169 goto err_free_mc_irq;
170 }
171
172 err = dprtc_set_irq_enable(mc_dev->mc_io, 0, mc_dev->mc_handle,
173 DPRTC_IRQ_INDEX, 1);
174 if (err < 0) {
175 dev_err(dev, "dprtc_set_irq_enable(): %d\n", err);
176 goto err_free_mc_irq;
177 }
178
Yangbo Lud346c9e2019-06-14 18:40:51 +0800179 err = ptp_qoriq_init(ptp_qoriq, base, &dpaa2_ptp_caps);
180 if (err)
Yangbo Lu8893a842019-06-14 18:40:54 +0800181 goto err_free_mc_irq;
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800182
Yangbo Lud346c9e2019-06-14 18:40:51 +0800183 dpaa2_phc_index = ptp_qoriq->phc_index;
184 dev_set_drvdata(dev, ptp_qoriq);
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800185
186 return 0;
187
Yangbo Lu8893a842019-06-14 18:40:54 +0800188err_free_mc_irq:
189 fsl_mc_free_irqs(mc_dev);
Yangbo Lud346c9e2019-06-14 18:40:51 +0800190err_unmap:
191 iounmap(base);
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800192err_close:
193 dprtc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
194err_free_mcp:
195 fsl_mc_portal_free(mc_dev->mc_io);
196err_exit:
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800197 return err;
198}
199
Yangbo Lu180f5392018-10-08 15:44:28 +0800200static int dpaa2_ptp_remove(struct fsl_mc_device *mc_dev)
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800201{
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800202 struct device *dev = &mc_dev->dev;
Yangbo Lud346c9e2019-06-14 18:40:51 +0800203 struct ptp_qoriq *ptp_qoriq;
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800204
Yangbo Lud346c9e2019-06-14 18:40:51 +0800205 ptp_qoriq = dev_get_drvdata(dev);
206
207 dpaa2_phc_index = -1;
208 ptp_qoriq_free(ptp_qoriq);
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800209
Yangbo Lu8893a842019-06-14 18:40:54 +0800210 fsl_mc_free_irqs(mc_dev);
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800211 dprtc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
212 fsl_mc_portal_free(mc_dev->mc_io);
213
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800214 return 0;
215}
216
Yangbo Lu180f5392018-10-08 15:44:28 +0800217static const struct fsl_mc_device_id dpaa2_ptp_match_id_table[] = {
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800218 {
219 .vendor = FSL_MC_VENDOR_FREESCALE,
220 .obj_type = "dprtc",
221 },
222 {}
223};
Yangbo Lu180f5392018-10-08 15:44:28 +0800224MODULE_DEVICE_TABLE(fslmc, dpaa2_ptp_match_id_table);
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800225
Yangbo Lu180f5392018-10-08 15:44:28 +0800226static struct fsl_mc_driver dpaa2_ptp_drv = {
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800227 .driver = {
228 .name = KBUILD_MODNAME,
229 .owner = THIS_MODULE,
230 },
Yangbo Lu180f5392018-10-08 15:44:28 +0800231 .probe = dpaa2_ptp_probe,
232 .remove = dpaa2_ptp_remove,
233 .match_id_table = dpaa2_ptp_match_id_table,
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800234};
235
Yangbo Lu180f5392018-10-08 15:44:28 +0800236module_fsl_mc_driver(dpaa2_ptp_drv);
Yangbo Lu9bdf43b2018-04-23 11:55:00 +0800237
238MODULE_LICENSE("GPL v2");
239MODULE_DESCRIPTION("DPAA2 PTP Clock Driver");