1 /* 2 * Copyright (C) 2017, 2018, 2019 Paul Boddie <paul@boddie.org.uk> 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License as 6 * published by the Free Software Foundation; either version 2 of 7 * the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA 18 */ 19 20 #pragma once 21 22 #include <l4/sys/types.h> 23 #include <stdint.h> 24 25 26 27 #ifdef __cplusplus 28 29 #include <l4/devices/cpm-jz4780.h> 30 #include <l4/devices/hw_mmio_register_block.h> 31 32 // I2C channel. 33 34 class I2c_jz4780_channel 35 { 36 private: 37 Hw::Register_block<32> _regs; 38 Cpm_jz4780_chip *_cpm; 39 uint32_t _frequency; 40 41 // Buffer management. 42 43 unsigned int _pos, _reqpos, _total; 44 uint8_t *_buf; 45 46 // Status conditions. 47 48 int _fail; 49 50 public: 51 I2c_jz4780_channel(l4_addr_t start, Cpm_jz4780_chip *cpm, 52 uint32_t frequency); 53 54 void set_target(uint8_t addr); 55 void start_read(uint8_t buf[], unsigned int total); 56 void read(); 57 void write(uint8_t buf[], unsigned int total); 58 59 // Transaction control. 60 61 void stop(); 62 void disable(); 63 64 // Specific status conditions. 65 66 unsigned int have_read(); 67 int read_done(); 68 int read_incomplete(); 69 int read_failed(); 70 int write_failed(); 71 72 private: 73 void enable(); 74 75 int active(); 76 int have_input(); 77 int have_output(); 78 int can_send(); 79 80 void reset_flags(); 81 void init_parameters(); 82 void set_frequency(); 83 84 void set_read_threshold(); 85 void queue_reads(); 86 void queue_writes(uint8_t buf[], unsigned int *pos, unsigned int total); 87 void store_reads(); 88 }; 89 90 // I2C device control. 91 92 class I2c_jz4780_chip 93 { 94 private: 95 l4_addr_t _start, _end; 96 Cpm_jz4780_chip *_cpm; 97 uint32_t _frequency; 98 99 public: 100 I2c_jz4780_chip(l4_addr_t start, l4_addr_t end, Cpm_jz4780_chip *cpm, 101 uint32_t frequency); 102 103 I2c_jz4780_channel *get_channel(uint8_t channel); 104 }; 105 106 #endif /* __cplusplus */ 107 108 109 110 /* C language interface. */ 111 112 EXTERN_C_BEGIN 113 114 void *jz4780_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, 115 uint32_t frequency); 116 117 void jz4780_i2c_disable(void *i2c_channel); 118 119 void *jz4780_i2c_get_channel(void *i2c, uint8_t channel); 120 121 void jz4780_i2c_set_target(void *i2c_channel, uint8_t addr); 122 123 void jz4780_i2c_start_read(void *i2c_channel, uint8_t buf[], unsigned int total); 124 125 void jz4780_i2c_read(void *i2c_channel); 126 127 int jz4780_i2c_read_done(void *i2c_channel); 128 129 unsigned int jz4780_i2c_have_read(void *i2c_channel); 130 131 int jz4780_i2c_read_incomplete(void *i2c_channel); 132 133 void jz4780_i2c_write(void *i2c_channel, uint8_t buf[], unsigned int total); 134 135 void jz4780_i2c_stop(void *i2c_channel); 136 137 EXTERN_C_END