pch_uart.c 49 KB

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