2 * UniNorth AGPGART routines.
4 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/pagemap.h>
8 #include <linux/agp_backend.h>
9 #include <linux/delay.h>
10 #include <asm/uninorth.h>
11 #include <asm/pci-bridge.h>
13 #include <asm/pmac_feature.h>
17 * NOTES for uninorth3 (G5 AGP) supports :
19 * There maybe also possibility to have bigger cache line size for
20 * agp (see pmac_pci.c and look for cache line). Need to be investigated
23 * PAGE size are hardcoded but this may change, see asm/page.h.
25 * Jerome Glisse <j.glisse@gmail.com>
27 static int uninorth_rev;
31 static int uninorth_fetch_size(void)
35 struct aper_size_info_32 *values;
37 pci_read_config_dword(agp_bridge->dev, UNI_N_CFG_GART_BASE, &temp);
38 temp &= ~(0xfffff000);
39 values = A_SIZE_32(agp_bridge->driver->aperture_sizes);
41 for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
42 if (temp == values[i].size_value) {
43 agp_bridge->previous_size =
44 agp_bridge->current_size = (void *) (values + i);
45 agp_bridge->aperture_size_idx = i;
46 return values[i].size;
50 agp_bridge->previous_size =
51 agp_bridge->current_size = (void *) (values + 1);
52 agp_bridge->aperture_size_idx = 1;
53 return values[1].size;
58 static void uninorth_tlbflush(struct agp_memory *mem)
60 u32 ctrl = UNI_N_CFG_GART_ENABLE;
63 ctrl |= U3_N_CFG_GART_PERFRD;
64 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
65 ctrl | UNI_N_CFG_GART_INVAL);
66 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, ctrl);
68 if (uninorth_rev <= 0x30) {
69 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
70 ctrl | UNI_N_CFG_GART_2xRESET);
71 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
76 static void uninorth_cleanup(void)
80 pci_read_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, &tmp);
81 if (!(tmp & UNI_N_CFG_GART_ENABLE))
83 tmp |= UNI_N_CFG_GART_INVAL;
84 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, tmp);
85 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, 0);
87 if (uninorth_rev <= 0x30) {
88 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
89 UNI_N_CFG_GART_2xRESET);
90 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
95 static int uninorth_configure(void)
97 struct aper_size_info_32 *current_size;
99 current_size = A_SIZE_32(agp_bridge->current_size);
101 printk(KERN_INFO PFX "configuring for size idx: %d\n",
102 current_size->size_value);
104 /* aperture size and gatt addr */
105 pci_write_config_dword(agp_bridge->dev,
107 (agp_bridge->gatt_bus_addr & 0xfffff000)
108 | current_size->size_value);
111 * UniNorth seem to be buggy enough not to handle properly when
112 * the AGP aperture isn't mapped at bus physical address 0
114 agp_bridge->gart_bus_addr = 0;
116 /* Assume U3 or later on PPC64 systems */
117 /* high 4 bits of GART physical address go in UNI_N_CFG_AGP_BASE */
118 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_AGP_BASE,
119 (agp_bridge->gatt_bus_addr >> 32) & 0xf);
121 pci_write_config_dword(agp_bridge->dev,
122 UNI_N_CFG_AGP_BASE, agp_bridge->gart_bus_addr);
126 pci_write_config_dword(agp_bridge->dev,
127 UNI_N_CFG_GART_DUMMY_PAGE,
128 agp_bridge->scratch_page_real >> 12);
134 static int uninorth_insert_memory(struct agp_memory *mem, off_t pg_start,
137 int i, j, num_entries;
140 temp = agp_bridge->current_size;
141 num_entries = A_SIZE_32(temp)->num_entries;
143 if (type != 0 || mem->type != 0)
144 /* We know nothing of memory types */
146 if ((pg_start + mem->page_count) > num_entries)
151 while (j < (pg_start + mem->page_count)) {
152 if (agp_bridge->gatt_table[j])
157 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
158 agp_bridge->gatt_table[j] =
159 cpu_to_le32((mem->memory[i] & 0xFFFFF000UL) | 0x1UL);
160 flush_dcache_range((unsigned long)__va(mem->memory[i]),
161 (unsigned long)__va(mem->memory[i])+0x1000);
163 (void)in_le32((volatile u32*)&agp_bridge->gatt_table[pg_start]);
165 flush_dcache_range((unsigned long)&agp_bridge->gatt_table[pg_start],
166 (unsigned long)&agp_bridge->gatt_table[pg_start + mem->page_count]);
168 uninorth_tlbflush(mem);
172 static int u3_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
178 temp = agp_bridge->current_size;
179 num_entries = A_SIZE_32(temp)->num_entries;
181 if (type != 0 || mem->type != 0)
182 /* We know nothing of memory types */
184 if ((pg_start + mem->page_count) > num_entries)
187 gp = (u32 *) &agp_bridge->gatt_table[pg_start];
188 for (i = 0; i < mem->page_count; ++i) {
190 printk("u3_insert_memory: entry 0x%x occupied (%x)\n",
196 for (i = 0; i < mem->page_count; i++) {
197 gp[i] = (mem->memory[i] >> PAGE_SHIFT) | 0x80000000UL;
198 flush_dcache_range((unsigned long)__va(mem->memory[i]),
199 (unsigned long)__va(mem->memory[i])+0x1000);
202 flush_dcache_range((unsigned long)gp, (unsigned long) &gp[i]);
203 uninorth_tlbflush(mem);
208 int u3_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
213 if (type != 0 || mem->type != 0)
214 /* We know nothing of memory types */
217 gp = (u32 *) &agp_bridge->gatt_table[pg_start];
218 for (i = 0; i < mem->page_count; ++i)
221 flush_dcache_range((unsigned long)gp, (unsigned long) &gp[i]);
222 uninorth_tlbflush(mem);
227 static void uninorth_agp_enable(struct agp_bridge_data *bridge, u32 mode)
229 u32 command, scratch, status;
232 pci_read_config_dword(bridge->dev,
233 bridge->capndx + PCI_AGP_STATUS,
236 command = agp_collect_device_status(bridge, mode, status);
237 command |= PCI_AGP_COMMAND_AGP;
239 if (uninorth_rev == 0x21) {
241 * Darwin disable AGP 4x on this revision, thus we
242 * may assume it's broken. This is an AGP2 controller.
244 command &= ~AGPSTAT2_4X;
247 if ((uninorth_rev >= 0x30) && (uninorth_rev <= 0x33)) {
249 * We need to to set REQ_DEPTH to 7 for U3 versions 1.0, 2.1,
250 * 2.2 and 2.3, Darwin do so.
252 if ((command >> AGPSTAT_RQ_DEPTH_SHIFT) > 7)
253 command = (command & ~AGPSTAT_RQ_DEPTH)
254 | (7 << AGPSTAT_RQ_DEPTH_SHIFT);
257 uninorth_tlbflush(NULL);
261 pci_write_config_dword(bridge->dev,
262 bridge->capndx + PCI_AGP_COMMAND,
264 pci_read_config_dword(bridge->dev,
265 bridge->capndx + PCI_AGP_COMMAND,
267 } while ((scratch & PCI_AGP_COMMAND_AGP) == 0 && ++timeout < 1000);
268 if ((scratch & PCI_AGP_COMMAND_AGP) == 0)
269 printk(KERN_ERR PFX "failed to write UniNorth AGP"
270 " command register\n");
272 if (uninorth_rev >= 0x30) {
273 /* This is an AGP V3 */
274 agp_device_command(command, (status & AGPSTAT_MODE_3_0));
277 agp_device_command(command, 0);
280 uninorth_tlbflush(NULL);
285 * These Power Management routines are _not_ called by the normal PCI PM layer,
286 * but directly by the video driver through function pointers in the device
289 static int agp_uninorth_suspend(struct pci_dev *pdev)
291 struct agp_bridge_data *bridge;
294 struct pci_dev *device = NULL;
296 bridge = agp_find_bridge(pdev);
300 /* Only one suspend supported */
301 if (bridge->dev_private_data)
304 /* turn off AGP on the video chip, if it was enabled */
305 for_each_pci_dev(device) {
306 /* Don't touch the bridge yet, device first */
309 /* Only deal with devices on the same bus here, no Mac has a P2P
310 * bridge on the AGP port, and mucking around the entire PCI
311 * tree is source of problems on some machines because of a bug
312 * in some versions of pci_find_capability() when hitting a dead
315 if (device->bus != pdev->bus)
317 agp = pci_find_capability(device, PCI_CAP_ID_AGP);
320 pci_read_config_dword(device, agp + PCI_AGP_COMMAND, &cmd);
321 if (!(cmd & PCI_AGP_COMMAND_AGP))
323 printk("uninorth-agp: disabling AGP on device %s\n",
325 cmd &= ~PCI_AGP_COMMAND_AGP;
326 pci_write_config_dword(device, agp + PCI_AGP_COMMAND, cmd);
329 /* turn off AGP on the bridge */
330 agp = pci_find_capability(pdev, PCI_CAP_ID_AGP);
331 pci_read_config_dword(pdev, agp + PCI_AGP_COMMAND, &cmd);
332 bridge->dev_private_data = (void *)cmd;
333 if (cmd & PCI_AGP_COMMAND_AGP) {
334 printk("uninorth-agp: disabling AGP on bridge %s\n",
336 cmd &= ~PCI_AGP_COMMAND_AGP;
337 pci_write_config_dword(pdev, agp + PCI_AGP_COMMAND, cmd);
339 /* turn off the GART */
345 static int agp_uninorth_resume(struct pci_dev *pdev)
347 struct agp_bridge_data *bridge;
350 bridge = agp_find_bridge(pdev);
354 command = (u32)bridge->dev_private_data;
355 bridge->dev_private_data = NULL;
356 if (!(command & PCI_AGP_COMMAND_AGP))
359 uninorth_agp_enable(bridge, command);
363 #endif /* CONFIG_PM */
365 static int uninorth_create_gatt_table(struct agp_bridge_data *bridge)
376 /* We can't handle 2 level gatt's */
377 if (bridge->driver->size_type == LVL2_APER_SIZE)
381 i = bridge->aperture_size_idx;
382 temp = bridge->current_size;
383 size = page_order = num_entries = 0;
386 size = A_SIZE_32(temp)->size;
387 page_order = A_SIZE_32(temp)->page_order;
388 num_entries = A_SIZE_32(temp)->num_entries;
390 table = (char *) __get_free_pages(GFP_KERNEL, page_order);
394 bridge->current_size = A_IDX32(bridge);
396 bridge->aperture_size_idx = i;
398 } while (!table && (i < bridge->driver->num_aperture_sizes));
403 table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
405 for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
406 SetPageReserved(page);
408 bridge->gatt_table_real = (u32 *) table;
409 bridge->gatt_table = (u32 *)table;
410 bridge->gatt_bus_addr = virt_to_gart(table);
412 for (i = 0; i < num_entries; i++)
413 bridge->gatt_table[i] = 0;
415 flush_dcache_range((unsigned long)table, (unsigned long)table_end);
420 static int uninorth_free_gatt_table(struct agp_bridge_data *bridge)
423 char *table, *table_end;
427 temp = bridge->current_size;
428 page_order = A_SIZE_32(temp)->page_order;
430 /* Do not worry about freeing memory, because if this is
431 * called, then all agp memory is deallocated and removed
435 table = (char *) bridge->gatt_table_real;
436 table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
438 for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
439 ClearPageReserved(page);
441 free_pages((unsigned long) bridge->gatt_table_real, page_order);
446 void null_cache_flush(void)
453 static struct aper_size_info_32 uninorth_sizes[7] =
455 #if 0 /* Not sure uninorth supports that high aperture sizes */
467 * Not sure that u3 supports that high aperture sizes but it
468 * would strange if it did not :)
470 static struct aper_size_info_32 u3_sizes[8] =
472 {512, 131072, 7, 128},
482 struct agp_bridge_driver uninorth_agp_driver = {
483 .owner = THIS_MODULE,
484 .aperture_sizes = (void *)uninorth_sizes,
485 .size_type = U32_APER_SIZE,
486 .num_aperture_sizes = 4,
487 .configure = uninorth_configure,
488 .fetch_size = uninorth_fetch_size,
489 .cleanup = uninorth_cleanup,
490 .tlb_flush = uninorth_tlbflush,
491 .mask_memory = agp_generic_mask_memory,
493 .cache_flush = null_cache_flush,
494 .agp_enable = uninorth_agp_enable,
495 .create_gatt_table = uninorth_create_gatt_table,
496 .free_gatt_table = uninorth_free_gatt_table,
497 .insert_memory = uninorth_insert_memory,
498 .remove_memory = agp_generic_remove_memory,
499 .alloc_by_type = agp_generic_alloc_by_type,
500 .free_by_type = agp_generic_free_by_type,
501 .agp_alloc_page = agp_generic_alloc_page,
502 .agp_destroy_page = agp_generic_destroy_page,
503 .cant_use_aperture = 1,
506 struct agp_bridge_driver u3_agp_driver = {
507 .owner = THIS_MODULE,
508 .aperture_sizes = (void *)u3_sizes,
509 .size_type = U32_APER_SIZE,
510 .num_aperture_sizes = 8,
511 .configure = uninorth_configure,
512 .fetch_size = uninorth_fetch_size,
513 .cleanup = uninorth_cleanup,
514 .tlb_flush = uninorth_tlbflush,
515 .mask_memory = agp_generic_mask_memory,
517 .cache_flush = null_cache_flush,
518 .agp_enable = uninorth_agp_enable,
519 .create_gatt_table = uninorth_create_gatt_table,
520 .free_gatt_table = uninorth_free_gatt_table,
521 .insert_memory = u3_insert_memory,
522 .remove_memory = u3_remove_memory,
523 .alloc_by_type = agp_generic_alloc_by_type,
524 .free_by_type = agp_generic_free_by_type,
525 .agp_alloc_page = agp_generic_alloc_page,
526 .agp_destroy_page = agp_generic_destroy_page,
527 .cant_use_aperture = 1,
528 .needs_scratch_page = 1,
531 static struct agp_device_ids uninorth_agp_device_ids[] __devinitdata = {
533 .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP,
534 .chipset_name = "UniNorth",
537 .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP_P,
538 .chipset_name = "UniNorth/Pangea",
541 .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP15,
542 .chipset_name = "UniNorth 1.5",
545 .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP2,
546 .chipset_name = "UniNorth 2",
549 .device_id = PCI_DEVICE_ID_APPLE_U3_AGP,
550 .chipset_name = "U3",
553 .device_id = PCI_DEVICE_ID_APPLE_U3L_AGP,
554 .chipset_name = "U3L",
557 .device_id = PCI_DEVICE_ID_APPLE_U3H_AGP,
558 .chipset_name = "U3H",
561 .device_id = PCI_DEVICE_ID_APPLE_IPID2_AGP,
562 .chipset_name = "UniNorth/Intrepid2",
566 static int __devinit agp_uninorth_probe(struct pci_dev *pdev,
567 const struct pci_device_id *ent)
569 struct agp_device_ids *devs = uninorth_agp_device_ids;
570 struct agp_bridge_data *bridge;
571 struct device_node *uninorth_node;
575 cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
579 /* probe for known chipsets */
580 for (j = 0; devs[j].chipset_name != NULL; ++j) {
581 if (pdev->device == devs[j].device_id) {
582 printk(KERN_INFO PFX "Detected Apple %s chipset\n",
583 devs[j].chipset_name);
588 printk(KERN_ERR PFX "Unsupported Apple chipset (device id: %04x).\n",
593 /* Set revision to 0 if we could not read it. */
596 /* Locate core99 Uni-N */
597 uninorth_node = of_find_node_by_name(NULL, "uni-n");
599 if (uninorth_node == NULL) {
601 uninorth_node = of_find_node_by_name(NULL, "u3");
604 int *revprop = (int *)
605 get_property(uninorth_node, "device-rev", NULL);
607 uninorth_rev = *revprop & 0x3f;
608 of_node_put(uninorth_node);
612 /* Inform platform of our suspend/resume caps */
613 pmac_register_agp_pm(pdev, agp_uninorth_suspend, agp_uninorth_resume);
616 /* Allocate & setup our driver */
617 bridge = agp_alloc_bridge();
622 bridge->driver = &u3_agp_driver;
624 bridge->driver = &uninorth_agp_driver;
627 bridge->capndx = cap_ptr;
628 bridge->flags = AGP_ERRATA_FASTWRITES;
630 /* Fill in the mode register */
631 pci_read_config_dword(pdev, cap_ptr+PCI_AGP_STATUS, &bridge->mode);
633 pci_set_drvdata(pdev, bridge);
634 return agp_add_bridge(bridge);
637 static void __devexit agp_uninorth_remove(struct pci_dev *pdev)
639 struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
642 /* Inform platform of our suspend/resume caps */
643 pmac_register_agp_pm(pdev, NULL, NULL);
646 agp_remove_bridge(bridge);
647 agp_put_bridge(bridge);
650 static struct pci_device_id agp_uninorth_pci_table[] = {
652 .class = (PCI_CLASS_BRIDGE_HOST << 8),
654 .vendor = PCI_VENDOR_ID_APPLE,
655 .device = PCI_ANY_ID,
656 .subvendor = PCI_ANY_ID,
657 .subdevice = PCI_ANY_ID,
662 MODULE_DEVICE_TABLE(pci, agp_uninorth_pci_table);
664 static struct pci_driver agp_uninorth_pci_driver = {
665 .name = "agpgart-uninorth",
666 .id_table = agp_uninorth_pci_table,
667 .probe = agp_uninorth_probe,
668 .remove = agp_uninorth_remove,
671 static int __init agp_uninorth_init(void)
675 return pci_register_driver(&agp_uninorth_pci_driver);
678 static void __exit agp_uninorth_cleanup(void)
680 pci_unregister_driver(&agp_uninorth_pci_driver);
683 module_init(agp_uninorth_init);
684 module_exit(agp_uninorth_cleanup);
686 MODULE_AUTHOR("Ben Herrenschmidt & Paul Mackerras");
687 MODULE_LICENSE("GPL");