dpaa2-console.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * Freescale DPAA2 Platforms Console Driver
  4. *
  5. * Copyright 2015-2016 Freescale Semiconductor Inc.
  6. * Copyright 2018 NXP
  7. */
  8. #define pr_fmt(fmt) "dpaa2-console: " fmt
  9. #include <linux/module.h>
  10. #include <linux/of_device.h>
  11. #include <linux/of_address.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/slab.h>
  15. #include <linux/fs.h>
  16. #include <linux/io.h>
  17. /* MC firmware base low/high registers indexes */
  18. #define MCFBALR_OFFSET 0
  19. #define MCFBAHR_OFFSET 1
  20. /* Bit masks used to get the most/least significant part of the MC base addr */
  21. #define MC_FW_ADDR_MASK_HIGH 0x1FFFF
  22. #define MC_FW_ADDR_MASK_LOW 0xE0000000
  23. #define MC_BUFFER_OFFSET 0x01000000
  24. #define MC_BUFFER_SIZE (1024 * 1024 * 16)
  25. #define MC_OFFSET_DELTA MC_BUFFER_OFFSET
  26. #define AIOP_BUFFER_OFFSET 0x06000000
  27. #define AIOP_BUFFER_SIZE (1024 * 1024 * 16)
  28. #define AIOP_OFFSET_DELTA 0
  29. #define LOG_HEADER_FLAG_BUFFER_WRAPAROUND 0x80000000
  30. #define LAST_BYTE(a) ((a) & ~(LOG_HEADER_FLAG_BUFFER_WRAPAROUND))
  31. /* MC and AIOP Magic words */
  32. #define MAGIC_MC 0x4d430100
  33. #define MAGIC_AIOP 0x41494F50
  34. struct log_header {
  35. __le32 magic_word;
  36. char reserved[4];
  37. __le32 buf_start;
  38. __le32 buf_length;
  39. __le32 last_byte;
  40. };
  41. struct console_data {
  42. void __iomem *map_addr;
  43. struct log_header __iomem *hdr;
  44. void __iomem *start_addr;
  45. void __iomem *end_addr;
  46. void __iomem *end_of_data;
  47. void __iomem *cur_ptr;
  48. };
  49. static struct resource mc_base_addr;
  50. static inline void adjust_end(struct console_data *cd)
  51. {
  52. u32 last_byte = readl(&cd->hdr->last_byte);
  53. cd->end_of_data = cd->start_addr + LAST_BYTE(last_byte);
  54. }
  55. static u64 get_mc_fw_base_address(void)
  56. {
  57. u64 mcfwbase = 0ULL;
  58. u32 __iomem *mcfbaregs;
  59. mcfbaregs = ioremap(mc_base_addr.start, resource_size(&mc_base_addr));
  60. if (!mcfbaregs) {
  61. pr_err("could not map MC Firmware Base registers\n");
  62. return 0;
  63. }
  64. mcfwbase = readl(mcfbaregs + MCFBAHR_OFFSET) &
  65. MC_FW_ADDR_MASK_HIGH;
  66. mcfwbase <<= 32;
  67. mcfwbase |= readl(mcfbaregs + MCFBALR_OFFSET) & MC_FW_ADDR_MASK_LOW;
  68. iounmap(mcfbaregs);
  69. pr_debug("MC base address at 0x%016llx\n", mcfwbase);
  70. return mcfwbase;
  71. }
  72. static ssize_t dpaa2_console_size(struct console_data *cd)
  73. {
  74. ssize_t size;
  75. if (cd->cur_ptr <= cd->end_of_data)
  76. size = cd->end_of_data - cd->cur_ptr;
  77. else
  78. size = (cd->end_addr - cd->cur_ptr) +
  79. (cd->end_of_data - cd->start_addr);
  80. return size;
  81. }
  82. static int dpaa2_generic_console_open(struct inode *node, struct file *fp,
  83. u64 offset, u64 size,
  84. u32 expected_magic,
  85. u32 offset_delta)
  86. {
  87. u32 read_magic, wrapped, last_byte, buf_start, buf_length;
  88. struct console_data *cd;
  89. u64 base_addr;
  90. int err;
  91. cd = kmalloc(sizeof(*cd), GFP_KERNEL);
  92. if (!cd)
  93. return -ENOMEM;
  94. base_addr = get_mc_fw_base_address();
  95. if (!base_addr) {
  96. err = -EIO;
  97. goto err_fwba;
  98. }
  99. cd->map_addr = ioremap(base_addr + offset, size);
  100. if (!cd->map_addr) {
  101. pr_err("cannot map console log memory\n");
  102. err = -EIO;
  103. goto err_ioremap;
  104. }
  105. cd->hdr = (struct log_header __iomem *)cd->map_addr;
  106. read_magic = readl(&cd->hdr->magic_word);
  107. last_byte = readl(&cd->hdr->last_byte);
  108. buf_start = readl(&cd->hdr->buf_start);
  109. buf_length = readl(&cd->hdr->buf_length);
  110. if (read_magic != expected_magic) {
  111. pr_warn("expected = %08x, read = %08x\n",
  112. expected_magic, read_magic);
  113. err = -EIO;
  114. goto err_magic;
  115. }
  116. cd->start_addr = cd->map_addr + buf_start - offset_delta;
  117. cd->end_addr = cd->start_addr + buf_length;
  118. wrapped = last_byte & LOG_HEADER_FLAG_BUFFER_WRAPAROUND;
  119. adjust_end(cd);
  120. if (wrapped && cd->end_of_data != cd->end_addr)
  121. cd->cur_ptr = cd->end_of_data + 1;
  122. else
  123. cd->cur_ptr = cd->start_addr;
  124. fp->private_data = cd;
  125. return 0;
  126. err_magic:
  127. iounmap(cd->map_addr);
  128. err_ioremap:
  129. err_fwba:
  130. kfree(cd);
  131. return err;
  132. }
  133. static int dpaa2_mc_console_open(struct inode *node, struct file *fp)
  134. {
  135. return dpaa2_generic_console_open(node, fp,
  136. MC_BUFFER_OFFSET, MC_BUFFER_SIZE,
  137. MAGIC_MC, MC_OFFSET_DELTA);
  138. }
  139. static int dpaa2_aiop_console_open(struct inode *node, struct file *fp)
  140. {
  141. return dpaa2_generic_console_open(node, fp,
  142. AIOP_BUFFER_OFFSET, AIOP_BUFFER_SIZE,
  143. MAGIC_AIOP, AIOP_OFFSET_DELTA);
  144. }
  145. static int dpaa2_console_close(struct inode *node, struct file *fp)
  146. {
  147. struct console_data *cd = fp->private_data;
  148. iounmap(cd->map_addr);
  149. kfree(cd);
  150. return 0;
  151. }
  152. static ssize_t dpaa2_console_read(struct file *fp, char __user *buf,
  153. size_t count, loff_t *f_pos)
  154. {
  155. struct console_data *cd = fp->private_data;
  156. size_t bytes = dpaa2_console_size(cd);
  157. size_t bytes_end = cd->end_addr - cd->cur_ptr;
  158. size_t written = 0;
  159. void *kbuf;
  160. int err;
  161. /* Check if we need to adjust the end of data addr */
  162. adjust_end(cd);
  163. if (cd->end_of_data == cd->cur_ptr)
  164. return 0;
  165. if (count < bytes)
  166. bytes = count;
  167. kbuf = kmalloc(bytes, GFP_KERNEL);
  168. if (!kbuf)
  169. return -ENOMEM;
  170. if (bytes > bytes_end) {
  171. memcpy_fromio(kbuf, cd->cur_ptr, bytes_end);
  172. if (copy_to_user(buf, kbuf, bytes_end)) {
  173. err = -EFAULT;
  174. goto err_free_buf;
  175. }
  176. buf += bytes_end;
  177. cd->cur_ptr = cd->start_addr;
  178. bytes -= bytes_end;
  179. written += bytes_end;
  180. }
  181. memcpy_fromio(kbuf, cd->cur_ptr, bytes);
  182. if (copy_to_user(buf, kbuf, bytes)) {
  183. err = -EFAULT;
  184. goto err_free_buf;
  185. }
  186. cd->cur_ptr += bytes;
  187. written += bytes;
  188. return written;
  189. err_free_buf:
  190. kfree(kbuf);
  191. return err;
  192. }
  193. static const struct file_operations dpaa2_mc_console_fops = {
  194. .owner = THIS_MODULE,
  195. .open = dpaa2_mc_console_open,
  196. .release = dpaa2_console_close,
  197. .read = dpaa2_console_read,
  198. };
  199. static struct miscdevice dpaa2_mc_console_dev = {
  200. .minor = MISC_DYNAMIC_MINOR,
  201. .name = "dpaa2_mc_console",
  202. .fops = &dpaa2_mc_console_fops
  203. };
  204. static const struct file_operations dpaa2_aiop_console_fops = {
  205. .owner = THIS_MODULE,
  206. .open = dpaa2_aiop_console_open,
  207. .release = dpaa2_console_close,
  208. .read = dpaa2_console_read,
  209. };
  210. static struct miscdevice dpaa2_aiop_console_dev = {
  211. .minor = MISC_DYNAMIC_MINOR,
  212. .name = "dpaa2_aiop_console",
  213. .fops = &dpaa2_aiop_console_fops
  214. };
  215. static int dpaa2_console_probe(struct platform_device *pdev)
  216. {
  217. int error;
  218. error = of_address_to_resource(pdev->dev.of_node, 0, &mc_base_addr);
  219. if (error < 0) {
  220. pr_err("of_address_to_resource() failed for %pOF with %d\n",
  221. pdev->dev.of_node, error);
  222. return error;
  223. }
  224. error = misc_register(&dpaa2_mc_console_dev);
  225. if (error) {
  226. pr_err("cannot register device %s\n",
  227. dpaa2_mc_console_dev.name);
  228. goto err_register_mc;
  229. }
  230. error = misc_register(&dpaa2_aiop_console_dev);
  231. if (error) {
  232. pr_err("cannot register device %s\n",
  233. dpaa2_aiop_console_dev.name);
  234. goto err_register_aiop;
  235. }
  236. return 0;
  237. err_register_aiop:
  238. misc_deregister(&dpaa2_mc_console_dev);
  239. err_register_mc:
  240. return error;
  241. }
  242. static int dpaa2_console_remove(struct platform_device *pdev)
  243. {
  244. misc_deregister(&dpaa2_mc_console_dev);
  245. misc_deregister(&dpaa2_aiop_console_dev);
  246. return 0;
  247. }
  248. static const struct of_device_id dpaa2_console_match_table[] = {
  249. { .compatible = "fsl,dpaa2-console",},
  250. {},
  251. };
  252. MODULE_DEVICE_TABLE(of, dpaa2_console_match_table);
  253. static struct platform_driver dpaa2_console_driver = {
  254. .driver = {
  255. .name = "dpaa2-console",
  256. .pm = NULL,
  257. .of_match_table = dpaa2_console_match_table,
  258. },
  259. .probe = dpaa2_console_probe,
  260. .remove = dpaa2_console_remove,
  261. };
  262. module_platform_driver(dpaa2_console_driver);
  263. MODULE_LICENSE("Dual BSD/GPL");
  264. MODULE_AUTHOR("Roy Pledge <roy.pledge@nxp.com>");
  265. MODULE_DESCRIPTION("DPAA2 console driver");