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@214 | 40 | Cpm_chip *_cpm; |
paul@214 | 41 | uint32_t _frequency; |
paul@214 | 42 | |
paul@214 | 43 | // Buffer management. |
paul@214 | 44 | |
paul@214 | 45 | unsigned int _pos, _reqpos, _total; |
paul@214 | 46 | uint8_t *_buf; |
paul@214 | 47 | |
paul@214 | 48 | // Status conditions. |
paul@214 | 49 | |
paul@214 | 50 | int _fail; |
paul@214 | 51 | int _stop; |
paul@214 | 52 | |
paul@214 | 53 | // Common operations. |
paul@214 | 54 | |
paul@214 | 55 | void enable(); |
paul@214 | 56 | |
paul@214 | 57 | int active(); |
paul@214 | 58 | int have_input(); |
paul@214 | 59 | int have_output(); |
paul@214 | 60 | int can_send(); |
paul@214 | 61 | |
paul@214 | 62 | void reset_flags(); |
paul@214 | 63 | void init_parameters(); |
paul@214 | 64 | void set_frequency(); |
paul@214 | 65 | |
paul@214 | 66 | void set_read_threshold(); |
paul@214 | 67 | void queue_reads(); |
paul@214 | 68 | void queue_writes(); |
paul@214 | 69 | void store_reads(); |
paul@214 | 70 | |
paul@214 | 71 | public: |
paul@214 | 72 | explicit I2c_channel(l4_addr_t start, enum Clock_identifiers clock, |
paul@214 | 73 | Cpm_chip *cpm, uint32_t frequency); |
paul@214 | 74 | |
paul@214 | 75 | uint32_t get_frequency(); |
paul@214 | 76 | void set_target(uint8_t addr); |
paul@214 | 77 | |
paul@214 | 78 | // Reading initiation and execution. |
paul@214 | 79 | |
paul@214 | 80 | void start_read(uint8_t buf[], unsigned int total, int stop = 0); |
paul@214 | 81 | void read(); |
paul@214 | 82 | |
paul@214 | 83 | // Writing initiation and execution. |
paul@214 | 84 | |
paul@214 | 85 | void start_write(uint8_t buf[], unsigned int total, int stop = 0); |
paul@214 | 86 | void write(); |
paul@214 | 87 | |
paul@214 | 88 | // Transaction control. |
paul@214 | 89 | |
paul@214 | 90 | void stop(); |
paul@214 | 91 | void disable(); |
paul@214 | 92 | |
paul@214 | 93 | // Specific status conditions. |
paul@214 | 94 | |
paul@214 | 95 | unsigned int have_read(); |
paul@214 | 96 | unsigned int have_written(); |
paul@214 | 97 | int read_done(); |
paul@214 | 98 | int write_done(); |
paul@214 | 99 | |
paul@214 | 100 | int failed(); |
paul@214 | 101 | int read_failed(); |
paul@214 | 102 | int write_failed(); |
paul@214 | 103 | }; |
paul@214 | 104 | |
paul@214 | 105 | // I2C device control. |
paul@214 | 106 | |
paul@214 | 107 | class I2c_chip |
paul@214 | 108 | { |
paul@214 | 109 | protected: |
paul@214 | 110 | l4_addr_t _start, _end; |
paul@214 | 111 | Cpm_chip *_cpm; |
paul@214 | 112 | uint32_t _frequency; |
paul@214 | 113 | |
paul@214 | 114 | virtual unsigned int num_channels() = 0; |
paul@214 | 115 | |
paul@214 | 116 | public: |
paul@214 | 117 | explicit I2c_chip(l4_addr_t start, l4_addr_t end, Cpm_chip *cpm, |
paul@214 | 118 | uint32_t frequency); |
paul@214 | 119 | |
paul@214 | 120 | I2c_channel *get_channel(uint8_t channel); |
paul@214 | 121 | }; |
paul@214 | 122 | |
paul@214 | 123 | #endif /* __cplusplus */ |