smu.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  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 <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 DECLARE_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. if (np->n_intrs > 0)
  408. smu->db_irq = np->intrs[0].line;
  409. of_node_put(np);
  410. /* Now look for the smu-interrupt GPIO */
  411. do {
  412. np = of_find_node_by_name(NULL, "smu-interrupt");
  413. if (np == NULL)
  414. break;
  415. data = (u32 *)get_property(np, "reg", NULL);
  416. if (data == NULL) {
  417. of_node_put(np);
  418. break;
  419. }
  420. smu->msg = *data;
  421. if (smu->msg < 0x50)
  422. smu->msg += 0x50;
  423. if (np->n_intrs > 0)
  424. smu->msg_irq = np->intrs[0].line;
  425. of_node_put(np);
  426. } while(0);
  427. /* Doorbell buffer is currently hard-coded, I didn't find a proper
  428. * device-tree entry giving the address. Best would probably to use
  429. * an offset for K2 base though, but let's do it that way for now.
  430. */
  431. smu->db_buf = ioremap(0x8000860c, 0x1000);
  432. if (smu->db_buf == NULL) {
  433. printk(KERN_ERR "SMU: Can't map doorbell buffer pointer !\n");
  434. goto fail;
  435. }
  436. sys_ctrler = SYS_CTRLER_SMU;
  437. return 0;
  438. fail:
  439. smu = NULL;
  440. return -ENXIO;
  441. }
  442. static int smu_late_init(void)
  443. {
  444. if (!smu)
  445. return 0;
  446. init_timer(&smu->i2c_timer);
  447. smu->i2c_timer.function = smu_i2c_retry;
  448. smu->i2c_timer.data = (unsigned long)smu;
  449. /*
  450. * Try to request the interrupts
  451. */
  452. if (smu->db_irq != NO_IRQ) {
  453. if (request_irq(smu->db_irq, smu_db_intr,
  454. SA_SHIRQ, "SMU doorbell", smu) < 0) {
  455. printk(KERN_WARNING "SMU: can't "
  456. "request interrupt %d\n",
  457. smu->db_irq);
  458. smu->db_irq = NO_IRQ;
  459. }
  460. }
  461. if (smu->msg_irq != NO_IRQ) {
  462. if (request_irq(smu->msg_irq, smu_msg_intr,
  463. SA_SHIRQ, "SMU message", smu) < 0) {
  464. printk(KERN_WARNING "SMU: can't "
  465. "request interrupt %d\n",
  466. smu->msg_irq);
  467. smu->msg_irq = NO_IRQ;
  468. }
  469. }
  470. return 0;
  471. }
  472. /* This has to be before arch_initcall as the low i2c stuff relies on the
  473. * above having been done before we reach arch_initcalls
  474. */
  475. core_initcall(smu_late_init);
  476. /*
  477. * sysfs visibility
  478. */
  479. static void smu_expose_childs(void *unused)
  480. {
  481. struct device_node *np;
  482. for (np = NULL; (np = of_get_next_child(smu->of_node, np)) != NULL;)
  483. if (device_is_compatible(np, "smu-sensors"))
  484. of_platform_device_create(np, "smu-sensors",
  485. &smu->of_dev->dev);
  486. }
  487. static DECLARE_WORK(smu_expose_childs_work, smu_expose_childs, NULL);
  488. static int smu_platform_probe(struct of_device* dev,
  489. const struct of_device_id *match)
  490. {
  491. if (!smu)
  492. return -ENODEV;
  493. smu->of_dev = dev;
  494. /*
  495. * Ok, we are matched, now expose all i2c busses. We have to defer
  496. * that unfortunately or it would deadlock inside the device model
  497. */
  498. schedule_work(&smu_expose_childs_work);
  499. return 0;
  500. }
  501. static struct of_device_id smu_platform_match[] =
  502. {
  503. {
  504. .type = "smu",
  505. },
  506. {},
  507. };
  508. static struct of_platform_driver smu_of_platform_driver =
  509. {
  510. .name = "smu",
  511. .match_table = smu_platform_match,
  512. .probe = smu_platform_probe,
  513. };
  514. static int __init smu_init_sysfs(void)
  515. {
  516. int rc;
  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. rc = 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 = down_interruptible(&smu_part_access);
  811. if (rc)
  812. return ERR_PTR(rc);
  813. } else
  814. down(&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. up(&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);