Atomic-scale mechanisms of ferroelastic domain-wall-mediated ferroelectric switching