i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / gpu / drm / gma500 / psb_irq.c
blob7be802baceb5d086ff491d954735d38a9e2ed076
1 /**************************************************************************
2 * Copyright (c) 2007, Intel Corporation.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 * Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
19 * develop this driver.
21 **************************************************************************/
25 #include <drm/drmP.h>
26 #include "psb_drv.h"
27 #include "psb_reg.h"
28 #include "psb_intel_reg.h"
29 #include "power.h"
32 * inline functions
35 static inline u32
36 psb_pipestat(int pipe)
38 if (pipe == 0)
39 return PIPEASTAT;
40 if (pipe == 1)
41 return PIPEBSTAT;
42 if (pipe == 2)
43 return PIPECSTAT;
44 BUG();
47 static inline u32
48 mid_pipe_event(int pipe)
50 if (pipe == 0)
51 return _PSB_PIPEA_EVENT_FLAG;
52 if (pipe == 1)
53 return _MDFLD_PIPEB_EVENT_FLAG;
54 if (pipe == 2)
55 return _MDFLD_PIPEC_EVENT_FLAG;
56 BUG();
59 static inline u32
60 mid_pipe_vsync(int pipe)
62 if (pipe == 0)
63 return _PSB_VSYNC_PIPEA_FLAG;
64 if (pipe == 1)
65 return _PSB_VSYNC_PIPEB_FLAG;
66 if (pipe == 2)
67 return _MDFLD_PIPEC_VBLANK_FLAG;
68 BUG();
71 static inline u32
72 mid_pipeconf(int pipe)
74 if (pipe == 0)
75 return PIPEACONF;
76 if (pipe == 1)
77 return PIPEBCONF;
78 if (pipe == 2)
79 return PIPECCONF;
80 BUG();
83 void
84 psb_enable_pipestat(struct drm_psb_private *dev_priv, int pipe, u32 mask)
86 if ((dev_priv->pipestat[pipe] & mask) != mask) {
87 u32 reg = psb_pipestat(pipe);
88 dev_priv->pipestat[pipe] |= mask;
89 /* Enable the interrupt, clear any pending status */
90 if (gma_power_begin(dev_priv->dev, false)) {
91 u32 writeVal = PSB_RVDC32(reg);
92 writeVal |= (mask | (mask >> 16));
93 PSB_WVDC32(writeVal, reg);
94 (void) PSB_RVDC32(reg);
95 gma_power_end(dev_priv->dev);
100 void
101 psb_disable_pipestat(struct drm_psb_private *dev_priv, int pipe, u32 mask)
103 if ((dev_priv->pipestat[pipe] & mask) != 0) {
104 u32 reg = psb_pipestat(pipe);
105 dev_priv->pipestat[pipe] &= ~mask;
106 if (gma_power_begin(dev_priv->dev, false)) {
107 u32 writeVal = PSB_RVDC32(reg);
108 writeVal &= ~mask;
109 PSB_WVDC32(writeVal, reg);
110 (void) PSB_RVDC32(reg);
111 gma_power_end(dev_priv->dev);
116 void mid_enable_pipe_event(struct drm_psb_private *dev_priv, int pipe)
118 if (gma_power_begin(dev_priv->dev, false)) {
119 u32 pipe_event = mid_pipe_event(pipe);
120 dev_priv->vdc_irq_mask |= pipe_event;
121 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
122 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
123 gma_power_end(dev_priv->dev);
127 void mid_disable_pipe_event(struct drm_psb_private *dev_priv, int pipe)
129 if (dev_priv->pipestat[pipe] == 0) {
130 if (gma_power_begin(dev_priv->dev, false)) {
131 u32 pipe_event = mid_pipe_event(pipe);
132 dev_priv->vdc_irq_mask &= ~pipe_event;
133 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
134 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
135 gma_power_end(dev_priv->dev);
141 * Display controller interrupt handler for pipe event.
144 static void mid_pipe_event_handler(struct drm_device *dev, int pipe)
146 struct drm_psb_private *dev_priv =
147 (struct drm_psb_private *) dev->dev_private;
149 uint32_t pipe_stat_val = 0;
150 uint32_t pipe_stat_reg = psb_pipestat(pipe);
151 uint32_t pipe_enable = dev_priv->pipestat[pipe];
152 uint32_t pipe_status = dev_priv->pipestat[pipe] >> 16;
153 uint32_t pipe_clear;
154 uint32_t i = 0;
156 spin_lock(&dev_priv->irqmask_lock);
158 pipe_stat_val = PSB_RVDC32(pipe_stat_reg);
159 pipe_stat_val &= pipe_enable | pipe_status;
160 pipe_stat_val &= pipe_stat_val >> 16;
162 spin_unlock(&dev_priv->irqmask_lock);
164 /* Clear the 2nd level interrupt status bits
165 * Sometimes the bits are very sticky so we repeat until they unstick */
166 for (i = 0; i < 0xffff; i++) {
167 PSB_WVDC32(PSB_RVDC32(pipe_stat_reg), pipe_stat_reg);
168 pipe_clear = PSB_RVDC32(pipe_stat_reg) & pipe_status;
170 if (pipe_clear == 0)
171 break;
174 if (pipe_clear)
175 dev_err(dev->dev,
176 "%s, can't clear status bits for pipe %d, its value = 0x%x.\n",
177 __func__, pipe, PSB_RVDC32(pipe_stat_reg));
179 if (pipe_stat_val & PIPE_VBLANK_STATUS)
180 drm_handle_vblank(dev, pipe);
182 if (pipe_stat_val & PIPE_TE_STATUS)
183 drm_handle_vblank(dev, pipe);
187 * Display controller interrupt handler.
189 static void psb_vdc_interrupt(struct drm_device *dev, uint32_t vdc_stat)
191 if (vdc_stat & _PSB_VSYNC_PIPEA_FLAG)
192 mid_pipe_event_handler(dev, 0);
194 if (vdc_stat & _PSB_VSYNC_PIPEB_FLAG)
195 mid_pipe_event_handler(dev, 1);
198 irqreturn_t psb_irq_handler(DRM_IRQ_ARGS)
200 struct drm_device *dev = (struct drm_device *) arg;
201 struct drm_psb_private *dev_priv =
202 (struct drm_psb_private *) dev->dev_private;
204 uint32_t vdc_stat, dsp_int = 0, sgx_int = 0;
205 int handled = 0;
207 spin_lock(&dev_priv->irqmask_lock);
209 vdc_stat = PSB_RVDC32(PSB_INT_IDENTITY_R);
211 if (vdc_stat & _PSB_PIPE_EVENT_FLAG)
212 dsp_int = 1;
214 /* FIXME: Handle Medfield
215 if (vdc_stat & _MDFLD_DISP_ALL_IRQ_FLAG)
216 dsp_int = 1;
219 if (vdc_stat & _PSB_IRQ_SGX_FLAG)
220 sgx_int = 1;
222 vdc_stat &= dev_priv->vdc_irq_mask;
223 spin_unlock(&dev_priv->irqmask_lock);
225 if (dsp_int && gma_power_is_on(dev)) {
226 psb_vdc_interrupt(dev, vdc_stat);
227 handled = 1;
230 if (sgx_int) {
231 /* Not expected - we have it masked, shut it up */
232 u32 s, s2;
233 s = PSB_RSGX32(PSB_CR_EVENT_STATUS);
234 s2 = PSB_RSGX32(PSB_CR_EVENT_STATUS2);
235 PSB_WSGX32(s, PSB_CR_EVENT_HOST_CLEAR);
236 PSB_WSGX32(s2, PSB_CR_EVENT_HOST_CLEAR2);
237 /* if s & _PSB_CE_TWOD_COMPLETE we have 2D done but
238 we may as well poll even if we add that ! */
239 handled = 1;
242 PSB_WVDC32(vdc_stat, PSB_INT_IDENTITY_R);
243 (void) PSB_RVDC32(PSB_INT_IDENTITY_R);
244 DRM_READMEMORYBARRIER();
246 if (!handled)
247 return IRQ_NONE;
249 return IRQ_HANDLED;
252 void psb_irq_preinstall(struct drm_device *dev)
254 struct drm_psb_private *dev_priv =
255 (struct drm_psb_private *) dev->dev_private;
256 unsigned long irqflags;
258 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
260 if (gma_power_is_on(dev))
261 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
262 if (dev->vblank_enabled[0])
263 dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEA_FLAG;
264 if (dev->vblank_enabled[1])
265 dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEB_FLAG;
267 /* FIXME: Handle Medfield irq mask
268 if (dev->vblank_enabled[1])
269 dev_priv->vdc_irq_mask |= _MDFLD_PIPEB_EVENT_FLAG;
270 if (dev->vblank_enabled[2])
271 dev_priv->vdc_irq_mask |= _MDFLD_PIPEC_EVENT_FLAG;
274 /* This register is safe even if display island is off */
275 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
276 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
279 int psb_irq_postinstall(struct drm_device *dev)
281 struct drm_psb_private *dev_priv =
282 (struct drm_psb_private *) dev->dev_private;
283 unsigned long irqflags;
285 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
287 /* This register is safe even if display island is off */
288 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
289 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
291 if (dev->vblank_enabled[0])
292 psb_enable_pipestat(dev_priv, 0, PIPE_VBLANK_INTERRUPT_ENABLE);
293 else
294 psb_disable_pipestat(dev_priv, 0, PIPE_VBLANK_INTERRUPT_ENABLE);
296 if (dev->vblank_enabled[1])
297 psb_enable_pipestat(dev_priv, 1, PIPE_VBLANK_INTERRUPT_ENABLE);
298 else
299 psb_disable_pipestat(dev_priv, 1, PIPE_VBLANK_INTERRUPT_ENABLE);
301 if (dev->vblank_enabled[2])
302 psb_enable_pipestat(dev_priv, 2, PIPE_VBLANK_INTERRUPT_ENABLE);
303 else
304 psb_disable_pipestat(dev_priv, 2, PIPE_VBLANK_INTERRUPT_ENABLE);
306 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
307 return 0;
310 void psb_irq_uninstall(struct drm_device *dev)
312 struct drm_psb_private *dev_priv =
313 (struct drm_psb_private *) dev->dev_private;
314 unsigned long irqflags;
316 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
318 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
320 if (dev->vblank_enabled[0])
321 psb_disable_pipestat(dev_priv, 0, PIPE_VBLANK_INTERRUPT_ENABLE);
323 if (dev->vblank_enabled[1])
324 psb_disable_pipestat(dev_priv, 1, PIPE_VBLANK_INTERRUPT_ENABLE);
326 if (dev->vblank_enabled[2])
327 psb_disable_pipestat(dev_priv, 2, PIPE_VBLANK_INTERRUPT_ENABLE);
329 dev_priv->vdc_irq_mask &= _PSB_IRQ_SGX_FLAG |
330 _PSB_IRQ_MSVDX_FLAG |
331 _LNC_IRQ_TOPAZ_FLAG;
333 /* These two registers are safe even if display island is off */
334 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
335 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
337 wmb();
339 /* This register is safe even if display island is off */
340 PSB_WVDC32(PSB_RVDC32(PSB_INT_IDENTITY_R), PSB_INT_IDENTITY_R);
341 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
344 void psb_irq_turn_on_dpst(struct drm_device *dev)
346 struct drm_psb_private *dev_priv =
347 (struct drm_psb_private *) dev->dev_private;
348 u32 hist_reg;
349 u32 pwm_reg;
351 if (gma_power_begin(dev, false)) {
352 PSB_WVDC32(1 << 31, HISTOGRAM_LOGIC_CONTROL);
353 hist_reg = PSB_RVDC32(HISTOGRAM_LOGIC_CONTROL);
354 PSB_WVDC32(1 << 31, HISTOGRAM_INT_CONTROL);
355 hist_reg = PSB_RVDC32(HISTOGRAM_INT_CONTROL);
357 PSB_WVDC32(0x80010100, PWM_CONTROL_LOGIC);
358 pwm_reg = PSB_RVDC32(PWM_CONTROL_LOGIC);
359 PSB_WVDC32(pwm_reg | PWM_PHASEIN_ENABLE
360 | PWM_PHASEIN_INT_ENABLE,
361 PWM_CONTROL_LOGIC);
362 pwm_reg = PSB_RVDC32(PWM_CONTROL_LOGIC);
364 psb_enable_pipestat(dev_priv, 0, PIPE_DPST_EVENT_ENABLE);
366 hist_reg = PSB_RVDC32(HISTOGRAM_INT_CONTROL);
367 PSB_WVDC32(hist_reg | HISTOGRAM_INT_CTRL_CLEAR,
368 HISTOGRAM_INT_CONTROL);
369 pwm_reg = PSB_RVDC32(PWM_CONTROL_LOGIC);
370 PSB_WVDC32(pwm_reg | 0x80010100 | PWM_PHASEIN_ENABLE,
371 PWM_CONTROL_LOGIC);
373 gma_power_end(dev);
377 int psb_irq_enable_dpst(struct drm_device *dev)
379 struct drm_psb_private *dev_priv =
380 (struct drm_psb_private *) dev->dev_private;
381 unsigned long irqflags;
383 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
385 /* enable DPST */
386 mid_enable_pipe_event(dev_priv, 0);
387 psb_irq_turn_on_dpst(dev);
389 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
390 return 0;
393 void psb_irq_turn_off_dpst(struct drm_device *dev)
395 struct drm_psb_private *dev_priv =
396 (struct drm_psb_private *) dev->dev_private;
397 u32 hist_reg;
398 u32 pwm_reg;
400 if (gma_power_begin(dev, false)) {
401 PSB_WVDC32(0x00000000, HISTOGRAM_INT_CONTROL);
402 hist_reg = PSB_RVDC32(HISTOGRAM_INT_CONTROL);
404 psb_disable_pipestat(dev_priv, 0, PIPE_DPST_EVENT_ENABLE);
406 pwm_reg = PSB_RVDC32(PWM_CONTROL_LOGIC);
407 PSB_WVDC32(pwm_reg & !(PWM_PHASEIN_INT_ENABLE),
408 PWM_CONTROL_LOGIC);
409 pwm_reg = PSB_RVDC32(PWM_CONTROL_LOGIC);
411 gma_power_end(dev);
415 int psb_irq_disable_dpst(struct drm_device *dev)
417 struct drm_psb_private *dev_priv =
418 (struct drm_psb_private *) dev->dev_private;
419 unsigned long irqflags;
421 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
423 mid_disable_pipe_event(dev_priv, 0);
424 psb_irq_turn_off_dpst(dev);
426 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
428 return 0;
431 #ifdef PSB_FIXME
432 static int psb_vblank_do_wait(struct drm_device *dev,
433 unsigned int *sequence, atomic_t *counter)
435 unsigned int cur_vblank;
436 int ret = 0;
437 DRM_WAIT_ON(ret, dev->vbl_queue, 3 * DRM_HZ,
438 (((cur_vblank = atomic_read(counter))
439 - *sequence) <= (1 << 23)));
440 *sequence = cur_vblank;
442 return ret;
444 #endif
447 * It is used to enable VBLANK interrupt
449 int psb_enable_vblank(struct drm_device *dev, int pipe)
451 struct drm_psb_private *dev_priv = dev->dev_private;
452 unsigned long irqflags;
453 uint32_t reg_val = 0;
454 uint32_t pipeconf_reg = mid_pipeconf(pipe);
456 if (gma_power_begin(dev, false)) {
457 reg_val = REG_READ(pipeconf_reg);
458 gma_power_end(dev);
461 if (!(reg_val & PIPEACONF_ENABLE))
462 return -EINVAL;
464 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
466 if (pipe == 0)
467 dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEA_FLAG;
468 else if (pipe == 1)
469 dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEB_FLAG;
471 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
472 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
473 psb_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_ENABLE);
475 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
477 return 0;
481 * It is used to disable VBLANK interrupt
483 void psb_disable_vblank(struct drm_device *dev, int pipe)
485 struct drm_psb_private *dev_priv = dev->dev_private;
486 unsigned long irqflags;
488 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
490 if (pipe == 0)
491 dev_priv->vdc_irq_mask &= ~_PSB_VSYNC_PIPEA_FLAG;
492 else if (pipe == 1)
493 dev_priv->vdc_irq_mask &= ~_PSB_VSYNC_PIPEB_FLAG;
495 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
496 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
497 psb_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_ENABLE);
499 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
502 /* Called from drm generic code, passed a 'crtc', which
503 * we use as a pipe index
505 u32 psb_get_vblank_counter(struct drm_device *dev, int pipe)
507 uint32_t high_frame = PIPEAFRAMEHIGH;
508 uint32_t low_frame = PIPEAFRAMEPIXEL;
509 uint32_t pipeconf_reg = PIPEACONF;
510 uint32_t reg_val = 0;
511 uint32_t high1 = 0, high2 = 0, low = 0, count = 0;
513 switch (pipe) {
514 case 0:
515 break;
516 case 1:
517 high_frame = PIPEBFRAMEHIGH;
518 low_frame = PIPEBFRAMEPIXEL;
519 pipeconf_reg = PIPEBCONF;
520 break;
521 case 2:
522 high_frame = PIPECFRAMEHIGH;
523 low_frame = PIPECFRAMEPIXEL;
524 pipeconf_reg = PIPECCONF;
525 break;
526 default:
527 dev_err(dev->dev, "%s, invalid pipe.\n", __func__);
528 return 0;
531 if (!gma_power_begin(dev, false))
532 return 0;
534 reg_val = REG_READ(pipeconf_reg);
536 if (!(reg_val & PIPEACONF_ENABLE)) {
537 dev_err(dev->dev, "trying to get vblank count for disabled pipe %d\n",
538 pipe);
539 goto psb_get_vblank_counter_exit;
543 * High & low register fields aren't synchronized, so make sure
544 * we get a low value that's stable across two reads of the high
545 * register.
547 do {
548 high1 = ((REG_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
549 PIPE_FRAME_HIGH_SHIFT);
550 low = ((REG_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
551 PIPE_FRAME_LOW_SHIFT);
552 high2 = ((REG_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
553 PIPE_FRAME_HIGH_SHIFT);
554 } while (high1 != high2);
556 count = (high1 << 8) | low;
558 psb_get_vblank_counter_exit:
560 gma_power_end(dev);
562 return count;