The question is very extensive, I understand that. I would appreciate both sources that investigate evolution of the "castle" over the time (if such thing exists) and particularly medieval castles. Thank you very much!
A good place to start is Charles Coulson's Castles in Medieval Society: Fortresses in England, France, and Ireland in the Central Middle Ages (Oxford Univ Press, 2003). He treats castles as a social space and not just defensive or architectural curio and engages with some current historiographical issues about the development of castles in the 9-12th centuries. You can preview it on Google Books here.