1 /* 2 * I2C support for the JZ4780 and X1600. 3 * 4 * Copyright (C) 2017, 2018, 2019, 2023 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.h> 32 #include <l4/devices/hw_mmio_register_block.h> 33 34 // I2C channel. 35 36 class I2c_channel 37 { 38 protected: 39 Hw::Register_block<32> _regs; 40 enum Clock_identifiers _clock; 41 Cpm_chip *_cpm; 42 uint32_t _frequency; 43 44 // Buffer management. 45 46 unsigned int _pos, _reqpos, _total; 47 uint8_t *_buf; 48 49 // Status conditions. 50 51 int _fail; 52 int _stop; 53 54 // Common operations. 55 56 void enable(); 57 58 int active(); 59 int have_input(); 60 int have_output(); 61 int can_send(); 62 63 void reset_flags(); 64 void init_parameters(); 65 void set_frequency(); 66 67 void set_read_threshold(); 68 void queue_reads(); 69 void queue_writes(); 70 void store_reads(); 71 72 // Behaviour adaptations. 73 74 virtual bool high_speed_mode() = 0; 75 virtual bool hold_enable() = 0; 76 virtual bool stop_hold() = 0; 77 uint32_t stop_command(); 78 79 public: 80 explicit I2c_channel(l4_addr_t start, enum Clock_identifiers clock, 81 Cpm_chip *cpm, uint32_t frequency); 82 83 uint32_t get_frequency(); 84 void set_target(uint8_t addr); 85 86 // Reading initiation and execution. 87 88 void start_read(uint8_t buf[], unsigned int total, int stop = 0); 89 void read(); 90 91 // Writing initiation and execution. 92 93 void start_write(uint8_t buf[], unsigned int total, int stop = 0); 94 void write(); 95 96 // Transaction control. 97 98 void stop(); 99 void disable(); 100 101 // Specific status conditions. 102 103 unsigned int have_read(); 104 unsigned int have_written(); 105 int read_done(); 106 int write_done(); 107 108 int failed(); 109 int read_failed(); 110 int write_failed(); 111 }; 112 113 // I2C device control. 114 115 class I2c_chip 116 { 117 protected: 118 l4_addr_t _start, _end; 119 Cpm_chip *_cpm; 120 uint32_t _frequency; 121 122 virtual unsigned int num_channels() = 0; 123 124 virtual I2c_channel *_get_channel(l4_addr_t addr, enum Clock_identifiers clock, 125 Cpm_chip *cpm, uint32_t frequency) = 0; 126 127 public: 128 explicit I2c_chip(l4_addr_t start, l4_addr_t end, Cpm_chip *cpm, 129 uint32_t frequency); 130 131 I2c_channel *get_channel(uint8_t channel); 132 }; 133 134 #endif /* __cplusplus */