1 /* 2 * Access various peripherals on a board using the X1600. 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 #include <l4/devices/aic-x1600.h> 23 #include <l4/devices/cpm-x1600.h> 24 #include <l4/devices/dma-x1600.h> 25 #include <l4/devices/gpio-x1600.h> 26 #include <l4/devices/i2c-x1600.h> 27 #include <l4/devices/spi-gpio.h> 28 #include <l4/devices/spi-hybrid.h> 29 #include <l4/devices/spi-jz4780.h> 30 #include "common.h" 31 32 33 34 /* AIC adapter functions. */ 35 36 void *aic_init(l4_addr_t aic_start, l4_addr_t start, l4_addr_t end, void *cpm) 37 { 38 return x1600_aic_init(aic_start, start, end, cpm); 39 } 40 41 void *aic_get_channel(void *aic, int num, void *channel) 42 { 43 return x1600_aic_get_channel(aic, num, channel); 44 } 45 46 unsigned int aic_transfer(void *channel, l4re_dma_space_dma_addr_t paddr, 47 uint32_t count, uint32_t sample_rate, 48 uint8_t sample_size) 49 { 50 return x1600_aic_transfer(channel, paddr, count, sample_rate, sample_size); 51 } 52 53 54 55 /* CPM adapter functions. */ 56 57 void *cpm_init(l4_addr_t cpm_base) 58 { 59 return x1600_cpm_init(cpm_base); 60 } 61 62 const char *cpm_clock_type(void *cpm, enum Clock_identifiers clock) 63 { 64 return x1600_cpm_clock_type(cpm, clock); 65 } 66 67 int cpm_have_clock(void *cpm, enum Clock_identifiers clock) 68 { 69 return x1600_cpm_have_clock(cpm, clock); 70 } 71 72 void cpm_start_clock(void *cpm, enum Clock_identifiers clock) 73 { 74 x1600_cpm_start_clock(cpm, clock); 75 } 76 77 void cpm_stop_clock(void *cpm, enum Clock_identifiers clock) 78 { 79 x1600_cpm_stop_clock(cpm, clock); 80 } 81 82 int cpm_get_parameters(void *cpm, enum Clock_identifiers clock, 83 uint32_t parameters[]) 84 { 85 return x1600_cpm_get_parameters(cpm, clock, parameters); 86 } 87 88 int cpm_set_parameters(void *cpm, enum Clock_identifiers clock, 89 int num_parameters, uint32_t parameters[]) 90 { 91 return x1600_cpm_set_parameters(cpm, clock, num_parameters, parameters); 92 } 93 94 uint8_t cpm_get_source(void *cpm, enum Clock_identifiers clock) 95 { 96 return x1600_cpm_get_source(cpm, clock); 97 } 98 99 void cpm_set_source(void *cpm, enum Clock_identifiers clock, uint8_t source) 100 { 101 x1600_cpm_set_source(cpm, clock, source); 102 } 103 104 enum Clock_identifiers cpm_get_source_clock(void *cpm, enum Clock_identifiers clock) 105 { 106 return x1600_cpm_get_source_clock(cpm, clock); 107 } 108 109 void cpm_set_source_clock(void *cpm, enum Clock_identifiers clock, enum Clock_identifiers source) 110 { 111 x1600_cpm_set_source_clock(cpm, clock, source); 112 } 113 114 uint64_t cpm_get_source_frequency(void *cpm, enum Clock_identifiers clock) 115 { 116 return x1600_cpm_get_source_frequency(cpm, clock); 117 } 118 119 uint64_t cpm_get_frequency(void *cpm, enum Clock_identifiers clock) 120 { 121 return x1600_cpm_get_frequency(cpm, clock); 122 } 123 124 int cpm_set_frequency(void *cpm, enum Clock_identifiers clock, uint64_t frequency) 125 { 126 return x1600_cpm_set_frequency(cpm, clock, frequency); 127 } 128 129 130 131 /* DMA adapter functions. */ 132 133 void *dma_init(l4_addr_t start, l4_addr_t end, void *cpm) 134 { 135 return x1600_dma_init(start, end, cpm); 136 } 137 138 void dma_disable(void *dma_chip) 139 { 140 x1600_dma_disable(dma_chip); 141 } 142 143 void dma_enable(void *dma_chip) 144 { 145 x1600_dma_enable(dma_chip); 146 } 147 148 void *dma_get_channel(void *dma, uint8_t channel, l4_cap_idx_t irq) 149 { 150 return x1600_dma_get_channel(dma, channel, irq); 151 } 152 153 unsigned int dma_transfer(void *dma_channel, 154 uint32_t source, uint32_t destination, 155 unsigned int count, 156 int source_increment, int destination_increment, 157 uint8_t source_width, uint8_t destination_width, 158 uint8_t transfer_unit_size, 159 int type) 160 { 161 return x1600_dma_transfer(dma_channel, source, destination, count, 162 source_increment, destination_increment, 163 source_width, destination_width, 164 transfer_unit_size, type); 165 } 166 167 unsigned int dma_wait(void *dma_channel) 168 { 169 return x1600_dma_wait(dma_channel); 170 } 171 172 173 174 /* GPIO adapter functions. */ 175 176 void *gpio_init(l4_addr_t start, l4_addr_t end, unsigned pins, 177 l4_uint32_t pull_ups, l4_uint32_t pull_downs) 178 { 179 return x1600_gpio_init(start, end, pins, pull_ups, pull_downs); 180 } 181 182 void gpio_setup(void *gpio, unsigned pin, unsigned mode, int value) 183 { 184 x1600_gpio_setup(gpio, pin, mode, value); 185 } 186 187 void gpio_config_pull(void *gpio, unsigned pin, unsigned mode) 188 { 189 x1600_gpio_config_pull(gpio, pin, mode); 190 } 191 192 void gpio_config_pad(void *gpio, unsigned pin, unsigned func, unsigned value) 193 { 194 x1600_gpio_config_pad(gpio, pin, func, value); 195 } 196 197 void gpio_config_get(void *gpio, unsigned pin, unsigned reg, unsigned *value) 198 { 199 x1600_gpio_config_get(gpio, pin, reg, value); 200 } 201 202 void gpio_config_pad_get(void *gpio, unsigned pin, unsigned *func, unsigned *value) 203 { 204 x1600_gpio_config_pad_get(gpio, pin, func, value); 205 } 206 207 void gpio_multi_setup(void *gpio, Pin_slice const *mask, unsigned mode, unsigned outvalues) 208 { 209 x1600_gpio_multi_setup(gpio, mask, mode, outvalues); 210 } 211 212 void gpio_multi_config_pad(void *gpio, Pin_slice const *mask, unsigned func, unsigned value) 213 { 214 x1600_gpio_multi_config_pad(gpio, mask, func, value); 215 } 216 217 void gpio_multi_set(void *gpio, Pin_slice const *mask, unsigned data) 218 { 219 x1600_gpio_multi_set(gpio, mask, data); 220 } 221 222 unsigned gpio_multi_get(void *gpio, unsigned offset) 223 { 224 return x1600_gpio_multi_get(gpio, offset); 225 } 226 227 int gpio_get(void *gpio, unsigned pin) 228 { 229 return x1600_gpio_get(gpio, pin); 230 } 231 232 void gpio_set(void *gpio, unsigned pin, int value) 233 { 234 x1600_gpio_set(gpio, pin, value); 235 } 236 237 void *gpio_get_irq(void *gpio, unsigned pin) 238 { 239 return x1600_gpio_get_irq(gpio, pin); 240 } 241 242 bool gpio_irq_set_mode(void *gpio_irq, unsigned mode) 243 { 244 return x1600_gpio_irq_set_mode(gpio_irq, mode); 245 } 246 247 248 249 /* I2C adapter functions. */ 250 251 void *i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, 252 uint32_t frequency) 253 { 254 return x1600_i2c_init(start, end, cpm, frequency); 255 } 256 257 void *i2c_get_channel(void *i2c, uint8_t channel) 258 { 259 return x1600_i2c_get_channel(i2c, channel); 260 } 261 262 uint32_t i2c_get_frequency(void *i2c_channel) 263 { 264 return x1600_i2c_get_frequency(i2c_channel); 265 } 266 267 void i2c_set_target(void *i2c_channel, uint8_t addr) 268 { 269 return x1600_i2c_set_target(i2c_channel, addr); 270 } 271 272 void i2c_start_read(void *i2c_channel, uint8_t buf[], unsigned int total, 273 int stop) 274 { 275 x1600_i2c_start_read(i2c_channel, buf, total, stop); 276 } 277 278 void i2c_read(void *i2c_channel) 279 { 280 x1600_i2c_read(i2c_channel); 281 } 282 283 void i2c_start_write(void *i2c_channel, uint8_t buf[], unsigned int total, 284 int stop) 285 { 286 x1600_i2c_start_write(i2c_channel, buf, total, stop); 287 } 288 289 void i2c_write(void *i2c_channel) 290 { 291 x1600_i2c_write(i2c_channel); 292 } 293 294 int i2c_read_done(void *i2c_channel) 295 { 296 return x1600_i2c_read_done(i2c_channel); 297 } 298 299 int i2c_write_done(void *i2c_channel) 300 { 301 return x1600_i2c_write_done(i2c_channel); 302 } 303 304 unsigned int i2c_have_read(void *i2c_channel) 305 { 306 return x1600_i2c_have_read(i2c_channel); 307 } 308 309 unsigned int i2c_have_written(void *i2c_channel) 310 { 311 return x1600_i2c_have_written(i2c_channel); 312 } 313 314 int i2c_failed(void *i2c_channel) 315 { 316 return x1600_i2c_failed(i2c_channel); 317 } 318 319 void i2c_stop(void *i2c_channel) 320 { 321 x1600_i2c_stop(i2c_channel); 322 } 323 324 325 326 /* SPI adapter functions. */ 327 328 void *spi_init(l4_addr_t spi_start, l4_addr_t start, l4_addr_t end, void *cpm) 329 { 330 return jz4780_spi_init(spi_start, start, end, cpm); 331 } 332 333 void *spi_get_channel(void *spi, uint8_t num, void *channel, uint64_t frequency, 334 void *control_chip, int control_pin, int control_alt_func) 335 { 336 void *ch = jz4780_spi_get_channel(spi, num, channel, frequency); 337 338 return spi_hybrid_get_channel(ch, control_chip, control_pin, control_alt_func); 339 } 340 341 void *spi_get_channel_gpio(uint64_t frequency, 342 void *clock_chip, int clock_pin, 343 void *data_chip, int data_pin, 344 void *enable_chip, int enable_pin, 345 void *control_chip, int control_pin) 346 { 347 void *ch = spi_gpio_get_channel(frequency, clock_chip, clock_pin, data_chip, 348 data_pin, enable_chip, enable_pin, control_chip, 349 control_pin); 350 351 return spi_hybrid_get_channel(ch, control_chip, control_pin, -1); 352 } 353 354 void spi_acquire_control(void *channel, int level) 355 { 356 spi_hybrid_acquire_control(channel, level); 357 } 358 359 void spi_release_control(void *channel) 360 { 361 spi_hybrid_release_control(channel); 362 } 363 364 void spi_send(void *channel, uint32_t bytes, const uint8_t data[]) 365 { 366 spi_hybrid_send(channel, bytes, data); 367 } 368 369 void spi_send_units(void *channel, uint32_t bytes, const uint8_t data[], uint8_t unit_size, 370 uint8_t char_size) 371 { 372 spi_hybrid_send_units(channel, bytes, data, unit_size, char_size); 373 } 374 375 uint32_t spi_transfer(void *channel, l4re_dma_space_dma_addr_t paddr, 376 uint32_t count, uint8_t unit_size, uint8_t char_size, 377 l4_addr_t desc_vaddr, l4re_dma_space_dma_addr_t desc_paddr) 378 { 379 /* Transfer is not supported by the common interface. */ 380 381 void *ch = spi_hybrid_get_raw_channel(channel); 382 383 return jz4780_spi_transfer_descriptor(ch, paddr, count, unit_size, char_size, 384 desc_vaddr, desc_paddr); 385 } 386 387 388 389 /* Memory regions. */ 390 391 const char *memory_regions[] = { 392 [AIC] = "x1600-aic", 393 [CPM] = "x1600-cpm", 394 [DMA] = "x1600-dma", 395 [GPIO] = "x1600-gpio", 396 [I2C] = "x1600-i2c", 397 [SSI] = "x1600-ssi", 398 }; 399 400 401 402 /* AIC definitions. */ 403 404 void *aic_channels[] = {NULL}; 405 406 const unsigned int num_aic_channels = 1; 407 408 l4_cap_idx_t aic_irqs[] = {L4_INVALID_CAP}; 409 410 411 412 /* CPM definitions. */ 413 414 struct clock_info clocks[] = { 415 {"ext", Clock_external, "External"}, 416 {"plla", Clock_pll_A, "PLL A"}, 417 {"plle", Clock_pll_E, "PLL E"}, 418 {"pllm", Clock_pll_M, "PLL M"}, 419 {"main", Clock_main, "Main"}, 420 {"cpu", Clock_cpu, "CPU"}, 421 {"ahb0", Clock_hclock0, "AHB0"}, 422 {"ahb2", Clock_hclock2, "AHB2"}, 423 {"apb", Clock_pclock, "APB"}, 424 {"aic", Clock_aic, "AIC"}, 425 {"dma", Clock_dma, "DMA"}, 426 {"lcd0", Clock_lcd_pixel0, "LCD pixel"}, 427 {"msc0", Clock_msc0, "MSC0"}, 428 {"msc1", Clock_msc1, "MSC1"}, 429 {"otg", Clock_otg0, "USB OTG"}, 430 {"i2c0", Clock_i2c0, "I2C0"}, 431 {"i2c1", Clock_i2c1, "I2C1"}, 432 {"i2s0", Clock_i2s0, "I2S0"}, 433 {"i2s1", Clock_i2s1, "I2S1"}, 434 {"i2s0r", Clock_i2s0_rx, "I2S0 RX"}, 435 {"i2s0t", Clock_i2s0_tx, "I2S0 TX"}, 436 {"ssi0", Clock_ssi0, "SSI"}, 437 {"uart0", Clock_uart0, "UART0"}, 438 {"uart1", Clock_uart1, "UART1"}, 439 {"uart2", Clock_uart2, "UART2"}, 440 {"uart3", Clock_uart3, "UART3"}, 441 {NULL, Clock_undefined, NULL}, 442 }; 443 444 445 446 /* DMA definitions. */ 447 448 void *dma_channels[32] = {NULL}; 449 450 const unsigned int num_dma_channels = 32; 451 452 struct dma_region dma_regions[8]; 453 454 const unsigned int num_dma_regions = 8; 455 456 l4_cap_idx_t dma_irq = L4_INVALID_CAP; 457 458 459 460 /* GPIO definitions. */ 461 462 struct gpio_port gpio_ports[] = { 463 {0xffffffff, 0x00000000}, 464 {0xdffbf7bf, 0x00000000}, 465 {0x987e0000, 0x07000007}, 466 {0x0000003f, 0x00000000} 467 }; 468 469 const unsigned int num_gpio_ports = 4; 470 471 const char gpio_port_labels[] = "ABCD"; 472 473 474 475 /* I2C definitions. */ 476 477 void *i2c_channels[] = {NULL, NULL}; 478 479 const unsigned int num_i2c_channels = 2; 480 481 l4_cap_idx_t i2c_irqs[] = {L4_INVALID_CAP, L4_INVALID_CAP}; 482 483 484 485 /* SPI definitions. */ 486 487 void *spi_channels[] = {NULL}; 488 489 const unsigned int num_spi_channels = 1;