ucc_uart.c 41 KB

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