1. Disconnect the negative battery cable.
2. Remove the headliner.
3. Disconnect the connector.
4. Remove the screws, and then remove the sunroof motor.

Sunroof unit disassembly/assembly
Sunroof motor installationMazda 6 Service Manual: Automatic Transaxle Controls
Various Lockouts:
Indicates that you must depress the brake
pedal and hold in the lock-release button to shift (The ignition must be switched
ON).
Indicates the shift lever can be shifted
freely into any position.
Indicates that you must hold in the lock-release
button to shift.
N ...
Mazda 6 Service Manual: No.4 air intake mode does not change
When performing an asterisked (*) troubleshooting inspection, shake the
wiring harness and connectors while performing the inspection to discover
whether poor contact points are the cause of any intermittent malfunctions.
If there is a problem, check to make sure connectors, terminal ...