paul@114 | 1 | /* |
paul@114 | 2 | * I2C support for the JZ4730. |
paul@114 | 3 | * |
paul@114 | 4 | * Copyright (C) 2017, 2018, 2019, 2020 Paul Boddie <paul@boddie.org.uk> |
paul@114 | 5 | * |
paul@114 | 6 | * This program is free software; you can redistribute it and/or |
paul@114 | 7 | * modify it under the terms of the GNU General Public License as |
paul@114 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@114 | 9 | * the License, or (at your option) any later version. |
paul@114 | 10 | * |
paul@114 | 11 | * This program is distributed in the hope that it will be useful, |
paul@114 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@114 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@114 | 14 | * GNU General Public License for more details. |
paul@114 | 15 | * |
paul@114 | 16 | * You should have received a copy of the GNU General Public License |
paul@114 | 17 | * along with this program; if not, write to the Free Software |
paul@114 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@114 | 19 | * Boston, MA 02110-1301, USA |
paul@114 | 20 | */ |
paul@114 | 21 | |
paul@114 | 22 | #pragma once |
paul@114 | 23 | |
paul@114 | 24 | #include <l4/sys/types.h> |
paul@114 | 25 | #include <stdint.h> |
paul@114 | 26 | |
paul@114 | 27 | |
paul@114 | 28 | |
paul@114 | 29 | #ifdef __cplusplus |
paul@114 | 30 | |
paul@114 | 31 | #include <l4/devices/cpm-jz4730.h> |
paul@114 | 32 | #include <l4/devices/hw_mmio_register_block.h> |
paul@114 | 33 | |
paul@136 | 34 | |
paul@136 | 35 | |
paul@136 | 36 | // State machine states. |
paul@136 | 37 | |
paul@136 | 38 | enum I2c_jz4730_state |
paul@136 | 39 | { |
paul@136 | 40 | I2c_jz4730_end = 0, |
paul@136 | 41 | I2c_jz4730_pre_start, |
paul@136 | 42 | I2c_jz4730_start_read, |
paul@136 | 43 | I2c_jz4730_perform_read, |
paul@136 | 44 | I2c_jz4730_perform_write, |
paul@136 | 45 | I2c_jz4730_stop_write |
paul@136 | 46 | }; |
paul@136 | 47 | |
paul@136 | 48 | |
paul@136 | 49 | |
paul@114 | 50 | // I2C channel. |
paul@114 | 51 | |
paul@114 | 52 | class I2c_jz4730_channel |
paul@114 | 53 | { |
paul@114 | 54 | private: |
paul@114 | 55 | Hw::Register_block<32> _regs; |
paul@114 | 56 | Cpm_jz4730_chip *_cpm; |
paul@114 | 57 | uint32_t _frequency; |
paul@118 | 58 | l4_cap_idx_t _irq=L4_INVALID_CAP; |
paul@114 | 59 | |
paul@114 | 60 | public: |
paul@114 | 61 | I2c_jz4730_channel(l4_addr_t start, Cpm_jz4730_chip *cpm, |
paul@118 | 62 | uint32_t frequency, l4_cap_idx_t irq); |
paul@114 | 63 | |
paul@114 | 64 | void disable(); |
paul@114 | 65 | void enable(); |
paul@114 | 66 | |
paul@114 | 67 | unsigned int read(uint8_t address, uint8_t buf[], unsigned int length); |
paul@114 | 68 | unsigned int write(uint8_t address, uint8_t buf[], unsigned int length); |
paul@114 | 69 | |
paul@137 | 70 | // Statistics. |
paul@137 | 71 | |
paul@137 | 72 | unsigned int start_not_possible = 0, read_not_ready = 0, read_not_ready_clear = 0, read_not_ready_stx = 0, |
paul@137 | 73 | read_not_possible = 0, read_nack = 0, read_complete = 0, |
paul@137 | 74 | write_not_possible = 0, write_complete = 0, total_reads = 0; |
paul@137 | 75 | |
paul@114 | 76 | protected: |
paul@114 | 77 | void set_frequency(); |
paul@114 | 78 | |
paul@136 | 79 | // State machine activities. |
paul@136 | 80 | |
paul@136 | 81 | unsigned int _nread, _nwritten, _length, _limit; |
paul@136 | 82 | uint8_t _address; |
paul@136 | 83 | uint8_t *_buf; |
paul@136 | 84 | bool _read; |
paul@136 | 85 | |
paul@136 | 86 | void communicate(); |
paul@136 | 87 | |
paul@136 | 88 | enum I2c_jz4730_state pre_start(); |
paul@136 | 89 | enum I2c_jz4730_state start_read(); |
paul@136 | 90 | enum I2c_jz4730_state perform_read(); |
paul@136 | 91 | enum I2c_jz4730_state perform_write(); |
paul@136 | 92 | enum I2c_jz4730_state stop_write(); |
paul@136 | 93 | |
paul@114 | 94 | // Transaction control. |
paul@114 | 95 | |
paul@118 | 96 | bool set_address(uint8_t address, bool read); |
paul@114 | 97 | |
paul@118 | 98 | void clear_next(); |
paul@114 | 99 | void send_next(); |
paul@114 | 100 | void signal_last(); |
paul@114 | 101 | |
paul@114 | 102 | void start(); |
paul@114 | 103 | void stop(); |
paul@114 | 104 | |
paul@118 | 105 | bool wait_for_irq(unsigned int timeout); |
paul@118 | 106 | |
paul@114 | 107 | // Specific status conditions. |
paul@114 | 108 | |
paul@118 | 109 | bool busy(); |
paul@114 | 110 | bool data_valid(); |
paul@114 | 111 | bool nack(); |
paul@114 | 112 | bool transferred(); |
paul@118 | 113 | bool transferring(); |
paul@114 | 114 | }; |
paul@114 | 115 | |
paul@114 | 116 | // I2C device control. |
paul@114 | 117 | |
paul@114 | 118 | class I2c_jz4730_chip |
paul@114 | 119 | { |
paul@114 | 120 | private: |
paul@114 | 121 | l4_addr_t _start, _end; |
paul@114 | 122 | Cpm_jz4730_chip *_cpm; |
paul@114 | 123 | uint32_t _frequency; |
paul@114 | 124 | |
paul@114 | 125 | public: |
paul@114 | 126 | I2c_jz4730_chip(l4_addr_t start, l4_addr_t end, Cpm_jz4730_chip *cpm, |
paul@114 | 127 | uint32_t frequency); |
paul@114 | 128 | |
paul@118 | 129 | I2c_jz4730_channel *get_channel(uint8_t channel, l4_cap_idx_t irq); |
paul@114 | 130 | }; |
paul@114 | 131 | |
paul@114 | 132 | #endif /* __cplusplus */ |
paul@114 | 133 | |
paul@114 | 134 | |
paul@114 | 135 | |
paul@114 | 136 | /* C language interface. */ |
paul@114 | 137 | |
paul@114 | 138 | EXTERN_C_BEGIN |
paul@114 | 139 | |
paul@114 | 140 | void *jz4730_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, |
paul@114 | 141 | uint32_t frequency); |
paul@114 | 142 | |
paul@118 | 143 | void *jz4730_i2c_get_channel(void *i2c, uint8_t channel, l4_cap_idx_t irq); |
paul@114 | 144 | |
paul@114 | 145 | void jz4730_i2c_disable(void *i2c_channel); |
paul@114 | 146 | |
paul@114 | 147 | void jz4730_i2c_enable(void *i2c_channel); |
paul@114 | 148 | |
paul@114 | 149 | unsigned int jz4730_i2c_read(void *i2c_channel, uint8_t address, uint8_t buf[], unsigned int length); |
paul@114 | 150 | |
paul@114 | 151 | unsigned int jz4730_i2c_write(void *i2c_channel, uint8_t address, uint8_t buf[], unsigned int length); |
paul@114 | 152 | |
paul@137 | 153 | unsigned int jz4730_i2c_start_not_possible(void *i2c_channel); |
paul@137 | 154 | |
paul@137 | 155 | unsigned int jz4730_i2c_read_not_ready(void *i2c_channel); |
paul@137 | 156 | |
paul@137 | 157 | unsigned int jz4730_i2c_read_not_ready_clear(void *i2c_channel); |
paul@137 | 158 | |
paul@137 | 159 | unsigned int jz4730_i2c_read_not_ready_stx(void *i2c_channel); |
paul@137 | 160 | |
paul@137 | 161 | unsigned int jz4730_i2c_read_not_possible(void *i2c_channel); |
paul@137 | 162 | |
paul@137 | 163 | unsigned int jz4730_i2c_read_nack(void *i2c_channel); |
paul@137 | 164 | |
paul@137 | 165 | unsigned int jz4730_i2c_read_complete(void *i2c_channel); |
paul@137 | 166 | |
paul@137 | 167 | unsigned int jz4730_i2c_write_not_possible(void *i2c_channel); |
paul@137 | 168 | |
paul@137 | 169 | unsigned int jz4730_i2c_write_complete(void *i2c_channel); |
paul@137 | 170 | |
paul@137 | 171 | unsigned int jz4730_i2c_total_reads(void *i2c_channel); |
paul@137 | 172 | |
paul@114 | 173 | EXTERN_C_END |