pch_uart.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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 int_dis_flag;
  203. int trigger;
  204. int trigger_level;
  205. struct pch_uart_buffer rxbuf;
  206. unsigned int dmsr;
  207. unsigned int fcr;
  208. unsigned int mcr;
  209. unsigned int use_dma;
  210. unsigned int use_dma_flag;
  211. struct dma_async_tx_descriptor *desc_tx;
  212. struct dma_async_tx_descriptor *desc_rx;
  213. struct pch_dma_slave param_tx;
  214. struct pch_dma_slave param_rx;
  215. struct dma_chan *chan_tx;
  216. struct dma_chan *chan_rx;
  217. struct scatterlist *sg_tx_p;
  218. int nent;
  219. struct scatterlist sg_rx;
  220. int tx_dma_use;
  221. void *rx_buf_virt;
  222. dma_addr_t rx_buf_dma;
  223. struct dentry *debugfs;
  224. };
  225. /**
  226. * struct pch_uart_driver_data - private data structure for UART-DMA
  227. * @port_type: The number of DMA channel
  228. * @line_no: UART port line number (0, 1, 2...)
  229. */
  230. struct pch_uart_driver_data {
  231. int port_type;
  232. int line_no;
  233. };
  234. enum pch_uart_num_t {
  235. pch_et20t_uart0 = 0,
  236. pch_et20t_uart1,
  237. pch_et20t_uart2,
  238. pch_et20t_uart3,
  239. pch_ml7213_uart0,
  240. pch_ml7213_uart1,
  241. pch_ml7213_uart2,
  242. pch_ml7223_uart0,
  243. pch_ml7223_uart1,
  244. pch_ml7831_uart0,
  245. pch_ml7831_uart1,
  246. };
  247. static struct pch_uart_driver_data drv_dat[] = {
  248. [pch_et20t_uart0] = {PCH_UART_8LINE, 0},
  249. [pch_et20t_uart1] = {PCH_UART_2LINE, 1},
  250. [pch_et20t_uart2] = {PCH_UART_2LINE, 2},
  251. [pch_et20t_uart3] = {PCH_UART_2LINE, 3},
  252. [pch_ml7213_uart0] = {PCH_UART_8LINE, 0},
  253. [pch_ml7213_uart1] = {PCH_UART_2LINE, 1},
  254. [pch_ml7213_uart2] = {PCH_UART_2LINE, 2},
  255. [pch_ml7223_uart0] = {PCH_UART_8LINE, 0},
  256. [pch_ml7223_uart1] = {PCH_UART_2LINE, 1},
  257. [pch_ml7831_uart0] = {PCH_UART_8LINE, 0},
  258. [pch_ml7831_uart1] = {PCH_UART_2LINE, 1},
  259. };
  260. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  261. static struct eg20t_port *pch_uart_ports[PCH_UART_NR];
  262. #endif
  263. static unsigned int default_baud = 9600;
  264. static unsigned int user_uartclk = 0;
  265. static const int trigger_level_256[4] = { 1, 64, 128, 224 };
  266. static const int trigger_level_64[4] = { 1, 16, 32, 56 };
  267. static const int trigger_level_16[4] = { 1, 4, 8, 14 };
  268. static const int trigger_level_1[4] = { 1, 1, 1, 1 };
  269. #ifdef CONFIG_DEBUG_FS
  270. #define PCH_REGS_BUFSIZE 1024
  271. static ssize_t port_show_regs(struct file *file, char __user *user_buf,
  272. size_t count, loff_t *ppos)
  273. {
  274. struct eg20t_port *priv = file->private_data;
  275. char *buf;
  276. u32 len = 0;
  277. ssize_t ret;
  278. unsigned char lcr;
  279. buf = kzalloc(PCH_REGS_BUFSIZE, GFP_KERNEL);
  280. if (!buf)
  281. return 0;
  282. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  283. "PCH EG20T port[%d] regs:\n", priv->port.line);
  284. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  285. "=================================\n");
  286. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  287. "IER: \t0x%02x\n", ioread8(priv->membase + UART_IER));
  288. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  289. "IIR: \t0x%02x\n", ioread8(priv->membase + UART_IIR));
  290. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  291. "LCR: \t0x%02x\n", ioread8(priv->membase + UART_LCR));
  292. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  293. "MCR: \t0x%02x\n", ioread8(priv->membase + UART_MCR));
  294. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  295. "LSR: \t0x%02x\n", ioread8(priv->membase + UART_LSR));
  296. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  297. "MSR: \t0x%02x\n", ioread8(priv->membase + UART_MSR));
  298. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  299. "BRCSR: \t0x%02x\n",
  300. ioread8(priv->membase + PCH_UART_BRCSR));
  301. lcr = ioread8(priv->membase + UART_LCR);
  302. iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
  303. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  304. "DLL: \t0x%02x\n", ioread8(priv->membase + UART_DLL));
  305. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  306. "DLM: \t0x%02x\n", ioread8(priv->membase + UART_DLM));
  307. iowrite8(lcr, priv->membase + UART_LCR);
  308. if (len > PCH_REGS_BUFSIZE)
  309. len = PCH_REGS_BUFSIZE;
  310. ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
  311. kfree(buf);
  312. return ret;
  313. }
  314. static const struct file_operations port_regs_ops = {
  315. .owner = THIS_MODULE,
  316. .open = simple_open,
  317. .read = port_show_regs,
  318. .llseek = default_llseek,
  319. };
  320. #endif /* CONFIG_DEBUG_FS */
  321. /* Return UART clock, checking for board specific clocks. */
  322. static int pch_uart_get_uartclk(void)
  323. {
  324. const char *cmp;
  325. if (user_uartclk)
  326. return user_uartclk;
  327. cmp = dmi_get_system_info(DMI_BOARD_NAME);
  328. if (cmp && strstr(cmp, "CM-iTC"))
  329. return CMITC_UARTCLK;
  330. cmp = dmi_get_system_info(DMI_BIOS_VERSION);
  331. if (cmp && strnstr(cmp, "FRI2", 4))
  332. return FRI2_64_UARTCLK;
  333. cmp = dmi_get_system_info(DMI_PRODUCT_NAME);
  334. if (cmp && strstr(cmp, "Fish River Island II"))
  335. return FRI2_48_UARTCLK;
  336. return DEFAULT_UARTCLK;
  337. }
  338. static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
  339. unsigned int flag)
  340. {
  341. u8 ier = ioread8(priv->membase + UART_IER);
  342. ier |= flag & PCH_UART_IER_MASK;
  343. iowrite8(ier, priv->membase + UART_IER);
  344. }
  345. static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
  346. unsigned int flag)
  347. {
  348. u8 ier = ioread8(priv->membase + UART_IER);
  349. ier &= ~(flag & PCH_UART_IER_MASK);
  350. iowrite8(ier, priv->membase + UART_IER);
  351. }
  352. static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
  353. unsigned int parity, unsigned int bits,
  354. unsigned int stb)
  355. {
  356. unsigned int dll, dlm, lcr;
  357. int div;
  358. div = DIV_ROUND_CLOSEST(priv->uartclk / 16, baud);
  359. if (div < 0 || USHRT_MAX <= div) {
  360. dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
  361. return -EINVAL;
  362. }
  363. dll = (unsigned int)div & 0x00FFU;
  364. dlm = ((unsigned int)div >> 8) & 0x00FFU;
  365. if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
  366. dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
  367. return -EINVAL;
  368. }
  369. if (bits & ~PCH_UART_LCR_WLS) {
  370. dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
  371. return -EINVAL;
  372. }
  373. if (stb & ~PCH_UART_LCR_STB) {
  374. dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
  375. return -EINVAL;
  376. }
  377. lcr = parity;
  378. lcr |= bits;
  379. lcr |= stb;
  380. dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
  381. __func__, baud, div, lcr, jiffies);
  382. iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
  383. iowrite8(dll, priv->membase + PCH_UART_DLL);
  384. iowrite8(dlm, priv->membase + PCH_UART_DLM);
  385. iowrite8(lcr, priv->membase + UART_LCR);
  386. return 0;
  387. }
  388. static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
  389. unsigned int flag)
  390. {
  391. if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
  392. dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
  393. __func__, flag);
  394. return -EINVAL;
  395. }
  396. iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
  397. iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
  398. priv->membase + UART_FCR);
  399. iowrite8(priv->fcr, priv->membase + UART_FCR);
  400. return 0;
  401. }
  402. static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
  403. unsigned int dmamode,
  404. unsigned int fifo_size, unsigned int trigger)
  405. {
  406. u8 fcr;
  407. if (dmamode & ~PCH_UART_FCR_DMS) {
  408. dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
  409. __func__, dmamode);
  410. return -EINVAL;
  411. }
  412. if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
  413. dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
  414. __func__, fifo_size);
  415. return -EINVAL;
  416. }
  417. if (trigger & ~PCH_UART_FCR_RFTL) {
  418. dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
  419. __func__, trigger);
  420. return -EINVAL;
  421. }
  422. switch (priv->fifo_size) {
  423. case 256:
  424. priv->trigger_level =
  425. trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  426. break;
  427. case 64:
  428. priv->trigger_level =
  429. trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  430. break;
  431. case 16:
  432. priv->trigger_level =
  433. trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  434. break;
  435. default:
  436. priv->trigger_level =
  437. trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  438. break;
  439. }
  440. fcr =
  441. dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
  442. iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
  443. iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
  444. priv->membase + UART_FCR);
  445. iowrite8(fcr, priv->membase + UART_FCR);
  446. priv->fcr = fcr;
  447. return 0;
  448. }
  449. static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
  450. {
  451. unsigned int msr = ioread8(priv->membase + UART_MSR);
  452. priv->dmsr = msr & PCH_UART_MSR_DELTA;
  453. return (u8)msr;
  454. }
  455. static void pch_uart_hal_write(struct eg20t_port *priv,
  456. const unsigned char *buf, int tx_size)
  457. {
  458. int i;
  459. unsigned int thr;
  460. for (i = 0; i < tx_size;) {
  461. thr = buf[i++];
  462. iowrite8(thr, priv->membase + PCH_UART_THR);
  463. }
  464. }
  465. static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
  466. int rx_size)
  467. {
  468. int i;
  469. u8 rbr, lsr;
  470. lsr = ioread8(priv->membase + UART_LSR);
  471. for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
  472. i < rx_size && lsr & UART_LSR_DR;
  473. lsr = ioread8(priv->membase + UART_LSR)) {
  474. rbr = ioread8(priv->membase + PCH_UART_RBR);
  475. buf[i++] = rbr;
  476. }
  477. return i;
  478. }
  479. static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
  480. {
  481. unsigned int iir;
  482. int ret;
  483. iir = ioread8(priv->membase + UART_IIR);
  484. ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
  485. return ret;
  486. }
  487. static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
  488. {
  489. return ioread8(priv->membase + UART_LSR);
  490. }
  491. static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
  492. {
  493. unsigned int lcr;
  494. lcr = ioread8(priv->membase + UART_LCR);
  495. if (on)
  496. lcr |= PCH_UART_LCR_SB;
  497. else
  498. lcr &= ~PCH_UART_LCR_SB;
  499. iowrite8(lcr, priv->membase + UART_LCR);
  500. }
  501. static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
  502. int size)
  503. {
  504. struct uart_port *port;
  505. struct tty_struct *tty;
  506. port = &priv->port;
  507. tty = tty_port_tty_get(&port->state->port);
  508. if (!tty) {
  509. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  510. return -EBUSY;
  511. }
  512. tty_insert_flip_string(tty, buf, size);
  513. tty_flip_buffer_push(tty);
  514. tty_kref_put(tty);
  515. return 0;
  516. }
  517. static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
  518. {
  519. int ret = 0;
  520. struct uart_port *port = &priv->port;
  521. if (port->x_char) {
  522. dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
  523. __func__, port->x_char, jiffies);
  524. buf[0] = port->x_char;
  525. port->x_char = 0;
  526. ret = 1;
  527. }
  528. return ret;
  529. }
  530. static int dma_push_rx(struct eg20t_port *priv, int size)
  531. {
  532. struct tty_struct *tty;
  533. int room;
  534. struct uart_port *port = &priv->port;
  535. port = &priv->port;
  536. tty = tty_port_tty_get(&port->state->port);
  537. if (!tty) {
  538. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  539. return 0;
  540. }
  541. room = tty_buffer_request_room(tty, size);
  542. if (room < size)
  543. dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
  544. size - room);
  545. if (!room)
  546. return room;
  547. tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
  548. port->icount.rx += room;
  549. tty_kref_put(tty);
  550. return room;
  551. }
  552. static void pch_free_dma(struct uart_port *port)
  553. {
  554. struct eg20t_port *priv;
  555. priv = container_of(port, struct eg20t_port, port);
  556. if (priv->chan_tx) {
  557. dma_release_channel(priv->chan_tx);
  558. priv->chan_tx = NULL;
  559. }
  560. if (priv->chan_rx) {
  561. dma_release_channel(priv->chan_rx);
  562. priv->chan_rx = NULL;
  563. }
  564. if (sg_dma_address(&priv->sg_rx))
  565. dma_free_coherent(port->dev, port->fifosize,
  566. sg_virt(&priv->sg_rx),
  567. sg_dma_address(&priv->sg_rx));
  568. return;
  569. }
  570. static bool filter(struct dma_chan *chan, void *slave)
  571. {
  572. struct pch_dma_slave *param = slave;
  573. if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
  574. chan->device->dev)) {
  575. chan->private = param;
  576. return true;
  577. } else {
  578. return false;
  579. }
  580. }
  581. static void pch_request_dma(struct uart_port *port)
  582. {
  583. dma_cap_mask_t mask;
  584. struct dma_chan *chan;
  585. struct pci_dev *dma_dev;
  586. struct pch_dma_slave *param;
  587. struct eg20t_port *priv =
  588. container_of(port, struct eg20t_port, port);
  589. dma_cap_zero(mask);
  590. dma_cap_set(DMA_SLAVE, mask);
  591. dma_dev = pci_get_bus_and_slot(priv->pdev->bus->number,
  592. PCI_DEVFN(0xa, 0)); /* Get DMA's dev
  593. information */
  594. /* Set Tx DMA */
  595. param = &priv->param_tx;
  596. param->dma_dev = &dma_dev->dev;
  597. param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
  598. param->tx_reg = port->mapbase + UART_TX;
  599. chan = dma_request_channel(mask, filter, param);
  600. if (!chan) {
  601. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
  602. __func__);
  603. return;
  604. }
  605. priv->chan_tx = chan;
  606. /* Set Rx DMA */
  607. param = &priv->param_rx;
  608. param->dma_dev = &dma_dev->dev;
  609. param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
  610. param->rx_reg = port->mapbase + UART_RX;
  611. chan = dma_request_channel(mask, filter, param);
  612. if (!chan) {
  613. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
  614. __func__);
  615. dma_release_channel(priv->chan_tx);
  616. priv->chan_tx = NULL;
  617. return;
  618. }
  619. /* Get Consistent memory for DMA */
  620. priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
  621. &priv->rx_buf_dma, GFP_KERNEL);
  622. priv->chan_rx = chan;
  623. }
  624. static void pch_dma_rx_complete(void *arg)
  625. {
  626. struct eg20t_port *priv = arg;
  627. struct uart_port *port = &priv->port;
  628. struct tty_struct *tty = tty_port_tty_get(&port->state->port);
  629. int count;
  630. if (!tty) {
  631. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  632. return;
  633. }
  634. dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
  635. count = dma_push_rx(priv, priv->trigger_level);
  636. if (count)
  637. tty_flip_buffer_push(tty);
  638. tty_kref_put(tty);
  639. async_tx_ack(priv->desc_rx);
  640. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
  641. }
  642. static void pch_dma_tx_complete(void *arg)
  643. {
  644. struct eg20t_port *priv = arg;
  645. struct uart_port *port = &priv->port;
  646. struct circ_buf *xmit = &port->state->xmit;
  647. struct scatterlist *sg = priv->sg_tx_p;
  648. int i;
  649. for (i = 0; i < priv->nent; i++, sg++) {
  650. xmit->tail += sg_dma_len(sg);
  651. port->icount.tx += sg_dma_len(sg);
  652. }
  653. xmit->tail &= UART_XMIT_SIZE - 1;
  654. async_tx_ack(priv->desc_tx);
  655. dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
  656. priv->tx_dma_use = 0;
  657. priv->nent = 0;
  658. kfree(priv->sg_tx_p);
  659. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
  660. }
  661. static int pop_tx(struct eg20t_port *priv, int size)
  662. {
  663. int count = 0;
  664. struct uart_port *port = &priv->port;
  665. struct circ_buf *xmit = &port->state->xmit;
  666. if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
  667. goto pop_tx_end;
  668. do {
  669. int cnt_to_end =
  670. CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  671. int sz = min(size - count, cnt_to_end);
  672. pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
  673. xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
  674. count += sz;
  675. } while (!uart_circ_empty(xmit) && count < size);
  676. pop_tx_end:
  677. dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
  678. count, size - count, jiffies);
  679. return count;
  680. }
  681. static int handle_rx_to(struct eg20t_port *priv)
  682. {
  683. struct pch_uart_buffer *buf;
  684. int rx_size;
  685. int ret;
  686. if (!priv->start_rx) {
  687. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
  688. return 0;
  689. }
  690. buf = &priv->rxbuf;
  691. do {
  692. rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
  693. ret = push_rx(priv, buf->buf, rx_size);
  694. if (ret)
  695. return 0;
  696. } while (rx_size == buf->size);
  697. return PCH_UART_HANDLED_RX_INT;
  698. }
  699. static int handle_rx(struct eg20t_port *priv)
  700. {
  701. return handle_rx_to(priv);
  702. }
  703. static int dma_handle_rx(struct eg20t_port *priv)
  704. {
  705. struct uart_port *port = &priv->port;
  706. struct dma_async_tx_descriptor *desc;
  707. struct scatterlist *sg;
  708. priv = container_of(port, struct eg20t_port, port);
  709. sg = &priv->sg_rx;
  710. sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
  711. sg_dma_len(sg) = priv->trigger_level;
  712. sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
  713. sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
  714. ~PAGE_MASK);
  715. sg_dma_address(sg) = priv->rx_buf_dma;
  716. desc = dmaengine_prep_slave_sg(priv->chan_rx,
  717. sg, 1, DMA_DEV_TO_MEM,
  718. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  719. if (!desc)
  720. return 0;
  721. priv->desc_rx = desc;
  722. desc->callback = pch_dma_rx_complete;
  723. desc->callback_param = priv;
  724. desc->tx_submit(desc);
  725. dma_async_issue_pending(priv->chan_rx);
  726. return PCH_UART_HANDLED_RX_INT;
  727. }
  728. static unsigned int handle_tx(struct eg20t_port *priv)
  729. {
  730. struct uart_port *port = &priv->port;
  731. struct circ_buf *xmit = &port->state->xmit;
  732. int fifo_size;
  733. int tx_size;
  734. int size;
  735. int tx_empty;
  736. if (!priv->start_tx) {
  737. dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
  738. __func__, jiffies);
  739. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  740. priv->tx_empty = 1;
  741. return 0;
  742. }
  743. fifo_size = max(priv->fifo_size, 1);
  744. tx_empty = 1;
  745. if (pop_tx_x(priv, xmit->buf)) {
  746. pch_uart_hal_write(priv, xmit->buf, 1);
  747. port->icount.tx++;
  748. tx_empty = 0;
  749. fifo_size--;
  750. }
  751. size = min(xmit->head - xmit->tail, fifo_size);
  752. if (size < 0)
  753. size = fifo_size;
  754. tx_size = pop_tx(priv, size);
  755. if (tx_size > 0) {
  756. port->icount.tx += tx_size;
  757. tx_empty = 0;
  758. }
  759. priv->tx_empty = tx_empty;
  760. if (tx_empty) {
  761. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  762. uart_write_wakeup(port);
  763. }
  764. return PCH_UART_HANDLED_TX_INT;
  765. }
  766. static unsigned int dma_handle_tx(struct eg20t_port *priv)
  767. {
  768. struct uart_port *port = &priv->port;
  769. struct circ_buf *xmit = &port->state->xmit;
  770. struct scatterlist *sg;
  771. int nent;
  772. int fifo_size;
  773. int tx_empty;
  774. struct dma_async_tx_descriptor *desc;
  775. int num;
  776. int i;
  777. int bytes;
  778. int size;
  779. int rem;
  780. if (!priv->start_tx) {
  781. dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
  782. __func__, jiffies);
  783. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  784. priv->tx_empty = 1;
  785. return 0;
  786. }
  787. if (priv->tx_dma_use) {
  788. dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
  789. __func__, jiffies);
  790. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  791. priv->tx_empty = 1;
  792. return 0;
  793. }
  794. fifo_size = max(priv->fifo_size, 1);
  795. tx_empty = 1;
  796. if (pop_tx_x(priv, xmit->buf)) {
  797. pch_uart_hal_write(priv, xmit->buf, 1);
  798. port->icount.tx++;
  799. tx_empty = 0;
  800. fifo_size--;
  801. }
  802. bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
  803. UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
  804. xmit->tail, UART_XMIT_SIZE));
  805. if (!bytes) {
  806. dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
  807. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  808. uart_write_wakeup(port);
  809. return 0;
  810. }
  811. if (bytes > fifo_size) {
  812. num = bytes / fifo_size + 1;
  813. size = fifo_size;
  814. rem = bytes % fifo_size;
  815. } else {
  816. num = 1;
  817. size = bytes;
  818. rem = bytes;
  819. }
  820. dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
  821. __func__, num, size, rem);
  822. priv->tx_dma_use = 1;
  823. priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
  824. sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
  825. sg = priv->sg_tx_p;
  826. for (i = 0; i < num; i++, sg++) {
  827. if (i == (num - 1))
  828. sg_set_page(sg, virt_to_page(xmit->buf),
  829. rem, fifo_size * i);
  830. else
  831. sg_set_page(sg, virt_to_page(xmit->buf),
  832. size, fifo_size * i);
  833. }
  834. sg = priv->sg_tx_p;
  835. nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
  836. if (!nent) {
  837. dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
  838. return 0;
  839. }
  840. priv->nent = nent;
  841. for (i = 0; i < nent; i++, sg++) {
  842. sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
  843. fifo_size * i;
  844. sg_dma_address(sg) = (sg_dma_address(sg) &
  845. ~(UART_XMIT_SIZE - 1)) + sg->offset;
  846. if (i == (nent - 1))
  847. sg_dma_len(sg) = rem;
  848. else
  849. sg_dma_len(sg) = size;
  850. }
  851. desc = dmaengine_prep_slave_sg(priv->chan_tx,
  852. priv->sg_tx_p, nent, DMA_MEM_TO_DEV,
  853. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  854. if (!desc) {
  855. dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
  856. __func__);
  857. return 0;
  858. }
  859. dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
  860. priv->desc_tx = desc;
  861. desc->callback = pch_dma_tx_complete;
  862. desc->callback_param = priv;
  863. desc->tx_submit(desc);
  864. dma_async_issue_pending(priv->chan_tx);
  865. return PCH_UART_HANDLED_TX_INT;
  866. }
  867. static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
  868. {
  869. u8 fcr = ioread8(priv->membase + UART_FCR);
  870. /* Reset FIFO */
  871. fcr |= UART_FCR_CLEAR_RCVR;
  872. iowrite8(fcr, priv->membase + UART_FCR);
  873. if (lsr & PCH_UART_LSR_ERR)
  874. dev_err(&priv->pdev->dev, "Error data in FIFO\n");
  875. if (lsr & UART_LSR_FE)
  876. dev_err(&priv->pdev->dev, "Framing Error\n");
  877. if (lsr & UART_LSR_PE)
  878. dev_err(&priv->pdev->dev, "Parity Error\n");
  879. if (lsr & UART_LSR_OE)
  880. dev_err(&priv->pdev->dev, "Overrun Error\n");
  881. }
  882. static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
  883. {
  884. struct eg20t_port *priv = dev_id;
  885. unsigned int handled;
  886. u8 lsr;
  887. int ret = 0;
  888. unsigned int iid;
  889. unsigned long flags;
  890. spin_lock_irqsave(&priv->port.lock, flags);
  891. handled = 0;
  892. while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
  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. ret = PCH_UART_HANDLED_MS_INT;
  927. break;
  928. default: /* Never junp to this label */
  929. dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__,
  930. iid, jiffies);
  931. ret = -1;
  932. break;
  933. }
  934. handled |= (unsigned int)ret;
  935. }
  936. if (handled == 0 && iid <= 1) {
  937. if (priv->int_dis_flag)
  938. priv->int_dis_flag = 0;
  939. }
  940. spin_unlock_irqrestore(&priv->port.lock, flags);
  941. return IRQ_RETVAL(handled);
  942. }
  943. /* This function tests whether the transmitter fifo and shifter for the port
  944. described by 'port' is empty. */
  945. static unsigned int pch_uart_tx_empty(struct uart_port *port)
  946. {
  947. struct eg20t_port *priv;
  948. priv = container_of(port, struct eg20t_port, port);
  949. if (priv->tx_empty)
  950. return TIOCSER_TEMT;
  951. else
  952. return 0;
  953. }
  954. /* Returns the current state of modem control inputs. */
  955. static unsigned int pch_uart_get_mctrl(struct uart_port *port)
  956. {
  957. struct eg20t_port *priv;
  958. u8 modem;
  959. unsigned int ret = 0;
  960. priv = container_of(port, struct eg20t_port, port);
  961. modem = pch_uart_hal_get_modem(priv);
  962. if (modem & UART_MSR_DCD)
  963. ret |= TIOCM_CAR;
  964. if (modem & UART_MSR_RI)
  965. ret |= TIOCM_RNG;
  966. if (modem & UART_MSR_DSR)
  967. ret |= TIOCM_DSR;
  968. if (modem & UART_MSR_CTS)
  969. ret |= TIOCM_CTS;
  970. return ret;
  971. }
  972. static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  973. {
  974. u32 mcr = 0;
  975. struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
  976. if (mctrl & TIOCM_DTR)
  977. mcr |= UART_MCR_DTR;
  978. if (mctrl & TIOCM_RTS)
  979. mcr |= UART_MCR_RTS;
  980. if (mctrl & TIOCM_LOOP)
  981. mcr |= UART_MCR_LOOP;
  982. if (priv->mcr & UART_MCR_AFE)
  983. mcr |= UART_MCR_AFE;
  984. if (mctrl)
  985. iowrite8(mcr, priv->membase + UART_MCR);
  986. }
  987. static void pch_uart_stop_tx(struct uart_port *port)
  988. {
  989. struct eg20t_port *priv;
  990. priv = container_of(port, struct eg20t_port, port);
  991. priv->start_tx = 0;
  992. priv->tx_dma_use = 0;
  993. }
  994. static void pch_uart_start_tx(struct uart_port *port)
  995. {
  996. struct eg20t_port *priv;
  997. priv = container_of(port, struct eg20t_port, port);
  998. if (priv->use_dma) {
  999. if (priv->tx_dma_use) {
  1000. dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
  1001. __func__);
  1002. return;
  1003. }
  1004. }
  1005. priv->start_tx = 1;
  1006. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
  1007. }
  1008. static void pch_uart_stop_rx(struct uart_port *port)
  1009. {
  1010. struct eg20t_port *priv;
  1011. priv = container_of(port, struct eg20t_port, port);
  1012. priv->start_rx = 0;
  1013. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
  1014. priv->int_dis_flag = 1;
  1015. }
  1016. /* Enable the modem status interrupts. */
  1017. static void pch_uart_enable_ms(struct uart_port *port)
  1018. {
  1019. struct eg20t_port *priv;
  1020. priv = container_of(port, struct eg20t_port, port);
  1021. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
  1022. }
  1023. /* Control the transmission of a break signal. */
  1024. static void pch_uart_break_ctl(struct uart_port *port, int ctl)
  1025. {
  1026. struct eg20t_port *priv;
  1027. unsigned long flags;
  1028. priv = container_of(port, struct eg20t_port, port);
  1029. spin_lock_irqsave(&port->lock, flags);
  1030. pch_uart_hal_set_break(priv, ctl);
  1031. spin_unlock_irqrestore(&port->lock, flags);
  1032. }
  1033. /* Grab any interrupt resources and initialise any low level driver state. */
  1034. static int pch_uart_startup(struct uart_port *port)
  1035. {
  1036. struct eg20t_port *priv;
  1037. int ret;
  1038. int fifo_size;
  1039. int trigger_level;
  1040. priv = container_of(port, struct eg20t_port, port);
  1041. priv->tx_empty = 1;
  1042. if (port->uartclk)
  1043. priv->uartclk = port->uartclk;
  1044. else
  1045. port->uartclk = priv->uartclk;
  1046. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1047. ret = pch_uart_hal_set_line(priv, default_baud,
  1048. PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
  1049. PCH_UART_HAL_STB1);
  1050. if (ret)
  1051. return ret;
  1052. switch (priv->fifo_size) {
  1053. case 256:
  1054. fifo_size = PCH_UART_HAL_FIFO256;
  1055. break;
  1056. case 64:
  1057. fifo_size = PCH_UART_HAL_FIFO64;
  1058. break;
  1059. case 16:
  1060. fifo_size = PCH_UART_HAL_FIFO16;
  1061. case 1:
  1062. default:
  1063. fifo_size = PCH_UART_HAL_FIFO_DIS;
  1064. break;
  1065. }
  1066. switch (priv->trigger) {
  1067. case PCH_UART_HAL_TRIGGER1:
  1068. trigger_level = 1;
  1069. break;
  1070. case PCH_UART_HAL_TRIGGER_L:
  1071. trigger_level = priv->fifo_size / 4;
  1072. break;
  1073. case PCH_UART_HAL_TRIGGER_M:
  1074. trigger_level = priv->fifo_size / 2;
  1075. break;
  1076. case PCH_UART_HAL_TRIGGER_H:
  1077. default:
  1078. trigger_level = priv->fifo_size - (priv->fifo_size / 8);
  1079. break;
  1080. }
  1081. priv->trigger_level = trigger_level;
  1082. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  1083. fifo_size, priv->trigger);
  1084. if (ret < 0)
  1085. return ret;
  1086. ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
  1087. KBUILD_MODNAME, priv);
  1088. if (ret < 0)
  1089. return ret;
  1090. if (priv->use_dma)
  1091. pch_request_dma(port);
  1092. priv->start_rx = 1;
  1093. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
  1094. uart_update_timeout(port, CS8, default_baud);
  1095. return 0;
  1096. }
  1097. static void pch_uart_shutdown(struct uart_port *port)
  1098. {
  1099. struct eg20t_port *priv;
  1100. int ret;
  1101. priv = container_of(port, struct eg20t_port, port);
  1102. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1103. pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
  1104. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  1105. PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
  1106. if (ret)
  1107. dev_err(priv->port.dev,
  1108. "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
  1109. pch_free_dma(port);
  1110. free_irq(priv->port.irq, priv);
  1111. }
  1112. /* Change the port parameters, including word length, parity, stop
  1113. *bits. Update read_status_mask and ignore_status_mask to indicate
  1114. *the types of events we are interested in receiving. */
  1115. static void pch_uart_set_termios(struct uart_port *port,
  1116. struct ktermios *termios, struct ktermios *old)
  1117. {
  1118. int baud;
  1119. int rtn;
  1120. unsigned int parity, bits, stb;
  1121. struct eg20t_port *priv;
  1122. unsigned long flags;
  1123. priv = container_of(port, struct eg20t_port, port);
  1124. switch (termios->c_cflag & CSIZE) {
  1125. case CS5:
  1126. bits = PCH_UART_HAL_5BIT;
  1127. break;
  1128. case CS6:
  1129. bits = PCH_UART_HAL_6BIT;
  1130. break;
  1131. case CS7:
  1132. bits = PCH_UART_HAL_7BIT;
  1133. break;
  1134. default: /* CS8 */
  1135. bits = PCH_UART_HAL_8BIT;
  1136. break;
  1137. }
  1138. if (termios->c_cflag & CSTOPB)
  1139. stb = PCH_UART_HAL_STB2;
  1140. else
  1141. stb = PCH_UART_HAL_STB1;
  1142. if (termios->c_cflag & PARENB) {
  1143. if (!(termios->c_cflag & PARODD))
  1144. parity = PCH_UART_HAL_PARITY_ODD;
  1145. else
  1146. parity = PCH_UART_HAL_PARITY_EVEN;
  1147. } else
  1148. parity = PCH_UART_HAL_PARITY_NONE;
  1149. /* Only UART0 has auto hardware flow function */
  1150. if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
  1151. priv->mcr |= UART_MCR_AFE;
  1152. else
  1153. priv->mcr &= ~UART_MCR_AFE;
  1154. termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
  1155. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  1156. spin_lock_irqsave(&port->lock, flags);
  1157. uart_update_timeout(port, termios->c_cflag, baud);
  1158. rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
  1159. if (rtn)
  1160. goto out;
  1161. pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
  1162. /* Don't rewrite B0 */
  1163. if (tty_termios_baud_rate(termios))
  1164. tty_termios_encode_baud_rate(termios, baud, baud);
  1165. out:
  1166. spin_unlock_irqrestore(&port->lock, flags);
  1167. }
  1168. static const char *pch_uart_type(struct uart_port *port)
  1169. {
  1170. return KBUILD_MODNAME;
  1171. }
  1172. static void pch_uart_release_port(struct uart_port *port)
  1173. {
  1174. struct eg20t_port *priv;
  1175. priv = container_of(port, struct eg20t_port, port);
  1176. pci_iounmap(priv->pdev, priv->membase);
  1177. pci_release_regions(priv->pdev);
  1178. }
  1179. static int pch_uart_request_port(struct uart_port *port)
  1180. {
  1181. struct eg20t_port *priv;
  1182. int ret;
  1183. void __iomem *membase;
  1184. priv = container_of(port, struct eg20t_port, port);
  1185. ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
  1186. if (ret < 0)
  1187. return -EBUSY;
  1188. membase = pci_iomap(priv->pdev, 1, 0);
  1189. if (!membase) {
  1190. pci_release_regions(priv->pdev);
  1191. return -EBUSY;
  1192. }
  1193. priv->membase = port->membase = membase;
  1194. return 0;
  1195. }
  1196. static void pch_uart_config_port(struct uart_port *port, int type)
  1197. {
  1198. struct eg20t_port *priv;
  1199. priv = container_of(port, struct eg20t_port, port);
  1200. if (type & UART_CONFIG_TYPE) {
  1201. port->type = priv->port_type;
  1202. pch_uart_request_port(port);
  1203. }
  1204. }
  1205. static int pch_uart_verify_port(struct uart_port *port,
  1206. struct serial_struct *serinfo)
  1207. {
  1208. struct eg20t_port *priv;
  1209. priv = container_of(port, struct eg20t_port, port);
  1210. if (serinfo->flags & UPF_LOW_LATENCY) {
  1211. dev_info(priv->port.dev,
  1212. "PCH UART : Use PIO Mode (without DMA)\n");
  1213. priv->use_dma = 0;
  1214. serinfo->flags &= ~UPF_LOW_LATENCY;
  1215. } else {
  1216. #ifndef CONFIG_PCH_DMA
  1217. dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
  1218. __func__);
  1219. return -EOPNOTSUPP;
  1220. #endif
  1221. priv->use_dma = 1;
  1222. priv->use_dma_flag = 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");