Here's an idea: assign a weight modifier onto infantry based upon how heavily armored and shielded they are. The more protected they are, the higher the value. Every time a horseman passes/tramples through an infantryman, subtract from the horseman's speed a ratio based upon the weight of the infantryman. This will allow the horsemen to "pass through" many more lightly armored foes before being forced into a halt due to a lack of momentum. At the same time, they can still be dealt with by the same horde of spear peasants by simply getting into a tight cluster formation, forcing the horsemen to continue bleeding speed as they trample more and more people, until they are eventually ground to a halt and speared. This would also help smaller, more elite units of infantry that have better armor in dealing with them since they still lack the numbers advantage against cavalry. This would also promote going into a tight formation against horses in the first place. With more bodies together, the horses will (try to) trample more, and eventually be forced to a grinding halt and slaughtered by the tightly packed troops.
Whereas on the opposite end of the spectrum, it would make it quite easy for horses to deal with spread/unorganized infantry like archers since they would have not enough numbers nor armor to stop a cav charge, especially so if they are in loose formation trying to avoid getting hit by arrows on top of this.
You could go even further with this and give troops a level of "training" in how tight of a formation they are willing to get into, with armed rabble being way more spread out and untrained in this than an elite force, thus still maintaining the balance between infantry and cavalry, relatively speaking.