smu.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. #ifndef _SMU_H
  2. #define _SMU_H
  3. /*
  4. * Definitions for talking to the SMU chip in newer G5 PowerMacs
  5. */
  6. #include <linux/config.h>
  7. #include <linux/list.h>
  8. /*
  9. * Known SMU commands
  10. *
  11. * Most of what is below comes from looking at the Open Firmware driver,
  12. * though this is still incomplete and could use better documentation here
  13. * or there...
  14. */
  15. /*
  16. * Partition info commands
  17. *
  18. * These commands are used to retreive the sdb-partition-XX datas from
  19. * the SMU. The lenght is always 2. First byte is the subcommand code
  20. * and second byte is the partition ID.
  21. *
  22. * The reply is 6 bytes:
  23. *
  24. * - 0..1 : partition address
  25. * - 2 : a byte containing the partition ID
  26. * - 3 : length (maybe other bits are rest of header ?)
  27. *
  28. * The data must then be obtained with calls to another command:
  29. * SMU_CMD_MISC_ee_GET_DATABLOCK_REC (described below).
  30. */
  31. #define SMU_CMD_PARTITION_COMMAND 0x3e
  32. #define SMU_CMD_PARTITION_LATEST 0x01
  33. #define SMU_CMD_PARTITION_BASE 0x02
  34. #define SMU_CMD_PARTITION_UPDATE 0x03
  35. /*
  36. * Fan control
  37. *
  38. * This is a "mux" for fan control commands. The command seem to
  39. * act differently based on the number of arguments. With 1 byte
  40. * of argument, this seem to be queries for fans status, setpoint,
  41. * etc..., while with 0xe arguments, we will set the fans speeds.
  42. *
  43. * Queries (1 byte arg):
  44. * ---------------------
  45. *
  46. * arg=0x01: read RPM fans status
  47. * arg=0x02: read RPM fans setpoint
  48. * arg=0x11: read PWM fans status
  49. * arg=0x12: read PWM fans setpoint
  50. *
  51. * the "status" queries return the current speed while the "setpoint" ones
  52. * return the programmed/target speed. It _seems_ that the result is a bit
  53. * mask in the first byte of active/available fans, followed by 6 words (16
  54. * bits) containing the requested speed.
  55. *
  56. * Setpoint (14 bytes arg):
  57. * ------------------------
  58. *
  59. * first arg byte is 0 for RPM fans and 0x10 for PWM. Second arg byte is the
  60. * mask of fans affected by the command. Followed by 6 words containing the
  61. * setpoint value for selected fans in the mask (or 0 if mask value is 0)
  62. */
  63. #define SMU_CMD_FAN_COMMAND 0x4a
  64. /*
  65. * Battery access
  66. *
  67. * Same command number as the PMU, could it be same syntax ?
  68. */
  69. #define SMU_CMD_BATTERY_COMMAND 0x6f
  70. #define SMU_CMD_GET_BATTERY_INFO 0x00
  71. /*
  72. * Real time clock control
  73. *
  74. * This is a "mux", first data byte contains the "sub" command.
  75. * The "RTC" part of the SMU controls the date, time, powerup
  76. * timer, but also a PRAM
  77. *
  78. * Dates are in BCD format on 7 bytes:
  79. * [sec] [min] [hour] [weekday] [month day] [month] [year]
  80. * with month being 1 based and year minus 100
  81. */
  82. #define SMU_CMD_RTC_COMMAND 0x8e
  83. #define SMU_CMD_RTC_SET_PWRUP_TIMER 0x00 /* i: 7 bytes date */
  84. #define SMU_CMD_RTC_GET_PWRUP_TIMER 0x01 /* o: 7 bytes date */
  85. #define SMU_CMD_RTC_STOP_PWRUP_TIMER 0x02
  86. #define SMU_CMD_RTC_SET_PRAM_BYTE_ACC 0x20 /* i: 1 byte (address?) */
  87. #define SMU_CMD_RTC_SET_PRAM_AUTOINC 0x21 /* i: 1 byte (data?) */
  88. #define SMU_CMD_RTC_SET_PRAM_LO_BYTES 0x22 /* i: 10 bytes */
  89. #define SMU_CMD_RTC_SET_PRAM_HI_BYTES 0x23 /* i: 10 bytes */
  90. #define SMU_CMD_RTC_GET_PRAM_BYTE 0x28 /* i: 1 bytes (address?) */
  91. #define SMU_CMD_RTC_GET_PRAM_LO_BYTES 0x29 /* o: 10 bytes */
  92. #define SMU_CMD_RTC_GET_PRAM_HI_BYTES 0x2a /* o: 10 bytes */
  93. #define SMU_CMD_RTC_SET_DATETIME 0x80 /* i: 7 bytes date */
  94. #define SMU_CMD_RTC_GET_DATETIME 0x81 /* o: 7 bytes date */
  95. /*
  96. * i2c commands
  97. *
  98. * To issue an i2c command, first is to send a parameter block to the
  99. * the SMU. This is a command of type 0x9a with 9 bytes of header
  100. * eventually followed by data for a write:
  101. *
  102. * 0: bus number (from device-tree usually, SMU has lots of busses !)
  103. * 1: transfer type/format (see below)
  104. * 2: device address. For combined and combined4 type transfers, this
  105. * is the "write" version of the address (bit 0x01 cleared)
  106. * 3: subaddress length (0..3)
  107. * 4: subaddress byte 0 (or only byte for subaddress length 1)
  108. * 5: subaddress byte 1
  109. * 6: subaddress byte 2
  110. * 7: combined address (device address for combined mode data phase)
  111. * 8: data length
  112. *
  113. * The transfer types are the same good old Apple ones it seems,
  114. * that is:
  115. * - 0x00: Simple transfer
  116. * - 0x01: Subaddress transfer (addr write + data tx, no restart)
  117. * - 0x02: Combined transfer (addr write + restart + data tx)
  118. *
  119. * This is then followed by actual data for a write.
  120. *
  121. * At this point, the OF driver seems to have a limitation on transfer
  122. * sizes of 0xd bytes on reads and 0x5 bytes on writes. I do not know
  123. * wether this is just an OF limit due to some temporary buffer size
  124. * or if this is an SMU imposed limit. This driver has the same limitation
  125. * for now as I use a 0x10 bytes temporary buffer as well
  126. *
  127. * Once that is completed, a response is expected from the SMU. This is
  128. * obtained via a command of type 0x9a with a length of 1 byte containing
  129. * 0 as the data byte. OF also fills the rest of the data buffer with 0xff's
  130. * though I can't tell yet if this is actually necessary. Once this command
  131. * is complete, at this point, all I can tell is what OF does. OF tests
  132. * byte 0 of the reply:
  133. * - on read, 0xfe or 0xfc : bus is busy, wait (see below) or nak ?
  134. * - on read, 0x00 or 0x01 : reply is in buffer (after the byte 0)
  135. * - on write, < 0 -> failure (immediate exit)
  136. * - else, OF just exists (without error, weird)
  137. *
  138. * So on read, there is this wait-for-busy thing when getting a 0xfc or
  139. * 0xfe result. OF does a loop of up to 64 retries, waiting 20ms and
  140. * doing the above again until either the retries expire or the result
  141. * is no longer 0xfe or 0xfc
  142. *
  143. * The Darwin I2C driver is less subtle though. On any non-success status
  144. * from the response command, it waits 5ms and tries again up to 20 times,
  145. * it doesn't differenciate between fatal errors or "busy" status.
  146. *
  147. * This driver provides an asynchronous paramblock based i2c command
  148. * interface to be used either directly by low level code or by a higher
  149. * level driver interfacing to the linux i2c layer. The current
  150. * implementation of this relies on working timers & timer interrupts
  151. * though, so be careful of calling context for now. This may be "fixed"
  152. * in the future by adding a polling facility.
  153. */
  154. #define SMU_CMD_I2C_COMMAND 0x9a
  155. /* transfer types */
  156. #define SMU_I2C_TRANSFER_SIMPLE 0x00
  157. #define SMU_I2C_TRANSFER_STDSUB 0x01
  158. #define SMU_I2C_TRANSFER_COMBINED 0x02
  159. /*
  160. * Power supply control
  161. *
  162. * The "sub" command is an ASCII string in the data, the
  163. * data lenght is that of the string.
  164. *
  165. * The VSLEW command can be used to get or set the voltage slewing.
  166. * - lenght 5 (only "VSLEW") : it returns "DONE" and 3 bytes of
  167. * reply at data offset 6, 7 and 8.
  168. * - lenght 8 ("VSLEWxyz") has 3 additional bytes appended, and is
  169. * used to set the voltage slewing point. The SMU replies with "DONE"
  170. * I yet have to figure out their exact meaning of those 3 bytes in
  171. * both cases. They seem to be:
  172. * x = processor mask
  173. * y = op. point index
  174. * z = processor freq. step index
  175. * I haven't yet decyphered result codes
  176. *
  177. */
  178. #define SMU_CMD_POWER_COMMAND 0xaa
  179. #define SMU_CMD_POWER_RESTART "RESTART"
  180. #define SMU_CMD_POWER_SHUTDOWN "SHUTDOWN"
  181. #define SMU_CMD_POWER_VOLTAGE_SLEW "VSLEW"
  182. /*
  183. * Read ADC sensors
  184. *
  185. * This command takes one byte of parameter: the sensor ID (or "reg"
  186. * value in the device-tree) and returns a 16 bits value
  187. */
  188. #define SMU_CMD_READ_ADC 0xd8
  189. /* Misc commands
  190. *
  191. * This command seem to be a grab bag of various things
  192. */
  193. #define SMU_CMD_MISC_df_COMMAND 0xdf
  194. #define SMU_CMD_MISC_df_SET_DISPLAY_LIT 0x02 /* i: 1 byte */
  195. #define SMU_CMD_MISC_df_NMI_OPTION 0x04
  196. /*
  197. * Version info commands
  198. *
  199. * I haven't quite tried to figure out how these work
  200. */
  201. #define SMU_CMD_VERSION_COMMAND 0xea
  202. /*
  203. * Misc commands
  204. *
  205. * This command seem to be a grab bag of various things
  206. *
  207. * SMU_CMD_MISC_ee_GET_DATABLOCK_REC is used, among others, to
  208. * transfer blocks of data from the SMU. So far, I've decrypted it's
  209. * usage to retreive partition data. In order to do that, you have to
  210. * break your transfer in "chunks" since that command cannot transfer
  211. * more than a chunk at a time. The chunk size used by OF is 0xe bytes,
  212. * but it seems that the darwin driver will let you do 0x1e bytes if
  213. * your "PMU" version is >= 0x30. You can get the "PMU" version apparently
  214. * either in the last 16 bits of property "smu-version-pmu" or as the 16
  215. * bytes at offset 1 of "smu-version-info"
  216. *
  217. * For each chunk, the command takes 7 bytes of arguments:
  218. * byte 0: subcommand code (0x02)
  219. * byte 1: 0x04 (always, I don't know what it means, maybe the address
  220. * space to use or some other nicety. It's hard coded in OF)
  221. * byte 2..5: SMU address of the chunk (big endian 32 bits)
  222. * byte 6: size to transfer (up to max chunk size)
  223. *
  224. * The data is returned directly
  225. */
  226. #define SMU_CMD_MISC_ee_COMMAND 0xee
  227. #define SMU_CMD_MISC_ee_GET_DATABLOCK_REC 0x02
  228. #define SMU_CMD_MISC_ee_LEDS_CTRL 0x04 /* i: 00 (00,01) [00] */
  229. #define SMU_CMD_MISC_ee_GET_DATA 0x05 /* i: 00 , o: ?? */
  230. /*
  231. * - Kernel side interface -
  232. */
  233. #ifdef __KERNEL__
  234. /*
  235. * Asynchronous SMU commands
  236. *
  237. * Fill up this structure and submit it via smu_queue_command(),
  238. * and get notified by the optional done() callback, or because
  239. * status becomes != 1
  240. */
  241. struct smu_cmd;
  242. struct smu_cmd
  243. {
  244. /* public */
  245. u8 cmd; /* command */
  246. int data_len; /* data len */
  247. int reply_len; /* reply len */
  248. void *data_buf; /* data buffer */
  249. void *reply_buf; /* reply buffer */
  250. int status; /* command status */
  251. void (*done)(struct smu_cmd *cmd, void *misc);
  252. void *misc;
  253. /* private */
  254. struct list_head link;
  255. };
  256. /*
  257. * Queues an SMU command, all fields have to be initialized
  258. */
  259. extern int smu_queue_cmd(struct smu_cmd *cmd);
  260. /*
  261. * Simple command wrapper. This structure embeds a small buffer
  262. * to ease sending simple SMU commands from the stack
  263. */
  264. struct smu_simple_cmd
  265. {
  266. struct smu_cmd cmd;
  267. u8 buffer[16];
  268. };
  269. /*
  270. * Queues a simple command. All fields will be initialized by that
  271. * function
  272. */
  273. extern int smu_queue_simple(struct smu_simple_cmd *scmd, u8 command,
  274. unsigned int data_len,
  275. void (*done)(struct smu_cmd *cmd, void *misc),
  276. void *misc,
  277. ...);
  278. /*
  279. * Completion helper. Pass it to smu_queue_simple or as 'done'
  280. * member to smu_queue_cmd, it will call complete() on the struct
  281. * completion passed in the "misc" argument
  282. */
  283. extern void smu_done_complete(struct smu_cmd *cmd, void *misc);
  284. /*
  285. * Synchronous helpers. Will spin-wait for completion of a command
  286. */
  287. extern void smu_spinwait_cmd(struct smu_cmd *cmd);
  288. static inline void smu_spinwait_simple(struct smu_simple_cmd *scmd)
  289. {
  290. smu_spinwait_cmd(&scmd->cmd);
  291. }
  292. /*
  293. * Poll routine to call if blocked with irqs off
  294. */
  295. extern void smu_poll(void);
  296. /*
  297. * Init routine, presence check....
  298. */
  299. extern int smu_init(void);
  300. extern int smu_present(void);
  301. struct of_device;
  302. extern struct of_device *smu_get_ofdev(void);
  303. /*
  304. * Common command wrappers
  305. */
  306. extern void smu_shutdown(void);
  307. extern void smu_restart(void);
  308. struct rtc_time;
  309. extern int smu_get_rtc_time(struct rtc_time *time, int spinwait);
  310. extern int smu_set_rtc_time(struct rtc_time *time, int spinwait);
  311. /*
  312. * SMU command buffer absolute address, exported by pmac_setup,
  313. * this is allocated very early during boot.
  314. */
  315. extern unsigned long smu_cmdbuf_abs;
  316. /*
  317. * Kenrel asynchronous i2c interface
  318. */
  319. /* SMU i2c header, exactly matches i2c header on wire */
  320. struct smu_i2c_param
  321. {
  322. u8 bus; /* SMU bus ID (from device tree) */
  323. u8 type; /* i2c transfer type */
  324. u8 devaddr; /* device address (includes direction) */
  325. u8 sublen; /* subaddress length */
  326. u8 subaddr[3]; /* subaddress */
  327. u8 caddr; /* combined address, filled by SMU driver */
  328. u8 datalen; /* length of transfer */
  329. u8 data[7]; /* data */
  330. };
  331. #define SMU_I2C_READ_MAX 0x0d
  332. #define SMU_I2C_WRITE_MAX 0x05
  333. struct smu_i2c_cmd
  334. {
  335. /* public */
  336. struct smu_i2c_param info;
  337. void (*done)(struct smu_i2c_cmd *cmd, void *misc);
  338. void *misc;
  339. int status; /* 1 = pending, 0 = ok, <0 = fail */
  340. /* private */
  341. struct smu_cmd scmd;
  342. int read;
  343. int stage;
  344. int retries;
  345. u8 pdata[0x10];
  346. struct list_head link;
  347. };
  348. /*
  349. * Call this to queue an i2c command to the SMU. You must fill info,
  350. * including info.data for a write, done and misc.
  351. * For now, no polling interface is provided so you have to use completion
  352. * callback.
  353. */
  354. extern int smu_queue_i2c(struct smu_i2c_cmd *cmd);
  355. #endif /* __KERNEL__ */
  356. /*
  357. * - SMU "sdb" partitions informations -
  358. */
  359. /*
  360. * Partition header format
  361. */
  362. struct smu_sdbp_header {
  363. __u8 id;
  364. __u8 len;
  365. __u8 version;
  366. __u8 flags;
  367. };
  368. /*
  369. * demangle 16 and 32 bits integer in some SMU partitions
  370. * (currently, afaik, this concerns only the FVT partition
  371. * (0x12)
  372. */
  373. #define SMU_U16_MIX(x) le16_to_cpu(x);
  374. #define SMU_U32_MIX(x) ((((x) & 0xff00ff00u) >> 8)|(((x) & 0x00ff00ffu) << 8))
  375. /* This is the definition of the SMU sdb-partition-0x12 table (called
  376. * CPU F/V/T operating points in Darwin). The definition for all those
  377. * SMU tables should be moved to some separate file
  378. */
  379. #define SMU_SDB_FVT_ID 0x12
  380. struct smu_sdbp_fvt {
  381. __u32 sysclk; /* Base SysClk frequency in Hz for
  382. * this operating point. Value need to
  383. * be unmixed with SMU_U32_MIX()
  384. */
  385. __u8 pad;
  386. __u8 maxtemp; /* Max temp. supported by this
  387. * operating point
  388. */
  389. __u16 volts[3]; /* CPU core voltage for the 3
  390. * PowerTune modes, a mode with
  391. * 0V = not supported. Value need
  392. * to be unmixed with SMU_U16_MIX()
  393. */
  394. };
  395. /* This partition contains voltage & current sensor calibration
  396. * informations
  397. */
  398. #define SMU_SDB_CPUVCP_ID 0x21
  399. struct smu_sdbp_cpuvcp {
  400. __u16 volt_scale; /* u4.12 fixed point */
  401. __s16 volt_offset; /* s4.12 fixed point */
  402. __u16 curr_scale; /* u4.12 fixed point */
  403. __s16 curr_offset; /* s4.12 fixed point */
  404. __s32 power_quads[3]; /* s4.28 fixed point */
  405. };
  406. /* This partition contains CPU thermal diode calibration
  407. */
  408. #define SMU_SDB_CPUDIODE_ID 0x18
  409. struct smu_sdbp_cpudiode {
  410. __u16 m_value; /* u1.15 fixed point */
  411. __s16 b_value; /* s10.6 fixed point */
  412. };
  413. /* This partition contains Slots power calibration
  414. */
  415. #define SMU_SDB_SLOTSPOW_ID 0x78
  416. struct smu_sdbp_slotspow {
  417. __u16 pow_scale; /* u4.12 fixed point */
  418. __s16 pow_offset; /* s4.12 fixed point */
  419. };
  420. /* This partition contains machine specific version information about
  421. * the sensor/control layout
  422. */
  423. #define SMU_SDB_SENSORTREE_ID 0x25
  424. struct smu_sdbp_sensortree {
  425. u8 model_id;
  426. u8 unknown[3];
  427. };
  428. /* This partition contains CPU thermal control PID informations. So far
  429. * only single CPU machines have been seen with an SMU, so we assume this
  430. * carries only informations for those
  431. */
  432. #define SMU_SDB_CPUPIDDATA_ID 0x17
  433. struct smu_sdbp_cpupiddata {
  434. u8 unknown1;
  435. u8 target_temp_delta;
  436. u8 unknown2;
  437. u8 history_len;
  438. s16 power_adj;
  439. u16 max_power;
  440. s32 gp,gr,gd;
  441. };
  442. /* Other partitions without known structures */
  443. #define SMU_SDB_DEBUG_SWITCHES_ID 0x05
  444. #ifdef __KERNEL__
  445. /*
  446. * This returns the pointer to an SMU "sdb" partition data or NULL
  447. * if not found. The data format is described below
  448. */
  449. extern struct smu_sdbp_header *smu_get_sdb_partition(int id,
  450. unsigned int *size);
  451. #endif /* __KERNEL__ */
  452. /*
  453. * - Userland interface -
  454. */
  455. /*
  456. * A given instance of the device can be configured for 2 different
  457. * things at the moment:
  458. *
  459. * - sending SMU commands (default at open() time)
  460. * - receiving SMU events (not yet implemented)
  461. *
  462. * Commands are written with write() of a command block. They can be
  463. * "driver" commands (for example to switch to event reception mode)
  464. * or real SMU commands. They are made of a header followed by command
  465. * data if any.
  466. *
  467. * For SMU commands (not for driver commands), you can then read() back
  468. * a reply. The reader will be blocked or not depending on how the device
  469. * file is opened. poll() isn't implemented yet. The reply will consist
  470. * of a header as well, followed by the reply data if any. You should
  471. * always provide a buffer large enough for the maximum reply data, I
  472. * recommand one page.
  473. *
  474. * It is illegal to send SMU commands through a file descriptor configured
  475. * for events reception
  476. *
  477. */
  478. struct smu_user_cmd_hdr
  479. {
  480. __u32 cmdtype;
  481. #define SMU_CMDTYPE_SMU 0 /* SMU command */
  482. #define SMU_CMDTYPE_WANTS_EVENTS 1 /* switch fd to events mode */
  483. #define SMU_CMDTYPE_GET_PARTITION 2 /* retreive an sdb partition */
  484. __u8 cmd; /* SMU command byte */
  485. __u8 pad[3]; /* padding */
  486. __u32 data_len; /* Lenght of data following */
  487. };
  488. struct smu_user_reply_hdr
  489. {
  490. __u32 status; /* Command status */
  491. __u32 reply_len; /* Lenght of data follwing */
  492. };
  493. #endif /* _SMU_H */