ucc_uart.c 41 KB

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