Have you looked at this application note? It is mostly concerned with using the ROM monitor resident routines. But, the information has some application to requirements if you are to be writing your own code. As you may know, one cannot be executing code out of the flash program space at the same time as any erase or program operation is ongoing to the flash program address space. So, you must write a routine to perform the erase operation or the program operation in your code, copy that routine to unused RAM space, and jump execution to that routine. After it has finished you can go back to your flash code. You must take care to not erase your own code from flash!
http://www.nxp.com/assets/documents/data/en/application-notes/AN1831.pdf