Author Topic: Setting PowerBook G4 fan speed in open firmware  (Read 1570 times)

Offline Greystash

  • Consistant Contributor
  • ***
  • Posts: 51
  • New Member
Setting PowerBook G4 fan speed in open firmware
« on: March 12, 2019, 01:40:50 AM »
Is there anyone out there with some knowledge on Open Firmware?

I've been playing around with OF recently to try turning the fans on my PowerBook G4 while running OS9. I've hit a roadblock trying to modify values within the 'fan' device, each time I try to modify a property OF returns "no current instance"

I can change values for other devices, the example below turns all LEDs on:

Code: [Select]
dev via-pmu
ledson true

Modifying fan properties example:
Code: [Select]
dev fan
fans-on true

RETURNS
> no current instance

I found this article where somebody enabled the fans on their Pismo, but I haven't been able to get close to this.

Below is a list of the 'words' relevant to the fan device.

Offline Daniel

  • Silver Member
  • ****
  • Posts: 220
  • Programmer, Hacker, Thinker
Re: Setting PowerBook G4 fan speed in open firmware
« Reply #1 on: March 12, 2019, 04:12:47 AM »
Most OF devices insist that you "open" them and create and instance.

Code: [Select]
" fan" open-dev constant fan-ih
" fans-on" fan-ih $call-method

Once you are done with an opened device, you can close it. It doesn't really matter if you do.
Code: [Select]
fan-ih close-dev
I don't know how to turn on the fan, but the "see" word lets you look inside the OF routines.
Code: [Select]
dev fan
see fan-on

Offline darthnVader

  • Gold Member
  • *****
  • Posts: 466
  • New Member
Re: Setting PowerBook G4 fan speed in open firmware
« Reply #2 on: March 12, 2019, 11:47:16 AM »
I don't want to hijack the thread, but I figure I'll side track it a little.

Something me and Elliot have been working on, iBooks and Powerbooks with the 7447a boot in dfs-low mode, and because OS 9 has no power management software for the 7447a the CPU runs at 50% under OS 9.

We've been working on fixing this through the Mac OS ROM, but this thread makes me think maybe we could do it with an nvram script, and I've found some things that make me think there is a way to do what we want...

Code: [Select]
Trying 10.1.2.3...
Connected to 10.1.2.3.
Escape character is '^]'.
 ok
0 > dev /cpus/@0  ok
0 > words

set-dfs-high    translate       modify          unmap           map
release         claim           set-dfs-low     set-dfs-high    spd
close           open  ok
0 > spd 140  pll-ratio*2 = 10  HID1 = 80416c80  DelayAACK = 0  GPIO1 = 4 BusClk = 133173712  ok
0 > set-dfs-high  ok
1 > spd 140  pll-ratio*2 = 10  HID1 = 80416c80  DelayAACK = 0  GPIO1 = 4 BusClk = 133173712  ok
1 > modify
STACK UNDERFLOW!
 ok
0 > claim
CLAIM failed
 ok
0 >

set-dfs-high is what we are looking for, but I'm unsure how to use the word?

Seems it would also use the "modify"  or maybe the "claim" word, or "translate"?

Code: [Select]
0 > " /cpus/@0" open-dev constant set-dfs-high  ok
0 > " translate" set-dfs-high $call-method  ok
0 > set-dfs-high close-dev  ok
0 > spd 140  pll-ratio*2 = 10  HID1 = 80416c80  DelayAACK = 0  GPIO1 = 4 BusClk = 133173712  ok
0 >

That didn't work, as we can see from "spd" HID1 bit 9 is still set to true, but I'm really close, and just need a little help getting the correct syntax, Daniel?

Offline darthnVader

  • Gold Member
  • *****
  • Posts: 466
  • New Member
Re: Setting PowerBook G4 fan speed in open firmware
« Reply #3 on: March 12, 2019, 12:12:26 PM »
Never mind, it couldn't be simpler.

Code: [Select]
Trying 10.1.2.3...
Connected to 10.1.2.3.
Escape character is '^]'.
 ok
0 > dev /cpus/@0  ok
0 > spd 139  pll-ratio*2 = 10  HID1 = 80416c80  DelayAACK = 0  GPIO1 = 4 BusClk = 133173483  ok
0 > set-dfs-high  ok
0 > spd 70  pll-ratio*2 = 20  HID1 = 80015c80  DelayAACK = 0  GPIO1 = 7 BusClk = 133173483  ok
0 >


Offline Greystash

  • Consistant Contributor
  • ***
  • Posts: 51
  • New Member
Re: Setting PowerBook G4 fan speed in open firmware
« Reply #4 on: March 13, 2019, 12:57:13 AM »
Most OF devices insist that you "open" them and create and instance.

Code: [Select]
" fan" open-dev constant fan-ih
" fans-on" fan-ih $call-method

Once you are done with an opened device, you can close it. It doesn't really matter if you do.
Code: [Select]
fan-ih close-dev
I don't know how to turn on the fan, but the "see" word lets you look inside the OF routines.
Code: [Select]
dev fan
see fan-on


Thanks Daniel! Your code turns the fans on at full speed which is a fantastic start!

However, I can't figure out how to work with any of the routines. There's a lot of options that look like they can be modified but it's a matter of figuring out the correct syntax to overwrite the default values. For example 'set-speed' and '.set-speed' give very different output when using the 'see' method and don't give much indication on what can be edited:

Code: [Select]
> see .speed
1 read-speed cr " TACH1 = " type .d 2 read-speed cr " TACH2 = " type .d 3 read-speed cr " TACH3 = " type .d 4 read-speed cr " TACH4 = type .d ; ok

Code: [Select]
> see set-speed
>r 40 dup read-reg 8 andc write-reg case
    1 of
      5c dup read-reg e0 or write-reg 30 r> write-reg
      endof
    2 of
      5d dup read-reg e0 or write-reg 31 r> write-reg
      endof
    3 of
      5e dup read-reg e0 or write-reg 32 r> write-reg
      endof
    dup of
      5e dup read-reg e0 or write-reg 32 r> write-reg
      endof
  endcase
; ok

Code: [Select]
> see set-speeds
1 over set-speed 2 over set-speed 3 swap set-speed

I'll keep mucking around with it but if anyone can offer any hints it would be greatly appreciated!

Offline Daniel

  • Silver Member
  • ****
  • Posts: 220
  • Programmer, Hacker, Thinker
Re: Setting PowerBook G4 fan speed in open firmware
« Reply #5 on: March 13, 2019, 03:35:27 PM »
Looks like 'set-speed' takes an index of what speed to set and the number to set the speed to.
Code: [Select]
1 2a " set-speed" fan-ih $call-method
That would set speed variable 1 to the value 0x2a (42 in decimal). There seem to be 3 speed variables. Who knows what they do?

I would suggest turning the fan on, then using '.speed' to see what the different speed values are. Then try using set-speed to change one of the values with one that is close to it. If the fan noise changes, you have found out something.

That stuff with 'case' and 'of' and 'endof' and 'endcase' is a switch statement.
Code: [Select]
: test-switch case
0 of ." 0 selected" cr endof
1 of ." 1 selected" cr endof
dup of ." something else selected" cr endof
endcase
;
0 test-switch
1 test-switch
51F244 test-switch
'dup of' is a tricky way to match any possible value in the switch statement.

'>r' puts a value onto the return stack and 'r>' moves it back to the data stack. It is a simple way to leapfrog data over a bunch of code. It can mess up loops and function returns if used incorrectly, so beware.

I think that should be enough for you to figure out what is happening. If all else fails, make small test functions that contain just the things you want to figure out. Mess with them and see what happens.

Offline Greystash

  • Consistant Contributor
  • ***
  • Posts: 51
  • New Member
Re: Setting PowerBook G4 fan speed in open firmware
« Reply #6 on: March 13, 2019, 06:07:31 PM »
Looks like 'set-speed' takes an index of what speed to set and the number to set the speed to.
Code: [Select]
1 2a " set-speed" fan-ih $call-method
That would set speed variable 1 to the value 0x2a (42 in decimal). There seem to be 3 speed variables. Who knows what they do?

I would suggest turning the fan on, then using '.speed' to see what the different speed values are. Then try using set-speed to change one of the values with one that is close to it. If the fan noise changes, you have found out something.

That stuff with 'case' and 'of' and 'endof' and 'endcase' is a switch statement.
Code: [Select]
: test-switch case
0 of ." 0 selected" cr endof
1 of ." 1 selected" cr endof
dup of ." something else selected" cr endof
endcase
;
0 test-switch
1 test-switch
51F244 test-switch
'dup of' is a tricky way to match any possible value in the switch statement.

'>r' puts a value onto the return stack and 'r>' moves it back to the data stack. It is a simple way to leapfrog data over a bunch of code. It can mess up loops and function returns if used incorrectly, so beware.

I think that should be enough for you to figure out what is happening. If all else fails, make small test functions that contain just the things you want to figure out. Mess with them and see what happens.


Thanks so much Daniel! I can change the fan speed by changing the hex value which is exactly what I want. I've tried adding the configuration I want to an NVRAM script with nvedit and use-nvramrc, and it sort of works when the PB is turned on but the fans stop as soon as it starts looking for the system. I'm guessing this is because the instance is no longer open/running when the PB has finished with Open Firmware?

Is there any way to get these settings to persist through boot?

This is my script:

Code: [Select]
nvedit

dev /cpus/PowerPC,G4@0
80010201 encode-int " cpu-version" property
device-end

dev fan
" fan" open-dev constant fan-ih
1 384 " set-speed" fan-ih $call-method
2 384 " set-speed" fan-ih $call-method
device-end

Exit with ctrl+c

Code: [Select]
nvstore
setenv use-nvramrc? true
reset-all

I've also tried specifying each 'fan' alias as it's full path but that doesn't work.

If I run the commands outside of the nvram editor and run `mac-boot` the fans continue to run at the speed I've specified
« Last Edit: March 13, 2019, 07:02:56 PM by Greystash »

Offline Greystash

  • Consistant Contributor
  • ***
  • Posts: 51
  • New Member
Re: Setting PowerBook G4 fan speed in open firmware
« Reply #7 on: March 13, 2019, 09:59:37 PM »
Since I haven't been able to get the fan speeds to work within the nvram properly I've just written a script with everything I need, and this is easier than typing the fan config each boot.

Code: [Select]
\ comment
cr
dev fan
" fan" open-dev constant fan-ih
1 384 " set-speed" fan-ih $call-method
2 384 " set-speed" fan-ih $call-method
cr
dev /cpus/PowerPC,G4@0
80010201 encode-int " cpu-version" property
device-end
mac-boot

Each time I want the fans to run when I'm using the PB I need to boot into OF, then execute the script ('fans' which was written in SimpleText):
Code: [Select]
boot hd:\fans
Is there a way to either force Open Firmware to load each boot, or to get this code working within the nvram?

Offline Daniel

  • Silver Member
  • ****
  • Posts: 220
  • Programmer, Hacker, Thinker
Re: Setting PowerBook G4 fan speed in open firmware
« Reply #8 on: March 14, 2019, 12:51:37 PM »
Since I haven't been able to get the fan speeds to work within the nvram properly I've just written a script with everything I need, and this is easier than typing the fan config each boot.

Code: [Select]
\ comment
cr
dev fan
" fan" open-dev constant fan-ih
1 384 " set-speed" fan-ih $call-method
2 384 " set-speed" fan-ih $call-method
cr
dev /cpus/PowerPC,G4@0
80010201 encode-int " cpu-version" property
device-end
mac-boot

Each time I want the fans to run when I'm using the PB I need to boot into OF, then execute the script ('fans' which was written in SimpleText):
Code: [Select]
boot hd:\fans
Is there a way to either force Open Firmware to load each boot, or to get this code working within the nvram?

The boot-device nvram variable can be used to boot from a file.
Code: [Select]
setenv boot-device hd:\fansIt is recommended that you make the boot script boot to the previous boot device (likely "hd:,\\:tbxi"). Just putting 'mac-boot' at the end would make it boot itself again and again and again if it was set as the boot-device.

As for getting it to work in nvram, I suspect that something resets the fans once the boot starts. Maybe make a function in nvram that starts the fans, and then call that function in the boot command?
Code: [Select]
nvedit

dev /cpus/PowerPC,G4@0
80010201 encode-int " cpu-version" property
device-end

0 value fan-ih
: activate-fans
" fan" open-dev to fan-ih
1 384 " set-speed" fan-ih $call-method
2 384 " set-speed" fan-ih $call-method
;
Then change the boot-command nvram variable so activate-fans is called.
Code: [Select]
setenv boot-command activate-fans mac-boot
There are other ways of doing it, like building ELN's NewWorld-rom repo and adding your code to the boot script.

If you really want to mess with stuff, try putting your code into a 'Apple_Driver_OpenFirmware' partition. If a filesystem is accessed from OF, the '/packages/mac-parts' package will look for a Apple Partition Map partition of type 'Apple_Driver_OpenFirmware' on the device. If it finds one, it will treat the contents of the partition as either raw forth or fcode and try to run it. It expects the code to properly create a device package which can be passed the device accesses, but it can be used to just run arbitrary code when the disk is first used. Try sticking fan code there  ;D

Offline Greystash

  • Consistant Contributor
  • ***
  • Posts: 51
  • New Member
Re: Setting PowerBook G4 fan speed in open firmware
« Reply #9 on: March 15, 2019, 02:01:33 AM »
Thanks so much Daniel!! For now I went with the boot from file method and that's working perfectly :D
I'll take a look at adding this to a ROM file and experiment with the 'Apple_Driver_OpenFirmware' partition method. I'll post an update once I've sorted experimented with all of the options.

Once again thank you so much for the help it's much appreciated! :)

Offline Daniel

  • Silver Member
  • ****
  • Posts: 220
  • Programmer, Hacker, Thinker
Re: Setting PowerBook G4 fan speed in open firmware
« Reply #10 on: March 15, 2019, 01:48:21 PM »
No problem. It's very fun :)