| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 1 | /* | 
 | 2 |  *  max3110.c - spi uart protocol driver for Maxim 3110 on Moorestown | 
 | 3 |  * | 
 | 4 |  *  Copyright (C) Intel 2008 Feng Tang <feng.tang@intel.com> | 
 | 5 |  * | 
 | 6 |  * This program is free software; you can redistribute it and/or modify it | 
 | 7 |  * under the terms and conditions of the GNU General Public License, | 
 | 8 |  * version 2, as published by the Free Software Foundation. | 
 | 9 |  * | 
 | 10 |  * This program is distributed in the hope it will be useful, but WITHOUT | 
 | 11 |  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 
 | 12 |  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for | 
 | 13 |  * more details. | 
 | 14 |  * | 
 | 15 |  * You should have received a copy of the GNU General Public License along with | 
 | 16 |  * this program; if not, write to the Free Software Foundation, Inc., | 
 | 17 |  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | 
 | 18 |  */ | 
 | 19 |  | 
 | 20 | /* | 
 | 21 |  * Note: | 
 | 22 |  * 1. From Max3110 spec, the Rx FIFO has 8 words, while the Tx FIFO only has | 
 | 23 |  *    1 word. If SPI master controller doesn't support sclk frequency change, | 
 | 24 |  *    then the char need be sent out one by one with some delay | 
 | 25 |  * | 
 | 26 |  * 2. Currently only RX availabe interrrupt is used, no need for waiting TXE | 
 | 27 |  *    interrupt for a low speed UART device | 
 | 28 |  */ | 
 | 29 |  | 
 | 30 | #include <linux/module.h> | 
 | 31 | #include <linux/ioport.h> | 
 | 32 | #include <linux/init.h> | 
 | 33 | #include <linux/console.h> | 
 | 34 | #include <linux/sysrq.h> | 
 | 35 | #include <linux/platform_device.h> | 
 | 36 | #include <linux/tty.h> | 
 | 37 | #include <linux/tty_flip.h> | 
 | 38 | #include <linux/serial_core.h> | 
 | 39 | #include <linux/serial_reg.h> | 
 | 40 |  | 
 | 41 | #include <linux/kthread.h> | 
 | 42 | #include <linux/delay.h> | 
 | 43 | #include <asm/atomic.h> | 
 | 44 | #include <linux/spi/spi.h> | 
 | 45 | #include <linux/spi/dw_spi.h> | 
 | 46 |  | 
 | 47 | #include "mrst_max3110.h" | 
 | 48 |  | 
 | 49 | #define PR_FMT	"mrst_max3110: " | 
 | 50 |  | 
| Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 51 | #define UART_TX_NEEDED 1 | 
 | 52 | #define CON_TX_NEEDED  2 | 
 | 53 | #define BIT_IRQ_PENDING    3 | 
 | 54 |  | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 55 | struct uart_max3110 { | 
 | 56 | 	struct uart_port port; | 
 | 57 | 	struct spi_device *spi; | 
 | 58 | 	char *name; | 
 | 59 |  | 
 | 60 | 	wait_queue_head_t wq; | 
 | 61 | 	struct task_struct *main_thread; | 
 | 62 | 	struct task_struct *read_thread; | 
| Arjan van de Ven | 68c16b4 | 2010-06-17 11:02:06 +0100 | [diff] [blame] | 63 | 	struct mutex thread_mutex;; | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 64 |  | 
 | 65 | 	u32 baud; | 
 | 66 | 	u16 cur_conf; | 
 | 67 | 	u8 clock; | 
 | 68 | 	u8 parity, word_7bits; | 
 | 69 |  | 
| Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 70 | 	unsigned long uart_flags; | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 71 |  | 
 | 72 | 	/* console related */ | 
 | 73 | 	struct circ_buf con_xmit; | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 74 |  | 
 | 75 | 	/* irq related */ | 
 | 76 | 	u16 irq; | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 77 | }; | 
 | 78 |  | 
 | 79 | /* global data structure, may need be removed */ | 
 | 80 | struct uart_max3110 *pmax; | 
 | 81 | static inline void receive_char(struct uart_max3110 *max, u8 ch); | 
 | 82 | static void receive_chars(struct uart_max3110 *max, | 
 | 83 | 				unsigned char *str, int len); | 
 | 84 | static int max3110_read_multi(struct uart_max3110 *max, int len, u8 *buf); | 
 | 85 | static void max3110_console_receive(struct uart_max3110 *max); | 
 | 86 |  | 
 | 87 | int max3110_write_then_read(struct uart_max3110 *max, | 
 | 88 | 		const u8 *txbuf, u8 *rxbuf, unsigned len, int always_fast) | 
 | 89 | { | 
 | 90 | 	struct spi_device *spi = max->spi; | 
 | 91 | 	struct spi_message	message; | 
 | 92 | 	struct spi_transfer	x; | 
 | 93 | 	int ret; | 
 | 94 |  | 
 | 95 | 	if (!txbuf || !rxbuf) | 
 | 96 | 		return -EINVAL; | 
 | 97 |  | 
 | 98 | 	spi_message_init(&message); | 
 | 99 | 	memset(&x, 0, sizeof x); | 
 | 100 | 	x.len = len; | 
 | 101 | 	x.tx_buf = txbuf; | 
 | 102 | 	x.rx_buf = rxbuf; | 
 | 103 | 	spi_message_add_tail(&x, &message); | 
 | 104 |  | 
 | 105 | 	if (always_fast) | 
 | 106 | 		x.speed_hz = 3125000; | 
 | 107 | 	else if (max->baud) | 
 | 108 | 		x.speed_hz = max->baud; | 
 | 109 |  | 
 | 110 | 	/* Do the i/o */ | 
 | 111 | 	ret = spi_sync(spi, &message); | 
 | 112 | 	return ret; | 
 | 113 | } | 
 | 114 |  | 
 | 115 | /* Write a u16 to the device, and return one u16 read back */ | 
 | 116 | int max3110_out(struct uart_max3110 *max, const u16 out) | 
 | 117 | { | 
 | 118 | 	u16 tmp; | 
 | 119 | 	int ret; | 
 | 120 |  | 
 | 121 | 	ret = max3110_write_then_read(max, (u8 *)&out, (u8 *)&tmp, 2, 1); | 
 | 122 | 	if (ret) | 
 | 123 | 		return ret; | 
 | 124 |  | 
 | 125 | 	/* If some valid data is read back */ | 
 | 126 | 	if (tmp & MAX3110_READ_DATA_AVAILABLE) | 
 | 127 | 		receive_char(max, (tmp & 0xff)); | 
 | 128 |  | 
 | 129 | 	return ret; | 
 | 130 | } | 
 | 131 |  | 
 | 132 | #define MAX_READ_LEN	20 | 
 | 133 | /* | 
 | 134 |  * This is usually used to read data from SPIC RX FIFO, which doesn't | 
 | 135 |  * need any delay like flushing character out. It returns how many | 
 | 136 |  * valide bytes are read back | 
 | 137 |  */ | 
 | 138 | static int max3110_read_multi(struct uart_max3110 *max, int len, u8 *buf) | 
 | 139 | { | 
 | 140 | 	u16 out[MAX_READ_LEN], in[MAX_READ_LEN]; | 
 | 141 | 	u8 *pbuf, valid_str[MAX_READ_LEN]; | 
 | 142 | 	int i, j, bytelen; | 
 | 143 |  | 
 | 144 | 	if (len > MAX_READ_LEN) { | 
 | 145 | 		pr_err(PR_FMT "read len %d is too large\n", len); | 
 | 146 | 		return 0; | 
 | 147 | 	} | 
 | 148 |  | 
 | 149 | 	bytelen = len * 2; | 
 | 150 | 	memset(out, 0, bytelen); | 
 | 151 | 	memset(in, 0, bytelen); | 
 | 152 |  | 
 | 153 | 	if (max3110_write_then_read(max, (u8 *)out, (u8 *)in, bytelen, 1)) | 
 | 154 | 		return 0; | 
 | 155 |  | 
 | 156 | 	/* If caller don't provide a buffer, then handle received char */ | 
 | 157 | 	pbuf = buf ? buf : valid_str; | 
 | 158 |  | 
 | 159 | 	for (i = 0, j = 0; i < len; i++) { | 
 | 160 | 		if (in[i] & MAX3110_READ_DATA_AVAILABLE) | 
 | 161 | 			pbuf[j++] = (u8)(in[i] & 0xff); | 
 | 162 | 	} | 
 | 163 |  | 
 | 164 | 	if (j && (pbuf == valid_str)) | 
 | 165 | 		receive_chars(max, valid_str, j); | 
 | 166 |  | 
 | 167 | 	return j; | 
 | 168 | } | 
 | 169 |  | 
 | 170 | static void serial_m3110_con_putchar(struct uart_port *port, int ch) | 
 | 171 | { | 
 | 172 | 	struct uart_max3110 *max = | 
 | 173 | 		container_of(port, struct uart_max3110, port); | 
 | 174 | 	struct circ_buf *xmit = &max->con_xmit; | 
 | 175 |  | 
 | 176 | 	if (uart_circ_chars_free(xmit)) { | 
 | 177 | 		xmit->buf[xmit->head] = (char)ch; | 
 | 178 | 		xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1); | 
 | 179 | 	} | 
 | 180 |  | 
| Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 181 |  | 
 | 182 | 	if (!test_and_set_bit(CON_TX_NEEDED, &max->uart_flags)) | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 183 | 		wake_up_process(max->main_thread); | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 184 | } | 
 | 185 |  | 
 | 186 | /* | 
 | 187 |  * Print a string to the serial port trying not to disturb | 
 | 188 |  * any possible real use of the port... | 
 | 189 |  * | 
 | 190 |  *	The console_lock must be held when we get here. | 
 | 191 |  */ | 
 | 192 | static void serial_m3110_con_write(struct console *co, | 
 | 193 | 				const char *s, unsigned int count) | 
 | 194 | { | 
 | 195 | 	if (!pmax) | 
 | 196 | 		return; | 
 | 197 |  | 
 | 198 | 	uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar); | 
 | 199 | } | 
 | 200 |  | 
 | 201 | static int __init | 
 | 202 | serial_m3110_con_setup(struct console *co, char *options) | 
 | 203 | { | 
 | 204 | 	struct uart_max3110 *max = pmax; | 
 | 205 | 	int baud = 115200; | 
 | 206 | 	int bits = 8; | 
 | 207 | 	int parity = 'n'; | 
 | 208 | 	int flow = 'n'; | 
 | 209 |  | 
 | 210 | 	pr_info(PR_FMT "setting up console\n"); | 
 | 211 |  | 
 | 212 | 	if (!max) { | 
 | 213 | 		pr_err(PR_FMT "pmax is NULL, return"); | 
 | 214 | 		return -ENODEV; | 
 | 215 | 	} | 
 | 216 |  | 
 | 217 | 	if (options) | 
 | 218 | 		uart_parse_options(options, &baud, &parity, &bits, &flow); | 
 | 219 |  | 
 | 220 | 	return uart_set_options(&max->port, co, baud, parity, bits, flow); | 
 | 221 | } | 
 | 222 |  | 
 | 223 | static struct tty_driver *serial_m3110_con_device(struct console *co, | 
 | 224 | 							int *index) | 
 | 225 | { | 
 | 226 | 	struct uart_driver *p = co->data; | 
 | 227 | 	*index = co->index; | 
 | 228 | 	return p->tty_driver; | 
 | 229 | } | 
 | 230 |  | 
 | 231 | static struct uart_driver serial_m3110_reg; | 
 | 232 | static struct console serial_m3110_console = { | 
 | 233 | 	.name		= "ttyS", | 
 | 234 | 	.write		= serial_m3110_con_write, | 
 | 235 | 	.device		= serial_m3110_con_device, | 
 | 236 | 	.setup		= serial_m3110_con_setup, | 
 | 237 | 	.flags		= CON_PRINTBUFFER, | 
 | 238 | 	.index		= -1, | 
 | 239 | 	.data		= &serial_m3110_reg, | 
 | 240 | }; | 
 | 241 |  | 
 | 242 | #define MRST_CONSOLE	(&serial_m3110_console) | 
 | 243 |  | 
 | 244 | static unsigned int serial_m3110_tx_empty(struct uart_port *port) | 
 | 245 | { | 
 | 246 | 	return 1; | 
 | 247 | } | 
 | 248 |  | 
 | 249 | static void serial_m3110_stop_tx(struct uart_port *port) | 
 | 250 | { | 
 | 251 | 	return; | 
 | 252 | } | 
 | 253 |  | 
 | 254 | /* stop_rx will be called in spin_lock env */ | 
 | 255 | static void serial_m3110_stop_rx(struct uart_port *port) | 
 | 256 | { | 
 | 257 | 	return; | 
 | 258 | } | 
 | 259 |  | 
 | 260 | #define WORDS_PER_XFER	128 | 
 | 261 | static inline void send_circ_buf(struct uart_max3110 *max, | 
 | 262 | 				struct circ_buf *xmit) | 
 | 263 | { | 
 | 264 | 	int len, left = 0; | 
 | 265 | 	u16 obuf[WORDS_PER_XFER], ibuf[WORDS_PER_XFER]; | 
 | 266 | 	u8 valid_str[WORDS_PER_XFER]; | 
 | 267 | 	int i, j; | 
 | 268 |  | 
 | 269 | 	while (!uart_circ_empty(xmit)) { | 
 | 270 | 		left = uart_circ_chars_pending(xmit); | 
 | 271 | 		while (left) { | 
 | 272 | 			len = (left >= WORDS_PER_XFER) ? WORDS_PER_XFER : left; | 
 | 273 |  | 
 | 274 | 			memset(obuf, 0, len * 2); | 
 | 275 | 			memset(ibuf, 0, len * 2); | 
 | 276 | 			for (i = 0; i < len; i++) { | 
 | 277 | 				obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG; | 
 | 278 | 				xmit->tail = (xmit->tail + 1) & | 
 | 279 | 						(UART_XMIT_SIZE - 1); | 
 | 280 | 			} | 
 | 281 | 			max3110_write_then_read(max, (u8 *)obuf, | 
 | 282 | 						(u8 *)ibuf, len * 2, 0); | 
 | 283 |  | 
 | 284 | 			for (i = 0, j = 0; i < len; i++) { | 
 | 285 | 				if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE) | 
 | 286 | 					valid_str[j++] = (u8)(ibuf[i] & 0xff); | 
 | 287 | 			} | 
 | 288 |  | 
 | 289 | 			if (j) | 
 | 290 | 				receive_chars(max, valid_str, j); | 
 | 291 |  | 
 | 292 | 			max->port.icount.tx += len; | 
 | 293 | 			left -= len; | 
 | 294 | 		} | 
 | 295 | 	} | 
 | 296 | } | 
 | 297 |  | 
 | 298 | static void transmit_char(struct uart_max3110 *max) | 
 | 299 | { | 
 | 300 | 	struct uart_port *port = &max->port; | 
 | 301 | 	struct circ_buf *xmit = &port->state->xmit; | 
 | 302 |  | 
 | 303 | 	if (uart_circ_empty(xmit) || uart_tx_stopped(port)) | 
 | 304 | 		return; | 
 | 305 |  | 
 | 306 | 	send_circ_buf(max, xmit); | 
 | 307 |  | 
 | 308 | 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) | 
 | 309 | 		uart_write_wakeup(port); | 
 | 310 |  | 
 | 311 | 	if (uart_circ_empty(xmit)) | 
 | 312 | 		serial_m3110_stop_tx(port); | 
 | 313 | } | 
 | 314 |  | 
 | 315 | /* This will be called by uart_write() and tty_write, can't | 
 | 316 |  * go to sleep */ | 
 | 317 | static void serial_m3110_start_tx(struct uart_port *port) | 
 | 318 | { | 
 | 319 | 	struct uart_max3110 *max = | 
 | 320 | 		container_of(port, struct uart_max3110, port); | 
 | 321 |  | 
| Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 322 | 	if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags)) | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 323 | 		wake_up_process(max->main_thread); | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 324 | } | 
 | 325 |  | 
 | 326 | static void receive_chars(struct uart_max3110 *max, unsigned char *str, int len) | 
 | 327 | { | 
 | 328 | 	struct uart_port *port = &max->port; | 
 | 329 | 	struct tty_struct *tty; | 
 | 330 | 	int usable; | 
 | 331 |  | 
 | 332 | 	/* If uart is not opened, just return */ | 
 | 333 | 	if (!port->state) | 
 | 334 | 		return; | 
 | 335 |  | 
 | 336 | 	tty = port->state->port.tty; | 
 | 337 | 	if (!tty) | 
 | 338 | 		return;	/* receive some char before the tty is opened */ | 
 | 339 |  | 
 | 340 | 	while (len) { | 
 | 341 | 		usable = tty_buffer_request_room(tty, len); | 
 | 342 | 		if (usable) { | 
 | 343 | 			tty_insert_flip_string(tty, str, usable); | 
 | 344 | 			str += usable; | 
 | 345 | 			port->icount.rx += usable; | 
 | 346 | 			tty_flip_buffer_push(tty); | 
 | 347 | 		} | 
 | 348 | 		len -= usable; | 
 | 349 | 	} | 
 | 350 | } | 
 | 351 |  | 
 | 352 | static inline void receive_char(struct uart_max3110 *max, u8 ch) | 
 | 353 | { | 
 | 354 | 	receive_chars(max, &ch, 1); | 
 | 355 | } | 
 | 356 |  | 
 | 357 | static void max3110_console_receive(struct uart_max3110 *max) | 
 | 358 | { | 
 | 359 | 	int loop = 1, num, total = 0; | 
 | 360 | 	u8 recv_buf[512], *pbuf; | 
 | 361 |  | 
 | 362 | 	pbuf = recv_buf; | 
 | 363 | 	do { | 
 | 364 | 		num = max3110_read_multi(max, 8, pbuf); | 
 | 365 |  | 
 | 366 | 		if (num) { | 
 | 367 | 			loop = 10; | 
 | 368 | 			pbuf += num; | 
 | 369 | 			total += num; | 
 | 370 |  | 
 | 371 | 			if (total >= 500) { | 
 | 372 | 				receive_chars(max, recv_buf, total); | 
 | 373 | 				pbuf = recv_buf; | 
 | 374 | 				total = 0; | 
 | 375 | 			} | 
 | 376 | 		} | 
 | 377 | 	} while (--loop); | 
 | 378 |  | 
 | 379 | 	if (total) | 
 | 380 | 		receive_chars(max, recv_buf, total); | 
 | 381 | } | 
 | 382 |  | 
 | 383 | static int max3110_main_thread(void *_max) | 
 | 384 | { | 
 | 385 | 	struct uart_max3110 *max = _max; | 
 | 386 | 	wait_queue_head_t *wq = &max->wq; | 
 | 387 | 	int ret = 0; | 
 | 388 | 	struct circ_buf *xmit = &max->con_xmit; | 
 | 389 |  | 
 | 390 | 	init_waitqueue_head(wq); | 
 | 391 | 	pr_info(PR_FMT "start main thread\n"); | 
 | 392 |  | 
 | 393 | 	do { | 
| Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 394 | 		wait_event_interruptible(*wq, max->uart_flags || kthread_should_stop()); | 
| Arjan van de Ven | 68c16b4 | 2010-06-17 11:02:06 +0100 | [diff] [blame] | 395 |  | 
 | 396 | 		mutex_lock(&max->thread_mutex); | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 397 |  | 
| Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 398 | 		if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags)) | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 399 | 			max3110_console_receive(max); | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 400 |  | 
 | 401 | 		/* first handle console output */ | 
| Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 402 | 		if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags)) | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 403 | 			send_circ_buf(max, xmit); | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 404 |  | 
 | 405 | 		/* handle uart output */ | 
| Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 406 | 		if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags)) | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 407 | 			transmit_char(max); | 
| Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 408 |  | 
| Arjan van de Ven | 68c16b4 | 2010-06-17 11:02:06 +0100 | [diff] [blame] | 409 | 		mutex_unlock(&max->thread_mutex); | 
| Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 410 |  | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 411 | 	} while (!kthread_should_stop()); | 
 | 412 |  | 
 | 413 | 	return ret; | 
 | 414 | } | 
 | 415 |  | 
 | 416 | #ifdef CONFIG_MRST_MAX3110_IRQ | 
 | 417 | static irqreturn_t serial_m3110_irq(int irq, void *dev_id) | 
 | 418 | { | 
 | 419 | 	struct uart_max3110 *max = dev_id; | 
 | 420 |  | 
 | 421 | 	/* max3110's irq is a falling edge, not level triggered, | 
 | 422 | 	 * so no need to disable the irq */ | 
| Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 423 | 	if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags)) | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 424 | 		wake_up_process(max->main_thread); | 
| Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 425 |  | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 426 | 	return IRQ_HANDLED; | 
 | 427 | } | 
 | 428 | #else | 
 | 429 | /* if don't use RX IRQ, then need a thread to polling read */ | 
 | 430 | static int max3110_read_thread(void *_max) | 
 | 431 | { | 
 | 432 | 	struct uart_max3110 *max = _max; | 
 | 433 |  | 
 | 434 | 	pr_info(PR_FMT "start read thread\n"); | 
 | 435 | 	do { | 
| Arjan van de Ven | 68c16b4 | 2010-06-17 11:02:06 +0100 | [diff] [blame] | 436 | 		mutex_lock(&max->thread_mutex); | 
 | 437 | 		max3110_console_receive(max); | 
 | 438 | 		mutex_unlock(&max->thread_mutex); | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 439 |  | 
 | 440 | 		set_current_state(TASK_INTERRUPTIBLE); | 
 | 441 | 		schedule_timeout(HZ / 20); | 
 | 442 | 	} while (!kthread_should_stop()); | 
 | 443 |  | 
 | 444 | 	return 0; | 
 | 445 | } | 
 | 446 | #endif | 
 | 447 |  | 
 | 448 | static int serial_m3110_startup(struct uart_port *port) | 
 | 449 | { | 
 | 450 | 	struct uart_max3110 *max = | 
 | 451 | 		container_of(port, struct uart_max3110, port); | 
 | 452 | 	u16 config = 0; | 
 | 453 | 	int ret = 0; | 
 | 454 |  | 
 | 455 | 	if (port->line != 0) | 
 | 456 | 		pr_err(PR_FMT "uart port startup failed\n"); | 
 | 457 |  | 
 | 458 | 	/* firstly disable all IRQ and config it to 115200, 8n1 */ | 
 | 459 | 	config = WC_TAG | WC_FIFO_ENABLE | 
 | 460 | 			| WC_1_STOPBITS | 
 | 461 | 			| WC_8BIT_WORD | 
 | 462 | 			| WC_BAUD_DR2; | 
 | 463 | 	ret = max3110_out(max, config); | 
 | 464 |  | 
 | 465 | 	/* as we use thread to handle tx/rx, need set low latency */ | 
 | 466 | 	port->state->port.tty->low_latency = 1; | 
 | 467 |  | 
 | 468 | #ifdef CONFIG_MRST_MAX3110_IRQ | 
 | 469 | 	ret = request_irq(max->irq, serial_m3110_irq, | 
 | 470 | 				IRQ_TYPE_EDGE_FALLING, "max3110", max); | 
 | 471 | 	if (ret) | 
 | 472 | 		return ret; | 
 | 473 |  | 
 | 474 | 	/* enable RX IRQ only */ | 
 | 475 | 	config |= WC_RXA_IRQ_ENABLE; | 
 | 476 | 	max3110_out(max, config); | 
 | 477 | #else | 
 | 478 | 	/* if IRQ is disabled, start a read thread for input data */ | 
 | 479 | 	max->read_thread = | 
 | 480 | 		kthread_run(max3110_read_thread, max, "max3110_read"); | 
 | 481 | #endif | 
 | 482 |  | 
 | 483 | 	max->cur_conf = config; | 
 | 484 | 	return 0; | 
 | 485 | } | 
 | 486 |  | 
 | 487 | static void serial_m3110_shutdown(struct uart_port *port) | 
 | 488 | { | 
 | 489 | 	struct uart_max3110 *max = | 
 | 490 | 		container_of(port, struct uart_max3110, port); | 
 | 491 | 	u16 config; | 
 | 492 |  | 
 | 493 | 	if (max->read_thread) { | 
 | 494 | 		kthread_stop(max->read_thread); | 
 | 495 | 		max->read_thread = NULL; | 
 | 496 | 	} | 
 | 497 |  | 
 | 498 | #ifdef CONFIG_MRST_MAX3110_IRQ | 
 | 499 | 	free_irq(max->irq, max); | 
 | 500 | #endif | 
 | 501 |  | 
 | 502 | 	/* Disable interrupts from this port */ | 
 | 503 | 	config = WC_TAG | WC_SW_SHDI; | 
 | 504 | 	max3110_out(max, config); | 
 | 505 | } | 
 | 506 |  | 
 | 507 | static void serial_m3110_release_port(struct uart_port *port) | 
 | 508 | { | 
 | 509 | } | 
 | 510 |  | 
 | 511 | static int serial_m3110_request_port(struct uart_port *port) | 
 | 512 | { | 
 | 513 | 	return 0; | 
 | 514 | } | 
 | 515 |  | 
 | 516 | static void serial_m3110_config_port(struct uart_port *port, int flags) | 
 | 517 | { | 
 | 518 | 	/* give it fake type */ | 
 | 519 | 	port->type = PORT_PXA; | 
 | 520 | } | 
 | 521 |  | 
 | 522 | static int | 
 | 523 | serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser) | 
 | 524 | { | 
 | 525 | 	/* we don't want the core code to modify any port params */ | 
 | 526 | 	return -EINVAL; | 
 | 527 | } | 
 | 528 |  | 
 | 529 |  | 
 | 530 | static const char *serial_m3110_type(struct uart_port *port) | 
 | 531 | { | 
 | 532 | 	struct uart_max3110 *max = | 
 | 533 | 		container_of(port, struct uart_max3110, port); | 
 | 534 | 	return max->name; | 
 | 535 | } | 
 | 536 |  | 
 | 537 | static void | 
 | 538 | serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, | 
 | 539 | 		       struct ktermios *old) | 
 | 540 | { | 
 | 541 | 	struct uart_max3110 *max = | 
 | 542 | 		container_of(port, struct uart_max3110, port); | 
 | 543 | 	unsigned char cval; | 
 | 544 | 	unsigned int baud, parity = 0; | 
 | 545 | 	int clk_div = -1; | 
 | 546 | 	u16 new_conf = max->cur_conf; | 
 | 547 |  | 
 | 548 | 	switch (termios->c_cflag & CSIZE) { | 
 | 549 | 	case CS7: | 
 | 550 | 		cval = UART_LCR_WLEN7; | 
 | 551 | 		new_conf |= WC_7BIT_WORD; | 
 | 552 | 		break; | 
 | 553 | 	default: | 
 | 554 | 	case CS8: | 
 | 555 | 		cval = UART_LCR_WLEN8; | 
 | 556 | 		new_conf |= WC_8BIT_WORD; | 
 | 557 | 		break; | 
 | 558 | 	} | 
 | 559 |  | 
 | 560 | 	baud = uart_get_baud_rate(port, termios, old, 0, 230400); | 
 | 561 |  | 
 | 562 | 	/* first calc the div for 1.8MHZ clock case */ | 
 | 563 | 	switch (baud) { | 
 | 564 | 	case 300: | 
 | 565 | 		clk_div = WC_BAUD_DR384; | 
 | 566 | 		break; | 
 | 567 | 	case 600: | 
 | 568 | 		clk_div = WC_BAUD_DR192; | 
 | 569 | 		break; | 
 | 570 | 	case 1200: | 
 | 571 | 		clk_div = WC_BAUD_DR96; | 
 | 572 | 		break; | 
 | 573 | 	case 2400: | 
 | 574 | 		clk_div = WC_BAUD_DR48; | 
 | 575 | 		break; | 
 | 576 | 	case 4800: | 
 | 577 | 		clk_div = WC_BAUD_DR24; | 
 | 578 | 		break; | 
 | 579 | 	case 9600: | 
 | 580 | 		clk_div = WC_BAUD_DR12; | 
 | 581 | 		break; | 
 | 582 | 	case 19200: | 
 | 583 | 		clk_div = WC_BAUD_DR6; | 
 | 584 | 		break; | 
 | 585 | 	case 38400: | 
 | 586 | 		clk_div = WC_BAUD_DR3; | 
 | 587 | 		break; | 
 | 588 | 	case 57600: | 
 | 589 | 		clk_div = WC_BAUD_DR2; | 
 | 590 | 		break; | 
 | 591 | 	case 115200: | 
 | 592 | 		clk_div = WC_BAUD_DR1; | 
 | 593 | 		break; | 
 | 594 | 	case 230400: | 
 | 595 | 		if (max->clock & MAX3110_HIGH_CLK) | 
 | 596 | 			break; | 
 | 597 | 	default: | 
 | 598 | 		/* pick the previous baud rate */ | 
 | 599 | 		baud = max->baud; | 
 | 600 | 		clk_div = max->cur_conf & WC_BAUD_DIV_MASK; | 
 | 601 | 		tty_termios_encode_baud_rate(termios, baud, baud); | 
 | 602 | 	} | 
 | 603 |  | 
 | 604 | 	if (max->clock & MAX3110_HIGH_CLK) { | 
 | 605 | 		clk_div += 1; | 
 | 606 | 		/* high clk version max3110 doesn't support B300 */ | 
 | 607 | 		if (baud == 300) | 
 | 608 | 			baud = 600; | 
 | 609 | 		if (baud == 230400) | 
 | 610 | 			clk_div = WC_BAUD_DR1; | 
 | 611 | 		tty_termios_encode_baud_rate(termios, baud, baud); | 
 | 612 | 	} | 
 | 613 |  | 
 | 614 | 	new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div; | 
 | 615 | 	if (termios->c_cflag & CSTOPB) | 
 | 616 | 		new_conf |= WC_2_STOPBITS; | 
 | 617 | 	else | 
 | 618 | 		new_conf &= ~WC_2_STOPBITS; | 
 | 619 |  | 
 | 620 | 	if (termios->c_cflag & PARENB) { | 
 | 621 | 		new_conf |= WC_PARITY_ENABLE; | 
 | 622 | 		parity |= UART_LCR_PARITY; | 
 | 623 | 	} else | 
 | 624 | 		new_conf &= ~WC_PARITY_ENABLE; | 
 | 625 |  | 
 | 626 | 	if (!(termios->c_cflag & PARODD)) | 
 | 627 | 		parity |= UART_LCR_EPAR; | 
 | 628 | 	max->parity = parity; | 
 | 629 |  | 
 | 630 | 	uart_update_timeout(port, termios->c_cflag, baud); | 
 | 631 |  | 
 | 632 | 	new_conf |= WC_TAG; | 
 | 633 | 	if (new_conf != max->cur_conf) { | 
 | 634 | 		max3110_out(max, new_conf); | 
 | 635 | 		max->cur_conf = new_conf; | 
 | 636 | 		max->baud = baud; | 
 | 637 | 	} | 
 | 638 | } | 
 | 639 |  | 
 | 640 | /* don't handle hw handshaking */ | 
 | 641 | static unsigned int serial_m3110_get_mctrl(struct uart_port *port) | 
 | 642 | { | 
 | 643 | 	return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR; | 
 | 644 | } | 
 | 645 |  | 
 | 646 | static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl) | 
 | 647 | { | 
 | 648 | } | 
 | 649 |  | 
 | 650 | static void serial_m3110_break_ctl(struct uart_port *port, int break_state) | 
 | 651 | { | 
 | 652 | } | 
 | 653 |  | 
 | 654 | static void serial_m3110_pm(struct uart_port *port, unsigned int state, | 
 | 655 | 			unsigned int oldstate) | 
 | 656 | { | 
 | 657 | } | 
 | 658 |  | 
 | 659 | static void serial_m3110_enable_ms(struct uart_port *port) | 
 | 660 | { | 
 | 661 | } | 
 | 662 |  | 
 | 663 | struct uart_ops serial_m3110_ops = { | 
 | 664 | 	.tx_empty	= serial_m3110_tx_empty, | 
 | 665 | 	.set_mctrl	= serial_m3110_set_mctrl, | 
 | 666 | 	.get_mctrl	= serial_m3110_get_mctrl, | 
 | 667 | 	.stop_tx	= serial_m3110_stop_tx, | 
 | 668 | 	.start_tx	= serial_m3110_start_tx, | 
 | 669 | 	.stop_rx	= serial_m3110_stop_rx, | 
 | 670 | 	.enable_ms	= serial_m3110_enable_ms, | 
 | 671 | 	.break_ctl	= serial_m3110_break_ctl, | 
 | 672 | 	.startup	= serial_m3110_startup, | 
 | 673 | 	.shutdown	= serial_m3110_shutdown, | 
 | 674 | 	.set_termios	= serial_m3110_set_termios,	/* must have */ | 
 | 675 | 	.pm		= serial_m3110_pm, | 
 | 676 | 	.type		= serial_m3110_type, | 
 | 677 | 	.release_port	= serial_m3110_release_port, | 
 | 678 | 	.request_port	= serial_m3110_request_port, | 
 | 679 | 	.config_port	= serial_m3110_config_port, | 
 | 680 | 	.verify_port	= serial_m3110_verify_port, | 
 | 681 | }; | 
 | 682 |  | 
 | 683 | static struct uart_driver serial_m3110_reg = { | 
 | 684 | 	.owner		= THIS_MODULE, | 
 | 685 | 	.driver_name	= "MRST serial", | 
 | 686 | 	.dev_name	= "ttyS", | 
 | 687 | 	.major		= TTY_MAJOR, | 
 | 688 | 	.minor		= 64, | 
 | 689 | 	.nr		= 1, | 
 | 690 | 	.cons		= MRST_CONSOLE, | 
 | 691 | }; | 
 | 692 |  | 
 | 693 | static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state) | 
 | 694 | { | 
 | 695 | 	return 0; | 
 | 696 | } | 
 | 697 |  | 
 | 698 | static int serial_m3110_resume(struct spi_device *spi) | 
 | 699 | { | 
 | 700 | 	return 0; | 
 | 701 | } | 
 | 702 |  | 
 | 703 | static struct dw_spi_chip spi0_uart = { | 
 | 704 | 	.poll_mode = 1, | 
 | 705 | 	.enable_dma = 0, | 
 | 706 | 	.type = SPI_FRF_SPI, | 
 | 707 | }; | 
 | 708 |  | 
 | 709 | static int serial_m3110_probe(struct spi_device *spi) | 
 | 710 | { | 
 | 711 | 	struct uart_max3110 *max; | 
 | 712 | 	int ret; | 
 | 713 | 	unsigned char *buffer; | 
| jianwei.yang | 99dd3f6 | 2010-06-16 14:46:49 +0100 | [diff] [blame] | 714 | 	u16 res; | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 715 | 	max = kzalloc(sizeof(*max), GFP_KERNEL); | 
 | 716 | 	if (!max) | 
 | 717 | 		return -ENOMEM; | 
 | 718 |  | 
 | 719 | 	/* set spi info */ | 
 | 720 | 	spi->mode = SPI_MODE_0; | 
 | 721 | 	spi->bits_per_word = 16; | 
 | 722 | 	max->clock = MAX3110_HIGH_CLK; | 
 | 723 | 	spi->controller_data = &spi0_uart; | 
 | 724 |  | 
 | 725 | 	spi_setup(spi); | 
 | 726 |  | 
 | 727 | 	max->port.type = PORT_PXA;	/* need apply for a max3110 type */ | 
 | 728 | 	max->port.fifosize = 2;		/* only have 16b buffer */ | 
 | 729 | 	max->port.ops = &serial_m3110_ops; | 
 | 730 | 	max->port.line = 0; | 
 | 731 | 	max->port.dev = &spi->dev; | 
 | 732 | 	max->port.uartclk = 115200; | 
 | 733 |  | 
 | 734 | 	max->spi = spi; | 
 | 735 | 	max->name = spi->modalias;	/* use spi name as the name */ | 
 | 736 | 	max->irq = (u16)spi->irq; | 
 | 737 |  | 
| Arjan van de Ven | 68c16b4 | 2010-06-17 11:02:06 +0100 | [diff] [blame] | 738 | 	mutex_init(&max->thread_mutex); | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 739 |  | 
 | 740 | 	max->word_7bits = 0; | 
 | 741 | 	max->parity = 0; | 
 | 742 | 	max->baud = 0; | 
 | 743 |  | 
 | 744 | 	max->cur_conf = 0; | 
| Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 745 | 	max->uart_flags = 0; | 
 | 746 |  | 
| jianwei.yang | 99dd3f6 | 2010-06-16 14:46:49 +0100 | [diff] [blame] | 747 | 	/* Check if reading configuration register returns something sane */ | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 748 |  | 
| jianwei.yang | 99dd3f6 | 2010-06-16 14:46:49 +0100 | [diff] [blame] | 749 | 	res = RC_TAG; | 
 | 750 | 	ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0); | 
 | 751 | 	if (ret < 0 || res == 0 || res == 0xffff) { | 
 | 752 | 		printk(KERN_ERR "MAX3111 deemed not present (conf reg %04x)", | 
 | 753 | 									res); | 
 | 754 | 		ret = -ENODEV; | 
 | 755 | 		goto err_get_page; | 
 | 756 | 	} | 
| Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 757 | 	buffer = (unsigned char *)__get_free_page(GFP_KERNEL); | 
 | 758 | 	if (!buffer) { | 
 | 759 | 		ret = -ENOMEM; | 
 | 760 | 		goto err_get_page; | 
 | 761 | 	} | 
 | 762 | 	max->con_xmit.buf = (unsigned char *)buffer; | 
 | 763 | 	max->con_xmit.head = max->con_xmit.tail = 0; | 
 | 764 |  | 
 | 765 | 	max->main_thread = kthread_run(max3110_main_thread, | 
 | 766 | 					max, "max3110_main"); | 
 | 767 | 	if (IS_ERR(max->main_thread)) { | 
 | 768 | 		ret = PTR_ERR(max->main_thread); | 
 | 769 | 		goto err_kthread; | 
 | 770 | 	} | 
 | 771 |  | 
 | 772 | 	pmax = max; | 
 | 773 | 	/* give membase a psudo value to pass serial_core's check */ | 
 | 774 | 	max->port.membase = (void *)0xff110000; | 
 | 775 | 	uart_add_one_port(&serial_m3110_reg, &max->port); | 
 | 776 |  | 
 | 777 | 	return 0; | 
 | 778 |  | 
 | 779 | err_kthread: | 
 | 780 | 	free_page((unsigned long)buffer); | 
 | 781 | err_get_page: | 
 | 782 | 	pmax = NULL; | 
 | 783 | 	kfree(max); | 
 | 784 | 	return ret; | 
 | 785 | } | 
 | 786 |  | 
 | 787 | static int max3110_remove(struct spi_device *dev) | 
 | 788 | { | 
 | 789 | 	struct uart_max3110 *max = pmax; | 
 | 790 |  | 
 | 791 | 	if (!pmax) | 
 | 792 | 		return 0; | 
 | 793 |  | 
 | 794 | 	pmax = NULL; | 
 | 795 | 	uart_remove_one_port(&serial_m3110_reg, &max->port); | 
 | 796 |  | 
 | 797 | 	free_page((unsigned long)max->con_xmit.buf); | 
 | 798 |  | 
 | 799 | 	if (max->main_thread) | 
 | 800 | 		kthread_stop(max->main_thread); | 
 | 801 |  | 
 | 802 | 	kfree(max); | 
 | 803 | 	return 0; | 
 | 804 | } | 
 | 805 |  | 
 | 806 | static struct spi_driver uart_max3110_driver = { | 
 | 807 | 	.driver = { | 
 | 808 | 			.name	= "spi_max3111", | 
 | 809 | 			.bus	= &spi_bus_type, | 
 | 810 | 			.owner	= THIS_MODULE, | 
 | 811 | 	}, | 
 | 812 | 	.probe		= serial_m3110_probe, | 
 | 813 | 	.remove		= __devexit_p(max3110_remove), | 
 | 814 | 	.suspend	= serial_m3110_suspend, | 
 | 815 | 	.resume		= serial_m3110_resume, | 
 | 816 | }; | 
 | 817 |  | 
 | 818 |  | 
 | 819 | int __init serial_m3110_init(void) | 
 | 820 | { | 
 | 821 | 	int ret = 0; | 
 | 822 |  | 
 | 823 | 	ret = uart_register_driver(&serial_m3110_reg); | 
 | 824 | 	if (ret) | 
 | 825 | 		return ret; | 
 | 826 |  | 
 | 827 | 	ret = spi_register_driver(&uart_max3110_driver); | 
 | 828 | 	if (ret) | 
 | 829 | 		uart_unregister_driver(&serial_m3110_reg); | 
 | 830 |  | 
 | 831 | 	return ret; | 
 | 832 | } | 
 | 833 |  | 
 | 834 | void __exit serial_m3110_exit(void) | 
 | 835 | { | 
 | 836 | 	spi_unregister_driver(&uart_max3110_driver); | 
 | 837 | 	uart_unregister_driver(&serial_m3110_reg); | 
 | 838 | } | 
 | 839 |  | 
 | 840 | module_init(serial_m3110_init); | 
 | 841 | module_exit(serial_m3110_exit); | 
 | 842 |  | 
 | 843 | MODULE_LICENSE("GPL"); | 
 | 844 | MODULE_ALIAS("max3110-uart"); |