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