I am a jr in HS and have had an IEP (very similar to a 504) since 6th grade. I was diagnosed with IBS a few months ago but it was caused by something so obscure they couldn't figure it out for a while. My case manager for the IEP emailed all my teachers so I am allowed to leave class at any time to use the bathroom.
I also go in to school super early bc (like your son) my symptoms and my anxiety are worse in the morning, that way when it is time for me to need a restroom, classes still haven't begun.
I would recommend making an appointment with someone at the school and possibly talk about putting him on a 504 plan. A 504 plan allows the school to make accommodations when the student has a documented health problem.
Have you talked to his school's principle or his teacher?