Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Driver for the PSC of the Freescale MPC52xx PSCs configured as UARTs. |
| 3 | * |
| 4 | * FIXME According to the usermanual the status bits in the status register |
| 5 | * are only updated when the peripherals access the FIFO and not when the |
| 6 | * CPU access them. So since we use this bits to know when we stop writing |
| 7 | * and reading, they may not be updated in-time and a race condition may |
| 8 | * exists. But I haven't be able to prove this and I don't care. But if |
| 9 | * any problem arises, it might worth checking. The TX/RX FIFO Stats |
| 10 | * registers should be used in addition. |
| 11 | * Update: Actually, they seem updated ... At least the bits we use. |
| 12 | * |
| 13 | * |
| 14 | * Maintainer : Sylvain Munaut <tnt@246tNt.com> |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 15 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * Some of the code has been inspired/copied from the 2.4 code written |
| 17 | * by Dale Farnsworth <dfarnsworth@mvista.com>. |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 18 | * |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 19 | * Copyright (C) 2006 Secret Lab Technologies Ltd. |
| 20 | * Grant Likely <grant.likely@secretlab.ca> |
| 21 | * Copyright (C) 2004-2006 Sylvain Munaut <tnt@246tNt.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * Copyright (C) 2003 MontaVista, Software, Inc. |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * This file is licensed under the terms of the GNU General Public License |
| 25 | * version 2. This program is licensed "as is" without any warranty of any |
| 26 | * kind, whether express or implied. |
| 27 | */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | /* Platform device Usage : |
| 30 | * |
| 31 | * Since PSCs can have multiple function, the correct driver for each one |
| 32 | * is selected by calling mpc52xx_match_psc_function(...). The function |
| 33 | * handled by this driver is "uart". |
| 34 | * |
| 35 | * The driver init all necessary registers to place the PSC in uart mode without |
| 36 | * DCD. However, the pin multiplexing aren't changed and should be set either |
| 37 | * by the bootloader or in the platform init code. |
| 38 | * |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 39 | * The idx field must be equal to the PSC index (e.g. 0 for PSC1, 1 for PSC2, |
Sylvain Munaut | d62de3a | 2006-01-06 00:11:32 -0800 | [diff] [blame] | 40 | * and so on). So the PSC1 is mapped to /dev/ttyPSC0, PSC2 to /dev/ttyPSC1 and |
| 41 | * so on. But be warned, it's an ABSOLUTE REQUIREMENT ! This is needed mainly |
| 42 | * fpr the console code : without this 1:1 mapping, at early boot time, when we |
Uwe Zeisberger | c30fe7f | 2006-03-24 18:23:14 +0100 | [diff] [blame] | 43 | * are parsing the kernel args console=ttyPSC?, we wouldn't know which PSC it |
Sylvain Munaut | d62de3a | 2006-01-06 00:11:32 -0800 | [diff] [blame] | 44 | * will be mapped to. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | */ |
| 46 | |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 47 | /* OF Platform device Usage : |
| 48 | * |
| 49 | * This driver is only used for PSCs configured in uart mode. The device |
| 50 | * tree will have a node for each PSC in uart mode w/ device_type = "serial" |
| 51 | * and "mpc52xx-psc-uart" in the compatible string |
| 52 | * |
| 53 | * By default, PSC devices are enumerated in the order they are found. However |
| 54 | * a particular PSC number can be forces by adding 'device_no = <port#>' |
| 55 | * to the device node. |
| 56 | * |
| 57 | * The driver init all necessary registers to place the PSC in uart mode without |
| 58 | * DCD. However, the pin multiplexing aren't changed and should be set either |
| 59 | * by the bootloader or in the platform init code. |
| 60 | */ |
| 61 | |
| 62 | #undef DEBUG |
| 63 | |
| 64 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #include <linux/module.h> |
| 66 | #include <linux/tty.h> |
| 67 | #include <linux/serial.h> |
| 68 | #include <linux/sysrq.h> |
| 69 | #include <linux/console.h> |
| 70 | |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 71 | #include <linux/delay.h> |
| 72 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 74 | #if defined(CONFIG_PPC_MERGE) |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 75 | #include <linux/of.h> |
| 76 | #include <linux/of_platform.h> |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 77 | #else |
| 78 | #include <linux/platform_device.h> |
| 79 | #endif |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | #include <asm/mpc52xx.h> |
| 82 | #include <asm/mpc52xx_psc.h> |
| 83 | |
| 84 | #if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
| 85 | #define SUPPORT_SYSRQ |
| 86 | #endif |
| 87 | |
| 88 | #include <linux/serial_core.h> |
| 89 | |
| 90 | |
Sylvain Munaut | d62de3a | 2006-01-06 00:11:32 -0800 | [diff] [blame] | 91 | /* We've been assigned a range on the "Low-density serial ports" major */ |
| 92 | #define SERIAL_PSC_MAJOR 204 |
| 93 | #define SERIAL_PSC_MINOR 148 |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | #define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */ |
| 97 | |
| 98 | |
| 99 | static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM]; |
| 100 | /* Rem: - We use the read_status_mask as a shadow of |
| 101 | * psc->mpc52xx_psc_imr |
| 102 | * - It's important that is array is all zero on start as we |
| 103 | * use it to know if it's initialized or not ! If it's not sure |
| 104 | * it's cleared, then a memset(...,0,...) should be added to |
| 105 | * the console_init |
| 106 | */ |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 107 | #if defined(CONFIG_PPC_MERGE) |
| 108 | /* lookup table for matching device nodes to index numbers */ |
| 109 | static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM]; |
| 110 | |
| 111 | static void mpc52xx_uart_of_enumerate(void); |
| 112 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | #define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase)) |
John Rigby | 94f3894 | 2008-01-17 08:37:24 +1100 | [diff] [blame] | 115 | #define FIFO(port) ((struct mpc52xx_psc_fifo __iomem *)(PSC(port)+1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | |
| 118 | /* Forward declaration of the interruption handling routine */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 119 | static irqreturn_t mpc52xx_uart_int(int irq, void *dev_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | |
| 122 | /* Simple macro to test if a port is console or not. This one is taken |
| 123 | * for serial_core.c and maybe should be moved to serial_core.h ? */ |
| 124 | #ifdef CONFIG_SERIAL_CORE_CONSOLE |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 125 | #define uart_console(port) \ |
| 126 | ((port)->cons && (port)->cons->index == (port)->line) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | #else |
| 128 | #define uart_console(port) (0) |
| 129 | #endif |
| 130 | |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 131 | #if defined(CONFIG_PPC_MERGE) |
| 132 | static struct of_device_id mpc52xx_uart_of_match[] = { |
Grant Likely | e3aba81 | 2007-02-12 13:36:55 -0700 | [diff] [blame] | 133 | { .type = "serial", .compatible = "mpc5200-psc-uart", }, |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 134 | {}, |
| 135 | }; |
| 136 | #endif |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | /* ======================================================================== */ |
| 140 | /* UART operations */ |
| 141 | /* ======================================================================== */ |
| 142 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 143 | static unsigned int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | mpc52xx_uart_tx_empty(struct uart_port *port) |
| 145 | { |
| 146 | int status = in_be16(&PSC(port)->mpc52xx_psc_status); |
| 147 | return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0; |
| 148 | } |
| 149 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 150 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 152 | { |
| 153 | /* Not implemented */ |
| 154 | } |
| 155 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 156 | static unsigned int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | mpc52xx_uart_get_mctrl(struct uart_port *port) |
| 158 | { |
| 159 | /* Not implemented */ |
| 160 | return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR; |
| 161 | } |
| 162 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 163 | static void |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 164 | mpc52xx_uart_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
| 166 | /* port->lock taken by caller */ |
| 167 | port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY; |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 168 | out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 171 | static void |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 172 | mpc52xx_uart_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
| 174 | /* port->lock taken by caller */ |
| 175 | port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY; |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 176 | out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 179 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | mpc52xx_uart_send_xchar(struct uart_port *port, char ch) |
| 181 | { |
| 182 | unsigned long flags; |
| 183 | spin_lock_irqsave(&port->lock, flags); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | port->x_char = ch; |
| 186 | if (ch) { |
| 187 | /* Make sure tx interrupts are on */ |
| 188 | /* Truly necessary ??? They should be anyway */ |
| 189 | port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY; |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 190 | out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | spin_unlock_irqrestore(&port->lock, flags); |
| 194 | } |
| 195 | |
| 196 | static void |
| 197 | mpc52xx_uart_stop_rx(struct uart_port *port) |
| 198 | { |
| 199 | /* port->lock taken by caller */ |
| 200 | port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY; |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 201 | out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void |
| 205 | mpc52xx_uart_enable_ms(struct uart_port *port) |
| 206 | { |
| 207 | /* Not implemented */ |
| 208 | } |
| 209 | |
| 210 | static void |
| 211 | mpc52xx_uart_break_ctl(struct uart_port *port, int ctl) |
| 212 | { |
| 213 | unsigned long flags; |
| 214 | spin_lock_irqsave(&port->lock, flags); |
| 215 | |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 216 | if (ctl == -1) |
| 217 | out_8(&PSC(port)->command, MPC52xx_PSC_START_BRK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | else |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 219 | out_8(&PSC(port)->command, MPC52xx_PSC_STOP_BRK); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | spin_unlock_irqrestore(&port->lock, flags); |
| 222 | } |
| 223 | |
| 224 | static int |
| 225 | mpc52xx_uart_startup(struct uart_port *port) |
| 226 | { |
| 227 | struct mpc52xx_psc __iomem *psc = PSC(port); |
John Rigby | 94f3894 | 2008-01-17 08:37:24 +1100 | [diff] [blame] | 228 | struct mpc52xx_psc_fifo __iomem *fifo = FIFO(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | int ret; |
| 230 | |
| 231 | /* Request IRQ */ |
| 232 | ret = request_irq(port->irq, mpc52xx_uart_int, |
Thomas Gleixner | 40663cc | 2006-07-01 19:29:43 -0700 | [diff] [blame] | 233 | IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | if (ret) |
| 235 | return ret; |
| 236 | |
| 237 | /* Reset/activate the port, clear and enable interrupts */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 238 | out_8(&psc->command, MPC52xx_PSC_RST_RX); |
| 239 | out_8(&psc->command, MPC52xx_PSC_RST_TX); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 240 | |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 241 | out_be32(&psc->sicr, 0); /* UART mode DCD ignored */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 244 | |
John Rigby | 94f3894 | 2008-01-17 08:37:24 +1100 | [diff] [blame] | 245 | out_8(&fifo->rfcntl, 0x00); |
| 246 | out_be16(&fifo->rfalarm, 0x1ff); |
| 247 | out_8(&fifo->tfcntl, 0x07); |
| 248 | out_be16(&fifo->tfalarm, 0x80); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
| 250 | port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY; |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 251 | out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 252 | |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 253 | out_8(&psc->command, MPC52xx_PSC_TX_ENABLE); |
| 254 | out_8(&psc->command, MPC52xx_PSC_RX_ENABLE); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static void |
| 260 | mpc52xx_uart_shutdown(struct uart_port *port) |
| 261 | { |
| 262 | struct mpc52xx_psc __iomem *psc = PSC(port); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 263 | |
Grant Likely | a348119 | 2007-05-07 01:38:51 +1000 | [diff] [blame] | 264 | /* Shut down the port. Leave TX active if on a console port */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 265 | out_8(&psc->command, MPC52xx_PSC_RST_RX); |
Grant Likely | a348119 | 2007-05-07 01:38:51 +1000 | [diff] [blame] | 266 | if (!uart_console(port)) |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 267 | out_8(&psc->command, MPC52xx_PSC_RST_TX); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 268 | |
| 269 | port->read_status_mask = 0; |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 270 | out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | /* Release interrupt */ |
| 273 | free_irq(port->irq, port); |
| 274 | } |
| 275 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 276 | static void |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 277 | mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new, |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 278 | struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
| 280 | struct mpc52xx_psc __iomem *psc = PSC(port); |
| 281 | unsigned long flags; |
| 282 | unsigned char mr1, mr2; |
| 283 | unsigned short ctr; |
| 284 | unsigned int j, baud, quot; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | /* Prepare what we're gonna write */ |
| 287 | mr1 = 0; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | switch (new->c_cflag & CSIZE) { |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 290 | case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS; |
| 291 | break; |
| 292 | case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS; |
| 293 | break; |
| 294 | case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS; |
| 295 | break; |
| 296 | case CS8: |
| 297 | default: mr1 |= MPC52xx_PSC_MODE_8_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (new->c_cflag & PARENB) { |
| 301 | mr1 |= (new->c_cflag & PARODD) ? |
| 302 | MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN; |
| 303 | } else |
| 304 | mr1 |= MPC52xx_PSC_MODE_PARNONE; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 305 | |
| 306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | mr2 = 0; |
| 308 | |
| 309 | if (new->c_cflag & CSTOPB) |
| 310 | mr2 |= MPC52xx_PSC_MODE_TWO_STOP; |
| 311 | else |
| 312 | mr2 |= ((new->c_cflag & CSIZE) == CS5) ? |
| 313 | MPC52xx_PSC_MODE_ONE_STOP_5_BITS : |
| 314 | MPC52xx_PSC_MODE_ONE_STOP; |
| 315 | |
| 316 | |
| 317 | baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16); |
| 318 | quot = uart_get_divisor(port, baud); |
| 319 | ctr = quot & 0xffff; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | /* Get the lock */ |
| 322 | spin_lock_irqsave(&port->lock, flags); |
| 323 | |
| 324 | /* Update the per-port timeout */ |
| 325 | uart_update_timeout(port, new->c_cflag, baud); |
| 326 | |
| 327 | /* Do our best to flush TX & RX, so we don't loose anything */ |
| 328 | /* But we don't wait indefinitly ! */ |
| 329 | j = 5000000; /* Maximum wait */ |
| 330 | /* FIXME Can't receive chars since set_termios might be called at early |
| 331 | * boot for the console, all stuff is not yet ready to receive at that |
| 332 | * time and that just makes the kernel oops */ |
| 333 | /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 334 | while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | --j) |
| 336 | udelay(1); |
| 337 | |
| 338 | if (!j) |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 339 | printk(KERN_ERR "mpc52xx_uart.c: " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | "Unable to flush RX & TX fifos in-time in set_termios." |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 341 | "Some chars may have been lost.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | /* Reset the TX & RX */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 344 | out_8(&psc->command, MPC52xx_PSC_RST_RX); |
| 345 | out_8(&psc->command, MPC52xx_PSC_RST_TX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
| 347 | /* Send new mode settings */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 348 | out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1); |
| 349 | out_8(&psc->mode, mr1); |
| 350 | out_8(&psc->mode, mr2); |
| 351 | out_8(&psc->ctur, ctr >> 8); |
| 352 | out_8(&psc->ctlr, ctr & 0xff); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | /* Reenable TX & RX */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 355 | out_8(&psc->command, MPC52xx_PSC_TX_ENABLE); |
| 356 | out_8(&psc->command, MPC52xx_PSC_RX_ENABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
| 358 | /* We're all set, release the lock */ |
| 359 | spin_unlock_irqrestore(&port->lock, flags); |
| 360 | } |
| 361 | |
| 362 | static const char * |
| 363 | mpc52xx_uart_type(struct uart_port *port) |
| 364 | { |
| 365 | return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL; |
| 366 | } |
| 367 | |
| 368 | static void |
| 369 | mpc52xx_uart_release_port(struct uart_port *port) |
| 370 | { |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 371 | /* remapped by us ? */ |
| 372 | if (port->flags & UPF_IOREMAP) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | iounmap(port->membase); |
| 374 | port->membase = NULL; |
| 375 | } |
| 376 | |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 377 | release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static int |
| 381 | mpc52xx_uart_request_port(struct uart_port *port) |
| 382 | { |
Amol Lad | be618f5 | 2006-09-30 23:29:23 -0700 | [diff] [blame] | 383 | int err; |
| 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | if (port->flags & UPF_IOREMAP) /* Need to remap ? */ |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 386 | port->membase = ioremap(port->mapbase, |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 387 | sizeof(struct mpc52xx_psc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | if (!port->membase) |
| 390 | return -EINVAL; |
| 391 | |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 392 | err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY; |
Amol Lad | be618f5 | 2006-09-30 23:29:23 -0700 | [diff] [blame] | 394 | |
| 395 | if (err && (port->flags & UPF_IOREMAP)) { |
| 396 | iounmap(port->membase); |
| 397 | port->membase = NULL; |
| 398 | } |
| 399 | |
| 400 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static void |
| 404 | mpc52xx_uart_config_port(struct uart_port *port, int flags) |
| 405 | { |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 406 | if ((flags & UART_CONFIG_TYPE) |
| 407 | && (mpc52xx_uart_request_port(port) == 0)) |
| 408 | port->type = PORT_MPC52xx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static int |
| 412 | mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 413 | { |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 414 | if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | return -EINVAL; |
| 416 | |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 417 | if ((ser->irq != port->irq) || |
| 418 | (ser->io_type != SERIAL_IO_MEM) || |
| 419 | (ser->baud_base != port->uartclk) || |
| 420 | (ser->iomem_base != (void *)port->mapbase) || |
| 421 | (ser->hub6 != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | return -EINVAL; |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | static struct uart_ops mpc52xx_uart_ops = { |
| 429 | .tx_empty = mpc52xx_uart_tx_empty, |
| 430 | .set_mctrl = mpc52xx_uart_set_mctrl, |
| 431 | .get_mctrl = mpc52xx_uart_get_mctrl, |
| 432 | .stop_tx = mpc52xx_uart_stop_tx, |
| 433 | .start_tx = mpc52xx_uart_start_tx, |
| 434 | .send_xchar = mpc52xx_uart_send_xchar, |
| 435 | .stop_rx = mpc52xx_uart_stop_rx, |
| 436 | .enable_ms = mpc52xx_uart_enable_ms, |
| 437 | .break_ctl = mpc52xx_uart_break_ctl, |
| 438 | .startup = mpc52xx_uart_startup, |
| 439 | .shutdown = mpc52xx_uart_shutdown, |
| 440 | .set_termios = mpc52xx_uart_set_termios, |
| 441 | /* .pm = mpc52xx_uart_pm, Not supported yet */ |
| 442 | /* .set_wake = mpc52xx_uart_set_wake, Not supported yet */ |
| 443 | .type = mpc52xx_uart_type, |
| 444 | .release_port = mpc52xx_uart_release_port, |
| 445 | .request_port = mpc52xx_uart_request_port, |
| 446 | .config_port = mpc52xx_uart_config_port, |
| 447 | .verify_port = mpc52xx_uart_verify_port |
| 448 | }; |
| 449 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | /* ======================================================================== */ |
| 452 | /* Interrupt handling */ |
| 453 | /* ======================================================================== */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | static inline int |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 456 | mpc52xx_uart_int_rx_chars(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
| 458 | struct tty_struct *tty = port->info->tty; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 459 | unsigned char ch, flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | unsigned short status; |
| 461 | |
| 462 | /* While we can read, do so ! */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 463 | while ((status = in_be16(&PSC(port)->mpc52xx_psc_status)) & |
| 464 | MPC52xx_PSC_SR_RXRDY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | /* Get the char */ |
| 467 | ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8); |
| 468 | |
| 469 | /* Handle sysreq char */ |
| 470 | #ifdef SUPPORT_SYSRQ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 471 | if (uart_handle_sysrq_char(port, ch)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | port->sysrq = 0; |
| 473 | continue; |
| 474 | } |
| 475 | #endif |
| 476 | |
| 477 | /* Store it */ |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 478 | |
| 479 | flag = TTY_NORMAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | port->icount.rx++; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 481 | |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 482 | if (status & (MPC52xx_PSC_SR_PE | |
| 483 | MPC52xx_PSC_SR_FE | |
| 484 | MPC52xx_PSC_SR_RB)) { |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | if (status & MPC52xx_PSC_SR_RB) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 487 | flag = TTY_BREAK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | uart_handle_break(port); |
| 489 | } else if (status & MPC52xx_PSC_SR_PE) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 490 | flag = TTY_PARITY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | else if (status & MPC52xx_PSC_SR_FE) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 492 | flag = TTY_FRAME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | /* Clear error condition */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 495 | out_8(&PSC(port)->command, MPC52xx_PSC_RST_ERR_STAT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
| 497 | } |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 498 | tty_insert_flip_char(tty, ch, flag); |
| 499 | if (status & MPC52xx_PSC_SR_OE) { |
| 500 | /* |
| 501 | * Overrun is special, since it's |
| 502 | * reported immediately, and doesn't |
| 503 | * affect the current character |
| 504 | */ |
| 505 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); |
| 506 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | tty_flip_buffer_push(tty); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY; |
| 512 | } |
| 513 | |
| 514 | static inline int |
| 515 | mpc52xx_uart_int_tx_chars(struct uart_port *port) |
| 516 | { |
| 517 | struct circ_buf *xmit = &port->info->xmit; |
| 518 | |
| 519 | /* Process out of band chars */ |
| 520 | if (port->x_char) { |
| 521 | out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char); |
| 522 | port->icount.tx++; |
| 523 | port->x_char = 0; |
| 524 | return 1; |
| 525 | } |
| 526 | |
| 527 | /* Nothing to do ? */ |
| 528 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 529 | mpc52xx_uart_stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | /* Send chars */ |
| 534 | while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) { |
| 535 | out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]); |
| 536 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 537 | port->icount.tx++; |
| 538 | if (uart_circ_empty(xmit)) |
| 539 | break; |
| 540 | } |
| 541 | |
| 542 | /* Wake up */ |
| 543 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 544 | uart_write_wakeup(port); |
| 545 | |
| 546 | /* Maybe we're done after all */ |
| 547 | if (uart_circ_empty(xmit)) { |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 548 | mpc52xx_uart_stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | return 1; |
| 553 | } |
| 554 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 555 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 556 | mpc52xx_uart_int(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
Jeff Garzik | c7bec5a | 2006-10-06 15:00:58 -0400 | [diff] [blame] | 558 | struct uart_port *port = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | unsigned long pass = ISR_PASS_LIMIT; |
| 560 | unsigned int keepgoing; |
| 561 | unsigned short status; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | spin_lock(&port->lock); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | /* While we have stuff to do, we continue */ |
| 566 | do { |
| 567 | /* If we don't find anything to do, we stop */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 568 | keepgoing = 0; |
| 569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | /* Read status */ |
| 571 | status = in_be16(&PSC(port)->mpc52xx_psc_isr); |
| 572 | status &= port->read_status_mask; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | /* Do we need to receive chars ? */ |
| 575 | /* For this RX interrupts must be on and some chars waiting */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 576 | if (status & MPC52xx_PSC_IMR_RXRDY) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 577 | keepgoing |= mpc52xx_uart_int_rx_chars(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
| 579 | /* Do we need to send chars ? */ |
| 580 | /* For this, TX must be ready and TX interrupt enabled */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 581 | if (status & MPC52xx_PSC_IMR_TXRDY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | keepgoing |= mpc52xx_uart_int_tx_chars(port); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | /* Limit number of iteration */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 585 | if (!(--pass)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | keepgoing = 0; |
| 587 | |
| 588 | } while (keepgoing); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | spin_unlock(&port->lock); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | return IRQ_HANDLED; |
| 593 | } |
| 594 | |
| 595 | |
| 596 | /* ======================================================================== */ |
| 597 | /* Console ( if applicable ) */ |
| 598 | /* ======================================================================== */ |
| 599 | |
| 600 | #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE |
| 601 | |
| 602 | static void __init |
| 603 | mpc52xx_console_get_options(struct uart_port *port, |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 604 | int *baud, int *parity, int *bits, int *flow) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { |
| 606 | struct mpc52xx_psc __iomem *psc = PSC(port); |
| 607 | unsigned char mr1; |
| 608 | |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 609 | pr_debug("mpc52xx_console_get_options(port=%p)\n", port); |
| 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | /* Read the mode registers */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 612 | out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | mr1 = in_8(&psc->mode); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | /* CT{U,L}R are write-only ! */ |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 616 | *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD; |
| 617 | #if !defined(CONFIG_PPC_MERGE) |
| 618 | if (__res.bi_baudrate) |
| 619 | *baud = __res.bi_baudrate; |
| 620 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | /* Parse them */ |
| 623 | switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) { |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 624 | case MPC52xx_PSC_MODE_5_BITS: |
| 625 | *bits = 5; |
| 626 | break; |
| 627 | case MPC52xx_PSC_MODE_6_BITS: |
| 628 | *bits = 6; |
| 629 | break; |
| 630 | case MPC52xx_PSC_MODE_7_BITS: |
| 631 | *bits = 7; |
| 632 | break; |
| 633 | case MPC52xx_PSC_MODE_8_BITS: |
| 634 | default: |
| 635 | *bits = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | if (mr1 & MPC52xx_PSC_MODE_PARNONE) |
| 639 | *parity = 'n'; |
| 640 | else |
| 641 | *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e'; |
| 642 | } |
| 643 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 644 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | mpc52xx_console_write(struct console *co, const char *s, unsigned int count) |
| 646 | { |
| 647 | struct uart_port *port = &mpc52xx_uart_ports[co->index]; |
| 648 | struct mpc52xx_psc __iomem *psc = PSC(port); |
| 649 | unsigned int i, j; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | /* Disable interrupts */ |
| 652 | out_be16(&psc->mpc52xx_psc_imr, 0); |
| 653 | |
| 654 | /* Wait the TX buffer to be empty */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 655 | j = 5000000; /* Maximum wait */ |
| 656 | while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | --j) |
| 658 | udelay(1); |
| 659 | |
| 660 | /* Write all the chars */ |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 661 | for (i = 0; i < count; i++, s++) { |
| 662 | /* Line return handling */ |
| 663 | if (*s == '\n') |
| 664 | out_8(&psc->mpc52xx_psc_buffer_8, '\r'); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | /* Send the char */ |
| 667 | out_8(&psc->mpc52xx_psc_buffer_8, *s); |
| 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | /* Wait the TX buffer to be empty */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 670 | j = 20000; /* Maximum wait */ |
| 671 | while (!(in_be16(&psc->mpc52xx_psc_status) & |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 672 | MPC52xx_PSC_SR_TXEMP) && --j) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | udelay(1); |
| 674 | } |
| 675 | |
| 676 | /* Restore interrupt state */ |
| 677 | out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask); |
| 678 | } |
| 679 | |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 680 | #if !defined(CONFIG_PPC_MERGE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | static int __init |
| 682 | mpc52xx_console_setup(struct console *co, char *options) |
| 683 | { |
| 684 | struct uart_port *port = &mpc52xx_uart_ports[co->index]; |
| 685 | |
| 686 | int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD; |
| 687 | int bits = 8; |
| 688 | int parity = 'n'; |
| 689 | int flow = 'n'; |
| 690 | |
| 691 | if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM) |
| 692 | return -EINVAL; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | /* Basic port init. Needed since we use some uart_??? func before |
| 695 | * real init for early access */ |
| 696 | spin_lock_init(&port->lock); |
| 697 | port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */ |
| 698 | port->ops = &mpc52xx_uart_ops; |
| 699 | port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1)); |
| 700 | |
| 701 | /* We ioremap ourself */ |
| 702 | port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE); |
| 703 | if (port->membase == NULL) |
| 704 | return -EINVAL; |
| 705 | |
| 706 | /* Setup the port parameters accoding to options */ |
| 707 | if (options) |
| 708 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 709 | else |
| 710 | mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow); |
| 711 | |
| 712 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 713 | } |
| 714 | |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 715 | #else |
| 716 | |
| 717 | static int __init |
| 718 | mpc52xx_console_setup(struct console *co, char *options) |
| 719 | { |
| 720 | struct uart_port *port = &mpc52xx_uart_ports[co->index]; |
| 721 | struct device_node *np = mpc52xx_uart_nodes[co->index]; |
| 722 | unsigned int ipb_freq; |
| 723 | struct resource res; |
| 724 | int ret; |
| 725 | |
| 726 | int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD; |
| 727 | int bits = 8; |
| 728 | int parity = 'n'; |
| 729 | int flow = 'n'; |
| 730 | |
| 731 | pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n", |
| 732 | co, co->index, options); |
| 733 | |
| 734 | if ((co->index < 0) || (co->index > MPC52xx_PSC_MAXNUM)) { |
| 735 | pr_debug("PSC%x out of range\n", co->index); |
| 736 | return -EINVAL; |
| 737 | } |
| 738 | |
| 739 | if (!np) { |
| 740 | pr_debug("PSC%x not found in device tree\n", co->index); |
| 741 | return -EINVAL; |
| 742 | } |
| 743 | |
| 744 | pr_debug("Console on ttyPSC%x is %s\n", |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 745 | co->index, mpc52xx_uart_nodes[co->index]->full_name); |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 746 | |
| 747 | /* Fetch register locations */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 748 | ret = of_address_to_resource(np, 0, &res); |
| 749 | if (ret) { |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 750 | pr_debug("Could not get resources for PSC%x\n", co->index); |
| 751 | return ret; |
| 752 | } |
| 753 | |
| 754 | /* Search for bus-frequency property in this node or a parent */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 755 | ipb_freq = mpc52xx_find_ipb_freq(np); |
| 756 | if (ipb_freq == 0) { |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 757 | pr_debug("Could not find IPB bus frequency!\n"); |
| 758 | return -EINVAL; |
| 759 | } |
| 760 | |
| 761 | /* Basic port init. Needed since we use some uart_??? func before |
| 762 | * real init for early access */ |
| 763 | spin_lock_init(&port->lock); |
| 764 | port->uartclk = ipb_freq / 2; |
| 765 | port->ops = &mpc52xx_uart_ops; |
| 766 | port->mapbase = res.start; |
| 767 | port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc)); |
| 768 | port->irq = irq_of_parse_and_map(np, 0); |
| 769 | |
| 770 | if (port->membase == NULL) |
| 771 | return -EINVAL; |
| 772 | |
Grant Likely | 5dd80d5 | 2007-10-13 22:37:02 -0600 | [diff] [blame] | 773 | pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n", |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 774 | (void *)port->mapbase, port->membase, |
| 775 | port->irq, port->uartclk); |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 776 | |
| 777 | /* Setup the port parameters accoding to options */ |
| 778 | if (options) |
| 779 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 780 | else |
| 781 | mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow); |
| 782 | |
| 783 | pr_debug("Setting console parameters: %i %i%c1 flow=%c\n", |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 784 | baud, bits, parity, flow); |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 785 | |
| 786 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 787 | } |
| 788 | #endif /* defined(CONFIG_PPC_MERGE) */ |
| 789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
Sylvain Munaut | 2d8179c | 2006-01-06 00:11:31 -0800 | [diff] [blame] | 791 | static struct uart_driver mpc52xx_uart_driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
| 793 | static struct console mpc52xx_console = { |
Sylvain Munaut | d62de3a | 2006-01-06 00:11:32 -0800 | [diff] [blame] | 794 | .name = "ttyPSC", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | .write = mpc52xx_console_write, |
| 796 | .device = uart_console_device, |
| 797 | .setup = mpc52xx_console_setup, |
| 798 | .flags = CON_PRINTBUFFER, |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 799 | .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | .data = &mpc52xx_uart_driver, |
| 801 | }; |
| 802 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 803 | |
| 804 | static int __init |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | mpc52xx_console_init(void) |
| 806 | { |
Grant Likely | c98750c | 2007-01-02 15:45:37 -0700 | [diff] [blame] | 807 | #if defined(CONFIG_PPC_MERGE) |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 808 | mpc52xx_uart_of_enumerate(); |
Grant Likely | c98750c | 2007-01-02 15:45:37 -0700 | [diff] [blame] | 809 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | register_console(&mpc52xx_console); |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | console_initcall(mpc52xx_console_init); |
| 815 | |
| 816 | #define MPC52xx_PSC_CONSOLE &mpc52xx_console |
| 817 | #else |
| 818 | #define MPC52xx_PSC_CONSOLE NULL |
| 819 | #endif |
| 820 | |
| 821 | |
| 822 | /* ======================================================================== */ |
| 823 | /* UART Driver */ |
| 824 | /* ======================================================================== */ |
| 825 | |
| 826 | static struct uart_driver mpc52xx_uart_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | .driver_name = "mpc52xx_psc_uart", |
Sylvain Munaut | d62de3a | 2006-01-06 00:11:32 -0800 | [diff] [blame] | 828 | .dev_name = "ttyPSC", |
Sylvain Munaut | d62de3a | 2006-01-06 00:11:32 -0800 | [diff] [blame] | 829 | .major = SERIAL_PSC_MAJOR, |
| 830 | .minor = SERIAL_PSC_MINOR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | .nr = MPC52xx_PSC_MAXNUM, |
| 832 | .cons = MPC52xx_PSC_CONSOLE, |
| 833 | }; |
| 834 | |
| 835 | |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 836 | #if !defined(CONFIG_PPC_MERGE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | /* ======================================================================== */ |
| 838 | /* Platform Driver */ |
| 839 | /* ======================================================================== */ |
| 840 | |
| 841 | static int __devinit |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 842 | mpc52xx_uart_probe(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 844 | struct resource *res = dev->resource; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
| 846 | struct uart_port *port = NULL; |
| 847 | int i, idx, ret; |
| 848 | |
| 849 | /* Check validity & presence */ |
Andrey Volkov | 38801e2 | 2005-11-12 22:04:06 +0000 | [diff] [blame] | 850 | idx = dev->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM) |
| 852 | return -EINVAL; |
| 853 | |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 854 | if (!mpc52xx_match_psc_function(idx, "uart")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | return -ENODEV; |
| 856 | |
| 857 | /* Init the port structure */ |
| 858 | port = &mpc52xx_uart_ports[idx]; |
| 859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | spin_lock_init(&port->lock); |
| 861 | port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */ |
Russell King | 947deee | 2006-07-02 20:45:51 +0100 | [diff] [blame] | 862 | port->fifosize = 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | port->iotype = UPIO_MEM; |
| 864 | port->flags = UPF_BOOT_AUTOCONF | |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 865 | (uart_console(port) ? 0 : UPF_IOREMAP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | port->line = idx; |
| 867 | port->ops = &mpc52xx_uart_ops; |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 868 | port->dev = &dev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | |
| 870 | /* Search for IRQ and mapbase */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 871 | for (i = 0 ; i < dev->num_resources ; i++, res++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | if (res->flags & IORESOURCE_MEM) |
| 873 | port->mapbase = res->start; |
| 874 | else if (res->flags & IORESOURCE_IRQ) |
| 875 | port->irq = res->start; |
| 876 | } |
| 877 | if (!port->irq || !port->mapbase) |
| 878 | return -EINVAL; |
| 879 | |
| 880 | /* Add the port to the uart sub-system */ |
| 881 | ret = uart_add_one_port(&mpc52xx_uart_driver, port); |
| 882 | if (!ret) |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 883 | platform_set_drvdata(dev, (void *)port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | return ret; |
| 886 | } |
| 887 | |
| 888 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 889 | mpc52xx_uart_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 891 | struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 893 | platform_set_drvdata(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | |
| 895 | if (port) |
| 896 | uart_remove_one_port(&mpc52xx_uart_driver, port); |
| 897 | |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | #ifdef CONFIG_PM |
| 902 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 903 | mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 905 | struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame] | 907 | if (port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | uart_suspend_port(&mpc52xx_uart_driver, port); |
| 909 | |
| 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 914 | mpc52xx_uart_resume(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 916 | struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 918 | if (port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | uart_resume_port(&mpc52xx_uart_driver, port); |
| 920 | |
| 921 | return 0; |
| 922 | } |
| 923 | #endif |
| 924 | |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 925 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 926 | static struct platform_driver mpc52xx_uart_platform_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | .probe = mpc52xx_uart_probe, |
| 928 | .remove = mpc52xx_uart_remove, |
| 929 | #ifdef CONFIG_PM |
| 930 | .suspend = mpc52xx_uart_suspend, |
| 931 | .resume = mpc52xx_uart_resume, |
| 932 | #endif |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 933 | .driver = { |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 934 | .owner = THIS_MODULE, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 935 | .name = "mpc52xx-psc", |
| 936 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | }; |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 938 | #endif /* !defined(CONFIG_PPC_MERGE) */ |
| 939 | |
| 940 | |
| 941 | #if defined(CONFIG_PPC_MERGE) |
| 942 | /* ======================================================================== */ |
| 943 | /* OF Platform Driver */ |
| 944 | /* ======================================================================== */ |
| 945 | |
| 946 | static int __devinit |
| 947 | mpc52xx_uart_of_probe(struct of_device *op, const struct of_device_id *match) |
| 948 | { |
| 949 | int idx = -1; |
| 950 | unsigned int ipb_freq; |
| 951 | struct uart_port *port = NULL; |
| 952 | struct resource res; |
| 953 | int ret; |
| 954 | |
| 955 | dev_dbg(&op->dev, "mpc52xx_uart_probe(op=%p, match=%p)\n", op, match); |
| 956 | |
| 957 | /* Check validity & presence */ |
| 958 | for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++) |
| 959 | if (mpc52xx_uart_nodes[idx] == op->node) |
| 960 | break; |
| 961 | if (idx >= MPC52xx_PSC_MAXNUM) |
| 962 | return -EINVAL; |
| 963 | pr_debug("Found %s assigned to ttyPSC%x\n", |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 964 | mpc52xx_uart_nodes[idx]->full_name, idx); |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 965 | |
| 966 | /* Search for bus-frequency property in this node or a parent */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 967 | ipb_freq = mpc52xx_find_ipb_freq(op->node); |
| 968 | if (ipb_freq == 0) { |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 969 | dev_dbg(&op->dev, "Could not find IPB bus frequency!\n"); |
| 970 | return -EINVAL; |
| 971 | } |
| 972 | |
| 973 | /* Init the port structure */ |
| 974 | port = &mpc52xx_uart_ports[idx]; |
| 975 | |
| 976 | spin_lock_init(&port->lock); |
| 977 | port->uartclk = ipb_freq / 2; |
| 978 | port->fifosize = 512; |
| 979 | port->iotype = UPIO_MEM; |
| 980 | port->flags = UPF_BOOT_AUTOCONF | |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 981 | (uart_console(port) ? 0 : UPF_IOREMAP); |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 982 | port->line = idx; |
| 983 | port->ops = &mpc52xx_uart_ops; |
| 984 | port->dev = &op->dev; |
| 985 | |
| 986 | /* Search for IRQ and mapbase */ |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 987 | ret = of_address_to_resource(op->node, 0, &res); |
| 988 | if (ret) |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 989 | return ret; |
| 990 | |
| 991 | port->mapbase = res.start; |
| 992 | port->irq = irq_of_parse_and_map(op->node, 0); |
| 993 | |
Grant Likely | 5dd80d5 | 2007-10-13 22:37:02 -0600 | [diff] [blame] | 994 | dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n", |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 995 | (void *)port->mapbase, port->irq, port->uartclk); |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 996 | |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 997 | if ((port->irq == NO_IRQ) || !port->mapbase) { |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 998 | printk(KERN_ERR "Could not allocate resources for PSC\n"); |
| 999 | return -EINVAL; |
| 1000 | } |
| 1001 | |
| 1002 | /* Add the port to the uart sub-system */ |
| 1003 | ret = uart_add_one_port(&mpc52xx_uart_driver, port); |
| 1004 | if (!ret) |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 1005 | dev_set_drvdata(&op->dev, (void *)port); |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1006 | |
| 1007 | return ret; |
| 1008 | } |
| 1009 | |
| 1010 | static int |
| 1011 | mpc52xx_uart_of_remove(struct of_device *op) |
| 1012 | { |
| 1013 | struct uart_port *port = dev_get_drvdata(&op->dev); |
| 1014 | dev_set_drvdata(&op->dev, NULL); |
| 1015 | |
Sylvain Munaut | fc7900b | 2007-02-15 23:18:08 +0100 | [diff] [blame] | 1016 | if (port) { |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1017 | uart_remove_one_port(&mpc52xx_uart_driver, port); |
Sylvain Munaut | fc7900b | 2007-02-15 23:18:08 +0100 | [diff] [blame] | 1018 | irq_dispose_mapping(port->irq); |
| 1019 | } |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | #ifdef CONFIG_PM |
| 1025 | static int |
| 1026 | mpc52xx_uart_of_suspend(struct of_device *op, pm_message_t state) |
| 1027 | { |
| 1028 | struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev); |
| 1029 | |
| 1030 | if (port) |
| 1031 | uart_suspend_port(&mpc52xx_uart_driver, port); |
| 1032 | |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | static int |
| 1037 | mpc52xx_uart_of_resume(struct of_device *op) |
| 1038 | { |
| 1039 | struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev); |
| 1040 | |
| 1041 | if (port) |
| 1042 | uart_resume_port(&mpc52xx_uart_driver, port); |
| 1043 | |
| 1044 | return 0; |
| 1045 | } |
| 1046 | #endif |
| 1047 | |
| 1048 | static void |
| 1049 | mpc52xx_uart_of_assign(struct device_node *np, int idx) |
| 1050 | { |
| 1051 | int free_idx = -1; |
| 1052 | int i; |
| 1053 | |
| 1054 | /* Find the first free node */ |
| 1055 | for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) { |
| 1056 | if (mpc52xx_uart_nodes[i] == NULL) { |
| 1057 | free_idx = i; |
| 1058 | break; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | if ((idx < 0) || (idx >= MPC52xx_PSC_MAXNUM)) |
| 1063 | idx = free_idx; |
| 1064 | |
| 1065 | if (idx < 0) |
| 1066 | return; /* No free slot; abort */ |
| 1067 | |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 1068 | of_node_get(np); |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1069 | /* If the slot is already occupied, then swap slots */ |
| 1070 | if (mpc52xx_uart_nodes[idx] && (free_idx != -1)) |
| 1071 | mpc52xx_uart_nodes[free_idx] = mpc52xx_uart_nodes[idx]; |
John Rigby | af6a9aa | 2007-05-23 11:30:55 -0600 | [diff] [blame] | 1072 | mpc52xx_uart_nodes[idx] = np; |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | static void |
| 1076 | mpc52xx_uart_of_enumerate(void) |
| 1077 | { |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 1078 | static int enum_done; |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1079 | struct device_node *np; |
| 1080 | const unsigned int *devno; |
| 1081 | int i; |
| 1082 | |
| 1083 | if (enum_done) |
| 1084 | return; |
| 1085 | |
| 1086 | for_each_node_by_type(np, "serial") { |
| 1087 | if (!of_match_node(mpc52xx_uart_of_match, np)) |
| 1088 | continue; |
| 1089 | |
| 1090 | /* Is a particular device number requested? */ |
Stephen Rothwell | 40cd3a4 | 2007-05-01 13:54:02 +1000 | [diff] [blame] | 1091 | devno = of_get_property(np, "port-number", NULL); |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 1092 | mpc52xx_uart_of_assign(np, devno ? *devno : -1); |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | enum_done = 1; |
| 1096 | |
| 1097 | for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) { |
| 1098 | if (mpc52xx_uart_nodes[i]) |
| 1099 | pr_debug("%s assigned to ttyPSC%x\n", |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 1100 | mpc52xx_uart_nodes[i]->full_name, i); |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match); |
| 1105 | |
| 1106 | static struct of_platform_driver mpc52xx_uart_of_driver = { |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1107 | .match_table = mpc52xx_uart_of_match, |
| 1108 | .probe = mpc52xx_uart_of_probe, |
| 1109 | .remove = mpc52xx_uart_of_remove, |
| 1110 | #ifdef CONFIG_PM |
| 1111 | .suspend = mpc52xx_uart_of_suspend, |
| 1112 | .resume = mpc52xx_uart_of_resume, |
| 1113 | #endif |
| 1114 | .driver = { |
| 1115 | .name = "mpc52xx-psc-uart", |
| 1116 | }, |
| 1117 | }; |
| 1118 | #endif /* defined(CONFIG_PPC_MERGE) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | |
| 1120 | |
| 1121 | /* ======================================================================== */ |
| 1122 | /* Module */ |
| 1123 | /* ======================================================================== */ |
| 1124 | |
| 1125 | static int __init |
| 1126 | mpc52xx_uart_init(void) |
| 1127 | { |
| 1128 | int ret; |
| 1129 | |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1130 | printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | |
John Rigby | 406b7d4 | 2008-01-17 08:37:25 +1100 | [diff] [blame^] | 1132 | ret = uart_register_driver(&mpc52xx_uart_driver); |
| 1133 | if (ret) { |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1134 | printk(KERN_ERR "%s: uart_register_driver failed (%i)\n", |
| 1135 | __FILE__, ret); |
| 1136 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1139 | #if defined(CONFIG_PPC_MERGE) |
| 1140 | mpc52xx_uart_of_enumerate(); |
| 1141 | |
| 1142 | ret = of_register_platform_driver(&mpc52xx_uart_of_driver); |
| 1143 | if (ret) { |
| 1144 | printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n", |
| 1145 | __FILE__, ret); |
| 1146 | uart_unregister_driver(&mpc52xx_uart_driver); |
| 1147 | return ret; |
| 1148 | } |
| 1149 | #else |
| 1150 | ret = platform_driver_register(&mpc52xx_uart_platform_driver); |
| 1151 | if (ret) { |
| 1152 | printk(KERN_ERR "%s: platform_driver_register failed (%i)\n", |
| 1153 | __FILE__, ret); |
| 1154 | uart_unregister_driver(&mpc52xx_uart_driver); |
| 1155 | return ret; |
| 1156 | } |
| 1157 | #endif |
| 1158 | |
| 1159 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | static void __exit |
| 1163 | mpc52xx_uart_exit(void) |
| 1164 | { |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1165 | #if defined(CONFIG_PPC_MERGE) |
| 1166 | of_unregister_platform_driver(&mpc52xx_uart_of_driver); |
| 1167 | #else |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1168 | platform_driver_unregister(&mpc52xx_uart_platform_driver); |
Grant Likely | b9272df | 2006-11-27 14:21:02 -0700 | [diff] [blame] | 1169 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | uart_unregister_driver(&mpc52xx_uart_driver); |
| 1171 | } |
| 1172 | |
| 1173 | |
| 1174 | module_init(mpc52xx_uart_init); |
| 1175 | module_exit(mpc52xx_uart_exit); |
| 1176 | |
| 1177 | MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>"); |
| 1178 | MODULE_DESCRIPTION("Freescale MPC52xx PSC UART"); |
| 1179 | MODULE_LICENSE("GPL"); |