Home | History | Annotate | Line # | Download | only in bfin
dv-bfin_ppi.c revision 1.6
      1  1.1  christos /* Blackfin Parallel Port Interface (PPI) model
      2  1.1  christos    For "old style" PPIs on BF53x/etc... parts.
      3  1.1  christos 
      4  1.6  christos    Copyright (C) 2010-2016 Free Software Foundation, Inc.
      5  1.1  christos    Contributed by Analog Devices, Inc.
      6  1.1  christos 
      7  1.1  christos    This file is part of simulators.
      8  1.1  christos 
      9  1.1  christos    This program is free software; you can redistribute it and/or modify
     10  1.1  christos    it under the terms of the GNU General Public License as published by
     11  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     12  1.1  christos    (at your option) any later version.
     13  1.1  christos 
     14  1.1  christos    This program is distributed in the hope that it will be useful,
     15  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  1.1  christos    GNU General Public License for more details.
     18  1.1  christos 
     19  1.1  christos    You should have received a copy of the GNU General Public License
     20  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     21  1.1  christos 
     22  1.1  christos #include "config.h"
     23  1.1  christos 
     24  1.1  christos #include "sim-main.h"
     25  1.1  christos #include "devices.h"
     26  1.1  christos #include "dv-bfin_ppi.h"
     27  1.1  christos #include "gui.h"
     28  1.1  christos 
     29  1.1  christos /* XXX: TX is merely a stub.  */
     30  1.1  christos 
     31  1.1  christos struct bfin_ppi
     32  1.1  christos {
     33  1.1  christos   /* This top portion matches common dv_bfin struct.  */
     34  1.1  christos   bu32 base;
     35  1.1  christos   struct hw *dma_master;
     36  1.1  christos   bool acked;
     37  1.1  christos 
     38  1.1  christos   struct hw_event *handler;
     39  1.1  christos   char saved_byte;
     40  1.1  christos   int saved_count;
     41  1.1  christos 
     42  1.1  christos   /* GUI state.  */
     43  1.1  christos   void *gui_state;
     44  1.1  christos   int color;
     45  1.1  christos 
     46  1.1  christos   /* Order after here is important -- matches hardware MMR layout.  */
     47  1.1  christos   bu16 BFIN_MMR_16(control);
     48  1.1  christos   bu16 BFIN_MMR_16(status);
     49  1.1  christos   bu16 BFIN_MMR_16(count);
     50  1.1  christos   bu16 BFIN_MMR_16(delay);
     51  1.1  christos   bu16 BFIN_MMR_16(frame);
     52  1.1  christos };
     53  1.1  christos #define mmr_base()      offsetof(struct bfin_ppi, control)
     54  1.1  christos #define mmr_offset(mmr) (offsetof(struct bfin_ppi, mmr) - mmr_base())
     55  1.1  christos 
     56  1.1  christos static const char * const mmr_names[] =
     57  1.1  christos {
     58  1.1  christos   "PPI_CONTROL", "PPI_STATUS", "PPI_COUNT", "PPI_DELAY", "PPI_FRAME",
     59  1.1  christos };
     60  1.1  christos #define mmr_name(off) mmr_names[(off) / 4]
     61  1.1  christos 
     62  1.1  christos static void
     63  1.1  christos bfin_ppi_gui_setup (struct bfin_ppi *ppi)
     64  1.1  christos {
     65  1.1  christos   int bpp;
     66  1.1  christos 
     67  1.1  christos   /* If we are in RX mode, nothing to do.  */
     68  1.1  christos   if (!(ppi->control & PORT_DIR))
     69  1.1  christos     return;
     70  1.1  christos 
     71  1.1  christos   bpp = bfin_gui_color_depth (ppi->color);
     72  1.1  christos   ppi->gui_state = bfin_gui_setup (ppi->gui_state,
     73  1.1  christos 				   ppi->control & PORT_EN,
     74  1.1  christos 				   (ppi->count + 1) / (bpp / 8),
     75  1.1  christos 				   ppi->frame,
     76  1.1  christos 				   ppi->color);
     77  1.1  christos }
     78  1.1  christos 
     79  1.1  christos static unsigned
     80  1.1  christos bfin_ppi_io_write_buffer (struct hw *me, const void *source, int space,
     81  1.1  christos 			  address_word addr, unsigned nr_bytes)
     82  1.1  christos {
     83  1.1  christos   struct bfin_ppi *ppi = hw_data (me);
     84  1.1  christos   bu32 mmr_off;
     85  1.1  christos   bu32 value;
     86  1.1  christos   bu16 *valuep;
     87  1.1  christos 
     88  1.6  christos   /* Invalid access mode is higher priority than missing register.  */
     89  1.6  christos   if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, true))
     90  1.6  christos     return 0;
     91  1.6  christos 
     92  1.1  christos   value = dv_load_2 (source);
     93  1.1  christos   mmr_off = addr - ppi->base;
     94  1.1  christos   valuep = (void *)((unsigned long)ppi + mmr_base() + mmr_off);
     95  1.1  christos 
     96  1.1  christos   HW_TRACE_WRITE ();
     97  1.1  christos 
     98  1.1  christos   switch (mmr_off)
     99  1.1  christos     {
    100  1.1  christos     case mmr_offset(control):
    101  1.1  christos       *valuep = value;
    102  1.1  christos       bfin_ppi_gui_setup (ppi);
    103  1.1  christos       break;
    104  1.1  christos     case mmr_offset(count):
    105  1.1  christos     case mmr_offset(delay):
    106  1.1  christos     case mmr_offset(frame):
    107  1.1  christos       *valuep = value;
    108  1.1  christos       break;
    109  1.1  christos     case mmr_offset(status):
    110  1.1  christos       dv_w1c_2 (valuep, value, ~(1 << 10));
    111  1.1  christos       break;
    112  1.1  christos     default:
    113  1.1  christos       dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
    114  1.6  christos       return 0;
    115  1.1  christos     }
    116  1.1  christos 
    117  1.1  christos   return nr_bytes;
    118  1.1  christos }
    119  1.1  christos 
    120  1.1  christos static unsigned
    121  1.1  christos bfin_ppi_io_read_buffer (struct hw *me, void *dest, int space,
    122  1.1  christos 			 address_word addr, unsigned nr_bytes)
    123  1.1  christos {
    124  1.1  christos   struct bfin_ppi *ppi = hw_data (me);
    125  1.1  christos   bu32 mmr_off;
    126  1.1  christos   bu16 *valuep;
    127  1.1  christos 
    128  1.6  christos   /* Invalid access mode is higher priority than missing register.  */
    129  1.6  christos   if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, false))
    130  1.6  christos     return 0;
    131  1.6  christos 
    132  1.1  christos   mmr_off = addr - ppi->base;
    133  1.1  christos   valuep = (void *)((unsigned long)ppi + mmr_base() + mmr_off);
    134  1.1  christos 
    135  1.1  christos   HW_TRACE_READ ();
    136  1.1  christos 
    137  1.1  christos   switch (mmr_off)
    138  1.1  christos     {
    139  1.1  christos     case mmr_offset(control):
    140  1.1  christos     case mmr_offset(count):
    141  1.1  christos     case mmr_offset(delay):
    142  1.1  christos     case mmr_offset(frame):
    143  1.1  christos     case mmr_offset(status):
    144  1.1  christos       dv_store_2 (dest, *valuep);
    145  1.1  christos       break;
    146  1.1  christos     default:
    147  1.1  christos       dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
    148  1.6  christos       return 0;
    149  1.1  christos     }
    150  1.1  christos 
    151  1.1  christos   return nr_bytes;
    152  1.1  christos }
    153  1.1  christos 
    154  1.1  christos static unsigned
    155  1.1  christos bfin_ppi_dma_read_buffer (struct hw *me, void *dest, int space,
    156  1.1  christos 			  unsigned_word addr, unsigned nr_bytes)
    157  1.1  christos {
    158  1.1  christos   HW_TRACE_DMA_READ ();
    159  1.1  christos   return 0;
    160  1.1  christos }
    161  1.1  christos 
    162  1.1  christos static unsigned
    163  1.1  christos bfin_ppi_dma_write_buffer (struct hw *me, const void *source,
    164  1.1  christos 			   int space, unsigned_word addr,
    165  1.1  christos 			   unsigned nr_bytes,
    166  1.1  christos 			   int violate_read_only_section)
    167  1.1  christos {
    168  1.1  christos   struct bfin_ppi *ppi = hw_data (me);
    169  1.1  christos 
    170  1.1  christos   HW_TRACE_DMA_WRITE ();
    171  1.1  christos 
    172  1.1  christos   return bfin_gui_update (ppi->gui_state, source, nr_bytes);
    173  1.1  christos }
    174  1.1  christos 
    175  1.1  christos static const struct hw_port_descriptor bfin_ppi_ports[] =
    176  1.1  christos {
    177  1.1  christos   { "stat", 0, 0, output_port, },
    178  1.1  christos   { NULL, 0, 0, 0, },
    179  1.1  christos };
    180  1.1  christos 
    181  1.1  christos static void
    182  1.1  christos attach_bfin_ppi_regs (struct hw *me, struct bfin_ppi *ppi)
    183  1.1  christos {
    184  1.1  christos   address_word attach_address;
    185  1.1  christos   int attach_space;
    186  1.1  christos   unsigned attach_size;
    187  1.1  christos   reg_property_spec reg;
    188  1.1  christos 
    189  1.1  christos   if (hw_find_property (me, "reg") == NULL)
    190  1.1  christos     hw_abort (me, "Missing \"reg\" property");
    191  1.1  christos 
    192  1.1  christos   if (!hw_find_reg_array_property (me, "reg", 0, &reg))
    193  1.1  christos     hw_abort (me, "\"reg\" property must contain three addr/size entries");
    194  1.1  christos 
    195  1.1  christos   hw_unit_address_to_attach_address (hw_parent (me),
    196  1.1  christos 				     &reg.address,
    197  1.1  christos 				     &attach_space, &attach_address, me);
    198  1.1  christos   hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
    199  1.1  christos 
    200  1.1  christos   if (attach_size != BFIN_MMR_PPI_SIZE)
    201  1.1  christos     hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_PPI_SIZE);
    202  1.1  christos 
    203  1.1  christos   hw_attach_address (hw_parent (me),
    204  1.1  christos 		     0, attach_space, attach_address, attach_size, me);
    205  1.1  christos 
    206  1.1  christos   ppi->base = attach_address;
    207  1.1  christos }
    208  1.1  christos 
    209  1.1  christos static void
    210  1.1  christos bfin_ppi_finish (struct hw *me)
    211  1.1  christos {
    212  1.1  christos   struct bfin_ppi *ppi;
    213  1.1  christos   const char *color;
    214  1.1  christos 
    215  1.1  christos   ppi = HW_ZALLOC (me, struct bfin_ppi);
    216  1.1  christos 
    217  1.1  christos   set_hw_data (me, ppi);
    218  1.1  christos   set_hw_io_read_buffer (me, bfin_ppi_io_read_buffer);
    219  1.1  christos   set_hw_io_write_buffer (me, bfin_ppi_io_write_buffer);
    220  1.1  christos   set_hw_dma_read_buffer (me, bfin_ppi_dma_read_buffer);
    221  1.1  christos   set_hw_dma_write_buffer (me, bfin_ppi_dma_write_buffer);
    222  1.1  christos   set_hw_ports (me, bfin_ppi_ports);
    223  1.1  christos 
    224  1.1  christos   attach_bfin_ppi_regs (me, ppi);
    225  1.1  christos 
    226  1.1  christos   /* Initialize the PPI.  */
    227  1.1  christos   if (hw_find_property (me, "color"))
    228  1.1  christos     color = hw_find_string_property (me, "color");
    229  1.1  christos   else
    230  1.1  christos     color = NULL;
    231  1.1  christos   ppi->color = bfin_gui_color (color);
    232  1.1  christos }
    233  1.1  christos 
    234  1.1  christos const struct hw_descriptor dv_bfin_ppi_descriptor[] =
    235  1.1  christos {
    236  1.1  christos   {"bfin_ppi", bfin_ppi_finish,},
    237  1.1  christos   {NULL, NULL},
    238  1.1  christos };
    239