But the Rambam wrote in the fourth chapter, law 15: Tefillin require a clean body… therefore it is forbidden to sleep in them, neither a permanent sleep nor a temporary sleep. However, if he spread a cloth over them and a woman was not with him—he may sleep in them a temporary sleep. And how does he do this? He places his head between his knees while he is sitting and sleeps. If his tefillin were wrapped in his hand—it is permitted to sleep in them even a permanent sleep. Thus far his words. And he explains that that which they said "that he spreads a cloth over them," this is its explanation: that they are placed on his head, and without a cloth it is forbidden even for a temporary sleep, for we fear lest he sink into sleep even when he places his head between his knees. But when he spreads a cloth over them—he will certainly awaken from his sleep in a short time. But there is no reason or logic in this, obviously, and therefore it must be said that without a cloth we fear he will forget that he is wearing tefillin, and by means of the cloth he will remember (Kessef Mishneh) and will not come to flatulence. And therefore it is permitted for a temporary sleep and not for a permanent sleep. And that which was taught in a Baraita that even a permanent sleep is permitted—this is when they are wrapped in his hand not on the place of tefillin, and they are wrapped tightly so they will not fall. And why did they not forbid it because of distraction of the mind when wearing them on his head? They wrote the reason that distraction of the mind is nothing other than levity and lightheadedness, as I wrote in siman 28. And we further explained there that during the time of sleep the prohibition of distraction of the mind is not applicable, for he is not in the world then at all, see there. And so it is implied in the Levush, see there.
אבל הרמב"ם כתב בפרק רביעי דין ט"ו: תפילין צריכין גוף נקי… לפיכך אסור לישן בהם, לא שינת קבע ולא שינת עראי. אלא אם הניח עליהם סודר ולא היתה עמו אשה – ישן בהם שינת עראי. וכיצד הוא עושה? מניח ראשו בין ברכיו, והוא יושב וישן. היו תפיליו כרוכים בידו – מותר לישן בהם אפילו שינת קבע. עד כאן לשונו. ומפרש דזה שאמרו "דפריס סודרא עלייהו" הכי פירושו: דמונחים בראשו, ובלא סודר אסור אפילו שינת עראי, דחיישינן שמא ישתקע בשינה אף כשמניח ראשו בין ברכיו. אך כשפורס סודר עליהם – בוודאי ייקץ משנתו בזמן קצר. אבל אין טעם וסברא בזה כמובן, ולכן צריך לומר דבלא סודר חיישינן שישכח שנושא תפילין, ועל ידי הסודר יזכור (כסף משנה) ולא יבוא להפחה. ולכן מותר בעראי ולא בקבע. והא דתניא דגם קבע מותר – זהו כשכרוכים בידו שלא על מקום תפילין, וכרוכים בחוזק שלא יפלו. ולמה לא אסרו מפני היסח הדעת בנושאם על ראשו? כתבו הטעם דהיסח הדעת אינו אלא שחוק וקלות ראש, כמו שכתבתי בסימן כ"ח. ועוד בארנו שם דבשעת שינה לא שייך איסור היסח הדעת, דאינו בעולם אז כלל, עיין שם. וכן משמע בלבוש, עיין שם.