1 | /* $NetBSD: pci_map.c,v 1.12 2002/05/30 12:06:43 drochner Exp $ */ |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 2004, 2005 Brookhaven National Laboratory |
---|
5 | * S. Kate Feng <feng1@bnl.gov> |
---|
6 | * |
---|
7 | * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * This code is derived from software contributed to The NetBSD Foundation |
---|
11 | * by Charles M. Hannum; by William R. Studenmund; by Jason R. Thorpe. |
---|
12 | * |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * PCI device mapping. |
---|
17 | */ |
---|
18 | |
---|
19 | #include <sys/cdefs.h> |
---|
20 | #include <sys/param.h> |
---|
21 | #include <sys/systm.h> |
---|
22 | #include <rtems/rtems/types.h> |
---|
23 | |
---|
24 | #include <bsp/pci.h> |
---|
25 | #include <bsp/pcireg.h> |
---|
26 | |
---|
27 | int pci_io_find(int b, int d, int f, int reg,unsigned *basep,unsigned *sizep) |
---|
28 | { |
---|
29 | uint32_t address, mask; |
---|
30 | |
---|
31 | if (reg < PCI_MAPREG_START || |
---|
32 | #if 0 |
---|
33 | /* |
---|
34 | * Can't do this check; some devices have mapping registers |
---|
35 | * way out in left field. |
---|
36 | */ |
---|
37 | reg >= PCI_MAPREG_END || |
---|
38 | #endif |
---|
39 | (reg & 3)) |
---|
40 | rtems_panic("pci_io_find: bad request"); |
---|
41 | |
---|
42 | /* |
---|
43 | * Section 6.2.5.1, `Address Maps', tells us that: |
---|
44 | * |
---|
45 | * 1) The builtin software should have already mapped the device in a |
---|
46 | * reasonable way. |
---|
47 | * |
---|
48 | * 2) A device which wants 2^n bytes of memory will hardwire the bottom |
---|
49 | * n bits of the address to 0. As recommended, we write all 1s and see |
---|
50 | * what we get back. |
---|
51 | */ |
---|
52 | pci_read_config_dword(b,d,f,reg, &address); |
---|
53 | if ( !(address & PCI_MAPREG_TYPE_IO)) return(1); |
---|
54 | pci_write_config_dword(b,d,f,reg, 0xffffffff); |
---|
55 | pci_read_config_dword(b,d,f,reg,&mask); |
---|
56 | pci_write_config_dword(b,d,f,reg, address); |
---|
57 | |
---|
58 | if ( (*sizep = PCI_MAPREG_IO_SIZE(mask))== 0) { |
---|
59 | printk("pci_io_find: void region\n"); |
---|
60 | return(1); |
---|
61 | } |
---|
62 | *basep = PCI_MAPREG_IO_ADDR(address); |
---|
63 | return(0); |
---|
64 | } |
---|
65 | |
---|
66 | int pci_mem_find(int b, int d, int f, int reg, unsigned *basep,unsigned *sizep) |
---|
67 | { |
---|
68 | uint32_t address, mask; |
---|
69 | int s; |
---|
70 | |
---|
71 | if (reg < PCI_MAPREG_START || |
---|
72 | #if 0 |
---|
73 | /* |
---|
74 | * Can't do this check; some devices have mapping registers |
---|
75 | * way out in left field. |
---|
76 | */ |
---|
77 | reg >= PCI_MAPREG_END || |
---|
78 | #endif |
---|
79 | (reg & 3)) |
---|
80 | rtems_panic("pci_mem_find: bad request"); |
---|
81 | |
---|
82 | pci_read_config_dword(b,d,f,reg, &address); |
---|
83 | if (address & PCI_MAPREG_TYPE_IO) { |
---|
84 | printk("pci_mem_find: expected type mem, found I/O\n"); |
---|
85 | return(1); |
---|
86 | } |
---|
87 | |
---|
88 | /* |
---|
89 | * Section 6.2.5.1, `Address Maps', tells us that: |
---|
90 | * |
---|
91 | * 1) The builtin software should have already mapped the device in a |
---|
92 | * reasonable way. |
---|
93 | * |
---|
94 | * 2) A device which wants 2^n bytes of memory will hardwire the bottom |
---|
95 | * n bits of the address to 0. As recommended, we write all 1s and see |
---|
96 | * what we get back. |
---|
97 | */ |
---|
98 | pci_write_config_dword(b,d,f,reg, 0xffffffff); |
---|
99 | pci_read_config_dword(b,d,f,reg,&mask); |
---|
100 | pci_write_config_dword(b,d,f,reg, address); |
---|
101 | if ( (*sizep = PCI_MAPREG_MEM_SIZE(mask))== 0) { |
---|
102 | printk("pci_io_find: void region\n"); |
---|
103 | return (1); |
---|
104 | } |
---|
105 | *basep = PCI_MAPREG_MEM_ADDR(address); |
---|
106 | return(0); |
---|
107 | } |
---|
108 | |
---|
109 | int pci_get_capability(int b, int d, int f, int capid,int *offset,uint32_t *value) |
---|
110 | { |
---|
111 | uint32_t reg, ofs; |
---|
112 | |
---|
113 | /* i82544EI PCI_CAPLISTPTR_REG */ |
---|
114 | pci_read_config_dword(b,d,f,PCI_CAPLISTPTR_REG, ®); |
---|
115 | ofs = PCI_CAPLIST_PTR(reg); |
---|
116 | while (ofs != 0) { |
---|
117 | #ifdef DIAGNOSTIC |
---|
118 | if ((ofs & 3) || (ofs < 0x40)) |
---|
119 | panic("pci_get_capability"); |
---|
120 | #endif |
---|
121 | pci_read_config_dword(b,d,f,ofs, ®); |
---|
122 | if (PCI_CAPLIST_CAP(reg) == capid) { |
---|
123 | if (offset) |
---|
124 | *offset = ofs; |
---|
125 | if (value) |
---|
126 | *value = reg; |
---|
127 | return (1); |
---|
128 | } |
---|
129 | ofs = PCI_CAPLIST_NEXT(reg); |
---|
130 | } |
---|
131 | return (0); |
---|
132 | } |
---|