1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/pkg/devices/lib/i2c/include/i2c-x1600.h Tue Sep 26 21:27:50 2023 +0200
1.3 @@ -0,0 +1,161 @@
1.4 +/*
1.5 + * I2C support for the X1600.
1.6 + *
1.7 + * Copyright (C) 2017, 2018, 2019, 2023 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#pragma once
1.26 +
1.27 +#include <l4/sys/types.h>
1.28 +#include <stdint.h>
1.29 +
1.30 +
1.31 +
1.32 +#ifdef __cplusplus
1.33 +
1.34 +#include <l4/devices/cpm-x1600.h>
1.35 +#include <l4/devices/hw_mmio_register_block.h>
1.36 +
1.37 +// I2C channel.
1.38 +
1.39 +class I2c_x1600_channel
1.40 +{
1.41 +private:
1.42 + Hw::Register_block<32> _regs;
1.43 + Cpm_x1600_chip *_cpm;
1.44 + uint32_t _frequency;
1.45 +
1.46 + // Buffer management.
1.47 +
1.48 + unsigned int _pos, _reqpos, _total;
1.49 + uint8_t *_buf;
1.50 +
1.51 + // Status conditions.
1.52 +
1.53 + int _fail;
1.54 + int _stop;
1.55 +
1.56 +public:
1.57 + I2c_x1600_channel(l4_addr_t start, Cpm_x1600_chip *cpm,
1.58 + uint32_t frequency);
1.59 +
1.60 + uint32_t get_frequency();
1.61 + void set_target(uint8_t addr);
1.62 +
1.63 + // Reading initiation and execution.
1.64 +
1.65 + void start_read(uint8_t buf[], unsigned int total, int stop = 0);
1.66 + void read();
1.67 +
1.68 + // Writing initiation and execution.
1.69 +
1.70 + void start_write(uint8_t buf[], unsigned int total, int stop = 0);
1.71 + void write();
1.72 +
1.73 + // Transaction control.
1.74 +
1.75 + void stop();
1.76 + void disable();
1.77 +
1.78 + // Specific status conditions.
1.79 +
1.80 + unsigned int have_read();
1.81 + unsigned int have_written();
1.82 + int read_done();
1.83 + int write_done();
1.84 +
1.85 + int failed();
1.86 + int read_failed();
1.87 + int write_failed();
1.88 +
1.89 +private:
1.90 + void enable();
1.91 +
1.92 + int active();
1.93 + int have_input();
1.94 + int have_output();
1.95 + int can_send();
1.96 +
1.97 + void reset_flags();
1.98 + void init_parameters();
1.99 + void set_frequency();
1.100 +
1.101 + void set_read_threshold();
1.102 + void queue_reads();
1.103 + void queue_writes();
1.104 + void store_reads();
1.105 +};
1.106 +
1.107 +// I2C device control.
1.108 +
1.109 +class I2c_x1600_chip
1.110 +{
1.111 +private:
1.112 + l4_addr_t _start, _end;
1.113 + Cpm_x1600_chip *_cpm;
1.114 + uint32_t _frequency;
1.115 +
1.116 +public:
1.117 + I2c_x1600_chip(l4_addr_t start, l4_addr_t end, Cpm_x1600_chip *cpm,
1.118 + uint32_t frequency);
1.119 +
1.120 + I2c_x1600_channel *get_channel(uint8_t channel);
1.121 +};
1.122 +
1.123 +#endif /* __cplusplus */
1.124 +
1.125 +
1.126 +
1.127 +/* C language interface. */
1.128 +
1.129 +EXTERN_C_BEGIN
1.130 +
1.131 +void *x1600_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm,
1.132 + uint32_t frequency);
1.133 +
1.134 +void x1600_i2c_disable(void *i2c_channel);
1.135 +
1.136 +void *x1600_i2c_get_channel(void *i2c, uint8_t channel);
1.137 +
1.138 +uint32_t x1600_i2c_get_frequency(void *i2c_channel);
1.139 +
1.140 +void x1600_i2c_set_target(void *i2c_channel, uint8_t addr);
1.141 +
1.142 +void x1600_i2c_start_read(void *i2c_channel, uint8_t buf[], unsigned int total,
1.143 + int stop);
1.144 +
1.145 +void x1600_i2c_read(void *i2c_channel);
1.146 +
1.147 +void x1600_i2c_start_write(void *i2c_channel, uint8_t buf[], unsigned int total,
1.148 + int stop);
1.149 +
1.150 +void x1600_i2c_write(void *i2c_channel);
1.151 +
1.152 +int x1600_i2c_read_done(void *i2c_channel);
1.153 +
1.154 +int x1600_i2c_write_done(void *i2c_channel);
1.155 +
1.156 +unsigned int x1600_i2c_have_read(void *i2c_channel);
1.157 +
1.158 +unsigned int x1600_i2c_have_written(void *i2c_channel);
1.159 +
1.160 +int x1600_i2c_failed(void *i2c_channel);
1.161 +
1.162 +void x1600_i2c_stop(void *i2c_channel);
1.163 +
1.164 +EXTERN_C_END
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/pkg/devices/lib/i2c/src/x1600.cc Tue Sep 26 21:27:50 2023 +0200
3.3 @@ -0,0 +1,803 @@
3.4 +/*
3.5 + * I2C support for the X1600.
3.6 + *
3.7 + * Copyright (C) 2017, 2018, 2021, 2023 Paul Boddie <paul@boddie.org.uk>
3.8 + *
3.9 + * This program is free software; you can redistribute it and/or
3.10 + * modify it under the terms of the GNU General Public License as
3.11 + * published by the Free Software Foundation; either version 2 of
3.12 + * the License, or (at your option) any later version.
3.13 + *
3.14 + * This program is distributed in the hope that it will be useful,
3.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.17 + * GNU General Public License for more details.
3.18 + *
3.19 + * You should have received a copy of the GNU General Public License
3.20 + * along with this program; if not, write to the Free Software
3.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
3.22 + * Boston, MA 02110-1301, USA
3.23 + */
3.24 +
3.25 +#include <l4/devices/i2c-x1600.h>
3.26 +#include <l4/devices/hw_mmio_register_block.h>
3.27 +
3.28 +#include <l4/sys/icu.h>
3.29 +#include <l4/util/util.h>
3.30 +#include <sys/time.h>
3.31 +
3.32 +#include <stdio.h>
3.33 +
3.34 +/* NOTE: This peripheral is very similar to the JZ4780 with the registers
3.35 + renamed to I2C from I2C, with a few high speed registers added, and
3.36 + with I2C_SDAHD appearing at a different location. */
3.37 +
3.38 +enum Regs
3.39 +{
3.40 + I2c_control = 0x000, // I2C_CON
3.41 + I2c_target_address = 0x004, // I2C_TAR
3.42 + I2c_slave_address = 0x008, // I2C_SAR
3.43 + I2c_master_code = 0x00c, // I2C_HS_MADDR
3.44 + I2c_data_command = 0x010, // I2C_DC
3.45 + Std_high_count = 0x014, // I2C_SHCNT
3.46 + Std_low_count = 0x018, // I2C_SLCNT
3.47 + Fast_high_count = 0x01c, // I2C_FHCNT
3.48 + Fast_low_count = 0x020, // I2C_FLCNT
3.49 + High_high_count = 0x024, // I2C_HHCNT
3.50 + High_low_count = 0x028, // I2C_HLCNT
3.51 + Int_status = 0x02c, // I2C_INTST (read-only)
3.52 + Int_mask = 0x030, // I2C_INTM
3.53 + Int_raw_status = 0x034, // I2C_RINTST (read-only)
3.54 + Rx_fifo_thold = 0x038, // I2C_RXTL
3.55 + Tx_fifo_thold = 0x03c, // I2C_TXTL
3.56 + Int_combined_clear = 0x040, // I2C_CINT (read-only)
3.57 + Int_rx_uf_clear = 0x044, // I2C_CRXUF (read-only)
3.58 + Int_rx_of_clear = 0x048, // I2C_CRXOF (read-only)
3.59 + Int_tx_of_clear = 0x04c, // I2C_CTXOF (read-only)
3.60 + Int_rd_req_clear = 0x050, // I2C_CRXREQ (read-only)
3.61 + Int_tx_abort_clear = 0x054, // I2C_CTXABT (read-only)
3.62 + Int_rx_done_clear = 0x058, // I2C_CRXDN (read-only)
3.63 + Int_activity_clear = 0x05c, // I2C_CACT (read-only)
3.64 + Int_stop_clear = 0x060, // I2C_CSTP (read-only)
3.65 + Int_start_clear = 0x064, // I2C_CSTT (read-only)
3.66 + Int_call_clear = 0x068, // I2C_CGC (read-only)
3.67 + I2c_enable = 0x06c, // I2C_ENB
3.68 + I2c_status = 0x070, // I2C_ST (read-only)
3.69 + Tx_fifo_count = 0x074, // I2C_TXFLR (read-only)
3.70 + Rx_fifo_count = 0x078, // I2C_RXFLR (read-only)
3.71 + I2c_sda_hold_time = 0x07c, // I2C_SDAHD
3.72 + Trans_abort_status = 0x080, // I2C_ABTSRC (read-only)
3.73 + Slv_data_nack = 0x084, // I2CSDNACK
3.74 + I2c_dma_ctrl = 0x088, // I2C_DMACR
3.75 + I2c_trans_data_lvl = 0x08c, // I2C_DMATDLR
3.76 + I2c_recv_data_lvl = 0x090, // I2C_DMARDLR
3.77 + I2c_sda_setup_time = 0x094, // I2C_SDASU
3.78 + I2c_ack_call = 0x098, // I2C_ACKGC
3.79 + I2c_enable_status = 0x09c, // I2C_ENBST (read-only)
3.80 + I2c_spike_suppress = 0x0a0, // I2C_FSPKLEN
3.81 +
3.82 + I2c_block_offset = 0x1000
3.83 +};
3.84 +
3.85 +enum I2c_control_bits : unsigned
3.86 +{
3.87 + I2c_disable_slave = 0x40, // SLVDIS (slave disabled)
3.88 + I2c_enable_restart = 0x20, // RESTART
3.89 + I2c_master_10bit = 0x10, // MATP (read-only)
3.90 + I2c_slave_10bit = 0x08, // SATP
3.91 + I2c_speed_mode_mask = 0x06, // SPEED
3.92 + I2c_enable_master = 0x01, // MD (master enabled)
3.93 + I2c_speed_bit = 1, // SPD
3.94 +};
3.95 +
3.96 +enum I2c_speed_mode_values : unsigned
3.97 +{
3.98 + I2c_speed_standard = 1,
3.99 + I2c_speed_fast = 2,
3.100 + I2c_speed_high = 3,
3.101 +};
3.102 +
3.103 +enum I2c_enable_bits : unsigned
3.104 +{
3.105 + I2c_enable_enabled = 0x01, // I2CEN
3.106 +};
3.107 +
3.108 +enum I2c_status_bits : unsigned
3.109 +{
3.110 + I2c_status_master_act = 0x20, // MSTACT (master active)
3.111 + I2c_status_rx_nempty = 0x08, // RFNE (read queue not empty)
3.112 + I2c_status_tx_empty = 0x04, // TFE (write queue empty)
3.113 + I2c_status_tx_nfull = 0x02, // TFNF (write queue not full)
3.114 + I2c_status_active = 0x01, // ACT (device active as master or slave)
3.115 +};
3.116 +
3.117 +enum I2c_target_bits : unsigned
3.118 +{
3.119 + I2c_target_master_10bit = 0x1000,
3.120 + I2c_target_special = 0x0800, // SPECIAL: perform general call or start byte
3.121 + I2c_target_start_byte = 0x0400, // Special: start byte (1) or general call (0)
3.122 + I2c_target_10bits = 0x3ff, // Mask for 10-bit address
3.123 + I2c_target_7bits = 0x7f, // Mask for 7-bit address
3.124 +};
3.125 +
3.126 +enum I2c_hold_control_bits : unsigned
3.127 +{
3.128 + /* The hold enable flag has been removed since the JZ4780 and the hold time
3.129 + field widened. */
3.130 +
3.131 + I2c_hold_mask = 0xffff,
3.132 +};
3.133 +
3.134 +enum I2c_setup_control_bits : unsigned
3.135 +{
3.136 + I2c_setup_mask = 0x0ff, // SDASU
3.137 +};
3.138 +
3.139 +enum I2c_command_bits : unsigned
3.140 +{
3.141 + I2c_command_restart = 0x400, // RESTART: explicit restart before next byte
3.142 + I2c_command_stop = 0x200, // STOP: explicit stop after next byte
3.143 + I2c_command_no_stop = 0x000,
3.144 + I2c_command_read = 0x100, // CMD
3.145 + I2c_command_write = 0x000, // CMD
3.146 +};
3.147 +
3.148 +enum I2c_fifo_bits : unsigned
3.149 +{
3.150 + I2c_fifo_limit = 64, // RXTL, TXTL (256 noted in field description)
3.151 +};
3.152 +
3.153 +enum Int_bits : unsigned
3.154 +{
3.155 + Int_call = 0x800, // IGC (general call received)
3.156 + Int_start = 0x400, // ISTT (start/restart condition occurred)
3.157 + Int_stop = 0x200, // ISTP (stop condition occurred)
3.158 + Int_activity = 0x100, // IACT (bus activity interrupt)
3.159 + Int_rx_done = 0x080, // RXDN (read from master device done)
3.160 + Int_tx_abort = 0x040, // TXABT (transmit abort)
3.161 + Int_rd_req = 0x020, // RDREQ (read request from master device)
3.162 + Int_tx_empty = 0x010, // TXEMP (threshold reached or passed)
3.163 + Int_tx_of = 0x008, // TXOF (overflow when writing to queue)
3.164 + Int_rx_full = 0x004, // RXFL (threshold reached or exceeded)
3.165 + Int_rx_of = 0x002, // RXOF (overflow from device)
3.166 + Int_rx_uf = 0x001, // RXUF (underflow when reading from queue)
3.167 +};
3.168 +
3.169 +
3.170 +
3.171 +// Initialise a channel.
3.172 +
3.173 +I2c_x1600_channel::I2c_x1600_channel(l4_addr_t start,
3.174 + Cpm_x1600_chip *cpm,
3.175 + uint32_t frequency)
3.176 +: _cpm(cpm), _frequency(frequency)
3.177 +{
3.178 + _regs = new Hw::Mmio_register_block<32>(start);
3.179 +}
3.180 +
3.181 +// Enable the channel.
3.182 +
3.183 +void
3.184 +I2c_x1600_channel::enable()
3.185 +{
3.186 + _regs[I2c_enable] = I2c_enable_enabled;
3.187 + while (!(_regs[I2c_enable_status] & I2c_enable_enabled));
3.188 +}
3.189 +
3.190 +// Disable the channel.
3.191 +
3.192 +void
3.193 +I2c_x1600_channel::disable()
3.194 +{
3.195 + _regs[I2c_enable] = 0;
3.196 + while (_regs[I2c_enable_status] & I2c_enable_enabled);
3.197 +}
3.198 +
3.199 +// Return the configured frequency.
3.200 +
3.201 +uint32_t
3.202 +I2c_x1600_channel::get_frequency()
3.203 +{
3.204 + return _frequency;
3.205 +}
3.206 +
3.207 +// Set the frequency-related peripheral parameters.
3.208 +
3.209 +void
3.210 +I2c_x1600_channel::set_frequency()
3.211 +{
3.212 + // The APB clock (PCLK) is used to drive I2C transfers. Its value must be
3.213 + // obtained from the CPM unit. It is known as I2C_DEV_CLK here and is scaled
3.214 + // to kHz in order to keep the numbers easily representable, as is the bus
3.215 + // frequency.
3.216 +
3.217 + uint32_t i2c_dev_clk = _cpm->get_frequency(Clock_pclock) / 1000;
3.218 +
3.219 + // Note that this is not I2C_DEV_CLK but the actual I2C bus frequency.
3.220 +
3.221 + uint32_t i2c_clk = _frequency / 1000;
3.222 +
3.223 + // Select the appropriate speed.
3.224 +
3.225 + unsigned int speed = (i2c_clk <= 100) ? I2c_speed_standard
3.226 + : (i2c_clk <= 400 ? I2c_speed_fast
3.227 + : I2c_speed_high);
3.228 +
3.229 + _regs[I2c_control] = _regs[I2c_control] | (speed << I2c_speed_bit) |
3.230 + I2c_disable_slave |
3.231 + I2c_enable_restart |
3.232 + I2c_enable_master;
3.233 +
3.234 + // According to the programming manual, if the PCLK period is T{I2C_DEV_CLK}
3.235 + // then the I2C clock period is...
3.236 +
3.237 + // T{SCL} = T{SCL_high} + T{SCL_low}
3.238 +
3.239 + // Where...
3.240 +
3.241 + // T{SCL_low} = T{I2C_DEV_CLK} * (#cycles for low signal)
3.242 + // T{SCL_high} = T{I2C_DEV_CLK} * (#cycles for high signal)
3.243 +
3.244 + // Since, with minimum periods being defined...
3.245 +
3.246 + // T{SCL} >= T{min_SCL}
3.247 + // T{SCL_low} >= T{min_SCL_low}
3.248 + // T{SCL_high} >= T{min_SCL_high}
3.249 + // T{min_SCL} = T{min_SCL_low} + T{min_SCL_high}
3.250 +
3.251 + // Then the following applies...
3.252 +
3.253 + // T{I2C_DEV_CLK} * (#cycles for low signal)) >= T{min_SCL_low}
3.254 + // T{I2C_DEV_CLK} * (#cycles for high signal) >= T{min_SCL_high}
3.255 +
3.256 + // To work with different clock speeds while maintaining the low-to-high
3.257 + // ratios:
3.258 +
3.259 + // T{min_SCL_low} = T{min_SCL} * T{min_SCL_low} / T{min_SCL}
3.260 + // = T{min_SCL} * (T{min_SCL_low} / (T{min_SCL_low} + T{min_SCL_high}))
3.261 +
3.262 + // T{min_SCL_high} = T{min_SCL} * T{min_SCL_high} / T{min_SCL}
3.263 + // = T{min_SCL} * (T{min_SCL_high} / (T{min_SCL_low} + T{min_SCL_high}))
3.264 +
3.265 + // Constraints are given with respect to the high and low count registers.
3.266 +
3.267 + // #cycles for high signal = I2CxHCNT + 8
3.268 + // #cycles for low signal = I2CxLCNT + 1
3.269 +
3.270 + // From earlier, this yields...
3.271 +
3.272 + // T{I2C_DEV_CLK} * (I2CxLCNT + 1) >= T{min_SCL_low}
3.273 + // T{I2C_DEV_CLK} * (I2CxHCNT + 8) >= T{min_SCL_high}
3.274 +
3.275 + // Rearranging...
3.276 +
3.277 + // I2CxLCNT >= (T{min_SCL_low} / T{I2C_DEV_CLK}) - 1
3.278 + // >= T{min_SCL_low} * I2C_DEV_CLK - 1
3.279 +
3.280 + // I2CxHCNT >= (T{min_SCL_high} / T{I2C_DEV_CLK}) - 8
3.281 + // >= T{min_SCL_high} * I2C_DEV_CLK - 8
3.282 +
3.283 + // Introducing the definitions for the high and low periods...
3.284 +
3.285 + // I2CxLCNT >= T{min_SCL} * (T{min_SCL_low} / (T{min_SCL_low} + T{min_SCL_high})) * I2C_DEV_CLK - 1
3.286 + // >= (T{min_SCL_low} / T{min_SCL}) * I2C_DEV_CLK / I2C_BUS_CLK - 1
3.287 +
3.288 + // I2CxHCNT >= T{min_SCL} * (T{min_SCL_high} / (T{min_SCL_low} + T{min_SCL_high})) * I2C_DEV_CLK - 8
3.289 + // >= (T{min_SCL_high} / T{min_SCL}) * I2C_DEV_CLK / I2C_BUS_CLK - 8
3.290 +
3.291 + uint32_t high_reg, low_reg;
3.292 + uint32_t high_count, low_count;
3.293 + int32_t hold_count;
3.294 + uint32_t setup_count;
3.295 +
3.296 + // Level hold times:
3.297 +
3.298 + // Standard Fast High
3.299 + // SCL low 4.7us 1.3us 0.5us
3.300 + // SCL high 4.0us 0.6us 0.26us +
3.301 + // SCL period 8.7us 1.9us 0.76us =
3.302 +
3.303 + // See: UM10204 "I2C-bus specification and user manual"
3.304 + // Table 10: t{LOW} and t{HIGH}
3.305 +
3.306 + if (i2c_clk <= 100) // 100 kHz
3.307 + {
3.308 + low_count = (i2c_dev_clk * 47) / (i2c_clk * 87) - 1;
3.309 + high_count = (i2c_dev_clk * 40) / (i2c_clk * 87) - 8;
3.310 + low_reg = Std_low_count;
3.311 + high_reg = Std_high_count;
3.312 + }
3.313 + else if (i2c_clk <= 400) // 400 kHz
3.314 + {
3.315 + low_count = (i2c_dev_clk * 13) / (i2c_clk * 19) - 1;
3.316 + high_count = (i2c_dev_clk * 6) / (i2c_clk * 19) - 8;
3.317 + low_reg = Fast_low_count;
3.318 + high_reg = Fast_high_count;
3.319 + }
3.320 + else // > 400 kHz
3.321 + {
3.322 + // Note how the frequencies are scaled to accommodate the extra precision
3.323 + // required.
3.324 +
3.325 + low_count = (i2c_dev_clk / 10 * 50) / (i2c_clk / 10 * 76) - 1;
3.326 + high_count = (i2c_dev_clk / 10 * 26) / (i2c_clk / 10 * 76) - 8;
3.327 + low_reg = High_low_count;
3.328 + high_reg = High_high_count;
3.329 + }
3.330 +
3.331 + // Minimum counts are 8 and 6 for low and high respectively.
3.332 +
3.333 + _regs[low_reg] = low_count < 8 ? 8 : low_count;
3.334 + _regs[high_reg] = high_count < 6 ? 6 : high_count;
3.335 +
3.336 + //printf("low_count: %d\n", low_count);
3.337 + //printf("high_count: %d\n", high_count);
3.338 +
3.339 + // Data hold and setup times:
3.340 +
3.341 + // Standard Fast High
3.342 + // t{HD;DAT} 300ns 300ns 300ns
3.343 + // t{SU;DAT} 250ns 100ns 50ns
3.344 +
3.345 + // See: UM10204 "I2C-bus specification and user manual"
3.346 + // Table 10: t{HD;DAT} and t{SU;DAT}, also note [3]
3.347 +
3.348 + // T{delay} = (I2CSDAHD + 2) * T{I2C_DEV_CLK}
3.349 + // I2CSDAHD = T{delay} / T{I2C_DEV_CLK} - 2
3.350 + // I2CSDAHD = I2C_DEV_CLK * T{delay} - 2
3.351 +
3.352 + // Since the device clock is in kHz (scaled down by 1000) and the times are
3.353 + // given in ns (scaled up by 1000000000), a division of 1000000 is introduced.
3.354 +
3.355 + hold_count = (i2c_dev_clk * 300) / 1000000 - 1;
3.356 +
3.357 + _regs[I2c_sda_hold_time] = (_regs[I2c_sda_hold_time] & ~I2c_hold_mask) |
3.358 + (hold_count < 0 ? 0
3.359 + : (hold_count < (int) I2c_hold_mask ? (uint32_t) hold_count
3.360 + : I2c_hold_mask));
3.361 +
3.362 + //printf("i2c_dev_clk: %d\n", i2c_dev_clk);
3.363 + //printf("SDA hold: %x\n", hold_count);
3.364 +
3.365 + // I2C_SDASU is apparently not used in master mode.
3.366 +
3.367 + // T{delay} = (I2CSDASU - 1) * T{I2C_DEV_CLK}
3.368 + // I2CSDASU = T{delay} / T{I2C_DEV_CLK} + 1
3.369 + // I2CSDASU = I2C_DEV_CLK * T{delay} + 1
3.370 +
3.371 + if (i2c_clk <= 100)
3.372 + setup_count = (i2c_dev_clk * 250) / 1000000 + 1;
3.373 + else if (i2c_clk <= 400)
3.374 + setup_count = (i2c_dev_clk * 100) / 1000000 + 1;
3.375 + else
3.376 + setup_count = (i2c_dev_clk * 50) / 1000000 + 1;
3.377 +
3.378 + _regs[I2c_sda_setup_time] = (_regs[I2c_sda_setup_time] & ~I2c_setup_mask) |
3.379 + (setup_count < I2c_setup_mask ? setup_count : I2c_setup_mask);
3.380 +}
3.381 +
3.382 +// Set the target address and enable transfer.
3.383 +// NOTE: Only supporting 7-bit addresses currently.
3.384 +
3.385 +void
3.386 +I2c_x1600_channel::set_target(uint8_t address)
3.387 +{
3.388 + //printf("set_target: %x\n", address);
3.389 + disable();
3.390 + set_frequency();
3.391 + _regs[I2c_target_address] = address & I2c_target_7bits;
3.392 + enable();
3.393 + init_parameters();
3.394 + //printf("I2c_enable_status: %x\n", (uint32_t) _regs[I2c_enable_status]);
3.395 + //printf("I2c_status: %x\n", (uint32_t) _regs[I2c_status]);
3.396 + //printf("Int_mask: %x\n", (uint32_t) _regs[Int_mask]);
3.397 + //printf("Int_status: %x\n", (uint32_t) _regs[Int_status]);
3.398 + printf("Int_raw_status: %x\n", (uint32_t) _regs[Int_raw_status]);
3.399 +}
3.400 +
3.401 +
3.402 +
3.403 +// Reset interrupt flags upon certain conditions.
3.404 +
3.405 +void
3.406 +I2c_x1600_channel::reset_flags()
3.407 +{
3.408 + volatile uint32_t r;
3.409 +
3.410 + _regs[Int_mask] = 0;
3.411 +
3.412 + // Read from the register to clear interrupts.
3.413 +
3.414 + r = _regs[Int_combined_clear];
3.415 + (void) r;
3.416 +}
3.417 +
3.418 +// Initialise interrupt flags and queue thresholds for reading and writing.
3.419 +
3.420 +void
3.421 +I2c_x1600_channel::init_parameters()
3.422 +{
3.423 + // Handle read queue conditions for data, write queue conditions for commands.
3.424 +
3.425 + reset_flags();
3.426 +
3.427 + _regs[Tx_fifo_thold] = 0; // write when 0 in queue
3.428 +}
3.429 +
3.430 +
3.431 +
3.432 +// Return whether the device is active.
3.433 +
3.434 +int
3.435 +I2c_x1600_channel::active()
3.436 +{
3.437 + return _regs[I2c_status] & I2c_status_master_act;
3.438 +}
3.439 +
3.440 +// Return whether data is available to receive.
3.441 +
3.442 +int
3.443 +I2c_x1600_channel::have_input()
3.444 +{
3.445 + return _regs[I2c_status] & I2c_status_rx_nempty;
3.446 +}
3.447 +
3.448 +// Return whether data is queued for sending.
3.449 +
3.450 +int
3.451 +I2c_x1600_channel::have_output()
3.452 +{
3.453 + return !(_regs[I2c_status] & I2c_status_tx_empty);
3.454 +}
3.455 +
3.456 +// Return whether data can be queued for sending.
3.457 +
3.458 +int
3.459 +I2c_x1600_channel::can_send()
3.460 +{
3.461 + return _regs[I2c_status] & I2c_status_tx_nfull;
3.462 +}
3.463 +
3.464 +// Return whether a receive operation has failed.
3.465 +
3.466 +int
3.467 +I2c_x1600_channel::read_failed()
3.468 +{
3.469 + return _regs[Int_status] & Int_rx_of;
3.470 +}
3.471 +
3.472 +// Return whether a send operation has failed.
3.473 +
3.474 +int
3.475 +I2c_x1600_channel::write_failed()
3.476 +{
3.477 + return _regs[Int_status] & Int_tx_abort;
3.478 +}
3.479 +
3.480 +int
3.481 +I2c_x1600_channel::read_done()
3.482 +{
3.483 + return _pos == _total;
3.484 +}
3.485 +
3.486 +int
3.487 +I2c_x1600_channel::write_done()
3.488 +{
3.489 + return _reqpos == _total;
3.490 +}
3.491 +
3.492 +unsigned
3.493 +I2c_x1600_channel::have_read()
3.494 +{
3.495 + return _pos;
3.496 +}
3.497 +
3.498 +unsigned
3.499 +I2c_x1600_channel::have_written()
3.500 +{
3.501 + return _reqpos;
3.502 +}
3.503 +
3.504 +int
3.505 +I2c_x1600_channel::failed()
3.506 +{
3.507 + return _fail;
3.508 +}
3.509 +
3.510 +
3.511 +
3.512 +// Send read commands for empty queue entries.
3.513 +
3.514 +void
3.515 +I2c_x1600_channel::queue_reads()
3.516 +{
3.517 + unsigned int remaining = _total - _reqpos;
3.518 + unsigned int queued = _reqpos - _pos;
3.519 + unsigned int can_queue = I2c_fifo_limit - queued;
3.520 +
3.521 + // Keep the number of reads in progress below the length of the read queue.
3.522 +
3.523 + if (!can_queue)
3.524 + return;
3.525 +
3.526 + // At most, only queue as many reads as are remaining.
3.527 +
3.528 + if (remaining < can_queue)
3.529 + can_queue = remaining;
3.530 +
3.531 + // Queue read requests for any remaining queue entries.
3.532 +
3.533 + while (can_queue && can_send())
3.534 + {
3.535 + uint32_t stop = _stop && (_reqpos == _total - 1) ? I2c_command_stop : I2c_command_no_stop;
3.536 + printf("Queue read %d/%d %s\n", _reqpos, _total - 1, stop ? "stop" : "continue");
3.537 +
3.538 + _regs[I2c_data_command] = I2c_command_read | stop;
3.539 + _reqpos++;
3.540 + can_queue--;
3.541 + }
3.542 +
3.543 + // Update the threshold to be notified of any reduced remaining amount.
3.544 +
3.545 + set_read_threshold();
3.546 +}
3.547 +
3.548 +// Send write commands for empty queue entries.
3.549 +
3.550 +void
3.551 +I2c_x1600_channel::queue_writes()
3.552 +{
3.553 + unsigned int remaining = _total - _reqpos;
3.554 + unsigned int can_queue = I2c_fifo_limit;
3.555 +
3.556 + if (remaining < can_queue)
3.557 + can_queue = remaining;
3.558 +
3.559 + printf("queue_writes: %d %s\n", can_queue, can_send() ? "can send" : "cannot send");
3.560 +
3.561 + // Queue write requests for any remaining queue entries.
3.562 +
3.563 + while (can_queue && can_send())
3.564 + {
3.565 + uint32_t stop = _stop && (_reqpos == _total - 1) ? I2c_command_stop : I2c_command_no_stop;
3.566 + printf("Queue write %d/%d %s\n", _reqpos, _total - 1, stop ? "stop" : "continue");
3.567 +
3.568 + _regs[I2c_data_command] = I2c_command_write | _buf[_reqpos] | stop;
3.569 + _reqpos++;
3.570 + can_queue--;
3.571 + }
3.572 +
3.573 + printf("Tx_fifo_count = %d\n", (uint32_t) _regs[Tx_fifo_count]);
3.574 +}
3.575 +
3.576 +// Store read command results from the queue.
3.577 +
3.578 +void
3.579 +I2c_x1600_channel::store_reads()
3.580 +{
3.581 + printf("store_reads: %s\n", have_input() ? "input" : "no input");
3.582 +
3.583 + // Read any input and store it in the buffer.
3.584 +
3.585 + while (have_input() && (_pos < _reqpos))
3.586 + {
3.587 + _buf[_pos] = _regs[I2c_data_command] & 0xff;
3.588 + _pos++;
3.589 + }
3.590 +}
3.591 +
3.592 +void
3.593 +I2c_x1600_channel::set_read_threshold()
3.594 +{
3.595 + unsigned int queued = _reqpos - _pos;
3.596 +
3.597 + if (!queued)
3.598 + return;
3.599 +
3.600 + // Read all expected.
3.601 +
3.602 + _regs[Rx_fifo_thold] = queued - 1;
3.603 + printf("Rx_fifo_thold = %d\n", (uint32_t) _regs[Rx_fifo_thold]);
3.604 +}
3.605 +
3.606 +// Read from the target device.
3.607 +
3.608 +void
3.609 +I2c_x1600_channel::start_read(uint8_t buf[], unsigned int total, int stop)
3.610 +{
3.611 + _buf = buf;
3.612 + _total = total;
3.613 + _pos = 0;
3.614 + _reqpos = 0;
3.615 + _fail = 0;
3.616 + _stop = stop;
3.617 +
3.618 + printf("start_read: %d\n", total);
3.619 +
3.620 + _regs[Int_mask] = Int_rx_full | // read condition (reading needed)
3.621 + Int_rx_of | // abort condition
3.622 + Int_tx_abort; // abort condition
3.623 +
3.624 + // Perform initial read requests.
3.625 +
3.626 + read();
3.627 +}
3.628 +
3.629 +void
3.630 +I2c_x1600_channel::read()
3.631 +{
3.632 + printf("Rx_fifo_count = %d\n", (uint32_t) _regs[Rx_fifo_count]);
3.633 + printf("Int_raw_status: %x\n", (uint32_t) _regs[Int_raw_status]);
3.634 + printf("Trans_abort_status: %x\n", (uint32_t) _regs[Trans_abort_status]);
3.635 +
3.636 + if (read_failed() || write_failed())
3.637 + {
3.638 + _fail = 1;
3.639 + _regs[Int_mask] = 0;
3.640 + return;
3.641 + }
3.642 +
3.643 + if (_regs[Int_status] & Int_rx_full)
3.644 + store_reads();
3.645 +
3.646 + // Always attempt to queue more read requests.
3.647 +
3.648 + queue_reads();
3.649 +}
3.650 +
3.651 +// Write to the target device.
3.652 +
3.653 +void
3.654 +I2c_x1600_channel::start_write(uint8_t buf[], unsigned int total, int stop)
3.655 +{
3.656 + _buf = buf;
3.657 + _total = total;
3.658 + _reqpos = 0;
3.659 + _fail = 0;
3.660 + _stop = stop;
3.661 +
3.662 + printf("start_write: %d\n", total);
3.663 +
3.664 + // Enable interrupts for further writes.
3.665 +
3.666 + _regs[Int_mask] = Int_tx_empty | // write condition (writing needed)
3.667 + Int_tx_abort; // abort condition
3.668 +
3.669 + // Perform initial writes.
3.670 +
3.671 + write();
3.672 +}
3.673 +
3.674 +void
3.675 +I2c_x1600_channel::write()
3.676 +{
3.677 + printf("Tx_fifo_count = %d\n", (uint32_t) _regs[Tx_fifo_count]);
3.678 + printf("Int_raw_status: %x\n", (uint32_t) _regs[Int_raw_status]);
3.679 + printf("Trans_abort_status: %x\n", (uint32_t) _regs[Trans_abort_status]);
3.680 +
3.681 + if (write_failed())
3.682 + {
3.683 + _fail = 1;
3.684 + _regs[Int_mask] = 0;
3.685 + return;
3.686 + }
3.687 +
3.688 + if (_regs[Int_status] & Int_tx_empty)
3.689 + queue_writes();
3.690 +}
3.691 +
3.692 +// Explicitly stop communication.
3.693 +
3.694 +void
3.695 +I2c_x1600_channel::stop()
3.696 +{
3.697 +}
3.698 +
3.699 +
3.700 +
3.701 +// Initialise the I2C controller.
3.702 +
3.703 +I2c_x1600_chip::I2c_x1600_chip(l4_addr_t start, l4_addr_t end,
3.704 + Cpm_x1600_chip *cpm,
3.705 + uint32_t frequency)
3.706 +: _start(start), _end(end), _cpm(cpm), _frequency(frequency)
3.707 +{
3.708 +}
3.709 +
3.710 +// Obtain a channel object.
3.711 +
3.712 +I2c_x1600_channel *
3.713 +I2c_x1600_chip::get_channel(uint8_t channel)
3.714 +{
3.715 + l4_addr_t block = _start + channel * I2c_block_offset;
3.716 + enum Clock_identifiers bits[] = {Clock_i2c0, Clock_i2c1};
3.717 +
3.718 + if (channel < 2)
3.719 + {
3.720 + _cpm->start_clock(bits[channel]);
3.721 + return new I2c_x1600_channel(block, _cpm, _frequency);
3.722 + }
3.723 + else
3.724 + throw -L4_EINVAL;
3.725 +}
3.726 +
3.727 +
3.728 +
3.729 +// C language interface functions.
3.730 +
3.731 +void *x1600_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, uint32_t frequency)
3.732 +{
3.733 + return (void *) new I2c_x1600_chip(start, end, static_cast<Cpm_x1600_chip *>(cpm), frequency);
3.734 +}
3.735 +
3.736 +void x1600_i2c_disable(void *i2c_channel)
3.737 +{
3.738 + static_cast<I2c_x1600_channel *>(i2c_channel)->disable();
3.739 +}
3.740 +
3.741 +void *x1600_i2c_get_channel(void *i2c, uint8_t channel)
3.742 +{
3.743 + return static_cast<I2c_x1600_chip *>(i2c)->get_channel(channel);
3.744 +}
3.745 +
3.746 +uint32_t x1600_i2c_get_frequency(void *i2c_channel)
3.747 +{
3.748 + return static_cast<I2c_x1600_channel *>(i2c_channel)->get_frequency();
3.749 +}
3.750 +
3.751 +void x1600_i2c_set_target(void *i2c_channel, uint8_t addr)
3.752 +{
3.753 + static_cast<I2c_x1600_channel *>(i2c_channel)->set_target(addr);
3.754 +}
3.755 +
3.756 +void x1600_i2c_start_read(void *i2c_channel, uint8_t buf[], unsigned int total,
3.757 + int stop)
3.758 +{
3.759 + static_cast<I2c_x1600_channel *>(i2c_channel)->start_read(buf, total, stop);
3.760 +}
3.761 +
3.762 +void x1600_i2c_read(void *i2c_channel)
3.763 +{
3.764 + static_cast<I2c_x1600_channel *>(i2c_channel)->read();
3.765 +}
3.766 +
3.767 +void x1600_i2c_start_write(void *i2c_channel, uint8_t buf[], unsigned int total,
3.768 + int stop)
3.769 +{
3.770 + static_cast<I2c_x1600_channel *>(i2c_channel)->start_write(buf, total, stop);
3.771 +}
3.772 +
3.773 +void x1600_i2c_write(void *i2c_channel)
3.774 +{
3.775 + static_cast<I2c_x1600_channel *>(i2c_channel)->write();
3.776 +}
3.777 +
3.778 +int x1600_i2c_read_done(void *i2c_channel)
3.779 +{
3.780 + return static_cast<I2c_x1600_channel *>(i2c_channel)->read_done();
3.781 +}
3.782 +
3.783 +int x1600_i2c_write_done(void *i2c_channel)
3.784 +{
3.785 + return static_cast<I2c_x1600_channel *>(i2c_channel)->write_done();
3.786 +}
3.787 +
3.788 +unsigned int x1600_i2c_have_read(void *i2c_channel)
3.789 +{
3.790 + return static_cast<I2c_x1600_channel *>(i2c_channel)->have_read();
3.791 +}
3.792 +
3.793 +unsigned int x1600_i2c_have_written(void *i2c_channel)
3.794 +{
3.795 + return static_cast<I2c_x1600_channel *>(i2c_channel)->have_written();
3.796 +}
3.797 +
3.798 +int x1600_i2c_failed(void *i2c_channel)
3.799 +{
3.800 + return static_cast<I2c_x1600_channel *>(i2c_channel)->failed();
3.801 +}
3.802 +
3.803 +void x1600_i2c_stop(void *i2c_channel)
3.804 +{
3.805 + static_cast<I2c_x1600_channel *>(i2c_channel)->stop();
3.806 +}