Cosmic ray feedback in hydrodynamical simulations of galaxy formation