ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / arm / mach-integrator / impd1.c
1 /*
2  *  linux/arch/arm/mach-integrator/impd1.c
3  *
4  *  Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  This file provides the core support for the IM-PD1 module.
11  *
12  * Module / boot parameters.
13  *   lmid=n   impd1.lmid=n - set the logic module position in stack to 'n'
14  */
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/errno.h>
20
21 #include <asm/io.h>
22 #include <asm/hardware/icst525.h>
23 #include <asm/hardware/amba.h>
24 #include <asm/arch/lm.h>
25 #include <asm/arch/impd1.h>
26 #include <asm/sizes.h>
27
28 static int module_id;
29
30 module_param_named(lmid, module_id, int, 0444);
31 MODULE_PARM_DESC(lmid, "logic module stack position");
32
33 struct impd1_module {
34         void    *base;
35 };
36
37 static const struct icst525_params impd1_vco_params = {
38         .ref            = 24000,        /* 24 MHz */
39         .vco_max        = 200000,       /* 200 MHz */
40         .vd_min         = 12,
41         .vd_max         = 519,
42         .rd_min         = 3,
43         .rd_max         = 120,
44 };
45
46 void impd1_set_vco(struct device *dev, int vconr, unsigned long period)
47 {
48         struct impd1_module *impd1 = dev_get_drvdata(dev);
49         struct icst525_vco vco;
50         u32 val;
51
52         vco = icst525_ps_to_vco(&impd1_vco_params, period);
53
54         pr_debug("Guessed VCO reg params: S=%d R=%d V=%d\n",
55                 vco.s, vco.r, vco.v);
56
57         val = vco.v | (vco.r << 9) | (vco.s << 16);
58
59         writel(0xa05f, impd1->base + IMPD1_LOCK);
60         switch (vconr) {
61         case 1:
62                 writel(val, impd1->base + IMPD1_OSC1);
63                 break;
64         case 2:
65                 writel(val, impd1->base + IMPD1_OSC2);
66                 break;
67         }
68         writel(0, impd1->base + IMPD1_LOCK);
69
70 #if DEBUG
71         vco.v = val & 0x1ff;
72         vco.r = (val >> 9) & 0x7f;
73         vco.s = (val >> 16) & 7;
74
75         pr_debug("IM-PD1: VCO%d clock is %ld kHz\n",
76                  vconr, icst525_khz(&impd1_vco_params, vco));
77 #endif
78 }
79
80 EXPORT_SYMBOL(impd1_set_vco);
81
82 void impd1_tweak_control(struct device *dev, u32 mask, u32 val)
83 {
84         struct impd1_module *impd1 = dev_get_drvdata(dev);
85         u32 cur;
86
87         val &= mask;
88         cur = readl(impd1->base + IMPD1_CTRL) & ~mask;
89         writel(cur | val, impd1->base + IMPD1_CTRL);
90 }
91
92 EXPORT_SYMBOL(impd1_tweak_control);
93
94 struct impd1_device {
95         unsigned long   offset;
96         unsigned int    irq[2];
97         unsigned int    id;
98 };
99
100 static struct impd1_device impd1_devs[] = {
101         {
102                 .offset = 0x03000000,
103                 .id     = 0x00041190,
104         }, {
105                 .offset = 0x00100000,
106                 .irq    = { 1 },
107                 .id     = 0x00141011,
108         }, {
109                 .offset = 0x00200000,
110                 .irq    = { 2 },
111                 .id     = 0x00141011,
112         }, {
113                 .offset = 0x00300000,
114                 .irq    = { 3 },
115                 .id     = 0x00041022,
116         }, {
117                 .offset = 0x00400000,
118                 .irq    = { 4 },
119                 .id     = 0x00041061,
120         }, {
121                 .offset = 0x00500000,
122                 .irq    = { 5 },
123                 .id     = 0x00041061,
124         }, {
125                 .offset = 0x00600000,
126                 .irq    = { 6 },
127                 .id     = 0x00041130,
128         }, {
129                 .offset = 0x00700000,
130                 .irq    = { 7, 8 },
131                 .id     = 0x00041181,
132         }, {
133                 .offset = 0x00800000,
134                 .irq    = { 9 },
135                 .id     = 0x00041041,
136         }, {
137                 .offset = 0x01000000,
138                 .irq    = { 11 },
139                 .id     = 0x00041110,
140         }
141 };
142
143 static int impd1_probe(struct lm_device *dev)
144 {
145         struct impd1_module *impd1;
146         int i, ret;
147
148         if (dev->id != module_id)
149                 return -EINVAL;
150
151         if (!request_mem_region(dev->resource.start, SZ_4K, "LM registers"))
152                 return -EBUSY;
153
154         impd1 = kmalloc(sizeof(struct impd1_module), GFP_KERNEL);
155         if (!impd1) {
156                 ret = -ENOMEM;
157                 goto release_lm;
158         }
159         memset(impd1, 0, sizeof(struct impd1_module));
160
161         impd1->base = ioremap(dev->resource.start, SZ_4K);
162         if (!impd1->base) {
163                 ret = -ENOMEM;
164                 goto free_impd1;
165         }
166
167         lm_set_drvdata(dev, impd1);
168
169         printk("IM-PD1 found at 0x%08lx\n", dev->resource.start);
170
171         for (i = 0; i < ARRAY_SIZE(impd1_devs); i++) {
172                 struct impd1_device *idev = impd1_devs + i;
173                 struct amba_device *d;
174                 unsigned long pc_base;
175
176                 pc_base = dev->resource.start + idev->offset;
177
178                 d = kmalloc(sizeof(struct amba_device), GFP_KERNEL);
179                 if (!d)
180                         continue;
181
182                 memset(d, 0, sizeof(struct amba_device));
183
184                 snprintf(d->dev.bus_id, sizeof(d->dev.bus_id),
185                          "lm%x:%5.5lx", dev->id, idev->offset >> 12);
186
187                 d->dev.parent   = &dev->dev;
188                 d->res.start    = dev->resource.start + idev->offset;
189                 d->res.end      = d->res.start + SZ_4K - 1;
190                 d->res.flags    = IORESOURCE_MEM;
191                 d->irq[0]       = dev->irq;
192                 d->irq[1]       = dev->irq;
193                 d->periphid     = idev->id;
194
195                 ret = amba_device_register(d, &dev->resource);
196                 if (ret) {
197                         printk("unable to register device %s: %d\n",
198                                 d->dev.bus_id, ret);
199                         kfree(d);
200                 }
201         }
202
203         return 0;
204
205  free_impd1:
206         if (impd1 && impd1->base)
207                 iounmap(impd1->base);
208         if (impd1)
209                 kfree(impd1);
210  release_lm:
211         release_mem_region(dev->resource.start, SZ_4K);
212         return ret;
213 }
214
215 static void impd1_remove(struct lm_device *dev)
216 {
217         struct impd1_module *impd1 = lm_get_drvdata(dev);
218         struct list_head *l, *n;
219
220         list_for_each_safe(l, n, &dev->dev.children) {
221                 struct device *d = list_to_dev(l);
222
223                 device_unregister(d);
224         }
225
226         lm_set_drvdata(dev, NULL);
227
228         iounmap(impd1->base);
229         kfree(impd1);
230         release_mem_region(dev->resource.start, SZ_4K);
231 }
232
233 static struct lm_driver impd1_driver = {
234         .drv = {
235                 .name   = "impd1",
236         },
237         .probe          = impd1_probe,
238         .remove         = impd1_remove,
239 };
240
241 static int __init impd1_init(void)
242 {
243         return lm_driver_register(&impd1_driver);
244 }
245
246 static void __exit impd1_exit(void)
247 {
248         lm_driver_unregister(&impd1_driver);
249 }
250
251 module_init(impd1_init);
252 module_exit(impd1_exit);
253
254 MODULE_LICENSE("GPL");
255 MODULE_DESCRIPTION("Integrator/IM-PD1 logic module core driver");
256 MODULE_AUTHOR("Deep Blue Solutions Ltd");