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>
23 * The default port config.
25 static struct via_port_cfg adap_configs[] = {
26 [VIA_PORT_26] = { VIA_PORT_I2C, VIA_MODE_I2C, VIASR, 0x26 },
27 [VIA_PORT_31] = { VIA_PORT_I2C, VIA_MODE_I2C, VIASR, 0x31 },
28 [VIA_PORT_25] = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x25 },
29 [VIA_PORT_2C] = { VIA_PORT_GPIO, VIA_MODE_I2C, VIASR, 0x2c },
30 [VIA_PORT_3D] = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x3d },
35 * The OLPC XO-1.5 puts the camera power and reset lines onto
38 static const struct via_port_cfg olpc_adap_configs[] = {
39 [VIA_PORT_26] = { VIA_PORT_I2C, VIA_MODE_I2C, VIASR, 0x26 },
40 [VIA_PORT_31] = { VIA_PORT_I2C, VIA_MODE_I2C, VIASR, 0x31 },
41 [VIA_PORT_25] = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x25 },
42 [VIA_PORT_2C] = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x2c },
43 [VIA_PORT_3D] = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x3d },
48 * We currently only support one viafb device (will there ever be
49 * more than one?), so just declare it globally here.
51 static struct viafb_dev global_dev;
55 * Basic register access; spinlock required.
57 static inline void viafb_mmio_write(int reg, u32 v)
59 iowrite32(v, global_dev.engine_mmio + reg);
62 static inline int viafb_mmio_read(int reg)
64 return ioread32(global_dev.engine_mmio + reg);
67 /* ---------------------------------------------------------------------- */
69 * Interrupt management. We have a single IRQ line for a lot of
70 * different functions, so we need to share it. The design here
71 * is that we don't want to reimplement the shared IRQ code here;
72 * we also want to avoid having contention for a single handler thread.
73 * So each subdev driver which needs interrupts just requests
74 * them directly from the kernel. We just have what's needed for
75 * overall access to the interrupt control register.
79 * Which interrupts are enabled now?
81 static u32 viafb_enabled_ints;
83 static void __devinit viafb_int_init(void)
85 viafb_enabled_ints = 0;
87 viafb_mmio_write(VDE_INTERRUPT, 0);
91 * Allow subdevs to ask for specific interrupts to be enabled. These
92 * functions must be called with reg_lock held
94 void viafb_irq_enable(u32 mask)
96 viafb_enabled_ints |= mask;
97 viafb_mmio_write(VDE_INTERRUPT, viafb_enabled_ints | VDE_I_ENABLE);
99 EXPORT_SYMBOL_GPL(viafb_irq_enable);
101 void viafb_irq_disable(u32 mask)
103 viafb_enabled_ints &= ~mask;
104 if (viafb_enabled_ints == 0)
105 viafb_mmio_write(VDE_INTERRUPT, 0); /* Disable entirely */
107 viafb_mmio_write(VDE_INTERRUPT,
108 viafb_enabled_ints | VDE_I_ENABLE);
110 EXPORT_SYMBOL_GPL(viafb_irq_disable);
112 /* ---------------------------------------------------------------------- */
114 * Access to the DMA engine. This currently provides what the camera
115 * driver needs (i.e. outgoing only) but is easily expandable if need
120 * There are four DMA channels in the vx855. For now, we only
121 * use one of them, though. Most of the time, the DMA channel
122 * will be idle, so we keep the IRQ handler unregistered except
123 * when some subsystem has indicated an interest.
125 static int viafb_dma_users;
126 static DECLARE_COMPLETION(viafb_dma_completion);
128 * This mutex protects viafb_dma_users and our global interrupt
129 * registration state; it also serializes access to the DMA
132 static DEFINE_MUTEX(viafb_dma_lock);
135 * The VX855 DMA descriptor (used for s/g transfers) looks
138 struct viafb_vx855_dma_descr {
139 u32 addr_low; /* Low part of phys addr */
140 u32 addr_high; /* High 12 bits of addr */
141 u32 fb_offset; /* Offset into FB memory */
142 u32 seg_size; /* Size, 16-byte units */
143 u32 tile_mode; /* "tile mode" setting */
144 u32 next_desc_low; /* Next descriptor addr */
146 u32 pad; /* Fill out to 64 bytes */
150 * Flags added to the "next descriptor low" pointers
152 #define VIAFB_DMA_MAGIC 0x01 /* ??? Just has to be there */
153 #define VIAFB_DMA_FINAL_SEGMENT 0x02 /* Final segment */
156 * The completion IRQ handler.
158 static irqreturn_t viafb_dma_irq(int irq, void *data)
161 irqreturn_t ret = IRQ_NONE;
163 spin_lock(&global_dev.reg_lock);
164 csr = viafb_mmio_read(VDMA_CSR0);
165 if (csr & VDMA_C_DONE) {
166 viafb_mmio_write(VDMA_CSR0, VDMA_C_DONE);
167 complete(&viafb_dma_completion);
170 spin_unlock(&global_dev.reg_lock);
175 * Indicate a need for DMA functionality.
177 int viafb_request_dma(void)
182 * Only VX855 is supported currently.
184 if (global_dev.chip_type != UNICHROME_VX855)
187 * Note the new user and set up our interrupt handler
190 mutex_lock(&viafb_dma_lock);
192 if (viafb_dma_users == 1) {
193 ret = request_irq(global_dev.pdev->irq, viafb_dma_irq,
194 IRQF_SHARED, "via-dma", &viafb_dma_users);
198 viafb_irq_enable(VDE_I_DMA0TDEN);
200 mutex_unlock(&viafb_dma_lock);
203 EXPORT_SYMBOL_GPL(viafb_request_dma);
205 void viafb_release_dma(void)
207 mutex_lock(&viafb_dma_lock);
209 if (viafb_dma_users == 0) {
210 viafb_irq_disable(VDE_I_DMA0TDEN);
211 free_irq(global_dev.pdev->irq, &viafb_dma_users);
213 mutex_unlock(&viafb_dma_lock);
215 EXPORT_SYMBOL_GPL(viafb_release_dma);
220 * Copy a single buffer from FB memory, synchronously. This code works
221 * but is not currently used.
223 void viafb_dma_copy_out(unsigned int offset, dma_addr_t paddr, int len)
228 mutex_lock(&viafb_dma_lock);
229 init_completion(&viafb_dma_completion);
231 * Program the controller.
233 spin_lock_irqsave(&global_dev.reg_lock, flags);
234 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_DONE);
235 /* Enable ints; must happen after CSR0 write! */
236 viafb_mmio_write(VDMA_MR0, VDMA_MR_TDIE);
237 viafb_mmio_write(VDMA_MARL0, (int) (paddr & 0xfffffff0));
238 viafb_mmio_write(VDMA_MARH0, (int) ((paddr >> 28) & 0xfff));
239 /* Data sheet suggests DAR0 should be <<4, but it lies */
240 viafb_mmio_write(VDMA_DAR0, offset);
241 viafb_mmio_write(VDMA_DQWCR0, len >> 4);
242 viafb_mmio_write(VDMA_TMR0, 0);
243 viafb_mmio_write(VDMA_DPRL0, 0);
244 viafb_mmio_write(VDMA_DPRH0, 0);
245 viafb_mmio_write(VDMA_PMR0, 0);
246 csr = viafb_mmio_read(VDMA_CSR0);
247 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_START);
248 spin_unlock_irqrestore(&global_dev.reg_lock, flags);
250 * Now we just wait until the interrupt handler says
253 wait_for_completion_interruptible(&viafb_dma_completion);
254 viafb_mmio_write(VDMA_MR0, 0); /* Reset int enable */
255 mutex_unlock(&viafb_dma_lock);
257 EXPORT_SYMBOL_GPL(viafb_dma_copy_out);
261 * Do a scatter/gather DMA copy from FB memory. You must have done
262 * a successful call to viafb_request_dma() first.
264 int viafb_dma_copy_out_sg(unsigned int offset, struct scatterlist *sg, int nsg)
266 struct viafb_vx855_dma_descr *descr;
268 dma_addr_t descr_handle;
271 struct scatterlist *sgentry;
275 * Get a place to put the descriptors.
277 descrpages = dma_alloc_coherent(&global_dev.pdev->dev,
278 nsg*sizeof(struct viafb_vx855_dma_descr),
279 &descr_handle, GFP_KERNEL);
280 if (descrpages == NULL) {
281 dev_err(&global_dev.pdev->dev, "Unable to get descr page.\n");
284 mutex_lock(&viafb_dma_lock);
289 nextdesc = descr_handle + sizeof(struct viafb_vx855_dma_descr);
290 for_each_sg(sg, sgentry, nsg, i) {
291 dma_addr_t paddr = sg_dma_address(sgentry);
292 descr->addr_low = paddr & 0xfffffff0;
293 descr->addr_high = ((u64) paddr >> 32) & 0x0fff;
294 descr->fb_offset = offset;
295 descr->seg_size = sg_dma_len(sgentry) >> 4;
296 descr->tile_mode = 0;
297 descr->next_desc_low = (nextdesc&0xfffffff0) | VIAFB_DMA_MAGIC;
298 descr->next_desc_high = ((u64) nextdesc >> 32) & 0x0fff;
299 descr->pad = 0xffffffff; /* VIA driver does this */
300 offset += sg_dma_len(sgentry);
301 nextdesc += sizeof(struct viafb_vx855_dma_descr);
304 descr[-1].next_desc_low = VIAFB_DMA_FINAL_SEGMENT|VIAFB_DMA_MAGIC;
306 * Program the engine.
308 spin_lock_irqsave(&global_dev.reg_lock, flags);
309 init_completion(&viafb_dma_completion);
310 viafb_mmio_write(VDMA_DQWCR0, 0);
311 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_DONE);
312 viafb_mmio_write(VDMA_MR0, VDMA_MR_TDIE | VDMA_MR_CHAIN);
313 viafb_mmio_write(VDMA_DPRL0, descr_handle | VIAFB_DMA_MAGIC);
314 viafb_mmio_write(VDMA_DPRH0,
315 (((u64)descr_handle >> 32) & 0x0fff) | 0xf0000);
316 (void) viafb_mmio_read(VDMA_CSR0);
317 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_START);
318 spin_unlock_irqrestore(&global_dev.reg_lock, flags);
320 * Now we just wait until the interrupt handler says
321 * we're done. Except that, actually, we need to wait a little
322 * longer: the interrupts seem to jump the gun a little and we
323 * get corrupted frames sometimes.
325 wait_for_completion_timeout(&viafb_dma_completion, 1);
327 if ((viafb_mmio_read(VDMA_CSR0)&VDMA_C_DONE) == 0)
328 printk(KERN_ERR "VIA DMA timeout!\n");
330 * Clean up and we're done.
332 viafb_mmio_write(VDMA_CSR0, VDMA_C_DONE);
333 viafb_mmio_write(VDMA_MR0, 0); /* Reset int enable */
334 mutex_unlock(&viafb_dma_lock);
335 dma_free_coherent(&global_dev.pdev->dev,
336 nsg*sizeof(struct viafb_vx855_dma_descr), descrpages,
340 EXPORT_SYMBOL_GPL(viafb_dma_copy_out_sg);
343 /* ---------------------------------------------------------------------- */
345 * Figure out how big our framebuffer memory is. Kind of ugly,
346 * but evidently we can't trust the information found in the
347 * fbdev configuration area.
349 static u16 via_function3[] = {
350 CLE266_FUNCTION3, KM400_FUNCTION3, CN400_FUNCTION3, CN700_FUNCTION3,
351 CX700_FUNCTION3, KM800_FUNCTION3, KM890_FUNCTION3, P4M890_FUNCTION3,
352 P4M900_FUNCTION3, VX800_FUNCTION3, VX855_FUNCTION3, VX900_FUNCTION3,
355 /* Get the BIOS-configured framebuffer size from PCI configuration space
356 * of function 3 in the respective chipset */
357 static int viafb_get_fb_size_from_pci(int chip_type)
364 /* search for the "FUNCTION3" device in this chipset */
365 for (i = 0; i < ARRAY_SIZE(via_function3); i++) {
366 struct pci_dev *pdev;
368 pdev = pci_get_device(PCI_VENDOR_ID_VIA, via_function3[i],
373 DEBUG_MSG(KERN_INFO "Device ID = %x\n", pdev->device);
375 switch (pdev->device) {
376 case CLE266_FUNCTION3:
377 case KM400_FUNCTION3:
380 case CN400_FUNCTION3:
381 case CN700_FUNCTION3:
382 case CX700_FUNCTION3:
383 case KM800_FUNCTION3:
384 case KM890_FUNCTION3:
385 case P4M890_FUNCTION3:
386 case P4M900_FUNCTION3:
387 case VX800_FUNCTION3:
388 case VX855_FUNCTION3:
389 case VX900_FUNCTION3:
390 /*case CN750_FUNCTION3: */
398 pci_read_config_dword(pdev, offset, &FBSize);
403 printk(KERN_ERR "cannot determine framebuffer size\n");
407 FBSize = FBSize & 0x00007000;
408 DEBUG_MSG(KERN_INFO "FB Size = %x\n", FBSize);
410 if (chip_type < UNICHROME_CX700) {
413 VideoMemSize = (16 << 20); /*16M */
417 VideoMemSize = (32 << 20); /*32M */
421 VideoMemSize = (64 << 20); /*64M */
425 VideoMemSize = (32 << 20); /*32M */
431 VideoMemSize = (8 << 20); /*8M */
435 VideoMemSize = (16 << 20); /*16M */
439 VideoMemSize = (32 << 20); /*32M */
443 VideoMemSize = (64 << 20); /*64M */
447 VideoMemSize = (128 << 20); /*128M */
451 VideoMemSize = (256 << 20); /*256M */
454 case 0x00007000: /* Only on VX855/875 */
455 VideoMemSize = (512 << 20); /*512M */
459 VideoMemSize = (32 << 20); /*32M */
469 * Figure out and map our MMIO regions.
471 static int __devinit via_pci_setup_mmio(struct viafb_dev *vdev)
475 * Hook up to the device registers. Note that we soldier
476 * on if it fails; the framebuffer can operate (without
477 * acceleration) without this region.
479 vdev->engine_start = pci_resource_start(vdev->pdev, 1);
480 vdev->engine_len = pci_resource_len(vdev->pdev, 1);
481 vdev->engine_mmio = ioremap_nocache(vdev->engine_start,
483 if (vdev->engine_mmio == NULL)
484 dev_err(&vdev->pdev->dev,
485 "Unable to map engine MMIO; operation will be "
486 "slow and crippled.\n");
488 * Map in framebuffer memory. For now, failure here is
489 * fatal. Unfortunately, in the absence of significant
490 * vmalloc space, failure here is also entirely plausible.
491 * Eventually we want to move away from mapping this
494 if (vdev->chip_type == UNICHROME_VX900)
495 vdev->fbmem_start = pci_resource_start(vdev->pdev, 2);
497 vdev->fbmem_start = pci_resource_start(vdev->pdev, 0);
498 ret = vdev->fbmem_len = viafb_get_fb_size_from_pci(vdev->chip_type);
501 vdev->fbmem = ioremap_nocache(vdev->fbmem_start, vdev->fbmem_len);
502 if (vdev->fbmem == NULL) {
508 iounmap(vdev->engine_mmio);
512 static void via_pci_teardown_mmio(struct viafb_dev *vdev)
514 iounmap(vdev->fbmem);
515 iounmap(vdev->engine_mmio);
519 * Create our subsidiary devices.
521 static struct viafb_subdev_info {
523 struct platform_device *platdev;
524 } viafb_subdevs[] = {
526 .name = "viafb-gpio",
532 #define N_SUBDEVS ARRAY_SIZE(viafb_subdevs)
534 static int __devinit via_create_subdev(struct viafb_dev *vdev,
535 struct viafb_subdev_info *info)
539 info->platdev = platform_device_alloc(info->name, -1);
540 if (!info->platdev) {
541 dev_err(&vdev->pdev->dev, "Unable to allocate pdev %s\n",
545 info->platdev->dev.parent = &vdev->pdev->dev;
546 info->platdev->dev.platform_data = vdev;
547 ret = platform_device_add(info->platdev);
549 dev_err(&vdev->pdev->dev, "Unable to add pdev %s\n",
551 platform_device_put(info->platdev);
552 info->platdev = NULL;
557 static int __devinit via_setup_subdevs(struct viafb_dev *vdev)
562 * Ignore return values. Even if some of the devices
563 * fail to be created, we'll still be able to use some
566 for (i = 0; i < N_SUBDEVS; i++)
567 via_create_subdev(vdev, viafb_subdevs + i);
571 static void via_teardown_subdevs(void)
575 for (i = 0; i < N_SUBDEVS; i++)
576 if (viafb_subdevs[i].platdev) {
577 viafb_subdevs[i].platdev->dev.platform_data = NULL;
578 platform_device_unregister(viafb_subdevs[i].platdev);
583 * Power management functions
586 static LIST_HEAD(viafb_pm_hooks);
587 static DEFINE_MUTEX(viafb_pm_hooks_lock);
589 void viafb_pm_register(struct viafb_pm_hooks *hooks)
591 INIT_LIST_HEAD(&hooks->list);
593 mutex_lock(&viafb_pm_hooks_lock);
594 list_add_tail(&hooks->list, &viafb_pm_hooks);
595 mutex_unlock(&viafb_pm_hooks_lock);
597 EXPORT_SYMBOL_GPL(viafb_pm_register);
599 void viafb_pm_unregister(struct viafb_pm_hooks *hooks)
601 mutex_lock(&viafb_pm_hooks_lock);
602 list_del(&hooks->list);
603 mutex_unlock(&viafb_pm_hooks_lock);
605 EXPORT_SYMBOL_GPL(viafb_pm_unregister);
607 static int via_suspend(struct pci_dev *pdev, pm_message_t state)
609 struct viafb_pm_hooks *hooks;
611 if (state.event != PM_EVENT_SUSPEND)
614 * "I've occasionally hit a few drivers that caused suspend
615 * failures, and each and every time it was a driver bug, and
616 * the right thing to do was to just ignore the error and suspend
617 * anyway - returning an error code and trying to undo the suspend
618 * is not what anybody ever really wants, even if our model
620 * -- Linus Torvalds, Dec. 7, 2009
622 mutex_lock(&viafb_pm_hooks_lock);
623 list_for_each_entry_reverse(hooks, &viafb_pm_hooks, list)
624 hooks->suspend(hooks->private);
625 mutex_unlock(&viafb_pm_hooks_lock);
627 pci_save_state(pdev);
628 pci_disable_device(pdev);
629 pci_set_power_state(pdev, pci_choose_state(pdev, state));
633 static int via_resume(struct pci_dev *pdev)
635 struct viafb_pm_hooks *hooks;
637 /* Get the bus side powered up */
638 pci_set_power_state(pdev, PCI_D0);
639 pci_restore_state(pdev);
640 if (pci_enable_device(pdev))
643 pci_set_master(pdev);
645 /* Now bring back any subdevs */
646 mutex_lock(&viafb_pm_hooks_lock);
647 list_for_each_entry(hooks, &viafb_pm_hooks, list)
648 hooks->resume(hooks->private);
649 mutex_unlock(&viafb_pm_hooks_lock);
653 #endif /* CONFIG_PM */
655 static int __devinit via_pci_probe(struct pci_dev *pdev,
656 const struct pci_device_id *ent)
660 ret = pci_enable_device(pdev);
665 * Global device initialization.
667 memset(&global_dev, 0, sizeof(global_dev));
668 global_dev.pdev = pdev;
669 global_dev.chip_type = ent->driver_data;
670 global_dev.port_cfg = adap_configs;
671 if (machine_is_olpc())
672 global_dev.port_cfg = olpc_adap_configs;
674 spin_lock_init(&global_dev.reg_lock);
675 ret = via_pci_setup_mmio(&global_dev);
679 * Set up interrupts and create our subdevices. Continue even if
683 via_setup_subdevs(&global_dev);
685 * Set up the framebuffer device
687 ret = via_fb_pci_probe(&global_dev);
693 via_teardown_subdevs();
694 via_pci_teardown_mmio(&global_dev);
696 pci_disable_device(pdev);
700 static void __devexit via_pci_remove(struct pci_dev *pdev)
702 via_teardown_subdevs();
703 via_fb_pci_remove(pdev);
704 via_pci_teardown_mmio(&global_dev);
705 pci_disable_device(pdev);
709 static struct pci_device_id via_pci_table[] __devinitdata = {
710 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CLE266_DID),
711 .driver_data = UNICHROME_CLE266 },
712 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K400_DID),
713 .driver_data = UNICHROME_K400 },
714 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K800_DID),
715 .driver_data = UNICHROME_K800 },
716 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_PM800_DID),
717 .driver_data = UNICHROME_PM800 },
718 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN700_DID),
719 .driver_data = UNICHROME_CN700 },
720 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CX700_DID),
721 .driver_data = UNICHROME_CX700 },
722 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN750_DID),
723 .driver_data = UNICHROME_CN750 },
724 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K8M890_DID),
725 .driver_data = UNICHROME_K8M890 },
726 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M890_DID),
727 .driver_data = UNICHROME_P4M890 },
728 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M900_DID),
729 .driver_data = UNICHROME_P4M900 },
730 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX800_DID),
731 .driver_data = UNICHROME_VX800 },
732 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX855_DID),
733 .driver_data = UNICHROME_VX855 },
734 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX900_DID),
735 .driver_data = UNICHROME_VX900 },
738 MODULE_DEVICE_TABLE(pci, via_pci_table);
740 static struct pci_driver via_driver = {
742 .id_table = via_pci_table,
743 .probe = via_pci_probe,
744 .remove = __devexit_p(via_pci_remove),
746 .suspend = via_suspend,
747 .resume = via_resume,
751 static int __init via_core_init(void)
760 return pci_register_driver(&via_driver);
763 static void __exit via_core_exit(void)
765 pci_unregister_driver(&via_driver);
771 module_init(via_core_init);
772 module_exit(via_core_exit);