Thermodynamics of f(R) theories of gravity