mpsc.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082
  1. /*
  2. * Generic driver for the MPSC (UART mode) on Marvell parts (e.g., GT64240,
  3. * GT64260, MV64340, MV64360, GT96100, ... ).
  4. *
  5. * Author: Mark A. Greer <mgreer@mvista.com>
  6. *
  7. * Based on an old MPSC driver that was in the linuxppc tree. It appears to
  8. * have been created by Chris Zankel (formerly of MontaVista) but there
  9. * is no proper Copyright so I'm not sure. Apparently, parts were also
  10. * taken from PPCBoot (now U-Boot). Also based on drivers/serial/8250.c
  11. * by Russell King.
  12. *
  13. * 2004 (c) MontaVista, Software, Inc. This file is licensed under
  14. * the terms of the GNU General Public License version 2. This program
  15. * is licensed "as is" without any warranty of any kind, whether express
  16. * or implied.
  17. */
  18. /*
  19. * The MPSC interface is much like a typical network controller's interface.
  20. * That is, you set up separate rings of descriptors for transmitting and
  21. * receiving data. There is also a pool of buffers with (one buffer per
  22. * descriptor) that incoming data are dma'd into or outgoing data are dma'd
  23. * out of.
  24. *
  25. * The MPSC requires two other controllers to be able to work. The Baud Rate
  26. * Generator (BRG) provides a clock at programmable frequencies which determines
  27. * the baud rate. The Serial DMA Controller (SDMA) takes incoming data from the
  28. * MPSC and DMA's it into memory or DMA's outgoing data and passes it to the
  29. * MPSC. It is actually the SDMA interrupt that the driver uses to keep the
  30. * transmit and receive "engines" going (i.e., indicate data has been
  31. * transmitted or received).
  32. *
  33. * NOTES:
  34. *
  35. * 1) Some chips have an erratum where several regs cannot be
  36. * read. To work around that, we keep a local copy of those regs in
  37. * 'mpsc_port_info'.
  38. *
  39. * 2) Some chips have an erratum where the ctlr will hang when the SDMA ctlr
  40. * accesses system mem with coherency enabled. For that reason, the driver
  41. * assumes that coherency for that ctlr has been disabled. This means
  42. * that when in a cache coherent system, the driver has to manually manage
  43. * the data cache on the areas that it touches because the dma_* macro are
  44. * basically no-ops.
  45. *
  46. * 3) There is an erratum (on PPC) where you can't use the instruction to do
  47. * a DMA_TO_DEVICE/cache clean so DMA_BIDIRECTIONAL/flushes are used in places
  48. * where a DMA_TO_DEVICE/clean would have [otherwise] sufficed.
  49. *
  50. * 4) AFAICT, hardware flow control isn't supported by the controller --MAG.
  51. */
  52. #include <linux/config.h>
  53. #if defined(CONFIG_SERIAL_MPSC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  54. #define SUPPORT_SYSRQ
  55. #endif
  56. #include <linux/module.h>
  57. #include <linux/moduleparam.h>
  58. #include <linux/tty.h>
  59. #include <linux/tty_flip.h>
  60. #include <linux/ioport.h>
  61. #include <linux/init.h>
  62. #include <linux/console.h>
  63. #include <linux/sysrq.h>
  64. #include <linux/serial.h>
  65. #include <linux/serial_core.h>
  66. #include <linux/delay.h>
  67. #include <linux/device.h>
  68. #include <linux/dma-mapping.h>
  69. #include <linux/mv643xx.h>
  70. #include <linux/platform_device.h>
  71. #include <asm/io.h>
  72. #include <asm/irq.h>
  73. #if defined(CONFIG_SERIAL_MPSC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  74. #define SUPPORT_SYSRQ
  75. #endif
  76. #define MPSC_NUM_CTLRS 2
  77. /*
  78. * Descriptors and buffers must be cache line aligned.
  79. * Buffers lengths must be multiple of cache line size.
  80. * Number of Tx & Rx descriptors must be powers of 2.
  81. */
  82. #define MPSC_RXR_ENTRIES 32
  83. #define MPSC_RXRE_SIZE dma_get_cache_alignment()
  84. #define MPSC_RXR_SIZE (MPSC_RXR_ENTRIES * MPSC_RXRE_SIZE)
  85. #define MPSC_RXBE_SIZE dma_get_cache_alignment()
  86. #define MPSC_RXB_SIZE (MPSC_RXR_ENTRIES * MPSC_RXBE_SIZE)
  87. #define MPSC_TXR_ENTRIES 32
  88. #define MPSC_TXRE_SIZE dma_get_cache_alignment()
  89. #define MPSC_TXR_SIZE (MPSC_TXR_ENTRIES * MPSC_TXRE_SIZE)
  90. #define MPSC_TXBE_SIZE dma_get_cache_alignment()
  91. #define MPSC_TXB_SIZE (MPSC_TXR_ENTRIES * MPSC_TXBE_SIZE)
  92. #define MPSC_DMA_ALLOC_SIZE (MPSC_RXR_SIZE + MPSC_RXB_SIZE + \
  93. MPSC_TXR_SIZE + MPSC_TXB_SIZE + \
  94. dma_get_cache_alignment() /* for alignment */)
  95. /* Rx and Tx Ring entry descriptors -- assume entry size is <= cacheline size */
  96. struct mpsc_rx_desc {
  97. u16 bufsize;
  98. u16 bytecnt;
  99. u32 cmdstat;
  100. u32 link;
  101. u32 buf_ptr;
  102. } __attribute((packed));
  103. struct mpsc_tx_desc {
  104. u16 bytecnt;
  105. u16 shadow;
  106. u32 cmdstat;
  107. u32 link;
  108. u32 buf_ptr;
  109. } __attribute((packed));
  110. /*
  111. * Some regs that have the erratum that you can't read them are are shared
  112. * between the two MPSC controllers. This struct contains those shared regs.
  113. */
  114. struct mpsc_shared_regs {
  115. phys_addr_t mpsc_routing_base_p;
  116. phys_addr_t sdma_intr_base_p;
  117. void __iomem *mpsc_routing_base;
  118. void __iomem *sdma_intr_base;
  119. u32 MPSC_MRR_m;
  120. u32 MPSC_RCRR_m;
  121. u32 MPSC_TCRR_m;
  122. u32 SDMA_INTR_CAUSE_m;
  123. u32 SDMA_INTR_MASK_m;
  124. };
  125. /* The main driver data structure */
  126. struct mpsc_port_info {
  127. struct uart_port port; /* Overlay uart_port structure */
  128. /* Internal driver state for this ctlr */
  129. u8 ready;
  130. u8 rcv_data;
  131. tcflag_t c_iflag; /* save termios->c_iflag */
  132. tcflag_t c_cflag; /* save termios->c_cflag */
  133. /* Info passed in from platform */
  134. u8 mirror_regs; /* Need to mirror regs? */
  135. u8 cache_mgmt; /* Need manual cache mgmt? */
  136. u8 brg_can_tune; /* BRG has baud tuning? */
  137. u32 brg_clk_src;
  138. u16 mpsc_max_idle;
  139. int default_baud;
  140. int default_bits;
  141. int default_parity;
  142. int default_flow;
  143. /* Physical addresses of various blocks of registers (from platform) */
  144. phys_addr_t mpsc_base_p;
  145. phys_addr_t sdma_base_p;
  146. phys_addr_t brg_base_p;
  147. /* Virtual addresses of various blocks of registers (from platform) */
  148. void __iomem *mpsc_base;
  149. void __iomem *sdma_base;
  150. void __iomem *brg_base;
  151. /* Descriptor ring and buffer allocations */
  152. void *dma_region;
  153. dma_addr_t dma_region_p;
  154. dma_addr_t rxr; /* Rx descriptor ring */
  155. dma_addr_t rxr_p; /* Phys addr of rxr */
  156. u8 *rxb; /* Rx Ring I/O buf */
  157. u8 *rxb_p; /* Phys addr of rxb */
  158. u32 rxr_posn; /* First desc w/ Rx data */
  159. dma_addr_t txr; /* Tx descriptor ring */
  160. dma_addr_t txr_p; /* Phys addr of txr */
  161. u8 *txb; /* Tx Ring I/O buf */
  162. u8 *txb_p; /* Phys addr of txb */
  163. int txr_head; /* Where new data goes */
  164. int txr_tail; /* Where sent data comes off */
  165. /* Mirrored values of regs we can't read (if 'mirror_regs' set) */
  166. u32 MPSC_MPCR_m;
  167. u32 MPSC_CHR_1_m;
  168. u32 MPSC_CHR_2_m;
  169. u32 MPSC_CHR_10_m;
  170. u32 BRG_BCR_m;
  171. struct mpsc_shared_regs *shared_regs;
  172. };
  173. /* Hooks to platform-specific code */
  174. int mpsc_platform_register_driver(void);
  175. void mpsc_platform_unregister_driver(void);
  176. /* Hooks back in to mpsc common to be called by platform-specific code */
  177. struct mpsc_port_info *mpsc_device_probe(int index);
  178. struct mpsc_port_info *mpsc_device_remove(int index);
  179. /* Main MPSC Configuration Register Offsets */
  180. #define MPSC_MMCRL 0x0000
  181. #define MPSC_MMCRH 0x0004
  182. #define MPSC_MPCR 0x0008
  183. #define MPSC_CHR_1 0x000c
  184. #define MPSC_CHR_2 0x0010
  185. #define MPSC_CHR_3 0x0014
  186. #define MPSC_CHR_4 0x0018
  187. #define MPSC_CHR_5 0x001c
  188. #define MPSC_CHR_6 0x0020
  189. #define MPSC_CHR_7 0x0024
  190. #define MPSC_CHR_8 0x0028
  191. #define MPSC_CHR_9 0x002c
  192. #define MPSC_CHR_10 0x0030
  193. #define MPSC_CHR_11 0x0034
  194. #define MPSC_MPCR_FRZ (1 << 9)
  195. #define MPSC_MPCR_CL_5 0
  196. #define MPSC_MPCR_CL_6 1
  197. #define MPSC_MPCR_CL_7 2
  198. #define MPSC_MPCR_CL_8 3
  199. #define MPSC_MPCR_SBL_1 0
  200. #define MPSC_MPCR_SBL_2 1
  201. #define MPSC_CHR_2_TEV (1<<1)
  202. #define MPSC_CHR_2_TA (1<<7)
  203. #define MPSC_CHR_2_TTCS (1<<9)
  204. #define MPSC_CHR_2_REV (1<<17)
  205. #define MPSC_CHR_2_RA (1<<23)
  206. #define MPSC_CHR_2_CRD (1<<25)
  207. #define MPSC_CHR_2_EH (1<<31)
  208. #define MPSC_CHR_2_PAR_ODD 0
  209. #define MPSC_CHR_2_PAR_SPACE 1
  210. #define MPSC_CHR_2_PAR_EVEN 2
  211. #define MPSC_CHR_2_PAR_MARK 3
  212. /* MPSC Signal Routing */
  213. #define MPSC_MRR 0x0000
  214. #define MPSC_RCRR 0x0004
  215. #define MPSC_TCRR 0x0008
  216. /* Serial DMA Controller Interface Registers */
  217. #define SDMA_SDC 0x0000
  218. #define SDMA_SDCM 0x0008
  219. #define SDMA_RX_DESC 0x0800
  220. #define SDMA_RX_BUF_PTR 0x0808
  221. #define SDMA_SCRDP 0x0810
  222. #define SDMA_TX_DESC 0x0c00
  223. #define SDMA_SCTDP 0x0c10
  224. #define SDMA_SFTDP 0x0c14
  225. #define SDMA_DESC_CMDSTAT_PE (1<<0)
  226. #define SDMA_DESC_CMDSTAT_CDL (1<<1)
  227. #define SDMA_DESC_CMDSTAT_FR (1<<3)
  228. #define SDMA_DESC_CMDSTAT_OR (1<<6)
  229. #define SDMA_DESC_CMDSTAT_BR (1<<9)
  230. #define SDMA_DESC_CMDSTAT_MI (1<<10)
  231. #define SDMA_DESC_CMDSTAT_A (1<<11)
  232. #define SDMA_DESC_CMDSTAT_AM (1<<12)
  233. #define SDMA_DESC_CMDSTAT_CT (1<<13)
  234. #define SDMA_DESC_CMDSTAT_C (1<<14)
  235. #define SDMA_DESC_CMDSTAT_ES (1<<15)
  236. #define SDMA_DESC_CMDSTAT_L (1<<16)
  237. #define SDMA_DESC_CMDSTAT_F (1<<17)
  238. #define SDMA_DESC_CMDSTAT_P (1<<18)
  239. #define SDMA_DESC_CMDSTAT_EI (1<<23)
  240. #define SDMA_DESC_CMDSTAT_O (1<<31)
  241. #define SDMA_DESC_DFLT (SDMA_DESC_CMDSTAT_O | \
  242. SDMA_DESC_CMDSTAT_EI)
  243. #define SDMA_SDC_RFT (1<<0)
  244. #define SDMA_SDC_SFM (1<<1)
  245. #define SDMA_SDC_BLMR (1<<6)
  246. #define SDMA_SDC_BLMT (1<<7)
  247. #define SDMA_SDC_POVR (1<<8)
  248. #define SDMA_SDC_RIFB (1<<9)
  249. #define SDMA_SDCM_ERD (1<<7)
  250. #define SDMA_SDCM_AR (1<<15)
  251. #define SDMA_SDCM_STD (1<<16)
  252. #define SDMA_SDCM_TXD (1<<23)
  253. #define SDMA_SDCM_AT (1<<31)
  254. #define SDMA_0_CAUSE_RXBUF (1<<0)
  255. #define SDMA_0_CAUSE_RXERR (1<<1)
  256. #define SDMA_0_CAUSE_TXBUF (1<<2)
  257. #define SDMA_0_CAUSE_TXEND (1<<3)
  258. #define SDMA_1_CAUSE_RXBUF (1<<8)
  259. #define SDMA_1_CAUSE_RXERR (1<<9)
  260. #define SDMA_1_CAUSE_TXBUF (1<<10)
  261. #define SDMA_1_CAUSE_TXEND (1<<11)
  262. #define SDMA_CAUSE_RX_MASK (SDMA_0_CAUSE_RXBUF | SDMA_0_CAUSE_RXERR | \
  263. SDMA_1_CAUSE_RXBUF | SDMA_1_CAUSE_RXERR)
  264. #define SDMA_CAUSE_TX_MASK (SDMA_0_CAUSE_TXBUF | SDMA_0_CAUSE_TXEND | \
  265. SDMA_1_CAUSE_TXBUF | SDMA_1_CAUSE_TXEND)
  266. /* SDMA Interrupt registers */
  267. #define SDMA_INTR_CAUSE 0x0000
  268. #define SDMA_INTR_MASK 0x0080
  269. /* Baud Rate Generator Interface Registers */
  270. #define BRG_BCR 0x0000
  271. #define BRG_BTR 0x0004
  272. /*
  273. * Define how this driver is known to the outside (we've been assigned a
  274. * range on the "Low-density serial ports" major).
  275. */
  276. #define MPSC_MAJOR 204
  277. #define MPSC_MINOR_START 44
  278. #define MPSC_DRIVER_NAME "MPSC"
  279. #define MPSC_DEVFS_NAME "ttymm/"
  280. #define MPSC_DEV_NAME "ttyMM"
  281. #define MPSC_VERSION "1.00"
  282. static struct mpsc_port_info mpsc_ports[MPSC_NUM_CTLRS];
  283. static struct mpsc_shared_regs mpsc_shared_regs;
  284. static struct uart_driver mpsc_reg;
  285. static void mpsc_start_rx(struct mpsc_port_info *pi);
  286. static void mpsc_free_ring_mem(struct mpsc_port_info *pi);
  287. static void mpsc_release_port(struct uart_port *port);
  288. /*
  289. ******************************************************************************
  290. *
  291. * Baud Rate Generator Routines (BRG)
  292. *
  293. ******************************************************************************
  294. */
  295. static void
  296. mpsc_brg_init(struct mpsc_port_info *pi, u32 clk_src)
  297. {
  298. u32 v;
  299. v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
  300. v = (v & ~(0xf << 18)) | ((clk_src & 0xf) << 18);
  301. if (pi->brg_can_tune)
  302. v &= ~(1 << 25);
  303. if (pi->mirror_regs)
  304. pi->BRG_BCR_m = v;
  305. writel(v, pi->brg_base + BRG_BCR);
  306. writel(readl(pi->brg_base + BRG_BTR) & 0xffff0000,
  307. pi->brg_base + BRG_BTR);
  308. return;
  309. }
  310. static void
  311. mpsc_brg_enable(struct mpsc_port_info *pi)
  312. {
  313. u32 v;
  314. v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
  315. v |= (1 << 16);
  316. if (pi->mirror_regs)
  317. pi->BRG_BCR_m = v;
  318. writel(v, pi->brg_base + BRG_BCR);
  319. return;
  320. }
  321. static void
  322. mpsc_brg_disable(struct mpsc_port_info *pi)
  323. {
  324. u32 v;
  325. v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
  326. v &= ~(1 << 16);
  327. if (pi->mirror_regs)
  328. pi->BRG_BCR_m = v;
  329. writel(v, pi->brg_base + BRG_BCR);
  330. return;
  331. }
  332. static inline void
  333. mpsc_set_baudrate(struct mpsc_port_info *pi, u32 baud)
  334. {
  335. /*
  336. * To set the baud, we adjust the CDV field in the BRG_BCR reg.
  337. * From manual: Baud = clk / ((CDV+1)*2) ==> CDV = (clk / (baud*2)) - 1.
  338. * However, the input clock is divided by 16 in the MPSC b/c of how
  339. * 'MPSC_MMCRH' was set up so we have to divide the 'clk' used in our
  340. * calculation by 16 to account for that. So the real calculation
  341. * that accounts for the way the mpsc is set up is:
  342. * CDV = (clk / (baud*2*16)) - 1 ==> CDV = (clk / (baud << 5)) - 1.
  343. */
  344. u32 cdv = (pi->port.uartclk / (baud << 5)) - 1;
  345. u32 v;
  346. mpsc_brg_disable(pi);
  347. v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
  348. v = (v & 0xffff0000) | (cdv & 0xffff);
  349. if (pi->mirror_regs)
  350. pi->BRG_BCR_m = v;
  351. writel(v, pi->brg_base + BRG_BCR);
  352. mpsc_brg_enable(pi);
  353. return;
  354. }
  355. /*
  356. ******************************************************************************
  357. *
  358. * Serial DMA Routines (SDMA)
  359. *
  360. ******************************************************************************
  361. */
  362. static void
  363. mpsc_sdma_burstsize(struct mpsc_port_info *pi, u32 burst_size)
  364. {
  365. u32 v;
  366. pr_debug("mpsc_sdma_burstsize[%d]: burst_size: %d\n",
  367. pi->port.line, burst_size);
  368. burst_size >>= 3; /* Divide by 8 b/c reg values are 8-byte chunks */
  369. if (burst_size < 2)
  370. v = 0x0; /* 1 64-bit word */
  371. else if (burst_size < 4)
  372. v = 0x1; /* 2 64-bit words */
  373. else if (burst_size < 8)
  374. v = 0x2; /* 4 64-bit words */
  375. else
  376. v = 0x3; /* 8 64-bit words */
  377. writel((readl(pi->sdma_base + SDMA_SDC) & (0x3 << 12)) | (v << 12),
  378. pi->sdma_base + SDMA_SDC);
  379. return;
  380. }
  381. static void
  382. mpsc_sdma_init(struct mpsc_port_info *pi, u32 burst_size)
  383. {
  384. pr_debug("mpsc_sdma_init[%d]: burst_size: %d\n", pi->port.line,
  385. burst_size);
  386. writel((readl(pi->sdma_base + SDMA_SDC) & 0x3ff) | 0x03f,
  387. pi->sdma_base + SDMA_SDC);
  388. mpsc_sdma_burstsize(pi, burst_size);
  389. return;
  390. }
  391. static inline u32
  392. mpsc_sdma_intr_mask(struct mpsc_port_info *pi, u32 mask)
  393. {
  394. u32 old, v;
  395. pr_debug("mpsc_sdma_intr_mask[%d]: mask: 0x%x\n", pi->port.line, mask);
  396. old = v = (pi->mirror_regs) ? pi->shared_regs->SDMA_INTR_MASK_m :
  397. readl(pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
  398. mask &= 0xf;
  399. if (pi->port.line)
  400. mask <<= 8;
  401. v &= ~mask;
  402. if (pi->mirror_regs)
  403. pi->shared_regs->SDMA_INTR_MASK_m = v;
  404. writel(v, pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
  405. if (pi->port.line)
  406. old >>= 8;
  407. return old & 0xf;
  408. }
  409. static inline void
  410. mpsc_sdma_intr_unmask(struct mpsc_port_info *pi, u32 mask)
  411. {
  412. u32 v;
  413. pr_debug("mpsc_sdma_intr_unmask[%d]: mask: 0x%x\n", pi->port.line,mask);
  414. v = (pi->mirror_regs) ? pi->shared_regs->SDMA_INTR_MASK_m :
  415. readl(pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
  416. mask &= 0xf;
  417. if (pi->port.line)
  418. mask <<= 8;
  419. v |= mask;
  420. if (pi->mirror_regs)
  421. pi->shared_regs->SDMA_INTR_MASK_m = v;
  422. writel(v, pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
  423. return;
  424. }
  425. static inline void
  426. mpsc_sdma_intr_ack(struct mpsc_port_info *pi)
  427. {
  428. pr_debug("mpsc_sdma_intr_ack[%d]: Acknowledging IRQ\n", pi->port.line);
  429. if (pi->mirror_regs)
  430. pi->shared_regs->SDMA_INTR_CAUSE_m = 0;
  431. writel(0, pi->shared_regs->sdma_intr_base + SDMA_INTR_CAUSE);
  432. return;
  433. }
  434. static inline void
  435. mpsc_sdma_set_rx_ring(struct mpsc_port_info *pi, struct mpsc_rx_desc *rxre_p)
  436. {
  437. pr_debug("mpsc_sdma_set_rx_ring[%d]: rxre_p: 0x%x\n",
  438. pi->port.line, (u32) rxre_p);
  439. writel((u32)rxre_p, pi->sdma_base + SDMA_SCRDP);
  440. return;
  441. }
  442. static inline void
  443. mpsc_sdma_set_tx_ring(struct mpsc_port_info *pi, struct mpsc_tx_desc *txre_p)
  444. {
  445. writel((u32)txre_p, pi->sdma_base + SDMA_SFTDP);
  446. writel((u32)txre_p, pi->sdma_base + SDMA_SCTDP);
  447. return;
  448. }
  449. static inline void
  450. mpsc_sdma_cmd(struct mpsc_port_info *pi, u32 val)
  451. {
  452. u32 v;
  453. v = readl(pi->sdma_base + SDMA_SDCM);
  454. if (val)
  455. v |= val;
  456. else
  457. v = 0;
  458. wmb();
  459. writel(v, pi->sdma_base + SDMA_SDCM);
  460. wmb();
  461. return;
  462. }
  463. static inline uint
  464. mpsc_sdma_tx_active(struct mpsc_port_info *pi)
  465. {
  466. return readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_TXD;
  467. }
  468. static inline void
  469. mpsc_sdma_start_tx(struct mpsc_port_info *pi)
  470. {
  471. struct mpsc_tx_desc *txre, *txre_p;
  472. /* If tx isn't running & there's a desc ready to go, start it */
  473. if (!mpsc_sdma_tx_active(pi)) {
  474. txre = (struct mpsc_tx_desc *)(pi->txr +
  475. (pi->txr_tail * MPSC_TXRE_SIZE));
  476. dma_cache_sync((void *) txre, MPSC_TXRE_SIZE, DMA_FROM_DEVICE);
  477. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  478. if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
  479. invalidate_dcache_range((ulong)txre,
  480. (ulong)txre + MPSC_TXRE_SIZE);
  481. #endif
  482. if (be32_to_cpu(txre->cmdstat) & SDMA_DESC_CMDSTAT_O) {
  483. txre_p = (struct mpsc_tx_desc *)(pi->txr_p +
  484. (pi->txr_tail *
  485. MPSC_TXRE_SIZE));
  486. mpsc_sdma_set_tx_ring(pi, txre_p);
  487. mpsc_sdma_cmd(pi, SDMA_SDCM_STD | SDMA_SDCM_TXD);
  488. }
  489. }
  490. return;
  491. }
  492. static inline void
  493. mpsc_sdma_stop(struct mpsc_port_info *pi)
  494. {
  495. pr_debug("mpsc_sdma_stop[%d]: Stopping SDMA\n", pi->port.line);
  496. /* Abort any SDMA transfers */
  497. mpsc_sdma_cmd(pi, 0);
  498. mpsc_sdma_cmd(pi, SDMA_SDCM_AR | SDMA_SDCM_AT);
  499. /* Clear the SDMA current and first TX and RX pointers */
  500. mpsc_sdma_set_tx_ring(pi, NULL);
  501. mpsc_sdma_set_rx_ring(pi, NULL);
  502. /* Disable interrupts */
  503. mpsc_sdma_intr_mask(pi, 0xf);
  504. mpsc_sdma_intr_ack(pi);
  505. return;
  506. }
  507. /*
  508. ******************************************************************************
  509. *
  510. * Multi-Protocol Serial Controller Routines (MPSC)
  511. *
  512. ******************************************************************************
  513. */
  514. static void
  515. mpsc_hw_init(struct mpsc_port_info *pi)
  516. {
  517. u32 v;
  518. pr_debug("mpsc_hw_init[%d]: Initializing hardware\n", pi->port.line);
  519. /* Set up clock routing */
  520. if (pi->mirror_regs) {
  521. v = pi->shared_regs->MPSC_MRR_m;
  522. v &= ~0x1c7;
  523. pi->shared_regs->MPSC_MRR_m = v;
  524. writel(v, pi->shared_regs->mpsc_routing_base + MPSC_MRR);
  525. v = pi->shared_regs->MPSC_RCRR_m;
  526. v = (v & ~0xf0f) | 0x100;
  527. pi->shared_regs->MPSC_RCRR_m = v;
  528. writel(v, pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
  529. v = pi->shared_regs->MPSC_TCRR_m;
  530. v = (v & ~0xf0f) | 0x100;
  531. pi->shared_regs->MPSC_TCRR_m = v;
  532. writel(v, pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
  533. }
  534. else {
  535. v = readl(pi->shared_regs->mpsc_routing_base + MPSC_MRR);
  536. v &= ~0x1c7;
  537. writel(v, pi->shared_regs->mpsc_routing_base + MPSC_MRR);
  538. v = readl(pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
  539. v = (v & ~0xf0f) | 0x100;
  540. writel(v, pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
  541. v = readl(pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
  542. v = (v & ~0xf0f) | 0x100;
  543. writel(v, pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
  544. }
  545. /* Put MPSC in UART mode & enabel Tx/Rx egines */
  546. writel(0x000004c4, pi->mpsc_base + MPSC_MMCRL);
  547. /* No preamble, 16x divider, low-latency, */
  548. writel(0x04400400, pi->mpsc_base + MPSC_MMCRH);
  549. if (pi->mirror_regs) {
  550. pi->MPSC_CHR_1_m = 0;
  551. pi->MPSC_CHR_2_m = 0;
  552. }
  553. writel(0, pi->mpsc_base + MPSC_CHR_1);
  554. writel(0, pi->mpsc_base + MPSC_CHR_2);
  555. writel(pi->mpsc_max_idle, pi->mpsc_base + MPSC_CHR_3);
  556. writel(0, pi->mpsc_base + MPSC_CHR_4);
  557. writel(0, pi->mpsc_base + MPSC_CHR_5);
  558. writel(0, pi->mpsc_base + MPSC_CHR_6);
  559. writel(0, pi->mpsc_base + MPSC_CHR_7);
  560. writel(0, pi->mpsc_base + MPSC_CHR_8);
  561. writel(0, pi->mpsc_base + MPSC_CHR_9);
  562. writel(0, pi->mpsc_base + MPSC_CHR_10);
  563. return;
  564. }
  565. static inline void
  566. mpsc_enter_hunt(struct mpsc_port_info *pi)
  567. {
  568. pr_debug("mpsc_enter_hunt[%d]: Hunting...\n", pi->port.line);
  569. if (pi->mirror_regs) {
  570. writel(pi->MPSC_CHR_2_m | MPSC_CHR_2_EH,
  571. pi->mpsc_base + MPSC_CHR_2);
  572. /* Erratum prevents reading CHR_2 so just delay for a while */
  573. udelay(100);
  574. }
  575. else {
  576. writel(readl(pi->mpsc_base + MPSC_CHR_2) | MPSC_CHR_2_EH,
  577. pi->mpsc_base + MPSC_CHR_2);
  578. while (readl(pi->mpsc_base + MPSC_CHR_2) & MPSC_CHR_2_EH)
  579. udelay(10);
  580. }
  581. return;
  582. }
  583. static inline void
  584. mpsc_freeze(struct mpsc_port_info *pi)
  585. {
  586. u32 v;
  587. pr_debug("mpsc_freeze[%d]: Freezing\n", pi->port.line);
  588. v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
  589. readl(pi->mpsc_base + MPSC_MPCR);
  590. v |= MPSC_MPCR_FRZ;
  591. if (pi->mirror_regs)
  592. pi->MPSC_MPCR_m = v;
  593. writel(v, pi->mpsc_base + MPSC_MPCR);
  594. return;
  595. }
  596. static inline void
  597. mpsc_unfreeze(struct mpsc_port_info *pi)
  598. {
  599. u32 v;
  600. v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
  601. readl(pi->mpsc_base + MPSC_MPCR);
  602. v &= ~MPSC_MPCR_FRZ;
  603. if (pi->mirror_regs)
  604. pi->MPSC_MPCR_m = v;
  605. writel(v, pi->mpsc_base + MPSC_MPCR);
  606. pr_debug("mpsc_unfreeze[%d]: Unfrozen\n", pi->port.line);
  607. return;
  608. }
  609. static inline void
  610. mpsc_set_char_length(struct mpsc_port_info *pi, u32 len)
  611. {
  612. u32 v;
  613. pr_debug("mpsc_set_char_length[%d]: char len: %d\n", pi->port.line,len);
  614. v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
  615. readl(pi->mpsc_base + MPSC_MPCR);
  616. v = (v & ~(0x3 << 12)) | ((len & 0x3) << 12);
  617. if (pi->mirror_regs)
  618. pi->MPSC_MPCR_m = v;
  619. writel(v, pi->mpsc_base + MPSC_MPCR);
  620. return;
  621. }
  622. static inline void
  623. mpsc_set_stop_bit_length(struct mpsc_port_info *pi, u32 len)
  624. {
  625. u32 v;
  626. pr_debug("mpsc_set_stop_bit_length[%d]: stop bits: %d\n",
  627. pi->port.line, len);
  628. v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
  629. readl(pi->mpsc_base + MPSC_MPCR);
  630. v = (v & ~(1 << 14)) | ((len & 0x1) << 14);
  631. if (pi->mirror_regs)
  632. pi->MPSC_MPCR_m = v;
  633. writel(v, pi->mpsc_base + MPSC_MPCR);
  634. return;
  635. }
  636. static inline void
  637. mpsc_set_parity(struct mpsc_port_info *pi, u32 p)
  638. {
  639. u32 v;
  640. pr_debug("mpsc_set_parity[%d]: parity bits: 0x%x\n", pi->port.line, p);
  641. v = (pi->mirror_regs) ? pi->MPSC_CHR_2_m :
  642. readl(pi->mpsc_base + MPSC_CHR_2);
  643. p &= 0x3;
  644. v = (v & ~0xc000c) | (p << 18) | (p << 2);
  645. if (pi->mirror_regs)
  646. pi->MPSC_CHR_2_m = v;
  647. writel(v, pi->mpsc_base + MPSC_CHR_2);
  648. return;
  649. }
  650. /*
  651. ******************************************************************************
  652. *
  653. * Driver Init Routines
  654. *
  655. ******************************************************************************
  656. */
  657. static void
  658. mpsc_init_hw(struct mpsc_port_info *pi)
  659. {
  660. pr_debug("mpsc_init_hw[%d]: Initializing\n", pi->port.line);
  661. mpsc_brg_init(pi, pi->brg_clk_src);
  662. mpsc_brg_enable(pi);
  663. mpsc_sdma_init(pi, dma_get_cache_alignment()); /* burst a cacheline */
  664. mpsc_sdma_stop(pi);
  665. mpsc_hw_init(pi);
  666. return;
  667. }
  668. static int
  669. mpsc_alloc_ring_mem(struct mpsc_port_info *pi)
  670. {
  671. int rc = 0;
  672. pr_debug("mpsc_alloc_ring_mem[%d]: Allocating ring mem\n",
  673. pi->port.line);
  674. if (!pi->dma_region) {
  675. if (!dma_supported(pi->port.dev, 0xffffffff)) {
  676. printk(KERN_ERR "MPSC: Inadequate DMA support\n");
  677. rc = -ENXIO;
  678. }
  679. else if ((pi->dma_region = dma_alloc_noncoherent(pi->port.dev,
  680. MPSC_DMA_ALLOC_SIZE, &pi->dma_region_p, GFP_KERNEL))
  681. == NULL) {
  682. printk(KERN_ERR "MPSC: Can't alloc Desc region\n");
  683. rc = -ENOMEM;
  684. }
  685. }
  686. return rc;
  687. }
  688. static void
  689. mpsc_free_ring_mem(struct mpsc_port_info *pi)
  690. {
  691. pr_debug("mpsc_free_ring_mem[%d]: Freeing ring mem\n", pi->port.line);
  692. if (pi->dma_region) {
  693. dma_free_noncoherent(pi->port.dev, MPSC_DMA_ALLOC_SIZE,
  694. pi->dma_region, pi->dma_region_p);
  695. pi->dma_region = NULL;
  696. pi->dma_region_p = (dma_addr_t) NULL;
  697. }
  698. return;
  699. }
  700. static void
  701. mpsc_init_rings(struct mpsc_port_info *pi)
  702. {
  703. struct mpsc_rx_desc *rxre;
  704. struct mpsc_tx_desc *txre;
  705. dma_addr_t dp, dp_p;
  706. u8 *bp, *bp_p;
  707. int i;
  708. pr_debug("mpsc_init_rings[%d]: Initializing rings\n", pi->port.line);
  709. BUG_ON(pi->dma_region == NULL);
  710. memset(pi->dma_region, 0, MPSC_DMA_ALLOC_SIZE);
  711. /*
  712. * Descriptors & buffers are multiples of cacheline size and must be
  713. * cacheline aligned.
  714. */
  715. dp = ALIGN((u32) pi->dma_region, dma_get_cache_alignment());
  716. dp_p = ALIGN((u32) pi->dma_region_p, dma_get_cache_alignment());
  717. /*
  718. * Partition dma region into rx ring descriptor, rx buffers,
  719. * tx ring descriptors, and tx buffers.
  720. */
  721. pi->rxr = dp;
  722. pi->rxr_p = dp_p;
  723. dp += MPSC_RXR_SIZE;
  724. dp_p += MPSC_RXR_SIZE;
  725. pi->rxb = (u8 *) dp;
  726. pi->rxb_p = (u8 *) dp_p;
  727. dp += MPSC_RXB_SIZE;
  728. dp_p += MPSC_RXB_SIZE;
  729. pi->rxr_posn = 0;
  730. pi->txr = dp;
  731. pi->txr_p = dp_p;
  732. dp += MPSC_TXR_SIZE;
  733. dp_p += MPSC_TXR_SIZE;
  734. pi->txb = (u8 *) dp;
  735. pi->txb_p = (u8 *) dp_p;
  736. pi->txr_head = 0;
  737. pi->txr_tail = 0;
  738. /* Init rx ring descriptors */
  739. dp = pi->rxr;
  740. dp_p = pi->rxr_p;
  741. bp = pi->rxb;
  742. bp_p = pi->rxb_p;
  743. for (i = 0; i < MPSC_RXR_ENTRIES; i++) {
  744. rxre = (struct mpsc_rx_desc *)dp;
  745. rxre->bufsize = cpu_to_be16(MPSC_RXBE_SIZE);
  746. rxre->bytecnt = cpu_to_be16(0);
  747. rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O |
  748. SDMA_DESC_CMDSTAT_EI |
  749. SDMA_DESC_CMDSTAT_F |
  750. SDMA_DESC_CMDSTAT_L);
  751. rxre->link = cpu_to_be32(dp_p + MPSC_RXRE_SIZE);
  752. rxre->buf_ptr = cpu_to_be32(bp_p);
  753. dp += MPSC_RXRE_SIZE;
  754. dp_p += MPSC_RXRE_SIZE;
  755. bp += MPSC_RXBE_SIZE;
  756. bp_p += MPSC_RXBE_SIZE;
  757. }
  758. rxre->link = cpu_to_be32(pi->rxr_p); /* Wrap last back to first */
  759. /* Init tx ring descriptors */
  760. dp = pi->txr;
  761. dp_p = pi->txr_p;
  762. bp = pi->txb;
  763. bp_p = pi->txb_p;
  764. for (i = 0; i < MPSC_TXR_ENTRIES; i++) {
  765. txre = (struct mpsc_tx_desc *)dp;
  766. txre->link = cpu_to_be32(dp_p + MPSC_TXRE_SIZE);
  767. txre->buf_ptr = cpu_to_be32(bp_p);
  768. dp += MPSC_TXRE_SIZE;
  769. dp_p += MPSC_TXRE_SIZE;
  770. bp += MPSC_TXBE_SIZE;
  771. bp_p += MPSC_TXBE_SIZE;
  772. }
  773. txre->link = cpu_to_be32(pi->txr_p); /* Wrap last back to first */
  774. dma_cache_sync((void *) pi->dma_region, MPSC_DMA_ALLOC_SIZE,
  775. DMA_BIDIRECTIONAL);
  776. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  777. if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
  778. flush_dcache_range((ulong)pi->dma_region,
  779. (ulong)pi->dma_region + MPSC_DMA_ALLOC_SIZE);
  780. #endif
  781. return;
  782. }
  783. static void
  784. mpsc_uninit_rings(struct mpsc_port_info *pi)
  785. {
  786. pr_debug("mpsc_uninit_rings[%d]: Uninitializing rings\n",pi->port.line);
  787. BUG_ON(pi->dma_region == NULL);
  788. pi->rxr = 0;
  789. pi->rxr_p = 0;
  790. pi->rxb = NULL;
  791. pi->rxb_p = NULL;
  792. pi->rxr_posn = 0;
  793. pi->txr = 0;
  794. pi->txr_p = 0;
  795. pi->txb = NULL;
  796. pi->txb_p = NULL;
  797. pi->txr_head = 0;
  798. pi->txr_tail = 0;
  799. return;
  800. }
  801. static int
  802. mpsc_make_ready(struct mpsc_port_info *pi)
  803. {
  804. int rc;
  805. pr_debug("mpsc_make_ready[%d]: Making cltr ready\n", pi->port.line);
  806. if (!pi->ready) {
  807. mpsc_init_hw(pi);
  808. if ((rc = mpsc_alloc_ring_mem(pi)))
  809. return rc;
  810. mpsc_init_rings(pi);
  811. pi->ready = 1;
  812. }
  813. return 0;
  814. }
  815. /*
  816. ******************************************************************************
  817. *
  818. * Interrupt Handling Routines
  819. *
  820. ******************************************************************************
  821. */
  822. static inline int
  823. mpsc_rx_intr(struct mpsc_port_info *pi, struct pt_regs *regs)
  824. {
  825. struct mpsc_rx_desc *rxre;
  826. struct tty_struct *tty = pi->port.info->tty;
  827. u32 cmdstat, bytes_in, i;
  828. int rc = 0;
  829. u8 *bp;
  830. char flag = TTY_NORMAL;
  831. pr_debug("mpsc_rx_intr[%d]: Handling Rx intr\n", pi->port.line);
  832. rxre = (struct mpsc_rx_desc *)(pi->rxr + (pi->rxr_posn*MPSC_RXRE_SIZE));
  833. dma_cache_sync((void *)rxre, MPSC_RXRE_SIZE, DMA_FROM_DEVICE);
  834. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  835. if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
  836. invalidate_dcache_range((ulong)rxre,
  837. (ulong)rxre + MPSC_RXRE_SIZE);
  838. #endif
  839. /*
  840. * Loop through Rx descriptors handling ones that have been completed.
  841. */
  842. while (!((cmdstat = be32_to_cpu(rxre->cmdstat)) & SDMA_DESC_CMDSTAT_O)){
  843. bytes_in = be16_to_cpu(rxre->bytecnt);
  844. /* Following use of tty struct directly is deprecated */
  845. if (unlikely(tty_buffer_request_room(tty, bytes_in) < bytes_in)) {
  846. if (tty->low_latency)
  847. tty_flip_buffer_push(tty);
  848. /*
  849. * If this failed then we will throw away the bytes
  850. * but must do so to clear interrupts.
  851. */
  852. }
  853. bp = pi->rxb + (pi->rxr_posn * MPSC_RXBE_SIZE);
  854. dma_cache_sync((void *) bp, MPSC_RXBE_SIZE, DMA_FROM_DEVICE);
  855. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  856. if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
  857. invalidate_dcache_range((ulong)bp,
  858. (ulong)bp + MPSC_RXBE_SIZE);
  859. #endif
  860. /*
  861. * Other than for parity error, the manual provides little
  862. * info on what data will be in a frame flagged by any of
  863. * these errors. For parity error, it is the last byte in
  864. * the buffer that had the error. As for the rest, I guess
  865. * we'll assume there is no data in the buffer.
  866. * If there is...it gets lost.
  867. */
  868. if (unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR |
  869. SDMA_DESC_CMDSTAT_FR | SDMA_DESC_CMDSTAT_OR))) {
  870. pi->port.icount.rx++;
  871. if (cmdstat & SDMA_DESC_CMDSTAT_BR) { /* Break */
  872. pi->port.icount.brk++;
  873. if (uart_handle_break(&pi->port))
  874. goto next_frame;
  875. }
  876. else if (cmdstat & SDMA_DESC_CMDSTAT_FR)/* Framing */
  877. pi->port.icount.frame++;
  878. else if (cmdstat & SDMA_DESC_CMDSTAT_OR) /* Overrun */
  879. pi->port.icount.overrun++;
  880. cmdstat &= pi->port.read_status_mask;
  881. if (cmdstat & SDMA_DESC_CMDSTAT_BR)
  882. flag = TTY_BREAK;
  883. else if (cmdstat & SDMA_DESC_CMDSTAT_FR)
  884. flag = TTY_FRAME;
  885. else if (cmdstat & SDMA_DESC_CMDSTAT_OR)
  886. flag = TTY_OVERRUN;
  887. else if (cmdstat & SDMA_DESC_CMDSTAT_PE)
  888. flag = TTY_PARITY;
  889. }
  890. if (uart_handle_sysrq_char(&pi->port, *bp, regs)) {
  891. bp++;
  892. bytes_in--;
  893. goto next_frame;
  894. }
  895. if ((unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR |
  896. SDMA_DESC_CMDSTAT_FR | SDMA_DESC_CMDSTAT_OR))) &&
  897. !(cmdstat & pi->port.ignore_status_mask))
  898. tty_insert_flip_char(tty, *bp, flag);
  899. else {
  900. for (i=0; i<bytes_in; i++)
  901. tty_insert_flip_char(tty, *bp++, TTY_NORMAL);
  902. pi->port.icount.rx += bytes_in;
  903. }
  904. next_frame:
  905. rxre->bytecnt = cpu_to_be16(0);
  906. wmb();
  907. rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O |
  908. SDMA_DESC_CMDSTAT_EI |
  909. SDMA_DESC_CMDSTAT_F |
  910. SDMA_DESC_CMDSTAT_L);
  911. wmb();
  912. dma_cache_sync((void *)rxre, MPSC_RXRE_SIZE, DMA_BIDIRECTIONAL);
  913. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  914. if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
  915. flush_dcache_range((ulong)rxre,
  916. (ulong)rxre + MPSC_RXRE_SIZE);
  917. #endif
  918. /* Advance to next descriptor */
  919. pi->rxr_posn = (pi->rxr_posn + 1) & (MPSC_RXR_ENTRIES - 1);
  920. rxre = (struct mpsc_rx_desc *)(pi->rxr +
  921. (pi->rxr_posn * MPSC_RXRE_SIZE));
  922. dma_cache_sync((void *)rxre, MPSC_RXRE_SIZE, DMA_FROM_DEVICE);
  923. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  924. if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
  925. invalidate_dcache_range((ulong)rxre,
  926. (ulong)rxre + MPSC_RXRE_SIZE);
  927. #endif
  928. rc = 1;
  929. }
  930. /* Restart rx engine, if its stopped */
  931. if ((readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_ERD) == 0)
  932. mpsc_start_rx(pi);
  933. tty_flip_buffer_push(tty);
  934. return rc;
  935. }
  936. static inline void
  937. mpsc_setup_tx_desc(struct mpsc_port_info *pi, u32 count, u32 intr)
  938. {
  939. struct mpsc_tx_desc *txre;
  940. txre = (struct mpsc_tx_desc *)(pi->txr +
  941. (pi->txr_head * MPSC_TXRE_SIZE));
  942. txre->bytecnt = cpu_to_be16(count);
  943. txre->shadow = txre->bytecnt;
  944. wmb(); /* ensure cmdstat is last field updated */
  945. txre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O | SDMA_DESC_CMDSTAT_F |
  946. SDMA_DESC_CMDSTAT_L | ((intr) ?
  947. SDMA_DESC_CMDSTAT_EI
  948. : 0));
  949. wmb();
  950. dma_cache_sync((void *) txre, MPSC_TXRE_SIZE, DMA_BIDIRECTIONAL);
  951. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  952. if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
  953. flush_dcache_range((ulong)txre,
  954. (ulong)txre + MPSC_TXRE_SIZE);
  955. #endif
  956. return;
  957. }
  958. static inline void
  959. mpsc_copy_tx_data(struct mpsc_port_info *pi)
  960. {
  961. struct circ_buf *xmit = &pi->port.info->xmit;
  962. u8 *bp;
  963. u32 i;
  964. /* Make sure the desc ring isn't full */
  965. while (CIRC_CNT(pi->txr_head, pi->txr_tail, MPSC_TXR_ENTRIES) <
  966. (MPSC_TXR_ENTRIES - 1)) {
  967. if (pi->port.x_char) {
  968. /*
  969. * Ideally, we should use the TCS field in
  970. * CHR_1 to put the x_char out immediately but
  971. * errata prevents us from being able to read
  972. * CHR_2 to know that its safe to write to
  973. * CHR_1. Instead, just put it in-band with
  974. * all the other Tx data.
  975. */
  976. bp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
  977. *bp = pi->port.x_char;
  978. pi->port.x_char = 0;
  979. i = 1;
  980. }
  981. else if (!uart_circ_empty(xmit) && !uart_tx_stopped(&pi->port)){
  982. i = min((u32) MPSC_TXBE_SIZE,
  983. (u32) uart_circ_chars_pending(xmit));
  984. i = min(i, (u32) CIRC_CNT_TO_END(xmit->head, xmit->tail,
  985. UART_XMIT_SIZE));
  986. bp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
  987. memcpy(bp, &xmit->buf[xmit->tail], i);
  988. xmit->tail = (xmit->tail + i) & (UART_XMIT_SIZE - 1);
  989. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  990. uart_write_wakeup(&pi->port);
  991. }
  992. else /* All tx data copied into ring bufs */
  993. return;
  994. dma_cache_sync((void *) bp, MPSC_TXBE_SIZE, DMA_BIDIRECTIONAL);
  995. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  996. if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
  997. flush_dcache_range((ulong)bp,
  998. (ulong)bp + MPSC_TXBE_SIZE);
  999. #endif
  1000. mpsc_setup_tx_desc(pi, i, 1);
  1001. /* Advance to next descriptor */
  1002. pi->txr_head = (pi->txr_head + 1) & (MPSC_TXR_ENTRIES - 1);
  1003. }
  1004. return;
  1005. }
  1006. static inline int
  1007. mpsc_tx_intr(struct mpsc_port_info *pi)
  1008. {
  1009. struct mpsc_tx_desc *txre;
  1010. int rc = 0;
  1011. if (!mpsc_sdma_tx_active(pi)) {
  1012. txre = (struct mpsc_tx_desc *)(pi->txr +
  1013. (pi->txr_tail * MPSC_TXRE_SIZE));
  1014. dma_cache_sync((void *) txre, MPSC_TXRE_SIZE, DMA_FROM_DEVICE);
  1015. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  1016. if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
  1017. invalidate_dcache_range((ulong)txre,
  1018. (ulong)txre + MPSC_TXRE_SIZE);
  1019. #endif
  1020. while (!(be32_to_cpu(txre->cmdstat) & SDMA_DESC_CMDSTAT_O)) {
  1021. rc = 1;
  1022. pi->port.icount.tx += be16_to_cpu(txre->bytecnt);
  1023. pi->txr_tail = (pi->txr_tail+1) & (MPSC_TXR_ENTRIES-1);
  1024. /* If no more data to tx, fall out of loop */
  1025. if (pi->txr_head == pi->txr_tail)
  1026. break;
  1027. txre = (struct mpsc_tx_desc *)(pi->txr +
  1028. (pi->txr_tail * MPSC_TXRE_SIZE));
  1029. dma_cache_sync((void *) txre, MPSC_TXRE_SIZE,
  1030. DMA_FROM_DEVICE);
  1031. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  1032. if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
  1033. invalidate_dcache_range((ulong)txre,
  1034. (ulong)txre + MPSC_TXRE_SIZE);
  1035. #endif
  1036. }
  1037. mpsc_copy_tx_data(pi);
  1038. mpsc_sdma_start_tx(pi); /* start next desc if ready */
  1039. }
  1040. return rc;
  1041. }
  1042. /*
  1043. * This is the driver's interrupt handler. To avoid a race, we first clear
  1044. * the interrupt, then handle any completed Rx/Tx descriptors. When done
  1045. * handling those descriptors, we restart the Rx/Tx engines if they're stopped.
  1046. */
  1047. static irqreturn_t
  1048. mpsc_sdma_intr(int irq, void *dev_id, struct pt_regs *regs)
  1049. {
  1050. struct mpsc_port_info *pi = dev_id;
  1051. ulong iflags;
  1052. int rc = IRQ_NONE;
  1053. pr_debug("mpsc_sdma_intr[%d]: SDMA Interrupt Received\n",pi->port.line);
  1054. spin_lock_irqsave(&pi->port.lock, iflags);
  1055. mpsc_sdma_intr_ack(pi);
  1056. if (mpsc_rx_intr(pi, regs))
  1057. rc = IRQ_HANDLED;
  1058. if (mpsc_tx_intr(pi))
  1059. rc = IRQ_HANDLED;
  1060. spin_unlock_irqrestore(&pi->port.lock, iflags);
  1061. pr_debug("mpsc_sdma_intr[%d]: SDMA Interrupt Handled\n", pi->port.line);
  1062. return rc;
  1063. }
  1064. /*
  1065. ******************************************************************************
  1066. *
  1067. * serial_core.c Interface routines
  1068. *
  1069. ******************************************************************************
  1070. */
  1071. static uint
  1072. mpsc_tx_empty(struct uart_port *port)
  1073. {
  1074. struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
  1075. ulong iflags;
  1076. uint rc;
  1077. spin_lock_irqsave(&pi->port.lock, iflags);
  1078. rc = mpsc_sdma_tx_active(pi) ? 0 : TIOCSER_TEMT;
  1079. spin_unlock_irqrestore(&pi->port.lock, iflags);
  1080. return rc;
  1081. }
  1082. static void
  1083. mpsc_set_mctrl(struct uart_port *port, uint mctrl)
  1084. {
  1085. /* Have no way to set modem control lines AFAICT */
  1086. return;
  1087. }
  1088. static uint
  1089. mpsc_get_mctrl(struct uart_port *port)
  1090. {
  1091. struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
  1092. u32 mflags, status;
  1093. status = (pi->mirror_regs) ? pi->MPSC_CHR_10_m :
  1094. readl(pi->mpsc_base + MPSC_CHR_10);
  1095. mflags = 0;
  1096. if (status & 0x1)
  1097. mflags |= TIOCM_CTS;
  1098. if (status & 0x2)
  1099. mflags |= TIOCM_CAR;
  1100. return mflags | TIOCM_DSR; /* No way to tell if DSR asserted */
  1101. }
  1102. static void
  1103. mpsc_stop_tx(struct uart_port *port)
  1104. {
  1105. struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
  1106. pr_debug("mpsc_stop_tx[%d]\n", port->line);
  1107. mpsc_freeze(pi);
  1108. return;
  1109. }
  1110. static void
  1111. mpsc_start_tx(struct uart_port *port)
  1112. {
  1113. struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
  1114. mpsc_unfreeze(pi);
  1115. mpsc_copy_tx_data(pi);
  1116. mpsc_sdma_start_tx(pi);
  1117. pr_debug("mpsc_start_tx[%d]\n", port->line);
  1118. return;
  1119. }
  1120. static void
  1121. mpsc_start_rx(struct mpsc_port_info *pi)
  1122. {
  1123. pr_debug("mpsc_start_rx[%d]: Starting...\n", pi->port.line);
  1124. /* Issue a Receive Abort to clear any receive errors */
  1125. writel(MPSC_CHR_2_RA, pi->mpsc_base + MPSC_CHR_2);
  1126. if (pi->rcv_data) {
  1127. mpsc_enter_hunt(pi);
  1128. mpsc_sdma_cmd(pi, SDMA_SDCM_ERD);
  1129. }
  1130. return;
  1131. }
  1132. static void
  1133. mpsc_stop_rx(struct uart_port *port)
  1134. {
  1135. struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
  1136. pr_debug("mpsc_stop_rx[%d]: Stopping...\n", port->line);
  1137. mpsc_sdma_cmd(pi, SDMA_SDCM_AR);
  1138. return;
  1139. }
  1140. static void
  1141. mpsc_enable_ms(struct uart_port *port)
  1142. {
  1143. return; /* Not supported */
  1144. }
  1145. static void
  1146. mpsc_break_ctl(struct uart_port *port, int ctl)
  1147. {
  1148. struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
  1149. ulong flags;
  1150. u32 v;
  1151. v = ctl ? 0x00ff0000 : 0;
  1152. spin_lock_irqsave(&pi->port.lock, flags);
  1153. if (pi->mirror_regs)
  1154. pi->MPSC_CHR_1_m = v;
  1155. writel(v, pi->mpsc_base + MPSC_CHR_1);
  1156. spin_unlock_irqrestore(&pi->port.lock, flags);
  1157. return;
  1158. }
  1159. static int
  1160. mpsc_startup(struct uart_port *port)
  1161. {
  1162. struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
  1163. u32 flag = 0;
  1164. int rc;
  1165. pr_debug("mpsc_startup[%d]: Starting up MPSC, irq: %d\n",
  1166. port->line, pi->port.irq);
  1167. if ((rc = mpsc_make_ready(pi)) == 0) {
  1168. /* Setup IRQ handler */
  1169. mpsc_sdma_intr_ack(pi);
  1170. /* If irq's are shared, need to set flag */
  1171. if (mpsc_ports[0].port.irq == mpsc_ports[1].port.irq)
  1172. flag = SA_SHIRQ;
  1173. if (request_irq(pi->port.irq, mpsc_sdma_intr, flag,
  1174. "mpsc-sdma", pi))
  1175. printk(KERN_ERR "MPSC: Can't get SDMA IRQ %d\n",
  1176. pi->port.irq);
  1177. mpsc_sdma_intr_unmask(pi, 0xf);
  1178. mpsc_sdma_set_rx_ring(pi, (struct mpsc_rx_desc *)(pi->rxr_p +
  1179. (pi->rxr_posn * MPSC_RXRE_SIZE)));
  1180. }
  1181. return rc;
  1182. }
  1183. static void
  1184. mpsc_shutdown(struct uart_port *port)
  1185. {
  1186. struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
  1187. pr_debug("mpsc_shutdown[%d]: Shutting down MPSC\n", port->line);
  1188. mpsc_sdma_stop(pi);
  1189. free_irq(pi->port.irq, pi);
  1190. return;
  1191. }
  1192. static void
  1193. mpsc_set_termios(struct uart_port *port, struct termios *termios,
  1194. struct termios *old)
  1195. {
  1196. struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
  1197. u32 baud;
  1198. ulong flags;
  1199. u32 chr_bits, stop_bits, par;
  1200. pi->c_iflag = termios->c_iflag;
  1201. pi->c_cflag = termios->c_cflag;
  1202. switch (termios->c_cflag & CSIZE) {
  1203. case CS5:
  1204. chr_bits = MPSC_MPCR_CL_5;
  1205. break;
  1206. case CS6:
  1207. chr_bits = MPSC_MPCR_CL_6;
  1208. break;
  1209. case CS7:
  1210. chr_bits = MPSC_MPCR_CL_7;
  1211. break;
  1212. case CS8:
  1213. default:
  1214. chr_bits = MPSC_MPCR_CL_8;
  1215. break;
  1216. }
  1217. if (termios->c_cflag & CSTOPB)
  1218. stop_bits = MPSC_MPCR_SBL_2;
  1219. else
  1220. stop_bits = MPSC_MPCR_SBL_1;
  1221. par = MPSC_CHR_2_PAR_EVEN;
  1222. if (termios->c_cflag & PARENB)
  1223. if (termios->c_cflag & PARODD)
  1224. par = MPSC_CHR_2_PAR_ODD;
  1225. #ifdef CMSPAR
  1226. if (termios->c_cflag & CMSPAR) {
  1227. if (termios->c_cflag & PARODD)
  1228. par = MPSC_CHR_2_PAR_MARK;
  1229. else
  1230. par = MPSC_CHR_2_PAR_SPACE;
  1231. }
  1232. #endif
  1233. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk);
  1234. spin_lock_irqsave(&pi->port.lock, flags);
  1235. uart_update_timeout(port, termios->c_cflag, baud);
  1236. mpsc_set_char_length(pi, chr_bits);
  1237. mpsc_set_stop_bit_length(pi, stop_bits);
  1238. mpsc_set_parity(pi, par);
  1239. mpsc_set_baudrate(pi, baud);
  1240. /* Characters/events to read */
  1241. pi->rcv_data = 1;
  1242. pi->port.read_status_mask = SDMA_DESC_CMDSTAT_OR;
  1243. if (termios->c_iflag & INPCK)
  1244. pi->port.read_status_mask |= SDMA_DESC_CMDSTAT_PE |
  1245. SDMA_DESC_CMDSTAT_FR;
  1246. if (termios->c_iflag & (BRKINT | PARMRK))
  1247. pi->port.read_status_mask |= SDMA_DESC_CMDSTAT_BR;
  1248. /* Characters/events to ignore */
  1249. pi->port.ignore_status_mask = 0;
  1250. if (termios->c_iflag & IGNPAR)
  1251. pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_PE |
  1252. SDMA_DESC_CMDSTAT_FR;
  1253. if (termios->c_iflag & IGNBRK) {
  1254. pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_BR;
  1255. if (termios->c_iflag & IGNPAR)
  1256. pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_OR;
  1257. }
  1258. /* Ignore all chars if CREAD not set */
  1259. if (!(termios->c_cflag & CREAD))
  1260. pi->rcv_data = 0;
  1261. else
  1262. mpsc_start_rx(pi);
  1263. spin_unlock_irqrestore(&pi->port.lock, flags);
  1264. return;
  1265. }
  1266. static const char *
  1267. mpsc_type(struct uart_port *port)
  1268. {
  1269. pr_debug("mpsc_type[%d]: port type: %s\n", port->line,MPSC_DRIVER_NAME);
  1270. return MPSC_DRIVER_NAME;
  1271. }
  1272. static int
  1273. mpsc_request_port(struct uart_port *port)
  1274. {
  1275. /* Should make chip/platform specific call */
  1276. return 0;
  1277. }
  1278. static void
  1279. mpsc_release_port(struct uart_port *port)
  1280. {
  1281. struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
  1282. if (pi->ready) {
  1283. mpsc_uninit_rings(pi);
  1284. mpsc_free_ring_mem(pi);
  1285. pi->ready = 0;
  1286. }
  1287. return;
  1288. }
  1289. static void
  1290. mpsc_config_port(struct uart_port *port, int flags)
  1291. {
  1292. return;
  1293. }
  1294. static int
  1295. mpsc_verify_port(struct uart_port *port, struct serial_struct *ser)
  1296. {
  1297. struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
  1298. int rc = 0;
  1299. pr_debug("mpsc_verify_port[%d]: Verifying port data\n", pi->port.line);
  1300. if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPSC)
  1301. rc = -EINVAL;
  1302. else if (pi->port.irq != ser->irq)
  1303. rc = -EINVAL;
  1304. else if (ser->io_type != SERIAL_IO_MEM)
  1305. rc = -EINVAL;
  1306. else if (pi->port.uartclk / 16 != ser->baud_base) /* Not sure */
  1307. rc = -EINVAL;
  1308. else if ((void *)pi->port.mapbase != ser->iomem_base)
  1309. rc = -EINVAL;
  1310. else if (pi->port.iobase != ser->port)
  1311. rc = -EINVAL;
  1312. else if (ser->hub6 != 0)
  1313. rc = -EINVAL;
  1314. return rc;
  1315. }
  1316. static struct uart_ops mpsc_pops = {
  1317. .tx_empty = mpsc_tx_empty,
  1318. .set_mctrl = mpsc_set_mctrl,
  1319. .get_mctrl = mpsc_get_mctrl,
  1320. .stop_tx = mpsc_stop_tx,
  1321. .start_tx = mpsc_start_tx,
  1322. .stop_rx = mpsc_stop_rx,
  1323. .enable_ms = mpsc_enable_ms,
  1324. .break_ctl = mpsc_break_ctl,
  1325. .startup = mpsc_startup,
  1326. .shutdown = mpsc_shutdown,
  1327. .set_termios = mpsc_set_termios,
  1328. .type = mpsc_type,
  1329. .release_port = mpsc_release_port,
  1330. .request_port = mpsc_request_port,
  1331. .config_port = mpsc_config_port,
  1332. .verify_port = mpsc_verify_port,
  1333. };
  1334. /*
  1335. ******************************************************************************
  1336. *
  1337. * Console Interface Routines
  1338. *
  1339. ******************************************************************************
  1340. */
  1341. #ifdef CONFIG_SERIAL_MPSC_CONSOLE
  1342. static void
  1343. mpsc_console_write(struct console *co, const char *s, uint count)
  1344. {
  1345. struct mpsc_port_info *pi = &mpsc_ports[co->index];
  1346. u8 *bp, *dp, add_cr = 0;
  1347. int i;
  1348. while (mpsc_sdma_tx_active(pi))
  1349. udelay(100);
  1350. while (count > 0) {
  1351. bp = dp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
  1352. for (i = 0; i < MPSC_TXBE_SIZE; i++) {
  1353. if (count == 0)
  1354. break;
  1355. if (add_cr) {
  1356. *(dp++) = '\r';
  1357. add_cr = 0;
  1358. }
  1359. else {
  1360. *(dp++) = *s;
  1361. if (*(s++) == '\n') { /* add '\r' after '\n' */
  1362. add_cr = 1;
  1363. count++;
  1364. }
  1365. }
  1366. count--;
  1367. }
  1368. dma_cache_sync((void *) bp, MPSC_TXBE_SIZE, DMA_BIDIRECTIONAL);
  1369. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  1370. if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
  1371. flush_dcache_range((ulong)bp,
  1372. (ulong)bp + MPSC_TXBE_SIZE);
  1373. #endif
  1374. mpsc_setup_tx_desc(pi, i, 0);
  1375. pi->txr_head = (pi->txr_head + 1) & (MPSC_TXR_ENTRIES - 1);
  1376. mpsc_sdma_start_tx(pi);
  1377. while (mpsc_sdma_tx_active(pi))
  1378. udelay(100);
  1379. pi->txr_tail = (pi->txr_tail + 1) & (MPSC_TXR_ENTRIES - 1);
  1380. }
  1381. return;
  1382. }
  1383. static int __init
  1384. mpsc_console_setup(struct console *co, char *options)
  1385. {
  1386. struct mpsc_port_info *pi;
  1387. int baud, bits, parity, flow;
  1388. pr_debug("mpsc_console_setup[%d]: options: %s\n", co->index, options);
  1389. if (co->index >= MPSC_NUM_CTLRS)
  1390. co->index = 0;
  1391. pi = &mpsc_ports[co->index];
  1392. baud = pi->default_baud;
  1393. bits = pi->default_bits;
  1394. parity = pi->default_parity;
  1395. flow = pi->default_flow;
  1396. if (!pi->port.ops)
  1397. return -ENODEV;
  1398. spin_lock_init(&pi->port.lock); /* Temporary fix--copied from 8250.c */
  1399. if (options)
  1400. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1401. return uart_set_options(&pi->port, co, baud, parity, bits, flow);
  1402. }
  1403. static struct console mpsc_console = {
  1404. .name = MPSC_DEV_NAME,
  1405. .write = mpsc_console_write,
  1406. .device = uart_console_device,
  1407. .setup = mpsc_console_setup,
  1408. .flags = CON_PRINTBUFFER,
  1409. .index = -1,
  1410. .data = &mpsc_reg,
  1411. };
  1412. static int __init
  1413. mpsc_late_console_init(void)
  1414. {
  1415. pr_debug("mpsc_late_console_init: Enter\n");
  1416. if (!(mpsc_console.flags & CON_ENABLED))
  1417. register_console(&mpsc_console);
  1418. return 0;
  1419. }
  1420. late_initcall(mpsc_late_console_init);
  1421. #define MPSC_CONSOLE &mpsc_console
  1422. #else
  1423. #define MPSC_CONSOLE NULL
  1424. #endif
  1425. /*
  1426. ******************************************************************************
  1427. *
  1428. * Dummy Platform Driver to extract & map shared register regions
  1429. *
  1430. ******************************************************************************
  1431. */
  1432. static void
  1433. mpsc_resource_err(char *s)
  1434. {
  1435. printk(KERN_WARNING "MPSC: Platform device resource error in %s\n", s);
  1436. return;
  1437. }
  1438. static int
  1439. mpsc_shared_map_regs(struct platform_device *pd)
  1440. {
  1441. struct resource *r;
  1442. if ((r = platform_get_resource(pd, IORESOURCE_MEM,
  1443. MPSC_ROUTING_BASE_ORDER)) && request_mem_region(r->start,
  1444. MPSC_ROUTING_REG_BLOCK_SIZE, "mpsc_routing_regs")) {
  1445. mpsc_shared_regs.mpsc_routing_base = ioremap(r->start,
  1446. MPSC_ROUTING_REG_BLOCK_SIZE);
  1447. mpsc_shared_regs.mpsc_routing_base_p = r->start;
  1448. }
  1449. else {
  1450. mpsc_resource_err("MPSC routing base");
  1451. return -ENOMEM;
  1452. }
  1453. if ((r = platform_get_resource(pd, IORESOURCE_MEM,
  1454. MPSC_SDMA_INTR_BASE_ORDER)) && request_mem_region(r->start,
  1455. MPSC_SDMA_INTR_REG_BLOCK_SIZE, "sdma_intr_regs")) {
  1456. mpsc_shared_regs.sdma_intr_base = ioremap(r->start,
  1457. MPSC_SDMA_INTR_REG_BLOCK_SIZE);
  1458. mpsc_shared_regs.sdma_intr_base_p = r->start;
  1459. }
  1460. else {
  1461. iounmap(mpsc_shared_regs.mpsc_routing_base);
  1462. release_mem_region(mpsc_shared_regs.mpsc_routing_base_p,
  1463. MPSC_ROUTING_REG_BLOCK_SIZE);
  1464. mpsc_resource_err("SDMA intr base");
  1465. return -ENOMEM;
  1466. }
  1467. return 0;
  1468. }
  1469. static void
  1470. mpsc_shared_unmap_regs(void)
  1471. {
  1472. if (!mpsc_shared_regs.mpsc_routing_base) {
  1473. iounmap(mpsc_shared_regs.mpsc_routing_base);
  1474. release_mem_region(mpsc_shared_regs.mpsc_routing_base_p,
  1475. MPSC_ROUTING_REG_BLOCK_SIZE);
  1476. }
  1477. if (!mpsc_shared_regs.sdma_intr_base) {
  1478. iounmap(mpsc_shared_regs.sdma_intr_base);
  1479. release_mem_region(mpsc_shared_regs.sdma_intr_base_p,
  1480. MPSC_SDMA_INTR_REG_BLOCK_SIZE);
  1481. }
  1482. mpsc_shared_regs.mpsc_routing_base = NULL;
  1483. mpsc_shared_regs.sdma_intr_base = NULL;
  1484. mpsc_shared_regs.mpsc_routing_base_p = 0;
  1485. mpsc_shared_regs.sdma_intr_base_p = 0;
  1486. return;
  1487. }
  1488. static int
  1489. mpsc_shared_drv_probe(struct platform_device *dev)
  1490. {
  1491. struct mpsc_shared_pdata *pdata;
  1492. int rc = -ENODEV;
  1493. if (dev->id == 0) {
  1494. if (!(rc = mpsc_shared_map_regs(dev))) {
  1495. pdata = (struct mpsc_shared_pdata *)dev->dev.platform_data;
  1496. mpsc_shared_regs.MPSC_MRR_m = pdata->mrr_val;
  1497. mpsc_shared_regs.MPSC_RCRR_m= pdata->rcrr_val;
  1498. mpsc_shared_regs.MPSC_TCRR_m= pdata->tcrr_val;
  1499. mpsc_shared_regs.SDMA_INTR_CAUSE_m =
  1500. pdata->intr_cause_val;
  1501. mpsc_shared_regs.SDMA_INTR_MASK_m =
  1502. pdata->intr_mask_val;
  1503. rc = 0;
  1504. }
  1505. }
  1506. return rc;
  1507. }
  1508. static int
  1509. mpsc_shared_drv_remove(struct platform_device *dev)
  1510. {
  1511. int rc = -ENODEV;
  1512. if (dev->id == 0) {
  1513. mpsc_shared_unmap_regs();
  1514. mpsc_shared_regs.MPSC_MRR_m = 0;
  1515. mpsc_shared_regs.MPSC_RCRR_m = 0;
  1516. mpsc_shared_regs.MPSC_TCRR_m = 0;
  1517. mpsc_shared_regs.SDMA_INTR_CAUSE_m = 0;
  1518. mpsc_shared_regs.SDMA_INTR_MASK_m = 0;
  1519. rc = 0;
  1520. }
  1521. return rc;
  1522. }
  1523. static struct platform_driver mpsc_shared_driver = {
  1524. .probe = mpsc_shared_drv_probe,
  1525. .remove = mpsc_shared_drv_remove,
  1526. .driver = {
  1527. .name = MPSC_SHARED_NAME,
  1528. },
  1529. };
  1530. /*
  1531. ******************************************************************************
  1532. *
  1533. * Driver Interface Routines
  1534. *
  1535. ******************************************************************************
  1536. */
  1537. static struct uart_driver mpsc_reg = {
  1538. .owner = THIS_MODULE,
  1539. .driver_name = MPSC_DRIVER_NAME,
  1540. .devfs_name = MPSC_DEVFS_NAME,
  1541. .dev_name = MPSC_DEV_NAME,
  1542. .major = MPSC_MAJOR,
  1543. .minor = MPSC_MINOR_START,
  1544. .nr = MPSC_NUM_CTLRS,
  1545. .cons = MPSC_CONSOLE,
  1546. };
  1547. static int
  1548. mpsc_drv_map_regs(struct mpsc_port_info *pi, struct platform_device *pd)
  1549. {
  1550. struct resource *r;
  1551. if ((r = platform_get_resource(pd, IORESOURCE_MEM, MPSC_BASE_ORDER)) &&
  1552. request_mem_region(r->start, MPSC_REG_BLOCK_SIZE, "mpsc_regs")){
  1553. pi->mpsc_base = ioremap(r->start, MPSC_REG_BLOCK_SIZE);
  1554. pi->mpsc_base_p = r->start;
  1555. }
  1556. else {
  1557. mpsc_resource_err("MPSC base");
  1558. return -ENOMEM;
  1559. }
  1560. if ((r = platform_get_resource(pd, IORESOURCE_MEM,
  1561. MPSC_SDMA_BASE_ORDER)) && request_mem_region(r->start,
  1562. MPSC_SDMA_REG_BLOCK_SIZE, "sdma_regs")) {
  1563. pi->sdma_base = ioremap(r->start,MPSC_SDMA_REG_BLOCK_SIZE);
  1564. pi->sdma_base_p = r->start;
  1565. }
  1566. else {
  1567. mpsc_resource_err("SDMA base");
  1568. return -ENOMEM;
  1569. }
  1570. if ((r = platform_get_resource(pd,IORESOURCE_MEM,MPSC_BRG_BASE_ORDER))
  1571. && request_mem_region(r->start, MPSC_BRG_REG_BLOCK_SIZE,
  1572. "brg_regs")) {
  1573. pi->brg_base = ioremap(r->start, MPSC_BRG_REG_BLOCK_SIZE);
  1574. pi->brg_base_p = r->start;
  1575. }
  1576. else {
  1577. mpsc_resource_err("BRG base");
  1578. return -ENOMEM;
  1579. }
  1580. return 0;
  1581. }
  1582. static void
  1583. mpsc_drv_unmap_regs(struct mpsc_port_info *pi)
  1584. {
  1585. if (!pi->mpsc_base) {
  1586. iounmap(pi->mpsc_base);
  1587. release_mem_region(pi->mpsc_base_p, MPSC_REG_BLOCK_SIZE);
  1588. }
  1589. if (!pi->sdma_base) {
  1590. iounmap(pi->sdma_base);
  1591. release_mem_region(pi->sdma_base_p, MPSC_SDMA_REG_BLOCK_SIZE);
  1592. }
  1593. if (!pi->brg_base) {
  1594. iounmap(pi->brg_base);
  1595. release_mem_region(pi->brg_base_p, MPSC_BRG_REG_BLOCK_SIZE);
  1596. }
  1597. pi->mpsc_base = NULL;
  1598. pi->sdma_base = NULL;
  1599. pi->brg_base = NULL;
  1600. pi->mpsc_base_p = 0;
  1601. pi->sdma_base_p = 0;
  1602. pi->brg_base_p = 0;
  1603. return;
  1604. }
  1605. static void
  1606. mpsc_drv_get_platform_data(struct mpsc_port_info *pi,
  1607. struct platform_device *pd, int num)
  1608. {
  1609. struct mpsc_pdata *pdata;
  1610. pdata = (struct mpsc_pdata *)pd->dev.platform_data;
  1611. pi->port.uartclk = pdata->brg_clk_freq;
  1612. pi->port.iotype = UPIO_MEM;
  1613. pi->port.line = num;
  1614. pi->port.type = PORT_MPSC;
  1615. pi->port.fifosize = MPSC_TXBE_SIZE;
  1616. pi->port.membase = pi->mpsc_base;
  1617. pi->port.mapbase = (ulong)pi->mpsc_base;
  1618. pi->port.ops = &mpsc_pops;
  1619. pi->mirror_regs = pdata->mirror_regs;
  1620. pi->cache_mgmt = pdata->cache_mgmt;
  1621. pi->brg_can_tune = pdata->brg_can_tune;
  1622. pi->brg_clk_src = pdata->brg_clk_src;
  1623. pi->mpsc_max_idle = pdata->max_idle;
  1624. pi->default_baud = pdata->default_baud;
  1625. pi->default_bits = pdata->default_bits;
  1626. pi->default_parity = pdata->default_parity;
  1627. pi->default_flow = pdata->default_flow;
  1628. /* Initial values of mirrored regs */
  1629. pi->MPSC_CHR_1_m = pdata->chr_1_val;
  1630. pi->MPSC_CHR_2_m = pdata->chr_2_val;
  1631. pi->MPSC_CHR_10_m = pdata->chr_10_val;
  1632. pi->MPSC_MPCR_m = pdata->mpcr_val;
  1633. pi->BRG_BCR_m = pdata->bcr_val;
  1634. pi->shared_regs = &mpsc_shared_regs;
  1635. pi->port.irq = platform_get_irq(pd, 0);
  1636. return;
  1637. }
  1638. static int
  1639. mpsc_drv_probe(struct platform_device *dev)
  1640. {
  1641. struct mpsc_port_info *pi;
  1642. int rc = -ENODEV;
  1643. pr_debug("mpsc_drv_probe: Adding MPSC %d\n", dev->id);
  1644. if (dev->id < MPSC_NUM_CTLRS) {
  1645. pi = &mpsc_ports[dev->id];
  1646. if (!(rc = mpsc_drv_map_regs(pi, dev))) {
  1647. mpsc_drv_get_platform_data(pi, dev, dev->id);
  1648. if (!(rc = mpsc_make_ready(pi)))
  1649. if (!(rc = uart_add_one_port(&mpsc_reg,
  1650. &pi->port)))
  1651. rc = 0;
  1652. else {
  1653. mpsc_release_port(
  1654. (struct uart_port *)pi);
  1655. mpsc_drv_unmap_regs(pi);
  1656. }
  1657. else
  1658. mpsc_drv_unmap_regs(pi);
  1659. }
  1660. }
  1661. return rc;
  1662. }
  1663. static int
  1664. mpsc_drv_remove(struct platform_device *dev)
  1665. {
  1666. pr_debug("mpsc_drv_exit: Removing MPSC %d\n", dev->id);
  1667. if (dev->id < MPSC_NUM_CTLRS) {
  1668. uart_remove_one_port(&mpsc_reg, &mpsc_ports[dev->id].port);
  1669. mpsc_release_port((struct uart_port *)&mpsc_ports[dev->id].port);
  1670. mpsc_drv_unmap_regs(&mpsc_ports[dev->id]);
  1671. return 0;
  1672. }
  1673. else
  1674. return -ENODEV;
  1675. }
  1676. static struct platform_driver mpsc_driver = {
  1677. .probe = mpsc_drv_probe,
  1678. .remove = mpsc_drv_remove,
  1679. .driver = {
  1680. .name = MPSC_CTLR_NAME,
  1681. },
  1682. };
  1683. static int __init
  1684. mpsc_drv_init(void)
  1685. {
  1686. int rc;
  1687. printk(KERN_INFO "Serial: MPSC driver $Revision: 1.00 $\n");
  1688. memset(mpsc_ports, 0, sizeof(mpsc_ports));
  1689. memset(&mpsc_shared_regs, 0, sizeof(mpsc_shared_regs));
  1690. if (!(rc = uart_register_driver(&mpsc_reg))) {
  1691. if (!(rc = platform_driver_register(&mpsc_shared_driver))) {
  1692. if ((rc = platform_driver_register(&mpsc_driver))) {
  1693. platform_driver_unregister(&mpsc_shared_driver);
  1694. uart_unregister_driver(&mpsc_reg);
  1695. }
  1696. }
  1697. else
  1698. uart_unregister_driver(&mpsc_reg);
  1699. }
  1700. return rc;
  1701. }
  1702. static void __exit
  1703. mpsc_drv_exit(void)
  1704. {
  1705. platform_driver_unregister(&mpsc_driver);
  1706. platform_driver_unregister(&mpsc_shared_driver);
  1707. uart_unregister_driver(&mpsc_reg);
  1708. memset(mpsc_ports, 0, sizeof(mpsc_ports));
  1709. memset(&mpsc_shared_regs, 0, sizeof(mpsc_shared_regs));
  1710. return;
  1711. }
  1712. module_init(mpsc_drv_init);
  1713. module_exit(mpsc_drv_exit);
  1714. MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
  1715. MODULE_DESCRIPTION("Generic Marvell MPSC serial/UART driver $Revision: 1.00 $");
  1716. MODULE_VERSION(MPSC_VERSION);
  1717. MODULE_LICENSE("GPL");
  1718. MODULE_ALIAS_CHARDEV_MAJOR(MPSC_MAJOR);