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