smu.c 29 KB

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