paul@218 | 1 | /* |
paul@218 | 2 | * Access various peripherals on the MIPS Creator CI20 board. |
paul@218 | 3 | * |
paul@253 | 4 | * Copyright (C) 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@218 | 5 | * |
paul@218 | 6 | * This program is free software; you can redistribute it and/or |
paul@218 | 7 | * modify it under the terms of the GNU General Public License as |
paul@218 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@218 | 9 | * the License, or (at your option) any later version. |
paul@218 | 10 | * |
paul@218 | 11 | * This program is distributed in the hope that it will be useful, |
paul@218 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@218 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@218 | 14 | * GNU General Public License for more details. |
paul@218 | 15 | * |
paul@218 | 16 | * You should have received a copy of the GNU General Public License |
paul@218 | 17 | * along with this program; if not, write to the Free Software |
paul@218 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@218 | 19 | * Boston, MA 02110-1301, USA |
paul@218 | 20 | */ |
paul@218 | 21 | |
paul@218 | 22 | #include "common.h" |
paul@218 | 23 | #include "defs.h" |
paul@218 | 24 | |
paul@218 | 25 | #include <l4/devices/dma.h> |
paul@218 | 26 | #include <l4/devices/memory.h> |
paul@218 | 27 | |
paul@218 | 28 | #include <l4/io/io.h> |
paul@218 | 29 | #include <l4/re/env.h> |
paul@218 | 30 | #include <l4/re/c/util/cap_alloc.h> |
paul@218 | 31 | #include <l4/sys/cache.h> |
paul@218 | 32 | #include <l4/sys/factory.h> |
paul@218 | 33 | #include <l4/sys/icu.h> |
paul@218 | 34 | #include <l4/sys/ipc.h> |
paul@218 | 35 | #include <l4/sys/irq.h> |
paul@218 | 36 | #include <l4/sys/rcv_endpoint.h> |
paul@218 | 37 | #include <l4/vbus/vbus.h> |
paul@218 | 38 | |
paul@218 | 39 | #include <stdio.h> |
paul@218 | 40 | #include <unistd.h> |
paul@218 | 41 | #include <stdint.h> |
paul@218 | 42 | #include <stdlib.h> |
paul@218 | 43 | #include <string.h> |
paul@218 | 44 | |
paul@218 | 45 | |
paul@218 | 46 | |
paul@218 | 47 | /* Common configuration. */ |
paul@218 | 48 | |
paul@218 | 49 | static l4_cap_idx_t icucap; |
paul@218 | 50 | |
paul@236 | 51 | /* Memory definitions. */ |
paul@236 | 52 | |
paul@236 | 53 | static const unsigned int num_memory_regions = 8; |
paul@236 | 54 | |
paul@236 | 55 | static struct memory_region memory_regions[] = { |
paul@236 | 56 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, |
paul@236 | 57 | }; |
paul@236 | 58 | |
paul@218 | 59 | |
paul@218 | 60 | |
paul@218 | 61 | /* Device and resource discovery. */ |
paul@218 | 62 | |
paul@218 | 63 | static long item_in_range(long start, long end, long index) |
paul@218 | 64 | { |
paul@218 | 65 | if (start < end) |
paul@218 | 66 | return start + index; |
paul@218 | 67 | else if (start > end) |
paul@218 | 68 | return start - index; |
paul@218 | 69 | else |
paul@218 | 70 | return start; |
paul@218 | 71 | } |
paul@218 | 72 | |
paul@218 | 73 | |
paul@218 | 74 | |
paul@218 | 75 | /* Common functions. */ |
paul@218 | 76 | |
paul@218 | 77 | static int init_irq(int num, l4_cap_idx_t irq, l4_uint32_t start, l4_uint32_t end) |
paul@218 | 78 | { |
paul@218 | 79 | /* Create interrupt object. */ |
paul@218 | 80 | |
paul@218 | 81 | long err = l4_error(l4_factory_create_irq(l4re_global_env->factory, irq)); |
paul@218 | 82 | |
paul@218 | 83 | if (err) |
paul@218 | 84 | { |
paul@218 | 85 | printf("Could not create IRQ object: %ld\n", err); |
paul@218 | 86 | return 1; |
paul@218 | 87 | } |
paul@218 | 88 | |
paul@218 | 89 | /* Bind interrupt objects to IRQ numbers. */ |
paul@218 | 90 | |
paul@218 | 91 | err = l4_error(l4_icu_bind(icucap, |
paul@218 | 92 | item_in_range(start, end, num), |
paul@218 | 93 | irq)); |
paul@218 | 94 | |
paul@218 | 95 | if (err) |
paul@218 | 96 | { |
paul@218 | 97 | printf("Could not bind IRQ to the ICU: %ld\n", err); |
paul@218 | 98 | return 1; |
paul@218 | 99 | } |
paul@218 | 100 | |
paul@218 | 101 | /* Attach ourselves to the interrupt handler. */ |
paul@218 | 102 | |
paul@218 | 103 | err = l4_error(l4_rcv_ep_bind_thread(irq, l4re_env()->main_thread, num)); |
paul@218 | 104 | |
paul@218 | 105 | if (err) |
paul@218 | 106 | { |
paul@218 | 107 | printf("Could not attach to IRQs: %ld\n", err); |
paul@218 | 108 | return 1; |
paul@218 | 109 | } |
paul@218 | 110 | |
paul@218 | 111 | return 0; |
paul@218 | 112 | } |
paul@218 | 113 | |
paul@218 | 114 | static const char *delimiters = " \n"; |
paul@218 | 115 | |
paul@218 | 116 | static char *read_token(const char *message) |
paul@218 | 117 | { |
paul@218 | 118 | char *token; |
paul@218 | 119 | |
paul@218 | 120 | if ((token = strtok(NULL, delimiters)) == NULL) |
paul@218 | 121 | { |
paul@218 | 122 | if (message != NULL) |
paul@218 | 123 | printf("%s?\n", message); |
paul@218 | 124 | } |
paul@218 | 125 | |
paul@218 | 126 | return token; |
paul@218 | 127 | } |
paul@218 | 128 | |
paul@236 | 129 | static int read_flag(const char *message, int *value, const char *flag, size_t n) |
paul@218 | 130 | { |
paul@218 | 131 | char *token = read_token(message); |
paul@218 | 132 | |
paul@218 | 133 | if (token == NULL) |
paul@218 | 134 | return 0; |
paul@218 | 135 | |
paul@236 | 136 | *value = !strncmp(token, flag, n); |
paul@218 | 137 | return 1; |
paul@218 | 138 | } |
paul@218 | 139 | |
paul@218 | 140 | static int read_encoded_number(const char *message, const char *format, unsigned int *num) |
paul@218 | 141 | { |
paul@218 | 142 | char *token = read_token(message); |
paul@218 | 143 | |
paul@218 | 144 | if (token == NULL) |
paul@218 | 145 | return 0; |
paul@218 | 146 | |
paul@218 | 147 | sscanf(token, format, num); |
paul@218 | 148 | return 1; |
paul@218 | 149 | } |
paul@218 | 150 | |
paul@218 | 151 | static int read_number(const char *message, unsigned int *num) |
paul@218 | 152 | { |
paul@218 | 153 | char *token = read_token(message); |
paul@218 | 154 | |
paul@218 | 155 | if (token == NULL) |
paul@218 | 156 | return 0; |
paul@218 | 157 | |
paul@218 | 158 | if (!strncmp(token, "0x", 2) || !strncmp(token, "0X", 2)) |
paul@218 | 159 | sscanf(token, "%x", num); |
paul@218 | 160 | else |
paul@218 | 161 | *num = atoi(token); |
paul@218 | 162 | |
paul@218 | 163 | return 1; |
paul@218 | 164 | } |
paul@218 | 165 | |
paul@218 | 166 | static int get_resource_number(const char *type, unsigned int available) |
paul@218 | 167 | { |
paul@218 | 168 | unsigned int num; |
paul@218 | 169 | |
paul@218 | 170 | if (!read_number(type, &num)) |
paul@218 | 171 | return -1; |
paul@218 | 172 | |
paul@218 | 173 | if (num >= available) |
paul@218 | 174 | { |
paul@218 | 175 | printf("%s number out of range.\n", type); |
paul@218 | 176 | return -1; |
paul@218 | 177 | } |
paul@218 | 178 | |
paul@218 | 179 | return num; |
paul@218 | 180 | } |
paul@218 | 181 | |
paul@218 | 182 | static int get_channel_number(unsigned int available) |
paul@218 | 183 | { |
paul@218 | 184 | return get_resource_number("Channel", available); |
paul@218 | 185 | } |
paul@218 | 186 | |
paul@218 | 187 | static int get_region_number(unsigned int available) |
paul@218 | 188 | { |
paul@218 | 189 | return get_resource_number("Region", available); |
paul@218 | 190 | } |
paul@218 | 191 | |
paul@218 | 192 | static void *get_channel(int num_channels, void *channels[], int *num) |
paul@218 | 193 | { |
paul@218 | 194 | int n = get_channel_number(num_channels); |
paul@218 | 195 | void *channel; |
paul@218 | 196 | |
paul@218 | 197 | if (n < 0) |
paul@218 | 198 | return NULL; |
paul@218 | 199 | |
paul@218 | 200 | channel = channels[n]; |
paul@218 | 201 | |
paul@218 | 202 | if (channel == NULL) |
paul@218 | 203 | printf("Channel needs creating.\n"); |
paul@218 | 204 | |
paul@218 | 205 | if (num != NULL) |
paul@218 | 206 | *num = n; |
paul@218 | 207 | |
paul@218 | 208 | return channel; |
paul@218 | 209 | } |
paul@218 | 210 | |
paul@218 | 211 | static void list_channels(int num_channels, void *channels[]) |
paul@218 | 212 | { |
paul@218 | 213 | int num; |
paul@218 | 214 | void *channel; |
paul@218 | 215 | |
paul@218 | 216 | for (num = 0; num < num_channels; num++) |
paul@218 | 217 | { |
paul@218 | 218 | printf("Channel %d: ", num); |
paul@218 | 219 | |
paul@218 | 220 | channel = channels[num]; |
paul@218 | 221 | |
paul@218 | 222 | if (channel == NULL) |
paul@218 | 223 | printf("(inactive)\n"); |
paul@218 | 224 | else |
paul@218 | 225 | printf("active\n"); |
paul@218 | 226 | } |
paul@218 | 227 | } |
paul@218 | 228 | |
paul@221 | 229 | static struct dma_region *_get_dma_region(void) |
paul@221 | 230 | { |
paul@221 | 231 | int num = get_region_number(num_dma_regions); |
paul@221 | 232 | |
paul@221 | 233 | if (num < 0) |
paul@221 | 234 | return NULL; |
paul@221 | 235 | |
paul@221 | 236 | return &dma_regions[num]; |
paul@221 | 237 | } |
paul@218 | 238 | |
paul@236 | 239 | static struct memory_region *_get_memory_region(void) |
paul@236 | 240 | { |
paul@236 | 241 | int num = get_region_number(num_memory_regions); |
paul@236 | 242 | |
paul@236 | 243 | if (num < 0) |
paul@236 | 244 | return NULL; |
paul@236 | 245 | |
paul@236 | 246 | return &memory_regions[num]; |
paul@236 | 247 | } |
paul@236 | 248 | |
paul@236 | 249 | static int get_memory_region(unsigned int size, l4_addr_t *addr) |
paul@236 | 250 | { |
paul@253 | 251 | void *a = calloc(size, sizeof(char)); |
paul@253 | 252 | |
paul@253 | 253 | *addr = (l4_addr_t) a; |
paul@253 | 254 | return a == NULL ? 1 : 0; |
paul@236 | 255 | } |
paul@236 | 256 | |
paul@218 | 257 | |
paul@218 | 258 | |
paul@218 | 259 | /* AIC/I2S operations. */ |
paul@218 | 260 | |
paul@218 | 261 | static void new_aic_channel(void *aic) |
paul@218 | 262 | { |
paul@218 | 263 | int num = get_channel_number(num_aic_channels); |
paul@218 | 264 | void *channel; |
paul@218 | 265 | |
paul@218 | 266 | if (num < 0) |
paul@218 | 267 | return; |
paul@218 | 268 | |
paul@218 | 269 | channel = get_channel(num_dma_channels, dma_channels, NULL); |
paul@218 | 270 | |
paul@218 | 271 | if (channel == NULL) |
paul@218 | 272 | return; |
paul@218 | 273 | |
paul@218 | 274 | aic_channels[num] = aic_get_channel(aic, num, channel); |
paul@218 | 275 | } |
paul@218 | 276 | |
paul@218 | 277 | static void aic_transfer_data(void) |
paul@218 | 278 | { |
paul@218 | 279 | void *channel = get_channel(num_aic_channels, aic_channels, NULL); |
paul@221 | 280 | struct dma_region *region; |
paul@221 | 281 | uint32_t count, rate, resolution, transferred; |
paul@218 | 282 | |
paul@218 | 283 | if (channel == NULL) |
paul@218 | 284 | return; |
paul@218 | 285 | |
paul@221 | 286 | region = _get_dma_region(); |
paul@221 | 287 | |
paul@221 | 288 | if (region == NULL) |
paul@221 | 289 | return; |
paul@221 | 290 | |
paul@218 | 291 | if (!read_number("Sample size", &count)) |
paul@218 | 292 | return; |
paul@218 | 293 | |
paul@218 | 294 | if (!read_number("Sample rate", &rate)) |
paul@218 | 295 | return; |
paul@218 | 296 | |
paul@218 | 297 | if (!read_number("Sample resolution", &resolution)) |
paul@218 | 298 | return; |
paul@218 | 299 | |
paul@221 | 300 | transferred = aic_transfer(channel, region->paddr, count, rate, resolution); |
paul@218 | 301 | |
paul@218 | 302 | printf("Transferred: %d\n", transferred); |
paul@218 | 303 | } |
paul@218 | 304 | |
paul@218 | 305 | |
paul@218 | 306 | |
paul@218 | 307 | /* CPM operations. */ |
paul@218 | 308 | |
paul@218 | 309 | static const char *clock_id(enum Clock_identifiers clock) |
paul@218 | 310 | { |
paul@218 | 311 | for (int i = 0; clocks[i].id != NULL; i++) |
paul@218 | 312 | if (clock == clocks[i].clock) |
paul@218 | 313 | return clocks[i].id; |
paul@218 | 314 | |
paul@218 | 315 | return NULL; |
paul@218 | 316 | } |
paul@218 | 317 | |
paul@218 | 318 | static enum Clock_identifiers get_clock(char *id) |
paul@218 | 319 | { |
paul@218 | 320 | if (id != NULL) |
paul@218 | 321 | { |
paul@218 | 322 | for (int i = 0; clocks[i].id != NULL; i++) |
paul@218 | 323 | if (!strcmp(id, clocks[i].id)) |
paul@218 | 324 | return clocks[i].clock; |
paul@218 | 325 | } |
paul@218 | 326 | |
paul@243 | 327 | return Clock_none; |
paul@218 | 328 | } |
paul@218 | 329 | |
paul@218 | 330 | static const char list_clocks_separator[] = "--------------------------------"; |
paul@218 | 331 | |
paul@218 | 332 | #define sep(x) (list_clocks_separator + sizeof(list_clocks_separator) - 1 - x) |
paul@218 | 333 | |
paul@218 | 334 | static void list_clocks(void *cpm) |
paul@218 | 335 | { |
paul@218 | 336 | /* Read information from the clock and power management unit. */ |
paul@218 | 337 | |
paul@239 | 338 | printf("%-10s %-15s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n" |
paul@239 | 339 | "%-10s %-15s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n", |
paul@218 | 340 | "Id", "Clock", "Type", "Source", "Source Clock", "Source Freq.", |
paul@218 | 341 | "Parameters", "Frequency", "On", |
paul@239 | 342 | sep(10), sep(15), sep(7), sep(6), sep(12), sep(12), |
paul@218 | 343 | sep(24), sep(12), sep(3)); |
paul@218 | 344 | |
paul@218 | 345 | for (int i = 0; clocks[i].id != NULL; i++) |
paul@218 | 346 | { |
paul@218 | 347 | uint32_t parameters[4]; |
paul@218 | 348 | char parameter_str[25] = {0}; |
paul@218 | 349 | const char *source_id = clock_id(cpm_get_source_clock(cpm, clocks[i].clock)); |
paul@218 | 350 | int num_parameters = cpm_get_parameters(cpm, clocks[i].clock, parameters); |
paul@218 | 351 | |
paul@218 | 352 | for (int p = 0, pos = 0; p < num_parameters; p++) |
paul@218 | 353 | { |
paul@218 | 354 | int result = sprintf(parameter_str + pos, "%-7d ", parameters[p]); |
paul@218 | 355 | if (result < 0) |
paul@218 | 356 | break; |
paul@218 | 357 | pos += result; |
paul@218 | 358 | } |
paul@218 | 359 | |
paul@239 | 360 | printf("%-10s %-15s %-7s %-6d %-12s %-12lld %-24s %-12lld %-3s\n", |
paul@218 | 361 | clocks[i].id, |
paul@218 | 362 | clocks[i].name, |
paul@218 | 363 | cpm_clock_type(cpm, clocks[i].clock), |
paul@218 | 364 | cpm_get_source(cpm, clocks[i].clock), |
paul@218 | 365 | source_id != NULL ? source_id : "...", |
paul@218 | 366 | cpm_get_source_frequency(cpm, clocks[i].clock), |
paul@218 | 367 | parameter_str, |
paul@218 | 368 | cpm_get_frequency(cpm, clocks[i].clock), |
paul@218 | 369 | cpm_have_clock(cpm, clocks[i].clock) ? "on" : "off"); |
paul@218 | 370 | } |
paul@218 | 371 | } |
paul@218 | 372 | |
paul@218 | 373 | static enum Clock_identifiers get_clock_by_name(void) |
paul@218 | 374 | { |
paul@218 | 375 | char *token; |
paul@218 | 376 | enum Clock_identifiers clock; |
paul@218 | 377 | |
paul@218 | 378 | if ((token = read_token("Clock")) == NULL) |
paul@243 | 379 | return Clock_none; |
paul@218 | 380 | |
paul@218 | 381 | clock = get_clock(token); |
paul@218 | 382 | |
paul@243 | 383 | if (clock == Clock_none) |
paul@218 | 384 | { |
paul@218 | 385 | printf("Unrecognised clock: %s\n", token); |
paul@243 | 386 | return Clock_none; |
paul@218 | 387 | } |
paul@218 | 388 | |
paul@218 | 389 | return clock; |
paul@218 | 390 | } |
paul@218 | 391 | |
paul@218 | 392 | static void set_frequency(void *cpm) |
paul@218 | 393 | { |
paul@218 | 394 | enum Clock_identifiers clock; |
paul@218 | 395 | unsigned int frequency; |
paul@218 | 396 | |
paul@218 | 397 | clock = get_clock_by_name(); |
paul@218 | 398 | |
paul@243 | 399 | if (clock == Clock_none) |
paul@218 | 400 | return; |
paul@218 | 401 | |
paul@218 | 402 | if (!read_number("Frequency", &frequency)) |
paul@218 | 403 | return; |
paul@218 | 404 | |
paul@218 | 405 | if (!cpm_set_frequency(cpm, clock, frequency)) |
paul@218 | 406 | printf("Frequency not set.\n"); |
paul@218 | 407 | } |
paul@218 | 408 | |
paul@218 | 409 | static void set_parameters(void *cpm) |
paul@218 | 410 | { |
paul@218 | 411 | char *token; |
paul@218 | 412 | enum Clock_identifiers clock; |
paul@218 | 413 | uint32_t parameters[4]; |
paul@218 | 414 | int num_parameters; |
paul@218 | 415 | |
paul@218 | 416 | clock = get_clock_by_name(); |
paul@218 | 417 | |
paul@243 | 418 | if (clock == Clock_none) |
paul@218 | 419 | return; |
paul@218 | 420 | |
paul@218 | 421 | for (num_parameters = 0; num_parameters < 4; num_parameters++) |
paul@218 | 422 | { |
paul@218 | 423 | if ((token = read_token(NULL)) == NULL) |
paul@218 | 424 | break; |
paul@218 | 425 | |
paul@218 | 426 | parameters[num_parameters] = atoi(token); |
paul@218 | 427 | } |
paul@218 | 428 | |
paul@218 | 429 | if (!num_parameters) |
paul@218 | 430 | { |
paul@218 | 431 | printf("Parameters?\n"); |
paul@218 | 432 | return; |
paul@218 | 433 | } |
paul@218 | 434 | |
paul@218 | 435 | if (!cpm_set_parameters(cpm, clock, num_parameters, parameters)) |
paul@218 | 436 | printf("Too few or invalid parameters.\n"); |
paul@218 | 437 | } |
paul@218 | 438 | |
paul@218 | 439 | static void set_source(void *cpm) |
paul@218 | 440 | { |
paul@218 | 441 | enum Clock_identifiers clock; |
paul@218 | 442 | unsigned int source; |
paul@218 | 443 | |
paul@218 | 444 | clock = get_clock_by_name(); |
paul@218 | 445 | |
paul@243 | 446 | if (clock == Clock_none) |
paul@218 | 447 | return; |
paul@218 | 448 | |
paul@218 | 449 | if (!read_number("Source", &source)) |
paul@218 | 450 | return; |
paul@218 | 451 | |
paul@218 | 452 | cpm_set_source(cpm, clock, source); |
paul@218 | 453 | } |
paul@218 | 454 | |
paul@218 | 455 | static void start_clock(void *cpm) |
paul@218 | 456 | { |
paul@218 | 457 | enum Clock_identifiers clock; |
paul@218 | 458 | |
paul@218 | 459 | clock = get_clock_by_name(); |
paul@218 | 460 | |
paul@243 | 461 | if (clock == Clock_none) |
paul@218 | 462 | return; |
paul@218 | 463 | |
paul@218 | 464 | cpm_start_clock(cpm, clock); |
paul@218 | 465 | } |
paul@218 | 466 | |
paul@218 | 467 | static void stop_clock(void *cpm) |
paul@218 | 468 | { |
paul@218 | 469 | enum Clock_identifiers clock; |
paul@218 | 470 | |
paul@218 | 471 | clock = get_clock_by_name(); |
paul@218 | 472 | |
paul@243 | 473 | if (clock == Clock_none) |
paul@218 | 474 | return; |
paul@218 | 475 | |
paul@218 | 476 | cpm_stop_clock(cpm, clock); |
paul@218 | 477 | } |
paul@218 | 478 | |
paul@218 | 479 | |
paul@218 | 480 | |
paul@218 | 481 | /* DMA configuration. */ |
paul@218 | 482 | |
paul@218 | 483 | static l4_uint32_t dma_irq_start = 0, dma_irq_end = 0; |
paul@218 | 484 | |
paul@218 | 485 | /* DMA operations. */ |
paul@218 | 486 | |
paul@218 | 487 | static int init_dma(void) |
paul@218 | 488 | { |
paul@218 | 489 | unsigned int i; |
paul@218 | 490 | |
paul@218 | 491 | for (i = 0; i < num_dma_regions; i++) |
paul@218 | 492 | dma_regions[i].mem = L4_INVALID_CAP; |
paul@218 | 493 | |
paul@218 | 494 | /* Here, only one IRQ is used. */ |
paul@218 | 495 | |
paul@218 | 496 | dma_irq = l4re_util_cap_alloc(); |
paul@218 | 497 | return init_irq(0, dma_irq, dma_irq_start, dma_irq_end); |
paul@218 | 498 | } |
paul@218 | 499 | |
paul@261 | 500 | static void _show_dma_region(struct dma_region *region, uint32_t offset) |
paul@261 | 501 | { |
paul@261 | 502 | unsigned int i; |
paul@261 | 503 | |
paul@261 | 504 | printf("size = %d; align = %d; virtual = 0x%lx; physical = 0x%llx\ndata (offset 0x%x) =", |
paul@261 | 505 | region->size, region->align, region->vaddr, region->paddr, offset); |
paul@261 | 506 | |
paul@261 | 507 | for (i = 0; (i < region->size) && (i < 16); i++) |
paul@261 | 508 | printf(" %02x", *((uint8_t *) region->vaddr + offset + i)); |
paul@261 | 509 | |
paul@261 | 510 | printf("\n"); |
paul@261 | 511 | } |
paul@261 | 512 | |
paul@261 | 513 | static void show_dma_region(void) |
paul@261 | 514 | { |
paul@261 | 515 | struct dma_region *region = _get_dma_region(); |
paul@261 | 516 | uint32_t offset; |
paul@261 | 517 | |
paul@261 | 518 | if (region == NULL) |
paul@261 | 519 | return; |
paul@261 | 520 | |
paul@261 | 521 | if (!read_number("Offset", &offset)) |
paul@261 | 522 | return; |
paul@261 | 523 | |
paul@261 | 524 | _show_dma_region(region, offset); |
paul@261 | 525 | } |
paul@261 | 526 | |
paul@218 | 527 | static void list_dma_regions(void) |
paul@218 | 528 | { |
paul@218 | 529 | unsigned int num; |
paul@218 | 530 | struct dma_region *region; |
paul@218 | 531 | |
paul@218 | 532 | for (num = 0; num < num_dma_regions; num++) |
paul@218 | 533 | { |
paul@218 | 534 | printf("Region %d: ", num); |
paul@218 | 535 | |
paul@218 | 536 | region = &dma_regions[num]; |
paul@218 | 537 | |
paul@218 | 538 | if (l4_is_invalid_cap(region->mem)) |
paul@218 | 539 | printf("(inactive)\n"); |
paul@218 | 540 | else |
paul@261 | 541 | _show_dma_region(region, 0); |
paul@218 | 542 | } |
paul@218 | 543 | } |
paul@218 | 544 | |
paul@218 | 545 | static void new_dma_channel(void *dma) |
paul@218 | 546 | { |
paul@218 | 547 | int num = get_channel_number(num_dma_channels); |
paul@218 | 548 | |
paul@218 | 549 | if (num < 0) |
paul@218 | 550 | return; |
paul@218 | 551 | |
paul@218 | 552 | if (dma_channels[num] != NULL) |
paul@218 | 553 | { |
paul@218 | 554 | printf("Channel already defined.\n"); |
paul@218 | 555 | return; |
paul@218 | 556 | } |
paul@218 | 557 | |
paul@218 | 558 | dma_channels[num] = dma_get_channel(dma, num, dma_irq); |
paul@218 | 559 | } |
paul@218 | 560 | |
paul@218 | 561 | static void new_dma_region(void) |
paul@218 | 562 | { |
paul@218 | 563 | struct dma_region *region = _get_dma_region(); |
paul@218 | 564 | |
paul@218 | 565 | if (region == NULL) |
paul@218 | 566 | { |
paul@218 | 567 | list_dma_regions(); |
paul@218 | 568 | return; |
paul@218 | 569 | } |
paul@218 | 570 | |
paul@218 | 571 | if (l4_is_valid_cap(region->mem)) |
paul@218 | 572 | { |
paul@218 | 573 | printf("Region already defined.\n"); |
paul@218 | 574 | return; |
paul@218 | 575 | } |
paul@218 | 576 | |
paul@218 | 577 | if (!read_number("Size", ®ion->size)) |
paul@218 | 578 | return; |
paul@218 | 579 | |
paul@218 | 580 | if (!read_number("Alignment", ®ion->align)) |
paul@218 | 581 | return; |
paul@218 | 582 | |
paul@218 | 583 | if (get_dma_region(region->size, region->align, ®ion->vaddr, ®ion->paddr, ®ion->mem)) |
paul@218 | 584 | printf("Could not allocate region.\n"); |
paul@218 | 585 | } |
paul@218 | 586 | |
paul@218 | 587 | static void set_dma_region(void) |
paul@218 | 588 | { |
paul@218 | 589 | char *token; |
paul@218 | 590 | struct dma_region *region = _get_dma_region(); |
paul@218 | 591 | FILE *fp; |
paul@218 | 592 | |
paul@218 | 593 | if (region == NULL) |
paul@218 | 594 | { |
paul@218 | 595 | list_dma_regions(); |
paul@218 | 596 | return; |
paul@218 | 597 | } |
paul@218 | 598 | |
paul@218 | 599 | if (l4_is_invalid_cap(region->mem)) |
paul@218 | 600 | { |
paul@218 | 601 | printf("Region needs creating.\n"); |
paul@218 | 602 | return; |
paul@218 | 603 | } |
paul@218 | 604 | |
paul@218 | 605 | memset((void *) region->vaddr, 0, region->size); |
paul@218 | 606 | |
paul@218 | 607 | if ((token = read_token("Filename")) == NULL) |
paul@218 | 608 | return; |
paul@218 | 609 | |
paul@218 | 610 | /* Populate the region from the file. */ |
paul@218 | 611 | |
paul@218 | 612 | fp = fopen(token, "r"); |
paul@218 | 613 | |
paul@218 | 614 | if (fp == NULL) |
paul@218 | 615 | { |
paul@218 | 616 | printf("File not readable.\n"); |
paul@218 | 617 | return; |
paul@218 | 618 | } |
paul@218 | 619 | |
paul@218 | 620 | fread((char *) region->vaddr, sizeof(char), region->size, fp); |
paul@218 | 621 | fclose(fp); |
paul@218 | 622 | |
paul@218 | 623 | l4_cache_flush_data(region->vaddr, region->vaddr + region->size); |
paul@218 | 624 | } |
paul@218 | 625 | |
paul@218 | 626 | static void dma_transfer_data(void) |
paul@218 | 627 | { |
paul@221 | 628 | uint32_t count, to_transfer, transferred; |
paul@218 | 629 | unsigned int source_address, destination_address, source_width, destination_width, |
paul@218 | 630 | transfer_unit_size, request_type; |
paul@218 | 631 | int source_increment, destination_increment; |
paul@218 | 632 | void *channel = get_channel(num_dma_channels, dma_channels, NULL); |
paul@218 | 633 | unsigned int i; |
paul@218 | 634 | |
paul@218 | 635 | if (channel == NULL) |
paul@218 | 636 | return; |
paul@218 | 637 | |
paul@218 | 638 | if (!read_number("Source address", &source_address)) |
paul@218 | 639 | return; |
paul@218 | 640 | |
paul@218 | 641 | if (!read_number("Destination address", &destination_address)) |
paul@218 | 642 | return; |
paul@218 | 643 | |
paul@218 | 644 | if (!read_number("Transfer size", &count)) |
paul@218 | 645 | return; |
paul@218 | 646 | |
paul@236 | 647 | if (!read_flag("Source increment", &source_increment, "i", 1)) |
paul@218 | 648 | return; |
paul@218 | 649 | |
paul@236 | 650 | if (!read_flag("Destination increment", &destination_increment, "i", 1)) |
paul@218 | 651 | return; |
paul@218 | 652 | |
paul@218 | 653 | if (!read_number("Source width", &source_width)) |
paul@218 | 654 | return; |
paul@218 | 655 | |
paul@218 | 656 | if (!read_number("Destination width", &destination_width)) |
paul@218 | 657 | return; |
paul@218 | 658 | |
paul@218 | 659 | if (!read_number("Transfer unit size", &transfer_unit_size)) |
paul@218 | 660 | return; |
paul@218 | 661 | |
paul@218 | 662 | if (!read_number("Request type", &request_type)) |
paul@218 | 663 | return; |
paul@218 | 664 | |
paul@218 | 665 | /* Invalidate DMA region contents in case of transfers between regions. */ |
paul@218 | 666 | |
paul@218 | 667 | for (i = 0; i < num_dma_regions; i++) |
paul@218 | 668 | l4_cache_inv_data(dma_regions[i].vaddr, dma_regions[i].vaddr + dma_regions[i].size); |
paul@218 | 669 | |
paul@218 | 670 | to_transfer = dma_transfer(channel, source_address, destination_address, |
paul@221 | 671 | count, |
paul@221 | 672 | source_increment, destination_increment, |
paul@221 | 673 | source_width, destination_width, |
paul@221 | 674 | transfer_unit_size, |
paul@221 | 675 | request_type); |
paul@218 | 676 | |
paul@218 | 677 | transferred = to_transfer ? count - dma_wait(channel) : 0; |
paul@218 | 678 | |
paul@218 | 679 | printf("Transferred: %d\n", transferred); |
paul@218 | 680 | } |
paul@218 | 681 | |
paul@218 | 682 | |
paul@218 | 683 | |
paul@218 | 684 | /* GPIO operations. */ |
paul@218 | 685 | |
paul@218 | 686 | static char get_gpio_dir_label(unsigned int value, unsigned int level) |
paul@218 | 687 | { |
paul@218 | 688 | return value == Fix_input ? (level ? 'I' : 'i') : (level ? 'O' : 'o'); |
paul@218 | 689 | } |
paul@218 | 690 | |
paul@218 | 691 | static char get_gpio_irq_label(unsigned int value) |
paul@218 | 692 | { |
paul@218 | 693 | switch (value) |
paul@218 | 694 | { |
paul@218 | 695 | case L4_IRQ_F_NEG_EDGE: return 'f'; |
paul@218 | 696 | case L4_IRQ_F_POS_EDGE: return 'r'; |
paul@218 | 697 | case L4_IRQ_F_LEVEL_LOW: return 'l'; |
paul@218 | 698 | case L4_IRQ_F_LEVEL_HIGH: return 'h'; |
paul@218 | 699 | default: return '?'; |
paul@218 | 700 | } |
paul@218 | 701 | } |
paul@218 | 702 | |
paul@218 | 703 | static void list_gpios(void *gpio[]) |
paul@218 | 704 | { |
paul@218 | 705 | unsigned int port, pin; |
paul@218 | 706 | unsigned int func, value, level; |
paul@218 | 707 | |
paul@218 | 708 | printf("gpio: I = input high; i = input low; O = output high; o = output low\n" |
paul@218 | 709 | "irq: h = high level; l = low level; r = rising edge; f = falling edge\n" |
paul@218 | 710 | "alt: function number\n\n"); |
paul@218 | 711 | |
paul@218 | 712 | /* Show pin numbering. */ |
paul@218 | 713 | |
paul@218 | 714 | printf("Port/Pin "); |
paul@218 | 715 | |
paul@218 | 716 | for (pin = 0; pin < 32; pin++) |
paul@218 | 717 | { |
paul@218 | 718 | if (!(pin % 10)) |
paul@218 | 719 | printf(" %d", pin); |
paul@218 | 720 | else |
paul@218 | 721 | printf(" %d", pin % 10); |
paul@218 | 722 | } |
paul@218 | 723 | |
paul@218 | 724 | printf("\n"); |
paul@218 | 725 | |
paul@218 | 726 | for (port = 0; port < num_gpio_ports; port++) |
paul@218 | 727 | { |
paul@218 | 728 | /* Show port and pin configuration. */ |
paul@218 | 729 | |
paul@218 | 730 | printf("%c ", gpio_port_labels[port]); |
paul@218 | 731 | |
paul@218 | 732 | for (pin = 0; pin < 32; pin++) |
paul@218 | 733 | { |
paul@218 | 734 | /* Pad below the first pin digit for multiples of ten other than zero. */ |
paul@218 | 735 | |
paul@218 | 736 | if (pin && !(pin % 10)) |
paul@218 | 737 | printf(" "); |
paul@218 | 738 | |
paul@218 | 739 | gpio_config_pad_get(gpio[port], pin, &func, &value); |
paul@218 | 740 | |
paul@218 | 741 | switch (func) |
paul@218 | 742 | { |
paul@218 | 743 | case Function_alt: printf(" %d", value); break; |
paul@218 | 744 | case Function_irq: printf(" %c", get_gpio_irq_label(value)); break; |
paul@218 | 745 | case Function_gpio: |
paul@218 | 746 | { |
paul@218 | 747 | level = gpio_get(gpio[port], pin); |
paul@218 | 748 | printf(" %c", get_gpio_dir_label(value, level)); |
paul@218 | 749 | break; |
paul@218 | 750 | } |
paul@218 | 751 | default: printf(" ?"); break; |
paul@218 | 752 | } |
paul@218 | 753 | } |
paul@218 | 754 | |
paul@218 | 755 | printf("\n"); |
paul@218 | 756 | } |
paul@218 | 757 | } |
paul@218 | 758 | |
paul@218 | 759 | static int get_port_and_pin(unsigned int *port, unsigned int *pin) |
paul@218 | 760 | { |
paul@218 | 761 | char *token; |
paul@218 | 762 | |
paul@218 | 763 | if ((token = read_token("Port")) == NULL) |
paul@218 | 764 | return 0; |
paul@218 | 765 | |
paul@218 | 766 | if ((token[0] < 'A') || ((unsigned int) token[0] - (unsigned int) 'A' >= num_gpio_ports)) |
paul@218 | 767 | { |
paul@218 | 768 | printf("Bad port: %c\n", token[0]); |
paul@218 | 769 | return 0; |
paul@218 | 770 | } |
paul@218 | 771 | |
paul@218 | 772 | *port = (unsigned int) token[0] - (unsigned int) 'A'; |
paul@218 | 773 | |
paul@218 | 774 | if (!read_number("Pin", pin)) |
paul@218 | 775 | return 0; |
paul@218 | 776 | |
paul@218 | 777 | return 1; |
paul@218 | 778 | } |
paul@218 | 779 | |
paul@218 | 780 | static void set_gpio_alt_func(void *gpio[]) |
paul@218 | 781 | { |
paul@218 | 782 | unsigned int port, pin, value; |
paul@218 | 783 | |
paul@218 | 784 | if (!get_port_and_pin(&port, &pin)) |
paul@218 | 785 | return; |
paul@218 | 786 | |
paul@218 | 787 | if (!read_number("Function", &value)) |
paul@218 | 788 | return; |
paul@218 | 789 | |
paul@218 | 790 | gpio_config_pad(gpio[port], pin, Function_alt, value); |
paul@218 | 791 | } |
paul@218 | 792 | |
paul@218 | 793 | static void set_gpio_pad(void *gpio[]) |
paul@218 | 794 | { |
paul@218 | 795 | char *token; |
paul@218 | 796 | unsigned int port, pin, mode, value = 0; |
paul@218 | 797 | |
paul@218 | 798 | if (!get_port_and_pin(&port, &pin)) |
paul@218 | 799 | return; |
paul@218 | 800 | |
paul@218 | 801 | if ((token = read_token("Mode")) == NULL) |
paul@218 | 802 | return; |
paul@218 | 803 | |
paul@218 | 804 | if (!strcmp(token, "in")) |
paul@218 | 805 | mode = Fix_input; |
paul@218 | 806 | else if (!strcmp(token, "irq")) |
paul@218 | 807 | mode = Fix_irq; |
paul@218 | 808 | else if (!strcmp(token, "out")) |
paul@218 | 809 | { |
paul@218 | 810 | mode = Fix_output; |
paul@218 | 811 | |
paul@218 | 812 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 813 | value = atoi(token); |
paul@218 | 814 | } |
paul@218 | 815 | else |
paul@218 | 816 | { |
paul@218 | 817 | printf("Mode not recognised.\n"); |
paul@218 | 818 | return; |
paul@218 | 819 | } |
paul@218 | 820 | |
paul@218 | 821 | gpio_setup(gpio[port], pin, mode, value); |
paul@218 | 822 | } |
paul@218 | 823 | |
paul@218 | 824 | static void set_gpio_pull(void *gpio[]) |
paul@218 | 825 | { |
paul@218 | 826 | char *token; |
paul@218 | 827 | unsigned int port, pin, mode; |
paul@218 | 828 | |
paul@218 | 829 | if (!get_port_and_pin(&port, &pin)) |
paul@218 | 830 | return; |
paul@218 | 831 | |
paul@218 | 832 | if ((token = read_token("Mode")) == NULL) |
paul@218 | 833 | return; |
paul@218 | 834 | |
paul@218 | 835 | if (!strcmp(token, "down")) |
paul@218 | 836 | mode = Pull_down; |
paul@218 | 837 | else if (!strcmp(token, "none")) |
paul@218 | 838 | mode = Pull_none; |
paul@218 | 839 | else if (!strcmp(token, "up")) |
paul@218 | 840 | mode = Pull_up; |
paul@218 | 841 | else |
paul@218 | 842 | { |
paul@218 | 843 | printf("Mode not recognised.\n"); |
paul@218 | 844 | return; |
paul@218 | 845 | } |
paul@218 | 846 | |
paul@218 | 847 | gpio_config_pull(gpio[port], pin, mode); |
paul@218 | 848 | } |
paul@218 | 849 | |
paul@218 | 850 | |
paul@218 | 851 | |
paul@218 | 852 | /* I2C configuration. */ |
paul@218 | 853 | |
paul@218 | 854 | static l4_uint32_t i2c_irq_start = 0, i2c_irq_end = 0; |
paul@218 | 855 | |
paul@218 | 856 | /* I2C operations. */ |
paul@218 | 857 | |
paul@218 | 858 | static int _i2c_read(void *channel, uint8_t *buf, unsigned length, |
paul@218 | 859 | int stop, l4_cap_idx_t irqcap, unsigned timeout) |
paul@218 | 860 | { |
paul@218 | 861 | l4_msgtag_t tag; |
paul@218 | 862 | |
paul@218 | 863 | i2c_start_read(channel, buf, length, stop); |
paul@218 | 864 | |
paul@218 | 865 | while (!i2c_read_done(channel)) |
paul@218 | 866 | { |
paul@218 | 867 | tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(timeout))); |
paul@218 | 868 | |
paul@218 | 869 | // NOTE: Error not returned. |
paul@218 | 870 | |
paul@218 | 871 | if (l4_ipc_error(tag, l4_utcb())) |
paul@218 | 872 | break; |
paul@218 | 873 | |
paul@218 | 874 | if (i2c_failed(channel)) |
paul@218 | 875 | break; |
paul@218 | 876 | |
paul@218 | 877 | i2c_read(channel); |
paul@218 | 878 | } |
paul@218 | 879 | |
paul@218 | 880 | if (stop) |
paul@218 | 881 | i2c_stop(channel); |
paul@218 | 882 | |
paul@218 | 883 | return i2c_have_read(channel); |
paul@218 | 884 | } |
paul@218 | 885 | |
paul@218 | 886 | static int _i2c_write(void *channel, uint8_t *buf, unsigned length, |
paul@218 | 887 | int stop, l4_cap_idx_t irqcap, unsigned timeout) |
paul@218 | 888 | { |
paul@218 | 889 | l4_msgtag_t tag; |
paul@218 | 890 | |
paul@218 | 891 | i2c_start_write(channel, buf, length, stop); |
paul@218 | 892 | |
paul@218 | 893 | while (!i2c_write_done(channel)) |
paul@218 | 894 | { |
paul@218 | 895 | tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(timeout))); |
paul@218 | 896 | |
paul@218 | 897 | // NOTE: Error not returned. |
paul@218 | 898 | |
paul@218 | 899 | if (l4_ipc_error(tag, l4_utcb())) |
paul@218 | 900 | break; |
paul@218 | 901 | |
paul@218 | 902 | if (i2c_failed(channel)) |
paul@218 | 903 | break; |
paul@218 | 904 | |
paul@218 | 905 | i2c_write(channel); |
paul@218 | 906 | } |
paul@218 | 907 | |
paul@218 | 908 | if (stop) |
paul@218 | 909 | i2c_stop(channel); |
paul@218 | 910 | |
paul@218 | 911 | return i2c_have_written(channel); |
paul@218 | 912 | } |
paul@218 | 913 | |
paul@218 | 914 | static void list_i2c_channels(void) |
paul@218 | 915 | { |
paul@218 | 916 | unsigned int num; |
paul@218 | 917 | void *channel; |
paul@218 | 918 | |
paul@218 | 919 | for (num = 0; num < num_i2c_channels; num++) |
paul@218 | 920 | { |
paul@218 | 921 | printf("Channel %d: ", num); |
paul@218 | 922 | |
paul@218 | 923 | channel = i2c_channels[num]; |
paul@218 | 924 | |
paul@218 | 925 | if (channel == NULL) |
paul@218 | 926 | printf("(inactive)\n"); |
paul@218 | 927 | else |
paul@218 | 928 | printf("%d Hz\n", i2c_get_frequency(channel)); |
paul@218 | 929 | } |
paul@218 | 930 | } |
paul@218 | 931 | |
paul@218 | 932 | static void new_i2c_channel(void *i2c) |
paul@218 | 933 | { |
paul@218 | 934 | l4_cap_idx_t irqcap; |
paul@218 | 935 | int num = get_channel_number(num_i2c_channels); |
paul@218 | 936 | |
paul@218 | 937 | if (num < 0) |
paul@218 | 938 | return; |
paul@218 | 939 | |
paul@218 | 940 | irqcap = l4re_util_cap_alloc(); |
paul@218 | 941 | |
paul@218 | 942 | if (init_irq(num, irqcap, i2c_irq_start, i2c_irq_end)) |
paul@218 | 943 | return; |
paul@218 | 944 | |
paul@218 | 945 | i2c_channels[num] = i2c_get_channel(i2c, num); |
paul@218 | 946 | i2c_irqs[num] = irqcap; |
paul@218 | 947 | } |
paul@218 | 948 | |
paul@218 | 949 | static void i2c_get(void) |
paul@218 | 950 | { |
paul@218 | 951 | void *channel; |
paul@218 | 952 | int num; |
paul@218 | 953 | uint8_t buffer[32]; |
paul@218 | 954 | unsigned int address, reg, count; |
paul@218 | 955 | int i, transferred; |
paul@218 | 956 | |
paul@218 | 957 | channel = get_channel(num_i2c_channels, i2c_channels, &num); |
paul@218 | 958 | |
paul@218 | 959 | if (channel == NULL) |
paul@218 | 960 | return; |
paul@218 | 961 | |
paul@218 | 962 | if (!read_encoded_number("Address", "%2x", &address)) |
paul@218 | 963 | return; |
paul@218 | 964 | |
paul@218 | 965 | if (address >= 0x80) |
paul@218 | 966 | { |
paul@218 | 967 | printf("Address must be less than 80.\n"); |
paul@218 | 968 | return; |
paul@218 | 969 | } |
paul@218 | 970 | |
paul@218 | 971 | if (!read_encoded_number("Register", "%2x", ®)) |
paul@218 | 972 | return; |
paul@218 | 973 | |
paul@218 | 974 | if (!read_number(NULL, &count)) |
paul@218 | 975 | count = 1; |
paul@218 | 976 | |
paul@218 | 977 | buffer[0] = (uint8_t) (reg & 0xff); |
paul@218 | 978 | |
paul@218 | 979 | i2c_set_target(channel, address); |
paul@218 | 980 | |
paul@218 | 981 | if (!_i2c_write(channel, buffer, 1, 0, i2c_irqs[num], 1000000)) |
paul@218 | 982 | { |
paul@218 | 983 | printf("Register request failed.\n"); |
paul@218 | 984 | return; |
paul@218 | 985 | } |
paul@218 | 986 | |
paul@218 | 987 | transferred = _i2c_read(channel, buffer, count, 1, i2c_irqs[num], 1000000); |
paul@218 | 988 | |
paul@218 | 989 | if (!transferred) |
paul@218 | 990 | { |
paul@218 | 991 | printf("Register read failed.\n"); |
paul@218 | 992 | return; |
paul@218 | 993 | } |
paul@218 | 994 | |
paul@218 | 995 | for (i = 0; i < transferred; i++) |
paul@218 | 996 | printf("%02x ", buffer[i]); |
paul@218 | 997 | printf("\n"); |
paul@218 | 998 | } |
paul@218 | 999 | |
paul@218 | 1000 | static void i2c_scan(void) |
paul@218 | 1001 | { |
paul@218 | 1002 | void *channel; |
paul@218 | 1003 | unsigned int address; |
paul@218 | 1004 | uint8_t buffer[1]; |
paul@218 | 1005 | int num; |
paul@218 | 1006 | |
paul@218 | 1007 | channel = get_channel(num_i2c_channels, i2c_channels, &num); |
paul@218 | 1008 | |
paul@218 | 1009 | if (channel == NULL) |
paul@218 | 1010 | return; |
paul@218 | 1011 | |
paul@218 | 1012 | for (address = 0; address < 0x20; address++) |
paul@218 | 1013 | printf("%02x ", address); |
paul@218 | 1014 | printf("\n"); |
paul@218 | 1015 | |
paul@218 | 1016 | for (address = 0; address < 0x20; address++) |
paul@218 | 1017 | printf("-- "); |
paul@218 | 1018 | |
paul@218 | 1019 | for (address = 0; address < 0x80; address++) |
paul@218 | 1020 | { |
paul@218 | 1021 | if ((address % 32) == 0) |
paul@218 | 1022 | printf("\n"); |
paul@218 | 1023 | |
paul@218 | 1024 | i2c_set_target(channel, address); |
paul@218 | 1025 | |
paul@218 | 1026 | if (_i2c_read(channel, buffer, 1, 1, i2c_irqs[num], 1000)) |
paul@218 | 1027 | printf("%02x ", address); |
paul@218 | 1028 | else |
paul@218 | 1029 | printf("-- "); |
paul@218 | 1030 | } |
paul@218 | 1031 | |
paul@218 | 1032 | printf("\n"); |
paul@218 | 1033 | for (address = 0; address < 0x20; address++) |
paul@218 | 1034 | printf("-- "); |
paul@218 | 1035 | printf("\n\n"); |
paul@218 | 1036 | } |
paul@218 | 1037 | |
paul@218 | 1038 | |
paul@218 | 1039 | |
paul@236 | 1040 | /* Memory operations. */ |
paul@236 | 1041 | |
paul@236 | 1042 | static void list_memory_regions(void) |
paul@236 | 1043 | { |
paul@236 | 1044 | unsigned int num; |
paul@236 | 1045 | struct memory_region *region; |
paul@236 | 1046 | unsigned int i; |
paul@236 | 1047 | |
paul@236 | 1048 | for (num = 0; num < num_memory_regions; num++) |
paul@236 | 1049 | { |
paul@236 | 1050 | printf("Region %d: ", num); |
paul@236 | 1051 | |
paul@236 | 1052 | region = &memory_regions[num]; |
paul@236 | 1053 | |
paul@236 | 1054 | if (!region->addr) |
paul@236 | 1055 | printf("(inactive)\n"); |
paul@236 | 1056 | else |
paul@236 | 1057 | { |
paul@236 | 1058 | printf("size = %d; virtual = 0x%lx; data =", |
paul@236 | 1059 | region->size, region->addr); |
paul@236 | 1060 | |
paul@236 | 1061 | for (i = 0; (i < region->size) && (i < 16); i++) |
paul@236 | 1062 | printf(" %02x", *((uint8_t *) region->addr + i)); |
paul@236 | 1063 | |
paul@236 | 1064 | printf("\n"); |
paul@236 | 1065 | } |
paul@236 | 1066 | } |
paul@236 | 1067 | } |
paul@236 | 1068 | |
paul@236 | 1069 | static void new_memory_region(void) |
paul@236 | 1070 | { |
paul@236 | 1071 | struct memory_region *region = _get_memory_region(); |
paul@236 | 1072 | |
paul@236 | 1073 | if (region == NULL) |
paul@236 | 1074 | { |
paul@236 | 1075 | list_memory_regions(); |
paul@236 | 1076 | return; |
paul@236 | 1077 | } |
paul@236 | 1078 | |
paul@236 | 1079 | if (region->addr) |
paul@236 | 1080 | { |
paul@236 | 1081 | printf("Region already defined.\n"); |
paul@236 | 1082 | return; |
paul@236 | 1083 | } |
paul@236 | 1084 | |
paul@236 | 1085 | if (!read_number("Size", ®ion->size)) |
paul@236 | 1086 | return; |
paul@236 | 1087 | |
paul@236 | 1088 | if (get_memory_region(region->size, ®ion->addr)) |
paul@236 | 1089 | printf("Could not allocate region.\n"); |
paul@236 | 1090 | } |
paul@236 | 1091 | |
paul@236 | 1092 | static void set_memory_region(void) |
paul@236 | 1093 | { |
paul@236 | 1094 | char *token; |
paul@236 | 1095 | struct memory_region *region = _get_memory_region(); |
paul@236 | 1096 | FILE *fp; |
paul@236 | 1097 | |
paul@236 | 1098 | if (region == NULL) |
paul@236 | 1099 | { |
paul@236 | 1100 | list_memory_regions(); |
paul@236 | 1101 | return; |
paul@236 | 1102 | } |
paul@236 | 1103 | |
paul@236 | 1104 | if (!region->addr) |
paul@236 | 1105 | { |
paul@236 | 1106 | printf("Region needs creating.\n"); |
paul@236 | 1107 | return; |
paul@236 | 1108 | } |
paul@236 | 1109 | |
paul@236 | 1110 | memset((void *) region->addr, 0, region->size); |
paul@236 | 1111 | |
paul@236 | 1112 | if ((token = read_token("Filename")) == NULL) |
paul@236 | 1113 | return; |
paul@236 | 1114 | |
paul@236 | 1115 | /* Populate the region from the file. */ |
paul@236 | 1116 | |
paul@236 | 1117 | fp = fopen(token, "r"); |
paul@236 | 1118 | |
paul@236 | 1119 | if (fp == NULL) |
paul@236 | 1120 | { |
paul@236 | 1121 | printf("File not readable.\n"); |
paul@236 | 1122 | return; |
paul@236 | 1123 | } |
paul@236 | 1124 | |
paul@236 | 1125 | fread((char *) region->addr, sizeof(char), region->size, fp); |
paul@236 | 1126 | fclose(fp); |
paul@236 | 1127 | |
paul@236 | 1128 | l4_cache_flush_data(region->addr, region->addr + region->size); |
paul@236 | 1129 | } |
paul@236 | 1130 | |
paul@236 | 1131 | |
paul@236 | 1132 | |
paul@258 | 1133 | /* MSC configuration. */ |
paul@258 | 1134 | |
paul@258 | 1135 | static l4_uint32_t msc_irq_start = 0, msc_irq_end = 0; |
paul@258 | 1136 | |
paul@258 | 1137 | /* MSC operations. */ |
paul@258 | 1138 | |
paul@258 | 1139 | static void list_msc_channels(void) |
paul@258 | 1140 | { |
paul@258 | 1141 | unsigned int num; |
paul@258 | 1142 | void *channel; |
paul@258 | 1143 | |
paul@258 | 1144 | printf("Channel Status\n"); |
paul@258 | 1145 | printf("------- --------\n"); |
paul@258 | 1146 | |
paul@258 | 1147 | for (num = 0; num < num_msc_channels; num++) |
paul@258 | 1148 | { |
paul@258 | 1149 | printf("%d ", num); |
paul@258 | 1150 | |
paul@258 | 1151 | channel = msc_channels[num]; |
paul@258 | 1152 | |
paul@258 | 1153 | if (channel == NULL) |
paul@258 | 1154 | printf("inactive\n"); |
paul@258 | 1155 | else |
paul@258 | 1156 | printf("%08x\n", msc_get_status(channel)); |
paul@258 | 1157 | } |
paul@258 | 1158 | } |
paul@258 | 1159 | |
paul@258 | 1160 | static void new_msc_channel(void *msc) |
paul@258 | 1161 | { |
paul@258 | 1162 | l4_cap_idx_t irqcap; |
paul@258 | 1163 | int num = get_channel_number(num_msc_channels); |
paul@258 | 1164 | void *channel; |
paul@258 | 1165 | |
paul@258 | 1166 | if (num < 0) |
paul@258 | 1167 | return; |
paul@258 | 1168 | |
paul@258 | 1169 | channel = get_channel(num_dma_channels, dma_channels, NULL); |
paul@258 | 1170 | |
paul@258 | 1171 | if (channel == NULL) |
paul@258 | 1172 | return; |
paul@258 | 1173 | |
paul@258 | 1174 | irqcap = l4re_util_cap_alloc(); |
paul@258 | 1175 | |
paul@258 | 1176 | if (init_irq(num, irqcap, msc_irq_start, msc_irq_end)) |
paul@258 | 1177 | return; |
paul@258 | 1178 | |
paul@258 | 1179 | msc_channels[num] = msc_get_channel(msc, num, irqcap, channel); |
paul@258 | 1180 | msc_irqs[num] = irqcap; |
paul@258 | 1181 | } |
paul@258 | 1182 | |
paul@258 | 1183 | static void enable_msc_channel(void) |
paul@258 | 1184 | { |
paul@258 | 1185 | void *channel = get_channel(num_msc_channels, msc_channels, NULL); |
paul@258 | 1186 | |
paul@258 | 1187 | if (channel == NULL) |
paul@258 | 1188 | return; |
paul@258 | 1189 | |
paul@258 | 1190 | msc_enable(channel); |
paul@258 | 1191 | } |
paul@258 | 1192 | |
paul@261 | 1193 | static void read_blocks_from_msc(void) |
paul@258 | 1194 | { |
paul@258 | 1195 | void *channel = get_channel(num_msc_channels, msc_channels, NULL); |
paul@258 | 1196 | struct dma_region *dma_region = NULL; |
paul@261 | 1197 | uint32_t card, transferred, block_address, block_count; |
paul@258 | 1198 | |
paul@258 | 1199 | if (channel == NULL) |
paul@258 | 1200 | return; |
paul@258 | 1201 | |
paul@258 | 1202 | dma_region = _get_dma_region(); |
paul@258 | 1203 | |
paul@258 | 1204 | if (dma_region == NULL) |
paul@258 | 1205 | return; |
paul@258 | 1206 | |
paul@258 | 1207 | if (!read_number("Card", &card)) |
paul@258 | 1208 | return; |
paul@258 | 1209 | |
paul@261 | 1210 | if (!read_number("Block address", &block_address)) |
paul@261 | 1211 | return; |
paul@261 | 1212 | |
paul@261 | 1213 | if (!read_number("Block count", &block_count)) |
paul@261 | 1214 | return; |
paul@261 | 1215 | |
paul@261 | 1216 | /* NOTE: Assuming 512-byte blocks. */ |
paul@261 | 1217 | |
paul@261 | 1218 | if (block_count * 512 > dma_region->size) |
paul@261 | 1219 | { |
paul@261 | 1220 | printf("Too many blocks for region.\n"); |
paul@261 | 1221 | return; |
paul@261 | 1222 | } |
paul@261 | 1223 | |
paul@258 | 1224 | l4_cache_inv_data(dma_region->vaddr, dma_region->vaddr + dma_region->size); |
paul@258 | 1225 | |
paul@261 | 1226 | transferred = msc_read_blocks(channel, (uint8_t) card, dma_region->paddr, |
paul@261 | 1227 | block_address, block_count); |
paul@258 | 1228 | |
paul@258 | 1229 | printf("Transferred: %d\n", transferred); |
paul@258 | 1230 | } |
paul@258 | 1231 | |
paul@258 | 1232 | |
paul@258 | 1233 | |
paul@237 | 1234 | /* RTC operations. */ |
paul@237 | 1235 | |
paul@238 | 1236 | static void rtc_reset(void *rtc, void *cpm) |
paul@238 | 1237 | { |
paul@238 | 1238 | unsigned int seconds; |
paul@238 | 1239 | |
paul@238 | 1240 | if (!read_number("Seconds", &seconds)) |
paul@238 | 1241 | return; |
paul@238 | 1242 | |
paul@238 | 1243 | /* NOTE: Assuming EXCLK/512 as RTC source. */ |
paul@238 | 1244 | |
paul@238 | 1245 | uint32_t rtc_seconds = rtc_get_seconds(rtc); |
paul@238 | 1246 | uint32_t value = seconds * cpm_get_frequency(cpm, Clock_external) / 512; |
paul@238 | 1247 | |
paul@239 | 1248 | rtc_alarm_disable(rtc); |
paul@238 | 1249 | rtc_set_alarm_seconds(rtc, rtc_seconds + value); |
paul@238 | 1250 | rtc_hibernate(rtc); |
paul@238 | 1251 | } |
paul@238 | 1252 | |
paul@237 | 1253 | static void _rtc_set_seconds(void *rtc, int alarm) |
paul@237 | 1254 | { |
paul@237 | 1255 | unsigned int seconds; |
paul@237 | 1256 | |
paul@237 | 1257 | if (!read_number("Seconds", &seconds)) |
paul@237 | 1258 | return; |
paul@237 | 1259 | |
paul@237 | 1260 | (alarm ? rtc_set_alarm_seconds : rtc_set_seconds)(rtc, seconds); |
paul@237 | 1261 | } |
paul@237 | 1262 | |
paul@237 | 1263 | |
paul@237 | 1264 | |
paul@218 | 1265 | /* SPI operations. */ |
paul@218 | 1266 | |
paul@222 | 1267 | static void new_spi_channel(void *spi, void *gpio[]) |
paul@221 | 1268 | { |
paul@222 | 1269 | unsigned int control_port, control_pin, control_alt_func_input; |
paul@222 | 1270 | int control_alt_func; |
paul@222 | 1271 | void *control_chip; |
paul@221 | 1272 | int num = get_channel_number(num_spi_channels); |
paul@221 | 1273 | void *channel; |
paul@221 | 1274 | uint32_t frequency; |
paul@221 | 1275 | |
paul@221 | 1276 | if (num < 0) |
paul@221 | 1277 | return; |
paul@221 | 1278 | |
paul@221 | 1279 | channel = get_channel(num_dma_channels, dma_channels, NULL); |
paul@221 | 1280 | |
paul@221 | 1281 | if (channel == NULL) |
paul@221 | 1282 | return; |
paul@221 | 1283 | |
paul@221 | 1284 | if (!read_number("Frequency", &frequency)) |
paul@221 | 1285 | return; |
paul@221 | 1286 | |
paul@222 | 1287 | if (!get_port_and_pin(&control_port, &control_pin)) |
paul@222 | 1288 | control_chip = NULL; |
paul@222 | 1289 | else |
paul@222 | 1290 | control_chip = gpio[control_port]; |
paul@222 | 1291 | |
paul@222 | 1292 | if (!read_number("Function", &control_alt_func_input)) |
paul@222 | 1293 | control_alt_func = -1; |
paul@222 | 1294 | else |
paul@222 | 1295 | control_alt_func = control_alt_func_input; |
paul@222 | 1296 | |
paul@222 | 1297 | spi_channels[num] = spi_get_channel(spi, num, channel, frequency, |
paul@222 | 1298 | control_chip, control_pin, |
paul@222 | 1299 | control_alt_func); |
paul@221 | 1300 | } |
paul@221 | 1301 | |
paul@221 | 1302 | static void new_spi_channel_gpio(void *gpio[]) |
paul@218 | 1303 | { |
paul@222 | 1304 | unsigned int clock_port, clock_pin, data_port, data_pin, enable_port, |
paul@222 | 1305 | enable_pin, control_port, control_pin; |
paul@222 | 1306 | void *control_chip; |
paul@218 | 1307 | int num = get_channel_number(num_spi_channels); |
paul@218 | 1308 | uint32_t frequency; |
paul@218 | 1309 | |
paul@218 | 1310 | if (num < 0) |
paul@218 | 1311 | return; |
paul@218 | 1312 | |
paul@222 | 1313 | if (!read_number("Frequency", &frequency)) |
paul@222 | 1314 | return; |
paul@222 | 1315 | |
paul@218 | 1316 | if (!get_port_and_pin(&clock_port, &clock_pin)) |
paul@218 | 1317 | return; |
paul@218 | 1318 | |
paul@218 | 1319 | if (!get_port_and_pin(&data_port, &data_pin)) |
paul@218 | 1320 | return; |
paul@218 | 1321 | |
paul@218 | 1322 | if (!get_port_and_pin(&enable_port, &enable_pin)) |
paul@218 | 1323 | return; |
paul@218 | 1324 | |
paul@222 | 1325 | if (!get_port_and_pin(&control_port, &control_pin)) |
paul@222 | 1326 | control_chip = NULL; |
paul@222 | 1327 | else |
paul@222 | 1328 | control_chip = gpio[control_port]; |
paul@218 | 1329 | |
paul@222 | 1330 | spi_channels[num] = spi_get_channel_gpio(frequency, |
paul@222 | 1331 | gpio[clock_port], clock_pin, |
paul@221 | 1332 | gpio[data_port], data_pin, |
paul@221 | 1333 | gpio[enable_port], enable_pin, |
paul@222 | 1334 | control_chip, control_pin); |
paul@222 | 1335 | } |
paul@222 | 1336 | |
paul@222 | 1337 | static void spi_control(int acquire) |
paul@222 | 1338 | { |
paul@222 | 1339 | unsigned int level; |
paul@222 | 1340 | void *channel = get_channel(num_spi_channels, spi_channels, NULL); |
paul@222 | 1341 | |
paul@222 | 1342 | if (acquire) |
paul@222 | 1343 | { |
paul@222 | 1344 | if (!read_number("Level", &level)) |
paul@222 | 1345 | return; |
paul@222 | 1346 | |
paul@222 | 1347 | spi_acquire_control(channel, level); |
paul@222 | 1348 | } |
paul@222 | 1349 | else |
paul@222 | 1350 | spi_release_control(channel); |
paul@218 | 1351 | } |
paul@218 | 1352 | |
paul@235 | 1353 | static void spi_send_data(void) |
paul@218 | 1354 | { |
paul@218 | 1355 | void *channel = get_channel(num_spi_channels, spi_channels, NULL); |
paul@218 | 1356 | int bytes; |
paul@218 | 1357 | unsigned int byte; |
paul@218 | 1358 | uint8_t buffer[256]; |
paul@218 | 1359 | |
paul@218 | 1360 | bytes = 0; |
paul@218 | 1361 | |
paul@218 | 1362 | while (read_encoded_number(NULL, "%2x", &byte)) |
paul@218 | 1363 | { |
paul@218 | 1364 | buffer[bytes] = (uint8_t) (byte & 0xff); |
paul@218 | 1365 | bytes++; |
paul@218 | 1366 | } |
paul@218 | 1367 | |
paul@235 | 1368 | spi_send(channel, bytes, buffer); |
paul@221 | 1369 | } |
paul@221 | 1370 | |
paul@221 | 1371 | static void spi_send_data_units(void) |
paul@221 | 1372 | { |
paul@221 | 1373 | void *channel = get_channel(num_spi_channels, spi_channels, NULL); |
paul@221 | 1374 | unsigned int char_size, unit_size, value; |
paul@221 | 1375 | uint8_t buffer[256]; |
paul@221 | 1376 | int byte = 0; |
paul@221 | 1377 | |
paul@221 | 1378 | if (!read_number("Unit size", &unit_size)) |
paul@221 | 1379 | return; |
paul@221 | 1380 | |
paul@221 | 1381 | if (!read_number("Character size", &char_size)) |
paul@221 | 1382 | return; |
paul@221 | 1383 | |
paul@221 | 1384 | /* Read hex digits for bytes. Multiple bytes make up each unit and are read |
paul@221 | 1385 | from most to least significant. Where the unit size exceeds the character |
paul@221 | 1386 | size, the last bit before the character indicates the GPC bit. */ |
paul@221 | 1387 | |
paul@221 | 1388 | while ((byte < 256) && read_encoded_number(NULL, "%2x", &value)) |
paul@221 | 1389 | buffer[byte++] = value; |
paul@221 | 1390 | |
paul@236 | 1391 | /* Explicitly indicate big endian data. */ |
paul@236 | 1392 | |
paul@236 | 1393 | spi_send_units(channel, byte, (uint8_t *) buffer, unit_size, char_size, 1); |
paul@221 | 1394 | } |
paul@221 | 1395 | |
paul@221 | 1396 | static void spi_transfer_data(void) |
paul@221 | 1397 | { |
paul@221 | 1398 | void *channel = get_channel(num_spi_channels, spi_channels, NULL); |
paul@236 | 1399 | struct dma_region *dma_region = NULL, *desc_region; |
paul@236 | 1400 | struct memory_region *memory_region = NULL; |
paul@221 | 1401 | unsigned int char_size, unit_size; |
paul@221 | 1402 | uint32_t count, transferred; |
paul@236 | 1403 | int using_dma; |
paul@221 | 1404 | |
paul@221 | 1405 | if (channel == NULL) |
paul@221 | 1406 | return; |
paul@221 | 1407 | |
paul@236 | 1408 | if (!read_flag("Using DMA", &using_dma, "d", 1)) |
paul@236 | 1409 | return; |
paul@221 | 1410 | |
paul@236 | 1411 | if (using_dma) |
paul@236 | 1412 | dma_region = _get_dma_region(); |
paul@236 | 1413 | else |
paul@236 | 1414 | memory_region = _get_memory_region(); |
paul@236 | 1415 | |
paul@236 | 1416 | if ((dma_region == NULL) && (memory_region == NULL)) |
paul@221 | 1417 | return; |
paul@221 | 1418 | |
paul@221 | 1419 | if (!read_number("Transfer size", &count)) |
paul@221 | 1420 | return; |
paul@221 | 1421 | |
paul@221 | 1422 | if (!read_number("Unit size", &unit_size)) |
paul@221 | 1423 | return; |
paul@221 | 1424 | |
paul@221 | 1425 | if (!read_number("Character size", &char_size)) |
paul@221 | 1426 | return; |
paul@221 | 1427 | |
paul@236 | 1428 | if (using_dma) |
paul@236 | 1429 | { |
paul@236 | 1430 | desc_region = _get_dma_region(); |
paul@223 | 1431 | |
paul@236 | 1432 | transferred = spi_transfer(channel, dma_region->vaddr, dma_region->paddr, count, |
paul@236 | 1433 | unit_size, char_size, |
paul@236 | 1434 | desc_region != NULL ? desc_region->vaddr : 0, |
paul@236 | 1435 | desc_region != NULL ? desc_region->paddr : 0); |
paul@236 | 1436 | } |
paul@236 | 1437 | else |
paul@236 | 1438 | transferred = spi_transfer(channel, memory_region->addr, 0, count, |
paul@236 | 1439 | unit_size, char_size, 0, 0); |
paul@221 | 1440 | |
paul@221 | 1441 | printf("Transferred: %d\n", transferred); |
paul@218 | 1442 | } |
paul@218 | 1443 | |
paul@218 | 1444 | |
paul@218 | 1445 | |
paul@253 | 1446 | /* TCU configuration. */ |
paul@253 | 1447 | |
paul@253 | 1448 | static l4_uint32_t tcu_irq_start = 0, tcu_irq_end = 0; |
paul@253 | 1449 | |
paul@253 | 1450 | /* TCU operations. */ |
paul@253 | 1451 | |
paul@253 | 1452 | static int init_tcu(void) |
paul@253 | 1453 | { |
paul@253 | 1454 | /* Here, only one IRQ is used. */ |
paul@253 | 1455 | |
paul@253 | 1456 | tcu_irq = l4re_util_cap_alloc(); |
paul@253 | 1457 | return init_irq(0, tcu_irq, tcu_irq_start, tcu_irq_end); |
paul@253 | 1458 | } |
paul@253 | 1459 | |
paul@253 | 1460 | static void disable_tcu_counter(void) |
paul@253 | 1461 | { |
paul@254 | 1462 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1463 | |
paul@253 | 1464 | if (channel == NULL) |
paul@253 | 1465 | return; |
paul@253 | 1466 | |
paul@253 | 1467 | tcu_disable(channel); |
paul@253 | 1468 | } |
paul@253 | 1469 | |
paul@253 | 1470 | static void enable_tcu_counter(void) |
paul@253 | 1471 | { |
paul@254 | 1472 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1473 | |
paul@253 | 1474 | if (channel == NULL) |
paul@253 | 1475 | return; |
paul@253 | 1476 | |
paul@253 | 1477 | tcu_enable(channel); |
paul@253 | 1478 | } |
paul@253 | 1479 | |
paul@253 | 1480 | static void new_tcu_channel(void *tcu) |
paul@253 | 1481 | { |
paul@253 | 1482 | int num = get_channel_number(num_tcu_channels); |
paul@253 | 1483 | |
paul@253 | 1484 | if (num < 0) |
paul@253 | 1485 | return; |
paul@253 | 1486 | |
paul@253 | 1487 | tcu_channels[num] = tcu_get_channel(tcu, num, tcu_irq); |
paul@253 | 1488 | } |
paul@253 | 1489 | |
paul@253 | 1490 | static void list_tcu_channels(void) |
paul@253 | 1491 | { |
paul@253 | 1492 | unsigned int num; |
paul@253 | 1493 | void *channel; |
paul@253 | 1494 | |
paul@253 | 1495 | printf(" Clock.. Counter......... Mask..............\n"); |
paul@253 | 1496 | printf("Channel Status C Pre Cnt Half Full Half Full Int\n"); |
paul@258 | 1497 | printf("------- -------- -- ---- ---- ---- ---- -------- -------- ----\n"); |
paul@253 | 1498 | |
paul@253 | 1499 | for (num = 0; num < num_tcu_channels; num++) |
paul@253 | 1500 | { |
paul@253 | 1501 | printf("%d ", num); |
paul@253 | 1502 | |
paul@253 | 1503 | channel = tcu_channels[num]; |
paul@253 | 1504 | |
paul@253 | 1505 | if (channel == NULL) |
paul@253 | 1506 | printf("inactive\n"); |
paul@253 | 1507 | else |
paul@253 | 1508 | printf("%s %d %4d %04x %04x %04x %s %s %s\n", |
paul@253 | 1509 | tcu_is_enabled(channel) ? "enabled " : "disabled", |
paul@253 | 1510 | tcu_get_clock(channel), |
paul@253 | 1511 | tcu_get_prescale(channel), |
paul@253 | 1512 | tcu_get_counter(channel), |
paul@253 | 1513 | tcu_get_half_data_value(channel), |
paul@253 | 1514 | tcu_get_full_data_value(channel), |
paul@253 | 1515 | tcu_get_half_data_mask(channel) ? "masked " : "unmasked", |
paul@253 | 1516 | tcu_get_full_data_mask(channel) ? "masked " : "unmasked", |
paul@253 | 1517 | tcu_have_interrupt(channel) ? "!" : "_"); |
paul@253 | 1518 | } |
paul@253 | 1519 | } |
paul@253 | 1520 | |
paul@253 | 1521 | static void set_tcu_counter(void) |
paul@253 | 1522 | { |
paul@254 | 1523 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1524 | uint32_t counter; |
paul@253 | 1525 | |
paul@253 | 1526 | if (channel == NULL) |
paul@253 | 1527 | return; |
paul@253 | 1528 | |
paul@253 | 1529 | if (!read_number("Value", &counter)) |
paul@253 | 1530 | return; |
paul@253 | 1531 | |
paul@253 | 1532 | tcu_set_counter(channel, counter); |
paul@253 | 1533 | } |
paul@253 | 1534 | |
paul@253 | 1535 | static void set_tcu_clock(void) |
paul@253 | 1536 | { |
paul@254 | 1537 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1538 | uint32_t clock; |
paul@253 | 1539 | |
paul@253 | 1540 | if (channel == NULL) |
paul@253 | 1541 | return; |
paul@253 | 1542 | |
paul@253 | 1543 | if (!read_number("Clock", &clock)) |
paul@253 | 1544 | return; |
paul@253 | 1545 | |
paul@253 | 1546 | tcu_set_clock(channel, clock); |
paul@253 | 1547 | } |
paul@253 | 1548 | |
paul@253 | 1549 | static void set_tcu_full(void) |
paul@253 | 1550 | { |
paul@254 | 1551 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1552 | uint32_t value; |
paul@253 | 1553 | |
paul@253 | 1554 | if (channel == NULL) |
paul@253 | 1555 | return; |
paul@253 | 1556 | |
paul@253 | 1557 | if (!read_number("Full value", &value)) |
paul@253 | 1558 | return; |
paul@253 | 1559 | |
paul@253 | 1560 | tcu_set_full_data_value(channel, value); |
paul@253 | 1561 | } |
paul@253 | 1562 | |
paul@253 | 1563 | static void set_tcu_half(void) |
paul@253 | 1564 | { |
paul@254 | 1565 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1566 | uint32_t value; |
paul@253 | 1567 | |
paul@253 | 1568 | if (channel == NULL) |
paul@253 | 1569 | return; |
paul@253 | 1570 | |
paul@253 | 1571 | if (!read_number("Half value", &value)) |
paul@253 | 1572 | return; |
paul@253 | 1573 | |
paul@253 | 1574 | tcu_set_half_data_value(channel, value); |
paul@253 | 1575 | } |
paul@253 | 1576 | |
paul@253 | 1577 | static void set_tcu_mask(int full_mask, int masked) |
paul@253 | 1578 | { |
paul@254 | 1579 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1580 | |
paul@253 | 1581 | if (channel == NULL) |
paul@253 | 1582 | return; |
paul@253 | 1583 | |
paul@253 | 1584 | if (full_mask) |
paul@253 | 1585 | tcu_set_full_data_mask(channel, masked); |
paul@253 | 1586 | else |
paul@253 | 1587 | tcu_set_half_data_mask(channel, masked); |
paul@253 | 1588 | } |
paul@253 | 1589 | |
paul@253 | 1590 | static void set_tcu_prescale(void) |
paul@253 | 1591 | { |
paul@254 | 1592 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1593 | uint32_t prescale; |
paul@253 | 1594 | |
paul@253 | 1595 | if (channel == NULL) |
paul@253 | 1596 | return; |
paul@253 | 1597 | |
paul@253 | 1598 | if (!read_number("Prescale", &prescale)) |
paul@253 | 1599 | return; |
paul@253 | 1600 | |
paul@253 | 1601 | tcu_set_prescale(channel, prescale); |
paul@253 | 1602 | } |
paul@253 | 1603 | |
paul@253 | 1604 | static void tcu_wait(void) |
paul@253 | 1605 | { |
paul@254 | 1606 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1607 | uint32_t timeout; |
paul@253 | 1608 | |
paul@253 | 1609 | if (channel == NULL) |
paul@253 | 1610 | return; |
paul@253 | 1611 | |
paul@253 | 1612 | if (!read_number("Timeout", &timeout)) |
paul@253 | 1613 | return; |
paul@253 | 1614 | |
paul@253 | 1615 | printf("IRQ received: %s\n", tcu_wait_for_irq(channel, timeout) ? "yes" : "no"); |
paul@253 | 1616 | } |
paul@253 | 1617 | |
paul@253 | 1618 | |
paul@253 | 1619 | |
paul@218 | 1620 | /* Command processing. */ |
paul@218 | 1621 | |
paul@218 | 1622 | static void handle_aic(void *aic) |
paul@218 | 1623 | { |
paul@218 | 1624 | char *token; |
paul@218 | 1625 | |
paul@218 | 1626 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1627 | { |
paul@218 | 1628 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1629 | list_channels(num_aic_channels, aic_channels); |
paul@218 | 1630 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@218 | 1631 | new_aic_channel(aic); |
paul@218 | 1632 | else if (!strcmp(token, "t") || !strcmp(token, "transfer")) |
paul@218 | 1633 | aic_transfer_data(); |
paul@218 | 1634 | else |
paul@236 | 1635 | printf("aic channel | list | transfer\n"); |
paul@218 | 1636 | } |
paul@218 | 1637 | else |
paul@218 | 1638 | list_channels(num_aic_channels, aic_channels); |
paul@218 | 1639 | } |
paul@218 | 1640 | |
paul@218 | 1641 | static void handle_cpm(void *cpm) |
paul@218 | 1642 | { |
paul@218 | 1643 | char *token; |
paul@218 | 1644 | |
paul@218 | 1645 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1646 | { |
paul@218 | 1647 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1648 | list_clocks(cpm); |
paul@218 | 1649 | else if (!strcmp(token, "f") || !strcmp(token, "frequency")) |
paul@218 | 1650 | set_frequency(cpm); |
paul@218 | 1651 | else if (!strcmp(token, "p") || !strcmp(token, "parameters")) |
paul@218 | 1652 | set_parameters(cpm); |
paul@218 | 1653 | else if (!strcmp(token, "s") || !strcmp(token, "source")) |
paul@218 | 1654 | set_source(cpm); |
paul@218 | 1655 | else if (!strcmp(token, "start")) |
paul@218 | 1656 | start_clock(cpm); |
paul@218 | 1657 | else if (!strcmp(token, "stop")) |
paul@218 | 1658 | stop_clock(cpm); |
paul@218 | 1659 | else |
paul@218 | 1660 | printf("cpm list | frequency | parameters | source | start | stop\n"); |
paul@218 | 1661 | } |
paul@218 | 1662 | else |
paul@218 | 1663 | list_clocks(cpm); |
paul@218 | 1664 | } |
paul@218 | 1665 | |
paul@218 | 1666 | static void handle_dma(void *dma) |
paul@218 | 1667 | { |
paul@218 | 1668 | char *token; |
paul@218 | 1669 | |
paul@218 | 1670 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1671 | { |
paul@218 | 1672 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1673 | list_channels(num_dma_channels, dma_channels); |
paul@218 | 1674 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@218 | 1675 | new_dma_channel(dma); |
paul@218 | 1676 | else if (!strcmp(token, "r") || !strcmp(token, "region")) |
paul@218 | 1677 | new_dma_region(); |
paul@261 | 1678 | else if (!strcmp(token, "R") || !strcmp(token, "show")) |
paul@261 | 1679 | show_dma_region(); |
paul@218 | 1680 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@218 | 1681 | set_dma_region(); |
paul@218 | 1682 | else if (!strcmp(token, "t") || !strcmp(token, "transfer")) |
paul@218 | 1683 | dma_transfer_data(); |
paul@218 | 1684 | else |
paul@218 | 1685 | printf("dma channel | list | region | set | transfer\n"); |
paul@218 | 1686 | } |
paul@218 | 1687 | else |
paul@218 | 1688 | list_channels(num_dma_channels, dma_channels); |
paul@218 | 1689 | } |
paul@218 | 1690 | |
paul@218 | 1691 | static void handle_gpio(void *gpio[]) |
paul@218 | 1692 | { |
paul@218 | 1693 | char *token; |
paul@218 | 1694 | |
paul@218 | 1695 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1696 | { |
paul@218 | 1697 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1698 | list_gpios(gpio); |
paul@218 | 1699 | else if (!strcmp(token, "a") || !strcmp(token, "alt")) |
paul@218 | 1700 | set_gpio_alt_func(gpio); |
paul@218 | 1701 | else if (!strcmp(token, "i") || !strcmp(token, "io")) |
paul@218 | 1702 | set_gpio_pad(gpio); |
paul@218 | 1703 | else if (!strcmp(token, "p") || !strcmp(token, "pull")) |
paul@218 | 1704 | set_gpio_pull(gpio); |
paul@218 | 1705 | else |
paul@218 | 1706 | printf("gpio list | alt | io | pull\n"); |
paul@218 | 1707 | } |
paul@218 | 1708 | else |
paul@218 | 1709 | list_gpios(gpio); |
paul@218 | 1710 | } |
paul@218 | 1711 | |
paul@218 | 1712 | static void handle_i2c(void *i2c) |
paul@218 | 1713 | { |
paul@218 | 1714 | char *token; |
paul@218 | 1715 | |
paul@218 | 1716 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1717 | { |
paul@218 | 1718 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1719 | list_i2c_channels(); |
paul@218 | 1720 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@218 | 1721 | new_i2c_channel(i2c); |
paul@218 | 1722 | else if (!strcmp(token, "g") || !strcmp(token, "get")) |
paul@218 | 1723 | i2c_get(); |
paul@218 | 1724 | else if (!strcmp(token, "s") || !strcmp(token, "scan")) |
paul@218 | 1725 | i2c_scan(); |
paul@218 | 1726 | else |
paul@218 | 1727 | printf("i2c channel | get | list | scan\n"); |
paul@218 | 1728 | } |
paul@218 | 1729 | else |
paul@218 | 1730 | list_i2c_channels(); |
paul@218 | 1731 | } |
paul@218 | 1732 | |
paul@236 | 1733 | static void handle_memory(void) |
paul@236 | 1734 | { |
paul@236 | 1735 | char *token; |
paul@236 | 1736 | |
paul@236 | 1737 | if ((token = read_token(NULL)) != NULL) |
paul@236 | 1738 | { |
paul@236 | 1739 | if (!strcmp(token, "r") || !strcmp(token, "region")) |
paul@236 | 1740 | new_memory_region(); |
paul@236 | 1741 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@236 | 1742 | set_memory_region(); |
paul@236 | 1743 | else |
paul@236 | 1744 | printf("memory region | set\n"); |
paul@236 | 1745 | } |
paul@236 | 1746 | else |
paul@236 | 1747 | list_memory_regions(); |
paul@236 | 1748 | } |
paul@236 | 1749 | |
paul@258 | 1750 | static void handle_msc(void *msc) |
paul@258 | 1751 | { |
paul@258 | 1752 | char *token; |
paul@258 | 1753 | |
paul@258 | 1754 | if ((token = read_token(NULL)) != NULL) |
paul@258 | 1755 | { |
paul@258 | 1756 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@258 | 1757 | list_msc_channels(); |
paul@258 | 1758 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@258 | 1759 | new_msc_channel(msc); |
paul@258 | 1760 | else if (!strcmp(token, "e") || !strcmp(token, "enable")) |
paul@258 | 1761 | enable_msc_channel(); |
paul@258 | 1762 | else if (!strcmp(token, "r") || !strcmp(token, "read")) |
paul@261 | 1763 | read_blocks_from_msc(); |
paul@258 | 1764 | else |
paul@258 | 1765 | printf("msc channel | enable | read\n"); |
paul@258 | 1766 | } |
paul@258 | 1767 | else |
paul@258 | 1768 | list_msc_channels(); |
paul@258 | 1769 | } |
paul@258 | 1770 | |
paul@238 | 1771 | static void handle_rtc(void *rtc, void *cpm) |
paul@237 | 1772 | { |
paul@237 | 1773 | char *token; |
paul@237 | 1774 | |
paul@237 | 1775 | if ((token = read_token(NULL)) != NULL) |
paul@237 | 1776 | { |
paul@237 | 1777 | if (!strcmp(token, "d") || !strcmp(token, "disable")) |
paul@237 | 1778 | rtc_disable(rtc); |
paul@237 | 1779 | else if (!strcmp(token, "e") || !strcmp(token, "enable")) |
paul@237 | 1780 | rtc_enable(rtc); |
paul@237 | 1781 | else if (!strcmp(token, "g") || !strcmp(token, "get")) |
paul@237 | 1782 | printf("seconds = %d\n", rtc_get_seconds(rtc)); |
paul@237 | 1783 | else if (!strcmp(token, "ga") || !strcmp(token, "get-alarm")) |
paul@237 | 1784 | printf("seconds = %d\n", rtc_get_alarm_seconds(rtc)); |
paul@237 | 1785 | else if (!strcmp(token, "p") || !strcmp(token, "power-down")) |
paul@237 | 1786 | rtc_power_down(rtc); |
paul@238 | 1787 | else if (!strcmp(token, "r") || !strcmp(token, "reset")) |
paul@238 | 1788 | rtc_reset(rtc, cpm); |
paul@237 | 1789 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@237 | 1790 | _rtc_set_seconds(rtc, 0); |
paul@237 | 1791 | else if (!strcmp(token, "sa") || !strcmp(token, "set-alarm")) |
paul@237 | 1792 | _rtc_set_seconds(rtc, 1); |
paul@237 | 1793 | else |
paul@238 | 1794 | printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); |
paul@237 | 1795 | } |
paul@237 | 1796 | else |
paul@238 | 1797 | printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); |
paul@237 | 1798 | } |
paul@237 | 1799 | |
paul@221 | 1800 | static void handle_spi(void *spi, void *gpio[]) |
paul@218 | 1801 | { |
paul@218 | 1802 | char *token; |
paul@218 | 1803 | |
paul@218 | 1804 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1805 | { |
paul@218 | 1806 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1807 | list_channels(num_spi_channels, spi_channels); |
paul@222 | 1808 | else if (!strcmp(token, "a") || !strcmp(token, "control-acquire")) |
paul@222 | 1809 | spi_control(1); |
paul@222 | 1810 | else if (!strcmp(token, "r") || !strcmp(token, "control-release")) |
paul@222 | 1811 | spi_control(0); |
paul@218 | 1812 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@222 | 1813 | new_spi_channel(spi, gpio); |
paul@221 | 1814 | else if (!strcmp(token, "g") || !strcmp(token, "gpio") || !strcmp(token, "gpio-channel")) |
paul@221 | 1815 | new_spi_channel_gpio(gpio); |
paul@218 | 1816 | else if (!strcmp(token, "s") || !strcmp(token, "send")) |
paul@235 | 1817 | spi_send_data(); |
paul@221 | 1818 | else if (!strcmp(token, "S") || !strcmp(token, "send-units")) |
paul@221 | 1819 | spi_send_data_units(); |
paul@221 | 1820 | else if (!strcmp(token, "t") || !strcmp(token, "transfer")) |
paul@221 | 1821 | spi_transfer_data(); |
paul@218 | 1822 | else |
paul@222 | 1823 | printf("spi channel | control-acquire | control-release | gpio-channel | list | send | send-units | transfer\n"); |
paul@218 | 1824 | } |
paul@218 | 1825 | else |
paul@218 | 1826 | list_channels(num_spi_channels, spi_channels); |
paul@218 | 1827 | } |
paul@218 | 1828 | |
paul@253 | 1829 | static void handle_tcu(void *tcu) |
paul@253 | 1830 | { |
paul@253 | 1831 | char *token; |
paul@253 | 1832 | |
paul@253 | 1833 | if ((token = read_token(NULL)) != NULL) |
paul@253 | 1834 | { |
paul@253 | 1835 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@253 | 1836 | list_tcu_channels(); |
paul@253 | 1837 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@253 | 1838 | new_tcu_channel(tcu); |
paul@253 | 1839 | else if (!strcmp(token, "C") || !strcmp(token, "clock")) |
paul@253 | 1840 | set_tcu_clock(); |
paul@253 | 1841 | else if (!strcmp(token, "d") || !strcmp(token, "disable")) |
paul@253 | 1842 | disable_tcu_counter(); |
paul@253 | 1843 | else if (!strcmp(token, "e") || !strcmp(token, "enable")) |
paul@253 | 1844 | enable_tcu_counter(); |
paul@253 | 1845 | else if (!strcmp(token, "f") || !strcmp(token, "full")) |
paul@253 | 1846 | set_tcu_full(); |
paul@253 | 1847 | else if (!strcmp(token, "fm") || !strcmp(token, "full-mask")) |
paul@253 | 1848 | set_tcu_mask(1, 1); |
paul@253 | 1849 | else if (!strcmp(token, "fu") || !strcmp(token, "full-unmask")) |
paul@253 | 1850 | set_tcu_mask(1, 0); |
paul@253 | 1851 | else if (!strcmp(token, "h") || !strcmp(token, "half")) |
paul@253 | 1852 | set_tcu_half(); |
paul@253 | 1853 | else if (!strcmp(token, "hm") || !strcmp(token, "half-mask")) |
paul@253 | 1854 | set_tcu_mask(0, 1); |
paul@253 | 1855 | else if (!strcmp(token, "hu") || !strcmp(token, "half-unmask")) |
paul@253 | 1856 | set_tcu_mask(0, 0); |
paul@253 | 1857 | else if (!strcmp(token, "p") || !strcmp(token, "prescale")) |
paul@253 | 1858 | set_tcu_prescale(); |
paul@253 | 1859 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@253 | 1860 | set_tcu_counter(); |
paul@253 | 1861 | else if (!strcmp(token, "w") || !strcmp(token, "wait")) |
paul@253 | 1862 | tcu_wait(); |
paul@253 | 1863 | else |
paul@253 | 1864 | printf("tcu channel | clock | disable | enable | full | full-mask | " \ |
paul@253 | 1865 | "full-unmask | half | half-mask | half-unmask | list | mask | " \ |
paul@253 | 1866 | "prescale | set | unmask | wait\n"); |
paul@253 | 1867 | } |
paul@253 | 1868 | else |
paul@253 | 1869 | list_tcu_channels(); |
paul@253 | 1870 | } |
paul@253 | 1871 | |
paul@218 | 1872 | |
paul@218 | 1873 | |
paul@218 | 1874 | int main(void) |
paul@218 | 1875 | { |
paul@218 | 1876 | l4_addr_t aic_base = 0, aic_base_end = 0; |
paul@218 | 1877 | l4_addr_t aic_phys_base = 0, aic_phys_base_end = 0; |
paul@218 | 1878 | l4_addr_t cpm_base = 0, cpm_base_end = 0; |
paul@218 | 1879 | l4_addr_t dma_base = 0, dma_base_end = 0; |
paul@218 | 1880 | l4_addr_t gpio_base = 0, gpio_base_end = 0; |
paul@218 | 1881 | l4_addr_t i2c_base = 0, i2c_base_end = 0; |
paul@258 | 1882 | l4_addr_t msc_base = 0, msc_base_end = 0; |
paul@258 | 1883 | l4_addr_t msc_phys_base = 0, msc_phys_base_end = 0; |
paul@237 | 1884 | l4_addr_t rtc_base = 0, rtc_base_end = 0; |
paul@221 | 1885 | l4_addr_t ssi_base = 0, ssi_base_end = 0; |
paul@221 | 1886 | l4_addr_t ssi_phys_base = 0, ssi_phys_base_end = 0; |
paul@253 | 1887 | l4_addr_t tcu_base = 0, tcu_base_end = 0; |
paul@258 | 1888 | void *aic, *cpm, *dma, *gpio[num_gpio_ports], *i2c, *msc, *rtc, *spi, *tcu; |
paul@218 | 1889 | int result = 0; |
paul@218 | 1890 | unsigned int port; |
paul@218 | 1891 | |
paul@218 | 1892 | icucap = l4re_env_get_cap("icu"); |
paul@218 | 1893 | |
paul@218 | 1894 | /* Obtain resource details describing I/O memory. */ |
paul@218 | 1895 | |
paul@218 | 1896 | printf("Access CPM...\n"); |
paul@218 | 1897 | |
paul@236 | 1898 | if ((result = get_memory(io_memory_regions[CPM], &cpm_base, &cpm_base_end)) < 0) |
paul@218 | 1899 | return 1; |
paul@218 | 1900 | |
paul@218 | 1901 | printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); |
paul@218 | 1902 | |
paul@218 | 1903 | cpm = cpm_init(cpm_base); |
paul@218 | 1904 | |
paul@218 | 1905 | printf("Access DMA...\n"); |
paul@218 | 1906 | |
paul@236 | 1907 | if ((result = get_memory(io_memory_regions[DMA], &dma_base, &dma_base_end)) < 0) |
paul@218 | 1908 | return 1; |
paul@218 | 1909 | |
paul@218 | 1910 | printf("DMA at 0x%lx...0x%lx.\n", dma_base, dma_base_end); |
paul@218 | 1911 | |
paul@218 | 1912 | dma = dma_init(dma_base, dma_base_end, cpm); |
paul@218 | 1913 | |
paul@236 | 1914 | if (get_irq(io_memory_regions[DMA], &dma_irq_start, &dma_irq_end) < 0) |
paul@218 | 1915 | return 1; |
paul@218 | 1916 | |
paul@218 | 1917 | printf("IRQ range at %d...%d.\n", dma_irq_start, dma_irq_end); |
paul@218 | 1918 | |
paul@218 | 1919 | if (init_dma()) |
paul@218 | 1920 | return 1; |
paul@218 | 1921 | |
paul@218 | 1922 | dma_enable(dma); |
paul@218 | 1923 | |
paul@218 | 1924 | printf("Access GPIO...\n"); |
paul@218 | 1925 | |
paul@236 | 1926 | if ((result = get_memory(io_memory_regions[GPIO], &gpio_base, &gpio_base_end)) < 0) |
paul@218 | 1927 | return 1; |
paul@218 | 1928 | |
paul@218 | 1929 | printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); |
paul@218 | 1930 | |
paul@218 | 1931 | for (port = 0; port < num_gpio_ports; port++) |
paul@218 | 1932 | gpio[port] = gpio_init(gpio_base + port * 0x100, gpio_base + (port + 1) * 0x100, |
paul@218 | 1933 | 32, gpio_ports[port].pull_ups, gpio_ports[port].pull_downs); |
paul@218 | 1934 | |
paul@218 | 1935 | printf("Access I2C...\n"); |
paul@218 | 1936 | |
paul@236 | 1937 | if ((result = get_memory(io_memory_regions[I2C], &i2c_base, &i2c_base_end)) < 0) |
paul@218 | 1938 | return 1; |
paul@218 | 1939 | |
paul@218 | 1940 | printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); |
paul@218 | 1941 | |
paul@218 | 1942 | i2c = i2c_init(i2c_base, i2c_base_end, cpm, 100000); |
paul@218 | 1943 | |
paul@236 | 1944 | if (get_irq(io_memory_regions[I2C], &i2c_irq_start, &i2c_irq_end) < 0) |
paul@218 | 1945 | return 1; |
paul@218 | 1946 | |
paul@218 | 1947 | printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); |
paul@218 | 1948 | |
paul@218 | 1949 | printf("Access AIC...\n"); |
paul@218 | 1950 | |
paul@236 | 1951 | if ((result = get_memory_complete(io_memory_regions[AIC], &aic_base, &aic_base_end, |
paul@218 | 1952 | &aic_phys_base, &aic_phys_base_end)) < 0) |
paul@218 | 1953 | return 1; |
paul@218 | 1954 | |
paul@218 | 1955 | printf("AIC at 0x%lx...0x%lx.\n", aic_base, aic_base_end); |
paul@218 | 1956 | |
paul@218 | 1957 | aic = aic_init(aic_phys_base, aic_base, aic_base_end, cpm); |
paul@218 | 1958 | |
paul@258 | 1959 | printf("Access MSC...\n"); |
paul@258 | 1960 | |
paul@258 | 1961 | if ((result = get_memory_complete(io_memory_regions[MSC], &msc_base, &msc_base_end, |
paul@258 | 1962 | &msc_phys_base, &msc_phys_base_end)) < 0) |
paul@258 | 1963 | return 1; |
paul@258 | 1964 | |
paul@258 | 1965 | printf("MSC at 0x%lx...0x%lx.\n", msc_base, msc_base_end); |
paul@258 | 1966 | |
paul@258 | 1967 | msc = msc_init(msc_phys_base, msc_base, msc_base_end); |
paul@258 | 1968 | |
paul@258 | 1969 | if (get_irq(io_memory_regions[MSC], &msc_irq_start, &msc_irq_end) < 0) |
paul@258 | 1970 | return 1; |
paul@258 | 1971 | |
paul@258 | 1972 | printf("IRQ range at %d...%d.\n", msc_irq_start, msc_irq_end); |
paul@258 | 1973 | |
paul@237 | 1974 | printf("Access RTC...\n"); |
paul@237 | 1975 | |
paul@237 | 1976 | if ((result = get_memory(io_memory_regions[RTC], &rtc_base, &rtc_base_end)) < 0) |
paul@237 | 1977 | return 1; |
paul@237 | 1978 | |
paul@237 | 1979 | printf("RTC at 0x%lx...0x%lx.\n", rtc_base, rtc_base_end); |
paul@237 | 1980 | |
paul@240 | 1981 | rtc = rtc_init(rtc_base, cpm); |
paul@237 | 1982 | |
paul@221 | 1983 | printf("Access SSI...\n"); |
paul@221 | 1984 | |
paul@236 | 1985 | if ((result = get_memory_complete(io_memory_regions[SSI], &ssi_base, &ssi_base_end, |
paul@221 | 1986 | &ssi_phys_base, &ssi_phys_base_end)) < 0) |
paul@218 | 1987 | return 1; |
paul@218 | 1988 | |
paul@221 | 1989 | printf("SSI at 0x%lx...0x%lx.\n", ssi_base, ssi_base_end); |
paul@221 | 1990 | |
paul@221 | 1991 | spi = spi_init(ssi_phys_base, ssi_base, ssi_base_end, cpm); |
paul@218 | 1992 | |
paul@253 | 1993 | printf("Access TCU...\n"); |
paul@253 | 1994 | |
paul@253 | 1995 | if ((result = get_memory(io_memory_regions[TCU], &tcu_base, &tcu_base_end)) < 0) |
paul@253 | 1996 | return 1; |
paul@253 | 1997 | |
paul@253 | 1998 | printf("TCU at 0x%lx...0x%lx.\n", tcu_base, tcu_base_end); |
paul@253 | 1999 | |
paul@253 | 2000 | tcu = tcu_init(tcu_base, tcu_base_end); |
paul@253 | 2001 | |
paul@253 | 2002 | if (get_irq(io_memory_regions[TCU], &tcu_irq_start, &tcu_irq_end) < 0) |
paul@253 | 2003 | return 1; |
paul@253 | 2004 | |
paul@253 | 2005 | printf("IRQ range at %d...%d.\n", tcu_irq_start, tcu_irq_end); |
paul@253 | 2006 | |
paul@253 | 2007 | if (init_tcu()) |
paul@253 | 2008 | return 1; |
paul@253 | 2009 | |
paul@218 | 2010 | /* Start the interactive session. */ |
paul@218 | 2011 | |
paul@258 | 2012 | printf("aic, cpm, dma, gpio, i2c, msc, rtc, spi, tcu\n"); |
paul@218 | 2013 | |
paul@218 | 2014 | while (1) |
paul@218 | 2015 | { |
paul@218 | 2016 | char cmdline[256], *token; |
paul@218 | 2017 | |
paul@218 | 2018 | printf("> "); |
paul@218 | 2019 | |
paul@221 | 2020 | token = fgets(cmdline, 256, stdin); |
paul@218 | 2021 | |
paul@218 | 2022 | if (token == NULL) |
paul@218 | 2023 | break; |
paul@218 | 2024 | |
paul@218 | 2025 | if ((token = strtok(cmdline, delimiters)) == NULL) |
paul@218 | 2026 | continue; |
paul@218 | 2027 | |
paul@218 | 2028 | /* AIC/I2S commands. */ |
paul@218 | 2029 | |
paul@218 | 2030 | if (!strcmp(token, "a") || !strcmp(token, "aic") || !strcmp(token, "i2s")) |
paul@218 | 2031 | handle_aic(aic); |
paul@218 | 2032 | |
paul@218 | 2033 | /* CPM commands. */ |
paul@218 | 2034 | |
paul@218 | 2035 | else if (!strcmp(token, "c") || !strcmp(token, "cpm")) |
paul@218 | 2036 | handle_cpm(cpm); |
paul@218 | 2037 | |
paul@218 | 2038 | /* DMA commands. */ |
paul@218 | 2039 | |
paul@218 | 2040 | else if (!strcmp(token, "d") || !strcmp(token, "dma")) |
paul@218 | 2041 | handle_dma(dma); |
paul@218 | 2042 | |
paul@218 | 2043 | /* GPIO commands. */ |
paul@218 | 2044 | |
paul@218 | 2045 | else if (!strcmp(token, "g") || !strcmp(token, "gpio")) |
paul@218 | 2046 | handle_gpio(gpio); |
paul@218 | 2047 | |
paul@218 | 2048 | /* I2C commands. */ |
paul@218 | 2049 | |
paul@218 | 2050 | else if (!strcmp(token, "i") || !strcmp(token, "i2c")) |
paul@218 | 2051 | handle_i2c(i2c); |
paul@218 | 2052 | |
paul@258 | 2053 | /* MSC commands. */ |
paul@258 | 2054 | |
paul@258 | 2055 | else if (!strcmp(token, "M") || !strcmp(token, "msc")) |
paul@258 | 2056 | handle_msc(msc); |
paul@258 | 2057 | |
paul@236 | 2058 | /* Generic memory commands. */ |
paul@236 | 2059 | |
paul@236 | 2060 | else if (!strcmp(token, "m") || !strcmp(token, "mem") || !strcmp(token, "memory")) |
paul@236 | 2061 | handle_memory(); |
paul@236 | 2062 | |
paul@237 | 2063 | /* RTC commands. */ |
paul@237 | 2064 | |
paul@237 | 2065 | else if (!strcmp(token, "r") || !strcmp(token, "rtc")) |
paul@238 | 2066 | handle_rtc(rtc, cpm); |
paul@237 | 2067 | |
paul@218 | 2068 | /* SPI commands. */ |
paul@218 | 2069 | |
paul@218 | 2070 | else if (!strcmp(token, "s") || !strcmp(token, "spi")) |
paul@221 | 2071 | handle_spi(spi, gpio); |
paul@218 | 2072 | |
paul@253 | 2073 | /* TCU commands. */ |
paul@253 | 2074 | |
paul@253 | 2075 | else if (!strcmp(token, "t") || !strcmp(token, "tcu")) |
paul@253 | 2076 | handle_tcu(tcu); |
paul@253 | 2077 | |
paul@221 | 2078 | /* Comments and blank lines. */ |
paul@221 | 2079 | |
paul@221 | 2080 | else if (strncmp(token, "#", 1) && strlen(token)) |
paul@218 | 2081 | printf("Command?\n"); |
paul@218 | 2082 | } |
paul@218 | 2083 | |
paul@218 | 2084 | printf("End of session.\n"); |
paul@218 | 2085 | return 0; |
paul@218 | 2086 | } |