Muggles, I think most of the people on this board would reply to your question with : "forever".
Madrid and Barcelona are very different and for the average non - spainophile (?) a week in each city would probably be enough but then again, I could spend a week just visiting the Prado in Madrid and still not feel I'd "done" it.
However long you stay, you'd need to get out of the cities to really experience Spain. It all boils down, I guess, to why you're going in the first place and what kinds of things you like to do. How much time do you have?