1 /* bonito.c - PCI bonito interface. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/misc.h>
23 static grub_uint32_t base_win
[GRUB_MACHINE_PCI_NUM_WIN
];
24 static const grub_size_t sizes_win
[GRUB_MACHINE_PCI_NUM_WIN
] =
25 {GRUB_MACHINE_PCI_WIN1_SIZE
, GRUB_MACHINE_PCI_WIN_SIZE
,
26 GRUB_MACHINE_PCI_WIN_SIZE
};
28 static int usage_win
[GRUB_MACHINE_PCI_NUM_WIN
];
29 static grub_addr_t addr_win
[GRUB_MACHINE_PCI_NUM_WIN
] =
30 {GRUB_MACHINE_PCI_WIN1_ADDR
, GRUB_MACHINE_PCI_WIN2_ADDR
,
31 GRUB_MACHINE_PCI_WIN3_ADDR
};
33 grub_bonito_type_t grub_bonito_type
;
35 static volatile void *
36 config_addr (grub_pci_address_t addr
)
38 if (grub_bonito_type
== GRUB_BONITO_2F
)
40 GRUB_MACHINE_PCI_CONF_CTRL_REG_2F
= 1 << ((addr
>> 11) & 0xf);
41 return (volatile void *) (GRUB_MACHINE_PCI_CONFSPACE_2F
48 return (volatile void *) (GRUB_MACHINE_PCI_CONFSPACE_3A_EXT
| addr
);
50 return (volatile void *) (GRUB_MACHINE_PCI_CONFSPACE_3A
| addr
);
55 grub_pci_read (grub_pci_address_t addr
)
57 return *(volatile grub_uint32_t
*) config_addr (addr
);
61 grub_pci_read_word (grub_pci_address_t addr
)
63 return *(volatile grub_uint16_t
*) config_addr (addr
);
67 grub_pci_read_byte (grub_pci_address_t addr
)
69 return *(volatile grub_uint8_t
*) config_addr (addr
);
73 grub_pci_write (grub_pci_address_t addr
, grub_uint32_t data
)
75 *(volatile grub_uint32_t
*) config_addr (addr
) = data
;
79 grub_pci_write_word (grub_pci_address_t addr
, grub_uint16_t data
)
81 *(volatile grub_uint16_t
*) config_addr (addr
) = data
;
85 grub_pci_write_byte (grub_pci_address_t addr
, grub_uint8_t data
)
87 *(volatile grub_uint8_t
*) config_addr (addr
) = data
;
95 grub_uint32_t reg
= 0;
96 for (i
= 0; i
< GRUB_MACHINE_PCI_NUM_WIN
; i
++)
97 reg
|= (((base_win
[i
] >> GRUB_MACHINE_PCI_WIN_SHIFT
)
98 & GRUB_MACHINE_PCI_WIN_MASK
)
99 << (i
* GRUB_MACHINE_PCI_WIN_MASK_SIZE
));
100 GRUB_MACHINE_PCI_IO_CTRL_REG_2F
= reg
;
104 grub_pci_device_map_range (grub_pci_device_t dev
__attribute__ ((unused
)),
105 grub_addr_t base
, grub_size_t size
)
107 if (grub_bonito_type
== GRUB_BONITO_2F
)
112 /* First try already used registers. */
113 for (i
= 0; i
< GRUB_MACHINE_PCI_NUM_WIN
; i
++)
114 if (usage_win
[i
] && base_win
[i
] <= base
115 && base_win
[i
] + sizes_win
[i
] > base
+ size
)
119 (addr_win
[i
] | (base
& GRUB_MACHINE_PCI_WIN_OFFSET_MASK
));
121 /* Map new register. */
122 newbase
= base
& ~GRUB_MACHINE_PCI_WIN_OFFSET_MASK
;
123 for (i
= 0; i
< GRUB_MACHINE_PCI_NUM_WIN
; i
++)
124 if (!usage_win
[i
] && newbase
<= base
125 && newbase
+ sizes_win
[i
] > base
+ size
)
128 base_win
[i
] = newbase
;
131 (addr_win
[i
] | (base
& GRUB_MACHINE_PCI_WIN_OFFSET_MASK
));
133 grub_fatal ("Out of PCI windows.");
138 if (base
>= 0x10000000
139 && base
+ size
<= 0x18000000)
141 if (base
>= 0x1c000000
142 && base
+ size
<= 0x1f000000)
145 grub_fatal ("Attempt to map out of regions");
146 return (void *) (0xa0000000 | base
);
151 grub_pci_device_map_range_cached (grub_pci_device_t dev
,
152 grub_addr_t base
, grub_size_t size
)
154 return (void *) (((grub_addr_t
) grub_pci_device_map_range (dev
, base
, size
))
159 grub_pci_device_unmap_range (grub_pci_device_t dev
__attribute__ ((unused
)),
161 grub_size_t size
__attribute__ ((unused
)))
163 if (grub_bonito_type
== GRUB_BONITO_2F
)
166 for (i
= 0; i
< GRUB_MACHINE_PCI_NUM_WIN
; i
++)
167 if (usage_win
[i
] && addr_win
[i
]
168 == (((grub_addr_t
) mem
| 0x20000000)
169 & ~GRUB_MACHINE_PCI_WIN_OFFSET_MASK
))
174 grub_fatal ("Tried to unmap not mapped region");