ucc_uart.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. /*
  2. * Freescale QUICC Engine UART device driver
  3. *
  4. * Author: Timur Tabi <timur@freescale.com>
  5. *
  6. * Copyright 2007 Freescale Semiconductor, Inc. This file is licensed under
  7. * the terms of the GNU General Public License version 2. This program
  8. * is licensed "as is" without any warranty of any kind, whether express
  9. * or implied.
  10. *
  11. * This driver adds support for UART devices via Freescale's QUICC Engine
  12. * found on some Freescale SOCs.
  13. *
  14. * If Soft-UART support is needed but not already present, then this driver
  15. * will request and upload the "Soft-UART" microcode upon probe. The
  16. * filename of the microcode should be fsl_qe_ucode_uart_X_YZ.bin, where "X"
  17. * is the name of the SOC (e.g. 8323), and YZ is the revision of the SOC,
  18. * (e.g. "11" for 1.1).
  19. */
  20. #include <linux/module.h>
  21. #include <linux/serial.h>
  22. #include <linux/serial_core.h>
  23. #include <linux/slab.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_flip.h>
  26. #include <linux/io.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/fs_uart_pd.h>
  30. #include <asm/ucc_slow.h>
  31. #include <linux/firmware.h>
  32. #include <asm/reg.h>
  33. /*
  34. * The GUMR flag for Soft UART. This would normally be defined in qe.h,
  35. * but Soft-UART is a hack and we want to keep everything related to it in
  36. * this file.
  37. */
  38. #define UCC_SLOW_GUMR_H_SUART 0x00004000 /* Soft-UART */
  39. /*
  40. * soft_uart is 1 if we need to use Soft-UART mode
  41. */
  42. static int soft_uart;
  43. /*
  44. * firmware_loaded is 1 if the firmware has been loaded, 0 otherwise.
  45. */
  46. static int firmware_loaded;
  47. /* Enable this macro to configure all serial ports in internal loopback
  48. mode */
  49. /* #define LOOPBACK */
  50. /* The major and minor device numbers are defined in
  51. * http://www.lanana.org/docs/device-list/devices-2.6+.txt. For the QE
  52. * UART, we have major number 204 and minor numbers 46 - 49, which are the
  53. * same as for the CPM2. This decision was made because no Freescale part
  54. * has both a CPM and a QE.
  55. */
  56. #define SERIAL_QE_MAJOR 204
  57. #define SERIAL_QE_MINOR 46
  58. /* Since we only have minor numbers 46 - 49, there is a hard limit of 4 ports */
  59. #define UCC_MAX_UART 4
  60. /* The number of buffer descriptors for receiving characters. */
  61. #define RX_NUM_FIFO 4
  62. /* The number of buffer descriptors for transmitting characters. */
  63. #define TX_NUM_FIFO 4
  64. /* The maximum size of the character buffer for a single RX BD. */
  65. #define RX_BUF_SIZE 32
  66. /* The maximum size of the character buffer for a single TX BD. */
  67. #define TX_BUF_SIZE 32
  68. /*
  69. * The number of jiffies to wait after receiving a close command before the
  70. * device is actually closed. This allows the last few characters to be
  71. * sent over the wire.
  72. */
  73. #define UCC_WAIT_CLOSING 100
  74. struct ucc_uart_pram {
  75. struct ucc_slow_pram common;
  76. u8 res1[8]; /* reserved */
  77. __be16 maxidl; /* Maximum idle chars */
  78. __be16 idlc; /* temp idle counter */
  79. __be16 brkcr; /* Break count register */
  80. __be16 parec; /* receive parity error counter */
  81. __be16 frmec; /* receive framing error counter */
  82. __be16 nosec; /* receive noise counter */
  83. __be16 brkec; /* receive break condition counter */
  84. __be16 brkln; /* last received break length */
  85. __be16 uaddr[2]; /* UART address character 1 & 2 */
  86. __be16 rtemp; /* Temp storage */
  87. __be16 toseq; /* Transmit out of sequence char */
  88. __be16 cchars[8]; /* control characters 1-8 */
  89. __be16 rccm; /* receive control character mask */
  90. __be16 rccr; /* receive control character register */
  91. __be16 rlbc; /* receive last break character */
  92. __be16 res2; /* reserved */
  93. __be32 res3; /* reserved, should be cleared */
  94. u8 res4; /* reserved, should be cleared */
  95. u8 res5[3]; /* reserved, should be cleared */
  96. __be32 res6; /* reserved, should be cleared */
  97. __be32 res7; /* reserved, should be cleared */
  98. __be32 res8; /* reserved, should be cleared */
  99. __be32 res9; /* reserved, should be cleared */
  100. __be32 res10; /* reserved, should be cleared */
  101. __be32 res11; /* reserved, should be cleared */
  102. __be32 res12; /* reserved, should be cleared */
  103. __be32 res13; /* reserved, should be cleared */
  104. /* The rest is for Soft-UART only */
  105. __be16 supsmr; /* 0x90, Shadow UPSMR */
  106. __be16 res92; /* 0x92, reserved, initialize to 0 */
  107. __be32 rx_state; /* 0x94, RX state, initialize to 0 */
  108. __be32 rx_cnt; /* 0x98, RX count, initialize to 0 */
  109. u8 rx_length; /* 0x9C, Char length, set to 1+CL+PEN+1+SL */
  110. u8 rx_bitmark; /* 0x9D, reserved, initialize to 0 */
  111. u8 rx_temp_dlst_qe; /* 0x9E, reserved, initialize to 0 */
  112. u8 res14[0xBC - 0x9F]; /* reserved */
  113. __be32 dump_ptr; /* 0xBC, Dump pointer */
  114. __be32 rx_frame_rem; /* 0xC0, reserved, initialize to 0 */
  115. u8 rx_frame_rem_size; /* 0xC4, reserved, initialize to 0 */
  116. u8 tx_mode; /* 0xC5, mode, 0=AHDLC, 1=UART */
  117. __be16 tx_state; /* 0xC6, TX state */
  118. u8 res15[0xD0 - 0xC8]; /* reserved */
  119. __be32 resD0; /* 0xD0, reserved, initialize to 0 */
  120. u8 resD4; /* 0xD4, reserved, initialize to 0 */
  121. __be16 resD5; /* 0xD5, reserved, initialize to 0 */
  122. } __attribute__ ((packed));
  123. /* SUPSMR definitions, for Soft-UART only */
  124. #define UCC_UART_SUPSMR_SL 0x8000
  125. #define UCC_UART_SUPSMR_RPM_MASK 0x6000
  126. #define UCC_UART_SUPSMR_RPM_ODD 0x0000
  127. #define UCC_UART_SUPSMR_RPM_LOW 0x2000
  128. #define UCC_UART_SUPSMR_RPM_EVEN 0x4000
  129. #define UCC_UART_SUPSMR_RPM_HIGH 0x6000
  130. #define UCC_UART_SUPSMR_PEN 0x1000
  131. #define UCC_UART_SUPSMR_TPM_MASK 0x0C00
  132. #define UCC_UART_SUPSMR_TPM_ODD 0x0000
  133. #define UCC_UART_SUPSMR_TPM_LOW 0x0400
  134. #define UCC_UART_SUPSMR_TPM_EVEN 0x0800
  135. #define UCC_UART_SUPSMR_TPM_HIGH 0x0C00
  136. #define UCC_UART_SUPSMR_FRZ 0x0100
  137. #define UCC_UART_SUPSMR_UM_MASK 0x00c0
  138. #define UCC_UART_SUPSMR_UM_NORMAL 0x0000
  139. #define UCC_UART_SUPSMR_UM_MAN_MULTI 0x0040
  140. #define UCC_UART_SUPSMR_UM_AUTO_MULTI 0x00c0
  141. #define UCC_UART_SUPSMR_CL_MASK 0x0030
  142. #define UCC_UART_SUPSMR_CL_8 0x0030
  143. #define UCC_UART_SUPSMR_CL_7 0x0020
  144. #define UCC_UART_SUPSMR_CL_6 0x0010
  145. #define UCC_UART_SUPSMR_CL_5 0x0000
  146. #define UCC_UART_TX_STATE_AHDLC 0x00
  147. #define UCC_UART_TX_STATE_UART 0x01
  148. #define UCC_UART_TX_STATE_X1 0x00
  149. #define UCC_UART_TX_STATE_X16 0x80
  150. #define UCC_UART_PRAM_ALIGNMENT 0x100
  151. #define UCC_UART_SIZE_OF_BD UCC_SLOW_SIZE_OF_BD
  152. #define NUM_CONTROL_CHARS 8
  153. /* Private per-port data structure */
  154. struct uart_qe_port {
  155. struct uart_port port;
  156. struct ucc_slow __iomem *uccp;
  157. struct ucc_uart_pram __iomem *uccup;
  158. struct ucc_slow_info us_info;
  159. struct ucc_slow_private *us_private;
  160. struct device_node *np;
  161. unsigned int ucc_num; /* First ucc is 0, not 1 */
  162. u16 rx_nrfifos;
  163. u16 rx_fifosize;
  164. u16 tx_nrfifos;
  165. u16 tx_fifosize;
  166. int wait_closing;
  167. u32 flags;
  168. struct qe_bd *rx_bd_base;
  169. struct qe_bd *rx_cur;
  170. struct qe_bd *tx_bd_base;
  171. struct qe_bd *tx_cur;
  172. unsigned char *tx_buf;
  173. unsigned char *rx_buf;
  174. void *bd_virt; /* virtual address of the BD buffers */
  175. dma_addr_t bd_dma_addr; /* bus address of the BD buffers */
  176. unsigned int bd_size; /* size of BD buffer space */
  177. };
  178. static struct uart_driver ucc_uart_driver = {
  179. .owner = THIS_MODULE,
  180. .driver_name = "ucc_uart",
  181. .dev_name = "ttyQE",
  182. .major = SERIAL_QE_MAJOR,
  183. .minor = SERIAL_QE_MINOR,
  184. .nr = UCC_MAX_UART,
  185. };
  186. /*
  187. * Virtual to physical address translation.
  188. *
  189. * Given the virtual address for a character buffer, this function returns
  190. * the physical (DMA) equivalent.
  191. */
  192. static inline dma_addr_t cpu2qe_addr(void *addr, struct uart_qe_port *qe_port)
  193. {
  194. if (likely((addr >= qe_port->bd_virt)) &&
  195. (addr < (qe_port->bd_virt + qe_port->bd_size)))
  196. return qe_port->bd_dma_addr + (addr - qe_port->bd_virt);
  197. /* something nasty happened */
  198. printk(KERN_ERR "%s: addr=%p\n", __func__, addr);
  199. BUG();
  200. return 0;
  201. }
  202. /*
  203. * Physical to virtual address translation.
  204. *
  205. * Given the physical (DMA) address for a character buffer, this function
  206. * returns the virtual equivalent.
  207. */
  208. static inline void *qe2cpu_addr(dma_addr_t addr, struct uart_qe_port *qe_port)
  209. {
  210. /* sanity check */
  211. if (likely((addr >= qe_port->bd_dma_addr) &&
  212. (addr < (qe_port->bd_dma_addr + qe_port->bd_size))))
  213. return qe_port->bd_virt + (addr - qe_port->bd_dma_addr);
  214. /* something nasty happened */
  215. printk(KERN_ERR "%s: addr=%llx\n", __func__, (u64)addr);
  216. BUG();
  217. return NULL;
  218. }
  219. /*
  220. * Return 1 if the QE is done transmitting all buffers for this port
  221. *
  222. * This function scans each BD in sequence. If we find a BD that is not
  223. * ready (READY=1), then we return 0 indicating that the QE is still sending
  224. * data. If we reach the last BD (WRAP=1), then we know we've scanned
  225. * the entire list, and all BDs are done.
  226. */
  227. static unsigned int qe_uart_tx_empty(struct uart_port *port)
  228. {
  229. struct uart_qe_port *qe_port =
  230. container_of(port, struct uart_qe_port, port);
  231. struct qe_bd *bdp = qe_port->tx_bd_base;
  232. while (1) {
  233. if (in_be16(&bdp->status) & BD_SC_READY)
  234. /* This BD is not done, so return "not done" */
  235. return 0;
  236. if (in_be16(&bdp->status) & BD_SC_WRAP)
  237. /*
  238. * This BD is done and it's the last one, so return
  239. * "done"
  240. */
  241. return 1;
  242. bdp++;
  243. };
  244. }
  245. /*
  246. * Set the modem control lines
  247. *
  248. * Although the QE can control the modem control lines (e.g. CTS), we
  249. * don't need that support. This function must exist, however, otherwise
  250. * the kernel will panic.
  251. */
  252. void qe_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  253. {
  254. }
  255. /*
  256. * Get the current modem control line status
  257. *
  258. * Although the QE can control the modem control lines (e.g. CTS), this
  259. * driver currently doesn't support that, so we always return Carrier
  260. * Detect, Data Set Ready, and Clear To Send.
  261. */
  262. static unsigned int qe_uart_get_mctrl(struct uart_port *port)
  263. {
  264. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  265. }
  266. /*
  267. * Disable the transmit interrupt.
  268. *
  269. * Although this function is called "stop_tx", it does not actually stop
  270. * transmission of data. Instead, it tells the QE to not generate an
  271. * interrupt when the UCC is finished sending characters.
  272. */
  273. static void qe_uart_stop_tx(struct uart_port *port)
  274. {
  275. struct uart_qe_port *qe_port =
  276. container_of(port, struct uart_qe_port, port);
  277. clrbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
  278. }
  279. /*
  280. * Transmit as many characters to the HW as possible.
  281. *
  282. * This function will attempt to stuff of all the characters from the
  283. * kernel's transmit buffer into TX BDs.
  284. *
  285. * A return value of non-zero indicates that it successfully stuffed all
  286. * characters from the kernel buffer.
  287. *
  288. * A return value of zero indicates that there are still characters in the
  289. * kernel's buffer that have not been transmitted, but there are no more BDs
  290. * available. This function should be called again after a BD has been made
  291. * available.
  292. */
  293. static int qe_uart_tx_pump(struct uart_qe_port *qe_port)
  294. {
  295. struct qe_bd *bdp;
  296. unsigned char *p;
  297. unsigned int count;
  298. struct uart_port *port = &qe_port->port;
  299. struct circ_buf *xmit = &port->state->xmit;
  300. bdp = qe_port->rx_cur;
  301. /* Handle xon/xoff */
  302. if (port->x_char) {
  303. /* Pick next descriptor and fill from buffer */
  304. bdp = qe_port->tx_cur;
  305. p = qe2cpu_addr(bdp->buf, qe_port);
  306. *p++ = port->x_char;
  307. out_be16(&bdp->length, 1);
  308. setbits16(&bdp->status, BD_SC_READY);
  309. /* Get next BD. */
  310. if (in_be16(&bdp->status) & BD_SC_WRAP)
  311. bdp = qe_port->tx_bd_base;
  312. else
  313. bdp++;
  314. qe_port->tx_cur = bdp;
  315. port->icount.tx++;
  316. port->x_char = 0;
  317. return 1;
  318. }
  319. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  320. qe_uart_stop_tx(port);
  321. return 0;
  322. }
  323. /* Pick next descriptor and fill from buffer */
  324. bdp = qe_port->tx_cur;
  325. while (!(in_be16(&bdp->status) & BD_SC_READY) &&
  326. (xmit->tail != xmit->head)) {
  327. count = 0;
  328. p = qe2cpu_addr(bdp->buf, qe_port);
  329. while (count < qe_port->tx_fifosize) {
  330. *p++ = xmit->buf[xmit->tail];
  331. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  332. port->icount.tx++;
  333. count++;
  334. if (xmit->head == xmit->tail)
  335. break;
  336. }
  337. out_be16(&bdp->length, count);
  338. setbits16(&bdp->status, BD_SC_READY);
  339. /* Get next BD. */
  340. if (in_be16(&bdp->status) & BD_SC_WRAP)
  341. bdp = qe_port->tx_bd_base;
  342. else
  343. bdp++;
  344. }
  345. qe_port->tx_cur = bdp;
  346. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  347. uart_write_wakeup(port);
  348. if (uart_circ_empty(xmit)) {
  349. /* The kernel buffer is empty, so turn off TX interrupts. We
  350. don't need to be told when the QE is finished transmitting
  351. the data. */
  352. qe_uart_stop_tx(port);
  353. return 0;
  354. }
  355. return 1;
  356. }
  357. /*
  358. * Start transmitting data
  359. *
  360. * This function will start transmitting any available data, if the port
  361. * isn't already transmitting data.
  362. */
  363. static void qe_uart_start_tx(struct uart_port *port)
  364. {
  365. struct uart_qe_port *qe_port =
  366. container_of(port, struct uart_qe_port, port);
  367. /* If we currently are transmitting, then just return */
  368. if (in_be16(&qe_port->uccp->uccm) & UCC_UART_UCCE_TX)
  369. return;
  370. /* Otherwise, pump the port and start transmission */
  371. if (qe_uart_tx_pump(qe_port))
  372. setbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
  373. }
  374. /*
  375. * Stop transmitting data
  376. */
  377. static void qe_uart_stop_rx(struct uart_port *port)
  378. {
  379. struct uart_qe_port *qe_port =
  380. container_of(port, struct uart_qe_port, port);
  381. clrbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
  382. }
  383. /*
  384. * Enable status change interrupts
  385. *
  386. * We don't support status change interrupts, but we need to define this
  387. * function otherwise the kernel will panic.
  388. */
  389. static void qe_uart_enable_ms(struct uart_port *port)
  390. {
  391. }
  392. /* Start or stop sending break signal
  393. *
  394. * This function controls the sending of a break signal. If break_state=1,
  395. * then we start sending a break signal. If break_state=0, then we stop
  396. * sending the break signal.
  397. */
  398. static void qe_uart_break_ctl(struct uart_port *port, int break_state)
  399. {
  400. struct uart_qe_port *qe_port =
  401. container_of(port, struct uart_qe_port, port);
  402. if (break_state)
  403. ucc_slow_stop_tx(qe_port->us_private);
  404. else
  405. ucc_slow_restart_tx(qe_port->us_private);
  406. }
  407. /* ISR helper function for receiving character.
  408. *
  409. * This function is called by the ISR to handling receiving characters
  410. */
  411. static void qe_uart_int_rx(struct uart_qe_port *qe_port)
  412. {
  413. int i;
  414. unsigned char ch, *cp;
  415. struct uart_port *port = &qe_port->port;
  416. struct tty_struct *tty = port->state->port.tty;
  417. struct qe_bd *bdp;
  418. u16 status;
  419. unsigned int flg;
  420. /* Just loop through the closed BDs and copy the characters into
  421. * the buffer.
  422. */
  423. bdp = qe_port->rx_cur;
  424. while (1) {
  425. status = in_be16(&bdp->status);
  426. /* If this one is empty, then we assume we've read them all */
  427. if (status & BD_SC_EMPTY)
  428. break;
  429. /* get number of characters, and check space in RX buffer */
  430. i = in_be16(&bdp->length);
  431. /* If we don't have enough room in RX buffer for the entire BD,
  432. * then we try later, which will be the next RX interrupt.
  433. */
  434. if (tty_buffer_request_room(tty, i) < i) {
  435. dev_dbg(port->dev, "ucc-uart: no room in RX buffer\n");
  436. return;
  437. }
  438. /* get pointer */
  439. cp = qe2cpu_addr(bdp->buf, qe_port);
  440. /* loop through the buffer */
  441. while (i-- > 0) {
  442. ch = *cp++;
  443. port->icount.rx++;
  444. flg = TTY_NORMAL;
  445. if (!i && status &
  446. (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
  447. goto handle_error;
  448. if (uart_handle_sysrq_char(port, ch))
  449. continue;
  450. error_return:
  451. tty_insert_flip_char(tty, ch, flg);
  452. }
  453. /* This BD is ready to be used again. Clear status. get next */
  454. clrsetbits_be16(&bdp->status, BD_SC_BR | BD_SC_FR | BD_SC_PR |
  455. BD_SC_OV | BD_SC_ID, BD_SC_EMPTY);
  456. if (in_be16(&bdp->status) & BD_SC_WRAP)
  457. bdp = qe_port->rx_bd_base;
  458. else
  459. bdp++;
  460. }
  461. /* Write back buffer pointer */
  462. qe_port->rx_cur = bdp;
  463. /* Activate BH processing */
  464. tty_flip_buffer_push(tty);
  465. return;
  466. /* Error processing */
  467. handle_error:
  468. /* Statistics */
  469. if (status & BD_SC_BR)
  470. port->icount.brk++;
  471. if (status & BD_SC_PR)
  472. port->icount.parity++;
  473. if (status & BD_SC_FR)
  474. port->icount.frame++;
  475. if (status & BD_SC_OV)
  476. port->icount.overrun++;
  477. /* Mask out ignored conditions */
  478. status &= port->read_status_mask;
  479. /* Handle the remaining ones */
  480. if (status & BD_SC_BR)
  481. flg = TTY_BREAK;
  482. else if (status & BD_SC_PR)
  483. flg = TTY_PARITY;
  484. else if (status & BD_SC_FR)
  485. flg = TTY_FRAME;
  486. /* Overrun does not affect the current character ! */
  487. if (status & BD_SC_OV)
  488. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  489. #ifdef SUPPORT_SYSRQ
  490. port->sysrq = 0;
  491. #endif
  492. goto error_return;
  493. }
  494. /* Interrupt handler
  495. *
  496. * This interrupt handler is called after a BD is processed.
  497. */
  498. static irqreturn_t qe_uart_int(int irq, void *data)
  499. {
  500. struct uart_qe_port *qe_port = (struct uart_qe_port *) data;
  501. struct ucc_slow __iomem *uccp = qe_port->uccp;
  502. u16 events;
  503. /* Clear the interrupts */
  504. events = in_be16(&uccp->ucce);
  505. out_be16(&uccp->ucce, events);
  506. if (events & UCC_UART_UCCE_BRKE)
  507. uart_handle_break(&qe_port->port);
  508. if (events & UCC_UART_UCCE_RX)
  509. qe_uart_int_rx(qe_port);
  510. if (events & UCC_UART_UCCE_TX)
  511. qe_uart_tx_pump(qe_port);
  512. return events ? IRQ_HANDLED : IRQ_NONE;
  513. }
  514. /* Initialize buffer descriptors
  515. *
  516. * This function initializes all of the RX and TX buffer descriptors.
  517. */
  518. static void qe_uart_initbd(struct uart_qe_port *qe_port)
  519. {
  520. int i;
  521. void *bd_virt;
  522. struct qe_bd *bdp;
  523. /* Set the physical address of the host memory buffers in the buffer
  524. * descriptors, and the virtual address for us to work with.
  525. */
  526. bd_virt = qe_port->bd_virt;
  527. bdp = qe_port->rx_bd_base;
  528. qe_port->rx_cur = qe_port->rx_bd_base;
  529. for (i = 0; i < (qe_port->rx_nrfifos - 1); i++) {
  530. out_be16(&bdp->status, BD_SC_EMPTY | BD_SC_INTRPT);
  531. out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
  532. out_be16(&bdp->length, 0);
  533. bd_virt += qe_port->rx_fifosize;
  534. bdp++;
  535. }
  536. /* */
  537. out_be16(&bdp->status, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
  538. out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
  539. out_be16(&bdp->length, 0);
  540. /* Set the physical address of the host memory
  541. * buffers in the buffer descriptors, and the
  542. * virtual address for us to work with.
  543. */
  544. bd_virt = qe_port->bd_virt +
  545. L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
  546. qe_port->tx_cur = qe_port->tx_bd_base;
  547. bdp = qe_port->tx_bd_base;
  548. for (i = 0; i < (qe_port->tx_nrfifos - 1); i++) {
  549. out_be16(&bdp->status, BD_SC_INTRPT);
  550. out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
  551. out_be16(&bdp->length, 0);
  552. bd_virt += qe_port->tx_fifosize;
  553. bdp++;
  554. }
  555. /* Loopback requires the preamble bit to be set on the first TX BD */
  556. #ifdef LOOPBACK
  557. setbits16(&qe_port->tx_cur->status, BD_SC_P);
  558. #endif
  559. out_be16(&bdp->status, BD_SC_WRAP | BD_SC_INTRPT);
  560. out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
  561. out_be16(&bdp->length, 0);
  562. }
  563. /*
  564. * Initialize a UCC for UART.
  565. *
  566. * This function configures a given UCC to be used as a UART device. Basic
  567. * UCC initialization is handled in qe_uart_request_port(). This function
  568. * does all the UART-specific stuff.
  569. */
  570. static void qe_uart_init_ucc(struct uart_qe_port *qe_port)
  571. {
  572. u32 cecr_subblock;
  573. struct ucc_slow __iomem *uccp = qe_port->uccp;
  574. struct ucc_uart_pram *uccup = qe_port->uccup;
  575. unsigned int i;
  576. /* First, disable TX and RX in the UCC */
  577. ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
  578. /* Program the UCC UART parameter RAM */
  579. out_8(&uccup->common.rbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
  580. out_8(&uccup->common.tbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
  581. out_be16(&uccup->common.mrblr, qe_port->rx_fifosize);
  582. out_be16(&uccup->maxidl, 0x10);
  583. out_be16(&uccup->brkcr, 1);
  584. out_be16(&uccup->parec, 0);
  585. out_be16(&uccup->frmec, 0);
  586. out_be16(&uccup->nosec, 0);
  587. out_be16(&uccup->brkec, 0);
  588. out_be16(&uccup->uaddr[0], 0);
  589. out_be16(&uccup->uaddr[1], 0);
  590. out_be16(&uccup->toseq, 0);
  591. for (i = 0; i < 8; i++)
  592. out_be16(&uccup->cchars[i], 0xC000);
  593. out_be16(&uccup->rccm, 0xc0ff);
  594. /* Configure the GUMR registers for UART */
  595. if (soft_uart) {
  596. /* Soft-UART requires a 1X multiplier for TX */
  597. clrsetbits_be32(&uccp->gumr_l,
  598. UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
  599. UCC_SLOW_GUMR_L_RDCR_MASK,
  600. UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_1 |
  601. UCC_SLOW_GUMR_L_RDCR_16);
  602. clrsetbits_be32(&uccp->gumr_h, UCC_SLOW_GUMR_H_RFW,
  603. UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX);
  604. } else {
  605. clrsetbits_be32(&uccp->gumr_l,
  606. UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
  607. UCC_SLOW_GUMR_L_RDCR_MASK,
  608. UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_16 |
  609. UCC_SLOW_GUMR_L_RDCR_16);
  610. clrsetbits_be32(&uccp->gumr_h,
  611. UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX,
  612. UCC_SLOW_GUMR_H_RFW);
  613. }
  614. #ifdef LOOPBACK
  615. clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
  616. UCC_SLOW_GUMR_L_DIAG_LOOP);
  617. clrsetbits_be32(&uccp->gumr_h,
  618. UCC_SLOW_GUMR_H_CTSP | UCC_SLOW_GUMR_H_RSYN,
  619. UCC_SLOW_GUMR_H_CDS);
  620. #endif
  621. /* Disable rx interrupts and clear all pending events. */
  622. out_be16(&uccp->uccm, 0);
  623. out_be16(&uccp->ucce, 0xffff);
  624. out_be16(&uccp->udsr, 0x7e7e);
  625. /* Initialize UPSMR */
  626. out_be16(&uccp->upsmr, 0);
  627. if (soft_uart) {
  628. out_be16(&uccup->supsmr, 0x30);
  629. out_be16(&uccup->res92, 0);
  630. out_be32(&uccup->rx_state, 0);
  631. out_be32(&uccup->rx_cnt, 0);
  632. out_8(&uccup->rx_bitmark, 0);
  633. out_8(&uccup->rx_length, 10);
  634. out_be32(&uccup->dump_ptr, 0x4000);
  635. out_8(&uccup->rx_temp_dlst_qe, 0);
  636. out_be32(&uccup->rx_frame_rem, 0);
  637. out_8(&uccup->rx_frame_rem_size, 0);
  638. /* Soft-UART requires TX to be 1X */
  639. out_8(&uccup->tx_mode,
  640. UCC_UART_TX_STATE_UART | UCC_UART_TX_STATE_X1);
  641. out_be16(&uccup->tx_state, 0);
  642. out_8(&uccup->resD4, 0);
  643. out_be16(&uccup->resD5, 0);
  644. /* Set UART mode.
  645. * Enable receive and transmit.
  646. */
  647. /* From the microcode errata:
  648. * 1.GUMR_L register, set mode=0010 (QMC).
  649. * 2.Set GUMR_H[17] bit. (UART/AHDLC mode).
  650. * 3.Set GUMR_H[19:20] (Transparent mode)
  651. * 4.Clear GUMR_H[26] (RFW)
  652. * ...
  653. * 6.Receiver must use 16x over sampling
  654. */
  655. clrsetbits_be32(&uccp->gumr_l,
  656. UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
  657. UCC_SLOW_GUMR_L_RDCR_MASK,
  658. UCC_SLOW_GUMR_L_MODE_QMC | UCC_SLOW_GUMR_L_TDCR_16 |
  659. UCC_SLOW_GUMR_L_RDCR_16);
  660. clrsetbits_be32(&uccp->gumr_h,
  661. UCC_SLOW_GUMR_H_RFW | UCC_SLOW_GUMR_H_RSYN,
  662. UCC_SLOW_GUMR_H_SUART | UCC_SLOW_GUMR_H_TRX |
  663. UCC_SLOW_GUMR_H_TTX | UCC_SLOW_GUMR_H_TFL);
  664. #ifdef LOOPBACK
  665. clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
  666. UCC_SLOW_GUMR_L_DIAG_LOOP);
  667. clrbits32(&uccp->gumr_h, UCC_SLOW_GUMR_H_CTSP |
  668. UCC_SLOW_GUMR_H_CDS);
  669. #endif
  670. cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
  671. qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
  672. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  673. } else {
  674. cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
  675. qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
  676. QE_CR_PROTOCOL_UART, 0);
  677. }
  678. }
  679. /*
  680. * Initialize the port.
  681. */
  682. static int qe_uart_startup(struct uart_port *port)
  683. {
  684. struct uart_qe_port *qe_port =
  685. container_of(port, struct uart_qe_port, port);
  686. int ret;
  687. /*
  688. * If we're using Soft-UART mode, then we need to make sure the
  689. * firmware has been uploaded first.
  690. */
  691. if (soft_uart && !firmware_loaded) {
  692. dev_err(port->dev, "Soft-UART firmware not uploaded\n");
  693. return -ENODEV;
  694. }
  695. qe_uart_initbd(qe_port);
  696. qe_uart_init_ucc(qe_port);
  697. /* Install interrupt handler. */
  698. ret = request_irq(port->irq, qe_uart_int, IRQF_SHARED, "ucc-uart",
  699. qe_port);
  700. if (ret) {
  701. dev_err(port->dev, "could not claim IRQ %u\n", port->irq);
  702. return ret;
  703. }
  704. /* Startup rx-int */
  705. setbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
  706. ucc_slow_enable(qe_port->us_private, COMM_DIR_RX_AND_TX);
  707. return 0;
  708. }
  709. /*
  710. * Shutdown the port.
  711. */
  712. static void qe_uart_shutdown(struct uart_port *port)
  713. {
  714. struct uart_qe_port *qe_port =
  715. container_of(port, struct uart_qe_port, port);
  716. struct ucc_slow __iomem *uccp = qe_port->uccp;
  717. unsigned int timeout = 20;
  718. /* Disable RX and TX */
  719. /* Wait for all the BDs marked sent */
  720. while (!qe_uart_tx_empty(port)) {
  721. if (!--timeout) {
  722. dev_warn(port->dev, "shutdown timeout\n");
  723. break;
  724. }
  725. set_current_state(TASK_UNINTERRUPTIBLE);
  726. schedule_timeout(2);
  727. }
  728. if (qe_port->wait_closing) {
  729. /* Wait a bit longer */
  730. set_current_state(TASK_UNINTERRUPTIBLE);
  731. schedule_timeout(qe_port->wait_closing);
  732. }
  733. /* Stop uarts */
  734. ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
  735. clrbits16(&uccp->uccm, UCC_UART_UCCE_TX | UCC_UART_UCCE_RX);
  736. /* Shut them really down and reinit buffer descriptors */
  737. ucc_slow_graceful_stop_tx(qe_port->us_private);
  738. qe_uart_initbd(qe_port);
  739. free_irq(port->irq, qe_port);
  740. }
  741. /*
  742. * Set the serial port parameters.
  743. */
  744. static void qe_uart_set_termios(struct uart_port *port,
  745. struct ktermios *termios, struct ktermios *old)
  746. {
  747. struct uart_qe_port *qe_port =
  748. container_of(port, struct uart_qe_port, port);
  749. struct ucc_slow __iomem *uccp = qe_port->uccp;
  750. unsigned int baud;
  751. unsigned long flags;
  752. u16 upsmr = in_be16(&uccp->upsmr);
  753. struct ucc_uart_pram __iomem *uccup = qe_port->uccup;
  754. u16 supsmr = in_be16(&uccup->supsmr);
  755. u8 char_length = 2; /* 1 + CL + PEN + 1 + SL */
  756. /* Character length programmed into the mode register is the
  757. * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
  758. * 1 or 2 stop bits, minus 1.
  759. * The value 'bits' counts this for us.
  760. */
  761. /* byte size */
  762. upsmr &= UCC_UART_UPSMR_CL_MASK;
  763. supsmr &= UCC_UART_SUPSMR_CL_MASK;
  764. switch (termios->c_cflag & CSIZE) {
  765. case CS5:
  766. upsmr |= UCC_UART_UPSMR_CL_5;
  767. supsmr |= UCC_UART_SUPSMR_CL_5;
  768. char_length += 5;
  769. break;
  770. case CS6:
  771. upsmr |= UCC_UART_UPSMR_CL_6;
  772. supsmr |= UCC_UART_SUPSMR_CL_6;
  773. char_length += 6;
  774. break;
  775. case CS7:
  776. upsmr |= UCC_UART_UPSMR_CL_7;
  777. supsmr |= UCC_UART_SUPSMR_CL_7;
  778. char_length += 7;
  779. break;
  780. default: /* case CS8 */
  781. upsmr |= UCC_UART_UPSMR_CL_8;
  782. supsmr |= UCC_UART_SUPSMR_CL_8;
  783. char_length += 8;
  784. break;
  785. }
  786. /* If CSTOPB is set, we want two stop bits */
  787. if (termios->c_cflag & CSTOPB) {
  788. upsmr |= UCC_UART_UPSMR_SL;
  789. supsmr |= UCC_UART_SUPSMR_SL;
  790. char_length++; /* + SL */
  791. }
  792. if (termios->c_cflag & PARENB) {
  793. upsmr |= UCC_UART_UPSMR_PEN;
  794. supsmr |= UCC_UART_SUPSMR_PEN;
  795. char_length++; /* + PEN */
  796. if (!(termios->c_cflag & PARODD)) {
  797. upsmr &= ~(UCC_UART_UPSMR_RPM_MASK |
  798. UCC_UART_UPSMR_TPM_MASK);
  799. upsmr |= UCC_UART_UPSMR_RPM_EVEN |
  800. UCC_UART_UPSMR_TPM_EVEN;
  801. supsmr &= ~(UCC_UART_SUPSMR_RPM_MASK |
  802. UCC_UART_SUPSMR_TPM_MASK);
  803. supsmr |= UCC_UART_SUPSMR_RPM_EVEN |
  804. UCC_UART_SUPSMR_TPM_EVEN;
  805. }
  806. }
  807. /*
  808. * Set up parity check flag
  809. */
  810. port->read_status_mask = BD_SC_EMPTY | BD_SC_OV;
  811. if (termios->c_iflag & INPCK)
  812. port->read_status_mask |= BD_SC_FR | BD_SC_PR;
  813. if (termios->c_iflag & (BRKINT | PARMRK))
  814. port->read_status_mask |= BD_SC_BR;
  815. /*
  816. * Characters to ignore
  817. */
  818. port->ignore_status_mask = 0;
  819. if (termios->c_iflag & IGNPAR)
  820. port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
  821. if (termios->c_iflag & IGNBRK) {
  822. port->ignore_status_mask |= BD_SC_BR;
  823. /*
  824. * If we're ignore parity and break indicators, ignore
  825. * overruns too. (For real raw support).
  826. */
  827. if (termios->c_iflag & IGNPAR)
  828. port->ignore_status_mask |= BD_SC_OV;
  829. }
  830. /*
  831. * !!! ignore all characters if CREAD is not set
  832. */
  833. if ((termios->c_cflag & CREAD) == 0)
  834. port->read_status_mask &= ~BD_SC_EMPTY;
  835. baud = uart_get_baud_rate(port, termios, old, 0, 115200);
  836. /* Do we really need a spinlock here? */
  837. spin_lock_irqsave(&port->lock, flags);
  838. /* Update the per-port timeout. */
  839. uart_update_timeout(port, termios->c_cflag, baud);
  840. out_be16(&uccp->upsmr, upsmr);
  841. if (soft_uart) {
  842. out_be16(&uccup->supsmr, supsmr);
  843. out_8(&uccup->rx_length, char_length);
  844. /* Soft-UART requires a 1X multiplier for TX */
  845. qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
  846. qe_setbrg(qe_port->us_info.tx_clock, baud, 1);
  847. } else {
  848. qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
  849. qe_setbrg(qe_port->us_info.tx_clock, baud, 16);
  850. }
  851. spin_unlock_irqrestore(&port->lock, flags);
  852. }
  853. /*
  854. * Return a pointer to a string that describes what kind of port this is.
  855. */
  856. static const char *qe_uart_type(struct uart_port *port)
  857. {
  858. return "QE";
  859. }
  860. /*
  861. * Allocate any memory and I/O resources required by the port.
  862. */
  863. static int qe_uart_request_port(struct uart_port *port)
  864. {
  865. int ret;
  866. struct uart_qe_port *qe_port =
  867. container_of(port, struct uart_qe_port, port);
  868. struct ucc_slow_info *us_info = &qe_port->us_info;
  869. struct ucc_slow_private *uccs;
  870. unsigned int rx_size, tx_size;
  871. void *bd_virt;
  872. dma_addr_t bd_dma_addr = 0;
  873. ret = ucc_slow_init(us_info, &uccs);
  874. if (ret) {
  875. dev_err(port->dev, "could not initialize UCC%u\n",
  876. qe_port->ucc_num);
  877. return ret;
  878. }
  879. qe_port->us_private = uccs;
  880. qe_port->uccp = uccs->us_regs;
  881. qe_port->uccup = (struct ucc_uart_pram *) uccs->us_pram;
  882. qe_port->rx_bd_base = uccs->rx_bd;
  883. qe_port->tx_bd_base = uccs->tx_bd;
  884. /*
  885. * Allocate the transmit and receive data buffers.
  886. */
  887. rx_size = L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
  888. tx_size = L1_CACHE_ALIGN(qe_port->tx_nrfifos * qe_port->tx_fifosize);
  889. bd_virt = dma_alloc_coherent(port->dev, rx_size + tx_size, &bd_dma_addr,
  890. GFP_KERNEL);
  891. if (!bd_virt) {
  892. dev_err(port->dev, "could not allocate buffer descriptors\n");
  893. return -ENOMEM;
  894. }
  895. qe_port->bd_virt = bd_virt;
  896. qe_port->bd_dma_addr = bd_dma_addr;
  897. qe_port->bd_size = rx_size + tx_size;
  898. qe_port->rx_buf = bd_virt;
  899. qe_port->tx_buf = qe_port->rx_buf + rx_size;
  900. return 0;
  901. }
  902. /*
  903. * Configure the port.
  904. *
  905. * We say we're a CPM-type port because that's mostly true. Once the device
  906. * is configured, this driver operates almost identically to the CPM serial
  907. * driver.
  908. */
  909. static void qe_uart_config_port(struct uart_port *port, int flags)
  910. {
  911. if (flags & UART_CONFIG_TYPE) {
  912. port->type = PORT_CPM;
  913. qe_uart_request_port(port);
  914. }
  915. }
  916. /*
  917. * Release any memory and I/O resources that were allocated in
  918. * qe_uart_request_port().
  919. */
  920. static void qe_uart_release_port(struct uart_port *port)
  921. {
  922. struct uart_qe_port *qe_port =
  923. container_of(port, struct uart_qe_port, port);
  924. struct ucc_slow_private *uccs = qe_port->us_private;
  925. dma_free_coherent(port->dev, qe_port->bd_size, qe_port->bd_virt,
  926. qe_port->bd_dma_addr);
  927. ucc_slow_free(uccs);
  928. }
  929. /*
  930. * Verify that the data in serial_struct is suitable for this device.
  931. */
  932. static int qe_uart_verify_port(struct uart_port *port,
  933. struct serial_struct *ser)
  934. {
  935. if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
  936. return -EINVAL;
  937. if (ser->irq < 0 || ser->irq >= nr_irqs)
  938. return -EINVAL;
  939. if (ser->baud_base < 9600)
  940. return -EINVAL;
  941. return 0;
  942. }
  943. /* UART operations
  944. *
  945. * Details on these functions can be found in Documentation/serial/driver
  946. */
  947. static struct uart_ops qe_uart_pops = {
  948. .tx_empty = qe_uart_tx_empty,
  949. .set_mctrl = qe_uart_set_mctrl,
  950. .get_mctrl = qe_uart_get_mctrl,
  951. .stop_tx = qe_uart_stop_tx,
  952. .start_tx = qe_uart_start_tx,
  953. .stop_rx = qe_uart_stop_rx,
  954. .enable_ms = qe_uart_enable_ms,
  955. .break_ctl = qe_uart_break_ctl,
  956. .startup = qe_uart_startup,
  957. .shutdown = qe_uart_shutdown,
  958. .set_termios = qe_uart_set_termios,
  959. .type = qe_uart_type,
  960. .release_port = qe_uart_release_port,
  961. .request_port = qe_uart_request_port,
  962. .config_port = qe_uart_config_port,
  963. .verify_port = qe_uart_verify_port,
  964. };
  965. /*
  966. * Obtain the SOC model number and revision level
  967. *
  968. * This function parses the device tree to obtain the SOC model. It then
  969. * reads the SVR register to the revision.
  970. *
  971. * The device tree stores the SOC model two different ways.
  972. *
  973. * The new way is:
  974. *
  975. * cpu@0 {
  976. * compatible = "PowerPC,8323";
  977. * device_type = "cpu";
  978. * ...
  979. *
  980. *
  981. * The old way is:
  982. * PowerPC,8323@0 {
  983. * device_type = "cpu";
  984. * ...
  985. *
  986. * This code first checks the new way, and then the old way.
  987. */
  988. static unsigned int soc_info(unsigned int *rev_h, unsigned int *rev_l)
  989. {
  990. struct device_node *np;
  991. const char *soc_string;
  992. unsigned int svr;
  993. unsigned int soc;
  994. /* Find the CPU node */
  995. np = of_find_node_by_type(NULL, "cpu");
  996. if (!np)
  997. return 0;
  998. /* Find the compatible property */
  999. soc_string = of_get_property(np, "compatible", NULL);
  1000. if (!soc_string)
  1001. /* No compatible property, so try the name. */
  1002. soc_string = np->name;
  1003. /* Extract the SOC number from the "PowerPC," string */
  1004. if ((sscanf(soc_string, "PowerPC,%u", &soc) != 1) || !soc)
  1005. return 0;
  1006. /* Get the revision from the SVR */
  1007. svr = mfspr(SPRN_SVR);
  1008. *rev_h = (svr >> 4) & 0xf;
  1009. *rev_l = svr & 0xf;
  1010. return soc;
  1011. }
  1012. /*
  1013. * requst_firmware_nowait() callback function
  1014. *
  1015. * This function is called by the kernel when a firmware is made available,
  1016. * or if it times out waiting for the firmware.
  1017. */
  1018. static void uart_firmware_cont(const struct firmware *fw, void *context)
  1019. {
  1020. struct qe_firmware *firmware;
  1021. struct device *dev = context;
  1022. int ret;
  1023. if (!fw) {
  1024. dev_err(dev, "firmware not found\n");
  1025. return;
  1026. }
  1027. firmware = (struct qe_firmware *) fw->data;
  1028. if (firmware->header.length != fw->size) {
  1029. dev_err(dev, "invalid firmware\n");
  1030. goto out;
  1031. }
  1032. ret = qe_upload_firmware(firmware);
  1033. if (ret) {
  1034. dev_err(dev, "could not load firmware\n");
  1035. goto out;
  1036. }
  1037. firmware_loaded = 1;
  1038. out:
  1039. release_firmware(fw);
  1040. }
  1041. static int ucc_uart_probe(struct platform_device *ofdev)
  1042. {
  1043. struct device_node *np = ofdev->dev.of_node;
  1044. const unsigned int *iprop; /* Integer OF properties */
  1045. const char *sprop; /* String OF properties */
  1046. struct uart_qe_port *qe_port = NULL;
  1047. struct resource res;
  1048. int ret;
  1049. /*
  1050. * Determine if we need Soft-UART mode
  1051. */
  1052. if (of_find_property(np, "soft-uart", NULL)) {
  1053. dev_dbg(&ofdev->dev, "using Soft-UART mode\n");
  1054. soft_uart = 1;
  1055. }
  1056. /*
  1057. * If we are using Soft-UART, determine if we need to upload the
  1058. * firmware, too.
  1059. */
  1060. if (soft_uart) {
  1061. struct qe_firmware_info *qe_fw_info;
  1062. qe_fw_info = qe_get_firmware_info();
  1063. /* Check if the firmware has been uploaded. */
  1064. if (qe_fw_info && strstr(qe_fw_info->id, "Soft-UART")) {
  1065. firmware_loaded = 1;
  1066. } else {
  1067. char filename[32];
  1068. unsigned int soc;
  1069. unsigned int rev_h;
  1070. unsigned int rev_l;
  1071. soc = soc_info(&rev_h, &rev_l);
  1072. if (!soc) {
  1073. dev_err(&ofdev->dev, "unknown CPU model\n");
  1074. return -ENXIO;
  1075. }
  1076. sprintf(filename, "fsl_qe_ucode_uart_%u_%u%u.bin",
  1077. soc, rev_h, rev_l);
  1078. dev_info(&ofdev->dev, "waiting for firmware %s\n",
  1079. filename);
  1080. /*
  1081. * We call request_firmware_nowait instead of
  1082. * request_firmware so that the driver can load and
  1083. * initialize the ports without holding up the rest of
  1084. * the kernel. If hotplug support is enabled in the
  1085. * kernel, then we use it.
  1086. */
  1087. ret = request_firmware_nowait(THIS_MODULE,
  1088. FW_ACTION_HOTPLUG, filename, &ofdev->dev,
  1089. GFP_KERNEL, &ofdev->dev, uart_firmware_cont);
  1090. if (ret) {
  1091. dev_err(&ofdev->dev,
  1092. "could not load firmware %s\n",
  1093. filename);
  1094. return ret;
  1095. }
  1096. }
  1097. }
  1098. qe_port = kzalloc(sizeof(struct uart_qe_port), GFP_KERNEL);
  1099. if (!qe_port) {
  1100. dev_err(&ofdev->dev, "can't allocate QE port structure\n");
  1101. return -ENOMEM;
  1102. }
  1103. /* Search for IRQ and mapbase */
  1104. ret = of_address_to_resource(np, 0, &res);
  1105. if (ret) {
  1106. dev_err(&ofdev->dev, "missing 'reg' property in device tree\n");
  1107. goto out_free;
  1108. }
  1109. if (!res.start) {
  1110. dev_err(&ofdev->dev, "invalid 'reg' property in device tree\n");
  1111. ret = -EINVAL;
  1112. goto out_free;
  1113. }
  1114. qe_port->port.mapbase = res.start;
  1115. /* Get the UCC number (device ID) */
  1116. /* UCCs are numbered 1-7 */
  1117. iprop = of_get_property(np, "cell-index", NULL);
  1118. if (!iprop) {
  1119. iprop = of_get_property(np, "device-id", NULL);
  1120. if (!iprop) {
  1121. dev_err(&ofdev->dev, "UCC is unspecified in "
  1122. "device tree\n");
  1123. ret = -EINVAL;
  1124. goto out_free;
  1125. }
  1126. }
  1127. if ((*iprop < 1) || (*iprop > UCC_MAX_NUM)) {
  1128. dev_err(&ofdev->dev, "no support for UCC%u\n", *iprop);
  1129. ret = -ENODEV;
  1130. goto out_free;
  1131. }
  1132. qe_port->ucc_num = *iprop - 1;
  1133. /*
  1134. * In the future, we should not require the BRG to be specified in the
  1135. * device tree. If no clock-source is specified, then just pick a BRG
  1136. * to use. This requires a new QE library function that manages BRG
  1137. * assignments.
  1138. */
  1139. sprop = of_get_property(np, "rx-clock-name", NULL);
  1140. if (!sprop) {
  1141. dev_err(&ofdev->dev, "missing rx-clock-name in device tree\n");
  1142. ret = -ENODEV;
  1143. goto out_free;
  1144. }
  1145. qe_port->us_info.rx_clock = qe_clock_source(sprop);
  1146. if ((qe_port->us_info.rx_clock < QE_BRG1) ||
  1147. (qe_port->us_info.rx_clock > QE_BRG16)) {
  1148. dev_err(&ofdev->dev, "rx-clock-name must be a BRG for UART\n");
  1149. ret = -ENODEV;
  1150. goto out_free;
  1151. }
  1152. #ifdef LOOPBACK
  1153. /* In internal loopback mode, TX and RX must use the same clock */
  1154. qe_port->us_info.tx_clock = qe_port->us_info.rx_clock;
  1155. #else
  1156. sprop = of_get_property(np, "tx-clock-name", NULL);
  1157. if (!sprop) {
  1158. dev_err(&ofdev->dev, "missing tx-clock-name in device tree\n");
  1159. ret = -ENODEV;
  1160. goto out_free;
  1161. }
  1162. qe_port->us_info.tx_clock = qe_clock_source(sprop);
  1163. #endif
  1164. if ((qe_port->us_info.tx_clock < QE_BRG1) ||
  1165. (qe_port->us_info.tx_clock > QE_BRG16)) {
  1166. dev_err(&ofdev->dev, "tx-clock-name must be a BRG for UART\n");
  1167. ret = -ENODEV;
  1168. goto out_free;
  1169. }
  1170. /* Get the port number, numbered 0-3 */
  1171. iprop = of_get_property(np, "port-number", NULL);
  1172. if (!iprop) {
  1173. dev_err(&ofdev->dev, "missing port-number in device tree\n");
  1174. ret = -EINVAL;
  1175. goto out_free;
  1176. }
  1177. qe_port->port.line = *iprop;
  1178. if (qe_port->port.line >= UCC_MAX_UART) {
  1179. dev_err(&ofdev->dev, "port-number must be 0-%u\n",
  1180. UCC_MAX_UART - 1);
  1181. ret = -EINVAL;
  1182. goto out_free;
  1183. }
  1184. qe_port->port.irq = irq_of_parse_and_map(np, 0);
  1185. if (qe_port->port.irq == 0) {
  1186. dev_err(&ofdev->dev, "could not map IRQ for UCC%u\n",
  1187. qe_port->ucc_num + 1);
  1188. ret = -EINVAL;
  1189. goto out_free;
  1190. }
  1191. /*
  1192. * Newer device trees have an "fsl,qe" compatible property for the QE
  1193. * node, but we still need to support older device trees.
  1194. */
  1195. np = of_find_compatible_node(NULL, NULL, "fsl,qe");
  1196. if (!np) {
  1197. np = of_find_node_by_type(NULL, "qe");
  1198. if (!np) {
  1199. dev_err(&ofdev->dev, "could not find 'qe' node\n");
  1200. ret = -EINVAL;
  1201. goto out_free;
  1202. }
  1203. }
  1204. iprop = of_get_property(np, "brg-frequency", NULL);
  1205. if (!iprop) {
  1206. dev_err(&ofdev->dev,
  1207. "missing brg-frequency in device tree\n");
  1208. ret = -EINVAL;
  1209. goto out_np;
  1210. }
  1211. if (*iprop)
  1212. qe_port->port.uartclk = *iprop;
  1213. else {
  1214. /*
  1215. * Older versions of U-Boot do not initialize the brg-frequency
  1216. * property, so in this case we assume the BRG frequency is
  1217. * half the QE bus frequency.
  1218. */
  1219. iprop = of_get_property(np, "bus-frequency", NULL);
  1220. if (!iprop) {
  1221. dev_err(&ofdev->dev,
  1222. "missing QE bus-frequency in device tree\n");
  1223. ret = -EINVAL;
  1224. goto out_np;
  1225. }
  1226. if (*iprop)
  1227. qe_port->port.uartclk = *iprop / 2;
  1228. else {
  1229. dev_err(&ofdev->dev,
  1230. "invalid QE bus-frequency in device tree\n");
  1231. ret = -EINVAL;
  1232. goto out_np;
  1233. }
  1234. }
  1235. spin_lock_init(&qe_port->port.lock);
  1236. qe_port->np = np;
  1237. qe_port->port.dev = &ofdev->dev;
  1238. qe_port->port.ops = &qe_uart_pops;
  1239. qe_port->port.iotype = UPIO_MEM;
  1240. qe_port->tx_nrfifos = TX_NUM_FIFO;
  1241. qe_port->tx_fifosize = TX_BUF_SIZE;
  1242. qe_port->rx_nrfifos = RX_NUM_FIFO;
  1243. qe_port->rx_fifosize = RX_BUF_SIZE;
  1244. qe_port->wait_closing = UCC_WAIT_CLOSING;
  1245. qe_port->port.fifosize = 512;
  1246. qe_port->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
  1247. qe_port->us_info.ucc_num = qe_port->ucc_num;
  1248. qe_port->us_info.regs = (phys_addr_t) res.start;
  1249. qe_port->us_info.irq = qe_port->port.irq;
  1250. qe_port->us_info.rx_bd_ring_len = qe_port->rx_nrfifos;
  1251. qe_port->us_info.tx_bd_ring_len = qe_port->tx_nrfifos;
  1252. /* Make sure ucc_slow_init() initializes both TX and RX */
  1253. qe_port->us_info.init_tx = 1;
  1254. qe_port->us_info.init_rx = 1;
  1255. /* Add the port to the uart sub-system. This will cause
  1256. * qe_uart_config_port() to be called, so the us_info structure must
  1257. * be initialized.
  1258. */
  1259. ret = uart_add_one_port(&ucc_uart_driver, &qe_port->port);
  1260. if (ret) {
  1261. dev_err(&ofdev->dev, "could not add /dev/ttyQE%u\n",
  1262. qe_port->port.line);
  1263. goto out_np;
  1264. }
  1265. dev_set_drvdata(&ofdev->dev, qe_port);
  1266. dev_info(&ofdev->dev, "UCC%u assigned to /dev/ttyQE%u\n",
  1267. qe_port->ucc_num + 1, qe_port->port.line);
  1268. /* Display the mknod command for this device */
  1269. dev_dbg(&ofdev->dev, "mknod command is 'mknod /dev/ttyQE%u c %u %u'\n",
  1270. qe_port->port.line, SERIAL_QE_MAJOR,
  1271. SERIAL_QE_MINOR + qe_port->port.line);
  1272. return 0;
  1273. out_np:
  1274. of_node_put(np);
  1275. out_free:
  1276. kfree(qe_port);
  1277. return ret;
  1278. }
  1279. static int ucc_uart_remove(struct platform_device *ofdev)
  1280. {
  1281. struct uart_qe_port *qe_port = dev_get_drvdata(&ofdev->dev);
  1282. dev_info(&ofdev->dev, "removing /dev/ttyQE%u\n", qe_port->port.line);
  1283. uart_remove_one_port(&ucc_uart_driver, &qe_port->port);
  1284. dev_set_drvdata(&ofdev->dev, NULL);
  1285. kfree(qe_port);
  1286. return 0;
  1287. }
  1288. static struct of_device_id ucc_uart_match[] = {
  1289. {
  1290. .type = "serial",
  1291. .compatible = "ucc_uart",
  1292. },
  1293. {},
  1294. };
  1295. MODULE_DEVICE_TABLE(of, ucc_uart_match);
  1296. static struct platform_driver ucc_uart_of_driver = {
  1297. .driver = {
  1298. .name = "ucc_uart",
  1299. .owner = THIS_MODULE,
  1300. .of_match_table = ucc_uart_match,
  1301. },
  1302. .probe = ucc_uart_probe,
  1303. .remove = ucc_uart_remove,
  1304. };
  1305. static int __init ucc_uart_init(void)
  1306. {
  1307. int ret;
  1308. printk(KERN_INFO "Freescale QUICC Engine UART device driver\n");
  1309. #ifdef LOOPBACK
  1310. printk(KERN_INFO "ucc-uart: Using loopback mode\n");
  1311. #endif
  1312. ret = uart_register_driver(&ucc_uart_driver);
  1313. if (ret) {
  1314. printk(KERN_ERR "ucc-uart: could not register UART driver\n");
  1315. return ret;
  1316. }
  1317. ret = platform_driver_register(&ucc_uart_of_driver);
  1318. if (ret)
  1319. printk(KERN_ERR
  1320. "ucc-uart: could not register platform driver\n");
  1321. return ret;
  1322. }
  1323. static void __exit ucc_uart_exit(void)
  1324. {
  1325. printk(KERN_INFO
  1326. "Freescale QUICC Engine UART device driver unloading\n");
  1327. platform_driver_unregister(&ucc_uart_of_driver);
  1328. uart_unregister_driver(&ucc_uart_driver);
  1329. }
  1330. module_init(ucc_uart_init);
  1331. module_exit(ucc_uart_exit);
  1332. MODULE_DESCRIPTION("Freescale QUICC Engine (QE) UART");
  1333. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  1334. MODULE_LICENSE("GPL v2");
  1335. MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_QE_MAJOR);