The story
From the literal action of intensely working a grindstone, whether powered by a treadle or waterwheel. The expression initially implied punishment or abusive management, forcing the worker into intense work, and was used in the anonymous translation (1557) of Erasmus's Merry Dialogue as a hyperbolic punishment threatened for an abusive husband. It was later adapted to forcing oneself into similarly intense effort.
Etymology from Wiktionary, CC BY-SA 4.0.