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