smu.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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. 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. IRQF_SHARED, "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. IRQF_SHARED, "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. /*
  517. * Due to sysfs bogosity, a sysdev is not a real device, so
  518. * we should in fact create both if we want sysdev semantics
  519. * for power management.
  520. * For now, we don't power manage machines with an SMU chip,
  521. * I'm a bit too far from figuring out how that works with those
  522. * new chipsets, but that will come back and bite us
  523. */
  524. of_register_driver(&smu_of_platform_driver);
  525. return 0;
  526. }
  527. device_initcall(smu_init_sysfs);
  528. struct of_device *smu_get_ofdev(void)
  529. {
  530. if (!smu)
  531. return NULL;
  532. return smu->of_dev;
  533. }
  534. EXPORT_SYMBOL_GPL(smu_get_ofdev);
  535. /*
  536. * i2c interface
  537. */
  538. static void smu_i2c_complete_command(struct smu_i2c_cmd *cmd, int fail)
  539. {
  540. void (*done)(struct smu_i2c_cmd *cmd, void *misc) = cmd->done;
  541. void *misc = cmd->misc;
  542. unsigned long flags;
  543. /* Check for read case */
  544. if (!fail && cmd->read) {
  545. if (cmd->pdata[0] < 1)
  546. fail = 1;
  547. else
  548. memcpy(cmd->info.data, &cmd->pdata[1],
  549. cmd->info.datalen);
  550. }
  551. DPRINTK("SMU: completing, success: %d\n", !fail);
  552. /* Update status and mark no pending i2c command with lock
  553. * held so nobody comes in while we dequeue an eventual
  554. * pending next i2c command
  555. */
  556. spin_lock_irqsave(&smu->lock, flags);
  557. smu->cmd_i2c_cur = NULL;
  558. wmb();
  559. cmd->status = fail ? -EIO : 0;
  560. /* Is there another i2c command waiting ? */
  561. if (!list_empty(&smu->cmd_i2c_list)) {
  562. struct smu_i2c_cmd *newcmd;
  563. /* Fetch it, new current, remove from list */
  564. newcmd = list_entry(smu->cmd_i2c_list.next,
  565. struct smu_i2c_cmd, link);
  566. smu->cmd_i2c_cur = newcmd;
  567. list_del(&cmd->link);
  568. /* Queue with low level smu */
  569. list_add_tail(&cmd->scmd.link, &smu->cmd_list);
  570. if (smu->cmd_cur == NULL)
  571. smu_start_cmd();
  572. }
  573. spin_unlock_irqrestore(&smu->lock, flags);
  574. /* Call command completion handler if any */
  575. if (done)
  576. done(cmd, misc);
  577. }
  578. static void smu_i2c_retry(unsigned long data)
  579. {
  580. struct smu_i2c_cmd *cmd = smu->cmd_i2c_cur;
  581. DPRINTK("SMU: i2c failure, requeuing...\n");
  582. /* requeue command simply by resetting reply_len */
  583. cmd->pdata[0] = 0xff;
  584. cmd->scmd.reply_len = sizeof(cmd->pdata);
  585. smu_queue_cmd(&cmd->scmd);
  586. }
  587. static void smu_i2c_low_completion(struct smu_cmd *scmd, void *misc)
  588. {
  589. struct smu_i2c_cmd *cmd = misc;
  590. int fail = 0;
  591. DPRINTK("SMU: i2c compl. stage=%d status=%x pdata[0]=%x rlen: %x\n",
  592. cmd->stage, scmd->status, cmd->pdata[0], scmd->reply_len);
  593. /* Check for possible status */
  594. if (scmd->status < 0)
  595. fail = 1;
  596. else if (cmd->read) {
  597. if (cmd->stage == 0)
  598. fail = cmd->pdata[0] != 0;
  599. else
  600. fail = cmd->pdata[0] >= 0x80;
  601. } else {
  602. fail = cmd->pdata[0] != 0;
  603. }
  604. /* Handle failures by requeuing command, after 5ms interval
  605. */
  606. if (fail && --cmd->retries > 0) {
  607. DPRINTK("SMU: i2c failure, starting timer...\n");
  608. BUG_ON(cmd != smu->cmd_i2c_cur);
  609. mod_timer(&smu->i2c_timer, jiffies + msecs_to_jiffies(5));
  610. return;
  611. }
  612. /* If failure or stage 1, command is complete */
  613. if (fail || cmd->stage != 0) {
  614. smu_i2c_complete_command(cmd, fail);
  615. return;
  616. }
  617. DPRINTK("SMU: going to stage 1\n");
  618. /* Ok, initial command complete, now poll status */
  619. scmd->reply_buf = cmd->pdata;
  620. scmd->reply_len = sizeof(cmd->pdata);
  621. scmd->data_buf = cmd->pdata;
  622. scmd->data_len = 1;
  623. cmd->pdata[0] = 0;
  624. cmd->stage = 1;
  625. cmd->retries = 20;
  626. smu_queue_cmd(scmd);
  627. }
  628. int smu_queue_i2c(struct smu_i2c_cmd *cmd)
  629. {
  630. unsigned long flags;
  631. if (smu == NULL)
  632. return -ENODEV;
  633. /* Fill most fields of scmd */
  634. cmd->scmd.cmd = SMU_CMD_I2C_COMMAND;
  635. cmd->scmd.done = smu_i2c_low_completion;
  636. cmd->scmd.misc = cmd;
  637. cmd->scmd.reply_buf = cmd->pdata;
  638. cmd->scmd.reply_len = sizeof(cmd->pdata);
  639. cmd->scmd.data_buf = (u8 *)(char *)&cmd->info;
  640. cmd->scmd.status = 1;
  641. cmd->stage = 0;
  642. cmd->pdata[0] = 0xff;
  643. cmd->retries = 20;
  644. cmd->status = 1;
  645. /* Check transfer type, sanitize some "info" fields
  646. * based on transfer type and do more checking
  647. */
  648. cmd->info.caddr = cmd->info.devaddr;
  649. cmd->read = cmd->info.devaddr & 0x01;
  650. switch(cmd->info.type) {
  651. case SMU_I2C_TRANSFER_SIMPLE:
  652. memset(&cmd->info.sublen, 0, 4);
  653. break;
  654. case SMU_I2C_TRANSFER_COMBINED:
  655. cmd->info.devaddr &= 0xfe;
  656. case SMU_I2C_TRANSFER_STDSUB:
  657. if (cmd->info.sublen > 3)
  658. return -EINVAL;
  659. break;
  660. default:
  661. return -EINVAL;
  662. }
  663. /* Finish setting up command based on transfer direction
  664. */
  665. if (cmd->read) {
  666. if (cmd->info.datalen > SMU_I2C_READ_MAX)
  667. return -EINVAL;
  668. memset(cmd->info.data, 0xff, cmd->info.datalen);
  669. cmd->scmd.data_len = 9;
  670. } else {
  671. if (cmd->info.datalen > SMU_I2C_WRITE_MAX)
  672. return -EINVAL;
  673. cmd->scmd.data_len = 9 + cmd->info.datalen;
  674. }
  675. DPRINTK("SMU: i2c enqueuing command\n");
  676. DPRINTK("SMU: %s, len=%d bus=%x addr=%x sub0=%x type=%x\n",
  677. cmd->read ? "read" : "write", cmd->info.datalen,
  678. cmd->info.bus, cmd->info.caddr,
  679. cmd->info.subaddr[0], cmd->info.type);
  680. /* Enqueue command in i2c list, and if empty, enqueue also in
  681. * main command list
  682. */
  683. spin_lock_irqsave(&smu->lock, flags);
  684. if (smu->cmd_i2c_cur == NULL) {
  685. smu->cmd_i2c_cur = cmd;
  686. list_add_tail(&cmd->scmd.link, &smu->cmd_list);
  687. if (smu->cmd_cur == NULL)
  688. smu_start_cmd();
  689. } else
  690. list_add_tail(&cmd->link, &smu->cmd_i2c_list);
  691. spin_unlock_irqrestore(&smu->lock, flags);
  692. return 0;
  693. }
  694. /*
  695. * Handling of "partitions"
  696. */
  697. static int smu_read_datablock(u8 *dest, unsigned int addr, unsigned int len)
  698. {
  699. DECLARE_COMPLETION(comp);
  700. unsigned int chunk;
  701. struct smu_cmd cmd;
  702. int rc;
  703. u8 params[8];
  704. /* We currently use a chunk size of 0xe. We could check the
  705. * SMU firmware version and use bigger sizes though
  706. */
  707. chunk = 0xe;
  708. while (len) {
  709. unsigned int clen = min(len, chunk);
  710. cmd.cmd = SMU_CMD_MISC_ee_COMMAND;
  711. cmd.data_len = 7;
  712. cmd.data_buf = params;
  713. cmd.reply_len = chunk;
  714. cmd.reply_buf = dest;
  715. cmd.done = smu_done_complete;
  716. cmd.misc = &comp;
  717. params[0] = SMU_CMD_MISC_ee_GET_DATABLOCK_REC;
  718. params[1] = 0x4;
  719. *((u32 *)&params[2]) = addr;
  720. params[6] = clen;
  721. rc = smu_queue_cmd(&cmd);
  722. if (rc)
  723. return rc;
  724. wait_for_completion(&comp);
  725. if (cmd.status != 0)
  726. return rc;
  727. if (cmd.reply_len != clen) {
  728. printk(KERN_DEBUG "SMU: short read in "
  729. "smu_read_datablock, got: %d, want: %d\n",
  730. cmd.reply_len, clen);
  731. return -EIO;
  732. }
  733. len -= clen;
  734. addr += clen;
  735. dest += clen;
  736. }
  737. return 0;
  738. }
  739. static struct smu_sdbp_header *smu_create_sdb_partition(int id)
  740. {
  741. DECLARE_COMPLETION(comp);
  742. struct smu_simple_cmd cmd;
  743. unsigned int addr, len, tlen;
  744. struct smu_sdbp_header *hdr;
  745. struct property *prop;
  746. /* First query the partition info */
  747. DPRINTK("SMU: Query partition infos ... (irq=%d)\n", smu->db_irq);
  748. smu_queue_simple(&cmd, SMU_CMD_PARTITION_COMMAND, 2,
  749. smu_done_complete, &comp,
  750. SMU_CMD_PARTITION_LATEST, id);
  751. wait_for_completion(&comp);
  752. DPRINTK("SMU: done, status: %d, reply_len: %d\n",
  753. cmd.cmd.status, cmd.cmd.reply_len);
  754. /* Partition doesn't exist (or other error) */
  755. if (cmd.cmd.status != 0 || cmd.cmd.reply_len != 6)
  756. return NULL;
  757. /* Fetch address and length from reply */
  758. addr = *((u16 *)cmd.buffer);
  759. len = cmd.buffer[3] << 2;
  760. /* Calucluate total length to allocate, including the 17 bytes
  761. * for "sdb-partition-XX" that we append at the end of the buffer
  762. */
  763. tlen = sizeof(struct property) + len + 18;
  764. prop = kcalloc(tlen, 1, GFP_KERNEL);
  765. if (prop == NULL)
  766. return NULL;
  767. hdr = (struct smu_sdbp_header *)(prop + 1);
  768. prop->name = ((char *)prop) + tlen - 18;
  769. sprintf(prop->name, "sdb-partition-%02x", id);
  770. prop->length = len;
  771. prop->value = (unsigned char *)hdr;
  772. prop->next = NULL;
  773. /* Read the datablock */
  774. if (smu_read_datablock((u8 *)hdr, addr, len)) {
  775. printk(KERN_DEBUG "SMU: datablock read failed while reading "
  776. "partition %02x !\n", id);
  777. goto failure;
  778. }
  779. /* Got it, check a few things and create the property */
  780. if (hdr->id != id) {
  781. printk(KERN_DEBUG "SMU: Reading partition %02x and got "
  782. "%02x !\n", id, hdr->id);
  783. goto failure;
  784. }
  785. if (prom_add_property(smu->of_node, prop)) {
  786. printk(KERN_DEBUG "SMU: Failed creating sdb-partition-%02x "
  787. "property !\n", id);
  788. goto failure;
  789. }
  790. return hdr;
  791. failure:
  792. kfree(prop);
  793. return NULL;
  794. }
  795. /* Note: Only allowed to return error code in pointers (using ERR_PTR)
  796. * when interruptible is 1
  797. */
  798. struct smu_sdbp_header *__smu_get_sdb_partition(int id, unsigned int *size,
  799. int interruptible)
  800. {
  801. char pname[32];
  802. struct smu_sdbp_header *part;
  803. if (!smu)
  804. return NULL;
  805. sprintf(pname, "sdb-partition-%02x", id);
  806. DPRINTK("smu_get_sdb_partition(%02x)\n", id);
  807. if (interruptible) {
  808. int rc;
  809. rc = mutex_lock_interruptible(&smu_part_access);
  810. if (rc)
  811. return ERR_PTR(rc);
  812. } else
  813. mutex_lock(&smu_part_access);
  814. part = (struct smu_sdbp_header *)get_property(smu->of_node,
  815. pname, size);
  816. if (part == NULL) {
  817. DPRINTK("trying to extract from SMU ...\n");
  818. part = smu_create_sdb_partition(id);
  819. if (part != NULL && size)
  820. *size = part->len << 2;
  821. }
  822. mutex_unlock(&smu_part_access);
  823. return part;
  824. }
  825. struct smu_sdbp_header *smu_get_sdb_partition(int id, unsigned int *size)
  826. {
  827. return __smu_get_sdb_partition(id, size, 0);
  828. }
  829. EXPORT_SYMBOL(smu_get_sdb_partition);
  830. /*
  831. * Userland driver interface
  832. */
  833. static LIST_HEAD(smu_clist);
  834. static DEFINE_SPINLOCK(smu_clist_lock);
  835. enum smu_file_mode {
  836. smu_file_commands,
  837. smu_file_events,
  838. smu_file_closing
  839. };
  840. struct smu_private
  841. {
  842. struct list_head list;
  843. enum smu_file_mode mode;
  844. int busy;
  845. struct smu_cmd cmd;
  846. spinlock_t lock;
  847. wait_queue_head_t wait;
  848. u8 buffer[SMU_MAX_DATA];
  849. };
  850. static int smu_open(struct inode *inode, struct file *file)
  851. {
  852. struct smu_private *pp;
  853. unsigned long flags;
  854. pp = kmalloc(sizeof(struct smu_private), GFP_KERNEL);
  855. if (pp == 0)
  856. return -ENOMEM;
  857. memset(pp, 0, sizeof(struct smu_private));
  858. spin_lock_init(&pp->lock);
  859. pp->mode = smu_file_commands;
  860. init_waitqueue_head(&pp->wait);
  861. spin_lock_irqsave(&smu_clist_lock, flags);
  862. list_add(&pp->list, &smu_clist);
  863. spin_unlock_irqrestore(&smu_clist_lock, flags);
  864. file->private_data = pp;
  865. return 0;
  866. }
  867. static void smu_user_cmd_done(struct smu_cmd *cmd, void *misc)
  868. {
  869. struct smu_private *pp = misc;
  870. wake_up_all(&pp->wait);
  871. }
  872. static ssize_t smu_write(struct file *file, const char __user *buf,
  873. size_t count, loff_t *ppos)
  874. {
  875. struct smu_private *pp = file->private_data;
  876. unsigned long flags;
  877. struct smu_user_cmd_hdr hdr;
  878. int rc = 0;
  879. if (pp->busy)
  880. return -EBUSY;
  881. else if (copy_from_user(&hdr, buf, sizeof(hdr)))
  882. return -EFAULT;
  883. else if (hdr.cmdtype == SMU_CMDTYPE_WANTS_EVENTS) {
  884. pp->mode = smu_file_events;
  885. return 0;
  886. } else if (hdr.cmdtype == SMU_CMDTYPE_GET_PARTITION) {
  887. struct smu_sdbp_header *part;
  888. part = __smu_get_sdb_partition(hdr.cmd, NULL, 1);
  889. if (part == NULL)
  890. return -EINVAL;
  891. else if (IS_ERR(part))
  892. return PTR_ERR(part);
  893. return 0;
  894. } else if (hdr.cmdtype != SMU_CMDTYPE_SMU)
  895. return -EINVAL;
  896. else if (pp->mode != smu_file_commands)
  897. return -EBADFD;
  898. else if (hdr.data_len > SMU_MAX_DATA)
  899. return -EINVAL;
  900. spin_lock_irqsave(&pp->lock, flags);
  901. if (pp->busy) {
  902. spin_unlock_irqrestore(&pp->lock, flags);
  903. return -EBUSY;
  904. }
  905. pp->busy = 1;
  906. pp->cmd.status = 1;
  907. spin_unlock_irqrestore(&pp->lock, flags);
  908. if (copy_from_user(pp->buffer, buf + sizeof(hdr), hdr.data_len)) {
  909. pp->busy = 0;
  910. return -EFAULT;
  911. }
  912. pp->cmd.cmd = hdr.cmd;
  913. pp->cmd.data_len = hdr.data_len;
  914. pp->cmd.reply_len = SMU_MAX_DATA;
  915. pp->cmd.data_buf = pp->buffer;
  916. pp->cmd.reply_buf = pp->buffer;
  917. pp->cmd.done = smu_user_cmd_done;
  918. pp->cmd.misc = pp;
  919. rc = smu_queue_cmd(&pp->cmd);
  920. if (rc < 0)
  921. return rc;
  922. return count;
  923. }
  924. static ssize_t smu_read_command(struct file *file, struct smu_private *pp,
  925. char __user *buf, size_t count)
  926. {
  927. DECLARE_WAITQUEUE(wait, current);
  928. struct smu_user_reply_hdr hdr;
  929. unsigned long flags;
  930. int size, rc = 0;
  931. if (!pp->busy)
  932. return 0;
  933. if (count < sizeof(struct smu_user_reply_hdr))
  934. return -EOVERFLOW;
  935. spin_lock_irqsave(&pp->lock, flags);
  936. if (pp->cmd.status == 1) {
  937. if (file->f_flags & O_NONBLOCK)
  938. return -EAGAIN;
  939. add_wait_queue(&pp->wait, &wait);
  940. for (;;) {
  941. set_current_state(TASK_INTERRUPTIBLE);
  942. rc = 0;
  943. if (pp->cmd.status != 1)
  944. break;
  945. rc = -ERESTARTSYS;
  946. if (signal_pending(current))
  947. break;
  948. spin_unlock_irqrestore(&pp->lock, flags);
  949. schedule();
  950. spin_lock_irqsave(&pp->lock, flags);
  951. }
  952. set_current_state(TASK_RUNNING);
  953. remove_wait_queue(&pp->wait, &wait);
  954. }
  955. spin_unlock_irqrestore(&pp->lock, flags);
  956. if (rc)
  957. return rc;
  958. if (pp->cmd.status != 0)
  959. pp->cmd.reply_len = 0;
  960. size = sizeof(hdr) + pp->cmd.reply_len;
  961. if (count < size)
  962. size = count;
  963. rc = size;
  964. hdr.status = pp->cmd.status;
  965. hdr.reply_len = pp->cmd.reply_len;
  966. if (copy_to_user(buf, &hdr, sizeof(hdr)))
  967. return -EFAULT;
  968. size -= sizeof(hdr);
  969. if (size && copy_to_user(buf + sizeof(hdr), pp->buffer, size))
  970. return -EFAULT;
  971. pp->busy = 0;
  972. return rc;
  973. }
  974. static ssize_t smu_read_events(struct file *file, struct smu_private *pp,
  975. char __user *buf, size_t count)
  976. {
  977. /* Not implemented */
  978. msleep_interruptible(1000);
  979. return 0;
  980. }
  981. static ssize_t smu_read(struct file *file, char __user *buf,
  982. size_t count, loff_t *ppos)
  983. {
  984. struct smu_private *pp = file->private_data;
  985. if (pp->mode == smu_file_commands)
  986. return smu_read_command(file, pp, buf, count);
  987. if (pp->mode == smu_file_events)
  988. return smu_read_events(file, pp, buf, count);
  989. return -EBADFD;
  990. }
  991. static unsigned int smu_fpoll(struct file *file, poll_table *wait)
  992. {
  993. struct smu_private *pp = file->private_data;
  994. unsigned int mask = 0;
  995. unsigned long flags;
  996. if (pp == 0)
  997. return 0;
  998. if (pp->mode == smu_file_commands) {
  999. poll_wait(file, &pp->wait, wait);
  1000. spin_lock_irqsave(&pp->lock, flags);
  1001. if (pp->busy && pp->cmd.status != 1)
  1002. mask |= POLLIN;
  1003. spin_unlock_irqrestore(&pp->lock, flags);
  1004. } if (pp->mode == smu_file_events) {
  1005. /* Not yet implemented */
  1006. }
  1007. return mask;
  1008. }
  1009. static int smu_release(struct inode *inode, struct file *file)
  1010. {
  1011. struct smu_private *pp = file->private_data;
  1012. unsigned long flags;
  1013. unsigned int busy;
  1014. if (pp == 0)
  1015. return 0;
  1016. file->private_data = NULL;
  1017. /* Mark file as closing to avoid races with new request */
  1018. spin_lock_irqsave(&pp->lock, flags);
  1019. pp->mode = smu_file_closing;
  1020. busy = pp->busy;
  1021. /* Wait for any pending request to complete */
  1022. if (busy && pp->cmd.status == 1) {
  1023. DECLARE_WAITQUEUE(wait, current);
  1024. add_wait_queue(&pp->wait, &wait);
  1025. for (;;) {
  1026. set_current_state(TASK_UNINTERRUPTIBLE);
  1027. if (pp->cmd.status != 1)
  1028. break;
  1029. spin_lock_irqsave(&pp->lock, flags);
  1030. schedule();
  1031. spin_unlock_irqrestore(&pp->lock, flags);
  1032. }
  1033. set_current_state(TASK_RUNNING);
  1034. remove_wait_queue(&pp->wait, &wait);
  1035. }
  1036. spin_unlock_irqrestore(&pp->lock, flags);
  1037. spin_lock_irqsave(&smu_clist_lock, flags);
  1038. list_del(&pp->list);
  1039. spin_unlock_irqrestore(&smu_clist_lock, flags);
  1040. kfree(pp);
  1041. return 0;
  1042. }
  1043. static struct file_operations smu_device_fops = {
  1044. .llseek = no_llseek,
  1045. .read = smu_read,
  1046. .write = smu_write,
  1047. .poll = smu_fpoll,
  1048. .open = smu_open,
  1049. .release = smu_release,
  1050. };
  1051. static struct miscdevice pmu_device = {
  1052. MISC_DYNAMIC_MINOR, "smu", &smu_device_fops
  1053. };
  1054. static int smu_device_init(void)
  1055. {
  1056. if (!smu)
  1057. return -ENODEV;
  1058. if (misc_register(&pmu_device) < 0)
  1059. printk(KERN_ERR "via-pmu: cannot register misc device.\n");
  1060. return 0;
  1061. }
  1062. device_initcall(smu_device_init);