diff options
author | Feng Tang <feng.tang@intel.com> | 2010-09-13 03:39:48 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2010-10-22 13:20:03 -0400 |
commit | ee9b4500eb6433015f35762d390666fede48e574 (patch) | |
tree | c27780057d181e06caf0f1db95cb11417bd1a195 /drivers/serial | |
parent | b670bde0b2497966d76154cfb9706c38cf8348f9 (diff) |
serial: mrst_max3110: some code cleanup
The cleanup for mrst_max3110 includes:
* remove unneeded head files
* make the spi_transfer dma safe, so that driver is more portable
* add more check for error return value
* use mutex_trylock for read thread
Signed-off-by: Feng Tang <feng.tang@intel.com>
Signed-off-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/serial')
-rw-r--r-- | drivers/serial/mrst_max3110.c | 327 | ||||
-rw-r--r-- | drivers/serial/mrst_max3110.h | 1 |
2 files changed, 201 insertions, 127 deletions
diff --git a/drivers/serial/mrst_max3110.c b/drivers/serial/mrst_max3110.c index 51c15f58e01e..af8db27f9e87 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)) |
@@ -434,9 +467,14 @@ static int max3110_read_thread(void *_max) | |||
434 | 467 | ||
435 | pr_info(PR_FMT "start read thread\n"); | 468 | pr_info(PR_FMT "start read thread\n"); |
436 | do { | 469 | do { |
437 | mutex_lock(&max->thread_mutex); | 470 | /* |
438 | max3110_console_receive(max); | 471 | * If can't acquire the mutex, it means the main thread |
439 | mutex_unlock(&max->thread_mutex); | 472 | * is running which will also perform the rx job |
473 | */ | ||
474 | if (mutex_trylock(&max->thread_mutex)) { | ||
475 | max3110_con_receive(max); | ||
476 | mutex_unlock(&max->thread_mutex); | ||
477 | } | ||
440 | 478 | ||
441 | set_current_state(TASK_INTERRUPTIBLE); | 479 | set_current_state(TASK_INTERRUPTIBLE); |
442 | schedule_timeout(HZ / 20); | 480 | schedule_timeout(HZ / 20); |
@@ -453,15 +491,16 @@ static int serial_m3110_startup(struct uart_port *port) | |||
453 | u16 config = 0; | 491 | u16 config = 0; |
454 | int ret = 0; | 492 | int ret = 0; |
455 | 493 | ||
456 | if (port->line != 0) | 494 | if (port->line != 0) { |
457 | pr_err(PR_FMT "uart port startup failed\n"); | 495 | pr_err(PR_FMT "uart port startup failed\n"); |
496 | return -1; | ||
497 | } | ||
458 | 498 | ||
459 | /* firstly disable all IRQ and config it to 115200, 8n1 */ | 499 | /* Disable all IRQ and config it to 115200, 8n1 */ |
460 | config = WC_TAG | WC_FIFO_ENABLE | 500 | config = WC_TAG | WC_FIFO_ENABLE |
461 | | WC_1_STOPBITS | 501 | | WC_1_STOPBITS |
462 | | WC_8BIT_WORD | 502 | | WC_8BIT_WORD |
463 | | WC_BAUD_DR2; | 503 | | WC_BAUD_DR2; |
464 | ret = max3110_out(max, config); | ||
465 | 504 | ||
466 | /* as we use thread to handle tx/rx, need set low latency */ | 505 | /* as we use thread to handle tx/rx, need set low latency */ |
467 | port->state->port.tty->low_latency = 1; | 506 | port->state->port.tty->low_latency = 1; |
@@ -472,14 +511,30 @@ static int serial_m3110_startup(struct uart_port *port) | |||
472 | if (ret) | 511 | if (ret) |
473 | return ret; | 512 | return ret; |
474 | 513 | ||
475 | /* enable RX IRQ only */ | 514 | /* Enable RX IRQ only */ |
476 | config |= WC_RXA_IRQ_ENABLE; | 515 | config |= WC_RXA_IRQ_ENABLE; |
477 | max3110_out(max, config); | ||
478 | #else | 516 | #else |
479 | /* if IRQ is disabled, start a read thread for input data */ | 517 | /* If IRQ is disabled, start a read thread for input data */ |
480 | max->read_thread = | 518 | max->read_thread = |
481 | kthread_run(max3110_read_thread, max, "max3110_read"); | 519 | kthread_run(max3110_read_thread, max, "max3110_read"); |
520 | if (IS_ERR(max->read_thread)) { | ||
521 | ret = PTR_ERR(max->read_thread); | ||
522 | max->read_thread = NULL; | ||
523 | pr_err(PR_FMT "Can't create read thread!"); | ||
524 | return ret; | ||
525 | } | ||
526 | #endif | ||
527 | |||
528 | ret = max3110_out(max, config); | ||
529 | if (ret) { | ||
530 | #ifdef CONFIG_MRST_MAX3110_IRQ | ||
531 | free_irq(max->irq, max); | ||
532 | #else | ||
533 | kthread_stop(max->read_thread); | ||
534 | max->read_thread = NULL; | ||
482 | #endif | 535 | #endif |
536 | return ret; | ||
537 | } | ||
483 | 538 | ||
484 | max->cur_conf = config; | 539 | max->cur_conf = config; |
485 | return 0; | 540 | return 0; |
@@ -516,8 +571,7 @@ static int serial_m3110_request_port(struct uart_port *port) | |||
516 | 571 | ||
517 | static void serial_m3110_config_port(struct uart_port *port, int flags) | 572 | static void serial_m3110_config_port(struct uart_port *port, int flags) |
518 | { | 573 | { |
519 | /* give it fake type */ | 574 | port->type = PORT_MAX3100; |
520 | port->type = PORT_PXA; | ||
521 | } | 575 | } |
522 | 576 | ||
523 | static int | 577 | static int |
@@ -552,6 +606,9 @@ serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, | |||
552 | new_conf |= WC_7BIT_WORD; | 606 | new_conf |= WC_7BIT_WORD; |
553 | break; | 607 | break; |
554 | default: | 608 | default: |
609 | /* We only support CS7 & CS8 */ | ||
610 | termios->c_cflag &= ~CSIZE; | ||
611 | termios->c_cflag |= CS8; | ||
555 | case CS8: | 612 | case CS8: |
556 | cval = UART_LCR_WLEN8; | 613 | cval = UART_LCR_WLEN8; |
557 | new_conf |= WC_8BIT_WORD; | 614 | new_conf |= WC_8BIT_WORD; |
@@ -560,7 +617,7 @@ serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, | |||
560 | 617 | ||
561 | baud = uart_get_baud_rate(port, termios, old, 0, 230400); | 618 | baud = uart_get_baud_rate(port, termios, old, 0, 230400); |
562 | 619 | ||
563 | /* first calc the div for 1.8MHZ clock case */ | 620 | /* First calc the div for 1.8MHZ clock case */ |
564 | switch (baud) { | 621 | switch (baud) { |
565 | case 300: | 622 | case 300: |
566 | clk_div = WC_BAUD_DR384; | 623 | clk_div = WC_BAUD_DR384; |
@@ -596,7 +653,7 @@ serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, | |||
596 | if (max->clock & MAX3110_HIGH_CLK) | 653 | if (max->clock & MAX3110_HIGH_CLK) |
597 | break; | 654 | break; |
598 | default: | 655 | default: |
599 | /* pick the previous baud rate */ | 656 | /* Pick the previous baud rate */ |
600 | baud = max->baud; | 657 | baud = max->baud; |
601 | clk_div = max->cur_conf & WC_BAUD_DIV_MASK; | 658 | clk_div = max->cur_conf & WC_BAUD_DIV_MASK; |
602 | tty_termios_encode_baud_rate(termios, baud, baud); | 659 | tty_termios_encode_baud_rate(termios, baud, baud); |
@@ -604,15 +661,21 @@ serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, | |||
604 | 661 | ||
605 | if (max->clock & MAX3110_HIGH_CLK) { | 662 | if (max->clock & MAX3110_HIGH_CLK) { |
606 | clk_div += 1; | 663 | clk_div += 1; |
607 | /* high clk version max3110 doesn't support B300 */ | 664 | /* High clk version max3110 doesn't support B300 */ |
608 | if (baud == 300) | 665 | if (baud == 300) { |
609 | baud = 600; | 666 | baud = 600; |
667 | clk_div = WC_BAUD_DR384; | ||
668 | } | ||
610 | if (baud == 230400) | 669 | if (baud == 230400) |
611 | clk_div = WC_BAUD_DR1; | 670 | clk_div = WC_BAUD_DR1; |
612 | tty_termios_encode_baud_rate(termios, baud, baud); | 671 | tty_termios_encode_baud_rate(termios, baud, baud); |
613 | } | 672 | } |
614 | 673 | ||
615 | new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div; | 674 | new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div; |
675 | |||
676 | if (unlikely(termios->c_cflag & CMSPAR)) | ||
677 | termios->c_cflag &= ~CMSPAR; | ||
678 | |||
616 | if (termios->c_cflag & CSTOPB) | 679 | if (termios->c_cflag & CSTOPB) |
617 | new_conf |= WC_2_STOPBITS; | 680 | new_conf |= WC_2_STOPBITS; |
618 | else | 681 | else |
@@ -632,13 +695,14 @@ serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, | |||
632 | 695 | ||
633 | new_conf |= WC_TAG; | 696 | new_conf |= WC_TAG; |
634 | if (new_conf != max->cur_conf) { | 697 | if (new_conf != max->cur_conf) { |
635 | max3110_out(max, new_conf); | 698 | if (!max3110_out(max, new_conf)) { |
636 | max->cur_conf = new_conf; | 699 | max->cur_conf = new_conf; |
637 | max->baud = baud; | 700 | max->baud = baud; |
701 | } | ||
638 | } | 702 | } |
639 | } | 703 | } |
640 | 704 | ||
641 | /* don't handle hw handshaking */ | 705 | /* Don't handle hw handshaking */ |
642 | static unsigned int serial_m3110_get_mctrl(struct uart_port *port) | 706 | static unsigned int serial_m3110_get_mctrl(struct uart_port *port) |
643 | { | 707 | { |
644 | return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR; | 708 | return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR; |
@@ -672,7 +736,7 @@ struct uart_ops serial_m3110_ops = { | |||
672 | .break_ctl = serial_m3110_break_ctl, | 736 | .break_ctl = serial_m3110_break_ctl, |
673 | .startup = serial_m3110_startup, | 737 | .startup = serial_m3110_startup, |
674 | .shutdown = serial_m3110_shutdown, | 738 | .shutdown = serial_m3110_shutdown, |
675 | .set_termios = serial_m3110_set_termios, /* must have */ | 739 | .set_termios = serial_m3110_set_termios, |
676 | .pm = serial_m3110_pm, | 740 | .pm = serial_m3110_pm, |
677 | .type = serial_m3110_type, | 741 | .type = serial_m3110_type, |
678 | .release_port = serial_m3110_release_port, | 742 | .release_port = serial_m3110_release_port, |
@@ -688,52 +752,60 @@ static struct uart_driver serial_m3110_reg = { | |||
688 | .major = TTY_MAJOR, | 752 | .major = TTY_MAJOR, |
689 | .minor = 64, | 753 | .minor = 64, |
690 | .nr = 1, | 754 | .nr = 1, |
691 | .cons = MRST_CONSOLE, | 755 | .cons = &serial_m3110_console, |
692 | }; | 756 | }; |
693 | 757 | ||
758 | #ifdef CONFIG_PM | ||
694 | static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state) | 759 | static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state) |
695 | { | 760 | { |
761 | struct uart_max3110 *max = spi_get_drvdata(spi); | ||
762 | |||
763 | disable_irq(max->irq); | ||
764 | uart_suspend_port(&serial_m3110_reg, &max->port); | ||
765 | max3110_out(max, max->cur_conf | WC_SW_SHDI); | ||
696 | return 0; | 766 | return 0; |
697 | } | 767 | } |
698 | 768 | ||
699 | static int serial_m3110_resume(struct spi_device *spi) | 769 | static int serial_m3110_resume(struct spi_device *spi) |
700 | { | 770 | { |
771 | struct uart_max3110 *max = spi_get_drvdata(spi); | ||
772 | |||
773 | max3110_out(max, max->cur_conf); | ||
774 | uart_resume_port(&serial_m3110_reg, &max->port); | ||
775 | enable_irq(max->irq); | ||
701 | return 0; | 776 | return 0; |
702 | } | 777 | } |
778 | #else | ||
779 | #define serial_m3110_suspend NULL | ||
780 | #define serial_m3110_resume NULL | ||
781 | #endif | ||
703 | 782 | ||
704 | static struct dw_spi_chip spi0_uart = { | 783 | 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 | { | 784 | { |
712 | struct uart_max3110 *max; | 785 | struct uart_max3110 *max; |
713 | int ret; | 786 | void *buffer; |
714 | unsigned char *buffer; | ||
715 | u16 res; | 787 | u16 res; |
788 | int ret = 0; | ||
789 | |||
716 | max = kzalloc(sizeof(*max), GFP_KERNEL); | 790 | max = kzalloc(sizeof(*max), GFP_KERNEL); |
717 | if (!max) | 791 | if (!max) |
718 | return -ENOMEM; | 792 | return -ENOMEM; |
719 | 793 | ||
720 | /* set spi info */ | 794 | /* Set spi info */ |
721 | spi->mode = SPI_MODE_0; | ||
722 | spi->bits_per_word = 16; | 795 | spi->bits_per_word = 16; |
723 | max->clock = MAX3110_HIGH_CLK; | 796 | max->clock = MAX3110_HIGH_CLK; |
724 | spi->controller_data = &spi0_uart; | ||
725 | 797 | ||
726 | spi_setup(spi); | 798 | spi_setup(spi); |
727 | 799 | ||
728 | max->port.type = PORT_PXA; /* need apply for a max3110 type */ | 800 | max->port.type = PORT_MAX3100; |
729 | max->port.fifosize = 2; /* only have 16b buffer */ | 801 | max->port.fifosize = 2; /* Only have 16b buffer */ |
730 | max->port.ops = &serial_m3110_ops; | 802 | max->port.ops = &serial_m3110_ops; |
731 | max->port.line = 0; | 803 | max->port.line = 0; |
732 | max->port.dev = &spi->dev; | 804 | max->port.dev = &spi->dev; |
733 | max->port.uartclk = 115200; | 805 | max->port.uartclk = 115200; |
734 | 806 | ||
735 | max->spi = spi; | 807 | max->spi = spi; |
736 | max->name = spi->modalias; /* use spi name as the name */ | 808 | strcpy(max->name, spi->modalias); |
737 | max->irq = (u16)spi->irq; | 809 | max->irq = (u16)spi->irq; |
738 | 810 | ||
739 | mutex_init(&max->thread_mutex); | 811 | mutex_init(&max->thread_mutex); |
@@ -755,13 +827,15 @@ static int serial_m3110_probe(struct spi_device *spi) | |||
755 | ret = -ENODEV; | 827 | ret = -ENODEV; |
756 | goto err_get_page; | 828 | goto err_get_page; |
757 | } | 829 | } |
758 | buffer = (unsigned char *)__get_free_page(GFP_KERNEL); | 830 | |
831 | buffer = (void *)__get_free_page(GFP_KERNEL); | ||
759 | if (!buffer) { | 832 | if (!buffer) { |
760 | ret = -ENOMEM; | 833 | ret = -ENOMEM; |
761 | goto err_get_page; | 834 | goto err_get_page; |
762 | } | 835 | } |
763 | max->con_xmit.buf = (unsigned char *)buffer; | 836 | max->con_xmit.buf = buffer; |
764 | max->con_xmit.head = max->con_xmit.tail = 0; | 837 | max->con_xmit.head = 0; |
838 | max->con_xmit.tail = 0; | ||
765 | 839 | ||
766 | max->main_thread = kthread_run(max3110_main_thread, | 840 | max->main_thread = kthread_run(max3110_main_thread, |
767 | max, "max3110_main"); | 841 | max, "max3110_main"); |
@@ -770,8 +844,10 @@ static int serial_m3110_probe(struct spi_device *spi) | |||
770 | goto err_kthread; | 844 | goto err_kthread; |
771 | } | 845 | } |
772 | 846 | ||
847 | spi_set_drvdata(spi, max); | ||
773 | pmax = max; | 848 | pmax = max; |
774 | /* give membase a psudo value to pass serial_core's check */ | 849 | |
850 | /* Give membase a psudo value to pass serial_core's check */ | ||
775 | max->port.membase = (void *)0xff110000; | 851 | max->port.membase = (void *)0xff110000; |
776 | uart_add_one_port(&serial_m3110_reg, &max->port); | 852 | uart_add_one_port(&serial_m3110_reg, &max->port); |
777 | 853 | ||
@@ -780,19 +856,17 @@ static int serial_m3110_probe(struct spi_device *spi) | |||
780 | err_kthread: | 856 | err_kthread: |
781 | free_page((unsigned long)buffer); | 857 | free_page((unsigned long)buffer); |
782 | err_get_page: | 858 | err_get_page: |
783 | pmax = NULL; | ||
784 | kfree(max); | 859 | kfree(max); |
785 | return ret; | 860 | return ret; |
786 | } | 861 | } |
787 | 862 | ||
788 | static int max3110_remove(struct spi_device *dev) | 863 | static int __devexit serial_m3110_remove(struct spi_device *dev) |
789 | { | 864 | { |
790 | struct uart_max3110 *max = pmax; | 865 | struct uart_max3110 *max = spi_get_drvdata(dev); |
791 | 866 | ||
792 | if (!pmax) | 867 | if (!max) |
793 | return 0; | 868 | return 0; |
794 | 869 | ||
795 | pmax = NULL; | ||
796 | uart_remove_one_port(&serial_m3110_reg, &max->port); | 870 | uart_remove_one_port(&serial_m3110_reg, &max->port); |
797 | 871 | ||
798 | free_page((unsigned long)max->con_xmit.buf); | 872 | free_page((unsigned long)max->con_xmit.buf); |
@@ -811,13 +885,12 @@ static struct spi_driver uart_max3110_driver = { | |||
811 | .owner = THIS_MODULE, | 885 | .owner = THIS_MODULE, |
812 | }, | 886 | }, |
813 | .probe = serial_m3110_probe, | 887 | .probe = serial_m3110_probe, |
814 | .remove = __devexit_p(max3110_remove), | 888 | .remove = __devexit_p(serial_m3110_remove), |
815 | .suspend = serial_m3110_suspend, | 889 | .suspend = serial_m3110_suspend, |
816 | .resume = serial_m3110_resume, | 890 | .resume = serial_m3110_resume, |
817 | }; | 891 | }; |
818 | 892 | ||
819 | 893 | static int __init serial_m3110_init(void) | |
820 | int __init serial_m3110_init(void) | ||
821 | { | 894 | { |
822 | int ret = 0; | 895 | int ret = 0; |
823 | 896 | ||
@@ -832,7 +905,7 @@ int __init serial_m3110_init(void) | |||
832 | return ret; | 905 | return ret; |
833 | } | 906 | } |
834 | 907 | ||
835 | void __exit serial_m3110_exit(void) | 908 | static void __exit serial_m3110_exit(void) |
836 | { | 909 | { |
837 | spi_unregister_driver(&uart_max3110_driver); | 910 | spi_unregister_driver(&uart_max3110_driver); |
838 | uart_unregister_driver(&serial_m3110_reg); | 911 | uart_unregister_driver(&serial_m3110_reg); |
@@ -841,5 +914,5 @@ void __exit serial_m3110_exit(void) | |||
841 | module_init(serial_m3110_init); | 914 | module_init(serial_m3110_init); |
842 | module_exit(serial_m3110_exit); | 915 | module_exit(serial_m3110_exit); |
843 | 916 | ||
844 | MODULE_LICENSE("GPL"); | 917 | MODULE_LICENSE("GPL v2"); |
845 | MODULE_ALIAS("max3110-uart"); | 918 | MODULE_ALIAS("max3110-uart"); |
diff --git a/drivers/serial/mrst_max3110.h b/drivers/serial/mrst_max3110.h index 363478acb2c3..d1ef43af397c 100644 --- a/drivers/serial/mrst_max3110.h +++ b/drivers/serial/mrst_max3110.h | |||
@@ -56,4 +56,5 @@ | |||
56 | #define WC_BAUD_DR192 (0xE) | 56 | #define WC_BAUD_DR192 (0xE) |
57 | #define WC_BAUD_DR384 (0xF) | 57 | #define WC_BAUD_DR384 (0xF) |
58 | 58 | ||
59 | #define M3110_RX_FIFO_DEPTH 8 | ||
59 | #endif | 60 | #endif |