You are going to some really great places (though I don't put the Costa del Sol in that category) but not spending enough time in any of them (except the Costa del Sol). Please reconsider. You arrive on May 6, jetlagged and quite possibly also severely sleep-deprived. You depart on May 22, a day which will be devoted basically entirely to getting to the airport 3 hours before your flight departs. Therefore, you have 15 fully usable days, and you are proposing to go to 3 countries and 9 cities, and you plan to change hotels 9 times. Every time you change hotels it costs you at least half a day, often more. So instead of 15 fully usable days you have 9 days chopped up by traveling to a new city and just 6 days to enjoy where you are. And two of them are on the Costa del Sol, your least interesting destination other then Morocco.
Speaking of which, Morocco is great, but Tangier is not. If you had more time you could fly to Morocco and see Fes and Marrakech. As it stands, you should scratch that idea and use that time somewhere else.
You need to use ViaMichelin.com to check the driving times for your travel legs by car. Those times don't include stopping, getting lost, looking for parking, etc., so you need to pad them considerably.
You can check train times most easily on the Deutsche Bahn website, but if you want to look at fares, you'll need to go elsewhere.
You can get to Malaga from Cordoba in half the time by train as compared to the driving time, so I'd suggest taking the train all the way south and picking up the car there. Actually, I'd suggest skipping Malaga and the Costa del Sol entirely, but it's your trip.
A car rented in Cordoba doesn't necessarily have to be returned to Cordoba. The drop fee may be minor or zero, so it's worth checking out.
I like Portugal, but 15 days is short for Spain alone, so I'd recommend that you hold off on Portugal until you can give it more time. You are proposing to spend over 5 hours driving to Lisbon, take a train to and from Porto, then spend over 6 hours driving from Lisbon to Cordoba--all in order to have one day and a few hours in Lisbon and less than a day in Porto. It's just not a good use of your time.