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>
18 #include <linux/list.h>
22 * The default port config.
24 static struct via_port_cfg adap_configs[] = {
25 [VIA_PORT_26] = { VIA_PORT_I2C, VIA_MODE_I2C, VIASR, 0x26 },
26 [VIA_PORT_31] = { VIA_PORT_I2C, VIA_MODE_I2C, VIASR, 0x31 },
27 [VIA_PORT_25] = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x25 },
28 [VIA_PORT_2C] = { VIA_PORT_GPIO, VIA_MODE_I2C, VIASR, 0x2c },
29 [VIA_PORT_3D] = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x3d },
34 * We currently only support one viafb device (will there ever be
35 * more than one?), so just declare it globally here.
37 static struct viafb_dev global_dev;
41 * Basic register access; spinlock required.
43 static inline void viafb_mmio_write(int reg, u32 v)
45 iowrite32(v, global_dev.engine_mmio + reg);
48 static inline int viafb_mmio_read(int reg)
50 return ioread32(global_dev.engine_mmio + reg);
53 /* ---------------------------------------------------------------------- */
55 * Interrupt management. We have a single IRQ line for a lot of
56 * different functions, so we need to share it. The design here
57 * is that we don't want to reimplement the shared IRQ code here;
58 * we also want to avoid having contention for a single handler thread.
59 * So each subdev driver which needs interrupts just requests
60 * them directly from the kernel. We just have what's needed for
61 * overall access to the interrupt control register.
65 * Which interrupts are enabled now?
67 static u32 viafb_enabled_ints;
69 static void __devinit viafb_int_init(void)
71 viafb_enabled_ints = 0;
73 viafb_mmio_write(VDE_INTERRUPT, 0);
77 * Allow subdevs to ask for specific interrupts to be enabled. These
78 * functions must be called with reg_lock held
80 void viafb_irq_enable(u32 mask)
82 viafb_enabled_ints |= mask;
83 viafb_mmio_write(VDE_INTERRUPT, viafb_enabled_ints | VDE_I_ENABLE);
85 EXPORT_SYMBOL_GPL(viafb_irq_enable);
87 void viafb_irq_disable(u32 mask)
89 viafb_enabled_ints &= ~mask;
90 if (viafb_enabled_ints == 0)
91 viafb_mmio_write(VDE_INTERRUPT, 0); /* Disable entirely */
93 viafb_mmio_write(VDE_INTERRUPT,
94 viafb_enabled_ints | VDE_I_ENABLE);
96 EXPORT_SYMBOL_GPL(viafb_irq_disable);
98 /* ---------------------------------------------------------------------- */
100 * Access to the DMA engine. This currently provides what the camera
101 * driver needs (i.e. outgoing only) but is easily expandable if need
106 * There are four DMA channels in the vx855. For now, we only
107 * use one of them, though. Most of the time, the DMA channel
108 * will be idle, so we keep the IRQ handler unregistered except
109 * when some subsystem has indicated an interest.
111 static int viafb_dma_users;
112 static DECLARE_COMPLETION(viafb_dma_completion);
114 * This mutex protects viafb_dma_users and our global interrupt
115 * registration state; it also serializes access to the DMA
118 static DEFINE_MUTEX(viafb_dma_lock);
121 * The VX855 DMA descriptor (used for s/g transfers) looks
124 struct viafb_vx855_dma_descr {
125 u32 addr_low; /* Low part of phys addr */
126 u32 addr_high; /* High 12 bits of addr */
127 u32 fb_offset; /* Offset into FB memory */
128 u32 seg_size; /* Size, 16-byte units */
129 u32 tile_mode; /* "tile mode" setting */
130 u32 next_desc_low; /* Next descriptor addr */
132 u32 pad; /* Fill out to 64 bytes */
136 * Flags added to the "next descriptor low" pointers
138 #define VIAFB_DMA_MAGIC 0x01 /* ??? Just has to be there */
139 #define VIAFB_DMA_FINAL_SEGMENT 0x02 /* Final segment */
142 * The completion IRQ handler.
144 static irqreturn_t viafb_dma_irq(int irq, void *data)
147 irqreturn_t ret = IRQ_NONE;
149 spin_lock(&global_dev.reg_lock);
150 csr = viafb_mmio_read(VDMA_CSR0);
151 if (csr & VDMA_C_DONE) {
152 viafb_mmio_write(VDMA_CSR0, VDMA_C_DONE);
153 complete(&viafb_dma_completion);
156 spin_unlock(&global_dev.reg_lock);
161 * Indicate a need for DMA functionality.
163 int viafb_request_dma(void)
168 * Only VX855 is supported currently.
170 if (global_dev.chip_type != UNICHROME_VX855)
173 * Note the new user and set up our interrupt handler
176 mutex_lock(&viafb_dma_lock);
178 if (viafb_dma_users == 1) {
179 ret = request_irq(global_dev.pdev->irq, viafb_dma_irq,
180 IRQF_SHARED, "via-dma", &viafb_dma_users);
184 viafb_irq_enable(VDE_I_DMA0TDEN);
186 mutex_unlock(&viafb_dma_lock);
189 EXPORT_SYMBOL_GPL(viafb_request_dma);
191 void viafb_release_dma(void)
193 mutex_lock(&viafb_dma_lock);
195 if (viafb_dma_users == 0) {
196 viafb_irq_disable(VDE_I_DMA0TDEN);
197 free_irq(global_dev.pdev->irq, &viafb_dma_users);
199 mutex_unlock(&viafb_dma_lock);
201 EXPORT_SYMBOL_GPL(viafb_release_dma);
206 * Copy a single buffer from FB memory, synchronously. This code works
207 * but is not currently used.
209 void viafb_dma_copy_out(unsigned int offset, dma_addr_t paddr, int len)
214 mutex_lock(&viafb_dma_lock);
215 init_completion(&viafb_dma_completion);
217 * Program the controller.
219 spin_lock_irqsave(&global_dev.reg_lock, flags);
220 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_DONE);
221 /* Enable ints; must happen after CSR0 write! */
222 viafb_mmio_write(VDMA_MR0, VDMA_MR_TDIE);
223 viafb_mmio_write(VDMA_MARL0, (int) (paddr & 0xfffffff0));
224 viafb_mmio_write(VDMA_MARH0, (int) ((paddr >> 28) & 0xfff));
225 /* Data sheet suggests DAR0 should be <<4, but it lies */
226 viafb_mmio_write(VDMA_DAR0, offset);
227 viafb_mmio_write(VDMA_DQWCR0, len >> 4);
228 viafb_mmio_write(VDMA_TMR0, 0);
229 viafb_mmio_write(VDMA_DPRL0, 0);
230 viafb_mmio_write(VDMA_DPRH0, 0);
231 viafb_mmio_write(VDMA_PMR0, 0);
232 csr = viafb_mmio_read(VDMA_CSR0);
233 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_START);
234 spin_unlock_irqrestore(&global_dev.reg_lock, flags);
236 * Now we just wait until the interrupt handler says
239 wait_for_completion_interruptible(&viafb_dma_completion);
240 viafb_mmio_write(VDMA_MR0, 0); /* Reset int enable */
241 mutex_unlock(&viafb_dma_lock);
243 EXPORT_SYMBOL_GPL(viafb_dma_copy_out);
247 * Do a scatter/gather DMA copy from FB memory. You must have done
248 * a successful call to viafb_request_dma() first.
250 int viafb_dma_copy_out_sg(unsigned int offset, struct scatterlist *sg, int nsg)
252 struct viafb_vx855_dma_descr *descr;
254 dma_addr_t descr_handle;
257 struct scatterlist *sgentry;
261 * Get a place to put the descriptors.
263 descrpages = dma_alloc_coherent(&global_dev.pdev->dev,
264 nsg*sizeof(struct viafb_vx855_dma_descr),
265 &descr_handle, GFP_KERNEL);
266 if (descrpages == NULL) {
267 dev_err(&global_dev.pdev->dev, "Unable to get descr page.\n");
270 mutex_lock(&viafb_dma_lock);
275 nextdesc = descr_handle + sizeof(struct viafb_vx855_dma_descr);
276 for_each_sg(sg, sgentry, nsg, i) {
277 dma_addr_t paddr = sg_dma_address(sgentry);
278 descr->addr_low = paddr & 0xfffffff0;
279 descr->addr_high = ((u64) paddr >> 32) & 0x0fff;
280 descr->fb_offset = offset;
281 descr->seg_size = sg_dma_len(sgentry) >> 4;
282 descr->tile_mode = 0;
283 descr->next_desc_low = (nextdesc&0xfffffff0) | VIAFB_DMA_MAGIC;
284 descr->next_desc_high = ((u64) nextdesc >> 32) & 0x0fff;
285 descr->pad = 0xffffffff; /* VIA driver does this */
286 offset += sg_dma_len(sgentry);
287 nextdesc += sizeof(struct viafb_vx855_dma_descr);
290 descr[-1].next_desc_low = VIAFB_DMA_FINAL_SEGMENT|VIAFB_DMA_MAGIC;
292 * Program the engine.
294 spin_lock_irqsave(&global_dev.reg_lock, flags);
295 init_completion(&viafb_dma_completion);
296 viafb_mmio_write(VDMA_DQWCR0, 0);
297 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_DONE);
298 viafb_mmio_write(VDMA_MR0, VDMA_MR_TDIE | VDMA_MR_CHAIN);
299 viafb_mmio_write(VDMA_DPRL0, descr_handle | VIAFB_DMA_MAGIC);
300 viafb_mmio_write(VDMA_DPRH0,
301 (((u64)descr_handle >> 32) & 0x0fff) | 0xf0000);
302 (void) viafb_mmio_read(VDMA_CSR0);
303 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_START);
304 spin_unlock_irqrestore(&global_dev.reg_lock, flags);
306 * Now we just wait until the interrupt handler says
307 * we're done. Except that, actually, we need to wait a little
308 * longer: the interrupts seem to jump the gun a little and we
309 * get corrupted frames sometimes.
311 wait_for_completion_timeout(&viafb_dma_completion, 1);
313 if ((viafb_mmio_read(VDMA_CSR0)&VDMA_C_DONE) == 0)
314 printk(KERN_ERR "VIA DMA timeout!\n");
316 * Clean up and we're done.
318 viafb_mmio_write(VDMA_CSR0, VDMA_C_DONE);
319 viafb_mmio_write(VDMA_MR0, 0); /* Reset int enable */
320 mutex_unlock(&viafb_dma_lock);
321 dma_free_coherent(&global_dev.pdev->dev,
322 nsg*sizeof(struct viafb_vx855_dma_descr), descrpages,
326 EXPORT_SYMBOL_GPL(viafb_dma_copy_out_sg);
329 /* ---------------------------------------------------------------------- */
331 * Figure out how big our framebuffer memory is. Kind of ugly,
332 * but evidently we can't trust the information found in the
333 * fbdev configuration area.
335 static u16 via_function3[] = {
336 CLE266_FUNCTION3, KM400_FUNCTION3, CN400_FUNCTION3, CN700_FUNCTION3,
337 CX700_FUNCTION3, KM800_FUNCTION3, KM890_FUNCTION3, P4M890_FUNCTION3,
338 P4M900_FUNCTION3, VX800_FUNCTION3, VX855_FUNCTION3, VX900_FUNCTION3,
341 /* Get the BIOS-configured framebuffer size from PCI configuration space
342 * of function 3 in the respective chipset */
343 static int viafb_get_fb_size_from_pci(int chip_type)
350 /* search for the "FUNCTION3" device in this chipset */
351 for (i = 0; i < ARRAY_SIZE(via_function3); i++) {
352 struct pci_dev *pdev;
354 pdev = pci_get_device(PCI_VENDOR_ID_VIA, via_function3[i],
359 DEBUG_MSG(KERN_INFO "Device ID = %x\n", pdev->device);
361 switch (pdev->device) {
362 case CLE266_FUNCTION3:
363 case KM400_FUNCTION3:
366 case CN400_FUNCTION3:
367 case CN700_FUNCTION3:
368 case CX700_FUNCTION3:
369 case KM800_FUNCTION3:
370 case KM890_FUNCTION3:
371 case P4M890_FUNCTION3:
372 case P4M900_FUNCTION3:
373 case VX800_FUNCTION3:
374 case VX855_FUNCTION3:
375 case VX900_FUNCTION3:
376 /*case CN750_FUNCTION3: */
384 pci_read_config_dword(pdev, offset, &FBSize);
389 printk(KERN_ERR "cannot determine framebuffer size\n");
393 FBSize = FBSize & 0x00007000;
394 DEBUG_MSG(KERN_INFO "FB Size = %x\n", FBSize);
396 if (chip_type < UNICHROME_CX700) {
399 VideoMemSize = (16 << 20); /*16M */
403 VideoMemSize = (32 << 20); /*32M */
407 VideoMemSize = (64 << 20); /*64M */
411 VideoMemSize = (32 << 20); /*32M */
417 VideoMemSize = (8 << 20); /*8M */
421 VideoMemSize = (16 << 20); /*16M */
425 VideoMemSize = (32 << 20); /*32M */
429 VideoMemSize = (64 << 20); /*64M */
433 VideoMemSize = (128 << 20); /*128M */
437 VideoMemSize = (256 << 20); /*256M */
440 case 0x00007000: /* Only on VX855/875 */
441 VideoMemSize = (512 << 20); /*512M */
445 VideoMemSize = (32 << 20); /*32M */
455 * Figure out and map our MMIO regions.
457 static int __devinit via_pci_setup_mmio(struct viafb_dev *vdev)
461 * Hook up to the device registers. Note that we soldier
462 * on if it fails; the framebuffer can operate (without
463 * acceleration) without this region.
465 vdev->engine_start = pci_resource_start(vdev->pdev, 1);
466 vdev->engine_len = pci_resource_len(vdev->pdev, 1);
467 vdev->engine_mmio = ioremap_nocache(vdev->engine_start,
469 if (vdev->engine_mmio == NULL)
470 dev_err(&vdev->pdev->dev,
471 "Unable to map engine MMIO; operation will be "
472 "slow and crippled.\n");
474 * Map in framebuffer memory. For now, failure here is
475 * fatal. Unfortunately, in the absence of significant
476 * vmalloc space, failure here is also entirely plausible.
477 * Eventually we want to move away from mapping this
480 if (vdev->chip_type == UNICHROME_VX900)
481 vdev->fbmem_start = pci_resource_start(vdev->pdev, 2);
483 vdev->fbmem_start = pci_resource_start(vdev->pdev, 0);
484 ret = vdev->fbmem_len = viafb_get_fb_size_from_pci(vdev->chip_type);
487 vdev->fbmem = ioremap_nocache(vdev->fbmem_start, vdev->fbmem_len);
488 if (vdev->fbmem == NULL) {
494 iounmap(vdev->engine_mmio);
498 static void via_pci_teardown_mmio(struct viafb_dev *vdev)
500 iounmap(vdev->fbmem);
501 iounmap(vdev->engine_mmio);
505 * Create our subsidiary devices.
507 static struct viafb_subdev_info {
509 struct platform_device *platdev;
510 } viafb_subdevs[] = {
512 .name = "viafb-gpio",
518 #define N_SUBDEVS ARRAY_SIZE(viafb_subdevs)
520 static int __devinit via_create_subdev(struct viafb_dev *vdev,
521 struct viafb_subdev_info *info)
525 info->platdev = platform_device_alloc(info->name, -1);
526 if (!info->platdev) {
527 dev_err(&vdev->pdev->dev, "Unable to allocate pdev %s\n",
531 info->platdev->dev.parent = &vdev->pdev->dev;
532 info->platdev->dev.platform_data = vdev;
533 ret = platform_device_add(info->platdev);
535 dev_err(&vdev->pdev->dev, "Unable to add pdev %s\n",
537 platform_device_put(info->platdev);
538 info->platdev = NULL;
543 static int __devinit via_setup_subdevs(struct viafb_dev *vdev)
548 * Ignore return values. Even if some of the devices
549 * fail to be created, we'll still be able to use some
552 for (i = 0; i < N_SUBDEVS; i++)
553 via_create_subdev(vdev, viafb_subdevs + i);
557 static void via_teardown_subdevs(void)
561 for (i = 0; i < N_SUBDEVS; i++)
562 if (viafb_subdevs[i].platdev) {
563 viafb_subdevs[i].platdev->dev.platform_data = NULL;
564 platform_device_unregister(viafb_subdevs[i].platdev);
569 * Power management functions
572 static LIST_HEAD(viafb_pm_hooks);
573 static DEFINE_MUTEX(viafb_pm_hooks_lock);
575 void viafb_pm_register(struct viafb_pm_hooks *hooks)
577 INIT_LIST_HEAD(&hooks->list);
579 mutex_lock(&viafb_pm_hooks_lock);
580 list_add_tail(&hooks->list, &viafb_pm_hooks);
581 mutex_unlock(&viafb_pm_hooks_lock);
583 EXPORT_SYMBOL_GPL(viafb_pm_register);
585 void viafb_pm_unregister(struct viafb_pm_hooks *hooks)
587 mutex_lock(&viafb_pm_hooks_lock);
588 list_del(&hooks->list);
589 mutex_unlock(&viafb_pm_hooks_lock);
591 EXPORT_SYMBOL_GPL(viafb_pm_unregister);
593 static int via_suspend(struct pci_dev *pdev, pm_message_t state)
595 struct viafb_pm_hooks *hooks;
597 if (state.event != PM_EVENT_SUSPEND)
600 * "I've occasionally hit a few drivers that caused suspend
601 * failures, and each and every time it was a driver bug, and
602 * the right thing to do was to just ignore the error and suspend
603 * anyway - returning an error code and trying to undo the suspend
604 * is not what anybody ever really wants, even if our model
606 * -- Linus Torvalds, Dec. 7, 2009
608 mutex_lock(&viafb_pm_hooks_lock);
609 list_for_each_entry_reverse(hooks, &viafb_pm_hooks, list)
610 hooks->suspend(hooks->private);
611 mutex_unlock(&viafb_pm_hooks_lock);
613 pci_save_state(pdev);
614 pci_disable_device(pdev);
615 pci_set_power_state(pdev, pci_choose_state(pdev, state));
619 static int via_resume(struct pci_dev *pdev)
621 struct viafb_pm_hooks *hooks;
623 /* Get the bus side powered up */
624 pci_set_power_state(pdev, PCI_D0);
625 pci_restore_state(pdev);
626 if (pci_enable_device(pdev))
629 pci_set_master(pdev);
631 /* Now bring back any subdevs */
632 mutex_lock(&viafb_pm_hooks_lock);
633 list_for_each_entry(hooks, &viafb_pm_hooks, list)
634 hooks->resume(hooks->private);
635 mutex_unlock(&viafb_pm_hooks_lock);
639 #endif /* CONFIG_PM */
641 static int __devinit via_pci_probe(struct pci_dev *pdev,
642 const struct pci_device_id *ent)
646 ret = pci_enable_device(pdev);
651 * Global device initialization.
653 memset(&global_dev, 0, sizeof(global_dev));
654 global_dev.pdev = pdev;
655 global_dev.chip_type = ent->driver_data;
656 global_dev.port_cfg = adap_configs;
657 spin_lock_init(&global_dev.reg_lock);
658 ret = via_pci_setup_mmio(&global_dev);
662 * Set up interrupts and create our subdevices. Continue even if
666 via_setup_subdevs(&global_dev);
668 * Set up the framebuffer device
670 ret = via_fb_pci_probe(&global_dev);
676 via_teardown_subdevs();
677 via_pci_teardown_mmio(&global_dev);
679 pci_disable_device(pdev);
683 static void __devexit via_pci_remove(struct pci_dev *pdev)
685 via_teardown_subdevs();
686 via_fb_pci_remove(pdev);
687 via_pci_teardown_mmio(&global_dev);
688 pci_disable_device(pdev);
692 static struct pci_device_id via_pci_table[] __devinitdata = {
693 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CLE266_DID),
694 .driver_data = UNICHROME_CLE266 },
695 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K400_DID),
696 .driver_data = UNICHROME_K400 },
697 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K800_DID),
698 .driver_data = UNICHROME_K800 },
699 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_PM800_DID),
700 .driver_data = UNICHROME_PM800 },
701 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN700_DID),
702 .driver_data = UNICHROME_CN700 },
703 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CX700_DID),
704 .driver_data = UNICHROME_CX700 },
705 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN750_DID),
706 .driver_data = UNICHROME_CN750 },
707 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K8M890_DID),
708 .driver_data = UNICHROME_K8M890 },
709 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M890_DID),
710 .driver_data = UNICHROME_P4M890 },
711 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M900_DID),
712 .driver_data = UNICHROME_P4M900 },
713 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX800_DID),
714 .driver_data = UNICHROME_VX800 },
715 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX855_DID),
716 .driver_data = UNICHROME_VX855 },
717 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX900_DID),
718 .driver_data = UNICHROME_VX900 },
721 MODULE_DEVICE_TABLE(pci, via_pci_table);
723 static struct pci_driver via_driver = {
725 .id_table = via_pci_table,
726 .probe = via_pci_probe,
727 .remove = __devexit_p(via_pci_remove),
729 .suspend = via_suspend,
730 .resume = via_resume,
734 static int __init via_core_init(void)
743 return 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);