1 /* 2 * I2C support for the JZ4730. 3 * 4 * Copyright (C) 2017, 2018, 2019, 2020 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-jz4730.h> 32 #include <l4/devices/hw_mmio_register_block.h> 33 34 // I2C channel. 35 36 class I2c_jz4730_channel 37 { 38 private: 39 Hw::Register_block<32> _regs; 40 Cpm_jz4730_chip *_cpm; 41 uint32_t _frequency; 42 43 public: 44 I2c_jz4730_channel(l4_addr_t start, Cpm_jz4730_chip *cpm, 45 uint32_t frequency); 46 47 void disable(); 48 void enable(); 49 50 unsigned int read(uint8_t address, uint8_t buf[], unsigned int length); 51 unsigned int write(uint8_t address, uint8_t buf[], unsigned int length); 52 53 protected: 54 void set_frequency(); 55 56 // Transaction control. 57 58 void set_address(uint8_t address, bool read); 59 60 void request_next(); 61 void send_next(); 62 void signal_last(); 63 64 void start(); 65 void stop(); 66 67 // Specific status conditions. 68 69 bool data_valid(); 70 bool nack(); 71 bool transferred(); 72 }; 73 74 // I2C device control. 75 76 class I2c_jz4730_chip 77 { 78 private: 79 l4_addr_t _start, _end; 80 Cpm_jz4730_chip *_cpm; 81 uint32_t _frequency; 82 83 public: 84 I2c_jz4730_chip(l4_addr_t start, l4_addr_t end, Cpm_jz4730_chip *cpm, 85 uint32_t frequency); 86 87 I2c_jz4730_channel *get_channel(uint8_t channel); 88 }; 89 90 #endif /* __cplusplus */ 91 92 93 94 /* C language interface. */ 95 96 EXTERN_C_BEGIN 97 98 void *jz4730_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, 99 uint32_t frequency); 100 101 void *jz4730_i2c_get_channel(void *i2c, uint8_t channel); 102 103 void jz4730_i2c_disable(void *i2c_channel); 104 105 void jz4730_i2c_enable(void *i2c_channel); 106 107 unsigned int jz4730_i2c_read(void *i2c_channel, uint8_t address, uint8_t buf[], unsigned int length); 108 109 unsigned int jz4730_i2c_write(void *i2c_channel, uint8_t address, uint8_t buf[], unsigned int length); 110 111 EXTERN_C_END