1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Telecom Clock driver for Intel NetStructure(tm) MPCBL0010
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2005 Kontron Canada
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * All rights reserved.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
9*4882a593Smuzhiyun * it under the terms of the GNU General Public License as published by
10*4882a593Smuzhiyun * the Free Software Foundation; either version 2 of the License, or (at
11*4882a593Smuzhiyun * your option) any later version.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun * WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
16*4882a593Smuzhiyun * NON INFRINGEMENT. See the GNU General Public License for more
17*4882a593Smuzhiyun * details.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * You should have received a copy of the GNU General Public License
20*4882a593Smuzhiyun * along with this program; if not, write to the Free Software
21*4882a593Smuzhiyun * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Send feedback to <sebastien.bouchard@ca.kontron.com> and the current
24*4882a593Smuzhiyun * Maintainer <mark.gross@intel.com>
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * Description : This is the TELECOM CLOCK module driver for the ATCA
27*4882a593Smuzhiyun * MPCBL0010 ATCA computer.
28*4882a593Smuzhiyun */
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #include <linux/module.h>
31*4882a593Smuzhiyun #include <linux/init.h>
32*4882a593Smuzhiyun #include <linux/kernel.h> /* printk() */
33*4882a593Smuzhiyun #include <linux/fs.h> /* everything... */
34*4882a593Smuzhiyun #include <linux/errno.h> /* error codes */
35*4882a593Smuzhiyun #include <linux/sched.h>
36*4882a593Smuzhiyun #include <linux/slab.h>
37*4882a593Smuzhiyun #include <linux/ioport.h>
38*4882a593Smuzhiyun #include <linux/interrupt.h>
39*4882a593Smuzhiyun #include <linux/spinlock.h>
40*4882a593Smuzhiyun #include <linux/mutex.h>
41*4882a593Smuzhiyun #include <linux/timer.h>
42*4882a593Smuzhiyun #include <linux/sysfs.h>
43*4882a593Smuzhiyun #include <linux/device.h>
44*4882a593Smuzhiyun #include <linux/miscdevice.h>
45*4882a593Smuzhiyun #include <linux/platform_device.h>
46*4882a593Smuzhiyun #include <asm/io.h> /* inb/outb */
47*4882a593Smuzhiyun #include <linux/uaccess.h>
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun MODULE_AUTHOR("Sebastien Bouchard <sebastien.bouchard@ca.kontron.com>");
50*4882a593Smuzhiyun MODULE_LICENSE("GPL");
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /*Hardware Reset of the PLL */
53*4882a593Smuzhiyun #define RESET_ON 0x00
54*4882a593Smuzhiyun #define RESET_OFF 0x01
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /* MODE SELECT */
57*4882a593Smuzhiyun #define NORMAL_MODE 0x00
58*4882a593Smuzhiyun #define HOLDOVER_MODE 0x10
59*4882a593Smuzhiyun #define FREERUN_MODE 0x20
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /* FILTER SELECT */
62*4882a593Smuzhiyun #define FILTER_6HZ 0x04
63*4882a593Smuzhiyun #define FILTER_12HZ 0x00
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /* SELECT REFERENCE FREQUENCY */
66*4882a593Smuzhiyun #define REF_CLK1_8kHz 0x00
67*4882a593Smuzhiyun #define REF_CLK2_19_44MHz 0x02
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* Select primary or secondary redundant clock */
70*4882a593Smuzhiyun #define PRIMARY_CLOCK 0x00
71*4882a593Smuzhiyun #define SECONDARY_CLOCK 0x01
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* CLOCK TRANSMISSION DEFINE */
74*4882a593Smuzhiyun #define CLK_8kHz 0xff
75*4882a593Smuzhiyun #define CLK_16_384MHz 0xfb
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun #define CLK_1_544MHz 0x00
78*4882a593Smuzhiyun #define CLK_2_048MHz 0x01
79*4882a593Smuzhiyun #define CLK_4_096MHz 0x02
80*4882a593Smuzhiyun #define CLK_6_312MHz 0x03
81*4882a593Smuzhiyun #define CLK_8_192MHz 0x04
82*4882a593Smuzhiyun #define CLK_19_440MHz 0x06
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun #define CLK_8_592MHz 0x08
85*4882a593Smuzhiyun #define CLK_11_184MHz 0x09
86*4882a593Smuzhiyun #define CLK_34_368MHz 0x0b
87*4882a593Smuzhiyun #define CLK_44_736MHz 0x0a
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /* RECEIVED REFERENCE */
90*4882a593Smuzhiyun #define AMC_B1 0
91*4882a593Smuzhiyun #define AMC_B2 1
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /* HARDWARE SWITCHING DEFINE */
94*4882a593Smuzhiyun #define HW_ENABLE 0x80
95*4882a593Smuzhiyun #define HW_DISABLE 0x00
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /* HARDWARE SWITCHING MODE DEFINE */
98*4882a593Smuzhiyun #define PLL_HOLDOVER 0x40
99*4882a593Smuzhiyun #define LOST_CLOCK 0x00
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /* ALARMS DEFINE */
102*4882a593Smuzhiyun #define UNLOCK_MASK 0x10
103*4882a593Smuzhiyun #define HOLDOVER_MASK 0x20
104*4882a593Smuzhiyun #define SEC_LOST_MASK 0x40
105*4882a593Smuzhiyun #define PRI_LOST_MASK 0x80
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /* INTERRUPT CAUSE DEFINE */
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun #define PRI_LOS_01_MASK 0x01
110*4882a593Smuzhiyun #define PRI_LOS_10_MASK 0x02
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun #define SEC_LOS_01_MASK 0x04
113*4882a593Smuzhiyun #define SEC_LOS_10_MASK 0x08
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun #define HOLDOVER_01_MASK 0x10
116*4882a593Smuzhiyun #define HOLDOVER_10_MASK 0x20
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun #define UNLOCK_01_MASK 0x40
119*4882a593Smuzhiyun #define UNLOCK_10_MASK 0x80
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun struct tlclk_alarms {
122*4882a593Smuzhiyun __u32 lost_clocks;
123*4882a593Smuzhiyun __u32 lost_primary_clock;
124*4882a593Smuzhiyun __u32 lost_secondary_clock;
125*4882a593Smuzhiyun __u32 primary_clock_back;
126*4882a593Smuzhiyun __u32 secondary_clock_back;
127*4882a593Smuzhiyun __u32 switchover_primary;
128*4882a593Smuzhiyun __u32 switchover_secondary;
129*4882a593Smuzhiyun __u32 pll_holdover;
130*4882a593Smuzhiyun __u32 pll_end_holdover;
131*4882a593Smuzhiyun __u32 pll_lost_sync;
132*4882a593Smuzhiyun __u32 pll_sync;
133*4882a593Smuzhiyun };
134*4882a593Smuzhiyun /* Telecom clock I/O register definition */
135*4882a593Smuzhiyun #define TLCLK_BASE 0xa08
136*4882a593Smuzhiyun #define TLCLK_REG0 TLCLK_BASE
137*4882a593Smuzhiyun #define TLCLK_REG1 (TLCLK_BASE+1)
138*4882a593Smuzhiyun #define TLCLK_REG2 (TLCLK_BASE+2)
139*4882a593Smuzhiyun #define TLCLK_REG3 (TLCLK_BASE+3)
140*4882a593Smuzhiyun #define TLCLK_REG4 (TLCLK_BASE+4)
141*4882a593Smuzhiyun #define TLCLK_REG5 (TLCLK_BASE+5)
142*4882a593Smuzhiyun #define TLCLK_REG6 (TLCLK_BASE+6)
143*4882a593Smuzhiyun #define TLCLK_REG7 (TLCLK_BASE+7)
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun #define SET_PORT_BITS(port, mask, val) outb(((inb(port) & mask) | val), port)
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /* 0 = Dynamic allocation of the major device number */
148*4882a593Smuzhiyun #define TLCLK_MAJOR 0
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /* sysfs interface definition:
151*4882a593Smuzhiyun Upon loading the driver will create a sysfs directory under
152*4882a593Smuzhiyun /sys/devices/platform/telco_clock.
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun This directory exports the following interfaces. There operation is
155*4882a593Smuzhiyun documented in the MCPBL0010 TPS under the Telecom Clock API section, 11.4.
156*4882a593Smuzhiyun alarms :
157*4882a593Smuzhiyun current_ref :
158*4882a593Smuzhiyun received_ref_clk3a :
159*4882a593Smuzhiyun received_ref_clk3b :
160*4882a593Smuzhiyun enable_clk3a_output :
161*4882a593Smuzhiyun enable_clk3b_output :
162*4882a593Smuzhiyun enable_clka0_output :
163*4882a593Smuzhiyun enable_clka1_output :
164*4882a593Smuzhiyun enable_clkb0_output :
165*4882a593Smuzhiyun enable_clkb1_output :
166*4882a593Smuzhiyun filter_select :
167*4882a593Smuzhiyun hardware_switching :
168*4882a593Smuzhiyun hardware_switching_mode :
169*4882a593Smuzhiyun telclock_version :
170*4882a593Smuzhiyun mode_select :
171*4882a593Smuzhiyun refalign :
172*4882a593Smuzhiyun reset :
173*4882a593Smuzhiyun select_amcb1_transmit_clock :
174*4882a593Smuzhiyun select_amcb2_transmit_clock :
175*4882a593Smuzhiyun select_redundant_clock :
176*4882a593Smuzhiyun select_ref_frequency :
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun All sysfs interfaces are integers in hex format, i.e echo 99 > refalign
179*4882a593Smuzhiyun has the same effect as echo 0x99 > refalign.
180*4882a593Smuzhiyun */
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun static unsigned int telclk_interrupt;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun static int int_events; /* Event that generate a interrupt */
185*4882a593Smuzhiyun static int got_event; /* if events processing have been done */
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun static void switchover_timeout(struct timer_list *t);
188*4882a593Smuzhiyun static struct timer_list switchover_timer;
189*4882a593Smuzhiyun static unsigned long tlclk_timer_data;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun static struct tlclk_alarms *alarm_events;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun static DEFINE_SPINLOCK(event_lock);
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun static int tlclk_major = TLCLK_MAJOR;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun static irqreturn_t tlclk_interrupt(int irq, void *dev_id);
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(wq);
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun static unsigned long useflags;
202*4882a593Smuzhiyun static DEFINE_MUTEX(tlclk_mutex);
203*4882a593Smuzhiyun
tlclk_open(struct inode * inode,struct file * filp)204*4882a593Smuzhiyun static int tlclk_open(struct inode *inode, struct file *filp)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun int result;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun mutex_lock(&tlclk_mutex);
209*4882a593Smuzhiyun if (test_and_set_bit(0, &useflags)) {
210*4882a593Smuzhiyun result = -EBUSY;
211*4882a593Smuzhiyun /* this legacy device is always one per system and it doesn't
212*4882a593Smuzhiyun * know how to handle multiple concurrent clients.
213*4882a593Smuzhiyun */
214*4882a593Smuzhiyun goto out;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /* Make sure there is no interrupt pending while
218*4882a593Smuzhiyun * initialising interrupt handler */
219*4882a593Smuzhiyun inb(TLCLK_REG6);
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /* This device is wired through the FPGA IO space of the ATCA blade
222*4882a593Smuzhiyun * we can't share this IRQ */
223*4882a593Smuzhiyun result = request_irq(telclk_interrupt, &tlclk_interrupt,
224*4882a593Smuzhiyun 0, "telco_clock", tlclk_interrupt);
225*4882a593Smuzhiyun if (result == -EBUSY)
226*4882a593Smuzhiyun printk(KERN_ERR "tlclk: Interrupt can't be reserved.\n");
227*4882a593Smuzhiyun else
228*4882a593Smuzhiyun inb(TLCLK_REG6); /* Clear interrupt events */
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun out:
231*4882a593Smuzhiyun mutex_unlock(&tlclk_mutex);
232*4882a593Smuzhiyun return result;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun
tlclk_release(struct inode * inode,struct file * filp)235*4882a593Smuzhiyun static int tlclk_release(struct inode *inode, struct file *filp)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun free_irq(telclk_interrupt, tlclk_interrupt);
238*4882a593Smuzhiyun clear_bit(0, &useflags);
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun return 0;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
tlclk_read(struct file * filp,char __user * buf,size_t count,loff_t * f_pos)243*4882a593Smuzhiyun static ssize_t tlclk_read(struct file *filp, char __user *buf, size_t count,
244*4882a593Smuzhiyun loff_t *f_pos)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun if (count < sizeof(struct tlclk_alarms))
247*4882a593Smuzhiyun return -EIO;
248*4882a593Smuzhiyun if (mutex_lock_interruptible(&tlclk_mutex))
249*4882a593Smuzhiyun return -EINTR;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun wait_event_interruptible(wq, got_event);
253*4882a593Smuzhiyun if (copy_to_user(buf, alarm_events, sizeof(struct tlclk_alarms))) {
254*4882a593Smuzhiyun mutex_unlock(&tlclk_mutex);
255*4882a593Smuzhiyun return -EFAULT;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun memset(alarm_events, 0, sizeof(struct tlclk_alarms));
259*4882a593Smuzhiyun got_event = 0;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun mutex_unlock(&tlclk_mutex);
262*4882a593Smuzhiyun return sizeof(struct tlclk_alarms);
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun static const struct file_operations tlclk_fops = {
266*4882a593Smuzhiyun .read = tlclk_read,
267*4882a593Smuzhiyun .open = tlclk_open,
268*4882a593Smuzhiyun .release = tlclk_release,
269*4882a593Smuzhiyun .llseek = noop_llseek,
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun };
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun static struct miscdevice tlclk_miscdev = {
274*4882a593Smuzhiyun .minor = MISC_DYNAMIC_MINOR,
275*4882a593Smuzhiyun .name = "telco_clock",
276*4882a593Smuzhiyun .fops = &tlclk_fops,
277*4882a593Smuzhiyun };
278*4882a593Smuzhiyun
show_current_ref(struct device * d,struct device_attribute * attr,char * buf)279*4882a593Smuzhiyun static ssize_t show_current_ref(struct device *d,
280*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun unsigned long ret_val;
283*4882a593Smuzhiyun unsigned long flags;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
286*4882a593Smuzhiyun ret_val = ((inb(TLCLK_REG1) & 0x08) >> 3);
287*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun return sprintf(buf, "0x%lX\n", ret_val);
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun static DEVICE_ATTR(current_ref, S_IRUGO, show_current_ref, NULL);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun
show_telclock_version(struct device * d,struct device_attribute * attr,char * buf)295*4882a593Smuzhiyun static ssize_t show_telclock_version(struct device *d,
296*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun unsigned long ret_val;
299*4882a593Smuzhiyun unsigned long flags;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
302*4882a593Smuzhiyun ret_val = inb(TLCLK_REG5);
303*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun return sprintf(buf, "0x%lX\n", ret_val);
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun static DEVICE_ATTR(telclock_version, S_IRUGO,
309*4882a593Smuzhiyun show_telclock_version, NULL);
310*4882a593Smuzhiyun
show_alarms(struct device * d,struct device_attribute * attr,char * buf)311*4882a593Smuzhiyun static ssize_t show_alarms(struct device *d,
312*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
313*4882a593Smuzhiyun {
314*4882a593Smuzhiyun unsigned long ret_val;
315*4882a593Smuzhiyun unsigned long flags;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
318*4882a593Smuzhiyun ret_val = (inb(TLCLK_REG2) & 0xf0);
319*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun return sprintf(buf, "0x%lX\n", ret_val);
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
325*4882a593Smuzhiyun
store_received_ref_clk3a(struct device * d,struct device_attribute * attr,const char * buf,size_t count)326*4882a593Smuzhiyun static ssize_t store_received_ref_clk3a(struct device *d,
327*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun unsigned long tmp;
330*4882a593Smuzhiyun unsigned char val;
331*4882a593Smuzhiyun unsigned long flags;
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
334*4882a593Smuzhiyun dev_dbg(d, ": tmp = 0x%lX\n", tmp);
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun val = (unsigned char)tmp;
337*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
338*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG1, 0xef, val);
339*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun return strnlen(buf, count);
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun static DEVICE_ATTR(received_ref_clk3a, (S_IWUSR|S_IWGRP), NULL,
345*4882a593Smuzhiyun store_received_ref_clk3a);
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun
store_received_ref_clk3b(struct device * d,struct device_attribute * attr,const char * buf,size_t count)348*4882a593Smuzhiyun static ssize_t store_received_ref_clk3b(struct device *d,
349*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun unsigned long tmp;
352*4882a593Smuzhiyun unsigned char val;
353*4882a593Smuzhiyun unsigned long flags;
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
356*4882a593Smuzhiyun dev_dbg(d, ": tmp = 0x%lX\n", tmp);
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun val = (unsigned char)tmp;
359*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
360*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG1, 0xdf, val << 1);
361*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun return strnlen(buf, count);
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun static DEVICE_ATTR(received_ref_clk3b, (S_IWUSR|S_IWGRP), NULL,
367*4882a593Smuzhiyun store_received_ref_clk3b);
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun
store_enable_clk3b_output(struct device * d,struct device_attribute * attr,const char * buf,size_t count)370*4882a593Smuzhiyun static ssize_t store_enable_clk3b_output(struct device *d,
371*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun unsigned long tmp;
374*4882a593Smuzhiyun unsigned char val;
375*4882a593Smuzhiyun unsigned long flags;
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
378*4882a593Smuzhiyun dev_dbg(d, ": tmp = 0x%lX\n", tmp);
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun val = (unsigned char)tmp;
381*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
382*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG3, 0x7f, val << 7);
383*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun return strnlen(buf, count);
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun static DEVICE_ATTR(enable_clk3b_output, (S_IWUSR|S_IWGRP), NULL,
389*4882a593Smuzhiyun store_enable_clk3b_output);
390*4882a593Smuzhiyun
store_enable_clk3a_output(struct device * d,struct device_attribute * attr,const char * buf,size_t count)391*4882a593Smuzhiyun static ssize_t store_enable_clk3a_output(struct device *d,
392*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun unsigned long flags;
395*4882a593Smuzhiyun unsigned long tmp;
396*4882a593Smuzhiyun unsigned char val;
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
399*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun val = (unsigned char)tmp;
402*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
403*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG3, 0xbf, val << 6);
404*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun return strnlen(buf, count);
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun static DEVICE_ATTR(enable_clk3a_output, (S_IWUSR|S_IWGRP), NULL,
410*4882a593Smuzhiyun store_enable_clk3a_output);
411*4882a593Smuzhiyun
store_enable_clkb1_output(struct device * d,struct device_attribute * attr,const char * buf,size_t count)412*4882a593Smuzhiyun static ssize_t store_enable_clkb1_output(struct device *d,
413*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun unsigned long flags;
416*4882a593Smuzhiyun unsigned long tmp;
417*4882a593Smuzhiyun unsigned char val;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
420*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun val = (unsigned char)tmp;
423*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
424*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG2, 0xf7, val << 3);
425*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun return strnlen(buf, count);
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun static DEVICE_ATTR(enable_clkb1_output, (S_IWUSR|S_IWGRP), NULL,
431*4882a593Smuzhiyun store_enable_clkb1_output);
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun
store_enable_clka1_output(struct device * d,struct device_attribute * attr,const char * buf,size_t count)434*4882a593Smuzhiyun static ssize_t store_enable_clka1_output(struct device *d,
435*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun unsigned long flags;
438*4882a593Smuzhiyun unsigned long tmp;
439*4882a593Smuzhiyun unsigned char val;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
442*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun val = (unsigned char)tmp;
445*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
446*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG2, 0xfb, val << 2);
447*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun return strnlen(buf, count);
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun static DEVICE_ATTR(enable_clka1_output, (S_IWUSR|S_IWGRP), NULL,
453*4882a593Smuzhiyun store_enable_clka1_output);
454*4882a593Smuzhiyun
store_enable_clkb0_output(struct device * d,struct device_attribute * attr,const char * buf,size_t count)455*4882a593Smuzhiyun static ssize_t store_enable_clkb0_output(struct device *d,
456*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun unsigned long flags;
459*4882a593Smuzhiyun unsigned long tmp;
460*4882a593Smuzhiyun unsigned char val;
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
463*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun val = (unsigned char)tmp;
466*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
467*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG2, 0xfd, val << 1);
468*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun return strnlen(buf, count);
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun static DEVICE_ATTR(enable_clkb0_output, (S_IWUSR|S_IWGRP), NULL,
474*4882a593Smuzhiyun store_enable_clkb0_output);
475*4882a593Smuzhiyun
store_enable_clka0_output(struct device * d,struct device_attribute * attr,const char * buf,size_t count)476*4882a593Smuzhiyun static ssize_t store_enable_clka0_output(struct device *d,
477*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun unsigned long flags;
480*4882a593Smuzhiyun unsigned long tmp;
481*4882a593Smuzhiyun unsigned char val;
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
484*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun val = (unsigned char)tmp;
487*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
488*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG2, 0xfe, val);
489*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun return strnlen(buf, count);
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun static DEVICE_ATTR(enable_clka0_output, (S_IWUSR|S_IWGRP), NULL,
495*4882a593Smuzhiyun store_enable_clka0_output);
496*4882a593Smuzhiyun
store_select_amcb2_transmit_clock(struct device * d,struct device_attribute * attr,const char * buf,size_t count)497*4882a593Smuzhiyun static ssize_t store_select_amcb2_transmit_clock(struct device *d,
498*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
499*4882a593Smuzhiyun {
500*4882a593Smuzhiyun unsigned long flags;
501*4882a593Smuzhiyun unsigned long tmp;
502*4882a593Smuzhiyun unsigned char val;
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
505*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun val = (unsigned char)tmp;
508*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
509*4882a593Smuzhiyun if ((val == CLK_8kHz) || (val == CLK_16_384MHz)) {
510*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG3, 0xc7, 0x28);
511*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG1, 0xfb, ~val);
512*4882a593Smuzhiyun } else if (val >= CLK_8_592MHz) {
513*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG3, 0xc7, 0x38);
514*4882a593Smuzhiyun switch (val) {
515*4882a593Smuzhiyun case CLK_8_592MHz:
516*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xfc, 2);
517*4882a593Smuzhiyun break;
518*4882a593Smuzhiyun case CLK_11_184MHz:
519*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xfc, 0);
520*4882a593Smuzhiyun break;
521*4882a593Smuzhiyun case CLK_34_368MHz:
522*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xfc, 3);
523*4882a593Smuzhiyun break;
524*4882a593Smuzhiyun case CLK_44_736MHz:
525*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xfc, 1);
526*4882a593Smuzhiyun break;
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun } else {
529*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG3, 0xc7, val << 3);
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun return strnlen(buf, count);
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun static DEVICE_ATTR(select_amcb2_transmit_clock, (S_IWUSR|S_IWGRP), NULL,
537*4882a593Smuzhiyun store_select_amcb2_transmit_clock);
538*4882a593Smuzhiyun
store_select_amcb1_transmit_clock(struct device * d,struct device_attribute * attr,const char * buf,size_t count)539*4882a593Smuzhiyun static ssize_t store_select_amcb1_transmit_clock(struct device *d,
540*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
541*4882a593Smuzhiyun {
542*4882a593Smuzhiyun unsigned long tmp;
543*4882a593Smuzhiyun unsigned char val;
544*4882a593Smuzhiyun unsigned long flags;
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
547*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun val = (unsigned char)tmp;
550*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
551*4882a593Smuzhiyun if ((val == CLK_8kHz) || (val == CLK_16_384MHz)) {
552*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG3, 0xf8, 0x5);
553*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG1, 0xfb, ~val);
554*4882a593Smuzhiyun } else if (val >= CLK_8_592MHz) {
555*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG3, 0xf8, 0x7);
556*4882a593Smuzhiyun switch (val) {
557*4882a593Smuzhiyun case CLK_8_592MHz:
558*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xfc, 2);
559*4882a593Smuzhiyun break;
560*4882a593Smuzhiyun case CLK_11_184MHz:
561*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xfc, 0);
562*4882a593Smuzhiyun break;
563*4882a593Smuzhiyun case CLK_34_368MHz:
564*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xfc, 3);
565*4882a593Smuzhiyun break;
566*4882a593Smuzhiyun case CLK_44_736MHz:
567*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xfc, 1);
568*4882a593Smuzhiyun break;
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun } else {
571*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG3, 0xf8, val);
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun return strnlen(buf, count);
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun static DEVICE_ATTR(select_amcb1_transmit_clock, (S_IWUSR|S_IWGRP), NULL,
579*4882a593Smuzhiyun store_select_amcb1_transmit_clock);
580*4882a593Smuzhiyun
store_select_redundant_clock(struct device * d,struct device_attribute * attr,const char * buf,size_t count)581*4882a593Smuzhiyun static ssize_t store_select_redundant_clock(struct device *d,
582*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun unsigned long tmp;
585*4882a593Smuzhiyun unsigned char val;
586*4882a593Smuzhiyun unsigned long flags;
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
589*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun val = (unsigned char)tmp;
592*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
593*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG1, 0xfe, val);
594*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun return strnlen(buf, count);
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun static DEVICE_ATTR(select_redundant_clock, (S_IWUSR|S_IWGRP), NULL,
600*4882a593Smuzhiyun store_select_redundant_clock);
601*4882a593Smuzhiyun
store_select_ref_frequency(struct device * d,struct device_attribute * attr,const char * buf,size_t count)602*4882a593Smuzhiyun static ssize_t store_select_ref_frequency(struct device *d,
603*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun unsigned long tmp;
606*4882a593Smuzhiyun unsigned char val;
607*4882a593Smuzhiyun unsigned long flags;
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
610*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun val = (unsigned char)tmp;
613*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
614*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG1, 0xfd, val);
615*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun return strnlen(buf, count);
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun static DEVICE_ATTR(select_ref_frequency, (S_IWUSR|S_IWGRP), NULL,
621*4882a593Smuzhiyun store_select_ref_frequency);
622*4882a593Smuzhiyun
store_filter_select(struct device * d,struct device_attribute * attr,const char * buf,size_t count)623*4882a593Smuzhiyun static ssize_t store_filter_select(struct device *d,
624*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
625*4882a593Smuzhiyun {
626*4882a593Smuzhiyun unsigned long tmp;
627*4882a593Smuzhiyun unsigned char val;
628*4882a593Smuzhiyun unsigned long flags;
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
631*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun val = (unsigned char)tmp;
634*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
635*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xfb, val);
636*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun return strnlen(buf, count);
639*4882a593Smuzhiyun }
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun static DEVICE_ATTR(filter_select, (S_IWUSR|S_IWGRP), NULL, store_filter_select);
642*4882a593Smuzhiyun
store_hardware_switching_mode(struct device * d,struct device_attribute * attr,const char * buf,size_t count)643*4882a593Smuzhiyun static ssize_t store_hardware_switching_mode(struct device *d,
644*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
645*4882a593Smuzhiyun {
646*4882a593Smuzhiyun unsigned long tmp;
647*4882a593Smuzhiyun unsigned char val;
648*4882a593Smuzhiyun unsigned long flags;
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
651*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun val = (unsigned char)tmp;
654*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
655*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xbf, val);
656*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun return strnlen(buf, count);
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun static DEVICE_ATTR(hardware_switching_mode, (S_IWUSR|S_IWGRP), NULL,
662*4882a593Smuzhiyun store_hardware_switching_mode);
663*4882a593Smuzhiyun
store_hardware_switching(struct device * d,struct device_attribute * attr,const char * buf,size_t count)664*4882a593Smuzhiyun static ssize_t store_hardware_switching(struct device *d,
665*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
666*4882a593Smuzhiyun {
667*4882a593Smuzhiyun unsigned long tmp;
668*4882a593Smuzhiyun unsigned char val;
669*4882a593Smuzhiyun unsigned long flags;
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
672*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun val = (unsigned char)tmp;
675*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
676*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0x7f, val);
677*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun return strnlen(buf, count);
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun static DEVICE_ATTR(hardware_switching, (S_IWUSR|S_IWGRP), NULL,
683*4882a593Smuzhiyun store_hardware_switching);
684*4882a593Smuzhiyun
store_refalign(struct device * d,struct device_attribute * attr,const char * buf,size_t count)685*4882a593Smuzhiyun static ssize_t store_refalign (struct device *d,
686*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
687*4882a593Smuzhiyun {
688*4882a593Smuzhiyun unsigned long tmp;
689*4882a593Smuzhiyun unsigned long flags;
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
692*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
693*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
694*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xf7, 0);
695*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xf7, 0x08);
696*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xf7, 0);
697*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun return strnlen(buf, count);
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun static DEVICE_ATTR(refalign, (S_IWUSR|S_IWGRP), NULL, store_refalign);
703*4882a593Smuzhiyun
store_mode_select(struct device * d,struct device_attribute * attr,const char * buf,size_t count)704*4882a593Smuzhiyun static ssize_t store_mode_select (struct device *d,
705*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
706*4882a593Smuzhiyun {
707*4882a593Smuzhiyun unsigned long tmp;
708*4882a593Smuzhiyun unsigned char val;
709*4882a593Smuzhiyun unsigned long flags;
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
712*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun val = (unsigned char)tmp;
715*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
716*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG0, 0xcf, val);
717*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun return strnlen(buf, count);
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun static DEVICE_ATTR(mode_select, (S_IWUSR|S_IWGRP), NULL, store_mode_select);
723*4882a593Smuzhiyun
store_reset(struct device * d,struct device_attribute * attr,const char * buf,size_t count)724*4882a593Smuzhiyun static ssize_t store_reset (struct device *d,
725*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
726*4882a593Smuzhiyun {
727*4882a593Smuzhiyun unsigned long tmp;
728*4882a593Smuzhiyun unsigned char val;
729*4882a593Smuzhiyun unsigned long flags;
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun sscanf(buf, "%lX", &tmp);
732*4882a593Smuzhiyun dev_dbg(d, "tmp = 0x%lX\n", tmp);
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun val = (unsigned char)tmp;
735*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
736*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG4, 0xfd, val);
737*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun return strnlen(buf, count);
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun static DEVICE_ATTR(reset, (S_IWUSR|S_IWGRP), NULL, store_reset);
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun static struct attribute *tlclk_sysfs_entries[] = {
745*4882a593Smuzhiyun &dev_attr_current_ref.attr,
746*4882a593Smuzhiyun &dev_attr_telclock_version.attr,
747*4882a593Smuzhiyun &dev_attr_alarms.attr,
748*4882a593Smuzhiyun &dev_attr_received_ref_clk3a.attr,
749*4882a593Smuzhiyun &dev_attr_received_ref_clk3b.attr,
750*4882a593Smuzhiyun &dev_attr_enable_clk3a_output.attr,
751*4882a593Smuzhiyun &dev_attr_enable_clk3b_output.attr,
752*4882a593Smuzhiyun &dev_attr_enable_clkb1_output.attr,
753*4882a593Smuzhiyun &dev_attr_enable_clka1_output.attr,
754*4882a593Smuzhiyun &dev_attr_enable_clkb0_output.attr,
755*4882a593Smuzhiyun &dev_attr_enable_clka0_output.attr,
756*4882a593Smuzhiyun &dev_attr_select_amcb1_transmit_clock.attr,
757*4882a593Smuzhiyun &dev_attr_select_amcb2_transmit_clock.attr,
758*4882a593Smuzhiyun &dev_attr_select_redundant_clock.attr,
759*4882a593Smuzhiyun &dev_attr_select_ref_frequency.attr,
760*4882a593Smuzhiyun &dev_attr_filter_select.attr,
761*4882a593Smuzhiyun &dev_attr_hardware_switching_mode.attr,
762*4882a593Smuzhiyun &dev_attr_hardware_switching.attr,
763*4882a593Smuzhiyun &dev_attr_refalign.attr,
764*4882a593Smuzhiyun &dev_attr_mode_select.attr,
765*4882a593Smuzhiyun &dev_attr_reset.attr,
766*4882a593Smuzhiyun NULL
767*4882a593Smuzhiyun };
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun static const struct attribute_group tlclk_attribute_group = {
770*4882a593Smuzhiyun .name = NULL, /* put in device directory */
771*4882a593Smuzhiyun .attrs = tlclk_sysfs_entries,
772*4882a593Smuzhiyun };
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun static struct platform_device *tlclk_device;
775*4882a593Smuzhiyun
tlclk_init(void)776*4882a593Smuzhiyun static int __init tlclk_init(void)
777*4882a593Smuzhiyun {
778*4882a593Smuzhiyun int ret;
779*4882a593Smuzhiyun
780*4882a593Smuzhiyun telclk_interrupt = (inb(TLCLK_REG7) & 0x0f);
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun alarm_events = kzalloc( sizeof(struct tlclk_alarms), GFP_KERNEL);
783*4882a593Smuzhiyun if (!alarm_events) {
784*4882a593Smuzhiyun ret = -ENOMEM;
785*4882a593Smuzhiyun goto out1;
786*4882a593Smuzhiyun }
787*4882a593Smuzhiyun
788*4882a593Smuzhiyun ret = register_chrdev(tlclk_major, "telco_clock", &tlclk_fops);
789*4882a593Smuzhiyun if (ret < 0) {
790*4882a593Smuzhiyun printk(KERN_ERR "tlclk: can't get major %d.\n", tlclk_major);
791*4882a593Smuzhiyun kfree(alarm_events);
792*4882a593Smuzhiyun return ret;
793*4882a593Smuzhiyun }
794*4882a593Smuzhiyun tlclk_major = ret;
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun /* Read telecom clock IRQ number (Set by BIOS) */
797*4882a593Smuzhiyun if (!request_region(TLCLK_BASE, 8, "telco_clock")) {
798*4882a593Smuzhiyun printk(KERN_ERR "tlclk: request_region 0x%X failed.\n",
799*4882a593Smuzhiyun TLCLK_BASE);
800*4882a593Smuzhiyun ret = -EBUSY;
801*4882a593Smuzhiyun goto out2;
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun if (0x0F == telclk_interrupt ) { /* not MCPBL0010 ? */
805*4882a593Smuzhiyun printk(KERN_ERR "telclk_interrupt = 0x%x non-mcpbl0010 hw.\n",
806*4882a593Smuzhiyun telclk_interrupt);
807*4882a593Smuzhiyun ret = -ENXIO;
808*4882a593Smuzhiyun goto out3;
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun timer_setup(&switchover_timer, switchover_timeout, 0);
812*4882a593Smuzhiyun
813*4882a593Smuzhiyun ret = misc_register(&tlclk_miscdev);
814*4882a593Smuzhiyun if (ret < 0) {
815*4882a593Smuzhiyun printk(KERN_ERR "tlclk: misc_register returns %d.\n", ret);
816*4882a593Smuzhiyun goto out3;
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun tlclk_device = platform_device_register_simple("telco_clock",
820*4882a593Smuzhiyun -1, NULL, 0);
821*4882a593Smuzhiyun if (IS_ERR(tlclk_device)) {
822*4882a593Smuzhiyun printk(KERN_ERR "tlclk: platform_device_register failed.\n");
823*4882a593Smuzhiyun ret = PTR_ERR(tlclk_device);
824*4882a593Smuzhiyun goto out4;
825*4882a593Smuzhiyun }
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun ret = sysfs_create_group(&tlclk_device->dev.kobj,
828*4882a593Smuzhiyun &tlclk_attribute_group);
829*4882a593Smuzhiyun if (ret) {
830*4882a593Smuzhiyun printk(KERN_ERR "tlclk: failed to create sysfs device attributes.\n");
831*4882a593Smuzhiyun goto out5;
832*4882a593Smuzhiyun }
833*4882a593Smuzhiyun
834*4882a593Smuzhiyun return 0;
835*4882a593Smuzhiyun out5:
836*4882a593Smuzhiyun platform_device_unregister(tlclk_device);
837*4882a593Smuzhiyun out4:
838*4882a593Smuzhiyun misc_deregister(&tlclk_miscdev);
839*4882a593Smuzhiyun out3:
840*4882a593Smuzhiyun release_region(TLCLK_BASE, 8);
841*4882a593Smuzhiyun out2:
842*4882a593Smuzhiyun kfree(alarm_events);
843*4882a593Smuzhiyun unregister_chrdev(tlclk_major, "telco_clock");
844*4882a593Smuzhiyun out1:
845*4882a593Smuzhiyun return ret;
846*4882a593Smuzhiyun }
847*4882a593Smuzhiyun
tlclk_cleanup(void)848*4882a593Smuzhiyun static void __exit tlclk_cleanup(void)
849*4882a593Smuzhiyun {
850*4882a593Smuzhiyun sysfs_remove_group(&tlclk_device->dev.kobj, &tlclk_attribute_group);
851*4882a593Smuzhiyun platform_device_unregister(tlclk_device);
852*4882a593Smuzhiyun misc_deregister(&tlclk_miscdev);
853*4882a593Smuzhiyun unregister_chrdev(tlclk_major, "telco_clock");
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun release_region(TLCLK_BASE, 8);
856*4882a593Smuzhiyun del_timer_sync(&switchover_timer);
857*4882a593Smuzhiyun kfree(alarm_events);
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun
switchover_timeout(struct timer_list * unused)861*4882a593Smuzhiyun static void switchover_timeout(struct timer_list *unused)
862*4882a593Smuzhiyun {
863*4882a593Smuzhiyun unsigned long flags = tlclk_timer_data;
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun if ((flags & 1)) {
866*4882a593Smuzhiyun if ((inb(TLCLK_REG1) & 0x08) != (flags & 0x08))
867*4882a593Smuzhiyun alarm_events->switchover_primary++;
868*4882a593Smuzhiyun } else {
869*4882a593Smuzhiyun if ((inb(TLCLK_REG1) & 0x08) != (flags & 0x08))
870*4882a593Smuzhiyun alarm_events->switchover_secondary++;
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun /* Alarm processing is done, wake up read task */
874*4882a593Smuzhiyun del_timer(&switchover_timer);
875*4882a593Smuzhiyun got_event = 1;
876*4882a593Smuzhiyun wake_up(&wq);
877*4882a593Smuzhiyun }
878*4882a593Smuzhiyun
tlclk_interrupt(int irq,void * dev_id)879*4882a593Smuzhiyun static irqreturn_t tlclk_interrupt(int irq, void *dev_id)
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun unsigned long flags;
882*4882a593Smuzhiyun
883*4882a593Smuzhiyun spin_lock_irqsave(&event_lock, flags);
884*4882a593Smuzhiyun /* Read and clear interrupt events */
885*4882a593Smuzhiyun int_events = inb(TLCLK_REG6);
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun /* Primary_Los changed from 0 to 1 ? */
888*4882a593Smuzhiyun if (int_events & PRI_LOS_01_MASK) {
889*4882a593Smuzhiyun if (inb(TLCLK_REG2) & SEC_LOST_MASK)
890*4882a593Smuzhiyun alarm_events->lost_clocks++;
891*4882a593Smuzhiyun else
892*4882a593Smuzhiyun alarm_events->lost_primary_clock++;
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun /* Primary_Los changed from 1 to 0 ? */
896*4882a593Smuzhiyun if (int_events & PRI_LOS_10_MASK) {
897*4882a593Smuzhiyun alarm_events->primary_clock_back++;
898*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG1, 0xFE, 1);
899*4882a593Smuzhiyun }
900*4882a593Smuzhiyun /* Secondary_Los changed from 0 to 1 ? */
901*4882a593Smuzhiyun if (int_events & SEC_LOS_01_MASK) {
902*4882a593Smuzhiyun if (inb(TLCLK_REG2) & PRI_LOST_MASK)
903*4882a593Smuzhiyun alarm_events->lost_clocks++;
904*4882a593Smuzhiyun else
905*4882a593Smuzhiyun alarm_events->lost_secondary_clock++;
906*4882a593Smuzhiyun }
907*4882a593Smuzhiyun /* Secondary_Los changed from 1 to 0 ? */
908*4882a593Smuzhiyun if (int_events & SEC_LOS_10_MASK) {
909*4882a593Smuzhiyun alarm_events->secondary_clock_back++;
910*4882a593Smuzhiyun SET_PORT_BITS(TLCLK_REG1, 0xFE, 0);
911*4882a593Smuzhiyun }
912*4882a593Smuzhiyun if (int_events & HOLDOVER_10_MASK)
913*4882a593Smuzhiyun alarm_events->pll_end_holdover++;
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun if (int_events & UNLOCK_01_MASK)
916*4882a593Smuzhiyun alarm_events->pll_lost_sync++;
917*4882a593Smuzhiyun
918*4882a593Smuzhiyun if (int_events & UNLOCK_10_MASK)
919*4882a593Smuzhiyun alarm_events->pll_sync++;
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun /* Holdover changed from 0 to 1 ? */
922*4882a593Smuzhiyun if (int_events & HOLDOVER_01_MASK) {
923*4882a593Smuzhiyun alarm_events->pll_holdover++;
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun /* TIMEOUT in ~10ms */
926*4882a593Smuzhiyun switchover_timer.expires = jiffies + msecs_to_jiffies(10);
927*4882a593Smuzhiyun tlclk_timer_data = inb(TLCLK_REG1);
928*4882a593Smuzhiyun mod_timer(&switchover_timer, switchover_timer.expires);
929*4882a593Smuzhiyun } else {
930*4882a593Smuzhiyun got_event = 1;
931*4882a593Smuzhiyun wake_up(&wq);
932*4882a593Smuzhiyun }
933*4882a593Smuzhiyun spin_unlock_irqrestore(&event_lock, flags);
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun return IRQ_HANDLED;
936*4882a593Smuzhiyun }
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun module_init(tlclk_init);
939*4882a593Smuzhiyun module_exit(tlclk_cleanup);
940