mailbox.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. unit mailbox;
  18. interface
  19. type
  20. TMBox = bitpacked Array[0..36] of DWord;
  21. var
  22. MBox: TMBox;
  23. const
  24. MBOX_REQUEST = 0;
  25. { Channels }
  26. MBOX_CH_POWER = 0;
  27. MBOX_CH_FB = 1;
  28. MBOX_CH_VUART = 2;
  29. MBOX_CH_VCHIQ = 3;
  30. MBOX_CH_LEDS = 4;
  31. MBOX_CH_BTNS = 5;
  32. MBOX_CH_TOUCH = 6;
  33. MBOX_CH_COUNT = 7;
  34. MBOX_CH_PROP = 8;
  35. { Tags }
  36. MBOX_TAG_GETSERIAL = $10004;
  37. MBOX_TAG_SETCLKRATE = $38002;
  38. MBOX_TAG_LAST = 0;
  39. { Responses }
  40. MBOX_RESPONSE = $80000000;
  41. MBOX_FULL = $80000000;
  42. MBOX_EMPTY = $40000000;
  43. { Mailbox offsets from PeripheralBase }
  44. VIDEOCORE_MBOX = $0000B880;
  45. MBOX_READ = $0;
  46. MBOX_POLL = $10;
  47. MBOX_SENDER = $14;
  48. MBOX_STATUS = $18;
  49. MBOX_CONFIG = $1C;
  50. MBOX_WRITE = $20;
  51. function MailboxCall(BaseAddr: DWord; Channel: DWord): DWord;
  52. implementation
  53. uses
  54. mmio;
  55. function MailboxCall(BaseAddr: DWord; Channel: DWord): DWord;
  56. var
  57. MBoxPtr: Pointer;
  58. R: PtrUint;
  59. MboxAddr: DWord;
  60. begin
  61. MboxAddr := BaseAddr + VIDEOCORE_MBOX;
  62. MBoxPtr := Addr(MBox);
  63. while True do
  64. begin
  65. DUMMY(1);
  66. if (GET32(MboxAddr + MBOX_STATUS) and MBOX_FULL) = 0 then break;
  67. end;
  68. { Join the address and channel together to identify our message }
  69. R := (PtrUint(MBoxPtr) and (not $0F)) or (Channel and $0F);
  70. { Write to the mailbox - put the address of our message on the mailbox }
  71. PUT32(MboxAddr + MBOX_WRITE, R);
  72. while True do
  73. begin
  74. DUMMY(1);
  75. { Are there any messages pending in the mailbox? }
  76. if (GET32(MboxAddr + MBOX_STATUS) and MBOX_EMPTY) > 0 then continue;
  77. { Is it a response to our message? }
  78. if (GET32(MboxAddr + MBOX_READ) = R) then
  79. begin
  80. Exit(MBox[1]);
  81. end;
  82. end;
  83. MailboxCall := 0;
  84. end;
  85. end.