Square wave to DC voltage with passive components
The following voltage doubler may give you what you want







simulate this circuit &ndash; Schematic created using CircuitLab

It presents the peak to peak input voltage to the output, less two diode drops. Choose D2/3 to be schottky types for lower drop than silicon. A 5v squarewave will give you a bit more than 4v.

Choose C1 depending on frequency. Its impedance at the input frequency needs to be much less than the load resistor R1.

R2 should be as small as possible, consistent with it limiting the current taken from the gate driving the line. If it's a 'buffer gate' like HC125 or similar, it could be quite small, or with a small C1, not needed at all.

The ratio C2/C1 determines how many cycles are needed to 'pump up' the output voltage.

The product C2.R1 determines the time it takes for the output voltage to drop once the input signal has gone to DC.

With its less than 5v output, slow ramp up, and slow ramp down, the circuit may or may not meet your requirements. You may want to add a transistor or logic buffer to get crisper outputs. 

To get 5v output, you'll need either to use a higher voltage input, to stack on another doubler stage Dickson multiplier style, or to add active FETs to reduce the diode drop to near zero, which is probably more complicated than you need.







simulate this circuit

The zener clamps the output voltage. 

Bonus Points. What's the difference between Dickson and Cockcroft-Walton? This is a Dickson. In a Cockcroft-Walton, C3 goes to the junction of C1/D2/D3. The Dickson style has a lower output impedance, but subjects its input capacitors to a higher voltage. This means low voltage cascades are best with a Dickson, and high voltage cascades usually need a Cockcroft-Walton.