mrst_max3110.c 19 KB

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