Landfall

pkg/landfall-examples/ci20_cpm/ci20_cpm.c

139:74eafd298a88
2022-01-14 Paul Boddie Removed superfluous flag, fixed comment.
     1 /*     2  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>     3  *     economic rights: Technische Universit??t Dresden (Germany)     4  * Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk>     5  *     6  * This file is part of TUD:OS and distributed under the terms of the     7  * GNU General Public License 2.     8  * Please see the COPYING-GPL-2 file for details.     9  */    10 /*    11  * Access the clock and power management unit on the MIPS Creator CI20 board.    12  */    13     14 #include <l4/devices/cpm-jz4780.h>    15 #include <l4/io/io.h>    16 #include <l4/re/env.h>    17 #include <l4/re/c/util/cap_alloc.h>    18 #include <l4/sys/factory.h>    19 #include <l4/sys/icu.h>    20 #include <l4/sys/ipc.h>    21 #include <l4/sys/irq.h>    22 #include <l4/vbus/vbus.h>    23 #include <stdio.h>    24 #include <unistd.h>    25 #include <stdint.h>    26     27     28     29 /* Device and resource discovery. */    30     31 static char const *resource_type(enum l4io_resource_types_t type)    32 {    33   switch (type)    34   {    35     case L4VBUS_RESOURCE_INVALID:    36     return "INVALID";    37     38     case L4VBUS_RESOURCE_IRQ:    39     return "IRQ";    40     41     case L4VBUS_RESOURCE_MEM:    42     return "MEMORY";    43     44     default:    45     return "OTHER";    46   }    47 }    48     49 static int cpm_get_device(char const *hid, l4io_device_handle_t *dh, l4io_resource_handle_t *rh)    50 {    51   int result = l4io_lookup_device(hid, dh, 0, rh);    52     53   if (result < 0)    54     printf("Could not access '%s': %s\n", hid, result == -L4_ENOENT ? "no such device" : "no device");    55     56   return result;    57 }    58     59 static int cpm_get_resource(l4io_device_handle_t dh, l4io_resource_t *res,    60                             enum l4io_resource_types_t type)    61 {    62   int current = 0, result = 0;    63   l4_cap_idx_t vbus = l4re_env_get_cap("vbus");    64     65   do    66   {    67     result = l4vbus_get_resource(vbus, dh, current, res);    68     69     if (result)    70       printf("Could not access resource of type %s.\n", resource_type(type));    71     else    72       printf("Resource %d: type %s, start=%lx, end=%lx\n", res->id,    73         resource_type(res->type), res->start, res->end);    74     75     current++;    76   }    77   while ((!result) && (res->type != type));    78     79   return result;    80 }    81     82 static int cpm_get_memory(char const *hid, l4_addr_t *start, l4_addr_t *end)    83 {    84   l4io_device_handle_t dh;    85   l4io_resource_handle_t rh;    86   l4io_resource_t res;    87   int result;    88     89   result = cpm_get_device(hid, &dh, &rh);    90     91   if (result < 0)    92     return result;    93     94   result = cpm_get_resource(dh, &res, L4IO_RESOURCE_MEM);    95     96   if (result)    97     return result;    98     99   if ((result = l4io_request_iomem(res.start, res.end - res.start + 1,   100                                   L4IO_MEM_NONCACHED, start)))   101   {   102     printf("Could not get address for '%s'.\n", hid);   103     return result;   104   }   105    106   printf("Resource at 0x%lx...0x%lx.\n", res.start, res.end);   107    108   *end = *start + (res.end - res.start + 1);   109    110   return 0;   111 }   112    113 int main(void)   114 {   115   l4_addr_t cpm_base = 0, cpm_base_end = 0;   116   void *cpm;   117   int result = 0;   118    119   /* Obtain resource details describing I/O memory. */   120    121   printf("Access CPM...\n");   122    123   if ((result = cpm_get_memory("jz4780-cpm", &cpm_base, &cpm_base_end)) < 0)   124     return 1;   125    126   printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end);   127    128   cpm = jz4780_cpm_init(cpm_base);   129    130   /* Read information from the clock and power management unit. */   131    132   printf("Main source: %d\n", jz4780_cpm_get_main_source(cpm));   133   printf("  Main frequency: %d\n", jz4780_cpm_get_main_frequency(cpm));   134   printf("APLL frequency: %d\n", jz4780_cpm_get_apll_frequency(cpm));   135   printf("EPLL frequency: %d\n", jz4780_cpm_get_epll_frequency(cpm));   136   printf("MPLL frequency: %d\n", jz4780_cpm_get_mpll_frequency(cpm));   137   printf("VPLL frequency: %d\n", jz4780_cpm_get_vpll_frequency(cpm));   138   printf("CPU divider: %d\n", jz4780_cpm_get_cpu_divider(cpm));   139   printf("  CPU frequency: %d\n", jz4780_cpm_get_cpu_frequency(cpm));   140   printf("Memory source: %d\n", jz4780_cpm_get_memory_source(cpm));   141   printf("  Memory source frequency: %d\n", jz4780_cpm_get_memory_source_frequency(cpm));   142   printf("  Memory divider: %d\n", jz4780_cpm_get_memory_divider(cpm));   143   printf("  Memory frequency: %d\n", jz4780_cpm_get_memory_frequency(cpm));   144   printf("APB source: %d\n", jz4780_cpm_get_pclock_source(cpm));   145   printf("  APB source frequency: %d\n", jz4780_cpm_get_pclock_source_frequency(cpm));   146   printf("  APB divider: %d\n", jz4780_cpm_get_pclock_divider(cpm));   147   printf("  Slow peripheral (APB) frequency: %d\n", jz4780_cpm_get_pclock_frequency(cpm));   148   printf("AHB0 source: %d\n", jz4780_cpm_get_hclock0_source(cpm));   149   printf("  AHB0 source frequency: %d\n", jz4780_cpm_get_hclock0_source_frequency(cpm));   150   printf("  AHB0 divider: %d\n", jz4780_cpm_get_hclock0_divider(cpm));   151   printf("  Fast peripheral (AHB0) frequency: %d\n", jz4780_cpm_get_hclock0_frequency(cpm));   152   printf("AHB2 source: %d\n", jz4780_cpm_get_hclock2_source(cpm));   153   printf("  AHB2 source frequency: %d\n", jz4780_cpm_get_hclock2_source_frequency(cpm));   154   printf("  AHB2 divider: %d\n", jz4780_cpm_get_hclock2_divider(cpm));   155   printf("  Fast peripheral (AHB2) frequency: %d\n", jz4780_cpm_get_hclock2_frequency(cpm));   156   printf("LCD source: %d\n", jz4780_cpm_get_lcd_source(cpm));   157   printf("  LCD source frequency: %d\n", jz4780_cpm_get_lcd_source_frequency(cpm));   158   printf("  LCD pixel clock divider: %d\n", jz4780_cpm_get_lcd_pixel_divider(cpm));   159   printf("  LCD pixel clock frequency: %d\n", jz4780_cpm_get_lcd_pixel_frequency(cpm));   160    161   /* Attempt to set the pixel clock frequency. */   162    163   jz4780_cpm_set_lcd_pixel_frequency(cpm, 108000000);   164    165   printf("LCD source: %d\n", jz4780_cpm_get_lcd_source(cpm));   166   printf("  LCD source frequency: %d\n", jz4780_cpm_get_lcd_source_frequency(cpm));   167   printf("  LCD pixel clock divider: %d\n", jz4780_cpm_get_lcd_pixel_divider(cpm));   168   printf("  LCD pixel clock frequency: %d\n", jz4780_cpm_get_lcd_pixel_frequency(cpm));   169    170   /* Attempt to set the peripheral clock frequency. */   171    172   jz4780_cpm_set_pclock_source(cpm, 1);   173    174   printf("APB source: %d\n", jz4780_cpm_get_pclock_source(cpm));   175   printf("  APB source frequency: %d\n", jz4780_cpm_get_pclock_source_frequency(cpm));   176   printf("  APB divider: %d\n", jz4780_cpm_get_pclock_divider(cpm));   177   printf("  Slow peripheral (APB) frequency: %d\n", jz4780_cpm_get_pclock_frequency(cpm));   178   printf("AHB2 source: %d\n", jz4780_cpm_get_hclock2_source(cpm));   179   printf("  AHB2 source frequency: %d\n", jz4780_cpm_get_hclock2_source_frequency(cpm));   180   printf("  AHB2 divider: %d\n", jz4780_cpm_get_hclock2_divider(cpm));   181   printf("  Fast peripheral (AHB2) frequency: %d\n", jz4780_cpm_get_hclock2_frequency(cpm));   182    183   /* Attempt to set the MPLL output to EXCLK divided by 4. */   184    185   jz4780_cpm_set_mpll_parameters(cpm, 1, 4, 1);   186    187   printf("APB source: %d\n", jz4780_cpm_get_pclock_source(cpm));   188   printf("  APB source frequency: %d\n", jz4780_cpm_get_pclock_source_frequency(cpm));   189   printf("  APB divider: %d\n", jz4780_cpm_get_pclock_divider(cpm));   190   printf("  Slow peripheral (APB) frequency: %d\n", jz4780_cpm_get_pclock_frequency(cpm));   191   printf("AHB2 source: %d\n", jz4780_cpm_get_hclock2_source(cpm));   192   printf("  AHB2 source frequency: %d\n", jz4780_cpm_get_hclock2_source_frequency(cpm));   193   printf("  AHB2 divider: %d\n", jz4780_cpm_get_hclock2_divider(cpm));   194   printf("  Fast peripheral (AHB2) frequency: %d\n", jz4780_cpm_get_hclock2_frequency(cpm));   195    196   return 0;   197 }