github.com/f-secure-foundry/tamago@v0.0.0-20220307101044-d73fcdd7f11b/soc/imx6/iomux.go (about)

     1  // NXP i.MX6 IOMUX driver
     2  // https://github.com/f-secure-foundry/tamago
     3  //
     4  // Copyright (c) F-Secure Corporation
     5  // https://foundry.f-secure.com
     6  //
     7  // Use of this source code is governed by the license
     8  // that can be found in the LICENSE file.
     9  
    10  package imx6
    11  
    12  import (
    13  	"fmt"
    14  
    15  	"github.com/f-secure-foundry/tamago/internal/reg"
    16  )
    17  
    18  // IOMUX registers
    19  const (
    20  	IOMUXC_START = 0x020e0000
    21  	IOMUXC_END   = 0x020e3fff
    22  
    23  	SW_PAD_CTL_HYS = 16
    24  
    25  	SW_PAD_CTL_PUS                = 14
    26  	SW_PAD_CTL_PUS_PULL_DOWN_100K = 0b00
    27  	SW_PAD_CTL_PUS_PULL_UP_47K    = 0b01
    28  	SW_PAD_CTL_PUS_PULL_UP_100K   = 0b10
    29  	SW_PAD_CTL_PUS_PULL_UP_22K    = 0b11
    30  
    31  	SW_PAD_CTL_PUE = 13
    32  	SW_PAD_CTL_PKE = 12
    33  	SW_PAD_CTL_ODE = 11
    34  
    35  	SW_PAD_CTL_SPEED        = 6
    36  	SW_PAD_CTL_SPEED_50MHZ  = 0b00
    37  	SW_PAD_CTL_SPEED_100MHZ = 0b10
    38  	SW_PAD_CTL_SPEED_200MHZ = 0b11
    39  
    40  	SW_PAD_CTL_DSE                        = 3
    41  	SW_PAD_CTL_DSE_OUTPUT_DRIVER_DISABLED = 0b000
    42  	SW_PAD_CTL_DSE_2_R0_2                 = 0b010
    43  	SW_PAD_CTL_DSE_2_R0_3                 = 0b011
    44  	SW_PAD_CTL_DSE_2_R0_4                 = 0b100
    45  	SW_PAD_CTL_DSE_2_R0_5                 = 0b101
    46  	SW_PAD_CTL_DSE_2_R0_6                 = 0b110
    47  	SW_PAD_CTL_DSE_2_R0_7                 = 0b111
    48  
    49  	SW_PAD_CTL_SRE = 0
    50  
    51  	SW_MUX_CTL_SION     = 4
    52  	SW_MUX_CTL_MUX_MODE = 0
    53  )
    54  
    55  // Pad instance.
    56  type Pad struct {
    57  	// mux register (IOMUXC_SW_MUX_CTL_PAD_*)
    58  	mux uint32
    59  	// pad register (IOMUXC_SW_PAD_CTL_PAD_*)
    60  	pad uint32
    61  	// daisy register (IOMUXC_*_SELECT_INPUT)
    62  	daisy uint32
    63  }
    64  
    65  // NewPad initializes a pad.
    66  func NewPad(mux uint32, pad uint32, daisy uint32) (*Pad, error) {
    67  	if mux < IOMUXC_START || mux > IOMUXC_END {
    68  		return nil, fmt.Errorf("invalid mux register %#x", pad)
    69  	}
    70  
    71  	if pad < IOMUXC_START || pad > IOMUXC_END {
    72  		return nil, fmt.Errorf("invalid pad register %#x", pad)
    73  	}
    74  
    75  	if daisy > 0 && (daisy < IOMUXC_START || daisy > IOMUXC_END) {
    76  		return nil, fmt.Errorf("invalid daisy register %#x", daisy)
    77  	}
    78  
    79  	return &Pad{
    80  		mux:   mux,
    81  		pad:   pad,
    82  		daisy: daisy,
    83  	}, nil
    84  }
    85  
    86  // Mode configures the pad iomux mode.
    87  func (pad *Pad) Mode(mode uint32) {
    88  	reg.SetN(pad.mux, SW_MUX_CTL_MUX_MODE, 0b1111, mode)
    89  }
    90  
    91  // SoftwareInput configures the pad SION bit.
    92  func (pad *Pad) SoftwareInput(enabled bool) {
    93  	if enabled {
    94  		reg.Set(pad.mux, SW_MUX_CTL_SION)
    95  	} else {
    96  		reg.Clear(pad.mux, SW_MUX_CTL_SION)
    97  	}
    98  }
    99  
   100  // Ctl configures the pad control register.
   101  func (pad *Pad) Ctl(ctl uint32) {
   102  	reg.Write(pad.pad, ctl)
   103  }
   104  
   105  // Select configures the pad daisy chain register.
   106  func (pad *Pad) Select(input uint32) {
   107  	if pad.daisy == 0 {
   108  		return
   109  	}
   110  
   111  	reg.Write(pad.daisy, input)
   112  }