A connection between star formation activity and cosmic rays in the starburst galaxy M82