1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/pkg/devices/lib/spi/include/spi-common.h Fri Jun 07 16:12:32 2024 +0200
1.3 @@ -0,0 +1,102 @@
1.4 +/*
1.5 + * Perform SPI communication using the SPI peripheral.
1.6 + *
1.7 + * Copyright (C) 2023, 2024 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#pragma once
1.26 +
1.27 +#include <l4/re/c/dma_space.h>
1.28 +#include <l4/sys/types.h>
1.29 +#include <stdint.h>
1.30 +
1.31 +
1.32 +
1.33 +#ifdef __cplusplus
1.34 +
1.35 +#include <l4/devices/cpm-generic.h>
1.36 +#include <l4/devices/dma-generic.h>
1.37 +#include <l4/devices/hw_mmio_register_block.h>
1.38 +#include <l4/devices/spi.h>
1.39 +
1.40 +/* SPI peripheral channel. */
1.41 +
1.42 +class Spi_channel : public Spi_channel_base
1.43 +{
1.44 + Hw::Register_block<32> _regs;
1.45 +
1.46 + /* Initialisation parameters. */
1.47 +
1.48 + l4_addr_t _spi_start;
1.49 + enum Clock_identifiers _clock;
1.50 + Cpm_chip *_cpm;
1.51 + Dma_channel *_dma;
1.52 + int _request_type;
1.53 + uint64_t _frequency;
1.54 +
1.55 + /* Common utilities. */
1.56 +
1.57 + void configure_transfer(uint8_t char_size);
1.58 + void wait_busy();
1.59 +
1.60 +public:
1.61 + explicit Spi_channel(l4_addr_t spi_start, l4_addr_t start,
1.62 + enum Clock_identifiers clock,
1.63 + Cpm_chip *cpm,
1.64 + Dma_channel *dma,
1.65 + int request_type,
1.66 + uint64_t frequency);
1.67 +
1.68 + /* Convenience operations. */
1.69 +
1.70 + virtual uint32_t send(uint32_t bytes, const uint8_t data[]);
1.71 +
1.72 + virtual uint32_t send_dc(uint32_t bytes, const uint8_t data[], const int dc[],
1.73 + uint8_t char_size, bool big_endian = true);
1.74 +
1.75 + uint32_t send_units(uint32_t bytes, const uint8_t data[], uint8_t unit_size,
1.76 + uint8_t char_size, bool big_endian = true);
1.77 +
1.78 + /* DMA operations. */
1.79 +
1.80 + uint32_t transfer(l4_addr_t vaddr, l4re_dma_space_dma_addr_t paddr,
1.81 + uint32_t count, uint8_t unit_size, uint8_t char_size,
1.82 + l4_addr_t desc_vaddr = 0,
1.83 + l4re_dma_space_dma_addr_t desc_paddr = 0);
1.84 +};
1.85 +
1.86 +/* SPI peripheral. */
1.87 +
1.88 +class Spi_chip
1.89 +{
1.90 +protected:
1.91 + l4_addr_t _spi_start, _start, _end;
1.92 + Cpm_chip *_cpm;
1.93 +
1.94 + virtual unsigned int num_channels() = 0;
1.95 +
1.96 + virtual Spi_channel *_get_channel(uint8_t channel, Dma_channel *dma,
1.97 + uint64_t frequency) = 0;
1.98 +
1.99 +public:
1.100 + explicit Spi_chip(l4_addr_t spi_start, l4_addr_t start, l4_addr_t end, Cpm_chip *cpm);
1.101 +
1.102 + Spi_channel *get_channel(uint8_t channel, Dma_channel *dma, uint64_t frequency);
1.103 +};
1.104 +
1.105 +#endif /* __cplusplus */