Commit | Line | Data |
---|---|---|
22510995 | 1 | /* |
ee9b4500 | 2 | * mrst_max3110.c - spi uart protocol driver for Maxim 3110 |
22510995 | 3 | * |
ee9b4500 | 4 | * Copyright (c) 2008-2010, Intel Corporation. |
22510995 FT |
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 | * | |
fb914ebf | 26 | * 2. Currently only RX available interrupt is used, no need for waiting TXE |
22510995 FT |
27 | * interrupt for a low speed UART device |
28 | */ | |
29 | ||
51808f05 AS |
30 | #ifdef CONFIG_MAGIC_SYSRQ |
31 | #define SUPPORT_SYSRQ | |
32 | #endif | |
33 | ||
22510995 FT |
34 | #include <linux/module.h> |
35 | #include <linux/ioport.h> | |
c044391b | 36 | #include <linux/irq.h> |
22510995 FT |
37 | #include <linux/init.h> |
38 | #include <linux/console.h> | |
22510995 FT |
39 | #include <linux/tty.h> |
40 | #include <linux/tty_flip.h> | |
41 | #include <linux/serial_core.h> | |
42 | #include <linux/serial_reg.h> | |
43 | ||
44 | #include <linux/kthread.h> | |
22510995 | 45 | #include <linux/spi/spi.h> |
22510995 FT |
46 | |
47 | #include "mrst_max3110.h" | |
48 | ||
49 | #define PR_FMT "mrst_max3110: " | |
50 | ||
d6e679b4 AV |
51 | #define UART_TX_NEEDED 1 |
52 | #define CON_TX_NEEDED 2 | |
53 | #define BIT_IRQ_PENDING 3 | |
54 | ||
22510995 FT |
55 | struct uart_max3110 { |
56 | struct uart_port port; | |
57 | struct spi_device *spi; | |
d8653d30 | 58 | char name[SPI_NAME_SIZE]; |
22510995 FT |
59 | |
60 | wait_queue_head_t wq; | |
61 | struct task_struct *main_thread; | |
62 | struct task_struct *read_thread; | |
6eab04a8 | 63 | struct mutex thread_mutex; |
22510995 FT |
64 | |
65 | u32 baud; | |
66 | u16 cur_conf; | |
67 | u8 clock; | |
68 | u8 parity, word_7bits; | |
ee9b4500 | 69 | u16 irq; |
22510995 | 70 | |
d6e679b4 | 71 | unsigned long uart_flags; |
22510995 FT |
72 | |
73 | /* console related */ | |
74 | struct circ_buf con_xmit; | |
22510995 FT |
75 | }; |
76 | ||
77 | /* global data structure, may need be removed */ | |
ee9b4500 FT |
78 | static struct uart_max3110 *pmax; |
79 | ||
51808f05 AS |
80 | static int receive_chars(struct uart_max3110 *max, |
81 | unsigned short *str, int len); | |
82 | static int max3110_read_multi(struct uart_max3110 *max); | |
ee9b4500 | 83 | static void max3110_con_receive(struct uart_max3110 *max); |
22510995 | 84 | |
ee9b4500 FT |
85 | static int max3110_write_then_read(struct uart_max3110 *max, |
86 | const void *txbuf, void *rxbuf, unsigned len, int always_fast) | |
22510995 FT |
87 | { |
88 | struct spi_device *spi = max->spi; | |
89 | struct spi_message message; | |
90 | struct spi_transfer x; | |
91 | int ret; | |
92 | ||
22510995 FT |
93 | spi_message_init(&message); |
94 | memset(&x, 0, sizeof x); | |
95 | x.len = len; | |
96 | x.tx_buf = txbuf; | |
97 | x.rx_buf = rxbuf; | |
98 | spi_message_add_tail(&x, &message); | |
99 | ||
100 | if (always_fast) | |
ee9b4500 | 101 | x.speed_hz = spi->max_speed_hz; |
22510995 FT |
102 | else if (max->baud) |
103 | x.speed_hz = max->baud; | |
104 | ||
105 | /* Do the i/o */ | |
106 | ret = spi_sync(spi, &message); | |
107 | return ret; | |
108 | } | |
109 | ||
ee9b4500 FT |
110 | /* Write a 16b word to the device */ |
111 | static int max3110_out(struct uart_max3110 *max, const u16 out) | |
22510995 | 112 | { |
ee9b4500 FT |
113 | void *buf; |
114 | u16 *obuf, *ibuf; | |
22510995 FT |
115 | int ret; |
116 | ||
ee9b4500 FT |
117 | buf = kzalloc(8, GFP_KERNEL | GFP_DMA); |
118 | if (!buf) | |
119 | return -ENOMEM; | |
120 | ||
121 | obuf = buf; | |
122 | ibuf = buf + 4; | |
123 | *obuf = out; | |
124 | ret = max3110_write_then_read(max, obuf, ibuf, 2, 1); | |
125 | if (ret) { | |
126 | pr_warning(PR_FMT "%s(): get err msg %d when sending 0x%x\n", | |
127 | __func__, ret, out); | |
128 | goto exit; | |
129 | } | |
22510995 | 130 | |
51808f05 | 131 | receive_chars(max, ibuf, 1); |
22510995 | 132 | |
ee9b4500 FT |
133 | exit: |
134 | kfree(buf); | |
22510995 FT |
135 | return ret; |
136 | } | |
137 | ||
22510995 FT |
138 | /* |
139 | * This is usually used to read data from SPIC RX FIFO, which doesn't | |
ee9b4500 FT |
140 | * need any delay like flushing character out. |
141 | * | |
142 | * Return how many valide bytes are read back | |
22510995 | 143 | */ |
51808f05 | 144 | static int max3110_read_multi(struct uart_max3110 *max) |
22510995 | 145 | { |
ee9b4500 FT |
146 | void *buf; |
147 | u16 *obuf, *ibuf; | |
51808f05 | 148 | int ret, blen; |
22510995 | 149 | |
ee9b4500 FT |
150 | blen = M3110_RX_FIFO_DEPTH * sizeof(u16); |
151 | buf = kzalloc(blen * 2, GFP_KERNEL | GFP_DMA); | |
152 | if (!buf) { | |
153 | pr_warning(PR_FMT "%s(): fail to alloc dma buffer\n", __func__); | |
22510995 FT |
154 | return 0; |
155 | } | |
156 | ||
ee9b4500 FT |
157 | /* tx/rx always have the same length */ |
158 | obuf = buf; | |
159 | ibuf = buf + blen; | |
22510995 | 160 | |
ee9b4500 FT |
161 | if (max3110_write_then_read(max, obuf, ibuf, blen, 1)) { |
162 | kfree(buf); | |
22510995 | 163 | return 0; |
ee9b4500 | 164 | } |
22510995 | 165 | |
51808f05 | 166 | ret = receive_chars(max, ibuf, M3110_RX_FIFO_DEPTH); |
22510995 | 167 | |
ee9b4500 | 168 | kfree(buf); |
51808f05 | 169 | return ret; |
22510995 FT |
170 | } |
171 | ||
172 | static void serial_m3110_con_putchar(struct uart_port *port, int ch) | |
173 | { | |
174 | struct uart_max3110 *max = | |
175 | container_of(port, struct uart_max3110, port); | |
176 | struct circ_buf *xmit = &max->con_xmit; | |
177 | ||
178 | if (uart_circ_chars_free(xmit)) { | |
179 | xmit->buf[xmit->head] = (char)ch; | |
180 | xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1); | |
181 | } | |
22510995 FT |
182 | } |
183 | ||
184 | /* | |
185 | * Print a string to the serial port trying not to disturb | |
186 | * any possible real use of the port... | |
187 | * | |
188 | * The console_lock must be held when we get here. | |
189 | */ | |
190 | static void serial_m3110_con_write(struct console *co, | |
191 | const char *s, unsigned int count) | |
192 | { | |
193 | if (!pmax) | |
194 | return; | |
195 | ||
196 | uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar); | |
ee9b4500 FT |
197 | |
198 | if (!test_and_set_bit(CON_TX_NEEDED, &pmax->uart_flags)) | |
7b18bd52 | 199 | wake_up(&pmax->wq); |
22510995 FT |
200 | } |
201 | ||
202 | static int __init | |
203 | serial_m3110_con_setup(struct console *co, char *options) | |
204 | { | |
205 | struct uart_max3110 *max = pmax; | |
206 | int baud = 115200; | |
207 | int bits = 8; | |
208 | int parity = 'n'; | |
209 | int flow = 'n'; | |
210 | ||
211 | pr_info(PR_FMT "setting up console\n"); | |
212 | ||
ee9b4500 FT |
213 | if (co->index == -1) |
214 | co->index = 0; | |
215 | ||
22510995 FT |
216 | if (!max) { |
217 | pr_err(PR_FMT "pmax is NULL, return"); | |
218 | return -ENODEV; | |
219 | } | |
220 | ||
221 | if (options) | |
222 | uart_parse_options(options, &baud, &parity, &bits, &flow); | |
223 | ||
224 | return uart_set_options(&max->port, co, baud, parity, bits, flow); | |
225 | } | |
226 | ||
227 | static struct tty_driver *serial_m3110_con_device(struct console *co, | |
228 | int *index) | |
229 | { | |
230 | struct uart_driver *p = co->data; | |
231 | *index = co->index; | |
232 | return p->tty_driver; | |
233 | } | |
234 | ||
235 | static struct uart_driver serial_m3110_reg; | |
236 | static struct console serial_m3110_console = { | |
237 | .name = "ttyS", | |
238 | .write = serial_m3110_con_write, | |
239 | .device = serial_m3110_con_device, | |
240 | .setup = serial_m3110_con_setup, | |
241 | .flags = CON_PRINTBUFFER, | |
242 | .index = -1, | |
243 | .data = &serial_m3110_reg, | |
244 | }; | |
245 | ||
22510995 FT |
246 | static unsigned int serial_m3110_tx_empty(struct uart_port *port) |
247 | { | |
248 | return 1; | |
249 | } | |
250 | ||
251 | static void serial_m3110_stop_tx(struct uart_port *port) | |
252 | { | |
253 | return; | |
254 | } | |
255 | ||
256 | /* stop_rx will be called in spin_lock env */ | |
257 | static void serial_m3110_stop_rx(struct uart_port *port) | |
258 | { | |
259 | return; | |
260 | } | |
261 | ||
262 | #define WORDS_PER_XFER 128 | |
ee9b4500 | 263 | static void send_circ_buf(struct uart_max3110 *max, |
22510995 FT |
264 | struct circ_buf *xmit) |
265 | { | |
ee9b4500 FT |
266 | void *buf; |
267 | u16 *obuf, *ibuf; | |
51808f05 | 268 | int i, len, blen, dma_size, left, ret = 0; |
ee9b4500 FT |
269 | |
270 | ||
271 | dma_size = WORDS_PER_XFER * sizeof(u16) * 2; | |
272 | buf = kzalloc(dma_size, GFP_KERNEL | GFP_DMA); | |
273 | if (!buf) | |
274 | return; | |
275 | obuf = buf; | |
276 | ibuf = buf + dma_size/2; | |
22510995 FT |
277 | |
278 | while (!uart_circ_empty(xmit)) { | |
279 | left = uart_circ_chars_pending(xmit); | |
280 | while (left) { | |
ee9b4500 FT |
281 | len = min(left, WORDS_PER_XFER); |
282 | blen = len * sizeof(u16); | |
283 | memset(ibuf, 0, blen); | |
22510995 | 284 | |
22510995 FT |
285 | for (i = 0; i < len; i++) { |
286 | obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG; | |
287 | xmit->tail = (xmit->tail + 1) & | |
288 | (UART_XMIT_SIZE - 1); | |
289 | } | |
ee9b4500 FT |
290 | |
291 | /* Fail to send msg to console is not very critical */ | |
7b18bd52 | 292 | |
ee9b4500 FT |
293 | ret = max3110_write_then_read(max, obuf, ibuf, blen, 0); |
294 | if (ret) | |
295 | pr_warning(PR_FMT "%s(): get err msg %d\n", | |
296 | __func__, ret); | |
22510995 | 297 | |
51808f05 | 298 | receive_chars(max, ibuf, len); |
22510995 FT |
299 | |
300 | max->port.icount.tx += len; | |
301 | left -= len; | |
302 | } | |
303 | } | |
ee9b4500 FT |
304 | |
305 | kfree(buf); | |
22510995 FT |
306 | } |
307 | ||
308 | static void transmit_char(struct uart_max3110 *max) | |
309 | { | |
310 | struct uart_port *port = &max->port; | |
311 | struct circ_buf *xmit = &port->state->xmit; | |
312 | ||
313 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) | |
314 | return; | |
315 | ||
316 | send_circ_buf(max, xmit); | |
317 | ||
318 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) | |
319 | uart_write_wakeup(port); | |
320 | ||
321 | if (uart_circ_empty(xmit)) | |
322 | serial_m3110_stop_tx(port); | |
323 | } | |
324 | ||
ee9b4500 FT |
325 | /* |
326 | * This will be called by uart_write() and tty_write, can't | |
327 | * go to sleep | |
328 | */ | |
22510995 FT |
329 | static void serial_m3110_start_tx(struct uart_port *port) |
330 | { | |
331 | struct uart_max3110 *max = | |
332 | container_of(port, struct uart_max3110, port); | |
333 | ||
d6e679b4 | 334 | if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags)) |
7b18bd52 | 335 | wake_up(&max->wq); |
22510995 FT |
336 | } |
337 | ||
51808f05 AS |
338 | static int |
339 | receive_chars(struct uart_max3110 *max, unsigned short *str, int len) | |
22510995 FT |
340 | { |
341 | struct uart_port *port = &max->port; | |
342 | struct tty_struct *tty; | |
51808f05 AS |
343 | char buf[M3110_RX_FIFO_DEPTH]; |
344 | int r, w, usable; | |
22510995 FT |
345 | |
346 | /* If uart is not opened, just return */ | |
347 | if (!port->state) | |
51808f05 | 348 | return 0; |
22510995 | 349 | |
da4e40e2 | 350 | tty = tty_port_tty_get(&port->state->port); |
22510995 | 351 | if (!tty) |
51808f05 | 352 | return 0; |
22510995 | 353 | |
51808f05 AS |
354 | for (r = 0, w = 0; r < len; r++) { |
355 | if (str[r] & MAX3110_BREAK && | |
356 | uart_handle_break(port)) | |
357 | continue; | |
358 | ||
359 | if (str[r] & MAX3110_READ_DATA_AVAILABLE) { | |
360 | if (uart_handle_sysrq_char(port, str[r] & 0xff)) | |
361 | continue; | |
362 | ||
363 | buf[w++] = str[r] & 0xff; | |
364 | } | |
365 | } | |
366 | ||
da4e40e2 AC |
367 | if (!w) { |
368 | tty_kref_put(tty); | |
51808f05 | 369 | return 0; |
da4e40e2 | 370 | } |
51808f05 AS |
371 | |
372 | for (r = 0; w; r += usable, w -= usable) { | |
373 | usable = tty_buffer_request_room(tty, w); | |
22510995 | 374 | if (usable) { |
51808f05 | 375 | tty_insert_flip_string(tty, buf + r, usable); |
22510995 | 376 | port->icount.rx += usable; |
22510995 | 377 | } |
22510995 | 378 | } |
ee9b4500 | 379 | tty_flip_buffer_push(tty); |
da4e40e2 | 380 | tty_kref_put(tty); |
51808f05 AS |
381 | |
382 | return r; | |
22510995 FT |
383 | } |
384 | ||
ee9b4500 FT |
385 | /* |
386 | * This routine will be used in read_thread or RX IRQ handling, | |
387 | * it will first do one round buffer read(8 words), if there is some | |
388 | * valid RX data, will try to read 5 more rounds till all data | |
389 | * is read out. | |
390 | * | |
391 | * Use stack space as data buffer to save some system load, and chose | |
392 | * 504 Btyes as a threadhold to do a bulk push to upper tty layer when | |
393 | * receiving bulk data, a much bigger buffer may cause stack overflow | |
394 | */ | |
395 | static void max3110_con_receive(struct uart_max3110 *max) | |
22510995 | 396 | { |
51808f05 | 397 | int loop = 1, num; |
22510995 | 398 | |
22510995 | 399 | do { |
51808f05 | 400 | num = max3110_read_multi(max); |
22510995 FT |
401 | |
402 | if (num) { | |
ee9b4500 | 403 | loop = 5; |
22510995 FT |
404 | } |
405 | } while (--loop); | |
22510995 FT |
406 | } |
407 | ||
408 | static int max3110_main_thread(void *_max) | |
409 | { | |
410 | struct uart_max3110 *max = _max; | |
411 | wait_queue_head_t *wq = &max->wq; | |
412 | int ret = 0; | |
413 | struct circ_buf *xmit = &max->con_xmit; | |
414 | ||
22510995 FT |
415 | pr_info(PR_FMT "start main thread\n"); |
416 | ||
417 | do { | |
7b18bd52 DB |
418 | wait_event_interruptible(*wq, |
419 | max->uart_flags || kthread_should_stop()); | |
68c16b41 AV |
420 | |
421 | mutex_lock(&max->thread_mutex); | |
22510995 | 422 | |
d6e679b4 | 423 | if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags)) |
ee9b4500 | 424 | max3110_con_receive(max); |
22510995 FT |
425 | |
426 | /* first handle console output */ | |
d6e679b4 | 427 | if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags)) |
22510995 | 428 | send_circ_buf(max, xmit); |
22510995 FT |
429 | |
430 | /* handle uart output */ | |
d6e679b4 | 431 | if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags)) |
22510995 | 432 | transmit_char(max); |
d6e679b4 | 433 | |
68c16b41 | 434 | mutex_unlock(&max->thread_mutex); |
d6e679b4 | 435 | |
22510995 FT |
436 | } while (!kthread_should_stop()); |
437 | ||
438 | return ret; | |
439 | } | |
440 | ||
22510995 FT |
441 | static irqreturn_t serial_m3110_irq(int irq, void *dev_id) |
442 | { | |
443 | struct uart_max3110 *max = dev_id; | |
444 | ||
445 | /* max3110's irq is a falling edge, not level triggered, | |
446 | * so no need to disable the irq */ | |
7b18bd52 | 447 | |
d6e679b4 | 448 | if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags)) |
7b18bd52 | 449 | wake_up(&max->wq); |
d6e679b4 | 450 | |
22510995 FT |
451 | return IRQ_HANDLED; |
452 | } | |
91efa75c | 453 | |
22510995 FT |
454 | /* if don't use RX IRQ, then need a thread to polling read */ |
455 | static int max3110_read_thread(void *_max) | |
456 | { | |
457 | struct uart_max3110 *max = _max; | |
458 | ||
459 | pr_info(PR_FMT "start read thread\n"); | |
460 | do { | |
ee9b4500 FT |
461 | /* |
462 | * If can't acquire the mutex, it means the main thread | |
463 | * is running which will also perform the rx job | |
464 | */ | |
465 | if (mutex_trylock(&max->thread_mutex)) { | |
466 | max3110_con_receive(max); | |
467 | mutex_unlock(&max->thread_mutex); | |
468 | } | |
22510995 FT |
469 | |
470 | set_current_state(TASK_INTERRUPTIBLE); | |
471 | schedule_timeout(HZ / 20); | |
472 | } while (!kthread_should_stop()); | |
473 | ||
474 | return 0; | |
475 | } | |
22510995 FT |
476 | |
477 | static int serial_m3110_startup(struct uart_port *port) | |
478 | { | |
479 | struct uart_max3110 *max = | |
480 | container_of(port, struct uart_max3110, port); | |
481 | u16 config = 0; | |
482 | int ret = 0; | |
483 | ||
ee9b4500 | 484 | if (port->line != 0) { |
22510995 | 485 | pr_err(PR_FMT "uart port startup failed\n"); |
ee9b4500 FT |
486 | return -1; |
487 | } | |
22510995 | 488 | |
ee9b4500 | 489 | /* Disable all IRQ and config it to 115200, 8n1 */ |
22510995 FT |
490 | config = WC_TAG | WC_FIFO_ENABLE |
491 | | WC_1_STOPBITS | |
492 | | WC_8BIT_WORD | |
493 | | WC_BAUD_DR2; | |
22510995 FT |
494 | |
495 | /* as we use thread to handle tx/rx, need set low latency */ | |
496 | port->state->port.tty->low_latency = 1; | |
497 | ||
91efa75c AC |
498 | if (max->irq) { |
499 | max->read_thread = NULL; | |
500 | ret = request_irq(max->irq, serial_m3110_irq, | |
22510995 | 501 | IRQ_TYPE_EDGE_FALLING, "max3110", max); |
91efa75c AC |
502 | if (ret) { |
503 | max->irq = 0; | |
504 | pr_err(PR_FMT "unable to allocate IRQ, polling\n"); | |
505 | } else { | |
506 | /* Enable RX IRQ only */ | |
507 | config |= WC_RXA_IRQ_ENABLE; | |
508 | } | |
509 | } | |
22510995 | 510 | |
91efa75c AC |
511 | if (max->irq == 0) { |
512 | /* If IRQ is disabled, start a read thread for input data */ | |
513 | max->read_thread = | |
514 | kthread_run(max3110_read_thread, max, "max3110_read"); | |
515 | if (IS_ERR(max->read_thread)) { | |
516 | ret = PTR_ERR(max->read_thread); | |
517 | max->read_thread = NULL; | |
518 | pr_err(PR_FMT "Can't create read thread!\n"); | |
519 | return ret; | |
520 | } | |
ee9b4500 | 521 | } |
ee9b4500 FT |
522 | |
523 | ret = max3110_out(max, config); | |
524 | if (ret) { | |
91efa75c AC |
525 | if (max->irq) |
526 | free_irq(max->irq, max); | |
527 | if (max->read_thread) | |
528 | kthread_stop(max->read_thread); | |
ee9b4500 | 529 | max->read_thread = NULL; |
ee9b4500 FT |
530 | return ret; |
531 | } | |
22510995 FT |
532 | |
533 | max->cur_conf = config; | |
534 | return 0; | |
535 | } | |
536 | ||
537 | static void serial_m3110_shutdown(struct uart_port *port) | |
538 | { | |
539 | struct uart_max3110 *max = | |
540 | container_of(port, struct uart_max3110, port); | |
541 | u16 config; | |
542 | ||
543 | if (max->read_thread) { | |
544 | kthread_stop(max->read_thread); | |
545 | max->read_thread = NULL; | |
546 | } | |
547 | ||
91efa75c AC |
548 | if (max->irq) |
549 | free_irq(max->irq, max); | |
22510995 FT |
550 | |
551 | /* Disable interrupts from this port */ | |
552 | config = WC_TAG | WC_SW_SHDI; | |
553 | max3110_out(max, config); | |
554 | } | |
555 | ||
556 | static void serial_m3110_release_port(struct uart_port *port) | |
557 | { | |
558 | } | |
559 | ||
560 | static int serial_m3110_request_port(struct uart_port *port) | |
561 | { | |
562 | return 0; | |
563 | } | |
564 | ||
565 | static void serial_m3110_config_port(struct uart_port *port, int flags) | |
566 | { | |
ee9b4500 | 567 | port->type = PORT_MAX3100; |
22510995 FT |
568 | } |
569 | ||
570 | static int | |
571 | serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser) | |
572 | { | |
573 | /* we don't want the core code to modify any port params */ | |
574 | return -EINVAL; | |
575 | } | |
576 | ||
577 | ||
578 | static const char *serial_m3110_type(struct uart_port *port) | |
579 | { | |
580 | struct uart_max3110 *max = | |
581 | container_of(port, struct uart_max3110, port); | |
582 | return max->name; | |
583 | } | |
584 | ||
585 | static void | |
586 | serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, | |
587 | struct ktermios *old) | |
588 | { | |
589 | struct uart_max3110 *max = | |
590 | container_of(port, struct uart_max3110, port); | |
591 | unsigned char cval; | |
592 | unsigned int baud, parity = 0; | |
593 | int clk_div = -1; | |
594 | u16 new_conf = max->cur_conf; | |
595 | ||
596 | switch (termios->c_cflag & CSIZE) { | |
597 | case CS7: | |
598 | cval = UART_LCR_WLEN7; | |
599 | new_conf |= WC_7BIT_WORD; | |
600 | break; | |
601 | default: | |
ee9b4500 FT |
602 | /* We only support CS7 & CS8 */ |
603 | termios->c_cflag &= ~CSIZE; | |
604 | termios->c_cflag |= CS8; | |
22510995 FT |
605 | case CS8: |
606 | cval = UART_LCR_WLEN8; | |
607 | new_conf |= WC_8BIT_WORD; | |
608 | break; | |
609 | } | |
610 | ||
611 | baud = uart_get_baud_rate(port, termios, old, 0, 230400); | |
612 | ||
ee9b4500 | 613 | /* First calc the div for 1.8MHZ clock case */ |
22510995 FT |
614 | switch (baud) { |
615 | case 300: | |
616 | clk_div = WC_BAUD_DR384; | |
617 | break; | |
618 | case 600: | |
619 | clk_div = WC_BAUD_DR192; | |
620 | break; | |
621 | case 1200: | |
622 | clk_div = WC_BAUD_DR96; | |
623 | break; | |
624 | case 2400: | |
625 | clk_div = WC_BAUD_DR48; | |
626 | break; | |
627 | case 4800: | |
628 | clk_div = WC_BAUD_DR24; | |
629 | break; | |
630 | case 9600: | |
631 | clk_div = WC_BAUD_DR12; | |
632 | break; | |
633 | case 19200: | |
634 | clk_div = WC_BAUD_DR6; | |
635 | break; | |
636 | case 38400: | |
637 | clk_div = WC_BAUD_DR3; | |
638 | break; | |
639 | case 57600: | |
640 | clk_div = WC_BAUD_DR2; | |
641 | break; | |
642 | case 115200: | |
643 | clk_div = WC_BAUD_DR1; | |
644 | break; | |
645 | case 230400: | |
646 | if (max->clock & MAX3110_HIGH_CLK) | |
647 | break; | |
648 | default: | |
ee9b4500 | 649 | /* Pick the previous baud rate */ |
22510995 FT |
650 | baud = max->baud; |
651 | clk_div = max->cur_conf & WC_BAUD_DIV_MASK; | |
652 | tty_termios_encode_baud_rate(termios, baud, baud); | |
653 | } | |
654 | ||
655 | if (max->clock & MAX3110_HIGH_CLK) { | |
656 | clk_div += 1; | |
ee9b4500 FT |
657 | /* High clk version max3110 doesn't support B300 */ |
658 | if (baud == 300) { | |
22510995 | 659 | baud = 600; |
ee9b4500 FT |
660 | clk_div = WC_BAUD_DR384; |
661 | } | |
22510995 FT |
662 | if (baud == 230400) |
663 | clk_div = WC_BAUD_DR1; | |
664 | tty_termios_encode_baud_rate(termios, baud, baud); | |
665 | } | |
666 | ||
667 | new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div; | |
ee9b4500 FT |
668 | |
669 | if (unlikely(termios->c_cflag & CMSPAR)) | |
670 | termios->c_cflag &= ~CMSPAR; | |
671 | ||
22510995 FT |
672 | if (termios->c_cflag & CSTOPB) |
673 | new_conf |= WC_2_STOPBITS; | |
674 | else | |
675 | new_conf &= ~WC_2_STOPBITS; | |
676 | ||
677 | if (termios->c_cflag & PARENB) { | |
678 | new_conf |= WC_PARITY_ENABLE; | |
679 | parity |= UART_LCR_PARITY; | |
680 | } else | |
681 | new_conf &= ~WC_PARITY_ENABLE; | |
682 | ||
683 | if (!(termios->c_cflag & PARODD)) | |
684 | parity |= UART_LCR_EPAR; | |
685 | max->parity = parity; | |
686 | ||
687 | uart_update_timeout(port, termios->c_cflag, baud); | |
688 | ||
689 | new_conf |= WC_TAG; | |
690 | if (new_conf != max->cur_conf) { | |
ee9b4500 FT |
691 | if (!max3110_out(max, new_conf)) { |
692 | max->cur_conf = new_conf; | |
693 | max->baud = baud; | |
694 | } | |
22510995 FT |
695 | } |
696 | } | |
697 | ||
ee9b4500 | 698 | /* Don't handle hw handshaking */ |
22510995 FT |
699 | static unsigned int serial_m3110_get_mctrl(struct uart_port *port) |
700 | { | |
701 | return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR; | |
702 | } | |
703 | ||
704 | static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl) | |
705 | { | |
706 | } | |
707 | ||
708 | static void serial_m3110_break_ctl(struct uart_port *port, int break_state) | |
709 | { | |
710 | } | |
711 | ||
712 | static void serial_m3110_pm(struct uart_port *port, unsigned int state, | |
713 | unsigned int oldstate) | |
714 | { | |
715 | } | |
716 | ||
717 | static void serial_m3110_enable_ms(struct uart_port *port) | |
718 | { | |
719 | } | |
720 | ||
721 | struct uart_ops serial_m3110_ops = { | |
722 | .tx_empty = serial_m3110_tx_empty, | |
723 | .set_mctrl = serial_m3110_set_mctrl, | |
724 | .get_mctrl = serial_m3110_get_mctrl, | |
725 | .stop_tx = serial_m3110_stop_tx, | |
726 | .start_tx = serial_m3110_start_tx, | |
727 | .stop_rx = serial_m3110_stop_rx, | |
728 | .enable_ms = serial_m3110_enable_ms, | |
729 | .break_ctl = serial_m3110_break_ctl, | |
730 | .startup = serial_m3110_startup, | |
731 | .shutdown = serial_m3110_shutdown, | |
ee9b4500 | 732 | .set_termios = serial_m3110_set_termios, |
22510995 FT |
733 | .pm = serial_m3110_pm, |
734 | .type = serial_m3110_type, | |
735 | .release_port = serial_m3110_release_port, | |
736 | .request_port = serial_m3110_request_port, | |
737 | .config_port = serial_m3110_config_port, | |
738 | .verify_port = serial_m3110_verify_port, | |
739 | }; | |
740 | ||
741 | static struct uart_driver serial_m3110_reg = { | |
742 | .owner = THIS_MODULE, | |
743 | .driver_name = "MRST serial", | |
744 | .dev_name = "ttyS", | |
745 | .major = TTY_MAJOR, | |
746 | .minor = 64, | |
747 | .nr = 1, | |
ee9b4500 | 748 | .cons = &serial_m3110_console, |
22510995 FT |
749 | }; |
750 | ||
ee9b4500 | 751 | #ifdef CONFIG_PM |
22510995 FT |
752 | static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state) |
753 | { | |
ee9b4500 FT |
754 | struct uart_max3110 *max = spi_get_drvdata(spi); |
755 | ||
756 | disable_irq(max->irq); | |
757 | uart_suspend_port(&serial_m3110_reg, &max->port); | |
758 | max3110_out(max, max->cur_conf | WC_SW_SHDI); | |
22510995 FT |
759 | return 0; |
760 | } | |
761 | ||
762 | static int serial_m3110_resume(struct spi_device *spi) | |
763 | { | |
ee9b4500 FT |
764 | struct uart_max3110 *max = spi_get_drvdata(spi); |
765 | ||
766 | max3110_out(max, max->cur_conf); | |
767 | uart_resume_port(&serial_m3110_reg, &max->port); | |
768 | enable_irq(max->irq); | |
22510995 FT |
769 | return 0; |
770 | } | |
ee9b4500 FT |
771 | #else |
772 | #define serial_m3110_suspend NULL | |
773 | #define serial_m3110_resume NULL | |
774 | #endif | |
22510995 | 775 | |
ee9b4500 | 776 | static int __devinit serial_m3110_probe(struct spi_device *spi) |
22510995 FT |
777 | { |
778 | struct uart_max3110 *max; | |
ee9b4500 | 779 | void *buffer; |
99dd3f6b | 780 | u16 res; |
ee9b4500 FT |
781 | int ret = 0; |
782 | ||
22510995 FT |
783 | max = kzalloc(sizeof(*max), GFP_KERNEL); |
784 | if (!max) | |
785 | return -ENOMEM; | |
786 | ||
ee9b4500 | 787 | /* Set spi info */ |
22510995 FT |
788 | spi->bits_per_word = 16; |
789 | max->clock = MAX3110_HIGH_CLK; | |
22510995 FT |
790 | |
791 | spi_setup(spi); | |
792 | ||
ee9b4500 FT |
793 | max->port.type = PORT_MAX3100; |
794 | max->port.fifosize = 2; /* Only have 16b buffer */ | |
22510995 FT |
795 | max->port.ops = &serial_m3110_ops; |
796 | max->port.line = 0; | |
797 | max->port.dev = &spi->dev; | |
798 | max->port.uartclk = 115200; | |
799 | ||
800 | max->spi = spi; | |
ee9b4500 | 801 | strcpy(max->name, spi->modalias); |
22510995 FT |
802 | max->irq = (u16)spi->irq; |
803 | ||
68c16b41 | 804 | mutex_init(&max->thread_mutex); |
22510995 FT |
805 | |
806 | max->word_7bits = 0; | |
807 | max->parity = 0; | |
808 | max->baud = 0; | |
809 | ||
810 | max->cur_conf = 0; | |
d6e679b4 AV |
811 | max->uart_flags = 0; |
812 | ||
99dd3f6b | 813 | /* Check if reading configuration register returns something sane */ |
814 | ||
815 | res = RC_TAG; | |
816 | ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0); | |
817 | if (ret < 0 || res == 0 || res == 0xffff) { | |
0bb04bf3 | 818 | dev_dbg(&spi->dev, "MAX3111 deemed not present (conf reg %04x)", |
99dd3f6b | 819 | res); |
820 | ret = -ENODEV; | |
821 | goto err_get_page; | |
822 | } | |
ee9b4500 FT |
823 | |
824 | buffer = (void *)__get_free_page(GFP_KERNEL); | |
22510995 FT |
825 | if (!buffer) { |
826 | ret = -ENOMEM; | |
827 | goto err_get_page; | |
828 | } | |
ee9b4500 FT |
829 | max->con_xmit.buf = buffer; |
830 | max->con_xmit.head = 0; | |
831 | max->con_xmit.tail = 0; | |
22510995 | 832 | |
33b1e693 MW |
833 | init_waitqueue_head(&max->wq); |
834 | ||
22510995 FT |
835 | max->main_thread = kthread_run(max3110_main_thread, |
836 | max, "max3110_main"); | |
837 | if (IS_ERR(max->main_thread)) { | |
838 | ret = PTR_ERR(max->main_thread); | |
839 | goto err_kthread; | |
840 | } | |
841 | ||
ee9b4500 | 842 | spi_set_drvdata(spi, max); |
22510995 | 843 | pmax = max; |
ee9b4500 FT |
844 | |
845 | /* Give membase a psudo value to pass serial_core's check */ | |
22510995 FT |
846 | max->port.membase = (void *)0xff110000; |
847 | uart_add_one_port(&serial_m3110_reg, &max->port); | |
848 | ||
849 | return 0; | |
850 | ||
851 | err_kthread: | |
852 | free_page((unsigned long)buffer); | |
853 | err_get_page: | |
22510995 FT |
854 | kfree(max); |
855 | return ret; | |
856 | } | |
857 | ||
ee9b4500 | 858 | static int __devexit serial_m3110_remove(struct spi_device *dev) |
22510995 | 859 | { |
ee9b4500 | 860 | struct uart_max3110 *max = spi_get_drvdata(dev); |
22510995 | 861 | |
ee9b4500 | 862 | if (!max) |
22510995 FT |
863 | return 0; |
864 | ||
22510995 FT |
865 | uart_remove_one_port(&serial_m3110_reg, &max->port); |
866 | ||
867 | free_page((unsigned long)max->con_xmit.buf); | |
868 | ||
869 | if (max->main_thread) | |
870 | kthread_stop(max->main_thread); | |
871 | ||
872 | kfree(max); | |
873 | return 0; | |
874 | } | |
875 | ||
876 | static struct spi_driver uart_max3110_driver = { | |
877 | .driver = { | |
878 | .name = "spi_max3111", | |
22510995 FT |
879 | .owner = THIS_MODULE, |
880 | }, | |
881 | .probe = serial_m3110_probe, | |
ee9b4500 | 882 | .remove = __devexit_p(serial_m3110_remove), |
22510995 FT |
883 | .suspend = serial_m3110_suspend, |
884 | .resume = serial_m3110_resume, | |
885 | }; | |
886 | ||
ee9b4500 | 887 | static int __init serial_m3110_init(void) |
22510995 FT |
888 | { |
889 | int ret = 0; | |
890 | ||
891 | ret = uart_register_driver(&serial_m3110_reg); | |
892 | if (ret) | |
893 | return ret; | |
894 | ||
895 | ret = spi_register_driver(&uart_max3110_driver); | |
896 | if (ret) | |
897 | uart_unregister_driver(&serial_m3110_reg); | |
898 | ||
899 | return ret; | |
900 | } | |
901 | ||
ee9b4500 | 902 | static void __exit serial_m3110_exit(void) |
22510995 FT |
903 | { |
904 | spi_unregister_driver(&uart_max3110_driver); | |
905 | uart_unregister_driver(&serial_m3110_reg); | |
906 | } | |
907 | ||
908 | module_init(serial_m3110_init); | |
909 | module_exit(serial_m3110_exit); | |
910 | ||
ee9b4500 | 911 | MODULE_LICENSE("GPL v2"); |
8c4074cd | 912 | MODULE_ALIAS("spi:max3110-uart"); |