1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
4 * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.
5 * Copyright 2009 Jonathan Corbet <corbet@lwn.net>
9 * Core code for the Via multifunction framebuffer device.
11 #include <linux/aperture.h>
12 #include <linux/via-core.h>
13 #include <linux/via_i2c.h>
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <linux/list.h>
24 * The default port config.
26 static struct via_port_cfg adap_configs
[] = {
27 [VIA_PORT_26
] = { VIA_PORT_I2C
, VIA_MODE_I2C
, VIASR
, 0x26 },
28 [VIA_PORT_31
] = { VIA_PORT_I2C
, VIA_MODE_I2C
, VIASR
, 0x31 },
29 [VIA_PORT_25
] = { VIA_PORT_GPIO
, VIA_MODE_GPIO
, VIASR
, 0x25 },
30 [VIA_PORT_2C
] = { VIA_PORT_GPIO
, VIA_MODE_I2C
, VIASR
, 0x2c },
31 [VIA_PORT_3D
] = { VIA_PORT_GPIO
, VIA_MODE_GPIO
, VIASR
, 0x3d },
36 * The OLPC XO-1.5 puts the camera power and reset lines onto
39 static struct via_port_cfg olpc_adap_configs
[] = {
40 [VIA_PORT_26
] = { VIA_PORT_I2C
, VIA_MODE_I2C
, VIASR
, 0x26 },
41 [VIA_PORT_31
] = { VIA_PORT_I2C
, VIA_MODE_I2C
, VIASR
, 0x31 },
42 [VIA_PORT_25
] = { VIA_PORT_GPIO
, VIA_MODE_GPIO
, VIASR
, 0x25 },
43 [VIA_PORT_2C
] = { VIA_PORT_GPIO
, VIA_MODE_GPIO
, VIASR
, 0x2c },
44 [VIA_PORT_3D
] = { VIA_PORT_GPIO
, VIA_MODE_GPIO
, VIASR
, 0x3d },
49 * We currently only support one viafb device (will there ever be
50 * more than one?), so just declare it globally here.
52 static struct viafb_dev global_dev
;
56 * Basic register access; spinlock required.
58 static inline void viafb_mmio_write(int reg
, u32 v
)
60 iowrite32(v
, global_dev
.engine_mmio
+ reg
);
63 static inline int viafb_mmio_read(int reg
)
65 return ioread32(global_dev
.engine_mmio
+ reg
);
68 /* ---------------------------------------------------------------------- */
70 * Interrupt management. We have a single IRQ line for a lot of
71 * different functions, so we need to share it. The design here
72 * is that we don't want to reimplement the shared IRQ code here;
73 * we also want to avoid having contention for a single handler thread.
74 * So each subdev driver which needs interrupts just requests
75 * them directly from the kernel. We just have what's needed for
76 * overall access to the interrupt control register.
80 * Which interrupts are enabled now?
82 static u32 viafb_enabled_ints
;
84 static void viafb_int_init(void)
86 viafb_enabled_ints
= 0;
88 viafb_mmio_write(VDE_INTERRUPT
, 0);
92 * Allow subdevs to ask for specific interrupts to be enabled. These
93 * functions must be called with reg_lock held
95 void viafb_irq_enable(u32 mask
)
97 viafb_enabled_ints
|= mask
;
98 viafb_mmio_write(VDE_INTERRUPT
, viafb_enabled_ints
| VDE_I_ENABLE
);
100 EXPORT_SYMBOL_GPL(viafb_irq_enable
);
102 void viafb_irq_disable(u32 mask
)
104 viafb_enabled_ints
&= ~mask
;
105 if (viafb_enabled_ints
== 0)
106 viafb_mmio_write(VDE_INTERRUPT
, 0); /* Disable entirely */
108 viafb_mmio_write(VDE_INTERRUPT
,
109 viafb_enabled_ints
| VDE_I_ENABLE
);
111 EXPORT_SYMBOL_GPL(viafb_irq_disable
);
113 /* ---------------------------------------------------------------------- */
115 * Currently, the camera driver is the only user of the DMA code, so we
116 * only compile it in if the camera driver is being built. Chances are,
117 * most viafb systems will not need to have this extra code for a while.
118 * As soon as another user comes long, the ifdef can be removed.
120 #if IS_ENABLED(CONFIG_VIDEO_VIA_CAMERA)
122 * Access to the DMA engine. This currently provides what the camera
123 * driver needs (i.e. outgoing only) but is easily expandable if need
128 * There are four DMA channels in the vx855. For now, we only
129 * use one of them, though. Most of the time, the DMA channel
130 * will be idle, so we keep the IRQ handler unregistered except
131 * when some subsystem has indicated an interest.
133 static int viafb_dma_users
;
134 static DECLARE_COMPLETION(viafb_dma_completion
);
136 * This mutex protects viafb_dma_users and our global interrupt
137 * registration state; it also serializes access to the DMA
140 static DEFINE_MUTEX(viafb_dma_lock
);
143 * The VX855 DMA descriptor (used for s/g transfers) looks
146 struct viafb_vx855_dma_descr
{
147 u32 addr_low
; /* Low part of phys addr */
148 u32 addr_high
; /* High 12 bits of addr */
149 u32 fb_offset
; /* Offset into FB memory */
150 u32 seg_size
; /* Size, 16-byte units */
151 u32 tile_mode
; /* "tile mode" setting */
152 u32 next_desc_low
; /* Next descriptor addr */
154 u32 pad
; /* Fill out to 64 bytes */
158 * Flags added to the "next descriptor low" pointers
160 #define VIAFB_DMA_MAGIC 0x01 /* ??? Just has to be there */
161 #define VIAFB_DMA_FINAL_SEGMENT 0x02 /* Final segment */
164 * The completion IRQ handler.
166 static irqreturn_t
viafb_dma_irq(int irq
, void *data
)
169 irqreturn_t ret
= IRQ_NONE
;
171 spin_lock(&global_dev
.reg_lock
);
172 csr
= viafb_mmio_read(VDMA_CSR0
);
173 if (csr
& VDMA_C_DONE
) {
174 viafb_mmio_write(VDMA_CSR0
, VDMA_C_DONE
);
175 complete(&viafb_dma_completion
);
178 spin_unlock(&global_dev
.reg_lock
);
183 * Indicate a need for DMA functionality.
185 int viafb_request_dma(void)
190 * Only VX855 is supported currently.
192 if (global_dev
.chip_type
!= UNICHROME_VX855
)
195 * Note the new user and set up our interrupt handler
198 mutex_lock(&viafb_dma_lock
);
200 if (viafb_dma_users
== 1) {
201 ret
= request_irq(global_dev
.pdev
->irq
, viafb_dma_irq
,
202 IRQF_SHARED
, "via-dma", &viafb_dma_users
);
206 viafb_irq_enable(VDE_I_DMA0TDEN
);
208 mutex_unlock(&viafb_dma_lock
);
211 EXPORT_SYMBOL_GPL(viafb_request_dma
);
213 void viafb_release_dma(void)
215 mutex_lock(&viafb_dma_lock
);
217 if (viafb_dma_users
== 0) {
218 viafb_irq_disable(VDE_I_DMA0TDEN
);
219 free_irq(global_dev
.pdev
->irq
, &viafb_dma_users
);
221 mutex_unlock(&viafb_dma_lock
);
223 EXPORT_SYMBOL_GPL(viafb_release_dma
);
226 * Do a scatter/gather DMA copy from FB memory. You must have done
227 * a successful call to viafb_request_dma() first.
229 int viafb_dma_copy_out_sg(unsigned int offset
, struct scatterlist
*sg
, int nsg
)
231 struct viafb_vx855_dma_descr
*descr
;
233 dma_addr_t descr_handle
;
236 struct scatterlist
*sgentry
;
240 * Get a place to put the descriptors.
242 descrpages
= dma_alloc_coherent(&global_dev
.pdev
->dev
,
243 nsg
*sizeof(struct viafb_vx855_dma_descr
),
244 &descr_handle
, GFP_KERNEL
);
245 if (descrpages
== NULL
) {
246 dev_err(&global_dev
.pdev
->dev
, "Unable to get descr page.\n");
249 mutex_lock(&viafb_dma_lock
);
254 nextdesc
= descr_handle
+ sizeof(struct viafb_vx855_dma_descr
);
255 for_each_sg(sg
, sgentry
, nsg
, i
) {
256 dma_addr_t paddr
= sg_dma_address(sgentry
);
257 descr
->addr_low
= paddr
& 0xfffffff0;
258 descr
->addr_high
= ((u64
) paddr
>> 32) & 0x0fff;
259 descr
->fb_offset
= offset
;
260 descr
->seg_size
= sg_dma_len(sgentry
) >> 4;
261 descr
->tile_mode
= 0;
262 descr
->next_desc_low
= (nextdesc
&0xfffffff0) | VIAFB_DMA_MAGIC
;
263 descr
->next_desc_high
= ((u64
) nextdesc
>> 32) & 0x0fff;
264 descr
->pad
= 0xffffffff; /* VIA driver does this */
265 offset
+= sg_dma_len(sgentry
);
266 nextdesc
+= sizeof(struct viafb_vx855_dma_descr
);
269 descr
[-1].next_desc_low
= VIAFB_DMA_FINAL_SEGMENT
|VIAFB_DMA_MAGIC
;
271 * Program the engine.
273 spin_lock_irqsave(&global_dev
.reg_lock
, flags
);
274 init_completion(&viafb_dma_completion
);
275 viafb_mmio_write(VDMA_DQWCR0
, 0);
276 viafb_mmio_write(VDMA_CSR0
, VDMA_C_ENABLE
|VDMA_C_DONE
);
277 viafb_mmio_write(VDMA_MR0
, VDMA_MR_TDIE
| VDMA_MR_CHAIN
);
278 viafb_mmio_write(VDMA_DPRL0
, descr_handle
| VIAFB_DMA_MAGIC
);
279 viafb_mmio_write(VDMA_DPRH0
,
280 (((u64
)descr_handle
>> 32) & 0x0fff) | 0xf0000);
281 (void) viafb_mmio_read(VDMA_CSR0
);
282 viafb_mmio_write(VDMA_CSR0
, VDMA_C_ENABLE
|VDMA_C_START
);
283 spin_unlock_irqrestore(&global_dev
.reg_lock
, flags
);
285 * Now we just wait until the interrupt handler says
286 * we're done. Except that, actually, we need to wait a little
287 * longer: the interrupts seem to jump the gun a little and we
288 * get corrupted frames sometimes.
290 wait_for_completion_timeout(&viafb_dma_completion
, 1);
292 if ((viafb_mmio_read(VDMA_CSR0
)&VDMA_C_DONE
) == 0)
293 printk(KERN_ERR
"VIA DMA timeout!\n");
295 * Clean up and we're done.
297 viafb_mmio_write(VDMA_CSR0
, VDMA_C_DONE
);
298 viafb_mmio_write(VDMA_MR0
, 0); /* Reset int enable */
299 mutex_unlock(&viafb_dma_lock
);
300 dma_free_coherent(&global_dev
.pdev
->dev
,
301 nsg
*sizeof(struct viafb_vx855_dma_descr
), descrpages
,
305 EXPORT_SYMBOL_GPL(viafb_dma_copy_out_sg
);
306 #endif /* CONFIG_VIDEO_VIA_CAMERA */
308 /* ---------------------------------------------------------------------- */
310 * Figure out how big our framebuffer memory is. Kind of ugly,
311 * but evidently we can't trust the information found in the
312 * fbdev configuration area.
314 static u16 via_function3
[] = {
315 CLE266_FUNCTION3
, KM400_FUNCTION3
, CN400_FUNCTION3
, CN700_FUNCTION3
,
316 CX700_FUNCTION3
, KM800_FUNCTION3
, KM890_FUNCTION3
, P4M890_FUNCTION3
,
317 P4M900_FUNCTION3
, VX800_FUNCTION3
, VX855_FUNCTION3
, VX900_FUNCTION3
,
320 /* Get the BIOS-configured framebuffer size from PCI configuration space
321 * of function 3 in the respective chipset */
322 static int viafb_get_fb_size_from_pci(int chip_type
)
329 /* search for the "FUNCTION3" device in this chipset */
330 for (i
= 0; i
< ARRAY_SIZE(via_function3
); i
++) {
331 struct pci_dev
*pdev
;
333 pdev
= pci_get_device(PCI_VENDOR_ID_VIA
, via_function3
[i
],
338 DEBUG_MSG(KERN_INFO
"Device ID = %x\n", pdev
->device
);
340 switch (pdev
->device
) {
341 case CLE266_FUNCTION3
:
342 case KM400_FUNCTION3
:
345 case CN400_FUNCTION3
:
346 case CN700_FUNCTION3
:
347 case CX700_FUNCTION3
:
348 case KM800_FUNCTION3
:
349 case KM890_FUNCTION3
:
350 case P4M890_FUNCTION3
:
351 case P4M900_FUNCTION3
:
352 case VX800_FUNCTION3
:
353 case VX855_FUNCTION3
:
354 case VX900_FUNCTION3
:
355 /*case CN750_FUNCTION3: */
363 pci_read_config_dword(pdev
, offset
, &FBSize
);
368 printk(KERN_ERR
"cannot determine framebuffer size\n");
372 FBSize
= FBSize
& 0x00007000;
373 DEBUG_MSG(KERN_INFO
"FB Size = %x\n", FBSize
);
375 if (chip_type
< UNICHROME_CX700
) {
378 VideoMemSize
= (16 << 20); /*16M */
382 VideoMemSize
= (32 << 20); /*32M */
386 VideoMemSize
= (64 << 20); /*64M */
390 VideoMemSize
= (32 << 20); /*32M */
396 VideoMemSize
= (8 << 20); /*8M */
400 VideoMemSize
= (16 << 20); /*16M */
404 VideoMemSize
= (32 << 20); /*32M */
408 VideoMemSize
= (64 << 20); /*64M */
412 VideoMemSize
= (128 << 20); /*128M */
416 VideoMemSize
= (256 << 20); /*256M */
419 case 0x00007000: /* Only on VX855/875 */
420 VideoMemSize
= (512 << 20); /*512M */
424 VideoMemSize
= (32 << 20); /*32M */
434 * Figure out and map our MMIO regions.
436 static int via_pci_setup_mmio(struct viafb_dev
*vdev
)
440 * Hook up to the device registers. Note that we soldier
441 * on if it fails; the framebuffer can operate (without
442 * acceleration) without this region.
444 vdev
->engine_start
= pci_resource_start(vdev
->pdev
, 1);
445 vdev
->engine_len
= pci_resource_len(vdev
->pdev
, 1);
446 vdev
->engine_mmio
= ioremap(vdev
->engine_start
,
448 if (vdev
->engine_mmio
== NULL
)
449 dev_err(&vdev
->pdev
->dev
,
450 "Unable to map engine MMIO; operation will be "
451 "slow and crippled.\n");
453 * Map in framebuffer memory. For now, failure here is
454 * fatal. Unfortunately, in the absence of significant
455 * vmalloc space, failure here is also entirely plausible.
456 * Eventually we want to move away from mapping this
459 if (vdev
->chip_type
== UNICHROME_VX900
)
460 vdev
->fbmem_start
= pci_resource_start(vdev
->pdev
, 2);
462 vdev
->fbmem_start
= pci_resource_start(vdev
->pdev
, 0);
463 ret
= vdev
->fbmem_len
= viafb_get_fb_size_from_pci(vdev
->chip_type
);
467 /* try to map less memory on failure, 8 MB should be still enough */
468 for (; vdev
->fbmem_len
>= 8 << 20; vdev
->fbmem_len
/= 2) {
469 vdev
->fbmem
= ioremap_wc(vdev
->fbmem_start
, vdev
->fbmem_len
);
474 if (vdev
->fbmem
== NULL
) {
480 iounmap(vdev
->engine_mmio
);
484 static void via_pci_teardown_mmio(struct viafb_dev
*vdev
)
486 iounmap(vdev
->fbmem
);
487 iounmap(vdev
->engine_mmio
);
491 * Create our subsidiary devices.
493 static struct viafb_subdev_info
{
495 struct platform_device
*platdev
;
496 } viafb_subdevs
[] = {
498 .name
= "viafb-gpio",
503 #if IS_ENABLED(CONFIG_VIDEO_VIA_CAMERA)
505 .name
= "viafb-camera",
509 #define N_SUBDEVS ARRAY_SIZE(viafb_subdevs)
511 static int via_create_subdev(struct viafb_dev
*vdev
,
512 struct viafb_subdev_info
*info
)
516 info
->platdev
= platform_device_alloc(info
->name
, -1);
517 if (!info
->platdev
) {
518 dev_err(&vdev
->pdev
->dev
, "Unable to allocate pdev %s\n",
522 info
->platdev
->dev
.parent
= &vdev
->pdev
->dev
;
523 info
->platdev
->dev
.platform_data
= vdev
;
524 ret
= platform_device_add(info
->platdev
);
526 dev_err(&vdev
->pdev
->dev
, "Unable to add pdev %s\n",
528 platform_device_put(info
->platdev
);
529 info
->platdev
= NULL
;
534 static int via_setup_subdevs(struct viafb_dev
*vdev
)
539 * Ignore return values. Even if some of the devices
540 * fail to be created, we'll still be able to use some
543 for (i
= 0; i
< N_SUBDEVS
; i
++)
544 via_create_subdev(vdev
, viafb_subdevs
+ i
);
548 static void via_teardown_subdevs(void)
552 for (i
= 0; i
< N_SUBDEVS
; i
++)
553 if (viafb_subdevs
[i
].platdev
) {
554 viafb_subdevs
[i
].platdev
->dev
.platform_data
= NULL
;
555 platform_device_unregister(viafb_subdevs
[i
].platdev
);
560 * Power management functions
562 static __maybe_unused
LIST_HEAD(viafb_pm_hooks
);
563 static __maybe_unused
DEFINE_MUTEX(viafb_pm_hooks_lock
);
565 void viafb_pm_register(struct viafb_pm_hooks
*hooks
)
567 INIT_LIST_HEAD(&hooks
->list
);
569 mutex_lock(&viafb_pm_hooks_lock
);
570 list_add_tail(&hooks
->list
, &viafb_pm_hooks
);
571 mutex_unlock(&viafb_pm_hooks_lock
);
573 EXPORT_SYMBOL_GPL(viafb_pm_register
);
575 void viafb_pm_unregister(struct viafb_pm_hooks
*hooks
)
577 mutex_lock(&viafb_pm_hooks_lock
);
578 list_del(&hooks
->list
);
579 mutex_unlock(&viafb_pm_hooks_lock
);
581 EXPORT_SYMBOL_GPL(viafb_pm_unregister
);
583 static int __maybe_unused
via_suspend(struct device
*dev
)
585 struct viafb_pm_hooks
*hooks
;
588 * "I've occasionally hit a few drivers that caused suspend
589 * failures, and each and every time it was a driver bug, and
590 * the right thing to do was to just ignore the error and suspend
591 * anyway - returning an error code and trying to undo the suspend
592 * is not what anybody ever really wants, even if our model
594 * -- Linus Torvalds, Dec. 7, 2009
596 mutex_lock(&viafb_pm_hooks_lock
);
597 list_for_each_entry_reverse(hooks
, &viafb_pm_hooks
, list
)
598 hooks
->suspend(hooks
->private);
599 mutex_unlock(&viafb_pm_hooks_lock
);
604 static int __maybe_unused
via_resume(struct device
*dev
)
606 struct viafb_pm_hooks
*hooks
;
608 /* Now bring back any subdevs */
609 mutex_lock(&viafb_pm_hooks_lock
);
610 list_for_each_entry(hooks
, &viafb_pm_hooks
, list
)
611 hooks
->resume(hooks
->private);
612 mutex_unlock(&viafb_pm_hooks_lock
);
617 static int via_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
621 ret
= aperture_remove_conflicting_pci_devices(pdev
, "viafb");
625 ret
= pci_enable_device(pdev
);
630 * Global device initialization.
632 memset(&global_dev
, 0, sizeof(global_dev
));
633 global_dev
.pdev
= pdev
;
634 global_dev
.chip_type
= ent
->driver_data
;
635 global_dev
.port_cfg
= adap_configs
;
636 if (machine_is_olpc())
637 global_dev
.port_cfg
= olpc_adap_configs
;
639 spin_lock_init(&global_dev
.reg_lock
);
640 ret
= via_pci_setup_mmio(&global_dev
);
644 * Set up interrupts and create our subdevices. Continue even if
648 via_setup_subdevs(&global_dev
);
650 * Set up the framebuffer device
652 ret
= via_fb_pci_probe(&global_dev
);
658 via_teardown_subdevs();
659 via_pci_teardown_mmio(&global_dev
);
661 pci_disable_device(pdev
);
665 static void via_pci_remove(struct pci_dev
*pdev
)
667 via_teardown_subdevs();
668 via_fb_pci_remove(pdev
);
669 via_pci_teardown_mmio(&global_dev
);
670 pci_disable_device(pdev
);
674 static const struct pci_device_id via_pci_table
[] = {
675 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_CLE266_DID
),
676 .driver_data
= UNICHROME_CLE266
},
677 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_K400_DID
),
678 .driver_data
= UNICHROME_K400
},
679 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_K800_DID
),
680 .driver_data
= UNICHROME_K800
},
681 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_PM800_DID
),
682 .driver_data
= UNICHROME_PM800
},
683 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_CN700_DID
),
684 .driver_data
= UNICHROME_CN700
},
685 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_CX700_DID
),
686 .driver_data
= UNICHROME_CX700
},
687 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_CN750_DID
),
688 .driver_data
= UNICHROME_CN750
},
689 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_K8M890_DID
),
690 .driver_data
= UNICHROME_K8M890
},
691 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_P4M890_DID
),
692 .driver_data
= UNICHROME_P4M890
},
693 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_P4M900_DID
),
694 .driver_data
= UNICHROME_P4M900
},
695 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_VX800_DID
),
696 .driver_data
= UNICHROME_VX800
},
697 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_VX855_DID
),
698 .driver_data
= UNICHROME_VX855
},
699 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_VX900_DID
),
700 .driver_data
= UNICHROME_VX900
},
703 MODULE_DEVICE_TABLE(pci
, via_pci_table
);
705 static const struct dev_pm_ops via_pm_ops
= {
706 #ifdef CONFIG_PM_SLEEP
707 .suspend
= via_suspend
,
708 .resume
= via_resume
,
712 .restore
= via_resume
,
716 static struct pci_driver via_driver
= {
718 .id_table
= via_pci_table
,
719 .probe
= via_pci_probe
,
720 .remove
= via_pci_remove
,
721 .driver
.pm
= &via_pm_ops
,
724 static int __init
via_core_init(void)
728 if (fb_modesetting_disabled("viafb"))
736 ret
= pci_register_driver(&via_driver
);
746 static void __exit
via_core_exit(void)
748 pci_unregister_driver(&via_driver
);
754 module_init(via_core_init
);
755 module_exit(via_core_exit
);