1 /*******************************************************************************
2 STMMAC external timer support.
4 Copyright (C) 2007-2009 STMicroelectronics Ltd
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
22 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
23 *******************************************************************************/
25 #include <linux/kernel.h>
26 #include <linux/etherdevice.h>
27 #include "stmmac_timer.h"
29 static void stmmac_timer_handler(void *data
)
31 struct net_device
*dev
= (struct net_device
*)data
;
38 #define STMMAC_TIMER_MSG(timer, freq) \
39 printk(KERN_INFO "stmmac_timer: %s Timer ON (freq %dHz)\n", timer, freq);
41 #if defined(CONFIG_STMMAC_RTC_TIMER)
42 #include <linux/rtc.h>
43 static struct rtc_device
*stmmac_rtc
;
44 static rtc_task_t stmmac_task
;
46 static void stmmac_rtc_start(unsigned int new_freq
)
48 rtc_irq_set_freq(stmmac_rtc
, &stmmac_task
, new_freq
);
49 rtc_irq_set_state(stmmac_rtc
, &stmmac_task
, 1);
53 static void stmmac_rtc_stop(void)
55 rtc_irq_set_state(stmmac_rtc
, &stmmac_task
, 0);
59 int stmmac_open_ext_timer(struct net_device
*dev
, struct stmmac_timer
*tm
)
61 stmmac_task
.private_data
= dev
;
62 stmmac_task
.func
= stmmac_timer_handler
;
64 stmmac_rtc
= rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE
);
65 if (stmmac_rtc
== NULL
) {
66 pr_error("open rtc device failed\n");
70 rtc_irq_register(stmmac_rtc
, &stmmac_task
);
72 /* Periodic mode is not supported */
73 if ((rtc_irq_set_freq(stmmac_rtc
, &stmmac_task
, tm
->freq
) < 0)) {
74 pr_error("set periodic failed\n");
75 rtc_irq_unregister(stmmac_rtc
, &stmmac_task
);
76 rtc_class_close(stmmac_rtc
);
80 STMMAC_TIMER_MSG(CONFIG_RTC_HCTOSYS_DEVICE
, tm
->freq
);
82 tm
->timer_start
= stmmac_rtc_start
;
83 tm
->timer_stop
= stmmac_rtc_stop
;
88 int stmmac_close_ext_timer(void)
90 rtc_irq_set_state(stmmac_rtc
, &stmmac_task
, 0);
91 rtc_irq_unregister(stmmac_rtc
, &stmmac_task
);
92 rtc_class_close(stmmac_rtc
);
96 #elif defined(CONFIG_STMMAC_TMU_TIMER)
97 #include <linux/clk.h>
98 #define TMU_CHANNEL "tmu2_clk"
99 static struct clk
*timer_clock
;
101 static void stmmac_tmu_start(unsigned int new_freq
)
103 clk_set_rate(timer_clock
, new_freq
);
104 clk_enable(timer_clock
);
108 static void stmmac_tmu_stop(void)
110 clk_disable(timer_clock
);
114 int stmmac_open_ext_timer(struct net_device
*dev
, struct stmmac_timer
*tm
)
116 timer_clock
= clk_get(NULL
, TMU_CHANNEL
);
118 if (timer_clock
== NULL
)
121 if (tmu2_register_user(stmmac_timer_handler
, (void *)dev
) < 0) {
126 STMMAC_TIMER_MSG("TMU2", tm
->freq
);
127 tm
->timer_start
= stmmac_tmu_start
;
128 tm
->timer_stop
= stmmac_tmu_stop
;
133 int stmmac_close_ext_timer(void)
135 clk_disable(timer_clock
);
136 tmu2_unregister_user();
137 clk_put(timer_clock
);