smu.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. * I do not know what those are for at this point
  19. */
  20. #define SMU_CMD_PARTITION_COMMAND 0x3e
  21. /*
  22. * Fan control
  23. *
  24. * This is a "mux" for fan control commands, first byte is the
  25. * "sub" command.
  26. */
  27. #define SMU_CMD_FAN_COMMAND 0x4a
  28. /*
  29. * Battery access
  30. *
  31. * Same command number as the PMU, could it be same syntax ?
  32. */
  33. #define SMU_CMD_BATTERY_COMMAND 0x6f
  34. #define SMU_CMD_GET_BATTERY_INFO 0x00
  35. /*
  36. * Real time clock control
  37. *
  38. * This is a "mux", first data byte contains the "sub" command.
  39. * The "RTC" part of the SMU controls the date, time, powerup
  40. * timer, but also a PRAM
  41. *
  42. * Dates are in BCD format on 7 bytes:
  43. * [sec] [min] [hour] [weekday] [month day] [month] [year]
  44. * with month being 1 based and year minus 100
  45. */
  46. #define SMU_CMD_RTC_COMMAND 0x8e
  47. #define SMU_CMD_RTC_SET_PWRUP_TIMER 0x00 /* i: 7 bytes date */
  48. #define SMU_CMD_RTC_GET_PWRUP_TIMER 0x01 /* o: 7 bytes date */
  49. #define SMU_CMD_RTC_STOP_PWRUP_TIMER 0x02
  50. #define SMU_CMD_RTC_SET_PRAM_BYTE_ACC 0x20 /* i: 1 byte (address?) */
  51. #define SMU_CMD_RTC_SET_PRAM_AUTOINC 0x21 /* i: 1 byte (data?) */
  52. #define SMU_CMD_RTC_SET_PRAM_LO_BYTES 0x22 /* i: 10 bytes */
  53. #define SMU_CMD_RTC_SET_PRAM_HI_BYTES 0x23 /* i: 10 bytes */
  54. #define SMU_CMD_RTC_GET_PRAM_BYTE 0x28 /* i: 1 bytes (address?) */
  55. #define SMU_CMD_RTC_GET_PRAM_LO_BYTES 0x29 /* o: 10 bytes */
  56. #define SMU_CMD_RTC_GET_PRAM_HI_BYTES 0x2a /* o: 10 bytes */
  57. #define SMU_CMD_RTC_SET_DATETIME 0x80 /* i: 7 bytes date */
  58. #define SMU_CMD_RTC_GET_DATETIME 0x81 /* o: 7 bytes date */
  59. /*
  60. * i2c commands
  61. *
  62. * To issue an i2c command, first is to send a parameter block to the
  63. * the SMU. This is a command of type 0x9a with 9 bytes of header
  64. * eventually followed by data for a write:
  65. *
  66. * 0: bus number (from device-tree usually, SMU has lots of busses !)
  67. * 1: transfer type/format (see below)
  68. * 2: device address. For combined and combined4 type transfers, this
  69. * is the "write" version of the address (bit 0x01 cleared)
  70. * 3: subaddress length (0..3)
  71. * 4: subaddress byte 0 (or only byte for subaddress length 1)
  72. * 5: subaddress byte 1
  73. * 6: subaddress byte 2
  74. * 7: combined address (device address for combined mode data phase)
  75. * 8: data length
  76. *
  77. * The transfer types are the same good old Apple ones it seems,
  78. * that is:
  79. * - 0x00: Simple transfer
  80. * - 0x01: Subaddress transfer (addr write + data tx, no restart)
  81. * - 0x02: Combined transfer (addr write + restart + data tx)
  82. *
  83. * This is then followed by actual data for a write.
  84. *
  85. * At this point, the OF driver seems to have a limitation on transfer
  86. * sizes of 0xd bytes on reads and 0x5 bytes on writes. I do not know
  87. * wether this is just an OF limit due to some temporary buffer size
  88. * or if this is an SMU imposed limit. This driver has the same limitation
  89. * for now as I use a 0x10 bytes temporary buffer as well
  90. *
  91. * Once that is completed, a response is expected from the SMU. This is
  92. * obtained via a command of type 0x9a with a length of 1 byte containing
  93. * 0 as the data byte. OF also fills the rest of the data buffer with 0xff's
  94. * though I can't tell yet if this is actually necessary. Once this command
  95. * is complete, at this point, all I can tell is what OF does. OF tests
  96. * byte 0 of the reply:
  97. * - on read, 0xfe or 0xfc : bus is busy, wait (see below) or nak ?
  98. * - on read, 0x00 or 0x01 : reply is in buffer (after the byte 0)
  99. * - on write, < 0 -> failure (immediate exit)
  100. * - else, OF just exists (without error, weird)
  101. *
  102. * So on read, there is this wait-for-busy thing when getting a 0xfc or
  103. * 0xfe result. OF does a loop of up to 64 retries, waiting 20ms and
  104. * doing the above again until either the retries expire or the result
  105. * is no longer 0xfe or 0xfc
  106. *
  107. * The Darwin I2C driver is less subtle though. On any non-success status
  108. * from the response command, it waits 5ms and tries again up to 20 times,
  109. * it doesn't differenciate between fatal errors or "busy" status.
  110. *
  111. * This driver provides an asynchronous paramblock based i2c command
  112. * interface to be used either directly by low level code or by a higher
  113. * level driver interfacing to the linux i2c layer. The current
  114. * implementation of this relies on working timers & timer interrupts
  115. * though, so be careful of calling context for now. This may be "fixed"
  116. * in the future by adding a polling facility.
  117. */
  118. #define SMU_CMD_I2C_COMMAND 0x9a
  119. /* transfer types */
  120. #define SMU_I2C_TRANSFER_SIMPLE 0x00
  121. #define SMU_I2C_TRANSFER_STDSUB 0x01
  122. #define SMU_I2C_TRANSFER_COMBINED 0x02
  123. /*
  124. * Power supply control
  125. *
  126. * The "sub" command is an ASCII string in the data, the
  127. * data lenght is that of the string.
  128. *
  129. * The VSLEW command can be used to get or set the voltage slewing.
  130. * - lenght 5 (only "VSLEW") : it returns "DONE" and 3 bytes of
  131. * reply at data offset 6, 7 and 8.
  132. * - lenght 8 ("VSLEWxyz") has 3 additional bytes appended, and is
  133. * used to set the voltage slewing point. The SMU replies with "DONE"
  134. * I yet have to figure out their exact meaning of those 3 bytes in
  135. * both cases. They seem to be:
  136. * x = processor mask
  137. * y = op. point index
  138. * z = processor freq. step index
  139. * I haven't yet decyphered result codes
  140. *
  141. */
  142. #define SMU_CMD_POWER_COMMAND 0xaa
  143. #define SMU_CMD_POWER_RESTART "RESTART"
  144. #define SMU_CMD_POWER_SHUTDOWN "SHUTDOWN"
  145. #define SMU_CMD_POWER_VOLTAGE_SLEW "VSLEW"
  146. /* Misc commands
  147. *
  148. * This command seem to be a grab bag of various things
  149. */
  150. #define SMU_CMD_MISC_df_COMMAND 0xdf
  151. #define SMU_CMD_MISC_df_SET_DISPLAY_LIT 0x02 /* i: 1 byte */
  152. #define SMU_CMD_MISC_df_NMI_OPTION 0x04
  153. /*
  154. * Version info commands
  155. *
  156. * I haven't quite tried to figure out how these work
  157. */
  158. #define SMU_CMD_VERSION_COMMAND 0xea
  159. /*
  160. * Misc commands
  161. *
  162. * This command seem to be a grab bag of various things
  163. */
  164. #define SMU_CMD_MISC_ee_COMMAND 0xee
  165. #define SMU_CMD_MISC_ee_GET_DATABLOCK_REC 0x02
  166. #define SMU_CMD_MISC_ee_LEDS_CTRL 0x04 /* i: 00 (00,01) [00] */
  167. #define SMU_CMD_MISC_ee_GET_DATA 0x05 /* i: 00 , o: ?? */
  168. /*
  169. * - Kernel side interface -
  170. */
  171. #ifdef __KERNEL__
  172. /*
  173. * Asynchronous SMU commands
  174. *
  175. * Fill up this structure and submit it via smu_queue_command(),
  176. * and get notified by the optional done() callback, or because
  177. * status becomes != 1
  178. */
  179. struct smu_cmd;
  180. struct smu_cmd
  181. {
  182. /* public */
  183. u8 cmd; /* command */
  184. int data_len; /* data len */
  185. int reply_len; /* reply len */
  186. void *data_buf; /* data buffer */
  187. void *reply_buf; /* reply buffer */
  188. int status; /* command status */
  189. void (*done)(struct smu_cmd *cmd, void *misc);
  190. void *misc;
  191. /* private */
  192. struct list_head link;
  193. };
  194. /*
  195. * Queues an SMU command, all fields have to be initialized
  196. */
  197. extern int smu_queue_cmd(struct smu_cmd *cmd);
  198. /*
  199. * Simple command wrapper. This structure embeds a small buffer
  200. * to ease sending simple SMU commands from the stack
  201. */
  202. struct smu_simple_cmd
  203. {
  204. struct smu_cmd cmd;
  205. u8 buffer[16];
  206. };
  207. /*
  208. * Queues a simple command. All fields will be initialized by that
  209. * function
  210. */
  211. extern int smu_queue_simple(struct smu_simple_cmd *scmd, u8 command,
  212. unsigned int data_len,
  213. void (*done)(struct smu_cmd *cmd, void *misc),
  214. void *misc,
  215. ...);
  216. /*
  217. * Completion helper. Pass it to smu_queue_simple or as 'done'
  218. * member to smu_queue_cmd, it will call complete() on the struct
  219. * completion passed in the "misc" argument
  220. */
  221. extern void smu_done_complete(struct smu_cmd *cmd, void *misc);
  222. /*
  223. * Synchronous helpers. Will spin-wait for completion of a command
  224. */
  225. extern void smu_spinwait_cmd(struct smu_cmd *cmd);
  226. static inline void smu_spinwait_simple(struct smu_simple_cmd *scmd)
  227. {
  228. smu_spinwait_cmd(&scmd->cmd);
  229. }
  230. /*
  231. * Poll routine to call if blocked with irqs off
  232. */
  233. extern void smu_poll(void);
  234. /*
  235. * Init routine, presence check....
  236. */
  237. extern int smu_init(void);
  238. extern int smu_present(void);
  239. struct of_device;
  240. extern struct of_device *smu_get_ofdev(void);
  241. /*
  242. * Common command wrappers
  243. */
  244. extern void smu_shutdown(void);
  245. extern void smu_restart(void);
  246. struct rtc_time;
  247. extern int smu_get_rtc_time(struct rtc_time *time, int spinwait);
  248. extern int smu_set_rtc_time(struct rtc_time *time, int spinwait);
  249. /*
  250. * SMU command buffer absolute address, exported by pmac_setup,
  251. * this is allocated very early during boot.
  252. */
  253. extern unsigned long smu_cmdbuf_abs;
  254. /*
  255. * Kenrel asynchronous i2c interface
  256. */
  257. /* SMU i2c header, exactly matches i2c header on wire */
  258. struct smu_i2c_param
  259. {
  260. u8 bus; /* SMU bus ID (from device tree) */
  261. u8 type; /* i2c transfer type */
  262. u8 devaddr; /* device address (includes direction) */
  263. u8 sublen; /* subaddress length */
  264. u8 subaddr[3]; /* subaddress */
  265. u8 caddr; /* combined address, filled by SMU driver */
  266. u8 datalen; /* length of transfer */
  267. u8 data[7]; /* data */
  268. };
  269. #define SMU_I2C_READ_MAX 0x0d
  270. #define SMU_I2C_WRITE_MAX 0x05
  271. struct smu_i2c_cmd
  272. {
  273. /* public */
  274. struct smu_i2c_param info;
  275. void (*done)(struct smu_i2c_cmd *cmd, void *misc);
  276. void *misc;
  277. int status; /* 1 = pending, 0 = ok, <0 = fail */
  278. /* private */
  279. struct smu_cmd scmd;
  280. int read;
  281. int stage;
  282. int retries;
  283. u8 pdata[0x10];
  284. struct list_head link;
  285. };
  286. /*
  287. * Call this to queue an i2c command to the SMU. You must fill info,
  288. * including info.data for a write, done and misc.
  289. * For now, no polling interface is provided so you have to use completion
  290. * callback.
  291. */
  292. extern int smu_queue_i2c(struct smu_i2c_cmd *cmd);
  293. #endif /* __KERNEL__ */
  294. /*
  295. * - SMU "sdb" partitions informations -
  296. */
  297. /*
  298. * Partition header format
  299. */
  300. struct smu_sdbp_header {
  301. __u8 id;
  302. __u8 len;
  303. __u8 version;
  304. __u8 flags;
  305. };
  306. /*
  307. * 32 bits integers are usually encoded with 2x16 bits swapped,
  308. * this demangles them
  309. */
  310. #define SMU_U32_MIX(x) ((((x) << 16) & 0xffff0000u) | (((x) >> 16) & 0xffffu))
  311. /* This is the definition of the SMU sdb-partition-0x12 table (called
  312. * CPU F/V/T operating points in Darwin). The definition for all those
  313. * SMU tables should be moved to some separate file
  314. */
  315. #define SMU_SDB_FVT_ID 0x12
  316. struct smu_sdbp_fvt {
  317. __u32 sysclk; /* Base SysClk frequency in Hz for
  318. * this operating point
  319. */
  320. __u8 pad;
  321. __u8 maxtemp; /* Max temp. supported by this
  322. * operating point
  323. */
  324. __u16 volts[3]; /* CPU core voltage for the 3
  325. * PowerTune modes, a mode with
  326. * 0V = not supported.
  327. */
  328. };
  329. #ifdef __KERNEL__
  330. /*
  331. * This returns the pointer to an SMU "sdb" partition data or NULL
  332. * if not found. The data format is described below
  333. */
  334. extern struct smu_sdbp_header *smu_get_sdb_partition(int id,
  335. unsigned int *size);
  336. #endif /* __KERNEL__ */
  337. /*
  338. * - Userland interface -
  339. */
  340. /*
  341. * A given instance of the device can be configured for 2 different
  342. * things at the moment:
  343. *
  344. * - sending SMU commands (default at open() time)
  345. * - receiving SMU events (not yet implemented)
  346. *
  347. * Commands are written with write() of a command block. They can be
  348. * "driver" commands (for example to switch to event reception mode)
  349. * or real SMU commands. They are made of a header followed by command
  350. * data if any.
  351. *
  352. * For SMU commands (not for driver commands), you can then read() back
  353. * a reply. The reader will be blocked or not depending on how the device
  354. * file is opened. poll() isn't implemented yet. The reply will consist
  355. * of a header as well, followed by the reply data if any. You should
  356. * always provide a buffer large enough for the maximum reply data, I
  357. * recommand one page.
  358. *
  359. * It is illegal to send SMU commands through a file descriptor configured
  360. * for events reception
  361. *
  362. */
  363. struct smu_user_cmd_hdr
  364. {
  365. __u32 cmdtype;
  366. #define SMU_CMDTYPE_SMU 0 /* SMU command */
  367. #define SMU_CMDTYPE_WANTS_EVENTS 1 /* switch fd to events mode */
  368. __u8 cmd; /* SMU command byte */
  369. __u32 data_len; /* Lenght of data following */
  370. };
  371. struct smu_user_reply_hdr
  372. {
  373. __u32 status; /* Command status */
  374. __u32 reply_len; /* Lenght of data follwing */
  375. };
  376. #endif /* _SMU_H */