xref: /openbmc/linux/drivers/ptp/ptp_ocp.c (revision 7c807572)
1a7e1abadSJonathan Lemon // SPDX-License-Identifier: GPL-2.0-only
2a7e1abadSJonathan Lemon /* Copyright (c) 2020 Facebook */
3a7e1abadSJonathan Lemon 
4a7e1abadSJonathan Lemon #include <linux/err.h>
5a7e1abadSJonathan Lemon #include <linux/kernel.h>
6a7e1abadSJonathan Lemon #include <linux/module.h>
7a7e1abadSJonathan Lemon #include <linux/init.h>
8a7e1abadSJonathan Lemon #include <linux/pci.h>
9773bda96SJonathan Lemon #include <linux/serial_8250.h>
10773bda96SJonathan Lemon #include <linux/clkdev.h>
11773bda96SJonathan Lemon #include <linux/clk-provider.h>
12773bda96SJonathan Lemon #include <linux/platform_device.h>
13a7e1abadSJonathan Lemon #include <linux/ptp_clock_kernel.h>
14773bda96SJonathan Lemon #include <linux/spi/spi.h>
15773bda96SJonathan Lemon #include <linux/spi/xilinx_spi.h>
16773bda96SJonathan Lemon #include <net/devlink.h>
17773bda96SJonathan Lemon #include <linux/i2c.h>
18773bda96SJonathan Lemon #include <linux/mtd/mtd.h>
19a7e1abadSJonathan Lemon 
20773bda96SJonathan Lemon #ifndef PCI_VENDOR_ID_FACEBOOK
21773bda96SJonathan Lemon #define PCI_VENDOR_ID_FACEBOOK 0x1d9b
22773bda96SJonathan Lemon #endif
23773bda96SJonathan Lemon 
24773bda96SJonathan Lemon #ifndef PCI_DEVICE_ID_FACEBOOK_TIMECARD
25773bda96SJonathan Lemon #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400
26773bda96SJonathan Lemon #endif
27773bda96SJonathan Lemon 
28773bda96SJonathan Lemon static struct class timecard_class = {
29773bda96SJonathan Lemon 	.owner		= THIS_MODULE,
30773bda96SJonathan Lemon 	.name		= "timecard",
31a7e1abadSJonathan Lemon };
32a7e1abadSJonathan Lemon 
33a7e1abadSJonathan Lemon struct ocp_reg {
34a7e1abadSJonathan Lemon 	u32	ctrl;
35a7e1abadSJonathan Lemon 	u32	status;
36a7e1abadSJonathan Lemon 	u32	select;
37a7e1abadSJonathan Lemon 	u32	version;
38a7e1abadSJonathan Lemon 	u32	time_ns;
39a7e1abadSJonathan Lemon 	u32	time_sec;
40a7e1abadSJonathan Lemon 	u32	__pad0[2];
41a7e1abadSJonathan Lemon 	u32	adjust_ns;
42a7e1abadSJonathan Lemon 	u32	adjust_sec;
43a7e1abadSJonathan Lemon 	u32	__pad1[2];
44a7e1abadSJonathan Lemon 	u32	offset_ns;
45a7e1abadSJonathan Lemon 	u32	offset_window_ns;
46773bda96SJonathan Lemon 	u32	__pad2[2];
47773bda96SJonathan Lemon 	u32	drift_ns;
48773bda96SJonathan Lemon 	u32	drift_window_ns;
49773bda96SJonathan Lemon 	u32	__pad3[6];
50773bda96SJonathan Lemon 	u32	servo_offset_p;
51773bda96SJonathan Lemon 	u32	servo_offset_i;
52773bda96SJonathan Lemon 	u32	servo_drift_p;
53773bda96SJonathan Lemon 	u32	servo_drift_i;
54a7e1abadSJonathan Lemon };
55a7e1abadSJonathan Lemon 
56a7e1abadSJonathan Lemon #define OCP_CTRL_ENABLE		BIT(0)
57a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_TIME	BIT(1)
58a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_OFFSET	BIT(2)
59773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_DRIFT	BIT(3)
60773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_SERVO	BIT(8)
61a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_REQ	BIT(30)
62a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_DONE	BIT(31)
63a7e1abadSJonathan Lemon 
64a7e1abadSJonathan Lemon #define OCP_STATUS_IN_SYNC	BIT(0)
65773bda96SJonathan Lemon #define OCP_STATUS_IN_HOLDOVER	BIT(1)
66a7e1abadSJonathan Lemon 
67a7e1abadSJonathan Lemon #define OCP_SELECT_CLK_NONE	0
68773bda96SJonathan Lemon #define OCP_SELECT_CLK_REG	0xfe
69a7e1abadSJonathan Lemon 
70a7e1abadSJonathan Lemon struct tod_reg {
71a7e1abadSJonathan Lemon 	u32	ctrl;
72a7e1abadSJonathan Lemon 	u32	status;
73a7e1abadSJonathan Lemon 	u32	uart_polarity;
74a7e1abadSJonathan Lemon 	u32	version;
75a7e1abadSJonathan Lemon 	u32	correction_sec;
76a7e1abadSJonathan Lemon 	u32	__pad0[3];
77a7e1abadSJonathan Lemon 	u32	uart_baud;
78a7e1abadSJonathan Lemon 	u32	__pad1[3];
79a7e1abadSJonathan Lemon 	u32	utc_status;
80a7e1abadSJonathan Lemon 	u32	leap;
81a7e1abadSJonathan Lemon };
82a7e1abadSJonathan Lemon 
83a7e1abadSJonathan Lemon #define TOD_CTRL_PROTOCOL	BIT(28)
84a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_A	BIT(17)
85a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_B	BIT(16)
86a7e1abadSJonathan Lemon #define TOD_CTRL_ENABLE		BIT(0)
87a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_MASK	((1U << 4) - 1)
88a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_SHIFT	24
89a7e1abadSJonathan Lemon 
90a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_MASK	0xff
91a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_VALID	BIT(8)
92a7e1abadSJonathan Lemon #define TOD_STATUS_LEAP_VALID	BIT(16)
93a7e1abadSJonathan Lemon 
94773bda96SJonathan Lemon struct ts_reg {
95773bda96SJonathan Lemon 	u32	enable;
96773bda96SJonathan Lemon 	u32	error;
97773bda96SJonathan Lemon 	u32	polarity;
98773bda96SJonathan Lemon 	u32	version;
99773bda96SJonathan Lemon 	u32	__pad0[4];
100773bda96SJonathan Lemon 	u32	cable_delay;
101773bda96SJonathan Lemon 	u32	__pad1[3];
102773bda96SJonathan Lemon 	u32	intr;
103773bda96SJonathan Lemon 	u32	intr_mask;
104773bda96SJonathan Lemon 	u32	event_count;
105773bda96SJonathan Lemon 	u32	__pad2[1];
106773bda96SJonathan Lemon 	u32	ts_count;
107773bda96SJonathan Lemon 	u32	time_ns;
108773bda96SJonathan Lemon 	u32	time_sec;
109773bda96SJonathan Lemon 	u32	data_width;
110773bda96SJonathan Lemon 	u32	data;
111773bda96SJonathan Lemon };
112773bda96SJonathan Lemon 
113773bda96SJonathan Lemon struct pps_reg {
114773bda96SJonathan Lemon 	u32	ctrl;
115773bda96SJonathan Lemon 	u32	status;
1160d43d4f2SJonathan Lemon 	u32	__pad0[6];
1170d43d4f2SJonathan Lemon 	u32	cable_delay;
118773bda96SJonathan Lemon };
119773bda96SJonathan Lemon 
120773bda96SJonathan Lemon #define PPS_STATUS_FILTER_ERR	BIT(0)
121773bda96SJonathan Lemon #define PPS_STATUS_SUPERV_ERR	BIT(1)
122773bda96SJonathan Lemon 
123773bda96SJonathan Lemon struct img_reg {
124773bda96SJonathan Lemon 	u32	version;
125773bda96SJonathan Lemon };
126773bda96SJonathan Lemon 
127773bda96SJonathan Lemon struct ptp_ocp_flash_info {
128773bda96SJonathan Lemon 	const char *name;
129773bda96SJonathan Lemon 	int pci_offset;
130773bda96SJonathan Lemon 	int data_size;
131773bda96SJonathan Lemon 	void *data;
132773bda96SJonathan Lemon };
133773bda96SJonathan Lemon 
134773bda96SJonathan Lemon struct ptp_ocp_ext_info {
135773bda96SJonathan Lemon 	const char *name;
136773bda96SJonathan Lemon 	int index;
137773bda96SJonathan Lemon 	irqreturn_t (*irq_fcn)(int irq, void *priv);
138773bda96SJonathan Lemon 	int (*enable)(void *priv, bool enable);
139773bda96SJonathan Lemon };
140773bda96SJonathan Lemon 
141773bda96SJonathan Lemon struct ptp_ocp_ext_src {
142773bda96SJonathan Lemon 	void __iomem		*mem;
143773bda96SJonathan Lemon 	struct ptp_ocp		*bp;
144773bda96SJonathan Lemon 	struct ptp_ocp_ext_info	*info;
145773bda96SJonathan Lemon 	int			irq_vec;
146773bda96SJonathan Lemon };
147773bda96SJonathan Lemon 
148a7e1abadSJonathan Lemon struct ptp_ocp {
149a7e1abadSJonathan Lemon 	struct pci_dev		*pdev;
150773bda96SJonathan Lemon 	struct device		dev;
151a7e1abadSJonathan Lemon 	spinlock_t		lock;
152a7e1abadSJonathan Lemon 	struct ocp_reg __iomem	*reg;
153a7e1abadSJonathan Lemon 	struct tod_reg __iomem	*tod;
1540d43d4f2SJonathan Lemon 	struct pps_reg __iomem	*pps_to_ext;
1550d43d4f2SJonathan Lemon 	struct pps_reg __iomem	*pps_to_clk;
156773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*pps;
157773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*ts0;
158773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*ts1;
159773bda96SJonathan Lemon 	struct img_reg __iomem	*image;
160a7e1abadSJonathan Lemon 	struct ptp_clock	*ptp;
161a7e1abadSJonathan Lemon 	struct ptp_clock_info	ptp_info;
162773bda96SJonathan Lemon 	struct platform_device	*i2c_ctrl;
163773bda96SJonathan Lemon 	struct platform_device	*spi_flash;
164773bda96SJonathan Lemon 	struct clk_hw		*i2c_clk;
165773bda96SJonathan Lemon 	struct timer_list	watchdog;
166ef0cfb34SJonathan Lemon 	time64_t		gnss_lost;
167773bda96SJonathan Lemon 	int			id;
168773bda96SJonathan Lemon 	int			n_irqs;
169ef0cfb34SJonathan Lemon 	int			gnss_port;
170773bda96SJonathan Lemon 	int			mac_port;	/* miniature atomic clock */
171773bda96SJonathan Lemon 	u8			serial[6];
172773bda96SJonathan Lemon 	int			flash_start;
173773bda96SJonathan Lemon 	bool			has_serial;
174a7e1abadSJonathan Lemon };
175a7e1abadSJonathan Lemon 
176773bda96SJonathan Lemon struct ocp_resource {
177773bda96SJonathan Lemon 	unsigned long offset;
178773bda96SJonathan Lemon 	int size;
179773bda96SJonathan Lemon 	int irq_vec;
180773bda96SJonathan Lemon 	int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r);
181773bda96SJonathan Lemon 	void *extra;
182773bda96SJonathan Lemon 	unsigned long bp_offset;
183773bda96SJonathan Lemon };
184773bda96SJonathan Lemon 
185773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r);
186773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r);
187773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r);
188773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r);
189773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r);
190773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
191773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv);
192773bda96SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, bool enable);
193773bda96SJonathan Lemon 
194773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({				\
195773bda96SJonathan Lemon 	uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset;		\
196773bda96SJonathan Lemon 	*(typeof(val) *)addr = val;					\
197773bda96SJonathan Lemon })
198773bda96SJonathan Lemon 
199773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \
200773bda96SJonathan Lemon 	.bp_offset = offsetof(struct ptp_ocp, member)
201773bda96SJonathan Lemon 
202773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \
203773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem
204773bda96SJonathan Lemon 
205773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \
206773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial
207773bda96SJonathan Lemon 
208773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \
209773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c
210773bda96SJonathan Lemon 
211773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \
212773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi
213773bda96SJonathan Lemon 
214773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \
215773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext
216773bda96SJonathan Lemon 
217773bda96SJonathan Lemon /* This is the MSI vector mapping used.
218773bda96SJonathan Lemon  * 0: N/C
219773bda96SJonathan Lemon  * 1: TS0
220773bda96SJonathan Lemon  * 2: TS1
221773bda96SJonathan Lemon  * 3: GPS
222773bda96SJonathan Lemon  * 4: GPS2 (n/c)
223773bda96SJonathan Lemon  * 5: MAC
224773bda96SJonathan Lemon  * 6: SPI IMU (inertial measurement unit)
225773bda96SJonathan Lemon  * 7: I2C oscillator
226773bda96SJonathan Lemon  * 8: HWICAP
227773bda96SJonathan Lemon  * 9: SPI Flash
228773bda96SJonathan Lemon  */
229773bda96SJonathan Lemon 
230773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = {
231773bda96SJonathan Lemon 	{
232773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(reg),
233773bda96SJonathan Lemon 		.offset = 0x01000000, .size = 0x10000,
234773bda96SJonathan Lemon 	},
235773bda96SJonathan Lemon 	{
236773bda96SJonathan Lemon 		OCP_EXT_RESOURCE(ts0),
237773bda96SJonathan Lemon 		.offset = 0x01010000, .size = 0x10000, .irq_vec = 1,
238773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
239773bda96SJonathan Lemon 			.name = "ts0", .index = 0,
240773bda96SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
241773bda96SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
242773bda96SJonathan Lemon 		},
243773bda96SJonathan Lemon 	},
244773bda96SJonathan Lemon 	{
245773bda96SJonathan Lemon 		OCP_EXT_RESOURCE(ts1),
246773bda96SJonathan Lemon 		.offset = 0x01020000, .size = 0x10000, .irq_vec = 2,
247773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
248773bda96SJonathan Lemon 			.name = "ts1", .index = 1,
249773bda96SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
250773bda96SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
251773bda96SJonathan Lemon 		},
252773bda96SJonathan Lemon 	},
253773bda96SJonathan Lemon 	{
2540d43d4f2SJonathan Lemon 		OCP_MEM_RESOURCE(pps_to_ext),
2550d43d4f2SJonathan Lemon 		.offset = 0x01030000, .size = 0x10000,
2560d43d4f2SJonathan Lemon 	},
2570d43d4f2SJonathan Lemon 	{
2580d43d4f2SJonathan Lemon 		OCP_MEM_RESOURCE(pps_to_clk),
259773bda96SJonathan Lemon 		.offset = 0x01040000, .size = 0x10000,
260773bda96SJonathan Lemon 	},
261773bda96SJonathan Lemon 	{
262773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(tod),
263773bda96SJonathan Lemon 		.offset = 0x01050000, .size = 0x10000,
264773bda96SJonathan Lemon 	},
265773bda96SJonathan Lemon 	{
266773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(image),
267773bda96SJonathan Lemon 		.offset = 0x00020000, .size = 0x1000,
268773bda96SJonathan Lemon 	},
269773bda96SJonathan Lemon 	{
270773bda96SJonathan Lemon 		OCP_I2C_RESOURCE(i2c_ctrl),
271773bda96SJonathan Lemon 		.offset = 0x00150000, .size = 0x10000, .irq_vec = 7,
272773bda96SJonathan Lemon 	},
273773bda96SJonathan Lemon 	{
274ef0cfb34SJonathan Lemon 		OCP_SERIAL_RESOURCE(gnss_port),
275773bda96SJonathan Lemon 		.offset = 0x00160000 + 0x1000, .irq_vec = 3,
276773bda96SJonathan Lemon 	},
277773bda96SJonathan Lemon 	{
278773bda96SJonathan Lemon 		OCP_SERIAL_RESOURCE(mac_port),
279773bda96SJonathan Lemon 		.offset = 0x00180000 + 0x1000, .irq_vec = 5,
280773bda96SJonathan Lemon 	},
281773bda96SJonathan Lemon 	{
282773bda96SJonathan Lemon 		OCP_SPI_RESOURCE(spi_flash),
283773bda96SJonathan Lemon 		.offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
284773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_flash_info) {
285773bda96SJonathan Lemon 			.name = "xilinx_spi", .pci_offset = 0,
286773bda96SJonathan Lemon 			.data_size = sizeof(struct xspi_platform_data),
287773bda96SJonathan Lemon 			.data = &(struct xspi_platform_data) {
288773bda96SJonathan Lemon 				.num_chipselect = 1,
289773bda96SJonathan Lemon 				.bits_per_word = 8,
290773bda96SJonathan Lemon 				.num_devices = 1,
291773bda96SJonathan Lemon 				.devices = &(struct spi_board_info) {
292773bda96SJonathan Lemon 					.modalias = "spi-nor",
293773bda96SJonathan Lemon 				},
294773bda96SJonathan Lemon 			},
295773bda96SJonathan Lemon 		},
296773bda96SJonathan Lemon 	},
297773bda96SJonathan Lemon 	{
298773bda96SJonathan Lemon 		.setup = ptp_ocp_fb_board_init,
299773bda96SJonathan Lemon 	},
300773bda96SJonathan Lemon 	{ }
301773bda96SJonathan Lemon };
302773bda96SJonathan Lemon 
303773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = {
304773bda96SJonathan Lemon 	{ PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) },
305773bda96SJonathan Lemon 	{ 0 }
306773bda96SJonathan Lemon };
307773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id);
308773bda96SJonathan Lemon 
309773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock);
310773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr);
311773bda96SJonathan Lemon 
312773bda96SJonathan Lemon static struct {
313773bda96SJonathan Lemon 	const char *name;
314773bda96SJonathan Lemon 	int value;
315773bda96SJonathan Lemon } ptp_ocp_clock[] = {
316773bda96SJonathan Lemon 	{ .name = "NONE",	.value = 0 },
317773bda96SJonathan Lemon 	{ .name = "TOD",	.value = 1 },
318773bda96SJonathan Lemon 	{ .name = "IRIG",	.value = 2 },
319773bda96SJonathan Lemon 	{ .name = "PPS",	.value = 3 },
320773bda96SJonathan Lemon 	{ .name = "PTP",	.value = 4 },
321773bda96SJonathan Lemon 	{ .name = "RTC",	.value = 5 },
322773bda96SJonathan Lemon 	{ .name = "DCF",	.value = 6 },
323773bda96SJonathan Lemon 	{ .name = "REGS",	.value = 0xfe },
324773bda96SJonathan Lemon 	{ .name = "EXT",	.value = 0xff },
325773bda96SJonathan Lemon };
326773bda96SJonathan Lemon 
327773bda96SJonathan Lemon static const char *
328773bda96SJonathan Lemon ptp_ocp_clock_name_from_val(int val)
329773bda96SJonathan Lemon {
330773bda96SJonathan Lemon 	int i;
331773bda96SJonathan Lemon 
332773bda96SJonathan Lemon 	for (i = 0; i < ARRAY_SIZE(ptp_ocp_clock); i++)
333773bda96SJonathan Lemon 		if (ptp_ocp_clock[i].value == val)
334773bda96SJonathan Lemon 			return ptp_ocp_clock[i].name;
335773bda96SJonathan Lemon 	return NULL;
336773bda96SJonathan Lemon }
337773bda96SJonathan Lemon 
338773bda96SJonathan Lemon static int
339773bda96SJonathan Lemon ptp_ocp_clock_val_from_name(const char *name)
340773bda96SJonathan Lemon {
341773bda96SJonathan Lemon 	const char *clk;
342773bda96SJonathan Lemon 	int i;
343773bda96SJonathan Lemon 
344773bda96SJonathan Lemon 	for (i = 0; i < ARRAY_SIZE(ptp_ocp_clock); i++) {
345773bda96SJonathan Lemon 		clk = ptp_ocp_clock[i].name;
346773bda96SJonathan Lemon 		if (!strncasecmp(name, clk, strlen(clk)))
347773bda96SJonathan Lemon 			return ptp_ocp_clock[i].value;
348773bda96SJonathan Lemon 	}
349773bda96SJonathan Lemon 	return -EINVAL;
350773bda96SJonathan Lemon }
351773bda96SJonathan Lemon 
352a7e1abadSJonathan Lemon static int
353a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts,
354a7e1abadSJonathan Lemon 			 struct ptp_system_timestamp *sts)
355a7e1abadSJonathan Lemon {
356a7e1abadSJonathan Lemon 	u32 ctrl, time_sec, time_ns;
357a7e1abadSJonathan Lemon 	int i;
358a7e1abadSJonathan Lemon 
359a7e1abadSJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
360a7e1abadSJonathan Lemon 	ctrl |= OCP_CTRL_READ_TIME_REQ;
361a7e1abadSJonathan Lemon 
362a7e1abadSJonathan Lemon 	ptp_read_system_prets(sts);
363a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
364a7e1abadSJonathan Lemon 
365a7e1abadSJonathan Lemon 	for (i = 0; i < 100; i++) {
366a7e1abadSJonathan Lemon 		ctrl = ioread32(&bp->reg->ctrl);
367a7e1abadSJonathan Lemon 		if (ctrl & OCP_CTRL_READ_TIME_DONE)
368a7e1abadSJonathan Lemon 			break;
369a7e1abadSJonathan Lemon 	}
370a7e1abadSJonathan Lemon 	ptp_read_system_postts(sts);
371a7e1abadSJonathan Lemon 
372a7e1abadSJonathan Lemon 	time_ns = ioread32(&bp->reg->time_ns);
373a7e1abadSJonathan Lemon 	time_sec = ioread32(&bp->reg->time_sec);
374a7e1abadSJonathan Lemon 
375a7e1abadSJonathan Lemon 	ts->tv_sec = time_sec;
376a7e1abadSJonathan Lemon 	ts->tv_nsec = time_ns;
377a7e1abadSJonathan Lemon 
378a7e1abadSJonathan Lemon 	return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT;
379a7e1abadSJonathan Lemon }
380a7e1abadSJonathan Lemon 
381a7e1abadSJonathan Lemon static int
382a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts,
383a7e1abadSJonathan Lemon 		 struct ptp_system_timestamp *sts)
384a7e1abadSJonathan Lemon {
385a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
386a7e1abadSJonathan Lemon 	unsigned long flags;
387a7e1abadSJonathan Lemon 	int err;
388a7e1abadSJonathan Lemon 
389a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
390a7e1abadSJonathan Lemon 	err = __ptp_ocp_gettime_locked(bp, ts, sts);
391a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
392a7e1abadSJonathan Lemon 
393a7e1abadSJonathan Lemon 	return err;
394a7e1abadSJonathan Lemon }
395a7e1abadSJonathan Lemon 
396a7e1abadSJonathan Lemon static void
397a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts)
398a7e1abadSJonathan Lemon {
399a7e1abadSJonathan Lemon 	u32 ctrl, time_sec, time_ns;
400a7e1abadSJonathan Lemon 	u32 select;
401a7e1abadSJonathan Lemon 
402a7e1abadSJonathan Lemon 	time_ns = ts->tv_nsec;
403a7e1abadSJonathan Lemon 	time_sec = ts->tv_sec;
404a7e1abadSJonathan Lemon 
405a7e1abadSJonathan Lemon 	select = ioread32(&bp->reg->select);
406a7e1abadSJonathan Lemon 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
407a7e1abadSJonathan Lemon 
408a7e1abadSJonathan Lemon 	iowrite32(time_ns, &bp->reg->adjust_ns);
409a7e1abadSJonathan Lemon 	iowrite32(time_sec, &bp->reg->adjust_sec);
410a7e1abadSJonathan Lemon 
411a7e1abadSJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
412a7e1abadSJonathan Lemon 	ctrl |= OCP_CTRL_ADJUST_TIME;
413a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
414a7e1abadSJonathan Lemon 
415a7e1abadSJonathan Lemon 	/* restore clock selection */
416a7e1abadSJonathan Lemon 	iowrite32(select >> 16, &bp->reg->select);
417a7e1abadSJonathan Lemon }
418a7e1abadSJonathan Lemon 
419a7e1abadSJonathan Lemon static int
420a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts)
421a7e1abadSJonathan Lemon {
422a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
423a7e1abadSJonathan Lemon 	unsigned long flags;
424a7e1abadSJonathan Lemon 
425a7e1abadSJonathan Lemon 	if (ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC)
426a7e1abadSJonathan Lemon 		return 0;
427a7e1abadSJonathan Lemon 
428a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
429a7e1abadSJonathan Lemon 	__ptp_ocp_settime_locked(bp, ts);
430a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
431a7e1abadSJonathan Lemon 
432a7e1abadSJonathan Lemon 	return 0;
433a7e1abadSJonathan Lemon }
434a7e1abadSJonathan Lemon 
435a7e1abadSJonathan Lemon static int
436a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns)
437a7e1abadSJonathan Lemon {
438a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
439a7e1abadSJonathan Lemon 	struct timespec64 ts;
440a7e1abadSJonathan Lemon 	unsigned long flags;
441a7e1abadSJonathan Lemon 	int err;
442a7e1abadSJonathan Lemon 
443a7e1abadSJonathan Lemon 	if (ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC)
444a7e1abadSJonathan Lemon 		return 0;
445a7e1abadSJonathan Lemon 
446a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
447a7e1abadSJonathan Lemon 	err = __ptp_ocp_gettime_locked(bp, &ts, NULL);
448a7e1abadSJonathan Lemon 	if (likely(!err)) {
449a7e1abadSJonathan Lemon 		timespec64_add_ns(&ts, delta_ns);
450a7e1abadSJonathan Lemon 		__ptp_ocp_settime_locked(bp, &ts);
451a7e1abadSJonathan Lemon 	}
452a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
453a7e1abadSJonathan Lemon 
454a7e1abadSJonathan Lemon 	return err;
455a7e1abadSJonathan Lemon }
456a7e1abadSJonathan Lemon 
457a7e1abadSJonathan Lemon static int
458a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
459a7e1abadSJonathan Lemon {
460a7e1abadSJonathan Lemon 	if (scaled_ppm == 0)
461a7e1abadSJonathan Lemon 		return 0;
462a7e1abadSJonathan Lemon 
463a7e1abadSJonathan Lemon 	return -EOPNOTSUPP;
464a7e1abadSJonathan Lemon }
465a7e1abadSJonathan Lemon 
466773bda96SJonathan Lemon static int
467773bda96SJonathan Lemon ptp_ocp_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns)
468773bda96SJonathan Lemon {
469773bda96SJonathan Lemon 	return -EOPNOTSUPP;
470773bda96SJonathan Lemon }
471773bda96SJonathan Lemon 
472773bda96SJonathan Lemon static int
473773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq,
474773bda96SJonathan Lemon 	       int on)
475773bda96SJonathan Lemon {
476773bda96SJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
477773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = NULL;
478773bda96SJonathan Lemon 	int err;
479773bda96SJonathan Lemon 
480773bda96SJonathan Lemon 	switch (rq->type) {
481773bda96SJonathan Lemon 	case PTP_CLK_REQ_EXTTS:
482773bda96SJonathan Lemon 		switch (rq->extts.index) {
483773bda96SJonathan Lemon 		case 0:
484773bda96SJonathan Lemon 			ext = bp->ts0;
485773bda96SJonathan Lemon 			break;
486773bda96SJonathan Lemon 		case 1:
487773bda96SJonathan Lemon 			ext = bp->ts1;
488773bda96SJonathan Lemon 			break;
489773bda96SJonathan Lemon 		}
490773bda96SJonathan Lemon 		break;
491773bda96SJonathan Lemon 	case PTP_CLK_REQ_PPS:
492773bda96SJonathan Lemon 		ext = bp->pps;
493773bda96SJonathan Lemon 		break;
494773bda96SJonathan Lemon 	default:
495773bda96SJonathan Lemon 		return -EOPNOTSUPP;
496773bda96SJonathan Lemon 	}
497773bda96SJonathan Lemon 
498773bda96SJonathan Lemon 	err = -ENXIO;
499773bda96SJonathan Lemon 	if (ext)
500773bda96SJonathan Lemon 		err = ext->info->enable(ext, on);
501773bda96SJonathan Lemon 
502773bda96SJonathan Lemon 	return err;
503773bda96SJonathan Lemon }
504773bda96SJonathan Lemon 
505a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = {
506a7e1abadSJonathan Lemon 	.owner		= THIS_MODULE,
507a7e1abadSJonathan Lemon 	.name		= KBUILD_MODNAME,
508a7e1abadSJonathan Lemon 	.max_adj	= 100000000,
509a7e1abadSJonathan Lemon 	.gettimex64	= ptp_ocp_gettimex,
510a7e1abadSJonathan Lemon 	.settime64	= ptp_ocp_settime,
511a7e1abadSJonathan Lemon 	.adjtime	= ptp_ocp_adjtime,
512a7e1abadSJonathan Lemon 	.adjfine	= ptp_ocp_null_adjfine,
513773bda96SJonathan Lemon 	.adjphase	= ptp_ocp_adjphase,
514773bda96SJonathan Lemon 	.enable		= ptp_ocp_enable,
515773bda96SJonathan Lemon 	.pps		= true,
516773bda96SJonathan Lemon 	.n_ext_ts	= 2,
517a7e1abadSJonathan Lemon };
518a7e1abadSJonathan Lemon 
519773bda96SJonathan Lemon static void
520773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp)
521773bda96SJonathan Lemon {
522773bda96SJonathan Lemon 	u32 ctrl, select;
523773bda96SJonathan Lemon 
524773bda96SJonathan Lemon 	select = ioread32(&bp->reg->select);
525773bda96SJonathan Lemon 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
526773bda96SJonathan Lemon 
527773bda96SJonathan Lemon 	iowrite32(0, &bp->reg->drift_ns);
528773bda96SJonathan Lemon 
529773bda96SJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
530773bda96SJonathan Lemon 	ctrl |= OCP_CTRL_ADJUST_DRIFT;
531773bda96SJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
532773bda96SJonathan Lemon 
533773bda96SJonathan Lemon 	/* restore clock selection */
534773bda96SJonathan Lemon 	iowrite32(select >> 16, &bp->reg->select);
535773bda96SJonathan Lemon }
536773bda96SJonathan Lemon 
537773bda96SJonathan Lemon static void
538773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t)
539773bda96SJonathan Lemon {
540773bda96SJonathan Lemon 	struct ptp_ocp *bp = from_timer(bp, t, watchdog);
541773bda96SJonathan Lemon 	unsigned long flags;
542773bda96SJonathan Lemon 	u32 status;
543773bda96SJonathan Lemon 
5440d43d4f2SJonathan Lemon 	status = ioread32(&bp->pps_to_clk->status);
545773bda96SJonathan Lemon 
546773bda96SJonathan Lemon 	if (status & PPS_STATUS_SUPERV_ERR) {
5470d43d4f2SJonathan Lemon 		iowrite32(status, &bp->pps_to_clk->status);
548ef0cfb34SJonathan Lemon 		if (!bp->gnss_lost) {
549773bda96SJonathan Lemon 			spin_lock_irqsave(&bp->lock, flags);
550773bda96SJonathan Lemon 			__ptp_ocp_clear_drift_locked(bp);
551773bda96SJonathan Lemon 			spin_unlock_irqrestore(&bp->lock, flags);
552ef0cfb34SJonathan Lemon 			bp->gnss_lost = ktime_get_real_seconds();
553773bda96SJonathan Lemon 		}
554773bda96SJonathan Lemon 
555ef0cfb34SJonathan Lemon 	} else if (bp->gnss_lost) {
556ef0cfb34SJonathan Lemon 		bp->gnss_lost = 0;
557773bda96SJonathan Lemon 	}
558773bda96SJonathan Lemon 
559773bda96SJonathan Lemon 	mod_timer(&bp->watchdog, jiffies + HZ);
560773bda96SJonathan Lemon }
561773bda96SJonathan Lemon 
562a7e1abadSJonathan Lemon static int
563773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp)
564a7e1abadSJonathan Lemon {
565a7e1abadSJonathan Lemon 	struct timespec64 ts;
566a7e1abadSJonathan Lemon 	bool sync;
567a7e1abadSJonathan Lemon 	u32 ctrl;
568a7e1abadSJonathan Lemon 
569a7e1abadSJonathan Lemon 	/* make sure clock is enabled */
570a7e1abadSJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
571a7e1abadSJonathan Lemon 	ctrl |= OCP_CTRL_ENABLE;
572a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
573a7e1abadSJonathan Lemon 
574773bda96SJonathan Lemon 	/* NO DRIFT Correction */
575773bda96SJonathan Lemon 	/* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */
576773bda96SJonathan Lemon 	iowrite32(0x2000, &bp->reg->servo_offset_p);
577773bda96SJonathan Lemon 	iowrite32(0x1000, &bp->reg->servo_offset_i);
578773bda96SJonathan Lemon 	iowrite32(0,	  &bp->reg->servo_drift_p);
579773bda96SJonathan Lemon 	iowrite32(0,	  &bp->reg->servo_drift_i);
580773bda96SJonathan Lemon 
581773bda96SJonathan Lemon 	/* latch servo values */
582773bda96SJonathan Lemon 	ctrl |= OCP_CTRL_ADJUST_SERVO;
583773bda96SJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
584773bda96SJonathan Lemon 
585a7e1abadSJonathan Lemon 	if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) {
586a7e1abadSJonathan Lemon 		dev_err(&bp->pdev->dev, "clock not enabled\n");
587a7e1abadSJonathan Lemon 		return -ENODEV;
588a7e1abadSJonathan Lemon 	}
589a7e1abadSJonathan Lemon 
590a7e1abadSJonathan Lemon 	sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
591a7e1abadSJonathan Lemon 	if (!sync) {
592a7e1abadSJonathan Lemon 		ktime_get_real_ts64(&ts);
593a7e1abadSJonathan Lemon 		ptp_ocp_settime(&bp->ptp_info, &ts);
594a7e1abadSJonathan Lemon 	}
595a7e1abadSJonathan Lemon 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL))
596a7e1abadSJonathan Lemon 		dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n",
597a7e1abadSJonathan Lemon 			 ts.tv_sec, ts.tv_nsec,
598a7e1abadSJonathan Lemon 			 sync ? "in-sync" : "UNSYNCED");
599a7e1abadSJonathan Lemon 
600773bda96SJonathan Lemon 	timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0);
601773bda96SJonathan Lemon 	mod_timer(&bp->watchdog, jiffies + HZ);
602773bda96SJonathan Lemon 
603a7e1abadSJonathan Lemon 	return 0;
604a7e1abadSJonathan Lemon }
605a7e1abadSJonathan Lemon 
606a7e1abadSJonathan Lemon static void
607a7e1abadSJonathan Lemon ptp_ocp_tod_info(struct ptp_ocp *bp)
608a7e1abadSJonathan Lemon {
609a7e1abadSJonathan Lemon 	static const char * const proto_name[] = {
610a7e1abadSJonathan Lemon 		"NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none",
611a7e1abadSJonathan Lemon 		"UBX", "UBX_UTC", "UBX_LS", "UBX_none"
612a7e1abadSJonathan Lemon 	};
613a7e1abadSJonathan Lemon 	static const char * const gnss_name[] = {
614a7e1abadSJonathan Lemon 		"ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
615a7e1abadSJonathan Lemon 	};
616a7e1abadSJonathan Lemon 	u32 version, ctrl, reg;
617a7e1abadSJonathan Lemon 	int idx;
618a7e1abadSJonathan Lemon 
619a7e1abadSJonathan Lemon 	version = ioread32(&bp->tod->version);
620a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "TOD Version %d.%d.%d\n",
621a7e1abadSJonathan Lemon 		 version >> 24, (version >> 16) & 0xff, version & 0xffff);
622a7e1abadSJonathan Lemon 
623a7e1abadSJonathan Lemon 	ctrl = ioread32(&bp->tod->ctrl);
624a7e1abadSJonathan Lemon 	ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE;
625a7e1abadSJonathan Lemon 	ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B);
626a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->tod->ctrl);
627a7e1abadSJonathan Lemon 
628a7e1abadSJonathan Lemon 	ctrl = ioread32(&bp->tod->ctrl);
629a7e1abadSJonathan Lemon 	idx = ctrl & TOD_CTRL_PROTOCOL ? 4 : 0;
630a7e1abadSJonathan Lemon 	idx += (ctrl >> 16) & 3;
631a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "control: %x\n", ctrl);
632a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "TOD Protocol %s %s\n", proto_name[idx],
633a7e1abadSJonathan Lemon 		 ctrl & TOD_CTRL_ENABLE ? "enabled" : "");
634a7e1abadSJonathan Lemon 
635a7e1abadSJonathan Lemon 	idx = (ctrl >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK;
636a7e1abadSJonathan Lemon 	if (idx < ARRAY_SIZE(gnss_name))
637a7e1abadSJonathan Lemon 		dev_info(&bp->pdev->dev, "GNSS %s\n", gnss_name[idx]);
638a7e1abadSJonathan Lemon 
639a7e1abadSJonathan Lemon 	reg = ioread32(&bp->tod->status);
640a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "status: %x\n", reg);
641a7e1abadSJonathan Lemon 
642a7e1abadSJonathan Lemon 	reg = ioread32(&bp->tod->correction_sec);
643a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "correction: %d\n", reg);
644a7e1abadSJonathan Lemon 
645a7e1abadSJonathan Lemon 	reg = ioread32(&bp->tod->utc_status);
646a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "utc_status: %x\n", reg);
647a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "utc_offset: %d  valid:%d  leap_valid:%d\n",
648a7e1abadSJonathan Lemon 		 reg & TOD_STATUS_UTC_MASK, reg & TOD_STATUS_UTC_VALID ? 1 : 0,
649a7e1abadSJonathan Lemon 		 reg & TOD_STATUS_LEAP_VALID ? 1 : 0);
650a7e1abadSJonathan Lemon }
651a7e1abadSJonathan Lemon 
652773bda96SJonathan Lemon static int
653773bda96SJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data)
654773bda96SJonathan Lemon {
655773bda96SJonathan Lemon 	return 1;
656773bda96SJonathan Lemon }
657773bda96SJonathan Lemon 
658773bda96SJonathan Lemon static int
659773bda96SJonathan Lemon ptp_ocp_read_i2c(struct i2c_adapter *adap, u8 addr, u8 reg, u8 sz, u8 *data)
660773bda96SJonathan Lemon {
661773bda96SJonathan Lemon 	struct i2c_msg msgs[2] = {
662773bda96SJonathan Lemon 		{
663773bda96SJonathan Lemon 			.addr = addr,
664773bda96SJonathan Lemon 			.len = 1,
665773bda96SJonathan Lemon 			.buf = &reg,
666773bda96SJonathan Lemon 		},
667773bda96SJonathan Lemon 		{
668773bda96SJonathan Lemon 			.addr = addr,
669773bda96SJonathan Lemon 			.flags = I2C_M_RD,
670773bda96SJonathan Lemon 			.len = 2,
671773bda96SJonathan Lemon 			.buf = data,
672773bda96SJonathan Lemon 		},
673773bda96SJonathan Lemon 	};
674773bda96SJonathan Lemon 	int err;
675773bda96SJonathan Lemon 	u8 len;
676773bda96SJonathan Lemon 
677773bda96SJonathan Lemon 	/* xiic-i2c for some stupid reason only does 2 byte reads. */
678773bda96SJonathan Lemon 	while (sz) {
679773bda96SJonathan Lemon 		len = min_t(u8, sz, 2);
680773bda96SJonathan Lemon 		msgs[1].len = len;
681773bda96SJonathan Lemon 		err = i2c_transfer(adap, msgs, 2);
682773bda96SJonathan Lemon 		if (err != msgs[1].len)
683773bda96SJonathan Lemon 			return err;
684773bda96SJonathan Lemon 		msgs[1].buf += len;
685773bda96SJonathan Lemon 		reg += len;
686773bda96SJonathan Lemon 		sz -= len;
687773bda96SJonathan Lemon 	}
688773bda96SJonathan Lemon 	return 0;
689773bda96SJonathan Lemon }
690773bda96SJonathan Lemon 
691773bda96SJonathan Lemon static void
692773bda96SJonathan Lemon ptp_ocp_get_serial_number(struct ptp_ocp *bp)
693773bda96SJonathan Lemon {
694773bda96SJonathan Lemon 	struct i2c_adapter *adap;
695773bda96SJonathan Lemon 	struct device *dev;
696773bda96SJonathan Lemon 	int err;
697773bda96SJonathan Lemon 
698773bda96SJonathan Lemon 	dev = device_find_child(&bp->i2c_ctrl->dev, NULL, ptp_ocp_firstchild);
699773bda96SJonathan Lemon 	if (!dev) {
700773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "Can't find I2C adapter\n");
701773bda96SJonathan Lemon 		return;
702773bda96SJonathan Lemon 	}
703773bda96SJonathan Lemon 
704773bda96SJonathan Lemon 	adap = i2c_verify_adapter(dev);
705773bda96SJonathan Lemon 	if (!adap) {
706773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "device '%s' isn't an I2C adapter\n",
707773bda96SJonathan Lemon 			dev_name(dev));
708773bda96SJonathan Lemon 		goto out;
709773bda96SJonathan Lemon 	}
710773bda96SJonathan Lemon 
711773bda96SJonathan Lemon 	err = ptp_ocp_read_i2c(adap, 0x58, 0x9A, 6, bp->serial);
712773bda96SJonathan Lemon 	if (err) {
713773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", err);
714773bda96SJonathan Lemon 		goto out;
715773bda96SJonathan Lemon 	}
716773bda96SJonathan Lemon 
717773bda96SJonathan Lemon 	bp->has_serial = true;
718773bda96SJonathan Lemon 
719773bda96SJonathan Lemon out:
720773bda96SJonathan Lemon 	put_device(dev);
721773bda96SJonathan Lemon }
722773bda96SJonathan Lemon 
723a7e1abadSJonathan Lemon static void
724a7e1abadSJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp)
725a7e1abadSJonathan Lemon {
726a7e1abadSJonathan Lemon 	u32 version, select;
727a7e1abadSJonathan Lemon 
728a7e1abadSJonathan Lemon 	version = ioread32(&bp->reg->version);
729a7e1abadSJonathan Lemon 	select = ioread32(&bp->reg->select);
730a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n",
731a7e1abadSJonathan Lemon 		 version >> 24, (version >> 16) & 0xff, version & 0xffff,
732773bda96SJonathan Lemon 		 ptp_ocp_clock_name_from_val(select >> 16),
733a7e1abadSJonathan Lemon 		 ptp_clock_index(bp->ptp));
734a7e1abadSJonathan Lemon 
735a7e1abadSJonathan Lemon 	ptp_ocp_tod_info(bp);
736a7e1abadSJonathan Lemon }
737a7e1abadSJonathan Lemon 
738773bda96SJonathan Lemon static struct device *
739773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp)
740773bda96SJonathan Lemon {
741773bda96SJonathan Lemon 	struct device *dev, *last;
742773bda96SJonathan Lemon 
743773bda96SJonathan Lemon 	last = NULL;
744773bda96SJonathan Lemon 	dev = &bp->spi_flash->dev;
745773bda96SJonathan Lemon 
746773bda96SJonathan Lemon 	while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) {
747773bda96SJonathan Lemon 		if (!strcmp("mtd", dev_bus_name(dev)))
748773bda96SJonathan Lemon 			break;
749773bda96SJonathan Lemon 		put_device(last);
750773bda96SJonathan Lemon 		last = dev;
751773bda96SJonathan Lemon 	}
752773bda96SJonathan Lemon 	put_device(last);
753773bda96SJonathan Lemon 
754773bda96SJonathan Lemon 	return dev;
755773bda96SJonathan Lemon }
756773bda96SJonathan Lemon 
757773bda96SJonathan Lemon static int
758773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev,
759773bda96SJonathan Lemon 		      const struct firmware *fw)
760773bda96SJonathan Lemon {
761773bda96SJonathan Lemon 	struct mtd_info *mtd = dev_get_drvdata(dev);
762773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
763773bda96SJonathan Lemon 	size_t off, len, resid, wrote;
764773bda96SJonathan Lemon 	struct erase_info erase;
765773bda96SJonathan Lemon 	size_t base, blksz;
766*7c807572SJonathan Lemon 	int err = 0;
767773bda96SJonathan Lemon 
768773bda96SJonathan Lemon 	off = 0;
769773bda96SJonathan Lemon 	base = bp->flash_start;
770773bda96SJonathan Lemon 	blksz = 4096;
771773bda96SJonathan Lemon 	resid = fw->size;
772773bda96SJonathan Lemon 
773773bda96SJonathan Lemon 	while (resid) {
774773bda96SJonathan Lemon 		devlink_flash_update_status_notify(devlink, "Flashing",
775773bda96SJonathan Lemon 						   NULL, off, fw->size);
776773bda96SJonathan Lemon 
777773bda96SJonathan Lemon 		len = min_t(size_t, resid, blksz);
778773bda96SJonathan Lemon 		erase.addr = base + off;
779773bda96SJonathan Lemon 		erase.len = blksz;
780773bda96SJonathan Lemon 
781773bda96SJonathan Lemon 		err = mtd_erase(mtd, &erase);
782773bda96SJonathan Lemon 		if (err)
783773bda96SJonathan Lemon 			goto out;
784773bda96SJonathan Lemon 
785773bda96SJonathan Lemon 		err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]);
786773bda96SJonathan Lemon 		if (err)
787773bda96SJonathan Lemon 			goto out;
788773bda96SJonathan Lemon 
789773bda96SJonathan Lemon 		off += blksz;
790773bda96SJonathan Lemon 		resid -= len;
791773bda96SJonathan Lemon 	}
792773bda96SJonathan Lemon out:
793773bda96SJonathan Lemon 	return err;
794773bda96SJonathan Lemon }
795773bda96SJonathan Lemon 
796773bda96SJonathan Lemon static int
797773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink,
798773bda96SJonathan Lemon 			     struct devlink_flash_update_params *params,
799773bda96SJonathan Lemon 			     struct netlink_ext_ack *extack)
800773bda96SJonathan Lemon {
801773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
802773bda96SJonathan Lemon 	struct device *dev;
803773bda96SJonathan Lemon 	const char *msg;
804773bda96SJonathan Lemon 	int err;
805773bda96SJonathan Lemon 
806773bda96SJonathan Lemon 	dev = ptp_ocp_find_flash(bp);
807773bda96SJonathan Lemon 	if (!dev) {
808773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n");
809773bda96SJonathan Lemon 		return -ENODEV;
810773bda96SJonathan Lemon 	}
811773bda96SJonathan Lemon 
812773bda96SJonathan Lemon 	devlink_flash_update_status_notify(devlink, "Preparing to flash",
813773bda96SJonathan Lemon 					   NULL, 0, 0);
814773bda96SJonathan Lemon 
815773bda96SJonathan Lemon 	err = ptp_ocp_devlink_flash(devlink, dev, params->fw);
816773bda96SJonathan Lemon 
817773bda96SJonathan Lemon 	msg = err ? "Flash error" : "Flash complete";
818773bda96SJonathan Lemon 	devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0);
819773bda96SJonathan Lemon 
820773bda96SJonathan Lemon 	put_device(dev);
821773bda96SJonathan Lemon 	return err;
822773bda96SJonathan Lemon }
823773bda96SJonathan Lemon 
824773bda96SJonathan Lemon static int
825773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
826773bda96SJonathan Lemon 			 struct netlink_ext_ack *extack)
827773bda96SJonathan Lemon {
828773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
829773bda96SJonathan Lemon 	char buf[32];
830773bda96SJonathan Lemon 	int err;
831773bda96SJonathan Lemon 
832773bda96SJonathan Lemon 	err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
833773bda96SJonathan Lemon 	if (err)
834773bda96SJonathan Lemon 		return err;
835773bda96SJonathan Lemon 
836773bda96SJonathan Lemon 	if (bp->image) {
837773bda96SJonathan Lemon 		u32 ver = ioread32(&bp->image->version);
838773bda96SJonathan Lemon 
839773bda96SJonathan Lemon 		if (ver & 0xffff) {
840773bda96SJonathan Lemon 			sprintf(buf, "%d", ver);
841773bda96SJonathan Lemon 			err = devlink_info_version_running_put(req,
8421a052da9SJonathan Lemon 							       "fw",
843773bda96SJonathan Lemon 							       buf);
844773bda96SJonathan Lemon 		} else {
845773bda96SJonathan Lemon 			sprintf(buf, "%d", ver >> 16);
846773bda96SJonathan Lemon 			err = devlink_info_version_running_put(req,
8471a052da9SJonathan Lemon 							       "loader",
848773bda96SJonathan Lemon 							       buf);
849773bda96SJonathan Lemon 		}
850773bda96SJonathan Lemon 		if (err)
851773bda96SJonathan Lemon 			return err;
852773bda96SJonathan Lemon 	}
853773bda96SJonathan Lemon 
854773bda96SJonathan Lemon 	if (!bp->has_serial)
855773bda96SJonathan Lemon 		ptp_ocp_get_serial_number(bp);
856773bda96SJonathan Lemon 
857773bda96SJonathan Lemon 	if (bp->has_serial) {
858773bda96SJonathan Lemon 		sprintf(buf, "%pM", bp->serial);
859773bda96SJonathan Lemon 		err = devlink_info_serial_number_put(req, buf);
860773bda96SJonathan Lemon 		if (err)
861773bda96SJonathan Lemon 			return err;
862773bda96SJonathan Lemon 	}
863773bda96SJonathan Lemon 
864773bda96SJonathan Lemon 	return 0;
865773bda96SJonathan Lemon }
866773bda96SJonathan Lemon 
867773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = {
868773bda96SJonathan Lemon 	.flash_update = ptp_ocp_devlink_flash_update,
869773bda96SJonathan Lemon 	.info_get = ptp_ocp_devlink_info_get,
870773bda96SJonathan Lemon };
871773bda96SJonathan Lemon 
872773bda96SJonathan Lemon static void __iomem *
873773bda96SJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size)
874773bda96SJonathan Lemon {
875773bda96SJonathan Lemon 	struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp");
876773bda96SJonathan Lemon 
877773bda96SJonathan Lemon 	return devm_ioremap_resource(&bp->pdev->dev, &res);
878773bda96SJonathan Lemon }
879773bda96SJonathan Lemon 
880773bda96SJonathan Lemon static void __iomem *
881773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r)
882773bda96SJonathan Lemon {
883773bda96SJonathan Lemon 	unsigned long start;
884773bda96SJonathan Lemon 
885773bda96SJonathan Lemon 	start = pci_resource_start(bp->pdev, 0) + r->offset;
886773bda96SJonathan Lemon 	return __ptp_ocp_get_mem(bp, start, r->size);
887773bda96SJonathan Lemon }
888773bda96SJonathan Lemon 
889773bda96SJonathan Lemon static void
890773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq)
891773bda96SJonathan Lemon {
892773bda96SJonathan Lemon 	struct resource r = DEFINE_RES_IRQ(irq);
893773bda96SJonathan Lemon 	*res = r;
894773bda96SJonathan Lemon }
895773bda96SJonathan Lemon 
896773bda96SJonathan Lemon static void
897773bda96SJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size)
898773bda96SJonathan Lemon {
899773bda96SJonathan Lemon 	struct resource r = DEFINE_RES_MEM(start, size);
900773bda96SJonathan Lemon 	*res = r;
901773bda96SJonathan Lemon }
902773bda96SJonathan Lemon 
903773bda96SJonathan Lemon static int
904773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r)
905773bda96SJonathan Lemon {
906773bda96SJonathan Lemon 	struct ptp_ocp_flash_info *info;
907773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
908773bda96SJonathan Lemon 	struct platform_device *p;
909773bda96SJonathan Lemon 	struct resource res[2];
910773bda96SJonathan Lemon 	unsigned long start;
911773bda96SJonathan Lemon 	int id;
912773bda96SJonathan Lemon 
913773bda96SJonathan Lemon 	/* XXX hack to work around old FPGA */
914773bda96SJonathan Lemon 	if (bp->n_irqs < 10) {
915773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "FPGA does not have SPI devices\n");
916773bda96SJonathan Lemon 		return 0;
917773bda96SJonathan Lemon 	}
918773bda96SJonathan Lemon 
919773bda96SJonathan Lemon 	if (r->irq_vec > bp->n_irqs) {
920773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "spi device irq %d out of range\n",
921773bda96SJonathan Lemon 			r->irq_vec);
922773bda96SJonathan Lemon 		return 0;
923773bda96SJonathan Lemon 	}
924773bda96SJonathan Lemon 
925773bda96SJonathan Lemon 	start = pci_resource_start(pdev, 0) + r->offset;
926773bda96SJonathan Lemon 	ptp_ocp_set_mem_resource(&res[0], start, r->size);
927773bda96SJonathan Lemon 	ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
928773bda96SJonathan Lemon 
929773bda96SJonathan Lemon 	info = r->extra;
930773bda96SJonathan Lemon 	id = pci_dev_id(pdev) << 1;
931773bda96SJonathan Lemon 	id += info->pci_offset;
932773bda96SJonathan Lemon 
933773bda96SJonathan Lemon 	p = platform_device_register_resndata(&pdev->dev, info->name, id,
934773bda96SJonathan Lemon 					      res, 2, info->data,
935773bda96SJonathan Lemon 					      info->data_size);
936773bda96SJonathan Lemon 	if (IS_ERR(p))
937773bda96SJonathan Lemon 		return PTR_ERR(p);
938773bda96SJonathan Lemon 
939773bda96SJonathan Lemon 	bp_assign_entry(bp, r, p);
940773bda96SJonathan Lemon 
941773bda96SJonathan Lemon 	return 0;
942773bda96SJonathan Lemon }
943773bda96SJonathan Lemon 
944773bda96SJonathan Lemon static struct platform_device *
945773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id)
946773bda96SJonathan Lemon {
947773bda96SJonathan Lemon 	struct resource res[2];
948773bda96SJonathan Lemon 	unsigned long start;
949773bda96SJonathan Lemon 
950773bda96SJonathan Lemon 	start = pci_resource_start(pdev, 0) + r->offset;
951773bda96SJonathan Lemon 	ptp_ocp_set_mem_resource(&res[0], start, r->size);
952773bda96SJonathan Lemon 	ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
953773bda96SJonathan Lemon 
954773bda96SJonathan Lemon 	return platform_device_register_resndata(&pdev->dev, "xiic-i2c",
955773bda96SJonathan Lemon 						 id, res, 2, NULL, 0);
956773bda96SJonathan Lemon }
957773bda96SJonathan Lemon 
958773bda96SJonathan Lemon static int
959773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r)
960773bda96SJonathan Lemon {
961773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
962773bda96SJonathan Lemon 	struct platform_device *p;
963773bda96SJonathan Lemon 	struct clk_hw *clk;
964773bda96SJonathan Lemon 	char buf[32];
965773bda96SJonathan Lemon 	int id;
966773bda96SJonathan Lemon 
967773bda96SJonathan Lemon 	if (r->irq_vec > bp->n_irqs) {
968773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "i2c device irq %d out of range\n",
969773bda96SJonathan Lemon 			r->irq_vec);
970773bda96SJonathan Lemon 		return 0;
971773bda96SJonathan Lemon 	}
972773bda96SJonathan Lemon 
973773bda96SJonathan Lemon 	id = pci_dev_id(bp->pdev);
974773bda96SJonathan Lemon 
975773bda96SJonathan Lemon 	sprintf(buf, "AXI.%d", id);
976773bda96SJonathan Lemon 	clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 50000000);
977773bda96SJonathan Lemon 	if (IS_ERR(clk))
978773bda96SJonathan Lemon 		return PTR_ERR(clk);
979773bda96SJonathan Lemon 	bp->i2c_clk = clk;
980773bda96SJonathan Lemon 
981773bda96SJonathan Lemon 	sprintf(buf, "xiic-i2c.%d", id);
982773bda96SJonathan Lemon 	devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf);
983773bda96SJonathan Lemon 	p = ptp_ocp_i2c_bus(bp->pdev, r, id);
984773bda96SJonathan Lemon 	if (IS_ERR(p))
985773bda96SJonathan Lemon 		return PTR_ERR(p);
986773bda96SJonathan Lemon 
987773bda96SJonathan Lemon 	bp_assign_entry(bp, r, p);
988773bda96SJonathan Lemon 
989773bda96SJonathan Lemon 	return 0;
990773bda96SJonathan Lemon }
991773bda96SJonathan Lemon 
992773bda96SJonathan Lemon static irqreturn_t
993773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv)
994773bda96SJonathan Lemon {
995773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
996773bda96SJonathan Lemon 	struct ts_reg __iomem *reg = ext->mem;
997773bda96SJonathan Lemon 	struct ptp_clock_event ev;
998773bda96SJonathan Lemon 	u32 sec, nsec;
999773bda96SJonathan Lemon 
1000773bda96SJonathan Lemon 	/* XXX should fix API - this converts s/ns -> ts -> s/ns */
1001773bda96SJonathan Lemon 	sec = ioread32(&reg->time_sec);
1002773bda96SJonathan Lemon 	nsec = ioread32(&reg->time_ns);
1003773bda96SJonathan Lemon 
1004773bda96SJonathan Lemon 	ev.type = PTP_CLOCK_EXTTS;
1005773bda96SJonathan Lemon 	ev.index = ext->info->index;
1006773bda96SJonathan Lemon 	ev.timestamp = sec * 1000000000ULL + nsec;
1007773bda96SJonathan Lemon 
1008773bda96SJonathan Lemon 	ptp_clock_event(ext->bp->ptp, &ev);
1009773bda96SJonathan Lemon 
1010773bda96SJonathan Lemon 	iowrite32(1, &reg->intr);	/* write 1 to ack */
1011773bda96SJonathan Lemon 
1012773bda96SJonathan Lemon 	return IRQ_HANDLED;
1013773bda96SJonathan Lemon }
1014773bda96SJonathan Lemon 
1015773bda96SJonathan Lemon static int
1016773bda96SJonathan Lemon ptp_ocp_ts_enable(void *priv, bool enable)
1017773bda96SJonathan Lemon {
1018773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1019773bda96SJonathan Lemon 	struct ts_reg __iomem *reg = ext->mem;
1020773bda96SJonathan Lemon 
1021773bda96SJonathan Lemon 	if (enable) {
1022773bda96SJonathan Lemon 		iowrite32(1, &reg->enable);
1023773bda96SJonathan Lemon 		iowrite32(1, &reg->intr_mask);
1024773bda96SJonathan Lemon 		iowrite32(1, &reg->intr);
1025773bda96SJonathan Lemon 	} else {
1026773bda96SJonathan Lemon 		iowrite32(0, &reg->intr_mask);
1027773bda96SJonathan Lemon 		iowrite32(0, &reg->enable);
1028773bda96SJonathan Lemon 	}
1029773bda96SJonathan Lemon 
1030773bda96SJonathan Lemon 	return 0;
1031773bda96SJonathan Lemon }
1032773bda96SJonathan Lemon 
1033773bda96SJonathan Lemon static void
1034773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext)
1035773bda96SJonathan Lemon {
1036773bda96SJonathan Lemon 	ext->info->enable(ext, false);
1037773bda96SJonathan Lemon 	pci_free_irq(ext->bp->pdev, ext->irq_vec, ext);
1038773bda96SJonathan Lemon 	kfree(ext);
1039773bda96SJonathan Lemon }
1040773bda96SJonathan Lemon 
1041773bda96SJonathan Lemon static int
1042773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r)
1043773bda96SJonathan Lemon {
1044773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
1045773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext;
1046773bda96SJonathan Lemon 	int err;
1047773bda96SJonathan Lemon 
1048773bda96SJonathan Lemon 	ext = kzalloc(sizeof(*ext), GFP_KERNEL);
1049773bda96SJonathan Lemon 	if (!ext)
1050773bda96SJonathan Lemon 		return -ENOMEM;
1051773bda96SJonathan Lemon 
1052773bda96SJonathan Lemon 	err = -EINVAL;
1053773bda96SJonathan Lemon 	ext->mem = ptp_ocp_get_mem(bp, r);
1054773bda96SJonathan Lemon 	if (!ext->mem)
1055773bda96SJonathan Lemon 		goto out;
1056773bda96SJonathan Lemon 
1057773bda96SJonathan Lemon 	ext->bp = bp;
1058773bda96SJonathan Lemon 	ext->info = r->extra;
1059773bda96SJonathan Lemon 	ext->irq_vec = r->irq_vec;
1060773bda96SJonathan Lemon 
1061773bda96SJonathan Lemon 	err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL,
1062773bda96SJonathan Lemon 			      ext, "ocp%d.%s", bp->id, ext->info->name);
1063773bda96SJonathan Lemon 	if (err) {
1064773bda96SJonathan Lemon 		dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec);
1065773bda96SJonathan Lemon 		goto out;
1066773bda96SJonathan Lemon 	}
1067773bda96SJonathan Lemon 
1068773bda96SJonathan Lemon 	bp_assign_entry(bp, r, ext);
1069773bda96SJonathan Lemon 
1070773bda96SJonathan Lemon 	return 0;
1071773bda96SJonathan Lemon 
1072773bda96SJonathan Lemon out:
1073773bda96SJonathan Lemon 	kfree(ext);
1074773bda96SJonathan Lemon 	return err;
1075773bda96SJonathan Lemon }
1076773bda96SJonathan Lemon 
1077773bda96SJonathan Lemon static int
1078773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r)
1079773bda96SJonathan Lemon {
1080773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
1081773bda96SJonathan Lemon 	struct uart_8250_port uart;
1082773bda96SJonathan Lemon 
1083773bda96SJonathan Lemon 	/* Setting UPF_IOREMAP and leaving port.membase unspecified lets
1084773bda96SJonathan Lemon 	 * the serial port device claim and release the pci resource.
1085773bda96SJonathan Lemon 	 */
1086773bda96SJonathan Lemon 	memset(&uart, 0, sizeof(uart));
1087773bda96SJonathan Lemon 	uart.port.dev = &pdev->dev;
1088773bda96SJonathan Lemon 	uart.port.iotype = UPIO_MEM;
1089773bda96SJonathan Lemon 	uart.port.regshift = 2;
1090773bda96SJonathan Lemon 	uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset;
1091773bda96SJonathan Lemon 	uart.port.irq = pci_irq_vector(pdev, r->irq_vec);
1092773bda96SJonathan Lemon 	uart.port.uartclk = 50000000;
1093773bda96SJonathan Lemon 	uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP;
1094773bda96SJonathan Lemon 	uart.port.type = PORT_16550A;
1095773bda96SJonathan Lemon 
1096773bda96SJonathan Lemon 	return serial8250_register_8250_port(&uart);
1097773bda96SJonathan Lemon }
1098773bda96SJonathan Lemon 
1099773bda96SJonathan Lemon static int
1100773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r)
1101773bda96SJonathan Lemon {
1102773bda96SJonathan Lemon 	int port;
1103773bda96SJonathan Lemon 
1104773bda96SJonathan Lemon 	if (r->irq_vec > bp->n_irqs) {
1105773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "serial device irq %d out of range\n",
1106773bda96SJonathan Lemon 			r->irq_vec);
1107773bda96SJonathan Lemon 		return 0;
1108773bda96SJonathan Lemon 	}
1109773bda96SJonathan Lemon 
1110773bda96SJonathan Lemon 	port = ptp_ocp_serial_line(bp, r);
1111773bda96SJonathan Lemon 	if (port < 0)
1112773bda96SJonathan Lemon 		return port;
1113773bda96SJonathan Lemon 
1114773bda96SJonathan Lemon 	bp_assign_entry(bp, r, port);
1115773bda96SJonathan Lemon 
1116773bda96SJonathan Lemon 	return 0;
1117773bda96SJonathan Lemon }
1118773bda96SJonathan Lemon 
1119773bda96SJonathan Lemon static int
1120773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1121773bda96SJonathan Lemon {
1122773bda96SJonathan Lemon 	void __iomem *mem;
1123773bda96SJonathan Lemon 
1124773bda96SJonathan Lemon 	mem = ptp_ocp_get_mem(bp, r);
1125773bda96SJonathan Lemon 	if (!mem)
1126773bda96SJonathan Lemon 		return -EINVAL;
1127773bda96SJonathan Lemon 
1128773bda96SJonathan Lemon 	bp_assign_entry(bp, r, mem);
1129773bda96SJonathan Lemon 
1130773bda96SJonathan Lemon 	return 0;
1131773bda96SJonathan Lemon }
1132773bda96SJonathan Lemon 
1133773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */
1134773bda96SJonathan Lemon static int
1135773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
1136773bda96SJonathan Lemon {
1137773bda96SJonathan Lemon 	bp->flash_start = 1024 * 4096;
1138773bda96SJonathan Lemon 
1139773bda96SJonathan Lemon 	return ptp_ocp_init_clock(bp);
1140773bda96SJonathan Lemon }
1141773bda96SJonathan Lemon 
1142773bda96SJonathan Lemon static int
1143773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data)
1144773bda96SJonathan Lemon {
1145773bda96SJonathan Lemon 	struct ocp_resource *r, *table;
1146773bda96SJonathan Lemon 	int err = 0;
1147773bda96SJonathan Lemon 
1148773bda96SJonathan Lemon 	table = (struct ocp_resource *)driver_data;
1149773bda96SJonathan Lemon 	for (r = table; r->setup; r++) {
1150773bda96SJonathan Lemon 		err = r->setup(bp, r);
1151773bda96SJonathan Lemon 		if (err)
1152773bda96SJonathan Lemon 			break;
1153773bda96SJonathan Lemon 	}
1154773bda96SJonathan Lemon 	return err;
1155773bda96SJonathan Lemon }
1156773bda96SJonathan Lemon 
1157773bda96SJonathan Lemon static ssize_t
1158773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
1159773bda96SJonathan Lemon {
1160773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1161773bda96SJonathan Lemon 
1162773bda96SJonathan Lemon 	if (!bp->has_serial)
1163773bda96SJonathan Lemon 		ptp_ocp_get_serial_number(bp);
1164773bda96SJonathan Lemon 
1165773bda96SJonathan Lemon 	return sysfs_emit(buf, "%pM\n", bp->serial);
1166773bda96SJonathan Lemon }
1167773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum);
1168773bda96SJonathan Lemon 
1169773bda96SJonathan Lemon static ssize_t
1170ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
1171773bda96SJonathan Lemon {
1172773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1173773bda96SJonathan Lemon 	ssize_t ret;
1174773bda96SJonathan Lemon 
1175ef0cfb34SJonathan Lemon 	if (bp->gnss_lost)
1176ef0cfb34SJonathan Lemon 		ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost);
1177773bda96SJonathan Lemon 	else
1178773bda96SJonathan Lemon 		ret = sysfs_emit(buf, "SYNC\n");
1179773bda96SJonathan Lemon 
1180773bda96SJonathan Lemon 	return ret;
1181773bda96SJonathan Lemon }
1182ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync);
1183773bda96SJonathan Lemon 
1184773bda96SJonathan Lemon static ssize_t
1185773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf)
1186773bda96SJonathan Lemon {
1187773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1188773bda96SJonathan Lemon 	const char *p;
1189773bda96SJonathan Lemon 	u32 select;
1190773bda96SJonathan Lemon 
1191773bda96SJonathan Lemon 	select = ioread32(&bp->reg->select);
1192773bda96SJonathan Lemon 	p = ptp_ocp_clock_name_from_val(select >> 16);
1193773bda96SJonathan Lemon 
1194773bda96SJonathan Lemon 	return sysfs_emit(buf, "%s\n", p);
1195773bda96SJonathan Lemon }
1196773bda96SJonathan Lemon 
1197773bda96SJonathan Lemon static ssize_t
1198773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr,
1199773bda96SJonathan Lemon 		   const char *buf, size_t count)
1200773bda96SJonathan Lemon {
1201773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1202773bda96SJonathan Lemon 	unsigned long flags;
1203773bda96SJonathan Lemon 	int val;
1204773bda96SJonathan Lemon 
1205773bda96SJonathan Lemon 	val = ptp_ocp_clock_val_from_name(buf);
1206773bda96SJonathan Lemon 	if (val < 0)
1207773bda96SJonathan Lemon 		return val;
1208773bda96SJonathan Lemon 
1209773bda96SJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
1210773bda96SJonathan Lemon 	iowrite32(val, &bp->reg->select);
1211773bda96SJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
1212773bda96SJonathan Lemon 
1213773bda96SJonathan Lemon 	return count;
1214773bda96SJonathan Lemon }
1215773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source);
1216773bda96SJonathan Lemon 
1217773bda96SJonathan Lemon static ssize_t
1218773bda96SJonathan Lemon available_clock_sources_show(struct device *dev,
1219773bda96SJonathan Lemon 			     struct device_attribute *attr, char *buf)
1220773bda96SJonathan Lemon {
1221773bda96SJonathan Lemon 	const char *clk;
1222773bda96SJonathan Lemon 	ssize_t count;
1223773bda96SJonathan Lemon 	int i;
1224773bda96SJonathan Lemon 
1225773bda96SJonathan Lemon 	count = 0;
1226773bda96SJonathan Lemon 	for (i = 0; i < ARRAY_SIZE(ptp_ocp_clock); i++) {
1227773bda96SJonathan Lemon 		clk = ptp_ocp_clock[i].name;
1228773bda96SJonathan Lemon 		count += sysfs_emit_at(buf, count, "%s ", clk);
1229773bda96SJonathan Lemon 	}
1230773bda96SJonathan Lemon 	if (count)
1231773bda96SJonathan Lemon 		count--;
1232773bda96SJonathan Lemon 	count += sysfs_emit_at(buf, count, "\n");
1233773bda96SJonathan Lemon 	return count;
1234773bda96SJonathan Lemon }
1235773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources);
1236773bda96SJonathan Lemon 
1237773bda96SJonathan Lemon static struct attribute *timecard_attrs[] = {
1238773bda96SJonathan Lemon 	&dev_attr_serialnum.attr,
1239ef0cfb34SJonathan Lemon 	&dev_attr_gnss_sync.attr,
1240773bda96SJonathan Lemon 	&dev_attr_clock_source.attr,
1241773bda96SJonathan Lemon 	&dev_attr_available_clock_sources.attr,
1242773bda96SJonathan Lemon 	NULL,
1243773bda96SJonathan Lemon };
1244773bda96SJonathan Lemon ATTRIBUTE_GROUPS(timecard);
1245773bda96SJonathan Lemon 
1246773bda96SJonathan Lemon static void
1247773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev)
1248773bda96SJonathan Lemon {
1249773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1250773bda96SJonathan Lemon 
1251773bda96SJonathan Lemon 	mutex_lock(&ptp_ocp_lock);
1252773bda96SJonathan Lemon 	idr_remove(&ptp_ocp_idr, bp->id);
1253773bda96SJonathan Lemon 	mutex_unlock(&ptp_ocp_lock);
1254773bda96SJonathan Lemon }
1255773bda96SJonathan Lemon 
1256773bda96SJonathan Lemon static int
1257773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
1258773bda96SJonathan Lemon {
1259773bda96SJonathan Lemon 	int err;
1260773bda96SJonathan Lemon 
1261773bda96SJonathan Lemon 	mutex_lock(&ptp_ocp_lock);
1262773bda96SJonathan Lemon 	err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL);
1263773bda96SJonathan Lemon 	mutex_unlock(&ptp_ocp_lock);
1264773bda96SJonathan Lemon 	if (err < 0) {
1265773bda96SJonathan Lemon 		dev_err(&pdev->dev, "idr_alloc failed: %d\n", err);
1266773bda96SJonathan Lemon 		return err;
1267773bda96SJonathan Lemon 	}
1268773bda96SJonathan Lemon 	bp->id = err;
1269773bda96SJonathan Lemon 
1270773bda96SJonathan Lemon 	bp->ptp_info = ptp_ocp_clock_info;
1271773bda96SJonathan Lemon 	spin_lock_init(&bp->lock);
1272ef0cfb34SJonathan Lemon 	bp->gnss_port = -1;
1273773bda96SJonathan Lemon 	bp->mac_port = -1;
1274773bda96SJonathan Lemon 	bp->pdev = pdev;
1275773bda96SJonathan Lemon 
1276773bda96SJonathan Lemon 	device_initialize(&bp->dev);
1277773bda96SJonathan Lemon 	dev_set_name(&bp->dev, "ocp%d", bp->id);
1278773bda96SJonathan Lemon 	bp->dev.class = &timecard_class;
1279773bda96SJonathan Lemon 	bp->dev.parent = &pdev->dev;
1280773bda96SJonathan Lemon 	bp->dev.release = ptp_ocp_dev_release;
1281773bda96SJonathan Lemon 	dev_set_drvdata(&bp->dev, bp);
1282773bda96SJonathan Lemon 
1283773bda96SJonathan Lemon 	err = device_add(&bp->dev);
1284773bda96SJonathan Lemon 	if (err) {
1285773bda96SJonathan Lemon 		dev_err(&bp->dev, "device add failed: %d\n", err);
1286773bda96SJonathan Lemon 		goto out;
1287773bda96SJonathan Lemon 	}
1288773bda96SJonathan Lemon 
1289773bda96SJonathan Lemon 	pci_set_drvdata(pdev, bp);
1290773bda96SJonathan Lemon 
1291773bda96SJonathan Lemon 	return 0;
1292773bda96SJonathan Lemon 
1293773bda96SJonathan Lemon out:
1294773bda96SJonathan Lemon 	ptp_ocp_dev_release(&bp->dev);
1295d12f23faSJonathan Lemon 	put_device(&bp->dev);
1296773bda96SJonathan Lemon 	return err;
1297773bda96SJonathan Lemon }
1298773bda96SJonathan Lemon 
1299773bda96SJonathan Lemon static void
1300773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link)
1301773bda96SJonathan Lemon {
1302773bda96SJonathan Lemon 	struct device *dev = &bp->dev;
1303773bda96SJonathan Lemon 
1304773bda96SJonathan Lemon 	if (sysfs_create_link(&dev->kobj, &child->kobj, link))
1305773bda96SJonathan Lemon 		dev_err(dev, "%s symlink failed\n", link);
1306773bda96SJonathan Lemon }
1307773bda96SJonathan Lemon 
1308773bda96SJonathan Lemon static void
1309773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link)
1310773bda96SJonathan Lemon {
1311773bda96SJonathan Lemon 	struct device *dev, *child;
1312773bda96SJonathan Lemon 
1313773bda96SJonathan Lemon 	dev = &bp->pdev->dev;
1314773bda96SJonathan Lemon 
1315773bda96SJonathan Lemon 	child = device_find_child_by_name(dev, name);
1316773bda96SJonathan Lemon 	if (!child) {
1317773bda96SJonathan Lemon 		dev_err(dev, "Could not find device %s\n", name);
1318773bda96SJonathan Lemon 		return;
1319773bda96SJonathan Lemon 	}
1320773bda96SJonathan Lemon 
1321773bda96SJonathan Lemon 	ptp_ocp_symlink(bp, child, link);
1322773bda96SJonathan Lemon 	put_device(child);
1323773bda96SJonathan Lemon }
1324773bda96SJonathan Lemon 
1325773bda96SJonathan Lemon static int
1326773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp)
1327773bda96SJonathan Lemon {
1328773bda96SJonathan Lemon 	struct pps_device *pps;
1329773bda96SJonathan Lemon 	char buf[32];
1330773bda96SJonathan Lemon 
1331ef0cfb34SJonathan Lemon 	if (bp->gnss_port != -1) {
1332ef0cfb34SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->gnss_port);
1333ef0cfb34SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyGNSS");
1334773bda96SJonathan Lemon 	}
1335773bda96SJonathan Lemon 	if (bp->mac_port != -1) {
1336773bda96SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->mac_port);
1337773bda96SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyMAC");
1338773bda96SJonathan Lemon 	}
1339773bda96SJonathan Lemon 	sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp));
1340773bda96SJonathan Lemon 	ptp_ocp_link_child(bp, buf, "ptp");
1341773bda96SJonathan Lemon 
1342773bda96SJonathan Lemon 	pps = pps_lookup_dev(bp->ptp);
1343773bda96SJonathan Lemon 	if (pps)
1344773bda96SJonathan Lemon 		ptp_ocp_symlink(bp, pps->dev, "pps");
1345773bda96SJonathan Lemon 
1346773bda96SJonathan Lemon 	if (device_add_groups(&bp->dev, timecard_groups))
1347773bda96SJonathan Lemon 		pr_err("device add groups failed\n");
1348773bda96SJonathan Lemon 
1349773bda96SJonathan Lemon 	return 0;
1350773bda96SJonathan Lemon }
1351773bda96SJonathan Lemon 
1352773bda96SJonathan Lemon static void
1353773bda96SJonathan Lemon ptp_ocp_resource_summary(struct ptp_ocp *bp)
1354773bda96SJonathan Lemon {
1355773bda96SJonathan Lemon 	struct device *dev = &bp->pdev->dev;
1356773bda96SJonathan Lemon 
1357773bda96SJonathan Lemon 	if (bp->image) {
1358773bda96SJonathan Lemon 		u32 ver = ioread32(&bp->image->version);
1359773bda96SJonathan Lemon 
1360773bda96SJonathan Lemon 		dev_info(dev, "version %x\n", ver);
1361773bda96SJonathan Lemon 		if (ver & 0xffff)
1362773bda96SJonathan Lemon 			dev_info(dev, "regular image, version %d\n",
1363773bda96SJonathan Lemon 				 ver & 0xffff);
1364773bda96SJonathan Lemon 		else
1365773bda96SJonathan Lemon 			dev_info(dev, "golden image, version %d\n",
1366773bda96SJonathan Lemon 				 ver >> 16);
1367773bda96SJonathan Lemon 	}
1368ef0cfb34SJonathan Lemon 	if (bp->gnss_port != -1)
1369ef0cfb34SJonathan Lemon 		dev_info(dev, "GNSS @ /dev/ttyS%d 115200\n", bp->gnss_port);
1370773bda96SJonathan Lemon 	if (bp->mac_port != -1)
1371773bda96SJonathan Lemon 		dev_info(dev, "MAC @ /dev/ttyS%d   57600\n", bp->mac_port);
1372773bda96SJonathan Lemon }
1373773bda96SJonathan Lemon 
1374773bda96SJonathan Lemon static void
1375773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
1376773bda96SJonathan Lemon {
1377773bda96SJonathan Lemon 	struct device *dev = &bp->dev;
1378773bda96SJonathan Lemon 
1379ef0cfb34SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ttyGNSS");
1380773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ttyMAC");
1381773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ptp");
1382773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "pps");
1383773bda96SJonathan Lemon 	device_remove_groups(dev, timecard_groups);
1384773bda96SJonathan Lemon }
1385773bda96SJonathan Lemon 
1386773bda96SJonathan Lemon static void
1387773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp)
1388773bda96SJonathan Lemon {
1389773bda96SJonathan Lemon 	ptp_ocp_detach_sysfs(bp);
1390773bda96SJonathan Lemon 	if (timer_pending(&bp->watchdog))
1391773bda96SJonathan Lemon 		del_timer_sync(&bp->watchdog);
1392773bda96SJonathan Lemon 	if (bp->ts0)
1393773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts0);
1394773bda96SJonathan Lemon 	if (bp->ts1)
1395773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts1);
1396773bda96SJonathan Lemon 	if (bp->pps)
1397773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->pps);
1398ef0cfb34SJonathan Lemon 	if (bp->gnss_port != -1)
1399ef0cfb34SJonathan Lemon 		serial8250_unregister_port(bp->gnss_port);
1400773bda96SJonathan Lemon 	if (bp->mac_port != -1)
1401773bda96SJonathan Lemon 		serial8250_unregister_port(bp->mac_port);
1402773bda96SJonathan Lemon 	if (bp->spi_flash)
1403773bda96SJonathan Lemon 		platform_device_unregister(bp->spi_flash);
1404773bda96SJonathan Lemon 	if (bp->i2c_ctrl)
1405773bda96SJonathan Lemon 		platform_device_unregister(bp->i2c_ctrl);
1406773bda96SJonathan Lemon 	if (bp->i2c_clk)
1407773bda96SJonathan Lemon 		clk_hw_unregister_fixed_rate(bp->i2c_clk);
1408773bda96SJonathan Lemon 	if (bp->n_irqs)
1409773bda96SJonathan Lemon 		pci_free_irq_vectors(bp->pdev);
1410773bda96SJonathan Lemon 	if (bp->ptp)
1411773bda96SJonathan Lemon 		ptp_clock_unregister(bp->ptp);
1412773bda96SJonathan Lemon 	device_unregister(&bp->dev);
1413773bda96SJonathan Lemon }
1414773bda96SJonathan Lemon 
1415a7e1abadSJonathan Lemon static int
1416a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1417a7e1abadSJonathan Lemon {
1418773bda96SJonathan Lemon 	struct devlink *devlink;
1419a7e1abadSJonathan Lemon 	struct ptp_ocp *bp;
1420a7e1abadSJonathan Lemon 	int err;
1421a7e1abadSJonathan Lemon 
1422919d13a7SLeon Romanovsky 	devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev);
1423773bda96SJonathan Lemon 	if (!devlink) {
1424773bda96SJonathan Lemon 		dev_err(&pdev->dev, "devlink_alloc failed\n");
1425a7e1abadSJonathan Lemon 		return -ENOMEM;
1426773bda96SJonathan Lemon 	}
1427773bda96SJonathan Lemon 
1428919d13a7SLeon Romanovsky 	err = devlink_register(devlink);
1429773bda96SJonathan Lemon 	if (err)
1430773bda96SJonathan Lemon 		goto out_free;
1431a7e1abadSJonathan Lemon 
1432a7e1abadSJonathan Lemon 	err = pci_enable_device(pdev);
1433a7e1abadSJonathan Lemon 	if (err) {
1434a7e1abadSJonathan Lemon 		dev_err(&pdev->dev, "pci_enable_device\n");
1435773bda96SJonathan Lemon 		goto out_unregister;
1436a7e1abadSJonathan Lemon 	}
1437a7e1abadSJonathan Lemon 
1438773bda96SJonathan Lemon 	bp = devlink_priv(devlink);
1439773bda96SJonathan Lemon 	err = ptp_ocp_device_init(bp, pdev);
1440773bda96SJonathan Lemon 	if (err)
1441773bda96SJonathan Lemon 		goto out_unregister;
1442a7e1abadSJonathan Lemon 
1443773bda96SJonathan Lemon 	/* compat mode.
1444773bda96SJonathan Lemon 	 * Older FPGA firmware only returns 2 irq's.
1445773bda96SJonathan Lemon 	 * allow this - if not all of the IRQ's are returned, skip the
1446773bda96SJonathan Lemon 	 * extra devices and just register the clock.
1447773bda96SJonathan Lemon 	 */
1448773bda96SJonathan Lemon 	err = pci_alloc_irq_vectors(pdev, 1, 10, PCI_IRQ_MSI | PCI_IRQ_MSIX);
1449773bda96SJonathan Lemon 	if (err < 0) {
1450773bda96SJonathan Lemon 		dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err);
1451773bda96SJonathan Lemon 		goto out;
1452a7e1abadSJonathan Lemon 	}
1453773bda96SJonathan Lemon 	bp->n_irqs = err;
1454773bda96SJonathan Lemon 	pci_set_master(pdev);
1455a7e1abadSJonathan Lemon 
1456773bda96SJonathan Lemon 	err = ptp_ocp_register_resources(bp, id->driver_data);
1457a7e1abadSJonathan Lemon 	if (err)
1458a7e1abadSJonathan Lemon 		goto out;
1459a7e1abadSJonathan Lemon 
1460a7e1abadSJonathan Lemon 	bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev);
1461a7e1abadSJonathan Lemon 	if (IS_ERR(bp->ptp)) {
1462a7e1abadSJonathan Lemon 		err = PTR_ERR(bp->ptp);
1463773bda96SJonathan Lemon 		dev_err(&pdev->dev, "ptp_clock_register: %d\n", err);
1464773bda96SJonathan Lemon 		bp->ptp = NULL;
1465a7e1abadSJonathan Lemon 		goto out;
1466a7e1abadSJonathan Lemon 	}
1467a7e1abadSJonathan Lemon 
1468773bda96SJonathan Lemon 	err = ptp_ocp_complete(bp);
1469773bda96SJonathan Lemon 	if (err)
1470773bda96SJonathan Lemon 		goto out;
1471773bda96SJonathan Lemon 
1472a7e1abadSJonathan Lemon 	ptp_ocp_info(bp);
1473773bda96SJonathan Lemon 	ptp_ocp_resource_summary(bp);
1474a7e1abadSJonathan Lemon 
1475a7e1abadSJonathan Lemon 	return 0;
1476a7e1abadSJonathan Lemon 
1477a7e1abadSJonathan Lemon out:
1478773bda96SJonathan Lemon 	ptp_ocp_detach(bp);
1479a7e1abadSJonathan Lemon 	pci_disable_device(pdev);
1480773bda96SJonathan Lemon 	pci_set_drvdata(pdev, NULL);
1481773bda96SJonathan Lemon out_unregister:
1482919d13a7SLeon Romanovsky 	devlink_unregister(devlink);
1483a7e1abadSJonathan Lemon out_free:
1484773bda96SJonathan Lemon 	devlink_free(devlink);
1485a7e1abadSJonathan Lemon 
1486a7e1abadSJonathan Lemon 	return err;
1487a7e1abadSJonathan Lemon }
1488a7e1abadSJonathan Lemon 
1489a7e1abadSJonathan Lemon static void
1490a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev)
1491a7e1abadSJonathan Lemon {
1492a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = pci_get_drvdata(pdev);
1493773bda96SJonathan Lemon 	struct devlink *devlink = priv_to_devlink(bp);
1494a7e1abadSJonathan Lemon 
1495773bda96SJonathan Lemon 	ptp_ocp_detach(bp);
1496a7e1abadSJonathan Lemon 	pci_disable_device(pdev);
1497a7e1abadSJonathan Lemon 	pci_set_drvdata(pdev, NULL);
1498773bda96SJonathan Lemon 
1499919d13a7SLeon Romanovsky 	devlink_unregister(devlink);
1500773bda96SJonathan Lemon 	devlink_free(devlink);
1501a7e1abadSJonathan Lemon }
1502a7e1abadSJonathan Lemon 
1503a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = {
1504a7e1abadSJonathan Lemon 	.name		= KBUILD_MODNAME,
1505a7e1abadSJonathan Lemon 	.id_table	= ptp_ocp_pcidev_id,
1506a7e1abadSJonathan Lemon 	.probe		= ptp_ocp_probe,
1507a7e1abadSJonathan Lemon 	.remove		= ptp_ocp_remove,
1508a7e1abadSJonathan Lemon };
1509a7e1abadSJonathan Lemon 
1510773bda96SJonathan Lemon static int
1511773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb,
1512773bda96SJonathan Lemon 			  unsigned long action, void *data)
1513773bda96SJonathan Lemon {
1514773bda96SJonathan Lemon 	struct device *dev, *child = data;
1515773bda96SJonathan Lemon 	struct ptp_ocp *bp;
1516773bda96SJonathan Lemon 	bool add;
1517773bda96SJonathan Lemon 
1518773bda96SJonathan Lemon 	switch (action) {
1519773bda96SJonathan Lemon 	case BUS_NOTIFY_ADD_DEVICE:
1520773bda96SJonathan Lemon 	case BUS_NOTIFY_DEL_DEVICE:
1521773bda96SJonathan Lemon 		add = action == BUS_NOTIFY_ADD_DEVICE;
1522773bda96SJonathan Lemon 		break;
1523773bda96SJonathan Lemon 	default:
1524773bda96SJonathan Lemon 		return 0;
1525773bda96SJonathan Lemon 	}
1526773bda96SJonathan Lemon 
1527773bda96SJonathan Lemon 	if (!i2c_verify_adapter(child))
1528773bda96SJonathan Lemon 		return 0;
1529773bda96SJonathan Lemon 
1530773bda96SJonathan Lemon 	dev = child;
1531773bda96SJonathan Lemon 	while ((dev = dev->parent))
1532773bda96SJonathan Lemon 		if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
1533773bda96SJonathan Lemon 			goto found;
1534773bda96SJonathan Lemon 	return 0;
1535773bda96SJonathan Lemon 
1536773bda96SJonathan Lemon found:
1537773bda96SJonathan Lemon 	bp = dev_get_drvdata(dev);
1538773bda96SJonathan Lemon 	if (add)
1539773bda96SJonathan Lemon 		ptp_ocp_symlink(bp, child, "i2c");
1540773bda96SJonathan Lemon 	else
1541773bda96SJonathan Lemon 		sysfs_remove_link(&bp->dev.kobj, "i2c");
1542773bda96SJonathan Lemon 
1543773bda96SJonathan Lemon 	return 0;
1544773bda96SJonathan Lemon }
1545773bda96SJonathan Lemon 
1546773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = {
1547773bda96SJonathan Lemon 	.notifier_call = ptp_ocp_i2c_notifier_call,
1548773bda96SJonathan Lemon };
1549773bda96SJonathan Lemon 
1550a7e1abadSJonathan Lemon static int __init
1551a7e1abadSJonathan Lemon ptp_ocp_init(void)
1552a7e1abadSJonathan Lemon {
1553773bda96SJonathan Lemon 	const char *what;
1554a7e1abadSJonathan Lemon 	int err;
1555a7e1abadSJonathan Lemon 
1556773bda96SJonathan Lemon 	what = "timecard class";
1557773bda96SJonathan Lemon 	err = class_register(&timecard_class);
1558773bda96SJonathan Lemon 	if (err)
1559773bda96SJonathan Lemon 		goto out;
1560773bda96SJonathan Lemon 
1561773bda96SJonathan Lemon 	what = "i2c notifier";
1562773bda96SJonathan Lemon 	err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
1563773bda96SJonathan Lemon 	if (err)
1564773bda96SJonathan Lemon 		goto out_notifier;
1565773bda96SJonathan Lemon 
1566773bda96SJonathan Lemon 	what = "ptp_ocp driver";
1567a7e1abadSJonathan Lemon 	err = pci_register_driver(&ptp_ocp_driver);
1568773bda96SJonathan Lemon 	if (err)
1569773bda96SJonathan Lemon 		goto out_register;
1570773bda96SJonathan Lemon 
1571773bda96SJonathan Lemon 	return 0;
1572773bda96SJonathan Lemon 
1573773bda96SJonathan Lemon out_register:
1574773bda96SJonathan Lemon 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
1575773bda96SJonathan Lemon out_notifier:
1576773bda96SJonathan Lemon 	class_unregister(&timecard_class);
1577773bda96SJonathan Lemon out:
1578773bda96SJonathan Lemon 	pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err);
1579a7e1abadSJonathan Lemon 	return err;
1580a7e1abadSJonathan Lemon }
1581a7e1abadSJonathan Lemon 
1582a7e1abadSJonathan Lemon static void __exit
1583a7e1abadSJonathan Lemon ptp_ocp_fini(void)
1584a7e1abadSJonathan Lemon {
1585773bda96SJonathan Lemon 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
1586a7e1abadSJonathan Lemon 	pci_unregister_driver(&ptp_ocp_driver);
1587773bda96SJonathan Lemon 	class_unregister(&timecard_class);
1588a7e1abadSJonathan Lemon }
1589a7e1abadSJonathan Lemon 
1590a7e1abadSJonathan Lemon module_init(ptp_ocp_init);
1591a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini);
1592a7e1abadSJonathan Lemon 
1593a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver");
1594a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2");
1595