ibm_rtl.c 8.1 KB

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