There are two different kinds of paid maternity leave, one based on a minimum period of contributing into the system in Spain, the other non-contributory.
To be entitled to either, you need to be working, or in receipt of unemployment benefits.
So even if if you haven´t worked the minimum amount of time to receive the contributory maternity pay, you are still entitled to the non-contributory maternity pay.
The window to apply for maternity pay starts the day after your maternity leave has begun and prescribes after 5 years.
The full information is in Spanish only. Some areas can be translated using the drop down menu. Where the option is not available, use computer translation or vist the INSS office.
You may wish to use a gestor or HR at your workplace may advise.