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.
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
19 * The default port config.
21 static struct via_port_cfg adap_configs[] = {
22 [VIA_PORT_26] = { VIA_PORT_I2C, VIA_MODE_OFF, VIASR, 0x26 },
23 [VIA_PORT_31] = { VIA_PORT_I2C, VIA_MODE_I2C, VIASR, 0x31 },
24 [VIA_PORT_25] = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x25 },
25 [VIA_PORT_2C] = { VIA_PORT_GPIO, VIA_MODE_I2C, VIASR, 0x2c },
26 [VIA_PORT_3D] = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x3d },
31 * We currently only support one viafb device (will there ever be
32 * more than one?), so just declare it globally here.
34 static struct viafb_dev global_dev;
38 * Figure out how big our framebuffer memory is. Kind of ugly,
39 * but evidently we can't trust the information found in the
40 * fbdev configuration area.
42 static u16 via_function3[] = {
43 CLE266_FUNCTION3, KM400_FUNCTION3, CN400_FUNCTION3, CN700_FUNCTION3,
44 CX700_FUNCTION3, KM800_FUNCTION3, KM890_FUNCTION3, P4M890_FUNCTION3,
45 P4M900_FUNCTION3, VX800_FUNCTION3, VX855_FUNCTION3,
48 /* Get the BIOS-configured framebuffer size from PCI configuration space
49 * of function 3 in the respective chipset */
50 static int viafb_get_fb_size_from_pci(int chip_type)
57 /* search for the "FUNCTION3" device in this chipset */
58 for (i = 0; i < ARRAY_SIZE(via_function3); i++) {
61 pdev = pci_get_device(PCI_VENDOR_ID_VIA, via_function3[i],
66 DEBUG_MSG(KERN_INFO "Device ID = %x\n", pdev->device);
68 switch (pdev->device) {
69 case CLE266_FUNCTION3:
78 case P4M890_FUNCTION3:
79 case P4M900_FUNCTION3:
82 /*case CN750_FUNCTION3: */
90 pci_read_config_dword(pdev, offset, &FBSize);
95 printk(KERN_ERR "cannot determine framebuffer size\n");
99 FBSize = FBSize & 0x00007000;
100 DEBUG_MSG(KERN_INFO "FB Size = %x\n", FBSize);
102 if (chip_type < UNICHROME_CX700) {
105 VideoMemSize = (16 << 20); /*16M */
109 VideoMemSize = (32 << 20); /*32M */
113 VideoMemSize = (64 << 20); /*64M */
117 VideoMemSize = (32 << 20); /*32M */
123 VideoMemSize = (8 << 20); /*8M */
127 VideoMemSize = (16 << 20); /*16M */
131 VideoMemSize = (32 << 20); /*32M */
135 VideoMemSize = (64 << 20); /*64M */
139 VideoMemSize = (128 << 20); /*128M */
143 VideoMemSize = (256 << 20); /*256M */
146 case 0x00007000: /* Only on VX855/875 */
147 VideoMemSize = (512 << 20); /*512M */
151 VideoMemSize = (32 << 20); /*32M */
161 * Figure out and map our MMIO regions.
163 static int __devinit via_pci_setup_mmio(struct viafb_dev *vdev)
166 * Hook up to the device registers.
168 vdev->engine_start = pci_resource_start(vdev->pdev, 1);
169 vdev->engine_len = pci_resource_len(vdev->pdev, 1);
170 /* If this fails, others will notice later */
171 vdev->engine_mmio = ioremap_nocache(vdev->engine_start,
175 * Likewise with I/O memory.
177 vdev->fbmem_start = pci_resource_start(vdev->pdev, 0);
178 vdev->fbmem_len = viafb_get_fb_size_from_pci(vdev->chip_type);
179 if (vdev->fbmem_len < 0)
180 return vdev->fbmem_len;
181 vdev->fbmem = ioremap_nocache(vdev->fbmem_start, vdev->fbmem_len);
182 if (vdev->fbmem == NULL)
187 static void __devexit via_pci_teardown_mmio(struct viafb_dev *vdev)
189 iounmap(vdev->fbmem);
190 iounmap(vdev->engine_mmio);
194 static int __devinit via_pci_probe(struct pci_dev *pdev,
195 const struct pci_device_id *ent)
199 ret = pci_enable_device(pdev);
203 * Global device initialization.
205 memset(&global_dev, 0, sizeof(global_dev));
206 global_dev.pdev = pdev;
207 global_dev.chip_type = ent->driver_data;
208 spin_lock_init(&global_dev.reg_lock);
209 ret = via_pci_setup_mmio(&global_dev);
213 * Create the I2C busses. Bailing out on failure seems extreme,
214 * but that's what the code did before.
216 ret = viafb_create_i2c_busses(&global_dev, adap_configs);
220 * Set up the framebuffer.
222 ret = via_fb_pci_probe(&global_dev);
226 * Create the GPIOs. We continue whether or not this succeeds;
227 * the framebuffer might be useful even without GPIO ports.
229 ret = viafb_create_gpios(&global_dev, adap_configs);
233 viafb_delete_i2c_busses();
235 via_pci_teardown_mmio(&global_dev);
237 pci_disable_device(pdev);
241 static void __devexit via_pci_remove(struct pci_dev *pdev)
243 viafb_destroy_gpios();
244 viafb_delete_i2c_busses();
245 via_fb_pci_remove(pdev);
246 via_pci_teardown_mmio(&global_dev);
247 pci_disable_device(pdev);
251 static struct pci_device_id via_pci_table[] __devinitdata = {
252 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CLE266_DID),
253 .driver_data = UNICHROME_CLE266 },
254 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_PM800_DID),
255 .driver_data = UNICHROME_PM800 },
256 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K400_DID),
257 .driver_data = UNICHROME_K400 },
258 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K800_DID),
259 .driver_data = UNICHROME_K800 },
260 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M890_DID),
261 .driver_data = UNICHROME_CN700 },
262 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K8M890_DID),
263 .driver_data = UNICHROME_K8M890 },
264 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CX700_DID),
265 .driver_data = UNICHROME_CX700 },
266 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M900_DID),
267 .driver_data = UNICHROME_P4M900 },
268 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN750_DID),
269 .driver_data = UNICHROME_CN750 },
270 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX800_DID),
271 .driver_data = UNICHROME_VX800 },
272 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX855_DID),
273 .driver_data = UNICHROME_VX855 },
276 MODULE_DEVICE_TABLE(pci, via_pci_table);
278 static struct pci_driver via_driver = {
280 .id_table = via_pci_table,
281 .probe = via_pci_probe,
282 .remove = __devexit_p(via_pci_remove),
285 static int __init via_core_init(void)
292 return pci_register_driver(&via_driver);
295 static void __exit via_core_exit(void)
297 pci_unregister_driver(&via_driver);
301 module_init(via_core_init);
302 module_exit(via_core_exit);