]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/net/stmmac/stmmac_timer.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6.git] / drivers / net / stmmac / stmmac_timer.c
1 /*******************************************************************************
2   STMMAC external timer support.
3
4   Copyright (C) 2007-2009  STMicroelectronics Ltd
5
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.
9
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
13   more details.
14
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.
18
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21
22   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
23 *******************************************************************************/
24
25 #include <linux/kernel.h>
26 #include <linux/etherdevice.h>
27 #include "stmmac_timer.h"
28
29 static void stmmac_timer_handler(void *data)
30 {
31         struct net_device *dev = (struct net_device *)data;
32
33         stmmac_schedule(dev);
34
35         return;
36 }
37
38 #define STMMAC_TIMER_MSG(timer, freq) \
39 printk(KERN_INFO "stmmac_timer: %s Timer ON (freq %dHz)\n", timer, freq);
40
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;
45
46 static void stmmac_rtc_start(unsigned int new_freq)
47 {
48         rtc_irq_set_freq(stmmac_rtc, &stmmac_task, new_freq);
49         rtc_irq_set_state(stmmac_rtc, &stmmac_task, 1);
50         return;
51 }
52
53 static void stmmac_rtc_stop(void)
54 {
55         rtc_irq_set_state(stmmac_rtc, &stmmac_task, 0);
56         return;
57 }
58
59 int stmmac_open_ext_timer(struct net_device *dev, struct stmmac_timer *tm)
60 {
61         stmmac_task.private_data = dev;
62         stmmac_task.func = stmmac_timer_handler;
63
64         stmmac_rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
65         if (stmmac_rtc == NULL) {
66                 pr_err("open rtc device failed\n");
67                 return -ENODEV;
68         }
69
70         rtc_irq_register(stmmac_rtc, &stmmac_task);
71
72         /* Periodic mode is not supported */
73         if ((rtc_irq_set_freq(stmmac_rtc, &stmmac_task, tm->freq) < 0)) {
74                 pr_err("set periodic failed\n");
75                 rtc_irq_unregister(stmmac_rtc, &stmmac_task);
76                 rtc_class_close(stmmac_rtc);
77                 return -1;
78         }
79
80         STMMAC_TIMER_MSG(CONFIG_RTC_HCTOSYS_DEVICE, tm->freq);
81
82         tm->timer_start = stmmac_rtc_start;
83         tm->timer_stop = stmmac_rtc_stop;
84
85         return 0;
86 }
87
88 int stmmac_close_ext_timer(void)
89 {
90         rtc_irq_set_state(stmmac_rtc, &stmmac_task, 0);
91         rtc_irq_unregister(stmmac_rtc, &stmmac_task);
92         rtc_class_close(stmmac_rtc);
93         return 0;
94 }
95
96 #elif defined(CONFIG_STMMAC_TMU_TIMER)
97 #include <linux/clk.h>
98 #define TMU_CHANNEL "tmu2_clk"
99 static struct clk *timer_clock;
100
101 static void stmmac_tmu_start(unsigned int new_freq)
102 {
103         clk_set_rate(timer_clock, new_freq);
104         clk_enable(timer_clock);
105         return;
106 }
107
108 static void stmmac_tmu_stop(void)
109 {
110         clk_disable(timer_clock);
111         return;
112 }
113
114 int stmmac_open_ext_timer(struct net_device *dev, struct stmmac_timer *tm)
115 {
116         timer_clock = clk_get(NULL, TMU_CHANNEL);
117
118         if (timer_clock == NULL)
119                 return -1;
120
121         if (tmu2_register_user(stmmac_timer_handler, (void *)dev) < 0) {
122                 timer_clock = NULL;
123                 return -1;
124         }
125
126         STMMAC_TIMER_MSG("TMU2", tm->freq);
127         tm->timer_start = stmmac_tmu_start;
128         tm->timer_stop = stmmac_tmu_stop;
129
130         return 0;
131 }
132
133 int stmmac_close_ext_timer(void)
134 {
135         clk_disable(timer_clock);
136         tmu2_unregister_user();
137         clk_put(timer_clock);
138         return 0;
139 }
140 #endif