smu.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*
  2. * PowerMac G5 SMU driver
  3. *
  4. * Copyright 2004 J. Mayer <l_indien@magic.fr>
  5. * Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
  6. *
  7. * Released under the term of the GNU GPL v2.
  8. */
  9. /*
  10. * TODO:
  11. * - maybe add timeout to commands ?
  12. * - blocking version of time functions
  13. * - polling version of i2c commands (including timer that works with
  14. * interrutps off)
  15. * - maybe avoid some data copies with i2c by directly using the smu cmd
  16. * buffer and a lower level internal interface
  17. * - understand SMU -> CPU events and implement reception of them via
  18. * the userland interface
  19. */
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/device.h>
  23. #include <linux/dmapool.h>
  24. #include <linux/bootmem.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/highmem.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/rtc.h>
  30. #include <linux/completion.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/delay.h>
  33. #include <linux/sysdev.h>
  34. #include <linux/poll.h>
  35. #include <linux/mutex.h>
  36. #include <asm/byteorder.h>
  37. #include <asm/io.h>
  38. #include <asm/prom.h>
  39. #include <asm/machdep.h>
  40. #include <asm/pmac_feature.h>
  41. #include <asm/smu.h>
  42. #include <asm/sections.h>
  43. #include <asm/abs_addr.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/of_device.h>
  46. #define VERSION "0.7"
  47. #define AUTHOR "(c) 2005 Benjamin Herrenschmidt, IBM Corp."
  48. #undef DEBUG_SMU
  49. #ifdef DEBUG_SMU
  50. #define DPRINTK(fmt, args...) do { printk(KERN_DEBUG fmt , ##args); } while (0)
  51. #else
  52. #define DPRINTK(fmt, args...) do { } while (0)
  53. #endif
  54. /*
  55. * This is the command buffer passed to the SMU hardware
  56. */
  57. #define SMU_MAX_DATA 254
  58. struct smu_cmd_buf {
  59. u8 cmd;
  60. u8 length;
  61. u8 data[SMU_MAX_DATA];
  62. };
  63. struct smu_device {
  64. spinlock_t lock;
  65. struct device_node *of_node;
  66. struct of_device *of_dev;
  67. int doorbell; /* doorbell gpio */
  68. u32 __iomem *db_buf; /* doorbell buffer */
  69. int db_irq;
  70. int msg;
  71. int msg_irq;
  72. struct smu_cmd_buf *cmd_buf; /* command buffer virtual */
  73. u32 cmd_buf_abs; /* command buffer absolute */
  74. struct list_head cmd_list;
  75. struct smu_cmd *cmd_cur; /* pending command */
  76. struct list_head cmd_i2c_list;
  77. struct smu_i2c_cmd *cmd_i2c_cur; /* pending i2c command */
  78. struct timer_list i2c_timer;
  79. };
  80. /*
  81. * I don't think there will ever be more than one SMU, so
  82. * for now, just hard code that
  83. */
  84. static struct smu_device *smu;
  85. static DEFINE_MUTEX(smu_part_access);
  86. static void smu_i2c_retry(unsigned long data);
  87. /*
  88. * SMU driver low level stuff
  89. */
  90. static void smu_start_cmd(void)
  91. {
  92. unsigned long faddr, fend;
  93. struct smu_cmd *cmd;
  94. if (list_empty(&smu->cmd_list))
  95. return;
  96. /* Fetch first command in queue */
  97. cmd = list_entry(smu->cmd_list.next, struct smu_cmd, link);
  98. smu->cmd_cur = cmd;
  99. list_del(&cmd->link);
  100. DPRINTK("SMU: starting cmd %x, %d bytes data\n", cmd->cmd,
  101. cmd->data_len);
  102. DPRINTK("SMU: data buffer: %02x %02x %02x %02x %02x %02x %02x %02x\n",
  103. ((u8 *)cmd->data_buf)[0], ((u8 *)cmd->data_buf)[1],
  104. ((u8 *)cmd->data_buf)[2], ((u8 *)cmd->data_buf)[3],
  105. ((u8 *)cmd->data_buf)[4], ((u8 *)cmd->data_buf)[5],
  106. ((u8 *)cmd->data_buf)[6], ((u8 *)cmd->data_buf)[7]);
  107. /* Fill the SMU command buffer */
  108. smu->cmd_buf->cmd = cmd->cmd;
  109. smu->cmd_buf->length = cmd->data_len;
  110. memcpy(smu->cmd_buf->data, cmd->data_buf, cmd->data_len);
  111. /* Flush command and data to RAM */
  112. faddr = (unsigned long)smu->cmd_buf;
  113. fend = faddr + smu->cmd_buf->length + 2;
  114. flush_inval_dcache_range(faddr, fend);
  115. /* This isn't exactly a DMA mapping here, I suspect
  116. * the SMU is actually communicating with us via i2c to the
  117. * northbridge or the CPU to access RAM.
  118. */
  119. writel(smu->cmd_buf_abs, smu->db_buf);
  120. /* Ring the SMU doorbell */
  121. pmac_do_feature_call(PMAC_FTR_WRITE_GPIO, NULL, smu->doorbell, 4);
  122. }
  123. static irqreturn_t smu_db_intr(int irq, void *arg, struct pt_regs *regs)
  124. {
  125. unsigned long flags;
  126. struct smu_cmd *cmd;
  127. void (*done)(struct smu_cmd *cmd, void *misc) = NULL;
  128. void *misc = NULL;
  129. u8 gpio;
  130. int rc = 0;
  131. /* SMU completed the command, well, we hope, let's make sure
  132. * of it
  133. */
  134. spin_lock_irqsave(&smu->lock, flags);
  135. gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell);
  136. if ((gpio & 7) != 7) {
  137. spin_unlock_irqrestore(&smu->lock, flags);
  138. return IRQ_HANDLED;
  139. }
  140. cmd = smu->cmd_cur;
  141. smu->cmd_cur = NULL;
  142. if (cmd == NULL)
  143. goto bail;
  144. if (rc == 0) {
  145. unsigned long faddr;
  146. int reply_len;
  147. u8 ack;
  148. /* CPU might have brought back the cache line, so we need
  149. * to flush again before peeking at the SMU response. We
  150. * flush the entire buffer for now as we haven't read the
  151. * reply lenght (it's only 2 cache lines anyway)
  152. */
  153. faddr = (unsigned long)smu->cmd_buf;
  154. flush_inval_dcache_range(faddr, faddr + 256);
  155. /* Now check ack */
  156. ack = (~cmd->cmd) & 0xff;
  157. if (ack != smu->cmd_buf->cmd) {
  158. DPRINTK("SMU: incorrect ack, want %x got %x\n",
  159. ack, smu->cmd_buf->cmd);
  160. rc = -EIO;
  161. }
  162. reply_len = rc == 0 ? smu->cmd_buf->length : 0;
  163. DPRINTK("SMU: reply len: %d\n", reply_len);
  164. if (reply_len > cmd->reply_len) {
  165. printk(KERN_WARNING "SMU: reply buffer too small,"
  166. "got %d bytes for a %d bytes buffer\n",
  167. reply_len, cmd->reply_len);
  168. reply_len = cmd->reply_len;
  169. }
  170. cmd->reply_len = reply_len;
  171. if (cmd->reply_buf && reply_len)
  172. memcpy(cmd->reply_buf, smu->cmd_buf->data, reply_len);
  173. }
  174. /* Now complete the command. Write status last in order as we lost
  175. * ownership of the command structure as soon as it's no longer -1
  176. */
  177. done = cmd->done;
  178. misc = cmd->misc;
  179. mb();
  180. cmd->status = rc;
  181. bail:
  182. /* Start next command if any */
  183. smu_start_cmd();
  184. spin_unlock_irqrestore(&smu->lock, flags);
  185. /* Call command completion handler if any */
  186. if (done)
  187. done(cmd, misc);
  188. /* It's an edge interrupt, nothing to do */
  189. return IRQ_HANDLED;
  190. }
  191. static irqreturn_t smu_msg_intr(int irq, void *arg, struct pt_regs *regs)
  192. {
  193. /* I don't quite know what to do with this one, we seem to never
  194. * receive it, so I suspect we have to arm it someway in the SMU
  195. * to start getting events that way.
  196. */
  197. printk(KERN_INFO "SMU: message interrupt !\n");
  198. /* It's an edge interrupt, nothing to do */
  199. return IRQ_HANDLED;
  200. }
  201. /*
  202. * Queued command management.
  203. *
  204. */
  205. int smu_queue_cmd(struct smu_cmd *cmd)
  206. {
  207. unsigned long flags;
  208. if (smu == NULL)
  209. return -ENODEV;
  210. if (cmd->data_len > SMU_MAX_DATA ||
  211. cmd->reply_len > SMU_MAX_DATA)
  212. return -EINVAL;
  213. cmd->status = 1;
  214. spin_lock_irqsave(&smu->lock, flags);
  215. list_add_tail(&cmd->link, &smu->cmd_list);
  216. if (smu->cmd_cur == NULL)
  217. smu_start_cmd();
  218. spin_unlock_irqrestore(&smu->lock, flags);
  219. return 0;
  220. }
  221. EXPORT_SYMBOL(smu_queue_cmd);
  222. int smu_queue_simple(struct smu_simple_cmd *scmd, u8 command,
  223. unsigned int data_len,
  224. void (*done)(struct smu_cmd *cmd, void *misc),
  225. void *misc, ...)
  226. {
  227. struct smu_cmd *cmd = &scmd->cmd;
  228. va_list list;
  229. int i;
  230. if (data_len > sizeof(scmd->buffer))
  231. return -EINVAL;
  232. memset(scmd, 0, sizeof(*scmd));
  233. cmd->cmd = command;
  234. cmd->data_len = data_len;
  235. cmd->data_buf = scmd->buffer;
  236. cmd->reply_len = sizeof(scmd->buffer);
  237. cmd->reply_buf = scmd->buffer;
  238. cmd->done = done;
  239. cmd->misc = misc;
  240. va_start(list, misc);
  241. for (i = 0; i < data_len; ++i)
  242. scmd->buffer[i] = (u8)va_arg(list, int);
  243. va_end(list);
  244. return smu_queue_cmd(cmd);
  245. }
  246. EXPORT_SYMBOL(smu_queue_simple);
  247. void smu_poll(void)
  248. {
  249. u8 gpio;
  250. if (smu == NULL)
  251. return;
  252. gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell);
  253. if ((gpio & 7) == 7)
  254. smu_db_intr(smu->db_irq, smu, NULL);
  255. }
  256. EXPORT_SYMBOL(smu_poll);
  257. void smu_done_complete(struct smu_cmd *cmd, void *misc)
  258. {
  259. struct completion *comp = misc;
  260. complete(comp);
  261. }
  262. EXPORT_SYMBOL(smu_done_complete);
  263. void smu_spinwait_cmd(struct smu_cmd *cmd)
  264. {
  265. while(cmd->status == 1)
  266. smu_poll();
  267. }
  268. EXPORT_SYMBOL(smu_spinwait_cmd);
  269. /* RTC low level commands */
  270. static inline int bcd2hex (int n)
  271. {
  272. return (((n & 0xf0) >> 4) * 10) + (n & 0xf);
  273. }
  274. static inline int hex2bcd (int n)
  275. {
  276. return ((n / 10) << 4) + (n % 10);
  277. }
  278. static inline void smu_fill_set_rtc_cmd(struct smu_cmd_buf *cmd_buf,
  279. struct rtc_time *time)
  280. {
  281. cmd_buf->cmd = 0x8e;
  282. cmd_buf->length = 8;
  283. cmd_buf->data[0] = 0x80;
  284. cmd_buf->data[1] = hex2bcd(time->tm_sec);
  285. cmd_buf->data[2] = hex2bcd(time->tm_min);
  286. cmd_buf->data[3] = hex2bcd(time->tm_hour);
  287. cmd_buf->data[4] = time->tm_wday;
  288. cmd_buf->data[5] = hex2bcd(time->tm_mday);
  289. cmd_buf->data[6] = hex2bcd(time->tm_mon) + 1;
  290. cmd_buf->data[7] = hex2bcd(time->tm_year - 100);
  291. }
  292. int smu_get_rtc_time(struct rtc_time *time, int spinwait)
  293. {
  294. struct smu_simple_cmd cmd;
  295. int rc;
  296. if (smu == NULL)
  297. return -ENODEV;
  298. memset(time, 0, sizeof(struct rtc_time));
  299. rc = smu_queue_simple(&cmd, SMU_CMD_RTC_COMMAND, 1, NULL, NULL,
  300. SMU_CMD_RTC_GET_DATETIME);
  301. if (rc)
  302. return rc;
  303. smu_spinwait_simple(&cmd);
  304. time->tm_sec = bcd2hex(cmd.buffer[0]);
  305. time->tm_min = bcd2hex(cmd.buffer[1]);
  306. time->tm_hour = bcd2hex(cmd.buffer[2]);
  307. time->tm_wday = bcd2hex(cmd.buffer[3]);
  308. time->tm_mday = bcd2hex(cmd.buffer[4]);
  309. time->tm_mon = bcd2hex(cmd.buffer[5]) - 1;
  310. time->tm_year = bcd2hex(cmd.buffer[6]) + 100;
  311. return 0;
  312. }
  313. int smu_set_rtc_time(struct rtc_time *time, int spinwait)
  314. {
  315. struct smu_simple_cmd cmd;
  316. int rc;
  317. if (smu == NULL)
  318. return -ENODEV;
  319. rc = smu_queue_simple(&cmd, SMU_CMD_RTC_COMMAND, 8, NULL, NULL,
  320. SMU_CMD_RTC_SET_DATETIME,
  321. hex2bcd(time->tm_sec),
  322. hex2bcd(time->tm_min),
  323. hex2bcd(time->tm_hour),
  324. time->tm_wday,
  325. hex2bcd(time->tm_mday),
  326. hex2bcd(time->tm_mon) + 1,
  327. hex2bcd(time->tm_year - 100));
  328. if (rc)
  329. return rc;
  330. smu_spinwait_simple(&cmd);
  331. return 0;
  332. }
  333. void smu_shutdown(void)
  334. {
  335. struct smu_simple_cmd cmd;
  336. if (smu == NULL)
  337. return;
  338. if (smu_queue_simple(&cmd, SMU_CMD_POWER_COMMAND, 9, NULL, NULL,
  339. 'S', 'H', 'U', 'T', 'D', 'O', 'W', 'N', 0))
  340. return;
  341. smu_spinwait_simple(&cmd);
  342. for (;;)
  343. ;
  344. }
  345. void smu_restart(void)
  346. {
  347. struct smu_simple_cmd cmd;
  348. if (smu == NULL)
  349. return;
  350. if (smu_queue_simple(&cmd, SMU_CMD_POWER_COMMAND, 8, NULL, NULL,
  351. 'R', 'E', 'S', 'T', 'A', 'R', 'T', 0))
  352. return;
  353. smu_spinwait_simple(&cmd);
  354. for (;;)
  355. ;
  356. }
  357. int smu_present(void)
  358. {
  359. return smu != NULL;
  360. }
  361. EXPORT_SYMBOL(smu_present);
  362. int __init smu_init (void)
  363. {
  364. struct device_node *np;
  365. u32 *data;
  366. np = of_find_node_by_type(NULL, "smu");
  367. if (np == NULL)
  368. return -ENODEV;
  369. printk(KERN_INFO "SMU driver %s %s\n", VERSION, AUTHOR);
  370. if (smu_cmdbuf_abs == 0) {
  371. printk(KERN_ERR "SMU: Command buffer not allocated !\n");
  372. return -EINVAL;
  373. }
  374. smu = alloc_bootmem(sizeof(struct smu_device));
  375. if (smu == NULL)
  376. return -ENOMEM;
  377. memset(smu, 0, sizeof(*smu));
  378. spin_lock_init(&smu->lock);
  379. INIT_LIST_HEAD(&smu->cmd_list);
  380. INIT_LIST_HEAD(&smu->cmd_i2c_list);
  381. smu->of_node = np;
  382. smu->db_irq = NO_IRQ;
  383. smu->msg_irq = NO_IRQ;
  384. /* smu_cmdbuf_abs is in the low 2G of RAM, can be converted to a
  385. * 32 bits value safely
  386. */
  387. smu->cmd_buf_abs = (u32)smu_cmdbuf_abs;
  388. smu->cmd_buf = (struct smu_cmd_buf *)abs_to_virt(smu_cmdbuf_abs);
  389. np = of_find_node_by_name(NULL, "smu-doorbell");
  390. if (np == NULL) {
  391. printk(KERN_ERR "SMU: Can't find doorbell GPIO !\n");
  392. goto fail;
  393. }
  394. data = (u32 *)get_property(np, "reg", NULL);
  395. if (data == NULL) {
  396. of_node_put(np);
  397. printk(KERN_ERR "SMU: Can't find doorbell GPIO address !\n");
  398. goto fail;
  399. }
  400. /* Current setup has one doorbell GPIO that does both doorbell
  401. * and ack. GPIOs are at 0x50, best would be to find that out
  402. * in the device-tree though.
  403. */
  404. smu->doorbell = *data;
  405. if (smu->doorbell < 0x50)
  406. smu->doorbell += 0x50;
  407. smu->db_irq = irq_of_parse_and_map(np, 0);
  408. of_node_put(np);
  409. /* Now look for the smu-interrupt GPIO */
  410. do {
  411. np = of_find_node_by_name(NULL, "smu-interrupt");
  412. if (np == NULL)
  413. break;
  414. data = (u32 *)get_property(np, "reg", NULL);
  415. if (data == NULL) {
  416. of_node_put(np);
  417. break;
  418. }
  419. smu->msg = *data;
  420. if (smu->msg < 0x50)
  421. smu->msg += 0x50;
  422. smu->msg_irq = irq_of_parse_and_map(np, 0);
  423. of_node_put(np);
  424. } while(0);
  425. /* Doorbell buffer is currently hard-coded, I didn't find a proper
  426. * device-tree entry giving the address. Best would probably to use
  427. * an offset for K2 base though, but let's do it that way for now.
  428. */
  429. smu->db_buf = ioremap(0x8000860c, 0x1000);
  430. if (smu->db_buf == NULL) {
  431. printk(KERN_ERR "SMU: Can't map doorbell buffer pointer !\n");
  432. goto fail;
  433. }
  434. sys_ctrler = SYS_CTRLER_SMU;
  435. return 0;
  436. fail:
  437. smu = NULL;
  438. return -ENXIO;
  439. }
  440. static int smu_late_init(void)
  441. {
  442. if (!smu)
  443. return 0;
  444. init_timer(&smu->i2c_timer);
  445. smu->i2c_timer.function = smu_i2c_retry;
  446. smu->i2c_timer.data = (unsigned long)smu;
  447. /*
  448. * Try to request the interrupts
  449. */
  450. if (smu->db_irq != NO_IRQ) {
  451. if (request_irq(smu->db_irq, smu_db_intr,
  452. IRQF_SHARED, "SMU doorbell", smu) < 0) {
  453. printk(KERN_WARNING "SMU: can't "
  454. "request interrupt %d\n",
  455. smu->db_irq);
  456. smu->db_irq = NO_IRQ;
  457. }
  458. }
  459. if (smu->msg_irq != NO_IRQ) {
  460. if (request_irq(smu->msg_irq, smu_msg_intr,
  461. IRQF_SHARED, "SMU message", smu) < 0) {
  462. printk(KERN_WARNING "SMU: can't "
  463. "request interrupt %d\n",
  464. smu->msg_irq);
  465. smu->msg_irq = NO_IRQ;
  466. }
  467. }
  468. return 0;
  469. }
  470. /* This has to be before arch_initcall as the low i2c stuff relies on the
  471. * above having been done before we reach arch_initcalls
  472. */
  473. core_initcall(smu_late_init);
  474. /*
  475. * sysfs visibility
  476. */
  477. static void smu_expose_childs(void *unused)
  478. {
  479. struct device_node *np;
  480. for (np = NULL; (np = of_get_next_child(smu->of_node, np)) != NULL;)
  481. if (device_is_compatible(np, "smu-sensors"))
  482. of_platform_device_create(np, "smu-sensors",
  483. &smu->of_dev->dev);
  484. }
  485. static DECLARE_WORK(smu_expose_childs_work, smu_expose_childs, NULL);
  486. static int smu_platform_probe(struct of_device* dev,
  487. const struct of_device_id *match)
  488. {
  489. if (!smu)
  490. return -ENODEV;
  491. smu->of_dev = dev;
  492. /*
  493. * Ok, we are matched, now expose all i2c busses. We have to defer
  494. * that unfortunately or it would deadlock inside the device model
  495. */
  496. schedule_work(&smu_expose_childs_work);
  497. return 0;
  498. }
  499. static struct of_device_id smu_platform_match[] =
  500. {
  501. {
  502. .type = "smu",
  503. },
  504. {},
  505. };
  506. static struct of_platform_driver smu_of_platform_driver =
  507. {
  508. .name = "smu",
  509. .match_table = smu_platform_match,
  510. .probe = smu_platform_probe,
  511. };
  512. static int __init smu_init_sysfs(void)
  513. {
  514. /*
  515. * Due to sysfs bogosity, a sysdev is not a real device, so
  516. * we should in fact create both if we want sysdev semantics
  517. * for power management.
  518. * For now, we don't power manage machines with an SMU chip,
  519. * I'm a bit too far from figuring out how that works with those
  520. * new chipsets, but that will come back and bite us
  521. */
  522. of_register_driver(&smu_of_platform_driver);
  523. return 0;
  524. }
  525. device_initcall(smu_init_sysfs);
  526. struct of_device *smu_get_ofdev(void)
  527. {
  528. if (!smu)
  529. return NULL;
  530. return smu->of_dev;
  531. }
  532. EXPORT_SYMBOL_GPL(smu_get_ofdev);
  533. /*
  534. * i2c interface
  535. */
  536. static void smu_i2c_complete_command(struct smu_i2c_cmd *cmd, int fail)
  537. {
  538. void (*done)(struct smu_i2c_cmd *cmd, void *misc) = cmd->done;
  539. void *misc = cmd->misc;
  540. unsigned long flags;
  541. /* Check for read case */
  542. if (!fail && cmd->read) {
  543. if (cmd->pdata[0] < 1)
  544. fail = 1;
  545. else
  546. memcpy(cmd->info.data, &cmd->pdata[1],
  547. cmd->info.datalen);
  548. }
  549. DPRINTK("SMU: completing, success: %d\n", !fail);
  550. /* Update status and mark no pending i2c command with lock
  551. * held so nobody comes in while we dequeue an eventual
  552. * pending next i2c command
  553. */
  554. spin_lock_irqsave(&smu->lock, flags);
  555. smu->cmd_i2c_cur = NULL;
  556. wmb();
  557. cmd->status = fail ? -EIO : 0;
  558. /* Is there another i2c command waiting ? */
  559. if (!list_empty(&smu->cmd_i2c_list)) {
  560. struct smu_i2c_cmd *newcmd;
  561. /* Fetch it, new current, remove from list */
  562. newcmd = list_entry(smu->cmd_i2c_list.next,
  563. struct smu_i2c_cmd, link);
  564. smu->cmd_i2c_cur = newcmd;
  565. list_del(&cmd->link);
  566. /* Queue with low level smu */
  567. list_add_tail(&cmd->scmd.link, &smu->cmd_list);
  568. if (smu->cmd_cur == NULL)
  569. smu_start_cmd();
  570. }
  571. spin_unlock_irqrestore(&smu->lock, flags);
  572. /* Call command completion handler if any */
  573. if (done)
  574. done(cmd, misc);
  575. }
  576. static void smu_i2c_retry(unsigned long data)
  577. {
  578. struct smu_i2c_cmd *cmd = smu->cmd_i2c_cur;
  579. DPRINTK("SMU: i2c failure, requeuing...\n");
  580. /* requeue command simply by resetting reply_len */
  581. cmd->pdata[0] = 0xff;
  582. cmd->scmd.reply_len = sizeof(cmd->pdata);
  583. smu_queue_cmd(&cmd->scmd);
  584. }
  585. static void smu_i2c_low_completion(struct smu_cmd *scmd, void *misc)
  586. {
  587. struct smu_i2c_cmd *cmd = misc;
  588. int fail = 0;
  589. DPRINTK("SMU: i2c compl. stage=%d status=%x pdata[0]=%x rlen: %x\n",
  590. cmd->stage, scmd->status, cmd->pdata[0], scmd->reply_len);
  591. /* Check for possible status */
  592. if (scmd->status < 0)
  593. fail = 1;
  594. else if (cmd->read) {
  595. if (cmd->stage == 0)
  596. fail = cmd->pdata[0] != 0;
  597. else
  598. fail = cmd->pdata[0] >= 0x80;
  599. } else {
  600. fail = cmd->pdata[0] != 0;
  601. }
  602. /* Handle failures by requeuing command, after 5ms interval
  603. */
  604. if (fail && --cmd->retries > 0) {
  605. DPRINTK("SMU: i2c failure, starting timer...\n");
  606. BUG_ON(cmd != smu->cmd_i2c_cur);
  607. mod_timer(&smu->i2c_timer, jiffies + msecs_to_jiffies(5));
  608. return;
  609. }
  610. /* If failure or stage 1, command is complete */
  611. if (fail || cmd->stage != 0) {
  612. smu_i2c_complete_command(cmd, fail);
  613. return;
  614. }
  615. DPRINTK("SMU: going to stage 1\n");
  616. /* Ok, initial command complete, now poll status */
  617. scmd->reply_buf = cmd->pdata;
  618. scmd->reply_len = sizeof(cmd->pdata);
  619. scmd->data_buf = cmd->pdata;
  620. scmd->data_len = 1;
  621. cmd->pdata[0] = 0;
  622. cmd->stage = 1;
  623. cmd->retries = 20;
  624. smu_queue_cmd(scmd);
  625. }
  626. int smu_queue_i2c(struct smu_i2c_cmd *cmd)
  627. {
  628. unsigned long flags;
  629. if (smu == NULL)
  630. return -ENODEV;
  631. /* Fill most fields of scmd */
  632. cmd->scmd.cmd = SMU_CMD_I2C_COMMAND;
  633. cmd->scmd.done = smu_i2c_low_completion;
  634. cmd->scmd.misc = cmd;
  635. cmd->scmd.reply_buf = cmd->pdata;
  636. cmd->scmd.reply_len = sizeof(cmd->pdata);
  637. cmd->scmd.data_buf = (u8 *)(char *)&cmd->info;
  638. cmd->scmd.status = 1;
  639. cmd->stage = 0;
  640. cmd->pdata[0] = 0xff;
  641. cmd->retries = 20;
  642. cmd->status = 1;
  643. /* Check transfer type, sanitize some "info" fields
  644. * based on transfer type and do more checking
  645. */
  646. cmd->info.caddr = cmd->info.devaddr;
  647. cmd->read = cmd->info.devaddr & 0x01;
  648. switch(cmd->info.type) {
  649. case SMU_I2C_TRANSFER_SIMPLE:
  650. memset(&cmd->info.sublen, 0, 4);
  651. break;
  652. case SMU_I2C_TRANSFER_COMBINED:
  653. cmd->info.devaddr &= 0xfe;
  654. case SMU_I2C_TRANSFER_STDSUB:
  655. if (cmd->info.sublen > 3)
  656. return -EINVAL;
  657. break;
  658. default:
  659. return -EINVAL;
  660. }
  661. /* Finish setting up command based on transfer direction
  662. */
  663. if (cmd->read) {
  664. if (cmd->info.datalen > SMU_I2C_READ_MAX)
  665. return -EINVAL;
  666. memset(cmd->info.data, 0xff, cmd->info.datalen);
  667. cmd->scmd.data_len = 9;
  668. } else {
  669. if (cmd->info.datalen > SMU_I2C_WRITE_MAX)
  670. return -EINVAL;
  671. cmd->scmd.data_len = 9 + cmd->info.datalen;
  672. }
  673. DPRINTK("SMU: i2c enqueuing command\n");
  674. DPRINTK("SMU: %s, len=%d bus=%x addr=%x sub0=%x type=%x\n",
  675. cmd->read ? "read" : "write", cmd->info.datalen,
  676. cmd->info.bus, cmd->info.caddr,
  677. cmd->info.subaddr[0], cmd->info.type);
  678. /* Enqueue command in i2c list, and if empty, enqueue also in
  679. * main command list
  680. */
  681. spin_lock_irqsave(&smu->lock, flags);
  682. if (smu->cmd_i2c_cur == NULL) {
  683. smu->cmd_i2c_cur = cmd;
  684. list_add_tail(&cmd->scmd.link, &smu->cmd_list);
  685. if (smu->cmd_cur == NULL)
  686. smu_start_cmd();
  687. } else
  688. list_add_tail(&cmd->link, &smu->cmd_i2c_list);
  689. spin_unlock_irqrestore(&smu->lock, flags);
  690. return 0;
  691. }
  692. /*
  693. * Handling of "partitions"
  694. */
  695. static int smu_read_datablock(u8 *dest, unsigned int addr, unsigned int len)
  696. {
  697. DECLARE_COMPLETION(comp);
  698. unsigned int chunk;
  699. struct smu_cmd cmd;
  700. int rc;
  701. u8 params[8];
  702. /* We currently use a chunk size of 0xe. We could check the
  703. * SMU firmware version and use bigger sizes though
  704. */
  705. chunk = 0xe;
  706. while (len) {
  707. unsigned int clen = min(len, chunk);
  708. cmd.cmd = SMU_CMD_MISC_ee_COMMAND;
  709. cmd.data_len = 7;
  710. cmd.data_buf = params;
  711. cmd.reply_len = chunk;
  712. cmd.reply_buf = dest;
  713. cmd.done = smu_done_complete;
  714. cmd.misc = &comp;
  715. params[0] = SMU_CMD_MISC_ee_GET_DATABLOCK_REC;
  716. params[1] = 0x4;
  717. *((u32 *)&params[2]) = addr;
  718. params[6] = clen;
  719. rc = smu_queue_cmd(&cmd);
  720. if (rc)
  721. return rc;
  722. wait_for_completion(&comp);
  723. if (cmd.status != 0)
  724. return rc;
  725. if (cmd.reply_len != clen) {
  726. printk(KERN_DEBUG "SMU: short read in "
  727. "smu_read_datablock, got: %d, want: %d\n",
  728. cmd.reply_len, clen);
  729. return -EIO;
  730. }
  731. len -= clen;
  732. addr += clen;
  733. dest += clen;
  734. }
  735. return 0;
  736. }
  737. static struct smu_sdbp_header *smu_create_sdb_partition(int id)
  738. {
  739. DECLARE_COMPLETION(comp);
  740. struct smu_simple_cmd cmd;
  741. unsigned int addr, len, tlen;
  742. struct smu_sdbp_header *hdr;
  743. struct property *prop;
  744. /* First query the partition info */
  745. DPRINTK("SMU: Query partition infos ... (irq=%d)\n", smu->db_irq);
  746. smu_queue_simple(&cmd, SMU_CMD_PARTITION_COMMAND, 2,
  747. smu_done_complete, &comp,
  748. SMU_CMD_PARTITION_LATEST, id);
  749. wait_for_completion(&comp);
  750. DPRINTK("SMU: done, status: %d, reply_len: %d\n",
  751. cmd.cmd.status, cmd.cmd.reply_len);
  752. /* Partition doesn't exist (or other error) */
  753. if (cmd.cmd.status != 0 || cmd.cmd.reply_len != 6)
  754. return NULL;
  755. /* Fetch address and length from reply */
  756. addr = *((u16 *)cmd.buffer);
  757. len = cmd.buffer[3] << 2;
  758. /* Calucluate total length to allocate, including the 17 bytes
  759. * for "sdb-partition-XX" that we append at the end of the buffer
  760. */
  761. tlen = sizeof(struct property) + len + 18;
  762. prop = kcalloc(tlen, 1, GFP_KERNEL);
  763. if (prop == NULL)
  764. return NULL;
  765. hdr = (struct smu_sdbp_header *)(prop + 1);
  766. prop->name = ((char *)prop) + tlen - 18;
  767. sprintf(prop->name, "sdb-partition-%02x", id);
  768. prop->length = len;
  769. prop->value = (unsigned char *)hdr;
  770. prop->next = NULL;
  771. /* Read the datablock */
  772. if (smu_read_datablock((u8 *)hdr, addr, len)) {
  773. printk(KERN_DEBUG "SMU: datablock read failed while reading "
  774. "partition %02x !\n", id);
  775. goto failure;
  776. }
  777. /* Got it, check a few things and create the property */
  778. if (hdr->id != id) {
  779. printk(KERN_DEBUG "SMU: Reading partition %02x and got "
  780. "%02x !\n", id, hdr->id);
  781. goto failure;
  782. }
  783. if (prom_add_property(smu->of_node, prop)) {
  784. printk(KERN_DEBUG "SMU: Failed creating sdb-partition-%02x "
  785. "property !\n", id);
  786. goto failure;
  787. }
  788. return hdr;
  789. failure:
  790. kfree(prop);
  791. return NULL;
  792. }
  793. /* Note: Only allowed to return error code in pointers (using ERR_PTR)
  794. * when interruptible is 1
  795. */
  796. struct smu_sdbp_header *__smu_get_sdb_partition(int id, unsigned int *size,
  797. int interruptible)
  798. {
  799. char pname[32];
  800. struct smu_sdbp_header *part;
  801. if (!smu)
  802. return NULL;
  803. sprintf(pname, "sdb-partition-%02x", id);
  804. DPRINTK("smu_get_sdb_partition(%02x)\n", id);
  805. if (interruptible) {
  806. int rc;
  807. rc = mutex_lock_interruptible(&smu_part_access);
  808. if (rc)
  809. return ERR_PTR(rc);
  810. } else
  811. mutex_lock(&smu_part_access);
  812. part = (struct smu_sdbp_header *)get_property(smu->of_node,
  813. pname, size);
  814. if (part == NULL) {
  815. DPRINTK("trying to extract from SMU ...\n");
  816. part = smu_create_sdb_partition(id);
  817. if (part != NULL && size)
  818. *size = part->len << 2;
  819. }
  820. mutex_unlock(&smu_part_access);
  821. return part;
  822. }
  823. struct smu_sdbp_header *smu_get_sdb_partition(int id, unsigned int *size)
  824. {
  825. return __smu_get_sdb_partition(id, size, 0);
  826. }
  827. EXPORT_SYMBOL(smu_get_sdb_partition);
  828. /*
  829. * Userland driver interface
  830. */
  831. static LIST_HEAD(smu_clist);
  832. static DEFINE_SPINLOCK(smu_clist_lock);
  833. enum smu_file_mode {
  834. smu_file_commands,
  835. smu_file_events,
  836. smu_file_closing
  837. };
  838. struct smu_private
  839. {
  840. struct list_head list;
  841. enum smu_file_mode mode;
  842. int busy;
  843. struct smu_cmd cmd;
  844. spinlock_t lock;
  845. wait_queue_head_t wait;
  846. u8 buffer[SMU_MAX_DATA];
  847. };
  848. static int smu_open(struct inode *inode, struct file *file)
  849. {
  850. struct smu_private *pp;
  851. unsigned long flags;
  852. pp = kmalloc(sizeof(struct smu_private), GFP_KERNEL);
  853. if (pp == 0)
  854. return -ENOMEM;
  855. memset(pp, 0, sizeof(struct smu_private));
  856. spin_lock_init(&pp->lock);
  857. pp->mode = smu_file_commands;
  858. init_waitqueue_head(&pp->wait);
  859. spin_lock_irqsave(&smu_clist_lock, flags);
  860. list_add(&pp->list, &smu_clist);
  861. spin_unlock_irqrestore(&smu_clist_lock, flags);
  862. file->private_data = pp;
  863. return 0;
  864. }
  865. static void smu_user_cmd_done(struct smu_cmd *cmd, void *misc)
  866. {
  867. struct smu_private *pp = misc;
  868. wake_up_all(&pp->wait);
  869. }
  870. static ssize_t smu_write(struct file *file, const char __user *buf,
  871. size_t count, loff_t *ppos)
  872. {
  873. struct smu_private *pp = file->private_data;
  874. unsigned long flags;
  875. struct smu_user_cmd_hdr hdr;
  876. int rc = 0;
  877. if (pp->busy)
  878. return -EBUSY;
  879. else if (copy_from_user(&hdr, buf, sizeof(hdr)))
  880. return -EFAULT;
  881. else if (hdr.cmdtype == SMU_CMDTYPE_WANTS_EVENTS) {
  882. pp->mode = smu_file_events;
  883. return 0;
  884. } else if (hdr.cmdtype == SMU_CMDTYPE_GET_PARTITION) {
  885. struct smu_sdbp_header *part;
  886. part = __smu_get_sdb_partition(hdr.cmd, NULL, 1);
  887. if (part == NULL)
  888. return -EINVAL;
  889. else if (IS_ERR(part))
  890. return PTR_ERR(part);
  891. return 0;
  892. } else if (hdr.cmdtype != SMU_CMDTYPE_SMU)
  893. return -EINVAL;
  894. else if (pp->mode != smu_file_commands)
  895. return -EBADFD;
  896. else if (hdr.data_len > SMU_MAX_DATA)
  897. return -EINVAL;
  898. spin_lock_irqsave(&pp->lock, flags);
  899. if (pp->busy) {
  900. spin_unlock_irqrestore(&pp->lock, flags);
  901. return -EBUSY;
  902. }
  903. pp->busy = 1;
  904. pp->cmd.status = 1;
  905. spin_unlock_irqrestore(&pp->lock, flags);
  906. if (copy_from_user(pp->buffer, buf + sizeof(hdr), hdr.data_len)) {
  907. pp->busy = 0;
  908. return -EFAULT;
  909. }
  910. pp->cmd.cmd = hdr.cmd;
  911. pp->cmd.data_len = hdr.data_len;
  912. pp->cmd.reply_len = SMU_MAX_DATA;
  913. pp->cmd.data_buf = pp->buffer;
  914. pp->cmd.reply_buf = pp->buffer;
  915. pp->cmd.done = smu_user_cmd_done;
  916. pp->cmd.misc = pp;
  917. rc = smu_queue_cmd(&pp->cmd);
  918. if (rc < 0)
  919. return rc;
  920. return count;
  921. }
  922. static ssize_t smu_read_command(struct file *file, struct smu_private *pp,
  923. char __user *buf, size_t count)
  924. {
  925. DECLARE_WAITQUEUE(wait, current);
  926. struct smu_user_reply_hdr hdr;
  927. unsigned long flags;
  928. int size, rc = 0;
  929. if (!pp->busy)
  930. return 0;
  931. if (count < sizeof(struct smu_user_reply_hdr))
  932. return -EOVERFLOW;
  933. spin_lock_irqsave(&pp->lock, flags);
  934. if (pp->cmd.status == 1) {
  935. if (file->f_flags & O_NONBLOCK)
  936. return -EAGAIN;
  937. add_wait_queue(&pp->wait, &wait);
  938. for (;;) {
  939. set_current_state(TASK_INTERRUPTIBLE);
  940. rc = 0;
  941. if (pp->cmd.status != 1)
  942. break;
  943. rc = -ERESTARTSYS;
  944. if (signal_pending(current))
  945. break;
  946. spin_unlock_irqrestore(&pp->lock, flags);
  947. schedule();
  948. spin_lock_irqsave(&pp->lock, flags);
  949. }
  950. set_current_state(TASK_RUNNING);
  951. remove_wait_queue(&pp->wait, &wait);
  952. }
  953. spin_unlock_irqrestore(&pp->lock, flags);
  954. if (rc)
  955. return rc;
  956. if (pp->cmd.status != 0)
  957. pp->cmd.reply_len = 0;
  958. size = sizeof(hdr) + pp->cmd.reply_len;
  959. if (count < size)
  960. size = count;
  961. rc = size;
  962. hdr.status = pp->cmd.status;
  963. hdr.reply_len = pp->cmd.reply_len;
  964. if (copy_to_user(buf, &hdr, sizeof(hdr)))
  965. return -EFAULT;
  966. size -= sizeof(hdr);
  967. if (size && copy_to_user(buf + sizeof(hdr), pp->buffer, size))
  968. return -EFAULT;
  969. pp->busy = 0;
  970. return rc;
  971. }
  972. static ssize_t smu_read_events(struct file *file, struct smu_private *pp,
  973. char __user *buf, size_t count)
  974. {
  975. /* Not implemented */
  976. msleep_interruptible(1000);
  977. return 0;
  978. }
  979. static ssize_t smu_read(struct file *file, char __user *buf,
  980. size_t count, loff_t *ppos)
  981. {
  982. struct smu_private *pp = file->private_data;
  983. if (pp->mode == smu_file_commands)
  984. return smu_read_command(file, pp, buf, count);
  985. if (pp->mode == smu_file_events)
  986. return smu_read_events(file, pp, buf, count);
  987. return -EBADFD;
  988. }
  989. static unsigned int smu_fpoll(struct file *file, poll_table *wait)
  990. {
  991. struct smu_private *pp = file->private_data;
  992. unsigned int mask = 0;
  993. unsigned long flags;
  994. if (pp == 0)
  995. return 0;
  996. if (pp->mode == smu_file_commands) {
  997. poll_wait(file, &pp->wait, wait);
  998. spin_lock_irqsave(&pp->lock, flags);
  999. if (pp->busy && pp->cmd.status != 1)
  1000. mask |= POLLIN;
  1001. spin_unlock_irqrestore(&pp->lock, flags);
  1002. } if (pp->mode == smu_file_events) {
  1003. /* Not yet implemented */
  1004. }
  1005. return mask;
  1006. }
  1007. static int smu_release(struct inode *inode, struct file *file)
  1008. {
  1009. struct smu_private *pp = file->private_data;
  1010. unsigned long flags;
  1011. unsigned int busy;
  1012. if (pp == 0)
  1013. return 0;
  1014. file->private_data = NULL;
  1015. /* Mark file as closing to avoid races with new request */
  1016. spin_lock_irqsave(&pp->lock, flags);
  1017. pp->mode = smu_file_closing;
  1018. busy = pp->busy;
  1019. /* Wait for any pending request to complete */
  1020. if (busy && pp->cmd.status == 1) {
  1021. DECLARE_WAITQUEUE(wait, current);
  1022. add_wait_queue(&pp->wait, &wait);
  1023. for (;;) {
  1024. set_current_state(TASK_UNINTERRUPTIBLE);
  1025. if (pp->cmd.status != 1)
  1026. break;
  1027. spin_lock_irqsave(&pp->lock, flags);
  1028. schedule();
  1029. spin_unlock_irqrestore(&pp->lock, flags);
  1030. }
  1031. set_current_state(TASK_RUNNING);
  1032. remove_wait_queue(&pp->wait, &wait);
  1033. }
  1034. spin_unlock_irqrestore(&pp->lock, flags);
  1035. spin_lock_irqsave(&smu_clist_lock, flags);
  1036. list_del(&pp->list);
  1037. spin_unlock_irqrestore(&smu_clist_lock, flags);
  1038. kfree(pp);
  1039. return 0;
  1040. }
  1041. static struct file_operations smu_device_fops = {
  1042. .llseek = no_llseek,
  1043. .read = smu_read,
  1044. .write = smu_write,
  1045. .poll = smu_fpoll,
  1046. .open = smu_open,
  1047. .release = smu_release,
  1048. };
  1049. static struct miscdevice pmu_device = {
  1050. MISC_DYNAMIC_MINOR, "smu", &smu_device_fops
  1051. };
  1052. static int smu_device_init(void)
  1053. {
  1054. if (!smu)
  1055. return -ENODEV;
  1056. if (misc_register(&pmu_device) < 0)
  1057. printk(KERN_ERR "via-pmu: cannot register misc device.\n");
  1058. return 0;
  1059. }
  1060. device_initcall(smu_device_init);