ucc_uart.c 41 KB

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