paul@221 | 1 | /* |
paul@221 | 2 | * Perform SPI communication using the JZ4780 SPI peripheral. |
paul@221 | 3 | * |
paul@221 | 4 | * Copyright (C) 2023 Paul Boddie <paul@boddie.org.uk> |
paul@221 | 5 | * |
paul@221 | 6 | * This program is free software; you can redistribute it and/or |
paul@221 | 7 | * modify it under the terms of the GNU General Public License as |
paul@221 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@221 | 9 | * the License, or (at your option) any later version. |
paul@221 | 10 | * |
paul@221 | 11 | * This program is distributed in the hope that it will be useful, |
paul@221 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@221 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@221 | 14 | * GNU General Public License for more details. |
paul@221 | 15 | * |
paul@221 | 16 | * You should have received a copy of the GNU General Public License |
paul@221 | 17 | * along with this program; if not, write to the Free Software |
paul@221 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@221 | 19 | * Boston, MA 02110-1301, USA |
paul@221 | 20 | */ |
paul@221 | 21 | |
paul@221 | 22 | #pragma once |
paul@221 | 23 | |
paul@221 | 24 | #include <l4/re/c/dma_space.h> |
paul@221 | 25 | #include <l4/sys/types.h> |
paul@221 | 26 | #include <stdint.h> |
paul@221 | 27 | |
paul@221 | 28 | |
paul@221 | 29 | |
paul@221 | 30 | #ifdef __cplusplus |
paul@221 | 31 | |
paul@221 | 32 | #include <l4/devices/cpm-jz4780.h> |
paul@221 | 33 | #include <l4/devices/dma-jz4780.h> |
paul@221 | 34 | #include <l4/devices/hw_mmio_register_block.h> |
paul@221 | 35 | |
paul@221 | 36 | /* SPI peripheral channel. */ |
paul@221 | 37 | |
paul@221 | 38 | class Spi_jz4780_channel |
paul@221 | 39 | { |
paul@221 | 40 | Hw::Register_block<32> _regs; |
paul@221 | 41 | |
paul@221 | 42 | /* Initialisation parameters. */ |
paul@221 | 43 | |
paul@221 | 44 | l4_addr_t _spi_start; |
paul@221 | 45 | enum Clock_identifiers _clock; |
paul@221 | 46 | Cpm_jz4780_chip *_cpm; |
paul@221 | 47 | Dma_jz4780_channel *_dma; |
paul@221 | 48 | enum Dma_jz4780_request_type _request_type; |
paul@221 | 49 | uint64_t _frequency; |
paul@221 | 50 | |
paul@221 | 51 | void configure_transfer(uint8_t char_size); |
paul@221 | 52 | |
paul@221 | 53 | public: |
paul@221 | 54 | explicit Spi_jz4780_channel(l4_addr_t spi_start, l4_addr_t start, |
paul@221 | 55 | enum Clock_identifiers clock, |
paul@221 | 56 | Cpm_jz4780_chip *cpm, |
paul@221 | 57 | Dma_jz4780_channel *dma, |
paul@221 | 58 | enum Dma_jz4780_request_type request_type, |
paul@221 | 59 | uint64_t frequency); |
paul@221 | 60 | |
paul@221 | 61 | /* DMA operations. */ |
paul@221 | 62 | |
paul@221 | 63 | uint32_t transfer(l4re_dma_space_dma_addr_t paddr, uint32_t count, |
paul@221 | 64 | uint8_t unit_size, uint8_t char_size); |
paul@221 | 65 | |
paul@221 | 66 | /* Convenience operations. */ |
paul@221 | 67 | |
paul@221 | 68 | uint32_t send(uint32_t bytes, const uint8_t data[], uint8_t unit_size, |
paul@221 | 69 | uint8_t char_size); |
paul@221 | 70 | }; |
paul@221 | 71 | |
paul@221 | 72 | /* SPI peripheral. */ |
paul@221 | 73 | |
paul@221 | 74 | class Spi_jz4780_chip |
paul@221 | 75 | { |
paul@221 | 76 | private: |
paul@221 | 77 | l4_addr_t _spi_start, _start, _end; |
paul@221 | 78 | Cpm_jz4780_chip *_cpm; |
paul@221 | 79 | |
paul@221 | 80 | public: |
paul@221 | 81 | explicit Spi_jz4780_chip(l4_addr_t spi_start, l4_addr_t start, l4_addr_t end, Cpm_jz4780_chip *cpm); |
paul@221 | 82 | |
paul@221 | 83 | Spi_jz4780_channel *get_channel(uint8_t channel, Dma_jz4780_channel *dma, uint64_t frequency); |
paul@221 | 84 | }; |
paul@221 | 85 | |
paul@221 | 86 | #endif /* __cplusplus */ |
paul@221 | 87 | |
paul@221 | 88 | |
paul@221 | 89 | |
paul@221 | 90 | /* C language interface. */ |
paul@221 | 91 | |
paul@221 | 92 | EXTERN_C_BEGIN |
paul@221 | 93 | |
paul@221 | 94 | void *jz4780_spi_init(l4_addr_t spi_start, l4_addr_t start, l4_addr_t end, |
paul@221 | 95 | void *cpm); |
paul@221 | 96 | |
paul@221 | 97 | void *jz4780_spi_get_channel(void *spi, uint8_t channel, void *dma, uint64_t frequency); |
paul@221 | 98 | |
paul@221 | 99 | uint32_t jz4780_spi_send(void *channel, uint32_t bytes, const uint8_t data[], |
paul@221 | 100 | uint8_t unit_size, uint8_t char_size); |
paul@221 | 101 | |
paul@221 | 102 | uint32_t jz4780_spi_transfer(void *channel, l4re_dma_space_dma_addr_t paddr, |
paul@221 | 103 | uint32_t count, uint8_t unit_size, uint8_t char_size); |
paul@221 | 104 | |
paul@221 | 105 | EXTERN_C_END |