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