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 Cpm_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 int _stop; 52 53 // Common operations. 54 55 void enable(); 56 57 int active(); 58 int have_input(); 59 int have_output(); 60 int can_send(); 61 62 void reset_flags(); 63 void init_parameters(); 64 void set_frequency(); 65 66 void set_read_threshold(); 67 void queue_reads(); 68 void queue_writes(); 69 void store_reads(); 70 71 public: 72 explicit I2c_channel(l4_addr_t start, enum Clock_identifiers clock, 73 Cpm_chip *cpm, uint32_t frequency); 74 75 uint32_t get_frequency(); 76 void set_target(uint8_t addr); 77 78 // Reading initiation and execution. 79 80 void start_read(uint8_t buf[], unsigned int total, int stop = 0); 81 void read(); 82 83 // Writing initiation and execution. 84 85 void start_write(uint8_t buf[], unsigned int total, int stop = 0); 86 void write(); 87 88 // Transaction control. 89 90 void stop(); 91 void disable(); 92 93 // Specific status conditions. 94 95 unsigned int have_read(); 96 unsigned int have_written(); 97 int read_done(); 98 int write_done(); 99 100 int failed(); 101 int read_failed(); 102 int write_failed(); 103 }; 104 105 // I2C device control. 106 107 class I2c_chip 108 { 109 protected: 110 l4_addr_t _start, _end; 111 Cpm_chip *_cpm; 112 uint32_t _frequency; 113 114 virtual unsigned int num_channels() = 0; 115 116 public: 117 explicit I2c_chip(l4_addr_t start, l4_addr_t end, Cpm_chip *cpm, 118 uint32_t frequency); 119 120 I2c_channel *get_channel(uint8_t channel); 121 }; 122 123 #endif /* __cplusplus */