paul@0 | 1 | /* |
paul@53 | 2 | * Copyright (C) 2017, 2018, 2019 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 3 | * |
paul@0 | 4 | * This program is free software; you can redistribute it and/or |
paul@0 | 5 | * modify it under the terms of the GNU General Public License as |
paul@0 | 6 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 7 | * the License, or (at your option) any later version. |
paul@0 | 8 | * |
paul@0 | 9 | * This program is distributed in the hope that it will be useful, |
paul@0 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 12 | * GNU General Public License for more details. |
paul@0 | 13 | * |
paul@0 | 14 | * You should have received a copy of the GNU General Public License |
paul@0 | 15 | * along with this program; if not, write to the Free Software |
paul@0 | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 17 | * Boston, MA 02110-1301, USA |
paul@0 | 18 | */ |
paul@0 | 19 | |
paul@0 | 20 | #pragma once |
paul@0 | 21 | |
paul@0 | 22 | #include <l4/sys/types.h> |
paul@0 | 23 | #include <stdint.h> |
paul@0 | 24 | |
paul@0 | 25 | |
paul@0 | 26 | |
paul@0 | 27 | #ifdef __cplusplus |
paul@0 | 28 | |
paul@0 | 29 | #include <l4/devices/cpm-jz4780.h> |
paul@0 | 30 | #include <l4/devices/hw_mmio_register_block.h> |
paul@0 | 31 | |
paul@0 | 32 | // I2C channel. |
paul@0 | 33 | |
paul@0 | 34 | class I2c_jz4780_channel |
paul@0 | 35 | { |
paul@0 | 36 | private: |
paul@0 | 37 | Hw::Register_block<32> _regs; |
paul@0 | 38 | Cpm_jz4780_chip *_cpm; |
paul@0 | 39 | uint32_t _frequency; |
paul@53 | 40 | |
paul@53 | 41 | // Buffer management. |
paul@53 | 42 | |
paul@53 | 43 | unsigned int _pos, _reqpos, _total; |
paul@0 | 44 | uint8_t *_buf; |
paul@53 | 45 | |
paul@53 | 46 | // Status conditions. |
paul@53 | 47 | |
paul@53 | 48 | int _fail; |
paul@0 | 49 | |
paul@0 | 50 | public: |
paul@0 | 51 | I2c_jz4780_channel(l4_addr_t start, Cpm_jz4780_chip *cpm, |
paul@0 | 52 | uint32_t frequency); |
paul@0 | 53 | |
paul@0 | 54 | void set_target(uint8_t addr); |
paul@56 | 55 | |
paul@56 | 56 | // Reading initiation and execution. |
paul@56 | 57 | |
paul@53 | 58 | void start_read(uint8_t buf[], unsigned int total); |
paul@0 | 59 | void read(); |
paul@56 | 60 | |
paul@56 | 61 | // Writing initiation and execution. |
paul@56 | 62 | |
paul@56 | 63 | void start_write(uint8_t buf[], unsigned int total); |
paul@56 | 64 | void write(); |
paul@53 | 65 | |
paul@53 | 66 | // Transaction control. |
paul@53 | 67 | |
paul@53 | 68 | void stop(); |
paul@53 | 69 | void disable(); |
paul@53 | 70 | |
paul@53 | 71 | // Specific status conditions. |
paul@53 | 72 | |
paul@53 | 73 | unsigned int have_read(); |
paul@56 | 74 | unsigned int have_written(); |
paul@0 | 75 | int read_done(); |
paul@56 | 76 | int write_done(); |
paul@56 | 77 | |
paul@56 | 78 | int failed(); |
paul@53 | 79 | int read_failed(); |
paul@53 | 80 | int write_failed(); |
paul@0 | 81 | |
paul@0 | 82 | private: |
paul@0 | 83 | void enable(); |
paul@0 | 84 | |
paul@0 | 85 | int active(); |
paul@0 | 86 | int have_input(); |
paul@0 | 87 | int have_output(); |
paul@0 | 88 | int can_send(); |
paul@0 | 89 | |
paul@0 | 90 | void reset_flags(); |
paul@0 | 91 | void init_parameters(); |
paul@0 | 92 | void set_frequency(); |
paul@0 | 93 | |
paul@0 | 94 | void set_read_threshold(); |
paul@0 | 95 | void queue_reads(); |
paul@56 | 96 | void queue_writes(); |
paul@0 | 97 | void store_reads(); |
paul@0 | 98 | }; |
paul@0 | 99 | |
paul@0 | 100 | // I2C device control. |
paul@0 | 101 | |
paul@0 | 102 | class I2c_jz4780_chip |
paul@0 | 103 | { |
paul@0 | 104 | private: |
paul@0 | 105 | l4_addr_t _start, _end; |
paul@0 | 106 | Cpm_jz4780_chip *_cpm; |
paul@0 | 107 | uint32_t _frequency; |
paul@0 | 108 | |
paul@0 | 109 | public: |
paul@0 | 110 | I2c_jz4780_chip(l4_addr_t start, l4_addr_t end, Cpm_jz4780_chip *cpm, |
paul@0 | 111 | uint32_t frequency); |
paul@0 | 112 | |
paul@0 | 113 | I2c_jz4780_channel *get_channel(uint8_t channel); |
paul@0 | 114 | }; |
paul@0 | 115 | |
paul@0 | 116 | #endif /* __cplusplus */ |
paul@0 | 117 | |
paul@0 | 118 | |
paul@0 | 119 | |
paul@0 | 120 | /* C language interface. */ |
paul@0 | 121 | |
paul@0 | 122 | EXTERN_C_BEGIN |
paul@0 | 123 | |
paul@0 | 124 | void *jz4780_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, |
paul@0 | 125 | uint32_t frequency); |
paul@0 | 126 | |
paul@53 | 127 | void jz4780_i2c_disable(void *i2c_channel); |
paul@53 | 128 | |
paul@0 | 129 | void *jz4780_i2c_get_channel(void *i2c, uint8_t channel); |
paul@0 | 130 | |
paul@0 | 131 | void jz4780_i2c_set_target(void *i2c_channel, uint8_t addr); |
paul@53 | 132 | |
paul@53 | 133 | void jz4780_i2c_start_read(void *i2c_channel, uint8_t buf[], unsigned int total); |
paul@53 | 134 | |
paul@0 | 135 | void jz4780_i2c_read(void *i2c_channel); |
paul@53 | 136 | |
paul@56 | 137 | void jz4780_i2c_start_write(void *i2c_channel, uint8_t buf[], unsigned int total); |
paul@56 | 138 | |
paul@56 | 139 | void jz4780_i2c_write(void *i2c_channel); |
paul@56 | 140 | |
paul@0 | 141 | int jz4780_i2c_read_done(void *i2c_channel); |
paul@53 | 142 | |
paul@56 | 143 | int jz4780_i2c_write_done(void *i2c_channel); |
paul@56 | 144 | |
paul@53 | 145 | unsigned int jz4780_i2c_have_read(void *i2c_channel); |
paul@53 | 146 | |
paul@56 | 147 | unsigned int jz4780_i2c_have_written(void *i2c_channel); |
paul@53 | 148 | |
paul@56 | 149 | int jz4780_i2c_failed(void *i2c_channel); |
paul@53 | 150 | |
paul@0 | 151 | void jz4780_i2c_stop(void *i2c_channel); |
paul@0 | 152 | |
paul@0 | 153 | EXTERN_C_END |