I have read suggestions that in the case of an inability to get to sleep, that magnesium is suggested, and that in cases of waking in the 2-4am range, that calcium may be of benefit. Most suggest supplementing with 400mg of Mg at night then adding Calcium(not specific amount mentioned, but I'd guess start with one capsule/tablet of 200-350mg) if early waking is a problem.
Magnesium plays a key role as a cofactor in the production and release of various hormones including neurotransmitters. So, how it benefits is fairly clear. Unfortunately, I have forgotten what I've read as to why calcium helps for staying sleep.