cpm_uart_cpm2.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for CPM (SCC/SMC) serial ports; CPM2 definitions
  4. *
  5. * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
  6. * Pantelis Antoniou (panto@intracom.gr) (CPM1)
  7. *
  8. * Copyright (C) 2004 Freescale Semiconductor, Inc.
  9. * (C) 2004 Intracom, S.A.
  10. * (C) 2006 MontaVista Software, Inc.
  11. * Vitaly Bordug <vbordug@ru.mvista.com>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/tty.h>
  15. #include <linux/ioport.h>
  16. #include <linux/slab.h>
  17. #include <linux/serial.h>
  18. #include <linux/console.h>
  19. #include <linux/sysrq.h>
  20. #include <linux/device.h>
  21. #include <linux/memblock.h>
  22. #include <linux/dma-mapping.h>
  23. #include <asm/io.h>
  24. #include <asm/irq.h>
  25. #include <asm/fs_pd.h>
  26. #include <asm/prom.h>
  27. #include <linux/serial_core.h>
  28. #include <linux/kernel.h>
  29. #include "cpm_uart.h"
  30. /**************************************************************/
  31. void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
  32. {
  33. cpm_command(port->command, cmd);
  34. }
  35. void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
  36. struct device_node *np)
  37. {
  38. void __iomem *pram;
  39. unsigned long offset;
  40. struct resource res;
  41. resource_size_t len;
  42. /* Don't remap parameter RAM if it has already been initialized
  43. * during console setup.
  44. */
  45. if (IS_SMC(port) && port->smcup)
  46. return port->smcup;
  47. else if (!IS_SMC(port) && port->sccup)
  48. return port->sccup;
  49. if (of_address_to_resource(np, 1, &res))
  50. return NULL;
  51. len = resource_size(&res);
  52. pram = ioremap(res.start, len);
  53. if (!pram)
  54. return NULL;
  55. if (!IS_SMC(port))
  56. return pram;
  57. if (len != 2) {
  58. printk(KERN_WARNING "cpm_uart[%d]: device tree references "
  59. "SMC pram, using boot loader/wrapper pram mapping. "
  60. "Please fix your device tree to reference the pram "
  61. "base register instead.\n",
  62. port->port.line);
  63. return pram;
  64. }
  65. offset = cpm_dpalloc(PROFF_SMC_SIZE, 64);
  66. out_be16(pram, offset);
  67. iounmap(pram);
  68. return cpm_muram_addr(offset);
  69. }
  70. void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram)
  71. {
  72. if (!IS_SMC(port))
  73. iounmap(pram);
  74. }
  75. /*
  76. * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
  77. * receive buffer descriptors from dual port ram, and a character
  78. * buffer area from host mem. If we are allocating for the console we need
  79. * to do it from bootmem
  80. */
  81. int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
  82. {
  83. int dpmemsz, memsz;
  84. u8 __iomem *dp_mem;
  85. unsigned long dp_offset;
  86. u8 *mem_addr;
  87. dma_addr_t dma_addr = 0;
  88. pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
  89. dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
  90. dp_offset = cpm_dpalloc(dpmemsz, 8);
  91. if (IS_ERR_VALUE(dp_offset)) {
  92. printk(KERN_ERR
  93. "cpm_uart_cpm.c: could not allocate buffer descriptors\n");
  94. return -ENOMEM;
  95. }
  96. dp_mem = cpm_dpram_addr(dp_offset);
  97. memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
  98. L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
  99. if (is_con) {
  100. mem_addr = kzalloc(memsz, GFP_NOWAIT);
  101. dma_addr = virt_to_bus(mem_addr);
  102. }
  103. else
  104. mem_addr = dma_alloc_coherent(pinfo->port.dev, memsz, &dma_addr,
  105. GFP_KERNEL);
  106. if (mem_addr == NULL) {
  107. cpm_dpfree(dp_offset);
  108. printk(KERN_ERR
  109. "cpm_uart_cpm.c: could not allocate coherent memory\n");
  110. return -ENOMEM;
  111. }
  112. pinfo->dp_addr = dp_offset;
  113. pinfo->mem_addr = mem_addr;
  114. pinfo->dma_addr = dma_addr;
  115. pinfo->mem_size = memsz;
  116. pinfo->rx_buf = mem_addr;
  117. pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
  118. * pinfo->rx_fifosize);
  119. pinfo->rx_bd_base = (cbd_t __iomem *)dp_mem;
  120. pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
  121. return 0;
  122. }
  123. void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
  124. {
  125. dma_free_coherent(pinfo->port.dev, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
  126. pinfo->rx_fifosize) +
  127. L1_CACHE_ALIGN(pinfo->tx_nrfifos *
  128. pinfo->tx_fifosize), (void __force *)pinfo->mem_addr,
  129. pinfo->dma_addr);
  130. cpm_dpfree(pinfo->dp_addr);
  131. }