smu.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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.
  136. *
  137. */
  138. #define SMU_CMD_POWER_COMMAND 0xaa
  139. #define SMU_CMD_POWER_RESTART "RESTART"
  140. #define SMU_CMD_POWER_SHUTDOWN "SHUTDOWN"
  141. #define SMU_CMD_POWER_VOLTAGE_SLEW "VSLEW"
  142. /* Misc commands
  143. *
  144. * This command seem to be a grab bag of various things
  145. */
  146. #define SMU_CMD_MISC_df_COMMAND 0xdf
  147. #define SMU_CMD_MISC_df_SET_DISPLAY_LIT 0x02 /* i: 1 byte */
  148. #define SMU_CMD_MISC_df_NMI_OPTION 0x04
  149. /*
  150. * Version info commands
  151. *
  152. * I haven't quite tried to figure out how these work
  153. */
  154. #define SMU_CMD_VERSION_COMMAND 0xea
  155. /*
  156. * Misc commands
  157. *
  158. * This command seem to be a grab bag of various things
  159. */
  160. #define SMU_CMD_MISC_ee_COMMAND 0xee
  161. #define SMU_CMD_MISC_ee_GET_DATABLOCK_REC 0x02
  162. #define SMU_CMD_MISC_ee_LEDS_CTRL 0x04 /* i: 00 (00,01) [00] */
  163. #define SMU_CMD_MISC_ee_GET_DATA 0x05 /* i: 00 , o: ?? */
  164. /*
  165. * - Kernel side interface -
  166. */
  167. #ifdef __KERNEL__
  168. /*
  169. * Asynchronous SMU commands
  170. *
  171. * Fill up this structure and submit it via smu_queue_command(),
  172. * and get notified by the optional done() callback, or because
  173. * status becomes != 1
  174. */
  175. struct smu_cmd;
  176. struct smu_cmd
  177. {
  178. /* public */
  179. u8 cmd; /* command */
  180. int data_len; /* data len */
  181. int reply_len; /* reply len */
  182. void *data_buf; /* data buffer */
  183. void *reply_buf; /* reply buffer */
  184. int status; /* command status */
  185. void (*done)(struct smu_cmd *cmd, void *misc);
  186. void *misc;
  187. /* private */
  188. struct list_head link;
  189. };
  190. /*
  191. * Queues an SMU command, all fields have to be initialized
  192. */
  193. extern int smu_queue_cmd(struct smu_cmd *cmd);
  194. /*
  195. * Simple command wrapper. This structure embeds a small buffer
  196. * to ease sending simple SMU commands from the stack
  197. */
  198. struct smu_simple_cmd
  199. {
  200. struct smu_cmd cmd;
  201. u8 buffer[16];
  202. };
  203. /*
  204. * Queues a simple command. All fields will be initialized by that
  205. * function
  206. */
  207. extern int smu_queue_simple(struct smu_simple_cmd *scmd, u8 command,
  208. unsigned int data_len,
  209. void (*done)(struct smu_cmd *cmd, void *misc),
  210. void *misc,
  211. ...);
  212. /*
  213. * Completion helper. Pass it to smu_queue_simple or as 'done'
  214. * member to smu_queue_cmd, it will call complete() on the struct
  215. * completion passed in the "misc" argument
  216. */
  217. extern void smu_done_complete(struct smu_cmd *cmd, void *misc);
  218. /*
  219. * Synchronous helpers. Will spin-wait for completion of a command
  220. */
  221. extern void smu_spinwait_cmd(struct smu_cmd *cmd);
  222. static inline void smu_spinwait_simple(struct smu_simple_cmd *scmd)
  223. {
  224. smu_spinwait_cmd(&scmd->cmd);
  225. }
  226. /*
  227. * Poll routine to call if blocked with irqs off
  228. */
  229. extern void smu_poll(void);
  230. /*
  231. * Init routine, presence check....
  232. */
  233. extern int smu_init(void);
  234. extern int smu_present(void);
  235. struct of_device;
  236. extern struct of_device *smu_get_ofdev(void);
  237. /*
  238. * Common command wrappers
  239. */
  240. extern void smu_shutdown(void);
  241. extern void smu_restart(void);
  242. struct rtc_time;
  243. extern int smu_get_rtc_time(struct rtc_time *time, int spinwait);
  244. extern int smu_set_rtc_time(struct rtc_time *time, int spinwait);
  245. /*
  246. * SMU command buffer absolute address, exported by pmac_setup,
  247. * this is allocated very early during boot.
  248. */
  249. extern unsigned long smu_cmdbuf_abs;
  250. /*
  251. * Kenrel asynchronous i2c interface
  252. */
  253. /* SMU i2c header, exactly matches i2c header on wire */
  254. struct smu_i2c_param
  255. {
  256. u8 bus; /* SMU bus ID (from device tree) */
  257. u8 type; /* i2c transfer type */
  258. u8 devaddr; /* device address (includes direction) */
  259. u8 sublen; /* subaddress length */
  260. u8 subaddr[3]; /* subaddress */
  261. u8 caddr; /* combined address, filled by SMU driver */
  262. u8 datalen; /* length of transfer */
  263. u8 data[7]; /* data */
  264. };
  265. #define SMU_I2C_READ_MAX 0x0d
  266. #define SMU_I2C_WRITE_MAX 0x05
  267. struct smu_i2c_cmd
  268. {
  269. /* public */
  270. struct smu_i2c_param info;
  271. void (*done)(struct smu_i2c_cmd *cmd, void *misc);
  272. void *misc;
  273. int status; /* 1 = pending, 0 = ok, <0 = fail */
  274. /* private */
  275. struct smu_cmd scmd;
  276. int read;
  277. int stage;
  278. int retries;
  279. u8 pdata[0x10];
  280. struct list_head link;
  281. };
  282. /*
  283. * Call this to queue an i2c command to the SMU. You must fill info,
  284. * including info.data for a write, done and misc.
  285. * For now, no polling interface is provided so you have to use completion
  286. * callback.
  287. */
  288. extern int smu_queue_i2c(struct smu_i2c_cmd *cmd);
  289. #endif /* __KERNEL__ */
  290. /*
  291. * - Userland interface -
  292. */
  293. /*
  294. * A given instance of the device can be configured for 2 different
  295. * things at the moment:
  296. *
  297. * - sending SMU commands (default at open() time)
  298. * - receiving SMU events (not yet implemented)
  299. *
  300. * Commands are written with write() of a command block. They can be
  301. * "driver" commands (for example to switch to event reception mode)
  302. * or real SMU commands. They are made of a header followed by command
  303. * data if any.
  304. *
  305. * For SMU commands (not for driver commands), you can then read() back
  306. * a reply. The reader will be blocked or not depending on how the device
  307. * file is opened. poll() isn't implemented yet. The reply will consist
  308. * of a header as well, followed by the reply data if any. You should
  309. * always provide a buffer large enough for the maximum reply data, I
  310. * recommand one page.
  311. *
  312. * It is illegal to send SMU commands through a file descriptor configured
  313. * for events reception
  314. *
  315. */
  316. struct smu_user_cmd_hdr
  317. {
  318. __u32 cmdtype;
  319. #define SMU_CMDTYPE_SMU 0 /* SMU command */
  320. #define SMU_CMDTYPE_WANTS_EVENTS 1 /* switch fd to events mode */
  321. __u8 cmd; /* SMU command byte */
  322. __u32 data_len; /* Lenght of data following */
  323. };
  324. struct smu_user_reply_hdr
  325. {
  326. __u32 status; /* Command status */
  327. __u32 reply_len; /* Lenght of data follwing */
  328. };
  329. #endif /* _SMU_H */