pch_uart.c 40 KB

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