smu.c 29 KB

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