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 l4_cap_idx_t _irq=L4_INVALID_CAP; 43 44 public: 45 I2c_jz4730_channel(l4_addr_t start, Cpm_jz4730_chip *cpm, 46 uint32_t frequency, l4_cap_idx_t irq); 47 48 void disable(); 49 void enable(); 50 51 unsigned int read(uint8_t address, uint8_t buf[], unsigned int length); 52 unsigned int write(uint8_t address, uint8_t buf[], unsigned int length); 53 54 protected: 55 void set_frequency(); 56 57 // Transaction control. 58 59 bool set_address(uint8_t address, bool read); 60 61 void clear_next(); 62 void send_next(); 63 void signal_last(); 64 65 void start(); 66 void stop(); 67 68 bool wait_for_irq(unsigned int timeout); 69 70 // Specific status conditions. 71 72 bool busy(); 73 bool data_valid(); 74 bool nack(); 75 bool transferred(); 76 bool transferring(); 77 }; 78 79 // I2C device control. 80 81 class I2c_jz4730_chip 82 { 83 private: 84 l4_addr_t _start, _end; 85 Cpm_jz4730_chip *_cpm; 86 uint32_t _frequency; 87 88 public: 89 I2c_jz4730_chip(l4_addr_t start, l4_addr_t end, Cpm_jz4730_chip *cpm, 90 uint32_t frequency); 91 92 I2c_jz4730_channel *get_channel(uint8_t channel, l4_cap_idx_t irq); 93 }; 94 95 #endif /* __cplusplus */ 96 97 98 99 /* C language interface. */ 100 101 EXTERN_C_BEGIN 102 103 void *jz4730_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, 104 uint32_t frequency); 105 106 void *jz4730_i2c_get_channel(void *i2c, uint8_t channel, l4_cap_idx_t irq); 107 108 void jz4730_i2c_disable(void *i2c_channel); 109 110 void jz4730_i2c_enable(void *i2c_channel); 111 112 unsigned int jz4730_i2c_read(void *i2c_channel, uint8_t address, uint8_t buf[], unsigned int length); 113 114 unsigned int jz4730_i2c_write(void *i2c_channel, uint8_t address, uint8_t buf[], unsigned int length); 115 116 EXTERN_C_END