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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * Copyright (C) 2004-2005 Sylvain Munaut <tnt@246tNt.com> |
| 20 | * Copyright (C) 2003 MontaVista, Software, Inc. |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 21 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * This file is licensed under the terms of the GNU General Public License |
| 23 | * version 2. This program is licensed "as is" without any warranty of any |
| 24 | * kind, whether express or implied. |
| 25 | */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | /* Platform device Usage : |
| 28 | * |
| 29 | * Since PSCs can have multiple function, the correct driver for each one |
| 30 | * is selected by calling mpc52xx_match_psc_function(...). The function |
| 31 | * handled by this driver is "uart". |
| 32 | * |
| 33 | * The driver init all necessary registers to place the PSC in uart mode without |
| 34 | * DCD. However, the pin multiplexing aren't changed and should be set either |
| 35 | * by the bootloader or in the platform init code. |
| 36 | * |
| 37 | * 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] | 38 | * and so on). So the PSC1 is mapped to /dev/ttyPSC0, PSC2 to /dev/ttyPSC1 and |
| 39 | * so on. But be warned, it's an ABSOLUTE REQUIREMENT ! This is needed mainly |
| 40 | * 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] | 41 | * 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] | 42 | * will be mapped to. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | */ |
| 44 | |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 45 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/module.h> |
| 47 | #include <linux/tty.h> |
| 48 | #include <linux/serial.h> |
| 49 | #include <linux/sysrq.h> |
| 50 | #include <linux/console.h> |
| 51 | |
| 52 | #include <asm/delay.h> |
| 53 | #include <asm/io.h> |
| 54 | |
| 55 | #include <asm/mpc52xx.h> |
| 56 | #include <asm/mpc52xx_psc.h> |
| 57 | |
| 58 | #if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
| 59 | #define SUPPORT_SYSRQ |
| 60 | #endif |
| 61 | |
| 62 | #include <linux/serial_core.h> |
| 63 | |
| 64 | |
Sylvain Munaut | d62de3a | 2006-01-06 00:11:32 -0800 | [diff] [blame] | 65 | /* We've been assigned a range on the "Low-density serial ports" major */ |
| 66 | #define SERIAL_PSC_MAJOR 204 |
| 67 | #define SERIAL_PSC_MINOR 148 |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | #define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */ |
| 71 | |
| 72 | |
| 73 | static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM]; |
| 74 | /* Rem: - We use the read_status_mask as a shadow of |
| 75 | * psc->mpc52xx_psc_imr |
| 76 | * - It's important that is array is all zero on start as we |
| 77 | * use it to know if it's initialized or not ! If it's not sure |
| 78 | * it's cleared, then a memset(...,0,...) should be added to |
| 79 | * the console_init |
| 80 | */ |
| 81 | |
| 82 | #define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase)) |
| 83 | |
| 84 | |
| 85 | /* Forward declaration of the interruption handling routine */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 86 | static irqreturn_t mpc52xx_uart_int(int irq,void *dev_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | |
| 89 | /* Simple macro to test if a port is console or not. This one is taken |
| 90 | * for serial_core.c and maybe should be moved to serial_core.h ? */ |
| 91 | #ifdef CONFIG_SERIAL_CORE_CONSOLE |
| 92 | #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line) |
| 93 | #else |
| 94 | #define uart_console(port) (0) |
| 95 | #endif |
| 96 | |
| 97 | |
| 98 | /* ======================================================================== */ |
| 99 | /* UART operations */ |
| 100 | /* ======================================================================== */ |
| 101 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 102 | static unsigned int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | mpc52xx_uart_tx_empty(struct uart_port *port) |
| 104 | { |
| 105 | int status = in_be16(&PSC(port)->mpc52xx_psc_status); |
| 106 | return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0; |
| 107 | } |
| 108 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 109 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 111 | { |
| 112 | /* Not implemented */ |
| 113 | } |
| 114 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 115 | static unsigned int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | mpc52xx_uart_get_mctrl(struct uart_port *port) |
| 117 | { |
| 118 | /* Not implemented */ |
| 119 | return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR; |
| 120 | } |
| 121 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 122 | static void |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 123 | mpc52xx_uart_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | /* port->lock taken by caller */ |
| 126 | port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY; |
| 127 | out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask); |
| 128 | } |
| 129 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 130 | static void |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 131 | mpc52xx_uart_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
| 133 | /* port->lock taken by caller */ |
| 134 | port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY; |
| 135 | out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask); |
| 136 | } |
| 137 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 138 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | mpc52xx_uart_send_xchar(struct uart_port *port, char ch) |
| 140 | { |
| 141 | unsigned long flags; |
| 142 | spin_lock_irqsave(&port->lock, flags); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | port->x_char = ch; |
| 145 | if (ch) { |
| 146 | /* Make sure tx interrupts are on */ |
| 147 | /* Truly necessary ??? They should be anyway */ |
| 148 | port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY; |
| 149 | out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask); |
| 150 | } |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | spin_unlock_irqrestore(&port->lock, flags); |
| 153 | } |
| 154 | |
| 155 | static void |
| 156 | mpc52xx_uart_stop_rx(struct uart_port *port) |
| 157 | { |
| 158 | /* port->lock taken by caller */ |
| 159 | port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY; |
| 160 | out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask); |
| 161 | } |
| 162 | |
| 163 | static void |
| 164 | mpc52xx_uart_enable_ms(struct uart_port *port) |
| 165 | { |
| 166 | /* Not implemented */ |
| 167 | } |
| 168 | |
| 169 | static void |
| 170 | mpc52xx_uart_break_ctl(struct uart_port *port, int ctl) |
| 171 | { |
| 172 | unsigned long flags; |
| 173 | spin_lock_irqsave(&port->lock, flags); |
| 174 | |
| 175 | if ( ctl == -1 ) |
| 176 | out_8(&PSC(port)->command,MPC52xx_PSC_START_BRK); |
| 177 | else |
| 178 | out_8(&PSC(port)->command,MPC52xx_PSC_STOP_BRK); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | spin_unlock_irqrestore(&port->lock, flags); |
| 181 | } |
| 182 | |
| 183 | static int |
| 184 | mpc52xx_uart_startup(struct uart_port *port) |
| 185 | { |
| 186 | struct mpc52xx_psc __iomem *psc = PSC(port); |
| 187 | int ret; |
| 188 | |
| 189 | /* Request IRQ */ |
| 190 | ret = request_irq(port->irq, mpc52xx_uart_int, |
Thomas Gleixner | 40663cc | 2006-07-01 19:29:43 -0700 | [diff] [blame] | 191 | IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | if (ret) |
| 193 | return ret; |
| 194 | |
| 195 | /* Reset/activate the port, clear and enable interrupts */ |
| 196 | out_8(&psc->command,MPC52xx_PSC_RST_RX); |
| 197 | out_8(&psc->command,MPC52xx_PSC_RST_TX); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | out_be32(&psc->sicr,0); /* UART mode DCD ignored */ |
| 200 | |
| 201 | out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | out_8(&psc->rfcntl, 0x00); |
| 204 | out_be16(&psc->rfalarm, 0x1ff); |
| 205 | out_8(&psc->tfcntl, 0x07); |
| 206 | out_be16(&psc->tfalarm, 0x80); |
| 207 | |
| 208 | port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY; |
| 209 | out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | out_8(&psc->command,MPC52xx_PSC_TX_ENABLE); |
| 212 | out_8(&psc->command,MPC52xx_PSC_RX_ENABLE); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static void |
| 218 | mpc52xx_uart_shutdown(struct uart_port *port) |
| 219 | { |
| 220 | struct mpc52xx_psc __iomem *psc = PSC(port); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | /* Shut down the port, interrupt and all */ |
| 223 | out_8(&psc->command,MPC52xx_PSC_RST_RX); |
| 224 | out_8(&psc->command,MPC52xx_PSC_RST_TX); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 225 | |
| 226 | port->read_status_mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask); |
| 228 | |
| 229 | /* Release interrupt */ |
| 230 | free_irq(port->irq, port); |
| 231 | } |
| 232 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 233 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | mpc52xx_uart_set_termios(struct uart_port *port, struct termios *new, |
| 235 | struct termios *old) |
| 236 | { |
| 237 | struct mpc52xx_psc __iomem *psc = PSC(port); |
| 238 | unsigned long flags; |
| 239 | unsigned char mr1, mr2; |
| 240 | unsigned short ctr; |
| 241 | unsigned int j, baud, quot; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | /* Prepare what we're gonna write */ |
| 244 | mr1 = 0; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | switch (new->c_cflag & CSIZE) { |
| 247 | case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS; |
| 248 | break; |
| 249 | case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS; |
| 250 | break; |
| 251 | case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS; |
| 252 | break; |
| 253 | case CS8: |
| 254 | default: mr1 |= MPC52xx_PSC_MODE_8_BITS; |
| 255 | } |
| 256 | |
| 257 | if (new->c_cflag & PARENB) { |
| 258 | mr1 |= (new->c_cflag & PARODD) ? |
| 259 | MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN; |
| 260 | } else |
| 261 | mr1 |= MPC52xx_PSC_MODE_PARNONE; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 262 | |
| 263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | mr2 = 0; |
| 265 | |
| 266 | if (new->c_cflag & CSTOPB) |
| 267 | mr2 |= MPC52xx_PSC_MODE_TWO_STOP; |
| 268 | else |
| 269 | mr2 |= ((new->c_cflag & CSIZE) == CS5) ? |
| 270 | MPC52xx_PSC_MODE_ONE_STOP_5_BITS : |
| 271 | MPC52xx_PSC_MODE_ONE_STOP; |
| 272 | |
| 273 | |
| 274 | baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16); |
| 275 | quot = uart_get_divisor(port, baud); |
| 276 | ctr = quot & 0xffff; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | /* Get the lock */ |
| 279 | spin_lock_irqsave(&port->lock, flags); |
| 280 | |
| 281 | /* Update the per-port timeout */ |
| 282 | uart_update_timeout(port, new->c_cflag, baud); |
| 283 | |
| 284 | /* Do our best to flush TX & RX, so we don't loose anything */ |
| 285 | /* But we don't wait indefinitly ! */ |
| 286 | j = 5000000; /* Maximum wait */ |
| 287 | /* FIXME Can't receive chars since set_termios might be called at early |
| 288 | * boot for the console, all stuff is not yet ready to receive at that |
| 289 | * time and that just makes the kernel oops */ |
| 290 | /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 291 | while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | --j) |
| 293 | udelay(1); |
| 294 | |
| 295 | if (!j) |
| 296 | printk( KERN_ERR "mpc52xx_uart.c: " |
| 297 | "Unable to flush RX & TX fifos in-time in set_termios." |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 298 | "Some chars may have been lost.\n" ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | /* Reset the TX & RX */ |
| 301 | out_8(&psc->command,MPC52xx_PSC_RST_RX); |
| 302 | out_8(&psc->command,MPC52xx_PSC_RST_TX); |
| 303 | |
| 304 | /* Send new mode settings */ |
| 305 | out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1); |
| 306 | out_8(&psc->mode,mr1); |
| 307 | out_8(&psc->mode,mr2); |
| 308 | out_8(&psc->ctur,ctr >> 8); |
| 309 | out_8(&psc->ctlr,ctr & 0xff); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | /* Reenable TX & RX */ |
| 312 | out_8(&psc->command,MPC52xx_PSC_TX_ENABLE); |
| 313 | out_8(&psc->command,MPC52xx_PSC_RX_ENABLE); |
| 314 | |
| 315 | /* We're all set, release the lock */ |
| 316 | spin_unlock_irqrestore(&port->lock, flags); |
| 317 | } |
| 318 | |
| 319 | static const char * |
| 320 | mpc52xx_uart_type(struct uart_port *port) |
| 321 | { |
| 322 | return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL; |
| 323 | } |
| 324 | |
| 325 | static void |
| 326 | mpc52xx_uart_release_port(struct uart_port *port) |
| 327 | { |
| 328 | if (port->flags & UPF_IOREMAP) { /* remapped by us ? */ |
| 329 | iounmap(port->membase); |
| 330 | port->membase = NULL; |
| 331 | } |
| 332 | |
| 333 | release_mem_region(port->mapbase, MPC52xx_PSC_SIZE); |
| 334 | } |
| 335 | |
| 336 | static int |
| 337 | mpc52xx_uart_request_port(struct uart_port *port) |
| 338 | { |
Amol Lad | be618f5 | 2006-09-30 23:29:23 -0700 | [diff] [blame] | 339 | int err; |
| 340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | if (port->flags & UPF_IOREMAP) /* Need to remap ? */ |
| 342 | port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE); |
| 343 | |
| 344 | if (!port->membase) |
| 345 | return -EINVAL; |
| 346 | |
Amol Lad | be618f5 | 2006-09-30 23:29:23 -0700 | [diff] [blame] | 347 | err = request_mem_region(port->mapbase, MPC52xx_PSC_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY; |
Amol Lad | be618f5 | 2006-09-30 23:29:23 -0700 | [diff] [blame] | 349 | |
| 350 | if (err && (port->flags & UPF_IOREMAP)) { |
| 351 | iounmap(port->membase); |
| 352 | port->membase = NULL; |
| 353 | } |
| 354 | |
| 355 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static void |
| 359 | mpc52xx_uart_config_port(struct uart_port *port, int flags) |
| 360 | { |
| 361 | if ( (flags & UART_CONFIG_TYPE) && |
| 362 | (mpc52xx_uart_request_port(port) == 0) ) |
| 363 | port->type = PORT_MPC52xx; |
| 364 | } |
| 365 | |
| 366 | static int |
| 367 | mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 368 | { |
| 369 | if ( ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx ) |
| 370 | return -EINVAL; |
| 371 | |
| 372 | if ( (ser->irq != port->irq) || |
| 373 | (ser->io_type != SERIAL_IO_MEM) || |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 374 | (ser->baud_base != port->uartclk) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | (ser->iomem_base != (void*)port->mapbase) || |
| 376 | (ser->hub6 != 0 ) ) |
| 377 | return -EINVAL; |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | static struct uart_ops mpc52xx_uart_ops = { |
| 384 | .tx_empty = mpc52xx_uart_tx_empty, |
| 385 | .set_mctrl = mpc52xx_uart_set_mctrl, |
| 386 | .get_mctrl = mpc52xx_uart_get_mctrl, |
| 387 | .stop_tx = mpc52xx_uart_stop_tx, |
| 388 | .start_tx = mpc52xx_uart_start_tx, |
| 389 | .send_xchar = mpc52xx_uart_send_xchar, |
| 390 | .stop_rx = mpc52xx_uart_stop_rx, |
| 391 | .enable_ms = mpc52xx_uart_enable_ms, |
| 392 | .break_ctl = mpc52xx_uart_break_ctl, |
| 393 | .startup = mpc52xx_uart_startup, |
| 394 | .shutdown = mpc52xx_uart_shutdown, |
| 395 | .set_termios = mpc52xx_uart_set_termios, |
| 396 | /* .pm = mpc52xx_uart_pm, Not supported yet */ |
| 397 | /* .set_wake = mpc52xx_uart_set_wake, Not supported yet */ |
| 398 | .type = mpc52xx_uart_type, |
| 399 | .release_port = mpc52xx_uart_release_port, |
| 400 | .request_port = mpc52xx_uart_request_port, |
| 401 | .config_port = mpc52xx_uart_config_port, |
| 402 | .verify_port = mpc52xx_uart_verify_port |
| 403 | }; |
| 404 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | /* ======================================================================== */ |
| 407 | /* Interrupt handling */ |
| 408 | /* ======================================================================== */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | static inline int |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 411 | mpc52xx_uart_int_rx_chars(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
| 413 | struct tty_struct *tty = port->info->tty; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 414 | unsigned char ch, flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | unsigned short status; |
| 416 | |
| 417 | /* While we can read, do so ! */ |
| 418 | while ( (status = in_be16(&PSC(port)->mpc52xx_psc_status)) & |
| 419 | MPC52xx_PSC_SR_RXRDY) { |
| 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | /* Get the char */ |
| 422 | ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8); |
| 423 | |
| 424 | /* Handle sysreq char */ |
| 425 | #ifdef SUPPORT_SYSRQ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 426 | if (uart_handle_sysrq_char(port, ch)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | port->sysrq = 0; |
| 428 | continue; |
| 429 | } |
| 430 | #endif |
| 431 | |
| 432 | /* Store it */ |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 433 | |
| 434 | flag = TTY_NORMAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | port->icount.rx++; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | if ( status & (MPC52xx_PSC_SR_PE | |
| 438 | MPC52xx_PSC_SR_FE | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 439 | MPC52xx_PSC_SR_RB) ) { |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | if (status & MPC52xx_PSC_SR_RB) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 442 | flag = TTY_BREAK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | uart_handle_break(port); |
| 444 | } else if (status & MPC52xx_PSC_SR_PE) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 445 | flag = TTY_PARITY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | else if (status & MPC52xx_PSC_SR_FE) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 447 | flag = TTY_FRAME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
| 449 | /* Clear error condition */ |
| 450 | out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT); |
| 451 | |
| 452 | } |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 453 | tty_insert_flip_char(tty, ch, flag); |
| 454 | if (status & MPC52xx_PSC_SR_OE) { |
| 455 | /* |
| 456 | * Overrun is special, since it's |
| 457 | * reported immediately, and doesn't |
| 458 | * affect the current character |
| 459 | */ |
| 460 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); |
| 461 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | tty_flip_buffer_push(tty); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY; |
| 467 | } |
| 468 | |
| 469 | static inline int |
| 470 | mpc52xx_uart_int_tx_chars(struct uart_port *port) |
| 471 | { |
| 472 | struct circ_buf *xmit = &port->info->xmit; |
| 473 | |
| 474 | /* Process out of band chars */ |
| 475 | if (port->x_char) { |
| 476 | out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char); |
| 477 | port->icount.tx++; |
| 478 | port->x_char = 0; |
| 479 | return 1; |
| 480 | } |
| 481 | |
| 482 | /* Nothing to do ? */ |
| 483 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 484 | mpc52xx_uart_stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | /* Send chars */ |
| 489 | while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) { |
| 490 | out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]); |
| 491 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 492 | port->icount.tx++; |
| 493 | if (uart_circ_empty(xmit)) |
| 494 | break; |
| 495 | } |
| 496 | |
| 497 | /* Wake up */ |
| 498 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 499 | uart_write_wakeup(port); |
| 500 | |
| 501 | /* Maybe we're done after all */ |
| 502 | if (uart_circ_empty(xmit)) { |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 503 | mpc52xx_uart_stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | return 1; |
| 508 | } |
| 509 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 510 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 511 | mpc52xx_uart_int(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | { |
Jeff Garzik | c7bec5a | 2006-10-06 15:00:58 -0400 | [diff] [blame] | 513 | struct uart_port *port = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | unsigned long pass = ISR_PASS_LIMIT; |
| 515 | unsigned int keepgoing; |
| 516 | unsigned short status; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | spin_lock(&port->lock); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | /* While we have stuff to do, we continue */ |
| 521 | do { |
| 522 | /* If we don't find anything to do, we stop */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 523 | keepgoing = 0; |
| 524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | /* Read status */ |
| 526 | status = in_be16(&PSC(port)->mpc52xx_psc_isr); |
| 527 | status &= port->read_status_mask; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | /* Do we need to receive chars ? */ |
| 530 | /* For this RX interrupts must be on and some chars waiting */ |
| 531 | if ( status & MPC52xx_PSC_IMR_RXRDY ) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 532 | keepgoing |= mpc52xx_uart_int_rx_chars(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
| 534 | /* Do we need to send chars ? */ |
| 535 | /* For this, TX must be ready and TX interrupt enabled */ |
| 536 | if ( status & MPC52xx_PSC_IMR_TXRDY ) |
| 537 | keepgoing |= mpc52xx_uart_int_tx_chars(port); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | /* Limit number of iteration */ |
| 540 | if ( !(--pass) ) |
| 541 | keepgoing = 0; |
| 542 | |
| 543 | } while (keepgoing); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | spin_unlock(&port->lock); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | return IRQ_HANDLED; |
| 548 | } |
| 549 | |
| 550 | |
| 551 | /* ======================================================================== */ |
| 552 | /* Console ( if applicable ) */ |
| 553 | /* ======================================================================== */ |
| 554 | |
| 555 | #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE |
| 556 | |
| 557 | static void __init |
| 558 | mpc52xx_console_get_options(struct uart_port *port, |
| 559 | int *baud, int *parity, int *bits, int *flow) |
| 560 | { |
| 561 | struct mpc52xx_psc __iomem *psc = PSC(port); |
| 562 | unsigned char mr1; |
| 563 | |
| 564 | /* Read the mode registers */ |
| 565 | out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1); |
| 566 | mr1 = in_8(&psc->mode); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | /* CT{U,L}R are write-only ! */ |
| 569 | *baud = __res.bi_baudrate ? |
| 570 | __res.bi_baudrate : CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD; |
| 571 | |
| 572 | /* Parse them */ |
| 573 | switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) { |
| 574 | case MPC52xx_PSC_MODE_5_BITS: *bits = 5; break; |
| 575 | case MPC52xx_PSC_MODE_6_BITS: *bits = 6; break; |
| 576 | case MPC52xx_PSC_MODE_7_BITS: *bits = 7; break; |
| 577 | case MPC52xx_PSC_MODE_8_BITS: |
| 578 | default: *bits = 8; |
| 579 | } |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | if (mr1 & MPC52xx_PSC_MODE_PARNONE) |
| 582 | *parity = 'n'; |
| 583 | else |
| 584 | *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e'; |
| 585 | } |
| 586 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 587 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | mpc52xx_console_write(struct console *co, const char *s, unsigned int count) |
| 589 | { |
| 590 | struct uart_port *port = &mpc52xx_uart_ports[co->index]; |
| 591 | struct mpc52xx_psc __iomem *psc = PSC(port); |
| 592 | unsigned int i, j; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | /* Disable interrupts */ |
| 595 | out_be16(&psc->mpc52xx_psc_imr, 0); |
| 596 | |
| 597 | /* Wait the TX buffer to be empty */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 598 | j = 5000000; /* Maximum wait */ |
| 599 | while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | --j) |
| 601 | udelay(1); |
| 602 | |
| 603 | /* Write all the chars */ |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 604 | for (i = 0; i < count; i++, s++) { |
| 605 | /* Line return handling */ |
| 606 | if (*s == '\n') |
| 607 | out_8(&psc->mpc52xx_psc_buffer_8, '\r'); |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | /* Send the char */ |
| 610 | out_8(&psc->mpc52xx_psc_buffer_8, *s); |
| 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | /* Wait the TX buffer to be empty */ |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 613 | j = 20000; /* Maximum wait */ |
| 614 | while (!(in_be16(&psc->mpc52xx_psc_status) & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | MPC52xx_PSC_SR_TXEMP) && --j) |
| 616 | udelay(1); |
| 617 | } |
| 618 | |
| 619 | /* Restore interrupt state */ |
| 620 | out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask); |
| 621 | } |
| 622 | |
| 623 | static int __init |
| 624 | mpc52xx_console_setup(struct console *co, char *options) |
| 625 | { |
| 626 | struct uart_port *port = &mpc52xx_uart_ports[co->index]; |
| 627 | |
| 628 | int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD; |
| 629 | int bits = 8; |
| 630 | int parity = 'n'; |
| 631 | int flow = 'n'; |
| 632 | |
| 633 | if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM) |
| 634 | return -EINVAL; |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | /* Basic port init. Needed since we use some uart_??? func before |
| 637 | * real init for early access */ |
| 638 | spin_lock_init(&port->lock); |
| 639 | port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */ |
| 640 | port->ops = &mpc52xx_uart_ops; |
| 641 | port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1)); |
| 642 | |
| 643 | /* We ioremap ourself */ |
| 644 | port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE); |
| 645 | if (port->membase == NULL) |
| 646 | return -EINVAL; |
| 647 | |
| 648 | /* Setup the port parameters accoding to options */ |
| 649 | if (options) |
| 650 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 651 | else |
| 652 | mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow); |
| 653 | |
| 654 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 655 | } |
| 656 | |
| 657 | |
Sylvain Munaut | 2d8179c | 2006-01-06 00:11:31 -0800 | [diff] [blame] | 658 | static struct uart_driver mpc52xx_uart_driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | static struct console mpc52xx_console = { |
Sylvain Munaut | d62de3a | 2006-01-06 00:11:32 -0800 | [diff] [blame] | 661 | .name = "ttyPSC", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | .write = mpc52xx_console_write, |
| 663 | .device = uart_console_device, |
| 664 | .setup = mpc52xx_console_setup, |
| 665 | .flags = CON_PRINTBUFFER, |
Sylvain Munaut | d62de3a | 2006-01-06 00:11:32 -0800 | [diff] [blame] | 666 | .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0 ) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | .data = &mpc52xx_uart_driver, |
| 668 | }; |
| 669 | |
Grant Likely | 9b9129e | 2006-11-27 14:21:01 -0700 | [diff] [blame^] | 670 | |
| 671 | static int __init |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | mpc52xx_console_init(void) |
| 673 | { |
| 674 | register_console(&mpc52xx_console); |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | console_initcall(mpc52xx_console_init); |
| 679 | |
| 680 | #define MPC52xx_PSC_CONSOLE &mpc52xx_console |
| 681 | #else |
| 682 | #define MPC52xx_PSC_CONSOLE NULL |
| 683 | #endif |
| 684 | |
| 685 | |
| 686 | /* ======================================================================== */ |
| 687 | /* UART Driver */ |
| 688 | /* ======================================================================== */ |
| 689 | |
| 690 | static struct uart_driver mpc52xx_uart_driver = { |
| 691 | .owner = THIS_MODULE, |
| 692 | .driver_name = "mpc52xx_psc_uart", |
Sylvain Munaut | d62de3a | 2006-01-06 00:11:32 -0800 | [diff] [blame] | 693 | .dev_name = "ttyPSC", |
Sylvain Munaut | d62de3a | 2006-01-06 00:11:32 -0800 | [diff] [blame] | 694 | .major = SERIAL_PSC_MAJOR, |
| 695 | .minor = SERIAL_PSC_MINOR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | .nr = MPC52xx_PSC_MAXNUM, |
| 697 | .cons = MPC52xx_PSC_CONSOLE, |
| 698 | }; |
| 699 | |
| 700 | |
| 701 | /* ======================================================================== */ |
| 702 | /* Platform Driver */ |
| 703 | /* ======================================================================== */ |
| 704 | |
| 705 | static int __devinit |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 706 | mpc52xx_uart_probe(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 708 | struct resource *res = dev->resource; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
| 710 | struct uart_port *port = NULL; |
| 711 | int i, idx, ret; |
| 712 | |
| 713 | /* Check validity & presence */ |
Andrey Volkov | 38801e2 | 2005-11-12 22:04:06 +0000 | [diff] [blame] | 714 | idx = dev->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM) |
| 716 | return -EINVAL; |
| 717 | |
| 718 | if (!mpc52xx_match_psc_function(idx,"uart")) |
| 719 | return -ENODEV; |
| 720 | |
| 721 | /* Init the port structure */ |
| 722 | port = &mpc52xx_uart_ports[idx]; |
| 723 | |
| 724 | memset(port, 0x00, sizeof(struct uart_port)); |
| 725 | |
| 726 | spin_lock_init(&port->lock); |
| 727 | port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */ |
Russell King | 947deee | 2006-07-02 20:45:51 +0100 | [diff] [blame] | 728 | port->fifosize = 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | port->iotype = UPIO_MEM; |
| 730 | port->flags = UPF_BOOT_AUTOCONF | |
| 731 | ( uart_console(port) ? 0 : UPF_IOREMAP ); |
| 732 | port->line = idx; |
| 733 | port->ops = &mpc52xx_uart_ops; |
| 734 | |
| 735 | /* Search for IRQ and mapbase */ |
Andrey Volkov | 38801e2 | 2005-11-12 22:04:06 +0000 | [diff] [blame] | 736 | for (i=0 ; i<dev->num_resources ; i++, res++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | if (res->flags & IORESOURCE_MEM) |
| 738 | port->mapbase = res->start; |
| 739 | else if (res->flags & IORESOURCE_IRQ) |
| 740 | port->irq = res->start; |
| 741 | } |
| 742 | if (!port->irq || !port->mapbase) |
| 743 | return -EINVAL; |
| 744 | |
| 745 | /* Add the port to the uart sub-system */ |
| 746 | ret = uart_add_one_port(&mpc52xx_uart_driver, port); |
| 747 | if (!ret) |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 748 | platform_set_drvdata(dev, (void*)port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
| 750 | return ret; |
| 751 | } |
| 752 | |
| 753 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 754 | mpc52xx_uart_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 756 | struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 758 | platform_set_drvdata(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
| 760 | if (port) |
| 761 | uart_remove_one_port(&mpc52xx_uart_driver, port); |
| 762 | |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | #ifdef CONFIG_PM |
| 767 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 768 | mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 770 | struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev); |
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 | if (port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | uart_suspend_port(&mpc52xx_uart_driver, port); |
| 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 779 | mpc52xx_uart_resume(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 781 | struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 783 | if (port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | uart_resume_port(&mpc52xx_uart_driver, port); |
| 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | #endif |
| 789 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 790 | static struct platform_driver mpc52xx_uart_platform_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | .probe = mpc52xx_uart_probe, |
| 792 | .remove = mpc52xx_uart_remove, |
| 793 | #ifdef CONFIG_PM |
| 794 | .suspend = mpc52xx_uart_suspend, |
| 795 | .resume = mpc52xx_uart_resume, |
| 796 | #endif |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 797 | .driver = { |
| 798 | .name = "mpc52xx-psc", |
| 799 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | }; |
| 801 | |
| 802 | |
| 803 | /* ======================================================================== */ |
| 804 | /* Module */ |
| 805 | /* ======================================================================== */ |
| 806 | |
| 807 | static int __init |
| 808 | mpc52xx_uart_init(void) |
| 809 | { |
| 810 | int ret; |
| 811 | |
| 812 | printk(KERN_INFO "Serial: MPC52xx PSC driver\n"); |
| 813 | |
| 814 | ret = uart_register_driver(&mpc52xx_uart_driver); |
| 815 | if (ret == 0) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 816 | ret = platform_driver_register(&mpc52xx_uart_platform_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | if (ret) |
| 818 | uart_unregister_driver(&mpc52xx_uart_driver); |
| 819 | } |
| 820 | |
| 821 | return ret; |
| 822 | } |
| 823 | |
| 824 | static void __exit |
| 825 | mpc52xx_uart_exit(void) |
| 826 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 827 | platform_driver_unregister(&mpc52xx_uart_platform_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | uart_unregister_driver(&mpc52xx_uart_driver); |
| 829 | } |
| 830 | |
| 831 | |
| 832 | module_init(mpc52xx_uart_init); |
| 833 | module_exit(mpc52xx_uart_exit); |
| 834 | |
| 835 | MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>"); |
| 836 | MODULE_DESCRIPTION("Freescale MPC52xx PSC UART"); |
| 837 | MODULE_LICENSE("GPL"); |