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 * Create our subsidiary devices.
196 static struct viafb_subdev_info {
198 struct platform_device *platdev;
199 } viafb_subdevs[] = {
201 .name = "viafb-gpio",
207 #define N_SUBDEVS ARRAY_SIZE(viafb_subdevs)
209 static int __devinit via_create_subdev(struct viafb_dev *vdev,
210 struct viafb_subdev_info *info)
214 info->platdev = platform_device_alloc(info->name, -1);
215 if (!info->platdev) {
216 dev_err(&vdev->pdev->dev, "Unable to allocate pdev %s\n",
220 info->platdev->dev.parent = &vdev->pdev->dev;
221 info->platdev->dev.platform_data = vdev;
222 ret = platform_device_add(info->platdev);
224 dev_err(&vdev->pdev->dev, "Unable to add pdev %s\n",
226 platform_device_put(info->platdev);
227 info->platdev = NULL;
232 static int __devinit via_setup_subdevs(struct viafb_dev *vdev)
237 * Ignore return values. Even if some of the devices
238 * fail to be created, we'll still be able to use some
241 for (i = 0; i < N_SUBDEVS; i++)
242 via_create_subdev(vdev, viafb_subdevs + i);
246 static void __devexit via_teardown_subdevs(void)
250 for (i = 0; i < N_SUBDEVS; i++)
251 if (viafb_subdevs[i].platdev) {
252 viafb_subdevs[i].platdev->dev.platform_data = NULL;
253 platform_device_unregister(viafb_subdevs[i].platdev);
258 static int __devinit via_pci_probe(struct pci_dev *pdev,
259 const struct pci_device_id *ent)
263 ret = pci_enable_device(pdev);
267 * Global device initialization.
269 memset(&global_dev, 0, sizeof(global_dev));
270 global_dev.pdev = pdev;
271 global_dev.chip_type = ent->driver_data;
272 global_dev.port_cfg = adap_configs;
273 spin_lock_init(&global_dev.reg_lock);
274 ret = via_pci_setup_mmio(&global_dev);
278 * Create our subdevices. Continue even if some things fail.
280 via_setup_subdevs(&global_dev);
282 * Set up the framebuffer.
284 ret = via_fb_pci_probe(&global_dev);
290 via_teardown_subdevs();
291 via_pci_teardown_mmio(&global_dev);
293 pci_disable_device(pdev);
297 static void __devexit via_pci_remove(struct pci_dev *pdev)
299 via_teardown_subdevs();
300 via_fb_pci_remove(pdev);
301 via_pci_teardown_mmio(&global_dev);
302 pci_disable_device(pdev);
306 static struct pci_device_id via_pci_table[] __devinitdata = {
307 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CLE266_DID),
308 .driver_data = UNICHROME_CLE266 },
309 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_PM800_DID),
310 .driver_data = UNICHROME_PM800 },
311 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K400_DID),
312 .driver_data = UNICHROME_K400 },
313 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K800_DID),
314 .driver_data = UNICHROME_K800 },
315 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M890_DID),
316 .driver_data = UNICHROME_CN700 },
317 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K8M890_DID),
318 .driver_data = UNICHROME_K8M890 },
319 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CX700_DID),
320 .driver_data = UNICHROME_CX700 },
321 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M900_DID),
322 .driver_data = UNICHROME_P4M900 },
323 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN750_DID),
324 .driver_data = UNICHROME_CN750 },
325 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX800_DID),
326 .driver_data = UNICHROME_VX800 },
327 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX855_DID),
328 .driver_data = UNICHROME_VX855 },
331 MODULE_DEVICE_TABLE(pci, via_pci_table);
333 static struct pci_driver via_driver = {
335 .id_table = via_pci_table,
336 .probe = via_pci_probe,
337 .remove = __devexit_p(via_pci_remove),
340 static int __init via_core_init(void)
349 return pci_register_driver(&via_driver);
352 static void __exit via_core_exit(void)
354 pci_unregister_driver(&via_driver);
360 module_init(via_core_init);
361 module_exit(via_core_exit);