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 56 // Reading initiation and execution. 57 58 void start_read(uint8_t buf[], unsigned int total); 59 void read(); 60 61 // Writing initiation and execution. 62 63 void start_write(uint8_t buf[], unsigned int total); 64 void write(); 65 66 // Transaction control. 67 68 void stop(); 69 void disable(); 70 71 // Specific status conditions. 72 73 unsigned int have_read(); 74 unsigned int have_written(); 75 int read_done(); 76 int write_done(); 77 78 int failed(); 79 int read_failed(); 80 int write_failed(); 81 82 private: 83 void enable(); 84 85 int active(); 86 int have_input(); 87 int have_output(); 88 int can_send(); 89 90 void reset_flags(); 91 void init_parameters(); 92 void set_frequency(); 93 94 void set_read_threshold(); 95 void queue_reads(); 96 void queue_writes(); 97 void store_reads(); 98 }; 99 100 // I2C device control. 101 102 class I2c_jz4780_chip 103 { 104 private: 105 l4_addr_t _start, _end; 106 Cpm_jz4780_chip *_cpm; 107 uint32_t _frequency; 108 109 public: 110 I2c_jz4780_chip(l4_addr_t start, l4_addr_t end, Cpm_jz4780_chip *cpm, 111 uint32_t frequency); 112 113 I2c_jz4780_channel *get_channel(uint8_t channel); 114 }; 115 116 #endif /* __cplusplus */ 117 118 119 120 /* C language interface. */ 121 122 EXTERN_C_BEGIN 123 124 void *jz4780_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, 125 uint32_t frequency); 126 127 void jz4780_i2c_disable(void *i2c_channel); 128 129 void *jz4780_i2c_get_channel(void *i2c, uint8_t channel); 130 131 void jz4780_i2c_set_target(void *i2c_channel, uint8_t addr); 132 133 void jz4780_i2c_start_read(void *i2c_channel, uint8_t buf[], unsigned int total); 134 135 void jz4780_i2c_read(void *i2c_channel); 136 137 void jz4780_i2c_start_write(void *i2c_channel, uint8_t buf[], unsigned int total); 138 139 void jz4780_i2c_write(void *i2c_channel); 140 141 int jz4780_i2c_read_done(void *i2c_channel); 142 143 int jz4780_i2c_write_done(void *i2c_channel); 144 145 unsigned int jz4780_i2c_have_read(void *i2c_channel); 146 147 unsigned int jz4780_i2c_have_written(void *i2c_channel); 148 149 int jz4780_i2c_failed(void *i2c_channel); 150 151 void jz4780_i2c_stop(void *i2c_channel); 152 153 EXTERN_C_END