sch56xx-common.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /***************************************************************************
  2. * Copyright (C) 2010-2011 Hans de Goede <hdegoede@redhat.com> *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/acpi.h>
  26. #include <linux/delay.h>
  27. #include "sch56xx-common.h"
  28. #define SIO_SCH56XX_LD_EM 0x0C /* Embedded uController Logical Dev */
  29. #define SIO_UNLOCK_KEY 0x55 /* Key to enable Super-I/O */
  30. #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */
  31. #define SIO_REG_LDSEL 0x07 /* Logical device select */
  32. #define SIO_REG_DEVID 0x20 /* Device ID */
  33. #define SIO_REG_ENABLE 0x30 /* Logical device enable */
  34. #define SIO_REG_ADDR 0x66 /* Logical device address (2 bytes) */
  35. #define SIO_SCH5627_ID 0xC6 /* Chipset ID */
  36. #define SIO_SCH5636_ID 0xC7 /* Chipset ID */
  37. #define REGION_LENGTH 9
  38. #define SCH56XX_CMD_READ 0x02
  39. #define SCH56XX_CMD_WRITE 0x03
  40. static struct platform_device *sch56xx_pdev;
  41. /* Super I/O functions */
  42. static inline int superio_inb(int base, int reg)
  43. {
  44. outb(reg, base);
  45. return inb(base + 1);
  46. }
  47. static inline int superio_enter(int base)
  48. {
  49. /* Don't step on other drivers' I/O space by accident */
  50. if (!request_muxed_region(base, 2, "sch56xx")) {
  51. pr_err("I/O address 0x%04x already in use\n", base);
  52. return -EBUSY;
  53. }
  54. outb(SIO_UNLOCK_KEY, base);
  55. return 0;
  56. }
  57. static inline void superio_select(int base, int ld)
  58. {
  59. outb(SIO_REG_LDSEL, base);
  60. outb(ld, base + 1);
  61. }
  62. static inline void superio_exit(int base)
  63. {
  64. outb(SIO_LOCK_KEY, base);
  65. release_region(base, 2);
  66. }
  67. static int sch56xx_send_cmd(u16 addr, u8 cmd, u16 reg, u8 v)
  68. {
  69. u8 val;
  70. int i;
  71. /*
  72. * According to SMSC for the commands we use the maximum time for
  73. * the EM to respond is 15 ms, but testing shows in practice it
  74. * responds within 15-32 reads, so we first busy poll, and if
  75. * that fails sleep a bit and try again until we are way past
  76. * the 15 ms maximum response time.
  77. */
  78. const int max_busy_polls = 64;
  79. const int max_lazy_polls = 32;
  80. /* (Optional) Write-Clear the EC to Host Mailbox Register */
  81. val = inb(addr + 1);
  82. outb(val, addr + 1);
  83. /* Set Mailbox Address Pointer to first location in Region 1 */
  84. outb(0x00, addr + 2);
  85. outb(0x80, addr + 3);
  86. /* Write Request Packet Header */
  87. outb(cmd, addr + 4); /* VREG Access Type read:0x02 write:0x03 */
  88. outb(0x01, addr + 5); /* # of Entries: 1 Byte (8-bit) */
  89. outb(0x04, addr + 2); /* Mailbox AP to first data entry loc. */
  90. /* Write Value field */
  91. if (cmd == SCH56XX_CMD_WRITE)
  92. outb(v, addr + 4);
  93. /* Write Address field */
  94. outb(reg & 0xff, addr + 6);
  95. outb(reg >> 8, addr + 7);
  96. /* Execute the Random Access Command */
  97. outb(0x01, addr); /* Write 01h to the Host-to-EC register */
  98. /* EM Interface Polling "Algorithm" */
  99. for (i = 0; i < max_busy_polls + max_lazy_polls; i++) {
  100. if (i >= max_busy_polls)
  101. msleep(1);
  102. /* Read Interrupt source Register */
  103. val = inb(addr + 8);
  104. /* Write Clear the interrupt source bits */
  105. if (val)
  106. outb(val, addr + 8);
  107. /* Command Completed ? */
  108. if (val & 0x01)
  109. break;
  110. }
  111. if (i == max_busy_polls + max_lazy_polls) {
  112. pr_err("Max retries exceeded reading virtual "
  113. "register 0x%04hx (%d)\n", reg, 1);
  114. return -EIO;
  115. }
  116. /*
  117. * According to SMSC we may need to retry this, but sofar I've always
  118. * seen this succeed in 1 try.
  119. */
  120. for (i = 0; i < max_busy_polls; i++) {
  121. /* Read EC-to-Host Register */
  122. val = inb(addr + 1);
  123. /* Command Completed ? */
  124. if (val == 0x01)
  125. break;
  126. if (i == 0)
  127. pr_warn("EC reports: 0x%02x reading virtual register "
  128. "0x%04hx\n", (unsigned int)val, reg);
  129. }
  130. if (i == max_busy_polls) {
  131. pr_err("Max retries exceeded reading virtual "
  132. "register 0x%04hx (%d)\n", reg, 2);
  133. return -EIO;
  134. }
  135. /*
  136. * According to the SMSC app note we should now do:
  137. *
  138. * Set Mailbox Address Pointer to first location in Region 1 *
  139. * outb(0x00, addr + 2);
  140. * outb(0x80, addr + 3);
  141. *
  142. * But if we do that things don't work, so let's not.
  143. */
  144. /* Read Value field */
  145. if (cmd == SCH56XX_CMD_READ)
  146. return inb(addr + 4);
  147. return 0;
  148. }
  149. int sch56xx_read_virtual_reg(u16 addr, u16 reg)
  150. {
  151. return sch56xx_send_cmd(addr, SCH56XX_CMD_READ, reg, 0);
  152. }
  153. EXPORT_SYMBOL(sch56xx_read_virtual_reg);
  154. int sch56xx_write_virtual_reg(u16 addr, u16 reg, u8 val)
  155. {
  156. return sch56xx_send_cmd(addr, SCH56XX_CMD_WRITE, reg, val);
  157. }
  158. EXPORT_SYMBOL(sch56xx_write_virtual_reg);
  159. int sch56xx_read_virtual_reg16(u16 addr, u16 reg)
  160. {
  161. int lsb, msb;
  162. /* Read LSB first, this will cause the matching MSB to be latched */
  163. lsb = sch56xx_read_virtual_reg(addr, reg);
  164. if (lsb < 0)
  165. return lsb;
  166. msb = sch56xx_read_virtual_reg(addr, reg + 1);
  167. if (msb < 0)
  168. return msb;
  169. return lsb | (msb << 8);
  170. }
  171. EXPORT_SYMBOL(sch56xx_read_virtual_reg16);
  172. int sch56xx_read_virtual_reg12(u16 addr, u16 msb_reg, u16 lsn_reg,
  173. int high_nibble)
  174. {
  175. int msb, lsn;
  176. /* Read MSB first, this will cause the matching LSN to be latched */
  177. msb = sch56xx_read_virtual_reg(addr, msb_reg);
  178. if (msb < 0)
  179. return msb;
  180. lsn = sch56xx_read_virtual_reg(addr, lsn_reg);
  181. if (lsn < 0)
  182. return lsn;
  183. if (high_nibble)
  184. return (msb << 4) | (lsn >> 4);
  185. else
  186. return (msb << 4) | (lsn & 0x0f);
  187. }
  188. EXPORT_SYMBOL(sch56xx_read_virtual_reg12);
  189. static int __init sch56xx_find(int sioaddr, unsigned short *address,
  190. const char **name)
  191. {
  192. u8 devid;
  193. int err;
  194. err = superio_enter(sioaddr);
  195. if (err)
  196. return err;
  197. devid = superio_inb(sioaddr, SIO_REG_DEVID);
  198. switch (devid) {
  199. case SIO_SCH5627_ID:
  200. *name = "sch5627";
  201. break;
  202. case SIO_SCH5636_ID:
  203. *name = "sch5636";
  204. break;
  205. default:
  206. pr_debug("Unsupported device id: 0x%02x\n",
  207. (unsigned int)devid);
  208. err = -ENODEV;
  209. goto exit;
  210. }
  211. superio_select(sioaddr, SIO_SCH56XX_LD_EM);
  212. if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
  213. pr_warn("Device not activated\n");
  214. err = -ENODEV;
  215. goto exit;
  216. }
  217. /*
  218. * Warning the order of the low / high byte is the other way around
  219. * as on most other superio devices!!
  220. */
  221. *address = superio_inb(sioaddr, SIO_REG_ADDR) |
  222. superio_inb(sioaddr, SIO_REG_ADDR + 1) << 8;
  223. if (*address == 0) {
  224. pr_warn("Base address not set\n");
  225. err = -ENODEV;
  226. goto exit;
  227. }
  228. exit:
  229. superio_exit(sioaddr);
  230. return err;
  231. }
  232. static int __init sch56xx_device_add(unsigned short address, const char *name)
  233. {
  234. struct resource res = {
  235. .start = address,
  236. .end = address + REGION_LENGTH - 1,
  237. .flags = IORESOURCE_IO,
  238. };
  239. int err;
  240. sch56xx_pdev = platform_device_alloc(name, address);
  241. if (!sch56xx_pdev)
  242. return -ENOMEM;
  243. res.name = sch56xx_pdev->name;
  244. err = acpi_check_resource_conflict(&res);
  245. if (err)
  246. goto exit_device_put;
  247. err = platform_device_add_resources(sch56xx_pdev, &res, 1);
  248. if (err) {
  249. pr_err("Device resource addition failed\n");
  250. goto exit_device_put;
  251. }
  252. err = platform_device_add(sch56xx_pdev);
  253. if (err) {
  254. pr_err("Device addition failed\n");
  255. goto exit_device_put;
  256. }
  257. return 0;
  258. exit_device_put:
  259. platform_device_put(sch56xx_pdev);
  260. return err;
  261. }
  262. static int __init sch56xx_init(void)
  263. {
  264. int err;
  265. unsigned short address;
  266. const char *name;
  267. err = sch56xx_find(0x4e, &address, &name);
  268. if (err)
  269. err = sch56xx_find(0x2e, &address, &name);
  270. if (err)
  271. return err;
  272. return sch56xx_device_add(address, name);
  273. }
  274. static void __exit sch56xx_exit(void)
  275. {
  276. platform_device_unregister(sch56xx_pdev);
  277. }
  278. MODULE_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code");
  279. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  280. MODULE_LICENSE("GPL");
  281. module_init(sch56xx_init);
  282. module_exit(sch56xx_exit);