diff options
Diffstat (limited to 'drivers/serial/mrst_max3110.c')
-rw-r--r-- | drivers/serial/mrst_max3110.c | 358 |
1 files changed, 216 insertions, 142 deletions
diff --git a/drivers/serial/mrst_max3110.c b/drivers/serial/mrst_max3110.c index 51c15f58e01e..b62857bf2fdb 100644 --- a/drivers/serial/mrst_max3110.c +++ b/drivers/serial/mrst_max3110.c | |||
@@ -1,7 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | * max3110.c - spi uart protocol driver for Maxim 3110 on Moorestown | 2 | * mrst_max3110.c - spi uart protocol driver for Maxim 3110 |
3 | * | 3 | * |
4 | * Copyright (C) Intel 2008 Feng Tang <feng.tang@intel.com> | 4 | * Copyright (c) 2008-2010, Intel Corporation. |
5 | * | 5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | 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, | 7 | * under the terms and conditions of the GNU General Public License, |
@@ -32,18 +32,13 @@ | |||
32 | #include <linux/irq.h> | 32 | #include <linux/irq.h> |
33 | #include <linux/init.h> | 33 | #include <linux/init.h> |
34 | #include <linux/console.h> | 34 | #include <linux/console.h> |
35 | #include <linux/sysrq.h> | ||
36 | #include <linux/platform_device.h> | ||
37 | #include <linux/tty.h> | 35 | #include <linux/tty.h> |
38 | #include <linux/tty_flip.h> | 36 | #include <linux/tty_flip.h> |
39 | #include <linux/serial_core.h> | 37 | #include <linux/serial_core.h> |
40 | #include <linux/serial_reg.h> | 38 | #include <linux/serial_reg.h> |
41 | 39 | ||
42 | #include <linux/kthread.h> | 40 | #include <linux/kthread.h> |
43 | #include <linux/delay.h> | ||
44 | #include <asm/atomic.h> | ||
45 | #include <linux/spi/spi.h> | 41 | #include <linux/spi/spi.h> |
46 | #include <linux/spi/dw_spi.h> | ||
47 | 42 | ||
48 | #include "mrst_max3110.h" | 43 | #include "mrst_max3110.h" |
49 | 44 | ||
@@ -56,7 +51,7 @@ | |||
56 | struct uart_max3110 { | 51 | struct uart_max3110 { |
57 | struct uart_port port; | 52 | struct uart_port port; |
58 | struct spi_device *spi; | 53 | struct spi_device *spi; |
59 | char *name; | 54 | char name[24]; |
60 | 55 | ||
61 | wait_queue_head_t wq; | 56 | wait_queue_head_t wq; |
62 | struct task_struct *main_thread; | 57 | struct task_struct *main_thread; |
@@ -67,35 +62,30 @@ struct uart_max3110 { | |||
67 | u16 cur_conf; | 62 | u16 cur_conf; |
68 | u8 clock; | 63 | u8 clock; |
69 | u8 parity, word_7bits; | 64 | u8 parity, word_7bits; |
65 | u16 irq; | ||
70 | 66 | ||
71 | unsigned long uart_flags; | 67 | unsigned long uart_flags; |
72 | 68 | ||
73 | /* console related */ | 69 | /* console related */ |
74 | struct circ_buf con_xmit; | 70 | struct circ_buf con_xmit; |
75 | |||
76 | /* irq related */ | ||
77 | u16 irq; | ||
78 | }; | 71 | }; |
79 | 72 | ||
80 | /* global data structure, may need be removed */ | 73 | /* global data structure, may need be removed */ |
81 | struct uart_max3110 *pmax; | 74 | static struct uart_max3110 *pmax; |
82 | static inline void receive_char(struct uart_max3110 *max, u8 ch); | 75 | |
83 | static void receive_chars(struct uart_max3110 *max, | 76 | static void receive_chars(struct uart_max3110 *max, |
84 | unsigned char *str, int len); | 77 | unsigned char *str, int len); |
85 | static int max3110_read_multi(struct uart_max3110 *max, int len, u8 *buf); | 78 | static int max3110_read_multi(struct uart_max3110 *max, u8 *buf); |
86 | static void max3110_console_receive(struct uart_max3110 *max); | 79 | static void max3110_con_receive(struct uart_max3110 *max); |
87 | 80 | ||
88 | int max3110_write_then_read(struct uart_max3110 *max, | 81 | static int max3110_write_then_read(struct uart_max3110 *max, |
89 | const u8 *txbuf, u8 *rxbuf, unsigned len, int always_fast) | 82 | const void *txbuf, void *rxbuf, unsigned len, int always_fast) |
90 | { | 83 | { |
91 | struct spi_device *spi = max->spi; | 84 | struct spi_device *spi = max->spi; |
92 | struct spi_message message; | 85 | struct spi_message message; |
93 | struct spi_transfer x; | 86 | struct spi_transfer x; |
94 | int ret; | 87 | int ret; |
95 | 88 | ||
96 | if (!txbuf || !rxbuf) | ||
97 | return -EINVAL; | ||
98 | |||
99 | spi_message_init(&message); | 89 | spi_message_init(&message); |
100 | memset(&x, 0, sizeof x); | 90 | memset(&x, 0, sizeof x); |
101 | x.len = len; | 91 | x.len = len; |
@@ -104,7 +94,7 @@ int max3110_write_then_read(struct uart_max3110 *max, | |||
104 | spi_message_add_tail(&x, &message); | 94 | spi_message_add_tail(&x, &message); |
105 | 95 | ||
106 | if (always_fast) | 96 | if (always_fast) |
107 | x.speed_hz = 3125000; | 97 | x.speed_hz = spi->max_speed_hz; |
108 | else if (max->baud) | 98 | else if (max->baud) |
109 | x.speed_hz = max->baud; | 99 | x.speed_hz = max->baud; |
110 | 100 | ||
@@ -113,58 +103,80 @@ int max3110_write_then_read(struct uart_max3110 *max, | |||
113 | return ret; | 103 | return ret; |
114 | } | 104 | } |
115 | 105 | ||
116 | /* Write a u16 to the device, and return one u16 read back */ | 106 | /* Write a 16b word to the device */ |
117 | int max3110_out(struct uart_max3110 *max, const u16 out) | 107 | static int max3110_out(struct uart_max3110 *max, const u16 out) |
118 | { | 108 | { |
119 | u16 tmp; | 109 | void *buf; |
110 | u16 *obuf, *ibuf; | ||
111 | u8 ch; | ||
120 | int ret; | 112 | int ret; |
121 | 113 | ||
122 | ret = max3110_write_then_read(max, (u8 *)&out, (u8 *)&tmp, 2, 1); | 114 | buf = kzalloc(8, GFP_KERNEL | GFP_DMA); |
123 | if (ret) | 115 | if (!buf) |
124 | return ret; | 116 | return -ENOMEM; |
117 | |||
118 | obuf = buf; | ||
119 | ibuf = buf + 4; | ||
120 | *obuf = out; | ||
121 | ret = max3110_write_then_read(max, obuf, ibuf, 2, 1); | ||
122 | if (ret) { | ||
123 | pr_warning(PR_FMT "%s(): get err msg %d when sending 0x%x\n", | ||
124 | __func__, ret, out); | ||
125 | goto exit; | ||
126 | } | ||
125 | 127 | ||
126 | /* If some valid data is read back */ | 128 | /* If some valid data is read back */ |
127 | if (tmp & MAX3110_READ_DATA_AVAILABLE) | 129 | if (*ibuf & MAX3110_READ_DATA_AVAILABLE) { |
128 | receive_char(max, (tmp & 0xff)); | 130 | ch = *ibuf & 0xff; |
131 | receive_chars(max, &ch, 1); | ||
132 | } | ||
129 | 133 | ||
134 | exit: | ||
135 | kfree(buf); | ||
130 | return ret; | 136 | return ret; |
131 | } | 137 | } |
132 | 138 | ||
133 | #define MAX_READ_LEN 20 | ||
134 | /* | 139 | /* |
135 | * This is usually used to read data from SPIC RX FIFO, which doesn't | 140 | * This is usually used to read data from SPIC RX FIFO, which doesn't |
136 | * need any delay like flushing character out. It returns how many | 141 | * need any delay like flushing character out. |
137 | * valide bytes are read back | 142 | * |
143 | * Return how many valide bytes are read back | ||
138 | */ | 144 | */ |
139 | static int max3110_read_multi(struct uart_max3110 *max, int len, u8 *buf) | 145 | static int max3110_read_multi(struct uart_max3110 *max, u8 *rxbuf) |
140 | { | 146 | { |
141 | u16 out[MAX_READ_LEN], in[MAX_READ_LEN]; | 147 | void *buf; |
142 | u8 *pbuf, valid_str[MAX_READ_LEN]; | 148 | u16 *obuf, *ibuf; |
143 | int i, j, bytelen; | 149 | u8 *pbuf, valid_str[M3110_RX_FIFO_DEPTH]; |
150 | int i, j, blen; | ||
144 | 151 | ||
145 | if (len > MAX_READ_LEN) { | 152 | blen = M3110_RX_FIFO_DEPTH * sizeof(u16); |
146 | pr_err(PR_FMT "read len %d is too large\n", len); | 153 | buf = kzalloc(blen * 2, GFP_KERNEL | GFP_DMA); |
154 | if (!buf) { | ||
155 | pr_warning(PR_FMT "%s(): fail to alloc dma buffer\n", __func__); | ||
147 | return 0; | 156 | return 0; |
148 | } | 157 | } |
149 | 158 | ||
150 | bytelen = len * 2; | 159 | /* tx/rx always have the same length */ |
151 | memset(out, 0, bytelen); | 160 | obuf = buf; |
152 | memset(in, 0, bytelen); | 161 | ibuf = buf + blen; |
153 | 162 | ||
154 | if (max3110_write_then_read(max, (u8 *)out, (u8 *)in, bytelen, 1)) | 163 | if (max3110_write_then_read(max, obuf, ibuf, blen, 1)) { |
164 | kfree(buf); | ||
155 | return 0; | 165 | return 0; |
166 | } | ||
156 | 167 | ||
157 | /* If caller don't provide a buffer, then handle received char */ | 168 | /* If caller doesn't provide a buffer, then handle received char */ |
158 | pbuf = buf ? buf : valid_str; | 169 | pbuf = rxbuf ? rxbuf : valid_str; |
159 | 170 | ||
160 | for (i = 0, j = 0; i < len; i++) { | 171 | for (i = 0, j = 0; i < M3110_RX_FIFO_DEPTH; i++) { |
161 | if (in[i] & MAX3110_READ_DATA_AVAILABLE) | 172 | if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE) |
162 | pbuf[j++] = (u8)(in[i] & 0xff); | 173 | pbuf[j++] = ibuf[i] & 0xff; |
163 | } | 174 | } |
164 | 175 | ||
165 | if (j && (pbuf == valid_str)) | 176 | if (j && (pbuf == valid_str)) |
166 | receive_chars(max, valid_str, j); | 177 | receive_chars(max, valid_str, j); |
167 | 178 | ||
179 | kfree(buf); | ||
168 | return j; | 180 | return j; |
169 | } | 181 | } |
170 | 182 | ||
@@ -178,10 +190,6 @@ static void serial_m3110_con_putchar(struct uart_port *port, int ch) | |||
178 | xmit->buf[xmit->head] = (char)ch; | 190 | xmit->buf[xmit->head] = (char)ch; |
179 | xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1); | 191 | xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1); |
180 | } | 192 | } |
181 | |||
182 | |||
183 | if (!test_and_set_bit(CON_TX_NEEDED, &max->uart_flags)) | ||
184 | wake_up_process(max->main_thread); | ||
185 | } | 193 | } |
186 | 194 | ||
187 | /* | 195 | /* |
@@ -197,6 +205,9 @@ static void serial_m3110_con_write(struct console *co, | |||
197 | return; | 205 | return; |
198 | 206 | ||
199 | uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar); | 207 | uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar); |
208 | |||
209 | if (!test_and_set_bit(CON_TX_NEEDED, &pmax->uart_flags)) | ||
210 | wake_up_process(pmax->main_thread); | ||
200 | } | 211 | } |
201 | 212 | ||
202 | static int __init | 213 | static int __init |
@@ -210,6 +221,9 @@ serial_m3110_con_setup(struct console *co, char *options) | |||
210 | 221 | ||
211 | pr_info(PR_FMT "setting up console\n"); | 222 | pr_info(PR_FMT "setting up console\n"); |
212 | 223 | ||
224 | if (co->index == -1) | ||
225 | co->index = 0; | ||
226 | |||
213 | if (!max) { | 227 | if (!max) { |
214 | pr_err(PR_FMT "pmax is NULL, return"); | 228 | pr_err(PR_FMT "pmax is NULL, return"); |
215 | return -ENODEV; | 229 | return -ENODEV; |
@@ -240,8 +254,6 @@ static struct console serial_m3110_console = { | |||
240 | .data = &serial_m3110_reg, | 254 | .data = &serial_m3110_reg, |
241 | }; | 255 | }; |
242 | 256 | ||
243 | #define MRST_CONSOLE (&serial_m3110_console) | ||
244 | |||
245 | static unsigned int serial_m3110_tx_empty(struct uart_port *port) | 257 | static unsigned int serial_m3110_tx_empty(struct uart_port *port) |
246 | { | 258 | { |
247 | return 1; | 259 | return 1; |
@@ -259,32 +271,44 @@ static void serial_m3110_stop_rx(struct uart_port *port) | |||
259 | } | 271 | } |
260 | 272 | ||
261 | #define WORDS_PER_XFER 128 | 273 | #define WORDS_PER_XFER 128 |
262 | static inline void send_circ_buf(struct uart_max3110 *max, | 274 | static void send_circ_buf(struct uart_max3110 *max, |
263 | struct circ_buf *xmit) | 275 | struct circ_buf *xmit) |
264 | { | 276 | { |
265 | int len, left = 0; | 277 | void *buf; |
266 | u16 obuf[WORDS_PER_XFER], ibuf[WORDS_PER_XFER]; | 278 | u16 *obuf, *ibuf; |
267 | u8 valid_str[WORDS_PER_XFER]; | 279 | u8 valid_str[WORDS_PER_XFER]; |
268 | int i, j; | 280 | int i, j, len, blen, dma_size, left, ret = 0; |
281 | |||
282 | |||
283 | dma_size = WORDS_PER_XFER * sizeof(u16) * 2; | ||
284 | buf = kzalloc(dma_size, GFP_KERNEL | GFP_DMA); | ||
285 | if (!buf) | ||
286 | return; | ||
287 | obuf = buf; | ||
288 | ibuf = buf + dma_size/2; | ||
269 | 289 | ||
270 | while (!uart_circ_empty(xmit)) { | 290 | while (!uart_circ_empty(xmit)) { |
271 | left = uart_circ_chars_pending(xmit); | 291 | left = uart_circ_chars_pending(xmit); |
272 | while (left) { | 292 | while (left) { |
273 | len = (left >= WORDS_PER_XFER) ? WORDS_PER_XFER : left; | 293 | len = min(left, WORDS_PER_XFER); |
294 | blen = len * sizeof(u16); | ||
295 | memset(ibuf, 0, blen); | ||
274 | 296 | ||
275 | memset(obuf, 0, len * 2); | ||
276 | memset(ibuf, 0, len * 2); | ||
277 | for (i = 0; i < len; i++) { | 297 | for (i = 0; i < len; i++) { |
278 | obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG; | 298 | obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG; |
279 | xmit->tail = (xmit->tail + 1) & | 299 | xmit->tail = (xmit->tail + 1) & |
280 | (UART_XMIT_SIZE - 1); | 300 | (UART_XMIT_SIZE - 1); |
281 | } | 301 | } |
282 | max3110_write_then_read(max, (u8 *)obuf, | 302 | |
283 | (u8 *)ibuf, len * 2, 0); | 303 | /* Fail to send msg to console is not very critical */ |
304 | ret = max3110_write_then_read(max, obuf, ibuf, blen, 0); | ||
305 | if (ret) | ||
306 | pr_warning(PR_FMT "%s(): get err msg %d\n", | ||
307 | __func__, ret); | ||
284 | 308 | ||
285 | for (i = 0, j = 0; i < len; i++) { | 309 | for (i = 0, j = 0; i < len; i++) { |
286 | if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE) | 310 | if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE) |
287 | valid_str[j++] = (u8)(ibuf[i] & 0xff); | 311 | valid_str[j++] = ibuf[i] & 0xff; |
288 | } | 312 | } |
289 | 313 | ||
290 | if (j) | 314 | if (j) |
@@ -294,6 +318,8 @@ static inline void send_circ_buf(struct uart_max3110 *max, | |||
294 | left -= len; | 318 | left -= len; |
295 | } | 319 | } |
296 | } | 320 | } |
321 | |||
322 | kfree(buf); | ||
297 | } | 323 | } |
298 | 324 | ||
299 | static void transmit_char(struct uart_max3110 *max) | 325 | static void transmit_char(struct uart_max3110 *max) |
@@ -313,8 +339,10 @@ static void transmit_char(struct uart_max3110 *max) | |||
313 | serial_m3110_stop_tx(port); | 339 | serial_m3110_stop_tx(port); |
314 | } | 340 | } |
315 | 341 | ||
316 | /* This will be called by uart_write() and tty_write, can't | 342 | /* |
317 | * go to sleep */ | 343 | * This will be called by uart_write() and tty_write, can't |
344 | * go to sleep | ||
345 | */ | ||
318 | static void serial_m3110_start_tx(struct uart_port *port) | 346 | static void serial_m3110_start_tx(struct uart_port *port) |
319 | { | 347 | { |
320 | struct uart_max3110 *max = | 348 | struct uart_max3110 *max = |
@@ -336,7 +364,7 @@ static void receive_chars(struct uart_max3110 *max, unsigned char *str, int len) | |||
336 | 364 | ||
337 | tty = port->state->port.tty; | 365 | tty = port->state->port.tty; |
338 | if (!tty) | 366 | if (!tty) |
339 | return; /* receive some char before the tty is opened */ | 367 | return; |
340 | 368 | ||
341 | while (len) { | 369 | while (len) { |
342 | usable = tty_buffer_request_room(tty, len); | 370 | usable = tty_buffer_request_room(tty, len); |
@@ -344,32 +372,37 @@ static void receive_chars(struct uart_max3110 *max, unsigned char *str, int len) | |||
344 | tty_insert_flip_string(tty, str, usable); | 372 | tty_insert_flip_string(tty, str, usable); |
345 | str += usable; | 373 | str += usable; |
346 | port->icount.rx += usable; | 374 | port->icount.rx += usable; |
347 | tty_flip_buffer_push(tty); | ||
348 | } | 375 | } |
349 | len -= usable; | 376 | len -= usable; |
350 | } | 377 | } |
378 | tty_flip_buffer_push(tty); | ||
351 | } | 379 | } |
352 | 380 | ||
353 | static inline void receive_char(struct uart_max3110 *max, u8 ch) | 381 | /* |
354 | { | 382 | * This routine will be used in read_thread or RX IRQ handling, |
355 | receive_chars(max, &ch, 1); | 383 | * it will first do one round buffer read(8 words), if there is some |
356 | } | 384 | * valid RX data, will try to read 5 more rounds till all data |
357 | 385 | * is read out. | |
358 | static void max3110_console_receive(struct uart_max3110 *max) | 386 | * |
387 | * Use stack space as data buffer to save some system load, and chose | ||
388 | * 504 Btyes as a threadhold to do a bulk push to upper tty layer when | ||
389 | * receiving bulk data, a much bigger buffer may cause stack overflow | ||
390 | */ | ||
391 | static void max3110_con_receive(struct uart_max3110 *max) | ||
359 | { | 392 | { |
360 | int loop = 1, num, total = 0; | 393 | int loop = 1, num, total = 0; |
361 | u8 recv_buf[512], *pbuf; | 394 | u8 recv_buf[512], *pbuf; |
362 | 395 | ||
363 | pbuf = recv_buf; | 396 | pbuf = recv_buf; |
364 | do { | 397 | do { |
365 | num = max3110_read_multi(max, 8, pbuf); | 398 | num = max3110_read_multi(max, pbuf); |
366 | 399 | ||
367 | if (num) { | 400 | if (num) { |
368 | loop = 10; | 401 | loop = 5; |
369 | pbuf += num; | 402 | pbuf += num; |
370 | total += num; | 403 | total += num; |
371 | 404 | ||
372 | if (total >= 500) { | 405 | if (total >= 504) { |
373 | receive_chars(max, recv_buf, total); | 406 | receive_chars(max, recv_buf, total); |
374 | pbuf = recv_buf; | 407 | pbuf = recv_buf; |
375 | total = 0; | 408 | total = 0; |
@@ -397,7 +430,7 @@ static int max3110_main_thread(void *_max) | |||
397 | mutex_lock(&max->thread_mutex); | 430 | mutex_lock(&max->thread_mutex); |
398 | 431 | ||
399 | if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags)) | 432 | if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags)) |
400 | max3110_console_receive(max); | 433 | max3110_con_receive(max); |
401 | 434 | ||
402 | /* first handle console output */ | 435 | /* first handle console output */ |
403 | if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags)) | 436 | if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags)) |
@@ -414,7 +447,6 @@ static int max3110_main_thread(void *_max) | |||
414 | return ret; | 447 | return ret; |
415 | } | 448 | } |
416 | 449 | ||
417 | #ifdef CONFIG_MRST_MAX3110_IRQ | ||
418 | static irqreturn_t serial_m3110_irq(int irq, void *dev_id) | 450 | static irqreturn_t serial_m3110_irq(int irq, void *dev_id) |
419 | { | 451 | { |
420 | struct uart_max3110 *max = dev_id; | 452 | struct uart_max3110 *max = dev_id; |
@@ -426,7 +458,7 @@ static irqreturn_t serial_m3110_irq(int irq, void *dev_id) | |||
426 | 458 | ||
427 | return IRQ_HANDLED; | 459 | return IRQ_HANDLED; |
428 | } | 460 | } |
429 | #else | 461 | |
430 | /* if don't use RX IRQ, then need a thread to polling read */ | 462 | /* if don't use RX IRQ, then need a thread to polling read */ |
431 | static int max3110_read_thread(void *_max) | 463 | static int max3110_read_thread(void *_max) |
432 | { | 464 | { |
@@ -434,9 +466,14 @@ static int max3110_read_thread(void *_max) | |||
434 | 466 | ||
435 | pr_info(PR_FMT "start read thread\n"); | 467 | pr_info(PR_FMT "start read thread\n"); |
436 | do { | 468 | do { |
437 | mutex_lock(&max->thread_mutex); | 469 | /* |
438 | max3110_console_receive(max); | 470 | * If can't acquire the mutex, it means the main thread |
439 | mutex_unlock(&max->thread_mutex); | 471 | * is running which will also perform the rx job |
472 | */ | ||
473 | if (mutex_trylock(&max->thread_mutex)) { | ||
474 | max3110_con_receive(max); | ||
475 | mutex_unlock(&max->thread_mutex); | ||
476 | } | ||
440 | 477 | ||
441 | set_current_state(TASK_INTERRUPTIBLE); | 478 | set_current_state(TASK_INTERRUPTIBLE); |
442 | schedule_timeout(HZ / 20); | 479 | schedule_timeout(HZ / 20); |
@@ -444,7 +481,6 @@ static int max3110_read_thread(void *_max) | |||
444 | 481 | ||
445 | return 0; | 482 | return 0; |
446 | } | 483 | } |
447 | #endif | ||
448 | 484 | ||
449 | static int serial_m3110_startup(struct uart_port *port) | 485 | static int serial_m3110_startup(struct uart_port *port) |
450 | { | 486 | { |
@@ -453,33 +489,54 @@ static int serial_m3110_startup(struct uart_port *port) | |||
453 | u16 config = 0; | 489 | u16 config = 0; |
454 | int ret = 0; | 490 | int ret = 0; |
455 | 491 | ||
456 | if (port->line != 0) | 492 | if (port->line != 0) { |
457 | pr_err(PR_FMT "uart port startup failed\n"); | 493 | pr_err(PR_FMT "uart port startup failed\n"); |
494 | return -1; | ||
495 | } | ||
458 | 496 | ||
459 | /* firstly disable all IRQ and config it to 115200, 8n1 */ | 497 | /* Disable all IRQ and config it to 115200, 8n1 */ |
460 | config = WC_TAG | WC_FIFO_ENABLE | 498 | config = WC_TAG | WC_FIFO_ENABLE |
461 | | WC_1_STOPBITS | 499 | | WC_1_STOPBITS |
462 | | WC_8BIT_WORD | 500 | | WC_8BIT_WORD |
463 | | WC_BAUD_DR2; | 501 | | WC_BAUD_DR2; |
464 | ret = max3110_out(max, config); | ||
465 | 502 | ||
466 | /* as we use thread to handle tx/rx, need set low latency */ | 503 | /* as we use thread to handle tx/rx, need set low latency */ |
467 | port->state->port.tty->low_latency = 1; | 504 | port->state->port.tty->low_latency = 1; |
468 | 505 | ||
469 | #ifdef CONFIG_MRST_MAX3110_IRQ | 506 | if (max->irq) { |
470 | ret = request_irq(max->irq, serial_m3110_irq, | 507 | max->read_thread = NULL; |
508 | ret = request_irq(max->irq, serial_m3110_irq, | ||
471 | IRQ_TYPE_EDGE_FALLING, "max3110", max); | 509 | IRQ_TYPE_EDGE_FALLING, "max3110", max); |
472 | if (ret) | 510 | if (ret) { |
473 | return ret; | 511 | max->irq = 0; |
512 | pr_err(PR_FMT "unable to allocate IRQ, polling\n"); | ||
513 | } else { | ||
514 | /* Enable RX IRQ only */ | ||
515 | config |= WC_RXA_IRQ_ENABLE; | ||
516 | } | ||
517 | } | ||
474 | 518 | ||
475 | /* enable RX IRQ only */ | 519 | if (max->irq == 0) { |
476 | config |= WC_RXA_IRQ_ENABLE; | 520 | /* If IRQ is disabled, start a read thread for input data */ |
477 | max3110_out(max, config); | 521 | max->read_thread = |
478 | #else | 522 | kthread_run(max3110_read_thread, max, "max3110_read"); |
479 | /* if IRQ is disabled, start a read thread for input data */ | 523 | if (IS_ERR(max->read_thread)) { |
480 | max->read_thread = | 524 | ret = PTR_ERR(max->read_thread); |
481 | kthread_run(max3110_read_thread, max, "max3110_read"); | 525 | max->read_thread = NULL; |
482 | #endif | 526 | pr_err(PR_FMT "Can't create read thread!\n"); |
527 | return ret; | ||
528 | } | ||
529 | } | ||
530 | |||
531 | ret = max3110_out(max, config); | ||
532 | if (ret) { | ||
533 | if (max->irq) | ||
534 | free_irq(max->irq, max); | ||
535 | if (max->read_thread) | ||
536 | kthread_stop(max->read_thread); | ||
537 | max->read_thread = NULL; | ||
538 | return ret; | ||
539 | } | ||
483 | 540 | ||
484 | max->cur_conf = config; | 541 | max->cur_conf = config; |
485 | return 0; | 542 | return 0; |
@@ -496,9 +553,8 @@ static void serial_m3110_shutdown(struct uart_port *port) | |||
496 | max->read_thread = NULL; | 553 | max->read_thread = NULL; |
497 | } | 554 | } |
498 | 555 | ||
499 | #ifdef CONFIG_MRST_MAX3110_IRQ | 556 | if (max->irq) |
500 | free_irq(max->irq, max); | 557 | free_irq(max->irq, max); |
501 | #endif | ||
502 | 558 | ||
503 | /* Disable interrupts from this port */ | 559 | /* Disable interrupts from this port */ |
504 | config = WC_TAG | WC_SW_SHDI; | 560 | config = WC_TAG | WC_SW_SHDI; |
@@ -516,8 +572,7 @@ static int serial_m3110_request_port(struct uart_port *port) | |||
516 | 572 | ||
517 | static void serial_m3110_config_port(struct uart_port *port, int flags) | 573 | static void serial_m3110_config_port(struct uart_port *port, int flags) |
518 | { | 574 | { |
519 | /* give it fake type */ | 575 | port->type = PORT_MAX3100; |
520 | port->type = PORT_PXA; | ||
521 | } | 576 | } |
522 | 577 | ||
523 | static int | 578 | static int |
@@ -552,6 +607,9 @@ serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, | |||
552 | new_conf |= WC_7BIT_WORD; | 607 | new_conf |= WC_7BIT_WORD; |
553 | break; | 608 | break; |
554 | default: | 609 | default: |
610 | /* We only support CS7 & CS8 */ | ||
611 | termios->c_cflag &= ~CSIZE; | ||
612 | termios->c_cflag |= CS8; | ||
555 | case CS8: | 613 | case CS8: |
556 | cval = UART_LCR_WLEN8; | 614 | cval = UART_LCR_WLEN8; |
557 | new_conf |= WC_8BIT_WORD; | 615 | new_conf |= WC_8BIT_WORD; |
@@ -560,7 +618,7 @@ serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, | |||
560 | 618 | ||
561 | baud = uart_get_baud_rate(port, termios, old, 0, 230400); | 619 | baud = uart_get_baud_rate(port, termios, old, 0, 230400); |
562 | 620 | ||
563 | /* first calc the div for 1.8MHZ clock case */ | 621 | /* First calc the div for 1.8MHZ clock case */ |
564 | switch (baud) { | 622 | switch (baud) { |
565 | case 300: | 623 | case 300: |
566 | clk_div = WC_BAUD_DR384; | 624 | clk_div = WC_BAUD_DR384; |
@@ -596,7 +654,7 @@ serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, | |||
596 | if (max->clock & MAX3110_HIGH_CLK) | 654 | if (max->clock & MAX3110_HIGH_CLK) |
597 | break; | 655 | break; |
598 | default: | 656 | default: |
599 | /* pick the previous baud rate */ | 657 | /* Pick the previous baud rate */ |
600 | baud = max->baud; | 658 | baud = max->baud; |
601 | clk_div = max->cur_conf & WC_BAUD_DIV_MASK; | 659 | clk_div = max->cur_conf & WC_BAUD_DIV_MASK; |
602 | tty_termios_encode_baud_rate(termios, baud, baud); | 660 | tty_termios_encode_baud_rate(termios, baud, baud); |
@@ -604,15 +662,21 @@ serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, | |||
604 | 662 | ||
605 | if (max->clock & MAX3110_HIGH_CLK) { | 663 | if (max->clock & MAX3110_HIGH_CLK) { |
606 | clk_div += 1; | 664 | clk_div += 1; |
607 | /* high clk version max3110 doesn't support B300 */ | 665 | /* High clk version max3110 doesn't support B300 */ |
608 | if (baud == 300) | 666 | if (baud == 300) { |
609 | baud = 600; | 667 | baud = 600; |
668 | clk_div = WC_BAUD_DR384; | ||
669 | } | ||
610 | if (baud == 230400) | 670 | if (baud == 230400) |
611 | clk_div = WC_BAUD_DR1; | 671 | clk_div = WC_BAUD_DR1; |
612 | tty_termios_encode_baud_rate(termios, baud, baud); | 672 | tty_termios_encode_baud_rate(termios, baud, baud); |
613 | } | 673 | } |
614 | 674 | ||
615 | new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div; | 675 | new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div; |
676 | |||
677 | if (unlikely(termios->c_cflag & CMSPAR)) | ||
678 | termios->c_cflag &= ~CMSPAR; | ||
679 | |||
616 | if (termios->c_cflag & CSTOPB) | 680 | if (termios->c_cflag & CSTOPB) |
617 | new_conf |= WC_2_STOPBITS; | 681 | new_conf |= WC_2_STOPBITS; |
618 | else | 682 | else |
@@ -632,13 +696,14 @@ serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, | |||
632 | 696 | ||
633 | new_conf |= WC_TAG; | 697 | new_conf |= WC_TAG; |
634 | if (new_conf != max->cur_conf) { | 698 | if (new_conf != max->cur_conf) { |
635 | max3110_out(max, new_conf); | 699 | if (!max3110_out(max, new_conf)) { |
636 | max->cur_conf = new_conf; | 700 | max->cur_conf = new_conf; |
637 | max->baud = baud; | 701 | max->baud = baud; |
702 | } | ||
638 | } | 703 | } |
639 | } | 704 | } |
640 | 705 | ||
641 | /* don't handle hw handshaking */ | 706 | /* Don't handle hw handshaking */ |
642 | static unsigned int serial_m3110_get_mctrl(struct uart_port *port) | 707 | static unsigned int serial_m3110_get_mctrl(struct uart_port *port) |
643 | { | 708 | { |
644 | return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR; | 709 | return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR; |
@@ -672,7 +737,7 @@ struct uart_ops serial_m3110_ops = { | |||
672 | .break_ctl = serial_m3110_break_ctl, | 737 | .break_ctl = serial_m3110_break_ctl, |
673 | .startup = serial_m3110_startup, | 738 | .startup = serial_m3110_startup, |
674 | .shutdown = serial_m3110_shutdown, | 739 | .shutdown = serial_m3110_shutdown, |
675 | .set_termios = serial_m3110_set_termios, /* must have */ | 740 | .set_termios = serial_m3110_set_termios, |
676 | .pm = serial_m3110_pm, | 741 | .pm = serial_m3110_pm, |
677 | .type = serial_m3110_type, | 742 | .type = serial_m3110_type, |
678 | .release_port = serial_m3110_release_port, | 743 | .release_port = serial_m3110_release_port, |
@@ -688,52 +753,60 @@ static struct uart_driver serial_m3110_reg = { | |||
688 | .major = TTY_MAJOR, | 753 | .major = TTY_MAJOR, |
689 | .minor = 64, | 754 | .minor = 64, |
690 | .nr = 1, | 755 | .nr = 1, |
691 | .cons = MRST_CONSOLE, | 756 | .cons = &serial_m3110_console, |
692 | }; | 757 | }; |
693 | 758 | ||
759 | #ifdef CONFIG_PM | ||
694 | static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state) | 760 | static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state) |
695 | { | 761 | { |
762 | struct uart_max3110 *max = spi_get_drvdata(spi); | ||
763 | |||
764 | disable_irq(max->irq); | ||
765 | uart_suspend_port(&serial_m3110_reg, &max->port); | ||
766 | max3110_out(max, max->cur_conf | WC_SW_SHDI); | ||
696 | return 0; | 767 | return 0; |
697 | } | 768 | } |
698 | 769 | ||
699 | static int serial_m3110_resume(struct spi_device *spi) | 770 | static int serial_m3110_resume(struct spi_device *spi) |
700 | { | 771 | { |
772 | struct uart_max3110 *max = spi_get_drvdata(spi); | ||
773 | |||
774 | max3110_out(max, max->cur_conf); | ||
775 | uart_resume_port(&serial_m3110_reg, &max->port); | ||
776 | enable_irq(max->irq); | ||
701 | return 0; | 777 | return 0; |
702 | } | 778 | } |
779 | #else | ||
780 | #define serial_m3110_suspend NULL | ||
781 | #define serial_m3110_resume NULL | ||
782 | #endif | ||
703 | 783 | ||
704 | static struct dw_spi_chip spi0_uart = { | 784 | static int __devinit serial_m3110_probe(struct spi_device *spi) |
705 | .poll_mode = 1, | ||
706 | .enable_dma = 0, | ||
707 | .type = SPI_FRF_SPI, | ||
708 | }; | ||
709 | |||
710 | static int serial_m3110_probe(struct spi_device *spi) | ||
711 | { | 785 | { |
712 | struct uart_max3110 *max; | 786 | struct uart_max3110 *max; |
713 | int ret; | 787 | void *buffer; |
714 | unsigned char *buffer; | ||
715 | u16 res; | 788 | u16 res; |
789 | int ret = 0; | ||
790 | |||
716 | max = kzalloc(sizeof(*max), GFP_KERNEL); | 791 | max = kzalloc(sizeof(*max), GFP_KERNEL); |
717 | if (!max) | 792 | if (!max) |
718 | return -ENOMEM; | 793 | return -ENOMEM; |
719 | 794 | ||
720 | /* set spi info */ | 795 | /* Set spi info */ |
721 | spi->mode = SPI_MODE_0; | ||
722 | spi->bits_per_word = 16; | 796 | spi->bits_per_word = 16; |
723 | max->clock = MAX3110_HIGH_CLK; | 797 | max->clock = MAX3110_HIGH_CLK; |
724 | spi->controller_data = &spi0_uart; | ||
725 | 798 | ||
726 | spi_setup(spi); | 799 | spi_setup(spi); |
727 | 800 | ||
728 | max->port.type = PORT_PXA; /* need apply for a max3110 type */ | 801 | max->port.type = PORT_MAX3100; |
729 | max->port.fifosize = 2; /* only have 16b buffer */ | 802 | max->port.fifosize = 2; /* Only have 16b buffer */ |
730 | max->port.ops = &serial_m3110_ops; | 803 | max->port.ops = &serial_m3110_ops; |
731 | max->port.line = 0; | 804 | max->port.line = 0; |
732 | max->port.dev = &spi->dev; | 805 | max->port.dev = &spi->dev; |
733 | max->port.uartclk = 115200; | 806 | max->port.uartclk = 115200; |
734 | 807 | ||
735 | max->spi = spi; | 808 | max->spi = spi; |
736 | max->name = spi->modalias; /* use spi name as the name */ | 809 | strcpy(max->name, spi->modalias); |
737 | max->irq = (u16)spi->irq; | 810 | max->irq = (u16)spi->irq; |
738 | 811 | ||
739 | mutex_init(&max->thread_mutex); | 812 | mutex_init(&max->thread_mutex); |
@@ -755,13 +828,15 @@ static int serial_m3110_probe(struct spi_device *spi) | |||
755 | ret = -ENODEV; | 828 | ret = -ENODEV; |
756 | goto err_get_page; | 829 | goto err_get_page; |
757 | } | 830 | } |
758 | buffer = (unsigned char *)__get_free_page(GFP_KERNEL); | 831 | |
832 | buffer = (void *)__get_free_page(GFP_KERNEL); | ||
759 | if (!buffer) { | 833 | if (!buffer) { |
760 | ret = -ENOMEM; | 834 | ret = -ENOMEM; |
761 | goto err_get_page; | 835 | goto err_get_page; |
762 | } | 836 | } |
763 | max->con_xmit.buf = (unsigned char *)buffer; | 837 | max->con_xmit.buf = buffer; |
764 | max->con_xmit.head = max->con_xmit.tail = 0; | 838 | max->con_xmit.head = 0; |
839 | max->con_xmit.tail = 0; | ||
765 | 840 | ||
766 | max->main_thread = kthread_run(max3110_main_thread, | 841 | max->main_thread = kthread_run(max3110_main_thread, |
767 | max, "max3110_main"); | 842 | max, "max3110_main"); |
@@ -770,8 +845,10 @@ static int serial_m3110_probe(struct spi_device *spi) | |||
770 | goto err_kthread; | 845 | goto err_kthread; |
771 | } | 846 | } |
772 | 847 | ||
848 | spi_set_drvdata(spi, max); | ||
773 | pmax = max; | 849 | pmax = max; |
774 | /* give membase a psudo value to pass serial_core's check */ | 850 | |
851 | /* Give membase a psudo value to pass serial_core's check */ | ||
775 | max->port.membase = (void *)0xff110000; | 852 | max->port.membase = (void *)0xff110000; |
776 | uart_add_one_port(&serial_m3110_reg, &max->port); | 853 | uart_add_one_port(&serial_m3110_reg, &max->port); |
777 | 854 | ||
@@ -780,19 +857,17 @@ static int serial_m3110_probe(struct spi_device *spi) | |||
780 | err_kthread: | 857 | err_kthread: |
781 | free_page((unsigned long)buffer); | 858 | free_page((unsigned long)buffer); |
782 | err_get_page: | 859 | err_get_page: |
783 | pmax = NULL; | ||
784 | kfree(max); | 860 | kfree(max); |
785 | return ret; | 861 | return ret; |
786 | } | 862 | } |
787 | 863 | ||
788 | static int max3110_remove(struct spi_device *dev) | 864 | static int __devexit serial_m3110_remove(struct spi_device *dev) |
789 | { | 865 | { |
790 | struct uart_max3110 *max = pmax; | 866 | struct uart_max3110 *max = spi_get_drvdata(dev); |
791 | 867 | ||
792 | if (!pmax) | 868 | if (!max) |
793 | return 0; | 869 | return 0; |
794 | 870 | ||
795 | pmax = NULL; | ||
796 | uart_remove_one_port(&serial_m3110_reg, &max->port); | 871 | uart_remove_one_port(&serial_m3110_reg, &max->port); |
797 | 872 | ||
798 | free_page((unsigned long)max->con_xmit.buf); | 873 | free_page((unsigned long)max->con_xmit.buf); |
@@ -811,13 +886,12 @@ static struct spi_driver uart_max3110_driver = { | |||
811 | .owner = THIS_MODULE, | 886 | .owner = THIS_MODULE, |
812 | }, | 887 | }, |
813 | .probe = serial_m3110_probe, | 888 | .probe = serial_m3110_probe, |
814 | .remove = __devexit_p(max3110_remove), | 889 | .remove = __devexit_p(serial_m3110_remove), |
815 | .suspend = serial_m3110_suspend, | 890 | .suspend = serial_m3110_suspend, |
816 | .resume = serial_m3110_resume, | 891 | .resume = serial_m3110_resume, |
817 | }; | 892 | }; |
818 | 893 | ||
819 | 894 | static int __init serial_m3110_init(void) | |
820 | int __init serial_m3110_init(void) | ||
821 | { | 895 | { |
822 | int ret = 0; | 896 | int ret = 0; |
823 | 897 | ||
@@ -832,7 +906,7 @@ int __init serial_m3110_init(void) | |||
832 | return ret; | 906 | return ret; |
833 | } | 907 | } |
834 | 908 | ||
835 | void __exit serial_m3110_exit(void) | 909 | static void __exit serial_m3110_exit(void) |
836 | { | 910 | { |
837 | spi_unregister_driver(&uart_max3110_driver); | 911 | spi_unregister_driver(&uart_max3110_driver); |
838 | uart_unregister_driver(&serial_m3110_reg); | 912 | uart_unregister_driver(&serial_m3110_reg); |
@@ -841,5 +915,5 @@ void __exit serial_m3110_exit(void) | |||
841 | module_init(serial_m3110_init); | 915 | module_init(serial_m3110_init); |
842 | module_exit(serial_m3110_exit); | 916 | module_exit(serial_m3110_exit); |
843 | 917 | ||
844 | MODULE_LICENSE("GPL"); | 918 | MODULE_LICENSE("GPL v2"); |
845 | MODULE_ALIAS("max3110-uart"); | 919 | MODULE_ALIAS("max3110-uart"); |