paul@218 | 1 | /* |
paul@218 | 2 | * Access various peripherals on a board. |
paul@218 | 3 | * |
paul@218 | 4 | * Copyright (C) 2023 Paul Boddie <paul@boddie.org.uk> |
paul@218 | 5 | * |
paul@218 | 6 | * This program is free software; you can redistribute it and/or |
paul@218 | 7 | * modify it under the terms of the GNU General Public License as |
paul@218 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@218 | 9 | * the License, or (at your option) any later version. |
paul@218 | 10 | * |
paul@218 | 11 | * This program is distributed in the hope that it will be useful, |
paul@218 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@218 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@218 | 14 | * GNU General Public License for more details. |
paul@218 | 15 | * |
paul@218 | 16 | * You should have received a copy of the GNU General Public License |
paul@218 | 17 | * along with this program; if not, write to the Free Software |
paul@218 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@218 | 19 | * Boston, MA 02110-1301, USA |
paul@218 | 20 | */ |
paul@218 | 21 | |
paul@218 | 22 | #pragma once |
paul@218 | 23 | |
paul@218 | 24 | #include <l4/re/c/dma_space.h> |
paul@218 | 25 | #include <l4/sys/types.h> |
paul@218 | 26 | #include <l4/devices/clocks.h> |
paul@218 | 27 | #include <l4/devices/gpio.h> |
paul@218 | 28 | #include <stdint.h> |
paul@218 | 29 | |
paul@218 | 30 | |
paul@218 | 31 | |
paul@218 | 32 | /* AIC adapter functions. */ |
paul@218 | 33 | |
paul@218 | 34 | void *aic_init(l4_addr_t aic_start, l4_addr_t start, l4_addr_t end, void *cpm); |
paul@218 | 35 | |
paul@218 | 36 | void *aic_get_channel(void *aic, int num, void *channel); |
paul@218 | 37 | |
paul@218 | 38 | long aic_get_buffer(void *channel, uint32_t count, l4_addr_t *addr); |
paul@218 | 39 | |
paul@218 | 40 | unsigned int aic_transfer(void *channel, uint32_t count, uint32_t sample_rate, |
paul@218 | 41 | uint8_t sample_size); |
paul@218 | 42 | |
paul@218 | 43 | |
paul@218 | 44 | |
paul@218 | 45 | /* CPM adapter functions. */ |
paul@218 | 46 | |
paul@218 | 47 | void *cpm_init(l4_addr_t cpm_base); |
paul@218 | 48 | |
paul@218 | 49 | const char *cpm_clock_type(void *cpm, enum Clock_identifiers clock); |
paul@218 | 50 | |
paul@218 | 51 | int cpm_have_clock(void *cpm, enum Clock_identifiers clock); |
paul@218 | 52 | |
paul@218 | 53 | void cpm_start_clock(void *cpm, enum Clock_identifiers clock); |
paul@218 | 54 | |
paul@218 | 55 | void cpm_stop_clock(void *cpm, enum Clock_identifiers clock); |
paul@218 | 56 | |
paul@218 | 57 | int cpm_get_parameters(void *cpm, enum Clock_identifiers clock, |
paul@218 | 58 | uint32_t parameters[]); |
paul@218 | 59 | |
paul@218 | 60 | int cpm_set_parameters(void *cpm, enum Clock_identifiers clock, |
paul@218 | 61 | int num_parameters, uint32_t parameters[]); |
paul@218 | 62 | |
paul@218 | 63 | uint8_t cpm_get_source(void *cpm, enum Clock_identifiers clock); |
paul@218 | 64 | |
paul@218 | 65 | void cpm_set_source(void *cpm, enum Clock_identifiers clock, uint8_t source); |
paul@218 | 66 | |
paul@218 | 67 | enum Clock_identifiers cpm_get_source_clock(void *cpm, enum Clock_identifiers clock); |
paul@218 | 68 | |
paul@218 | 69 | void cpm_set_source_clock(void *cpm, enum Clock_identifiers clock, |
paul@218 | 70 | enum Clock_identifiers source); |
paul@218 | 71 | |
paul@218 | 72 | uint64_t cpm_get_source_frequency(void *cpm, enum Clock_identifiers clock); |
paul@218 | 73 | |
paul@218 | 74 | uint64_t cpm_get_frequency(void *cpm, enum Clock_identifiers clock); |
paul@218 | 75 | |
paul@218 | 76 | int cpm_set_frequency(void *cpm, enum Clock_identifiers clock, uint64_t frequency); |
paul@218 | 77 | |
paul@218 | 78 | |
paul@218 | 79 | |
paul@218 | 80 | /* DMA adapter functions. */ |
paul@218 | 81 | |
paul@218 | 82 | void *dma_init(l4_addr_t start, l4_addr_t end, void *cpm); |
paul@218 | 83 | |
paul@218 | 84 | void dma_disable(void *dma_chip); |
paul@218 | 85 | |
paul@218 | 86 | void dma_enable(void *dma_chip); |
paul@218 | 87 | |
paul@218 | 88 | void *dma_get_channel(void *dma, uint8_t channel, l4_cap_idx_t irq); |
paul@218 | 89 | |
paul@218 | 90 | unsigned int dma_transfer(void *dma_channel, |
paul@218 | 91 | uint32_t source, uint32_t destination, |
paul@218 | 92 | unsigned int count, |
paul@218 | 93 | int source_increment, int destination_increment, |
paul@218 | 94 | uint8_t source_width, uint8_t destination_width, |
paul@218 | 95 | uint8_t transfer_unit_size, |
paul@218 | 96 | int type); |
paul@218 | 97 | |
paul@218 | 98 | unsigned int dma_wait(void *dma_channel); |
paul@218 | 99 | |
paul@218 | 100 | |
paul@218 | 101 | |
paul@218 | 102 | /* GPIO adapter functions. */ |
paul@218 | 103 | |
paul@218 | 104 | void *gpio_init(l4_addr_t start, l4_addr_t end, unsigned pins, |
paul@218 | 105 | l4_uint32_t pull_ups, l4_uint32_t pull_downs); |
paul@218 | 106 | |
paul@218 | 107 | void gpio_setup(void *gpio, unsigned pin, unsigned mode, int value); |
paul@218 | 108 | |
paul@218 | 109 | void gpio_config_pull(void *gpio, unsigned pin, unsigned mode); |
paul@218 | 110 | |
paul@218 | 111 | void gpio_config_pad(void *gpio, unsigned pin, unsigned func, unsigned value); |
paul@218 | 112 | |
paul@218 | 113 | void gpio_config_get(void *gpio, unsigned pin, unsigned reg, unsigned *value); |
paul@218 | 114 | |
paul@218 | 115 | void gpio_config_pad_get(void *gpio, unsigned pin, unsigned *func, unsigned *value); |
paul@218 | 116 | |
paul@218 | 117 | void gpio_multi_setup(void *gpio, Pin_slice const *mask, unsigned mode, unsigned outvalues); |
paul@218 | 118 | |
paul@218 | 119 | void gpio_multi_config_pad(void *gpio, Pin_slice const *mask, unsigned func, unsigned value); |
paul@218 | 120 | |
paul@218 | 121 | void gpio_multi_set(void *gpio, Pin_slice const *mask, unsigned data); |
paul@218 | 122 | |
paul@218 | 123 | unsigned gpio_multi_get(void *gpio, unsigned offset); |
paul@218 | 124 | |
paul@218 | 125 | int gpio_get(void *gpio, unsigned pin); |
paul@218 | 126 | |
paul@218 | 127 | void gpio_set(void *gpio, unsigned pin, int value); |
paul@218 | 128 | |
paul@218 | 129 | void *gpio_get_irq(void *gpio, unsigned pin); |
paul@218 | 130 | |
paul@218 | 131 | bool gpio_irq_set_mode(void *gpio_irq, unsigned mode); |
paul@218 | 132 | |
paul@218 | 133 | |
paul@218 | 134 | |
paul@218 | 135 | /* I2C adapter functions. */ |
paul@218 | 136 | |
paul@218 | 137 | void *i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, uint32_t frequency); |
paul@218 | 138 | |
paul@218 | 139 | void *i2c_get_channel(void *i2c, uint8_t channel); |
paul@218 | 140 | |
paul@218 | 141 | uint32_t i2c_get_frequency(void *i2c_channel); |
paul@218 | 142 | |
paul@218 | 143 | void i2c_set_target(void *i2c_channel, uint8_t addr); |
paul@218 | 144 | |
paul@218 | 145 | void i2c_start_read(void *i2c_channel, uint8_t buf[], unsigned int total, |
paul@218 | 146 | int stop); |
paul@218 | 147 | |
paul@218 | 148 | void i2c_read(void *i2c_channel); |
paul@218 | 149 | |
paul@218 | 150 | void i2c_start_write(void *i2c_channel, uint8_t buf[], unsigned int total, |
paul@218 | 151 | int stop); |
paul@218 | 152 | |
paul@218 | 153 | void i2c_write(void *i2c_channel); |
paul@218 | 154 | |
paul@218 | 155 | int i2c_read_done(void *i2c_channel); |
paul@218 | 156 | |
paul@218 | 157 | int i2c_write_done(void *i2c_channel); |
paul@218 | 158 | |
paul@218 | 159 | unsigned int i2c_have_read(void *i2c_channel); |
paul@218 | 160 | |
paul@218 | 161 | unsigned int i2c_have_written(void *i2c_channel); |
paul@218 | 162 | |
paul@218 | 163 | int i2c_failed(void *i2c_channel); |
paul@218 | 164 | |
paul@218 | 165 | void i2c_stop(void *i2c_channel); |
paul@218 | 166 | |
paul@218 | 167 | |
paul@218 | 168 | |
paul@218 | 169 | /* SPI adapter functions. */ |
paul@218 | 170 | |
paul@218 | 171 | void *spi_get_channel(void *clock_chip, int clock_pin, |
paul@218 | 172 | void *data_chip, int data_pin, |
paul@218 | 173 | void *enable_chip, int enable_pin, |
paul@218 | 174 | uint32_t frequency); |
paul@218 | 175 | |
paul@218 | 176 | void spi_send(void *channel, int bytes, const uint8_t data[]); |
paul@218 | 177 | |
paul@218 | 178 | |
paul@218 | 179 | |
paul@218 | 180 | /* Memory regions. */ |
paul@218 | 181 | |
paul@218 | 182 | enum memory_regions |
paul@218 | 183 | { |
paul@218 | 184 | AIC, CPM, DMA, GPIO, I2C |
paul@218 | 185 | }; |
paul@218 | 186 | |
paul@218 | 187 | |
paul@218 | 188 | |
paul@218 | 189 | /* CPM definitions. */ |
paul@218 | 190 | |
paul@218 | 191 | struct clock_info |
paul@218 | 192 | { |
paul@218 | 193 | const char *id; |
paul@218 | 194 | enum Clock_identifiers clock; |
paul@218 | 195 | const char *name; |
paul@218 | 196 | }; |
paul@218 | 197 | |
paul@218 | 198 | |
paul@218 | 199 | |
paul@218 | 200 | /* DMA definitions. */ |
paul@218 | 201 | |
paul@218 | 202 | struct dma_region |
paul@218 | 203 | { |
paul@218 | 204 | unsigned int size; |
paul@218 | 205 | unsigned int align; |
paul@218 | 206 | l4_addr_t vaddr; |
paul@218 | 207 | l4re_dma_space_dma_addr_t paddr; |
paul@218 | 208 | l4_cap_idx_t mem; |
paul@218 | 209 | }; |
paul@218 | 210 | |
paul@218 | 211 | |
paul@218 | 212 | |
paul@218 | 213 | /* GPIO definitions. */ |
paul@218 | 214 | |
paul@218 | 215 | struct gpio_port |
paul@218 | 216 | { |
paul@218 | 217 | const uint32_t pull_ups, pull_downs; |
paul@218 | 218 | }; |