2 * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.
4 * Copyright 2009 Jonathan Corbet <corbet@lwn.net>
8 * Core code for the Via multifunction framebuffer device.
10 #include <linux/via-core.h>
11 #include <linux/via_i2c.h>
12 #include <linux/via-gpio.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/platform_device.h>
20 * The default port config.
22 static struct via_port_cfg adap_configs[] = {
23 [VIA_PORT_26] = { VIA_PORT_I2C, VIA_MODE_I2C, VIASR, 0x26 },
24 [VIA_PORT_31] = { VIA_PORT_I2C, VIA_MODE_I2C, VIASR, 0x31 },
25 [VIA_PORT_25] = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x25 },
26 [VIA_PORT_2C] = { VIA_PORT_GPIO, VIA_MODE_I2C, VIASR, 0x2c },
27 [VIA_PORT_3D] = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x3d },
32 * We currently only support one viafb device (will there ever be
33 * more than one?), so just declare it globally here.
35 static struct viafb_dev global_dev;
39 * Basic register access; spinlock required.
41 static inline void viafb_mmio_write(int reg, u32 v)
43 iowrite32(v, global_dev.engine_mmio + reg);
46 static inline int viafb_mmio_read(int reg)
48 return ioread32(global_dev.engine_mmio + reg);
51 /* ---------------------------------------------------------------------- */
53 * Interrupt management. We have a single IRQ line for a lot of
54 * different functions, so we need to share it. The design here
55 * is that we don't want to reimplement the shared IRQ code here;
56 * we also want to avoid having contention for a single handler thread.
57 * So each subdev driver which needs interrupts just requests
58 * them directly from the kernel. We just have what's needed for
59 * overall access to the interrupt control register.
63 * Which interrupts are enabled now?
65 static u32 viafb_enabled_ints;
67 static void __devinit viafb_int_init(void)
69 viafb_enabled_ints = 0;
71 viafb_mmio_write(VDE_INTERRUPT, 0);
75 * Allow subdevs to ask for specific interrupts to be enabled. These
76 * functions must be called with reg_lock held
78 void viafb_irq_enable(u32 mask)
80 viafb_enabled_ints |= mask;
81 viafb_mmio_write(VDE_INTERRUPT, viafb_enabled_ints | VDE_I_ENABLE);
83 EXPORT_SYMBOL_GPL(viafb_irq_enable);
85 void viafb_irq_disable(u32 mask)
87 viafb_enabled_ints &= ~mask;
88 if (viafb_enabled_ints == 0)
89 viafb_mmio_write(VDE_INTERRUPT, 0); /* Disable entirely */
91 viafb_mmio_write(VDE_INTERRUPT,
92 viafb_enabled_ints | VDE_I_ENABLE);
94 EXPORT_SYMBOL_GPL(viafb_irq_disable);
96 /* ---------------------------------------------------------------------- */
98 * Currently, the camera driver is the only user of the DMA code, so we
99 * only compile it in if the camera driver is being built. Chances are,
100 * most viafb systems will not need to have this extra code for a while.
101 * As soon as another user comes long, the ifdef can be removed.
103 #if defined(CONFIG_VIDEO_VIA_CAMERA) || defined(CONFIG_VIDEO_VIA_CAMERA_MODULE)
105 * Access to the DMA engine. This currently provides what the camera
106 * driver needs (i.e. outgoing only) but is easily expandable if need
111 * There are four DMA channels in the vx855. For now, we only
112 * use one of them, though. Most of the time, the DMA channel
113 * will be idle, so we keep the IRQ handler unregistered except
114 * when some subsystem has indicated an interest.
116 static int viafb_dma_users;
117 static DECLARE_COMPLETION(viafb_dma_completion);
119 * This mutex protects viafb_dma_users and our global interrupt
120 * registration state; it also serializes access to the DMA
123 static DEFINE_MUTEX(viafb_dma_lock);
126 * The VX855 DMA descriptor (used for s/g transfers) looks
129 struct viafb_vx855_dma_descr {
130 u32 addr_low; /* Low part of phys addr */
131 u32 addr_high; /* High 12 bits of addr */
132 u32 fb_offset; /* Offset into FB memory */
133 u32 seg_size; /* Size, 16-byte units */
134 u32 tile_mode; /* "tile mode" setting */
135 u32 next_desc_low; /* Next descriptor addr */
137 u32 pad; /* Fill out to 64 bytes */
141 * Flags added to the "next descriptor low" pointers
143 #define VIAFB_DMA_MAGIC 0x01 /* ??? Just has to be there */
144 #define VIAFB_DMA_FINAL_SEGMENT 0x02 /* Final segment */
147 * The completion IRQ handler.
149 static irqreturn_t viafb_dma_irq(int irq, void *data)
152 irqreturn_t ret = IRQ_NONE;
154 spin_lock(&global_dev.reg_lock);
155 csr = viafb_mmio_read(VDMA_CSR0);
156 if (csr & VDMA_C_DONE) {
157 viafb_mmio_write(VDMA_CSR0, VDMA_C_DONE);
158 complete(&viafb_dma_completion);
161 spin_unlock(&global_dev.reg_lock);
166 * Indicate a need for DMA functionality.
168 int viafb_request_dma(void)
173 * Only VX855 is supported currently.
175 if (global_dev.chip_type != UNICHROME_VX855)
178 * Note the new user and set up our interrupt handler
181 mutex_lock(&viafb_dma_lock);
183 if (viafb_dma_users == 1) {
184 ret = request_irq(global_dev.pdev->irq, viafb_dma_irq,
185 IRQF_SHARED, "via-dma", &viafb_dma_users);
189 viafb_irq_enable(VDE_I_DMA0TDEN);
191 mutex_unlock(&viafb_dma_lock);
194 EXPORT_SYMBOL_GPL(viafb_request_dma);
196 void viafb_release_dma(void)
198 mutex_lock(&viafb_dma_lock);
200 if (viafb_dma_users == 0) {
201 viafb_irq_disable(VDE_I_DMA0TDEN);
202 free_irq(global_dev.pdev->irq, &viafb_dma_users);
204 mutex_unlock(&viafb_dma_lock);
206 EXPORT_SYMBOL_GPL(viafb_release_dma);
211 * Copy a single buffer from FB memory, synchronously. This code works
212 * but is not currently used.
214 void viafb_dma_copy_out(unsigned int offset, dma_addr_t paddr, int len)
219 mutex_lock(&viafb_dma_lock);
220 init_completion(&viafb_dma_completion);
222 * Program the controller.
224 spin_lock_irqsave(&global_dev.reg_lock, flags);
225 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_DONE);
226 /* Enable ints; must happen after CSR0 write! */
227 viafb_mmio_write(VDMA_MR0, VDMA_MR_TDIE);
228 viafb_mmio_write(VDMA_MARL0, (int) (paddr & 0xfffffff0));
229 viafb_mmio_write(VDMA_MARH0, (int) ((paddr >> 28) & 0xfff));
230 /* Data sheet suggests DAR0 should be <<4, but it lies */
231 viafb_mmio_write(VDMA_DAR0, offset);
232 viafb_mmio_write(VDMA_DQWCR0, len >> 4);
233 viafb_mmio_write(VDMA_TMR0, 0);
234 viafb_mmio_write(VDMA_DPRL0, 0);
235 viafb_mmio_write(VDMA_DPRH0, 0);
236 viafb_mmio_write(VDMA_PMR0, 0);
237 csr = viafb_mmio_read(VDMA_CSR0);
238 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_START);
239 spin_unlock_irqrestore(&global_dev.reg_lock, flags);
241 * Now we just wait until the interrupt handler says
244 wait_for_completion_interruptible(&viafb_dma_completion);
245 viafb_mmio_write(VDMA_MR0, 0); /* Reset int enable */
246 mutex_unlock(&viafb_dma_lock);
248 EXPORT_SYMBOL_GPL(viafb_dma_copy_out);
252 * Do a scatter/gather DMA copy from FB memory. You must have done
253 * a successful call to viafb_request_dma() first.
255 int viafb_dma_copy_out_sg(unsigned int offset, struct scatterlist *sg, int nsg)
257 struct viafb_vx855_dma_descr *descr;
259 dma_addr_t descr_handle;
262 struct scatterlist *sgentry;
266 * Get a place to put the descriptors.
268 descrpages = dma_alloc_coherent(&global_dev.pdev->dev,
269 nsg*sizeof(struct viafb_vx855_dma_descr),
270 &descr_handle, GFP_KERNEL);
271 if (descrpages == NULL) {
272 dev_err(&global_dev.pdev->dev, "Unable to get descr page.\n");
275 mutex_lock(&viafb_dma_lock);
280 nextdesc = descr_handle + sizeof(struct viafb_vx855_dma_descr);
281 for_each_sg(sg, sgentry, nsg, i) {
282 dma_addr_t paddr = sg_dma_address(sgentry);
283 descr->addr_low = paddr & 0xfffffff0;
284 descr->addr_high = ((u64) paddr >> 32) & 0x0fff;
285 descr->fb_offset = offset;
286 descr->seg_size = sg_dma_len(sgentry) >> 4;
287 descr->tile_mode = 0;
288 descr->next_desc_low = (nextdesc&0xfffffff0) | VIAFB_DMA_MAGIC;
289 descr->next_desc_high = ((u64) nextdesc >> 32) & 0x0fff;
290 descr->pad = 0xffffffff; /* VIA driver does this */
291 offset += sg_dma_len(sgentry);
292 nextdesc += sizeof(struct viafb_vx855_dma_descr);
295 descr[-1].next_desc_low = VIAFB_DMA_FINAL_SEGMENT|VIAFB_DMA_MAGIC;
297 * Program the engine.
299 spin_lock_irqsave(&global_dev.reg_lock, flags);
300 init_completion(&viafb_dma_completion);
301 viafb_mmio_write(VDMA_DQWCR0, 0);
302 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_DONE);
303 viafb_mmio_write(VDMA_MR0, VDMA_MR_TDIE | VDMA_MR_CHAIN);
304 viafb_mmio_write(VDMA_DPRL0, descr_handle | VIAFB_DMA_MAGIC);
305 viafb_mmio_write(VDMA_DPRH0,
306 (((u64)descr_handle >> 32) & 0x0fff) | 0xf0000);
307 (void) viafb_mmio_read(VDMA_CSR0);
308 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_START);
309 spin_unlock_irqrestore(&global_dev.reg_lock, flags);
311 * Now we just wait until the interrupt handler says
312 * we're done. Except that, actually, we need to wait a little
313 * longer: the interrupts seem to jump the gun a little and we
314 * get corrupted frames sometimes.
316 wait_for_completion_timeout(&viafb_dma_completion, 1);
318 if ((viafb_mmio_read(VDMA_CSR0)&VDMA_C_DONE) == 0)
319 printk(KERN_ERR "VIA DMA timeout!\n");
321 * Clean up and we're done.
323 viafb_mmio_write(VDMA_CSR0, VDMA_C_DONE);
324 viafb_mmio_write(VDMA_MR0, 0); /* Reset int enable */
325 mutex_unlock(&viafb_dma_lock);
326 dma_free_coherent(&global_dev.pdev->dev,
327 nsg*sizeof(struct viafb_vx855_dma_descr), descrpages,
331 EXPORT_SYMBOL_GPL(viafb_dma_copy_out_sg);
332 #endif /* CONFIG_VIDEO_VIA_CAMERA */
334 /* ---------------------------------------------------------------------- */
336 * Figure out how big our framebuffer memory is. Kind of ugly,
337 * but evidently we can't trust the information found in the
338 * fbdev configuration area.
340 static u16 via_function3[] = {
341 CLE266_FUNCTION3, KM400_FUNCTION3, CN400_FUNCTION3, CN700_FUNCTION3,
342 CX700_FUNCTION3, KM800_FUNCTION3, KM890_FUNCTION3, P4M890_FUNCTION3,
343 P4M900_FUNCTION3, VX800_FUNCTION3, VX855_FUNCTION3, VX900_FUNCTION3,
346 /* Get the BIOS-configured framebuffer size from PCI configuration space
347 * of function 3 in the respective chipset */
348 static int viafb_get_fb_size_from_pci(int chip_type)
355 /* search for the "FUNCTION3" device in this chipset */
356 for (i = 0; i < ARRAY_SIZE(via_function3); i++) {
357 struct pci_dev *pdev;
359 pdev = pci_get_device(PCI_VENDOR_ID_VIA, via_function3[i],
364 DEBUG_MSG(KERN_INFO "Device ID = %x\n", pdev->device);
366 switch (pdev->device) {
367 case CLE266_FUNCTION3:
368 case KM400_FUNCTION3:
371 case CN400_FUNCTION3:
372 case CN700_FUNCTION3:
373 case CX700_FUNCTION3:
374 case KM800_FUNCTION3:
375 case KM890_FUNCTION3:
376 case P4M890_FUNCTION3:
377 case P4M900_FUNCTION3:
378 case VX800_FUNCTION3:
379 case VX855_FUNCTION3:
380 case VX900_FUNCTION3:
381 /*case CN750_FUNCTION3: */
389 pci_read_config_dword(pdev, offset, &FBSize);
394 printk(KERN_ERR "cannot determine framebuffer size\n");
398 FBSize = FBSize & 0x00007000;
399 DEBUG_MSG(KERN_INFO "FB Size = %x\n", FBSize);
401 if (chip_type < UNICHROME_CX700) {
404 VideoMemSize = (16 << 20); /*16M */
408 VideoMemSize = (32 << 20); /*32M */
412 VideoMemSize = (64 << 20); /*64M */
416 VideoMemSize = (32 << 20); /*32M */
422 VideoMemSize = (8 << 20); /*8M */
426 VideoMemSize = (16 << 20); /*16M */
430 VideoMemSize = (32 << 20); /*32M */
434 VideoMemSize = (64 << 20); /*64M */
438 VideoMemSize = (128 << 20); /*128M */
442 VideoMemSize = (256 << 20); /*256M */
445 case 0x00007000: /* Only on VX855/875 */
446 VideoMemSize = (512 << 20); /*512M */
450 VideoMemSize = (32 << 20); /*32M */
460 * Figure out and map our MMIO regions.
462 static int __devinit via_pci_setup_mmio(struct viafb_dev *vdev)
466 * Hook up to the device registers. Note that we soldier
467 * on if it fails; the framebuffer can operate (without
468 * acceleration) without this region.
470 vdev->engine_start = pci_resource_start(vdev->pdev, 1);
471 vdev->engine_len = pci_resource_len(vdev->pdev, 1);
472 vdev->engine_mmio = ioremap_nocache(vdev->engine_start,
474 if (vdev->engine_mmio == NULL)
475 dev_err(&vdev->pdev->dev,
476 "Unable to map engine MMIO; operation will be "
477 "slow and crippled.\n");
479 * Map in framebuffer memory. For now, failure here is
480 * fatal. Unfortunately, in the absence of significant
481 * vmalloc space, failure here is also entirely plausible.
482 * Eventually we want to move away from mapping this
485 if (vdev->chip_type == UNICHROME_VX900)
486 vdev->fbmem_start = pci_resource_start(vdev->pdev, 2);
488 vdev->fbmem_start = pci_resource_start(vdev->pdev, 0);
489 ret = vdev->fbmem_len = viafb_get_fb_size_from_pci(vdev->chip_type);
492 vdev->fbmem = ioremap_nocache(vdev->fbmem_start, vdev->fbmem_len);
493 if (vdev->fbmem == NULL) {
499 iounmap(vdev->engine_mmio);
503 static void via_pci_teardown_mmio(struct viafb_dev *vdev)
505 iounmap(vdev->fbmem);
506 iounmap(vdev->engine_mmio);
510 * Create our subsidiary devices.
512 static struct viafb_subdev_info {
514 struct platform_device *platdev;
515 } viafb_subdevs[] = {
517 .name = "viafb-gpio",
522 #if defined(CONFIG_VIDEO_VIA_CAMERA) || defined(CONFIG_VIDEO_VIA_CAMERA_MODULE)
524 .name = "viafb-camera",
528 #define N_SUBDEVS ARRAY_SIZE(viafb_subdevs)
530 static int __devinit via_create_subdev(struct viafb_dev *vdev,
531 struct viafb_subdev_info *info)
535 info->platdev = platform_device_alloc(info->name, -1);
536 if (!info->platdev) {
537 dev_err(&vdev->pdev->dev, "Unable to allocate pdev %s\n",
541 info->platdev->dev.parent = &vdev->pdev->dev;
542 info->platdev->dev.platform_data = vdev;
543 ret = platform_device_add(info->platdev);
545 dev_err(&vdev->pdev->dev, "Unable to add pdev %s\n",
547 platform_device_put(info->platdev);
548 info->platdev = NULL;
553 static int __devinit via_setup_subdevs(struct viafb_dev *vdev)
558 * Ignore return values. Even if some of the devices
559 * fail to be created, we'll still be able to use some
562 for (i = 0; i < N_SUBDEVS; i++)
563 via_create_subdev(vdev, viafb_subdevs + i);
567 static void via_teardown_subdevs(void)
571 for (i = 0; i < N_SUBDEVS; i++)
572 if (viafb_subdevs[i].platdev) {
573 viafb_subdevs[i].platdev->dev.platform_data = NULL;
574 platform_device_unregister(viafb_subdevs[i].platdev);
579 static int __devinit via_pci_probe(struct pci_dev *pdev,
580 const struct pci_device_id *ent)
584 ret = pci_enable_device(pdev);
588 * Global device initialization.
590 memset(&global_dev, 0, sizeof(global_dev));
591 global_dev.pdev = pdev;
592 global_dev.chip_type = ent->driver_data;
593 global_dev.port_cfg = adap_configs;
594 spin_lock_init(&global_dev.reg_lock);
595 ret = via_pci_setup_mmio(&global_dev);
599 * Set up interrupts and create our subdevices. Continue even if
603 via_setup_subdevs(&global_dev);
605 * Set up the framebuffer device
607 ret = via_fb_pci_probe(&global_dev);
613 via_teardown_subdevs();
614 via_pci_teardown_mmio(&global_dev);
616 pci_disable_device(pdev);
620 static void __devexit via_pci_remove(struct pci_dev *pdev)
622 via_teardown_subdevs();
623 via_fb_pci_remove(pdev);
624 via_pci_teardown_mmio(&global_dev);
625 pci_disable_device(pdev);
629 static struct pci_device_id via_pci_table[] __devinitdata = {
630 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CLE266_DID),
631 .driver_data = UNICHROME_CLE266 },
632 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K400_DID),
633 .driver_data = UNICHROME_K400 },
634 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K800_DID),
635 .driver_data = UNICHROME_K800 },
636 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_PM800_DID),
637 .driver_data = UNICHROME_PM800 },
638 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN700_DID),
639 .driver_data = UNICHROME_CN700 },
640 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CX700_DID),
641 .driver_data = UNICHROME_CX700 },
642 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN750_DID),
643 .driver_data = UNICHROME_CN750 },
644 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K8M890_DID),
645 .driver_data = UNICHROME_K8M890 },
646 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M890_DID),
647 .driver_data = UNICHROME_P4M890 },
648 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M900_DID),
649 .driver_data = UNICHROME_P4M900 },
650 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX800_DID),
651 .driver_data = UNICHROME_VX800 },
652 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX855_DID),
653 .driver_data = UNICHROME_VX855 },
654 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX900_DID),
655 .driver_data = UNICHROME_VX900 },
658 MODULE_DEVICE_TABLE(pci, via_pci_table);
660 static struct pci_driver via_driver = {
662 .id_table = via_pci_table,
663 .probe = via_pci_probe,
664 .remove = __devexit_p(via_pci_remove),
666 .suspend = viafb_suspend,
667 .resume = viafb_resume,
671 static int __init via_core_init(void)
680 return pci_register_driver(&via_driver);
683 static void __exit via_core_exit(void)
685 pci_unregister_driver(&via_driver);
691 module_init(via_core_init);
692 module_exit(via_core_exit);