| Nico Huber | c74eafd | 2024-07-23 12:38:02 +0200 | [diff] [blame] | 1 | -- |
| 2 | -- Copyright (C) 2022 Google, LLC |
| 3 | -- |
| 4 | -- This program is free software; you can redistribute it and/or modify |
| 5 | -- it under the terms of the GNU General Public License as published by |
| 6 | -- the Free Software Foundation; either version 2 of the License, or |
| 7 | -- (at your option) any later version. |
| 8 | -- |
| 9 | -- This program is distributed in the hope that it will be useful, |
| 10 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | -- GNU General Public License for more details. |
| 13 | -- |
| 14 | |
| 15 | with HW.GFX.GMA.Config; |
| 16 | with HW.GFX.GMA.Registers; |
| Nico Huber | dea094a | 2024-08-19 21:31:23 +0200 | [diff] [blame] | 17 | with HW.GFX.DP_Info; |
| 18 | with HW.GFX.GMA.PCode; |
| Nico Huber | c74eafd | 2024-07-23 12:38:02 +0200 | [diff] [blame] | 19 | |
| 20 | with HW.Debug; |
| 21 | with GNAT.Source_Info; |
| 22 | |
| Nico Huber | dea094a | 2024-08-19 21:31:23 +0200 | [diff] [blame] | 23 | use type HW.Word64; |
| 24 | |
| Nico Huber | c74eafd | 2024-07-23 12:38:02 +0200 | [diff] [blame] | 25 | package body HW.GFX.GMA.Connectors.TC.Ownership is |
| 26 | |
| Nico Huber | dea094a | 2024-08-19 21:31:23 +0200 | [diff] [blame] | 27 | type Port_Regs_Array is array (Valid_TC_Port) of Registers.Registers_Index; |
| 28 | |
| 29 | TCSS_DDI_STATUS : constant Port_Regs_Array := |
| 30 | Port_Regs_Array' |
| 31 | (DDI_TC1 => Registers.TCSS_DDI_STATUS_1, |
| 32 | DDI_TC2 => Registers.TCSS_DDI_STATUS_2, |
| 33 | DDI_TC3 => Registers.TCSS_DDI_STATUS_3, |
| 34 | DDI_TC4 => Registers.TCSS_DDI_STATUS_4); |
| 35 | |
| 36 | TCSS_DDI_STATUS_HPD_LIVE_STATUS_ALT : constant := 1 * 2 ** 0; |
| 37 | TCSS_DDI_STATUS_READY : constant := 1 * 2 ** 2; |
| 38 | |
| Nico Huber | c74eafd | 2024-07-23 12:38:02 +0200 | [diff] [blame] | 39 | procedure Claim |
| 40 | (Port : in USBC_Port; |
| 41 | DP_Alt : in Boolean; |
| 42 | Success : out Boolean) |
| 43 | is |
| 44 | begin |
| Nico Huber | dea094a | 2024-08-19 21:31:23 +0200 | [diff] [blame] | 45 | pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity)); |
| 46 | |
| 47 | if Port not in Valid_TC_Port then |
| 48 | Success := False; |
| 49 | return; |
| 50 | end if; |
| 51 | |
| 52 | if not DP_Alt then |
| 53 | -- For legacy ports, this is supposed |
| 54 | -- to be initialized once during boot. |
| 55 | Registers.Wait_Set_Mask |
| 56 | (Register => TCSS_DDI_STATUS (Port), |
| 57 | Mask => TCSS_DDI_STATUS_READY, |
| 58 | TOut_MS => 100); |
| 59 | end if; |
| 60 | |
| 61 | Registers.Is_Set_Mask |
| 62 | (Register => TCSS_DDI_STATUS (Port), |
| 63 | Mask => TCSS_DDI_STATUS_READY, |
| 64 | Result => Success); |
| 65 | if not Success then |
| 66 | pragma Debug (Debug.Put_Line ("DP PHY mode status not complete")); |
| 67 | return; |
| 68 | end if; |
| 69 | |
| 70 | Registers.Set_Mask |
| 71 | (Register => DDI_BUF_CTL (Port), |
| 72 | Mask => DDI_BUF_CTL_TC_PHY_OWNERSHIP); |
| Nico Huber | c74eafd | 2024-07-23 12:38:02 +0200 | [diff] [blame] | 73 | end Claim; |
| 74 | |
| Nico Huber | dea094a | 2024-08-19 21:31:23 +0200 | [diff] [blame] | 75 | procedure Claimed (Port : USBC_Port; Is_Claimed : out Boolean) |
| 76 | with |
| 77 | Refined_Post => (if Is_Claimed then Port in Valid_TC_Port) |
| 78 | is |
| Nico Huber | c74eafd | 2024-07-23 12:38:02 +0200 | [diff] [blame] | 79 | begin |
| Nico Huber | dea094a | 2024-08-19 21:31:23 +0200 | [diff] [blame] | 80 | if Port not in Valid_TC_Port then |
| 81 | Is_Claimed := False; |
| 82 | return; |
| 83 | end if; |
| 84 | |
| 85 | Registers.Is_Set_Mask |
| 86 | (Register => DDI_BUF_CTL (Port), |
| 87 | Mask => DDI_BUF_CTL_TC_PHY_OWNERSHIP, |
| 88 | Result => Is_Claimed); |
| Nico Huber | c74eafd | 2024-07-23 12:38:02 +0200 | [diff] [blame] | 89 | end Claimed; |
| 90 | |
| 91 | --------------------------------------------------------------------- |
| 92 | |
| 93 | procedure Connect |
| 94 | (Port : in USBC_Port; |
| 95 | DP_Alt : in Boolean; |
| 96 | Lanes : in DP_Lane_Count; |
| 97 | Success : out Boolean) |
| 98 | is |
| Nico Huber | dea094a | 2024-08-19 21:31:23 +0200 | [diff] [blame] | 99 | Assigned_Lanes : DP_Lane_Count; |
| Nico Huber | c74eafd | 2024-07-23 12:38:02 +0200 | [diff] [blame] | 100 | begin |
| Nico Huber | dea094a | 2024-08-19 21:31:23 +0200 | [diff] [blame] | 101 | pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity)); |
| 102 | |
| 103 | Claimed (Port, Success); |
| 104 | if not Success then |
| 105 | pragma Debug (Debug.Put_Line ("Tried to connect to unclaimed port.")); |
| 106 | return; |
| 107 | end if; |
| 108 | |
| 109 | if DP_Alt then |
| 110 | Registers.Is_Set_Mask |
| 111 | (Register => TCSS_DDI_STATUS (Port), |
| 112 | Mask => TCSS_DDI_STATUS_HPD_LIVE_STATUS_ALT, |
| 113 | Result => Success); |
| 114 | if not Success then |
| 115 | pragma Debug (Debug.Put_Line ("DP-Alt is not connected.")); |
| 116 | return; |
| 117 | end if; |
| 118 | |
| 119 | Get_Lane_Assignment_Count (Port, Assigned_Lanes); |
| 120 | Set_Lane_Count (Port, Assigned_Lanes); |
| 121 | else |
| 122 | Set_Lane_Count (Port, Lanes); |
| 123 | end if; |
| Nico Huber | c74eafd | 2024-07-23 12:38:02 +0200 | [diff] [blame] | 124 | end Connect; |
| 125 | |
| Nico Huber | dea094a | 2024-08-19 21:31:23 +0200 | [diff] [blame] | 126 | procedure Disconnect (Port : USBC_Port) is |
| 127 | begin |
| 128 | pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity)); |
| 129 | |
| 130 | if Port in Valid_TC_Port then |
| 131 | Registers.Unset_Mask |
| 132 | (Register => DDI_BUF_CTL (Port), |
| 133 | Mask => DDI_BUF_CTL_TC_PHY_OWNERSHIP); |
| 134 | end if; |
| 135 | end Disconnect; |
| Nico Huber | c74eafd | 2024-07-23 12:38:02 +0200 | [diff] [blame] | 136 | |
| 137 | end HW.GFX.GMA.Connectors.TC.Ownership; |