]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/misc/sti/sdti.c
SDTI: Fix sdti to use right clocks from clockfw
[linux-2.6-omap-h63xx.git] / drivers / misc / sti / sdti.c
1 /*
2  * Support functions for OMAP3 SDTI (Serial Debug Tracing Interface)
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  * Written by: Roman Tereshonkov <roman.tereshonkov@nokia.com>
6  *
7  * This file is subject to the terms and conditions of the GNU General Public
8  * License.  See the file "COPYING" in the main directory of this archive
9  * for more details.
10  */
11 #include <linux/init.h>
12 #include <linux/err.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/spinlock.h>
16 #include <linux/interrupt.h>
17 #include <linux/platform_device.h>
18 #include <linux/clk.h>
19 #include <mach/sti.h>
20 #include <asm/byteorder.h>
21 #include <asm/io.h>
22
23 #define SDTI_REVISION           0x000
24 #define SDTI_SYSCONFIG          0x010
25 #define SDTI_SYSSTATUS          0x014
26 #define SDTI_WINCTRL            0x024
27 #define SDTI_SCONFIG            0x028
28 #define SDTI_TESTCTRL           0x02C
29 #define SDTI_LOCK_ACCESS        0xFB0
30
31 #define CPU1_TRACE_EN           0x01
32 #define CPU2_TRACE_EN           0x02
33
34 #define SDTI_SYSCONFIG_SOFTRESET        (1 << 1)
35 #define SDTI_SYSCONFIG_AUTOIDLE         (1 << 0)
36
37 static struct clk *sdti_fck, *sdti_ick;
38 void __iomem *sti_base, *sti_channel_base;
39 static DEFINE_SPINLOCK(sdti_lock);
40
41 void sti_channel_write_trace(int len, int id, void *data,
42                                 unsigned int channel)
43 {
44         const u8 *tpntr = data;
45
46         spin_lock_irq(&sdti_lock);
47
48         sti_channel_writeb(id, channel);
49         while (len--)
50                 sti_channel_writeb(*tpntr++, channel);
51         sti_channel_flush(channel);
52
53         spin_unlock_irq(&sdti_lock);
54 }
55 EXPORT_SYMBOL(sti_channel_write_trace);
56
57 static void omap_sdti_reset(void)
58 {
59         int i;
60
61         sti_writel(SDTI_SYSCONFIG_SOFTRESET, SDTI_SYSCONFIG);
62
63         for (i = 0; i < 10000; i++)
64                 if (sti_readl(SDTI_SYSSTATUS) & 1)
65                         break;
66         if (i == 10000)
67                 printk(KERN_WARNING "XTI: no real reset\n");
68 }
69
70 static int __init omap_sdti_init(void)
71 {
72         char buf[64];
73         int i, ret = 0;
74
75         sdti_fck = clk_get(NULL, "pclk_fck");
76         if (IS_ERR(sdti_fck)) {
77                 printk(KERN_ERR "Cannot get clk pclk_fck\n");
78                 ret = PTR_ERR(sdti_fck);
79                 goto err0;
80         }
81         sdti_ick = clk_get(NULL, "pclkx2_fck");
82         if (IS_ERR(sdti_ick)) {
83                 printk(KERN_ERR "Cannot get clk pclkx2_fck\n");
84                 ret = PTR_ERR(sdti_ick);
85                 goto err1;
86         }
87         ret = clk_enable(sdti_fck);
88         if (ret) {
89                 printk(KERN_ERR "Cannot enable sdti_fck\n");
90                 goto err2;
91         }
92         ret = clk_enable(sdti_ick);
93         if (ret) {
94                 printk(KERN_ERR "Cannot enable sdti_ick\n");
95                 goto err3;
96         }
97
98         omap_sdti_reset();
99         sti_writel(0xC5ACCE55, SDTI_LOCK_ACCESS);
100
101         /* Autoidle */
102         sti_writel(SDTI_SYSCONFIG_AUTOIDLE, SDTI_SYSCONFIG);
103
104         /* Claim SDTI */
105         sti_writel(1 << 30, SDTI_WINCTRL);
106         i = sti_readl(SDTI_WINCTRL);
107         if (!(i & (1 << 30)))
108                 printk(KERN_WARNING "SDTI: cannot claim SDTI\n");
109
110         /* 4 bits dual, fclk/3 */
111         sti_writel(0x43, SDTI_SCONFIG);
112
113         /* CPU2 trace enable */
114         sti_writel(i | CPU2_TRACE_EN, SDTI_WINCTRL);
115         i = sti_readl(SDTI_WINCTRL);
116
117         /* Enable SDTI */
118         sti_writel((1 << 31) | (i & 0x3FFFFFFF), SDTI_WINCTRL);
119
120         i = sti_readl(SDTI_REVISION);
121         snprintf(buf, sizeof(buf), "OMAP SDTI support loaded (HW v%u.%u)\n",
122                 (i >> 4) & 0x0f, i & 0x0f);
123         printk(KERN_INFO "%s", buf);
124         sti_channel_write_trace(strlen(buf), 0xc3, buf, 239);
125
126         return ret;
127
128 err3:
129         clk_disable(sdti_fck);
130 err2:
131         clk_put(sdti_ick);
132 err1:
133         clk_put(sdti_fck);
134 err0:
135         return ret;
136 }
137
138 static void omap_sdti_exit(void)
139 {
140         sti_writel(0, SDTI_WINCTRL);
141         clk_disable(sdti_fck);
142         clk_disable(sdti_ick);
143         clk_put(sdti_fck);
144         clk_put(sdti_ick);
145 }
146
147 static int __devinit omap_sdti_probe(struct platform_device *pdev)
148 {
149         struct resource *res, *cres;
150         unsigned int size;
151
152         if (pdev->num_resources != 2) {
153                 dev_err(&pdev->dev, "invalid number of resources: %d\n",
154                         pdev->num_resources);
155                 return -ENODEV;
156         }
157
158         /* SDTI base */
159         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
160         if (unlikely(!res)) {
161                 dev_err(&pdev->dev, "invalid mem resource\n");
162                 return -ENODEV;
163         }
164
165         /* Channel base */
166         cres = platform_get_resource(pdev, IORESOURCE_MEM, 1);
167         if (unlikely(!cres)) {
168                 dev_err(&pdev->dev, "invalid channel mem resource\n");
169                 return -ENODEV;
170         }
171
172         size = res->end - res->start;
173         sti_base = ioremap(res->start, size);
174         if (unlikely(!sti_base))
175                 return -ENODEV;
176
177         size = cres->end - cres->start;
178         sti_channel_base = ioremap(cres->start, size);
179         if (unlikely(!sti_channel_base)) {
180                 iounmap(sti_base);
181                 return -ENODEV;
182         }
183
184         return omap_sdti_init();
185 }
186
187 static int __devexit omap_sdti_remove(struct platform_device *pdev)
188 {
189         iounmap(sti_channel_base);
190         iounmap(sti_base);
191         omap_sdti_exit();
192
193         return 0;
194 }
195
196 static struct platform_driver omap_sdti_driver = {
197         .probe          = omap_sdti_probe,
198         .remove         = __devexit_p(omap_sdti_remove),
199         .driver         = {
200                 .name   = "sti",
201                 .owner  = THIS_MODULE,
202         },
203 };
204
205 static int __init omap_sdti_module_init(void)
206 {
207         return platform_driver_register(&omap_sdti_driver);
208 }
209
210 static void __exit omap_sdti_module_exit(void)
211 {
212         platform_driver_unregister(&omap_sdti_driver);
213 }
214 subsys_initcall(omap_sdti_module_init);
215 module_exit(omap_sdti_module_exit);
216
217 MODULE_AUTHOR("Roman Tereshonkov");
218 MODULE_LICENSE("GPL");