Aims. We analyze optical and X-ray observations of GRB 050904 obtained with TAROT and SWIFT. Methods. We perform temporal and spectral analysis of the X-ray and optical data. Results. We find significant absorption in the early phase of the X-ray light curve, with some evidence (3s level) of variability. We interpret this as a progressive photo-ionization. We investigate the environment of the burst and constrain its density profile. We find that the overall behavior of the afterglow is compatible with a fireball expanding in a wind environment during the first 2000 s after the burst (observer frame). On the other hand, the late (after 0.5 days, observer frame) afterglow is consistent with an interstellar medium, suggesting the possible presence of a termination shock. We estimate the termination shock position to be R-t similar to 1.8 x 10(-2) pc, and the wind density parameter to be A(*) similar to 1.8. We try to explain the simultaneous flares observed in optical and X-ray bands in light of different models: delayed external shock from a thick shell, inverse Compton emission from reverse shock, inverse Compton emission from late internal shocks or a very long internal shock activity. Among these models, those based on a single emission mechanism, are unable to account for the broad-band observations. Models invoking late internal shocks, with the inclusion of IC emission, or a properly tuned very long internal shock activity, offer possible explanations.