mailbox.pp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. {
  2. Copyright (c) 1998-2022 by the Free Pascal development team
  3. Broadcom "mailbox" call interface
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {$IFNDEF FPC_DOTTEDUNITS}
  18. unit mailbox;
  19. {$ENDIF FPC_DOTTEDUNITS}
  20. interface
  21. type
  22. TMBox = bitpacked Array[0..36] of DWord;
  23. var
  24. MBox: TMBox;
  25. const
  26. MBOX_REQUEST = 0;
  27. { Channels }
  28. MBOX_CH_POWER = 0;
  29. MBOX_CH_FB = 1;
  30. MBOX_CH_VUART = 2;
  31. MBOX_CH_VCHIQ = 3;
  32. MBOX_CH_LEDS = 4;
  33. MBOX_CH_BTNS = 5;
  34. MBOX_CH_TOUCH = 6;
  35. MBOX_CH_COUNT = 7;
  36. MBOX_CH_PROP = 8;
  37. { Tags }
  38. MBOX_TAG_GETSERIAL = $10004;
  39. MBOX_TAG_SETCLKRATE = $38002;
  40. MBOX_TAG_LAST = 0;
  41. { Responses }
  42. MBOX_RESPONSE = $80000000;
  43. MBOX_FULL = $80000000;
  44. MBOX_EMPTY = $40000000;
  45. { Mailbox offsets from PeripheralBase }
  46. VIDEOCORE_MBOX = $0000B880;
  47. MBOX_READ = $0;
  48. MBOX_POLL = $10;
  49. MBOX_SENDER = $14;
  50. MBOX_STATUS = $18;
  51. MBOX_CONFIG = $1C;
  52. MBOX_WRITE = $20;
  53. function MailboxCall(BaseAddr: DWord; Channel: DWord): DWord;
  54. implementation
  55. {$IFDEF FPC_DOTTEDUNITS}
  56. uses
  57. EmbeddedApi.mmio;
  58. {$ELSE FPC_DOTTEDUNITS}
  59. uses
  60. mmio;
  61. {$ENDIF FPC_DOTTEDUNITS}
  62. function MailboxCall(BaseAddr: DWord; Channel: DWord): DWord;
  63. var
  64. MBoxPtr: Pointer;
  65. R: PtrUint;
  66. MboxAddr: DWord;
  67. begin
  68. MboxAddr := BaseAddr + VIDEOCORE_MBOX;
  69. MBoxPtr := Addr(MBox);
  70. while True do
  71. begin
  72. DUMMY(1);
  73. if (GET32(MboxAddr + MBOX_STATUS) and MBOX_FULL) = 0 then break;
  74. end;
  75. { Join the address and channel together to identify our message }
  76. R := (PtrUint(MBoxPtr) and (not $0F)) or (Channel and $0F);
  77. { Write to the mailbox - put the address of our message on the mailbox }
  78. PUT32(MboxAddr + MBOX_WRITE, R);
  79. while True do
  80. begin
  81. DUMMY(1);
  82. { Are there any messages pending in the mailbox? }
  83. if (GET32(MboxAddr + MBOX_STATUS) and MBOX_EMPTY) > 0 then continue;
  84. { Is it a response to our message? }
  85. if (GET32(MboxAddr + MBOX_READ) = R) then
  86. begin
  87. Exit(MBox[1]);
  88. end;
  89. end;
  90. MailboxCall := 0;
  91. end;
  92. end.