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