cris32-serial.patch 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886
  1. diff -Nur linux-4.0.3.orig/arch/cris/arch-v32/drivers/Kconfig linux-4.0.3/arch/cris/arch-v32/drivers/Kconfig
  2. --- linux-4.0.3.orig/arch/cris/arch-v32/drivers/Kconfig 2015-05-13 14:14:53.000000000 +0200
  3. +++ linux-4.0.3/arch/cris/arch-v32/drivers/Kconfig 2015-05-25 17:20:55.000000000 +0200
  4. @@ -49,24 +49,261 @@
  5. if you do not need DMA to something else.
  6. ser0 can use dma4 or dma6 for output and dma5 or dma7 for input.
  7. +choice
  8. + prompt "Ser0 default port type "
  9. + depends on ETRAX_SERIAL_PORT0
  10. + default ETRAX_SERIAL_PORT0_TYPE_232
  11. + help
  12. + Type of serial port.
  13. +
  14. +config ETRAX_SERIAL_PORT0_TYPE_232
  15. + bool "Ser0 is a RS-232 port"
  16. + help
  17. + Configure serial port 0 to be a RS-232 port.
  18. +
  19. +config ETRAX_SERIAL_PORT0_TYPE_485HD
  20. + bool "Ser0 is a half duplex RS-485 port"
  21. + depends on ETRAX_RS485
  22. + help
  23. + Configure serial port 0 to be a half duplex (two wires) RS-485 port.
  24. +
  25. +config ETRAX_SERIAL_PORT0_TYPE_485FD
  26. + bool "Ser0 is a full duplex RS-485 port"
  27. + depends on ETRAX_RS485
  28. + help
  29. + Configure serial port 0 to be a full duplex (four wires) RS-485 port.
  30. +endchoice
  31. +
  32. +config ETRAX_SER0_DTR_BIT
  33. + string "Ser 0 DTR bit (empty = not used)"
  34. + depends on ETRAX_SERIAL_PORT0
  35. +
  36. +config ETRAX_SER0_RI_BIT
  37. + string "Ser 0 RI bit (empty = not used)"
  38. + depends on ETRAX_SERIAL_PORT0
  39. +
  40. +config ETRAX_SER0_DSR_BIT
  41. + string "Ser 0 DSR bit (empty = not used)"
  42. + depends on ETRAX_SERIAL_PORT0
  43. +
  44. +config ETRAX_SER0_CD_BIT
  45. + string "Ser 0 CD bit (empty = not used)"
  46. + depends on ETRAX_SERIAL_PORT0
  47. +
  48. config ETRAX_SERIAL_PORT1
  49. bool "Serial port 1 enabled"
  50. depends on ETRAXFS_SERIAL
  51. help
  52. Enables the ETRAX FS serial driver for ser1 (ttyS1).
  53. +choice
  54. + prompt "Ser1 default port type"
  55. + depends on ETRAX_SERIAL_PORT1
  56. + default ETRAX_SERIAL_PORT1_TYPE_232
  57. + help
  58. + Type of serial port.
  59. +
  60. +config ETRAX_SERIAL_PORT1_TYPE_232
  61. + bool "Ser1 is a RS-232 port"
  62. + help
  63. + Configure serial port 1 to be a RS-232 port.
  64. +
  65. +config ETRAX_SERIAL_PORT1_TYPE_485HD
  66. + bool "Ser1 is a half duplex RS-485 port"
  67. + depends on ETRAX_RS485
  68. + help
  69. + Configure serial port 1 to be a half duplex (two wires) RS-485 port.
  70. +
  71. +config ETRAX_SERIAL_PORT1_TYPE_485FD
  72. + bool "Ser1 is a full duplex RS-485 port"
  73. + depends on ETRAX_RS485
  74. + help
  75. + Configure serial port 1 to be a full duplex (four wires) RS-485 port.
  76. +endchoice
  77. +
  78. +config ETRAX_SER1_DTR_BIT
  79. + string "Ser 1 DTR bit (empty = not used)"
  80. + depends on ETRAX_SERIAL_PORT1
  81. +
  82. +config ETRAX_SER1_RI_BIT
  83. + string "Ser 1 RI bit (empty = not used)"
  84. + depends on ETRAX_SERIAL_PORT1
  85. +
  86. +config ETRAX_SER1_DSR_BIT
  87. + string "Ser 1 DSR bit (empty = not used)"
  88. + depends on ETRAX_SERIAL_PORT1
  89. +
  90. +config ETRAX_SER1_CD_BIT
  91. + string "Ser 1 CD bit (empty = not used)"
  92. + depends on ETRAX_SERIAL_PORT1
  93. +
  94. config ETRAX_SERIAL_PORT2
  95. bool "Serial port 2 enabled"
  96. depends on ETRAXFS_SERIAL
  97. help
  98. Enables the ETRAX FS serial driver for ser2 (ttyS2).
  99. +choice
  100. + prompt "Ser2 default port type"
  101. + depends on ETRAX_SERIAL_PORT2
  102. + default ETRAX_SERIAL_PORT2_TYPE_232
  103. + help
  104. + What DMA channel to use for ser2
  105. +
  106. +config ETRAX_SERIAL_PORT2_TYPE_232
  107. + bool "Ser2 is a RS-232 port"
  108. + help
  109. + Configure serial port 2 to be a RS-232 port.
  110. +
  111. +config ETRAX_SERIAL_PORT2_TYPE_485HD
  112. + bool "Ser2 is a half duplex RS-485 port"
  113. + depends on ETRAX_RS485
  114. + help
  115. + Configure serial port 2 to be a half duplex (two wires) RS-485 port.
  116. +
  117. +config ETRAX_SERIAL_PORT2_TYPE_485FD
  118. + bool "Ser2 is a full duplex RS-485 port"
  119. + depends on ETRAX_RS485
  120. + help
  121. + Configure serial port 2 to be a full duplex (four wires) RS-485 port.
  122. +endchoice
  123. +
  124. +
  125. +config ETRAX_SER2_DTR_BIT
  126. + string "Ser 2 DTR bit (empty = not used)"
  127. + depends on ETRAX_SERIAL_PORT2
  128. +
  129. +config ETRAX_SER2_RI_BIT
  130. + string "Ser 2 RI bit (empty = not used)"
  131. + depends on ETRAX_SERIAL_PORT2
  132. +
  133. +config ETRAX_SER2_DSR_BIT
  134. + string "Ser 2 DSR bit (empty = not used)"
  135. + depends on ETRAX_SERIAL_PORT2
  136. +
  137. +config ETRAX_SER2_CD_BIT
  138. + string "Ser 2 CD bit (empty = not used)"
  139. + depends on ETRAX_SERIAL_PORT2
  140. +
  141. config ETRAX_SERIAL_PORT3
  142. bool "Serial port 3 enabled"
  143. depends on ETRAXFS_SERIAL
  144. help
  145. Enables the ETRAX FS serial driver for ser3 (ttyS3).
  146. +choice
  147. + prompt "Ser3 default port type"
  148. + depends on ETRAX_SERIAL_PORT3
  149. + default ETRAX_SERIAL_PORT3_TYPE_232
  150. + help
  151. + What DMA channel to use for ser3.
  152. +
  153. +config ETRAX_SERIAL_PORT3_TYPE_232
  154. + bool "Ser3 is a RS-232 port"
  155. + help
  156. + Configure serial port 3 to be a RS-232 port.
  157. +
  158. +config ETRAX_SERIAL_PORT3_TYPE_485HD
  159. + bool "Ser3 is a half duplex RS-485 port"
  160. + depends on ETRAX_RS485
  161. + help
  162. + Configure serial port 3 to be a half duplex (two wires) RS-485 port.
  163. +
  164. +config ETRAX_SERIAL_PORT3_TYPE_485FD
  165. + bool "Ser3 is a full duplex RS-485 port"
  166. + depends on ETRAX_RS485
  167. + help
  168. + Configure serial port 3 to be a full duplex (four wires) RS-485 port.
  169. +endchoice
  170. +
  171. +config ETRAX_SER3_DTR_BIT
  172. + string "Ser 3 DTR bit (empty = not used)"
  173. + depends on ETRAX_SERIAL_PORT3
  174. +
  175. +config ETRAX_SER3_RI_BIT
  176. + string "Ser 3 RI bit (empty = not used)"
  177. + depends on ETRAX_SERIAL_PORT3
  178. +
  179. +config ETRAX_SER3_DSR_BIT
  180. + string "Ser 3 DSR bit (empty = not used)"
  181. + depends on ETRAX_SERIAL_PORT3
  182. +
  183. +config ETRAX_SER3_CD_BIT
  184. + string "Ser 3 CD bit (empty = not used)"
  185. + depends on ETRAX_SERIAL_PORT3
  186. +
  187. +config ETRAX_SERIAL_PORT4
  188. + bool "Serial port 4 enabled"
  189. + depends on ETRAXFS_SERIAL && CRIS_MACH_ARTPEC3
  190. + help
  191. + Enables the ETRAX FS serial driver for ser4 (ttyS4).
  192. +
  193. +choice
  194. + prompt "Ser4 default port type"
  195. + depends on ETRAX_SERIAL_PORT4
  196. + default ETRAX_SERIAL_PORT4_TYPE_232
  197. + help
  198. + What DMA channel to use for ser4.
  199. +
  200. +config ETRAX_SERIAL_PORT4_TYPE_232
  201. + bool "Ser4 is a RS-232 port"
  202. + help
  203. + Configure serial port 4 to be a RS-232 port.
  204. +
  205. +config ETRAX_SERIAL_PORT4_TYPE_485HD
  206. + bool "Ser4 is a half duplex RS-485 port"
  207. + depends on ETRAX_RS485
  208. + help
  209. + Configure serial port 4 to be a half duplex (two wires) RS-485 port.
  210. +
  211. +config ETRAX_SERIAL_PORT4_TYPE_485FD
  212. + bool "Ser4 is a full duplex RS-485 port"
  213. + depends on ETRAX_RS485
  214. + help
  215. + Configure serial port 4 to be a full duplex (four wires) RS-485 port.
  216. +endchoice
  217. +
  218. +choice
  219. + prompt "Ser4 DMA in channel "
  220. + depends on ETRAX_SERIAL_PORT4
  221. + default ETRAX_SERIAL_PORT4_NO_DMA_IN
  222. + help
  223. + What DMA channel to use for ser4.
  224. +
  225. +
  226. +config ETRAX_SERIAL_PORT4_NO_DMA_IN
  227. + bool "Ser4 uses no DMA for input"
  228. + help
  229. + Do not use DMA for ser4 input.
  230. +
  231. +config ETRAX_SERIAL_PORT4_DMA9_IN
  232. + bool "Ser4 uses DMA9 for input"
  233. + depends on ETRAX_SERIAL_PORT4
  234. + help
  235. + Enables the DMA9 input channel for ser4 (ttyS4).
  236. + If you do not enable DMA, an interrupt for each character will be
  237. + used when receiving data.
  238. + Normally you want to use DMA, unless you use the DMA channel for
  239. + something else.
  240. +
  241. +endchoice
  242. +
  243. +config ETRAX_SER4_DTR_BIT
  244. + string "Ser 4 DTR bit (empty = not used)"
  245. + depends on ETRAX_SERIAL_PORT4
  246. +
  247. +config ETRAX_SER4_RI_BIT
  248. + string "Ser 4 RI bit (empty = not used)"
  249. + depends on ETRAX_SERIAL_PORT4
  250. +
  251. +config ETRAX_SER4_DSR_BIT
  252. + string "Ser 4 DSR bit (empty = not used)"
  253. + depends on ETRAX_SERIAL_PORT4
  254. +
  255. +config ETRAX_SER4_CD_BIT
  256. + string "Ser 4 CD bit (empty = not used)"
  257. + depends on ETRAX_SERIAL_PORT4
  258. +
  259. config ETRAX_SYNCHRONOUS_SERIAL
  260. bool "Synchronous serial-port support"
  261. depends on ETRAX_ARCH_V32
  262. diff -Nur linux-4.0.3.orig/arch/cris/include/uapi/asm/ioctls.h linux-4.0.3/arch/cris/include/uapi/asm/ioctls.h
  263. --- linux-4.0.3.orig/arch/cris/include/uapi/asm/ioctls.h 2015-05-13 14:14:53.000000000 +0200
  264. +++ linux-4.0.3/arch/cris/include/uapi/asm/ioctls.h 2015-05-25 17:20:56.000000000 +0200
  265. @@ -5,6 +5,10 @@
  266. #define TIOCSERSETRS485 0x5461 /* enable rs-485 (deprecated) */
  267. #define TIOCSERWRRS485 0x5462 /* write rs-485 */
  268. #define TIOCSRS485 0x5463 /* enable rs-485 */
  269. +#define TIOCSERSETRS485FD 0x5464 /* set rs-485 full/half duplex mode */
  270. +
  271. +
  272. +#define TIOCSERSETDIVISOR 0x5465 /* set the divisor for non standard bauds */
  273. #include <asm-generic/ioctls.h>
  274. diff -Nur linux-4.0.3.orig/drivers/tty/serial/crisv32.c linux-4.0.3/drivers/tty/serial/crisv32.c
  275. --- linux-4.0.3.orig/drivers/tty/serial/crisv32.c 1970-01-01 01:00:00.000000000 +0100
  276. +++ linux-4.0.3/drivers/tty/serial/crisv32.c 2015-05-25 17:20:56.000000000 +0200
  277. @@ -0,0 +1,2581 @@
  278. +/* $Id: crisv32.c,v 1.109 2010-07-09 15:00:44 jespern Exp $
  279. + *
  280. + * Serial port driver for the ETRAX FS chip
  281. + *
  282. + * Copyright (C) 1998-2006 Axis Communications AB
  283. + *
  284. + * Many, many authors. Based once upon a time on serial.c for 16x50.
  285. + *
  286. + * Johan Adolfsson - port to ETRAX FS
  287. + * Mikael Starvik - port to serial_core framework
  288. + *
  289. + */
  290. +
  291. +#include <linux/module.h>
  292. +#include <linux/init.h>
  293. +#include <linux/console.h>
  294. +#include <linux/types.h>
  295. +#include <linux/errno.h>
  296. +#include <linux/serial_core.h>
  297. +#include <linux/delay.h>
  298. +#include <linux/slab.h>
  299. +#include <linux/tty_flip.h>
  300. +
  301. +#include <asm/io.h>
  302. +#include <asm/irq.h>
  303. +#include <asm/uaccess.h>
  304. +
  305. +#include <dma.h>
  306. +#include <arch/system.h>
  307. +#include <mach/pinmux.h>
  308. +#include <hwregs/dma.h>
  309. +#include <hwregs/reg_rdwr.h>
  310. +#include <hwregs/ser_defs.h>
  311. +#include <hwregs/dma_defs.h>
  312. +#include <hwregs/gio_defs.h>
  313. +#include <hwregs/intr_vect_defs.h>
  314. +#include <hwregs/reg_map.h>
  315. +
  316. +#define UART_NR CONFIG_ETRAX_SERIAL_PORTS + 1 /* Ports + dummy port */
  317. +#define SERIAL_RECV_DESCRIPTORS 8
  318. +
  319. +/* We only buffer 255 characters here, no need for more tx descriptors. */
  320. +#define SERIAL_TX_DESCRIPTORS 4
  321. +
  322. +/* Kept for experimental purposes. */
  323. +#define SERIAL_DESCR_BUF_SIZE 256
  324. +#define regi_NULL 0
  325. +#define DMA_WAIT_UNTIL_RESET(inst) \
  326. + do { \
  327. + reg_dma_rw_stat r; \
  328. + do { \
  329. + r = REG_RD(dma, (inst), rw_stat); \
  330. + } while (r.mode != regk_dma_rst); \
  331. + } while (0)
  332. +
  333. +#define __DMA(ch) regi_dma##ch
  334. +#define DMA(ch) __DMA(ch)
  335. +#define DMA_IRQ(ch) (DMA0_INTR_VECT + (ch))
  336. +
  337. +/* Macro to set up control lines for a port. */
  338. +#define SETUP_PINS(port) \
  339. + if (serial_cris_ports[port].used) { \
  340. + if (strcmp(CONFIG_ETRAX_SER##port##_DTR_BIT, "")) \
  341. + crisv32_io_get_name(&serial_cris_ports[port].dtr_pin, \
  342. + CONFIG_ETRAX_SER##port##_DTR_BIT); \
  343. + else \
  344. + serial_cris_ports[port].dtr_pin = dummy_pin; \
  345. + if (strcmp(CONFIG_ETRAX_SER##port##_DSR_BIT, "")) \
  346. + crisv32_io_get_name(&serial_cris_ports[port].dsr_pin, \
  347. + CONFIG_ETRAX_SER##port##_DSR_BIT); \
  348. + else \
  349. + serial_cris_ports[port].dsr_pin = dummy_pin; \
  350. + if (strcmp(CONFIG_ETRAX_SER##port##_RI_BIT, "")) \
  351. + crisv32_io_get_name(&serial_cris_ports[port].ri_pin, \
  352. + CONFIG_ETRAX_SER##port##_RI_BIT); \
  353. + else \
  354. + serial_cris_ports[port].ri_pin = dummy_pin; \
  355. + if (strcmp(CONFIG_ETRAX_SER##port##_CD_BIT, "")) \
  356. + crisv32_io_get_name(&serial_cris_ports[port].cd_pin, \
  357. + CONFIG_ETRAX_SER##port##_CD_BIT); \
  358. + else \
  359. + serial_cris_ports[port].cd_pin = dummy_pin; \
  360. + }
  361. +
  362. +/* Set a serial port register if anything has changed. */
  363. +#define MODIFY_REG(instance, reg, var) \
  364. + if (REG_RD_INT(ser, instance, reg) \
  365. + != REG_TYPE_CONV(int, reg_ser_##reg, var)) \
  366. + REG_WR(ser, instance, reg, var);
  367. +
  368. +/*
  369. + * Regarding RS485 operation in crisv32 serial driver.
  370. + * ---------------------------------------------------
  371. + * RS485 can be run in two modes, full duplex using four wires (485FD) and
  372. + * half duplex using two wires (485HD). The default mode of each serial port
  373. + * is configured in the kernel configuration. The available modes are:
  374. + * RS-232, RS-485 half duplex, and RS-485 full duplex.
  375. + *
  376. + * In the 485HD mode the direction of the data bus must be able to switch.
  377. + * The direction of the transceiver is controlled by the RTS signal. Hence
  378. + * the auto_rts function in the ETRAX FS chip is enabled in this mode, which
  379. + * automatically toggle RTS when transmitting. The initial direction of the
  380. + * port is receiving.
  381. + *
  382. + * In the 485FD mode two transceivers will be used, one in each direction.
  383. + * Usually the hardware can handle both 485HD and 485FD, which implies that
  384. + * one of the transceivers can change direction. Consequently that transceiver
  385. + * must be tied to operate in the opposite direction of the other one, setting
  386. + * and keeping RTS to a fixed value do this.
  387. + *
  388. + * There are two special "ioctl" that can configure the ports. These two are
  389. + * left for backward compatible with older applications. The effects of using
  390. + * them are described below:
  391. + * The TIOCSERSETRS485:
  392. + * This ioctl sets a serial port in 232 mode to 485HD mode or vise versa. The
  393. + * state of the port is kept when closing the port. Note that this ioctl has no
  394. + * effect on a serial port in the 485FD mode.
  395. + * The TIOCSERWRRS485:
  396. + * This ioctl set a serial port in 232 mode to 485HD mode and writes the data
  397. + * "included" in the ioctl to the port. The port will then stay in 485HD mode.
  398. + * Using this ioctl on a serial port in the 485HD mode will transmit the data
  399. + * without changing the mode. Using this ioctl on a serial port in 485FD mode
  400. + * will not change the mode and simply send the data using the 485FD mode.
  401. + */
  402. +
  403. +#define TYPE_232 0
  404. +#define TYPE_485HD 1
  405. +#define TYPE_485FD 2
  406. +
  407. +struct etrax_recv_buffer {
  408. + struct etrax_recv_buffer *next;
  409. + unsigned short length;
  410. + unsigned char error;
  411. + unsigned char pad;
  412. +
  413. + unsigned char buffer[0];
  414. +};
  415. +
  416. +struct uart_cris_port {
  417. + struct uart_port port;
  418. +
  419. + int initialized;
  420. + int used;
  421. + int irq;
  422. +
  423. + /* Used to check if port enabled as well by testing for zero. */
  424. + reg_scope_instances regi_ser;
  425. + reg_scope_instances regi_dmain;
  426. + reg_scope_instances regi_dmaout;
  427. +
  428. + struct crisv32_iopin dtr_pin;
  429. + struct crisv32_iopin dsr_pin;
  430. + struct crisv32_iopin ri_pin;
  431. + struct crisv32_iopin cd_pin;
  432. +
  433. + struct dma_descr_context tr_context_descr
  434. + __attribute__ ((__aligned__(32)));
  435. + struct dma_descr_data tr_descr[SERIAL_TX_DESCRIPTORS]
  436. + __attribute__ ((__aligned__(32)));
  437. + struct dma_descr_context rec_context_descr
  438. + __attribute__ ((__aligned__(32)));
  439. + struct dma_descr_data rec_descr[SERIAL_RECV_DESCRIPTORS]
  440. + __attribute__ ((__aligned__(32)));
  441. +
  442. + /* This is the first one in the list the HW is working on now. */
  443. + struct dma_descr_data* first_tx_descr;
  444. +
  445. + /* This is the last one in the list the HW is working on now. */
  446. + struct dma_descr_data* last_tx_descr;
  447. +
  448. + /* This is how many characters the HW is working on now. */
  449. + unsigned int tx_pending_chars;
  450. +
  451. + int tx_started;
  452. + unsigned int cur_rec_descr;
  453. + struct etrax_recv_buffer *first_recv_buffer;
  454. + struct etrax_recv_buffer *last_recv_buffer;
  455. +
  456. + unsigned int recv_cnt;
  457. + unsigned int max_recv_cnt;
  458. +
  459. + /* The time for 1 char, in usecs. */
  460. + unsigned long char_time_usec;
  461. +
  462. + /* Last tx usec in the jiffies. */
  463. + unsigned long last_tx_active_usec;
  464. +
  465. + /* Last tx time in jiffies. */
  466. + unsigned long last_tx_active;
  467. +
  468. + /* Last rx usec in the jiffies. */
  469. + unsigned long last_rx_active_usec;
  470. +
  471. + /* Last rx time in jiffies. */
  472. + unsigned long last_rx_active;
  473. +
  474. +#ifdef CONFIG_ETRAX_RS485
  475. + /* RS-485 support, duh. */
  476. + struct rs485_control rs485;
  477. +#endif
  478. + int port_type;
  479. + int write_ongoing;
  480. +};
  481. +
  482. +extern struct uart_driver serial_cris_driver;
  483. +static struct uart_port *console_port;
  484. +static int console_baud = 115200;
  485. +static struct uart_cris_port serial_cris_ports[UART_NR] = {
  486. +{
  487. +#ifdef CONFIG_ETRAX_SERIAL_PORT0
  488. + .used = 1,
  489. + .irq = SER0_INTR_VECT,
  490. + .regi_ser = regi_ser0,
  491. + /*
  492. + * We initialize the dma stuff like this to get a compiler error
  493. + * if a CONFIG is missing
  494. + */
  495. + .regi_dmain =
  496. +# ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA7_IN
  497. + regi_dma7,
  498. +# elif defined(CONFIG_ETRAX_SERIAL_PORT0_DMA1_IN)
  499. + regi_dma1,
  500. +# elif defined CONFIG_ETRAX_SERIAL_PORT0_NO_DMA_IN
  501. + regi_NULL,
  502. +# endif
  503. +
  504. + .regi_dmaout =
  505. +# ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA6_OUT
  506. + regi_dma6,
  507. +# elif defined(CONFIG_ETRAX_SERIAL_PORT0_DMA7_OUT)
  508. + regi_dma7,
  509. +# else
  510. + regi_NULL,
  511. +# endif
  512. +
  513. +# ifdef CONFIG_ETRAX_RS485
  514. +# ifdef CONFIG_ETRAX_SERIAL_PORT0_TYPE_485HD
  515. + .port_type = TYPE_485HD,
  516. +# endif
  517. +# ifdef CONFIG_ETRAX_SERIAL_PORT0_TYPE_485FD
  518. + .port_type = TYPE_485FD,
  519. +# endif
  520. +# endif
  521. +#else
  522. + .regi_ser = regi_NULL,
  523. + .regi_dmain = regi_NULL,
  524. + .regi_dmaout = regi_NULL,
  525. +#endif
  526. + .write_ongoing = 0
  527. +}, /* ttyS0 */
  528. +{
  529. +#ifdef CONFIG_ETRAX_SERIAL_PORT1
  530. + .used = 1,
  531. + .irq = SER1_INTR_VECT,
  532. + .regi_ser = regi_ser1,
  533. + .regi_dmain =
  534. +# ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA5_IN
  535. + regi_dma5,
  536. +# elif defined(CONFIG_ETRAX_SERIAL_PORT1_NO_DMA_IN)
  537. + regi_NULL,
  538. +# endif
  539. +
  540. + .regi_dmaout =
  541. +# ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA4_OUT
  542. + regi_dma4,
  543. +# elif defined(CONFIG_ETRAX_SERIAL_PORT1_NO_DMA_OUT)
  544. + regi_NULL,
  545. +# endif
  546. +
  547. +# ifdef CONFIG_ETRAX_RS485
  548. +# ifdef CONFIG_ETRAX_SERIAL_PORT1_TYPE_485HD
  549. + .port_type = TYPE_485HD,
  550. +# endif
  551. +# ifdef CONFIG_ETRAX_SERIAL_PORT1_TYPE_485FD
  552. + .port_type = TYPE_485FD,
  553. +# endif
  554. +# endif
  555. +#else
  556. + .regi_ser = regi_NULL,
  557. + .regi_dmain = regi_NULL,
  558. + .regi_dmaout = regi_NULL,
  559. +#endif
  560. + .write_ongoing = 0
  561. +}, /* ttyS1 */
  562. +{
  563. +#ifdef CONFIG_ETRAX_SERIAL_PORT2
  564. + .used = 1,
  565. + .irq = SER2_INTR_VECT,
  566. + .regi_ser = regi_ser2,
  567. + .regi_dmain =
  568. +# ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA3_IN
  569. + regi_dma3,
  570. +# elif defined(CONFIG_ETRAX_SERIAL_PORT2_DMA7_IN)
  571. + regi_dma7,
  572. +# elif defined(CONFIG_ETRAX_SERIAL_PORT2_NO_DMA_IN)
  573. + regi_NULL,
  574. +# endif
  575. +
  576. + .regi_dmaout =
  577. +# ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA2_OUT
  578. + regi_dma2,
  579. +# elif defined(CONFIG_ETRAX_SERIAL_PORT2_DMA6_OUT)
  580. + regi_dma6,
  581. +# elif defined(CONFIG_ETRAX_SERIAL_PORT2_NO_DMA_OUT)
  582. + regi_NULL,
  583. +# endif
  584. +
  585. +# ifdef CONFIG_ETRAX_RS485
  586. +# ifdef CONFIG_ETRAX_SERIAL_PORT2_TYPE_485HD
  587. + .port_type = TYPE_485HD,
  588. +# endif
  589. +# ifdef CONFIG_ETRAX_SERIAL_PORT2_TYPE_485FD
  590. + .port_type = TYPE_485FD,
  591. +# endif
  592. +# endif
  593. +#else
  594. + .regi_ser = regi_NULL,
  595. + .regi_dmain = regi_NULL,
  596. + .regi_dmaout = regi_NULL,
  597. +#endif
  598. + .write_ongoing = 0
  599. +}, /* ttyS2 */
  600. +{
  601. +#ifdef CONFIG_ETRAX_SERIAL_PORT3
  602. + .used = 1,
  603. + .irq = SER3_INTR_VECT,
  604. + .regi_ser = regi_ser3,
  605. + .regi_dmain =
  606. +# ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA9_IN
  607. + regi_dma9,
  608. +# elif defined(CONFIG_ETRAX_SERIAL_PORT3_DMA4_IN)
  609. + regi_dma3,
  610. +# else
  611. + regi_NULL,
  612. +# endif
  613. +
  614. + .regi_dmaout =
  615. +# ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA8_OUT
  616. + regi_dma8,
  617. +# elif defined(CONFIG_ETRAX_SERIAL_PORT3_DMA2_OUT)
  618. + regi_dma2,
  619. +# else
  620. + regi_NULL,
  621. +# endif
  622. +# ifdef CONFIG_ETRAX_RS485
  623. +# ifdef CONFIG_ETRAX_SERIAL_PORT3_TYPE_485HD
  624. + .port_type = TYPE_485HD,
  625. +# endif
  626. +# ifdef CONFIG_ETRAX_SERIAL_PORT3_TYPE_485FD
  627. + .port_type = TYPE_485FD,
  628. +# endif
  629. +# endif
  630. +#else
  631. + .regi_ser = regi_NULL,
  632. + .regi_dmain = regi_NULL,
  633. + .regi_dmaout = regi_NULL,
  634. +#endif
  635. + .write_ongoing = 0
  636. +}, /* ttyS3 */
  637. +#if CONFIG_ETRAX_SERIAL_PORTS == 5
  638. +{
  639. +#ifdef CONFIG_ETRAX_SERIAL_PORT4
  640. + .used = 1,
  641. + .irq = SER4_INTR_VECT,
  642. + .regi_ser = regi_ser4,
  643. + .regi_dmain =
  644. +# ifdef CONFIG_ETRAX_SERIAL_PORT4_DMA9_IN
  645. + regi_dma9,
  646. +# else
  647. + regi_NULL,
  648. +# endif
  649. +
  650. + .regi_dmaout = regi_NULL,
  651. +# ifdef CONFIG_ETRAX_RS485
  652. +# ifdef CONFIG_ETRAX_SERIAL_PORT4_TYPE_485HD
  653. + .port_type = TYPE_485HD,
  654. +# endif
  655. +# ifdef CONFIG_ETRAX_SERIAL_PORT4_TYPE_485FD
  656. + .port_type = TYPE_485FD,
  657. +# endif
  658. +# endif
  659. +#else
  660. + .regi_ser = regi_NULL,
  661. + .regi_dmain = regi_NULL,
  662. + .regi_dmaout = regi_NULL,
  663. +#endif
  664. + .write_ongoing = 0
  665. +}, /* ttyS4 */
  666. +#endif
  667. +{
  668. +#ifdef CONFIG_ETRAX_DEBUG_PORT_NULL
  669. + .used = 1,
  670. +#endif
  671. + .regi_ser = regi_NULL,
  672. + .write_ongoing = 0
  673. +} /* Dummy console port */
  674. +
  675. +};
  676. +
  677. +/* Dummy pin used for unused CD, DSR, DTR and RI signals. */
  678. +static unsigned long io_dummy;
  679. +static struct crisv32_ioport dummy_port =
  680. +{
  681. + &io_dummy,
  682. + &io_dummy,
  683. + &io_dummy,
  684. + 32
  685. +};
  686. +static struct crisv32_iopin dummy_pin =
  687. +{
  688. + &dummy_port,
  689. + 0
  690. +};
  691. +
  692. +static int selected_console =
  693. +#if defined(CONFIG_ETRAX_DEBUG_PORT0)
  694. +0;
  695. +#elif defined(CONFIG_ETRAX_DEBUG_PORT1)
  696. +1;
  697. +#elif defined(CONFIG_ETRAX_DEBUG_PORT2)
  698. +2;
  699. +#elif defined(CONFIG_ETRAX_DEBUG_PORT3)
  700. +3;
  701. +#elif defined(CONFIG_ETRAX_DEBUG_PORT4)
  702. +4;
  703. +#else /* CONFIG_ETRAX_DEBUG_PORT_NULL */
  704. +#if CONFIG_ETRAX_SERIAL_PORTS == 5
  705. +5;
  706. +#else
  707. +4;
  708. +#endif
  709. +#endif
  710. +
  711. +extern void reset_watchdog(void);
  712. +
  713. +static void serial_cris_stop_rx(struct uart_port *port);
  714. +
  715. +/*
  716. + * Interrupts are disabled on entering
  717. + */
  718. +#ifndef CONFIG_ETRAX_VCS_SIM
  719. +static void
  720. +cris_console_write(struct console *co, const char *s, unsigned int count)
  721. +{
  722. + struct uart_cris_port *up;
  723. + int i;
  724. + reg_ser_r_stat_din stat;
  725. + reg_ser_rw_tr_dma_en tr_dma_en, old;
  726. +
  727. + up = &serial_cris_ports[selected_console];
  728. +
  729. + /*
  730. + * This function isn't covered by the struct uart_ops, so we
  731. + * have to check manually that the port really is there,
  732. + * configured and live.
  733. + */
  734. + if (!up->regi_ser)
  735. + return;
  736. +
  737. + /* Switch to manual mode. */
  738. + tr_dma_en = old = REG_RD (ser, up->regi_ser, rw_tr_dma_en);
  739. + if (tr_dma_en.en == regk_ser_yes) {
  740. + tr_dma_en.en = regk_ser_no;
  741. + REG_WR(ser, up->regi_ser, rw_tr_dma_en, tr_dma_en);
  742. + }
  743. +
  744. + /* Send data. */
  745. + for (i = 0; i < count; i++) {
  746. + /* LF -> CRLF */
  747. + if (s[i] == '\n') {
  748. + do {
  749. + stat = REG_RD (ser, up->regi_ser, r_stat_din);
  750. + } while (!stat.tr_rdy);
  751. + REG_WR_INT (ser, up->regi_ser, rw_dout, '\r');
  752. + }
  753. + /* Wait until transmitter is ready and send. */
  754. + do {
  755. + stat = REG_RD (ser, up->regi_ser, r_stat_din);
  756. + } while (!stat.tr_rdy);
  757. + REG_WR_INT (ser, up->regi_ser, rw_dout, s[i]);
  758. +
  759. + /* Feed watchdog, because this may take looong time. */
  760. + reset_watchdog();
  761. + }
  762. +
  763. + /* Restore mode. */
  764. + if (tr_dma_en.en != old.en)
  765. + REG_WR(ser, up->regi_ser, rw_tr_dma_en, old);
  766. +}
  767. +#else
  768. +
  769. +extern void print_str( const char *str );
  770. +static char buffer[1024];
  771. +static char msg[] = "Debug: ";
  772. +static int buffer_pos = sizeof(msg) - 1;
  773. +
  774. +static void
  775. +cris_console_write(struct console *co, const char *buf, unsigned int len)
  776. +{
  777. + char* pos;
  778. + pos = memchr(buf, '\n', len);
  779. + if (pos) {
  780. + int l = ++pos - buf;
  781. + memcpy(buffer + buffer_pos, buf, l);
  782. + memcpy(buffer, msg, sizeof(msg) - 1);
  783. + buffer[buffer_pos + l] = '\0';
  784. + print_str(buffer);
  785. + buffer_pos = sizeof(msg) - 1;
  786. + if (pos - buf != len) {
  787. + memcpy(buffer + buffer_pos, pos, len - l);
  788. + buffer_pos += len - l;
  789. + }
  790. + } else {
  791. + memcpy(buffer + buffer_pos, buf, len);
  792. + buffer_pos += len;
  793. + }
  794. +}
  795. +#endif
  796. +
  797. +static void cris_serial_port_init(struct uart_port *port, int line);
  798. +static int __init
  799. +cris_console_setup(struct console *co, char *options)
  800. +{
  801. + struct uart_port *port;
  802. + int baud = 115200;
  803. + int bits = 8;
  804. + int parity = 'n';
  805. + int flow = 'n';
  806. +
  807. + if (co->index >= UART_NR)
  808. + co->index = 0;
  809. + if (options)
  810. + selected_console = co->index;
  811. + port = &serial_cris_ports[selected_console].port;
  812. + console_port = port;
  813. +
  814. + co->flags |= CON_CONSDEV;
  815. +
  816. + if (options)
  817. + uart_parse_options(options, &baud, &parity, &bits, &flow);
  818. + console_baud = baud;
  819. + cris_serial_port_init(port, selected_console);
  820. + co->index = port->line;
  821. + uart_set_options(port, co, baud, parity, bits, flow);
  822. +
  823. + return 0;
  824. +}
  825. +
  826. +static struct tty_driver*
  827. +cris_console_device(struct console* co, int *index)
  828. +{
  829. + struct uart_driver *p = co->data;
  830. + *index = selected_console;
  831. + return p->tty_driver;
  832. +}
  833. +
  834. +static struct console cris_console = {
  835. + .name = "ttyS",
  836. + .write = cris_console_write,
  837. + .device = cris_console_device,
  838. + .setup = cris_console_setup,
  839. + .flags = CON_PRINTBUFFER,
  840. + .index = -1,
  841. + .data = &serial_cris_driver,
  842. +};
  843. +
  844. +#define SERIAL_CRIS_CONSOLE &cris_console
  845. +
  846. +struct uart_driver serial_cris_driver = {
  847. + .owner = THIS_MODULE,
  848. + .driver_name = "serial",
  849. + .dev_name = "ttyS",
  850. + .major = TTY_MAJOR,
  851. + .minor = 64,
  852. + .nr = UART_NR,
  853. + .cons = SERIAL_CRIS_CONSOLE,
  854. +};
  855. +
  856. +static int inline crisv32_serial_get_rts(struct uart_cris_port *up)
  857. +{
  858. + reg_scope_instances regi_ser = up->regi_ser;
  859. + /*
  860. + * Return what the user has controlled rts to or
  861. + * what the pin is? (if auto_rts is used it differs during tx)
  862. + */
  863. + reg_ser_r_stat_din rstat = REG_RD(ser, regi_ser, r_stat_din);
  864. + return !(rstat.rts_n == regk_ser_active);
  865. +}
  866. +
  867. +/*
  868. + * A set = 0 means 3.3V on the pin, bitvalue: 0=active, 1=inactive
  869. + * 0=0V , 1=3.3V
  870. + */
  871. +static inline void crisv32_serial_set_rts(struct uart_cris_port *up, int set, int force)
  872. +{
  873. + reg_scope_instances regi_ser = up->regi_ser;
  874. +
  875. +#ifdef CONFIG_ETRAX_RS485
  876. + /* Never toggle RTS if port is in 485 mode. If port is in 485FD mode we
  877. + * do not want to send with the reciever and for 485HD mode auto_rts
  878. + * take care of the RTS for us.
  879. + */
  880. + if (force || !up->rs485.enabled) {
  881. +#else
  882. + {
  883. +#endif
  884. + unsigned long flags;
  885. + reg_ser_rw_rec_ctrl rec_ctrl;
  886. +
  887. + local_irq_save(flags);
  888. + rec_ctrl = REG_RD(ser, regi_ser, rw_rec_ctrl);
  889. +
  890. + if (set)
  891. + rec_ctrl.rts_n = regk_ser_active;
  892. + else
  893. + rec_ctrl.rts_n = regk_ser_inactive;
  894. + REG_WR(ser, regi_ser, rw_rec_ctrl, rec_ctrl);
  895. + local_irq_restore(flags);
  896. + }
  897. +}
  898. +
  899. +/* Input */
  900. +static int inline crisv32_serial_get_cts(struct uart_cris_port *up)
  901. +{
  902. + reg_scope_instances regi_ser = up->regi_ser;
  903. + reg_ser_r_stat_din rstat = REG_RD(ser, regi_ser, r_stat_din);
  904. + return (rstat.cts_n == regk_ser_active);
  905. +}
  906. +
  907. +/*
  908. + * Send a single character for XON/XOFF purposes. We do it in this separate
  909. + * function instead of the alternative support port.x_char, in the ...start_tx
  910. + * function, so we don't mix up this case with possibly enabling transmission
  911. + * of queued-up data (in case that's disabled after *receiving* an XOFF or
  912. + * negative CTS). This function is used for both DMA and non-DMA case; see HW
  913. + * docs specifically blessing sending characters manually when DMA for
  914. + * transmission is enabled and running. We may be asked to transmit despite
  915. + * the transmitter being disabled by a ..._stop_tx call so we need to enable
  916. + * it temporarily but restore the state afterwards.
  917. + *
  918. + * Beware: I'm not sure how the RS-485 stuff is supposed to work. Using
  919. + * XON/XOFF seems problematic if there are several controllers, but if it's
  920. + * actually RS-422 (multi-drop; one sender and multiple receivers), it might
  921. + * Just Work, so don't bail out just because it looks a little suspicious.
  922. + */
  923. +
  924. +void serial_cris_send_xchar(struct uart_port *port, char ch)
  925. +{
  926. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  927. + reg_ser_rw_dout dout = { .data = ch };
  928. + reg_ser_rw_ack_intr ack_intr = { .tr_rdy = regk_ser_yes };
  929. + reg_ser_r_stat_din rstat;
  930. + reg_ser_rw_tr_ctrl prev_tr_ctrl, tr_ctrl;
  931. + reg_scope_instances regi_ser = up->regi_ser;
  932. + unsigned long flags;
  933. +
  934. + /*
  935. + * Wait for tr_rdy in case a character is already being output. Make
  936. + * sure we have integrity between the register reads and the writes
  937. + * below, but don't busy-wait with interrupts off and the port lock
  938. + * taken.
  939. + */
  940. + spin_lock_irqsave(&port->lock, flags);
  941. + do {
  942. + spin_unlock_irqrestore(&port->lock, flags);
  943. + spin_lock_irqsave(&port->lock, flags);
  944. + prev_tr_ctrl = tr_ctrl = REG_RD(ser, regi_ser, rw_tr_ctrl);
  945. + rstat = REG_RD(ser, regi_ser, r_stat_din);
  946. + } while (!rstat.tr_rdy);
  947. +
  948. + /*
  949. + * Ack an interrupt if one was just issued for the previous character
  950. + * that was output. This is required for non-DMA as the interrupt is
  951. + * used as the only indicator that the transmitter is ready and it
  952. + * isn't while this x_char is being transmitted.
  953. + */
  954. + REG_WR(ser, regi_ser, rw_ack_intr, ack_intr);
  955. +
  956. + /* Enable the transmitter in case it was disabled. */
  957. + tr_ctrl.stop = 0;
  958. + REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
  959. +
  960. + /*
  961. + * Finally, send the blessed character; nothing should stop it now,
  962. + * except for an xoff-detected state, which we'll handle below.
  963. + */
  964. + REG_WR(ser, regi_ser, rw_dout, dout);
  965. + up->port.icount.tx++;
  966. +
  967. + /* There might be an xoff state to clear. */
  968. + rstat = REG_RD(ser, up->regi_ser, r_stat_din);
  969. +
  970. + /*
  971. + * Clear any xoff state that *may* have been there to
  972. + * inhibit transmission of the character.
  973. + */
  974. + if (rstat.xoff_detect) {
  975. + reg_ser_rw_xoff_clr xoff_clr = { .clr = 1 };
  976. + reg_ser_rw_tr_dma_en tr_dma_en;
  977. + REG_WR(ser, regi_ser, rw_xoff_clr, xoff_clr);
  978. + tr_dma_en = REG_RD(ser, regi_ser, rw_tr_dma_en);
  979. +
  980. + /*
  981. + * If we had an xoff state but cleared it, instead sneak in a
  982. + * disabled state for the transmitter, after the character we
  983. + * sent. Thus we keep the port disabled, just as if the xoff
  984. + * state was still in effect (or actually, as if stop_tx had
  985. + * been called, as we stop DMA too).
  986. + */
  987. + prev_tr_ctrl.stop = 1;
  988. +
  989. + tr_dma_en.en = 0;
  990. + REG_WR(ser, regi_ser, rw_tr_dma_en, tr_dma_en);
  991. + }
  992. +
  993. + /* Restore "previous" enabled/disabled state of the transmitter. */
  994. + REG_WR(ser, regi_ser, rw_tr_ctrl, prev_tr_ctrl);
  995. +
  996. + spin_unlock_irqrestore(&port->lock, flags);
  997. +}
  998. +
  999. +static void transmit_chars_dma(struct uart_cris_port *up);
  1000. +
  1001. +/*
  1002. + * Do not spin_lock_irqsave or disable interrupts by other means here; it's
  1003. + * already done by the caller.
  1004. + */
  1005. +
  1006. +static void serial_cris_start_tx(struct uart_port *port)
  1007. +{
  1008. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  1009. + reg_scope_instances regi_ser = up->regi_ser;
  1010. + reg_ser_rw_tr_ctrl tr_ctrl;
  1011. +
  1012. + /* we have already done below if a write is ongoing */
  1013. + if (!up->regi_dmaout && up->write_ongoing)
  1014. + return;
  1015. +
  1016. +#ifdef CONFIG_ETRAX_RS485
  1017. + if (up->rs485.enabled)
  1018. + {
  1019. + /* If we are in RS-485 mode, we need to toggle RTS and disable
  1020. + * the receiver before initiating a DMA transfer
  1021. + */
  1022. +
  1023. + if (up->rs485.delay_rts_before_send > 0) {
  1024. + reg_ser_rw_tr_ctrl tr_ctrl = REG_RD(ser, regi_ser, rw_tr_ctrl);
  1025. + tr_ctrl.auto_rts = regk_ser_no;
  1026. + REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
  1027. + crisv32_serial_set_rts(up, up->rs485.rts_on_send, 1);
  1028. + msleep(up->rs485.delay_rts_before_send);
  1029. + tr_ctrl.auto_rts = regk_ser_yes;
  1030. + REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
  1031. + crisv32_serial_set_rts(up, !up->rs485.rts_on_send, 1);
  1032. + }
  1033. + }
  1034. +#endif
  1035. +
  1036. + tr_ctrl = REG_RD(ser, regi_ser, rw_tr_ctrl);
  1037. + tr_ctrl.stop = regk_ser_no;
  1038. + REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
  1039. + if (!up->regi_dmaout) {
  1040. + reg_ser_rw_intr_mask intr_mask =
  1041. + REG_RD(ser, regi_ser, rw_intr_mask);
  1042. + intr_mask.tr_rdy = regk_ser_yes;
  1043. + REG_WR(ser, regi_ser, rw_intr_mask, intr_mask);
  1044. + up->write_ongoing = 1;
  1045. + } else {
  1046. + /*
  1047. + * We're called possibly to re-enable transmission after it
  1048. + * has been disabled. If so, DMA needs to be re-enabled.
  1049. + */
  1050. + reg_ser_rw_tr_dma_en tr_dma_en = { .en = 1 };
  1051. + REG_WR(ser, regi_ser, rw_tr_dma_en, tr_dma_en);
  1052. + transmit_chars_dma(up);
  1053. + }
  1054. +}
  1055. +
  1056. +/*
  1057. + * This function handles both the DMA and non-DMA case by ordering the
  1058. + * transmitter to stop of after the current character. We don't need to wait
  1059. + * for any such character to be completely transmitted; we do that where it
  1060. + * matters, like in serial_cris_set_termios. Don't busy-wait here; see
  1061. + * Documentation/serial/driver: this function is called within
  1062. + * spin_lock_irq{,save} and thus separate ones would be disastrous (when SMP).
  1063. + * There's no documented need to set the txd pin to any particular value;
  1064. + * break setting is controlled solely by serial_cris_break_ctl.
  1065. + */
  1066. +
  1067. +static void serial_cris_stop_tx(struct uart_port *port)
  1068. +{
  1069. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  1070. + reg_scope_instances regi_ser = up->regi_ser;
  1071. + reg_ser_rw_tr_ctrl tr_ctrl;
  1072. + reg_ser_rw_intr_mask intr_mask;
  1073. + reg_ser_rw_tr_dma_en tr_dma_en = {0};
  1074. + reg_ser_rw_xoff_clr xoff_clr = {0};
  1075. +
  1076. + /*
  1077. + * For the non-DMA case, we'd get a tr_rdy interrupt that we're not
  1078. + * interested in as we're not transmitting any characters. For the
  1079. + * DMA case, that interrupt is already turned off, but no reason to
  1080. + * waste code on conditionals here.
  1081. + */
  1082. + intr_mask = REG_RD(ser, regi_ser, rw_intr_mask);
  1083. + intr_mask.tr_rdy = regk_ser_no;
  1084. + REG_WR(ser, regi_ser, rw_intr_mask, intr_mask);
  1085. +
  1086. + tr_ctrl = REG_RD(ser, regi_ser, rw_tr_ctrl);
  1087. + tr_ctrl.stop = 1;
  1088. + REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
  1089. +
  1090. + /*
  1091. + * Always clear possible hardware xoff-detected state here, no need to
  1092. + * unnecessary consider mctrl settings and when they change. We clear
  1093. + * it here rather than in start_tx: both functions are called as the
  1094. + * effect of XOFF processing, but start_tx is also called when upper
  1095. + * levels tell the driver that there are more characters to send, so
  1096. + * avoid adding code there.
  1097. + */
  1098. + xoff_clr.clr = 1;
  1099. + REG_WR(ser, regi_ser, rw_xoff_clr, xoff_clr);
  1100. +
  1101. + /*
  1102. + * Disable transmitter DMA, so that if we're in XON/XOFF, we can send
  1103. + * those single characters without also giving go-ahead for queued up
  1104. + * DMA data.
  1105. + */
  1106. + tr_dma_en.en = 0;
  1107. + REG_WR(ser, regi_ser, rw_tr_dma_en, tr_dma_en);
  1108. +}
  1109. +
  1110. +static void serial_cris_stop_rx(struct uart_port *port)
  1111. +{
  1112. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  1113. + reg_scope_instances regi_ser = up->regi_ser;
  1114. + reg_ser_rw_rec_ctrl rec_ctrl = REG_RD(ser, regi_ser, rw_rec_ctrl);
  1115. +
  1116. + rec_ctrl.en = regk_ser_no;
  1117. + REG_WR(ser, regi_ser, rw_rec_ctrl, rec_ctrl);
  1118. +}
  1119. +
  1120. +static void serial_cris_enable_ms(struct uart_port *port)
  1121. +{
  1122. +}
  1123. +
  1124. +static void check_modem_status(struct uart_cris_port *up)
  1125. +{
  1126. +}
  1127. +
  1128. +static unsigned int serial_cris_tx_empty(struct uart_port *port)
  1129. +{
  1130. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  1131. + unsigned long flags;
  1132. + unsigned int ret;
  1133. + reg_ser_r_stat_din rstat = {0};
  1134. +
  1135. + spin_lock_irqsave(&up->port.lock, flags);
  1136. + if (up->regi_dmaout) {
  1137. + /*
  1138. + * For DMA, before looking at r_stat, we need to check that we
  1139. + * either haven't actually started or that end-of-list is
  1140. + * reached, else a tr_empty indication is just an internal
  1141. + * state. The caller qualifies, if needed, that the
  1142. + * port->info.xmit buffer is empty, so we don't need to
  1143. + * check that.
  1144. + */
  1145. + reg_dma_rw_stat status = REG_RD(dma, up->regi_dmaout, rw_stat);
  1146. +
  1147. + if (!up->tx_started) {
  1148. + ret = 1;
  1149. + goto done;
  1150. + }
  1151. +
  1152. + if (status.list_state != regk_dma_data_at_eol) {
  1153. + ret = 0;
  1154. + goto done;
  1155. + }
  1156. + }
  1157. +
  1158. + rstat = REG_RD(ser, up->regi_ser, r_stat_din);
  1159. + ret = rstat.tr_empty ? TIOCSER_TEMT : 0;
  1160. +
  1161. + done:
  1162. + spin_unlock_irqrestore(&up->port.lock, flags);
  1163. + return ret;
  1164. +}
  1165. +static unsigned int serial_cris_get_mctrl(struct uart_port *port)
  1166. +{
  1167. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  1168. + unsigned int ret;
  1169. +
  1170. + ret = 0;
  1171. + if (crisv32_serial_get_rts(up))
  1172. + ret |= TIOCM_RTS;
  1173. + /* DTR is active low */
  1174. + if (!crisv32_io_rd(&up->dtr_pin))
  1175. + ret |= TIOCM_DTR;
  1176. + /* CD is active low */
  1177. + if (!crisv32_io_rd(&up->cd_pin))
  1178. + ret |= TIOCM_CD;
  1179. + /* RI is active low */
  1180. + if (!crisv32_io_rd(&up->ri_pin))
  1181. + ret |= TIOCM_RI;
  1182. + /* DSR is active low */
  1183. + if (!crisv32_io_rd(&up->dsr_pin))
  1184. + ret |= TIOCM_DSR;
  1185. + if (crisv32_serial_get_cts(up))
  1186. + ret |= TIOCM_CTS;
  1187. + return ret;
  1188. +}
  1189. +
  1190. +static void serial_cris_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1191. +{
  1192. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  1193. +
  1194. + crisv32_serial_set_rts(up, mctrl & TIOCM_RTS ? 1 : 0, 0);
  1195. + /* DTR is active low */
  1196. + crisv32_io_set(&up->dtr_pin, mctrl & TIOCM_DTR ? 0 : 1);
  1197. + /* RI is active low */
  1198. + crisv32_io_set(&up->ri_pin, mctrl & TIOCM_RNG ? 0 : 1);
  1199. + /* CD is active low */
  1200. + crisv32_io_set(&up->cd_pin, mctrl & TIOCM_CD ? 0 : 1);
  1201. +}
  1202. +
  1203. +static void serial_cris_break_ctl(struct uart_port *port, int break_state)
  1204. +{
  1205. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  1206. + unsigned long flags;
  1207. + reg_ser_rw_tr_ctrl tr_ctrl;
  1208. + reg_ser_rw_tr_dma_en tr_dma_en;
  1209. + reg_ser_rw_intr_mask intr_mask;
  1210. +
  1211. + spin_lock_irqsave(&up->port.lock, flags);
  1212. + tr_ctrl = REG_RD(ser, up->regi_ser, rw_tr_ctrl);
  1213. + tr_dma_en = REG_RD(ser, up->regi_ser, rw_tr_dma_en);
  1214. + intr_mask = REG_RD(ser, up->regi_ser, rw_intr_mask);
  1215. +
  1216. + if (break_state != 0) { /* Send break */
  1217. + /*
  1218. + * We need to disable DMA (if used) or tr_rdy interrupts if no
  1219. + * DMA. No need to make this conditional on use of DMA;
  1220. + * disabling will be a no-op for the other mode.
  1221. + */
  1222. + intr_mask.tr_rdy = regk_ser_no;
  1223. + tr_dma_en.en = 0;
  1224. +
  1225. + /*
  1226. + * Stop transmission and set the txd pin to 0 after the
  1227. + * current character. The txd setting will take effect after
  1228. + * any current transmission has completed.
  1229. + */
  1230. + tr_ctrl.stop = 1;
  1231. + tr_ctrl.txd = 0;
  1232. + } else {
  1233. + /* Re-enable either transmit DMA or the serial interrupt. */
  1234. + if (up->regi_dmaout)
  1235. + tr_dma_en.en = 1;
  1236. + else
  1237. + intr_mask.tr_rdy = regk_ser_yes;
  1238. +
  1239. +
  1240. + tr_ctrl.stop = 0;
  1241. + tr_ctrl.txd = 1;
  1242. + }
  1243. + REG_WR(ser, up->regi_ser, rw_tr_ctrl, tr_ctrl);
  1244. + REG_WR(ser, up->regi_ser, rw_tr_dma_en, tr_dma_en);
  1245. + REG_WR(ser, up->regi_ser, rw_intr_mask, intr_mask);
  1246. +
  1247. + spin_unlock_irqrestore(&up->port.lock, flags);
  1248. +}
  1249. +
  1250. +/*
  1251. + * The output DMA channel is free - use it to send as many chars as
  1252. + * possible.
  1253. + */
  1254. +
  1255. +static void
  1256. +transmit_chars_dma(struct uart_cris_port *up)
  1257. +{
  1258. + struct dma_descr_data *descr, *pending_descr, *dmapos;
  1259. + struct dma_descr_data *last_tx_descr;
  1260. + struct circ_buf *xmit = &up->port.state->xmit;
  1261. + unsigned int sentl = 0;
  1262. + reg_dma_rw_ack_intr ack_intr = { .data = regk_dma_yes };
  1263. + reg_dma_rw_stat status;
  1264. + reg_scope_instances regi_dmaout = up->regi_dmaout;
  1265. + unsigned int chars_in_q;
  1266. + unsigned int chars_to_send;
  1267. +
  1268. + /* Acknowledge dma data descriptor irq, if there was one. */
  1269. + REG_WR(dma, regi_dmaout, rw_ack_intr, ack_intr);
  1270. +
  1271. + /*
  1272. + * First get the amount of bytes sent during the last DMA transfer,
  1273. + * and update xmit accordingly.
  1274. + */
  1275. + status = REG_RD(dma, regi_dmaout, rw_stat);
  1276. + if (status.list_state == regk_dma_data_at_eol || !up->tx_started)
  1277. + dmapos = phys_to_virt((int)up->last_tx_descr->next);
  1278. + else
  1279. + dmapos = phys_to_virt(REG_RD_INT(dma, regi_dmaout, rw_data));
  1280. +
  1281. + pending_descr = up->first_tx_descr;
  1282. + while (pending_descr != dmapos) {
  1283. + sentl += pending_descr->after - pending_descr->buf;
  1284. + pending_descr->after = pending_descr->buf = NULL;
  1285. + pending_descr = phys_to_virt((int)pending_descr->next);
  1286. + }
  1287. +
  1288. + up->first_tx_descr = pending_descr;
  1289. + last_tx_descr = up->last_tx_descr;
  1290. +
  1291. + /* Update stats. */
  1292. + up->port.icount.tx += sentl;
  1293. +
  1294. + up->tx_pending_chars -= sentl;
  1295. +
  1296. + /* Update xmit buffer. */
  1297. + xmit->tail = (xmit->tail + sentl) & (UART_XMIT_SIZE - 1);
  1298. +
  1299. + /*
  1300. + * Find out the largest amount of consecutive bytes we want to send
  1301. + * now.
  1302. + */
  1303. + chars_in_q = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  1304. +
  1305. + if (chars_in_q == 0)
  1306. + /* Tell upper layers that we're now idle. */
  1307. + goto done;
  1308. +
  1309. + /* Some of those characters are actually pending output. */
  1310. + chars_to_send = chars_in_q - up->tx_pending_chars;
  1311. +
  1312. + /*
  1313. + * Clamp the new number of pending chars to the advertised
  1314. + * one.
  1315. + */
  1316. + if (chars_to_send + up->tx_pending_chars > up->port.fifosize)
  1317. + chars_to_send = up->port.fifosize - up->tx_pending_chars;
  1318. +
  1319. + /* If we don't want to send any, we're done. */
  1320. + if (chars_to_send == 0)
  1321. + goto done;
  1322. +
  1323. + descr = phys_to_virt((int)last_tx_descr->next);
  1324. +
  1325. + /*
  1326. + * We can't send anything if we could make the condition in
  1327. + * the while-loop above (reaping finished descriptors) be met
  1328. + * immediately before the first iteration. However, don't
  1329. + * mistake the full state for the empty state.
  1330. + */
  1331. + if ((descr == up->first_tx_descr && up->tx_pending_chars != 0)
  1332. + || descr->next == up->first_tx_descr)
  1333. + goto done;
  1334. +
  1335. + /* Set up the descriptor for output. */
  1336. + descr->buf = (void*)virt_to_phys(xmit->buf + xmit->tail
  1337. + + up->tx_pending_chars);
  1338. + descr->after = descr->buf + chars_to_send;
  1339. + descr->eol = 1;
  1340. + descr->out_eop = 0;
  1341. + descr->intr = 1;
  1342. + descr->wait = 0;
  1343. + descr->in_eop = 0;
  1344. + descr->md = 0;
  1345. + /*
  1346. + * Make sure GCC doesn't move this eol clear before the eol set
  1347. + * above.
  1348. + */
  1349. + barrier();
  1350. + last_tx_descr->eol = 0;
  1351. +
  1352. + up->last_tx_descr = descr;
  1353. + up->tx_pending_chars += chars_to_send;
  1354. +
  1355. + if (!up->tx_started) {
  1356. + up->tx_started = 1;
  1357. + up->tr_context_descr.next = 0;
  1358. + up->tr_context_descr.saved_data
  1359. + = (dma_descr_data*)virt_to_phys(descr);
  1360. + up->tr_context_descr.saved_data_buf = descr->buf;
  1361. + DMA_START_CONTEXT(regi_dmaout,
  1362. + virt_to_phys(&up->tr_context_descr));
  1363. + } else
  1364. + DMA_CONTINUE_DATA(regi_dmaout);
  1365. +
  1366. + /* DMA is now running (hopefully). */
  1367. +
  1368. + done:
  1369. + if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1370. + uart_write_wakeup(&up->port);
  1371. +}
  1372. +
  1373. +static void
  1374. +transmit_chars_no_dma(struct uart_cris_port *up)
  1375. +{
  1376. + int max_count;
  1377. + struct circ_buf *xmit = &up->port.state->xmit;
  1378. +
  1379. + reg_scope_instances regi_ser = up->regi_ser;
  1380. + reg_ser_r_stat_din rstat;
  1381. + reg_ser_rw_ack_intr ack_intr = { .tr_rdy = regk_ser_yes };
  1382. +
  1383. + if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  1384. + /* No more to send, so disable the interrupt. */
  1385. + reg_ser_rw_intr_mask intr_mask;
  1386. + intr_mask = REG_RD(ser, regi_ser, rw_intr_mask);
  1387. + intr_mask.tr_rdy = 0;
  1388. + intr_mask.tr_empty = 0;
  1389. + REG_WR(ser, regi_ser, rw_intr_mask, intr_mask);
  1390. + up->write_ongoing=0;
  1391. + return;
  1392. + }
  1393. +
  1394. + /* If the serport is fast, we send up to max_count bytes before
  1395. + exiting the loop. */
  1396. + max_count = 64;
  1397. + do {
  1398. + reg_ser_rw_dout dout = { .data = xmit->buf[xmit->tail] };
  1399. + REG_WR(ser, regi_ser, rw_dout, dout);
  1400. + REG_WR(ser, regi_ser, rw_ack_intr, ack_intr);
  1401. + xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
  1402. + up->port.icount.tx++;
  1403. + if (xmit->head == xmit->tail)
  1404. + break;
  1405. + rstat = REG_RD(ser, regi_ser, r_stat_din);
  1406. + } while ((--max_count > 0) && rstat.tr_rdy);
  1407. +
  1408. + if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1409. + uart_write_wakeup(&up->port);
  1410. +} /* transmit_chars_no_dma */
  1411. +
  1412. +static struct etrax_recv_buffer *
  1413. +alloc_recv_buffer(unsigned int size)
  1414. +{
  1415. + struct etrax_recv_buffer *buffer;
  1416. +
  1417. + if (!(buffer = kmalloc(sizeof *buffer + size, GFP_ATOMIC)))
  1418. + panic("%s: Could not allocate %d bytes buffer\n",
  1419. + __FUNCTION__, size);
  1420. +
  1421. + buffer->next = NULL;
  1422. + buffer->length = 0;
  1423. + buffer->error = TTY_NORMAL;
  1424. +
  1425. + return buffer;
  1426. +}
  1427. +
  1428. +static void
  1429. +append_recv_buffer(struct uart_cris_port *up,
  1430. + struct etrax_recv_buffer *buffer)
  1431. +{
  1432. + unsigned long flags;
  1433. +
  1434. + local_irq_save(flags);
  1435. +
  1436. + if (!up->first_recv_buffer)
  1437. + up->first_recv_buffer = buffer;
  1438. + else
  1439. + up->last_recv_buffer->next = buffer;
  1440. +
  1441. + up->last_recv_buffer = buffer;
  1442. +
  1443. + up->recv_cnt += buffer->length;
  1444. + if (up->recv_cnt > up->max_recv_cnt)
  1445. + up->max_recv_cnt = up->recv_cnt;
  1446. +
  1447. + local_irq_restore(flags);
  1448. +}
  1449. +
  1450. +static int
  1451. +add_char_and_flag(struct uart_cris_port *up, unsigned char data,
  1452. + unsigned char flag)
  1453. +{
  1454. + struct etrax_recv_buffer *buffer;
  1455. +
  1456. + buffer = alloc_recv_buffer(4);
  1457. + buffer->length = 1;
  1458. + buffer->error = flag;
  1459. + buffer->buffer[0] = data;
  1460. +
  1461. + append_recv_buffer(up, buffer);
  1462. +
  1463. + up->port.icount.rx++;
  1464. +
  1465. + return 1;
  1466. +}
  1467. +
  1468. +static void
  1469. +flush_to_flip_buffer(struct uart_cris_port *up)
  1470. +{
  1471. + struct etrax_recv_buffer *buffer;
  1472. +
  1473. + if (!up->first_recv_buffer)
  1474. + return;
  1475. +
  1476. + while ((buffer = up->first_recv_buffer)) {
  1477. + unsigned int count = (unsigned int)
  1478. + tty_insert_flip_string(&up->port.state->port,
  1479. + buffer->buffer,
  1480. + buffer->length);
  1481. +
  1482. + up->recv_cnt -= count;
  1483. +
  1484. + if (count == buffer->length) {
  1485. + up->first_recv_buffer = buffer->next;
  1486. + kfree(buffer);
  1487. + } else {
  1488. + buffer->length -= count;
  1489. + memmove(buffer->buffer, buffer->buffer + count,
  1490. + buffer->length);
  1491. + buffer->error = TTY_NORMAL;
  1492. + }
  1493. + }
  1494. +
  1495. + if (!up->first_recv_buffer)
  1496. + up->last_recv_buffer = NULL;
  1497. +
  1498. + /* This call includes a check for low-latency. */
  1499. + tty_flip_buffer_push(&up->port.state->port);
  1500. +}
  1501. +
  1502. +static unsigned int
  1503. +handle_descr_data(struct uart_cris_port *up, struct dma_descr_data *descr,
  1504. + unsigned int recvl)
  1505. +{
  1506. + struct etrax_recv_buffer *buffer
  1507. + = phys_to_virt((unsigned long)descr->buf) - sizeof *buffer;
  1508. +
  1509. + if (up->recv_cnt + recvl > 65536) {
  1510. + printk(KERN_ERR "Too much pending incoming data on %s!"
  1511. + " Dropping %u bytes.\n", up->port.state->port.tty->name,
  1512. + recvl);
  1513. + return 0;
  1514. + }
  1515. +
  1516. + buffer->length = recvl;
  1517. +
  1518. + append_recv_buffer(up, buffer);
  1519. +
  1520. + flush_to_flip_buffer(up);
  1521. +
  1522. + buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE);
  1523. + descr->buf = (void*)virt_to_phys(buffer->buffer);
  1524. + descr->after = descr->buf + SERIAL_DESCR_BUF_SIZE;
  1525. +
  1526. + return recvl;
  1527. +}
  1528. +
  1529. +static unsigned int
  1530. +handle_all_descr_data(struct uart_cris_port *up)
  1531. +{
  1532. + struct dma_descr_data *descr
  1533. + = &up->rec_descr[(up->cur_rec_descr - 1)
  1534. + % SERIAL_RECV_DESCRIPTORS];
  1535. + struct dma_descr_data *prev_descr;
  1536. + unsigned int recvl;
  1537. + unsigned int ret = 0;
  1538. + reg_scope_instances regi_dmain = up->regi_dmain;
  1539. +
  1540. + while (1) {
  1541. + prev_descr = descr;
  1542. + descr = &up->rec_descr[up->cur_rec_descr];
  1543. +
  1544. + if (descr == phys_to_virt(REG_RD(dma, regi_dmain, rw_data)))
  1545. + break;
  1546. +
  1547. + if (++up->cur_rec_descr == SERIAL_RECV_DESCRIPTORS)
  1548. + up->cur_rec_descr = 0;
  1549. +
  1550. + /* Find out how many bytes were read. */
  1551. + recvl = descr->after - descr->buf;
  1552. +
  1553. + /* Update stats. */
  1554. + up->port.icount.rx += recvl;
  1555. +
  1556. + ret += handle_descr_data(up, descr, recvl);
  1557. + descr->eol = 1;
  1558. + /*
  1559. + * Make sure GCC doesn't move this eol clear before the
  1560. + * eol set above.
  1561. + */
  1562. + barrier();
  1563. + prev_descr->eol = 0;
  1564. + flush_dma_descr(descr,1); // Cache bug workaround
  1565. + flush_dma_descr(prev_descr,0); // Cache bug workaround
  1566. + }
  1567. +
  1568. + return ret;
  1569. +}
  1570. +
  1571. +static void
  1572. +receive_chars_dma(struct uart_cris_port *up)
  1573. +{
  1574. + reg_ser_r_stat_din rstat;
  1575. + reg_dma_rw_ack_intr ack_intr = {0};
  1576. +
  1577. + /* Acknowledge both dma_descr and dma_eop irq. */
  1578. + ack_intr.data = 1;
  1579. + ack_intr.in_eop = 1;
  1580. + REG_WR(dma, up->regi_dmain, rw_ack_intr, ack_intr);
  1581. +
  1582. + handle_all_descr_data(up);
  1583. +
  1584. + /* Read the status register to detect errors. */
  1585. + rstat = REG_RD(ser, up->regi_ser, r_stat_din);
  1586. +
  1587. + if (rstat.framing_err | rstat.par_err | rstat.orun) {
  1588. + /*
  1589. + * If we got an error, we must reset it by reading the
  1590. + * rs_stat_din register and put the data in buffer manually.
  1591. + */
  1592. + reg_ser_rs_stat_din stat_din;
  1593. + stat_din = REG_RD(ser, up->regi_ser, rs_stat_din);
  1594. +
  1595. + if (stat_din.par_err)
  1596. + add_char_and_flag(up, stat_din.data, TTY_PARITY);
  1597. + else if (stat_din.orun)
  1598. + add_char_and_flag(up, stat_din.data, TTY_OVERRUN);
  1599. + else if (stat_din.framing_err)
  1600. + add_char_and_flag(up, stat_din.data, TTY_FRAME);
  1601. + }
  1602. +
  1603. + /* Restart the receiving DMA, in case it got stuck on an EOL. */
  1604. + DMA_CONTINUE_DATA(up->regi_dmain);
  1605. +}
  1606. +
  1607. +void receive_chars_no_dma(struct uart_cris_port *up)
  1608. +{
  1609. + reg_ser_rs_stat_din stat_din;
  1610. + reg_ser_r_stat_din rstat;
  1611. + struct uart_icount *icount;
  1612. + int max_count = 16;
  1613. + char flag;
  1614. + reg_ser_rw_ack_intr ack_intr = { 0 };
  1615. +
  1616. + rstat = REG_RD(ser, up->regi_ser, r_stat_din);
  1617. + up->last_rx_active_usec = GET_JIFFIES_USEC();
  1618. + up->last_rx_active = jiffies;
  1619. + icount = &up->port.icount;
  1620. +
  1621. + do {
  1622. + stat_din = REG_RD(ser, up->regi_ser, rs_stat_din);
  1623. +
  1624. + flag = TTY_NORMAL;
  1625. + ack_intr.dav = 1;
  1626. + REG_WR(ser, up->regi_ser, rw_ack_intr, ack_intr);
  1627. + icount->rx++;
  1628. +
  1629. + if (stat_din.framing_err | stat_din.par_err | stat_din.orun) {
  1630. + if (stat_din.data == 0x00 &&
  1631. + stat_din.framing_err) {
  1632. + /* Most likely a break. */
  1633. + flag = TTY_BREAK;
  1634. + icount->brk++;
  1635. + } else if (stat_din.par_err) {
  1636. + flag = TTY_PARITY;
  1637. + icount->parity++;
  1638. + } else if (stat_din.orun) {
  1639. + flag = TTY_OVERRUN;
  1640. + icount->overrun++;
  1641. + } else if (stat_din.framing_err) {
  1642. + flag = TTY_FRAME;
  1643. + icount->frame++;
  1644. + }
  1645. + }
  1646. +
  1647. + /*
  1648. + * If this becomes important, we probably *could* handle this
  1649. + * gracefully by keeping track of the unhandled character.
  1650. + */
  1651. + if (!tty_insert_flip_char(&up->port.state->port, stat_din.data, flag))
  1652. + panic("%s: No tty buffer space", __FUNCTION__);
  1653. + rstat = REG_RD(ser, up->regi_ser, r_stat_din);
  1654. + } while (rstat.dav && (max_count-- > 0));
  1655. + spin_unlock(&up->port.lock);
  1656. + tty_flip_buffer_push(&up->port.state->port);
  1657. + spin_lock(&up->port.lock);
  1658. +} /* receive_chars_no_dma */
  1659. +
  1660. +/*
  1661. + * DMA output channel interrupt handler.
  1662. + * this interrupt is called from DMA2(ser2), DMA8(ser3), DMA6(ser0) or
  1663. + * DMA4(ser1) when they have finished a descriptor with the intr flag set.
  1664. + */
  1665. +static irqreturn_t dma_tr_interrupt(int irq, void *dev_id)
  1666. +{
  1667. + struct uart_cris_port *up = (struct uart_cris_port *)dev_id;
  1668. + reg_dma_r_masked_intr masked_intr;
  1669. + reg_scope_instances regi_dmaout;
  1670. + int handled = 0;
  1671. +
  1672. + spin_lock(&up->port.lock);
  1673. + regi_dmaout = up->regi_dmaout;
  1674. + if (!regi_dmaout) {
  1675. + spin_unlock(&up->port.lock);
  1676. + return IRQ_NONE;
  1677. + }
  1678. +
  1679. + /*
  1680. + * Check for dma_descr (don't need to check for dma_eop in
  1681. + * output DMA for serial).
  1682. + */
  1683. + masked_intr = REG_RD(dma, regi_dmaout, r_masked_intr);
  1684. +
  1685. + if (masked_intr.data) {
  1686. + /* We can send a new dma bunch. make it so. */
  1687. +
  1688. + /*
  1689. + * Read jiffies_usec first.
  1690. + * We want this time to be as late as possible.
  1691. + */
  1692. + up->last_tx_active_usec = GET_JIFFIES_USEC();
  1693. + up->last_tx_active = jiffies;
  1694. + transmit_chars_dma(up);
  1695. + handled = 1;
  1696. + }
  1697. + check_modem_status(up);
  1698. + spin_unlock(&up->port.lock);
  1699. + return IRQ_RETVAL(handled);
  1700. +}
  1701. +
  1702. +/* DMA input channel interrupt handler. */
  1703. +
  1704. +static irqreturn_t
  1705. +dma_rec_interrupt(int irq, void *dev_id)
  1706. +{
  1707. + struct uart_cris_port *up = (struct uart_cris_port *)dev_id;
  1708. + reg_dma_r_masked_intr masked_intr;
  1709. + reg_scope_instances regi_dmain;
  1710. + int handled = 0;
  1711. +
  1712. + spin_lock(&up->port.lock);
  1713. + regi_dmain = up->regi_dmain;
  1714. + if (!regi_dmain) {
  1715. + spin_unlock(&up->port.lock);
  1716. + return IRQ_NONE;
  1717. + }
  1718. +
  1719. + /* Check for both dma_eop and dma_descr for the input dma channel. */
  1720. + masked_intr = REG_RD(dma, regi_dmain, r_masked_intr);
  1721. + if (masked_intr.data || masked_intr.in_eop) {
  1722. + /* We have received something. */
  1723. + receive_chars_dma(up);
  1724. + handled = 1;
  1725. + }
  1726. + check_modem_status(up);
  1727. + spin_unlock(&up->port.lock);
  1728. + return IRQ_RETVAL(handled);
  1729. +}
  1730. +
  1731. +/* "Normal" serial port interrupt handler - both rx and tx. */
  1732. +
  1733. +static irqreturn_t
  1734. +ser_interrupt(int irq, void *dev_id)
  1735. +{
  1736. + struct uart_cris_port *up = (struct uart_cris_port *)dev_id;
  1737. + reg_scope_instances regi_ser;
  1738. + int handled = 0;
  1739. +
  1740. + spin_lock(&up->port.lock);
  1741. + if (up->regi_dmain && up->regi_dmaout) {
  1742. + spin_unlock(&up->port.lock);
  1743. + return IRQ_NONE;
  1744. + }
  1745. +
  1746. + regi_ser = up->regi_ser;
  1747. +
  1748. + if (regi_ser) {
  1749. + reg_ser_r_masked_intr masked_intr;
  1750. + masked_intr = REG_RD(ser, regi_ser, r_masked_intr);
  1751. + /*
  1752. + * Check what interrupts are active before taking
  1753. + * actions. If DMA is used the interrupt shouldn't
  1754. + * be enabled.
  1755. + */
  1756. + if (masked_intr.dav) {
  1757. + receive_chars_no_dma(up);
  1758. + handled = 1;
  1759. + }
  1760. + check_modem_status(up);
  1761. +
  1762. + if (masked_intr.tr_rdy) {
  1763. + transmit_chars_no_dma(up);
  1764. + handled = 1;
  1765. + }
  1766. + }
  1767. + spin_unlock(&up->port.lock);
  1768. + return IRQ_RETVAL(handled);
  1769. +} /* ser_interrupt */
  1770. +
  1771. +static int start_recv_dma(struct uart_cris_port *up)
  1772. +{
  1773. + struct dma_descr_data *descr = up->rec_descr;
  1774. + struct etrax_recv_buffer *buffer;
  1775. + int i;
  1776. +
  1777. + /* Set up the receiving descriptors. */
  1778. + for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++) {
  1779. + buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE);
  1780. + descr[i].next = (void*)virt_to_phys(&descr[i+1]);
  1781. + descr[i].buf = (void*)virt_to_phys(buffer->buffer);
  1782. + descr[i].after = descr[i].buf + SERIAL_DESCR_BUF_SIZE;
  1783. + descr[i].eol = 0;
  1784. + descr[i].out_eop = 0;
  1785. + descr[i].intr = 1;
  1786. + descr[i].wait = 0;
  1787. + descr[i].in_eop = 0;
  1788. + descr[i].md = 0;
  1789. +
  1790. + }
  1791. +
  1792. + /* Link the last descriptor to the first. */
  1793. + descr[i-1].next = (void*)virt_to_phys(&descr[0]);
  1794. +
  1795. + /* And mark it as end of list. */
  1796. + descr[i-1].eol = 1;
  1797. +
  1798. + /* Start with the first descriptor in the list. */
  1799. + up->cur_rec_descr = 0;
  1800. + up->rec_context_descr.next = 0;
  1801. + up->rec_context_descr.saved_data
  1802. + = (dma_descr_data *)virt_to_phys(&descr[up->cur_rec_descr]);
  1803. + up->rec_context_descr.saved_data_buf = descr[up->cur_rec_descr].buf;
  1804. +
  1805. + /* Start the DMA. */
  1806. + DMA_START_CONTEXT(up->regi_dmain,
  1807. + virt_to_phys(&up->rec_context_descr));
  1808. +
  1809. + /* Input DMA should be running now. */
  1810. + return 1;
  1811. +}
  1812. +
  1813. +#ifdef CONFIG_CONSOLE_POLL
  1814. +/*
  1815. + * Console polling routines for writing and reading from the uart while
  1816. + * in an interrupt or debug context.
  1817. + */
  1818. +
  1819. +static int serial_artpec_get_poll_char(struct uart_port *port)
  1820. +{
  1821. + reg_ser_rs_stat_din stat;
  1822. + reg_ser_rw_ack_intr ack_intr = { 0 };
  1823. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  1824. +
  1825. + do {
  1826. + stat = REG_RD(ser, up->regi_ser, rs_stat_din);
  1827. + } while (!stat.dav);
  1828. +
  1829. + /* Ack the data_avail interrupt. */
  1830. + ack_intr.dav = 1;
  1831. + REG_WR(ser, up->regi_ser, rw_ack_intr, ack_intr);
  1832. +
  1833. + return stat.data;
  1834. +}
  1835. +
  1836. +static void serial_artpec_put_poll_char(struct uart_port *port,
  1837. + unsigned char c)
  1838. +{
  1839. + reg_ser_r_stat_din stat;
  1840. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  1841. +
  1842. + do {
  1843. + stat = REG_RD (ser, up->regi_ser, r_stat_din);
  1844. + } while (!stat.tr_rdy);
  1845. + REG_WR_INT (ser, up->regi_ser, rw_dout, c);
  1846. +}
  1847. +#endif /* CONFIG_CONSOLE_POLL */
  1848. +
  1849. +static void start_receive(struct uart_cris_port *up)
  1850. +{
  1851. + reg_scope_instances regi_dmain = up->regi_dmain;
  1852. + if (regi_dmain) {
  1853. + start_recv_dma(up);
  1854. + }
  1855. +}
  1856. +
  1857. +
  1858. +static void start_transmitter(struct uart_cris_port *up)
  1859. +{
  1860. + int i;
  1861. + reg_scope_instances regi_dmaout = up->regi_dmaout;
  1862. + if (regi_dmaout) {
  1863. + for (i = 0; i < SERIAL_TX_DESCRIPTORS; i++) {
  1864. + memset(&up->tr_descr[i], 0, sizeof(up->tr_descr[i]));
  1865. + up->tr_descr[i].eol = 1;
  1866. + up->tr_descr[i].intr = 1;
  1867. + up->tr_descr[i].next = (dma_descr_data *)
  1868. + virt_to_phys(&up->tr_descr[i+1]);
  1869. + }
  1870. + up->tr_descr[i-1].next = (dma_descr_data *)
  1871. + virt_to_phys(&up->tr_descr[0]);
  1872. + up->first_tx_descr = &up->tr_descr[0];
  1873. +
  1874. + /*
  1875. + * We'll be counting up to up->last_tx_descr->next from
  1876. + * up->first_tx_descr when starting DMA, so we should make
  1877. + * them the same for the very first round. If instead we'd
  1878. + * set last_tx_descr = first_tx_descr, we'd rely on
  1879. + * accidentally working code and data as we'd take a pass over
  1880. + * the first, unused, descriptor.
  1881. + */
  1882. + up->last_tx_descr = &up->tr_descr[i-1];
  1883. + up->tx_started = 0;
  1884. + up->tx_pending_chars = 0;
  1885. + }
  1886. +}
  1887. +
  1888. +static int serial_cris_startup(struct uart_port *port)
  1889. +{
  1890. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  1891. + unsigned long flags;
  1892. + reg_ser_rw_intr_mask ser_intr_mask = {0};
  1893. + reg_dma_rw_intr_mask dmain_intr_mask = {0};
  1894. + reg_dma_rw_intr_mask dmaout_intr_mask = {0};
  1895. + reg_dma_rw_cfg cfg = {.en = 1};
  1896. + reg_scope_instances regi_dma;
  1897. +
  1898. + /* We dont disable interrupts here because request_irq should
  1899. + not be called with ints disabled. */
  1900. + spin_lock(&up->port.lock);
  1901. +
  1902. + dmain_intr_mask.data = dmain_intr_mask.in_eop = regk_dma_yes;
  1903. + dmaout_intr_mask.data = regk_dma_yes;
  1904. + if (!up->regi_dmain)
  1905. + ser_intr_mask.dav = regk_ser_yes;
  1906. +
  1907. + if (port->line == 0) {
  1908. + if (request_irq(SER0_INTR_VECT, ser_interrupt,
  1909. + IRQF_DISABLED, "ser0",
  1910. + &serial_cris_ports[0]))
  1911. + panic("irq ser0");
  1912. + /* Port ser0 can use dma6 for tx and dma7 for rx. */
  1913. +#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA6_OUT
  1914. + if (request_irq(DMA6_INTR_VECT, dma_tr_interrupt,
  1915. + IRQF_DISABLED, "serial 0 dma tr",
  1916. + &serial_cris_ports[0]))
  1917. + panic("irq ser0txdma");
  1918. + crisv32_request_dma(6, "ser0", DMA_PANIC_ON_ERROR, 0,
  1919. + dma_ser0);
  1920. +#endif
  1921. +#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA7_IN
  1922. + if (request_irq(DMA7_INTR_VECT, dma_rec_interrupt,
  1923. + IRQF_DISABLED, "serial 0 dma rec",
  1924. + &serial_cris_ports[0]))
  1925. + panic("irq ser0rxdma");
  1926. + crisv32_request_dma(7, "ser0", DMA_PANIC_ON_ERROR, 0,
  1927. + dma_ser0);
  1928. +#endif
  1929. + } else if (port->line == 1) {
  1930. + if (request_irq(SER1_INTR_VECT, ser_interrupt,
  1931. + IRQF_DISABLED, "ser1",
  1932. + &serial_cris_ports[1]))
  1933. + panic("irq ser1");
  1934. +
  1935. + /* Port ser1 can use dma4 for tx and dma5 for rx. */
  1936. +#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA4_OUT
  1937. + if (request_irq(DMA4_INTR_VECT, dma_tr_interrupt,
  1938. + IRQF_DISABLED, "serial 1 dma tr",
  1939. + &serial_cris_ports[1]))
  1940. + panic("irq ser1txdma");
  1941. + crisv32_request_dma(4, "ser1", DMA_PANIC_ON_ERROR, 0,
  1942. + dma_ser1);
  1943. +#endif
  1944. +#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA5_IN
  1945. + if (request_irq(DMA5_INTR_VECT, dma_rec_interrupt,
  1946. + IRQF_DISABLED, "serial 1 dma rec",
  1947. + &serial_cris_ports[1]))
  1948. + panic("irq ser1rxdma");
  1949. + crisv32_request_dma(5, "ser1", DMA_PANIC_ON_ERROR, 0,
  1950. + dma_ser1);
  1951. +#endif
  1952. + } else if (port->line == 2) {
  1953. + if (request_irq(SER2_INTR_VECT, ser_interrupt,
  1954. + IRQF_DISABLED, "ser2",
  1955. + &serial_cris_ports[2]))
  1956. + panic("irq ser2");
  1957. +
  1958. + /* Port ser2 can use dma2 for tx and dma3 for rx. */
  1959. +#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA2_OUT
  1960. + if (request_irq(DMA2_INTR_VECT, dma_tr_interrupt,
  1961. + IRQF_DISABLED, "serial 2 dma tr",
  1962. + &serial_cris_ports[2]))
  1963. + panic("irq ser2txdma");
  1964. + crisv32_request_dma(2, "ser2", DMA_PANIC_ON_ERROR, 0,
  1965. + dma_ser2);
  1966. +#endif
  1967. +#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA3_IN
  1968. + if (request_irq(DMA3_INTR_VECT, dma_rec_interrupt,
  1969. + IRQF_DISABLED, "serial 2 dma rec",
  1970. + &serial_cris_ports[2]))
  1971. + panic("irq ser2rxdma");
  1972. + crisv32_request_dma(3, "ser2", DMA_PANIC_ON_ERROR, 0,
  1973. + dma_ser2);
  1974. +#endif
  1975. + } else if (port->line == 3) {
  1976. + if (request_irq(SER3_INTR_VECT, ser_interrupt,
  1977. + IRQF_DISABLED, "ser3",
  1978. + &serial_cris_ports[3]))
  1979. + panic("irq ser3" );
  1980. +
  1981. + /* Port ser3 can use dma8 for tx and dma9 for rx. */
  1982. +#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA8_OUT
  1983. + if (request_irq(DMA8_INTR_VECT, dma_tr_interrupt,
  1984. + IRQF_DISABLED, "serial 3 dma tr",
  1985. + &serial_cris_ports[3]))
  1986. + panic("irq ser3txdma");
  1987. + crisv32_request_dma(8, "ser3", DMA_PANIC_ON_ERROR, 0,
  1988. + dma_ser3);
  1989. +#endif
  1990. +#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA9_IN
  1991. + if (request_irq(DMA9_INTR_VECT, dma_rec_interrupt,
  1992. + IRQF_DISABLED, "serial 3 dma rec",
  1993. + &serial_cris_ports[3]))
  1994. + panic("irq ser3rxdma");
  1995. + crisv32_request_dma(9, "ser3", DMA_PANIC_ON_ERROR, 0,
  1996. + dma_ser3);
  1997. +#endif
  1998. + }
  1999. +#if CONFIG_ETRAX_SERIAL_PORTS == 5
  2000. + else if (port->line == 4) {
  2001. + if (request_irq(SER4_INTR_VECT, ser_interrupt,
  2002. + IRQF_DISABLED, "ser4",
  2003. + &serial_cris_ports[4]))
  2004. + panic("irq ser4" );
  2005. +
  2006. +#ifdef CONFIG_ETRAX_SERIAL_PORT4_DMA_OUT
  2007. + if (request_irq(DMA4_INTR_VECT, dma_tr_interrupt,
  2008. + IRQF_DISABLED, "serial 4 dma tr",
  2009. + &serial_cris_ports[4]))
  2010. + panic("irq ser4txdma");
  2011. + crisv32_request_dma(5, "ser4", DMA_PANIC_ON_ERROR, 0,
  2012. + dma_ser4);
  2013. +#endif
  2014. +#ifdef CONFIG_ETRAX_SERIAL_PORT4_DMA_IN
  2015. + if (request_irq(DMA5_INTR_VECT, dma_rec_interrupt,
  2016. + IRQF_DISABLED, "serial 4 dma rec",
  2017. + &serial_cris_ports[4]))
  2018. + panic("irq ser4rxdma");
  2019. + crisv32_request_dma(5, "ser4", DMA_PANIC_ON_ERROR, 0,
  2020. + dma_ser4);
  2021. +#endif
  2022. + }
  2023. +#endif
  2024. +
  2025. + local_irq_save(flags);
  2026. +
  2027. + /*
  2028. + * Reset the DMA channels and make sure their interrupts are cleared.
  2029. + */
  2030. +
  2031. + regi_dma = up->regi_dmain;
  2032. + if (regi_dma) {
  2033. + reg_dma_rw_ack_intr ack_intr = { 0 };
  2034. + DMA_RESET(regi_dma);
  2035. + /* Wait until reset cycle is complete. */
  2036. + DMA_WAIT_UNTIL_RESET(regi_dma);
  2037. + REG_WR(dma, regi_dma, rw_cfg, cfg);
  2038. + /* Make sure the irqs are cleared. */
  2039. + ack_intr.group = 1;
  2040. + ack_intr.ctxt = 1;
  2041. + ack_intr.data = 1;
  2042. + ack_intr.in_eop = 1;
  2043. + ack_intr.stream_cmd = 1;
  2044. + REG_WR(dma, regi_dma, rw_ack_intr, ack_intr);
  2045. + }
  2046. + regi_dma = up->regi_dmaout;
  2047. + if (regi_dma) {
  2048. + reg_dma_rw_ack_intr ack_intr = { 0 };
  2049. + DMA_RESET(regi_dma);
  2050. + /* Wait until reset cycle is complete. */
  2051. + DMA_WAIT_UNTIL_RESET(regi_dma);
  2052. + REG_WR(dma, regi_dma, rw_cfg, cfg);
  2053. + /* Make sure the irqs are cleared. */
  2054. + ack_intr.group = 1;
  2055. + ack_intr.ctxt = 1;
  2056. + ack_intr.data = 1;
  2057. + ack_intr.in_eop = 1;
  2058. + ack_intr.stream_cmd = 1;
  2059. + REG_WR(dma, regi_dma, rw_ack_intr, ack_intr);
  2060. + }
  2061. +
  2062. + REG_WR(ser, up->regi_ser, rw_intr_mask, ser_intr_mask);
  2063. + if (up->regi_dmain)
  2064. + REG_WR(dma, up->regi_dmain, rw_intr_mask, dmain_intr_mask);
  2065. + if (up->regi_dmaout)
  2066. + REG_WR(dma, up->regi_dmaout, rw_intr_mask, dmaout_intr_mask);
  2067. +
  2068. + start_receive(up);
  2069. + start_transmitter(up);
  2070. +
  2071. + serial_cris_set_mctrl(&up->port, up->port.mctrl);
  2072. +
  2073. + local_irq_restore(flags);
  2074. + spin_unlock(&up->port.lock);
  2075. +
  2076. + return 0;
  2077. +}
  2078. +
  2079. +static void serial_cris_shutdown(struct uart_port *port)
  2080. +{
  2081. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  2082. + unsigned long flags;
  2083. +
  2084. + spin_lock_irqsave(&up->port.lock, flags);
  2085. +
  2086. + serial_cris_stop_tx(port);
  2087. + serial_cris_stop_rx(port);
  2088. +
  2089. + if (port->line == 0) {
  2090. + free_irq(SER0_INTR_VECT, &serial_cris_ports[0]);
  2091. +#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA6_OUT
  2092. + crisv32_free_dma(6);
  2093. + free_irq(DMA6_INTR_VECT, &serial_cris_ports[0]);
  2094. +#elif defined(CONFIG_ETRAX_SERIAL_PORT0_DMA0_OUT)
  2095. + crisv32_free_dma(0);
  2096. + free_irq(DMA0_INTR_VECT, &serial_cris_ports[0]);
  2097. +#endif
  2098. +#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA7_IN
  2099. + crisv32_free_dma(7);
  2100. + free_irq(DMA7_INTR_VECT, &serial_cris_ports[0]);
  2101. +#elif defined(CONFIG_ETRAX_SERIAL_PORT0_DMA1_IN)
  2102. + crisv32_free_dma(1);
  2103. + free_irq(DMA1_INTR_VECT, &serial_cris_ports[0]);
  2104. +#endif
  2105. + } else if (port->line == 1) {
  2106. + free_irq(SER1_INTR_VECT, &serial_cris_ports[1]);
  2107. +#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA4_OUT
  2108. + crisv32_free_dma(4);
  2109. + free_irq(DMA4_INTR_VECT, &serial_cris_ports[1]);
  2110. +#endif
  2111. +#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA5_IN
  2112. + crisv32_free_dma(5);
  2113. + free_irq(DMA5_INTR_VECT, &serial_cris_ports[1]);
  2114. +#endif
  2115. + } else if (port->line == 2) {
  2116. + free_irq(SER2_INTR_VECT, &serial_cris_ports[2]);
  2117. +#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA2_OUT
  2118. + crisv32_free_dma(2);
  2119. + free_irq(DMA2_INTR_VECT, &serial_cris_ports[2]);
  2120. +#elif defined(CONFIG_ETRAX_SERIAL_PORT2_DMA6_OUT)
  2121. + crisv32_free_dma(6);
  2122. + free_irq(DMA6_INTR_VECT, &serial_cris_ports[2]);
  2123. +#endif
  2124. +#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA3_IN
  2125. + crisv32_free_dma(3);
  2126. + free_irq(DMA3_INTR_VECT, &serial_cris_ports[2]);
  2127. +#elif defined(CONFIG_ETRAX_SERIAL_PORT2_DMA7_IN)
  2128. + crisv32_free_dma(7);
  2129. + free_irq(DMA7_INTR_VECT, &serial_cris_ports[2]);
  2130. +#endif
  2131. + } else if (port->line == 3) {
  2132. + free_irq(SER3_INTR_VECT, &serial_cris_ports[3]);
  2133. +#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA8_OUT
  2134. + crisv32_free_dma(8);
  2135. + free_irq(DMA8_INTR_VECT, &serial_cris_ports[3]);
  2136. +#elif defined(CONFIG_ETRAX_SERIAL_PORT3_DMA2_OUT)
  2137. + crisv32_free_dma(2);
  2138. + free_irq(DMA2_INTR_VECT, &serial_cris_ports[3]);
  2139. +#endif
  2140. +#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA9_IN
  2141. + crisv32_free_dma(9);
  2142. + free_irq(DMA9_INTR_VECT, &serial_cris_ports[3]);
  2143. +#elif defined(CONFIG_ETRAX_SERIAL_PORT3_DMA3_IN)
  2144. + crisv32_free_dma(3);
  2145. + free_irq(DMA3_INTR_VECT, &serial_cris_ports[3]);
  2146. +#endif
  2147. + }
  2148. +#if CONFIG_ETRAX_SERIAL_PORTS == 5
  2149. + else if (port->line == 4) {
  2150. + free_irq(SER4_INTR_VECT, &serial_cris_ports[4]);
  2151. +#ifdef CONFIG_ETRAX_SERIAL_PORT4_DMA9_IN
  2152. + crisv32_free_dma(9);
  2153. + free_irq(DMA9_INTR_VECT, &serial_cris_ports[4]);
  2154. +#endif
  2155. + }
  2156. +#endif
  2157. +
  2158. + serial_cris_set_mctrl(&up->port, up->port.mctrl);
  2159. +
  2160. + if (up->regi_dmain) {
  2161. + struct etrax_recv_buffer *rb;
  2162. + struct etrax_recv_buffer *rb_next;
  2163. + int i;
  2164. + struct dma_descr_data *descr;
  2165. +
  2166. + /*
  2167. + * In case of DMA and receive errors, there might be pending
  2168. + * receive buffers still linked here and not flushed upwards.
  2169. + * Release them.
  2170. + */
  2171. + for (rb = up->first_recv_buffer; rb != NULL; rb = rb_next) {
  2172. + rb_next = rb->next;
  2173. + kfree (rb);
  2174. + }
  2175. + up->first_recv_buffer = NULL;
  2176. + up->last_recv_buffer = NULL;
  2177. +
  2178. + /*
  2179. + * Also release buffers that were attached to the DMA
  2180. + * before we shut down the hardware above.
  2181. + */
  2182. + for (i = 0, descr = up->rec_descr;
  2183. + i < SERIAL_RECV_DESCRIPTORS;
  2184. + i++)
  2185. + if (descr[i].buf) {
  2186. + rb = phys_to_virt((u32) descr[i].buf)
  2187. + - sizeof *rb;
  2188. + kfree(rb);
  2189. + descr[i].buf = NULL;
  2190. + }
  2191. + }
  2192. +
  2193. + spin_unlock_irqrestore(&up->port.lock, flags);
  2194. +
  2195. +}
  2196. +
  2197. +static void
  2198. +serial_cris_set_termios(struct uart_port *port, struct ktermios *termios,
  2199. + struct ktermios *old)
  2200. +{
  2201. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  2202. + unsigned long flags;
  2203. + reg_ser_rw_xoff xoff;
  2204. + reg_ser_rw_xoff_clr xoff_clr = {0};
  2205. + reg_ser_rw_tr_ctrl tx_ctrl = {0};
  2206. + reg_ser_rw_tr_dma_en tx_dma_en = {0};
  2207. + reg_ser_rw_rec_ctrl rx_ctrl = {0};
  2208. + reg_ser_rw_tr_baud_div tx_baud_div = {0};
  2209. + reg_ser_rw_rec_baud_div rx_baud_div = {0};
  2210. + reg_ser_r_stat_din rstat;
  2211. + int baud;
  2212. +
  2213. + if (old &&
  2214. + termios->c_cflag == old->c_cflag &&
  2215. + termios->c_iflag == old->c_iflag)
  2216. + return;
  2217. +
  2218. + /* Start with default settings and then fill in changes. */
  2219. +
  2220. + /* Tx: 8 bit, no/even parity, 1 stop bit, no cts. */
  2221. + tx_ctrl.base_freq = regk_ser_f29_493;
  2222. + tx_ctrl.en = 0;
  2223. + tx_ctrl.stop = 0;
  2224. +#ifdef CONFIG_ETRAX_RS485
  2225. + if (up->rs485.enabled && (up->port_type != TYPE_485FD)) {
  2226. + tx_ctrl.auto_rts = regk_ser_yes;
  2227. + tx_ctrl.rts_setup = regk_ser_bits2;
  2228. +
  2229. + if (termios->c_cflag & CSTOPB) {
  2230. + /* 2 stop bits. */
  2231. + tx_ctrl.rts_delay = regk_ser_del2;
  2232. + }
  2233. + else {
  2234. + /* 1 stop bits. */
  2235. + tx_ctrl.rts_delay = regk_ser_del1;
  2236. + }
  2237. + } else
  2238. +#endif
  2239. + tx_ctrl.auto_rts = regk_ser_no;
  2240. + tx_ctrl.txd = 1;
  2241. + tx_ctrl.auto_cts = 0;
  2242. + /* Rx: 8 bit, no/even parity. */
  2243. + if (up->regi_dmain) {
  2244. + rx_ctrl.dma_mode = 1;
  2245. + rx_ctrl.auto_eop = 1;
  2246. + }
  2247. + rx_ctrl.dma_err = regk_ser_stop;
  2248. + rx_ctrl.sampling = regk_ser_majority;
  2249. + rx_ctrl.timeout = 1;
  2250. +
  2251. +#ifdef CONFIG_ETRAX_RS485
  2252. + if (up->rs485.enabled && (up->port_type != TYPE_485FD)) {
  2253. +# ifdef CONFIG_ETRAX_RS485_DISABLE_RECEIVER
  2254. + rx_ctrl.half_duplex = regk_ser_yes;
  2255. +# endif
  2256. + rx_ctrl.rts_n = up->rs485.rts_after_sent ?
  2257. + regk_ser_active : regk_ser_inactive;
  2258. + } else if (up->port_type == TYPE_485FD) {
  2259. + rx_ctrl.rts_n = regk_ser_active;
  2260. + } else
  2261. +#endif
  2262. + rx_ctrl.rts_n = regk_ser_inactive;
  2263. +
  2264. + /* Common for tx and rx: 8N1. */
  2265. + tx_ctrl.data_bits = regk_ser_bits8;
  2266. + rx_ctrl.data_bits = regk_ser_bits8;
  2267. + tx_ctrl.par = regk_ser_even;
  2268. + rx_ctrl.par = regk_ser_even;
  2269. + tx_ctrl.par_en = regk_ser_no;
  2270. + rx_ctrl.par_en = regk_ser_no;
  2271. +
  2272. + tx_ctrl.stop_bits = regk_ser_bits1;
  2273. +
  2274. +
  2275. + /* Change baud-rate and write it to the hardware. */
  2276. +
  2277. + /* baud_clock = base_freq / (divisor*8)
  2278. + * divisor = base_freq / (baud_clock * 8)
  2279. + * base_freq is either:
  2280. + * off, ext, 29.493MHz, 32.000 MHz, 32.768 MHz or 100 MHz
  2281. + * 20.493MHz is used for standard baudrates
  2282. + */
  2283. +
  2284. + /*
  2285. + * For the console port we keep the original baudrate here. Not very
  2286. + * beautiful.
  2287. + */
  2288. + if ((port != console_port) || old)
  2289. + baud = uart_get_baud_rate(port, termios, old, 0,
  2290. + port->uartclk / 8);
  2291. + else
  2292. + baud = console_baud;
  2293. +
  2294. + tx_baud_div.div = 29493000 / (8 * baud);
  2295. + /* Rx uses same as tx. */
  2296. + rx_baud_div.div = tx_baud_div.div;
  2297. + rx_ctrl.base_freq = tx_ctrl.base_freq;
  2298. +
  2299. + if ((termios->c_cflag & CSIZE) == CS7) {
  2300. + /* Set 7 bit mode. */
  2301. + tx_ctrl.data_bits = regk_ser_bits7;
  2302. + rx_ctrl.data_bits = regk_ser_bits7;
  2303. + }
  2304. +
  2305. + if (termios->c_cflag & CSTOPB) {
  2306. + /* Set 2 stop bit mode. */
  2307. + tx_ctrl.stop_bits = regk_ser_bits2;
  2308. + }
  2309. +
  2310. + if (termios->c_cflag & PARENB) {
  2311. + /* Enable parity. */
  2312. + tx_ctrl.par_en = regk_ser_yes;
  2313. + rx_ctrl.par_en = regk_ser_yes;
  2314. + }
  2315. +
  2316. + if (termios->c_cflag & CMSPAR) {
  2317. + if (termios->c_cflag & PARODD) {
  2318. + /* Set mark parity if PARODD and CMSPAR. */
  2319. + tx_ctrl.par = regk_ser_mark;
  2320. + rx_ctrl.par = regk_ser_mark;
  2321. + } else {
  2322. + tx_ctrl.par = regk_ser_space;
  2323. + rx_ctrl.par = regk_ser_space;
  2324. + }
  2325. + } else {
  2326. + if (termios->c_cflag & PARODD) {
  2327. + /* Set odd parity. */
  2328. + tx_ctrl.par = regk_ser_odd;
  2329. + rx_ctrl.par = regk_ser_odd;
  2330. + }
  2331. + }
  2332. +
  2333. + if (termios->c_cflag & CRTSCTS) {
  2334. + /* Enable automatic CTS handling. */
  2335. + tx_ctrl.auto_cts = regk_ser_yes;
  2336. + }
  2337. +
  2338. + /* Make sure the tx and rx are enabled. */
  2339. + tx_ctrl.en = regk_ser_yes;
  2340. + rx_ctrl.en = regk_ser_yes;
  2341. +
  2342. + /*
  2343. + * Wait for tr_idle in case a character is being output, so it won't
  2344. + * be damaged by the changes we do below. It seems the termios
  2345. + * changes "sometimes" (we can't see e.g. a tcsetattr TCSANOW
  2346. + * parameter here) should take place no matter what state. However,
  2347. + * in case we should wait, we may have a non-empty transmitter state
  2348. + * as we tell the upper layers that we're all done when we've passed
  2349. + * characters to the hardware, but we don't wait for them being
  2350. + * actually shifted out.
  2351. + */
  2352. + spin_lock_irqsave(&port->lock, flags);
  2353. +
  2354. + /*
  2355. + * None of our interrupts re-enable DMA, so it's thankfully ok to
  2356. + * disable it once, outside the loop.
  2357. + */
  2358. + tx_dma_en.en = 0;
  2359. + REG_WR(ser, up->regi_ser, rw_tr_dma_en, tx_dma_en);
  2360. + do {
  2361. + /*
  2362. + * Make sure we have integrity between the read r_stat status
  2363. + * and us writing the registers below, but don't busy-wait
  2364. + * with interrupts off. We need to keep the port lock though
  2365. + * (if we go SMP), so nobody else writes characters.
  2366. + */
  2367. + local_irq_restore(flags);
  2368. + local_irq_save(flags);
  2369. + rstat = REG_RD(ser, up->regi_ser, r_stat_din);
  2370. + } while (!rstat.tr_idle);
  2371. +
  2372. + /* Actually write the control regs (if modified) to the hardware. */
  2373. +
  2374. + uart_update_timeout(port, termios->c_cflag, port->uartclk/8);
  2375. + MODIFY_REG(up->regi_ser, rw_rec_baud_div, rx_baud_div);
  2376. + MODIFY_REG(up->regi_ser, rw_rec_ctrl, rx_ctrl);
  2377. +
  2378. + MODIFY_REG(up->regi_ser, rw_tr_baud_div, tx_baud_div);
  2379. + MODIFY_REG(up->regi_ser, rw_tr_ctrl, tx_ctrl);
  2380. +
  2381. + tx_dma_en.en = up->regi_dmaout != 0;
  2382. + REG_WR(ser, up->regi_ser, rw_tr_dma_en, tx_dma_en);
  2383. +
  2384. + xoff = REG_RD(ser, up->regi_ser, rw_xoff);
  2385. +
  2386. + if (up->port.state && up->port.state->port.tty &&
  2387. + (termios->c_iflag & IXON)) {
  2388. + xoff.chr = STOP_CHAR(up->port.state->port.tty);
  2389. + xoff.automatic = regk_ser_yes;
  2390. + } else
  2391. + xoff.automatic = regk_ser_no;
  2392. +
  2393. + MODIFY_REG(up->regi_ser, rw_xoff, xoff);
  2394. +
  2395. + /*
  2396. + * Make sure we don't start in an automatically shut-off state due to
  2397. + * a previous early exit.
  2398. + */
  2399. + xoff_clr.clr = 1;
  2400. + REG_WR(ser, up->regi_ser, rw_xoff_clr, xoff_clr);
  2401. +
  2402. + serial_cris_set_mctrl(&up->port, up->port.mctrl);
  2403. + spin_unlock_irqrestore(&up->port.lock, flags);
  2404. +}
  2405. +
  2406. +static const char *
  2407. +serial_cris_type(struct uart_port *port)
  2408. +{
  2409. + return "CRISv32";
  2410. +}
  2411. +
  2412. +static void serial_cris_release_port(struct uart_port *port)
  2413. +{
  2414. +}
  2415. +
  2416. +static int serial_cris_request_port(struct uart_port *port)
  2417. +{
  2418. + return 0;
  2419. +}
  2420. +
  2421. +static void serial_cris_config_port(struct uart_port *port, int flags)
  2422. +{
  2423. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  2424. + up->port.type = PORT_CRIS;
  2425. +}
  2426. +
  2427. +#if defined(CONFIG_ETRAX_RS485)
  2428. +
  2429. +static void cris_set_rs485_mode(struct uart_cris_port* up) {
  2430. + reg_ser_rw_tr_ctrl tr_ctrl;
  2431. + reg_ser_rw_rec_ctrl rec_ctrl;
  2432. + reg_scope_instances regi_ser = up->regi_ser;
  2433. +
  2434. + if (up->port_type == TYPE_485FD)
  2435. + /* We do not want to change anything if we are in 485FD mode */
  2436. + return;
  2437. +
  2438. + tr_ctrl = REG_RD(ser, regi_ser, rw_tr_ctrl);
  2439. + rec_ctrl = REG_RD(ser, regi_ser, rw_rec_ctrl);
  2440. +
  2441. + /* Set port in RS-485 mode */
  2442. + if (up->rs485.enabled) {
  2443. + tr_ctrl.auto_rts = regk_ser_yes;
  2444. + rec_ctrl.rts_n = up->rs485.rts_after_sent ?
  2445. + regk_ser_active : regk_ser_inactive;
  2446. + }
  2447. + /* Set port to RS-232 mode */
  2448. + else {
  2449. + rec_ctrl.rts_n = regk_ser_inactive;
  2450. + tr_ctrl.auto_rts = regk_ser_no;
  2451. + rec_ctrl.half_duplex = regk_ser_no;
  2452. + }
  2453. +
  2454. + REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
  2455. + REG_WR(ser, regi_ser, rw_rec_ctrl, rec_ctrl);
  2456. +}
  2457. +
  2458. +/* Enable/disable RS-485 mode on selected port. */
  2459. +static int
  2460. +cris_enable_rs485(struct uart_cris_port* up, struct rs485_control *r)
  2461. +{
  2462. + if (up->port_type == TYPE_485FD)
  2463. + /* Port in 485FD mode can not chage mode */
  2464. + goto out;
  2465. +
  2466. + up->rs485.enabled = 0x1 & r->enabled;
  2467. + up->rs485.rts_on_send = 0x01 & r->rts_on_send;
  2468. + up->rs485.rts_after_sent = 0x01 & r->rts_after_sent;
  2469. + up->rs485.delay_rts_before_send = r->delay_rts_before_send;
  2470. +
  2471. + cris_set_rs485_mode(up);
  2472. + out:
  2473. + return 0;
  2474. +}
  2475. +
  2476. +
  2477. +/* Enable RS485 mode on port and send the data. Port will stay
  2478. + * in 485 mode after the data has been sent.
  2479. + */
  2480. +static int
  2481. +cris_write_rs485(struct uart_cris_port *up, const unsigned char* buf, int count)
  2482. +{
  2483. + up->rs485.enabled = 1;
  2484. +
  2485. + /* Set the port in RS485 mode */
  2486. + cris_set_rs485_mode(up);
  2487. +
  2488. + /* Send the data */
  2489. + count = serial_cris_driver.tty_driver->ops->write(up->port.state->port.tty, buf, count);
  2490. +
  2491. + return count;
  2492. +}
  2493. +
  2494. +#endif /* CONFIG_ETRAX_RS485 */
  2495. +
  2496. +static int serial_cris_ioctl(struct uart_port *port, unsigned int cmd,
  2497. + unsigned long arg)
  2498. +{
  2499. +#if defined(CONFIG_ETRAX_RS485)
  2500. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  2501. +
  2502. + switch (cmd) {
  2503. + case TIOCSRS485: {
  2504. + struct serial_rs485 in;
  2505. + struct rs485_control rs485ctrl;
  2506. + if (copy_from_user(&in, (struct serial_rs485 *)cmd,
  2507. + sizeof(rs485ctrl)))
  2508. + return -EFAULT;
  2509. + rs485ctrl.rts_on_send = in.flags & SER_RS485_RTS_ON_SEND;
  2510. + rs485ctrl.rts_after_sent = in.flags & SER_RS485_RTS_AFTER_SEND;
  2511. + rs485ctrl.enabled = in.flags & SER_RS485_ENABLED;
  2512. + rs485ctrl.delay_rts_before_send = in.delay_rts_before_send;
  2513. +
  2514. + return cris_enable_rs485(up, &rs485ctrl);
  2515. + }
  2516. +
  2517. + case TIOCSERSETRS485: {
  2518. + struct rs485_control rs485ctrl;
  2519. + if (copy_from_user(&rs485ctrl, (struct rs485_control*) arg,
  2520. + sizeof(rs485ctrl)))
  2521. + return -EFAULT;
  2522. +
  2523. + return cris_enable_rs485(up, &rs485ctrl);
  2524. + }
  2525. +
  2526. + case TIOCSERWRRS485: {
  2527. + struct rs485_write rs485wr;
  2528. + if (copy_from_user(&rs485wr, (struct rs485_write*)arg,
  2529. + sizeof(rs485wr)))
  2530. + return -EFAULT;
  2531. +
  2532. + return cris_write_rs485(up, rs485wr.outc, rs485wr.outc_size);
  2533. + }
  2534. +
  2535. + case TIOCSERSETRS485FD: {
  2536. + reg_scope_instances regi_ser = up->regi_ser;
  2537. + reg_ser_rw_rec_ctrl rec_ctrl = REG_RD(ser, regi_ser, rw_rec_ctrl);
  2538. +
  2539. + if (arg)
  2540. + rec_ctrl.half_duplex = regk_ser_no;
  2541. + else
  2542. + rec_ctrl.half_duplex = regk_ser_yes;
  2543. +
  2544. + REG_WR(ser, regi_ser, rw_rec_ctrl, rec_ctrl);
  2545. + return 0;
  2546. + }
  2547. +
  2548. + case TIOCSERSETDIVISOR: {
  2549. + reg_ser_rw_tr_baud_div tx_baud_div = {0};
  2550. + reg_ser_rw_rec_baud_div rx_baud_div = {0};
  2551. +
  2552. + /* divisor must be >= 8 */
  2553. + if (arg < 8)
  2554. + return -EINVAL;
  2555. +
  2556. + tx_baud_div.div = arg;
  2557. + rx_baud_div.div = tx_baud_div.div; /* same as tx. */
  2558. +
  2559. + MODIFY_REG(up->regi_ser, rw_rec_baud_div, rx_baud_div);
  2560. + MODIFY_REG(up->regi_ser, rw_tr_baud_div, tx_baud_div);
  2561. +
  2562. + return 0;
  2563. + }
  2564. +
  2565. + default:
  2566. + return -ENOIOCTLCMD;
  2567. + }
  2568. +
  2569. + return 0;
  2570. +#else
  2571. + return -ENOIOCTLCMD;
  2572. +#endif
  2573. +}
  2574. +
  2575. +static const struct uart_ops serial_cris_pops = {
  2576. + .tx_empty = serial_cris_tx_empty,
  2577. + .set_mctrl = serial_cris_set_mctrl,
  2578. + .get_mctrl = serial_cris_get_mctrl,
  2579. + .stop_tx = serial_cris_stop_tx,
  2580. + .start_tx = serial_cris_start_tx,
  2581. + .send_xchar = serial_cris_send_xchar,
  2582. + .stop_rx = serial_cris_stop_rx,
  2583. + .enable_ms = serial_cris_enable_ms,
  2584. + .break_ctl = serial_cris_break_ctl,
  2585. + .startup = serial_cris_startup,
  2586. + .shutdown = serial_cris_shutdown,
  2587. + .set_termios = serial_cris_set_termios,
  2588. + .type = serial_cris_type,
  2589. + .release_port = serial_cris_release_port,
  2590. + .request_port = serial_cris_request_port,
  2591. + .config_port = serial_cris_config_port,
  2592. + .ioctl = serial_cris_ioctl,
  2593. +#ifdef CONFIG_CONSOLE_POLL
  2594. + .poll_get_char = serial_artpec_get_poll_char,
  2595. + .poll_put_char = serial_artpec_put_poll_char,
  2596. +#endif
  2597. +};
  2598. +
  2599. +/*
  2600. + * It's too easy to break CONFIG_ETRAX_DEBUG_PORT_NULL and the
  2601. + * no-config choices by adding and moving code to before a necessary
  2602. + * early exit in all functions for the special case of
  2603. + * up->regi_ser == 0. This collection of dummy functions lets us
  2604. + * avoid that. Maybe there should be a generic table of dummy serial
  2605. + * functions?
  2606. + */
  2607. +
  2608. +static unsigned int serial_cris_tx_empty_dummy(struct uart_port *port)
  2609. +{
  2610. + return TIOCSER_TEMT;
  2611. +}
  2612. +
  2613. +static void serial_cris_set_mctrl_dummy(struct uart_port *port,
  2614. + unsigned int mctrl)
  2615. +{
  2616. +}
  2617. +
  2618. +static unsigned int serial_cris_get_mctrl_dummy(struct uart_port *port)
  2619. +{
  2620. + return 0;
  2621. +}
  2622. +
  2623. +static void serial_cris_stop_tx_dummy(struct uart_port *port)
  2624. +{
  2625. +}
  2626. +
  2627. +static void serial_cris_start_tx_dummy(struct uart_port *port)
  2628. +{
  2629. + /* Discard outbound characters. */
  2630. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  2631. + struct circ_buf *xmit = &up->port.state->xmit;
  2632. + xmit->tail = xmit->head;
  2633. + uart_write_wakeup(port);
  2634. +}
  2635. +
  2636. +#define serial_cris_stop_rx_dummy serial_cris_stop_tx_dummy
  2637. +
  2638. +#define serial_cris_enable_ms_dummy serial_cris_stop_tx_dummy
  2639. +
  2640. +static void serial_cris_break_ctl_dummy(struct uart_port *port,
  2641. + int break_state)
  2642. +{
  2643. +}
  2644. +
  2645. +static int serial_cris_startup_dummy(struct uart_port *port)
  2646. +{
  2647. + return 0;
  2648. +}
  2649. +
  2650. +#define serial_cris_shutdown_dummy serial_cris_stop_tx_dummy
  2651. +
  2652. +static void
  2653. +serial_cris_set_termios_dummy(struct uart_port *port, struct ktermios *termios,
  2654. + struct ktermios *old)
  2655. +{
  2656. +}
  2657. +
  2658. +#define serial_cris_release_port_dummy serial_cris_stop_tx_dummy
  2659. +#define serial_cris_request_port_dummy serial_cris_startup_dummy
  2660. +
  2661. +static const struct uart_ops serial_cris_dummy_pops = {
  2662. + /*
  2663. + * We *could* save one or two of those with different
  2664. + * signature by casting and knowledge of the ABI, but it's
  2665. + * just not worth the maintenance headache.
  2666. + * For the ones we don't define here, the default (usually meaning
  2667. + * "unimplemented") makes sense.
  2668. + */
  2669. + .tx_empty = serial_cris_tx_empty_dummy,
  2670. + .set_mctrl = serial_cris_set_mctrl_dummy,
  2671. + .get_mctrl = serial_cris_get_mctrl_dummy,
  2672. + .stop_tx = serial_cris_stop_tx_dummy,
  2673. + .start_tx = serial_cris_start_tx_dummy,
  2674. + .stop_rx = serial_cris_stop_rx_dummy,
  2675. + .enable_ms = serial_cris_enable_ms_dummy,
  2676. + .break_ctl = serial_cris_break_ctl_dummy,
  2677. + .startup = serial_cris_startup_dummy,
  2678. + .shutdown = serial_cris_shutdown_dummy,
  2679. + .set_termios = serial_cris_set_termios_dummy,
  2680. +
  2681. + /* This one we keep the same. */
  2682. + .type = serial_cris_type,
  2683. +
  2684. + .release_port = serial_cris_release_port_dummy,
  2685. + .request_port = serial_cris_request_port_dummy,
  2686. +
  2687. + /*
  2688. + * This one we keep the same too, as long as it doesn't do
  2689. + * anything else but to set the type.
  2690. + */
  2691. + .config_port = serial_cris_config_port,
  2692. +};
  2693. +
  2694. +static void cris_serial_port_init(struct uart_port *port, int line)
  2695. +{
  2696. + struct uart_cris_port *up = (struct uart_cris_port *)port;
  2697. + static int first = 1;
  2698. +
  2699. + if (up->initialized)
  2700. + return;
  2701. + up->initialized = 1;
  2702. + port->line = line;
  2703. + spin_lock_init(&port->lock);
  2704. + port->ops =
  2705. + up->regi_ser == 0 ? &serial_cris_dummy_pops :
  2706. + &serial_cris_pops;
  2707. + port->irq = up->irq;
  2708. + port->iobase = up->regi_ser ? up->regi_ser : 1;
  2709. + port->uartclk = 29493000;
  2710. +
  2711. + /*
  2712. + * We can't fit any more than 255 here (unsigned char), though
  2713. + * actually UART_XMIT_SIZE characters could be pending output (if it
  2714. + * wasn't for the single test in transmit_chars_dma). At time of this
  2715. + * writing, the definition of "fifosize" is here the amount of
  2716. + * characters that can be pending output after a start_tx call until
  2717. + * tx_empty returns 1: see serial_core.c:uart_wait_until_sent. This
  2718. + * matters for timeout calculations unfortunately, but keeping larger
  2719. + * amounts at the DMA wouldn't win much so let's just play nice.
  2720. + */
  2721. + port->fifosize = 255;
  2722. + port->flags = UPF_BOOT_AUTOCONF;
  2723. +
  2724. +#ifdef CONFIG_ETRAX_RS485
  2725. + /* Set sane defaults. */
  2726. + up->rs485.rts_on_send = 0;
  2727. + up->rs485.rts_after_sent = 1;
  2728. + up->rs485.delay_rts_before_send = 0;
  2729. + if (up->port_type > TYPE_232)
  2730. + up->rs485.enabled = 1;
  2731. + else
  2732. + up->rs485.enabled = 0;
  2733. +#endif
  2734. +
  2735. + if (first) {
  2736. + first = 0;
  2737. +#ifdef CONFIG_ETRAX_SERIAL_PORT0
  2738. + SETUP_PINS(0);
  2739. +#endif
  2740. +#ifdef CONFIG_ETRAX_SERIAL_PORT1
  2741. + SETUP_PINS(1);
  2742. +#endif
  2743. +#ifdef CONFIG_ETRAX_SERIAL_PORT2
  2744. + SETUP_PINS(2);
  2745. +#endif
  2746. +#ifdef CONFIG_ETRAX_SERIAL_PORT3
  2747. + SETUP_PINS(3);
  2748. +#endif
  2749. + }
  2750. +}
  2751. +
  2752. +static int __init serial_cris_init(void)
  2753. +{
  2754. + int ret, i;
  2755. + printk(KERN_INFO "Serial: CRISv32 driver $Revision: 1.109 $ ");
  2756. +
  2757. + ret = uart_register_driver(&serial_cris_driver);
  2758. + if (ret)
  2759. + goto out;
  2760. +
  2761. + for (i = 0; i < UART_NR; i++) {
  2762. + if (serial_cris_ports[i].used) {
  2763. + struct uart_port *port;
  2764. +#ifdef CONFIG_ETRAX_RS485
  2765. + reg_ser_rw_rec_ctrl rec_ctrl;
  2766. +
  2767. + /* Make sure that the RTS pin stays low when allocating
  2768. + * pins for a port in 485 mode.
  2769. + */
  2770. + if (serial_cris_ports[i].port_type > TYPE_232) {
  2771. + rec_ctrl = REG_RD(ser, serial_cris_ports[i].regi_ser, rw_rec_ctrl);
  2772. + rec_ctrl.rts_n = regk_ser_active;
  2773. + REG_WR(ser, serial_cris_ports[i].regi_ser, rw_rec_ctrl, rec_ctrl);
  2774. + }
  2775. +#endif
  2776. + switch (serial_cris_ports[i].regi_ser) {
  2777. + case regi_ser0:
  2778. + break;
  2779. + case regi_ser1:
  2780. + if (crisv32_pinmux_alloc_fixed(pinmux_ser1)) {
  2781. + printk("Failed to allocate pins for ser1, disable port\n");
  2782. + serial_cris_ports[i].used = 0;
  2783. + continue;
  2784. + }
  2785. + break;
  2786. + case regi_ser2:
  2787. + if (crisv32_pinmux_alloc_fixed(pinmux_ser2)) {
  2788. + printk("Failed to allocate pins for ser2, disable port\n");
  2789. + serial_cris_ports[i].used = 0;
  2790. + continue;
  2791. + }
  2792. + break;
  2793. + case regi_ser3:
  2794. + if (crisv32_pinmux_alloc_fixed(pinmux_ser3)) {
  2795. + printk("Failed to allocate pins for ser3, disable port\n");
  2796. + serial_cris_ports[i].used = 0;
  2797. + continue;
  2798. + }
  2799. + break;
  2800. +#if CONFIG_ETRAX_SERIAL_PORTS == 5
  2801. + case regi_ser4:
  2802. + if (crisv32_pinmux_alloc_fixed(pinmux_ser4)) {
  2803. + printk("Failed to allocate pins for ser4, disable port\n");
  2804. + serial_cris_ports[i].used = 0;
  2805. + continue;
  2806. + }
  2807. + break;
  2808. +#endif
  2809. + default:
  2810. + printk("Error: No such serial port (%d) \n", serial_cris_ports[i].regi_ser);
  2811. + serial_cris_ports[i].used = 0;
  2812. + break;
  2813. + }
  2814. +
  2815. + port = &serial_cris_ports[i].port;
  2816. + cris_console.index = i;
  2817. + cris_serial_port_init(port, i);
  2818. + uart_add_one_port(&serial_cris_driver, port);
  2819. + }
  2820. + }
  2821. +
  2822. +out:
  2823. + return ret;
  2824. +}
  2825. +
  2826. +static void __exit serial_cris_exit(void)
  2827. +{
  2828. + int i;
  2829. + for (i = 0; i < UART_NR; i++)
  2830. + if (serial_cris_ports[i].used) {
  2831. + switch (serial_cris_ports[i].regi_ser) {
  2832. + case regi_ser1:
  2833. + crisv32_pinmux_dealloc_fixed(pinmux_ser1);
  2834. + break;
  2835. + case regi_ser2:
  2836. + crisv32_pinmux_dealloc_fixed(pinmux_ser2);
  2837. + break;
  2838. + case regi_ser3:
  2839. + crisv32_pinmux_dealloc_fixed(pinmux_ser3);
  2840. + break;
  2841. +#if CONFIG_ETRAX_SERIAL_PORTS == 5
  2842. + case regi_ser4:
  2843. + crisv32_pinmux_dealloc_fixed(pinmux_ser4);
  2844. + break;
  2845. +#endif
  2846. + default:
  2847. + printk("Error: No such serial port (%d) \n", serial_cris_ports[i].regi_ser);
  2848. + serial_cris_ports[i].used = 0;
  2849. + break;
  2850. + }
  2851. + uart_remove_one_port(&serial_cris_driver,
  2852. + &serial_cris_ports[i].port);
  2853. + }
  2854. + uart_unregister_driver(&serial_cris_driver);
  2855. +}
  2856. +
  2857. +module_init(serial_cris_init);
  2858. +module_exit(serial_cris_exit);
  2859. diff -Nur linux-4.0.3.orig/drivers/tty/serial/Makefile linux-4.0.3/drivers/tty/serial/Makefile
  2860. --- linux-4.0.3.orig/drivers/tty/serial/Makefile 2015-05-13 14:14:53.000000000 +0200
  2861. +++ linux-4.0.3/drivers/tty/serial/Makefile 2015-05-25 17:21:47.000000000 +0200
  2862. @@ -51,6 +51,7 @@
  2863. obj-$(CONFIG_SERIAL_MESON) += meson_uart.o
  2864. obj-$(CONFIG_SERIAL_SB1250_DUART) += sb1250-duart.o
  2865. obj-$(CONFIG_ETRAX_SERIAL) += crisv10.o
  2866. +obj-$(CONFIG_ETRAXFS_SERIAL) += crisv32.o
  2867. obj-$(CONFIG_SERIAL_ETRAXFS) += etraxfs-uart.o
  2868. obj-$(CONFIG_SERIAL_SCCNXP) += sccnxp.o
  2869. obj-$(CONFIG_SERIAL_SC16IS7XX) += sc16is7xx.o
  2870. diff -Nur linux-4.0.3.orig/include/uapi/linux/serial_core.h linux-4.0.3/include/uapi/linux/serial_core.h
  2871. --- linux-4.0.3.orig/include/uapi/linux/serial_core.h 2015-05-13 14:14:53.000000000 +0200
  2872. +++ linux-4.0.3/include/uapi/linux/serial_core.h 2015-05-25 17:20:56.000000000 +0200
  2873. @@ -258,4 +258,7 @@
  2874. /* Cris v10 / v32 SoC */
  2875. #define PORT_CRIS 112
  2876. +/* Cris v10 / v32 SoC */
  2877. +#define PORT_CRIS 109
  2878. +
  2879. #endif /* _UAPILINUX_SERIAL_CORE_H */