pch_uart.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/serial.h>
  7. #include <linux/serial_reg.h>
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/pci.h>
  11. #include <linux/console.h>
  12. #include <linux/serial_core.h>
  13. #include <linux/tty.h>
  14. #include <linux/tty_flip.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/dmi.h>
  18. #include <linux/nmi.h>
  19. #include <linux/delay.h>
  20. #include <linux/of.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/pch_dma.h>
  24. enum {
  25. PCH_UART_HANDLED_RX_INT_SHIFT,
  26. PCH_UART_HANDLED_TX_INT_SHIFT,
  27. PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
  28. PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
  29. PCH_UART_HANDLED_MS_INT_SHIFT,
  30. PCH_UART_HANDLED_LS_INT_SHIFT,
  31. };
  32. #define PCH_UART_DRIVER_DEVICE "ttyPCH"
  33. /* Set the max number of UART port
  34. * Intel EG20T PCH: 4 port
  35. * LAPIS Semiconductor ML7213 IOH: 3 port
  36. * LAPIS Semiconductor ML7223 IOH: 2 port
  37. */
  38. #define PCH_UART_NR 4
  39. #define PCH_UART_HANDLED_RX_INT (1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
  40. #define PCH_UART_HANDLED_TX_INT (1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
  41. #define PCH_UART_HANDLED_RX_ERR_INT (1<<((\
  42. PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
  43. #define PCH_UART_HANDLED_RX_TRG_INT (1<<((\
  44. PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
  45. #define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))
  46. #define PCH_UART_HANDLED_LS_INT (1<<((PCH_UART_HANDLED_LS_INT_SHIFT)<<1))
  47. #define PCH_UART_RBR 0x00
  48. #define PCH_UART_THR 0x00
  49. #define PCH_UART_IER_MASK (PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
  50. PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
  51. #define PCH_UART_IER_ERBFI 0x00000001
  52. #define PCH_UART_IER_ETBEI 0x00000002
  53. #define PCH_UART_IER_ELSI 0x00000004
  54. #define PCH_UART_IER_EDSSI 0x00000008
  55. #define PCH_UART_IIR_IP 0x00000001
  56. #define PCH_UART_IIR_IID 0x00000006
  57. #define PCH_UART_IIR_MSI 0x00000000
  58. #define PCH_UART_IIR_TRI 0x00000002
  59. #define PCH_UART_IIR_RRI 0x00000004
  60. #define PCH_UART_IIR_REI 0x00000006
  61. #define PCH_UART_IIR_TOI 0x00000008
  62. #define PCH_UART_IIR_FIFO256 0x00000020
  63. #define PCH_UART_IIR_FIFO64 PCH_UART_IIR_FIFO256
  64. #define PCH_UART_IIR_FE 0x000000C0
  65. #define PCH_UART_FCR_FIFOE 0x00000001
  66. #define PCH_UART_FCR_RFR 0x00000002
  67. #define PCH_UART_FCR_TFR 0x00000004
  68. #define PCH_UART_FCR_DMS 0x00000008
  69. #define PCH_UART_FCR_FIFO256 0x00000020
  70. #define PCH_UART_FCR_RFTL 0x000000C0
  71. #define PCH_UART_FCR_RFTL1 0x00000000
  72. #define PCH_UART_FCR_RFTL64 0x00000040
  73. #define PCH_UART_FCR_RFTL128 0x00000080
  74. #define PCH_UART_FCR_RFTL224 0x000000C0
  75. #define PCH_UART_FCR_RFTL16 PCH_UART_FCR_RFTL64
  76. #define PCH_UART_FCR_RFTL32 PCH_UART_FCR_RFTL128
  77. #define PCH_UART_FCR_RFTL56 PCH_UART_FCR_RFTL224
  78. #define PCH_UART_FCR_RFTL4 PCH_UART_FCR_RFTL64
  79. #define PCH_UART_FCR_RFTL8 PCH_UART_FCR_RFTL128
  80. #define PCH_UART_FCR_RFTL14 PCH_UART_FCR_RFTL224
  81. #define PCH_UART_FCR_RFTL_SHIFT 6
  82. #define PCH_UART_LCR_WLS 0x00000003
  83. #define PCH_UART_LCR_STB 0x00000004
  84. #define PCH_UART_LCR_PEN 0x00000008
  85. #define PCH_UART_LCR_EPS 0x00000010
  86. #define PCH_UART_LCR_SP 0x00000020
  87. #define PCH_UART_LCR_SB 0x00000040
  88. #define PCH_UART_LCR_DLAB 0x00000080
  89. #define PCH_UART_LCR_NP 0x00000000
  90. #define PCH_UART_LCR_OP PCH_UART_LCR_PEN
  91. #define PCH_UART_LCR_EP (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
  92. #define PCH_UART_LCR_1P (PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
  93. #define PCH_UART_LCR_0P (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
  94. PCH_UART_LCR_SP)
  95. #define PCH_UART_LCR_5BIT 0x00000000
  96. #define PCH_UART_LCR_6BIT 0x00000001
  97. #define PCH_UART_LCR_7BIT 0x00000002
  98. #define PCH_UART_LCR_8BIT 0x00000003
  99. #define PCH_UART_MCR_DTR 0x00000001
  100. #define PCH_UART_MCR_RTS 0x00000002
  101. #define PCH_UART_MCR_OUT 0x0000000C
  102. #define PCH_UART_MCR_LOOP 0x00000010
  103. #define PCH_UART_MCR_AFE 0x00000020
  104. #define PCH_UART_LSR_DR 0x00000001
  105. #define PCH_UART_LSR_ERR (1<<7)
  106. #define PCH_UART_MSR_DCTS 0x00000001
  107. #define PCH_UART_MSR_DDSR 0x00000002
  108. #define PCH_UART_MSR_TERI 0x00000004
  109. #define PCH_UART_MSR_DDCD 0x00000008
  110. #define PCH_UART_MSR_CTS 0x00000010
  111. #define PCH_UART_MSR_DSR 0x00000020
  112. #define PCH_UART_MSR_RI 0x00000040
  113. #define PCH_UART_MSR_DCD 0x00000080
  114. #define PCH_UART_MSR_DELTA (PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
  115. PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)
  116. #define PCH_UART_DLL 0x00
  117. #define PCH_UART_DLM 0x01
  118. #define PCH_UART_BRCSR 0x0E
  119. #define PCH_UART_IID_RLS (PCH_UART_IIR_REI)
  120. #define PCH_UART_IID_RDR (PCH_UART_IIR_RRI)
  121. #define PCH_UART_IID_RDR_TO (PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
  122. #define PCH_UART_IID_THRE (PCH_UART_IIR_TRI)
  123. #define PCH_UART_IID_MS (PCH_UART_IIR_MSI)
  124. #define PCH_UART_HAL_PARITY_NONE (PCH_UART_LCR_NP)
  125. #define PCH_UART_HAL_PARITY_ODD (PCH_UART_LCR_OP)
  126. #define PCH_UART_HAL_PARITY_EVEN (PCH_UART_LCR_EP)
  127. #define PCH_UART_HAL_PARITY_FIX1 (PCH_UART_LCR_1P)
  128. #define PCH_UART_HAL_PARITY_FIX0 (PCH_UART_LCR_0P)
  129. #define PCH_UART_HAL_5BIT (PCH_UART_LCR_5BIT)
  130. #define PCH_UART_HAL_6BIT (PCH_UART_LCR_6BIT)
  131. #define PCH_UART_HAL_7BIT (PCH_UART_LCR_7BIT)
  132. #define PCH_UART_HAL_8BIT (PCH_UART_LCR_8BIT)
  133. #define PCH_UART_HAL_STB1 0
  134. #define PCH_UART_HAL_STB2 (PCH_UART_LCR_STB)
  135. #define PCH_UART_HAL_CLR_TX_FIFO (PCH_UART_FCR_TFR)
  136. #define PCH_UART_HAL_CLR_RX_FIFO (PCH_UART_FCR_RFR)
  137. #define PCH_UART_HAL_CLR_ALL_FIFO (PCH_UART_HAL_CLR_TX_FIFO | \
  138. PCH_UART_HAL_CLR_RX_FIFO)
  139. #define PCH_UART_HAL_DMA_MODE0 0
  140. #define PCH_UART_HAL_FIFO_DIS 0
  141. #define PCH_UART_HAL_FIFO16 (PCH_UART_FCR_FIFOE)
  142. #define PCH_UART_HAL_FIFO256 (PCH_UART_FCR_FIFOE | \
  143. PCH_UART_FCR_FIFO256)
  144. #define PCH_UART_HAL_FIFO64 (PCH_UART_HAL_FIFO256)
  145. #define PCH_UART_HAL_TRIGGER1 (PCH_UART_FCR_RFTL1)
  146. #define PCH_UART_HAL_TRIGGER64 (PCH_UART_FCR_RFTL64)
  147. #define PCH_UART_HAL_TRIGGER128 (PCH_UART_FCR_RFTL128)
  148. #define PCH_UART_HAL_TRIGGER224 (PCH_UART_FCR_RFTL224)
  149. #define PCH_UART_HAL_TRIGGER16 (PCH_UART_FCR_RFTL16)
  150. #define PCH_UART_HAL_TRIGGER32 (PCH_UART_FCR_RFTL32)
  151. #define PCH_UART_HAL_TRIGGER56 (PCH_UART_FCR_RFTL56)
  152. #define PCH_UART_HAL_TRIGGER4 (PCH_UART_FCR_RFTL4)
  153. #define PCH_UART_HAL_TRIGGER8 (PCH_UART_FCR_RFTL8)
  154. #define PCH_UART_HAL_TRIGGER14 (PCH_UART_FCR_RFTL14)
  155. #define PCH_UART_HAL_TRIGGER_L (PCH_UART_FCR_RFTL64)
  156. #define PCH_UART_HAL_TRIGGER_M (PCH_UART_FCR_RFTL128)
  157. #define PCH_UART_HAL_TRIGGER_H (PCH_UART_FCR_RFTL224)
  158. #define PCH_UART_HAL_RX_INT (PCH_UART_IER_ERBFI)
  159. #define PCH_UART_HAL_TX_INT (PCH_UART_IER_ETBEI)
  160. #define PCH_UART_HAL_RX_ERR_INT (PCH_UART_IER_ELSI)
  161. #define PCH_UART_HAL_MS_INT (PCH_UART_IER_EDSSI)
  162. #define PCH_UART_HAL_ALL_INT (PCH_UART_IER_MASK)
  163. #define PCH_UART_HAL_DTR (PCH_UART_MCR_DTR)
  164. #define PCH_UART_HAL_RTS (PCH_UART_MCR_RTS)
  165. #define PCH_UART_HAL_OUT (PCH_UART_MCR_OUT)
  166. #define PCH_UART_HAL_LOOP (PCH_UART_MCR_LOOP)
  167. #define PCH_UART_HAL_AFE (PCH_UART_MCR_AFE)
  168. #define DEFAULT_UARTCLK 1843200 /* 1.8432 MHz */
  169. #define CMITC_UARTCLK 192000000 /* 192.0000 MHz */
  170. #define FRI2_64_UARTCLK 64000000 /* 64.0000 MHz */
  171. #define FRI2_48_UARTCLK 48000000 /* 48.0000 MHz */
  172. #define NTC1_UARTCLK 64000000 /* 64.0000 MHz */
  173. #define MINNOW_UARTCLK 50000000 /* 50.0000 MHz */
  174. struct pch_uart_buffer {
  175. unsigned char *buf;
  176. int size;
  177. };
  178. struct eg20t_port {
  179. struct uart_port port;
  180. int port_type;
  181. void __iomem *membase;
  182. resource_size_t mapbase;
  183. unsigned int iobase;
  184. struct pci_dev *pdev;
  185. int fifo_size;
  186. unsigned int uartclk;
  187. int start_tx;
  188. int start_rx;
  189. int tx_empty;
  190. int trigger;
  191. int trigger_level;
  192. struct pch_uart_buffer rxbuf;
  193. unsigned int dmsr;
  194. unsigned int fcr;
  195. unsigned int mcr;
  196. unsigned int use_dma;
  197. struct dma_async_tx_descriptor *desc_tx;
  198. struct dma_async_tx_descriptor *desc_rx;
  199. struct pch_dma_slave param_tx;
  200. struct pch_dma_slave param_rx;
  201. struct dma_chan *chan_tx;
  202. struct dma_chan *chan_rx;
  203. struct scatterlist *sg_tx_p;
  204. int nent;
  205. int orig_nent;
  206. struct scatterlist sg_rx;
  207. int tx_dma_use;
  208. void *rx_buf_virt;
  209. dma_addr_t rx_buf_dma;
  210. #define IRQ_NAME_SIZE 17
  211. char irq_name[IRQ_NAME_SIZE];
  212. /* protect the eg20t_port private structure and io access to membase */
  213. spinlock_t lock;
  214. };
  215. /**
  216. * struct pch_uart_driver_data - private data structure for UART-DMA
  217. * @port_type: The type of UART port
  218. * @line_no: UART port line number (0, 1, 2...)
  219. */
  220. struct pch_uart_driver_data {
  221. int port_type;
  222. int line_no;
  223. };
  224. enum pch_uart_num_t {
  225. pch_et20t_uart0 = 0,
  226. pch_et20t_uart1,
  227. pch_et20t_uart2,
  228. pch_et20t_uart3,
  229. pch_ml7213_uart0,
  230. pch_ml7213_uart1,
  231. pch_ml7213_uart2,
  232. pch_ml7223_uart0,
  233. pch_ml7223_uart1,
  234. pch_ml7831_uart0,
  235. pch_ml7831_uart1,
  236. };
  237. static struct pch_uart_driver_data drv_dat[] = {
  238. [pch_et20t_uart0] = {PORT_PCH_8LINE, 0},
  239. [pch_et20t_uart1] = {PORT_PCH_2LINE, 1},
  240. [pch_et20t_uart2] = {PORT_PCH_2LINE, 2},
  241. [pch_et20t_uart3] = {PORT_PCH_2LINE, 3},
  242. [pch_ml7213_uart0] = {PORT_PCH_8LINE, 0},
  243. [pch_ml7213_uart1] = {PORT_PCH_2LINE, 1},
  244. [pch_ml7213_uart2] = {PORT_PCH_2LINE, 2},
  245. [pch_ml7223_uart0] = {PORT_PCH_8LINE, 0},
  246. [pch_ml7223_uart1] = {PORT_PCH_2LINE, 1},
  247. [pch_ml7831_uart0] = {PORT_PCH_8LINE, 0},
  248. [pch_ml7831_uart1] = {PORT_PCH_2LINE, 1},
  249. };
  250. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  251. static struct eg20t_port *pch_uart_ports[PCH_UART_NR];
  252. #endif
  253. static unsigned int default_baud = 9600;
  254. static unsigned int user_uartclk = 0;
  255. static const int trigger_level_256[4] = { 1, 64, 128, 224 };
  256. static const int trigger_level_64[4] = { 1, 16, 32, 56 };
  257. static const int trigger_level_16[4] = { 1, 4, 8, 14 };
  258. static const int trigger_level_1[4] = { 1, 1, 1, 1 };
  259. #define PCH_REGS_BUFSIZE 1024
  260. static ssize_t port_show_regs(struct file *file, char __user *user_buf,
  261. size_t count, loff_t *ppos)
  262. {
  263. struct eg20t_port *priv = file->private_data;
  264. char *buf;
  265. u32 len = 0;
  266. ssize_t ret;
  267. unsigned char lcr;
  268. buf = kzalloc(PCH_REGS_BUFSIZE, GFP_KERNEL);
  269. if (!buf)
  270. return 0;
  271. len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
  272. "PCH EG20T port[%d] regs:\n", priv->port.line);
  273. len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
  274. "=================================\n");
  275. len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
  276. "IER: \t0x%02x\n", ioread8(priv->membase + UART_IER));
  277. len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
  278. "IIR: \t0x%02x\n", ioread8(priv->membase + UART_IIR));
  279. len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
  280. "LCR: \t0x%02x\n", ioread8(priv->membase + UART_LCR));
  281. len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
  282. "MCR: \t0x%02x\n", ioread8(priv->membase + UART_MCR));
  283. len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
  284. "LSR: \t0x%02x\n", ioread8(priv->membase + UART_LSR));
  285. len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
  286. "MSR: \t0x%02x\n", ioread8(priv->membase + UART_MSR));
  287. len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
  288. "BRCSR: \t0x%02x\n",
  289. ioread8(priv->membase + PCH_UART_BRCSR));
  290. lcr = ioread8(priv->membase + UART_LCR);
  291. iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
  292. len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
  293. "DLL: \t0x%02x\n", ioread8(priv->membase + UART_DLL));
  294. len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
  295. "DLM: \t0x%02x\n", ioread8(priv->membase + UART_DLM));
  296. iowrite8(lcr, priv->membase + UART_LCR);
  297. if (len > PCH_REGS_BUFSIZE)
  298. len = PCH_REGS_BUFSIZE;
  299. ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
  300. kfree(buf);
  301. return ret;
  302. }
  303. static const struct file_operations port_regs_ops = {
  304. .owner = THIS_MODULE,
  305. .open = simple_open,
  306. .read = port_show_regs,
  307. .llseek = default_llseek,
  308. };
  309. static const struct dmi_system_id pch_uart_dmi_table[] = {
  310. {
  311. .ident = "CM-iTC",
  312. {
  313. DMI_MATCH(DMI_BOARD_NAME, "CM-iTC"),
  314. },
  315. (void *)CMITC_UARTCLK,
  316. },
  317. {
  318. .ident = "FRI2",
  319. {
  320. DMI_MATCH(DMI_BIOS_VERSION, "FRI2"),
  321. },
  322. (void *)FRI2_64_UARTCLK,
  323. },
  324. {
  325. .ident = "Fish River Island II",
  326. {
  327. DMI_MATCH(DMI_PRODUCT_NAME, "Fish River Island II"),
  328. },
  329. (void *)FRI2_48_UARTCLK,
  330. },
  331. {
  332. .ident = "COMe-mTT",
  333. {
  334. DMI_MATCH(DMI_BOARD_NAME, "COMe-mTT"),
  335. },
  336. (void *)NTC1_UARTCLK,
  337. },
  338. {
  339. .ident = "nanoETXexpress-TT",
  340. {
  341. DMI_MATCH(DMI_BOARD_NAME, "nanoETXexpress-TT"),
  342. },
  343. (void *)NTC1_UARTCLK,
  344. },
  345. {
  346. .ident = "MinnowBoard",
  347. {
  348. DMI_MATCH(DMI_BOARD_NAME, "MinnowBoard"),
  349. },
  350. (void *)MINNOW_UARTCLK,
  351. },
  352. { }
  353. };
  354. /* Return UART clock, checking for board specific clocks. */
  355. static unsigned int pch_uart_get_uartclk(void)
  356. {
  357. const struct dmi_system_id *d;
  358. if (user_uartclk)
  359. return user_uartclk;
  360. d = dmi_first_match(pch_uart_dmi_table);
  361. if (d)
  362. return (unsigned long)d->driver_data;
  363. return DEFAULT_UARTCLK;
  364. }
  365. static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
  366. unsigned int flag)
  367. {
  368. u8 ier = ioread8(priv->membase + UART_IER);
  369. ier |= flag & PCH_UART_IER_MASK;
  370. iowrite8(ier, priv->membase + UART_IER);
  371. }
  372. static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
  373. unsigned int flag)
  374. {
  375. u8 ier = ioread8(priv->membase + UART_IER);
  376. ier &= ~(flag & PCH_UART_IER_MASK);
  377. iowrite8(ier, priv->membase + UART_IER);
  378. }
  379. static int pch_uart_hal_set_line(struct eg20t_port *priv, unsigned int baud,
  380. unsigned int parity, unsigned int bits,
  381. unsigned int stb)
  382. {
  383. unsigned int dll, dlm, lcr;
  384. int div;
  385. div = DIV_ROUND_CLOSEST(priv->uartclk / 16, baud);
  386. if (div < 0 || USHRT_MAX <= div) {
  387. dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
  388. return -EINVAL;
  389. }
  390. dll = (unsigned int)div & 0x00FFU;
  391. dlm = ((unsigned int)div >> 8) & 0x00FFU;
  392. if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
  393. dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
  394. return -EINVAL;
  395. }
  396. if (bits & ~PCH_UART_LCR_WLS) {
  397. dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
  398. return -EINVAL;
  399. }
  400. if (stb & ~PCH_UART_LCR_STB) {
  401. dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
  402. return -EINVAL;
  403. }
  404. lcr = parity;
  405. lcr |= bits;
  406. lcr |= stb;
  407. dev_dbg(priv->port.dev, "%s:baud = %u, div = %04x, lcr = %02x (%lu)\n",
  408. __func__, baud, div, lcr, jiffies);
  409. iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
  410. iowrite8(dll, priv->membase + PCH_UART_DLL);
  411. iowrite8(dlm, priv->membase + PCH_UART_DLM);
  412. iowrite8(lcr, priv->membase + UART_LCR);
  413. return 0;
  414. }
  415. static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
  416. unsigned int flag)
  417. {
  418. if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
  419. dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
  420. __func__, flag);
  421. return -EINVAL;
  422. }
  423. iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
  424. iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
  425. priv->membase + UART_FCR);
  426. iowrite8(priv->fcr, priv->membase + UART_FCR);
  427. return 0;
  428. }
  429. static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
  430. unsigned int dmamode,
  431. unsigned int fifo_size, unsigned int trigger)
  432. {
  433. u8 fcr;
  434. if (dmamode & ~PCH_UART_FCR_DMS) {
  435. dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
  436. __func__, dmamode);
  437. return -EINVAL;
  438. }
  439. if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
  440. dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
  441. __func__, fifo_size);
  442. return -EINVAL;
  443. }
  444. if (trigger & ~PCH_UART_FCR_RFTL) {
  445. dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
  446. __func__, trigger);
  447. return -EINVAL;
  448. }
  449. switch (priv->fifo_size) {
  450. case 256:
  451. priv->trigger_level =
  452. trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  453. break;
  454. case 64:
  455. priv->trigger_level =
  456. trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  457. break;
  458. case 16:
  459. priv->trigger_level =
  460. trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  461. break;
  462. default:
  463. priv->trigger_level =
  464. trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  465. break;
  466. }
  467. fcr =
  468. dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
  469. iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
  470. iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
  471. priv->membase + UART_FCR);
  472. iowrite8(fcr, priv->membase + UART_FCR);
  473. priv->fcr = fcr;
  474. return 0;
  475. }
  476. static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
  477. {
  478. unsigned int msr = ioread8(priv->membase + UART_MSR);
  479. priv->dmsr = msr & PCH_UART_MSR_DELTA;
  480. return (u8)msr;
  481. }
  482. static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
  483. int rx_size)
  484. {
  485. int i;
  486. u8 rbr, lsr;
  487. struct uart_port *port = &priv->port;
  488. lsr = ioread8(priv->membase + UART_LSR);
  489. for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
  490. i < rx_size && lsr & (UART_LSR_DR | UART_LSR_BI);
  491. lsr = ioread8(priv->membase + UART_LSR)) {
  492. rbr = ioread8(priv->membase + PCH_UART_RBR);
  493. if (lsr & UART_LSR_BI) {
  494. port->icount.brk++;
  495. if (uart_handle_break(port))
  496. continue;
  497. }
  498. if (uart_handle_sysrq_char(port, rbr))
  499. continue;
  500. buf[i++] = rbr;
  501. }
  502. return i;
  503. }
  504. static unsigned char pch_uart_hal_get_iid(struct eg20t_port *priv)
  505. {
  506. return ioread8(priv->membase + UART_IIR) &\
  507. (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP);
  508. }
  509. static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
  510. {
  511. return ioread8(priv->membase + UART_LSR);
  512. }
  513. static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
  514. {
  515. unsigned int lcr;
  516. lcr = ioread8(priv->membase + UART_LCR);
  517. if (on)
  518. lcr |= PCH_UART_LCR_SB;
  519. else
  520. lcr &= ~PCH_UART_LCR_SB;
  521. iowrite8(lcr, priv->membase + UART_LCR);
  522. }
  523. static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
  524. int size)
  525. {
  526. struct uart_port *port = &priv->port;
  527. struct tty_port *tport = &port->state->port;
  528. tty_insert_flip_string(tport, buf, size);
  529. tty_flip_buffer_push(tport);
  530. return 0;
  531. }
  532. static int dma_push_rx(struct eg20t_port *priv, int size)
  533. {
  534. int room;
  535. struct uart_port *port = &priv->port;
  536. struct tty_port *tport = &port->state->port;
  537. room = tty_buffer_request_room(tport, size);
  538. if (room < size)
  539. dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
  540. size - room);
  541. if (!room)
  542. return 0;
  543. tty_insert_flip_string(tport, sg_virt(&priv->sg_rx), size);
  544. port->icount.rx += room;
  545. return room;
  546. }
  547. static void pch_free_dma(struct uart_port *port)
  548. {
  549. struct eg20t_port *priv;
  550. priv = container_of(port, struct eg20t_port, port);
  551. if (priv->chan_tx) {
  552. dma_release_channel(priv->chan_tx);
  553. priv->chan_tx = NULL;
  554. }
  555. if (priv->chan_rx) {
  556. dma_release_channel(priv->chan_rx);
  557. priv->chan_rx = NULL;
  558. }
  559. if (priv->rx_buf_dma) {
  560. dma_free_coherent(port->dev, port->fifosize, priv->rx_buf_virt,
  561. priv->rx_buf_dma);
  562. priv->rx_buf_virt = NULL;
  563. priv->rx_buf_dma = 0;
  564. }
  565. return;
  566. }
  567. static bool filter(struct dma_chan *chan, void *slave)
  568. {
  569. struct pch_dma_slave *param = slave;
  570. if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
  571. chan->device->dev)) {
  572. chan->private = param;
  573. return true;
  574. } else {
  575. return false;
  576. }
  577. }
  578. static void pch_request_dma(struct uart_port *port)
  579. {
  580. dma_cap_mask_t mask;
  581. struct dma_chan *chan;
  582. struct pci_dev *dma_dev;
  583. struct pch_dma_slave *param;
  584. struct eg20t_port *priv =
  585. container_of(port, struct eg20t_port, port);
  586. dma_cap_zero(mask);
  587. dma_cap_set(DMA_SLAVE, mask);
  588. /* Get DMA's dev information */
  589. dma_dev = pci_get_slot(priv->pdev->bus,
  590. PCI_DEVFN(PCI_SLOT(priv->pdev->devfn), 0));
  591. /* Set Tx DMA */
  592. param = &priv->param_tx;
  593. param->dma_dev = &dma_dev->dev;
  594. param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
  595. param->tx_reg = port->mapbase + UART_TX;
  596. chan = dma_request_channel(mask, filter, param);
  597. if (!chan) {
  598. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
  599. __func__);
  600. pci_dev_put(dma_dev);
  601. return;
  602. }
  603. priv->chan_tx = chan;
  604. /* Set Rx DMA */
  605. param = &priv->param_rx;
  606. param->dma_dev = &dma_dev->dev;
  607. param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
  608. param->rx_reg = port->mapbase + UART_RX;
  609. chan = dma_request_channel(mask, filter, param);
  610. if (!chan) {
  611. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
  612. __func__);
  613. dma_release_channel(priv->chan_tx);
  614. priv->chan_tx = NULL;
  615. pci_dev_put(dma_dev);
  616. return;
  617. }
  618. /* Get Consistent memory for DMA */
  619. priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
  620. &priv->rx_buf_dma, GFP_KERNEL);
  621. priv->chan_rx = chan;
  622. pci_dev_put(dma_dev);
  623. }
  624. static void pch_dma_rx_complete(void *arg)
  625. {
  626. struct eg20t_port *priv = arg;
  627. struct uart_port *port = &priv->port;
  628. int count;
  629. dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
  630. count = dma_push_rx(priv, priv->trigger_level);
  631. if (count)
  632. tty_flip_buffer_push(&port->state->port);
  633. async_tx_ack(priv->desc_rx);
  634. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
  635. PCH_UART_HAL_RX_ERR_INT);
  636. }
  637. static void pch_dma_tx_complete(void *arg)
  638. {
  639. struct eg20t_port *priv = arg;
  640. struct uart_port *port = &priv->port;
  641. struct circ_buf *xmit = &port->state->xmit;
  642. struct scatterlist *sg = priv->sg_tx_p;
  643. int i;
  644. for (i = 0; i < priv->nent; i++, sg++) {
  645. xmit->tail += sg_dma_len(sg);
  646. port->icount.tx += sg_dma_len(sg);
  647. }
  648. xmit->tail &= UART_XMIT_SIZE - 1;
  649. async_tx_ack(priv->desc_tx);
  650. dma_unmap_sg(port->dev, priv->sg_tx_p, priv->orig_nent, DMA_TO_DEVICE);
  651. priv->tx_dma_use = 0;
  652. priv->nent = 0;
  653. priv->orig_nent = 0;
  654. kfree(priv->sg_tx_p);
  655. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
  656. }
  657. static int handle_rx_to(struct eg20t_port *priv)
  658. {
  659. struct pch_uart_buffer *buf;
  660. int rx_size;
  661. int ret;
  662. if (!priv->start_rx) {
  663. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
  664. PCH_UART_HAL_RX_ERR_INT);
  665. return 0;
  666. }
  667. buf = &priv->rxbuf;
  668. do {
  669. rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
  670. ret = push_rx(priv, buf->buf, rx_size);
  671. if (ret)
  672. return 0;
  673. } while (rx_size == buf->size);
  674. return PCH_UART_HANDLED_RX_INT;
  675. }
  676. static int handle_rx(struct eg20t_port *priv)
  677. {
  678. return handle_rx_to(priv);
  679. }
  680. static int dma_handle_rx(struct eg20t_port *priv)
  681. {
  682. struct uart_port *port = &priv->port;
  683. struct dma_async_tx_descriptor *desc;
  684. struct scatterlist *sg;
  685. priv = container_of(port, struct eg20t_port, port);
  686. sg = &priv->sg_rx;
  687. sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
  688. sg_dma_len(sg) = priv->trigger_level;
  689. sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
  690. sg_dma_len(sg), offset_in_page(priv->rx_buf_virt));
  691. sg_dma_address(sg) = priv->rx_buf_dma;
  692. desc = dmaengine_prep_slave_sg(priv->chan_rx,
  693. sg, 1, DMA_DEV_TO_MEM,
  694. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  695. if (!desc)
  696. return 0;
  697. priv->desc_rx = desc;
  698. desc->callback = pch_dma_rx_complete;
  699. desc->callback_param = priv;
  700. desc->tx_submit(desc);
  701. dma_async_issue_pending(priv->chan_rx);
  702. return PCH_UART_HANDLED_RX_INT;
  703. }
  704. static unsigned int handle_tx(struct eg20t_port *priv)
  705. {
  706. struct uart_port *port = &priv->port;
  707. struct circ_buf *xmit = &port->state->xmit;
  708. int fifo_size;
  709. int tx_empty;
  710. if (!priv->start_tx) {
  711. dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
  712. __func__, jiffies);
  713. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  714. priv->tx_empty = 1;
  715. return 0;
  716. }
  717. fifo_size = max(priv->fifo_size, 1);
  718. tx_empty = 1;
  719. if (port->x_char) {
  720. iowrite8(port->x_char, priv->membase + PCH_UART_THR);
  721. port->icount.tx++;
  722. port->x_char = 0;
  723. tx_empty = 0;
  724. fifo_size--;
  725. }
  726. while (!uart_tx_stopped(port) && !uart_circ_empty(xmit) && fifo_size) {
  727. iowrite8(xmit->buf[xmit->tail], priv->membase + PCH_UART_THR);
  728. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  729. port->icount.tx++;
  730. fifo_size--;
  731. tx_empty = 0;
  732. }
  733. priv->tx_empty = tx_empty;
  734. if (tx_empty) {
  735. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  736. uart_write_wakeup(port);
  737. }
  738. return PCH_UART_HANDLED_TX_INT;
  739. }
  740. static unsigned int dma_handle_tx(struct eg20t_port *priv)
  741. {
  742. struct uart_port *port = &priv->port;
  743. struct circ_buf *xmit = &port->state->xmit;
  744. struct scatterlist *sg;
  745. int nent;
  746. int fifo_size;
  747. struct dma_async_tx_descriptor *desc;
  748. int num;
  749. int i;
  750. int bytes;
  751. int size;
  752. int rem;
  753. if (!priv->start_tx) {
  754. dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
  755. __func__, jiffies);
  756. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  757. priv->tx_empty = 1;
  758. return 0;
  759. }
  760. if (priv->tx_dma_use) {
  761. dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
  762. __func__, jiffies);
  763. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  764. priv->tx_empty = 1;
  765. return 0;
  766. }
  767. fifo_size = max(priv->fifo_size, 1);
  768. if (port->x_char) {
  769. iowrite8(port->x_char, priv->membase + PCH_UART_THR);
  770. port->icount.tx++;
  771. port->x_char = 0;
  772. fifo_size--;
  773. }
  774. bytes = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  775. if (!bytes) {
  776. dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
  777. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  778. uart_write_wakeup(port);
  779. return 0;
  780. }
  781. if (bytes > fifo_size) {
  782. num = bytes / fifo_size + 1;
  783. size = fifo_size;
  784. rem = bytes % fifo_size;
  785. } else {
  786. num = 1;
  787. size = bytes;
  788. rem = bytes;
  789. }
  790. dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
  791. __func__, num, size, rem);
  792. priv->tx_dma_use = 1;
  793. priv->sg_tx_p = kmalloc_array(num, sizeof(struct scatterlist), GFP_ATOMIC);
  794. if (!priv->sg_tx_p) {
  795. dev_err(priv->port.dev, "%s:kzalloc Failed\n", __func__);
  796. return 0;
  797. }
  798. sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
  799. sg = priv->sg_tx_p;
  800. for (i = 0; i < num; i++, sg++) {
  801. if (i == (num - 1))
  802. sg_set_page(sg, virt_to_page(xmit->buf),
  803. rem, fifo_size * i);
  804. else
  805. sg_set_page(sg, virt_to_page(xmit->buf),
  806. size, fifo_size * i);
  807. }
  808. sg = priv->sg_tx_p;
  809. nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
  810. if (!nent) {
  811. dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
  812. return 0;
  813. }
  814. priv->orig_nent = num;
  815. priv->nent = nent;
  816. for (i = 0; i < nent; i++, sg++) {
  817. sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
  818. fifo_size * i;
  819. sg_dma_address(sg) = (sg_dma_address(sg) &
  820. ~(UART_XMIT_SIZE - 1)) + sg->offset;
  821. if (i == (nent - 1))
  822. sg_dma_len(sg) = rem;
  823. else
  824. sg_dma_len(sg) = size;
  825. }
  826. desc = dmaengine_prep_slave_sg(priv->chan_tx,
  827. priv->sg_tx_p, nent, DMA_MEM_TO_DEV,
  828. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  829. if (!desc) {
  830. dev_err(priv->port.dev, "%s:dmaengine_prep_slave_sg Failed\n",
  831. __func__);
  832. return 0;
  833. }
  834. dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
  835. priv->desc_tx = desc;
  836. desc->callback = pch_dma_tx_complete;
  837. desc->callback_param = priv;
  838. desc->tx_submit(desc);
  839. dma_async_issue_pending(priv->chan_tx);
  840. return PCH_UART_HANDLED_TX_INT;
  841. }
  842. static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
  843. {
  844. struct uart_port *port = &priv->port;
  845. struct tty_struct *tty = tty_port_tty_get(&port->state->port);
  846. char *error_msg[5] = {};
  847. int i = 0;
  848. if (lsr & PCH_UART_LSR_ERR)
  849. error_msg[i++] = "Error data in FIFO\n";
  850. if (lsr & UART_LSR_FE) {
  851. port->icount.frame++;
  852. error_msg[i++] = " Framing Error\n";
  853. }
  854. if (lsr & UART_LSR_PE) {
  855. port->icount.parity++;
  856. error_msg[i++] = " Parity Error\n";
  857. }
  858. if (lsr & UART_LSR_OE) {
  859. port->icount.overrun++;
  860. error_msg[i++] = " Overrun Error\n";
  861. }
  862. if (tty == NULL) {
  863. for (i = 0; error_msg[i] != NULL; i++)
  864. dev_err(&priv->pdev->dev, error_msg[i]);
  865. } else {
  866. tty_kref_put(tty);
  867. }
  868. }
  869. static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
  870. {
  871. struct eg20t_port *priv = dev_id;
  872. unsigned int handled;
  873. u8 lsr;
  874. int ret = 0;
  875. unsigned char iid;
  876. unsigned long flags;
  877. int next = 1;
  878. u8 msr;
  879. spin_lock_irqsave(&priv->lock, flags);
  880. handled = 0;
  881. while (next) {
  882. iid = pch_uart_hal_get_iid(priv);
  883. if (iid & PCH_UART_IIR_IP) /* No Interrupt */
  884. break;
  885. switch (iid) {
  886. case PCH_UART_IID_RLS: /* Receiver Line Status */
  887. lsr = pch_uart_hal_get_line_status(priv);
  888. if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
  889. UART_LSR_PE | UART_LSR_OE)) {
  890. pch_uart_err_ir(priv, lsr);
  891. ret = PCH_UART_HANDLED_RX_ERR_INT;
  892. } else {
  893. ret = PCH_UART_HANDLED_LS_INT;
  894. }
  895. break;
  896. case PCH_UART_IID_RDR: /* Received Data Ready */
  897. if (priv->use_dma) {
  898. pch_uart_hal_disable_interrupt(priv,
  899. PCH_UART_HAL_RX_INT |
  900. PCH_UART_HAL_RX_ERR_INT);
  901. ret = dma_handle_rx(priv);
  902. if (!ret)
  903. pch_uart_hal_enable_interrupt(priv,
  904. PCH_UART_HAL_RX_INT |
  905. PCH_UART_HAL_RX_ERR_INT);
  906. } else {
  907. ret = handle_rx(priv);
  908. }
  909. break;
  910. case PCH_UART_IID_RDR_TO: /* Received Data Ready
  911. (FIFO Timeout) */
  912. ret = handle_rx_to(priv);
  913. break;
  914. case PCH_UART_IID_THRE: /* Transmitter Holding Register
  915. Empty */
  916. if (priv->use_dma)
  917. ret = dma_handle_tx(priv);
  918. else
  919. ret = handle_tx(priv);
  920. break;
  921. case PCH_UART_IID_MS: /* Modem Status */
  922. msr = pch_uart_hal_get_modem(priv);
  923. next = 0; /* MS ir prioirty is the lowest. So, MS ir
  924. means final interrupt */
  925. if ((msr & UART_MSR_ANY_DELTA) == 0)
  926. break;
  927. ret |= PCH_UART_HANDLED_MS_INT;
  928. break;
  929. default: /* Never junp to this label */
  930. dev_err(priv->port.dev, "%s:iid=%02x (%lu)\n", __func__,
  931. iid, jiffies);
  932. ret = -1;
  933. next = 0;
  934. break;
  935. }
  936. handled |= (unsigned int)ret;
  937. }
  938. spin_unlock_irqrestore(&priv->lock, flags);
  939. return IRQ_RETVAL(handled);
  940. }
  941. /* This function tests whether the transmitter fifo and shifter for the port
  942. described by 'port' is empty. */
  943. static unsigned int pch_uart_tx_empty(struct uart_port *port)
  944. {
  945. struct eg20t_port *priv;
  946. priv = container_of(port, struct eg20t_port, port);
  947. if (priv->tx_empty)
  948. return TIOCSER_TEMT;
  949. else
  950. return 0;
  951. }
  952. /* Returns the current state of modem control inputs. */
  953. static unsigned int pch_uart_get_mctrl(struct uart_port *port)
  954. {
  955. struct eg20t_port *priv;
  956. u8 modem;
  957. unsigned int ret = 0;
  958. priv = container_of(port, struct eg20t_port, port);
  959. modem = pch_uart_hal_get_modem(priv);
  960. if (modem & UART_MSR_DCD)
  961. ret |= TIOCM_CAR;
  962. if (modem & UART_MSR_RI)
  963. ret |= TIOCM_RNG;
  964. if (modem & UART_MSR_DSR)
  965. ret |= TIOCM_DSR;
  966. if (modem & UART_MSR_CTS)
  967. ret |= TIOCM_CTS;
  968. return ret;
  969. }
  970. static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  971. {
  972. u32 mcr = 0;
  973. struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
  974. if (mctrl & TIOCM_DTR)
  975. mcr |= UART_MCR_DTR;
  976. if (mctrl & TIOCM_RTS)
  977. mcr |= UART_MCR_RTS;
  978. if (mctrl & TIOCM_LOOP)
  979. mcr |= UART_MCR_LOOP;
  980. if (priv->mcr & UART_MCR_AFE)
  981. mcr |= UART_MCR_AFE;
  982. if (mctrl)
  983. iowrite8(mcr, priv->membase + UART_MCR);
  984. }
  985. static void pch_uart_stop_tx(struct uart_port *port)
  986. {
  987. struct eg20t_port *priv;
  988. priv = container_of(port, struct eg20t_port, port);
  989. priv->start_tx = 0;
  990. priv->tx_dma_use = 0;
  991. }
  992. static void pch_uart_start_tx(struct uart_port *port)
  993. {
  994. struct eg20t_port *priv;
  995. priv = container_of(port, struct eg20t_port, port);
  996. if (priv->use_dma) {
  997. if (priv->tx_dma_use) {
  998. dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
  999. __func__);
  1000. return;
  1001. }
  1002. }
  1003. priv->start_tx = 1;
  1004. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
  1005. }
  1006. static void pch_uart_stop_rx(struct uart_port *port)
  1007. {
  1008. struct eg20t_port *priv;
  1009. priv = container_of(port, struct eg20t_port, port);
  1010. priv->start_rx = 0;
  1011. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
  1012. PCH_UART_HAL_RX_ERR_INT);
  1013. }
  1014. /* Enable the modem status interrupts. */
  1015. static void pch_uart_enable_ms(struct uart_port *port)
  1016. {
  1017. struct eg20t_port *priv;
  1018. priv = container_of(port, struct eg20t_port, port);
  1019. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
  1020. }
  1021. /* Control the transmission of a break signal. */
  1022. static void pch_uart_break_ctl(struct uart_port *port, int ctl)
  1023. {
  1024. struct eg20t_port *priv;
  1025. unsigned long flags;
  1026. priv = container_of(port, struct eg20t_port, port);
  1027. spin_lock_irqsave(&priv->lock, flags);
  1028. pch_uart_hal_set_break(priv, ctl);
  1029. spin_unlock_irqrestore(&priv->lock, flags);
  1030. }
  1031. /* Grab any interrupt resources and initialise any low level driver state. */
  1032. static int pch_uart_startup(struct uart_port *port)
  1033. {
  1034. struct eg20t_port *priv;
  1035. int ret;
  1036. int fifo_size;
  1037. int trigger_level;
  1038. priv = container_of(port, struct eg20t_port, port);
  1039. priv->tx_empty = 1;
  1040. if (port->uartclk)
  1041. priv->uartclk = port->uartclk;
  1042. else
  1043. port->uartclk = priv->uartclk;
  1044. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1045. ret = pch_uart_hal_set_line(priv, default_baud,
  1046. PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
  1047. PCH_UART_HAL_STB1);
  1048. if (ret)
  1049. return ret;
  1050. switch (priv->fifo_size) {
  1051. case 256:
  1052. fifo_size = PCH_UART_HAL_FIFO256;
  1053. break;
  1054. case 64:
  1055. fifo_size = PCH_UART_HAL_FIFO64;
  1056. break;
  1057. case 16:
  1058. fifo_size = PCH_UART_HAL_FIFO16;
  1059. break;
  1060. case 1:
  1061. default:
  1062. fifo_size = PCH_UART_HAL_FIFO_DIS;
  1063. break;
  1064. }
  1065. switch (priv->trigger) {
  1066. case PCH_UART_HAL_TRIGGER1:
  1067. trigger_level = 1;
  1068. break;
  1069. case PCH_UART_HAL_TRIGGER_L:
  1070. trigger_level = priv->fifo_size / 4;
  1071. break;
  1072. case PCH_UART_HAL_TRIGGER_M:
  1073. trigger_level = priv->fifo_size / 2;
  1074. break;
  1075. case PCH_UART_HAL_TRIGGER_H:
  1076. default:
  1077. trigger_level = priv->fifo_size - (priv->fifo_size / 8);
  1078. break;
  1079. }
  1080. priv->trigger_level = trigger_level;
  1081. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  1082. fifo_size, priv->trigger);
  1083. if (ret < 0)
  1084. return ret;
  1085. ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
  1086. priv->irq_name, priv);
  1087. if (ret < 0)
  1088. return ret;
  1089. if (priv->use_dma)
  1090. pch_request_dma(port);
  1091. priv->start_rx = 1;
  1092. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
  1093. PCH_UART_HAL_RX_ERR_INT);
  1094. uart_update_timeout(port, CS8, default_baud);
  1095. return 0;
  1096. }
  1097. static void pch_uart_shutdown(struct uart_port *port)
  1098. {
  1099. struct eg20t_port *priv;
  1100. int ret;
  1101. priv = container_of(port, struct eg20t_port, port);
  1102. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1103. pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
  1104. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  1105. PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
  1106. if (ret)
  1107. dev_err(priv->port.dev,
  1108. "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
  1109. pch_free_dma(port);
  1110. free_irq(priv->port.irq, priv);
  1111. }
  1112. /* Change the port parameters, including word length, parity, stop
  1113. *bits. Update read_status_mask and ignore_status_mask to indicate
  1114. *the types of events we are interested in receiving. */
  1115. static void pch_uart_set_termios(struct uart_port *port,
  1116. struct ktermios *termios,
  1117. const struct ktermios *old)
  1118. {
  1119. int rtn;
  1120. unsigned int baud, parity, bits, stb;
  1121. struct eg20t_port *priv;
  1122. unsigned long flags;
  1123. priv = container_of(port, struct eg20t_port, port);
  1124. switch (termios->c_cflag & CSIZE) {
  1125. case CS5:
  1126. bits = PCH_UART_HAL_5BIT;
  1127. break;
  1128. case CS6:
  1129. bits = PCH_UART_HAL_6BIT;
  1130. break;
  1131. case CS7:
  1132. bits = PCH_UART_HAL_7BIT;
  1133. break;
  1134. default: /* CS8 */
  1135. bits = PCH_UART_HAL_8BIT;
  1136. break;
  1137. }
  1138. if (termios->c_cflag & CSTOPB)
  1139. stb = PCH_UART_HAL_STB2;
  1140. else
  1141. stb = PCH_UART_HAL_STB1;
  1142. if (termios->c_cflag & PARENB) {
  1143. if (termios->c_cflag & PARODD)
  1144. parity = PCH_UART_HAL_PARITY_ODD;
  1145. else
  1146. parity = PCH_UART_HAL_PARITY_EVEN;
  1147. } else
  1148. parity = PCH_UART_HAL_PARITY_NONE;
  1149. /* Only UART0 has auto hardware flow function */
  1150. if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
  1151. priv->mcr |= UART_MCR_AFE;
  1152. else
  1153. priv->mcr &= ~UART_MCR_AFE;
  1154. termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
  1155. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  1156. spin_lock_irqsave(&priv->lock, flags);
  1157. spin_lock(&port->lock);
  1158. uart_update_timeout(port, termios->c_cflag, baud);
  1159. rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
  1160. if (rtn)
  1161. goto out;
  1162. pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
  1163. /* Don't rewrite B0 */
  1164. if (tty_termios_baud_rate(termios))
  1165. tty_termios_encode_baud_rate(termios, baud, baud);
  1166. out:
  1167. spin_unlock(&port->lock);
  1168. spin_unlock_irqrestore(&priv->lock, flags);
  1169. }
  1170. static const char *pch_uart_type(struct uart_port *port)
  1171. {
  1172. return KBUILD_MODNAME;
  1173. }
  1174. static void pch_uart_release_port(struct uart_port *port)
  1175. {
  1176. struct eg20t_port *priv;
  1177. priv = container_of(port, struct eg20t_port, port);
  1178. pci_iounmap(priv->pdev, priv->membase);
  1179. pci_release_regions(priv->pdev);
  1180. }
  1181. static int pch_uart_request_port(struct uart_port *port)
  1182. {
  1183. struct eg20t_port *priv;
  1184. int ret;
  1185. void __iomem *membase;
  1186. priv = container_of(port, struct eg20t_port, port);
  1187. ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
  1188. if (ret < 0)
  1189. return -EBUSY;
  1190. membase = pci_iomap(priv->pdev, 1, 0);
  1191. if (!membase) {
  1192. pci_release_regions(priv->pdev);
  1193. return -EBUSY;
  1194. }
  1195. priv->membase = port->membase = membase;
  1196. return 0;
  1197. }
  1198. static void pch_uart_config_port(struct uart_port *port, int type)
  1199. {
  1200. struct eg20t_port *priv;
  1201. priv = container_of(port, struct eg20t_port, port);
  1202. if (type & UART_CONFIG_TYPE) {
  1203. port->type = priv->port_type;
  1204. pch_uart_request_port(port);
  1205. }
  1206. }
  1207. static int pch_uart_verify_port(struct uart_port *port,
  1208. struct serial_struct *serinfo)
  1209. {
  1210. struct eg20t_port *priv;
  1211. priv = container_of(port, struct eg20t_port, port);
  1212. if (serinfo->flags & UPF_LOW_LATENCY) {
  1213. dev_info(priv->port.dev,
  1214. "PCH UART : Use PIO Mode (without DMA)\n");
  1215. priv->use_dma = 0;
  1216. serinfo->flags &= ~UPF_LOW_LATENCY;
  1217. } else {
  1218. #ifndef CONFIG_PCH_DMA
  1219. dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
  1220. __func__);
  1221. return -EOPNOTSUPP;
  1222. #endif
  1223. if (!priv->use_dma) {
  1224. pch_request_dma(port);
  1225. if (priv->chan_rx)
  1226. priv->use_dma = 1;
  1227. }
  1228. dev_info(priv->port.dev, "PCH UART: %s\n",
  1229. priv->use_dma ?
  1230. "Use DMA Mode" : "No DMA");
  1231. }
  1232. return 0;
  1233. }
  1234. #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_PCH_UART_CONSOLE)
  1235. /*
  1236. * Wait for transmitter & holding register to empty
  1237. */
  1238. static void wait_for_xmitr(struct eg20t_port *up, int bits)
  1239. {
  1240. unsigned int status, tmout = 10000;
  1241. /* Wait up to 10ms for the character(s) to be sent. */
  1242. for (;;) {
  1243. status = ioread8(up->membase + UART_LSR);
  1244. if ((status & bits) == bits)
  1245. break;
  1246. if (--tmout == 0)
  1247. break;
  1248. udelay(1);
  1249. }
  1250. /* Wait up to 1s for flow control if necessary */
  1251. if (up->port.flags & UPF_CONS_FLOW) {
  1252. unsigned int tmout;
  1253. for (tmout = 1000000; tmout; tmout--) {
  1254. unsigned int msr = ioread8(up->membase + UART_MSR);
  1255. if (msr & UART_MSR_CTS)
  1256. break;
  1257. udelay(1);
  1258. touch_nmi_watchdog();
  1259. }
  1260. }
  1261. }
  1262. #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_PCH_UART_CONSOLE */
  1263. #ifdef CONFIG_CONSOLE_POLL
  1264. /*
  1265. * Console polling routines for communicate via uart while
  1266. * in an interrupt or debug context.
  1267. */
  1268. static int pch_uart_get_poll_char(struct uart_port *port)
  1269. {
  1270. struct eg20t_port *priv =
  1271. container_of(port, struct eg20t_port, port);
  1272. u8 lsr = ioread8(priv->membase + UART_LSR);
  1273. if (!(lsr & UART_LSR_DR))
  1274. return NO_POLL_CHAR;
  1275. return ioread8(priv->membase + PCH_UART_RBR);
  1276. }
  1277. static void pch_uart_put_poll_char(struct uart_port *port,
  1278. unsigned char c)
  1279. {
  1280. unsigned int ier;
  1281. struct eg20t_port *priv =
  1282. container_of(port, struct eg20t_port, port);
  1283. /*
  1284. * First save the IER then disable the interrupts
  1285. */
  1286. ier = ioread8(priv->membase + UART_IER);
  1287. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1288. wait_for_xmitr(priv, UART_LSR_THRE);
  1289. /*
  1290. * Send the character out.
  1291. */
  1292. iowrite8(c, priv->membase + PCH_UART_THR);
  1293. /*
  1294. * Finally, wait for transmitter to become empty
  1295. * and restore the IER
  1296. */
  1297. wait_for_xmitr(priv, UART_LSR_BOTH_EMPTY);
  1298. iowrite8(ier, priv->membase + UART_IER);
  1299. }
  1300. #endif /* CONFIG_CONSOLE_POLL */
  1301. static const struct uart_ops pch_uart_ops = {
  1302. .tx_empty = pch_uart_tx_empty,
  1303. .set_mctrl = pch_uart_set_mctrl,
  1304. .get_mctrl = pch_uart_get_mctrl,
  1305. .stop_tx = pch_uart_stop_tx,
  1306. .start_tx = pch_uart_start_tx,
  1307. .stop_rx = pch_uart_stop_rx,
  1308. .enable_ms = pch_uart_enable_ms,
  1309. .break_ctl = pch_uart_break_ctl,
  1310. .startup = pch_uart_startup,
  1311. .shutdown = pch_uart_shutdown,
  1312. .set_termios = pch_uart_set_termios,
  1313. /* .pm = pch_uart_pm, Not supported yet */
  1314. .type = pch_uart_type,
  1315. .release_port = pch_uart_release_port,
  1316. .request_port = pch_uart_request_port,
  1317. .config_port = pch_uart_config_port,
  1318. .verify_port = pch_uart_verify_port,
  1319. #ifdef CONFIG_CONSOLE_POLL
  1320. .poll_get_char = pch_uart_get_poll_char,
  1321. .poll_put_char = pch_uart_put_poll_char,
  1322. #endif
  1323. };
  1324. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1325. static void pch_console_putchar(struct uart_port *port, unsigned char ch)
  1326. {
  1327. struct eg20t_port *priv =
  1328. container_of(port, struct eg20t_port, port);
  1329. wait_for_xmitr(priv, UART_LSR_THRE);
  1330. iowrite8(ch, priv->membase + PCH_UART_THR);
  1331. }
  1332. /*
  1333. * Print a string to the serial port trying not to disturb
  1334. * any possible real use of the port...
  1335. *
  1336. * The console_lock must be held when we get here.
  1337. */
  1338. static void
  1339. pch_console_write(struct console *co, const char *s, unsigned int count)
  1340. {
  1341. struct eg20t_port *priv;
  1342. unsigned long flags;
  1343. int priv_locked = 1;
  1344. int port_locked = 1;
  1345. u8 ier;
  1346. priv = pch_uart_ports[co->index];
  1347. touch_nmi_watchdog();
  1348. local_irq_save(flags);
  1349. if (priv->port.sysrq) {
  1350. /* call to uart_handle_sysrq_char already took the priv lock */
  1351. priv_locked = 0;
  1352. /* serial8250_handle_port() already took the port lock */
  1353. port_locked = 0;
  1354. } else if (oops_in_progress) {
  1355. priv_locked = spin_trylock(&priv->lock);
  1356. port_locked = spin_trylock(&priv->port.lock);
  1357. } else {
  1358. spin_lock(&priv->lock);
  1359. spin_lock(&priv->port.lock);
  1360. }
  1361. /*
  1362. * First save the IER then disable the interrupts
  1363. */
  1364. ier = ioread8(priv->membase + UART_IER);
  1365. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1366. uart_console_write(&priv->port, s, count, pch_console_putchar);
  1367. /*
  1368. * Finally, wait for transmitter to become empty
  1369. * and restore the IER
  1370. */
  1371. wait_for_xmitr(priv, UART_LSR_BOTH_EMPTY);
  1372. iowrite8(ier, priv->membase + UART_IER);
  1373. if (port_locked)
  1374. spin_unlock(&priv->port.lock);
  1375. if (priv_locked)
  1376. spin_unlock(&priv->lock);
  1377. local_irq_restore(flags);
  1378. }
  1379. static int __init pch_console_setup(struct console *co, char *options)
  1380. {
  1381. struct uart_port *port;
  1382. int baud = default_baud;
  1383. int bits = 8;
  1384. int parity = 'n';
  1385. int flow = 'n';
  1386. /*
  1387. * Check whether an invalid uart number has been specified, and
  1388. * if so, search for the first available port that does have
  1389. * console support.
  1390. */
  1391. if (co->index >= PCH_UART_NR)
  1392. co->index = 0;
  1393. port = &pch_uart_ports[co->index]->port;
  1394. if (!port || (!port->iobase && !port->membase))
  1395. return -ENODEV;
  1396. port->uartclk = pch_uart_get_uartclk();
  1397. if (options)
  1398. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1399. return uart_set_options(port, co, baud, parity, bits, flow);
  1400. }
  1401. static struct uart_driver pch_uart_driver;
  1402. static struct console pch_console = {
  1403. .name = PCH_UART_DRIVER_DEVICE,
  1404. .write = pch_console_write,
  1405. .device = uart_console_device,
  1406. .setup = pch_console_setup,
  1407. .flags = CON_PRINTBUFFER | CON_ANYTIME,
  1408. .index = -1,
  1409. .data = &pch_uart_driver,
  1410. };
  1411. #define PCH_CONSOLE (&pch_console)
  1412. #else
  1413. #define PCH_CONSOLE NULL
  1414. #endif /* CONFIG_SERIAL_PCH_UART_CONSOLE */
  1415. static struct uart_driver pch_uart_driver = {
  1416. .owner = THIS_MODULE,
  1417. .driver_name = KBUILD_MODNAME,
  1418. .dev_name = PCH_UART_DRIVER_DEVICE,
  1419. .major = 0,
  1420. .minor = 0,
  1421. .nr = PCH_UART_NR,
  1422. .cons = PCH_CONSOLE,
  1423. };
  1424. static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
  1425. const struct pci_device_id *id)
  1426. {
  1427. struct eg20t_port *priv;
  1428. int ret;
  1429. unsigned int iobase;
  1430. unsigned int mapbase;
  1431. unsigned char *rxbuf;
  1432. int fifosize;
  1433. int port_type;
  1434. struct pch_uart_driver_data *board;
  1435. char name[32];
  1436. board = &drv_dat[id->driver_data];
  1437. port_type = board->port_type;
  1438. priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
  1439. if (priv == NULL)
  1440. goto init_port_alloc_err;
  1441. rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  1442. if (!rxbuf)
  1443. goto init_port_free_txbuf;
  1444. switch (port_type) {
  1445. case PORT_PCH_8LINE:
  1446. fifosize = 256; /* EG20T/ML7213: UART0 */
  1447. break;
  1448. case PORT_PCH_2LINE:
  1449. fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
  1450. break;
  1451. default:
  1452. dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
  1453. goto init_port_hal_free;
  1454. }
  1455. pci_enable_msi(pdev);
  1456. pci_set_master(pdev);
  1457. spin_lock_init(&priv->lock);
  1458. iobase = pci_resource_start(pdev, 0);
  1459. mapbase = pci_resource_start(pdev, 1);
  1460. priv->mapbase = mapbase;
  1461. priv->iobase = iobase;
  1462. priv->pdev = pdev;
  1463. priv->tx_empty = 1;
  1464. priv->rxbuf.buf = rxbuf;
  1465. priv->rxbuf.size = PAGE_SIZE;
  1466. priv->fifo_size = fifosize;
  1467. priv->uartclk = pch_uart_get_uartclk();
  1468. priv->port_type = port_type;
  1469. priv->port.dev = &pdev->dev;
  1470. priv->port.iobase = iobase;
  1471. priv->port.membase = NULL;
  1472. priv->port.mapbase = mapbase;
  1473. priv->port.irq = pdev->irq;
  1474. priv->port.iotype = UPIO_PORT;
  1475. priv->port.ops = &pch_uart_ops;
  1476. priv->port.flags = UPF_BOOT_AUTOCONF;
  1477. priv->port.fifosize = fifosize;
  1478. priv->port.line = board->line_no;
  1479. priv->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_PCH_UART_CONSOLE);
  1480. priv->trigger = PCH_UART_HAL_TRIGGER_M;
  1481. snprintf(priv->irq_name, IRQ_NAME_SIZE,
  1482. KBUILD_MODNAME ":" PCH_UART_DRIVER_DEVICE "%d",
  1483. priv->port.line);
  1484. spin_lock_init(&priv->port.lock);
  1485. pci_set_drvdata(pdev, priv);
  1486. priv->trigger_level = 1;
  1487. priv->fcr = 0;
  1488. if (pdev->dev.of_node)
  1489. of_property_read_u32(pdev->dev.of_node, "clock-frequency"
  1490. , &user_uartclk);
  1491. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1492. pch_uart_ports[board->line_no] = priv;
  1493. #endif
  1494. ret = uart_add_one_port(&pch_uart_driver, &priv->port);
  1495. if (ret < 0)
  1496. goto init_port_hal_free;
  1497. snprintf(name, sizeof(name), "uart%d_regs", priv->port.line);
  1498. debugfs_create_file(name, S_IFREG | S_IRUGO, NULL, priv,
  1499. &port_regs_ops);
  1500. return priv;
  1501. init_port_hal_free:
  1502. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1503. pch_uart_ports[board->line_no] = NULL;
  1504. #endif
  1505. free_page((unsigned long)rxbuf);
  1506. init_port_free_txbuf:
  1507. kfree(priv);
  1508. init_port_alloc_err:
  1509. return NULL;
  1510. }
  1511. static void pch_uart_exit_port(struct eg20t_port *priv)
  1512. {
  1513. char name[32];
  1514. snprintf(name, sizeof(name), "uart%d_regs", priv->port.line);
  1515. debugfs_lookup_and_remove(name, NULL);
  1516. uart_remove_one_port(&pch_uart_driver, &priv->port);
  1517. free_page((unsigned long)priv->rxbuf.buf);
  1518. }
  1519. static void pch_uart_pci_remove(struct pci_dev *pdev)
  1520. {
  1521. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1522. pci_disable_msi(pdev);
  1523. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1524. pch_uart_ports[priv->port.line] = NULL;
  1525. #endif
  1526. pch_uart_exit_port(priv);
  1527. pci_disable_device(pdev);
  1528. kfree(priv);
  1529. return;
  1530. }
  1531. static int __maybe_unused pch_uart_pci_suspend(struct device *dev)
  1532. {
  1533. struct eg20t_port *priv = dev_get_drvdata(dev);
  1534. uart_suspend_port(&pch_uart_driver, &priv->port);
  1535. return 0;
  1536. }
  1537. static int __maybe_unused pch_uart_pci_resume(struct device *dev)
  1538. {
  1539. struct eg20t_port *priv = dev_get_drvdata(dev);
  1540. uart_resume_port(&pch_uart_driver, &priv->port);
  1541. return 0;
  1542. }
  1543. static const struct pci_device_id pch_uart_pci_id[] = {
  1544. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
  1545. .driver_data = pch_et20t_uart0},
  1546. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
  1547. .driver_data = pch_et20t_uart1},
  1548. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
  1549. .driver_data = pch_et20t_uart2},
  1550. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
  1551. .driver_data = pch_et20t_uart3},
  1552. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
  1553. .driver_data = pch_ml7213_uart0},
  1554. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
  1555. .driver_data = pch_ml7213_uart1},
  1556. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
  1557. .driver_data = pch_ml7213_uart2},
  1558. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
  1559. .driver_data = pch_ml7223_uart0},
  1560. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
  1561. .driver_data = pch_ml7223_uart1},
  1562. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
  1563. .driver_data = pch_ml7831_uart0},
  1564. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
  1565. .driver_data = pch_ml7831_uart1},
  1566. {0,},
  1567. };
  1568. static int pch_uart_pci_probe(struct pci_dev *pdev,
  1569. const struct pci_device_id *id)
  1570. {
  1571. int ret;
  1572. struct eg20t_port *priv;
  1573. ret = pci_enable_device(pdev);
  1574. if (ret < 0)
  1575. goto probe_error;
  1576. priv = pch_uart_init_port(pdev, id);
  1577. if (!priv) {
  1578. ret = -EBUSY;
  1579. goto probe_disable_device;
  1580. }
  1581. pci_set_drvdata(pdev, priv);
  1582. return ret;
  1583. probe_disable_device:
  1584. pci_disable_msi(pdev);
  1585. pci_disable_device(pdev);
  1586. probe_error:
  1587. return ret;
  1588. }
  1589. static SIMPLE_DEV_PM_OPS(pch_uart_pci_pm_ops,
  1590. pch_uart_pci_suspend,
  1591. pch_uart_pci_resume);
  1592. static struct pci_driver pch_uart_pci_driver = {
  1593. .name = "pch_uart",
  1594. .id_table = pch_uart_pci_id,
  1595. .probe = pch_uart_pci_probe,
  1596. .remove = pch_uart_pci_remove,
  1597. .driver.pm = &pch_uart_pci_pm_ops,
  1598. };
  1599. static int __init pch_uart_module_init(void)
  1600. {
  1601. int ret;
  1602. /* register as UART driver */
  1603. ret = uart_register_driver(&pch_uart_driver);
  1604. if (ret < 0)
  1605. return ret;
  1606. /* register as PCI driver */
  1607. ret = pci_register_driver(&pch_uart_pci_driver);
  1608. if (ret < 0)
  1609. uart_unregister_driver(&pch_uart_driver);
  1610. return ret;
  1611. }
  1612. module_init(pch_uart_module_init);
  1613. static void __exit pch_uart_module_exit(void)
  1614. {
  1615. pci_unregister_driver(&pch_uart_pci_driver);
  1616. uart_unregister_driver(&pch_uart_driver);
  1617. }
  1618. module_exit(pch_uart_module_exit);
  1619. MODULE_LICENSE("GPL v2");
  1620. MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
  1621. MODULE_DEVICE_TABLE(pci, pch_uart_pci_id);
  1622. module_param(default_baud, uint, S_IRUGO);
  1623. MODULE_PARM_DESC(default_baud,
  1624. "Default BAUD for initial driver state and console (default 9600)");
  1625. module_param(user_uartclk, uint, S_IRUGO);
  1626. MODULE_PARM_DESC(user_uartclk,
  1627. "Override UART default or board specific UART clock");