blob: 2a0e1abde7e73eeea25c9c457999765b5315a01f [file] [log] [blame]
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001/*******************************************************************************
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
29static void stmmac_timer_handler(void *data)
30{
31 struct net_device *dev = (struct net_device *)data;
32
33 stmmac_schedule(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070034}
35
36#define STMMAC_TIMER_MSG(timer, freq) \
37printk(KERN_INFO "stmmac_timer: %s Timer ON (freq %dHz)\n", timer, freq);
38
39#if defined(CONFIG_STMMAC_RTC_TIMER)
40#include <linux/rtc.h>
41static struct rtc_device *stmmac_rtc;
42static rtc_task_t stmmac_task;
43
44static void stmmac_rtc_start(unsigned int new_freq)
45{
46 rtc_irq_set_freq(stmmac_rtc, &stmmac_task, new_freq);
47 rtc_irq_set_state(stmmac_rtc, &stmmac_task, 1);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070048}
49
50static void stmmac_rtc_stop(void)
51{
52 rtc_irq_set_state(stmmac_rtc, &stmmac_task, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070053}
54
55int stmmac_open_ext_timer(struct net_device *dev, struct stmmac_timer *tm)
56{
57 stmmac_task.private_data = dev;
58 stmmac_task.func = stmmac_timer_handler;
59
60 stmmac_rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
61 if (stmmac_rtc == NULL) {
Giuseppe CAVALLAROe6e36252009-11-22 22:58:40 +000062 pr_err("open rtc device failed\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070063 return -ENODEV;
64 }
65
66 rtc_irq_register(stmmac_rtc, &stmmac_task);
67
68 /* Periodic mode is not supported */
69 if ((rtc_irq_set_freq(stmmac_rtc, &stmmac_task, tm->freq) < 0)) {
Giuseppe CAVALLAROe6e36252009-11-22 22:58:40 +000070 pr_err("set periodic failed\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070071 rtc_irq_unregister(stmmac_rtc, &stmmac_task);
72 rtc_class_close(stmmac_rtc);
73 return -1;
74 }
75
76 STMMAC_TIMER_MSG(CONFIG_RTC_HCTOSYS_DEVICE, tm->freq);
77
78 tm->timer_start = stmmac_rtc_start;
79 tm->timer_stop = stmmac_rtc_stop;
80
81 return 0;
82}
83
84int stmmac_close_ext_timer(void)
85{
86 rtc_irq_set_state(stmmac_rtc, &stmmac_task, 0);
87 rtc_irq_unregister(stmmac_rtc, &stmmac_task);
88 rtc_class_close(stmmac_rtc);
89 return 0;
90}
91
92#elif defined(CONFIG_STMMAC_TMU_TIMER)
93#include <linux/clk.h>
94#define TMU_CHANNEL "tmu2_clk"
95static struct clk *timer_clock;
96
97static void stmmac_tmu_start(unsigned int new_freq)
98{
99 clk_set_rate(timer_clock, new_freq);
100 clk_enable(timer_clock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700101}
102
103static void stmmac_tmu_stop(void)
104{
105 clk_disable(timer_clock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700106}
107
108int stmmac_open_ext_timer(struct net_device *dev, struct stmmac_timer *tm)
109{
110 timer_clock = clk_get(NULL, TMU_CHANNEL);
111
112 if (timer_clock == NULL)
113 return -1;
114
115 if (tmu2_register_user(stmmac_timer_handler, (void *)dev) < 0) {
116 timer_clock = NULL;
117 return -1;
118 }
119
120 STMMAC_TIMER_MSG("TMU2", tm->freq);
121 tm->timer_start = stmmac_tmu_start;
122 tm->timer_stop = stmmac_tmu_stop;
123
124 return 0;
125}
126
127int stmmac_close_ext_timer(void)
128{
129 clk_disable(timer_clock);
130 tmu2_unregister_user();
131 clk_put(timer_clock);
132 return 0;
133}
134#endif