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