1 /* Smedia Glamo 336x/337x driver
3 * (C) 2007 by Openmoko, Inc.
4 * Author: Harald Welte <laforge@openmoko.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
28 #include <linux/delay.h>
30 #include <linux/init.h>
31 #include <linux/irq.h>
32 #include <linux/interrupt.h>
33 #include <linux/workqueue.h>
34 #include <linux/platform_device.h>
35 #include <linux/kernel_stat.h>
36 #include <linux/spinlock.h>
37 #include <linux/mfd/core.h>
38 #include <linux/mfd/glamo.h>
39 #include <linux/spi/glamo.h>
40 #include <linux/glamo-gpio.h>
41 #include <linux/glamofb.h>
44 #include <asm/div64.h>
48 #include "glamo-regs.h"
49 #include "glamo-core.h"
51 #define GLAMO_MEM_REFRESH_COUNT 0x100
54 * Glamo internal settings
56 * We run the memory interface from the faster PLLB on 2.6.28 kernels and
57 * above. Couple of GTA02 users report trouble with memory bus when they
58 * upgraded from 2.6.24. So this parameter allows reversion to 2.6.24
59 * scheme if their Glamo chip needs it.
61 * you can override the faster default on kernel commandline using
63 * glamo3362.slow_memory=1
68 static int slow_memory
= 0;
69 module_param(slow_memory
, int, 0644);
78 struct reg_range reg_range
[] = {
79 { 0x0000, 0x76, "General", 1 },
80 { 0x0200, 0x18, "Host Bus", 1 },
81 { 0x0300, 0x38, "Memory", 1 },
82 /* { 0x0400, 0x100, "Sensor", 0 }, */
83 /* { 0x0500, 0x300, "ISP", 0 }, */
84 /* { 0x0800, 0x400, "JPEG", 0 }, */
85 /* { 0x0c00, 0xcc, "MPEG", 0 }, */
86 { 0x1100, 0xb2, "LCD 1", 1 },
87 { 0x1200, 0x64, "LCD 2", 1 },
88 { 0x1400, 0x42, "MMC", 1 },
89 /* { 0x1500, 0x080, "MPU 0", 0 },
90 { 0x1580, 0x080, "MPU 1", 0 },
91 { 0x1600, 0x080, "Cmd Queue", 0 },
92 { 0x1680, 0x080, "RISC CPU", 0 },*/
93 { 0x1700, 0x400, "2D Unit", 0 },
94 /* { 0x1b00, 0x900, "3D Unit", 0 }, */
97 static inline void __reg_write(struct glamo_core
*glamo
,
98 u_int16_t reg
, u_int16_t val
)
100 writew(val
, glamo
->base
+ reg
);
103 static inline u_int16_t
__reg_read(struct glamo_core
*glamo
,
106 return readw(glamo
->base
+ reg
);
109 static void __reg_set_bit_mask(struct glamo_core
*glamo
,
110 u_int16_t reg
, u_int16_t mask
,
117 tmp
= __reg_read(glamo
, reg
);
120 __reg_write(glamo
, reg
, tmp
);
123 static void reg_set_bit_mask(struct glamo_core
*glamo
,
124 u_int16_t reg
, u_int16_t mask
,
127 spin_lock(&glamo
->lock
);
128 __reg_set_bit_mask(glamo
, reg
, mask
, val
);
129 spin_unlock(&glamo
->lock
);
132 static inline void __reg_set_bit(struct glamo_core
*glamo
,
133 u_int16_t reg
, u_int16_t bit
)
135 __reg_set_bit_mask(glamo
, reg
, bit
, 0xffff);
138 static inline void __reg_clear_bit(struct glamo_core
*glamo
,
139 u_int16_t reg
, u_int16_t bit
)
141 __reg_set_bit_mask(glamo
, reg
, bit
, 0);
144 /***********************************************************************
145 * resources of sibling devices
146 ***********************************************************************/
148 static struct resource glamo_fb_resources
[] = {
150 .name
= "glamo-fb-regs",
151 .start
= GLAMO_REGOFS_LCD
,
152 .end
= GLAMO_REGOFS_MMC
- 1,
153 .flags
= IORESOURCE_MEM
,
155 .name
= "glamo-fb-mem",
156 .start
= GLAMO_OFFSET_FB
,
157 .end
= GLAMO_OFFSET_FB
+ GLAMO_FB_SIZE
- 1,
158 .flags
= IORESOURCE_MEM
,
162 static struct resource glamo_mmc_resources
[] = {
164 .start
= GLAMO_REGOFS_MMC
,
165 .end
= GLAMO_REGOFS_MPROC0
- 1,
166 .flags
= IORESOURCE_MEM
168 .start
= IRQ_GLAMO_MMC
,
169 .end
= IRQ_GLAMO_MMC
,
170 .flags
= IORESOURCE_IRQ
,
171 }, { /* our data buffer for MMC transfers */
172 .start
= GLAMO_OFFSET_FB
+ GLAMO_FB_SIZE
,
173 .end
= GLAMO_OFFSET_FB
+ GLAMO_FB_SIZE
+
174 GLAMO_MMC_BUFFER_SIZE
- 1,
175 .flags
= IORESOURCE_MEM
185 static struct mfd_cell glamo_cells
[] = {
188 .num_resources
= ARRAY_SIZE(glamo_fb_resources
),
189 .resources
= glamo_fb_resources
,
193 .num_resources
= ARRAY_SIZE(glamo_mmc_resources
),
194 .resources
= glamo_mmc_resources
,
196 [GLAMO_CELL_GPIO
] = {
197 .name
= "glamo-gpio",
202 /***********************************************************************
204 ***********************************************************************/
205 #define irq2glamo(x) (x - IRQ_GLAMO(0))
207 static void glamo_ack_irq(unsigned int irq
)
209 struct glamo_core
*glamo
= (struct glamo_core
*)get_irq_chip_data(irq
);
210 /* clear interrupt source */
211 __reg_write(glamo
, GLAMO_REG_IRQ_CLEAR
,
212 1 << irq2glamo(irq
));
215 static void glamo_mask_irq(unsigned int irq
)
217 struct glamo_core
*glamo
= (struct glamo_core
*)get_irq_chip_data(irq
);
220 /* clear bit in enable register */
221 tmp
= __reg_read(glamo
, GLAMO_REG_IRQ_ENABLE
);
222 tmp
&= ~(1 << irq2glamo(irq
));
223 __reg_write(glamo
, GLAMO_REG_IRQ_ENABLE
, tmp
);
226 static void glamo_unmask_irq(unsigned int irq
)
228 struct glamo_core
*glamo
= (struct glamo_core
*)get_irq_chip_data(irq
);
231 /* set bit in enable register */
232 tmp
= __reg_read(glamo
, GLAMO_REG_IRQ_ENABLE
);
233 tmp
|= (1 << irq2glamo(irq
));
234 __reg_write(glamo
, GLAMO_REG_IRQ_ENABLE
, tmp
);
237 static struct irq_chip glamo_irq_chip
= {
239 .ack
= glamo_ack_irq
,
240 .mask
= glamo_mask_irq
,
241 .unmask
= glamo_unmask_irq
,
244 static void glamo_irq_demux_handler(unsigned int irq
, struct irq_desc
*desc
)
246 struct glamo_core
*glamo
= get_irq_desc_chip_data(desc
);
247 desc
->status
&= ~(IRQ_REPLAY
| IRQ_WAITING
);
249 if (unlikely(desc
->status
& IRQ_INPROGRESS
)) {
250 desc
->status
|= (IRQ_PENDING
| IRQ_MASKED
);
251 desc
->chip
->mask(irq
);
252 desc
->chip
->ack(irq
);
255 kstat_incr_irqs_this_cpu(irq
, desc
);
257 desc
->chip
->ack(irq
);
258 desc
->status
|= IRQ_INPROGRESS
;
264 if (unlikely((desc
->status
&
265 (IRQ_PENDING
| IRQ_MASKED
| IRQ_DISABLED
)) ==
266 (IRQ_PENDING
| IRQ_MASKED
))) {
267 /* dealing with pending IRQ, unmasking */
268 desc
->chip
->unmask(irq
);
269 desc
->status
&= ~IRQ_MASKED
;
272 desc
->status
&= ~IRQ_PENDING
;
274 /* read IRQ status register */
275 irqstatus
= __reg_read(glamo
, GLAMO_REG_IRQ_STATUS
);
276 for (i
= 0; i
< 9; i
++)
277 if (irqstatus
& (1 << i
))
278 desc_handle_irq(IRQ_GLAMO(i
),
279 irq_desc
+IRQ_GLAMO(i
));
281 } while ((desc
->status
& (IRQ_PENDING
| IRQ_DISABLED
)) == IRQ_PENDING
);
283 desc
->status
&= ~IRQ_INPROGRESS
;
287 static ssize_t
regs_write(struct device
*dev
, struct device_attribute
*attr
,
288 const char *buf
, size_t count
)
290 unsigned long reg
= simple_strtoul(buf
, NULL
, 10);
291 struct glamo_core
*glamo
= dev_get_drvdata(dev
);
293 while (*buf
&& (*buf
!= ' '))
297 while (*buf
&& (*buf
== ' '))
302 printk(KERN_INFO
"reg 0x%02lX <-- 0x%04lX\n",
303 reg
, simple_strtoul(buf
, NULL
, 10));
305 __reg_write(glamo
, reg
, simple_strtoul(buf
, NULL
, 10));
310 static ssize_t
regs_read(struct device
*dev
, struct device_attribute
*attr
,
313 struct glamo_core
*glamo
= dev_get_drvdata(dev
);
317 spin_lock(&glamo
->lock
);
319 for (r
= 0; r
< ARRAY_SIZE(reg_range
); r
++) {
320 if (!reg_range
[r
].dump
)
323 end
+= sprintf(end
, "\n%s\n", reg_range
[r
].name
);
324 for (n
= reg_range
[r
].start
;
325 n
< reg_range
[r
].start
+ reg_range
[r
].count
; n
+= 2) {
326 if (((n1
++) & 7) == 0)
327 end
+= sprintf(end
, "\n%04X: ", n
);
328 end
+= sprintf(end
, "%04x ", __reg_read(glamo
, n
));
330 end
+= sprintf(end
, "\n");
336 spin_unlock(&glamo
->lock
);
341 static DEVICE_ATTR(regs
, 0644, regs_read
, regs_write
);
342 static struct attribute
*glamo_sysfs_entries
[] = {
346 static struct attribute_group glamo_attr_group
= {
348 .attrs
= glamo_sysfs_entries
,
353 /***********************************************************************
355 ***********************************************************************/
357 int __glamo_engine_enable(struct glamo_core
*glamo
, enum glamo_engine engine
)
360 case GLAMO_ENGINE_LCD
:
361 __reg_set_bit_mask(glamo
, GLAMO_REG_HOSTBUS(2),
362 GLAMO_HOSTBUS2_MMIO_EN_LCD
,
363 GLAMO_HOSTBUS2_MMIO_EN_LCD
);
364 __reg_write(glamo
, GLAMO_REG_CLOCK_LCD
,
365 GLAMO_CLOCK_LCD_EN_M5CLK
|
366 GLAMO_CLOCK_LCD_EN_DHCLK
|
367 GLAMO_CLOCK_LCD_EN_DMCLK
|
368 GLAMO_CLOCK_LCD_EN_DCLK
|
369 GLAMO_CLOCK_LCD_DG_M5CLK
|
370 GLAMO_CLOCK_LCD_DG_DMCLK
);
371 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_GEN5_1
,
372 GLAMO_CLOCK_GEN51_EN_DIV_DHCLK
|
373 GLAMO_CLOCK_GEN51_EN_DIV_DMCLK
|
374 GLAMO_CLOCK_GEN51_EN_DIV_DCLK
, 0xffff);
376 case GLAMO_ENGINE_MMC
:
377 __reg_set_bit_mask(glamo
, GLAMO_REG_HOSTBUS(2),
378 GLAMO_HOSTBUS2_MMIO_EN_MMC
,
379 GLAMO_HOSTBUS2_MMIO_EN_MMC
);
380 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_MMC
,
381 GLAMO_CLOCK_MMC_EN_M9CLK
|
382 GLAMO_CLOCK_MMC_EN_TCLK
|
383 GLAMO_CLOCK_MMC_DG_M9CLK
|
384 GLAMO_CLOCK_MMC_DG_TCLK
,
386 /* enable the TCLK divider clk input */
387 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_GEN5_1
,
388 GLAMO_CLOCK_GEN51_EN_DIV_TCLK
,
389 GLAMO_CLOCK_GEN51_EN_DIV_TCLK
);
391 case GLAMO_ENGINE_2D
:
392 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_2D
,
393 GLAMO_CLOCK_2D_EN_M7CLK
|
394 GLAMO_CLOCK_2D_EN_GCLK
|
395 GLAMO_CLOCK_2D_DG_M7CLK
|
396 GLAMO_CLOCK_2D_DG_GCLK
, 0xffff);
397 __reg_set_bit_mask(glamo
, GLAMO_REG_HOSTBUS(2),
398 GLAMO_HOSTBUS2_MMIO_EN_2D
,
399 GLAMO_HOSTBUS2_MMIO_EN_2D
);
400 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_GEN5_1
,
401 GLAMO_CLOCK_GEN51_EN_DIV_GCLK
,
404 case GLAMO_ENGINE_CMDQ
:
405 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_2D
,
406 GLAMO_CLOCK_2D_EN_M6CLK
, 0xffff);
407 __reg_set_bit_mask(glamo
, GLAMO_REG_HOSTBUS(2),
408 GLAMO_HOSTBUS2_MMIO_EN_CQ
,
409 GLAMO_HOSTBUS2_MMIO_EN_CQ
);
410 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_GEN5_1
,
411 GLAMO_CLOCK_GEN51_EN_DIV_MCLK
,
414 /* FIXME: Implementation */
419 glamo
->engine_enabled_bitfield
|= 1 << engine
;
424 int glamo_engine_enable(struct glamo_core
*glamo
, enum glamo_engine engine
)
428 spin_lock(&glamo
->lock
);
430 ret
= __glamo_engine_enable(glamo
, engine
);
432 spin_unlock(&glamo
->lock
);
436 EXPORT_SYMBOL_GPL(glamo_engine_enable
);
438 int __glamo_engine_disable(struct glamo_core
*glamo
, enum glamo_engine engine
)
441 case GLAMO_ENGINE_LCD
:
442 /* remove pixel clock to LCM */
443 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_LCD
,
444 GLAMO_CLOCK_LCD_EN_DCLK
, 0);
445 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_LCD
,
446 GLAMO_CLOCK_LCD_EN_DHCLK
|
447 GLAMO_CLOCK_LCD_EN_DMCLK
, 0);
448 /* kill memory clock */
449 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_LCD
,
450 GLAMO_CLOCK_LCD_EN_M5CLK
, 0);
451 /* stop dividing the clocks */
452 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_GEN5_1
,
453 GLAMO_CLOCK_GEN51_EN_DIV_DHCLK
|
454 GLAMO_CLOCK_GEN51_EN_DIV_DMCLK
|
455 GLAMO_CLOCK_GEN51_EN_DIV_DCLK
, 0);
458 case GLAMO_ENGINE_MMC
:
459 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_MMC
,
460 GLAMO_CLOCK_MMC_EN_M9CLK
|
461 GLAMO_CLOCK_MMC_EN_TCLK
|
462 GLAMO_CLOCK_MMC_DG_M9CLK
|
463 GLAMO_CLOCK_MMC_DG_TCLK
, 0);
464 /* disable the TCLK divider clk input */
465 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_GEN5_1
,
466 GLAMO_CLOCK_GEN51_EN_DIV_TCLK
, 0);
468 case GLAMO_ENGINE_CMDQ
:
469 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_2D
,
470 GLAMO_CLOCK_2D_EN_M6CLK
,
472 __reg_set_bit_mask(glamo
, GLAMO_REG_HOSTBUS(2),
473 GLAMO_HOSTBUS2_MMIO_EN_CQ
,
474 GLAMO_HOSTBUS2_MMIO_EN_CQ
);
475 /* __reg_set_bit_mask(glamo, GLAMO_REG_CLOCK_GEN5_1,
476 GLAMO_CLOCK_GEN51_EN_DIV_MCLK,
479 case GLAMO_ENGINE_2D
:
480 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_2D
,
481 GLAMO_CLOCK_2D_EN_M7CLK
|
482 GLAMO_CLOCK_2D_EN_GCLK
|
483 GLAMO_CLOCK_2D_DG_M7CLK
|
484 GLAMO_CLOCK_2D_DG_GCLK
,
486 __reg_set_bit_mask(glamo
, GLAMO_REG_HOSTBUS(2),
487 GLAMO_HOSTBUS2_MMIO_EN_2D
,
488 GLAMO_HOSTBUS2_MMIO_EN_2D
);
489 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_GEN5_1
,
490 GLAMO_CLOCK_GEN51_EN_DIV_GCLK
,
497 glamo
->engine_enabled_bitfield
&= ~(1 << engine
);
501 int glamo_engine_disable(struct glamo_core
*glamo
, enum glamo_engine engine
)
505 spin_lock(&glamo
->lock
);
507 ret
= __glamo_engine_disable(glamo
, engine
);
509 spin_unlock(&glamo
->lock
);
513 EXPORT_SYMBOL_GPL(glamo_engine_disable
);
515 static const u_int16_t engine_clock_regs
[__NUM_GLAMO_ENGINES
] = {
516 [GLAMO_ENGINE_LCD
] = GLAMO_REG_CLOCK_LCD
,
517 [GLAMO_ENGINE_MMC
] = GLAMO_REG_CLOCK_MMC
,
518 [GLAMO_ENGINE_ISP
] = GLAMO_REG_CLOCK_ISP
,
519 [GLAMO_ENGINE_JPEG
] = GLAMO_REG_CLOCK_JPEG
,
520 [GLAMO_ENGINE_3D
] = GLAMO_REG_CLOCK_3D
,
521 [GLAMO_ENGINE_2D
] = GLAMO_REG_CLOCK_2D
,
522 [GLAMO_ENGINE_MPEG_ENC
] = GLAMO_REG_CLOCK_MPEG
,
523 [GLAMO_ENGINE_MPEG_DEC
] = GLAMO_REG_CLOCK_MPEG
,
526 void glamo_engine_clkreg_set(struct glamo_core
*glamo
,
527 enum glamo_engine engine
,
528 u_int16_t mask
, u_int16_t val
)
530 reg_set_bit_mask(glamo
, engine_clock_regs
[engine
], mask
, val
);
532 EXPORT_SYMBOL_GPL(glamo_engine_clkreg_set
);
534 u_int16_t
glamo_engine_clkreg_get(struct glamo_core
*glamo
,
535 enum glamo_engine engine
)
539 spin_lock(&glamo
->lock
);
540 val
= __reg_read(glamo
, engine_clock_regs
[engine
]);
541 spin_unlock(&glamo
->lock
);
545 EXPORT_SYMBOL_GPL(glamo_engine_clkreg_get
);
547 static const struct glamo_script engine_div_regs
[__NUM_GLAMO_ENGINES
] = {
548 [GLAMO_ENGINE_LCD
] = {GLAMO_REG_CLOCK_GEN5_1
, GLAMO_CLOCK_GEN51_EN_DIV_DCLK
},
549 [GLAMO_ENGINE_MMC
] = {GLAMO_REG_CLOCK_GEN5_1
, GLAMO_CLOCK_GEN51_EN_DIV_TCLK
},
550 [GLAMO_ENGINE_2D
] = {GLAMO_REG_CLOCK_GEN5_1
, GLAMO_CLOCK_GEN51_EN_DIV_GCLK
},
553 void glamo_engine_div_enable(struct glamo_core
*glamo
, enum glamo_engine engine
)
555 uint16_t reg
= engine_div_regs
[engine
].reg
;
556 uint16_t bit
= engine_div_regs
[engine
].val
;
559 spin_lock(&glamo
->lock
);
560 val
= __reg_read(glamo
, reg
);
561 __reg_write(glamo
, reg
, val
| bit
);
562 spin_unlock(&glamo
->lock
);
565 EXPORT_SYMBOL_GPL(glamo_engine_div_enable
);
567 void glamo_engine_div_disable(struct glamo_core
*glamo
, enum glamo_engine engine
)
569 uint16_t reg
= engine_div_regs
[engine
].reg
;
570 uint16_t bit
= engine_div_regs
[engine
].val
;
573 spin_lock(&glamo
->lock
);
574 val
= __reg_read(glamo
, reg
);
575 __reg_write(glamo
, reg
, val
& ~bit
);
576 spin_unlock(&glamo
->lock
);
578 EXPORT_SYMBOL_GPL(glamo_engine_div_disable
);
580 static const struct glamo_script reset_regs
[] = {
581 [GLAMO_ENGINE_LCD
] = {
582 GLAMO_REG_CLOCK_LCD
, GLAMO_CLOCK_LCD_RESET
585 [GLAMO_ENGINE_HOST
] = {
586 GLAMO_REG_CLOCK_HOST
, GLAMO_CLOCK_HOST_RESET
588 [GLAMO_ENGINE_MEM
] = {
589 GLAMO_REG_CLOCK_MEM
, GLAMO_CLOCK_MEM_RESET
592 [GLAMO_ENGINE_MMC
] = {
593 GLAMO_REG_CLOCK_MMC
, GLAMO_CLOCK_MMC_RESET
595 [GLAMO_ENGINE_CMDQ
] = {
596 GLAMO_REG_CLOCK_2D
, GLAMO_CLOCK_2D_CQ_RESET
598 [GLAMO_ENGINE_2D
] = {
599 GLAMO_REG_CLOCK_2D
, GLAMO_CLOCK_2D_RESET
601 [GLAMO_ENGINE_JPEG
] = {
602 GLAMO_REG_CLOCK_JPEG
, GLAMO_CLOCK_JPEG_RESET
606 void glamo_engine_reset(struct glamo_core
*glamo
, enum glamo_engine engine
)
608 uint16_t reg
= reset_regs
[engine
].reg
;
609 uint16_t val
= reset_regs
[engine
].val
;
611 if (engine
>= ARRAY_SIZE(reset_regs
)) {
612 dev_warn(&glamo
->pdev
->dev
, "unknown engine %u ", engine
);
617 spin_lock(&glamo
->lock
);
618 __reg_set_bit(glamo
, reg
, val
);
619 __reg_clear_bit(glamo
, reg
, val
);
620 spin_unlock(&glamo
->lock
);
622 EXPORT_SYMBOL_GPL(glamo_engine_reset
);
624 int glamo_pll_rate(struct glamo_core
*glamo
,
628 unsigned int osci
= glamo
->pdata
->osci_clock_rate
;
632 reg
= __reg_read(glamo
, GLAMO_REG_PLL_GEN1
);
635 reg
= __reg_read(glamo
, GLAMO_REG_PLL_GEN3
);
642 EXPORT_SYMBOL_GPL(glamo_pll_rate
);
644 int glamo_engine_reclock(struct glamo_core
*glamo
,
645 enum glamo_engine engine
,
649 u_int16_t reg
, mask
, div
;
655 case GLAMO_ENGINE_LCD
:
657 reg
= GLAMO_REG_CLOCK_GEN7
;
660 case GLAMO_ENGINE_MMC
:
662 reg
= GLAMO_REG_CLOCK_GEN8
;
666 dev_warn(&glamo
->pdev
->dev
,
667 "reclock of engine 0x%x not supported\n", engine
);
672 pll
= glamo_pll_rate(glamo
, pll
);
676 if (div
!= 0 && pll
/ div
<= hz
)
682 dev_dbg(&glamo
->pdev
->dev
,
683 "PLL %d, kHZ %d, div %d\n", pll
, hz
/ 1000, div
);
685 reg_set_bit_mask(glamo
, reg
, mask
, div
);
686 mdelay(5); /* wait some time to stabilize */
688 return pll
/ (div
+ 1);
690 EXPORT_SYMBOL_GPL(glamo_engine_reclock
);
692 /***********************************************************************
694 ***********************************************************************/
696 int glamo_run_script(struct glamo_core
*glamo
, const struct glamo_script
*script
,
697 int len
, int may_sleep
)
700 const struct glamo_script
*line
= script
;
702 for (i
= 0; i
< len
; ++i
, ++line
) {
710 mdelay(line
->val
* 4);
713 /* spin until PLLs lock */
714 while ((__reg_read(glamo
, GLAMO_REG_PLL_GEN5
) & 3) != 3)
719 * couple of people reported artefacts with 2.6.28 changes, this
720 * allows reversion to 2.6.24 settings
724 switch (slow_memory
) {
725 /* choice 1 is the most conservative */
726 case 1: /* 3 waits on Async BB R & W, Use PLL 1 for mem bus */
727 __reg_write(glamo
, script
[i
].reg
, 0xef0);
729 case 2: /* 2 waits on Async BB R & W, Use PLL 1 for mem bus */
730 __reg_write(glamo
, script
[i
].reg
, 0xea0);
732 case 3: /* 1 waits on Async BB R & W, Use PLL 1 for mem bus */
733 __reg_write(glamo
, script
[i
].reg
, 0xe50);
735 case 4: /* 0 waits on Async BB R & W, Use PLL 1 for mem bus */
736 __reg_write(glamo
, script
[i
].reg
, 0xe00);
739 /* using PLL2 for memory bus increases CPU bandwidth significantly */
740 case 5: /* 3 waits on Async BB R & W, Use PLL 2 for mem bus */
741 __reg_write(glamo
, script
[i
].reg
, 0xef3);
743 case 6: /* 2 waits on Async BB R & W, Use PLL 2 for mem bus */
744 __reg_write(glamo
, script
[i
].reg
, 0xea3);
746 case 7: /* 1 waits on Async BB R & W, Use PLL 2 for mem bus */
747 __reg_write(glamo
, script
[i
].reg
, 0xe53);
749 /* default of 0 or >7 is fastest */
750 default: /* 0 waits on Async BB R & W, Use PLL 2 for mem bus */
751 __reg_write(glamo
, script
[i
].reg
, 0xe03);
757 __reg_write(glamo
, script
[i
].reg
, script
[i
].val
);
764 EXPORT_SYMBOL(glamo_run_script
);
766 static const struct glamo_script glamo_init_script
[] = {
767 { GLAMO_REG_CLOCK_HOST
, 0x1000 },
769 { GLAMO_REG_CLOCK_MEMORY
, 0x1000 },
770 { GLAMO_REG_CLOCK_MEMORY
, 0x2000 },
771 { GLAMO_REG_CLOCK_LCD
, 0x1000 },
772 { GLAMO_REG_CLOCK_MMC
, 0x1000 },
773 { GLAMO_REG_CLOCK_ISP
, 0x1000 },
774 { GLAMO_REG_CLOCK_ISP
, 0x3000 },
775 { GLAMO_REG_CLOCK_JPEG
, 0x1000 },
776 { GLAMO_REG_CLOCK_3D
, 0x1000 },
777 { GLAMO_REG_CLOCK_3D
, 0x3000 },
778 { GLAMO_REG_CLOCK_2D
, 0x1000 },
779 { GLAMO_REG_CLOCK_2D
, 0x3000 },
780 { GLAMO_REG_CLOCK_RISC1
, 0x1000 },
781 { GLAMO_REG_CLOCK_MPEG
, 0x3000 },
782 { GLAMO_REG_CLOCK_MPEG
, 0x3000 },
783 { GLAMO_REG_CLOCK_MPROC
, 0x1000 /*0x100f*/ },
785 { GLAMO_REG_CLOCK_HOST
, 0x0000 },
786 { GLAMO_REG_CLOCK_MEMORY
, 0x0000 },
787 { GLAMO_REG_CLOCK_LCD
, 0x0000 },
788 { GLAMO_REG_CLOCK_MMC
, 0x0000 },
790 /* unused engines must be left in reset to stop MMC block read "blackouts" */
791 { GLAMO_REG_CLOCK_ISP
, 0x0000 },
792 { GLAMO_REG_CLOCK_ISP
, 0x0000 },
793 { GLAMO_REG_CLOCK_JPEG
, 0x0000 },
794 { GLAMO_REG_CLOCK_3D
, 0x0000 },
795 { GLAMO_REG_CLOCK_3D
, 0x0000 },
796 { GLAMO_REG_CLOCK_2D
, 0x0000 },
797 { GLAMO_REG_CLOCK_2D
, 0x0000 },
798 { GLAMO_REG_CLOCK_RISC1
, 0x0000 },
799 { GLAMO_REG_CLOCK_MPEG
, 0x0000 },
800 { GLAMO_REG_CLOCK_MPEG
, 0x0000 },
802 { GLAMO_REG_PLL_GEN1
, 0x05db }, /* 48MHz */
803 { GLAMO_REG_PLL_GEN3
, 0x0aba }, /* 90MHz */
806 * b9 of this register MUST be zero to get any interrupts on INT#
807 * the other set bits enable all the engine interrupt sources
809 { GLAMO_REG_IRQ_ENABLE
, 0x01ff },
810 { GLAMO_REG_CLOCK_GEN6
, 0x2000 },
811 { GLAMO_REG_CLOCK_GEN7
, 0x0101 },
812 { GLAMO_REG_CLOCK_GEN8
, 0x0100 },
813 { GLAMO_REG_CLOCK_HOST
, 0x000d },
815 * b7..b4 = 0 = no wait states on read or write
816 * b0 = 1 select PLL2 for Host interface, b1 = enable it
818 { 0x200, 0x0e03 /* this is replaced by script parser */ },
824 /* S-Media recommended "set tiling mode to 512 mode for memory access
825 * more efficiency when 640x480" */
826 { GLAMO_REG_MEM_TYPE
, 0x0c74 }, /* 8MB, 16 word pg wr+rd */
827 { GLAMO_REG_MEM_GEN
, 0xafaf }, /* 63 grants min + max */
829 { GLAMO_REGOFS_HOSTBUS
+ 2, 0xffff }, /* enable on MMIO*/
831 { GLAMO_REG_MEM_TIMING1
, 0x0108 },
832 { GLAMO_REG_MEM_TIMING2
, 0x0010 }, /* Taa = 3 MCLK */
833 { GLAMO_REG_MEM_TIMING3
, 0x0000 },
834 { GLAMO_REG_MEM_TIMING4
, 0x0000 }, /* CE1# delay fall/rise */
835 { GLAMO_REG_MEM_TIMING5
, 0x0000 }, /* UB# LB# */
836 { GLAMO_REG_MEM_TIMING6
, 0x0000 }, /* OE# */
837 { GLAMO_REG_MEM_TIMING7
, 0x0000 }, /* WE# */
838 { GLAMO_REG_MEM_TIMING8
, 0x1002 }, /* MCLK delay, was 0x1000 */
839 { GLAMO_REG_MEM_TIMING9
, 0x6006 },
840 { GLAMO_REG_MEM_TIMING10
, 0x00ff },
841 { GLAMO_REG_MEM_TIMING11
, 0x0001 },
842 { GLAMO_REG_MEM_POWER1
, 0x0020 },
843 { GLAMO_REG_MEM_POWER2
, 0x0000 },
844 { GLAMO_REG_MEM_DRAM1
, 0x0000 },
846 { GLAMO_REG_MEM_DRAM1
, 0xc100 },
848 { GLAMO_REG_MEM_DRAM1
, 0xe100 },
849 { GLAMO_REG_MEM_DRAM2
, 0x01d6 },
850 { GLAMO_REG_CLOCK_MEMORY
, 0x000b },
853 static struct glamo_script glamo_resume_script
[] = {
855 { GLAMO_REG_PLL_GEN1
, 0x05db }, /* 48MHz */
856 { GLAMO_REG_PLL_GEN3
, 0x0aba }, /* 90MHz */
857 { GLAMO_REG_DFT_GEN6
, 1 },
863 * b9 of this register MUST be zero to get any interrupts on INT#
864 * the other set bits enable all the engine interrupt sources
866 { GLAMO_REG_IRQ_ENABLE
, 0x01ff },
867 { GLAMO_REG_CLOCK_HOST
, 0x0018 },
868 { GLAMO_REG_CLOCK_GEN5_1
, 0x18b1 },
870 { GLAMO_REG_MEM_DRAM1
, 0x0000 },
872 { GLAMO_REG_MEM_DRAM1
, 0xc100 },
874 { GLAMO_REG_MEM_DRAM1
, 0xe100 },
875 { GLAMO_REG_MEM_DRAM2
, 0x01d6 },
876 { GLAMO_REG_CLOCK_MEMORY
, 0x000b },
885 static void glamo_power(struct glamo_core
*glamo
,
886 enum glamo_power new_state
)
891 spin_lock_irqsave(&glamo
->lock
, flags
);
893 dev_info(&glamo
->pdev
->dev
, "***** glamo_power -> %d\n", new_state
);
897 static const REG_VALUE_MASK_TYPE reg_powerOn[] =
899 { REG_GEN_DFT6, REG_BIT_ALL, REG_DATA(1u << 0) },
900 { REG_GEN_PLL3, 0u, REG_DATA(1u << 13) },
901 { REG_GEN_MEM_CLK, REG_BIT_ALL, REG_BIT_EN_MOCACLK },
902 { REG_MEM_DRAM2, 0u, REG_BIT_EN_DEEP_POWER_DOWN },
903 { REG_MEM_DRAM1, 0u, REG_BIT_SELF_REFRESH }
906 static const REG_VALUE_MASK_TYPE reg_powerStandby[] =
908 { REG_MEM_DRAM1, REG_BIT_ALL, REG_BIT_SELF_REFRESH },
909 { REG_GEN_MEM_CLK, 0u, REG_BIT_EN_MOCACLK },
910 { REG_GEN_PLL3, REG_BIT_ALL, REG_DATA(1u << 13) },
911 { REG_GEN_DFT5, REG_BIT_ALL, REG_DATA(1u << 0) }
914 static const REG_VALUE_MASK_TYPE reg_powerSuspend[] =
916 { REG_MEM_DRAM2, REG_BIT_ALL, REG_BIT_EN_DEEP_POWER_DOWN },
917 { REG_GEN_MEM_CLK, 0u, REG_BIT_EN_MOCACLK },
918 { REG_GEN_PLL3, REG_BIT_ALL, REG_DATA(1u << 13) },
919 { REG_GEN_DFT5, REG_BIT_ALL, REG_DATA(1u << 0) }
927 * glamo state on resume is nondeterministic in some
928 * fundamental way, it has also been observed that the
929 * Glamo reset pin can get asserted by, eg, touching it with
930 * a scope probe. So the only answer is to roll with it and
931 * force an external reset on the Glamo during resume.
934 (glamo
->pdata
->glamo_external_reset
)(0);
936 (glamo
->pdata
->glamo_external_reset
)(1);
939 glamo_run_script(glamo
, glamo_init_script
,
940 ARRAY_SIZE(glamo_init_script
), 0);
944 case GLAMO_POWER_SUSPEND
:
946 /* nuke interrupts */
947 __reg_write(glamo
, GLAMO_REG_IRQ_ENABLE
, 0x200);
949 /* stash a copy of which engines were running */
950 glamo
->engine_enabled_bitfield_suspend
=
951 glamo
->engine_enabled_bitfield
;
953 /* take down each engine before we kill mem and pll */
954 for (n
= 0; n
< __NUM_GLAMO_ENGINES
; n
++)
955 if (glamo
->engine_enabled_bitfield
& (1 << n
))
956 __glamo_engine_disable(glamo
, n
);
958 /* enable self-refresh */
960 __reg_write(glamo
, GLAMO_REG_MEM_DRAM1
,
961 GLAMO_MEM_DRAM1_EN_DRAM_REFRESH
|
962 GLAMO_MEM_DRAM1_EN_GATE_CKE
|
963 GLAMO_MEM_DRAM1_SELF_REFRESH
|
964 GLAMO_MEM_REFRESH_COUNT
);
965 __reg_write(glamo
, GLAMO_REG_MEM_DRAM1
,
966 GLAMO_MEM_DRAM1_EN_MODEREG_SET
|
967 GLAMO_MEM_DRAM1_EN_DRAM_REFRESH
|
968 GLAMO_MEM_DRAM1_EN_GATE_CKE
|
969 GLAMO_MEM_DRAM1_SELF_REFRESH
|
970 GLAMO_MEM_REFRESH_COUNT
);
972 /* force RAM into deep powerdown */
974 __reg_write(glamo
, GLAMO_REG_MEM_DRAM2
,
975 GLAMO_MEM_DRAM2_DEEP_PWRDOWN
|
978 (1 << 2) | /* tRCD */
979 2); /* CAS latency */
981 /* disable clocks to memory */
982 __reg_write(glamo
, GLAMO_REG_CLOCK_MEMORY
, 0);
984 /* all dividers from OSCI */
985 __reg_set_bit_mask(glamo
, GLAMO_REG_CLOCK_GEN5_1
, 0x400, 0x400);
987 /* PLL2 into bypass */
988 __reg_set_bit_mask(glamo
, GLAMO_REG_PLL_GEN3
, 1 << 12, 1 << 12);
990 __reg_write(glamo
, 0x200, 0x0e00);
993 /* kill PLLS 1 then 2 */
994 __reg_write(glamo
, GLAMO_REG_DFT_GEN5
, 0x0001);
995 __reg_set_bit_mask(glamo
, GLAMO_REG_PLL_GEN3
, 1 << 13, 1 << 13);
1000 spin_unlock_irqrestore(&glamo
->lock
, flags
);
1004 #define MEMDETECT_RETRY 6
1005 static unsigned int detect_memsize(struct glamo_core
*glamo
)
1009 /*static const u_int16_t pattern[] = {
1010 0x1111, 0x8a8a, 0x2222, 0x7a7a,
1011 0x3333, 0x6a6a, 0x4444, 0x5a5a,
1012 0x5555, 0x4a4a, 0x6666, 0x3a3a,
1013 0x7777, 0x2a2a, 0x8888, 0x1a1a
1016 for (i
= 0; i
< MEMDETECT_RETRY
; i
++) {
1017 switch (glamo
->type
) {
1019 __reg_write(glamo
, GLAMO_REG_MEM_TYPE
, 0x0072);
1020 __reg_write(glamo
, GLAMO_REG_MEM_DRAM1
, 0xc100);
1023 switch (glamo
->revision
) {
1024 case GLAMO_CORE_REV_A0
:
1026 __reg_write(glamo
, GLAMO_REG_MEM_TYPE
,
1029 __reg_write(glamo
, GLAMO_REG_MEM_TYPE
,
1032 __reg_write(glamo
, GLAMO_REG_MEM_DRAM1
, 0x0000);
1034 __reg_write(glamo
, GLAMO_REG_MEM_DRAM1
, 0xc100);
1038 __reg_write(glamo
, GLAMO_REG_MEM_TYPE
,
1041 __reg_write(glamo
, GLAMO_REG_MEM_TYPE
,
1044 __reg_write(glamo
, GLAMO_REG_MEM_DRAM1
, 0x0000);
1046 __reg_write(glamo
, GLAMO_REG_MEM_DRAM1
, 0xe100);
1057 /* FIXME: finish implementation */
1058 for (j
= 0; j
< 8; j
++) {
1067 /* Find out if we can support this version of the Glamo chip */
1068 static int glamo_supported(struct glamo_core
*glamo
)
1070 u_int16_t dev_id
, rev_id
; /*, memsize; */
1072 dev_id
= __reg_read(glamo
, GLAMO_REG_DEVICE_ID
);
1073 rev_id
= __reg_read(glamo
, GLAMO_REG_REVISION_ID
);
1078 case GLAMO_CORE_REV_A2
:
1080 case GLAMO_CORE_REV_A0
:
1081 case GLAMO_CORE_REV_A1
:
1082 case GLAMO_CORE_REV_A3
:
1083 dev_warn(&glamo
->pdev
->dev
, "untested core revision "
1084 "%04x, your mileage may vary\n", rev_id
);
1087 dev_warn(&glamo
->pdev
->dev
, "unknown glamo revision "
1088 "%04x, your mileage may vary\n", rev_id
);
1089 /* maybe should abort ? */
1095 dev_err(&glamo
->pdev
->dev
, "unsupported Glamo device %04x\n",
1100 dev_dbg(&glamo
->pdev
->dev
, "Detected Glamo core %04x Revision %04x "
1101 "(%uHz CPU / %uHz Memory)\n", dev_id
, rev_id
,
1102 glamo_pll_rate(glamo
, GLAMO_PLL1
),
1103 glamo_pll_rate(glamo
, GLAMO_PLL2
));
1108 static int __init
glamo_probe(struct platform_device
*pdev
)
1111 struct glamo_core
*glamo
;
1113 glamo
= kmalloc(GFP_KERNEL
, sizeof(*glamo
));
1117 spin_lock_init(&glamo
->lock
);
1119 glamo
->mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1120 glamo
->irq
= platform_get_irq(pdev
, 0);
1121 glamo
->pdata
= pdev
->dev
.platform_data
;
1122 if (!glamo
->mem
|| !glamo
->pdata
) {
1123 dev_err(&pdev
->dev
, "platform device with no MEM/PDATA ?\n");
1128 /* register a number of sibling devices whoise IOMEM resources
1129 * are siblings of pdev's IOMEM resource */
1131 /* only remap the generic, hostbus and memory controller registers */
1132 glamo
->base
= ioremap(glamo
->mem
->start
, 0x4000 /*GLAMO_REGOFS_VIDCAP*/);
1134 dev_err(&pdev
->dev
, "failed to ioremap() memory region\n");
1138 platform_set_drvdata(pdev
, glamo
);
1140 (glamo
->pdata
->glamo_external_reset
)(0);
1142 (glamo
->pdata
->glamo_external_reset
)(1);
1146 * finally set the mfd interrupts up
1147 * can't do them earlier or sibling probes blow up
1150 for (irq
= IRQ_GLAMO(0); irq
<= IRQ_GLAMO(8); irq
++) {
1151 set_irq_chip_and_handler(irq
, &glamo_irq_chip
, handle_level_irq
);
1152 set_irq_flags(irq
, IRQF_VALID
);
1153 set_irq_chip_data(irq
, glamo
);
1156 if (glamo
->pdata
->glamo_irq_is_wired
&&
1157 !glamo
->pdata
->glamo_irq_is_wired()) {
1158 set_irq_chained_handler(glamo
->irq
, glamo_irq_demux_handler
);
1159 set_irq_type(glamo
->irq
, IRQ_TYPE_EDGE_FALLING
);
1160 set_irq_chip_data(glamo
->irq
, glamo
);
1161 dev_info(&pdev
->dev
, "Glamo interrupt registered\n");
1162 glamo
->irq_works
= 1;
1164 dev_err(&pdev
->dev
, "Glamo interrupt not used\n");
1165 glamo
->irq_works
= 0;
1168 /* confirm it isn't insane version */
1169 if (!glamo_supported(glamo
)) {
1170 dev_err(&pdev
->dev
, "This Glamo is not supported\n");
1175 rc
= sysfs_create_group(&pdev
->dev
.kobj
, &glamo_attr_group
);
1177 dev_err(&pdev
->dev
, "cannot create sysfs group\n");
1181 /* init the chip with canned register set */
1183 dev_dbg(&glamo
->pdev
->dev
, "running init script\n");
1184 glamo_run_script(glamo
, glamo_init_script
,
1185 ARRAY_SIZE(glamo_init_script
), 1);
1187 dev_info(&glamo
->pdev
->dev
, "Glamo core PLL1: %uHz, PLL2: %uHz\n",
1188 glamo_pll_rate(glamo
, GLAMO_PLL1
),
1189 glamo_pll_rate(glamo
, GLAMO_PLL2
));
1191 /* register siblings */
1192 glamo
->pdata
->mmc_data
->core
= glamo
;
1193 glamo_cells
[GLAMO_CELL_MMC
].platform_data
= glamo
->pdata
->mmc_data
;
1194 glamo_cells
[GLAMO_CELL_MMC
].data_size
=
1195 sizeof(struct glamo_mmc_platform_data
);
1197 glamo
->pdata
->fb_data
->core
= glamo
;
1198 glamo_cells
[GLAMO_CELL_FB
].platform_data
= glamo
->pdata
->fb_data
;
1199 glamo_cells
[GLAMO_CELL_FB
].data_size
= sizeof(struct glamo_fb_platform_data
);
1201 mfd_add_devices(&pdev
->dev
, pdev
->id
, glamo_cells
,
1202 ARRAY_SIZE(glamo_cells
),
1205 /* only request the generic, hostbus and memory controller MMIO */
1206 glamo
->mem
= request_mem_region(glamo
->mem
->start
,
1207 GLAMO_REGOFS_VIDCAP
, "glamo-core");
1209 dev_err(&pdev
->dev
, "failed to request memory region\n");
1216 disable_irq(glamo
->irq
);
1217 set_irq_chained_handler(glamo
->irq
, NULL
);
1218 set_irq_chip_data(glamo
->irq
, NULL
);
1220 for (irq
= IRQ_GLAMO(0); irq
<= IRQ_GLAMO(8); irq
++) {
1221 set_irq_flags(irq
, 0);
1222 set_irq_chip(irq
, NULL
);
1223 set_irq_chip_data(irq
, NULL
);
1226 iounmap(glamo
->base
);
1228 platform_set_drvdata(pdev
, NULL
);
1234 static int glamo_remove(struct platform_device
*pdev
)
1236 struct glamo_core
*glamo
= platform_get_drvdata(pdev
);
1239 disable_irq(glamo
->irq
);
1240 set_irq_chained_handler(glamo
->irq
, NULL
);
1241 set_irq_chip_data(glamo
->irq
, NULL
);
1243 for (irq
= IRQ_GLAMO(0); irq
<= IRQ_GLAMO(8); irq
++) {
1244 set_irq_flags(irq
, 0);
1245 set_irq_chip(irq
, NULL
);
1246 set_irq_chip_data(irq
, NULL
);
1249 platform_set_drvdata(pdev
, NULL
);
1250 mfd_remove_devices(&pdev
->dev
);
1251 iounmap(glamo
->base
);
1252 release_mem_region(glamo
->mem
->start
, GLAMO_REGOFS_VIDCAP
);
1260 static int glamo_suspend(struct device
*dev
)
1262 struct glamo_core
*glamo
= dev_get_drvdata(dev
);
1263 glamo
->suspending
= 1;
1264 glamo_power(glamo
, GLAMO_POWER_SUSPEND
);
1269 static int glamo_resume(struct device
*dev
)
1271 struct glamo_core
*glamo
= dev_get_drvdata(dev
);
1272 glamo_power(glamo
, GLAMO_POWER_ON
);
1273 glamo
->suspending
= 0;
1277 static struct dev_pm_ops glamo_pm_ops
= {
1278 .suspend
= glamo_suspend
,
1279 .resume
= glamo_resume
,
1282 #define GLAMO_PM_OPS (&glamo_pm_ops)
1285 #define GLAMO_PM_OPS NULL
1288 static struct platform_driver glamo_driver
= {
1289 .probe
= glamo_probe
,
1290 .remove
= glamo_remove
,
1292 .name
= "glamo3362",
1293 .owner
= THIS_MODULE
,
1298 static int __devinit
glamo_init(void)
1300 return platform_driver_register(&glamo_driver
);
1303 static void __exit
glamo_cleanup(void)
1305 platform_driver_unregister(&glamo_driver
);
1308 module_init(glamo_init
);
1309 module_exit(glamo_cleanup
);
1311 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
1312 MODULE_DESCRIPTION("Smedia Glamo 336x/337x core/resource driver");
1313 MODULE_LICENSE("GPL");