claim.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (C) Paul Mackerras 1997.
  3. * Copyright (C) Leigh Brown 2002.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. */
  10. #include "of1275.h"
  11. #include "nonstdio.h"
  12. /*
  13. * Older OF's require that when claiming a specific range of addresses,
  14. * we claim the physical space in the /memory node and the virtual
  15. * space in the chosen mmu node, and then do a map operation to
  16. * map virtual to physical.
  17. */
  18. static int need_map = -1;
  19. static ihandle chosen_mmu;
  20. static phandle memory;
  21. /* returns true if s2 is a prefix of s1 */
  22. static int string_match(const char *s1, const char *s2)
  23. {
  24. for (; *s2; ++s2)
  25. if (*s1++ != *s2)
  26. return 0;
  27. return 1;
  28. }
  29. static int check_of_version(void)
  30. {
  31. phandle oprom, chosen;
  32. char version[64];
  33. oprom = finddevice("/openprom");
  34. if (oprom == OF_INVALID_HANDLE)
  35. return 0;
  36. if (getprop(oprom, "model", version, sizeof(version)) <= 0)
  37. return 0;
  38. version[sizeof(version)-1] = 0;
  39. printf("OF version = '%s'\n", version);
  40. if (!string_match(version, "Open Firmware, 1.")
  41. && !string_match(version, "FirmWorks,3."))
  42. return 0;
  43. chosen = finddevice("/chosen");
  44. if (chosen == OF_INVALID_HANDLE) {
  45. chosen = finddevice("/chosen@0");
  46. if (chosen == OF_INVALID_HANDLE) {
  47. printf("no chosen\n");
  48. return 0;
  49. }
  50. }
  51. if (getprop(chosen, "mmu", &chosen_mmu, sizeof(chosen_mmu)) <= 0) {
  52. printf("no mmu\n");
  53. return 0;
  54. }
  55. memory = (ihandle) call_prom("open", 1, 1, "/memory");
  56. if (memory == OF_INVALID_HANDLE) {
  57. memory = (ihandle) call_prom("open", 1, 1, "/memory@0");
  58. if (memory == OF_INVALID_HANDLE) {
  59. printf("no memory node\n");
  60. return 0;
  61. }
  62. }
  63. printf("old OF detected\n");
  64. return 1;
  65. }
  66. void *claim(unsigned int virt, unsigned int size, unsigned int align)
  67. {
  68. int ret;
  69. unsigned int result;
  70. if (need_map < 0)
  71. need_map = check_of_version();
  72. if (align || !need_map)
  73. return (void *) call_prom("claim", 3, 1, virt, size, align);
  74. ret = call_prom_ret("call-method", 5, 2, &result, "claim", memory,
  75. align, size, virt);
  76. if (ret != 0 || result == -1)
  77. return (void *) -1;
  78. ret = call_prom_ret("call-method", 5, 2, &result, "claim", chosen_mmu,
  79. align, size, virt);
  80. /* 0x12 == coherent + read/write */
  81. ret = call_prom("call-method", 6, 1, "map", chosen_mmu,
  82. 0x12, size, virt, virt);
  83. return virt;
  84. }