paul@114 | 1 | /* |
paul@114 | 2 | * I2C support for the JZ4730. |
paul@114 | 3 | * |
paul@114 | 4 | * Copyright (C) 2017, 2018, 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 | #include <l4/devices/i2c-jz4730.h> |
paul@114 | 23 | #include <l4/devices/hw_mmio_register_block.h> |
paul@114 | 24 | |
paul@114 | 25 | #include <l4/sys/icu.h> |
paul@118 | 26 | #include <l4/sys/ipc.h> |
paul@118 | 27 | #include <l4/sys/irq.h> |
paul@114 | 28 | #include <l4/util/util.h> |
paul@114 | 29 | |
paul@114 | 30 | #include <cstdio> |
paul@114 | 31 | |
paul@114 | 32 | /* |
paul@114 | 33 | I2C pins are dedicated to I2C only and are not GPIO-controlled: |
paul@114 | 34 | |
paul@114 | 35 | I2C0: Y4/SMB0_SDA, V5/SMB0_SCK |
paul@114 | 36 | |
paul@114 | 37 | Note that there is effectively only one I2C channel. |
paul@114 | 38 | */ |
paul@114 | 39 | |
paul@114 | 40 | enum Regs |
paul@114 | 41 | { |
paul@114 | 42 | I2c_data = 0x000, // I2CDR |
paul@114 | 43 | I2c_control = 0x004, // I2CCR |
paul@114 | 44 | I2c_status = 0x008, // I2CSR |
paul@114 | 45 | I2c_clock = 0x00c, // I2CGR |
paul@114 | 46 | }; |
paul@114 | 47 | |
paul@114 | 48 | enum I2c_control_bits : unsigned |
paul@114 | 49 | { |
paul@114 | 50 | I2c_control_enable_irq = 0x10, // IEN |
paul@114 | 51 | I2c_control_start = 0x08, // STA |
paul@114 | 52 | I2c_control_stop = 0x04, // STO |
paul@114 | 53 | I2c_control_nack = 0x02, // AC |
paul@114 | 54 | I2c_control_enable = 0x01, // I2CE |
paul@114 | 55 | }; |
paul@114 | 56 | |
paul@114 | 57 | enum I2c_status_bits : unsigned |
paul@114 | 58 | { |
paul@114 | 59 | I2c_status_buffer_nempty = 0x10, // STX |
paul@114 | 60 | I2c_status_busy = 0x08, // BUSY |
paul@114 | 61 | I2c_status_transmit_end = 0x04, // TEND |
paul@114 | 62 | I2c_status_data_valid = 0x02, // DRF |
paul@114 | 63 | I2c_status_nack = 0x01, // ACKF |
paul@114 | 64 | }; |
paul@114 | 65 | |
paul@114 | 66 | enum I2c_clock_values : unsigned |
paul@114 | 67 | { |
paul@114 | 68 | I2c_clock_max = 0xffff, |
paul@114 | 69 | I2c_clock_min = 0, |
paul@114 | 70 | }; |
paul@114 | 71 | |
paul@114 | 72 | |
paul@114 | 73 | |
paul@114 | 74 | // Initialise a channel. |
paul@114 | 75 | |
paul@114 | 76 | I2c_jz4730_channel::I2c_jz4730_channel(l4_addr_t start, |
paul@114 | 77 | Cpm_jz4730_chip *cpm, |
paul@118 | 78 | uint32_t frequency, |
paul@118 | 79 | l4_cap_idx_t irq) |
paul@118 | 80 | : _cpm(cpm), _frequency(frequency), _irq(irq) |
paul@114 | 81 | { |
paul@114 | 82 | _regs = new Hw::Mmio_register_block<32>(start); |
paul@114 | 83 | } |
paul@114 | 84 | |
paul@114 | 85 | // Enable the channel. |
paul@114 | 86 | |
paul@114 | 87 | void |
paul@114 | 88 | I2c_jz4730_channel::enable() |
paul@114 | 89 | { |
paul@114 | 90 | // Make sure that the I2C clock is available. |
paul@114 | 91 | |
paul@114 | 92 | _cpm->start_i2c(); |
paul@114 | 93 | |
paul@114 | 94 | // Set the bus clock frequency. |
paul@114 | 95 | |
paul@114 | 96 | set_frequency(); |
paul@114 | 97 | |
paul@114 | 98 | // Enable the channel and interrupts. |
paul@114 | 99 | |
paul@114 | 100 | _regs[I2c_control] = I2c_control_enable | I2c_control_enable_irq; |
paul@114 | 101 | while (!(_regs[I2c_control] & I2c_control_enable)); |
paul@114 | 102 | } |
paul@114 | 103 | |
paul@114 | 104 | // Disable the channel. |
paul@114 | 105 | |
paul@114 | 106 | void |
paul@114 | 107 | I2c_jz4730_channel::disable() |
paul@114 | 108 | { |
paul@114 | 109 | _regs[I2c_control] = 0; |
paul@114 | 110 | while (_regs[I2c_control] & I2c_control_enable); |
paul@114 | 111 | } |
paul@114 | 112 | |
paul@114 | 113 | // Set the frequency-related peripheral parameters. |
paul@114 | 114 | |
paul@114 | 115 | void |
paul@114 | 116 | I2c_jz4730_channel::set_frequency() |
paul@114 | 117 | { |
paul@114 | 118 | // The APB clock (PCLK) is used to drive I2C transfers. Its value must be |
paul@114 | 119 | // obtained from the CPM unit and is scaled to kHz in order to keep the |
paul@114 | 120 | // numbers easily representable, as is the bus frequency. |
paul@114 | 121 | |
paul@114 | 122 | uint32_t pclk = _cpm->get_pclock_frequency() / 1000; |
paul@114 | 123 | uint32_t i2c_clk = _frequency / 1000; |
paul@114 | 124 | uint32_t division = pclk / (16 * i2c_clk); |
paul@114 | 125 | |
paul@114 | 126 | if (division > I2c_clock_min) |
paul@114 | 127 | { |
paul@114 | 128 | division -= 1; |
paul@114 | 129 | if (division > I2c_clock_max) |
paul@114 | 130 | division = I2c_clock_max; |
paul@114 | 131 | } |
paul@114 | 132 | |
paul@114 | 133 | _regs[I2c_clock] = division; |
paul@114 | 134 | } |
paul@114 | 135 | |
paul@114 | 136 | // Present the address on the bus. |
paul@114 | 137 | |
paul@118 | 138 | bool |
paul@114 | 139 | I2c_jz4730_channel::set_address(uint8_t address, bool read) |
paul@114 | 140 | { |
paul@118 | 141 | unsigned int limit = 10; |
paul@118 | 142 | |
paul@118 | 143 | do |
paul@118 | 144 | { |
paul@118 | 145 | if (!wait_for_irq(1000) && !(--limit)) |
paul@118 | 146 | return false; |
paul@118 | 147 | } |
paul@118 | 148 | while (busy()); |
paul@118 | 149 | |
paul@114 | 150 | start(); |
paul@114 | 151 | |
paul@116 | 152 | _regs[I2c_data] = (address << 1) | (read ? 1 : 0); |
paul@114 | 153 | |
paul@114 | 154 | send_next(); |
paul@114 | 155 | |
paul@118 | 156 | return true; |
paul@118 | 157 | } |
paul@118 | 158 | |
paul@118 | 159 | // Wait up to the given timeout (in microseconds) for an interrupt request, |
paul@118 | 160 | // returning true if one was delivered. |
paul@118 | 161 | |
paul@118 | 162 | bool |
paul@118 | 163 | I2c_jz4730_channel::wait_for_irq(unsigned int timeout) |
paul@118 | 164 | { |
paul@118 | 165 | return !l4_error(l4_irq_receive(_irq, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4util_micros2l4to(timeout)))); |
paul@114 | 166 | } |
paul@114 | 167 | |
paul@114 | 168 | // Read data from the bus. |
paul@114 | 169 | |
paul@114 | 170 | unsigned int |
paul@114 | 171 | I2c_jz4730_channel::read(uint8_t address, uint8_t buf[], unsigned int length) |
paul@114 | 172 | { |
paul@114 | 173 | unsigned int nread = 0; |
paul@114 | 174 | |
paul@118 | 175 | if (!set_address(address, true)) |
paul@118 | 176 | return 0; |
paul@118 | 177 | |
paul@118 | 178 | do |
paul@118 | 179 | { |
paul@118 | 180 | if (!wait_for_irq(1000000)) |
paul@118 | 181 | printf("start (no irq): status = %x\n", (uint32_t) _regs[I2c_status]); |
paul@118 | 182 | } |
paul@118 | 183 | while (transferring() || (!data_valid() && !nack())); |
paul@114 | 184 | |
paul@118 | 185 | while (nread < length) |
paul@114 | 186 | { |
paul@118 | 187 | do |
paul@118 | 188 | { |
paul@118 | 189 | if (!wait_for_irq(1000000)) |
paul@118 | 190 | { |
paul@118 | 191 | stop(); |
paul@118 | 192 | return nread; |
paul@118 | 193 | } |
paul@118 | 194 | } |
paul@118 | 195 | while (!data_valid() && !nack()); |
paul@118 | 196 | |
paul@118 | 197 | if (nack()) |
paul@118 | 198 | break; |
paul@118 | 199 | |
paul@118 | 200 | if ((!nread && (length == 1)) || (nread == length - 2)) |
paul@114 | 201 | signal_last(); |
paul@114 | 202 | |
paul@118 | 203 | buf[nread++] = _regs[I2c_data]; |
paul@118 | 204 | clear_next(); |
paul@114 | 205 | } |
paul@114 | 206 | |
paul@114 | 207 | stop(); |
paul@114 | 208 | return nread; |
paul@114 | 209 | } |
paul@114 | 210 | |
paul@114 | 211 | // Write data to the bus. |
paul@114 | 212 | |
paul@114 | 213 | unsigned int |
paul@114 | 214 | I2c_jz4730_channel::write(uint8_t address, uint8_t buf[], unsigned int length) |
paul@114 | 215 | { |
paul@114 | 216 | unsigned int nwritten = 0; |
paul@114 | 217 | |
paul@118 | 218 | if (!set_address(address, false)) |
paul@118 | 219 | return 0; |
paul@118 | 220 | |
paul@118 | 221 | do |
paul@118 | 222 | { |
paul@118 | 223 | if (!wait_for_irq(1000000)) |
paul@118 | 224 | { |
paul@118 | 225 | printf("write (no irq): status = %x\n", (uint32_t) _regs[I2c_status]); |
paul@118 | 226 | stop(); |
paul@118 | 227 | return nwritten; |
paul@118 | 228 | } |
paul@118 | 229 | } |
paul@118 | 230 | while (data_valid() && !nack()); |
paul@114 | 231 | |
paul@114 | 232 | while ((nwritten < length) && !nack()) |
paul@114 | 233 | { |
paul@114 | 234 | _regs[I2c_data] = buf[nwritten++]; |
paul@114 | 235 | send_next(); |
paul@114 | 236 | |
paul@118 | 237 | do |
paul@118 | 238 | { |
paul@118 | 239 | if (!wait_for_irq(1000000)) |
paul@118 | 240 | { |
paul@118 | 241 | printf("write (no irq): status = %x\n", (uint32_t) _regs[I2c_status]); |
paul@118 | 242 | stop(); |
paul@118 | 243 | return nwritten; |
paul@118 | 244 | } |
paul@118 | 245 | } |
paul@114 | 246 | while (data_valid() && !nack()); |
paul@114 | 247 | } |
paul@114 | 248 | |
paul@114 | 249 | stop(); |
paul@114 | 250 | |
paul@118 | 251 | do |
paul@118 | 252 | { |
paul@118 | 253 | if (!wait_for_irq(1000000)) |
paul@118 | 254 | break; |
paul@118 | 255 | } |
paul@114 | 256 | while (!transferred()); |
paul@114 | 257 | |
paul@114 | 258 | return nwritten; |
paul@114 | 259 | } |
paul@114 | 260 | |
paul@114 | 261 | // Test for data validity. |
paul@114 | 262 | |
paul@114 | 263 | bool |
paul@114 | 264 | I2c_jz4730_channel::data_valid() |
paul@114 | 265 | { |
paul@114 | 266 | return (_regs[I2c_status] & I2c_status_data_valid) ? true : false; |
paul@114 | 267 | } |
paul@114 | 268 | |
paul@114 | 269 | // Request the next byte by clearing the data validity flag. |
paul@114 | 270 | |
paul@114 | 271 | void |
paul@118 | 272 | I2c_jz4730_channel::clear_next() |
paul@114 | 273 | { |
paul@114 | 274 | _regs[I2c_status] = _regs[I2c_status] & ~I2c_status_data_valid; |
paul@114 | 275 | } |
paul@114 | 276 | |
paul@114 | 277 | // Indicate data ready for sending. |
paul@114 | 278 | |
paul@114 | 279 | void |
paul@114 | 280 | I2c_jz4730_channel::send_next() |
paul@114 | 281 | { |
paul@114 | 282 | _regs[I2c_status] = _regs[I2c_status] | I2c_status_data_valid; |
paul@114 | 283 | } |
paul@114 | 284 | |
paul@114 | 285 | // Test for non-acknowledgement. |
paul@114 | 286 | |
paul@114 | 287 | bool |
paul@114 | 288 | I2c_jz4730_channel::nack() |
paul@114 | 289 | { |
paul@114 | 290 | return (_regs[I2c_status] & I2c_status_nack) ? true : false; |
paul@114 | 291 | } |
paul@114 | 292 | |
paul@114 | 293 | // Set non-acknowledgement when receiving data. |
paul@114 | 294 | |
paul@114 | 295 | void |
paul@114 | 296 | I2c_jz4730_channel::signal_last() |
paul@114 | 297 | { |
paul@114 | 298 | _regs[I2c_control] = _regs[I2c_control] | I2c_control_nack; |
paul@114 | 299 | } |
paul@114 | 300 | |
paul@118 | 301 | // Test for bus activity. |
paul@118 | 302 | |
paul@118 | 303 | bool |
paul@118 | 304 | I2c_jz4730_channel::busy() |
paul@118 | 305 | { |
paul@118 | 306 | return (_regs[I2c_status] & I2c_status_busy) ? true : false; |
paul@118 | 307 | } |
paul@118 | 308 | |
paul@118 | 309 | // Test for transfer activity. |
paul@118 | 310 | |
paul@118 | 311 | bool |
paul@118 | 312 | I2c_jz4730_channel::transferring() |
paul@118 | 313 | { |
paul@118 | 314 | return (_regs[I2c_status] & I2c_status_buffer_nempty) ? true : false; |
paul@118 | 315 | } |
paul@118 | 316 | |
paul@114 | 317 | // Test for write transfer completion. |
paul@114 | 318 | |
paul@114 | 319 | bool |
paul@114 | 320 | I2c_jz4730_channel::transferred() |
paul@114 | 321 | { |
paul@114 | 322 | return (_regs[I2c_status] & I2c_status_transmit_end) ? true : false; |
paul@114 | 323 | } |
paul@114 | 324 | |
paul@114 | 325 | // Explicitly start communication. |
paul@114 | 326 | |
paul@114 | 327 | void |
paul@114 | 328 | I2c_jz4730_channel::start() |
paul@114 | 329 | { |
paul@114 | 330 | _regs[I2c_control] = (_regs[I2c_control] & ~I2c_control_nack) | I2c_control_start; |
paul@114 | 331 | } |
paul@114 | 332 | |
paul@114 | 333 | // Explicitly stop communication. |
paul@114 | 334 | |
paul@114 | 335 | void |
paul@114 | 336 | I2c_jz4730_channel::stop() |
paul@114 | 337 | { |
paul@114 | 338 | _regs[I2c_control] = _regs[I2c_control] | I2c_control_stop; |
paul@114 | 339 | } |
paul@114 | 340 | |
paul@114 | 341 | |
paul@114 | 342 | |
paul@114 | 343 | // Initialise the I2C controller. |
paul@114 | 344 | |
paul@114 | 345 | I2c_jz4730_chip::I2c_jz4730_chip(l4_addr_t start, l4_addr_t end, |
paul@114 | 346 | Cpm_jz4730_chip *cpm, |
paul@114 | 347 | uint32_t frequency) |
paul@114 | 348 | : _start(start), _end(end), _cpm(cpm), _frequency(frequency) |
paul@114 | 349 | { |
paul@114 | 350 | } |
paul@114 | 351 | |
paul@114 | 352 | // Obtain a channel object. Only one channel is supported. |
paul@114 | 353 | |
paul@114 | 354 | I2c_jz4730_channel * |
paul@118 | 355 | I2c_jz4730_chip::get_channel(uint8_t channel, l4_cap_idx_t irq) |
paul@114 | 356 | { |
paul@114 | 357 | if (channel == 0) |
paul@118 | 358 | return new I2c_jz4730_channel(_start, _cpm, _frequency, irq); |
paul@114 | 359 | else |
paul@114 | 360 | throw -L4_EINVAL; |
paul@114 | 361 | } |
paul@114 | 362 | |
paul@114 | 363 | |
paul@114 | 364 | |
paul@114 | 365 | // C language interface functions. |
paul@114 | 366 | |
paul@114 | 367 | void *jz4730_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, uint32_t frequency) |
paul@114 | 368 | { |
paul@114 | 369 | return (void *) new I2c_jz4730_chip(start, end, static_cast<Cpm_jz4730_chip *>(cpm), frequency); |
paul@114 | 370 | } |
paul@114 | 371 | |
paul@118 | 372 | void *jz4730_i2c_get_channel(void *i2c, uint8_t channel, l4_cap_idx_t irq) |
paul@114 | 373 | { |
paul@118 | 374 | return static_cast<I2c_jz4730_chip *>(i2c)->get_channel(channel, irq); |
paul@114 | 375 | } |
paul@114 | 376 | |
paul@114 | 377 | void jz4730_i2c_disable(void *i2c_channel) |
paul@114 | 378 | { |
paul@114 | 379 | static_cast<I2c_jz4730_channel *>(i2c_channel)->disable(); |
paul@114 | 380 | } |
paul@114 | 381 | |
paul@114 | 382 | void jz4730_i2c_enable(void *i2c_channel) |
paul@114 | 383 | { |
paul@114 | 384 | static_cast<I2c_jz4730_channel *>(i2c_channel)->enable(); |
paul@114 | 385 | } |
paul@114 | 386 | |
paul@114 | 387 | unsigned int jz4730_i2c_read(void *i2c_channel, uint8_t address, uint8_t buf[], unsigned int length) |
paul@114 | 388 | { |
paul@114 | 389 | return static_cast<I2c_jz4730_channel *>(i2c_channel)->read(address, buf, length); |
paul@114 | 390 | } |
paul@114 | 391 | |
paul@114 | 392 | unsigned int jz4730_i2c_write(void *i2c_channel, uint8_t address, uint8_t buf[], unsigned int length) |
paul@114 | 393 | { |
paul@114 | 394 | return static_cast<I2c_jz4730_channel *>(i2c_channel)->write(address, buf, length); |
paul@114 | 395 | } |