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@114 | 70 | protected: |
paul@114 | 71 | void set_frequency(); |
paul@114 | 72 | |
paul@136 | 73 | // State machine activities. |
paul@136 | 74 | |
paul@136 | 75 | unsigned int _nread, _nwritten, _length, _limit; |
paul@136 | 76 | uint8_t _address; |
paul@136 | 77 | uint8_t *_buf; |
paul@136 | 78 | bool _read; |
paul@136 | 79 | |
paul@136 | 80 | void communicate(); |
paul@136 | 81 | |
paul@136 | 82 | enum I2c_jz4730_state pre_start(); |
paul@136 | 83 | enum I2c_jz4730_state start_read(); |
paul@136 | 84 | enum I2c_jz4730_state perform_read(); |
paul@136 | 85 | enum I2c_jz4730_state perform_write(); |
paul@136 | 86 | enum I2c_jz4730_state stop_write(); |
paul@136 | 87 | |
paul@114 | 88 | // Transaction control. |
paul@114 | 89 | |
paul@118 | 90 | bool set_address(uint8_t address, bool read); |
paul@114 | 91 | |
paul@118 | 92 | void clear_next(); |
paul@114 | 93 | void send_next(); |
paul@114 | 94 | void signal_last(); |
paul@114 | 95 | |
paul@114 | 96 | void start(); |
paul@114 | 97 | void stop(); |
paul@114 | 98 | |
paul@118 | 99 | bool wait_for_irq(unsigned int timeout); |
paul@118 | 100 | |
paul@114 | 101 | // Specific status conditions. |
paul@114 | 102 | |
paul@118 | 103 | bool busy(); |
paul@114 | 104 | bool data_valid(); |
paul@114 | 105 | bool nack(); |
paul@114 | 106 | bool transferred(); |
paul@118 | 107 | bool transferring(); |
paul@114 | 108 | }; |
paul@114 | 109 | |
paul@114 | 110 | // I2C device control. |
paul@114 | 111 | |
paul@114 | 112 | class I2c_jz4730_chip |
paul@114 | 113 | { |
paul@114 | 114 | private: |
paul@114 | 115 | l4_addr_t _start, _end; |
paul@114 | 116 | Cpm_jz4730_chip *_cpm; |
paul@114 | 117 | uint32_t _frequency; |
paul@114 | 118 | |
paul@114 | 119 | public: |
paul@114 | 120 | I2c_jz4730_chip(l4_addr_t start, l4_addr_t end, Cpm_jz4730_chip *cpm, |
paul@114 | 121 | uint32_t frequency); |
paul@114 | 122 | |
paul@118 | 123 | I2c_jz4730_channel *get_channel(uint8_t channel, l4_cap_idx_t irq); |
paul@114 | 124 | }; |
paul@114 | 125 | |
paul@114 | 126 | #endif /* __cplusplus */ |
paul@114 | 127 | |
paul@114 | 128 | |
paul@114 | 129 | |
paul@114 | 130 | /* C language interface. */ |
paul@114 | 131 | |
paul@114 | 132 | EXTERN_C_BEGIN |
paul@114 | 133 | |
paul@114 | 134 | void *jz4730_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, |
paul@114 | 135 | uint32_t frequency); |
paul@114 | 136 | |
paul@118 | 137 | void *jz4730_i2c_get_channel(void *i2c, uint8_t channel, l4_cap_idx_t irq); |
paul@114 | 138 | |
paul@114 | 139 | void jz4730_i2c_disable(void *i2c_channel); |
paul@114 | 140 | |
paul@114 | 141 | void jz4730_i2c_enable(void *i2c_channel); |
paul@114 | 142 | |
paul@114 | 143 | unsigned int jz4730_i2c_read(void *i2c_channel, uint8_t address, uint8_t buf[], unsigned int length); |
paul@114 | 144 | |
paul@114 | 145 | unsigned int jz4730_i2c_write(void *i2c_channel, uint8_t address, uint8_t buf[], unsigned int length); |
paul@114 | 146 | |
paul@114 | 147 | EXTERN_C_END |