You can only write to flash if the code is running in the the last segment of the memory
ATmega88PA, ATmega168PA and ATmega328P support a real Read-While-Write Self-Programmingmechanism. There is a separate Boot Loader Section, and the SPM instruction canonly execute from there. In ATmega48PA, there is no Read-While-Write support and no separateBoot Loader Section. The SPM instruction can execute from the entire Flash.
Keep in mind that the flash memory only have a 10,000 write cycle endurance