1 /* 2 * Perform SPI communication using the JZ4780 SPI peripheral. 3 * 4 * Copyright (C) 2023, 2024 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/spi-jz4780.h> 23 24 enum Regs_jz4780 25 { 26 /* Register block offset. */ 27 28 Ssi_block_offset = 0x1000, 29 }; 30 31 32 33 /* Initialise a channel. */ 34 35 Spi_jz4780_channel::Spi_jz4780_channel(l4_addr_t spi_start, l4_addr_t start, 36 enum Clock_identifiers clock, 37 Cpm_chip *cpm, 38 Dma_channel *dma, 39 enum Dma_jz4780_request_type request_type, 40 uint64_t frequency) 41 : Spi_channel(spi_start, start, clock, cpm, dma, request_type, frequency) 42 { 43 } 44 45 46 47 /* Initialise the peripheral abstraction. */ 48 49 Spi_jz4780_chip::Spi_jz4780_chip(l4_addr_t spi_start, l4_addr_t start, 50 l4_addr_t end, Cpm_chip *cpm) 51 : Spi_chip(spi_start, start, end, cpm) 52 { 53 } 54 55 Spi_channel * 56 Spi_jz4780_chip::_get_channel(uint8_t channel, Dma_channel *dma, 57 uint64_t frequency) 58 { 59 // NOTE: Only sending is supported. 60 61 enum Dma_jz4780_request_type request_types[] = {Dma_request_ssi0_out, Dma_request_ssi1_out}; 62 enum Clock_identifiers clocks[] = {Clock_ssi0, Clock_ssi1}; 63 64 return new Spi_jz4780_channel(_spi_start + channel * Ssi_block_offset, 65 _start + channel * Ssi_block_offset, 66 clocks[channel], 67 _cpm, dma, request_types[channel], frequency); 68 } 69 70 71 72 /* C language interface. */ 73 74 void *jz4780_spi_init(l4_addr_t spi_start, l4_addr_t start, l4_addr_t end, void *cpm) 75 { 76 return new Spi_jz4780_chip(spi_start, start, end, static_cast<Cpm_chip *>(cpm)); 77 } 78 79 void *jz4780_spi_get_channel(void *spi, uint8_t channel, void *dma, uint64_t frequency) 80 { 81 return static_cast<Spi_jz4780_chip *>(spi)->get_channel(channel, 82 static_cast<Dma_channel *>(dma), frequency); 83 } 84 85 uint32_t jz4780_spi_send(void *channel, uint32_t bytes, const uint8_t data[]) 86 { 87 return static_cast<Spi_jz4780_channel *>(channel)->send(bytes, data); 88 } 89 90 uint32_t jz4780_spi_send_dc(void *channel, uint32_t bytes, const uint8_t data[], 91 const int dc[], uint8_t char_size, int big_endian) 92 { 93 return static_cast<Spi_jz4780_channel *>(channel)->send_dc(bytes, data, dc, 94 char_size, big_endian); 95 } 96 97 uint32_t jz4780_spi_send_units(void *channel, uint32_t bytes, const uint8_t data[], 98 uint8_t unit_size, uint8_t char_size, int big_endian) 99 { 100 return static_cast<Spi_jz4780_channel *>(channel)->send_units(bytes, data, 101 unit_size, char_size, big_endian); 102 } 103 104 uint32_t jz4780_spi_transfer(void *channel, l4_addr_t vaddr, 105 l4re_dma_space_dma_addr_t paddr, 106 uint32_t count, uint8_t unit_size, 107 uint8_t char_size) 108 { 109 return static_cast<Spi_jz4780_channel *>(channel)->transfer(vaddr, paddr, 110 count, unit_size, char_size); 111 } 112 113 uint32_t jz4780_spi_transfer_descriptor(void *channel, l4_addr_t vaddr, 114 l4re_dma_space_dma_addr_t paddr, 115 uint32_t count, uint8_t unit_size, 116 uint8_t char_size, l4_addr_t desc_vaddr, 117 l4re_dma_space_dma_addr_t desc_paddr) 118 { 119 return static_cast<Spi_jz4780_channel *>(channel)->transfer(vaddr, paddr, 120 count, unit_size, char_size, desc_vaddr, desc_paddr); 121 }