sdio_uart.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SDIO UART/GPS driver
  4. *
  5. * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
  6. * by Russell King.
  7. *
  8. * Author: Nicolas Pitre
  9. * Created: June 15, 2007
  10. * Copyright: MontaVista Software, Inc.
  11. */
  12. /*
  13. * Note: Although this driver assumes a 16550A-like UART implementation,
  14. * it is not possible to leverage the common 8250/16550 driver, nor the
  15. * core UART infrastructure, as they assumes direct access to the hardware
  16. * registers, often under a spinlock. This is not possible in the SDIO
  17. * context as SDIO access functions must be able to sleep.
  18. *
  19. * Because we need to lock the SDIO host to ensure an exclusive access to
  20. * the card, we simply rely on that lock to also prevent and serialize
  21. * concurrent access to the same port.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/sched.h>
  27. #include <linux/mutex.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/serial_reg.h>
  30. #include <linux/circ_buf.h>
  31. #include <linux/tty.h>
  32. #include <linux/tty_flip.h>
  33. #include <linux/kfifo.h>
  34. #include <linux/slab.h>
  35. #include <linux/mmc/core.h>
  36. #include <linux/mmc/card.h>
  37. #include <linux/mmc/sdio_func.h>
  38. #include <linux/mmc/sdio_ids.h>
  39. #define UART_NR 8 /* Number of UARTs this driver can handle */
  40. #define FIFO_SIZE PAGE_SIZE
  41. #define WAKEUP_CHARS 256
  42. struct uart_icount {
  43. __u32 cts;
  44. __u32 dsr;
  45. __u32 rng;
  46. __u32 dcd;
  47. __u32 rx;
  48. __u32 tx;
  49. __u32 frame;
  50. __u32 overrun;
  51. __u32 parity;
  52. __u32 brk;
  53. };
  54. struct sdio_uart_port {
  55. struct tty_port port;
  56. unsigned int index;
  57. struct sdio_func *func;
  58. struct mutex func_lock;
  59. struct task_struct *in_sdio_uart_irq;
  60. unsigned int regs_offset;
  61. struct kfifo xmit_fifo;
  62. spinlock_t write_lock;
  63. struct uart_icount icount;
  64. unsigned int uartclk;
  65. unsigned int mctrl;
  66. unsigned int rx_mctrl;
  67. unsigned int read_status_mask;
  68. unsigned int ignore_status_mask;
  69. unsigned char x_char;
  70. unsigned char ier;
  71. unsigned char lcr;
  72. };
  73. static struct sdio_uart_port *sdio_uart_table[UART_NR];
  74. static DEFINE_SPINLOCK(sdio_uart_table_lock);
  75. static int sdio_uart_add_port(struct sdio_uart_port *port)
  76. {
  77. int index, ret = -EBUSY;
  78. mutex_init(&port->func_lock);
  79. spin_lock_init(&port->write_lock);
  80. if (kfifo_alloc(&port->xmit_fifo, FIFO_SIZE, GFP_KERNEL))
  81. return -ENOMEM;
  82. spin_lock(&sdio_uart_table_lock);
  83. for (index = 0; index < UART_NR; index++) {
  84. if (!sdio_uart_table[index]) {
  85. port->index = index;
  86. sdio_uart_table[index] = port;
  87. ret = 0;
  88. break;
  89. }
  90. }
  91. spin_unlock(&sdio_uart_table_lock);
  92. return ret;
  93. }
  94. static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
  95. {
  96. struct sdio_uart_port *port;
  97. if (index >= UART_NR)
  98. return NULL;
  99. spin_lock(&sdio_uart_table_lock);
  100. port = sdio_uart_table[index];
  101. if (port)
  102. tty_port_get(&port->port);
  103. spin_unlock(&sdio_uart_table_lock);
  104. return port;
  105. }
  106. static void sdio_uart_port_put(struct sdio_uart_port *port)
  107. {
  108. tty_port_put(&port->port);
  109. }
  110. static void sdio_uart_port_remove(struct sdio_uart_port *port)
  111. {
  112. struct sdio_func *func;
  113. spin_lock(&sdio_uart_table_lock);
  114. sdio_uart_table[port->index] = NULL;
  115. spin_unlock(&sdio_uart_table_lock);
  116. /*
  117. * We're killing a port that potentially still is in use by
  118. * the tty layer. Be careful to prevent any further access
  119. * to the SDIO function and arrange for the tty layer to
  120. * give up on that port ASAP.
  121. * Beware: the lock ordering is critical.
  122. */
  123. mutex_lock(&port->port.mutex);
  124. mutex_lock(&port->func_lock);
  125. func = port->func;
  126. sdio_claim_host(func);
  127. port->func = NULL;
  128. mutex_unlock(&port->func_lock);
  129. /* tty_hangup is async so is this safe as is ?? */
  130. tty_port_tty_hangup(&port->port, false);
  131. mutex_unlock(&port->port.mutex);
  132. sdio_release_irq(func);
  133. sdio_disable_func(func);
  134. sdio_release_host(func);
  135. sdio_uart_port_put(port);
  136. }
  137. static int sdio_uart_claim_func(struct sdio_uart_port *port)
  138. {
  139. mutex_lock(&port->func_lock);
  140. if (unlikely(!port->func)) {
  141. mutex_unlock(&port->func_lock);
  142. return -ENODEV;
  143. }
  144. if (likely(port->in_sdio_uart_irq != current))
  145. sdio_claim_host(port->func);
  146. mutex_unlock(&port->func_lock);
  147. return 0;
  148. }
  149. static inline void sdio_uart_release_func(struct sdio_uart_port *port)
  150. {
  151. if (likely(port->in_sdio_uart_irq != current))
  152. sdio_release_host(port->func);
  153. }
  154. static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
  155. {
  156. unsigned char c;
  157. c = sdio_readb(port->func, port->regs_offset + offset, NULL);
  158. return c;
  159. }
  160. static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
  161. {
  162. sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
  163. }
  164. static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
  165. {
  166. unsigned char status;
  167. unsigned int ret;
  168. /* FIXME: What stops this losing the delta bits and breaking
  169. sdio_uart_check_modem_status ? */
  170. status = sdio_in(port, UART_MSR);
  171. ret = 0;
  172. if (status & UART_MSR_DCD)
  173. ret |= TIOCM_CAR;
  174. if (status & UART_MSR_RI)
  175. ret |= TIOCM_RNG;
  176. if (status & UART_MSR_DSR)
  177. ret |= TIOCM_DSR;
  178. if (status & UART_MSR_CTS)
  179. ret |= TIOCM_CTS;
  180. return ret;
  181. }
  182. static void sdio_uart_write_mctrl(struct sdio_uart_port *port,
  183. unsigned int mctrl)
  184. {
  185. unsigned char mcr = 0;
  186. if (mctrl & TIOCM_RTS)
  187. mcr |= UART_MCR_RTS;
  188. if (mctrl & TIOCM_DTR)
  189. mcr |= UART_MCR_DTR;
  190. if (mctrl & TIOCM_OUT1)
  191. mcr |= UART_MCR_OUT1;
  192. if (mctrl & TIOCM_OUT2)
  193. mcr |= UART_MCR_OUT2;
  194. if (mctrl & TIOCM_LOOP)
  195. mcr |= UART_MCR_LOOP;
  196. sdio_out(port, UART_MCR, mcr);
  197. }
  198. static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
  199. unsigned int set, unsigned int clear)
  200. {
  201. unsigned int old;
  202. old = port->mctrl;
  203. port->mctrl = (old & ~clear) | set;
  204. if (old != port->mctrl)
  205. sdio_uart_write_mctrl(port, port->mctrl);
  206. }
  207. #define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
  208. #define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
  209. static void sdio_uart_change_speed(struct sdio_uart_port *port,
  210. struct ktermios *termios,
  211. struct ktermios *old)
  212. {
  213. unsigned char cval, fcr = 0;
  214. unsigned int baud, quot;
  215. switch (termios->c_cflag & CSIZE) {
  216. case CS5:
  217. cval = UART_LCR_WLEN5;
  218. break;
  219. case CS6:
  220. cval = UART_LCR_WLEN6;
  221. break;
  222. case CS7:
  223. cval = UART_LCR_WLEN7;
  224. break;
  225. default:
  226. case CS8:
  227. cval = UART_LCR_WLEN8;
  228. break;
  229. }
  230. if (termios->c_cflag & CSTOPB)
  231. cval |= UART_LCR_STOP;
  232. if (termios->c_cflag & PARENB)
  233. cval |= UART_LCR_PARITY;
  234. if (!(termios->c_cflag & PARODD))
  235. cval |= UART_LCR_EPAR;
  236. for (;;) {
  237. baud = tty_termios_baud_rate(termios);
  238. if (baud == 0)
  239. baud = 9600; /* Special case: B0 rate. */
  240. if (baud <= port->uartclk)
  241. break;
  242. /*
  243. * Oops, the quotient was zero. Try again with the old
  244. * baud rate if possible, otherwise default to 9600.
  245. */
  246. termios->c_cflag &= ~CBAUD;
  247. if (old) {
  248. termios->c_cflag |= old->c_cflag & CBAUD;
  249. old = NULL;
  250. } else
  251. termios->c_cflag |= B9600;
  252. }
  253. quot = (2 * port->uartclk + baud) / (2 * baud);
  254. if (baud < 2400)
  255. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
  256. else
  257. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
  258. port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  259. if (termios->c_iflag & INPCK)
  260. port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  261. if (termios->c_iflag & (BRKINT | PARMRK))
  262. port->read_status_mask |= UART_LSR_BI;
  263. /*
  264. * Characters to ignore
  265. */
  266. port->ignore_status_mask = 0;
  267. if (termios->c_iflag & IGNPAR)
  268. port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  269. if (termios->c_iflag & IGNBRK) {
  270. port->ignore_status_mask |= UART_LSR_BI;
  271. /*
  272. * If we're ignoring parity and break indicators,
  273. * ignore overruns too (for real raw support).
  274. */
  275. if (termios->c_iflag & IGNPAR)
  276. port->ignore_status_mask |= UART_LSR_OE;
  277. }
  278. /*
  279. * ignore all characters if CREAD is not set
  280. */
  281. if ((termios->c_cflag & CREAD) == 0)
  282. port->ignore_status_mask |= UART_LSR_DR;
  283. /*
  284. * CTS flow control flag and modem status interrupts
  285. */
  286. port->ier &= ~UART_IER_MSI;
  287. if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
  288. port->ier |= UART_IER_MSI;
  289. port->lcr = cval;
  290. sdio_out(port, UART_IER, port->ier);
  291. sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
  292. sdio_out(port, UART_DLL, quot & 0xff);
  293. sdio_out(port, UART_DLM, quot >> 8);
  294. sdio_out(port, UART_LCR, cval);
  295. sdio_out(port, UART_FCR, fcr);
  296. sdio_uart_write_mctrl(port, port->mctrl);
  297. }
  298. static void sdio_uart_start_tx(struct sdio_uart_port *port)
  299. {
  300. if (!(port->ier & UART_IER_THRI)) {
  301. port->ier |= UART_IER_THRI;
  302. sdio_out(port, UART_IER, port->ier);
  303. }
  304. }
  305. static void sdio_uart_stop_tx(struct sdio_uart_port *port)
  306. {
  307. if (port->ier & UART_IER_THRI) {
  308. port->ier &= ~UART_IER_THRI;
  309. sdio_out(port, UART_IER, port->ier);
  310. }
  311. }
  312. static void sdio_uart_stop_rx(struct sdio_uart_port *port)
  313. {
  314. port->ier &= ~UART_IER_RLSI;
  315. port->read_status_mask &= ~UART_LSR_DR;
  316. sdio_out(port, UART_IER, port->ier);
  317. }
  318. static void sdio_uart_receive_chars(struct sdio_uart_port *port,
  319. unsigned int *status)
  320. {
  321. unsigned int ch, flag;
  322. int max_count = 256;
  323. do {
  324. ch = sdio_in(port, UART_RX);
  325. flag = TTY_NORMAL;
  326. port->icount.rx++;
  327. if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
  328. UART_LSR_FE | UART_LSR_OE))) {
  329. /*
  330. * For statistics only
  331. */
  332. if (*status & UART_LSR_BI) {
  333. *status &= ~(UART_LSR_FE | UART_LSR_PE);
  334. port->icount.brk++;
  335. } else if (*status & UART_LSR_PE)
  336. port->icount.parity++;
  337. else if (*status & UART_LSR_FE)
  338. port->icount.frame++;
  339. if (*status & UART_LSR_OE)
  340. port->icount.overrun++;
  341. /*
  342. * Mask off conditions which should be ignored.
  343. */
  344. *status &= port->read_status_mask;
  345. if (*status & UART_LSR_BI)
  346. flag = TTY_BREAK;
  347. else if (*status & UART_LSR_PE)
  348. flag = TTY_PARITY;
  349. else if (*status & UART_LSR_FE)
  350. flag = TTY_FRAME;
  351. }
  352. if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
  353. tty_insert_flip_char(&port->port, ch, flag);
  354. /*
  355. * Overrun is special. Since it's reported immediately,
  356. * it doesn't affect the current character.
  357. */
  358. if (*status & ~port->ignore_status_mask & UART_LSR_OE)
  359. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  360. *status = sdio_in(port, UART_LSR);
  361. } while ((*status & UART_LSR_DR) && (max_count-- > 0));
  362. tty_flip_buffer_push(&port->port);
  363. }
  364. static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
  365. {
  366. struct kfifo *xmit = &port->xmit_fifo;
  367. int count;
  368. struct tty_struct *tty;
  369. u8 iobuf[16];
  370. int len;
  371. if (port->x_char) {
  372. sdio_out(port, UART_TX, port->x_char);
  373. port->icount.tx++;
  374. port->x_char = 0;
  375. return;
  376. }
  377. tty = tty_port_tty_get(&port->port);
  378. if (tty == NULL || !kfifo_len(xmit) ||
  379. tty->stopped || tty->hw_stopped) {
  380. sdio_uart_stop_tx(port);
  381. tty_kref_put(tty);
  382. return;
  383. }
  384. len = kfifo_out_locked(xmit, iobuf, 16, &port->write_lock);
  385. for (count = 0; count < len; count++) {
  386. sdio_out(port, UART_TX, iobuf[count]);
  387. port->icount.tx++;
  388. }
  389. len = kfifo_len(xmit);
  390. if (len < WAKEUP_CHARS) {
  391. tty_wakeup(tty);
  392. if (len == 0)
  393. sdio_uart_stop_tx(port);
  394. }
  395. tty_kref_put(tty);
  396. }
  397. static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
  398. {
  399. int status;
  400. struct tty_struct *tty;
  401. status = sdio_in(port, UART_MSR);
  402. if ((status & UART_MSR_ANY_DELTA) == 0)
  403. return;
  404. if (status & UART_MSR_TERI)
  405. port->icount.rng++;
  406. if (status & UART_MSR_DDSR)
  407. port->icount.dsr++;
  408. if (status & UART_MSR_DDCD) {
  409. port->icount.dcd++;
  410. /* DCD raise - wake for open */
  411. if (status & UART_MSR_DCD)
  412. wake_up_interruptible(&port->port.open_wait);
  413. else {
  414. /* DCD drop - hang up if tty attached */
  415. tty_port_tty_hangup(&port->port, false);
  416. }
  417. }
  418. if (status & UART_MSR_DCTS) {
  419. port->icount.cts++;
  420. tty = tty_port_tty_get(&port->port);
  421. if (tty && C_CRTSCTS(tty)) {
  422. int cts = (status & UART_MSR_CTS);
  423. if (tty->hw_stopped) {
  424. if (cts) {
  425. tty->hw_stopped = 0;
  426. sdio_uart_start_tx(port);
  427. tty_wakeup(tty);
  428. }
  429. } else {
  430. if (!cts) {
  431. tty->hw_stopped = 1;
  432. sdio_uart_stop_tx(port);
  433. }
  434. }
  435. }
  436. tty_kref_put(tty);
  437. }
  438. }
  439. /*
  440. * This handles the interrupt from one port.
  441. */
  442. static void sdio_uart_irq(struct sdio_func *func)
  443. {
  444. struct sdio_uart_port *port = sdio_get_drvdata(func);
  445. unsigned int iir, lsr;
  446. /*
  447. * In a few places sdio_uart_irq() is called directly instead of
  448. * waiting for the actual interrupt to be raised and the SDIO IRQ
  449. * thread scheduled in order to reduce latency. However, some
  450. * interaction with the tty core may end up calling us back
  451. * (serial echo, flow control, etc.) through those same places
  452. * causing undesirable effects. Let's stop the recursion here.
  453. */
  454. if (unlikely(port->in_sdio_uart_irq == current))
  455. return;
  456. iir = sdio_in(port, UART_IIR);
  457. if (iir & UART_IIR_NO_INT)
  458. return;
  459. port->in_sdio_uart_irq = current;
  460. lsr = sdio_in(port, UART_LSR);
  461. if (lsr & UART_LSR_DR)
  462. sdio_uart_receive_chars(port, &lsr);
  463. sdio_uart_check_modem_status(port);
  464. if (lsr & UART_LSR_THRE)
  465. sdio_uart_transmit_chars(port);
  466. port->in_sdio_uart_irq = NULL;
  467. }
  468. static int uart_carrier_raised(struct tty_port *tport)
  469. {
  470. struct sdio_uart_port *port =
  471. container_of(tport, struct sdio_uart_port, port);
  472. unsigned int ret = sdio_uart_claim_func(port);
  473. if (ret) /* Missing hardware shouldn't block for carrier */
  474. return 1;
  475. ret = sdio_uart_get_mctrl(port);
  476. sdio_uart_release_func(port);
  477. if (ret & TIOCM_CAR)
  478. return 1;
  479. return 0;
  480. }
  481. /**
  482. * uart_dtr_rts - port helper to set uart signals
  483. * @tport: tty port to be updated
  484. * @onoff: set to turn on DTR/RTS
  485. *
  486. * Called by the tty port helpers when the modem signals need to be
  487. * adjusted during an open, close and hangup.
  488. */
  489. static void uart_dtr_rts(struct tty_port *tport, int onoff)
  490. {
  491. struct sdio_uart_port *port =
  492. container_of(tport, struct sdio_uart_port, port);
  493. int ret = sdio_uart_claim_func(port);
  494. if (ret)
  495. return;
  496. if (onoff == 0)
  497. sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  498. else
  499. sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  500. sdio_uart_release_func(port);
  501. }
  502. /**
  503. * sdio_uart_activate - start up hardware
  504. * @tport: tty port to activate
  505. * @tty: tty bound to this port
  506. *
  507. * Activate a tty port. The port locking guarantees us this will be
  508. * run exactly once per set of opens, and if successful will see the
  509. * shutdown method run exactly once to match. Start up and shutdown are
  510. * protected from each other by the internal locking and will not run
  511. * at the same time even during a hangup event.
  512. *
  513. * If we successfully start up the port we take an extra kref as we
  514. * will keep it around until shutdown when the kref is dropped.
  515. */
  516. static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
  517. {
  518. struct sdio_uart_port *port =
  519. container_of(tport, struct sdio_uart_port, port);
  520. int ret;
  521. /*
  522. * Set the TTY IO error marker - we will only clear this
  523. * once we have successfully opened the port.
  524. */
  525. set_bit(TTY_IO_ERROR, &tty->flags);
  526. kfifo_reset(&port->xmit_fifo);
  527. ret = sdio_uart_claim_func(port);
  528. if (ret)
  529. return ret;
  530. ret = sdio_enable_func(port->func);
  531. if (ret)
  532. goto err1;
  533. ret = sdio_claim_irq(port->func, sdio_uart_irq);
  534. if (ret)
  535. goto err2;
  536. /*
  537. * Clear the FIFO buffers and disable them.
  538. * (they will be reenabled in sdio_change_speed())
  539. */
  540. sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
  541. sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
  542. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  543. sdio_out(port, UART_FCR, 0);
  544. /*
  545. * Clear the interrupt registers.
  546. */
  547. (void) sdio_in(port, UART_LSR);
  548. (void) sdio_in(port, UART_RX);
  549. (void) sdio_in(port, UART_IIR);
  550. (void) sdio_in(port, UART_MSR);
  551. /*
  552. * Now, initialize the UART
  553. */
  554. sdio_out(port, UART_LCR, UART_LCR_WLEN8);
  555. port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE;
  556. port->mctrl = TIOCM_OUT2;
  557. sdio_uart_change_speed(port, &tty->termios, NULL);
  558. if (C_BAUD(tty))
  559. sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
  560. if (C_CRTSCTS(tty))
  561. if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
  562. tty->hw_stopped = 1;
  563. clear_bit(TTY_IO_ERROR, &tty->flags);
  564. /* Kick the IRQ handler once while we're still holding the host lock */
  565. sdio_uart_irq(port->func);
  566. sdio_uart_release_func(port);
  567. return 0;
  568. err2:
  569. sdio_disable_func(port->func);
  570. err1:
  571. sdio_uart_release_func(port);
  572. return ret;
  573. }
  574. /**
  575. * sdio_uart_shutdown - stop hardware
  576. * @tport: tty port to shut down
  577. *
  578. * Deactivate a tty port. The port locking guarantees us this will be
  579. * run only if a successful matching activate already ran. The two are
  580. * protected from each other by the internal locking and will not run
  581. * at the same time even during a hangup event.
  582. */
  583. static void sdio_uart_shutdown(struct tty_port *tport)
  584. {
  585. struct sdio_uart_port *port =
  586. container_of(tport, struct sdio_uart_port, port);
  587. int ret;
  588. ret = sdio_uart_claim_func(port);
  589. if (ret)
  590. return;
  591. sdio_uart_stop_rx(port);
  592. /* Disable interrupts from this port */
  593. sdio_release_irq(port->func);
  594. port->ier = 0;
  595. sdio_out(port, UART_IER, 0);
  596. sdio_uart_clear_mctrl(port, TIOCM_OUT2);
  597. /* Disable break condition and FIFOs. */
  598. port->lcr &= ~UART_LCR_SBC;
  599. sdio_out(port, UART_LCR, port->lcr);
  600. sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
  601. UART_FCR_CLEAR_RCVR |
  602. UART_FCR_CLEAR_XMIT);
  603. sdio_out(port, UART_FCR, 0);
  604. sdio_disable_func(port->func);
  605. sdio_uart_release_func(port);
  606. }
  607. static void sdio_uart_port_destroy(struct tty_port *tport)
  608. {
  609. struct sdio_uart_port *port =
  610. container_of(tport, struct sdio_uart_port, port);
  611. kfifo_free(&port->xmit_fifo);
  612. kfree(port);
  613. }
  614. /**
  615. * sdio_uart_install - install method
  616. * @driver: the driver in use (sdio_uart in our case)
  617. * @tty: the tty being bound
  618. *
  619. * Look up and bind the tty and the driver together. Initialize
  620. * any needed private data (in our case the termios)
  621. */
  622. static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty)
  623. {
  624. int idx = tty->index;
  625. struct sdio_uart_port *port = sdio_uart_port_get(idx);
  626. int ret = tty_standard_install(driver, tty);
  627. if (ret == 0)
  628. /* This is the ref sdio_uart_port get provided */
  629. tty->driver_data = port;
  630. else
  631. sdio_uart_port_put(port);
  632. return ret;
  633. }
  634. /**
  635. * sdio_uart_cleanup - called on the last tty kref drop
  636. * @tty: the tty being destroyed
  637. *
  638. * Called asynchronously when the last reference to the tty is dropped.
  639. * We cannot destroy the tty->driver_data port kref until this point
  640. */
  641. static void sdio_uart_cleanup(struct tty_struct *tty)
  642. {
  643. struct sdio_uart_port *port = tty->driver_data;
  644. tty->driver_data = NULL; /* Bug trap */
  645. sdio_uart_port_put(port);
  646. }
  647. /*
  648. * Open/close/hangup is now entirely boilerplate
  649. */
  650. static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
  651. {
  652. struct sdio_uart_port *port = tty->driver_data;
  653. return tty_port_open(&port->port, tty, filp);
  654. }
  655. static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
  656. {
  657. struct sdio_uart_port *port = tty->driver_data;
  658. tty_port_close(&port->port, tty, filp);
  659. }
  660. static void sdio_uart_hangup(struct tty_struct *tty)
  661. {
  662. struct sdio_uart_port *port = tty->driver_data;
  663. tty_port_hangup(&port->port);
  664. }
  665. static int sdio_uart_write(struct tty_struct *tty, const unsigned char *buf,
  666. int count)
  667. {
  668. struct sdio_uart_port *port = tty->driver_data;
  669. int ret;
  670. if (!port->func)
  671. return -ENODEV;
  672. ret = kfifo_in_locked(&port->xmit_fifo, buf, count, &port->write_lock);
  673. if (!(port->ier & UART_IER_THRI)) {
  674. int err = sdio_uart_claim_func(port);
  675. if (!err) {
  676. sdio_uart_start_tx(port);
  677. sdio_uart_irq(port->func);
  678. sdio_uart_release_func(port);
  679. } else
  680. ret = err;
  681. }
  682. return ret;
  683. }
  684. static int sdio_uart_write_room(struct tty_struct *tty)
  685. {
  686. struct sdio_uart_port *port = tty->driver_data;
  687. return FIFO_SIZE - kfifo_len(&port->xmit_fifo);
  688. }
  689. static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
  690. {
  691. struct sdio_uart_port *port = tty->driver_data;
  692. return kfifo_len(&port->xmit_fifo);
  693. }
  694. static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
  695. {
  696. struct sdio_uart_port *port = tty->driver_data;
  697. port->x_char = ch;
  698. if (ch && !(port->ier & UART_IER_THRI)) {
  699. if (sdio_uart_claim_func(port) != 0)
  700. return;
  701. sdio_uart_start_tx(port);
  702. sdio_uart_irq(port->func);
  703. sdio_uart_release_func(port);
  704. }
  705. }
  706. static void sdio_uart_throttle(struct tty_struct *tty)
  707. {
  708. struct sdio_uart_port *port = tty->driver_data;
  709. if (!I_IXOFF(tty) && !C_CRTSCTS(tty))
  710. return;
  711. if (sdio_uart_claim_func(port) != 0)
  712. return;
  713. if (I_IXOFF(tty)) {
  714. port->x_char = STOP_CHAR(tty);
  715. sdio_uart_start_tx(port);
  716. }
  717. if (C_CRTSCTS(tty))
  718. sdio_uart_clear_mctrl(port, TIOCM_RTS);
  719. sdio_uart_irq(port->func);
  720. sdio_uart_release_func(port);
  721. }
  722. static void sdio_uart_unthrottle(struct tty_struct *tty)
  723. {
  724. struct sdio_uart_port *port = tty->driver_data;
  725. if (!I_IXOFF(tty) && !C_CRTSCTS(tty))
  726. return;
  727. if (sdio_uart_claim_func(port) != 0)
  728. return;
  729. if (I_IXOFF(tty)) {
  730. if (port->x_char) {
  731. port->x_char = 0;
  732. } else {
  733. port->x_char = START_CHAR(tty);
  734. sdio_uart_start_tx(port);
  735. }
  736. }
  737. if (C_CRTSCTS(tty))
  738. sdio_uart_set_mctrl(port, TIOCM_RTS);
  739. sdio_uart_irq(port->func);
  740. sdio_uart_release_func(port);
  741. }
  742. static void sdio_uart_set_termios(struct tty_struct *tty,
  743. struct ktermios *old_termios)
  744. {
  745. struct sdio_uart_port *port = tty->driver_data;
  746. unsigned int cflag = tty->termios.c_cflag;
  747. if (sdio_uart_claim_func(port) != 0)
  748. return;
  749. sdio_uart_change_speed(port, &tty->termios, old_termios);
  750. /* Handle transition to B0 status */
  751. if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
  752. sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
  753. /* Handle transition away from B0 status */
  754. if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
  755. unsigned int mask = TIOCM_DTR;
  756. if (!(cflag & CRTSCTS) || !tty_throttled(tty))
  757. mask |= TIOCM_RTS;
  758. sdio_uart_set_mctrl(port, mask);
  759. }
  760. /* Handle turning off CRTSCTS */
  761. if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
  762. tty->hw_stopped = 0;
  763. sdio_uart_start_tx(port);
  764. }
  765. /* Handle turning on CRTSCTS */
  766. if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
  767. if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
  768. tty->hw_stopped = 1;
  769. sdio_uart_stop_tx(port);
  770. }
  771. }
  772. sdio_uart_release_func(port);
  773. }
  774. static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
  775. {
  776. struct sdio_uart_port *port = tty->driver_data;
  777. int result;
  778. result = sdio_uart_claim_func(port);
  779. if (result != 0)
  780. return result;
  781. if (break_state == -1)
  782. port->lcr |= UART_LCR_SBC;
  783. else
  784. port->lcr &= ~UART_LCR_SBC;
  785. sdio_out(port, UART_LCR, port->lcr);
  786. sdio_uart_release_func(port);
  787. return 0;
  788. }
  789. static int sdio_uart_tiocmget(struct tty_struct *tty)
  790. {
  791. struct sdio_uart_port *port = tty->driver_data;
  792. int result;
  793. result = sdio_uart_claim_func(port);
  794. if (!result) {
  795. result = port->mctrl | sdio_uart_get_mctrl(port);
  796. sdio_uart_release_func(port);
  797. }
  798. return result;
  799. }
  800. static int sdio_uart_tiocmset(struct tty_struct *tty,
  801. unsigned int set, unsigned int clear)
  802. {
  803. struct sdio_uart_port *port = tty->driver_data;
  804. int result;
  805. result = sdio_uart_claim_func(port);
  806. if (!result) {
  807. sdio_uart_update_mctrl(port, set, clear);
  808. sdio_uart_release_func(port);
  809. }
  810. return result;
  811. }
  812. static int sdio_uart_proc_show(struct seq_file *m, void *v)
  813. {
  814. int i;
  815. seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
  816. "", "", "");
  817. for (i = 0; i < UART_NR; i++) {
  818. struct sdio_uart_port *port = sdio_uart_port_get(i);
  819. if (port) {
  820. seq_printf(m, "%d: uart:SDIO", i);
  821. if (capable(CAP_SYS_ADMIN)) {
  822. seq_printf(m, " tx:%d rx:%d",
  823. port->icount.tx, port->icount.rx);
  824. if (port->icount.frame)
  825. seq_printf(m, " fe:%d",
  826. port->icount.frame);
  827. if (port->icount.parity)
  828. seq_printf(m, " pe:%d",
  829. port->icount.parity);
  830. if (port->icount.brk)
  831. seq_printf(m, " brk:%d",
  832. port->icount.brk);
  833. if (port->icount.overrun)
  834. seq_printf(m, " oe:%d",
  835. port->icount.overrun);
  836. if (port->icount.cts)
  837. seq_printf(m, " cts:%d",
  838. port->icount.cts);
  839. if (port->icount.dsr)
  840. seq_printf(m, " dsr:%d",
  841. port->icount.dsr);
  842. if (port->icount.rng)
  843. seq_printf(m, " rng:%d",
  844. port->icount.rng);
  845. if (port->icount.dcd)
  846. seq_printf(m, " dcd:%d",
  847. port->icount.dcd);
  848. }
  849. sdio_uart_port_put(port);
  850. seq_putc(m, '\n');
  851. }
  852. }
  853. return 0;
  854. }
  855. static const struct tty_port_operations sdio_uart_port_ops = {
  856. .dtr_rts = uart_dtr_rts,
  857. .carrier_raised = uart_carrier_raised,
  858. .shutdown = sdio_uart_shutdown,
  859. .activate = sdio_uart_activate,
  860. .destruct = sdio_uart_port_destroy,
  861. };
  862. static const struct tty_operations sdio_uart_ops = {
  863. .open = sdio_uart_open,
  864. .close = sdio_uart_close,
  865. .write = sdio_uart_write,
  866. .write_room = sdio_uart_write_room,
  867. .chars_in_buffer = sdio_uart_chars_in_buffer,
  868. .send_xchar = sdio_uart_send_xchar,
  869. .throttle = sdio_uart_throttle,
  870. .unthrottle = sdio_uart_unthrottle,
  871. .set_termios = sdio_uart_set_termios,
  872. .hangup = sdio_uart_hangup,
  873. .break_ctl = sdio_uart_break_ctl,
  874. .tiocmget = sdio_uart_tiocmget,
  875. .tiocmset = sdio_uart_tiocmset,
  876. .install = sdio_uart_install,
  877. .cleanup = sdio_uart_cleanup,
  878. .proc_show = sdio_uart_proc_show,
  879. };
  880. static struct tty_driver *sdio_uart_tty_driver;
  881. static int sdio_uart_probe(struct sdio_func *func,
  882. const struct sdio_device_id *id)
  883. {
  884. struct sdio_uart_port *port;
  885. int ret;
  886. port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
  887. if (!port)
  888. return -ENOMEM;
  889. if (func->class == SDIO_CLASS_UART) {
  890. pr_warn("%s: need info on UART class basic setup\n",
  891. sdio_func_id(func));
  892. kfree(port);
  893. return -ENOSYS;
  894. } else if (func->class == SDIO_CLASS_GPS) {
  895. /*
  896. * We need tuple 0x91. It contains SUBTPL_SIOREG
  897. * and SUBTPL_RCVCAPS.
  898. */
  899. struct sdio_func_tuple *tpl;
  900. for (tpl = func->tuples; tpl; tpl = tpl->next) {
  901. if (tpl->code != 0x91)
  902. continue;
  903. if (tpl->size < 10)
  904. continue;
  905. if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
  906. break;
  907. }
  908. if (!tpl) {
  909. pr_warn("%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
  910. sdio_func_id(func));
  911. kfree(port);
  912. return -EINVAL;
  913. }
  914. pr_debug("%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
  915. sdio_func_id(func), tpl->data[2], tpl->data[3]);
  916. port->regs_offset = (tpl->data[4] << 0) |
  917. (tpl->data[5] << 8) |
  918. (tpl->data[6] << 16);
  919. pr_debug("%s: regs offset = 0x%x\n",
  920. sdio_func_id(func), port->regs_offset);
  921. port->uartclk = tpl->data[7] * 115200;
  922. if (port->uartclk == 0)
  923. port->uartclk = 115200;
  924. pr_debug("%s: clk %d baudcode %u 4800-div %u\n",
  925. sdio_func_id(func), port->uartclk,
  926. tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
  927. } else {
  928. kfree(port);
  929. return -EINVAL;
  930. }
  931. port->func = func;
  932. sdio_set_drvdata(func, port);
  933. tty_port_init(&port->port);
  934. port->port.ops = &sdio_uart_port_ops;
  935. ret = sdio_uart_add_port(port);
  936. if (ret) {
  937. kfree(port);
  938. } else {
  939. struct device *dev;
  940. dev = tty_port_register_device(&port->port,
  941. sdio_uart_tty_driver, port->index, &func->dev);
  942. if (IS_ERR(dev)) {
  943. sdio_uart_port_remove(port);
  944. ret = PTR_ERR(dev);
  945. }
  946. }
  947. return ret;
  948. }
  949. static void sdio_uart_remove(struct sdio_func *func)
  950. {
  951. struct sdio_uart_port *port = sdio_get_drvdata(func);
  952. tty_unregister_device(sdio_uart_tty_driver, port->index);
  953. sdio_uart_port_remove(port);
  954. }
  955. static const struct sdio_device_id sdio_uart_ids[] = {
  956. { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
  957. { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
  958. { /* end: all zeroes */ },
  959. };
  960. MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
  961. static struct sdio_driver sdio_uart_driver = {
  962. .probe = sdio_uart_probe,
  963. .remove = sdio_uart_remove,
  964. .name = "sdio_uart",
  965. .id_table = sdio_uart_ids,
  966. };
  967. static int __init sdio_uart_init(void)
  968. {
  969. int ret;
  970. struct tty_driver *tty_drv;
  971. sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
  972. if (!tty_drv)
  973. return -ENOMEM;
  974. tty_drv->driver_name = "sdio_uart";
  975. tty_drv->name = "ttySDIO";
  976. tty_drv->major = 0; /* dynamically allocated */
  977. tty_drv->minor_start = 0;
  978. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  979. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  980. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  981. tty_drv->init_termios = tty_std_termios;
  982. tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
  983. tty_drv->init_termios.c_ispeed = 4800;
  984. tty_drv->init_termios.c_ospeed = 4800;
  985. tty_set_operations(tty_drv, &sdio_uart_ops);
  986. ret = tty_register_driver(tty_drv);
  987. if (ret)
  988. goto err1;
  989. ret = sdio_register_driver(&sdio_uart_driver);
  990. if (ret)
  991. goto err2;
  992. return 0;
  993. err2:
  994. tty_unregister_driver(tty_drv);
  995. err1:
  996. put_tty_driver(tty_drv);
  997. return ret;
  998. }
  999. static void __exit sdio_uart_exit(void)
  1000. {
  1001. sdio_unregister_driver(&sdio_uart_driver);
  1002. tty_unregister_driver(sdio_uart_tty_driver);
  1003. put_tty_driver(sdio_uart_tty_driver);
  1004. }
  1005. module_init(sdio_uart_init);
  1006. module_exit(sdio_uart_exit);
  1007. MODULE_AUTHOR("Nicolas Pitre");
  1008. MODULE_LICENSE("GPL");