1 /* 2 * (c) 2017, 2018 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 unsigned queued, _pos, _reqpos, _total; 41 uint8_t *_buf; 42 int _fail, _stop, _last; 43 44 public: 45 I2c_jz4780_channel(l4_addr_t start, Cpm_jz4780_chip *cpm, 46 uint32_t frequency); 47 48 void set_target(uint8_t addr); 49 void start_read(uint8_t buf[], unsigned total); 50 void read(); 51 int read_done(); 52 unsigned have_read(); 53 int read_incomplete(); 54 void write(uint8_t buf[], unsigned total); 55 void stop(); 56 57 private: 58 void disable(); 59 void enable(); 60 61 int active(); 62 int have_input(); 63 int have_output(); 64 int can_send(); 65 int read_failed(); 66 int write_failed(); 67 68 void reset_flags(); 69 void init_parameters(); 70 void set_frequency(); 71 72 void set_read_threshold(); 73 void queue_reads(); 74 void queue_writes(uint8_t buf[], unsigned *pos, unsigned total); 75 void store_reads(); 76 }; 77 78 // I2C device control. 79 80 class I2c_jz4780_chip 81 { 82 private: 83 l4_addr_t _start, _end; 84 Cpm_jz4780_chip *_cpm; 85 uint32_t _frequency; 86 87 public: 88 I2c_jz4780_chip(l4_addr_t start, l4_addr_t end, Cpm_jz4780_chip *cpm, 89 uint32_t frequency); 90 91 I2c_jz4780_channel *get_channel(uint8_t channel); 92 }; 93 94 #endif /* __cplusplus */ 95 96 97 98 /* C language interface. */ 99 100 EXTERN_C_BEGIN 101 102 void *jz4780_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, 103 uint32_t frequency); 104 105 void *jz4780_i2c_get_channel(void *i2c, uint8_t channel); 106 107 void jz4780_i2c_set_target(void *i2c_channel, uint8_t addr); 108 void jz4780_i2c_start_read(void *i2c_channel, uint8_t buf[], unsigned total); 109 void jz4780_i2c_read(void *i2c_channel); 110 int jz4780_i2c_read_done(void *i2c_channel); 111 unsigned jz4780_i2c_have_read(void *i2c_channel); 112 int jz4780_i2c_read_incomplete(void *i2c_channel); 113 void jz4780_i2c_write(void *i2c_channel, uint8_t buf[], unsigned total); 114 void jz4780_i2c_stop(void *i2c_channel); 115 116 EXTERN_C_END