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