1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Renesas Emma Mobile 8250 driver 4 * 5 * Copyright (C) 2012 Magnus Damm 6 */ 7 8 #include <linux/device.h> 9 #include <linux/io.h> 10 #include <linux/module.h> 11 #include <linux/serial_8250.h> 12 #include <linux/serial_reg.h> 13 #include <linux/platform_device.h> 14 #include <linux/clk.h> 15 #include <linux/slab.h> 16 17 #include "8250.h" 18 19 #define UART_DLL_EM 9 20 #define UART_DLM_EM 10 21 22 struct serial8250_em_priv { 23 struct clk *sclk; 24 int line; 25 }; 26 27 static void serial8250_em_serial_out(struct uart_port *p, int offset, int value) 28 { 29 switch (offset) { 30 case UART_TX: /* TX @ 0x00 */ 31 writeb(value, p->membase); 32 break; 33 case UART_FCR: /* FCR @ 0x0c (+1) */ 34 case UART_LCR: /* LCR @ 0x10 (+1) */ 35 case UART_MCR: /* MCR @ 0x14 (+1) */ 36 case UART_SCR: /* SCR @ 0x20 (+1) */ 37 writel(value, p->membase + ((offset + 1) << 2)); 38 break; 39 case UART_IER: /* IER @ 0x04 */ 40 value &= 0x0f; /* only 4 valid bits - not Xscale */ 41 /* fall-through */ 42 case UART_DLL_EM: /* DLL @ 0x24 (+9) */ 43 case UART_DLM_EM: /* DLM @ 0x28 (+9) */ 44 writel(value, p->membase + (offset << 2)); 45 } 46 } 47 48 static unsigned int serial8250_em_serial_in(struct uart_port *p, int offset) 49 { 50 switch (offset) { 51 case UART_RX: /* RX @ 0x00 */ 52 return readb(p->membase); 53 case UART_MCR: /* MCR @ 0x14 (+1) */ 54 case UART_LSR: /* LSR @ 0x18 (+1) */ 55 case UART_MSR: /* MSR @ 0x1c (+1) */ 56 case UART_SCR: /* SCR @ 0x20 (+1) */ 57 return readl(p->membase + ((offset + 1) << 2)); 58 case UART_IER: /* IER @ 0x04 */ 59 case UART_IIR: /* IIR @ 0x08 */ 60 case UART_DLL_EM: /* DLL @ 0x24 (+9) */ 61 case UART_DLM_EM: /* DLM @ 0x28 (+9) */ 62 return readl(p->membase + (offset << 2)); 63 } 64 return 0; 65 } 66 67 static int serial8250_em_serial_dl_read(struct uart_8250_port *up) 68 { 69 return serial_in(up, UART_DLL_EM) | serial_in(up, UART_DLM_EM) << 8; 70 } 71 72 static void serial8250_em_serial_dl_write(struct uart_8250_port *up, int value) 73 { 74 serial_out(up, UART_DLL_EM, value & 0xff); 75 serial_out(up, UART_DLM_EM, value >> 8 & 0xff); 76 } 77 78 static int serial8250_em_probe(struct platform_device *pdev) 79 { 80 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); 81 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 82 struct serial8250_em_priv *priv; 83 struct uart_8250_port up; 84 int ret; 85 86 if (!regs || !irq) { 87 dev_err(&pdev->dev, "missing registers or irq\n"); 88 return -EINVAL; 89 } 90 91 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 92 if (!priv) 93 return -ENOMEM; 94 95 priv->sclk = devm_clk_get(&pdev->dev, "sclk"); 96 if (IS_ERR(priv->sclk)) { 97 dev_err(&pdev->dev, "unable to get clock\n"); 98 return PTR_ERR(priv->sclk); 99 } 100 101 memset(&up, 0, sizeof(up)); 102 up.port.mapbase = regs->start; 103 up.port.irq = irq->start; 104 up.port.type = PORT_UNKNOWN; 105 up.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT | UPF_IOREMAP; 106 up.port.dev = &pdev->dev; 107 up.port.private_data = priv; 108 109 clk_prepare_enable(priv->sclk); 110 up.port.uartclk = clk_get_rate(priv->sclk); 111 112 up.port.iotype = UPIO_MEM32; 113 up.port.serial_in = serial8250_em_serial_in; 114 up.port.serial_out = serial8250_em_serial_out; 115 up.dl_read = serial8250_em_serial_dl_read; 116 up.dl_write = serial8250_em_serial_dl_write; 117 118 ret = serial8250_register_8250_port(&up); 119 if (ret < 0) { 120 dev_err(&pdev->dev, "unable to register 8250 port\n"); 121 clk_disable_unprepare(priv->sclk); 122 return ret; 123 } 124 125 priv->line = ret; 126 platform_set_drvdata(pdev, priv); 127 return 0; 128 } 129 130 static int serial8250_em_remove(struct platform_device *pdev) 131 { 132 struct serial8250_em_priv *priv = platform_get_drvdata(pdev); 133 134 serial8250_unregister_port(priv->line); 135 clk_disable_unprepare(priv->sclk); 136 return 0; 137 } 138 139 static const struct of_device_id serial8250_em_dt_ids[] = { 140 { .compatible = "renesas,em-uart", }, 141 {}, 142 }; 143 MODULE_DEVICE_TABLE(of, serial8250_em_dt_ids); 144 145 static struct platform_driver serial8250_em_platform_driver = { 146 .driver = { 147 .name = "serial8250-em", 148 .of_match_table = serial8250_em_dt_ids, 149 }, 150 .probe = serial8250_em_probe, 151 .remove = serial8250_em_remove, 152 }; 153 154 module_platform_driver(serial8250_em_platform_driver); 155 156 MODULE_AUTHOR("Magnus Damm"); 157 MODULE_DESCRIPTION("Renesas Emma Mobile 8250 Driver"); 158 MODULE_LICENSE("GPL v2"); 159