1 /* 2 * I2C support for the JZ4780. 3 * 4 * Copyright (C) 2017, 2018, 2019 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 #pragma once 23 24 #include <l4/sys/types.h> 25 #include <stdint.h> 26 27 28 29 #ifdef __cplusplus 30 31 #include <l4/devices/cpm-jz4780.h> 32 #include <l4/devices/hw_mmio_register_block.h> 33 34 // I2C channel. 35 36 class I2c_jz4780_channel 37 { 38 private: 39 Hw::Register_block<32> _regs; 40 Cpm_jz4780_chip *_cpm; 41 uint32_t _frequency; 42 43 // Buffer management. 44 45 unsigned int _pos, _reqpos, _total; 46 uint8_t *_buf; 47 48 // Status conditions. 49 50 int _fail; 51 52 public: 53 I2c_jz4780_channel(l4_addr_t start, Cpm_jz4780_chip *cpm, 54 uint32_t frequency); 55 56 void set_target(uint8_t addr); 57 58 // Reading initiation and execution. 59 60 void start_read(uint8_t buf[], unsigned int total); 61 void read(); 62 63 // Writing initiation and execution. 64 65 void start_write(uint8_t buf[], unsigned int total); 66 void write(); 67 68 // Transaction control. 69 70 void stop(); 71 void disable(); 72 73 // Specific status conditions. 74 75 unsigned int have_read(); 76 unsigned int have_written(); 77 int read_done(); 78 int write_done(); 79 80 int failed(); 81 int read_failed(); 82 int write_failed(); 83 84 private: 85 void enable(); 86 87 int active(); 88 int have_input(); 89 int have_output(); 90 int can_send(); 91 92 void reset_flags(); 93 void init_parameters(); 94 void set_frequency(); 95 96 void set_read_threshold(); 97 void queue_reads(); 98 void queue_writes(); 99 void store_reads(); 100 }; 101 102 // I2C device control. 103 104 class I2c_jz4780_chip 105 { 106 private: 107 l4_addr_t _start, _end; 108 Cpm_jz4780_chip *_cpm; 109 uint32_t _frequency; 110 111 public: 112 I2c_jz4780_chip(l4_addr_t start, l4_addr_t end, Cpm_jz4780_chip *cpm, 113 uint32_t frequency); 114 115 I2c_jz4780_channel *get_channel(uint8_t channel); 116 }; 117 118 #endif /* __cplusplus */ 119 120 121 122 /* C language interface. */ 123 124 EXTERN_C_BEGIN 125 126 void *jz4780_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, 127 uint32_t frequency); 128 129 void jz4780_i2c_disable(void *i2c_channel); 130 131 void *jz4780_i2c_get_channel(void *i2c, uint8_t channel); 132 133 void jz4780_i2c_set_target(void *i2c_channel, uint8_t addr); 134 135 void jz4780_i2c_start_read(void *i2c_channel, uint8_t buf[], unsigned int total); 136 137 void jz4780_i2c_read(void *i2c_channel); 138 139 void jz4780_i2c_start_write(void *i2c_channel, uint8_t buf[], unsigned int total); 140 141 void jz4780_i2c_write(void *i2c_channel); 142 143 int jz4780_i2c_read_done(void *i2c_channel); 144 145 int jz4780_i2c_write_done(void *i2c_channel); 146 147 unsigned int jz4780_i2c_have_read(void *i2c_channel); 148 149 unsigned int jz4780_i2c_have_written(void *i2c_channel); 150 151 int jz4780_i2c_failed(void *i2c_channel); 152 153 void jz4780_i2c_stop(void *i2c_channel); 154 155 EXTERN_C_END