Autonomous berthing is a crucial technology for autonomous ships, requiring optimal trajectory planning to prevent collisions and minimize time and control efforts. This paper presents a two-phase, two-point boundary value problem (TPBVP) strategy for creating an optimal berthing trajectory for a twin-propeller, twin-rudder ship with autonomous berthing capabilities. The process is divided into two phases: the approach and the terminal. Tunnel thruster use is limited during the approach but fully employed during the terminal phase. This strategy permits concurrent optimization of the total trajectory duration, individual phase trajectories, and phase transition time. The efficacy of the proposed method is validated through two simulations. The first explores a scenario with phase transition, and the second generates a trajectory relying solely on the approach phase. The results affirm our algorithm's effectiveness in deciding transition necessity, identifying optimal transition timing, and optimizing the trajectory accordingly. The proposed two-phase TPBVP approach holds significant implications for advancements in autonomous ship navigation, enhancing safety and efficiency in berthing operations.