1
2
3
4
5
6
7
8
9
10
11#include <linux/via-core.h>
12#include <linux/via_i2c.h>
13#include <linux/via-gpio.h>
14#include "global.h"
15
16#include <linux/module.h>
17#include <linux/interrupt.h>
18#include <linux/platform_device.h>
19#include <linux/list.h>
20#include <linux/pm.h>
21
22
23
24
25static 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 },
31 { 0, 0, 0, 0 }
32};
33
34
35
36
37
38static 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 },
44 { 0, 0, 0, 0 }
45};
46
47
48
49
50
51static struct viafb_dev global_dev;
52
53
54
55
56
57static inline void viafb_mmio_write(int reg, u32 v)
58{
59 iowrite32(v, global_dev.engine_mmio + reg);
60}
61
62static inline int viafb_mmio_read(int reg)
63{
64 return ioread32(global_dev.engine_mmio + reg);
65}
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81static u32 viafb_enabled_ints;
82
83static void viafb_int_init(void)
84{
85 viafb_enabled_ints = 0;
86
87 viafb_mmio_write(VDE_INTERRUPT, 0);
88}
89
90
91
92
93
94void viafb_irq_enable(u32 mask)
95{
96 viafb_enabled_ints |= mask;
97 viafb_mmio_write(VDE_INTERRUPT, viafb_enabled_ints | VDE_I_ENABLE);
98}
99EXPORT_SYMBOL_GPL(viafb_irq_enable);
100
101void viafb_irq_disable(u32 mask)
102{
103 viafb_enabled_ints &= ~mask;
104 if (viafb_enabled_ints == 0)
105 viafb_mmio_write(VDE_INTERRUPT, 0);
106 else
107 viafb_mmio_write(VDE_INTERRUPT,
108 viafb_enabled_ints | VDE_I_ENABLE);
109}
110EXPORT_SYMBOL_GPL(viafb_irq_disable);
111
112
113
114
115
116
117
118
119#if IS_ENABLED(CONFIG_VIDEO_VIA_CAMERA)
120
121
122
123
124
125
126
127
128
129
130
131
132static int viafb_dma_users;
133static DECLARE_COMPLETION(viafb_dma_completion);
134
135
136
137
138
139static DEFINE_MUTEX(viafb_dma_lock);
140
141
142
143
144
145struct viafb_vx855_dma_descr {
146 u32 addr_low;
147 u32 addr_high;
148 u32 fb_offset;
149 u32 seg_size;
150 u32 tile_mode;
151 u32 next_desc_low;
152 u32 next_desc_high;
153 u32 pad;
154};
155
156
157
158
159#define VIAFB_DMA_MAGIC 0x01
160#define VIAFB_DMA_FINAL_SEGMENT 0x02
161
162
163
164
165static irqreturn_t viafb_dma_irq(int irq, void *data)
166{
167 int csr;
168 irqreturn_t ret = IRQ_NONE;
169
170 spin_lock(&global_dev.reg_lock);
171 csr = viafb_mmio_read(VDMA_CSR0);
172 if (csr & VDMA_C_DONE) {
173 viafb_mmio_write(VDMA_CSR0, VDMA_C_DONE);
174 complete(&viafb_dma_completion);
175 ret = IRQ_HANDLED;
176 }
177 spin_unlock(&global_dev.reg_lock);
178 return ret;
179}
180
181
182
183
184int viafb_request_dma(void)
185{
186 int ret = 0;
187
188
189
190
191 if (global_dev.chip_type != UNICHROME_VX855)
192 return -ENODEV;
193
194
195
196
197 mutex_lock(&viafb_dma_lock);
198 viafb_dma_users++;
199 if (viafb_dma_users == 1) {
200 ret = request_irq(global_dev.pdev->irq, viafb_dma_irq,
201 IRQF_SHARED, "via-dma", &viafb_dma_users);
202 if (ret)
203 viafb_dma_users--;
204 else
205 viafb_irq_enable(VDE_I_DMA0TDEN);
206 }
207 mutex_unlock(&viafb_dma_lock);
208 return ret;
209}
210EXPORT_SYMBOL_GPL(viafb_request_dma);
211
212void viafb_release_dma(void)
213{
214 mutex_lock(&viafb_dma_lock);
215 viafb_dma_users--;
216 if (viafb_dma_users == 0) {
217 viafb_irq_disable(VDE_I_DMA0TDEN);
218 free_irq(global_dev.pdev->irq, &viafb_dma_users);
219 }
220 mutex_unlock(&viafb_dma_lock);
221}
222EXPORT_SYMBOL_GPL(viafb_release_dma);
223
224
225
226
227
228int viafb_dma_copy_out_sg(unsigned int offset, struct scatterlist *sg, int nsg)
229{
230 struct viafb_vx855_dma_descr *descr;
231 void *descrpages;
232 dma_addr_t descr_handle;
233 unsigned long flags;
234 int i;
235 struct scatterlist *sgentry;
236 dma_addr_t nextdesc;
237
238
239
240
241 descrpages = dma_alloc_coherent(&global_dev.pdev->dev,
242 nsg*sizeof(struct viafb_vx855_dma_descr),
243 &descr_handle, GFP_KERNEL);
244 if (descrpages == NULL) {
245 dev_err(&global_dev.pdev->dev, "Unable to get descr page.\n");
246 return -ENOMEM;
247 }
248 mutex_lock(&viafb_dma_lock);
249
250
251
252 descr = descrpages;
253 nextdesc = descr_handle + sizeof(struct viafb_vx855_dma_descr);
254 for_each_sg(sg, sgentry, nsg, i) {
255 dma_addr_t paddr = sg_dma_address(sgentry);
256 descr->addr_low = paddr & 0xfffffff0;
257 descr->addr_high = ((u64) paddr >> 32) & 0x0fff;
258 descr->fb_offset = offset;
259 descr->seg_size = sg_dma_len(sgentry) >> 4;
260 descr->tile_mode = 0;
261 descr->next_desc_low = (nextdesc&0xfffffff0) | VIAFB_DMA_MAGIC;
262 descr->next_desc_high = ((u64) nextdesc >> 32) & 0x0fff;
263 descr->pad = 0xffffffff;
264 offset += sg_dma_len(sgentry);
265 nextdesc += sizeof(struct viafb_vx855_dma_descr);
266 descr++;
267 }
268 descr[-1].next_desc_low = VIAFB_DMA_FINAL_SEGMENT|VIAFB_DMA_MAGIC;
269
270
271
272 spin_lock_irqsave(&global_dev.reg_lock, flags);
273 init_completion(&viafb_dma_completion);
274 viafb_mmio_write(VDMA_DQWCR0, 0);
275 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_DONE);
276 viafb_mmio_write(VDMA_MR0, VDMA_MR_TDIE | VDMA_MR_CHAIN);
277 viafb_mmio_write(VDMA_DPRL0, descr_handle | VIAFB_DMA_MAGIC);
278 viafb_mmio_write(VDMA_DPRH0,
279 (((u64)descr_handle >> 32) & 0x0fff) | 0xf0000);
280 (void) viafb_mmio_read(VDMA_CSR0);
281 viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_START);
282 spin_unlock_irqrestore(&global_dev.reg_lock, flags);
283
284
285
286
287
288
289 wait_for_completion_timeout(&viafb_dma_completion, 1);
290 msleep(1);
291 if ((viafb_mmio_read(VDMA_CSR0)&VDMA_C_DONE) == 0)
292 printk(KERN_ERR "VIA DMA timeout!\n");
293
294
295
296 viafb_mmio_write(VDMA_CSR0, VDMA_C_DONE);
297 viafb_mmio_write(VDMA_MR0, 0);
298 mutex_unlock(&viafb_dma_lock);
299 dma_free_coherent(&global_dev.pdev->dev,
300 nsg*sizeof(struct viafb_vx855_dma_descr), descrpages,
301 descr_handle);
302 return 0;
303}
304EXPORT_SYMBOL_GPL(viafb_dma_copy_out_sg);
305#endif
306
307
308
309
310
311
312
313static u16 via_function3[] = {
314 CLE266_FUNCTION3, KM400_FUNCTION3, CN400_FUNCTION3, CN700_FUNCTION3,
315 CX700_FUNCTION3, KM800_FUNCTION3, KM890_FUNCTION3, P4M890_FUNCTION3,
316 P4M900_FUNCTION3, VX800_FUNCTION3, VX855_FUNCTION3, VX900_FUNCTION3,
317};
318
319
320
321static int viafb_get_fb_size_from_pci(int chip_type)
322{
323 int i;
324 u8 offset = 0;
325 u32 FBSize;
326 u32 VideoMemSize;
327
328
329 for (i = 0; i < ARRAY_SIZE(via_function3); i++) {
330 struct pci_dev *pdev;
331
332 pdev = pci_get_device(PCI_VENDOR_ID_VIA, via_function3[i],
333 NULL);
334 if (!pdev)
335 continue;
336
337 DEBUG_MSG(KERN_INFO "Device ID = %x\n", pdev->device);
338
339 switch (pdev->device) {
340 case CLE266_FUNCTION3:
341 case KM400_FUNCTION3:
342 offset = 0xE0;
343 break;
344 case CN400_FUNCTION3:
345 case CN700_FUNCTION3:
346 case CX700_FUNCTION3:
347 case KM800_FUNCTION3:
348 case KM890_FUNCTION3:
349 case P4M890_FUNCTION3:
350 case P4M900_FUNCTION3:
351 case VX800_FUNCTION3:
352 case VX855_FUNCTION3:
353 case VX900_FUNCTION3:
354
355 offset = 0xA0;
356 break;
357 }
358
359 if (!offset)
360 break;
361
362 pci_read_config_dword(pdev, offset, &FBSize);
363 pci_dev_put(pdev);
364 }
365
366 if (!offset) {
367 printk(KERN_ERR "cannot determine framebuffer size\n");
368 return -EIO;
369 }
370
371 FBSize = FBSize & 0x00007000;
372 DEBUG_MSG(KERN_INFO "FB Size = %x\n", FBSize);
373
374 if (chip_type < UNICHROME_CX700) {
375 switch (FBSize) {
376 case 0x00004000:
377 VideoMemSize = (16 << 20);
378 break;
379
380 case 0x00005000:
381 VideoMemSize = (32 << 20);
382 break;
383
384 case 0x00006000:
385 VideoMemSize = (64 << 20);
386 break;
387
388 default:
389 VideoMemSize = (32 << 20);
390 break;
391 }
392 } else {
393 switch (FBSize) {
394 case 0x00001000:
395 VideoMemSize = (8 << 20);
396 break;
397
398 case 0x00002000:
399 VideoMemSize = (16 << 20);
400 break;
401
402 case 0x00003000:
403 VideoMemSize = (32 << 20);
404 break;
405
406 case 0x00004000:
407 VideoMemSize = (64 << 20);
408 break;
409
410 case 0x00005000:
411 VideoMemSize = (128 << 20);
412 break;
413
414 case 0x00006000:
415 VideoMemSize = (256 << 20);
416 break;
417
418 case 0x00007000:
419 VideoMemSize = (512 << 20);
420 break;
421
422 default:
423 VideoMemSize = (32 << 20);
424 break;
425 }
426 }
427
428 return VideoMemSize;
429}
430
431
432
433
434
435static int via_pci_setup_mmio(struct viafb_dev *vdev)
436{
437 int ret;
438
439
440
441
442
443 vdev->engine_start = pci_resource_start(vdev->pdev, 1);
444 vdev->engine_len = pci_resource_len(vdev->pdev, 1);
445 vdev->engine_mmio = ioremap(vdev->engine_start,
446 vdev->engine_len);
447 if (vdev->engine_mmio == NULL)
448 dev_err(&vdev->pdev->dev,
449 "Unable to map engine MMIO; operation will be "
450 "slow and crippled.\n");
451
452
453
454
455
456
457
458 if (vdev->chip_type == UNICHROME_VX900)
459 vdev->fbmem_start = pci_resource_start(vdev->pdev, 2);
460 else
461 vdev->fbmem_start = pci_resource_start(vdev->pdev, 0);
462 ret = vdev->fbmem_len = viafb_get_fb_size_from_pci(vdev->chip_type);
463 if (ret < 0)
464 goto out_unmap;
465
466
467 for (; vdev->fbmem_len >= 8 << 20; vdev->fbmem_len /= 2) {
468 vdev->fbmem = ioremap_wc(vdev->fbmem_start, vdev->fbmem_len);
469 if (vdev->fbmem)
470 break;
471 }
472
473 if (vdev->fbmem == NULL) {
474 ret = -ENOMEM;
475 goto out_unmap;
476 }
477 return 0;
478out_unmap:
479 iounmap(vdev->engine_mmio);
480 return ret;
481}
482
483static void via_pci_teardown_mmio(struct viafb_dev *vdev)
484{
485 iounmap(vdev->fbmem);
486 iounmap(vdev->engine_mmio);
487}
488
489
490
491
492static struct viafb_subdev_info {
493 char *name;
494 struct platform_device *platdev;
495} viafb_subdevs[] = {
496 {
497 .name = "viafb-gpio",
498 },
499 {
500 .name = "viafb-i2c",
501 },
502#if IS_ENABLED(CONFIG_VIDEO_VIA_CAMERA)
503 {
504 .name = "viafb-camera",
505 },
506#endif
507};
508#define N_SUBDEVS ARRAY_SIZE(viafb_subdevs)
509
510static int via_create_subdev(struct viafb_dev *vdev,
511 struct viafb_subdev_info *info)
512{
513 int ret;
514
515 info->platdev = platform_device_alloc(info->name, -1);
516 if (!info->platdev) {
517 dev_err(&vdev->pdev->dev, "Unable to allocate pdev %s\n",
518 info->name);
519 return -ENOMEM;
520 }
521 info->platdev->dev.parent = &vdev->pdev->dev;
522 info->platdev->dev.platform_data = vdev;
523 ret = platform_device_add(info->platdev);
524 if (ret) {
525 dev_err(&vdev->pdev->dev, "Unable to add pdev %s\n",
526 info->name);
527 platform_device_put(info->platdev);
528 info->platdev = NULL;
529 }
530 return ret;
531}
532
533static int via_setup_subdevs(struct viafb_dev *vdev)
534{
535 int i;
536
537
538
539
540
541
542 for (i = 0; i < N_SUBDEVS; i++)
543 via_create_subdev(vdev, viafb_subdevs + i);
544 return 0;
545}
546
547static void via_teardown_subdevs(void)
548{
549 int i;
550
551 for (i = 0; i < N_SUBDEVS; i++)
552 if (viafb_subdevs[i].platdev) {
553 viafb_subdevs[i].platdev->dev.platform_data = NULL;
554 platform_device_unregister(viafb_subdevs[i].platdev);
555 }
556}
557
558
559
560
561#ifdef CONFIG_PM
562static LIST_HEAD(viafb_pm_hooks);
563static DEFINE_MUTEX(viafb_pm_hooks_lock);
564
565void viafb_pm_register(struct viafb_pm_hooks *hooks)
566{
567 INIT_LIST_HEAD(&hooks->list);
568
569 mutex_lock(&viafb_pm_hooks_lock);
570 list_add_tail(&hooks->list, &viafb_pm_hooks);
571 mutex_unlock(&viafb_pm_hooks_lock);
572}
573EXPORT_SYMBOL_GPL(viafb_pm_register);
574
575void viafb_pm_unregister(struct viafb_pm_hooks *hooks)
576{
577 mutex_lock(&viafb_pm_hooks_lock);
578 list_del(&hooks->list);
579 mutex_unlock(&viafb_pm_hooks_lock);
580}
581EXPORT_SYMBOL_GPL(viafb_pm_unregister);
582
583static int via_suspend(struct pci_dev *pdev, pm_message_t state)
584{
585 struct viafb_pm_hooks *hooks;
586
587 if (state.event != PM_EVENT_SUSPEND)
588 return 0;
589
590
591
592
593
594
595
596
597
598 mutex_lock(&viafb_pm_hooks_lock);
599 list_for_each_entry_reverse(hooks, &viafb_pm_hooks, list)
600 hooks->suspend(hooks->private);
601 mutex_unlock(&viafb_pm_hooks_lock);
602
603 pci_save_state(pdev);
604 pci_disable_device(pdev);
605 pci_set_power_state(pdev, pci_choose_state(pdev, state));
606 return 0;
607}
608
609static int via_resume(struct pci_dev *pdev)
610{
611 struct viafb_pm_hooks *hooks;
612
613
614 pci_set_power_state(pdev, PCI_D0);
615 pci_restore_state(pdev);
616 if (pci_enable_device(pdev))
617 return 0;
618
619 pci_set_master(pdev);
620
621
622 mutex_lock(&viafb_pm_hooks_lock);
623 list_for_each_entry(hooks, &viafb_pm_hooks, list)
624 hooks->resume(hooks->private);
625 mutex_unlock(&viafb_pm_hooks_lock);
626
627 return 0;
628}
629#endif
630
631static int via_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
632{
633 int ret;
634
635 ret = pci_enable_device(pdev);
636 if (ret)
637 return ret;
638
639
640
641
642 memset(&global_dev, 0, sizeof(global_dev));
643 global_dev.pdev = pdev;
644 global_dev.chip_type = ent->driver_data;
645 global_dev.port_cfg = adap_configs;
646 if (machine_is_olpc())
647 global_dev.port_cfg = olpc_adap_configs;
648
649 spin_lock_init(&global_dev.reg_lock);
650 ret = via_pci_setup_mmio(&global_dev);
651 if (ret)
652 goto out_disable;
653
654
655
656
657 viafb_int_init();
658 via_setup_subdevs(&global_dev);
659
660
661
662 ret = via_fb_pci_probe(&global_dev);
663 if (ret)
664 goto out_subdevs;
665 return 0;
666
667out_subdevs:
668 via_teardown_subdevs();
669 via_pci_teardown_mmio(&global_dev);
670out_disable:
671 pci_disable_device(pdev);
672 return ret;
673}
674
675static void via_pci_remove(struct pci_dev *pdev)
676{
677 via_teardown_subdevs();
678 via_fb_pci_remove(pdev);
679 via_pci_teardown_mmio(&global_dev);
680 pci_disable_device(pdev);
681}
682
683
684static const struct pci_device_id via_pci_table[] = {
685 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CLE266_DID),
686 .driver_data = UNICHROME_CLE266 },
687 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K400_DID),
688 .driver_data = UNICHROME_K400 },
689 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K800_DID),
690 .driver_data = UNICHROME_K800 },
691 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_PM800_DID),
692 .driver_data = UNICHROME_PM800 },
693 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN700_DID),
694 .driver_data = UNICHROME_CN700 },
695 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CX700_DID),
696 .driver_data = UNICHROME_CX700 },
697 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN750_DID),
698 .driver_data = UNICHROME_CN750 },
699 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K8M890_DID),
700 .driver_data = UNICHROME_K8M890 },
701 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M890_DID),
702 .driver_data = UNICHROME_P4M890 },
703 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M900_DID),
704 .driver_data = UNICHROME_P4M900 },
705 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX800_DID),
706 .driver_data = UNICHROME_VX800 },
707 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX855_DID),
708 .driver_data = UNICHROME_VX855 },
709 { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX900_DID),
710 .driver_data = UNICHROME_VX900 },
711 { }
712};
713MODULE_DEVICE_TABLE(pci, via_pci_table);
714
715static struct pci_driver via_driver = {
716 .name = "viafb",
717 .id_table = via_pci_table,
718 .probe = via_pci_probe,
719 .remove = via_pci_remove,
720#ifdef CONFIG_PM
721 .suspend = via_suspend,
722 .resume = via_resume,
723#endif
724};
725
726static int __init via_core_init(void)
727{
728 int ret;
729
730 ret = viafb_init();
731 if (ret)
732 return ret;
733 viafb_i2c_init();
734 viafb_gpio_init();
735 return pci_register_driver(&via_driver);
736}
737
738static void __exit via_core_exit(void)
739{
740 pci_unregister_driver(&via_driver);
741 viafb_gpio_exit();
742 viafb_i2c_exit();
743 viafb_exit();
744}
745
746module_init(via_core_init);
747module_exit(via_core_exit);
748