Fix cross compilation (e.g. on Darwin). Following changes to make.tmpl,
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / bus / bonito.c
blob9a63f073cacccf2f3921a6ef4c02995972a13707
1 /* bonito.c - PCI bonito interface. */
2 /*
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/>.
20 #include <grub/pci.h>
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};
27 /* Usage counters. */
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
42 | (addr & 0x07ff));
44 else
47 if (addr >> 16)
48 return (volatile void *) (GRUB_MACHINE_PCI_CONFSPACE_3A_EXT | addr);
49 else
50 return (volatile void *) (GRUB_MACHINE_PCI_CONFSPACE_3A | addr);
54 grub_uint32_t
55 grub_pci_read (grub_pci_address_t addr)
57 return *(volatile grub_uint32_t *) config_addr (addr);
60 grub_uint16_t
61 grub_pci_read_word (grub_pci_address_t addr)
63 return *(volatile grub_uint16_t *) config_addr (addr);
66 grub_uint8_t
67 grub_pci_read_byte (grub_pci_address_t addr)
69 return *(volatile grub_uint8_t *) config_addr (addr);
72 void
73 grub_pci_write (grub_pci_address_t addr, grub_uint32_t data)
75 *(volatile grub_uint32_t *) config_addr (addr) = data;
78 void
79 grub_pci_write_word (grub_pci_address_t addr, grub_uint16_t data)
81 *(volatile grub_uint16_t *) config_addr (addr) = data;
84 void
85 grub_pci_write_byte (grub_pci_address_t addr, grub_uint8_t data)
87 *(volatile grub_uint8_t *) config_addr (addr) = data;
91 static inline void
92 write_bases_2f (void)
94 int i;
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;
103 volatile void *
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)
109 int i;
110 grub_addr_t newbase;
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)
117 usage_win[i]++;
118 return (void *)
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)
127 usage_win[i]++;
128 base_win[i] = newbase;
129 write_bases_2f ();
130 return (void *)
131 (addr_win[i] | (base & GRUB_MACHINE_PCI_WIN_OFFSET_MASK));
133 grub_fatal ("Out of PCI windows.");
135 else
137 int region = 0;
138 if (base >= 0x10000000
139 && base + size <= 0x18000000)
140 region = 1;
141 if (base >= 0x1c000000
142 && base + size <= 0x1f000000)
143 region = 2;
144 if (region == 0)
145 grub_fatal ("Attempt to map out of regions");
146 return (void *) (0xa0000000 | base);
150 void *
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))
155 & ~0x20000000);
158 void
159 grub_pci_device_unmap_range (grub_pci_device_t dev __attribute__ ((unused)),
160 volatile void *mem,
161 grub_size_t size __attribute__ ((unused)))
163 if (grub_bonito_type == GRUB_BONITO_2F)
165 int i;
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))
171 usage_win[i]--;
172 return;
174 grub_fatal ("Tried to unmap not mapped region");