Here's some old code I used some years ago for a PIC32MX230F064B which has a reasonably similar, if not identical, clock module to you device.
It boots in FRCPLL 40MHz mode, then switches to 8MHz FRC for a short while, then to a 40MHz PRIPLL based on EC of 8MHz because the internal FRC is not accurate enough for the USB peripheral. It's been a long time, but I think you need to switch to a non PLL mode before switching back to a PLL mode.
Some notes...
There's also a slight complication as this code is in the bootloader, so it has to re-initialise stuff in the event that the main application fails (e.g., non-POR reset or trap of some sort).
On my board, the 8MHz EC is not available at boot, it is derived from an external clock distribution chain that needs initialising.
It looks like if it fails PRIPLL, it tries to set itself back to FRCPLL.
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_2
#pragma config POSCMOD = EC, FNOSC = FRCPLL, FPBDIV = DIV_1
#pragma config FCKSM = CSECME
#pragma config FSOSCEN = OFF
#pragma config UPLLEN = ON, UPLLIDIV = DIV_2, FVBUSONIO = OFF, FUSBIDIO = OFF
#pragma config JTAGEN = OFF
#pragma config IESO = OFF, OSCIOFNC = OFF
#pragma config ICESEL = ICS_PGx1
#pragma config FWDTEN = ON, WDTPS = PS2048
#pragma config PMDL1WAY = OFF // Allow multiple PMD reconfigurations
#pragma config IOL1WAY = OFF // Allow multiple PPS reconfigurations
#if defined(__DEBUG) || !defined(CODEPROTECT)
#pragma config CP = OFF // Code Protect: OFF
#pragma config BWP = OFF // Boot Flash Write Protect: OFF
#pragma config PWP = OFF // Program Flash Write Protect: OFF
#else
#pragma config CP = ON // Code Protect: ON
#pragma config BWP = ON // Boot Flash Write Protect: ON
#pragma config PWP = PWP19K // Program Flash Write Protect: ON /**** HL ****/
#endif
// from legacy peripheral library lock.h:
#ifdef _DMAC
#define mSYSTEMUnlock(intStat, dmaSusp) do{intStat=INTDisableInterrupts(); dmaSusp=DmaSuspend(); \
SYSKEY = 0, SYSKEY = 0xAA996655, SYSKEY = 0x556699AA;}while(0)
#else
#define mSYSTEMUnlock(intStat, dmaSusp) do{intStat=INTDisableInterrupts(); \
SYSKEY = 0, SYSKEY = 0xAA996655, SYSKEY = 0x556699AA;}while(0)
#endif // _DMAC
#ifdef _DMAC
#define mSYSTEMLock(intStat, dmaSusp) do{SYSKEY = 0x33333333; DmaResume(dmaSusp); INTRestoreInterrupts(intStat);}while(0)
#else
#define mSYSTEMLock(intStat, dmaSusp) do{SYSKEY = 0x33333333; INTRestoreInterrupts(intStat);}while(0)
#endif // _DMAC
static BOOL MyBoardSwitchOscSet(int nOsc,int nCount)
{
// Call with nCount==-1 for no timeout
OSCCONbits.NOSC=nOsc;
OSCCONSET=1;
while (OSCCONbits.OSWEN!=0)
{
if (nCount>=0)
{
nCount--;
if (nCount<=0)
{
return FALSE;
}
ClearWDT();
}
}
return TRUE;
}
static void MyBoardSwitchOsc(void)
{
static int nCount=0;
int nIntStatus;
int nDMASuspend;
BOOL b;
mSYSTEMUnlock(nIntStatus,nDMASuspend);
MyBoardSwitchOscSet(0,-1); // Set to basic FRC mode to start with (won't work otherwise)
b=MyBoardSwitchOscSet(3,100000); // Try PRIPLL with timout
if (!b || OSCCONbits.NOSC!=OSCCONbits.COSC || OSCCONbits.CF || !OSCCONbits.SLOCK)
{
MyBoardSwitchOscSet(0,-1); // Basic FRC
MyBoardSwitchOscSet(1,-1); // Try FRCPLL if possible
}
mSYSTEMLock(nIntStatus,nDMASuspend);
if (OSCCONbits.COSC!=3)
{
LEDStateHalt(LED_BAD_OSCILLATOR);
}
}