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 * Basic register access; spinlock required.
40 static inline void viafb_mmio_write(int reg
, u32 v
)
42 iowrite32(v
, global_dev
.engine_mmio
+ reg
);
45 static inline int viafb_mmio_read(int reg
)
47 return ioread32(global_dev
.engine_mmio
+ reg
);
50 /* ---------------------------------------------------------------------- */
52 * Interrupt management. We have a single IRQ line for a lot of
53 * different functions, so we need to share it. The design here
54 * is that we don't want to reimplement the shared IRQ code here;
55 * we also want to avoid having contention for a single handler thread.
56 * So each subdev driver which needs interrupts just requests
57 * them directly from the kernel. We just have what's needed for
58 * overall access to the interrupt control register.
62 * Which interrupts are enabled now?
64 static u32 viafb_enabled_ints
;
66 static void viafb_int_init(void)
68 viafb_enabled_ints
= 0;
70 viafb_mmio_write(VDE_INTERRUPT
, 0);
74 * Allow subdevs to ask for specific interrupts to be enabled. These
75 * functions must be called with reg_lock held
77 void viafb_irq_enable(u32 mask
)
79 viafb_enabled_ints
|= mask
;
80 viafb_mmio_write(VDE_INTERRUPT
, viafb_enabled_ints
| VDE_I_ENABLE
);
82 EXPORT_SYMBOL_GPL(viafb_irq_enable
);
84 void viafb_irq_disable(u32 mask
)
86 viafb_enabled_ints
&= ~mask
;
87 if (viafb_enabled_ints
== 0)
88 viafb_mmio_write(VDE_INTERRUPT
, 0); /* Disable entirely */
90 viafb_mmio_write(VDE_INTERRUPT
,
91 viafb_enabled_ints
| VDE_I_ENABLE
);
93 EXPORT_SYMBOL_GPL(viafb_irq_disable
);
97 * Figure out how big our framebuffer memory is. Kind of ugly,
98 * but evidently we can't trust the information found in the
99 * fbdev configuration area.
101 static u16 via_function3
[] = {
102 CLE266_FUNCTION3
, KM400_FUNCTION3
, CN400_FUNCTION3
, CN700_FUNCTION3
,
103 CX700_FUNCTION3
, KM800_FUNCTION3
, KM890_FUNCTION3
, P4M890_FUNCTION3
,
104 P4M900_FUNCTION3
, VX800_FUNCTION3
, VX855_FUNCTION3
,
107 /* Get the BIOS-configured framebuffer size from PCI configuration space
108 * of function 3 in the respective chipset */
109 static int viafb_get_fb_size_from_pci(int chip_type
)
116 /* search for the "FUNCTION3" device in this chipset */
117 for (i
= 0; i
< ARRAY_SIZE(via_function3
); i
++) {
118 struct pci_dev
*pdev
;
120 pdev
= pci_get_device(PCI_VENDOR_ID_VIA
, via_function3
[i
],
125 DEBUG_MSG(KERN_INFO
"Device ID = %x\n", pdev
->device
);
127 switch (pdev
->device
) {
128 case CLE266_FUNCTION3
:
129 case KM400_FUNCTION3
:
132 case CN400_FUNCTION3
:
133 case CN700_FUNCTION3
:
134 case CX700_FUNCTION3
:
135 case KM800_FUNCTION3
:
136 case KM890_FUNCTION3
:
137 case P4M890_FUNCTION3
:
138 case P4M900_FUNCTION3
:
139 case VX800_FUNCTION3
:
140 case VX855_FUNCTION3
:
141 /*case CN750_FUNCTION3: */
149 pci_read_config_dword(pdev
, offset
, &FBSize
);
154 printk(KERN_ERR
"cannot determine framebuffer size\n");
158 FBSize
= FBSize
& 0x00007000;
159 DEBUG_MSG(KERN_INFO
"FB Size = %x\n", FBSize
);
161 if (chip_type
< UNICHROME_CX700
) {
164 VideoMemSize
= (16 << 20); /*16M */
168 VideoMemSize
= (32 << 20); /*32M */
172 VideoMemSize
= (64 << 20); /*64M */
176 VideoMemSize
= (32 << 20); /*32M */
182 VideoMemSize
= (8 << 20); /*8M */
186 VideoMemSize
= (16 << 20); /*16M */
190 VideoMemSize
= (32 << 20); /*32M */
194 VideoMemSize
= (64 << 20); /*64M */
198 VideoMemSize
= (128 << 20); /*128M */
202 VideoMemSize
= (256 << 20); /*256M */
205 case 0x00007000: /* Only on VX855/875 */
206 VideoMemSize
= (512 << 20); /*512M */
210 VideoMemSize
= (32 << 20); /*32M */
220 * Figure out and map our MMIO regions.
222 static int __devinit
via_pci_setup_mmio(struct viafb_dev
*vdev
)
225 * Hook up to the device registers.
227 vdev
->engine_start
= pci_resource_start(vdev
->pdev
, 1);
228 vdev
->engine_len
= pci_resource_len(vdev
->pdev
, 1);
229 /* If this fails, others will notice later */
230 vdev
->engine_mmio
= ioremap_nocache(vdev
->engine_start
,
234 * Likewise with I/O memory.
236 vdev
->fbmem_start
= pci_resource_start(vdev
->pdev
, 0);
237 vdev
->fbmem_len
= viafb_get_fb_size_from_pci(vdev
->chip_type
);
238 if (vdev
->fbmem_len
< 0)
239 return vdev
->fbmem_len
;
240 vdev
->fbmem
= ioremap_nocache(vdev
->fbmem_start
, vdev
->fbmem_len
);
241 if (vdev
->fbmem
== NULL
)
246 static void __devexit
via_pci_teardown_mmio(struct viafb_dev
*vdev
)
248 iounmap(vdev
->fbmem
);
249 iounmap(vdev
->engine_mmio
);
253 * Create our subsidiary devices.
255 static struct viafb_subdev_info
{
257 struct platform_device
*platdev
;
258 } viafb_subdevs
[] = {
260 .name
= "viafb-gpio",
266 #define N_SUBDEVS ARRAY_SIZE(viafb_subdevs)
268 static int __devinit
via_create_subdev(struct viafb_dev
*vdev
,
269 struct viafb_subdev_info
*info
)
273 info
->platdev
= platform_device_alloc(info
->name
, -1);
274 if (!info
->platdev
) {
275 dev_err(&vdev
->pdev
->dev
, "Unable to allocate pdev %s\n",
279 info
->platdev
->dev
.parent
= &vdev
->pdev
->dev
;
280 info
->platdev
->dev
.platform_data
= vdev
;
281 ret
= platform_device_add(info
->platdev
);
283 dev_err(&vdev
->pdev
->dev
, "Unable to add pdev %s\n",
285 platform_device_put(info
->platdev
);
286 info
->platdev
= NULL
;
291 static int __devinit
via_setup_subdevs(struct viafb_dev
*vdev
)
296 * Ignore return values. Even if some of the devices
297 * fail to be created, we'll still be able to use some
300 for (i
= 0; i
< N_SUBDEVS
; i
++)
301 via_create_subdev(vdev
, viafb_subdevs
+ i
);
305 static void __devexit
via_teardown_subdevs(void)
309 for (i
= 0; i
< N_SUBDEVS
; i
++)
310 if (viafb_subdevs
[i
].platdev
) {
311 viafb_subdevs
[i
].platdev
->dev
.platform_data
= NULL
;
312 platform_device_unregister(viafb_subdevs
[i
].platdev
);
317 static int __devinit
via_pci_probe(struct pci_dev
*pdev
,
318 const struct pci_device_id
*ent
)
322 ret
= pci_enable_device(pdev
);
326 * Global device initialization.
328 memset(&global_dev
, 0, sizeof(global_dev
));
329 global_dev
.pdev
= pdev
;
330 global_dev
.chip_type
= ent
->driver_data
;
331 global_dev
.port_cfg
= adap_configs
;
332 spin_lock_init(&global_dev
.reg_lock
);
333 ret
= via_pci_setup_mmio(&global_dev
);
337 * Set up interrupts and create our subdevices. Continue even if
341 via_setup_subdevs(&global_dev
);
343 * Set up the framebuffer.
345 ret
= via_fb_pci_probe(&global_dev
);
352 via_teardown_subdevs();
353 via_pci_teardown_mmio(&global_dev
);
355 pci_disable_device(pdev
);
359 static void __devexit
via_pci_remove(struct pci_dev
*pdev
)
361 via_teardown_subdevs();
362 via_fb_pci_remove(pdev
);
363 via_pci_teardown_mmio(&global_dev
);
364 pci_disable_device(pdev
);
368 static struct pci_device_id via_pci_table
[] __devinitdata
= {
369 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_CLE266_DID
),
370 .driver_data
= UNICHROME_CLE266
},
371 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_PM800_DID
),
372 .driver_data
= UNICHROME_PM800
},
373 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_K400_DID
),
374 .driver_data
= UNICHROME_K400
},
375 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_K800_DID
),
376 .driver_data
= UNICHROME_K800
},
377 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_P4M890_DID
),
378 .driver_data
= UNICHROME_CN700
},
379 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_K8M890_DID
),
380 .driver_data
= UNICHROME_K8M890
},
381 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_CX700_DID
),
382 .driver_data
= UNICHROME_CX700
},
383 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_P4M900_DID
),
384 .driver_data
= UNICHROME_P4M900
},
385 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_CN750_DID
),
386 .driver_data
= UNICHROME_CN750
},
387 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_VX800_DID
),
388 .driver_data
= UNICHROME_VX800
},
389 { PCI_DEVICE(PCI_VENDOR_ID_VIA
, UNICHROME_VX855_DID
),
390 .driver_data
= UNICHROME_VX855
},
393 MODULE_DEVICE_TABLE(pci
, via_pci_table
);
395 static struct pci_driver via_driver
= {
397 .id_table
= via_pci_table
,
398 .probe
= via_pci_probe
,
399 .remove
= __devexit_p(via_pci_remove
),
402 static int __init
via_core_init(void)
411 return pci_register_driver(&via_driver
);
414 static void __exit
via_core_exit(void)
416 pci_unregister_driver(&via_driver
);
422 module_init(via_core_init
);
423 module_exit(via_core_exit
);