pch_uart.c 50 KB

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