Merge branch 'sh/smp'
[pandora-kernel.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 #include <linux/mm.h>
21 #include <linux/amba/bus.h>
22 #include <linux/amba/clcd.h>
23 #include <linux/io.h>
24 #include <linux/slab.h>
25
26 #include <asm/clkdev.h>
27 #include <mach/clkdev.h>
28 #include <asm/hardware/icst525.h>
29 #include <mach/lm.h>
30 #include <mach/impd1.h>
31 #include <asm/sizes.h>
32
33 static int module_id;
34
35 module_param_named(lmid, module_id, int, 0444);
36 MODULE_PARM_DESC(lmid, "logic module stack position");
37
38 struct impd1_module {
39         void __iomem    *base;
40         struct clk      vcos[2];
41         struct clk_lookup *clks[3];
42 };
43
44 static const struct icst525_params impd1_vco_params = {
45         .ref            = 24000,        /* 24 MHz */
46         .vco_max        = 200000,       /* 200 MHz */
47         .vd_min         = 12,
48         .vd_max         = 519,
49         .rd_min         = 3,
50         .rd_max         = 120,
51 };
52
53 static void impd1_setvco(struct clk *clk, struct icst525_vco vco)
54 {
55         struct impd1_module *impd1 = clk->data;
56         int vconr = clk - impd1->vcos;
57         u32 val;
58
59         val = vco.v | (vco.r << 9) | (vco.s << 16);
60
61         writel(0xa05f, impd1->base + IMPD1_LOCK);
62         switch (vconr) {
63         case 0:
64                 writel(val, impd1->base + IMPD1_OSC1);
65                 break;
66         case 1:
67                 writel(val, impd1->base + IMPD1_OSC2);
68                 break;
69         }
70         writel(0, impd1->base + IMPD1_LOCK);
71
72 #ifdef DEBUG
73         vco.v = val & 0x1ff;
74         vco.r = (val >> 9) & 0x7f;
75         vco.s = (val >> 16) & 7;
76
77         pr_debug("IM-PD1: VCO%d clock is %ld kHz\n",
78                  vconr, icst525_khz(&impd1_vco_params, vco));
79 #endif
80 }
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 /*
95  * CLCD support
96  */
97 #define PANEL           PROSPECTOR
98
99 #define LTM10C209               1
100 #define PROSPECTOR              2
101 #define SVGA                    3
102 #define VGA                     4
103
104 #if PANEL == VGA
105 #define PANELTYPE       vga
106 static struct clcd_panel vga = {
107         .mode           = {
108                 .name           = "VGA",
109                 .refresh        = 60,
110                 .xres           = 640,
111                 .yres           = 480,
112                 .pixclock       = 39721,
113                 .left_margin    = 40,
114                 .right_margin   = 24,
115                 .upper_margin   = 32,
116                 .lower_margin   = 11,
117                 .hsync_len      = 96,
118                 .vsync_len      = 2,
119                 .sync           = 0,
120                 .vmode          = FB_VMODE_NONINTERLACED,
121         },
122         .width          = -1,
123         .height         = -1,
124         .tim2           = TIM2_BCD | TIM2_IPC,
125         .cntl           = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
126         .connector      = IMPD1_CTRL_DISP_VGA,
127         .bpp            = 16,
128         .grayscale      = 0,
129 };
130
131 #elif PANEL == SVGA
132 #define PANELTYPE       svga
133 static struct clcd_panel svga = {
134         .mode           = {
135                 .name           = "SVGA",
136                 .refresh        = 0,
137                 .xres           = 800,
138                 .yres           = 600,
139                 .pixclock       = 27778,
140                 .left_margin    = 20,
141                 .right_margin   = 20,
142                 .upper_margin   = 5,
143                 .lower_margin   = 5,
144                 .hsync_len      = 164,
145                 .vsync_len      = 62,
146                 .sync           = 0,
147                 .vmode          = FB_VMODE_NONINTERLACED,
148         },
149         .width          = -1,
150         .height         = -1,
151         .tim2           = TIM2_BCD,
152         .cntl           = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
153         .connector      = IMPD1_CTRL_DISP_VGA,
154         .bpp            = 16,
155         .grayscale      = 0,
156 };
157
158 #elif PANEL == PROSPECTOR
159 #define PANELTYPE       prospector
160 static struct clcd_panel prospector = {
161         .mode           = {
162                 .name           = "PROSPECTOR",
163                 .refresh        = 0,
164                 .xres           = 640,
165                 .yres           = 480,
166                 .pixclock       = 40000,
167                 .left_margin    = 33,
168                 .right_margin   = 64,
169                 .upper_margin   = 36,
170                 .lower_margin   = 7,
171                 .hsync_len      = 64,
172                 .vsync_len      = 25,
173                 .sync           = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
174                 .vmode          = FB_VMODE_NONINTERLACED,
175         },
176         .width          = -1,
177         .height         = -1,
178         .tim2           = TIM2_BCD,
179         .cntl           = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
180         .fixedtimings   = 1,
181         .connector      = IMPD1_CTRL_DISP_LCD,
182         .bpp            = 16,
183         .grayscale      = 0,
184 };
185
186 #elif PANEL == LTM10C209
187 #define PANELTYPE       ltm10c209
188 /*
189  * Untested.
190  */
191 static struct clcd_panel ltm10c209 = {
192         .mode           = {
193                 .name           = "LTM10C209",
194                 .refresh        = 0,
195                 .xres           = 640,
196                 .yres           = 480,
197                 .pixclock       = 40000,
198                 .left_margin    = 20,
199                 .right_margin   = 20,
200                 .upper_margin   = 19,
201                 .lower_margin   = 19,
202                 .hsync_len      = 20,
203                 .vsync_len      = 10,
204                 .sync           = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
205                 .vmode          = FB_VMODE_NONINTERLACED,
206         },
207         .width          = -1,
208         .height         = -1,
209         .tim2           = TIM2_BCD,
210         .cntl           = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
211         .fixedtimings   = 1,
212         .connector      = IMPD1_CTRL_DISP_LCD,
213         .bpp            = 16,
214         .grayscale      = 0,
215 };
216 #endif
217
218 /*
219  * Disable all display connectors on the interface module.
220  */
221 static void impd1fb_clcd_disable(struct clcd_fb *fb)
222 {
223         impd1_tweak_control(fb->dev->dev.parent, IMPD1_CTRL_DISP_MASK, 0);
224 }
225
226 /*
227  * Enable the relevant connector on the interface module.
228  */
229 static void impd1fb_clcd_enable(struct clcd_fb *fb)
230 {
231         impd1_tweak_control(fb->dev->dev.parent, IMPD1_CTRL_DISP_MASK,
232                         fb->panel->connector | IMPD1_CTRL_DISP_ENABLE);
233 }
234
235 static int impd1fb_clcd_setup(struct clcd_fb *fb)
236 {
237         unsigned long framebase = fb->dev->res.start + 0x01000000;
238         unsigned long framesize = SZ_1M;
239         int ret = 0;
240
241         fb->panel = &PANELTYPE;
242
243         if (!request_mem_region(framebase, framesize, "clcd framebuffer")) {
244                 printk(KERN_ERR "IM-PD1: unable to reserve framebuffer\n");
245                 return -EBUSY;
246         }
247
248         fb->fb.screen_base = ioremap(framebase, framesize);
249         if (!fb->fb.screen_base) {
250                 printk(KERN_ERR "IM-PD1: unable to map framebuffer\n");
251                 ret = -ENOMEM;
252                 goto free_buffer;
253         }
254
255         fb->fb.fix.smem_start   = framebase;
256         fb->fb.fix.smem_len     = framesize;
257
258         return 0;
259
260  free_buffer:
261         release_mem_region(framebase, framesize);
262         return ret;
263 }
264
265 static int impd1fb_clcd_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
266 {
267         unsigned long start, size;
268
269         start = vma->vm_pgoff + (fb->fb.fix.smem_start >> PAGE_SHIFT);
270         size = vma->vm_end - vma->vm_start;
271
272         return remap_pfn_range(vma, vma->vm_start, start, size,
273                                vma->vm_page_prot);
274 }
275
276 static void impd1fb_clcd_remove(struct clcd_fb *fb)
277 {
278         iounmap(fb->fb.screen_base);
279         release_mem_region(fb->fb.fix.smem_start, fb->fb.fix.smem_len);
280 }
281
282 static struct clcd_board impd1_clcd_data = {
283         .name           = "IM-PD/1",
284         .check          = clcdfb_check,
285         .decode         = clcdfb_decode,
286         .disable        = impd1fb_clcd_disable,
287         .enable         = impd1fb_clcd_enable,
288         .setup          = impd1fb_clcd_setup,
289         .mmap           = impd1fb_clcd_mmap,
290         .remove         = impd1fb_clcd_remove,
291 };
292
293 struct impd1_device {
294         unsigned long   offset;
295         unsigned int    irq[2];
296         unsigned int    id;
297         void            *platform_data;
298 };
299
300 static struct impd1_device impd1_devs[] = {
301         {
302                 .offset = 0x03000000,
303                 .id     = 0x00041190,
304         }, {
305                 .offset = 0x00100000,
306                 .irq    = { 1 },
307                 .id     = 0x00141011,
308         }, {
309                 .offset = 0x00200000,
310                 .irq    = { 2 },
311                 .id     = 0x00141011,
312         }, {
313                 .offset = 0x00300000,
314                 .irq    = { 3 },
315                 .id     = 0x00041022,
316         }, {
317                 .offset = 0x00400000,
318                 .irq    = { 4 },
319                 .id     = 0x00041061,
320         }, {
321                 .offset = 0x00500000,
322                 .irq    = { 5 },
323                 .id     = 0x00041061,
324         }, {
325                 .offset = 0x00600000,
326                 .irq    = { 6 },
327                 .id     = 0x00041130,
328         }, {
329                 .offset = 0x00700000,
330                 .irq    = { 7, 8 },
331                 .id     = 0x00041181,
332         }, {
333                 .offset = 0x00800000,
334                 .irq    = { 9 },
335                 .id     = 0x00041041,
336         }, {
337                 .offset = 0x01000000,
338                 .irq    = { 11 },
339                 .id     = 0x00041110,
340                 .platform_data = &impd1_clcd_data,
341         }
342 };
343
344 static struct clk fixed_14745600 = {
345         .rate = 14745600,
346 };
347
348 static int impd1_probe(struct lm_device *dev)
349 {
350         struct impd1_module *impd1;
351         int i, ret;
352
353         if (dev->id != module_id)
354                 return -EINVAL;
355
356         if (!request_mem_region(dev->resource.start, SZ_4K, "LM registers"))
357                 return -EBUSY;
358
359         impd1 = kzalloc(sizeof(struct impd1_module), GFP_KERNEL);
360         if (!impd1) {
361                 ret = -ENOMEM;
362                 goto release_lm;
363         }
364
365         impd1->base = ioremap(dev->resource.start, SZ_4K);
366         if (!impd1->base) {
367                 ret = -ENOMEM;
368                 goto free_impd1;
369         }
370
371         lm_set_drvdata(dev, impd1);
372
373         printk("IM-PD1 found at 0x%08lx\n",
374                 (unsigned long)dev->resource.start);
375
376         for (i = 0; i < ARRAY_SIZE(impd1->vcos); i++) {
377                 impd1->vcos[i].owner = THIS_MODULE,
378                 impd1->vcos[i].params = &impd1_vco_params,
379                 impd1->vcos[i].data = impd1,
380                 impd1->vcos[i].setvco = impd1_setvco;
381         }
382
383         impd1->clks[0] = clkdev_alloc(&impd1->vcos[0], NULL, "lm%x:01000",
384                                         dev->id);
385         impd1->clks[1] = clkdev_alloc(&fixed_14745600, NULL, "lm%x:00100",
386                                         dev->id);
387         impd1->clks[2] = clkdev_alloc(&fixed_14745600, NULL, "lm%x:00200",
388                                         dev->id);
389         for (i = 0; i < ARRAY_SIZE(impd1->clks); i++)
390                 clkdev_add(impd1->clks[i]);
391
392         for (i = 0; i < ARRAY_SIZE(impd1_devs); i++) {
393                 struct impd1_device *idev = impd1_devs + i;
394                 struct amba_device *d;
395                 unsigned long pc_base;
396
397                 pc_base = dev->resource.start + idev->offset;
398
399                 d = kzalloc(sizeof(struct amba_device), GFP_KERNEL);
400                 if (!d)
401                         continue;
402
403                 dev_set_name(&d->dev, "lm%x:%5.5lx", dev->id, idev->offset >> 12);
404                 d->dev.parent   = &dev->dev;
405                 d->res.start    = dev->resource.start + idev->offset;
406                 d->res.end      = d->res.start + SZ_4K - 1;
407                 d->res.flags    = IORESOURCE_MEM;
408                 d->irq[0]       = dev->irq;
409                 d->irq[1]       = dev->irq;
410                 d->periphid     = idev->id;
411                 d->dev.platform_data = idev->platform_data;
412
413                 ret = amba_device_register(d, &dev->resource);
414                 if (ret) {
415                         dev_err(&d->dev, "unable to register device: %d\n", ret);
416                         kfree(d);
417                 }
418         }
419
420         return 0;
421
422  free_impd1:
423         if (impd1 && impd1->base)
424                 iounmap(impd1->base);
425         kfree(impd1);
426  release_lm:
427         release_mem_region(dev->resource.start, SZ_4K);
428         return ret;
429 }
430
431 static int impd1_remove_one(struct device *dev, void *data)
432 {
433         device_unregister(dev);
434         return 0;
435 }
436
437 static void impd1_remove(struct lm_device *dev)
438 {
439         struct impd1_module *impd1 = lm_get_drvdata(dev);
440         int i;
441
442         device_for_each_child(&dev->dev, NULL, impd1_remove_one);
443
444         for (i = 0; i < ARRAY_SIZE(impd1->clks); i++)
445                 clkdev_drop(impd1->clks[i]);
446
447         lm_set_drvdata(dev, NULL);
448
449         iounmap(impd1->base);
450         kfree(impd1);
451         release_mem_region(dev->resource.start, SZ_4K);
452 }
453
454 static struct lm_driver impd1_driver = {
455         .drv = {
456                 .name   = "impd1",
457         },
458         .probe          = impd1_probe,
459         .remove         = impd1_remove,
460 };
461
462 static int __init impd1_init(void)
463 {
464         return lm_driver_register(&impd1_driver);
465 }
466
467 static void __exit impd1_exit(void)
468 {
469         lm_driver_unregister(&impd1_driver);
470 }
471
472 module_init(impd1_init);
473 module_exit(impd1_exit);
474
475 MODULE_LICENSE("GPL");
476 MODULE_DESCRIPTION("Integrator/IM-PD1 logic module core driver");
477 MODULE_AUTHOR("Deep Blue Solutions Ltd");