1 /* 2 * Common definitions and adapter function signatures for access to 3 * board-specific functionality. 4 * 5 * Copyright (C) 2023, 2024 Paul Boddie <paul@boddie.org.uk> 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation; either version 2 of 10 * the License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 20 * Boston, MA 02110-1301, USA 21 */ 22 23 #pragma once 24 25 #include <l4/devices/clocks.h> 26 #include <l4/devices/dma.h> 27 #include <l4/devices/gpio.h> 28 #include <l4/sys/types.h> 29 #include <stdint.h> 30 31 32 33 /* AIC adapter functions. */ 34 35 void *aic_init(l4_addr_t aic_start, l4_addr_t start, l4_addr_t end, void *cpm); 36 37 void *aic_get_channel(void *aic, int num, void *channel); 38 39 unsigned int aic_transfer(void *channel, l4re_dma_space_dma_addr_t paddr, 40 uint32_t count, uint32_t sample_rate, 41 uint8_t sample_size); 42 43 44 45 /* CPM adapter functions. */ 46 47 void *cpm_init(l4_addr_t cpm_base); 48 49 const char *cpm_clock_type(void *cpm, enum Clock_identifiers clock); 50 51 int cpm_have_clock(void *cpm, enum Clock_identifiers clock); 52 53 void cpm_start_clock(void *cpm, enum Clock_identifiers clock); 54 55 void cpm_stop_clock(void *cpm, enum Clock_identifiers clock); 56 57 int cpm_get_parameters(void *cpm, enum Clock_identifiers clock, 58 uint32_t parameters[]); 59 60 int cpm_set_parameters(void *cpm, enum Clock_identifiers clock, 61 int num_parameters, uint32_t parameters[]); 62 63 uint8_t cpm_get_source(void *cpm, enum Clock_identifiers clock); 64 65 void cpm_set_source(void *cpm, enum Clock_identifiers clock, uint8_t source); 66 67 enum Clock_identifiers cpm_get_source_clock(void *cpm, enum Clock_identifiers clock); 68 69 void cpm_set_source_clock(void *cpm, enum Clock_identifiers clock, 70 enum Clock_identifiers source); 71 72 uint64_t cpm_get_source_frequency(void *cpm, enum Clock_identifiers clock); 73 74 uint64_t cpm_get_frequency(void *cpm, enum Clock_identifiers clock); 75 76 int cpm_set_frequency(void *cpm, enum Clock_identifiers clock, uint64_t frequency); 77 78 79 80 /* DMA adapter functions. */ 81 82 void *dma_init(l4_addr_t start, l4_addr_t end, void *cpm); 83 84 void dma_disable(void *dma_chip); 85 86 void dma_enable(void *dma_chip); 87 88 void *dma_get_channel(void *dma, uint8_t channel, l4_cap_idx_t irq); 89 90 unsigned int dma_transfer(void *dma_channel, 91 uint32_t source, uint32_t destination, 92 unsigned int count, 93 int source_increment, int destination_increment, 94 uint8_t source_width, uint8_t destination_width, 95 uint8_t transfer_unit_size, 96 int type); 97 98 unsigned int dma_wait(void *dma_channel); 99 100 101 102 /* GPIO adapter functions. */ 103 104 void *gpio_init(l4_addr_t start, uint8_t port_number); 105 106 void *gpio_init_shadow(l4_addr_t start, uint8_t port_number); 107 108 void gpio_setup(void *gpio, unsigned pin, unsigned mode, int value); 109 110 void gpio_config_pull(void *gpio, unsigned pin, unsigned mode); 111 112 void gpio_config_pad(void *gpio, unsigned pin, unsigned func, unsigned value); 113 114 void gpio_config_get(void *gpio, unsigned pin, unsigned reg, unsigned *value); 115 116 void gpio_config_pad_get(void *gpio, unsigned pin, unsigned *func, unsigned *value); 117 118 void gpio_multi_setup(void *gpio, Pin_slice const *mask, unsigned mode, unsigned outvalues); 119 120 void gpio_multi_config_pad(void *gpio, Pin_slice const *mask, unsigned func, unsigned value); 121 122 void gpio_multi_set(void *gpio, Pin_slice const *mask, unsigned data); 123 124 unsigned gpio_multi_get(void *gpio, unsigned offset); 125 126 int gpio_get(void *gpio, unsigned pin); 127 128 void gpio_set(void *gpio, unsigned pin, int value); 129 130 void *gpio_get_irq(void *gpio, unsigned pin); 131 132 bool gpio_irq_set_mode(void *gpio_irq, unsigned mode); 133 134 135 136 /* I2C adapter functions. */ 137 138 void *i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, uint32_t frequency); 139 140 void *i2c_get_channel(void *i2c, uint8_t channel); 141 142 uint32_t i2c_get_frequency(void *i2c_channel); 143 144 void i2c_set_target(void *i2c_channel, uint8_t addr); 145 146 void i2c_start_read(void *i2c_channel, uint8_t buf[], unsigned int total, 147 int stop); 148 149 void i2c_read(void *i2c_channel); 150 151 void i2c_start_write(void *i2c_channel, uint8_t buf[], unsigned int total, 152 int stop); 153 154 void i2c_write(void *i2c_channel); 155 156 int i2c_read_done(void *i2c_channel); 157 158 int i2c_write_done(void *i2c_channel); 159 160 unsigned int i2c_have_read(void *i2c_channel); 161 162 unsigned int i2c_have_written(void *i2c_channel); 163 164 int i2c_failed(void *i2c_channel); 165 166 void i2c_stop(void *i2c_channel); 167 168 169 170 /* MSC adapter functions. */ 171 172 void *msc_init(l4_addr_t msc_start, l4_addr_t start, l4_addr_t end, void *cpm); 173 174 void *msc_get_channel(void *msc, uint8_t channel, l4_cap_idx_t irq, void *dma); 175 176 struct msc_card *msc_get_cards(void *msc_channel); 177 178 uint8_t msc_num_cards(void *msc_channel); 179 180 void msc_enable(void *msc_channel); 181 182 uint32_t msc_read_blocks(void *msc_channel, uint8_t card, 183 struct dma_region *region, 184 uint32_t block_address, uint32_t block_count); 185 186 187 188 /* RTC adapter functions. */ 189 190 void *rtc_init(l4_addr_t start, void *cpm); 191 192 void rtc_disable(void *rtc); 193 194 void rtc_enable(void *rtc); 195 196 void rtc_alarm_disable(void *rtc); 197 198 void rtc_alarm_enable(void *rtc); 199 200 uint32_t rtc_get_seconds(void *rtc); 201 202 void rtc_set_seconds(void *rtc, uint32_t seconds); 203 204 uint32_t rtc_get_alarm_seconds(void *rtc); 205 206 void rtc_set_alarm_seconds(void *rtc, uint32_t seconds); 207 208 void rtc_hibernate(void *rtc); 209 210 void rtc_power_down(void *rtc); 211 212 void rtc_set_regulator(void *rtc, uint32_t base, uint32_t adjustment); 213 214 215 216 /* SPI adapter functions. */ 217 218 void *spi_init(l4_addr_t spi_start, l4_addr_t start, l4_addr_t end, void *cpm); 219 220 void *spi_get_channel(void *spi, uint8_t num, void *channel, uint64_t frequency, 221 void *control_chip, int control_pin, int control_alt_func); 222 223 void *spi_get_channel_gpio(uint64_t frequency, 224 void *clock_chip, int clock_pin, 225 void *data_chip, int data_pin, 226 void *enable_chip, int enable_pin, 227 void *control_chip, int control_pin); 228 229 void spi_acquire_control(void *channel, int level); 230 231 void spi_release_control(void *channel); 232 233 void spi_send(void *channel, uint32_t bytes, const uint8_t data[]); 234 235 void spi_send_units(void *channel, uint32_t bytes, const uint8_t data[], 236 uint8_t unit_size, uint8_t char_size, int big_endian); 237 238 uint32_t spi_transfer(void *channel, l4_addr_t vaddr, 239 l4re_dma_space_dma_addr_t paddr, uint32_t count, 240 uint8_t unit_size, uint8_t char_size, 241 l4_addr_t desc_vaddr, l4re_dma_space_dma_addr_t desc_paddr); 242 243 244 245 /* TCU adapter functions. */ 246 247 void *tcu_init(l4_addr_t start, l4_addr_t end); 248 249 void *tcu_get_channel(void *tcu, uint8_t channel, l4_cap_idx_t irq); 250 251 void tcu_disable(void *tcu_channel); 252 253 void tcu_enable(void *tcu_channel); 254 255 int tcu_is_enabled(void *tcu_channel); 256 257 uint8_t tcu_get_clock(void *tcu_channel); 258 259 void tcu_set_clock(void *tcu_channel, uint8_t clock); 260 261 uint32_t tcu_get_prescale(void *tcu_channel); 262 263 void tcu_set_prescale(void *tcu_channel, uint32_t prescale); 264 265 uint32_t tcu_get_counter(void *tcu_channel); 266 267 void tcu_set_counter(void *tcu_channel, uint32_t value); 268 269 uint8_t tcu_get_count_mode(void *tcu_channel); 270 271 void tcu_set_count_mode(void *tcu_channel, uint8_t mode); 272 273 uint32_t tcu_get_full_data_value(void *tcu_channel); 274 275 void tcu_set_full_data_value(void *tcu_channel, uint32_t value); 276 277 uint32_t tcu_get_half_data_value(void *tcu_channel); 278 279 void tcu_set_half_data_value(void *tcu_channel, uint32_t value); 280 281 int tcu_get_full_data_mask(void *tcu_channel); 282 283 void tcu_set_full_data_mask(void *tcu_channel, int masked); 284 285 int tcu_get_half_data_mask(void *tcu_channel); 286 287 void tcu_set_half_data_mask(void *tcu_channel, int masked); 288 289 int tcu_have_interrupt(void *tcu_channel); 290 291 int tcu_wait_for_irq(void *tcu_channel, uint32_t timeout); 292 293 294 295 /* Memory regions. */ 296 297 enum memory_regions 298 { 299 AIC, CPM, DMA, GPIO, I2C, MSC, RTC, SSI, TCU 300 }; 301 302 303 304 /* CPM definitions. */ 305 306 struct clock_info 307 { 308 const char *id; 309 enum Clock_identifiers clock; 310 const char *name; 311 }; 312 313 314 315 /* Memory definitions. */ 316 317 struct memory_region 318 { 319 unsigned int size; 320 l4_addr_t addr; 321 };