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@222 | 35 | #include <l4/devices/spi.h> |
paul@221 | 36 | |
paul@221 | 37 | /* SPI peripheral channel. */ |
paul@221 | 38 | |
paul@222 | 39 | class Spi_jz4780_channel : public Spi_channel_base |
paul@221 | 40 | { |
paul@221 | 41 | Hw::Register_block<32> _regs; |
paul@221 | 42 | |
paul@221 | 43 | /* Initialisation parameters. */ |
paul@221 | 44 | |
paul@221 | 45 | l4_addr_t _spi_start; |
paul@221 | 46 | enum Clock_identifiers _clock; |
paul@221 | 47 | Cpm_jz4780_chip *_cpm; |
paul@221 | 48 | Dma_jz4780_channel *_dma; |
paul@221 | 49 | enum Dma_jz4780_request_type _request_type; |
paul@221 | 50 | uint64_t _frequency; |
paul@221 | 51 | |
paul@222 | 52 | /* Common utilities. */ |
paul@222 | 53 | |
paul@221 | 54 | void configure_transfer(uint8_t char_size); |
paul@221 | 55 | |
paul@222 | 56 | void wait_busy(); |
paul@222 | 57 | |
paul@221 | 58 | public: |
paul@221 | 59 | explicit Spi_jz4780_channel(l4_addr_t spi_start, l4_addr_t start, |
paul@221 | 60 | enum Clock_identifiers clock, |
paul@221 | 61 | Cpm_jz4780_chip *cpm, |
paul@221 | 62 | Dma_jz4780_channel *dma, |
paul@221 | 63 | enum Dma_jz4780_request_type request_type, |
paul@221 | 64 | uint64_t frequency); |
paul@221 | 65 | |
paul@222 | 66 | /* Convenience operations. */ |
paul@222 | 67 | |
paul@222 | 68 | virtual uint32_t send(uint32_t bytes, const uint8_t data[]); |
paul@222 | 69 | |
paul@236 | 70 | virtual uint32_t send_dc(uint32_t bytes, const uint8_t data[], const int dc[], |
paul@236 | 71 | uint8_t char_size, bool big_endian); |
paul@222 | 72 | |
paul@222 | 73 | uint32_t send_units(uint32_t bytes, const uint8_t data[], uint8_t unit_size, |
paul@236 | 74 | uint8_t char_size, bool big_endian); |
paul@222 | 75 | |
paul@221 | 76 | /* DMA operations. */ |
paul@221 | 77 | |
paul@236 | 78 | uint32_t transfer(l4_addr_t vaddr, l4re_dma_space_dma_addr_t paddr, |
paul@236 | 79 | uint32_t count, uint8_t unit_size, uint8_t char_size, |
paul@223 | 80 | l4_addr_t desc_vaddr = 0, |
paul@223 | 81 | l4re_dma_space_dma_addr_t desc_paddr = 0); |
paul@221 | 82 | }; |
paul@221 | 83 | |
paul@221 | 84 | /* SPI peripheral. */ |
paul@221 | 85 | |
paul@221 | 86 | class Spi_jz4780_chip |
paul@221 | 87 | { |
paul@221 | 88 | private: |
paul@221 | 89 | l4_addr_t _spi_start, _start, _end; |
paul@221 | 90 | Cpm_jz4780_chip *_cpm; |
paul@221 | 91 | |
paul@221 | 92 | public: |
paul@221 | 93 | explicit Spi_jz4780_chip(l4_addr_t spi_start, l4_addr_t start, l4_addr_t end, Cpm_jz4780_chip *cpm); |
paul@221 | 94 | |
paul@221 | 95 | Spi_jz4780_channel *get_channel(uint8_t channel, Dma_jz4780_channel *dma, uint64_t frequency); |
paul@221 | 96 | }; |
paul@221 | 97 | |
paul@221 | 98 | #endif /* __cplusplus */ |
paul@221 | 99 | |
paul@221 | 100 | |
paul@221 | 101 | |
paul@221 | 102 | /* C language interface. */ |
paul@221 | 103 | |
paul@221 | 104 | EXTERN_C_BEGIN |
paul@221 | 105 | |
paul@221 | 106 | void *jz4780_spi_init(l4_addr_t spi_start, l4_addr_t start, l4_addr_t end, |
paul@221 | 107 | void *cpm); |
paul@221 | 108 | |
paul@221 | 109 | void *jz4780_spi_get_channel(void *spi, uint8_t channel, void *dma, uint64_t frequency); |
paul@221 | 110 | |
paul@222 | 111 | uint32_t jz4780_spi_send(void *channel, uint32_t bytes, const uint8_t data[]); |
paul@222 | 112 | |
paul@222 | 113 | uint32_t jz4780_spi_send_dc(void *channel, uint32_t bytes, const uint8_t data[], |
paul@236 | 114 | const int dc[], uint8_t char_size, int big_endian); |
paul@222 | 115 | |
paul@222 | 116 | uint32_t jz4780_spi_send_units(void *channel, uint32_t bytes, const uint8_t data[], |
paul@236 | 117 | uint8_t unit_size, uint8_t char_size, int big_endian); |
paul@221 | 118 | |
paul@236 | 119 | uint32_t jz4780_spi_transfer(void *channel, l4_addr_t vaddr, |
paul@236 | 120 | l4re_dma_space_dma_addr_t paddr, uint32_t count, |
paul@236 | 121 | uint8_t unit_size, uint8_t char_size); |
paul@221 | 122 | |
paul@236 | 123 | uint32_t jz4780_spi_transfer_descriptor(void *channel, l4_addr_t vaddr, |
paul@236 | 124 | l4re_dma_space_dma_addr_t paddr, |
paul@223 | 125 | uint32_t count, uint8_t unit_size, |
paul@223 | 126 | uint8_t char_size, l4_addr_t desc_vaddr, |
paul@223 | 127 | l4re_dma_space_dma_addr_t desc_paddr); |
paul@223 | 128 | |
paul@221 | 129 | EXTERN_C_END |