For an 9 day trip (not counting arrival or departure days), I'd set it up as....
Day 1-3....Madrid (including a day trip to Segovia or Toledo)
Day 4...transit to Sevilla...leave early and spend some hours in Cordoba on the way.
Day 5-6.....Sevilla....leave for Granada on the evening of Day 6.
Day 7-8...Granada.
Day 9....back to Madrid.
You won't have time to get to Portugal without sacrificing some of the great sights and experiences of Spain.
For discounts on the AVE fast train Madrid-Cordoba and Cordoba-Sevilla, you can book at the Spanish national rail website, RENFE. Book up to 62 days in advance....you can check a couple of months out, since some of the runs are put up early, but don't panic if they don't match your travel schedule right now. I would also use this website to book your Toledo or Segovia daytrip, if your dates are firm. No discounts, but you'll avoid ticket lines, and be assured of getting the trains you want to maximize your time. The site is in Spanish, but it's not hard to figure out...and if you have problems, we can help.
For Sevilla-Granada and Granada-Madrid, you can book trains, or you can check the buses, which run more frequently, take about the same length of time, and cost less. Check Movelia for more info.