pch_uart.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  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. static struct uart_ops pch_uart_ops = {
  1270. .tx_empty = pch_uart_tx_empty,
  1271. .set_mctrl = pch_uart_set_mctrl,
  1272. .get_mctrl = pch_uart_get_mctrl,
  1273. .stop_tx = pch_uart_stop_tx,
  1274. .start_tx = pch_uart_start_tx,
  1275. .stop_rx = pch_uart_stop_rx,
  1276. .enable_ms = pch_uart_enable_ms,
  1277. .break_ctl = pch_uart_break_ctl,
  1278. .startup = pch_uart_startup,
  1279. .shutdown = pch_uart_shutdown,
  1280. .set_termios = pch_uart_set_termios,
  1281. /* .pm = pch_uart_pm, Not supported yet */
  1282. /* .set_wake = pch_uart_set_wake, Not supported yet */
  1283. .type = pch_uart_type,
  1284. .release_port = pch_uart_release_port,
  1285. .request_port = pch_uart_request_port,
  1286. .config_port = pch_uart_config_port,
  1287. .verify_port = pch_uart_verify_port
  1288. };
  1289. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1290. /*
  1291. * Wait for transmitter & holding register to empty
  1292. */
  1293. static void wait_for_xmitr(struct eg20t_port *up, int bits)
  1294. {
  1295. unsigned int status, tmout = 10000;
  1296. /* Wait up to 10ms for the character(s) to be sent. */
  1297. for (;;) {
  1298. status = ioread8(up->membase + UART_LSR);
  1299. if ((status & bits) == bits)
  1300. break;
  1301. if (--tmout == 0)
  1302. break;
  1303. udelay(1);
  1304. }
  1305. /* Wait up to 1s for flow control if necessary */
  1306. if (up->port.flags & UPF_CONS_FLOW) {
  1307. unsigned int tmout;
  1308. for (tmout = 1000000; tmout; tmout--) {
  1309. unsigned int msr = ioread8(up->membase + UART_MSR);
  1310. if (msr & UART_MSR_CTS)
  1311. break;
  1312. udelay(1);
  1313. touch_nmi_watchdog();
  1314. }
  1315. }
  1316. }
  1317. static void pch_console_putchar(struct uart_port *port, int ch)
  1318. {
  1319. struct eg20t_port *priv =
  1320. container_of(port, struct eg20t_port, port);
  1321. wait_for_xmitr(priv, UART_LSR_THRE);
  1322. iowrite8(ch, priv->membase + PCH_UART_THR);
  1323. }
  1324. /*
  1325. * Print a string to the serial port trying not to disturb
  1326. * any possible real use of the port...
  1327. *
  1328. * The console_lock must be held when we get here.
  1329. */
  1330. static void
  1331. pch_console_write(struct console *co, const char *s, unsigned int count)
  1332. {
  1333. struct eg20t_port *priv;
  1334. unsigned long flags;
  1335. int priv_locked = 1;
  1336. int port_locked = 1;
  1337. u8 ier;
  1338. priv = pch_uart_ports[co->index];
  1339. touch_nmi_watchdog();
  1340. local_irq_save(flags);
  1341. if (priv->port.sysrq) {
  1342. /* call to uart_handle_sysrq_char already took the priv lock */
  1343. priv_locked = 0;
  1344. /* serial8250_handle_port() already took the port lock */
  1345. port_locked = 0;
  1346. } else if (oops_in_progress) {
  1347. priv_locked = spin_trylock(&priv->lock);
  1348. port_locked = spin_trylock(&priv->port.lock);
  1349. } else {
  1350. spin_lock(&priv->lock);
  1351. spin_lock(&priv->port.lock);
  1352. }
  1353. /*
  1354. * First save the IER then disable the interrupts
  1355. */
  1356. ier = ioread8(priv->membase + UART_IER);
  1357. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1358. uart_console_write(&priv->port, s, count, pch_console_putchar);
  1359. /*
  1360. * Finally, wait for transmitter to become empty
  1361. * and restore the IER
  1362. */
  1363. wait_for_xmitr(priv, BOTH_EMPTY);
  1364. iowrite8(ier, priv->membase + UART_IER);
  1365. if (port_locked)
  1366. spin_unlock(&priv->port.lock);
  1367. if (priv_locked)
  1368. spin_unlock(&priv->lock);
  1369. local_irq_restore(flags);
  1370. }
  1371. static int __init pch_console_setup(struct console *co, char *options)
  1372. {
  1373. struct uart_port *port;
  1374. int baud = default_baud;
  1375. int bits = 8;
  1376. int parity = 'n';
  1377. int flow = 'n';
  1378. /*
  1379. * Check whether an invalid uart number has been specified, and
  1380. * if so, search for the first available port that does have
  1381. * console support.
  1382. */
  1383. if (co->index >= PCH_UART_NR)
  1384. co->index = 0;
  1385. port = &pch_uart_ports[co->index]->port;
  1386. if (!port || (!port->iobase && !port->membase))
  1387. return -ENODEV;
  1388. port->uartclk = pch_uart_get_uartclk();
  1389. if (options)
  1390. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1391. return uart_set_options(port, co, baud, parity, bits, flow);
  1392. }
  1393. static struct uart_driver pch_uart_driver;
  1394. static struct console pch_console = {
  1395. .name = PCH_UART_DRIVER_DEVICE,
  1396. .write = pch_console_write,
  1397. .device = uart_console_device,
  1398. .setup = pch_console_setup,
  1399. .flags = CON_PRINTBUFFER | CON_ANYTIME,
  1400. .index = -1,
  1401. .data = &pch_uart_driver,
  1402. };
  1403. #define PCH_CONSOLE (&pch_console)
  1404. #else
  1405. #define PCH_CONSOLE NULL
  1406. #endif
  1407. static struct uart_driver pch_uart_driver = {
  1408. .owner = THIS_MODULE,
  1409. .driver_name = KBUILD_MODNAME,
  1410. .dev_name = PCH_UART_DRIVER_DEVICE,
  1411. .major = 0,
  1412. .minor = 0,
  1413. .nr = PCH_UART_NR,
  1414. .cons = PCH_CONSOLE,
  1415. };
  1416. static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
  1417. const struct pci_device_id *id)
  1418. {
  1419. struct eg20t_port *priv;
  1420. int ret;
  1421. unsigned int iobase;
  1422. unsigned int mapbase;
  1423. unsigned char *rxbuf;
  1424. int fifosize;
  1425. int port_type;
  1426. struct pch_uart_driver_data *board;
  1427. char name[32]; /* for debugfs file name */
  1428. board = &drv_dat[id->driver_data];
  1429. port_type = board->port_type;
  1430. priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
  1431. if (priv == NULL)
  1432. goto init_port_alloc_err;
  1433. rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  1434. if (!rxbuf)
  1435. goto init_port_free_txbuf;
  1436. switch (port_type) {
  1437. case PORT_UNKNOWN:
  1438. fifosize = 256; /* EG20T/ML7213: UART0 */
  1439. break;
  1440. case PORT_8250:
  1441. fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
  1442. break;
  1443. default:
  1444. dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
  1445. goto init_port_hal_free;
  1446. }
  1447. pci_enable_msi(pdev);
  1448. pci_set_master(pdev);
  1449. spin_lock_init(&priv->lock);
  1450. iobase = pci_resource_start(pdev, 0);
  1451. mapbase = pci_resource_start(pdev, 1);
  1452. priv->mapbase = mapbase;
  1453. priv->iobase = iobase;
  1454. priv->pdev = pdev;
  1455. priv->tx_empty = 1;
  1456. priv->rxbuf.buf = rxbuf;
  1457. priv->rxbuf.size = PAGE_SIZE;
  1458. priv->fifo_size = fifosize;
  1459. priv->uartclk = pch_uart_get_uartclk();
  1460. priv->port_type = PORT_MAX_8250 + port_type + 1;
  1461. priv->port.dev = &pdev->dev;
  1462. priv->port.iobase = iobase;
  1463. priv->port.membase = NULL;
  1464. priv->port.mapbase = mapbase;
  1465. priv->port.irq = pdev->irq;
  1466. priv->port.iotype = UPIO_PORT;
  1467. priv->port.ops = &pch_uart_ops;
  1468. priv->port.flags = UPF_BOOT_AUTOCONF;
  1469. priv->port.fifosize = fifosize;
  1470. priv->port.line = board->line_no;
  1471. priv->trigger = PCH_UART_HAL_TRIGGER_M;
  1472. spin_lock_init(&priv->port.lock);
  1473. pci_set_drvdata(pdev, priv);
  1474. priv->trigger_level = 1;
  1475. priv->fcr = 0;
  1476. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1477. pch_uart_ports[board->line_no] = priv;
  1478. #endif
  1479. ret = uart_add_one_port(&pch_uart_driver, &priv->port);
  1480. if (ret < 0)
  1481. goto init_port_hal_free;
  1482. #ifdef CONFIG_DEBUG_FS
  1483. snprintf(name, sizeof(name), "uart%d_regs", board->line_no);
  1484. priv->debugfs = debugfs_create_file(name, S_IFREG | S_IRUGO,
  1485. NULL, priv, &port_regs_ops);
  1486. #endif
  1487. return priv;
  1488. init_port_hal_free:
  1489. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1490. pch_uart_ports[board->line_no] = NULL;
  1491. #endif
  1492. free_page((unsigned long)rxbuf);
  1493. init_port_free_txbuf:
  1494. kfree(priv);
  1495. init_port_alloc_err:
  1496. return NULL;
  1497. }
  1498. static void pch_uart_exit_port(struct eg20t_port *priv)
  1499. {
  1500. #ifdef CONFIG_DEBUG_FS
  1501. if (priv->debugfs)
  1502. debugfs_remove(priv->debugfs);
  1503. #endif
  1504. uart_remove_one_port(&pch_uart_driver, &priv->port);
  1505. pci_set_drvdata(priv->pdev, NULL);
  1506. free_page((unsigned long)priv->rxbuf.buf);
  1507. }
  1508. static void pch_uart_pci_remove(struct pci_dev *pdev)
  1509. {
  1510. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1511. pci_disable_msi(pdev);
  1512. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1513. pch_uart_ports[priv->port.line] = NULL;
  1514. #endif
  1515. pch_uart_exit_port(priv);
  1516. pci_disable_device(pdev);
  1517. kfree(priv);
  1518. return;
  1519. }
  1520. #ifdef CONFIG_PM
  1521. static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1522. {
  1523. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1524. uart_suspend_port(&pch_uart_driver, &priv->port);
  1525. pci_save_state(pdev);
  1526. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1527. return 0;
  1528. }
  1529. static int pch_uart_pci_resume(struct pci_dev *pdev)
  1530. {
  1531. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1532. int ret;
  1533. pci_set_power_state(pdev, PCI_D0);
  1534. pci_restore_state(pdev);
  1535. ret = pci_enable_device(pdev);
  1536. if (ret) {
  1537. dev_err(&pdev->dev,
  1538. "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
  1539. return ret;
  1540. }
  1541. uart_resume_port(&pch_uart_driver, &priv->port);
  1542. return 0;
  1543. }
  1544. #else
  1545. #define pch_uart_pci_suspend NULL
  1546. #define pch_uart_pci_resume NULL
  1547. #endif
  1548. static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
  1549. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
  1550. .driver_data = pch_et20t_uart0},
  1551. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
  1552. .driver_data = pch_et20t_uart1},
  1553. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
  1554. .driver_data = pch_et20t_uart2},
  1555. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
  1556. .driver_data = pch_et20t_uart3},
  1557. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
  1558. .driver_data = pch_ml7213_uart0},
  1559. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
  1560. .driver_data = pch_ml7213_uart1},
  1561. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
  1562. .driver_data = pch_ml7213_uart2},
  1563. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
  1564. .driver_data = pch_ml7223_uart0},
  1565. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
  1566. .driver_data = pch_ml7223_uart1},
  1567. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
  1568. .driver_data = pch_ml7831_uart0},
  1569. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
  1570. .driver_data = pch_ml7831_uart1},
  1571. {0,},
  1572. };
  1573. static int pch_uart_pci_probe(struct pci_dev *pdev,
  1574. const struct pci_device_id *id)
  1575. {
  1576. int ret;
  1577. struct eg20t_port *priv;
  1578. ret = pci_enable_device(pdev);
  1579. if (ret < 0)
  1580. goto probe_error;
  1581. priv = pch_uart_init_port(pdev, id);
  1582. if (!priv) {
  1583. ret = -EBUSY;
  1584. goto probe_disable_device;
  1585. }
  1586. pci_set_drvdata(pdev, priv);
  1587. return ret;
  1588. probe_disable_device:
  1589. pci_disable_msi(pdev);
  1590. pci_disable_device(pdev);
  1591. probe_error:
  1592. return ret;
  1593. }
  1594. static struct pci_driver pch_uart_pci_driver = {
  1595. .name = "pch_uart",
  1596. .id_table = pch_uart_pci_id,
  1597. .probe = pch_uart_pci_probe,
  1598. .remove = pch_uart_pci_remove,
  1599. .suspend = pch_uart_pci_suspend,
  1600. .resume = pch_uart_pci_resume,
  1601. };
  1602. static int __init pch_uart_module_init(void)
  1603. {
  1604. int ret;
  1605. /* register as UART driver */
  1606. ret = uart_register_driver(&pch_uart_driver);
  1607. if (ret < 0)
  1608. return ret;
  1609. /* register as PCI driver */
  1610. ret = pci_register_driver(&pch_uart_pci_driver);
  1611. if (ret < 0)
  1612. uart_unregister_driver(&pch_uart_driver);
  1613. return ret;
  1614. }
  1615. module_init(pch_uart_module_init);
  1616. static void __exit pch_uart_module_exit(void)
  1617. {
  1618. pci_unregister_driver(&pch_uart_pci_driver);
  1619. uart_unregister_driver(&pch_uart_driver);
  1620. }
  1621. module_exit(pch_uart_module_exit);
  1622. MODULE_LICENSE("GPL v2");
  1623. MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
  1624. module_param(default_baud, uint, S_IRUGO);
  1625. MODULE_PARM_DESC(default_baud,
  1626. "Default BAUD for initial driver state and console (default 9600)");
  1627. module_param(user_uartclk, uint, S_IRUGO);
  1628. MODULE_PARM_DESC(user_uartclk,
  1629. "Override UART default or board specific UART clock");