1 /* 2 * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de> 3 * economic rights: Technische Universit??t Dresden (Germany) 4 * Copyright (C) 2017, 2018, 2019, 2020, 2023 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This file is part of TUD:OS and distributed under the terms of the 7 * GNU General Public License 2. 8 * Please see the COPYING-GPL-2 file for details. 9 */ 10 /* 11 * Access the I2C peripherals on the MIPS Creator CI20 board. 12 */ 13 14 #include <l4/devices/cpm-jz4780.h> 15 #include <l4/devices/gpio-jz4780.h> 16 #include <l4/devices/i2c-jz4780.h> 17 #include <l4/devices/memory.h> 18 #include <l4/io/io.h> 19 #include <l4/re/env.h> 20 #include <l4/re/c/util/cap_alloc.h> 21 #include <l4/sys/factory.h> 22 #include <l4/sys/icu.h> 23 #include <l4/sys/ipc.h> 24 #include <l4/sys/irq.h> 25 #include <l4/sys/rcv_endpoint.h> 26 #include <l4/vbus/vbus.h> 27 #include <stdio.h> 28 #include <unistd.h> 29 #include <stdint.h> 30 #include <string.h> 31 32 #define REG(x) *((volatile uint32_t *) (x)) 33 34 #define RTC_RTCCR 0x00 35 #define RTC_HCR 0x20 36 #define RTC_HSPR 0x34 37 #define RTC_WENR 0x3c 38 #define RTC_PWRONCR 0x3c 39 40 41 42 enum { 43 PMSDA = 30, /* via PORTD */ 44 PMSCL = 31, /* via PORTD */ 45 PWM3 = 3, /* via PORTE */ 46 PWM4 = 4, /* via PORTE */ 47 RTCSDA = 12, /* via PORTE */ 48 RTCSCL = 13, /* via PORTE */ 49 DDCSCL = 24, /* via PORTF */ 50 DDCSDA = 25, /* via PORTF */ 51 }; 52 53 54 55 /* Device and resource discovery. */ 56 57 static long item_in_range(long start, long end, long index) 58 { 59 if (start < end) 60 return start + index; 61 else 62 return start - index; 63 } 64 65 static int i2c_read(void *i2c_channel, uint8_t *buf, unsigned length, 66 int stop, l4_cap_idx_t irqcap) 67 { 68 l4_msgtag_t tag; 69 70 jz4780_i2c_start_read(i2c_channel, buf, length, stop); 71 72 while (!jz4780_i2c_read_done(i2c_channel)) 73 { 74 tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(1000))); 75 76 if (l4_ipc_error(tag, l4_utcb())) 77 break; 78 79 if (jz4780_i2c_failed(i2c_channel)) 80 break; 81 82 jz4780_i2c_read(i2c_channel); 83 } 84 85 if (stop) 86 jz4780_i2c_stop(i2c_channel); 87 88 return jz4780_i2c_have_read(i2c_channel); 89 } 90 91 static int i2c_write(void *i2c_channel, uint8_t *buf, unsigned length, 92 int stop, l4_cap_idx_t irqcap) 93 { 94 l4_msgtag_t tag; 95 long err; 96 97 jz4780_i2c_start_write(i2c_channel, buf, length, stop); 98 99 while (!jz4780_i2c_write_done(i2c_channel)) 100 { 101 tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(1000))); 102 103 if ((err = l4_ipc_error(tag, l4_utcb()))) 104 break; 105 106 if (jz4780_i2c_failed(i2c_channel)) 107 break; 108 109 jz4780_i2c_write(i2c_channel); 110 } 111 112 if (stop) 113 jz4780_i2c_stop(i2c_channel); 114 115 return jz4780_i2c_have_written(i2c_channel); 116 } 117 118 static long i2c_get(void *i2c_channel, uint8_t *buffer, long length, l4_cap_idx_t irqcap) 119 { 120 long pos; 121 122 memset(buffer, 0, length); 123 pos = i2c_read(i2c_channel, buffer, length, 1, irqcap); 124 125 return pos; 126 } 127 128 static long i2c_get_reg(void *i2c_channel, uint8_t reg, uint8_t *buffer, long length, l4_cap_idx_t irqcap) 129 { 130 buffer[0] = reg; 131 i2c_write(i2c_channel, buffer, 1, 0, irqcap); 132 133 return i2c_get(i2c_channel, buffer, length, irqcap); 134 } 135 136 static long i2c_set_reg(void *i2c_channel, uint8_t reg, uint8_t *buffer, long length, l4_cap_idx_t irqcap) 137 { 138 uint8_t buf[length + 1]; 139 long pos; 140 141 buf[0] = reg; 142 memcpy(buf + 1, buffer, length); 143 pos = i2c_write(i2c_channel, buf, length + 1, 1, irqcap); 144 145 return pos; 146 } 147 148 static void i2c_report(void *i2c_channel, uint8_t *buffer, long length) 149 { 150 if (!jz4780_i2c_failed(i2c_channel)) 151 { 152 printf("Read: "); 153 while (length--) 154 printf("%02x", *(buffer++)); 155 printf("\n"); 156 } 157 else 158 printf("Read failed.\n"); 159 } 160 161 static void i2c_scan(void *i2c_channel, l4_cap_idx_t irqcap) 162 { 163 uint8_t buf[1]; 164 unsigned int address; 165 166 for (address = 0; address < 0x20; address++) 167 printf("%02x ", address); 168 printf("\n"); 169 170 for (address = 0; address < 0x20; address++) 171 printf("-- "); 172 173 for (address = 0; address < 0x80; address++) 174 { 175 if ((address % 32) == 0) 176 printf("\n"); 177 178 jz4780_i2c_set_target(i2c_channel, address); 179 i2c_get(i2c_channel, buf, 1, irqcap); 180 181 if (!jz4780_i2c_failed(i2c_channel)) 182 printf("%02x ", address); 183 else 184 printf("?? "); 185 } 186 187 printf("\n"); 188 for (address = 0; address < 0x20; address++) 189 printf("-- "); 190 printf("\n\n"); 191 } 192 193 static void pmic_dump(void *i2c_channel, l4_cap_idx_t irqcap) 194 { 195 uint8_t regs[] = {0x00, 0x01, 0x10, 0x12, 0x20, 0x22, 0x30, 0x32, 196 0x40, 0x41, 0x50, 0x51, 0x60, 0x61, 0x70, 0x71, 197 0x80, 0x81, 0x91}; 198 uint8_t buf[1]; 199 long pos; 200 unsigned i; 201 202 for (i = 0; i < sizeof(regs); i++) 203 { 204 printf("PMIC %02x:\n", regs[i]); 205 jz4780_i2c_set_target(i2c_channel, 0x5a); 206 pos = i2c_get_reg(i2c_channel, regs[i], buf, 1, irqcap); 207 i2c_report(i2c_channel, buf, pos); 208 } 209 } 210 211 static void rtc_dump(void *i2c_channel, l4_cap_idx_t irqcap, uint8_t *rtcregs) 212 { 213 /* Attempt to read from address 0x51 for RTC. */ 214 215 jz4780_i2c_set_target(i2c_channel, 0x51); 216 i2c_get_reg(i2c_channel, 0x00, rtcregs, 16, irqcap); 217 i2c_report(i2c_channel, rtcregs, 16); 218 } 219 220 static void rtc_datetime(uint8_t *rtcregs) 221 { 222 char *weekdays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 223 printf("%s %d%d-%d%d-%d%d %d%d:%d%d:%d%d\n", 224 weekdays[rtcregs[0x06] & 0x07], 225 (rtcregs[0x08] & 0xf0) >> 4, 226 rtcregs[0x08] & 0x0f, 227 (rtcregs[0x07] & 0x10) >> 4, 228 rtcregs[0x07] & 0x0f, 229 (rtcregs[0x05] & 0x30) >> 4, 230 rtcregs[0x05] & 0x0f, 231 (rtcregs[0x04] & 0x30) >> 4, 232 rtcregs[0x04] & 0x0f, 233 (rtcregs[0x03] & 0x70) >> 4, 234 rtcregs[0x03] & 0x0f, 235 (rtcregs[0x02] & 0x70) >> 4, 236 rtcregs[0x02] & 0x0f); 237 } 238 239 static int rtc_update(void *i2c_channel, l4_cap_idx_t irqcap, uint8_t *rtcregs) 240 { 241 jz4780_i2c_set_target(i2c_channel, 0x51); 242 return i2c_set_reg(i2c_channel, 0x02, rtcregs + 2, 7, irqcap); 243 } 244 245 static void rtc_write_ready(l4_addr_t rtc_base) 246 { 247 while (!(REG(rtc_base + RTC_RTCCR) & 0x80)); 248 } 249 250 static void rtc_write_enable(l4_addr_t rtc_base) 251 { 252 rtc_write_ready(rtc_base); 253 REG(rtc_base + RTC_WENR) = 0xa55a; 254 while (!(REG(rtc_base + RTC_WENR) & 0x80000000)); 255 } 256 257 int main(void) 258 { 259 long err; 260 261 /* Peripheral memory. */ 262 263 l4_addr_t gpio_base = 0, gpio_base_end = 0; 264 l4_addr_t i2c_base = 0, i2c_base_end = 0; 265 l4_addr_t cpm_base = 0, cpm_base_end = 0; 266 l4_addr_t rtc_base = 0, rtc_base_end = 0; 267 l4_addr_t port_d, port_d_end; 268 l4_addr_t port_e, port_e_end; 269 270 /* Peripheral abstractions. */ 271 272 void *gpio_port_d, *gpio_port_e; 273 void *i2c, *cpm; 274 void *i2c0, *i2c4; 275 276 /* IRQ resources. */ 277 278 l4_uint32_t i2c_irq_start = 0, i2c_irq_end = 0; 279 l4_cap_idx_t icucap, irq0cap, irq4cap; 280 281 /* RTC registers. */ 282 283 uint8_t rtcregs[16]; 284 int i; 285 286 /* Obtain capabilities for the interrupt controller and an interrupt. */ 287 288 irq0cap = l4re_util_cap_alloc(); 289 irq4cap = l4re_util_cap_alloc(); 290 icucap = l4re_env_get_cap("icu"); 291 292 if (l4_is_invalid_cap(icucap)) 293 { 294 printf("No 'icu' capability available in the virtual bus.\n"); 295 return 1; 296 } 297 298 if (l4_is_invalid_cap(irq0cap) || l4_is_invalid_cap(irq4cap)) 299 { 300 printf("Capabilities not available for interrupts.\n"); 301 return 1; 302 } 303 304 /* Obtain resource details describing the interrupt for I2C channel 4. */ 305 306 printf("Access IRQ...\n"); 307 308 if (get_irq("jz4780-i2c", &i2c_irq_start, &i2c_irq_end) < 0) 309 return 1; 310 311 printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); 312 313 /* Obtain resource details describing I/O memory. */ 314 315 printf("Access GPIO...\n"); 316 317 if (get_memory("jz4780-gpio", &gpio_base, &gpio_base_end) < 0) 318 return 1; 319 320 printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); 321 322 printf("Access CPM...\n"); 323 324 if (get_memory("jz4780-cpm", &cpm_base, &cpm_base_end) < 0) 325 return 1; 326 327 printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); 328 329 printf("Access I2C...\n"); 330 331 if (get_memory("jz4780-i2c", &i2c_base, &i2c_base_end) < 0) 332 return 1; 333 334 printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); 335 336 printf("Access RTC...\n"); 337 338 if (get_memory("jz4780-rtc", &rtc_base, &rtc_base_end) < 0) 339 return 1; 340 341 printf("RTC at 0x%lx...0x%lx.\n", rtc_base, rtc_base_end); 342 343 /* Create interrupt objects. */ 344 345 err = l4_error(l4_factory_create_irq(l4re_global_env->factory, irq0cap)) || 346 l4_error(l4_factory_create_irq(l4re_global_env->factory, irq4cap)); 347 348 if (err) 349 { 350 printf("Could not create IRQ object: %lx\n", err); 351 return 1; 352 } 353 354 /* Bind interrupt objects to IRQ numbers. */ 355 356 err = l4_error(l4_icu_bind(icucap, 357 item_in_range(i2c_irq_start, i2c_irq_end, 0), 358 irq0cap)) || 359 l4_error(l4_icu_bind(icucap, 360 item_in_range(i2c_irq_start, i2c_irq_end, 4), 361 irq4cap)); 362 363 if (err) 364 { 365 printf("Could not bind IRQ to the ICU: %ld\n", err); 366 return 1; 367 } 368 369 /* Attach ourselves to the interrupt handler. */ 370 371 err = l4_error(l4_rcv_ep_bind_thread(irq0cap, l4re_env()->main_thread, 0)) || 372 l4_error(l4_rcv_ep_bind_thread(irq4cap, l4re_env()->main_thread, 4)); 373 374 if (err) 375 { 376 printf("Could not attach to IRQs: %ld\n", err); 377 return 1; 378 } 379 380 /* Configure pins. */ 381 382 port_d = gpio_base + 0x300; 383 port_d_end = port_d + 0x100; 384 port_e = gpio_base + 0x400; 385 port_e_end = port_e + 0x100; 386 387 printf("PORTD at 0x%lx...0x%lx.\n", port_d, port_d_end); 388 printf("PORTE at 0x%lx...0x%lx.\n", port_e, port_e_end); 389 390 gpio_port_d = jz4780_gpio_init(port_d, port_d_end, 32, 0xffff4fff, 0x0000b000); 391 gpio_port_e = jz4780_gpio_init(port_e, port_e_end, 32, 0xfffff37c, 0x00000483); 392 393 printf("Set up GPIO pins...\n"); 394 395 jz4780_gpio_config_pad(gpio_port_d, PMSCL, Function_alt, 0); 396 jz4780_gpio_config_pad(gpio_port_d, PMSDA, Function_alt, 0); 397 398 jz4780_gpio_config_pad(gpio_port_e, RTCSCL, Function_alt, 1); 399 jz4780_gpio_config_pad(gpio_port_e, RTCSDA, Function_alt, 1); 400 401 /* Obtain CPM and I2C objects. */ 402 403 cpm = jz4780_cpm_init(cpm_base); 404 405 /* Attempt to set the PCLK source to SCLK_A. */ 406 407 jz4780_cpm_set_source_clock(cpm, Clock_pclock, Clock_main); 408 printf("Peripheral clock: %lld\n", jz4780_cpm_get_frequency(cpm, Clock_pclock)); 409 410 /* Obtain I2C reference. */ 411 412 i2c = jz4780_i2c_init(i2c_base, i2c_base_end, cpm, 100000); /* 100 kHz */ 413 i2c0 = jz4780_i2c_get_channel(i2c, 0); 414 i2c4 = jz4780_i2c_get_channel(i2c, 4); 415 416 printf("Scan I2C0...\n"); 417 i2c_scan(i2c0, irq0cap); 418 419 printf("Scan I2C4...\n"); 420 i2c_scan(i2c4, irq4cap); 421 422 printf("PMIC...\n"); 423 pmic_dump(i2c0, irq0cap); 424 425 printf("RTC...\n"); 426 rtc_dump(i2c4, irq4cap, rtcregs); 427 428 rtcregs[0x02] = 0x56; 429 rtcregs[0x03] = 0x34; 430 rtcregs[0x04] = 0x12; 431 rtcregs[0x05] = 0x25; /* 25th */ 432 rtcregs[0x06] = 0x06; /* Saturday */ 433 rtcregs[0x07] = 0x01; /* January */ 434 rtcregs[0x08] = 0x20; /* 2020 */ 435 436 printf("Updated %d registers.\n", rtc_update(i2c4, irq4cap, rtcregs)); 437 438 for (i = 0; i < 3; i++) 439 { 440 rtc_dump(i2c4, irq4cap, rtcregs); 441 rtc_datetime(rtcregs); 442 sleep(1); 443 } 444 445 /* Investigate the internal RTC registers. */ 446 447 printf("Control: %08x\n", REG(rtc_base + RTC_RTCCR)); 448 printf("Hibernate: %08x\n", REG(rtc_base + RTC_HCR)); 449 printf("Scratchpad: %08x\n", REG(rtc_base + RTC_HSPR)); 450 printf("Power on control: %08x\n", REG(rtc_base + RTC_PWRONCR)); 451 printf("Write enable: %08x\n", REG(rtc_base + RTC_WENR)); 452 453 rtc_write_enable(rtc_base); 454 rtc_write_ready(rtc_base); 455 REG(rtc_base + RTC_HSPR) = 0x12345678; 456 457 for (i = 0; i < 3; i++) 458 { 459 printf("Scratchpad: %08x\n", REG(rtc_base + RTC_HSPR)); 460 sleep(1); 461 } 462 463 //jz4780_i2c_disable(i2c0); 464 //jz4780_i2c_disable(i2c4); 465 466 /* Detach from the interrupt. */ 467 468 err = l4_error(l4_irq_detach(irq0cap)) || l4_error(l4_irq_detach(irq4cap)); 469 470 if (err) 471 printf("Error detaching from IRQ objects: %ld\n", err); 472 473 printf("Done.\n"); 474 475 return 0; 476 }