pch_uart.c 47 KB

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