pch_uart.c 48 KB

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