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