ibm_rtl.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * IBM Real-Time Linux driver
  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 Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2010
  19. *
  20. * Author: Keith Mannthey <kmannth@us.ibm.com>
  21. * Vernon Mauery <vernux@us.ibm.com>
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/delay.h>
  26. #include <linux/module.h>
  27. #include <linux/io.h>
  28. #include <linux/sysdev.h>
  29. #include <linux/dmi.h>
  30. #include <linux/efi.h>
  31. #include <linux/mutex.h>
  32. #include <asm/bios_ebda.h>
  33. static bool force;
  34. module_param(force, bool, 0);
  35. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  36. static bool debug;
  37. module_param(debug, bool, 0644);
  38. MODULE_PARM_DESC(debug, "Show debug output");
  39. MODULE_LICENSE("GPL");
  40. MODULE_AUTHOR("Keith Mannthey <kmmanth@us.ibm.com>");
  41. MODULE_AUTHOR("Vernon Mauery <vernux@us.ibm.com>");
  42. #define RTL_ADDR_TYPE_IO 1
  43. #define RTL_ADDR_TYPE_MMIO 2
  44. #define RTL_CMD_ENTER_PRTM 1
  45. #define RTL_CMD_EXIT_PRTM 2
  46. /* The RTL table as presented by the EBDA: */
  47. struct ibm_rtl_table {
  48. char signature[5]; /* signature should be "_RTL_" */
  49. u8 version;
  50. u8 rt_status;
  51. u8 command;
  52. u8 command_status;
  53. u8 cmd_address_type;
  54. u8 cmd_granularity;
  55. u8 cmd_offset;
  56. u16 reserve1;
  57. u32 cmd_port_address; /* platform dependent address */
  58. u32 cmd_port_value; /* platform dependent value */
  59. } __attribute__((packed));
  60. /* to locate "_RTL_" signature do a masked 5-byte integer compare */
  61. #define RTL_SIGNATURE 0x0000005f4c54525fULL
  62. #define RTL_MASK 0x000000ffffffffffULL
  63. #define RTL_DEBUG(A, ...) do { \
  64. if (debug) \
  65. pr_info("ibm-rtl: " A, ##__VA_ARGS__ ); \
  66. } while (0)
  67. static DEFINE_MUTEX(rtl_lock);
  68. static struct ibm_rtl_table __iomem *rtl_table;
  69. static void __iomem *ebda_map;
  70. static void __iomem *rtl_cmd_addr;
  71. static u8 rtl_cmd_type;
  72. static u8 rtl_cmd_width;
  73. #ifndef readq
  74. static inline __u64 readq(const volatile void __iomem *addr)
  75. {
  76. const volatile u32 __iomem *p = addr;
  77. u32 low, high;
  78. low = readl(p);
  79. high = readl(p + 1);
  80. return low + ((u64)high << 32);
  81. }
  82. #endif
  83. static void __iomem *rtl_port_map(phys_addr_t addr, unsigned long len)
  84. {
  85. if (rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
  86. return ioremap(addr, len);
  87. return ioport_map(addr, len);
  88. }
  89. static void rtl_port_unmap(void __iomem *addr)
  90. {
  91. if (addr && rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
  92. iounmap(addr);
  93. else
  94. ioport_unmap(addr);
  95. }
  96. static int ibm_rtl_write(u8 value)
  97. {
  98. int ret = 0, count = 0;
  99. static u32 cmd_port_val;
  100. RTL_DEBUG("%s(%d)\n", __FUNCTION__, value);
  101. value = value == 1 ? RTL_CMD_ENTER_PRTM : RTL_CMD_EXIT_PRTM;
  102. mutex_lock(&rtl_lock);
  103. if (ioread8(&rtl_table->rt_status) != value) {
  104. iowrite8(value, &rtl_table->command);
  105. switch (rtl_cmd_width) {
  106. case 8:
  107. cmd_port_val = ioread8(&rtl_table->cmd_port_value);
  108. RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
  109. iowrite8((u8)cmd_port_val, rtl_cmd_addr);
  110. break;
  111. case 16:
  112. cmd_port_val = ioread16(&rtl_table->cmd_port_value);
  113. RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
  114. iowrite16((u16)cmd_port_val, rtl_cmd_addr);
  115. break;
  116. case 32:
  117. cmd_port_val = ioread32(&rtl_table->cmd_port_value);
  118. RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
  119. iowrite32(cmd_port_val, rtl_cmd_addr);
  120. break;
  121. }
  122. while (ioread8(&rtl_table->command)) {
  123. msleep(10);
  124. if (count++ > 500) {
  125. pr_err("ibm-rtl: Hardware not responding to "
  126. "mode switch request\n");
  127. ret = -EIO;
  128. break;
  129. }
  130. }
  131. if (ioread8(&rtl_table->command_status)) {
  132. RTL_DEBUG("command_status reports failed command\n");
  133. ret = -EIO;
  134. }
  135. }
  136. mutex_unlock(&rtl_lock);
  137. return ret;
  138. }
  139. static ssize_t rtl_show_version(struct sysdev_class * dev,
  140. struct sysdev_class_attribute *attr,
  141. char *buf)
  142. {
  143. return sprintf(buf, "%d\n", (int)ioread8(&rtl_table->version));
  144. }
  145. static ssize_t rtl_show_state(struct sysdev_class *dev,
  146. struct sysdev_class_attribute *attr,
  147. char *buf)
  148. {
  149. return sprintf(buf, "%d\n", ioread8(&rtl_table->rt_status));
  150. }
  151. static ssize_t rtl_set_state(struct sysdev_class *dev,
  152. struct sysdev_class_attribute *attr,
  153. const char *buf,
  154. size_t count)
  155. {
  156. ssize_t ret;
  157. if (count < 1 || count > 2)
  158. return -EINVAL;
  159. switch (buf[0]) {
  160. case '0':
  161. ret = ibm_rtl_write(0);
  162. break;
  163. case '1':
  164. ret = ibm_rtl_write(1);
  165. break;
  166. default:
  167. ret = -EINVAL;
  168. }
  169. if (ret >= 0)
  170. ret = count;
  171. return ret;
  172. }
  173. static struct sysdev_class class_rtl = {
  174. .name = "ibm_rtl",
  175. };
  176. static SYSDEV_CLASS_ATTR(version, S_IRUGO, rtl_show_version, NULL);
  177. static SYSDEV_CLASS_ATTR(state, 0600, rtl_show_state, rtl_set_state);
  178. static struct sysdev_class_attribute *rtl_attributes[] = {
  179. &attr_version,
  180. &attr_state,
  181. NULL
  182. };
  183. static int rtl_setup_sysfs(void) {
  184. int ret, i;
  185. ret = sysdev_class_register(&class_rtl);
  186. if (!ret) {
  187. for (i = 0; rtl_attributes[i]; i ++)
  188. sysdev_class_create_file(&class_rtl, rtl_attributes[i]);
  189. }
  190. return ret;
  191. }
  192. static void rtl_teardown_sysfs(void) {
  193. int i;
  194. for (i = 0; rtl_attributes[i]; i ++)
  195. sysdev_class_remove_file(&class_rtl, rtl_attributes[i]);
  196. sysdev_class_unregister(&class_rtl);
  197. }
  198. static struct dmi_system_id __initdata ibm_rtl_dmi_table[] = {
  199. { \
  200. .matches = { \
  201. DMI_MATCH(DMI_SYS_VENDOR, "IBM"), \
  202. }, \
  203. },
  204. { }
  205. };
  206. static int __init ibm_rtl_init(void) {
  207. unsigned long ebda_addr, ebda_size;
  208. unsigned int ebda_kb;
  209. int ret = -ENODEV, i;
  210. if (force)
  211. pr_warning("ibm-rtl: module loaded by force\n");
  212. /* first ensure that we are running on IBM HW */
  213. else if (efi_enabled || !dmi_check_system(ibm_rtl_dmi_table))
  214. return -ENODEV;
  215. /* Get the address for the Extended BIOS Data Area */
  216. ebda_addr = get_bios_ebda();
  217. if (!ebda_addr) {
  218. RTL_DEBUG("no BIOS EBDA found\n");
  219. return -ENODEV;
  220. }
  221. ebda_map = ioremap(ebda_addr, 4);
  222. if (!ebda_map)
  223. return -ENOMEM;
  224. /* First word in the EDBA is the Size in KB */
  225. ebda_kb = ioread16(ebda_map);
  226. RTL_DEBUG("EBDA is %d kB\n", ebda_kb);
  227. if (ebda_kb == 0)
  228. goto out;
  229. iounmap(ebda_map);
  230. ebda_size = ebda_kb*1024;
  231. /* Remap the whole table */
  232. ebda_map = ioremap(ebda_addr, ebda_size);
  233. if (!ebda_map)
  234. return -ENOMEM;
  235. /* search for the _RTL_ signature at the start of the table */
  236. for (i = 0 ; i < ebda_size/sizeof(unsigned int); i++) {
  237. struct ibm_rtl_table __iomem * tmp;
  238. tmp = (struct ibm_rtl_table __iomem *) (ebda_map+i);
  239. if ((readq(&tmp->signature) & RTL_MASK) == RTL_SIGNATURE) {
  240. phys_addr_t addr;
  241. unsigned int plen;
  242. RTL_DEBUG("found RTL_SIGNATURE at %#llx\n", (u64)tmp);
  243. rtl_table = tmp;
  244. /* The address, value, width and offset are platform
  245. * dependent and found in the ibm_rtl_table */
  246. rtl_cmd_width = ioread8(&rtl_table->cmd_granularity);
  247. rtl_cmd_type = ioread8(&rtl_table->cmd_address_type);
  248. RTL_DEBUG("rtl_cmd_width = %u, rtl_cmd_type = %u\n",
  249. rtl_cmd_width, rtl_cmd_type);
  250. addr = ioread32(&rtl_table->cmd_port_address);
  251. RTL_DEBUG("addr = %#llx\n", (unsigned long long)addr);
  252. plen = rtl_cmd_width/sizeof(char);
  253. rtl_cmd_addr = rtl_port_map(addr, plen);
  254. RTL_DEBUG("rtl_cmd_addr = %#llx\n", (u64)rtl_cmd_addr);
  255. if (!rtl_cmd_addr) {
  256. ret = -ENOMEM;
  257. break;
  258. }
  259. ret = rtl_setup_sysfs();
  260. break;
  261. }
  262. }
  263. out:
  264. if (ret) {
  265. iounmap(ebda_map);
  266. rtl_port_unmap(rtl_cmd_addr);
  267. }
  268. return ret;
  269. }
  270. static void __exit ibm_rtl_exit(void)
  271. {
  272. if (rtl_table) {
  273. RTL_DEBUG("cleaning up");
  274. /* do not leave the machine in SMI-free mode */
  275. ibm_rtl_write(0);
  276. /* unmap, unlink and remove all traces */
  277. rtl_teardown_sysfs();
  278. iounmap(ebda_map);
  279. rtl_port_unmap(rtl_cmd_addr);
  280. }
  281. }
  282. module_init(ibm_rtl_init);
  283. module_exit(ibm_rtl_exit);