As early as the reign of Alfonso II "el Casto" King of Asturias (791-842), the construction of a series of fortified villages and castles was started in the mountains above the upper Ebro to guard against Muslim invasions, from which the name "Castilla" (later given to the area) was derived.
In 804, a separate bishopric was established at Valpuesta to administer religious affairs in the area.
Ibn al-Athir records that Fruela I King of Asturias (so dated to the 760s) took control of Lugo, Portucale, Salamanca, Zamora, vila, Segovia y Castilla Martnez Dez indicates that this passage represents the earliest mention of Castilla in history, although he concedes that Ibn al-Athir may have updated texts written in the 9th or 10th century by substituting the term which described the area when he compiled his own work (in the mid-13th century).
In 1029, the last independent Count of Castile, Garca II Snchez, was murdered.
They also strengthened the county's frontiers against Muslim incursions, although in the later 10th and early 11th centuries the ruling counts benefited from strategic alliances with certain Muslim rulers.
In the early 11th century, the county of Castile was to some extent eclipsed by the powerful Sancho III King of Navarre, who had succeeded as king in 999 and already controlled the county of Aragon in addition to Navarre itself.
Although his sister, the queen of Navarre, nominally succeeded as countess in her own right, the county was claimed in her name by her husband King Sancho, who installed their second son Fernando as count in her place.
After extending his influence to the kingdom of Len through another judicious marriage, King Sancho became the focus of all political power in the various Christian states in Spain. The king divided his territories between his sons on his death in 1035.